blob: fe1ee198e2c79f3ad0e6d8b7233eae4de6020654 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafcb149c62016-03-04 01:09:58 +01002/*
3 * EFI image loader
4 *
5 * based partly on wine code
6 *
7 * Copyright (c) 2016 Alexander Graf
Alexander Grafcb149c62016-03-04 01:09:58 +01008 */
9
Heinrich Schuchardt24586052020-08-25 17:51:20 +000010#define LOG_CATEGORY LOGC_EFI
11
Alexander Grafcb149c62016-03-04 01:09:58 +010012#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010014#include <efi_loader.h>
Heinrich Schuchardt24586052020-08-25 17:51:20 +000015#include <log.h>
AKASHI Takahiro4540dab2020-04-14 11:51:44 +090016#include <malloc.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010017#include <pe.h>
AKASHI Takahiro4540dab2020-04-14 11:51:44 +090018#include <sort.h>
Heinrich Schuchardtd7ca3ce2020-05-07 17:57:43 +020019#include <crypto/pkcs7_parser.h>
Patrick Wildt6f146152020-05-07 02:17:14 +020020#include <linux/err.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010021
Rob Clark9975fe92017-09-13 18:05:38 -040022const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020023const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
24const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID;
25const efi_guid_t efi_guid_loaded_image_device_path =
26 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
Rob Clark2a920802017-09-13 18:05:34 -040027const efi_guid_t efi_simple_file_system_protocol_guid =
28 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
29const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Grafcb149c62016-03-04 01:09:58 +010030
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070031static int machines[] = {
Alexander Grafb59f6972018-06-18 17:22:57 +020032#if defined(__aarch64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070033 IMAGE_FILE_MACHINE_ARM64,
Alexander Grafb59f6972018-06-18 17:22:57 +020034#elif defined(__arm__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070035 IMAGE_FILE_MACHINE_ARM,
36 IMAGE_FILE_MACHINE_THUMB,
37 IMAGE_FILE_MACHINE_ARMNT,
38#endif
39
Alexander Grafb59f6972018-06-18 17:22:57 +020040#if defined(__x86_64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070041 IMAGE_FILE_MACHINE_AMD64,
Alexander Grafb59f6972018-06-18 17:22:57 +020042#elif defined(__i386__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070043 IMAGE_FILE_MACHINE_I386,
44#endif
45
Alexander Grafb59f6972018-06-18 17:22:57 +020046#if defined(__riscv) && (__riscv_xlen == 32)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070047 IMAGE_FILE_MACHINE_RISCV32,
48#endif
49
Alexander Grafb59f6972018-06-18 17:22:57 +020050#if defined(__riscv) && (__riscv_xlen == 64)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070051 IMAGE_FILE_MACHINE_RISCV64,
52#endif
53 0 };
54
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010055/**
56 * efi_print_image_info() - print information about a loaded image
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020057 *
58 * If the program counter is located within the image the offset to the base
59 * address is shown.
60 *
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020061 * @obj: EFI object
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020062 * @image: loaded image
63 * @pc: program counter (use NULL to suppress offset output)
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010064 * Return: status code
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020065 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020066static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
67 struct efi_loaded_image *image,
68 void *pc)
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020069{
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020070 printf("UEFI image");
71 printf(" [0x%p:0x%p]",
AKASHI Takahiro8458bf62018-10-11 04:09:58 -070072 image->image_base, image->image_base + image->image_size - 1);
73 if (pc && pc >= image->image_base &&
74 pc < image->image_base + image->image_size)
75 printf(" pc=0x%zx", pc - image->image_base);
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020076 if (image->file_path)
77 printf(" '%pD'", image->file_path);
78 printf("\n");
79 return EFI_SUCCESS;
80}
81
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010082/**
83 * efi_print_image_infos() - print information about all loaded images
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020084 *
85 * @pc: program counter (use NULL to suppress offset output)
86 */
87void efi_print_image_infos(void *pc)
88{
89 struct efi_object *efiobj;
90 struct efi_handler *handler;
91
92 list_for_each_entry(efiobj, &efi_obj_list, link) {
93 list_for_each_entry(handler, &efiobj->protocols, link) {
94 if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
95 efi_print_image_info(
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020096 (struct efi_loaded_image_obj *)efiobj,
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020097 handler->protocol_interface, pc);
98 }
99 }
100 }
101}
102
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100103/**
104 * efi_loader_relocate() - relocate UEFI binary
105 *
106 * @rel: pointer to the relocation table
107 * @rel_size: size of the relocation table in bytes
108 * @efi_reloc: actual load address of the image
109 * @pref_address: preferred load address of the image
110 * Return: status code
111 */
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200112static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700113 unsigned long rel_size, void *efi_reloc,
114 unsigned long pref_address)
Alexander Grafcb149c62016-03-04 01:09:58 +0100115{
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700116 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Grafcb149c62016-03-04 01:09:58 +0100117 const IMAGE_BASE_RELOCATION *end;
118 int i;
119
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700120 if (delta == 0)
121 return EFI_SUCCESS;
122
Alexander Grafcb149c62016-03-04 01:09:58 +0100123 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
Heinrich Schuchardt997fc122019-02-16 15:36:33 +0100124 while (rel < end && rel->SizeOfBlock) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100125 const uint16_t *relocs = (const uint16_t *)(rel + 1);
126 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
127 while (i--) {
Alexander Grafb1237c62016-08-18 23:45:18 +0200128 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Grafcb149c62016-03-04 01:09:58 +0100129 rel->VirtualAddress;
130 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Grafcb149c62016-03-04 01:09:58 +0100131 uint64_t *x64 = efi_reloc + offset;
132 uint32_t *x32 = efi_reloc + offset;
133 uint16_t *x16 = efi_reloc + offset;
134
135 switch (type) {
136 case IMAGE_REL_BASED_ABSOLUTE:
137 break;
138 case IMAGE_REL_BASED_HIGH:
139 *x16 += ((uint32_t)delta) >> 16;
140 break;
141 case IMAGE_REL_BASED_LOW:
142 *x16 += (uint16_t)delta;
143 break;
144 case IMAGE_REL_BASED_HIGHLOW:
145 *x32 += (uint32_t)delta;
146 break;
147 case IMAGE_REL_BASED_DIR64:
148 *x64 += (uint64_t)delta;
149 break;
Alexander Grafde452c02018-06-05 19:20:32 +0200150#ifdef __riscv
151 case IMAGE_REL_BASED_RISCV_HI20:
152 *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
153 (*x32 & 0x00000fff);
154 break;
155 case IMAGE_REL_BASED_RISCV_LOW12I:
156 case IMAGE_REL_BASED_RISCV_LOW12S:
157 /* We know that we're 4k aligned */
158 if (delta & 0xfff) {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000159 log_err("Unsupported reloc offset\n");
Alexander Grafde452c02018-06-05 19:20:32 +0200160 return EFI_LOAD_ERROR;
161 }
162 break;
163#endif
Alexander Grafcb149c62016-03-04 01:09:58 +0100164 default:
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000165 log_err("Unknown Relocation off %x type %x\n",
166 offset, type);
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200167 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100168 }
169 relocs++;
170 }
171 rel = (const IMAGE_BASE_RELOCATION *)relocs;
172 }
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200173 return EFI_SUCCESS;
Alexander Grafcb149c62016-03-04 01:09:58 +0100174}
175
176void __weak invalidate_icache_all(void)
177{
178 /* If the system doesn't support icache_all flush, cross our fingers */
179}
180
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100181/**
182 * efi_set_code_and_data_type() - determine the memory types to be used for code
183 * and data.
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100184 *
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100185 * @loaded_image_info: image descriptor
186 * @image_type: field Subsystem of the optional header for
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100187 * Windows specific field
188 */
189static void efi_set_code_and_data_type(
190 struct efi_loaded_image *loaded_image_info,
191 uint16_t image_type)
192{
193 switch (image_type) {
194 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
195 loaded_image_info->image_code_type = EFI_LOADER_CODE;
196 loaded_image_info->image_data_type = EFI_LOADER_DATA;
197 break;
198 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
199 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
200 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
201 break;
202 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt268ec6e2018-01-31 18:45:35 +0000203 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100204 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
205 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
206 break;
207 default:
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000208 log_err("invalid image type: %u\n", image_type);
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100209 /* Let's assume it is an application */
210 loaded_image_info->image_code_type = EFI_LOADER_CODE;
211 loaded_image_info->image_data_type = EFI_LOADER_DATA;
212 break;
213 }
214}
215
Masahisa Kojimaf6081a82021-05-14 09:53:36 +0900216/**
217 * efi_image_region_add() - add an entry of region
218 * @regs: Pointer to array of regions
219 * @start: Start address of region (included)
220 * @end: End address of region (excluded)
221 * @nocheck: flag against overlapped regions
222 *
223 * Take one entry of region [@start, @end[ and insert it into the list.
224 *
225 * * If @nocheck is false, the list will be sorted ascending by address.
226 * Overlapping entries will not be allowed.
227 *
228 * * If @nocheck is true, the list will be sorted ascending by sequence
229 * of adding the entries. Overlapping is allowed.
230 *
231 * Return: status code
232 */
233efi_status_t efi_image_region_add(struct efi_image_regions *regs,
234 const void *start, const void *end,
235 int nocheck)
236{
237 struct image_region *reg;
238 int i, j;
239
240 if (regs->num >= regs->max) {
241 EFI_PRINT("%s: no more room for regions\n", __func__);
242 return EFI_OUT_OF_RESOURCES;
243 }
244
245 if (end < start)
246 return EFI_INVALID_PARAMETER;
247
248 for (i = 0; i < regs->num; i++) {
249 reg = &regs->reg[i];
250 if (nocheck)
251 continue;
252
253 /* new data after registered region */
254 if (start >= reg->data + reg->size)
255 continue;
256
257 /* new data preceding registered region */
258 if (end <= reg->data) {
259 for (j = regs->num - 1; j >= i; j--)
260 memcpy(&regs->reg[j + 1], &regs->reg[j],
261 sizeof(*reg));
262 break;
263 }
264
265 /* new data overlapping registered region */
266 EFI_PRINT("%s: new region already part of another\n", __func__);
267 return EFI_INVALID_PARAMETER;
268 }
269
270 reg = &regs->reg[i];
271 reg->data = start;
272 reg->size = end - start;
273 regs->num++;
274
275 return EFI_SUCCESS;
276}
277
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900278/**
Heinrich Schuchardt13f62d92020-05-30 06:44:48 +0200279 * cmp_pe_section() - compare virtual addresses of two PE image sections
280 * @arg1: pointer to pointer to first section header
281 * @arg2: pointer to pointer to second section header
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900282 *
Heinrich Schuchardt13f62d92020-05-30 06:44:48 +0200283 * Compare the virtual addresses of two sections of an portable executable.
284 * The arguments are defined as const void * to allow usage with qsort().
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900285 *
Heinrich Schuchardt13f62d92020-05-30 06:44:48 +0200286 * Return: -1 if the virtual address of arg1 is less than that of arg2,
287 * 0 if the virtual addresses are equal, 1 if the virtual address
288 * of arg1 is greater than that of arg2.
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900289 */
290static int cmp_pe_section(const void *arg1, const void *arg2)
291{
292 const IMAGE_SECTION_HEADER *section1, *section2;
293
294 section1 = *((const IMAGE_SECTION_HEADER **)arg1);
295 section2 = *((const IMAGE_SECTION_HEADER **)arg2);
296
297 if (section1->VirtualAddress < section2->VirtualAddress)
298 return -1;
299 else if (section1->VirtualAddress == section2->VirtualAddress)
300 return 0;
301 else
302 return 1;
303}
304
305/**
Heinrich Schuchardt4afceb42020-05-30 05:48:08 +0200306 * efi_image_parse() - parse a PE image
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900307 * @efi: Pointer to image
308 * @len: Size of @efi
309 * @regp: Pointer to a list of regions
310 * @auth: Pointer to a pointer to authentication data in PE
311 * @auth_len: Size of @auth
312 *
313 * Parse image binary in PE32(+) format, assuming that sanity of PE image
314 * has been checked by a caller.
315 * On success, an address of authentication data in @efi and its size will
316 * be returned in @auth and @auth_len, respectively.
317 *
318 * Return: true on success, false on error
319 */
320bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
321 WIN_CERTIFICATE **auth, size_t *auth_len)
322{
323 struct efi_image_regions *regs;
324 IMAGE_DOS_HEADER *dos;
325 IMAGE_NT_HEADERS32 *nt;
326 IMAGE_SECTION_HEADER *sections, **sorted;
327 int num_regions, num_sections, i;
328 int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY;
329 u32 align, size, authsz, authoff;
330 size_t bytes_hashed;
331
332 dos = (void *)efi;
333 nt = (void *)(efi + dos->e_lfanew);
AKASHI Takahiroeb537fd2020-07-08 14:01:53 +0900334 authoff = 0;
335 authsz = 0;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900336
337 /*
338 * Count maximum number of regions to be digested.
339 * We don't have to have an exact number here.
340 * See efi_image_region_add()'s in parsing below.
341 */
342 num_regions = 3; /* for header */
343 num_regions += nt->FileHeader.NumberOfSections;
344 num_regions++; /* for extra */
345
346 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions,
347 1);
348 if (!regs)
349 goto err;
350 regs->max = num_regions;
351
352 /*
353 * Collect data regions for hash calculation
354 * 1. File headers
355 */
356 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
357 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
358 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
359
360 /* Skip CheckSum */
361 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
362 if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
363 efi_image_region_add(regs,
AKASHI Takahiro52d7bfe2020-05-08 14:51:59 +0900364 &opt->Subsystem,
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900365 efi + opt->SizeOfHeaders, 0);
366 } else {
367 /* Skip Certificates Table */
368 efi_image_region_add(regs,
AKASHI Takahiro52d7bfe2020-05-08 14:51:59 +0900369 &opt->Subsystem,
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900370 &opt->DataDirectory[ctidx], 0);
371 efi_image_region_add(regs,
372 &opt->DataDirectory[ctidx] + 1,
373 efi + opt->SizeOfHeaders, 0);
AKASHI Takahiroeb537fd2020-07-08 14:01:53 +0900374
375 authoff = opt->DataDirectory[ctidx].VirtualAddress;
376 authsz = opt->DataDirectory[ctidx].Size;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900377 }
378
379 bytes_hashed = opt->SizeOfHeaders;
380 align = opt->FileAlignment;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900381 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
382 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
383
AKASHI Takahiroeb537fd2020-07-08 14:01:53 +0900384 /* Skip CheckSum */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900385 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
AKASHI Takahiroeb537fd2020-07-08 14:01:53 +0900386 if (nt->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
387 efi_image_region_add(regs,
388 &opt->Subsystem,
389 efi + opt->SizeOfHeaders, 0);
390 } else {
391 /* Skip Certificates Table */
392 efi_image_region_add(regs, &opt->Subsystem,
393 &opt->DataDirectory[ctidx], 0);
394 efi_image_region_add(regs,
395 &opt->DataDirectory[ctidx] + 1,
396 efi + opt->SizeOfHeaders, 0);
397
398 authoff = opt->DataDirectory[ctidx].VirtualAddress;
399 authsz = opt->DataDirectory[ctidx].Size;
400 }
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900401
402 bytes_hashed = opt->SizeOfHeaders;
403 align = opt->FileAlignment;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900404 } else {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900405 EFI_PRINT("%s: Invalid optional header magic %x\n", __func__,
406 nt->OptionalHeader.Magic);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900407 goto err;
408 }
409
410 /* 2. Sections */
411 num_sections = nt->FileHeader.NumberOfSections;
412 sections = (void *)((uint8_t *)&nt->OptionalHeader +
413 nt->FileHeader.SizeOfOptionalHeader);
414 sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections);
415 if (!sorted) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900416 EFI_PRINT("%s: Out of memory\n", __func__);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900417 goto err;
418 }
419
420 /*
421 * Make sure the section list is in ascending order.
422 */
423 for (i = 0; i < num_sections; i++)
424 sorted[i] = &sections[i];
425 qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section);
426
427 for (i = 0; i < num_sections; i++) {
428 if (!sorted[i]->SizeOfRawData)
429 continue;
430
431 size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1);
432 efi_image_region_add(regs, efi + sorted[i]->PointerToRawData,
433 efi + sorted[i]->PointerToRawData + size,
434 0);
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900435 EFI_PRINT("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
436 i, sorted[i]->Name,
437 sorted[i]->PointerToRawData,
438 sorted[i]->PointerToRawData + size,
439 sorted[i]->VirtualAddress,
440 sorted[i]->VirtualAddress
441 + sorted[i]->Misc.VirtualSize);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900442
443 bytes_hashed += size;
444 }
445 free(sorted);
446
447 /* 3. Extra data excluding Certificates Table */
448 if (bytes_hashed + authsz < len) {
Heinrich Schuchardt39a75f52020-07-07 07:23:44 +0200449 EFI_PRINT("extra data for hash: %zu\n",
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900450 len - (bytes_hashed + authsz));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900451 efi_image_region_add(regs, efi + bytes_hashed,
452 efi + len - authsz, 0);
453 }
454
455 /* Return Certificates Table */
456 if (authsz) {
457 if (len < authoff + authsz) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900458 EFI_PRINT("%s: Size for auth too large: %u >= %zu\n",
459 __func__, authsz, len - authoff);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900460 goto err;
461 }
462 if (authsz < sizeof(*auth)) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900463 EFI_PRINT("%s: Size for auth too small: %u < %zu\n",
464 __func__, authsz, sizeof(*auth));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900465 goto err;
466 }
467 *auth = efi + authoff;
468 *auth_len = authsz;
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900469 EFI_PRINT("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff,
470 authsz);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900471 } else {
472 *auth = NULL;
473 *auth_len = 0;
474 }
475
476 *regp = regs;
477
478 return true;
479
480err:
481 free(regs);
482
483 return false;
484}
485
Masahisa Kojimaf6081a82021-05-14 09:53:36 +0900486#ifdef CONFIG_EFI_SECURE_BOOT
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900487/**
Heinrich Schuchardt4afceb42020-05-30 05:48:08 +0200488 * efi_image_unsigned_authenticate() - authenticate unsigned image with
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900489 * SHA256 hash
490 * @regs: List of regions to be verified
491 *
492 * If an image is not signed, it doesn't have a signature. In this case,
493 * its message digest is calculated and it will be compared with one of
494 * hash values stored in signature databases.
495 *
496 * Return: true if authenticated, false if not
497 */
498static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs)
499{
500 struct efi_signature_store *db = NULL, *dbx = NULL;
501 bool ret = false;
502
503 dbx = efi_sigstore_parse_sigdb(L"dbx");
504 if (!dbx) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900505 EFI_PRINT("Getting signature database(dbx) failed\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900506 goto out;
507 }
508
509 db = efi_sigstore_parse_sigdb(L"db");
510 if (!db) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900511 EFI_PRINT("Getting signature database(db) failed\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900512 goto out;
513 }
514
515 /* try black-list first */
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900516 if (efi_signature_lookup_digest(regs, dbx)) {
517 EFI_PRINT("Image is not signed and its digest found in \"dbx\"\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900518 goto out;
519 }
520
521 /* try white-list */
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900522 if (efi_signature_lookup_digest(regs, db))
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900523 ret = true;
524 else
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900525 EFI_PRINT("Image is not signed and its digest not found in \"db\" or \"dbx\"\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900526
527out:
528 efi_sigstore_free(db);
529 efi_sigstore_free(dbx);
530
531 return ret;
532}
533
534/**
Heinrich Schuchardt4afceb42020-05-30 05:48:08 +0200535 * efi_image_authenticate() - verify a signature of signed image
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900536 * @efi: Pointer to image
537 * @efi_size: Size of @efi
538 *
539 * A signed image should have its signature stored in a table of its PE header.
540 * So if an image is signed and only if if its signature is verified using
541 * signature databases, an image is authenticated.
542 * If an image is not signed, its validity is checked by using
543 * efi_image_unsigned_authenticated().
544 * TODO:
545 * When AuditMode==0, if the image's signature is not found in
546 * the authorized database, or is found in the forbidden database,
547 * the image will not be started and instead, information about it
548 * will be placed in this table.
549 * When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created
550 * in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found
551 * in the certificate table of every image that is validated.
552 *
553 * Return: true if authenticated, false if not
554 */
555static bool efi_image_authenticate(void *efi, size_t efi_size)
556{
557 struct efi_image_regions *regs = NULL;
558 WIN_CERTIFICATE *wincerts = NULL, *wincert;
559 size_t wincerts_len;
560 struct pkcs7_message *msg = NULL;
561 struct efi_signature_store *db = NULL, *dbx = NULL;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900562 void *new_efi = NULL;
AKASHI Takahiro1a44b702020-07-08 14:01:52 +0900563 u8 *auth, *wincerts_end;
564 size_t new_efi_size, auth_size;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900565 bool ret = false;
566
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000567 EFI_PRINT("%s: Enter, %d\n", __func__, ret);
AKASHI Takahiro11bafb22020-07-08 14:01:56 +0900568
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900569 if (!efi_secure_boot_enabled())
570 return true;
571
572 /*
573 * Size must be 8-byte aligned and the trailing bytes must be
574 * zero'ed. Otherwise hash value may be incorrect.
575 */
576 if (efi_size & 0x7) {
577 new_efi_size = (efi_size + 0x7) & ~0x7ULL;
578 new_efi = calloc(new_efi_size, 1);
579 if (!new_efi)
580 return false;
581 memcpy(new_efi, efi, efi_size);
582 efi = new_efi;
583 efi_size = new_efi_size;
584 }
585
586 if (!efi_image_parse(efi, efi_size, &regs, &wincerts,
587 &wincerts_len)) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900588 EFI_PRINT("Parsing PE executable image failed\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900589 goto err;
590 }
591
592 if (!wincerts) {
593 /* The image is not signed */
594 ret = efi_image_unsigned_authenticate(regs);
595
596 goto err;
597 }
598
599 /*
600 * verify signature using db and dbx
601 */
602 db = efi_sigstore_parse_sigdb(L"db");
603 if (!db) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900604 EFI_PRINT("Getting signature database(db) failed\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900605 goto err;
606 }
607
608 dbx = efi_sigstore_parse_sigdb(L"dbx");
609 if (!dbx) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900610 EFI_PRINT("Getting signature database(dbx) failed\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900611 goto err;
612 }
613
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900614 if (efi_signature_lookup_digest(regs, dbx)) {
615 EFI_PRINT("Image's digest was found in \"dbx\"\n");
616 goto err;
617 }
618
AKASHI Takahiro11bafb22020-07-08 14:01:56 +0900619 /*
620 * go through WIN_CERTIFICATE list
621 * NOTE:
622 * We may have multiple signatures either as WIN_CERTIFICATE's
623 * in PE header, or as pkcs7 SignerInfo's in SignedData.
624 * So the verification policy here is:
625 * - Success if, at least, one of signatures is verified
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900626 * - unless signature is rejected explicitly with its digest.
AKASHI Takahiro11bafb22020-07-08 14:01:56 +0900627 */
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900628
AKASHI Takahiro1a44b702020-07-08 14:01:52 +0900629 for (wincert = wincerts, wincerts_end = (u8 *)wincerts + wincerts_len;
630 (u8 *)wincert < wincerts_end;
631 wincert = (WIN_CERTIFICATE *)
632 ((u8 *)wincert + ALIGN(wincert->dwLength, 8))) {
633 if ((u8 *)wincert + sizeof(*wincert) >= wincerts_end)
634 break;
635
636 if (wincert->dwLength <= sizeof(*wincert)) {
637 EFI_PRINT("dwLength too small: %u < %zu\n",
638 wincert->dwLength, sizeof(*wincert));
639 continue;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900640 }
AKASHI Takahiro1a44b702020-07-08 14:01:52 +0900641
642 EFI_PRINT("WIN_CERTIFICATE_TYPE: 0x%x\n",
643 wincert->wCertificateType);
644
645 auth = (u8 *)wincert + sizeof(*wincert);
646 auth_size = wincert->dwLength - sizeof(*wincert);
647 if (wincert->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
648 if (auth + sizeof(efi_guid_t) >= wincerts_end)
649 break;
650
651 if (auth_size <= sizeof(efi_guid_t)) {
652 EFI_PRINT("dwLength too small: %u < %zu\n",
653 wincert->dwLength, sizeof(*wincert));
654 continue;
655 }
656 if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) {
657 EFI_PRINT("Certificate type not supported: %pUl\n",
658 auth);
659 continue;
660 }
661
662 auth += sizeof(efi_guid_t);
663 auth_size -= sizeof(efi_guid_t);
664 } else if (wincert->wCertificateType
665 != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
666 EFI_PRINT("Certificate type not supported\n");
667 continue;
668 }
669
670 msg = pkcs7_parse_message(auth, auth_size);
Patrick Wildt6f146152020-05-07 02:17:14 +0200671 if (IS_ERR(msg)) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900672 EFI_PRINT("Parsing image's signature failed\n");
Patrick Wildt6f146152020-05-07 02:17:14 +0200673 msg = NULL;
AKASHI Takahiro1a44b702020-07-08 14:01:52 +0900674 continue;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900675 }
676
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900677 /*
678 * NOTE:
679 * UEFI specification defines two signature types possible
680 * in signature database:
681 * a. x509 certificate, where a signature in image is
682 * a message digest encrypted by RSA public key
683 * (EFI_CERT_X509_GUID)
684 * b. bare hash value of message digest
685 * (EFI_CERT_SHAxxx_GUID)
686 *
687 * efi_signature_verify() handles case (a), while
688 * efi_signature_lookup_digest() handles case (b).
689 *
690 * There is a third type:
691 * c. message digest of a certificate
692 * (EFI_CERT_X509_SHAAxxx_GUID)
693 * This type of signature is used only in revocation list
694 * (dbx) and handled as part of efi_signatgure_verify().
695 */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900696 /* try black-list first */
AKASHI Takahiro11bafb22020-07-08 14:01:56 +0900697 if (efi_signature_verify_one(regs, msg, dbx)) {
AKASHI Takahiro1b6c0852020-06-09 14:09:35 +0900698 EFI_PRINT("Signature was rejected by \"dbx\"\n");
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900699 continue;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900700 }
701
AKASHI Takahiro11bafb22020-07-08 14:01:56 +0900702 if (!efi_signature_check_signers(msg, dbx)) {
703 EFI_PRINT("Signer(s) in \"dbx\"\n");
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900704 continue;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900705 }
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900706
707 /* try white-list */
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900708 if (efi_signature_verify(regs, msg, db, dbx)) {
709 ret = true;
710 break;
711 }
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900712
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000713 EFI_PRINT("Signature was not verified by \"db\"\n");
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900714
AKASHI Takahiro52956e52020-08-14 14:39:23 +0900715 if (efi_signature_lookup_digest(regs, db)) {
716 ret = true;
717 break;
718 }
AKASHI Takahiro7926dfb2020-07-08 14:01:57 +0900719
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000720 EFI_PRINT("Image's digest was not found in \"db\" or \"dbx\"\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900721 }
722
723err:
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900724 efi_sigstore_free(db);
725 efi_sigstore_free(dbx);
726 pkcs7_free_message(msg);
727 free(regs);
728 free(new_efi);
729
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000730 EFI_PRINT("%s: Exit, %d\n", __func__, ret);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900731 return ret;
732}
733#else
734static bool efi_image_authenticate(void *efi, size_t efi_size)
735{
736 return true;
737}
738#endif /* CONFIG_EFI_SECURE_BOOT */
739
Heinrich Schuchardt5dad05a2021-01-12 12:40:32 +0100740
741/**
742 * efi_check_pe() - check if a memory buffer contains a PE-COFF image
743 *
744 * @buffer: buffer to check
745 * @size: size of buffer
746 * @nt_header: on return pointer to NT header of PE-COFF image
747 * Return: EFI_SUCCESS if the buffer contains a PE-COFF image
748 */
749efi_status_t efi_check_pe(void *buffer, size_t size, void **nt_header)
750{
751 IMAGE_DOS_HEADER *dos = buffer;
752 IMAGE_NT_HEADERS32 *nt;
753
754 if (size < sizeof(*dos))
755 return EFI_INVALID_PARAMETER;
756
757 /* Check for DOS magix */
758 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
759 return EFI_INVALID_PARAMETER;
760
761 /*
762 * Check if the image section header fits into the file. Knowing that at
763 * least one section header follows we only need to check for the length
764 * of the 64bit header which is longer than the 32bit header.
765 */
766 if (size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS32))
767 return EFI_INVALID_PARAMETER;
768 nt = (IMAGE_NT_HEADERS32 *)((u8 *)buffer + dos->e_lfanew);
769
770 /* Check for PE-COFF magic */
771 if (nt->Signature != IMAGE_NT_SIGNATURE)
772 return EFI_INVALID_PARAMETER;
773
774 if (nt_header)
775 *nt_header = nt;
776
777 return EFI_SUCCESS;
778}
779
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100780/**
781 * efi_load_pe() - relocate EFI binary
782 *
Alexander Grafcb149c62016-03-04 01:09:58 +0100783 * This function loads all sections from a PE binary into a newly reserved
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100784 * piece of memory. On success the entry point is returned as handle->entry.
785 *
786 * @handle: loaded image handle
787 * @efi: pointer to the EFI binary
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900788 * @efi_size: size of @efi binary
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100789 * @loaded_image_info: loaded image protocol
790 * Return: status code
Alexander Grafcb149c62016-03-04 01:09:58 +0100791 */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900792efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
793 void *efi, size_t efi_size,
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100794 struct efi_loaded_image *loaded_image_info)
Alexander Grafcb149c62016-03-04 01:09:58 +0100795{
796 IMAGE_NT_HEADERS32 *nt;
797 IMAGE_DOS_HEADER *dos;
798 IMAGE_SECTION_HEADER *sections;
799 int num_sections;
800 void *efi_reloc;
801 int i;
802 const IMAGE_BASE_RELOCATION *rel;
803 unsigned long rel_size;
804 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700805 uint64_t image_base;
Alexander Grafcb149c62016-03-04 01:09:58 +0100806 unsigned long virt_size = 0;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700807 int supported = 0;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900808 efi_status_t ret;
809
Heinrich Schuchardt5dad05a2021-01-12 12:40:32 +0100810 ret = efi_check_pe(efi, efi_size, (void **)&nt);
811 if (ret != EFI_SUCCESS) {
812 log_err("Not a PE-COFF file\n");
813 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100814 }
815
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700816 for (i = 0; machines[i]; i++)
817 if (machines[i] == nt->FileHeader.Machine) {
818 supported = 1;
819 break;
820 }
821
822 if (!supported) {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000823 log_err("Machine type 0x%04x is not supported\n",
824 nt->FileHeader.Machine);
Heinrich Schuchardt5dad05a2021-01-12 12:40:32 +0100825 return EFI_LOAD_ERROR;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700826 }
827
Alexander Grafcb149c62016-03-04 01:09:58 +0100828 num_sections = nt->FileHeader.NumberOfSections;
829 sections = (void *)&nt->OptionalHeader +
830 nt->FileHeader.SizeOfOptionalHeader;
831
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900832 if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections
833 - efi)) {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000834 log_err("Invalid number of sections: %d\n", num_sections);
Heinrich Schuchardt5dad05a2021-01-12 12:40:32 +0100835 return EFI_LOAD_ERROR;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900836 }
837
838 /* Authenticate an image */
Heinrich Schuchardt0f7878b2020-08-27 17:51:32 +0200839 if (efi_image_authenticate(efi, efi_size)) {
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900840 handle->auth_status = EFI_IMAGE_AUTH_PASSED;
Heinrich Schuchardt0f7878b2020-08-27 17:51:32 +0200841 } else {
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900842 handle->auth_status = EFI_IMAGE_AUTH_FAILED;
Heinrich Schuchardt0f7878b2020-08-27 17:51:32 +0200843 log_err("Image not authenticated\n");
844 }
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900845
846 /* Calculate upper virtual address boundary */
Alexander Grafcb149c62016-03-04 01:09:58 +0100847 for (i = num_sections - 1; i >= 0; i--) {
848 IMAGE_SECTION_HEADER *sec = &sections[i];
849 virt_size = max_t(unsigned long, virt_size,
850 sec->VirtualAddress + sec->Misc.VirtualSize);
851 }
852
853 /* Read 32/64bit specific header bits */
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700854 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100855 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
856 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700857 image_base = opt->ImageBase;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100858 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +0200859 handle->image_type = opt->Subsystem;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100860 efi_reloc = efi_alloc(virt_size,
861 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100862 if (!efi_reloc) {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000863 log_err("Out of memory\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900864 ret = EFI_OUT_OF_RESOURCES;
865 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100866 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100867 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100868 rel_size = opt->DataDirectory[rel_idx].Size;
869 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200870 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700871 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100872 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700873 image_base = opt->ImageBase;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100874 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +0200875 handle->image_type = opt->Subsystem;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100876 efi_reloc = efi_alloc(virt_size,
877 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100878 if (!efi_reloc) {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000879 log_err("Out of memory\n");
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900880 ret = EFI_OUT_OF_RESOURCES;
881 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100882 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100883 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100884 rel_size = opt->DataDirectory[rel_idx].Size;
885 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200886 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Grafcb149c62016-03-04 01:09:58 +0100887 } else {
Heinrich Schuchardt24586052020-08-25 17:51:20 +0000888 log_err("Invalid optional header magic %x\n",
889 nt->OptionalHeader.Magic);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900890 ret = EFI_LOAD_ERROR;
891 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100892 }
893
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700894 /* Copy PE headers */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900895 memcpy(efi_reloc, efi,
896 sizeof(*dos)
897 + sizeof(*nt)
898 + nt->FileHeader.SizeOfOptionalHeader
899 + num_sections * sizeof(IMAGE_SECTION_HEADER));
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700900
Alexander Grafcb149c62016-03-04 01:09:58 +0100901 /* Load sections into RAM */
902 for (i = num_sections - 1; i >= 0; i--) {
903 IMAGE_SECTION_HEADER *sec = &sections[i];
904 memset(efi_reloc + sec->VirtualAddress, 0,
905 sec->Misc.VirtualSize);
906 memcpy(efi_reloc + sec->VirtualAddress,
907 efi + sec->PointerToRawData,
Asherah Connor9d30a942021-02-09 06:19:48 +0000908 min(sec->Misc.VirtualSize, sec->SizeOfRawData));
Alexander Grafcb149c62016-03-04 01:09:58 +0100909 }
910
911 /* Run through relocations */
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700912 if (efi_loader_relocate(rel, rel_size, efi_reloc,
913 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200914 efi_free_pages((uintptr_t) efi_reloc,
915 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900916 ret = EFI_LOAD_ERROR;
917 goto err;
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200918 }
Alexander Grafcb149c62016-03-04 01:09:58 +0100919
920 /* Flush cache */
Simon Glassd0d90992016-11-07 08:47:04 -0700921 flush_cache((ulong)efi_reloc,
Alexander Graf89aea432018-04-23 07:59:47 +0200922 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Grafcb149c62016-03-04 01:09:58 +0100923 invalidate_icache_all();
924
925 /* Populate the loaded image interface bits */
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700926 loaded_image_info->image_base = efi_reloc;
927 loaded_image_info->image_size = virt_size;
Alexander Grafcb149c62016-03-04 01:09:58 +0100928
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900929 if (handle->auth_status == EFI_IMAGE_AUTH_PASSED)
930 return EFI_SUCCESS;
931 else
932 return EFI_SECURITY_VIOLATION;
933
934err:
935 return ret;
Alexander Grafcb149c62016-03-04 01:09:58 +0100936}