forked from intel/kernelflinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kf4aic.c
288 lines (241 loc) · 7.31 KB
/
kf4aic.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
/*
* 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 "vbmeta_ias.h"
#include "lib.h"
#include "ux.h"
#ifdef RPMB_STORAGE
#include "rpmb.h"
#include "rpmb_storage.h"
#endif
#ifdef USE_TRUSTY
#include "security.h"
#include "trusty_interface.h"
#include "security_interface.h"
#endif
#define SYSTEMD_BOOT_FILE L"loaderx64.efi"
#define TOS_IMAGE_FILE L"tos.img"
#define VBMETA_IAS_FILE L"vbmeta.ias"
#define ESP_PARTITION L"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;
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 (StriCmp(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;
StrCpy(abs_path, base_path);
StrCat(abs_path, L"\\");
StrCat(abs_path, file);
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
struct rot_data_t g_rot_data = {0};
EFI_STATUS load_file(EFI_HANDLE image_handle, CHAR16 *file, OUT VOID **image)
{
EFI_STATUS ret;
UINTN size = 0;
EFI_FILE_IO_INTERFACE *io;
EFI_LOADED_IMAGE *g_loaded_image = NULL;
CHAR16 *abs_path;
uefi_call_wrapper(BS->HandleProtocol, 3, image_handle, &LoadedImageProtocol, (void **)&g_loaded_image);
ret = handle_protocol(g_loaded_image->DeviceHandle, &FileSystemProtocol, (void **)&io);
if (EFI_ERROR(ret))
return ret;
abs_path = absolute_path(image_handle, file);
if (abs_path == NULL)
return EFI_NOT_FOUND;
ret = uefi_read_file(io, abs_path, image, &size);
FreePool(abs_path);
if (EFI_ERROR(ret)) {
error(L"read file failed: %s", file);
return ret;
}
debug(L"file size of '%s' = %d\n", file, size);
return ret;
}
EFI_STATUS load_and_start_tos(EFI_HANDLE image)
{
EFI_STATUS ret;
VOID *tosimage = NULL;
ret = load_file(image, TOS_IMAGE_FILE, &tosimage);
if (EFI_ERROR(ret))
return ret;
ret = start_trusty(tosimage);
debug(L"start_trusty return: %r(%x)\n", ret, ret);
return ret;
}
#endif
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
{
EFI_STATUS ret;
BOOLEAN verify_pass = FALSE;
CHAR16 *vbmeta_path = NULL;
#ifdef RPMB_STORAGE
UINT32 boot_state;
#endif
InitializeLib(image, _table);
vbmeta_path = absolute_path(image, VBMETA_IAS_FILE);
// if secureboot is disabled, return always successful and verify_pass always true
ret = verify_vbmeta_ias(ESP_PARTITION, vbmeta_path, &verify_pass);
if (vbmeta_path != NULL)
FreePool(vbmeta_path);
if (EFI_ERROR(ret) || !verify_pass)
return ret;
#ifdef RPMB_STORAGE
ret = set_device_security_info(NULL);
if (EFI_ERROR(ret))
error(L"Failed to init security info");
if (is_platform_secure_boot_enabled())
boot_state = BOOT_STATE_GREEN;
else {
boot_state = BOOT_STATE_YELLOW;
show_disable_secure_boot_warnning();
}
init_rot_data(boot_state, &g_rot_data);
debug(L"teedata region init...\n");
ret = rpmb_storage_init();
if (EFI_ERROR(ret))
error(L"Failed to init teedata region");
debug(L"teedata region init ret = %X\n", ret);
ret = rpmb_key_init();
if (EFI_ERROR(ret))
error(L"teedata region init failure for osloader.\n");
#endif
#ifdef USE_TRUSTY
debug(L"TRUSTY enabled...\n");
ret = load_and_start_tos(image);
if (EFI_ERROR(ret))
return ret;
#endif
ret = start_systemd_boot(image);
return ret;
}