1b9939336SAlexander Graf /* 2b9939336SAlexander Graf * EFI application loader 3b9939336SAlexander Graf * 4b9939336SAlexander Graf * Copyright (c) 2016 Alexander Graf 5b9939336SAlexander Graf * 6b9939336SAlexander Graf * SPDX-License-Identifier: GPL-2.0+ 7b9939336SAlexander Graf */ 8b9939336SAlexander Graf 9b9939336SAlexander Graf #include <common.h> 10b9939336SAlexander Graf #include <command.h> 119d922450SSimon Glass #include <dm.h> 12b9939336SAlexander Graf #include <efi_loader.h> 13b9939336SAlexander Graf #include <errno.h> 14b9939336SAlexander Graf #include <libfdt.h> 15b9939336SAlexander Graf #include <libfdt_env.h> 16ad0c1a3dSAlexander Graf #include <memalign.h> 170d9d501fSAlexander Graf #include <asm/global_data.h> 18e275458cSSimon Glass #include <asm-generic/sections.h> 19e275458cSSimon Glass #include <linux/linkage.h> 200d9d501fSAlexander Graf 210d9d501fSAlexander Graf DECLARE_GLOBAL_DATA_PTR; 22b9939336SAlexander Graf 23b9939336SAlexander Graf /* 24b9939336SAlexander Graf * When booting using the "bootefi" command, we don't know which 25b9939336SAlexander Graf * physical device the file came from. So we create a pseudo-device 26b9939336SAlexander Graf * called "bootefi" with the device path /bootefi. 27b9939336SAlexander Graf * 28b9939336SAlexander Graf * In addition to the originating device we also declare the file path 29b9939336SAlexander Graf * of "bootefi" based loads to be /bootefi. 30b9939336SAlexander Graf */ 310f4060ebSAlexander Graf static struct efi_device_path_file_path bootefi_image_path[] = { 32b9939336SAlexander Graf { 33b9939336SAlexander Graf .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE, 34b9939336SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH, 350f4060ebSAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 36b9939336SAlexander Graf .str = { 'b','o','o','t','e','f','i' }, 37b9939336SAlexander Graf }, { 38b9939336SAlexander Graf .dp.type = DEVICE_PATH_TYPE_END, 39b9939336SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_END, 400f4060ebSAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 41b9939336SAlexander Graf } 42b9939336SAlexander Graf }; 43b9939336SAlexander Graf 44c07ad7c0SAlexander Graf static struct efi_device_path_file_path bootefi_device_path[] = { 45c07ad7c0SAlexander Graf { 46c07ad7c0SAlexander Graf .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE, 47c07ad7c0SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH, 48c07ad7c0SAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 49c07ad7c0SAlexander Graf .str = { 'b','o','o','t','e','f','i' }, 50c07ad7c0SAlexander Graf }, { 51c07ad7c0SAlexander Graf .dp.type = DEVICE_PATH_TYPE_END, 52c07ad7c0SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_END, 53c07ad7c0SAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 54c07ad7c0SAlexander Graf } 55c07ad7c0SAlexander Graf }; 56c07ad7c0SAlexander Graf 57b9939336SAlexander Graf /* The EFI loaded_image interface for the image executed via "bootefi" */ 58b9939336SAlexander Graf static struct efi_loaded_image loaded_image_info = { 59c07ad7c0SAlexander Graf .device_handle = bootefi_device_path, 600f4060ebSAlexander Graf .file_path = bootefi_image_path, 61b9939336SAlexander Graf }; 62b9939336SAlexander Graf 63b9939336SAlexander Graf /* The EFI object struct for the image executed via "bootefi" */ 64b9939336SAlexander Graf static struct efi_object loaded_image_info_obj = { 65b9939336SAlexander Graf .handle = &loaded_image_info, 66b9939336SAlexander Graf .protocols = { 67b9939336SAlexander Graf { 68b9939336SAlexander Graf /* 69b9939336SAlexander Graf * When asking for the loaded_image interface, just 70b9939336SAlexander Graf * return handle which points to loaded_image_info 71b9939336SAlexander Graf */ 72b9939336SAlexander Graf .guid = &efi_guid_loaded_image, 73*b5349f74Sxypron.glpk@gmx.de .protocol_interface = &loaded_image_info, 74b9939336SAlexander Graf }, 75b9939336SAlexander Graf { 76b9939336SAlexander Graf /* 77b9939336SAlexander Graf * When asking for the device path interface, return 78c07ad7c0SAlexander Graf * bootefi_device_path 79b9939336SAlexander Graf */ 80b9939336SAlexander Graf .guid = &efi_guid_device_path, 81*b5349f74Sxypron.glpk@gmx.de .protocol_interface = bootefi_device_path, 82b9939336SAlexander Graf }, 83b9939336SAlexander Graf }, 84b9939336SAlexander Graf }; 85b9939336SAlexander Graf 86b9939336SAlexander Graf /* The EFI object struct for the device the "bootefi" image was loaded from */ 87b9939336SAlexander Graf static struct efi_object bootefi_device_obj = { 88c07ad7c0SAlexander Graf .handle = bootefi_device_path, 89b9939336SAlexander Graf .protocols = { 90b9939336SAlexander Graf { 91b9939336SAlexander Graf /* When asking for the device path interface, return 92c07ad7c0SAlexander Graf * bootefi_device_path */ 93b9939336SAlexander Graf .guid = &efi_guid_device_path, 94*b5349f74Sxypron.glpk@gmx.de .protocol_interface = bootefi_device_path 95b9939336SAlexander Graf } 96b9939336SAlexander Graf }, 97b9939336SAlexander Graf }; 98b9939336SAlexander Graf 990d9d501fSAlexander Graf static void *copy_fdt(void *fdt) 1000d9d501fSAlexander Graf { 1010d9d501fSAlexander Graf u64 fdt_size = fdt_totalsize(fdt); 102ad0c1a3dSAlexander Graf unsigned long fdt_ram_start = -1L, fdt_pages; 103ad0c1a3dSAlexander Graf u64 new_fdt_addr; 1040d9d501fSAlexander Graf void *new_fdt; 105ad0c1a3dSAlexander Graf int i; 1060d9d501fSAlexander Graf 107ad0c1a3dSAlexander Graf for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 108ad0c1a3dSAlexander Graf u64 ram_start = gd->bd->bi_dram[i].start; 109ad0c1a3dSAlexander Graf u64 ram_size = gd->bd->bi_dram[i].size; 1100d9d501fSAlexander Graf 111ad0c1a3dSAlexander Graf if (!ram_size) 112ad0c1a3dSAlexander Graf continue; 113ad0c1a3dSAlexander Graf 114ad0c1a3dSAlexander Graf if (ram_start < fdt_ram_start) 115ad0c1a3dSAlexander Graf fdt_ram_start = ram_start; 116ad0c1a3dSAlexander Graf } 117ad0c1a3dSAlexander Graf 118ad0c1a3dSAlexander Graf /* Give us at least 4kb breathing room */ 119ad0c1a3dSAlexander Graf fdt_size = ALIGN(fdt_size + 4096, 4096); 120ad0c1a3dSAlexander Graf fdt_pages = fdt_size >> EFI_PAGE_SHIFT; 121ad0c1a3dSAlexander Graf 122ad0c1a3dSAlexander Graf /* Safe fdt location is at 128MB */ 123ad0c1a3dSAlexander Graf new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size; 124ad0c1a3dSAlexander Graf if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages, 125ad0c1a3dSAlexander Graf &new_fdt_addr) != EFI_SUCCESS) { 126ad0c1a3dSAlexander Graf /* If we can't put it there, put it somewhere */ 127ad0c1a3dSAlexander Graf new_fdt_addr = (ulong)memalign(4096, fdt_size); 12885a6e9b3SAlexander Graf if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages, 12985a6e9b3SAlexander Graf &new_fdt_addr) != EFI_SUCCESS) { 13085a6e9b3SAlexander Graf printf("ERROR: Failed to reserve space for FDT\n"); 13185a6e9b3SAlexander Graf return NULL; 132ad0c1a3dSAlexander Graf } 13385a6e9b3SAlexander Graf } 13485a6e9b3SAlexander Graf 135ad0c1a3dSAlexander Graf new_fdt = (void*)(ulong)new_fdt_addr; 1360d9d501fSAlexander Graf memcpy(new_fdt, fdt, fdt_totalsize(fdt)); 1370d9d501fSAlexander Graf fdt_set_totalsize(new_fdt, fdt_size); 1380d9d501fSAlexander Graf 1390d9d501fSAlexander Graf return new_fdt; 1400d9d501fSAlexander Graf } 1410d9d501fSAlexander Graf 142ec6617c3SAlison Wang #ifdef CONFIG_ARM64 143ec6617c3SAlison Wang static unsigned long efi_run_in_el2(ulong (*entry)(void *image_handle, 144ec6617c3SAlison Wang struct efi_system_table *st), void *image_handle, 145ec6617c3SAlison Wang struct efi_system_table *st) 146ec6617c3SAlison Wang { 147ec6617c3SAlison Wang /* Enable caches again */ 148ec6617c3SAlison Wang dcache_enable(); 149ec6617c3SAlison Wang 150ec6617c3SAlison Wang return entry(image_handle, st); 151ec6617c3SAlison Wang } 152ec6617c3SAlison Wang #endif 153ec6617c3SAlison Wang 154b9939336SAlexander Graf /* 155b9939336SAlexander Graf * Load an EFI payload into a newly allocated piece of memory, register all 156b9939336SAlexander Graf * EFI objects it would want to access and jump to it. 157b9939336SAlexander Graf */ 1581c39809bSAlexander Graf static unsigned long do_bootefi_exec(void *efi, void *fdt) 159b9939336SAlexander Graf { 160e275458cSSimon Glass ulong (*entry)(void *image_handle, struct efi_system_table *st) 161e275458cSSimon Glass asmlinkage; 162b9939336SAlexander Graf ulong fdt_pages, fdt_size, fdt_start, fdt_end; 163dea2174dSAlexander Graf bootm_headers_t img = { 0 }; 164b9939336SAlexander Graf 165b9939336SAlexander Graf /* 166b9939336SAlexander Graf * gd lives in a fixed register which may get clobbered while we execute 167b9939336SAlexander Graf * the payload. So save it here and restore it on every callback entry 168b9939336SAlexander Graf */ 169b9939336SAlexander Graf efi_save_gd(); 170b9939336SAlexander Graf 1711c39809bSAlexander Graf if (fdt && !fdt_check_header(fdt)) { 172dea2174dSAlexander Graf /* Prepare fdt for payload */ 1730d9d501fSAlexander Graf fdt = copy_fdt(fdt); 1740d9d501fSAlexander Graf 1750d9d501fSAlexander Graf if (image_setup_libfdt(&img, fdt, 0, NULL)) { 176dea2174dSAlexander Graf printf("ERROR: Failed to process device tree\n"); 177dea2174dSAlexander Graf return -EINVAL; 178dea2174dSAlexander Graf } 179dea2174dSAlexander Graf 180dea2174dSAlexander Graf /* Link to it in the efi tables */ 181b9939336SAlexander Graf systab.tables[0].guid = EFI_FDT_GUID; 1820d9d501fSAlexander Graf systab.tables[0].table = fdt; 183b9939336SAlexander Graf systab.nr_tables = 1; 184b9939336SAlexander Graf 185b9939336SAlexander Graf /* And reserve the space in the memory map */ 1860d9d501fSAlexander Graf fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK; 1870d9d501fSAlexander Graf fdt_end = ((ulong)fdt) + fdt_totalsize(fdt); 188b9939336SAlexander Graf fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK; 189b9939336SAlexander Graf fdt_pages = fdt_size >> EFI_PAGE_SHIFT; 190b9939336SAlexander Graf /* Give a bootloader the chance to modify the device tree */ 191b9939336SAlexander Graf fdt_pages += 2; 192b9939336SAlexander Graf efi_add_memory_map(fdt_start, fdt_pages, 193b9939336SAlexander Graf EFI_BOOT_SERVICES_DATA, true); 194b9939336SAlexander Graf } else { 1951c39809bSAlexander Graf printf("WARNING: Invalid device tree, expect boot to fail\n"); 196b9939336SAlexander Graf systab.nr_tables = 0; 197b9939336SAlexander Graf } 198b9939336SAlexander Graf 199b9939336SAlexander Graf /* Load the EFI payload */ 200b9939336SAlexander Graf entry = efi_load_pe(efi, &loaded_image_info); 201b9939336SAlexander Graf if (!entry) 202b9939336SAlexander Graf return -ENOENT; 203b9939336SAlexander Graf 204b9939336SAlexander Graf /* Initialize and populate EFI object list */ 205b9939336SAlexander Graf INIT_LIST_HEAD(&efi_obj_list); 206b9939336SAlexander Graf list_add_tail(&loaded_image_info_obj.link, &efi_obj_list); 207b9939336SAlexander Graf list_add_tail(&bootefi_device_obj.link, &efi_obj_list); 208b9939336SAlexander Graf #ifdef CONFIG_PARTITIONS 209b9939336SAlexander Graf efi_disk_register(); 210b9939336SAlexander Graf #endif 211be8d3241SAlexander Graf #ifdef CONFIG_LCD 212be8d3241SAlexander Graf efi_gop_register(); 213be8d3241SAlexander Graf #endif 2140efe1bcfSAlexander Graf #ifdef CONFIG_NET 2150efe1bcfSAlexander Graf void *nethandle = loaded_image_info.device_handle; 2160efe1bcfSAlexander Graf efi_net_register(&nethandle); 2170efe1bcfSAlexander Graf 2180efe1bcfSAlexander Graf if (!memcmp(bootefi_device_path[0].str, "N\0e\0t", 6)) 2190efe1bcfSAlexander Graf loaded_image_info.device_handle = nethandle; 2203fb97e26SAlexander Graf else 2213fb97e26SAlexander Graf loaded_image_info.device_handle = bootefi_device_path; 2220efe1bcfSAlexander Graf #endif 223e663b350SAlexander Graf #ifdef CONFIG_GENERATE_SMBIOS_TABLE 224e663b350SAlexander Graf efi_smbios_register(); 225e663b350SAlexander Graf #endif 226b9939336SAlexander Graf 22780a4800eSAlexander Graf /* Initialize EFI runtime services */ 22880a4800eSAlexander Graf efi_reset_system_init(); 22980a4800eSAlexander Graf efi_get_time_init(); 23080a4800eSAlexander Graf 231b9939336SAlexander Graf /* Call our payload! */ 232edcef3baSAlexander Graf debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); 233a86aeaf2SAlexander Graf 234a86aeaf2SAlexander Graf if (setjmp(&loaded_image_info.exit_jmp)) { 235a86aeaf2SAlexander Graf efi_status_t status = loaded_image_info.exit_status; 236a86aeaf2SAlexander Graf return status == EFI_SUCCESS ? 0 : -EINVAL; 237a86aeaf2SAlexander Graf } 238a86aeaf2SAlexander Graf 23969bd459dSAlexander Graf #ifdef CONFIG_ARM64 24069bd459dSAlexander Graf /* On AArch64 we need to make sure we call our payload in < EL3 */ 24169bd459dSAlexander Graf if (current_el() == 3) { 24269bd459dSAlexander Graf smp_kick_all_cpus(); 24369bd459dSAlexander Graf dcache_disable(); /* flush cache before switch to EL2 */ 244ec6617c3SAlison Wang 245ec6617c3SAlison Wang /* Move into EL2 and keep running there */ 246ec6617c3SAlison Wang armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info, 2477c5e1febSAlison Wang (ulong)&systab, 0, (ulong)efi_run_in_el2, 248ec6617c3SAlison Wang ES_TO_AARCH64); 249ec6617c3SAlison Wang 250ec6617c3SAlison Wang /* Should never reach here, efi exits with longjmp */ 251ec6617c3SAlison Wang while (1) { } 25269bd459dSAlexander Graf } 25369bd459dSAlexander Graf #endif 25469bd459dSAlexander Graf 255b9939336SAlexander Graf return entry(&loaded_image_info, &systab); 256b9939336SAlexander Graf } 257b9939336SAlexander Graf 258b9939336SAlexander Graf 259b9939336SAlexander Graf /* Interpreter command to boot an arbitrary EFI image from memory */ 260b9939336SAlexander Graf static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 261b9939336SAlexander Graf { 2621c39809bSAlexander Graf char *saddr, *sfdt; 2631c39809bSAlexander Graf unsigned long addr, fdt_addr = 0; 264b9939336SAlexander Graf int r = 0; 265b9939336SAlexander Graf 266b9939336SAlexander Graf if (argc < 2) 2673c1dcef6SBin Meng return CMD_RET_USAGE; 268c7ae3dfdSSimon Glass #ifdef CONFIG_CMD_BOOTEFI_HELLO 269c7ae3dfdSSimon Glass if (!strcmp(argv[1], "hello")) { 270c7ae3dfdSSimon Glass ulong size = __efi_hello_world_end - __efi_hello_world_begin; 271c7ae3dfdSSimon Glass 272c7ae3dfdSSimon Glass addr = CONFIG_SYS_LOAD_ADDR; 273c7ae3dfdSSimon Glass memcpy((char *)addr, __efi_hello_world_begin, size); 274c7ae3dfdSSimon Glass } else 275c7ae3dfdSSimon Glass #endif 276c7ae3dfdSSimon Glass { 277b9939336SAlexander Graf saddr = argv[1]; 278b9939336SAlexander Graf 279b9939336SAlexander Graf addr = simple_strtoul(saddr, NULL, 16); 280b9939336SAlexander Graf 2811c39809bSAlexander Graf if (argc > 2) { 2821c39809bSAlexander Graf sfdt = argv[2]; 2831c39809bSAlexander Graf fdt_addr = simple_strtoul(sfdt, NULL, 16); 2841c39809bSAlexander Graf } 285c7ae3dfdSSimon Glass } 2861c39809bSAlexander Graf 2875ee31bafSSimon Glass printf("## Starting EFI application at %08lx ...\n", addr); 2881c39809bSAlexander Graf r = do_bootefi_exec((void *)addr, (void*)fdt_addr); 289b9939336SAlexander Graf printf("## Application terminated, r = %d\n", r); 290b9939336SAlexander Graf 291b9939336SAlexander Graf if (r != 0) 292b9939336SAlexander Graf r = 1; 293b9939336SAlexander Graf 294b9939336SAlexander Graf return r; 295b9939336SAlexander Graf } 296b9939336SAlexander Graf 297b9939336SAlexander Graf #ifdef CONFIG_SYS_LONGHELP 298b9939336SAlexander Graf static char bootefi_help_text[] = 2991c39809bSAlexander Graf "<image address> [fdt address]\n" 3001c39809bSAlexander Graf " - boot EFI payload stored at address <image address>.\n" 3011c39809bSAlexander Graf " If specified, the device tree located at <fdt address> gets\n" 302c7ae3dfdSSimon Glass " exposed as EFI configuration table.\n" 303c7ae3dfdSSimon Glass #ifdef CONFIG_CMD_BOOTEFI_HELLO 304c7ae3dfdSSimon Glass "hello\n" 305c7ae3dfdSSimon Glass " - boot a sample Hello World application stored within U-Boot" 306c7ae3dfdSSimon Glass #endif 307c7ae3dfdSSimon Glass ; 308b9939336SAlexander Graf #endif 309b9939336SAlexander Graf 310b9939336SAlexander Graf U_BOOT_CMD( 3111c39809bSAlexander Graf bootefi, 3, 0, do_bootefi, 31292dfd922SSergey Kubushyn "Boots an EFI payload from memory", 313b9939336SAlexander Graf bootefi_help_text 314b9939336SAlexander Graf ); 3150f4060ebSAlexander Graf 316c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path) 3170f4060ebSAlexander Graf { 3188c3df0bfSAlexander Graf __maybe_unused struct blk_desc *desc; 319ecbe1a07SAlexander Graf char devname[32] = { 0 }; /* dp->str is u16[32] long */ 3200f4060ebSAlexander Graf char *colon; 3210f4060ebSAlexander Graf 3221acc0087SPatrick Delaunay #if defined(CONFIG_BLK) || CONFIG_IS_ENABLED(ISO_PARTITION) 323f9d334bdSAlexander Graf desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10)); 324f9d334bdSAlexander Graf #endif 325f9d334bdSAlexander Graf 326f9d334bdSAlexander Graf #ifdef CONFIG_BLK 327f9d334bdSAlexander Graf if (desc) { 328f9d334bdSAlexander Graf snprintf(devname, sizeof(devname), "%s", desc->bdev->name); 329f9d334bdSAlexander Graf } else 330f9d334bdSAlexander Graf #endif 331f9d334bdSAlexander Graf 332f9d334bdSAlexander Graf { 3330f4060ebSAlexander Graf /* Assemble the condensed device name we use in efi_disk.c */ 3340f4060ebSAlexander Graf snprintf(devname, sizeof(devname), "%s%s", dev, devnr); 335f9d334bdSAlexander Graf } 336f9d334bdSAlexander Graf 3370f4060ebSAlexander Graf colon = strchr(devname, ':'); 3388c3df0bfSAlexander Graf 3391acc0087SPatrick Delaunay #if CONFIG_IS_ENABLED(ISO_PARTITION) 3408c3df0bfSAlexander Graf /* For ISOs we create partition block devices */ 3418c3df0bfSAlexander Graf if (desc && (desc->type != DEV_TYPE_UNKNOWN) && 3428c3df0bfSAlexander Graf (desc->part_type == PART_TYPE_ISO)) { 3438c3df0bfSAlexander Graf if (!colon) 344f9d334bdSAlexander Graf snprintf(devname, sizeof(devname), "%s:1", devname); 345f9d334bdSAlexander Graf 3468c3df0bfSAlexander Graf colon = NULL; 3478c3df0bfSAlexander Graf } 3488c3df0bfSAlexander Graf #endif 3498c3df0bfSAlexander Graf 3500f4060ebSAlexander Graf if (colon) 3510f4060ebSAlexander Graf *colon = '\0'; 3520f4060ebSAlexander Graf 353c07ad7c0SAlexander Graf /* Patch bootefi_device_path to the target device */ 354c07ad7c0SAlexander Graf memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str)); 355c07ad7c0SAlexander Graf ascii2unicode(bootefi_device_path[0].str, devname); 356c07ad7c0SAlexander Graf 357c07ad7c0SAlexander Graf /* Patch bootefi_image_path to the target file path */ 3580f4060ebSAlexander Graf memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str)); 35949271666SAlexander Graf if (strcmp(dev, "Net")) { 36049271666SAlexander Graf /* Add leading / to fs paths, because they're absolute */ 36149271666SAlexander Graf snprintf(devname, sizeof(devname), "/%s", path); 36249271666SAlexander Graf } else { 363c07ad7c0SAlexander Graf snprintf(devname, sizeof(devname), "%s", path); 36449271666SAlexander Graf } 3650f4060ebSAlexander Graf ascii2unicode(bootefi_image_path[0].str, devname); 3660f4060ebSAlexander Graf } 367