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 29*da684a64Sxypron.glpk@gmx.de static efi_status_t 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--) { 40b1237c6eSAlexander 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); 66*da684a64Sxypron.glpk@gmx.de return EFI_LOAD_ERROR; 67cb149c66SAlexander Graf } 68cb149c66SAlexander Graf relocs++; 69cb149c66SAlexander Graf } 70cb149c66SAlexander Graf rel = (const IMAGE_BASE_RELOCATION *)relocs; 71cb149c66SAlexander Graf } 72*da684a64Sxypron.glpk@gmx.de return EFI_SUCCESS; 73cb149c66SAlexander Graf } 74cb149c66SAlexander Graf 75cb149c66SAlexander Graf void __weak invalidate_icache_all(void) 76cb149c66SAlexander Graf { 77cb149c66SAlexander Graf /* If the system doesn't support icache_all flush, cross our fingers */ 78cb149c66SAlexander Graf } 79cb149c66SAlexander Graf 80cb149c66SAlexander Graf /* 81cb149c66SAlexander Graf * This function loads all sections from a PE binary into a newly reserved 82cb149c66SAlexander Graf * piece of memory. On successful load it then returns the entry point for 83cb149c66SAlexander Graf * the binary. Otherwise NULL. 84cb149c66SAlexander Graf */ 85cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info) 86cb149c66SAlexander Graf { 87cb149c66SAlexander Graf IMAGE_NT_HEADERS32 *nt; 88cb149c66SAlexander Graf IMAGE_DOS_HEADER *dos; 89cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sections; 90cb149c66SAlexander Graf int num_sections; 91cb149c66SAlexander Graf void *efi_reloc; 92cb149c66SAlexander Graf int i; 93cb149c66SAlexander Graf const IMAGE_BASE_RELOCATION *rel; 94cb149c66SAlexander Graf unsigned long rel_size; 95cb149c66SAlexander Graf int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; 96cb149c66SAlexander Graf void *entry; 97cb149c66SAlexander Graf uint64_t image_size; 98cb149c66SAlexander Graf unsigned long virt_size = 0; 99cb149c66SAlexander Graf bool can_run_nt64 = true; 100cb149c66SAlexander Graf bool can_run_nt32 = true; 101cb149c66SAlexander Graf 102cb149c66SAlexander Graf #if defined(CONFIG_ARM64) 103cb149c66SAlexander Graf can_run_nt32 = false; 104cb149c66SAlexander Graf #elif defined(CONFIG_ARM) 105cb149c66SAlexander Graf can_run_nt64 = false; 106cb149c66SAlexander Graf #endif 107cb149c66SAlexander Graf 108cb149c66SAlexander Graf dos = efi; 109cb149c66SAlexander Graf if (dos->e_magic != IMAGE_DOS_SIGNATURE) { 110cb149c66SAlexander Graf printf("%s: Invalid DOS Signature\n", __func__); 111cb149c66SAlexander Graf return NULL; 112cb149c66SAlexander Graf } 113cb149c66SAlexander Graf 114cb149c66SAlexander Graf nt = (void *) ((char *)efi + dos->e_lfanew); 115cb149c66SAlexander Graf if (nt->Signature != IMAGE_NT_SIGNATURE) { 116cb149c66SAlexander Graf printf("%s: Invalid NT Signature\n", __func__); 117cb149c66SAlexander Graf return NULL; 118cb149c66SAlexander Graf } 119cb149c66SAlexander Graf 120cb149c66SAlexander Graf /* Calculate upper virtual address boundary */ 121cb149c66SAlexander Graf num_sections = nt->FileHeader.NumberOfSections; 122cb149c66SAlexander Graf sections = (void *)&nt->OptionalHeader + 123cb149c66SAlexander Graf nt->FileHeader.SizeOfOptionalHeader; 124cb149c66SAlexander Graf 125cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) { 126cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i]; 127cb149c66SAlexander Graf virt_size = max_t(unsigned long, virt_size, 128cb149c66SAlexander Graf sec->VirtualAddress + sec->Misc.VirtualSize); 129cb149c66SAlexander Graf } 130cb149c66SAlexander Graf 131cb149c66SAlexander Graf /* Read 32/64bit specific header bits */ 132cb149c66SAlexander Graf if (can_run_nt64 && 133cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) { 134cb149c66SAlexander Graf IMAGE_NT_HEADERS64 *nt64 = (void *)nt; 135cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; 136cb149c66SAlexander Graf image_size = opt->SizeOfImage; 137cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA); 138cb149c66SAlexander Graf if (!efi_reloc) { 139cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n", 140cb149c66SAlexander Graf __func__, virt_size); 141cb149c66SAlexander Graf return NULL; 142cb149c66SAlexander Graf } 143cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint; 144cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size; 145cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; 146cb149c66SAlexander Graf } else if (can_run_nt32 && 147cb149c66SAlexander Graf (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) { 148cb149c66SAlexander Graf IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; 149cb149c66SAlexander Graf image_size = opt->SizeOfImage; 150cb149c66SAlexander Graf efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA); 151cb149c66SAlexander Graf if (!efi_reloc) { 152cb149c66SAlexander Graf printf("%s: Could not allocate %ld bytes\n", 153cb149c66SAlexander Graf __func__, virt_size); 154cb149c66SAlexander Graf return NULL; 155cb149c66SAlexander Graf } 156cb149c66SAlexander Graf entry = efi_reloc + opt->AddressOfEntryPoint; 157cb149c66SAlexander Graf rel_size = opt->DataDirectory[rel_idx].Size; 158cb149c66SAlexander Graf rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; 159cb149c66SAlexander Graf } else { 160cb149c66SAlexander Graf printf("%s: Invalid optional header magic %x\n", __func__, 161cb149c66SAlexander Graf nt->OptionalHeader.Magic); 162cb149c66SAlexander Graf return NULL; 163cb149c66SAlexander Graf } 164cb149c66SAlexander Graf 165cb149c66SAlexander Graf /* Load sections into RAM */ 166cb149c66SAlexander Graf for (i = num_sections - 1; i >= 0; i--) { 167cb149c66SAlexander Graf IMAGE_SECTION_HEADER *sec = §ions[i]; 168cb149c66SAlexander Graf memset(efi_reloc + sec->VirtualAddress, 0, 169cb149c66SAlexander Graf sec->Misc.VirtualSize); 170cb149c66SAlexander Graf memcpy(efi_reloc + sec->VirtualAddress, 171cb149c66SAlexander Graf efi + sec->PointerToRawData, 172cb149c66SAlexander Graf sec->SizeOfRawData); 173cb149c66SAlexander Graf } 174cb149c66SAlexander Graf 175cb149c66SAlexander Graf /* Run through relocations */ 176*da684a64Sxypron.glpk@gmx.de if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) { 177*da684a64Sxypron.glpk@gmx.de efi_free_pages((uintptr_t) efi_reloc, 178*da684a64Sxypron.glpk@gmx.de (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); 179*da684a64Sxypron.glpk@gmx.de return NULL; 180*da684a64Sxypron.glpk@gmx.de } 181cb149c66SAlexander Graf 182cb149c66SAlexander Graf /* Flush cache */ 183d0d90993SSimon Glass flush_cache((ulong)efi_reloc, 184d0d90993SSimon Glass ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE)); 185cb149c66SAlexander Graf invalidate_icache_all(); 186cb149c66SAlexander Graf 187cb149c66SAlexander Graf /* Populate the loaded image interface bits */ 188cb149c66SAlexander Graf loaded_image_info->image_base = efi; 189cb149c66SAlexander Graf loaded_image_info->image_size = image_size; 190cb149c66SAlexander Graf 191cb149c66SAlexander Graf return entry; 192cb149c66SAlexander Graf } 193