Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI image loader |
| 4 | * |
| 5 | * based partly on wine code |
| 6 | * |
| 7 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
| 13 | #include <pe.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 14 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 15 | const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID; |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 16 | const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID; |
| 17 | const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
| 18 | const efi_guid_t efi_guid_loaded_image_device_path = |
| 19 | EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 20 | const efi_guid_t efi_simple_file_system_protocol_guid = |
| 21 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
| 22 | const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 23 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 24 | static int machines[] = { |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 25 | #if defined(__aarch64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 26 | IMAGE_FILE_MACHINE_ARM64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 27 | #elif defined(__arm__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 28 | IMAGE_FILE_MACHINE_ARM, |
| 29 | IMAGE_FILE_MACHINE_THUMB, |
| 30 | IMAGE_FILE_MACHINE_ARMNT, |
| 31 | #endif |
| 32 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 33 | #if defined(__x86_64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 34 | IMAGE_FILE_MACHINE_AMD64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 35 | #elif defined(__i386__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 36 | IMAGE_FILE_MACHINE_I386, |
| 37 | #endif |
| 38 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 39 | #if defined(__riscv) && (__riscv_xlen == 32) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 40 | IMAGE_FILE_MACHINE_RISCV32, |
| 41 | #endif |
| 42 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 43 | #if defined(__riscv) && (__riscv_xlen == 64) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 44 | IMAGE_FILE_MACHINE_RISCV64, |
| 45 | #endif |
| 46 | 0 }; |
| 47 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 48 | /** |
| 49 | * efi_print_image_info() - print information about a loaded image |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 50 | * |
| 51 | * If the program counter is located within the image the offset to the base |
| 52 | * address is shown. |
| 53 | * |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 54 | * @obj: EFI object |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 55 | * @image: loaded image |
| 56 | * @pc: program counter (use NULL to suppress offset output) |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 57 | * Return: status code |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 58 | */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 59 | static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj, |
| 60 | struct efi_loaded_image *image, |
| 61 | void *pc) |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 62 | { |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 63 | printf("UEFI image"); |
| 64 | printf(" [0x%p:0x%p]", |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 65 | image->image_base, image->image_base + image->image_size - 1); |
| 66 | if (pc && pc >= image->image_base && |
| 67 | pc < image->image_base + image->image_size) |
| 68 | printf(" pc=0x%zx", pc - image->image_base); |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 69 | if (image->file_path) |
| 70 | printf(" '%pD'", image->file_path); |
| 71 | printf("\n"); |
| 72 | return EFI_SUCCESS; |
| 73 | } |
| 74 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 75 | /** |
| 76 | * efi_print_image_infos() - print information about all loaded images |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 77 | * |
| 78 | * @pc: program counter (use NULL to suppress offset output) |
| 79 | */ |
| 80 | void efi_print_image_infos(void *pc) |
| 81 | { |
| 82 | struct efi_object *efiobj; |
| 83 | struct efi_handler *handler; |
| 84 | |
| 85 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 86 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 87 | if (!guidcmp(handler->guid, &efi_guid_loaded_image)) { |
| 88 | efi_print_image_info( |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 89 | (struct efi_loaded_image_obj *)efiobj, |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 90 | handler->protocol_interface, pc); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 96 | /** |
| 97 | * efi_loader_relocate() - relocate UEFI binary |
| 98 | * |
| 99 | * @rel: pointer to the relocation table |
| 100 | * @rel_size: size of the relocation table in bytes |
| 101 | * @efi_reloc: actual load address of the image |
| 102 | * @pref_address: preferred load address of the image |
| 103 | * Return: status code |
| 104 | */ |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 105 | static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 106 | unsigned long rel_size, void *efi_reloc, |
| 107 | unsigned long pref_address) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 108 | { |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 109 | unsigned long delta = (unsigned long)efi_reloc - pref_address; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 110 | const IMAGE_BASE_RELOCATION *end; |
| 111 | int i; |
| 112 | |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 113 | if (delta == 0) |
| 114 | return EFI_SUCCESS; |
| 115 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 116 | end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); |
Heinrich Schuchardt | e9644cc | 2019-02-16 15:36:33 +0100 | [diff] [blame] | 117 | while (rel < end && rel->SizeOfBlock) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 118 | const uint16_t *relocs = (const uint16_t *)(rel + 1); |
| 119 | i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); |
| 120 | while (i--) { |
Alexander Graf | 0703225 | 2016-08-18 23:45:18 +0200 | [diff] [blame] | 121 | uint32_t offset = (uint32_t)(*relocs & 0xfff) + |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 122 | rel->VirtualAddress; |
| 123 | int type = *relocs >> EFI_PAGE_SHIFT; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 124 | uint64_t *x64 = efi_reloc + offset; |
| 125 | uint32_t *x32 = efi_reloc + offset; |
| 126 | uint16_t *x16 = efi_reloc + offset; |
| 127 | |
| 128 | switch (type) { |
| 129 | case IMAGE_REL_BASED_ABSOLUTE: |
| 130 | break; |
| 131 | case IMAGE_REL_BASED_HIGH: |
| 132 | *x16 += ((uint32_t)delta) >> 16; |
| 133 | break; |
| 134 | case IMAGE_REL_BASED_LOW: |
| 135 | *x16 += (uint16_t)delta; |
| 136 | break; |
| 137 | case IMAGE_REL_BASED_HIGHLOW: |
| 138 | *x32 += (uint32_t)delta; |
| 139 | break; |
| 140 | case IMAGE_REL_BASED_DIR64: |
| 141 | *x64 += (uint64_t)delta; |
| 142 | break; |
Alexander Graf | 71f9120 | 2018-06-05 19:20:32 +0200 | [diff] [blame] | 143 | #ifdef __riscv |
| 144 | case IMAGE_REL_BASED_RISCV_HI20: |
| 145 | *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) | |
| 146 | (*x32 & 0x00000fff); |
| 147 | break; |
| 148 | case IMAGE_REL_BASED_RISCV_LOW12I: |
| 149 | case IMAGE_REL_BASED_RISCV_LOW12S: |
| 150 | /* We know that we're 4k aligned */ |
| 151 | if (delta & 0xfff) { |
| 152 | printf("Unsupported reloc offset\n"); |
| 153 | return EFI_LOAD_ERROR; |
| 154 | } |
| 155 | break; |
| 156 | #endif |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 157 | default: |
| 158 | printf("Unknown Relocation off %x type %x\n", |
| 159 | offset, type); |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 160 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 161 | } |
| 162 | relocs++; |
| 163 | } |
| 164 | rel = (const IMAGE_BASE_RELOCATION *)relocs; |
| 165 | } |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 166 | return EFI_SUCCESS; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void __weak invalidate_icache_all(void) |
| 170 | { |
| 171 | /* If the system doesn't support icache_all flush, cross our fingers */ |
| 172 | } |
| 173 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 174 | /** |
| 175 | * efi_set_code_and_data_type() - determine the memory types to be used for code |
| 176 | * and data. |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 177 | * |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 178 | * @loaded_image_info: image descriptor |
| 179 | * @image_type: field Subsystem of the optional header for |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 180 | * Windows specific field |
| 181 | */ |
| 182 | static void efi_set_code_and_data_type( |
| 183 | struct efi_loaded_image *loaded_image_info, |
| 184 | uint16_t image_type) |
| 185 | { |
| 186 | switch (image_type) { |
| 187 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 188 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 189 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 190 | break; |
| 191 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 192 | loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE; |
| 193 | loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA; |
| 194 | break; |
| 195 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
Heinrich Schuchardt | 467fd7a | 2018-01-31 18:45:35 +0000 | [diff] [blame] | 196 | case IMAGE_SUBSYSTEM_EFI_ROM: |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 197 | loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE; |
| 198 | loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA; |
| 199 | break; |
| 200 | default: |
| 201 | printf("%s: invalid image type: %u\n", __func__, image_type); |
| 202 | /* Let's assume it is an application */ |
| 203 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 204 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 209 | /** |
| 210 | * efi_load_pe() - relocate EFI binary |
| 211 | * |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 212 | * This function loads all sections from a PE binary into a newly reserved |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 213 | * piece of memory. On success the entry point is returned as handle->entry. |
| 214 | * |
| 215 | * @handle: loaded image handle |
| 216 | * @efi: pointer to the EFI binary |
| 217 | * @loaded_image_info: loaded image protocol |
| 218 | * Return: status code |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 219 | */ |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 220 | efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, |
| 221 | struct efi_loaded_image *loaded_image_info) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 222 | { |
| 223 | IMAGE_NT_HEADERS32 *nt; |
| 224 | IMAGE_DOS_HEADER *dos; |
| 225 | IMAGE_SECTION_HEADER *sections; |
| 226 | int num_sections; |
| 227 | void *efi_reloc; |
| 228 | int i; |
| 229 | const IMAGE_BASE_RELOCATION *rel; |
| 230 | unsigned long rel_size; |
| 231 | int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 232 | uint64_t image_base; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 233 | unsigned long virt_size = 0; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 234 | int supported = 0; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 235 | |
| 236 | dos = efi; |
| 237 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) { |
| 238 | printf("%s: Invalid DOS Signature\n", __func__); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 239 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | nt = (void *) ((char *)efi + dos->e_lfanew); |
| 243 | if (nt->Signature != IMAGE_NT_SIGNATURE) { |
| 244 | printf("%s: Invalid NT Signature\n", __func__); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 245 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 248 | for (i = 0; machines[i]; i++) |
| 249 | if (machines[i] == nt->FileHeader.Machine) { |
| 250 | supported = 1; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | if (!supported) { |
| 255 | printf("%s: Machine type 0x%04x is not supported\n", |
| 256 | __func__, nt->FileHeader.Machine); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 257 | return EFI_LOAD_ERROR; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 260 | /* Calculate upper virtual address boundary */ |
| 261 | num_sections = nt->FileHeader.NumberOfSections; |
| 262 | sections = (void *)&nt->OptionalHeader + |
| 263 | nt->FileHeader.SizeOfOptionalHeader; |
| 264 | |
| 265 | for (i = num_sections - 1; i >= 0; i--) { |
| 266 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
| 267 | virt_size = max_t(unsigned long, virt_size, |
| 268 | sec->VirtualAddress + sec->Misc.VirtualSize); |
| 269 | } |
| 270 | |
| 271 | /* Read 32/64bit specific header bits */ |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 272 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 273 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
| 274 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 275 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 276 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 277 | handle->image_type = opt->Subsystem; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 278 | efi_reloc = efi_alloc(virt_size, |
| 279 | loaded_image_info->image_code_type); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 280 | if (!efi_reloc) { |
Heinrich Schuchardt | b10f299 | 2017-12-22 19:16:57 +0100 | [diff] [blame] | 281 | printf("%s: Could not allocate %lu bytes\n", |
| 282 | __func__, virt_size); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 283 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 284 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 285 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 286 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 287 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
Heinrich Schuchardt | 4c07b8e | 2018-04-03 22:29:32 +0200 | [diff] [blame] | 288 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 289 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 290 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 291 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 292 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 293 | handle->image_type = opt->Subsystem; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 294 | efi_reloc = efi_alloc(virt_size, |
| 295 | loaded_image_info->image_code_type); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 296 | if (!efi_reloc) { |
Heinrich Schuchardt | b10f299 | 2017-12-22 19:16:57 +0100 | [diff] [blame] | 297 | printf("%s: Could not allocate %lu bytes\n", |
| 298 | __func__, virt_size); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 299 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 300 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 301 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 302 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 303 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
Heinrich Schuchardt | 4c07b8e | 2018-04-03 22:29:32 +0200 | [diff] [blame] | 304 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 305 | } else { |
| 306 | printf("%s: Invalid optional header magic %x\n", __func__, |
| 307 | nt->OptionalHeader.Magic); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 308 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 309 | } |
| 310 | |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 311 | /* Copy PE headers */ |
| 312 | memcpy(efi_reloc, efi, sizeof(*dos) + sizeof(*nt) |
| 313 | + nt->FileHeader.SizeOfOptionalHeader |
| 314 | + num_sections * sizeof(IMAGE_SECTION_HEADER)); |
| 315 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 316 | /* Load sections into RAM */ |
| 317 | for (i = num_sections - 1; i >= 0; i--) { |
| 318 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
| 319 | memset(efi_reloc + sec->VirtualAddress, 0, |
| 320 | sec->Misc.VirtualSize); |
| 321 | memcpy(efi_reloc + sec->VirtualAddress, |
| 322 | efi + sec->PointerToRawData, |
| 323 | sec->SizeOfRawData); |
| 324 | } |
| 325 | |
| 326 | /* Run through relocations */ |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 327 | if (efi_loader_relocate(rel, rel_size, efi_reloc, |
| 328 | (unsigned long)image_base) != EFI_SUCCESS) { |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 329 | efi_free_pages((uintptr_t) efi_reloc, |
| 330 | (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 331 | return EFI_LOAD_ERROR; |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 332 | } |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 333 | |
| 334 | /* Flush cache */ |
Simon Glass | d25bb30 | 2016-11-07 08:47:04 -0700 | [diff] [blame] | 335 | flush_cache((ulong)efi_reloc, |
Alexander Graf | 59254d6 | 2018-04-23 07:59:47 +0200 | [diff] [blame] | 336 | ALIGN(virt_size, EFI_CACHELINE_SIZE)); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 337 | invalidate_icache_all(); |
| 338 | |
| 339 | /* Populate the loaded image interface bits */ |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 340 | loaded_image_info->image_base = efi_reloc; |
| 341 | loaded_image_info->image_size = virt_size; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 342 | |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 343 | return EFI_SUCCESS; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 344 | } |