2 * EFI application loader
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <efi_loader.h>
15 #include <libfdt_env.h>
17 #include <asm/global_data.h>
18 #include <asm-generic/sections.h>
19 #include <linux/linkage.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 static uint8_t efi_obj_list_initalized;
25 static struct efi_device_path *bootefi_image_path;
26 static struct efi_device_path *bootefi_device_path;
28 /* Initialize and populate EFI object list */
29 static void efi_init_obj_list(void)
31 efi_obj_list_initalized = 1;
33 efi_console_register();
34 #ifdef CONFIG_PARTITIONS
37 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
43 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
44 efi_smbios_register();
46 efi_watchdog_register();
48 /* Initialize EFI runtime services */
49 efi_reset_system_init();
53 static void *copy_fdt(void *fdt)
55 u64 fdt_size = fdt_totalsize(fdt);
56 unsigned long fdt_ram_start = -1L, fdt_pages;
61 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
62 u64 ram_start = gd->bd->bi_dram[i].start;
63 u64 ram_size = gd->bd->bi_dram[i].size;
68 if (ram_start < fdt_ram_start)
69 fdt_ram_start = ram_start;
72 /* Give us at least 4kb breathing room */
73 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
74 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
76 /* Safe fdt location is at 128MB */
77 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
78 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
79 &new_fdt_addr) != EFI_SUCCESS) {
80 /* If we can't put it there, put it somewhere */
81 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
82 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
83 &new_fdt_addr) != EFI_SUCCESS) {
84 printf("ERROR: Failed to reserve space for FDT\n");
89 new_fdt = (void*)(ulong)new_fdt_addr;
90 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
91 fdt_set_totalsize(new_fdt, fdt_size);
96 static efi_status_t efi_do_enter(
97 void *image_handle, struct efi_system_table *st,
98 asmlinkage ulong (*entry)(void *image_handle,
99 struct efi_system_table *st))
101 efi_status_t ret = EFI_LOAD_ERROR;
104 ret = entry(image_handle, st);
105 st->boottime->exit(image_handle, ret, 0, NULL);
110 static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)(
111 void *image_handle, struct efi_system_table *st),
112 void *image_handle, struct efi_system_table *st)
114 /* Enable caches again */
117 return efi_do_enter(image_handle, st, entry);
122 * Load an EFI payload into a newly allocated piece of memory, register all
123 * EFI objects it would want to access and jump to it.
125 static efi_status_t do_bootefi_exec(void *efi, void *fdt,
126 struct efi_device_path *device_path,
127 struct efi_device_path *image_path)
129 struct efi_loaded_image loaded_image_info = {};
130 struct efi_object loaded_image_info_obj = {};
131 struct efi_device_path *memdp = NULL;
134 ulong (*entry)(void *image_handle, struct efi_system_table *st)
136 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
137 const efi_guid_t fdt_guid = EFI_FDT_GUID;
138 bootm_headers_t img = { 0 };
141 * Special case for efi payload not loaded from disk, such as
142 * 'bootefi hello' or for example payload loaded directly into
143 * memory via jtag/etc:
145 if (!device_path && !image_path) {
146 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
147 /* actual addresses filled in after efi_load_pe() */
148 memdp = efi_dp_from_mem(0, 0, 0);
149 device_path = image_path = memdp;
151 assert(device_path && image_path);
154 /* Initialize and populate EFI object list */
155 if (!efi_obj_list_initalized)
158 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
159 device_path, image_path);
162 * gd lives in a fixed register which may get clobbered while we execute
163 * the payload. So save it here and restore it on every callback entry
167 if (fdt && !fdt_check_header(fdt)) {
168 /* Prepare fdt for payload */
171 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
172 printf("ERROR: Failed to process device tree\n");
176 /* Link to it in the efi tables */
177 efi_install_configuration_table(&fdt_guid, fdt);
179 /* And reserve the space in the memory map */
180 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
181 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
182 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
183 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
184 /* Give a bootloader the chance to modify the device tree */
186 efi_add_memory_map(fdt_start, fdt_pages,
187 EFI_BOOT_SERVICES_DATA, true);
189 printf("WARNING: Invalid device tree, expect boot to fail\n");
190 efi_install_configuration_table(&fdt_guid, NULL);
193 /* Load the EFI payload */
194 entry = efi_load_pe(efi, &loaded_image_info);
201 struct efi_device_path_memory *mdp = (void *)memdp;
202 mdp->memory_type = loaded_image_info.image_code_type;
203 mdp->start_address = (uintptr_t)loaded_image_info.image_base;
204 mdp->end_address = mdp->start_address +
205 loaded_image_info.image_size;
208 /* we don't support much: */
209 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
210 "{ro,boot}(blob)0000000000000000");
212 /* Call our payload! */
213 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
215 if (setjmp(&loaded_image_info.exit_jmp)) {
216 ret = loaded_image_info.exit_status;
221 /* On AArch64 we need to make sure we call our payload in < EL3 */
222 if (current_el() == 3) {
224 dcache_disable(); /* flush cache before switch to EL2 */
226 /* Move into EL2 and keep running there */
227 armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
228 (ulong)&systab, 0, (ulong)efi_run_in_el2,
231 /* Should never reach here, efi exits with longjmp */
236 ret = efi_do_enter(&loaded_image_info, &systab, entry);
239 /* image has returned, loaded-image obj goes *poof*: */
240 list_del(&loaded_image_info_obj.link);
245 static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
247 struct efi_device_path *device_path, *file_path;
251 /* Initialize and populate EFI object list */
252 if (!efi_obj_list_initalized)
256 * gd lives in a fixed register which may get clobbered while we execute
257 * the payload. So save it here and restore it on every callback entry
261 addr = efi_bootmgr_load(&device_path, &file_path);
265 printf("## Starting EFI application at %p ...\n", addr);
266 r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
267 printf("## Application terminated, r = %lu\n",
268 r & ~EFI_ERROR_MASK);
270 if (r != EFI_SUCCESS)
276 /* Interpreter command to boot an arbitrary EFI image from memory */
277 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
280 unsigned long addr, fdt_addr = 0;
284 return CMD_RET_USAGE;
285 #ifdef CONFIG_CMD_BOOTEFI_HELLO
286 if (!strcmp(argv[1], "hello")) {
287 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
289 saddr = env_get("loadaddr");
291 addr = simple_strtoul(saddr, NULL, 16);
293 addr = CONFIG_SYS_LOAD_ADDR;
294 memcpy((char *)addr, __efi_helloworld_begin, size);
297 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
298 if (!strcmp(argv[1], "selftest")) {
299 struct efi_loaded_image loaded_image_info = {};
300 struct efi_object loaded_image_info_obj = {};
302 /* Construct a dummy device path. */
303 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
304 (uintptr_t)&efi_selftest,
305 (uintptr_t)&efi_selftest);
306 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
308 efi_setup_loaded_image(&loaded_image_info,
309 &loaded_image_info_obj,
310 bootefi_device_path, bootefi_image_path);
312 * gd lives in a fixed register which may get clobbered while we
313 * execute the payload. So save it here and restore it on every
317 /* Initialize and populate EFI object list */
318 if (!efi_obj_list_initalized)
320 return efi_selftest(&loaded_image_info, &systab);
323 if (!strcmp(argv[1], "bootmgr")) {
324 unsigned long fdt_addr = 0;
327 fdt_addr = simple_strtoul(argv[2], NULL, 16);
329 return do_bootefi_bootmgr_exec(fdt_addr);
333 addr = simple_strtoul(saddr, NULL, 16);
337 fdt_addr = simple_strtoul(sfdt, NULL, 16);
341 printf("## Starting EFI application at %08lx ...\n", addr);
342 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
343 bootefi_device_path, bootefi_image_path);
344 printf("## Application terminated, r = %lu\n",
345 r & ~EFI_ERROR_MASK);
347 if (r != EFI_SUCCESS)
353 #ifdef CONFIG_SYS_LONGHELP
354 static char bootefi_help_text[] =
355 "<image address> [fdt address]\n"
356 " - boot EFI payload stored at address <image address>.\n"
357 " If specified, the device tree located at <fdt address> gets\n"
358 " exposed as EFI configuration table.\n"
359 #ifdef CONFIG_CMD_BOOTEFI_HELLO
361 " - boot a sample Hello World application stored within U-Boot\n"
363 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
365 " - boot an EFI selftest application stored within U-Boot\n"
367 "bootmgr [fdt addr]\n"
368 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
370 " If specified, the device tree located at <fdt address> gets\n"
371 " exposed as EFI configuration table.\n";
375 bootefi, 3, 0, do_bootefi,
376 "Boots an EFI payload from memory",
380 static int parse_partnum(const char *devnr)
382 const char *str = strchr(devnr, ':');
385 return simple_strtoul(str, NULL, 16);
390 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
392 char filename[32] = { 0 }; /* dp->str is u16[32] long */
395 if (strcmp(dev, "Net")) {
396 struct blk_desc *desc;
399 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
400 part = parse_partnum(devnr);
402 bootefi_device_path = efi_dp_from_part(desc, part);
405 bootefi_device_path = efi_dp_from_eth();
412 if (strcmp(dev, "Net")) {
413 /* Add leading / to fs paths, because they're absolute */
414 snprintf(filename, sizeof(filename), "/%s", path);
416 snprintf(filename, sizeof(filename), "%s", path);
418 /* DOS style file path: */
420 while ((s = strchr(s, '/')))
422 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);