Merge branch 'master' of git://git.denx.de/u-boot-sh
[pandora-u-boot.git] / lib / efi_loader / efi_image_loader.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI image loader
4  *
5  *  based partly on wine code
6  *
7  *  Copyright (c) 2016 Alexander Graf
8  */
9
10 #include <common.h>
11 #include <efi_loader.h>
12 #include <pe.h>
13 #include <asm/global_data.h>
14
15 const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
16 const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
17 const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
18 const efi_guid_t efi_simple_file_system_protocol_guid =
19                 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
20 const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
21
22 static 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
46 /*
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  */
56 efi_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  */
77 void 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
92 static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
93                         unsigned long rel_size, void *efi_reloc)
94 {
95         const IMAGE_BASE_RELOCATION *end;
96         int i;
97
98         end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
99         while (rel < end - 1 && rel->SizeOfBlock) {
100                 const uint16_t *relocs = (const uint16_t *)(rel + 1);
101                 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
102                 while (i--) {
103                         uint32_t offset = (uint32_t)(*relocs & 0xfff) +
104                                           rel->VirtualAddress;
105                         int type = *relocs >> EFI_PAGE_SHIFT;
106                         unsigned long delta = (unsigned long)efi_reloc;
107                         uint64_t *x64 = efi_reloc + offset;
108                         uint32_t *x32 = efi_reloc + offset;
109                         uint16_t *x16 = efi_reloc + offset;
110
111                         switch (type) {
112                         case IMAGE_REL_BASED_ABSOLUTE:
113                                 break;
114                         case IMAGE_REL_BASED_HIGH:
115                                 *x16 += ((uint32_t)delta) >> 16;
116                                 break;
117                         case IMAGE_REL_BASED_LOW:
118                                 *x16 += (uint16_t)delta;
119                                 break;
120                         case IMAGE_REL_BASED_HIGHLOW:
121                                 *x32 += (uint32_t)delta;
122                                 break;
123                         case IMAGE_REL_BASED_DIR64:
124                                 *x64 += (uint64_t)delta;
125                                 break;
126                         default:
127                                 printf("Unknown Relocation off %x type %x\n",
128                                        offset, type);
129                                 return EFI_LOAD_ERROR;
130                         }
131                         relocs++;
132                 }
133                 rel = (const IMAGE_BASE_RELOCATION *)relocs;
134         }
135         return EFI_SUCCESS;
136 }
137
138 void __weak invalidate_icache_all(void)
139 {
140         /* If the system doesn't support icache_all flush, cross our fingers */
141 }
142
143 /*
144  * Determine the memory types to be used for code and data.
145  *
146  * @loaded_image_info   image descriptor
147  * @image_type          field Subsystem of the optional header for
148  *                      Windows specific field
149  */
150 static void efi_set_code_and_data_type(
151                         struct efi_loaded_image *loaded_image_info,
152                         uint16_t image_type)
153 {
154         switch (image_type) {
155         case IMAGE_SUBSYSTEM_EFI_APPLICATION:
156                 loaded_image_info->image_code_type = EFI_LOADER_CODE;
157                 loaded_image_info->image_data_type = EFI_LOADER_DATA;
158                 break;
159         case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
160                 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
161                 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
162                 break;
163         case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
164         case IMAGE_SUBSYSTEM_EFI_ROM:
165                 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
166                 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
167                 break;
168         default:
169                 printf("%s: invalid image type: %u\n", __func__, image_type);
170                 /* Let's assume it is an application */
171                 loaded_image_info->image_code_type = EFI_LOADER_CODE;
172                 loaded_image_info->image_data_type = EFI_LOADER_DATA;
173                 break;
174         }
175 }
176
177 /*
178  * This function loads all sections from a PE binary into a newly reserved
179  * piece of memory. On successful load it then returns the entry point for
180  * the binary. Otherwise NULL.
181  */
182 void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
183 {
184         IMAGE_NT_HEADERS32 *nt;
185         IMAGE_DOS_HEADER *dos;
186         IMAGE_SECTION_HEADER *sections;
187         int num_sections;
188         void *efi_reloc;
189         int i;
190         const IMAGE_BASE_RELOCATION *rel;
191         unsigned long rel_size;
192         int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
193         void *entry;
194         uint64_t image_size;
195         unsigned long virt_size = 0;
196         int supported = 0;
197
198         dos = efi;
199         if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
200                 printf("%s: Invalid DOS Signature\n", __func__);
201                 return NULL;
202         }
203
204         nt = (void *) ((char *)efi + dos->e_lfanew);
205         if (nt->Signature != IMAGE_NT_SIGNATURE) {
206                 printf("%s: Invalid NT Signature\n", __func__);
207                 return NULL;
208         }
209
210         for (i = 0; machines[i]; i++)
211                 if (machines[i] == nt->FileHeader.Machine) {
212                         supported = 1;
213                         break;
214                 }
215
216         if (!supported) {
217                 printf("%s: Machine type 0x%04x is not supported\n",
218                        __func__, nt->FileHeader.Machine);
219                 return NULL;
220         }
221
222         /* Calculate upper virtual address boundary */
223         num_sections = nt->FileHeader.NumberOfSections;
224         sections = (void *)&nt->OptionalHeader +
225                             nt->FileHeader.SizeOfOptionalHeader;
226
227         for (i = num_sections - 1; i >= 0; i--) {
228                 IMAGE_SECTION_HEADER *sec = &sections[i];
229                 virt_size = max_t(unsigned long, virt_size,
230                                   sec->VirtualAddress + sec->Misc.VirtualSize);
231         }
232
233         /* Read 32/64bit specific header bits */
234         if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
235                 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
236                 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
237                 image_size = opt->SizeOfImage;
238                 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
239                 efi_reloc = efi_alloc(virt_size,
240                                       loaded_image_info->image_code_type);
241                 if (!efi_reloc) {
242                         printf("%s: Could not allocate %lu bytes\n",
243                                __func__, virt_size);
244                         return NULL;
245                 }
246                 entry = efi_reloc + opt->AddressOfEntryPoint;
247                 rel_size = opt->DataDirectory[rel_idx].Size;
248                 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
249                 virt_size = ALIGN(virt_size, opt->SectionAlignment);
250         } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
251                 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
252                 image_size = opt->SizeOfImage;
253                 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
254                 efi_reloc = efi_alloc(virt_size,
255                                       loaded_image_info->image_code_type);
256                 if (!efi_reloc) {
257                         printf("%s: Could not allocate %lu bytes\n",
258                                __func__, virt_size);
259                         return NULL;
260                 }
261                 entry = efi_reloc + opt->AddressOfEntryPoint;
262                 rel_size = opt->DataDirectory[rel_idx].Size;
263                 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
264                 virt_size = ALIGN(virt_size, opt->SectionAlignment);
265         } else {
266                 printf("%s: Invalid optional header magic %x\n", __func__,
267                        nt->OptionalHeader.Magic);
268                 return NULL;
269         }
270
271         /* Load sections into RAM */
272         for (i = num_sections - 1; i >= 0; i--) {
273                 IMAGE_SECTION_HEADER *sec = &sections[i];
274                 memset(efi_reloc + sec->VirtualAddress, 0,
275                        sec->Misc.VirtualSize);
276                 memcpy(efi_reloc + sec->VirtualAddress,
277                        efi + sec->PointerToRawData,
278                        sec->SizeOfRawData);
279         }
280
281         /* Run through relocations */
282         if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
283                 efi_free_pages((uintptr_t) efi_reloc,
284                                (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
285                 return NULL;
286         }
287
288         /* Flush cache */
289         flush_cache((ulong)efi_reloc,
290                     ALIGN(virt_size, EFI_CACHELINE_SIZE));
291         invalidate_icache_all();
292
293         /* Populate the loaded image interface bits */
294         loaded_image_info->image_base = efi;
295         loaded_image_info->image_size = image_size;
296         loaded_image_info->reloc_base = efi_reloc;
297         loaded_image_info->reloc_size = virt_size;
298
299         return entry;
300 }