1
2
3
4
5
6
7
8
9
10#define LOG_CATEGORY LOGC_EFI
11
12#include <common.h>
13#include <cpu_func.h>
14#include <efi_loader.h>
15#include <log.h>
16#include <malloc.h>
17#include <pe.h>
18#include <sort.h>
19#include <crypto/pkcs7_parser.h>
20#include <linux/err.h>
21
22const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
23const 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;
27const 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;
30
31static int machines[] = {
32#if defined(__aarch64__)
33 IMAGE_FILE_MACHINE_ARM64,
34#elif defined(__arm__)
35 IMAGE_FILE_MACHINE_ARM,
36 IMAGE_FILE_MACHINE_THUMB,
37 IMAGE_FILE_MACHINE_ARMNT,
38#endif
39
40#if defined(__x86_64__)
41 IMAGE_FILE_MACHINE_AMD64,
42#elif defined(__i386__)
43 IMAGE_FILE_MACHINE_I386,
44#endif
45
46#if defined(__riscv) && (__riscv_xlen == 32)
47 IMAGE_FILE_MACHINE_RISCV32,
48#endif
49
50#if defined(__riscv) && (__riscv_xlen == 64)
51 IMAGE_FILE_MACHINE_RISCV64,
52#endif
53 0 };
54
55
56
57
58
59
60
61
62
63
64
65
66static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
67 struct efi_loaded_image *image,
68 void *pc)
69{
70 printf("UEFI image");
71 printf(" [0x%p:0x%p]",
72 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);
76 if (image->file_path)
77 printf(" '%pD'", image->file_path);
78 printf("\n");
79 return EFI_SUCCESS;
80}
81
82
83
84
85
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(
96 (struct efi_loaded_image_obj *)efiobj,
97 handler->protocol_interface, pc);
98 }
99 }
100 }
101}
102
103
104
105
106
107
108
109
110
111
112static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
113 unsigned long rel_size, void *efi_reloc,
114 unsigned long pref_address)
115{
116 unsigned long delta = (unsigned long)efi_reloc - pref_address;
117 const IMAGE_BASE_RELOCATION *end;
118 int i;
119
120 if (delta == 0)
121 return EFI_SUCCESS;
122
123 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
124 while (rel < end && rel->SizeOfBlock) {
125 const uint16_t *relocs = (const uint16_t *)(rel + 1);
126 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
127 while (i--) {
128 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
129 rel->VirtualAddress;
130 int type = *relocs >> EFI_PAGE_SHIFT;
131 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;
150#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
158 if (delta & 0xfff) {
159 log_err("Unsupported reloc offset\n");
160 return EFI_LOAD_ERROR;
161 }
162 break;
163#endif
164 default:
165 log_err("Unknown Relocation off %x type %x\n",
166 offset, type);
167 return EFI_LOAD_ERROR;
168 }
169 relocs++;
170 }
171 rel = (const IMAGE_BASE_RELOCATION *)relocs;
172 }
173 return EFI_SUCCESS;
174}
175
176void __weak invalidate_icache_all(void)
177{
178
179}
180
181
182
183
184
185
186
187
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:
203 case IMAGE_SUBSYSTEM_EFI_ROM:
204 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:
208 log_err("invalid image type: %u\n", image_type);
209
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
216#ifdef CONFIG_EFI_SECURE_BOOT
217
218
219
220
221
222
223
224
225
226
227
228
229static int cmp_pe_section(const void *arg1, const void *arg2)
230{
231 const IMAGE_SECTION_HEADER *section1, *section2;
232
233 section1 = *((const IMAGE_SECTION_HEADER **)arg1);
234 section2 = *((const IMAGE_SECTION_HEADER **)arg2);
235
236 if (section1->VirtualAddress < section2->VirtualAddress)
237 return -1;
238 else if (section1->VirtualAddress == section2->VirtualAddress)
239 return 0;
240 else
241 return 1;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
260 WIN_CERTIFICATE **auth, size_t *auth_len)
261{
262 struct efi_image_regions *regs;
263 IMAGE_DOS_HEADER *dos;
264 IMAGE_NT_HEADERS32 *nt;
265 IMAGE_SECTION_HEADER *sections, **sorted;
266 int num_regions, num_sections, i;
267 int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY;
268 u32 align, size, authsz, authoff;
269 size_t bytes_hashed;
270
271 dos = (void *)efi;
272 nt = (void *)(efi + dos->e_lfanew);
273 authoff = 0;
274 authsz = 0;
275
276
277
278
279
280
281 num_regions = 3;
282 num_regions += nt->FileHeader.NumberOfSections;
283 num_regions++;
284
285 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions,
286 1);
287 if (!regs)
288 goto err;
289 regs->max = num_regions;
290
291
292
293
294
295 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
296 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
297 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
298
299
300 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
301 if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
302 efi_image_region_add(regs,
303 &opt->Subsystem,
304 efi + opt->SizeOfHeaders, 0);
305 } else {
306
307 efi_image_region_add(regs,
308 &opt->Subsystem,
309 &opt->DataDirectory[ctidx], 0);
310 efi_image_region_add(regs,
311 &opt->DataDirectory[ctidx] + 1,
312 efi + opt->SizeOfHeaders, 0);
313
314 authoff = opt->DataDirectory[ctidx].VirtualAddress;
315 authsz = opt->DataDirectory[ctidx].Size;
316 }
317
318 bytes_hashed = opt->SizeOfHeaders;
319 align = opt->FileAlignment;
320 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
321 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
322
323
324 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
325 if (nt->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
326 efi_image_region_add(regs,
327 &opt->Subsystem,
328 efi + opt->SizeOfHeaders, 0);
329 } else {
330
331 efi_image_region_add(regs, &opt->Subsystem,
332 &opt->DataDirectory[ctidx], 0);
333 efi_image_region_add(regs,
334 &opt->DataDirectory[ctidx] + 1,
335 efi + opt->SizeOfHeaders, 0);
336
337 authoff = opt->DataDirectory[ctidx].VirtualAddress;
338 authsz = opt->DataDirectory[ctidx].Size;
339 }
340
341 bytes_hashed = opt->SizeOfHeaders;
342 align = opt->FileAlignment;
343 } else {
344 EFI_PRINT("%s: Invalid optional header magic %x\n", __func__,
345 nt->OptionalHeader.Magic);
346 goto err;
347 }
348
349
350 num_sections = nt->FileHeader.NumberOfSections;
351 sections = (void *)((uint8_t *)&nt->OptionalHeader +
352 nt->FileHeader.SizeOfOptionalHeader);
353 sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections);
354 if (!sorted) {
355 EFI_PRINT("%s: Out of memory\n", __func__);
356 goto err;
357 }
358
359
360
361
362 for (i = 0; i < num_sections; i++)
363 sorted[i] = §ions[i];
364 qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section);
365
366 for (i = 0; i < num_sections; i++) {
367 if (!sorted[i]->SizeOfRawData)
368 continue;
369
370 size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1);
371 efi_image_region_add(regs, efi + sorted[i]->PointerToRawData,
372 efi + sorted[i]->PointerToRawData + size,
373 0);
374 EFI_PRINT("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
375 i, sorted[i]->Name,
376 sorted[i]->PointerToRawData,
377 sorted[i]->PointerToRawData + size,
378 sorted[i]->VirtualAddress,
379 sorted[i]->VirtualAddress
380 + sorted[i]->Misc.VirtualSize);
381
382 bytes_hashed += size;
383 }
384 free(sorted);
385
386
387 if (bytes_hashed + authsz < len) {
388 EFI_PRINT("extra data for hash: %zu\n",
389 len - (bytes_hashed + authsz));
390 efi_image_region_add(regs, efi + bytes_hashed,
391 efi + len - authsz, 0);
392 }
393
394
395 if (authsz) {
396 if (len < authoff + authsz) {
397 EFI_PRINT("%s: Size for auth too large: %u >= %zu\n",
398 __func__, authsz, len - authoff);
399 goto err;
400 }
401 if (authsz < sizeof(*auth)) {
402 EFI_PRINT("%s: Size for auth too small: %u < %zu\n",
403 __func__, authsz, sizeof(*auth));
404 goto err;
405 }
406 *auth = efi + authoff;
407 *auth_len = authsz;
408 EFI_PRINT("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff,
409 authsz);
410 } else {
411 *auth = NULL;
412 *auth_len = 0;
413 }
414
415 *regp = regs;
416
417 return true;
418
419err:
420 free(regs);
421
422 return false;
423}
424
425
426
427
428
429
430
431
432
433
434
435
436static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs)
437{
438 struct efi_signature_store *db = NULL, *dbx = NULL;
439 bool ret = false;
440
441 dbx = efi_sigstore_parse_sigdb(L"dbx");
442 if (!dbx) {
443 EFI_PRINT("Getting signature database(dbx) failed\n");
444 goto out;
445 }
446
447 db = efi_sigstore_parse_sigdb(L"db");
448 if (!db) {
449 EFI_PRINT("Getting signature database(db) failed\n");
450 goto out;
451 }
452
453
454 if (efi_signature_lookup_digest(regs, dbx)) {
455 EFI_PRINT("Image is not signed and its digest found in \"dbx\"\n");
456 goto out;
457 }
458
459
460 if (efi_signature_lookup_digest(regs, db))
461 ret = true;
462 else
463 EFI_PRINT("Image is not signed and its digest not found in \"db\" or \"dbx\"\n");
464
465out:
466 efi_sigstore_free(db);
467 efi_sigstore_free(dbx);
468
469 return ret;
470}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493static bool efi_image_authenticate(void *efi, size_t efi_size)
494{
495 struct efi_image_regions *regs = NULL;
496 WIN_CERTIFICATE *wincerts = NULL, *wincert;
497 size_t wincerts_len;
498 struct pkcs7_message *msg = NULL;
499 struct efi_signature_store *db = NULL, *dbx = NULL;
500 void *new_efi = NULL;
501 u8 *auth, *wincerts_end;
502 size_t new_efi_size, auth_size;
503 bool ret = false;
504
505 EFI_PRINT("%s: Enter, %d\n", __func__, ret);
506
507 if (!efi_secure_boot_enabled())
508 return true;
509
510
511
512
513
514 if (efi_size & 0x7) {
515 new_efi_size = (efi_size + 0x7) & ~0x7ULL;
516 new_efi = calloc(new_efi_size, 1);
517 if (!new_efi)
518 return false;
519 memcpy(new_efi, efi, efi_size);
520 efi = new_efi;
521 efi_size = new_efi_size;
522 }
523
524 if (!efi_image_parse(efi, efi_size, ®s, &wincerts,
525 &wincerts_len)) {
526 EFI_PRINT("Parsing PE executable image failed\n");
527 goto err;
528 }
529
530 if (!wincerts) {
531
532 ret = efi_image_unsigned_authenticate(regs);
533
534 goto err;
535 }
536
537
538
539
540 db = efi_sigstore_parse_sigdb(L"db");
541 if (!db) {
542 EFI_PRINT("Getting signature database(db) failed\n");
543 goto err;
544 }
545
546 dbx = efi_sigstore_parse_sigdb(L"dbx");
547 if (!dbx) {
548 EFI_PRINT("Getting signature database(dbx) failed\n");
549 goto err;
550 }
551
552 if (efi_signature_lookup_digest(regs, dbx)) {
553 EFI_PRINT("Image's digest was found in \"dbx\"\n");
554 goto err;
555 }
556
557
558
559
560
561
562
563
564
565
566
567 for (wincert = wincerts, wincerts_end = (u8 *)wincerts + wincerts_len;
568 (u8 *)wincert < wincerts_end;
569 wincert = (WIN_CERTIFICATE *)
570 ((u8 *)wincert + ALIGN(wincert->dwLength, 8))) {
571 if ((u8 *)wincert + sizeof(*wincert) >= wincerts_end)
572 break;
573
574 if (wincert->dwLength <= sizeof(*wincert)) {
575 EFI_PRINT("dwLength too small: %u < %zu\n",
576 wincert->dwLength, sizeof(*wincert));
577 continue;
578 }
579
580 EFI_PRINT("WIN_CERTIFICATE_TYPE: 0x%x\n",
581 wincert->wCertificateType);
582
583 auth = (u8 *)wincert + sizeof(*wincert);
584 auth_size = wincert->dwLength - sizeof(*wincert);
585 if (wincert->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
586 if (auth + sizeof(efi_guid_t) >= wincerts_end)
587 break;
588
589 if (auth_size <= sizeof(efi_guid_t)) {
590 EFI_PRINT("dwLength too small: %u < %zu\n",
591 wincert->dwLength, sizeof(*wincert));
592 continue;
593 }
594 if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) {
595 EFI_PRINT("Certificate type not supported: %pUl\n",
596 auth);
597 continue;
598 }
599
600 auth += sizeof(efi_guid_t);
601 auth_size -= sizeof(efi_guid_t);
602 } else if (wincert->wCertificateType
603 != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
604 EFI_PRINT("Certificate type not supported\n");
605 continue;
606 }
607
608 msg = pkcs7_parse_message(auth, auth_size);
609 if (IS_ERR(msg)) {
610 EFI_PRINT("Parsing image's signature failed\n");
611 msg = NULL;
612 continue;
613 }
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635 if (efi_signature_verify_one(regs, msg, dbx)) {
636 EFI_PRINT("Signature was rejected by \"dbx\"\n");
637 continue;
638 }
639
640 if (!efi_signature_check_signers(msg, dbx)) {
641 EFI_PRINT("Signer(s) in \"dbx\"\n");
642 continue;
643 }
644
645
646 if (efi_signature_verify(regs, msg, db, dbx)) {
647 ret = true;
648 break;
649 }
650
651 EFI_PRINT("Signature was not verified by \"db\"\n");
652
653 if (efi_signature_lookup_digest(regs, db)) {
654 ret = true;
655 break;
656 }
657
658 EFI_PRINT("Image's digest was not found in \"db\" or \"dbx\"\n");
659 }
660
661err:
662 efi_sigstore_free(db);
663 efi_sigstore_free(dbx);
664 pkcs7_free_message(msg);
665 free(regs);
666 free(new_efi);
667
668 EFI_PRINT("%s: Exit, %d\n", __func__, ret);
669 return ret;
670}
671#else
672static bool efi_image_authenticate(void *efi, size_t efi_size)
673{
674 return true;
675}
676#endif
677
678
679
680
681
682
683
684
685
686
687
688
689
690efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
691 void *efi, size_t efi_size,
692 struct efi_loaded_image *loaded_image_info)
693{
694 IMAGE_NT_HEADERS32 *nt;
695 IMAGE_DOS_HEADER *dos;
696 IMAGE_SECTION_HEADER *sections;
697 int num_sections;
698 void *efi_reloc;
699 int i;
700 const IMAGE_BASE_RELOCATION *rel;
701 unsigned long rel_size;
702 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
703 uint64_t image_base;
704 unsigned long virt_size = 0;
705 int supported = 0;
706 efi_status_t ret;
707
708
709 if (efi_size < sizeof(*dos)) {
710 log_err("Truncated DOS Header\n");
711 ret = EFI_LOAD_ERROR;
712 goto err;
713 }
714
715 dos = efi;
716 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
717 log_err("Invalid DOS Signature\n");
718 ret = EFI_LOAD_ERROR;
719 goto err;
720 }
721
722
723
724
725
726
727 if (efi_size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS64)) {
728 log_err("Invalid offset for Extended Header\n");
729 ret = EFI_LOAD_ERROR;
730 goto err;
731 }
732
733 nt = (void *) ((char *)efi + dos->e_lfanew);
734 if (nt->Signature != IMAGE_NT_SIGNATURE) {
735 log_err("Invalid NT Signature\n");
736 ret = EFI_LOAD_ERROR;
737 goto err;
738 }
739
740 for (i = 0; machines[i]; i++)
741 if (machines[i] == nt->FileHeader.Machine) {
742 supported = 1;
743 break;
744 }
745
746 if (!supported) {
747 log_err("Machine type 0x%04x is not supported\n",
748 nt->FileHeader.Machine);
749 ret = EFI_LOAD_ERROR;
750 goto err;
751 }
752
753 num_sections = nt->FileHeader.NumberOfSections;
754 sections = (void *)&nt->OptionalHeader +
755 nt->FileHeader.SizeOfOptionalHeader;
756
757 if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections
758 - efi)) {
759 log_err("Invalid number of sections: %d\n", num_sections);
760 ret = EFI_LOAD_ERROR;
761 goto err;
762 }
763
764
765 if (efi_image_authenticate(efi, efi_size)) {
766 handle->auth_status = EFI_IMAGE_AUTH_PASSED;
767 } else {
768 handle->auth_status = EFI_IMAGE_AUTH_FAILED;
769 log_err("Image not authenticated\n");
770 }
771
772
773 for (i = num_sections - 1; i >= 0; i--) {
774 IMAGE_SECTION_HEADER *sec = §ions[i];
775 virt_size = max_t(unsigned long, virt_size,
776 sec->VirtualAddress + sec->Misc.VirtualSize);
777 }
778
779
780 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
781 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
782 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
783 image_base = opt->ImageBase;
784 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
785 handle->image_type = opt->Subsystem;
786 efi_reloc = efi_alloc(virt_size,
787 loaded_image_info->image_code_type);
788 if (!efi_reloc) {
789 log_err("Out of memory\n");
790 ret = EFI_OUT_OF_RESOURCES;
791 goto err;
792 }
793 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
794 rel_size = opt->DataDirectory[rel_idx].Size;
795 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
796 virt_size = ALIGN(virt_size, opt->SectionAlignment);
797 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
798 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
799 image_base = opt->ImageBase;
800 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
801 handle->image_type = opt->Subsystem;
802 efi_reloc = efi_alloc(virt_size,
803 loaded_image_info->image_code_type);
804 if (!efi_reloc) {
805 log_err("Out of memory\n");
806 ret = EFI_OUT_OF_RESOURCES;
807 goto err;
808 }
809 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
810 rel_size = opt->DataDirectory[rel_idx].Size;
811 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
812 virt_size = ALIGN(virt_size, opt->SectionAlignment);
813 } else {
814 log_err("Invalid optional header magic %x\n",
815 nt->OptionalHeader.Magic);
816 ret = EFI_LOAD_ERROR;
817 goto err;
818 }
819
820
821 memcpy(efi_reloc, efi,
822 sizeof(*dos)
823 + sizeof(*nt)
824 + nt->FileHeader.SizeOfOptionalHeader
825 + num_sections * sizeof(IMAGE_SECTION_HEADER));
826
827
828 for (i = num_sections - 1; i >= 0; i--) {
829 IMAGE_SECTION_HEADER *sec = §ions[i];
830 memset(efi_reloc + sec->VirtualAddress, 0,
831 sec->Misc.VirtualSize);
832 memcpy(efi_reloc + sec->VirtualAddress,
833 efi + sec->PointerToRawData,
834 sec->SizeOfRawData);
835 }
836
837
838 if (efi_loader_relocate(rel, rel_size, efi_reloc,
839 (unsigned long)image_base) != EFI_SUCCESS) {
840 efi_free_pages((uintptr_t) efi_reloc,
841 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
842 ret = EFI_LOAD_ERROR;
843 goto err;
844 }
845
846
847 flush_cache((ulong)efi_reloc,
848 ALIGN(virt_size, EFI_CACHELINE_SIZE));
849 invalidate_icache_all();
850
851
852 loaded_image_info->image_base = efi_reloc;
853 loaded_image_info->image_size = virt_size;
854
855 if (handle->auth_status == EFI_IMAGE_AUTH_PASSED)
856 return EFI_SUCCESS;
857 else
858 return EFI_SECURITY_VIOLATION;
859
860err:
861 return ret;
862}
863