2 * EFI application loader
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
14 #include <libfdt_env.h>
17 * When booting using the "bootefi" command, we don't know which
18 * physical device the file came from. So we create a pseudo-device
19 * called "bootefi" with the device path /bootefi.
21 * In addition to the originating device we also declare the file path
22 * of "bootefi" based loads to be /bootefi.
24 static struct efi_device_path_file_path bootefi_image_path[] = {
26 .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
27 .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
28 .dp.length = sizeof(bootefi_image_path[0]),
29 .str = { 'b','o','o','t','e','f','i' },
31 .dp.type = DEVICE_PATH_TYPE_END,
32 .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
33 .dp.length = sizeof(bootefi_image_path[0]),
37 static efi_status_t bootefi_open_dp(void *handle, efi_guid_t *protocol,
38 void **protocol_interface, void *agent_handle,
39 void *controller_handle, uint32_t attributes)
41 *protocol_interface = bootefi_image_path;
45 /* The EFI loaded_image interface for the image executed via "bootefi" */
46 static struct efi_loaded_image loaded_image_info = {
47 .device_handle = bootefi_image_path,
48 .file_path = bootefi_image_path,
51 /* The EFI object struct for the image executed via "bootefi" */
52 static struct efi_object loaded_image_info_obj = {
53 .handle = &loaded_image_info,
57 * When asking for the loaded_image interface, just
58 * return handle which points to loaded_image_info
60 .guid = &efi_guid_loaded_image,
61 .open = &efi_return_handle,
65 * When asking for the device path interface, return
68 .guid = &efi_guid_device_path,
69 .open = &bootefi_open_dp,
74 /* The EFI object struct for the device the "bootefi" image was loaded from */
75 static struct efi_object bootefi_device_obj = {
76 .handle = bootefi_image_path,
79 /* When asking for the device path interface, return
80 * bootefi_image_path */
81 .guid = &efi_guid_device_path,
82 .open = &bootefi_open_dp,
88 * Load an EFI payload into a newly allocated piece of memory, register all
89 * EFI objects it would want to access and jump to it.
91 static unsigned long do_bootefi_exec(void *efi)
93 ulong (*entry)(void *image_handle, struct efi_system_table *st);
94 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
95 bootm_headers_t img = { 0 };
98 * gd lives in a fixed register which may get clobbered while we execute
99 * the payload. So save it here and restore it on every callback entry
103 /* Update system table to point to our currently loaded FDT */
106 /* Prepare fdt for payload */
107 if (image_setup_libfdt(&img, working_fdt, 0, NULL)) {
108 printf("ERROR: Failed to process device tree\n");
112 /* Link to it in the efi tables */
113 systab.tables[0].guid = EFI_FDT_GUID;
114 systab.tables[0].table = working_fdt;
115 systab.nr_tables = 1;
117 /* And reserve the space in the memory map */
118 fdt_start = ((ulong)working_fdt) & ~EFI_PAGE_MASK;
119 fdt_end = ((ulong)working_fdt) + fdt_totalsize(working_fdt);
120 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
121 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
122 /* Give a bootloader the chance to modify the device tree */
124 efi_add_memory_map(fdt_start, fdt_pages,
125 EFI_BOOT_SERVICES_DATA, true);
128 printf("WARNING: No device tree loaded, expect boot to fail\n");
129 systab.nr_tables = 0;
132 /* Load the EFI payload */
133 entry = efi_load_pe(efi, &loaded_image_info);
137 /* Initialize and populate EFI object list */
138 INIT_LIST_HEAD(&efi_obj_list);
139 list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
140 list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
141 #ifdef CONFIG_PARTITIONS
148 /* Call our payload! */
150 printf("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
152 return entry(&loaded_image_info, &systab);
156 /* Interpreter command to boot an arbitrary EFI image from memory */
157 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
167 addr = simple_strtoul(saddr, NULL, 16);
169 printf("## Starting EFI application at 0x%08lx ...\n", addr);
170 r = do_bootefi_exec((void *)addr);
171 printf("## Application terminated, r = %d\n", r);
179 #ifdef CONFIG_SYS_LONGHELP
180 static char bootefi_help_text[] =
182 " - boot EFI payload stored at address <image address>\n"
184 "Since most EFI payloads want to have a device tree provided, please\n"
185 "make sure you load a device tree using the fdt addr command before\n"
186 "executing bootefi.\n";
190 bootefi, 2, 0, do_bootefi,
191 "Boots an EFI payload from memory\n",
195 void efi_set_bootdev(const char *dev, const char *devnr)
197 char devname[16] = { 0 }; /* dp->str is u16[16] long */
200 /* Assemble the condensed device name we use in efi_disk.c */
201 snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
202 colon = strchr(devname, ':');
206 /* Patch the bootefi_image_path to the target device */
207 memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
208 ascii2unicode(bootefi_image_path[0].str, devname);