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