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> 11b9939336SAlexander Graf #include <efi_loader.h> 12b9939336SAlexander Graf #include <errno.h> 13b9939336SAlexander Graf #include <libfdt.h> 14b9939336SAlexander Graf #include <libfdt_env.h> 15*0d9d501fSAlexander Graf #include <malloc.h> 16*0d9d501fSAlexander Graf #include <asm/global_data.h> 17*0d9d501fSAlexander Graf 18*0d9d501fSAlexander Graf DECLARE_GLOBAL_DATA_PTR; 19b9939336SAlexander Graf 20b9939336SAlexander Graf /* 21b9939336SAlexander Graf * When booting using the "bootefi" command, we don't know which 22b9939336SAlexander Graf * physical device the file came from. So we create a pseudo-device 23b9939336SAlexander Graf * called "bootefi" with the device path /bootefi. 24b9939336SAlexander Graf * 25b9939336SAlexander Graf * In addition to the originating device we also declare the file path 26b9939336SAlexander Graf * of "bootefi" based loads to be /bootefi. 27b9939336SAlexander Graf */ 280f4060ebSAlexander Graf static struct efi_device_path_file_path bootefi_image_path[] = { 29b9939336SAlexander Graf { 30b9939336SAlexander Graf .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE, 31b9939336SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH, 320f4060ebSAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 33b9939336SAlexander Graf .str = { 'b','o','o','t','e','f','i' }, 34b9939336SAlexander Graf }, { 35b9939336SAlexander Graf .dp.type = DEVICE_PATH_TYPE_END, 36b9939336SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_END, 370f4060ebSAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 38b9939336SAlexander Graf } 39b9939336SAlexander Graf }; 40b9939336SAlexander Graf 41c07ad7c0SAlexander Graf static struct efi_device_path_file_path bootefi_device_path[] = { 42c07ad7c0SAlexander Graf { 43c07ad7c0SAlexander Graf .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE, 44c07ad7c0SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH, 45c07ad7c0SAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 46c07ad7c0SAlexander Graf .str = { 'b','o','o','t','e','f','i' }, 47c07ad7c0SAlexander Graf }, { 48c07ad7c0SAlexander Graf .dp.type = DEVICE_PATH_TYPE_END, 49c07ad7c0SAlexander Graf .dp.sub_type = DEVICE_PATH_SUB_TYPE_END, 50c07ad7c0SAlexander Graf .dp.length = sizeof(bootefi_image_path[0]), 51c07ad7c0SAlexander Graf } 52c07ad7c0SAlexander Graf }; 53c07ad7c0SAlexander Graf 54b9939336SAlexander Graf static efi_status_t bootefi_open_dp(void *handle, efi_guid_t *protocol, 55b9939336SAlexander Graf void **protocol_interface, void *agent_handle, 56b9939336SAlexander Graf void *controller_handle, uint32_t attributes) 57b9939336SAlexander Graf { 58c07ad7c0SAlexander Graf *protocol_interface = bootefi_device_path; 59b9939336SAlexander Graf return EFI_SUCCESS; 60b9939336SAlexander Graf } 61b9939336SAlexander Graf 62b9939336SAlexander Graf /* The EFI loaded_image interface for the image executed via "bootefi" */ 63b9939336SAlexander Graf static struct efi_loaded_image loaded_image_info = { 64c07ad7c0SAlexander Graf .device_handle = bootefi_device_path, 650f4060ebSAlexander Graf .file_path = bootefi_image_path, 66b9939336SAlexander Graf }; 67b9939336SAlexander Graf 68b9939336SAlexander Graf /* The EFI object struct for the image executed via "bootefi" */ 69b9939336SAlexander Graf static struct efi_object loaded_image_info_obj = { 70b9939336SAlexander Graf .handle = &loaded_image_info, 71b9939336SAlexander Graf .protocols = { 72b9939336SAlexander Graf { 73b9939336SAlexander Graf /* 74b9939336SAlexander Graf * When asking for the loaded_image interface, just 75b9939336SAlexander Graf * return handle which points to loaded_image_info 76b9939336SAlexander Graf */ 77b9939336SAlexander Graf .guid = &efi_guid_loaded_image, 78b9939336SAlexander Graf .open = &efi_return_handle, 79b9939336SAlexander Graf }, 80b9939336SAlexander Graf { 81b9939336SAlexander Graf /* 82b9939336SAlexander Graf * When asking for the device path interface, return 83c07ad7c0SAlexander Graf * bootefi_device_path 84b9939336SAlexander Graf */ 85b9939336SAlexander Graf .guid = &efi_guid_device_path, 86b9939336SAlexander Graf .open = &bootefi_open_dp, 87b9939336SAlexander Graf }, 88b9939336SAlexander Graf }, 89b9939336SAlexander Graf }; 90b9939336SAlexander Graf 91b9939336SAlexander Graf /* The EFI object struct for the device the "bootefi" image was loaded from */ 92b9939336SAlexander Graf static struct efi_object bootefi_device_obj = { 93c07ad7c0SAlexander Graf .handle = bootefi_device_path, 94b9939336SAlexander Graf .protocols = { 95b9939336SAlexander Graf { 96b9939336SAlexander Graf /* When asking for the device path interface, return 97c07ad7c0SAlexander Graf * bootefi_device_path */ 98b9939336SAlexander Graf .guid = &efi_guid_device_path, 99b9939336SAlexander Graf .open = &bootefi_open_dp, 100b9939336SAlexander Graf } 101b9939336SAlexander Graf }, 102b9939336SAlexander Graf }; 103b9939336SAlexander Graf 104*0d9d501fSAlexander Graf static void *copy_fdt(void *fdt) 105*0d9d501fSAlexander Graf { 106*0d9d501fSAlexander Graf u64 fdt_size = fdt_totalsize(fdt); 107*0d9d501fSAlexander Graf void *new_fdt; 108*0d9d501fSAlexander Graf 109*0d9d501fSAlexander Graf /* Give us 64kb breathing room */ 110*0d9d501fSAlexander Graf fdt_size += 64 * 1024; 111*0d9d501fSAlexander Graf 112*0d9d501fSAlexander Graf new_fdt = malloc(fdt_size); 113*0d9d501fSAlexander Graf memcpy(new_fdt, fdt, fdt_totalsize(fdt)); 114*0d9d501fSAlexander Graf fdt_set_totalsize(new_fdt, fdt_size); 115*0d9d501fSAlexander Graf 116*0d9d501fSAlexander Graf return new_fdt; 117*0d9d501fSAlexander Graf } 118*0d9d501fSAlexander Graf 119b9939336SAlexander Graf /* 120b9939336SAlexander Graf * Load an EFI payload into a newly allocated piece of memory, register all 121b9939336SAlexander Graf * EFI objects it would want to access and jump to it. 122b9939336SAlexander Graf */ 123b9939336SAlexander Graf static unsigned long do_bootefi_exec(void *efi) 124b9939336SAlexander Graf { 125b9939336SAlexander Graf ulong (*entry)(void *image_handle, struct efi_system_table *st); 126b9939336SAlexander Graf ulong fdt_pages, fdt_size, fdt_start, fdt_end; 127dea2174dSAlexander Graf bootm_headers_t img = { 0 }; 128*0d9d501fSAlexander Graf void *fdt = working_fdt; 129b9939336SAlexander Graf 130b9939336SAlexander Graf /* 131b9939336SAlexander Graf * gd lives in a fixed register which may get clobbered while we execute 132b9939336SAlexander Graf * the payload. So save it here and restore it on every callback entry 133b9939336SAlexander Graf */ 134b9939336SAlexander Graf efi_save_gd(); 135b9939336SAlexander Graf 136b9939336SAlexander Graf /* Update system table to point to our currently loaded FDT */ 137b9939336SAlexander Graf 138*0d9d501fSAlexander Graf /* Fall back to included fdt if none was manually loaded */ 139*0d9d501fSAlexander Graf if (!fdt && gd->fdt_blob) 140*0d9d501fSAlexander Graf fdt = (void *)gd->fdt_blob; 141*0d9d501fSAlexander Graf 142*0d9d501fSAlexander Graf if (fdt) { 143dea2174dSAlexander Graf /* Prepare fdt for payload */ 144*0d9d501fSAlexander Graf fdt = copy_fdt(fdt); 145*0d9d501fSAlexander Graf 146*0d9d501fSAlexander Graf if (image_setup_libfdt(&img, fdt, 0, NULL)) { 147dea2174dSAlexander Graf printf("ERROR: Failed to process device tree\n"); 148dea2174dSAlexander Graf return -EINVAL; 149dea2174dSAlexander Graf } 150dea2174dSAlexander Graf 151dea2174dSAlexander Graf /* Link to it in the efi tables */ 152b9939336SAlexander Graf systab.tables[0].guid = EFI_FDT_GUID; 153*0d9d501fSAlexander Graf systab.tables[0].table = fdt; 154b9939336SAlexander Graf systab.nr_tables = 1; 155b9939336SAlexander Graf 156b9939336SAlexander Graf /* And reserve the space in the memory map */ 157*0d9d501fSAlexander Graf fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK; 158*0d9d501fSAlexander Graf fdt_end = ((ulong)fdt) + fdt_totalsize(fdt); 159b9939336SAlexander Graf fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK; 160b9939336SAlexander Graf fdt_pages = fdt_size >> EFI_PAGE_SHIFT; 161b9939336SAlexander Graf /* Give a bootloader the chance to modify the device tree */ 162b9939336SAlexander Graf fdt_pages += 2; 163b9939336SAlexander Graf efi_add_memory_map(fdt_start, fdt_pages, 164b9939336SAlexander Graf EFI_BOOT_SERVICES_DATA, true); 165b9939336SAlexander Graf } else { 166b9939336SAlexander Graf printf("WARNING: No device tree loaded, expect boot to fail\n"); 167b9939336SAlexander Graf systab.nr_tables = 0; 168b9939336SAlexander Graf } 169b9939336SAlexander Graf 170b9939336SAlexander Graf /* Load the EFI payload */ 171b9939336SAlexander Graf entry = efi_load_pe(efi, &loaded_image_info); 172b9939336SAlexander Graf if (!entry) 173b9939336SAlexander Graf return -ENOENT; 174b9939336SAlexander Graf 175b9939336SAlexander Graf /* Initialize and populate EFI object list */ 176b9939336SAlexander Graf INIT_LIST_HEAD(&efi_obj_list); 177b9939336SAlexander Graf list_add_tail(&loaded_image_info_obj.link, &efi_obj_list); 178b9939336SAlexander Graf list_add_tail(&bootefi_device_obj.link, &efi_obj_list); 179b9939336SAlexander Graf #ifdef CONFIG_PARTITIONS 180b9939336SAlexander Graf efi_disk_register(); 181b9939336SAlexander Graf #endif 182be8d3241SAlexander Graf #ifdef CONFIG_LCD 183be8d3241SAlexander Graf efi_gop_register(); 184be8d3241SAlexander Graf #endif 185b9939336SAlexander Graf 186b9939336SAlexander Graf /* Call our payload! */ 187b9939336SAlexander Graf #ifdef DEBUG_EFI 188b9939336SAlexander Graf printf("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); 189b9939336SAlexander Graf #endif 190b9939336SAlexander Graf return entry(&loaded_image_info, &systab); 191b9939336SAlexander Graf } 192b9939336SAlexander Graf 193b9939336SAlexander Graf 194b9939336SAlexander Graf /* Interpreter command to boot an arbitrary EFI image from memory */ 195b9939336SAlexander Graf static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 196b9939336SAlexander Graf { 197b9939336SAlexander Graf char *saddr; 198b9939336SAlexander Graf unsigned long addr; 199b9939336SAlexander Graf int r = 0; 200b9939336SAlexander Graf 201b9939336SAlexander Graf if (argc < 2) 202b9939336SAlexander Graf return 1; 203b9939336SAlexander Graf saddr = argv[1]; 204b9939336SAlexander Graf 205b9939336SAlexander Graf addr = simple_strtoul(saddr, NULL, 16); 206b9939336SAlexander Graf 207b9939336SAlexander Graf printf("## Starting EFI application at 0x%08lx ...\n", addr); 208b9939336SAlexander Graf r = do_bootefi_exec((void *)addr); 209b9939336SAlexander Graf printf("## Application terminated, r = %d\n", r); 210b9939336SAlexander Graf 211b9939336SAlexander Graf if (r != 0) 212b9939336SAlexander Graf r = 1; 213b9939336SAlexander Graf 214b9939336SAlexander Graf return r; 215b9939336SAlexander Graf } 216b9939336SAlexander Graf 217b9939336SAlexander Graf #ifdef CONFIG_SYS_LONGHELP 218b9939336SAlexander Graf static char bootefi_help_text[] = 219b9939336SAlexander Graf "<image address>\n" 220b9939336SAlexander Graf " - boot EFI payload stored at address <image address>\n" 221b9939336SAlexander Graf "\n" 222b9939336SAlexander Graf "Since most EFI payloads want to have a device tree provided, please\n" 223b9939336SAlexander Graf "make sure you load a device tree using the fdt addr command before\n" 224b9939336SAlexander Graf "executing bootefi.\n"; 225b9939336SAlexander Graf #endif 226b9939336SAlexander Graf 227b9939336SAlexander Graf U_BOOT_CMD( 228b9939336SAlexander Graf bootefi, 2, 0, do_bootefi, 229b9939336SAlexander Graf "Boots an EFI payload from memory\n", 230b9939336SAlexander Graf bootefi_help_text 231b9939336SAlexander Graf ); 2320f4060ebSAlexander Graf 233c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path) 2340f4060ebSAlexander Graf { 2358c3df0bfSAlexander Graf __maybe_unused struct blk_desc *desc; 236ecbe1a07SAlexander Graf char devname[32] = { 0 }; /* dp->str is u16[32] long */ 2370f4060ebSAlexander Graf char *colon; 2380f4060ebSAlexander Graf 2390f4060ebSAlexander Graf /* Assemble the condensed device name we use in efi_disk.c */ 2400f4060ebSAlexander Graf snprintf(devname, sizeof(devname), "%s%s", dev, devnr); 2410f4060ebSAlexander Graf colon = strchr(devname, ':'); 2428c3df0bfSAlexander Graf 2438c3df0bfSAlexander Graf #ifdef CONFIG_ISO_PARTITION 2448c3df0bfSAlexander Graf /* For ISOs we create partition block devices */ 2458c3df0bfSAlexander Graf desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10)); 2468c3df0bfSAlexander Graf if (desc && (desc->type != DEV_TYPE_UNKNOWN) && 2478c3df0bfSAlexander Graf (desc->part_type == PART_TYPE_ISO)) { 2488c3df0bfSAlexander Graf if (!colon) 2498c3df0bfSAlexander Graf snprintf(devname, sizeof(devname), "%s%s:1", dev, 2508c3df0bfSAlexander Graf devnr); 2518c3df0bfSAlexander Graf colon = NULL; 2528c3df0bfSAlexander Graf } 2538c3df0bfSAlexander Graf #endif 2548c3df0bfSAlexander Graf 2550f4060ebSAlexander Graf if (colon) 2560f4060ebSAlexander Graf *colon = '\0'; 2570f4060ebSAlexander Graf 258c07ad7c0SAlexander Graf /* Patch bootefi_device_path to the target device */ 259c07ad7c0SAlexander Graf memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str)); 260c07ad7c0SAlexander Graf ascii2unicode(bootefi_device_path[0].str, devname); 261c07ad7c0SAlexander Graf 262c07ad7c0SAlexander Graf /* Patch bootefi_image_path to the target file path */ 2630f4060ebSAlexander Graf memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str)); 264c07ad7c0SAlexander Graf snprintf(devname, sizeof(devname), "%s", path); 2650f4060ebSAlexander Graf ascii2unicode(bootefi_image_path[0].str, devname); 2660f4060ebSAlexander Graf } 267