blob: e702afd87c1d8ae231d276c8b58ca0df9839a84c [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>
13#include <asm/global_data.h>
14
Rob Clarkc84c1102017-09-13 18:05:38 -040015const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010016const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
17const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
Rob Clark53142032017-09-13 18:05:34 -040018const efi_guid_t efi_simple_file_system_protocol_guid =
19 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
20const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010021
Ivan Gorinov749d5c12018-04-05 18:32:06 -070022static int machines[] = {
23#if defined(CONFIG_ARM64)
24 IMAGE_FILE_MACHINE_ARM64,
25#elif defined(CONFIG_ARM)
26 IMAGE_FILE_MACHINE_ARM,
27 IMAGE_FILE_MACHINE_THUMB,
28 IMAGE_FILE_MACHINE_ARMNT,
29#endif
30
31#if defined(CONFIG_X86_64)
32 IMAGE_FILE_MACHINE_AMD64,
33#elif defined(CONFIG_X86)
34 IMAGE_FILE_MACHINE_I386,
35#endif
36
37#if defined(CONFIG_CPU_RISCV_32)
38 IMAGE_FILE_MACHINE_RISCV32,
39#endif
40
41#if defined(CONFIG_CPU_RISCV_64)
42 IMAGE_FILE_MACHINE_RISCV64,
43#endif
44 0 };
45
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020046/*
47 * Print information about a loaded image.
48 *
49 * If the program counter is located within the image the offset to the base
50 * address is shown.
51 *
52 * @image: loaded image
53 * @pc: program counter (use NULL to suppress offset output)
54 * @return: status code
55 */
56efi_status_t efi_print_image_info(struct efi_loaded_image *image, void *pc)
57{
58 if (!image)
59 return EFI_INVALID_PARAMETER;
60 printf("UEFI image");
61 printf(" [0x%p:0x%p]",
62 image->reloc_base, image->reloc_base + image->reloc_size - 1);
63 if (pc && pc >= image->reloc_base &&
64 pc < image->reloc_base + image->reloc_size)
65 printf(" pc=0x%zx", pc - image->reloc_base);
66 if (image->file_path)
67 printf(" '%pD'", image->file_path);
68 printf("\n");
69 return EFI_SUCCESS;
70}
71
72/*
73 * Print information about all loaded images.
74 *
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(
86 handler->protocol_interface, pc);
87 }
88 }
89 }
90}
91
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +020092static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinovc7270bc2018-05-02 16:36:02 -070093 unsigned long rel_size, void *efi_reloc,
94 unsigned long pref_address)
Alexander Graf4e0c1c12016-03-04 01:09:58 +010095{
Ivan Gorinovc7270bc2018-05-02 16:36:02 -070096 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010097 const IMAGE_BASE_RELOCATION *end;
98 int i;
99
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700100 if (delta == 0)
101 return EFI_SUCCESS;
102
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100103 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
104 while (rel < end - 1 && rel->SizeOfBlock) {
105 const uint16_t *relocs = (const uint16_t *)(rel + 1);
106 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
107 while (i--) {
Alexander Graf07032252016-08-18 23:45:18 +0200108 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100109 rel->VirtualAddress;
110 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100111 uint64_t *x64 = efi_reloc + offset;
112 uint32_t *x32 = efi_reloc + offset;
113 uint16_t *x16 = efi_reloc + offset;
114
115 switch (type) {
116 case IMAGE_REL_BASED_ABSOLUTE:
117 break;
118 case IMAGE_REL_BASED_HIGH:
119 *x16 += ((uint32_t)delta) >> 16;
120 break;
121 case IMAGE_REL_BASED_LOW:
122 *x16 += (uint16_t)delta;
123 break;
124 case IMAGE_REL_BASED_HIGHLOW:
125 *x32 += (uint32_t)delta;
126 break;
127 case IMAGE_REL_BASED_DIR64:
128 *x64 += (uint64_t)delta;
129 break;
130 default:
131 printf("Unknown Relocation off %x type %x\n",
132 offset, type);
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200133 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100134 }
135 relocs++;
136 }
137 rel = (const IMAGE_BASE_RELOCATION *)relocs;
138 }
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200139 return EFI_SUCCESS;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100140}
141
142void __weak invalidate_icache_all(void)
143{
144 /* If the system doesn't support icache_all flush, cross our fingers */
145}
146
147/*
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100148 * Determine the memory types to be used for code and data.
149 *
150 * @loaded_image_info image descriptor
151 * @image_type field Subsystem of the optional header for
152 * Windows specific field
153 */
154static void efi_set_code_and_data_type(
155 struct efi_loaded_image *loaded_image_info,
156 uint16_t image_type)
157{
158 switch (image_type) {
159 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
160 loaded_image_info->image_code_type = EFI_LOADER_CODE;
161 loaded_image_info->image_data_type = EFI_LOADER_DATA;
162 break;
163 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
164 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
165 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
166 break;
167 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt467fd7a2018-01-31 18:45:35 +0000168 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100169 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
170 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
171 break;
172 default:
173 printf("%s: invalid image type: %u\n", __func__, image_type);
174 /* Let's assume it is an application */
175 loaded_image_info->image_code_type = EFI_LOADER_CODE;
176 loaded_image_info->image_data_type = EFI_LOADER_DATA;
177 break;
178 }
179}
180
181/*
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100182 * This function loads all sections from a PE binary into a newly reserved
183 * piece of memory. On successful load it then returns the entry point for
184 * the binary. Otherwise NULL.
185 */
186void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
187{
188 IMAGE_NT_HEADERS32 *nt;
189 IMAGE_DOS_HEADER *dos;
190 IMAGE_SECTION_HEADER *sections;
191 int num_sections;
192 void *efi_reloc;
193 int i;
194 const IMAGE_BASE_RELOCATION *rel;
195 unsigned long rel_size;
196 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
197 void *entry;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700198 uint64_t image_base;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100199 uint64_t image_size;
200 unsigned long virt_size = 0;
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700201 int supported = 0;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100202
203 dos = efi;
204 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
205 printf("%s: Invalid DOS Signature\n", __func__);
206 return NULL;
207 }
208
209 nt = (void *) ((char *)efi + dos->e_lfanew);
210 if (nt->Signature != IMAGE_NT_SIGNATURE) {
211 printf("%s: Invalid NT Signature\n", __func__);
212 return NULL;
213 }
214
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700215 for (i = 0; machines[i]; i++)
216 if (machines[i] == nt->FileHeader.Machine) {
217 supported = 1;
218 break;
219 }
220
221 if (!supported) {
222 printf("%s: Machine type 0x%04x is not supported\n",
223 __func__, nt->FileHeader.Machine);
224 return NULL;
225 }
226
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100227 /* Calculate upper virtual address boundary */
228 num_sections = nt->FileHeader.NumberOfSections;
229 sections = (void *)&nt->OptionalHeader +
230 nt->FileHeader.SizeOfOptionalHeader;
231
232 for (i = num_sections - 1; i >= 0; i--) {
233 IMAGE_SECTION_HEADER *sec = &sections[i];
234 virt_size = max_t(unsigned long, virt_size,
235 sec->VirtualAddress + sec->Misc.VirtualSize);
236 }
237
238 /* Read 32/64bit specific header bits */
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700239 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100240 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
241 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700242 image_base = opt->ImageBase;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100243 image_size = opt->SizeOfImage;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100244 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
245 efi_reloc = efi_alloc(virt_size,
246 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100247 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100248 printf("%s: Could not allocate %lu bytes\n",
249 __func__, virt_size);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100250 return NULL;
251 }
252 entry = efi_reloc + opt->AddressOfEntryPoint;
253 rel_size = opt->DataDirectory[rel_idx].Size;
254 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200255 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700256 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100257 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700258 image_base = opt->ImageBase;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100259 image_size = opt->SizeOfImage;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100260 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
261 efi_reloc = efi_alloc(virt_size,
262 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100263 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100264 printf("%s: Could not allocate %lu bytes\n",
265 __func__, virt_size);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100266 return NULL;
267 }
268 entry = efi_reloc + opt->AddressOfEntryPoint;
269 rel_size = opt->DataDirectory[rel_idx].Size;
270 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200271 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100272 } else {
273 printf("%s: Invalid optional header magic %x\n", __func__,
274 nt->OptionalHeader.Magic);
275 return NULL;
276 }
277
278 /* Load sections into RAM */
279 for (i = num_sections - 1; i >= 0; i--) {
280 IMAGE_SECTION_HEADER *sec = &sections[i];
281 memset(efi_reloc + sec->VirtualAddress, 0,
282 sec->Misc.VirtualSize);
283 memcpy(efi_reloc + sec->VirtualAddress,
284 efi + sec->PointerToRawData,
285 sec->SizeOfRawData);
286 }
287
288 /* Run through relocations */
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700289 if (efi_loader_relocate(rel, rel_size, efi_reloc,
290 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200291 efi_free_pages((uintptr_t) efi_reloc,
292 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
293 return NULL;
294 }
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100295
296 /* Flush cache */
Simon Glassd25bb302016-11-07 08:47:04 -0700297 flush_cache((ulong)efi_reloc,
Alexander Graf59254d62018-04-23 07:59:47 +0200298 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100299 invalidate_icache_all();
300
301 /* Populate the loaded image interface bits */
302 loaded_image_info->image_base = efi;
303 loaded_image_info->image_size = image_size;
Heinrich Schuchardt4f2102f2018-04-03 22:29:31 +0200304 loaded_image_info->reloc_base = efi_reloc;
305 loaded_image_info->reloc_size = virt_size;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100306
307 return entry;
308}