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> |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 13 | #include <malloc.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 14 | #include <pe.h> |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 15 | #include <sort.h> |
Heinrich Schuchardt | 2aa0942 | 2020-05-07 17:57:43 +0200 | [diff] [blame] | 16 | #include <crypto/pkcs7_parser.h> |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 17 | #include <linux/err.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 18 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 19 | const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID; |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 20 | const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID; |
| 21 | const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
| 22 | const efi_guid_t efi_guid_loaded_image_device_path = |
| 23 | EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 24 | const efi_guid_t efi_simple_file_system_protocol_guid = |
| 25 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
| 26 | const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 27 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 28 | static int machines[] = { |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 29 | #if defined(__aarch64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 30 | IMAGE_FILE_MACHINE_ARM64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 31 | #elif defined(__arm__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 32 | IMAGE_FILE_MACHINE_ARM, |
| 33 | IMAGE_FILE_MACHINE_THUMB, |
| 34 | IMAGE_FILE_MACHINE_ARMNT, |
| 35 | #endif |
| 36 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 37 | #if defined(__x86_64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 38 | IMAGE_FILE_MACHINE_AMD64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 39 | #elif defined(__i386__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 40 | IMAGE_FILE_MACHINE_I386, |
| 41 | #endif |
| 42 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 43 | #if defined(__riscv) && (__riscv_xlen == 32) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 44 | IMAGE_FILE_MACHINE_RISCV32, |
| 45 | #endif |
| 46 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 47 | #if defined(__riscv) && (__riscv_xlen == 64) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 48 | IMAGE_FILE_MACHINE_RISCV64, |
| 49 | #endif |
| 50 | 0 }; |
| 51 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 52 | /** |
| 53 | * efi_print_image_info() - print information about a loaded image |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 54 | * |
| 55 | * If the program counter is located within the image the offset to the base |
| 56 | * address is shown. |
| 57 | * |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 58 | * @obj: EFI object |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 59 | * @image: loaded image |
| 60 | * @pc: program counter (use NULL to suppress offset output) |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 61 | * Return: status code |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 62 | */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 63 | static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj, |
| 64 | struct efi_loaded_image *image, |
| 65 | void *pc) |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 66 | { |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 67 | printf("UEFI image"); |
| 68 | printf(" [0x%p:0x%p]", |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 69 | image->image_base, image->image_base + image->image_size - 1); |
| 70 | if (pc && pc >= image->image_base && |
| 71 | pc < image->image_base + image->image_size) |
| 72 | printf(" pc=0x%zx", pc - image->image_base); |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 73 | if (image->file_path) |
| 74 | printf(" '%pD'", image->file_path); |
| 75 | printf("\n"); |
| 76 | return EFI_SUCCESS; |
| 77 | } |
| 78 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 79 | /** |
| 80 | * efi_print_image_infos() - print information about all loaded images |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 81 | * |
| 82 | * @pc: program counter (use NULL to suppress offset output) |
| 83 | */ |
| 84 | void efi_print_image_infos(void *pc) |
| 85 | { |
| 86 | struct efi_object *efiobj; |
| 87 | struct efi_handler *handler; |
| 88 | |
| 89 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 90 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 91 | if (!guidcmp(handler->guid, &efi_guid_loaded_image)) { |
| 92 | efi_print_image_info( |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 93 | (struct efi_loaded_image_obj *)efiobj, |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 94 | handler->protocol_interface, pc); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 100 | /** |
| 101 | * efi_loader_relocate() - relocate UEFI binary |
| 102 | * |
| 103 | * @rel: pointer to the relocation table |
| 104 | * @rel_size: size of the relocation table in bytes |
| 105 | * @efi_reloc: actual load address of the image |
| 106 | * @pref_address: preferred load address of the image |
| 107 | * Return: status code |
| 108 | */ |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 109 | static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 110 | unsigned long rel_size, void *efi_reloc, |
| 111 | unsigned long pref_address) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 112 | { |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 113 | unsigned long delta = (unsigned long)efi_reloc - pref_address; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 114 | const IMAGE_BASE_RELOCATION *end; |
| 115 | int i; |
| 116 | |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 117 | if (delta == 0) |
| 118 | return EFI_SUCCESS; |
| 119 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 120 | end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); |
Heinrich Schuchardt | e9644cc | 2019-02-16 15:36:33 +0100 | [diff] [blame] | 121 | while (rel < end && rel->SizeOfBlock) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 122 | const uint16_t *relocs = (const uint16_t *)(rel + 1); |
| 123 | i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); |
| 124 | while (i--) { |
Alexander Graf | 0703225 | 2016-08-18 23:45:18 +0200 | [diff] [blame] | 125 | uint32_t offset = (uint32_t)(*relocs & 0xfff) + |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 126 | rel->VirtualAddress; |
| 127 | int type = *relocs >> EFI_PAGE_SHIFT; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 128 | uint64_t *x64 = efi_reloc + offset; |
| 129 | uint32_t *x32 = efi_reloc + offset; |
| 130 | uint16_t *x16 = efi_reloc + offset; |
| 131 | |
| 132 | switch (type) { |
| 133 | case IMAGE_REL_BASED_ABSOLUTE: |
| 134 | break; |
| 135 | case IMAGE_REL_BASED_HIGH: |
| 136 | *x16 += ((uint32_t)delta) >> 16; |
| 137 | break; |
| 138 | case IMAGE_REL_BASED_LOW: |
| 139 | *x16 += (uint16_t)delta; |
| 140 | break; |
| 141 | case IMAGE_REL_BASED_HIGHLOW: |
| 142 | *x32 += (uint32_t)delta; |
| 143 | break; |
| 144 | case IMAGE_REL_BASED_DIR64: |
| 145 | *x64 += (uint64_t)delta; |
| 146 | break; |
Alexander Graf | 71f9120 | 2018-06-05 19:20:32 +0200 | [diff] [blame] | 147 | #ifdef __riscv |
| 148 | case IMAGE_REL_BASED_RISCV_HI20: |
| 149 | *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) | |
| 150 | (*x32 & 0x00000fff); |
| 151 | break; |
| 152 | case IMAGE_REL_BASED_RISCV_LOW12I: |
| 153 | case IMAGE_REL_BASED_RISCV_LOW12S: |
| 154 | /* We know that we're 4k aligned */ |
| 155 | if (delta & 0xfff) { |
| 156 | printf("Unsupported reloc offset\n"); |
| 157 | return EFI_LOAD_ERROR; |
| 158 | } |
| 159 | break; |
| 160 | #endif |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 161 | default: |
| 162 | printf("Unknown Relocation off %x type %x\n", |
| 163 | offset, type); |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 164 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 165 | } |
| 166 | relocs++; |
| 167 | } |
| 168 | rel = (const IMAGE_BASE_RELOCATION *)relocs; |
| 169 | } |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 170 | return EFI_SUCCESS; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void __weak invalidate_icache_all(void) |
| 174 | { |
| 175 | /* If the system doesn't support icache_all flush, cross our fingers */ |
| 176 | } |
| 177 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 178 | /** |
| 179 | * efi_set_code_and_data_type() - determine the memory types to be used for code |
| 180 | * and data. |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 181 | * |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 182 | * @loaded_image_info: image descriptor |
| 183 | * @image_type: field Subsystem of the optional header for |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 184 | * Windows specific field |
| 185 | */ |
| 186 | static void efi_set_code_and_data_type( |
| 187 | struct efi_loaded_image *loaded_image_info, |
| 188 | uint16_t image_type) |
| 189 | { |
| 190 | switch (image_type) { |
| 191 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 192 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 193 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 194 | break; |
| 195 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 196 | loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE; |
| 197 | loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA; |
| 198 | break; |
| 199 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
Heinrich Schuchardt | 467fd7a | 2018-01-31 18:45:35 +0000 | [diff] [blame] | 200 | case IMAGE_SUBSYSTEM_EFI_ROM: |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 201 | loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE; |
| 202 | loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA; |
| 203 | break; |
| 204 | default: |
| 205 | printf("%s: invalid image type: %u\n", __func__, image_type); |
| 206 | /* Let's assume it is an application */ |
| 207 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 208 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 213 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 214 | /** |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 215 | * cmp_pe_section() - compare virtual addresses of two PE image sections |
| 216 | * @arg1: pointer to pointer to first section header |
| 217 | * @arg2: pointer to pointer to second section header |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 218 | * |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 219 | * Compare the virtual addresses of two sections of an portable executable. |
| 220 | * The arguments are defined as const void * to allow usage with qsort(). |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 221 | * |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 222 | * Return: -1 if the virtual address of arg1 is less than that of arg2, |
| 223 | * 0 if the virtual addresses are equal, 1 if the virtual address |
| 224 | * of arg1 is greater than that of arg2. |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 225 | */ |
| 226 | static int cmp_pe_section(const void *arg1, const void *arg2) |
| 227 | { |
| 228 | const IMAGE_SECTION_HEADER *section1, *section2; |
| 229 | |
| 230 | section1 = *((const IMAGE_SECTION_HEADER **)arg1); |
| 231 | section2 = *((const IMAGE_SECTION_HEADER **)arg2); |
| 232 | |
| 233 | if (section1->VirtualAddress < section2->VirtualAddress) |
| 234 | return -1; |
| 235 | else if (section1->VirtualAddress == section2->VirtualAddress) |
| 236 | return 0; |
| 237 | else |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | /** |
Heinrich Schuchardt | da6d607 | 2020-05-30 05:48:08 +0200 | [diff] [blame] | 242 | * efi_image_parse() - parse a PE image |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 243 | * @efi: Pointer to image |
| 244 | * @len: Size of @efi |
| 245 | * @regp: Pointer to a list of regions |
| 246 | * @auth: Pointer to a pointer to authentication data in PE |
| 247 | * @auth_len: Size of @auth |
| 248 | * |
| 249 | * Parse image binary in PE32(+) format, assuming that sanity of PE image |
| 250 | * has been checked by a caller. |
| 251 | * On success, an address of authentication data in @efi and its size will |
| 252 | * be returned in @auth and @auth_len, respectively. |
| 253 | * |
| 254 | * Return: true on success, false on error |
| 255 | */ |
| 256 | bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp, |
| 257 | WIN_CERTIFICATE **auth, size_t *auth_len) |
| 258 | { |
| 259 | struct efi_image_regions *regs; |
| 260 | IMAGE_DOS_HEADER *dos; |
| 261 | IMAGE_NT_HEADERS32 *nt; |
| 262 | IMAGE_SECTION_HEADER *sections, **sorted; |
| 263 | int num_regions, num_sections, i; |
| 264 | int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY; |
| 265 | u32 align, size, authsz, authoff; |
| 266 | size_t bytes_hashed; |
| 267 | |
| 268 | dos = (void *)efi; |
| 269 | nt = (void *)(efi + dos->e_lfanew); |
| 270 | |
| 271 | /* |
| 272 | * Count maximum number of regions to be digested. |
| 273 | * We don't have to have an exact number here. |
| 274 | * See efi_image_region_add()'s in parsing below. |
| 275 | */ |
| 276 | num_regions = 3; /* for header */ |
| 277 | num_regions += nt->FileHeader.NumberOfSections; |
| 278 | num_regions++; /* for extra */ |
| 279 | |
| 280 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions, |
| 281 | 1); |
| 282 | if (!regs) |
| 283 | goto err; |
| 284 | regs->max = num_regions; |
| 285 | |
| 286 | /* |
| 287 | * Collect data regions for hash calculation |
| 288 | * 1. File headers |
| 289 | */ |
| 290 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
| 291 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
| 292 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
| 293 | |
| 294 | /* Skip CheckSum */ |
| 295 | efi_image_region_add(regs, efi, &opt->CheckSum, 0); |
| 296 | if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { |
| 297 | efi_image_region_add(regs, |
AKASHI Takahiro | b69546f | 2020-05-08 14:51:59 +0900 | [diff] [blame] | 298 | &opt->Subsystem, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 299 | efi + opt->SizeOfHeaders, 0); |
| 300 | } else { |
| 301 | /* Skip Certificates Table */ |
| 302 | efi_image_region_add(regs, |
AKASHI Takahiro | b69546f | 2020-05-08 14:51:59 +0900 | [diff] [blame] | 303 | &opt->Subsystem, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 304 | &opt->DataDirectory[ctidx], 0); |
| 305 | efi_image_region_add(regs, |
| 306 | &opt->DataDirectory[ctidx] + 1, |
| 307 | efi + opt->SizeOfHeaders, 0); |
| 308 | } |
| 309 | |
| 310 | bytes_hashed = opt->SizeOfHeaders; |
| 311 | align = opt->FileAlignment; |
| 312 | authoff = opt->DataDirectory[ctidx].VirtualAddress; |
| 313 | authsz = opt->DataDirectory[ctidx].Size; |
| 314 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 315 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
| 316 | |
| 317 | efi_image_region_add(regs, efi, &opt->CheckSum, 0); |
AKASHI Takahiro | b69546f | 2020-05-08 14:51:59 +0900 | [diff] [blame] | 318 | efi_image_region_add(regs, &opt->Subsystem, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 319 | &opt->DataDirectory[ctidx], 0); |
| 320 | efi_image_region_add(regs, &opt->DataDirectory[ctidx] + 1, |
| 321 | efi + opt->SizeOfHeaders, 0); |
| 322 | |
| 323 | bytes_hashed = opt->SizeOfHeaders; |
| 324 | align = opt->FileAlignment; |
| 325 | authoff = opt->DataDirectory[ctidx].VirtualAddress; |
| 326 | authsz = opt->DataDirectory[ctidx].Size; |
| 327 | } else { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 328 | EFI_PRINT("%s: Invalid optional header magic %x\n", __func__, |
| 329 | nt->OptionalHeader.Magic); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 330 | goto err; |
| 331 | } |
| 332 | |
| 333 | /* 2. Sections */ |
| 334 | num_sections = nt->FileHeader.NumberOfSections; |
| 335 | sections = (void *)((uint8_t *)&nt->OptionalHeader + |
| 336 | nt->FileHeader.SizeOfOptionalHeader); |
| 337 | sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections); |
| 338 | if (!sorted) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 339 | EFI_PRINT("%s: Out of memory\n", __func__); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 340 | goto err; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Make sure the section list is in ascending order. |
| 345 | */ |
| 346 | for (i = 0; i < num_sections; i++) |
| 347 | sorted[i] = §ions[i]; |
| 348 | qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section); |
| 349 | |
| 350 | for (i = 0; i < num_sections; i++) { |
| 351 | if (!sorted[i]->SizeOfRawData) |
| 352 | continue; |
| 353 | |
| 354 | size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1); |
| 355 | efi_image_region_add(regs, efi + sorted[i]->PointerToRawData, |
| 356 | efi + sorted[i]->PointerToRawData + size, |
| 357 | 0); |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 358 | EFI_PRINT("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n", |
| 359 | i, sorted[i]->Name, |
| 360 | sorted[i]->PointerToRawData, |
| 361 | sorted[i]->PointerToRawData + size, |
| 362 | sorted[i]->VirtualAddress, |
| 363 | sorted[i]->VirtualAddress |
| 364 | + sorted[i]->Misc.VirtualSize); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 365 | |
| 366 | bytes_hashed += size; |
| 367 | } |
| 368 | free(sorted); |
| 369 | |
| 370 | /* 3. Extra data excluding Certificates Table */ |
| 371 | if (bytes_hashed + authsz < len) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 372 | EFI_PRINT("extra data for hash: %lu\n", |
| 373 | len - (bytes_hashed + authsz)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 374 | efi_image_region_add(regs, efi + bytes_hashed, |
| 375 | efi + len - authsz, 0); |
| 376 | } |
| 377 | |
| 378 | /* Return Certificates Table */ |
| 379 | if (authsz) { |
| 380 | if (len < authoff + authsz) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 381 | EFI_PRINT("%s: Size for auth too large: %u >= %zu\n", |
| 382 | __func__, authsz, len - authoff); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 383 | goto err; |
| 384 | } |
| 385 | if (authsz < sizeof(*auth)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 386 | EFI_PRINT("%s: Size for auth too small: %u < %zu\n", |
| 387 | __func__, authsz, sizeof(*auth)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 388 | goto err; |
| 389 | } |
| 390 | *auth = efi + authoff; |
| 391 | *auth_len = authsz; |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 392 | EFI_PRINT("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff, |
| 393 | authsz); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 394 | } else { |
| 395 | *auth = NULL; |
| 396 | *auth_len = 0; |
| 397 | } |
| 398 | |
| 399 | *regp = regs; |
| 400 | |
| 401 | return true; |
| 402 | |
| 403 | err: |
| 404 | free(regs); |
| 405 | |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | /** |
Heinrich Schuchardt | da6d607 | 2020-05-30 05:48:08 +0200 | [diff] [blame] | 410 | * efi_image_unsigned_authenticate() - authenticate unsigned image with |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 411 | * SHA256 hash |
| 412 | * @regs: List of regions to be verified |
| 413 | * |
| 414 | * If an image is not signed, it doesn't have a signature. In this case, |
| 415 | * its message digest is calculated and it will be compared with one of |
| 416 | * hash values stored in signature databases. |
| 417 | * |
| 418 | * Return: true if authenticated, false if not |
| 419 | */ |
| 420 | static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs) |
| 421 | { |
| 422 | struct efi_signature_store *db = NULL, *dbx = NULL; |
| 423 | bool ret = false; |
| 424 | |
| 425 | dbx = efi_sigstore_parse_sigdb(L"dbx"); |
| 426 | if (!dbx) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 427 | EFI_PRINT("Getting signature database(dbx) failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 428 | goto out; |
| 429 | } |
| 430 | |
| 431 | db = efi_sigstore_parse_sigdb(L"db"); |
| 432 | if (!db) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 433 | EFI_PRINT("Getting signature database(db) failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 434 | goto out; |
| 435 | } |
| 436 | |
| 437 | /* try black-list first */ |
| 438 | if (efi_signature_verify_with_sigdb(regs, NULL, dbx, NULL)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 439 | EFI_PRINT("Image is not signed and rejected by \"dbx\"\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 440 | goto out; |
| 441 | } |
| 442 | |
| 443 | /* try white-list */ |
| 444 | if (efi_signature_verify_with_sigdb(regs, NULL, db, NULL)) |
| 445 | ret = true; |
| 446 | else |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 447 | EFI_PRINT("Image is not signed and not found in \"db\" or \"dbx\"\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 448 | |
| 449 | out: |
| 450 | efi_sigstore_free(db); |
| 451 | efi_sigstore_free(dbx); |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | /** |
Heinrich Schuchardt | da6d607 | 2020-05-30 05:48:08 +0200 | [diff] [blame] | 457 | * efi_image_authenticate() - verify a signature of signed image |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 458 | * @efi: Pointer to image |
| 459 | * @efi_size: Size of @efi |
| 460 | * |
| 461 | * A signed image should have its signature stored in a table of its PE header. |
| 462 | * So if an image is signed and only if if its signature is verified using |
| 463 | * signature databases, an image is authenticated. |
| 464 | * If an image is not signed, its validity is checked by using |
| 465 | * efi_image_unsigned_authenticated(). |
| 466 | * TODO: |
| 467 | * When AuditMode==0, if the image's signature is not found in |
| 468 | * the authorized database, or is found in the forbidden database, |
| 469 | * the image will not be started and instead, information about it |
| 470 | * will be placed in this table. |
| 471 | * When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created |
| 472 | * in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found |
| 473 | * in the certificate table of every image that is validated. |
| 474 | * |
| 475 | * Return: true if authenticated, false if not |
| 476 | */ |
| 477 | static bool efi_image_authenticate(void *efi, size_t efi_size) |
| 478 | { |
| 479 | struct efi_image_regions *regs = NULL; |
| 480 | WIN_CERTIFICATE *wincerts = NULL, *wincert; |
| 481 | size_t wincerts_len; |
| 482 | struct pkcs7_message *msg = NULL; |
| 483 | struct efi_signature_store *db = NULL, *dbx = NULL; |
| 484 | struct x509_certificate *cert = NULL; |
| 485 | void *new_efi = NULL; |
| 486 | size_t new_efi_size; |
| 487 | bool ret = false; |
| 488 | |
| 489 | if (!efi_secure_boot_enabled()) |
| 490 | return true; |
| 491 | |
| 492 | /* |
| 493 | * Size must be 8-byte aligned and the trailing bytes must be |
| 494 | * zero'ed. Otherwise hash value may be incorrect. |
| 495 | */ |
| 496 | if (efi_size & 0x7) { |
| 497 | new_efi_size = (efi_size + 0x7) & ~0x7ULL; |
| 498 | new_efi = calloc(new_efi_size, 1); |
| 499 | if (!new_efi) |
| 500 | return false; |
| 501 | memcpy(new_efi, efi, efi_size); |
| 502 | efi = new_efi; |
| 503 | efi_size = new_efi_size; |
| 504 | } |
| 505 | |
| 506 | if (!efi_image_parse(efi, efi_size, ®s, &wincerts, |
| 507 | &wincerts_len)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 508 | EFI_PRINT("Parsing PE executable image failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 509 | goto err; |
| 510 | } |
| 511 | |
| 512 | if (!wincerts) { |
| 513 | /* The image is not signed */ |
| 514 | ret = efi_image_unsigned_authenticate(regs); |
| 515 | |
| 516 | goto err; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * verify signature using db and dbx |
| 521 | */ |
| 522 | db = efi_sigstore_parse_sigdb(L"db"); |
| 523 | if (!db) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 524 | EFI_PRINT("Getting signature database(db) failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 525 | goto err; |
| 526 | } |
| 527 | |
| 528 | dbx = efi_sigstore_parse_sigdb(L"dbx"); |
| 529 | if (!dbx) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 530 | EFI_PRINT("Getting signature database(dbx) failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 531 | goto err; |
| 532 | } |
| 533 | |
| 534 | /* go through WIN_CERTIFICATE list */ |
| 535 | for (wincert = wincerts; |
| 536 | (void *)wincert < (void *)wincerts + wincerts_len; |
| 537 | wincert = (void *)wincert + ALIGN(wincert->dwLength, 8)) { |
| 538 | if (wincert->dwLength < sizeof(*wincert)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 539 | EFI_PRINT("%s: dwLength too small: %u < %zu\n", |
| 540 | __func__, wincert->dwLength, |
| 541 | sizeof(*wincert)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 542 | goto err; |
| 543 | } |
| 544 | msg = pkcs7_parse_message((void *)wincert + sizeof(*wincert), |
| 545 | wincert->dwLength - sizeof(*wincert)); |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 546 | if (IS_ERR(msg)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 547 | EFI_PRINT("Parsing image's signature failed\n"); |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 548 | msg = NULL; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 549 | goto err; |
| 550 | } |
| 551 | |
| 552 | /* try black-list first */ |
| 553 | if (efi_signature_verify_with_sigdb(regs, msg, dbx, NULL)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 554 | EFI_PRINT("Signature was rejected by \"dbx\"\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 555 | goto err; |
| 556 | } |
| 557 | |
| 558 | if (!efi_signature_verify_signers(msg, dbx)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 559 | EFI_PRINT("Signer was rejected by \"dbx\"\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 560 | goto err; |
| 561 | } else { |
| 562 | ret = true; |
| 563 | } |
| 564 | |
| 565 | /* try white-list */ |
| 566 | if (!efi_signature_verify_with_sigdb(regs, msg, db, &cert)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 567 | EFI_PRINT("Verifying signature with \"db\" failed\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 568 | goto err; |
| 569 | } else { |
| 570 | ret = true; |
| 571 | } |
| 572 | |
| 573 | if (!efi_signature_verify_cert(cert, dbx)) { |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame^] | 574 | EFI_PRINT("Certificate was rejected by \"dbx\"\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 575 | goto err; |
| 576 | } else { |
| 577 | ret = true; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | err: |
| 582 | x509_free_certificate(cert); |
| 583 | efi_sigstore_free(db); |
| 584 | efi_sigstore_free(dbx); |
| 585 | pkcs7_free_message(msg); |
| 586 | free(regs); |
| 587 | free(new_efi); |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | #else |
| 592 | static bool efi_image_authenticate(void *efi, size_t efi_size) |
| 593 | { |
| 594 | return true; |
| 595 | } |
| 596 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 597 | |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 598 | /** |
| 599 | * efi_load_pe() - relocate EFI binary |
| 600 | * |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 601 | * 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] | 602 | * piece of memory. On success the entry point is returned as handle->entry. |
| 603 | * |
| 604 | * @handle: loaded image handle |
| 605 | * @efi: pointer to the EFI binary |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 606 | * @efi_size: size of @efi binary |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 607 | * @loaded_image_info: loaded image protocol |
| 608 | * Return: status code |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 609 | */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 610 | efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, |
| 611 | void *efi, size_t efi_size, |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 612 | struct efi_loaded_image *loaded_image_info) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 613 | { |
| 614 | IMAGE_NT_HEADERS32 *nt; |
| 615 | IMAGE_DOS_HEADER *dos; |
| 616 | IMAGE_SECTION_HEADER *sections; |
| 617 | int num_sections; |
| 618 | void *efi_reloc; |
| 619 | int i; |
| 620 | const IMAGE_BASE_RELOCATION *rel; |
| 621 | unsigned long rel_size; |
| 622 | int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 623 | uint64_t image_base; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 624 | unsigned long virt_size = 0; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 625 | int supported = 0; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 626 | efi_status_t ret; |
| 627 | |
| 628 | /* Sanity check for a file header */ |
| 629 | if (efi_size < sizeof(*dos)) { |
| 630 | printf("%s: Truncated DOS Header\n", __func__); |
| 631 | ret = EFI_LOAD_ERROR; |
| 632 | goto err; |
| 633 | } |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 634 | |
| 635 | dos = efi; |
| 636 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) { |
| 637 | printf("%s: Invalid DOS Signature\n", __func__); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 638 | ret = EFI_LOAD_ERROR; |
| 639 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Heinrich Schuchardt | 5605e66 | 2020-05-30 07:35:59 +0200 | [diff] [blame] | 642 | /* |
| 643 | * Check if the image section header fits into the file. Knowing that at |
| 644 | * least one section header follows we only need to check for the length |
| 645 | * of the 64bit header which is longer than the 32bit header. |
| 646 | */ |
| 647 | if (efi_size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS64)) { |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 648 | printf("%s: Invalid offset for Extended Header\n", __func__); |
| 649 | ret = EFI_LOAD_ERROR; |
| 650 | goto err; |
| 651 | } |
| 652 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 653 | nt = (void *) ((char *)efi + dos->e_lfanew); |
| 654 | if (nt->Signature != IMAGE_NT_SIGNATURE) { |
| 655 | printf("%s: Invalid NT Signature\n", __func__); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 656 | ret = EFI_LOAD_ERROR; |
| 657 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 660 | for (i = 0; machines[i]; i++) |
| 661 | if (machines[i] == nt->FileHeader.Machine) { |
| 662 | supported = 1; |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | if (!supported) { |
| 667 | printf("%s: Machine type 0x%04x is not supported\n", |
| 668 | __func__, nt->FileHeader.Machine); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 669 | ret = EFI_LOAD_ERROR; |
| 670 | goto err; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 673 | num_sections = nt->FileHeader.NumberOfSections; |
| 674 | sections = (void *)&nt->OptionalHeader + |
| 675 | nt->FileHeader.SizeOfOptionalHeader; |
| 676 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 677 | if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections |
| 678 | - efi)) { |
| 679 | printf("%s: Invalid number of sections: %d\n", |
| 680 | __func__, num_sections); |
| 681 | ret = EFI_LOAD_ERROR; |
| 682 | goto err; |
| 683 | } |
| 684 | |
| 685 | /* Authenticate an image */ |
| 686 | if (efi_image_authenticate(efi, efi_size)) |
| 687 | handle->auth_status = EFI_IMAGE_AUTH_PASSED; |
| 688 | else |
| 689 | handle->auth_status = EFI_IMAGE_AUTH_FAILED; |
| 690 | |
| 691 | /* Calculate upper virtual address boundary */ |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 692 | for (i = num_sections - 1; i >= 0; i--) { |
| 693 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
| 694 | virt_size = max_t(unsigned long, virt_size, |
| 695 | sec->VirtualAddress + sec->Misc.VirtualSize); |
| 696 | } |
| 697 | |
| 698 | /* Read 32/64bit specific header bits */ |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 699 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 700 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
| 701 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 702 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 703 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 704 | handle->image_type = opt->Subsystem; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 705 | efi_reloc = efi_alloc(virt_size, |
| 706 | loaded_image_info->image_code_type); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 707 | if (!efi_reloc) { |
Heinrich Schuchardt | b10f299 | 2017-12-22 19:16:57 +0100 | [diff] [blame] | 708 | printf("%s: Could not allocate %lu bytes\n", |
| 709 | __func__, virt_size); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 710 | ret = EFI_OUT_OF_RESOURCES; |
| 711 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 712 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 713 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 714 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 715 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
Heinrich Schuchardt | 4c07b8e | 2018-04-03 22:29:32 +0200 | [diff] [blame] | 716 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 717 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 718 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 719 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 720 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 721 | handle->image_type = opt->Subsystem; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 722 | efi_reloc = efi_alloc(virt_size, |
| 723 | loaded_image_info->image_code_type); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 724 | if (!efi_reloc) { |
Heinrich Schuchardt | b10f299 | 2017-12-22 19:16:57 +0100 | [diff] [blame] | 725 | printf("%s: Could not allocate %lu bytes\n", |
| 726 | __func__, virt_size); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 727 | ret = EFI_OUT_OF_RESOURCES; |
| 728 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 729 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 730 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 731 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 732 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
Heinrich Schuchardt | 4c07b8e | 2018-04-03 22:29:32 +0200 | [diff] [blame] | 733 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 734 | } else { |
| 735 | printf("%s: Invalid optional header magic %x\n", __func__, |
| 736 | nt->OptionalHeader.Magic); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 737 | ret = EFI_LOAD_ERROR; |
| 738 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 739 | } |
| 740 | |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 741 | /* Copy PE headers */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 742 | memcpy(efi_reloc, efi, |
| 743 | sizeof(*dos) |
| 744 | + sizeof(*nt) |
| 745 | + nt->FileHeader.SizeOfOptionalHeader |
| 746 | + num_sections * sizeof(IMAGE_SECTION_HEADER)); |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 747 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 748 | /* Load sections into RAM */ |
| 749 | for (i = num_sections - 1; i >= 0; i--) { |
| 750 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
| 751 | memset(efi_reloc + sec->VirtualAddress, 0, |
| 752 | sec->Misc.VirtualSize); |
| 753 | memcpy(efi_reloc + sec->VirtualAddress, |
| 754 | efi + sec->PointerToRawData, |
| 755 | sec->SizeOfRawData); |
| 756 | } |
| 757 | |
| 758 | /* Run through relocations */ |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 759 | if (efi_loader_relocate(rel, rel_size, efi_reloc, |
| 760 | (unsigned long)image_base) != EFI_SUCCESS) { |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 761 | efi_free_pages((uintptr_t) efi_reloc, |
| 762 | (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 763 | ret = EFI_LOAD_ERROR; |
| 764 | goto err; |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 765 | } |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 766 | |
| 767 | /* Flush cache */ |
Simon Glass | d25bb30 | 2016-11-07 08:47:04 -0700 | [diff] [blame] | 768 | flush_cache((ulong)efi_reloc, |
Alexander Graf | 59254d6 | 2018-04-23 07:59:47 +0200 | [diff] [blame] | 769 | ALIGN(virt_size, EFI_CACHELINE_SIZE)); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 770 | invalidate_icache_all(); |
| 771 | |
| 772 | /* Populate the loaded image interface bits */ |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 773 | loaded_image_info->image_base = efi_reloc; |
| 774 | loaded_image_info->image_size = virt_size; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 775 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 776 | if (handle->auth_status == EFI_IMAGE_AUTH_PASSED) |
| 777 | return EFI_SUCCESS; |
| 778 | else |
| 779 | return EFI_SECURITY_VIOLATION; |
| 780 | |
| 781 | err: |
| 782 | return ret; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 783 | } |