blob: 13541cfa7a2873862239fcfc889c45685b403ac2 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf4e0c1c12016-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 Graf4e0c1c12016-03-04 01:09:58 +01008 */
9
10#include <common.h>
11#include <efi_loader.h>
12#include <pe.h>
Alexander Graf4e0c1c12016-03-04 01:09:58 +010013
Rob Clarkc84c1102017-09-13 18:05:38 -040014const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020015const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
16const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID;
17const efi_guid_t efi_guid_loaded_image_device_path =
18 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
Rob Clark53142032017-09-13 18:05:34 -040019const efi_guid_t efi_simple_file_system_protocol_guid =
20 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
21const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010022
Ivan Gorinov749d5c12018-04-05 18:32:06 -070023static int machines[] = {
Alexander Graf8aec3082018-06-18 17:22:57 +020024#if defined(__aarch64__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070025 IMAGE_FILE_MACHINE_ARM64,
Alexander Graf8aec3082018-06-18 17:22:57 +020026#elif defined(__arm__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070027 IMAGE_FILE_MACHINE_ARM,
28 IMAGE_FILE_MACHINE_THUMB,
29 IMAGE_FILE_MACHINE_ARMNT,
30#endif
31
Alexander Graf8aec3082018-06-18 17:22:57 +020032#if defined(__x86_64__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070033 IMAGE_FILE_MACHINE_AMD64,
Alexander Graf8aec3082018-06-18 17:22:57 +020034#elif defined(__i386__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070035 IMAGE_FILE_MACHINE_I386,
36#endif
37
Alexander Graf8aec3082018-06-18 17:22:57 +020038#if defined(__riscv) && (__riscv_xlen == 32)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070039 IMAGE_FILE_MACHINE_RISCV32,
40#endif
41
Alexander Graf8aec3082018-06-18 17:22:57 +020042#if defined(__riscv) && (__riscv_xlen == 64)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070043 IMAGE_FILE_MACHINE_RISCV64,
44#endif
45 0 };
46
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010047/**
48 * efi_print_image_info() - print information about a loaded image
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020049 *
50 * If the program counter is located within the image the offset to the base
51 * address is shown.
52 *
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020053 * @obj: EFI object
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020054 * @image: loaded image
55 * @pc: program counter (use NULL to suppress offset output)
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010056 * Return: status code
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020057 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020058static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
59 struct efi_loaded_image *image,
60 void *pc)
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020061{
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020062 printf("UEFI image");
63 printf(" [0x%p:0x%p]",
AKASHI Takahiro068879a2018-10-11 04:09:58 -070064 image->image_base, image->image_base + image->image_size - 1);
65 if (pc && pc >= image->image_base &&
66 pc < image->image_base + image->image_size)
67 printf(" pc=0x%zx", pc - image->image_base);
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020068 if (image->file_path)
69 printf(" '%pD'", image->file_path);
70 printf("\n");
71 return EFI_SUCCESS;
72}
73
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010074/**
75 * efi_print_image_infos() - print information about all loaded images
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020076 *
77 * @pc: program counter (use NULL to suppress offset output)
78 */
79void efi_print_image_infos(void *pc)
80{
81 struct efi_object *efiobj;
82 struct efi_handler *handler;
83
84 list_for_each_entry(efiobj, &efi_obj_list, link) {
85 list_for_each_entry(handler, &efiobj->protocols, link) {
86 if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
87 efi_print_image_info(
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020088 (struct efi_loaded_image_obj *)efiobj,
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020089 handler->protocol_interface, pc);
90 }
91 }
92 }
93}
94
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010095/**
96 * efi_loader_relocate() - relocate UEFI binary
97 *
98 * @rel: pointer to the relocation table
99 * @rel_size: size of the relocation table in bytes
100 * @efi_reloc: actual load address of the image
101 * @pref_address: preferred load address of the image
102 * Return: status code
103 */
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200104static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700105 unsigned long rel_size, void *efi_reloc,
106 unsigned long pref_address)
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100107{
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700108 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100109 const IMAGE_BASE_RELOCATION *end;
110 int i;
111
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700112 if (delta == 0)
113 return EFI_SUCCESS;
114
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100115 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
Heinrich Schuchardte9644cc2019-02-16 15:36:33 +0100116 while (rel < end && rel->SizeOfBlock) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100117 const uint16_t *relocs = (const uint16_t *)(rel + 1);
118 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
119 while (i--) {
Alexander Graf07032252016-08-18 23:45:18 +0200120 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100121 rel->VirtualAddress;
122 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100123 uint64_t *x64 = efi_reloc + offset;
124 uint32_t *x32 = efi_reloc + offset;
125 uint16_t *x16 = efi_reloc + offset;
126
127 switch (type) {
128 case IMAGE_REL_BASED_ABSOLUTE:
129 break;
130 case IMAGE_REL_BASED_HIGH:
131 *x16 += ((uint32_t)delta) >> 16;
132 break;
133 case IMAGE_REL_BASED_LOW:
134 *x16 += (uint16_t)delta;
135 break;
136 case IMAGE_REL_BASED_HIGHLOW:
137 *x32 += (uint32_t)delta;
138 break;
139 case IMAGE_REL_BASED_DIR64:
140 *x64 += (uint64_t)delta;
141 break;
Alexander Graf71f91202018-06-05 19:20:32 +0200142#ifdef __riscv
143 case IMAGE_REL_BASED_RISCV_HI20:
144 *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
145 (*x32 & 0x00000fff);
146 break;
147 case IMAGE_REL_BASED_RISCV_LOW12I:
148 case IMAGE_REL_BASED_RISCV_LOW12S:
149 /* We know that we're 4k aligned */
150 if (delta & 0xfff) {
151 printf("Unsupported reloc offset\n");
152 return EFI_LOAD_ERROR;
153 }
154 break;
155#endif
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100156 default:
157 printf("Unknown Relocation off %x type %x\n",
158 offset, type);
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200159 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100160 }
161 relocs++;
162 }
163 rel = (const IMAGE_BASE_RELOCATION *)relocs;
164 }
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200165 return EFI_SUCCESS;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100166}
167
168void __weak invalidate_icache_all(void)
169{
170 /* If the system doesn't support icache_all flush, cross our fingers */
171}
172
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +0100173/**
174 * efi_set_code_and_data_type() - determine the memory types to be used for code
175 * and data.
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100176 *
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +0100177 * @loaded_image_info: image descriptor
178 * @image_type: field Subsystem of the optional header for
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100179 * Windows specific field
180 */
181static void efi_set_code_and_data_type(
182 struct efi_loaded_image *loaded_image_info,
183 uint16_t image_type)
184{
185 switch (image_type) {
186 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
187 loaded_image_info->image_code_type = EFI_LOADER_CODE;
188 loaded_image_info->image_data_type = EFI_LOADER_DATA;
189 break;
190 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
191 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
192 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
193 break;
194 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt467fd7a2018-01-31 18:45:35 +0000195 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100196 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
197 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
198 break;
199 default:
200 printf("%s: invalid image type: %u\n", __func__, image_type);
201 /* Let's assume it is an application */
202 loaded_image_info->image_code_type = EFI_LOADER_CODE;
203 loaded_image_info->image_data_type = EFI_LOADER_DATA;
204 break;
205 }
206}
207
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100208/**
209 * efi_load_pe() - relocate EFI binary
210 *
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100211 * This function loads all sections from a PE binary into a newly reserved
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100212 * piece of memory. On success the entry point is returned as handle->entry.
213 *
214 * @handle: loaded image handle
215 * @efi: pointer to the EFI binary
216 * @loaded_image_info: loaded image protocol
217 * Return: status code
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100218 */
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100219efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,
220 struct efi_loaded_image *loaded_image_info)
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100221{
222 IMAGE_NT_HEADERS32 *nt;
223 IMAGE_DOS_HEADER *dos;
224 IMAGE_SECTION_HEADER *sections;
225 int num_sections;
226 void *efi_reloc;
227 int i;
228 const IMAGE_BASE_RELOCATION *rel;
229 unsigned long rel_size;
230 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700231 uint64_t image_base;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100232 unsigned long virt_size = 0;
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700233 int supported = 0;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100234
235 dos = efi;
236 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
237 printf("%s: Invalid DOS Signature\n", __func__);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100238 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100239 }
240
241 nt = (void *) ((char *)efi + dos->e_lfanew);
242 if (nt->Signature != IMAGE_NT_SIGNATURE) {
243 printf("%s: Invalid NT Signature\n", __func__);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100244 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100245 }
246
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700247 for (i = 0; machines[i]; i++)
248 if (machines[i] == nt->FileHeader.Machine) {
249 supported = 1;
250 break;
251 }
252
253 if (!supported) {
254 printf("%s: Machine type 0x%04x is not supported\n",
255 __func__, nt->FileHeader.Machine);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100256 return EFI_LOAD_ERROR;
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700257 }
258
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100259 /* Calculate upper virtual address boundary */
260 num_sections = nt->FileHeader.NumberOfSections;
261 sections = (void *)&nt->OptionalHeader +
262 nt->FileHeader.SizeOfOptionalHeader;
263
264 for (i = num_sections - 1; i >= 0; i--) {
265 IMAGE_SECTION_HEADER *sec = &sections[i];
266 virt_size = max_t(unsigned long, virt_size,
267 sec->VirtualAddress + sec->Misc.VirtualSize);
268 }
269
270 /* Read 32/64bit specific header bits */
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700271 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100272 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
273 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700274 image_base = opt->ImageBase;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100275 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt37587522019-05-01 20:07:04 +0200276 handle->image_type = opt->Subsystem;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100277 efi_reloc = efi_alloc(virt_size,
278 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100279 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100280 printf("%s: Could not allocate %lu bytes\n",
281 __func__, virt_size);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100282 return EFI_OUT_OF_RESOURCES;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100283 }
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100284 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100285 rel_size = opt->DataDirectory[rel_idx].Size;
286 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200287 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700288 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100289 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700290 image_base = opt->ImageBase;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100291 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt37587522019-05-01 20:07:04 +0200292 handle->image_type = opt->Subsystem;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100293 efi_reloc = efi_alloc(virt_size,
294 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100295 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100296 printf("%s: Could not allocate %lu bytes\n",
297 __func__, virt_size);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100298 return EFI_OUT_OF_RESOURCES;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100299 }
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100300 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100301 rel_size = opt->DataDirectory[rel_idx].Size;
302 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200303 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100304 } else {
305 printf("%s: Invalid optional header magic %x\n", __func__,
306 nt->OptionalHeader.Magic);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100307 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100308 }
309
AKASHI Takahiro068879a2018-10-11 04:09:58 -0700310 /* Copy PE headers */
311 memcpy(efi_reloc, efi, sizeof(*dos) + sizeof(*nt)
312 + nt->FileHeader.SizeOfOptionalHeader
313 + num_sections * sizeof(IMAGE_SECTION_HEADER));
314
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100315 /* Load sections into RAM */
316 for (i = num_sections - 1; i >= 0; i--) {
317 IMAGE_SECTION_HEADER *sec = &sections[i];
318 memset(efi_reloc + sec->VirtualAddress, 0,
319 sec->Misc.VirtualSize);
320 memcpy(efi_reloc + sec->VirtualAddress,
321 efi + sec->PointerToRawData,
322 sec->SizeOfRawData);
323 }
324
325 /* Run through relocations */
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700326 if (efi_loader_relocate(rel, rel_size, efi_reloc,
327 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200328 efi_free_pages((uintptr_t) efi_reloc,
329 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100330 return EFI_LOAD_ERROR;
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200331 }
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100332
333 /* Flush cache */
Simon Glassd25bb302016-11-07 08:47:04 -0700334 flush_cache((ulong)efi_reloc,
Alexander Graf59254d62018-04-23 07:59:47 +0200335 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100336 invalidate_icache_all();
337
338 /* Populate the loaded image interface bits */
AKASHI Takahiro068879a2018-10-11 04:09:58 -0700339 loaded_image_info->image_base = efi_reloc;
340 loaded_image_info->image_size = virt_size;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100341
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100342 return EFI_SUCCESS;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100343}