forked from intel/kernelflinger
-
Notifications
You must be signed in to change notification settings - Fork 30
/
kf4cic.c
executable file
·318 lines (268 loc) · 7.47 KB
/
kf4cic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2019, Intel Corporation
* All rights reserved.
*
* Author: Zhou, Jianfeng <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <efi.h>
#include <efiapi.h>
#include <efilib.h>
#include "log.h"
#include "protocol.h"
#include "uefi_utils.h"
#include "lib.h"
#include "ux.h"
#include "security.h"
#include "security_interface.h"
#ifdef USE_TRUSTY
#include "trusty_interface.h"
#include "trusty_common.h"
#endif
#ifdef USE_TPM
#include "tpm2_security.h"
#endif
#define SYSTEMD_BOOT_FILE L"loaderx64.efi"
EFI_STATUS load_and_start_efi(EFI_HANDLE image_handle, CHAR16 *efi_file)
{
EFI_GUID gEfiLoadedImageProtocolGuid = LOADED_IMAGE_PROTOCOL;
EFI_STATUS Status = EFI_SUCCESS;
EFI_HANDLE efi_handle = NULL;
EFI_DEVICE_PATH *device_path;
EFI_LOADED_IMAGE *image_info;
EFI_LOADED_IMAGE *g_loaded_image = NULL;
UINTN exit_data_size = 0;
uefi_call_wrapper(BS->HandleProtocol, 3, image_handle, &LoadedImageProtocol, (void **)&g_loaded_image);
device_path = FileDevicePath(g_loaded_image->DeviceHandle, efi_file);
Status = BS->LoadImage(
FALSE,
image_handle,
device_path,
(VOID *) NULL,
0,
&efi_handle);
if (Status != EFI_SUCCESS && Status != EFI_SECURITY_VIOLATION) {
error(L"Could not load the image '%s'", efi_file);
return Status;
}
debug(L"Load '%s' success", efi_file);
Status = BS->OpenProtocol(
efi_handle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &image_info,
image_handle,
(VOID *) NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR(Status))
debug(L"ImageSize = %d", image_info->ImageSize);
Status = BS->StartImage(efi_handle, &exit_data_size, (CHAR16 **) NULL);
if (Status != EFI_SUCCESS) {
error(L"Could not start image");
error(L"Exit data size: %d", exit_data_size);
}
return Status;
}
CHAR16 *get_base_path(EFI_HANDLE image_handle)
{
EFI_STATUS ret;
EFI_LOADED_IMAGE *g_loaded_image = NULL;
CHAR16 *self_path = NULL;
ret = uefi_call_wrapper(BS->HandleProtocol, 3, image_handle, &LoadedImageProtocol, (void **)&g_loaded_image);
if (EFI_ERROR(ret)) {
error(L"OpenProtocol LoadedImageProtocol failed");
return NULL;
}
self_path = ((FILEPATH_DEVICE_PATH *)(g_loaded_image->FilePath))->PathName;
return self_path;
}
CHAR16 *absolute_path(EFI_HANDLE image_handle, CHAR16 *file)
{
CHAR16 *base_path = NULL;
CHAR16 *abs_path = NULL;
UINTN len;
EFI_STATUS ret;
if (file[0] == L'\\')
return StrDuplicate(file);
base_path = get_base_path(image_handle);
if (base_path == NULL)
return NULL;
len = StrLen(base_path);
if (len > 4) {
if (StrcaseCmp(base_path + len - 4, L".EFI") == 0) {
UINTN i = len - 4;
while (i > 0 && base_path[i] != L'\\')
i--;
base_path[i] = 0;
}
}
len = StrLen(base_path);
if (len == 0)
return StrDuplicate(file);
len = StrLen(base_path) + StrLen(file) + 2;
abs_path = (CHAR16 *)AllocatePool(len * sizeof(CHAR16));
if (abs_path == NULL)
return NULL;
ret = strcpy16_s(abs_path, len, base_path);
if (EFI_ERROR(ret))
return NULL;
ret = strcat16_s(abs_path, len, L"\\");
if (EFI_ERROR(ret))
return NULL;
ret = strcat16_s(abs_path, len, file);
if (EFI_ERROR(ret))
return NULL;
return abs_path;
}
static VOID show_disable_secure_boot_warnning()
{
enum boot_target bt = NORMAL_BOOT;
#ifdef USE_UI
bt = ux_prompt_user(SECURE_BOOT_CODE, FALSE, BOOT_STATE_YELLOW, NULL, 0);
#else
debug(L"Secure boot is disabled");
#endif
if (bt != NORMAL_BOOT)
halt_system();
}
EFI_STATUS start_systemd_boot(EFI_HANDLE image_handle)
{
EFI_STATUS ret;
CHAR16 *boot_path = NULL;
boot_path = absolute_path(image_handle, SYSTEMD_BOOT_FILE);
if (boot_path == NULL)
return EFI_NOT_STARTED;
debug(L"load and start '%s'...", boot_path);
ret = load_and_start_efi(image_handle, boot_path);
FreePool(boot_path);
return ret;
}
#ifdef USE_TRUSTY
static EFI_STATUS load_and_start_tos(void)
{
EFI_STATUS ret;
VOID *tosimage = NULL;
debug(L"loading trusty");
ret = load_tos_image(&tosimage);
if (EFI_ERROR(ret)) {
efi_perror(ret, L"Load tos image failed");
return ret;
}
debug(L"start trusty");
ret = start_trusty(tosimage);
if (EFI_ERROR(ret)) {
efi_perror(ret, L"Unable to start trusty;");
return ret;
}
return ret;
}
static EFI_STATUS update_rollback_indexes()
{
EFI_STATUS ret;
AvbOps *ops;
AvbSlotVerifyResult verify_result;
AvbSlotVerifyData *slot_data = NULL;
UINT8 boot_state = BOOT_STATE_GREEN;
const char *requested_partitions[] = {"tos", NULL};
ops = avb_init();
if (!ops) {
error(L"Failed to init avb");
return EFI_OUT_OF_RESOURCES;
}
verify_result = avb_slot_verify(ops,
requested_partitions,
"",
AVB_SLOT_VERIFY_FLAGS_NONE,
AVB_HASHTREE_ERROR_MODE_RESTART,
&slot_data);
ret = get_avb_result(slot_data,
FALSE,
verify_result,
&boot_state);
if (EFI_ERROR(ret)) {
error(L"Failed to get avb result for tos");
return ret;
}
avb_update_stored_rollback_indexes_for_slot(ops, slot_data);
return ret;
}
#endif
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
{
EFI_STATUS ret;
UINT32 boot_state;
InitializeLib(image, _table);
/* Set device state as locked due to there is no fastboot
* implement in CIC host oS side to support whole devcie lock/unlock */
if (device_is_unlocked()) {
ret = set_current_state(LOCKED);
if (EFI_ERROR(ret)) {
error(L"Failed to set device state");
return ret;
}
}
if (is_platform_secure_boot_enabled())
boot_state = BOOT_STATE_GREEN;
else {
boot_state = BOOT_STATE_YELLOW;
show_disable_secure_boot_warnning();
}
#ifdef USE_TPM
if (is_platform_secure_boot_enabled()) {
ret = tpm2_init();
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND) {
error(L"Failed to init TPM");
return ret;
}
}
#endif
ret = set_device_security_info(NULL);
if (EFI_ERROR(ret)) {
error(L"Failed to init security info");
return ret;
}
init_rot_data(boot_state);
init_attestation_ids();
#ifdef USE_TRUSTY
debug(L"TRUSTY enabled...\n");
ret = load_and_start_tos();
if (EFI_ERROR(ret))
return ret;
if (boot_state == BOOT_STATE_GREEN) {
ret = update_rollback_indexes();
if (EFI_ERROR(ret)) {
error(L"Failed to update rollback indexes.\n");
return ret;
}
}
#endif
#ifdef USE_TPM
// Make sure the TPM2 is ended
tpm2_end();
#endif
ret = start_systemd_boot(image);
return ret;
}