1cb149c66SAlexander Graf /* 2cb149c66SAlexander Graf * EFI image loader 3cb149c66SAlexander Graf * 4cb149c66SAlexander Graf * based partly on wine code 5cb149c66SAlexander Graf * 6cb149c66SAlexander Graf * Copyright (c) 2016 Alexander Graf 7cb149c66SAlexander Graf * 8cb149c66SAlexander Graf * SPDX-License-Identifier: GPL-2.0+ 9cb149c66SAlexander Graf */ 10cb149c66SAlexander Graf 11cb149c66SAlexander Graf #include <common.h> 12cb149c66SAlexander Graf #include <efi_loader.h> 13cb149c66SAlexander Graf #include <pe.h> 14cb149c66SAlexander Graf #include <asm/global_data.h> 15cb149c66SAlexander Graf 16cb149c66SAlexander Graf DECLARE_GLOBAL_DATA_PTR; 17cb149c66SAlexander Graf 18cb149c66SAlexander Graf const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID; 19cb149c66SAlexander Graf const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID; 20cb149c66SAlexander Graf 21cb149c66SAlexander Graf efi_status_t EFIAPI efi_return_handle(void *handle, efi_guid_t *protocol, 22cb149c66SAlexander Graf void **protocol_interface, void *agent_handle, 23cb149c66SAlexander Graf void *controller_handle, uint32_t attributes) 24cb149c66SAlexander Graf { 25cb149c66SAlexander Graf *protocol_interface = handle; 261cd29f0aSAlexander Graf return EFI_SUCCESS; 27cb149c66SAlexander Graf } 28cb149c66SAlexander Graf 29cb149c66SAlexander Graf static void efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, 30cb149c66SAlexander Graf unsigned long rel_size, void *efi_reloc) 31cb149c66SAlexander Graf { 32cb149c66SAlexander Graf const IMAGE_BASE_RELOCATION *end; 33cb149c66SAlexander Graf int i; 34cb149c66SAlexander Graf 35cb149c66SAlexander Graf end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); 36cb149c66SAlexander Graf while (rel < end - 1 && rel->SizeOfBlock) { 37cb149c66SAlexander Graf const uint16_t *relocs = (const uint16_t *)(rel + 1); 38cb149c66SAlexander Graf i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); 39cb149c66SAlexander Graf while (i--) { 40*b1237c6eSAlexander Graf uint32_t offset = (uint32_t)(*relocs & 0xfff) + 41cb149c66SAlexander Graf rel->VirtualAddress; 42cb149c66SAlexander Graf int type = *relocs >> EFI_PAGE_SHIFT; 43cb149c66SAlexander Graf unsigned long delta = (unsigned long)efi_reloc; 44cb149c66SAlexander Graf uint64_t *x64 = efi_reloc + offset; 45cb149c66SAlexander Graf uint32_t *x32 = efi_reloc + offset; 46cb149c66SAlexander Graf uint16_t *x16 = efi_reloc + offset; 47cb149c66SAlexander Graf 48cb149c66SAlexander Graf switch (type) { 49cb149c66SAlexander Graf case IMAGE_REL_BASED_ABSOLUTE: 50cb149c66SAlexander Graf break; 51cb149c66SAlexander Graf case IMAGE_REL_BASED_HIGH: 52cb149c66SAlexander Graf *x16 += ((uint32_t)delta) >> 16; 53cb149c66SAlexander Graf break; 54cb149c66SAlexander Graf case IMAGE_REL_BASED_LOW: 55cb149c66SAlexander Graf *x16 += (uint16_t)delta; 56cb149c66SAlexander Graf break; 57cb149c66SAlexander Graf case IMAGE_REL_BASED_HIGHLOW: 58cb149c66SAlexander Graf *x32 += (uint32_t)delta; 59cb149c66SAlexander Graf break; 60cb149c66SAlexander Graf case IMAGE_REL_BASED_DIR64: 61cb149c66SAlexander Graf *x64 += (uint64_t)delta; 62cb149c66SAlexander Graf break; 63cb149c66SAlexander Graf default: 64cb149c66SAlexander Graf printf("Unknown Relocation off %x type %x\n", 65cb149c66SAlexander Graf offset, type); 66cb149c66SAlexander Graf } 67cb149c66SAlexander Graf relocs++; 68cb149c66SAlexander Graf } 69cb149c66SAlexander Graf rel = (const IMAGE_BASE_RELOCATION *)relocs; 70cb149c66SAlexander Graf } 71cb149c66SAlexander Graf } 72cb149c66SAlexander Graf 73cb149c66SAlexander Graf void __weak invalidate_icache_all(void) 74cb149c66SAlexander Graf { 75cb149c66SAlexander Graf /* If the system doesn't support icache_all flush, cross our fingers */ 76cb149c66SAlexander Graf } 77cb149c66SAlexander Graf 78cb149c66SAlexander Graf /* 79cb149c66SAlexander Graf * This function loads all sections from a PE binary into a newly reserved 80cb149c66SAlexander Graf * piece of memory. On successful load it then returns the entry point for 81cb149c66SAlexander Graf * the binary. Otherwise NULL. 82cb149c66SAlexander Graf */ 83cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info) 84cb149c66SAlexander Graf { 85cb149c66SAlexander Graf IMAGE_NT_HEADERS32 *nt; 86cb149c66SAlexander Graf IMAGE_DOS_HEADER *dos; 87cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sections; 88cb149c66SAlexander Graf int num_sections; 89cb149c66SAlexander Graf void *efi_reloc; 90cb149c66SAlexander Graf int i; 91cb149c66SAlexander Graf const IMAGE_BASE_RELOCATION *rel; 92cb149c66SAlexander Graf unsigned long rel_size; 93cb149c66SAlexander Graf int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; 94cb149c66SAlexander Graf void *entry; 95cb149c66SAlexander Graf uint64_t image_size; 96cb149c66SAlexander Graf unsigned long virt_size = 0; 97cb149c66SAlexander Graf bool can_run_nt64 = true; 98cb149c66SAlexander Graf bool can_run_nt32 = true; 99cb149c66SAlexander Graf 100cb149c66SAlexander Graf #if defined(CONFIG_ARM64) 101cb149c66SAlexander Graf can_run_nt32 = false; 102cb149c66SAlexander Graf #elif defined(CONFIG_ARM) 103cb149c66SAlexander Graf can_run_nt64 = false; 104cb149c66SAlexander Graf #endif 105cb149c66SAlexander Graf 106cb149c66SAlexander Graf dos = efi; 107cb149c66SAlexander Graf if (dos->e_magic != IMAGE_DOS_SIGNATURE) { 108cb149c66SAlexander Graf printf("%s: Invalid DOS Signature\n", __func__); 109cb149c66SAlexander Graf return NULL; 110cb149c66SAlexander Graf } 111cb149c66SAlexander Graf 112cb149c66SAlexander Graf nt = (void *) ((char *)efi + dos->e_lfanew); 113cb149c66SAlexander Graf if (nt->Signature != IMAGE_NT_SIGNATURE) { 114cb149c66SAlexander Graf printf("%s: Invalid NT Signature\n", __func__); 115cb149c66SAlexander Graf return NULL; 116cb149c66SAlexander Graf } 117cb149c66SAlexander Graf 118cb149c66SAlexander Graf /* Calculate upper virtual address boundary */ 119cb149c66SAlexander Graf num_sections = nt->FileHeader.NumberOfSections; 120cb149c66SAlexander Graf sections = (void *)&nt->OptionalHeader + 121cb149c66SAlexander Graf nt->FileHeader.SizeOfOptionalHeader; 122cb149c66SAlexander Graf 123cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) { 124cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i]; 125cb149c66SAlexander Graf virt_size = max_t(unsigned long, virt_size, 126cb149c66SAlexander Graf sec->VirtualAddress + sec->Misc.VirtualSize); 127cb149c66SAlexander Graf } 128cb149c66SAlexander Graf 129cb149c66SAlexander Graf /* Read 32/64bit specific header bits */ 130cb149c66SAlexander Graf if (can_run_nt64 && 131cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) { 132cb149c66SAlexander Graf IMAGE_NT_HEADERS64 *nt64 = (void *)nt; 133cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; 134cb149c66SAlexander Graf image_size = opt->SizeOfImage; 135cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA); 136cb149c66SAlexander Graf if (!efi_reloc) { 137cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n", 138cb149c66SAlexander Graf __func__, virt_size); 139cb149c66SAlexander Graf return NULL; 140cb149c66SAlexander Graf } 141cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint; 142cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size; 143cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; 144cb149c66SAlexander Graf } else if (can_run_nt32 && 145cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) { 146cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; 147cb149c66SAlexander Graf image_size = opt->SizeOfImage; 148cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA); 149cb149c66SAlexander Graf if (!efi_reloc) { 150cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n", 151cb149c66SAlexander Graf __func__, virt_size); 152cb149c66SAlexander Graf return NULL; 153cb149c66SAlexander Graf } 154cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint; 155cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size; 156cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; 157cb149c66SAlexander Graf } else { 158cb149c66SAlexander Graf printf("%s: Invalid optional header magic %x\n", __func__, 159cb149c66SAlexander Graf nt->OptionalHeader.Magic); 160cb149c66SAlexander Graf return NULL; 161cb149c66SAlexander Graf } 162cb149c66SAlexander Graf 163cb149c66SAlexander Graf /* Load sections into RAM */ 164cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) { 165cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i]; 166cb149c66SAlexander Graf memset(efi_reloc + sec->VirtualAddress, 0, 167cb149c66SAlexander Graf sec->Misc.VirtualSize); 168cb149c66SAlexander Graf memcpy(efi_reloc + sec->VirtualAddress, 169cb149c66SAlexander Graf efi + sec->PointerToRawData, 170cb149c66SAlexander Graf sec->SizeOfRawData); 171cb149c66SAlexander Graf } 172cb149c66SAlexander Graf 173cb149c66SAlexander Graf /* Run through relocations */ 174cb149c66SAlexander Graf efi_loader_relocate(rel, rel_size, efi_reloc); 175cb149c66SAlexander Graf 176cb149c66SAlexander Graf /* Flush cache */ 177cb149c66SAlexander Graf flush_cache((ulong)efi_reloc, virt_size); 178cb149c66SAlexander Graf invalidate_icache_all(); 179cb149c66SAlexander Graf 180cb149c66SAlexander Graf /* Populate the loaded image interface bits */ 181cb149c66SAlexander Graf loaded_image_info->image_base = efi; 182cb149c66SAlexander Graf loaded_image_info->image_size = image_size; 183cb149c66SAlexander Graf 184cb149c66SAlexander Graf return entry; 185cb149c66SAlexander Graf } 186