blob: fe66e7b9ffe1146a8bb27e28403649071c4bbd04 [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
10#include <common.h>
11#include <efi_loader.h>
12#include <pe.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010013
Rob Clark9975fe92017-09-13 18:05:38 -040014const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Alexander Grafcb149c62016-03-04 01:09:58 +010015const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
16const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
Rob Clark2a920802017-09-13 18:05:34 -040017const efi_guid_t efi_simple_file_system_protocol_guid =
18 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
19const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Grafcb149c62016-03-04 01:09:58 +010020
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070021static int machines[] = {
Alexander Grafb59f6972018-06-18 17:22:57 +020022#if defined(__aarch64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070023 IMAGE_FILE_MACHINE_ARM64,
Alexander Grafb59f6972018-06-18 17:22:57 +020024#elif defined(__arm__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070025 IMAGE_FILE_MACHINE_ARM,
26 IMAGE_FILE_MACHINE_THUMB,
27 IMAGE_FILE_MACHINE_ARMNT,
28#endif
29
Alexander Grafb59f6972018-06-18 17:22:57 +020030#if defined(__x86_64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070031 IMAGE_FILE_MACHINE_AMD64,
Alexander Grafb59f6972018-06-18 17:22:57 +020032#elif defined(__i386__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070033 IMAGE_FILE_MACHINE_I386,
34#endif
35
Alexander Grafb59f6972018-06-18 17:22:57 +020036#if defined(__riscv) && (__riscv_xlen == 32)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070037 IMAGE_FILE_MACHINE_RISCV32,
38#endif
39
Alexander Grafb59f6972018-06-18 17:22:57 +020040#if defined(__riscv) && (__riscv_xlen == 64)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070041 IMAGE_FILE_MACHINE_RISCV64,
42#endif
43 0 };
44
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010045/**
46 * efi_print_image_info() - print information about a loaded image
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020047 *
48 * If the program counter is located within the image the offset to the base
49 * address is shown.
50 *
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020051 * @obj: EFI object
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020052 * @image: loaded image
53 * @pc: program counter (use NULL to suppress offset output)
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010054 * Return: status code
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020055 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020056static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
57 struct efi_loaded_image *image,
58 void *pc)
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020059{
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020060 printf("UEFI image");
61 printf(" [0x%p:0x%p]",
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020062 obj->reloc_base, obj->reloc_base + obj->reloc_size - 1);
63 if (pc && pc >= obj->reloc_base &&
64 pc < obj->reloc_base + obj->reloc_size)
65 printf(" pc=0x%zx", pc - obj->reloc_base);
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020066 if (image->file_path)
67 printf(" '%pD'", image->file_path);
68 printf("\n");
69 return EFI_SUCCESS;
70}
71
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010072/**
73 * efi_print_image_infos() - print information about all loaded images
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020074 *
75 * @pc: program counter (use NULL to suppress offset output)
76 */
77void efi_print_image_infos(void *pc)
78{
79 struct efi_object *efiobj;
80 struct efi_handler *handler;
81
82 list_for_each_entry(efiobj, &efi_obj_list, link) {
83 list_for_each_entry(handler, &efiobj->protocols, link) {
84 if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
85 efi_print_image_info(
Heinrich Schuchardtc9828742018-09-23 17:21:51 +020086 (struct efi_loaded_image_obj *)efiobj,
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020087 handler->protocol_interface, pc);
88 }
89 }
90 }
91}
92
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010093/**
94 * efi_loader_relocate() - relocate UEFI binary
95 *
96 * @rel: pointer to the relocation table
97 * @rel_size: size of the relocation table in bytes
98 * @efi_reloc: actual load address of the image
99 * @pref_address: preferred load address of the image
100 * Return: status code
101 */
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200102static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700103 unsigned long rel_size, void *efi_reloc,
104 unsigned long pref_address)
Alexander Grafcb149c62016-03-04 01:09:58 +0100105{
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700106 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Grafcb149c62016-03-04 01:09:58 +0100107 const IMAGE_BASE_RELOCATION *end;
108 int i;
109
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700110 if (delta == 0)
111 return EFI_SUCCESS;
112
Alexander Grafcb149c62016-03-04 01:09:58 +0100113 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
Heinrich Schuchardt997fc122019-02-16 15:36:33 +0100114 while (rel < end && rel->SizeOfBlock) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100115 const uint16_t *relocs = (const uint16_t *)(rel + 1);
116 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
117 while (i--) {
Alexander Grafb1237c62016-08-18 23:45:18 +0200118 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Grafcb149c62016-03-04 01:09:58 +0100119 rel->VirtualAddress;
120 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Grafcb149c62016-03-04 01:09:58 +0100121 uint64_t *x64 = efi_reloc + offset;
122 uint32_t *x32 = efi_reloc + offset;
123 uint16_t *x16 = efi_reloc + offset;
124
125 switch (type) {
126 case IMAGE_REL_BASED_ABSOLUTE:
127 break;
128 case IMAGE_REL_BASED_HIGH:
129 *x16 += ((uint32_t)delta) >> 16;
130 break;
131 case IMAGE_REL_BASED_LOW:
132 *x16 += (uint16_t)delta;
133 break;
134 case IMAGE_REL_BASED_HIGHLOW:
135 *x32 += (uint32_t)delta;
136 break;
137 case IMAGE_REL_BASED_DIR64:
138 *x64 += (uint64_t)delta;
139 break;
Alexander Grafde452c02018-06-05 19:20:32 +0200140#ifdef __riscv
141 case IMAGE_REL_BASED_RISCV_HI20:
142 *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
143 (*x32 & 0x00000fff);
144 break;
145 case IMAGE_REL_BASED_RISCV_LOW12I:
146 case IMAGE_REL_BASED_RISCV_LOW12S:
147 /* We know that we're 4k aligned */
148 if (delta & 0xfff) {
149 printf("Unsupported reloc offset\n");
150 return EFI_LOAD_ERROR;
151 }
152 break;
153#endif
Alexander Grafcb149c62016-03-04 01:09:58 +0100154 default:
155 printf("Unknown Relocation off %x type %x\n",
156 offset, type);
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200157 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100158 }
159 relocs++;
160 }
161 rel = (const IMAGE_BASE_RELOCATION *)relocs;
162 }
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200163 return EFI_SUCCESS;
Alexander Grafcb149c62016-03-04 01:09:58 +0100164}
165
166void __weak invalidate_icache_all(void)
167{
168 /* If the system doesn't support icache_all flush, cross our fingers */
169}
170
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100171/**
172 * efi_set_code_and_data_type() - determine the memory types to be used for code
173 * and data.
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100174 *
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100175 * @loaded_image_info: image descriptor
176 * @image_type: field Subsystem of the optional header for
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100177 * Windows specific field
178 */
179static void efi_set_code_and_data_type(
180 struct efi_loaded_image *loaded_image_info,
181 uint16_t image_type)
182{
183 switch (image_type) {
184 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
185 loaded_image_info->image_code_type = EFI_LOADER_CODE;
186 loaded_image_info->image_data_type = EFI_LOADER_DATA;
187 break;
188 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
189 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
190 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
191 break;
192 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt268ec6e2018-01-31 18:45:35 +0000193 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100194 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
195 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
196 break;
197 default:
198 printf("%s: invalid image type: %u\n", __func__, image_type);
199 /* Let's assume it is an application */
200 loaded_image_info->image_code_type = EFI_LOADER_CODE;
201 loaded_image_info->image_data_type = EFI_LOADER_DATA;
202 break;
203 }
204}
205
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100206/**
207 * efi_load_pe() - relocate EFI binary
208 *
Alexander Grafcb149c62016-03-04 01:09:58 +0100209 * This function loads all sections from a PE binary into a newly reserved
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100210 * piece of memory. On success the entry point is returned as handle->entry.
211 *
212 * @handle: loaded image handle
213 * @efi: pointer to the EFI binary
214 * @loaded_image_info: loaded image protocol
215 * Return: status code
Alexander Grafcb149c62016-03-04 01:09:58 +0100216 */
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100217efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,
218 struct efi_loaded_image *loaded_image_info)
Alexander Grafcb149c62016-03-04 01:09:58 +0100219{
220 IMAGE_NT_HEADERS32 *nt;
221 IMAGE_DOS_HEADER *dos;
222 IMAGE_SECTION_HEADER *sections;
223 int num_sections;
224 void *efi_reloc;
225 int i;
226 const IMAGE_BASE_RELOCATION *rel;
227 unsigned long rel_size;
228 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700229 uint64_t image_base;
Alexander Grafcb149c62016-03-04 01:09:58 +0100230 uint64_t image_size;
231 unsigned long virt_size = 0;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700232 int supported = 0;
Alexander Grafcb149c62016-03-04 01:09:58 +0100233
234 dos = efi;
235 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
236 printf("%s: Invalid DOS Signature\n", __func__);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100237 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100238 }
239
240 nt = (void *) ((char *)efi + dos->e_lfanew);
241 if (nt->Signature != IMAGE_NT_SIGNATURE) {
242 printf("%s: Invalid NT Signature\n", __func__);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100243 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100244 }
245
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700246 for (i = 0; machines[i]; i++)
247 if (machines[i] == nt->FileHeader.Machine) {
248 supported = 1;
249 break;
250 }
251
252 if (!supported) {
253 printf("%s: Machine type 0x%04x is not supported\n",
254 __func__, nt->FileHeader.Machine);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100255 return EFI_LOAD_ERROR;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700256 }
257
Alexander Grafcb149c62016-03-04 01:09:58 +0100258 /* Calculate upper virtual address boundary */
259 num_sections = nt->FileHeader.NumberOfSections;
260 sections = (void *)&nt->OptionalHeader +
261 nt->FileHeader.SizeOfOptionalHeader;
262
263 for (i = num_sections - 1; i >= 0; i--) {
264 IMAGE_SECTION_HEADER *sec = &sections[i];
265 virt_size = max_t(unsigned long, virt_size,
266 sec->VirtualAddress + sec->Misc.VirtualSize);
267 }
268
269 /* Read 32/64bit specific header bits */
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700270 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100271 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
272 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700273 image_base = opt->ImageBase;
Alexander Grafcb149c62016-03-04 01:09:58 +0100274 image_size = opt->SizeOfImage;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100275 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
276 efi_reloc = efi_alloc(virt_size,
277 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100278 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100279 printf("%s: Could not allocate %lu bytes\n",
280 __func__, virt_size);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100281 return EFI_OUT_OF_RESOURCES;
Alexander Grafcb149c62016-03-04 01:09:58 +0100282 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100283 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100284 rel_size = opt->DataDirectory[rel_idx].Size;
285 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200286 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700287 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100288 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700289 image_base = opt->ImageBase;
Alexander Grafcb149c62016-03-04 01:09:58 +0100290 image_size = opt->SizeOfImage;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100291 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
292 efi_reloc = efi_alloc(virt_size,
293 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100294 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100295 printf("%s: Could not allocate %lu bytes\n",
296 __func__, virt_size);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100297 return EFI_OUT_OF_RESOURCES;
Alexander Grafcb149c62016-03-04 01:09:58 +0100298 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100299 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100300 rel_size = opt->DataDirectory[rel_idx].Size;
301 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200302 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Grafcb149c62016-03-04 01:09:58 +0100303 } else {
304 printf("%s: Invalid optional header magic %x\n", __func__,
305 nt->OptionalHeader.Magic);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100306 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100307 }
308
309 /* Load sections into RAM */
310 for (i = num_sections - 1; i >= 0; i--) {
311 IMAGE_SECTION_HEADER *sec = &sections[i];
312 memset(efi_reloc + sec->VirtualAddress, 0,
313 sec->Misc.VirtualSize);
314 memcpy(efi_reloc + sec->VirtualAddress,
315 efi + sec->PointerToRawData,
316 sec->SizeOfRawData);
317 }
318
319 /* Run through relocations */
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700320 if (efi_loader_relocate(rel, rel_size, efi_reloc,
321 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200322 efi_free_pages((uintptr_t) efi_reloc,
323 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100324 return EFI_LOAD_ERROR;
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200325 }
Alexander Grafcb149c62016-03-04 01:09:58 +0100326
327 /* Flush cache */
Simon Glassd0d90992016-11-07 08:47:04 -0700328 flush_cache((ulong)efi_reloc,
Alexander Graf89aea432018-04-23 07:59:47 +0200329 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Grafcb149c62016-03-04 01:09:58 +0100330 invalidate_icache_all();
331
332 /* Populate the loaded image interface bits */
333 loaded_image_info->image_base = efi;
334 loaded_image_info->image_size = image_size;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200335 handle->reloc_base = efi_reloc;
336 handle->reloc_size = virt_size;
Alexander Grafcb149c62016-03-04 01:09:58 +0100337
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100338 return EFI_SUCCESS;
Alexander Grafcb149c62016-03-04 01:09:58 +0100339}