1cb149c66SAlexander Graf /*
2cb149c66SAlexander Graf * EFI image loader
3cb149c66SAlexander Graf *
4cb149c66SAlexander Graf * based partly on wine code
5cb149c66SAlexander Graf *
6cb149c66SAlexander Graf * Copyright (c) 2016 Alexander Graf
7cb149c66SAlexander Graf *
8cb149c66SAlexander Graf * SPDX-License-Identifier: GPL-2.0+
9cb149c66SAlexander Graf */
10cb149c66SAlexander Graf
11cb149c66SAlexander Graf #include <common.h>
12cb149c66SAlexander Graf #include <efi_loader.h>
13cb149c66SAlexander Graf #include <pe.h>
14cb149c66SAlexander Graf #include <asm/global_data.h>
15cb149c66SAlexander Graf
16cb149c66SAlexander Graf DECLARE_GLOBAL_DATA_PTR;
17cb149c66SAlexander Graf
18cb149c66SAlexander Graf const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
19cb149c66SAlexander Graf const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
20cb149c66SAlexander Graf
efi_loader_relocate(const IMAGE_BASE_RELOCATION * rel,unsigned long rel_size,void * efi_reloc)21*da684a64Sxypron.glpk@gmx.de static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
22cb149c66SAlexander Graf unsigned long rel_size, void *efi_reloc)
23cb149c66SAlexander Graf {
24cb149c66SAlexander Graf const IMAGE_BASE_RELOCATION *end;
25cb149c66SAlexander Graf int i;
26cb149c66SAlexander Graf
27cb149c66SAlexander Graf end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
28cb149c66SAlexander Graf while (rel < end - 1 && rel->SizeOfBlock) {
29cb149c66SAlexander Graf const uint16_t *relocs = (const uint16_t *)(rel + 1);
30cb149c66SAlexander Graf i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
31cb149c66SAlexander Graf while (i--) {
32b1237c6eSAlexander Graf uint32_t offset = (uint32_t)(*relocs & 0xfff) +
33cb149c66SAlexander Graf rel->VirtualAddress;
34cb149c66SAlexander Graf int type = *relocs >> EFI_PAGE_SHIFT;
35cb149c66SAlexander Graf unsigned long delta = (unsigned long)efi_reloc;
36cb149c66SAlexander Graf uint64_t *x64 = efi_reloc + offset;
37cb149c66SAlexander Graf uint32_t *x32 = efi_reloc + offset;
38cb149c66SAlexander Graf uint16_t *x16 = efi_reloc + offset;
39cb149c66SAlexander Graf
40cb149c66SAlexander Graf switch (type) {
41cb149c66SAlexander Graf case IMAGE_REL_BASED_ABSOLUTE:
42cb149c66SAlexander Graf break;
43cb149c66SAlexander Graf case IMAGE_REL_BASED_HIGH:
44cb149c66SAlexander Graf *x16 += ((uint32_t)delta) >> 16;
45cb149c66SAlexander Graf break;
46cb149c66SAlexander Graf case IMAGE_REL_BASED_LOW:
47cb149c66SAlexander Graf *x16 += (uint16_t)delta;
48cb149c66SAlexander Graf break;
49cb149c66SAlexander Graf case IMAGE_REL_BASED_HIGHLOW:
50cb149c66SAlexander Graf *x32 += (uint32_t)delta;
51cb149c66SAlexander Graf break;
52cb149c66SAlexander Graf case IMAGE_REL_BASED_DIR64:
53cb149c66SAlexander Graf *x64 += (uint64_t)delta;
54cb149c66SAlexander Graf break;
55cb149c66SAlexander Graf default:
56cb149c66SAlexander Graf printf("Unknown Relocation off %x type %x\n",
57cb149c66SAlexander Graf offset, type);
58*da684a64Sxypron.glpk@gmx.de return EFI_LOAD_ERROR;
59cb149c66SAlexander Graf }
60cb149c66SAlexander Graf relocs++;
61cb149c66SAlexander Graf }
62cb149c66SAlexander Graf rel = (const IMAGE_BASE_RELOCATION *)relocs;
63cb149c66SAlexander Graf }
64*da684a64Sxypron.glpk@gmx.de return EFI_SUCCESS;
65cb149c66SAlexander Graf }
66cb149c66SAlexander Graf
invalidate_icache_all(void)67cb149c66SAlexander Graf void __weak invalidate_icache_all(void)
68cb149c66SAlexander Graf {
69cb149c66SAlexander Graf /* If the system doesn't support icache_all flush, cross our fingers */
70cb149c66SAlexander Graf }
71cb149c66SAlexander Graf
72cb149c66SAlexander Graf /*
73cb149c66SAlexander Graf * This function loads all sections from a PE binary into a newly reserved
74cb149c66SAlexander Graf * piece of memory. On successful load it then returns the entry point for
75cb149c66SAlexander Graf * the binary. Otherwise NULL.
76cb149c66SAlexander Graf */
efi_load_pe(void * efi,struct efi_loaded_image * loaded_image_info)77cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
78cb149c66SAlexander Graf {
79cb149c66SAlexander Graf IMAGE_NT_HEADERS32 *nt;
80cb149c66SAlexander Graf IMAGE_DOS_HEADER *dos;
81cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sections;
82cb149c66SAlexander Graf int num_sections;
83cb149c66SAlexander Graf void *efi_reloc;
84cb149c66SAlexander Graf int i;
85cb149c66SAlexander Graf const IMAGE_BASE_RELOCATION *rel;
86cb149c66SAlexander Graf unsigned long rel_size;
87cb149c66SAlexander Graf int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
88cb149c66SAlexander Graf void *entry;
89cb149c66SAlexander Graf uint64_t image_size;
90cb149c66SAlexander Graf unsigned long virt_size = 0;
91cb149c66SAlexander Graf bool can_run_nt64 = true;
92cb149c66SAlexander Graf bool can_run_nt32 = true;
93cb149c66SAlexander Graf
94cb149c66SAlexander Graf #if defined(CONFIG_ARM64)
95cb149c66SAlexander Graf can_run_nt32 = false;
96cb149c66SAlexander Graf #elif defined(CONFIG_ARM)
97cb149c66SAlexander Graf can_run_nt64 = false;
98cb149c66SAlexander Graf #endif
99cb149c66SAlexander Graf
100cb149c66SAlexander Graf dos = efi;
101cb149c66SAlexander Graf if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
102cb149c66SAlexander Graf printf("%s: Invalid DOS Signature\n", __func__);
103cb149c66SAlexander Graf return NULL;
104cb149c66SAlexander Graf }
105cb149c66SAlexander Graf
106cb149c66SAlexander Graf nt = (void *) ((char *)efi + dos->e_lfanew);
107cb149c66SAlexander Graf if (nt->Signature != IMAGE_NT_SIGNATURE) {
108cb149c66SAlexander Graf printf("%s: Invalid NT Signature\n", __func__);
109cb149c66SAlexander Graf return NULL;
110cb149c66SAlexander Graf }
111cb149c66SAlexander Graf
112cb149c66SAlexander Graf /* Calculate upper virtual address boundary */
113cb149c66SAlexander Graf num_sections = nt->FileHeader.NumberOfSections;
114cb149c66SAlexander Graf sections = (void *)&nt->OptionalHeader +
115cb149c66SAlexander Graf nt->FileHeader.SizeOfOptionalHeader;
116cb149c66SAlexander Graf
117cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) {
118cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i];
119cb149c66SAlexander Graf virt_size = max_t(unsigned long, virt_size,
120cb149c66SAlexander Graf sec->VirtualAddress + sec->Misc.VirtualSize);
121cb149c66SAlexander Graf }
122cb149c66SAlexander Graf
123cb149c66SAlexander Graf /* Read 32/64bit specific header bits */
124cb149c66SAlexander Graf if (can_run_nt64 &&
125cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
126cb149c66SAlexander Graf IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
127cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
128cb149c66SAlexander Graf image_size = opt->SizeOfImage;
129cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
130cb149c66SAlexander Graf if (!efi_reloc) {
131cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n",
132cb149c66SAlexander Graf __func__, virt_size);
133cb149c66SAlexander Graf return NULL;
134cb149c66SAlexander Graf }
135cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint;
136cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size;
137cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
138cb149c66SAlexander Graf } else if (can_run_nt32 &&
139cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
140cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
141cb149c66SAlexander Graf image_size = opt->SizeOfImage;
142cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
143cb149c66SAlexander Graf if (!efi_reloc) {
144cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n",
145cb149c66SAlexander Graf __func__, virt_size);
146cb149c66SAlexander Graf return NULL;
147cb149c66SAlexander Graf }
148cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint;
149cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size;
150cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
151cb149c66SAlexander Graf } else {
152cb149c66SAlexander Graf printf("%s: Invalid optional header magic %x\n", __func__,
153cb149c66SAlexander Graf nt->OptionalHeader.Magic);
154cb149c66SAlexander Graf return NULL;
155cb149c66SAlexander Graf }
156cb149c66SAlexander Graf
157cb149c66SAlexander Graf /* Load sections into RAM */
158cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) {
159cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i];
160cb149c66SAlexander Graf memset(efi_reloc + sec->VirtualAddress, 0,
161cb149c66SAlexander Graf sec->Misc.VirtualSize);
162cb149c66SAlexander Graf memcpy(efi_reloc + sec->VirtualAddress,
163cb149c66SAlexander Graf efi + sec->PointerToRawData,
164cb149c66SAlexander Graf sec->SizeOfRawData);
165cb149c66SAlexander Graf }
166cb149c66SAlexander Graf
167cb149c66SAlexander Graf /* Run through relocations */
168*da684a64Sxypron.glpk@gmx.de if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
169*da684a64Sxypron.glpk@gmx.de efi_free_pages((uintptr_t) efi_reloc,
170*da684a64Sxypron.glpk@gmx.de (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
171*da684a64Sxypron.glpk@gmx.de return NULL;
172*da684a64Sxypron.glpk@gmx.de }
173cb149c66SAlexander Graf
174cb149c66SAlexander Graf /* Flush cache */
175d0d90993SSimon Glass flush_cache((ulong)efi_reloc,
176d0d90993SSimon Glass ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
177cb149c66SAlexander Graf invalidate_icache_all();
178cb149c66SAlexander Graf
179cb149c66SAlexander Graf /* Populate the loaded image interface bits */
180cb149c66SAlexander Graf loaded_image_info->image_base = efi;
181cb149c66SAlexander Graf loaded_image_info->image_size = image_size;
182cb149c66SAlexander Graf
183cb149c66SAlexander Graf return entry;
184cb149c66SAlexander Graf }
185