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