blob: f5885760d416338e58bab63736c97319cf8c3da5 [file] [log] [blame]
Alexander Grafcb149c62016-03-04 01:09:58 +01001/*
2 * EFI image loader
3 *
4 * based partly on wine code
5 *
6 * Copyright (c) 2016 Alexander Graf
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
12#include <efi_loader.h>
13#include <pe.h>
14#include <asm/global_data.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Rob Clark9975fe92017-09-13 18:05:38 -040018const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Alexander Grafcb149c62016-03-04 01:09:58 +010019const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
20const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
Rob Clark2a920802017-09-13 18:05:34 -040021const efi_guid_t efi_simple_file_system_protocol_guid =
22 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
23const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Grafcb149c62016-03-04 01:09:58 +010024
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020025/*
26 * Print information about a loaded image.
27 *
28 * If the program counter is located within the image the offset to the base
29 * address is shown.
30 *
31 * @image: loaded image
32 * @pc: program counter (use NULL to suppress offset output)
33 * @return: status code
34 */
35efi_status_t efi_print_image_info(struct efi_loaded_image *image, void *pc)
36{
37 if (!image)
38 return EFI_INVALID_PARAMETER;
39 printf("UEFI image");
40 printf(" [0x%p:0x%p]",
41 image->reloc_base, image->reloc_base + image->reloc_size - 1);
42 if (pc && pc >= image->reloc_base &&
43 pc < image->reloc_base + image->reloc_size)
44 printf(" pc=0x%zx", pc - image->reloc_base);
45 if (image->file_path)
46 printf(" '%pD'", image->file_path);
47 printf("\n");
48 return EFI_SUCCESS;
49}
50
51/*
52 * Print information about all loaded images.
53 *
54 * @pc: program counter (use NULL to suppress offset output)
55 */
56void efi_print_image_infos(void *pc)
57{
58 struct efi_object *efiobj;
59 struct efi_handler *handler;
60
61 list_for_each_entry(efiobj, &efi_obj_list, link) {
62 list_for_each_entry(handler, &efiobj->protocols, link) {
63 if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
64 efi_print_image_info(
65 handler->protocol_interface, pc);
66 }
67 }
68 }
69}
70
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +020071static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Alexander Grafcb149c62016-03-04 01:09:58 +010072 unsigned long rel_size, void *efi_reloc)
73{
74 const IMAGE_BASE_RELOCATION *end;
75 int i;
76
77 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
78 while (rel < end - 1 && rel->SizeOfBlock) {
79 const uint16_t *relocs = (const uint16_t *)(rel + 1);
80 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
81 while (i--) {
Alexander Grafb1237c62016-08-18 23:45:18 +020082 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Grafcb149c62016-03-04 01:09:58 +010083 rel->VirtualAddress;
84 int type = *relocs >> EFI_PAGE_SHIFT;
85 unsigned long delta = (unsigned long)efi_reloc;
86 uint64_t *x64 = efi_reloc + offset;
87 uint32_t *x32 = efi_reloc + offset;
88 uint16_t *x16 = efi_reloc + offset;
89
90 switch (type) {
91 case IMAGE_REL_BASED_ABSOLUTE:
92 break;
93 case IMAGE_REL_BASED_HIGH:
94 *x16 += ((uint32_t)delta) >> 16;
95 break;
96 case IMAGE_REL_BASED_LOW:
97 *x16 += (uint16_t)delta;
98 break;
99 case IMAGE_REL_BASED_HIGHLOW:
100 *x32 += (uint32_t)delta;
101 break;
102 case IMAGE_REL_BASED_DIR64:
103 *x64 += (uint64_t)delta;
104 break;
105 default:
106 printf("Unknown Relocation off %x type %x\n",
107 offset, type);
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200108 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100109 }
110 relocs++;
111 }
112 rel = (const IMAGE_BASE_RELOCATION *)relocs;
113 }
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200114 return EFI_SUCCESS;
Alexander Grafcb149c62016-03-04 01:09:58 +0100115}
116
117void __weak invalidate_icache_all(void)
118{
119 /* If the system doesn't support icache_all flush, cross our fingers */
120}
121
122/*
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100123 * Determine the memory types to be used for code and data.
124 *
125 * @loaded_image_info image descriptor
126 * @image_type field Subsystem of the optional header for
127 * Windows specific field
128 */
129static void efi_set_code_and_data_type(
130 struct efi_loaded_image *loaded_image_info,
131 uint16_t image_type)
132{
133 switch (image_type) {
134 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
135 loaded_image_info->image_code_type = EFI_LOADER_CODE;
136 loaded_image_info->image_data_type = EFI_LOADER_DATA;
137 break;
138 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
139 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
140 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
141 break;
142 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt268ec6e2018-01-31 18:45:35 +0000143 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100144 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
145 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
146 break;
147 default:
148 printf("%s: invalid image type: %u\n", __func__, image_type);
149 /* Let's assume it is an application */
150 loaded_image_info->image_code_type = EFI_LOADER_CODE;
151 loaded_image_info->image_data_type = EFI_LOADER_DATA;
152 break;
153 }
154}
155
156/*
Alexander Grafcb149c62016-03-04 01:09:58 +0100157 * This function loads all sections from a PE binary into a newly reserved
158 * piece of memory. On successful load it then returns the entry point for
159 * the binary. Otherwise NULL.
160 */
161void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
162{
163 IMAGE_NT_HEADERS32 *nt;
164 IMAGE_DOS_HEADER *dos;
165 IMAGE_SECTION_HEADER *sections;
166 int num_sections;
167 void *efi_reloc;
168 int i;
169 const IMAGE_BASE_RELOCATION *rel;
170 unsigned long rel_size;
171 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
172 void *entry;
173 uint64_t image_size;
174 unsigned long virt_size = 0;
175 bool can_run_nt64 = true;
176 bool can_run_nt32 = true;
177
178#if defined(CONFIG_ARM64)
179 can_run_nt32 = false;
180#elif defined(CONFIG_ARM)
181 can_run_nt64 = false;
182#endif
183
184 dos = efi;
185 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
186 printf("%s: Invalid DOS Signature\n", __func__);
187 return NULL;
188 }
189
190 nt = (void *) ((char *)efi + dos->e_lfanew);
191 if (nt->Signature != IMAGE_NT_SIGNATURE) {
192 printf("%s: Invalid NT Signature\n", __func__);
193 return NULL;
194 }
195
196 /* Calculate upper virtual address boundary */
197 num_sections = nt->FileHeader.NumberOfSections;
198 sections = (void *)&nt->OptionalHeader +
199 nt->FileHeader.SizeOfOptionalHeader;
200
201 for (i = num_sections - 1; i >= 0; i--) {
202 IMAGE_SECTION_HEADER *sec = &sections[i];
203 virt_size = max_t(unsigned long, virt_size,
204 sec->VirtualAddress + sec->Misc.VirtualSize);
205 }
206
207 /* Read 32/64bit specific header bits */
208 if (can_run_nt64 &&
209 (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
210 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
211 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
212 image_size = opt->SizeOfImage;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100213 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
214 efi_reloc = efi_alloc(virt_size,
215 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100216 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100217 printf("%s: Could not allocate %lu bytes\n",
218 __func__, virt_size);
Alexander Grafcb149c62016-03-04 01:09:58 +0100219 return NULL;
220 }
221 entry = efi_reloc + opt->AddressOfEntryPoint;
222 rel_size = opt->DataDirectory[rel_idx].Size;
223 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200224 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Grafcb149c62016-03-04 01:09:58 +0100225 } else if (can_run_nt32 &&
226 (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
227 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
228 image_size = opt->SizeOfImage;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100229 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
230 efi_reloc = efi_alloc(virt_size,
231 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100232 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100233 printf("%s: Could not allocate %lu bytes\n",
234 __func__, virt_size);
Alexander Grafcb149c62016-03-04 01:09:58 +0100235 return NULL;
236 }
237 entry = efi_reloc + opt->AddressOfEntryPoint;
238 rel_size = opt->DataDirectory[rel_idx].Size;
239 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200240 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Grafcb149c62016-03-04 01:09:58 +0100241 } else {
242 printf("%s: Invalid optional header magic %x\n", __func__,
243 nt->OptionalHeader.Magic);
244 return NULL;
245 }
246
247 /* Load sections into RAM */
248 for (i = num_sections - 1; i >= 0; i--) {
249 IMAGE_SECTION_HEADER *sec = &sections[i];
250 memset(efi_reloc + sec->VirtualAddress, 0,
251 sec->Misc.VirtualSize);
252 memcpy(efi_reloc + sec->VirtualAddress,
253 efi + sec->PointerToRawData,
254 sec->SizeOfRawData);
255 }
256
257 /* Run through relocations */
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200258 if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
259 efi_free_pages((uintptr_t) efi_reloc,
260 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
261 return NULL;
262 }
Alexander Grafcb149c62016-03-04 01:09:58 +0100263
264 /* Flush cache */
Simon Glassd0d90992016-11-07 08:47:04 -0700265 flush_cache((ulong)efi_reloc,
266 ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
Alexander Grafcb149c62016-03-04 01:09:58 +0100267 invalidate_icache_all();
268
269 /* Populate the loaded image interface bits */
270 loaded_image_info->image_base = efi;
271 loaded_image_info->image_size = image_size;
Heinrich Schuchardt84b40b42018-04-03 22:29:31 +0200272 loaded_image_info->reloc_base = efi_reloc;
273 loaded_image_info->reloc_size = virt_size;
Alexander Grafcb149c62016-03-04 01:09:58 +0100274
275 return entry;
276}