1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application loader
5 * Copyright (c) 2016 Alexander Graf
12 #include <efi_loader.h>
13 #include <efi_selftest.h>
15 #include <linux/libfdt.h>
16 #include <linux/libfdt_env.h>
19 #include <asm/global_data.h>
20 #include <asm-generic/sections.h>
21 #include <asm-generic/unaligned.h>
22 #include <linux/linkage.h>
24 #ifdef CONFIG_ARMV7_NONSEC
25 #include <asm/armv7.h>
26 #include <asm/secure.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 #define OBJ_LIST_NOT_INITIALIZED 1
33 static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
35 static struct efi_device_path *bootefi_image_path;
36 static struct efi_device_path *bootefi_device_path;
38 /* Initialize and populate EFI object list */
39 efi_status_t efi_init_obj_list(void)
41 efi_status_t ret = EFI_SUCCESS;
44 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
45 * As this register may be overwritten by an EFI payload we save it here
46 * and restore it on every callback entered.
50 /* Initialize once only */
51 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
52 return efi_obj_list_initialized;
54 /* Initialize system table */
55 ret = efi_initialize_system_table();
56 if (ret != EFI_SUCCESS)
59 /* Initialize root node */
60 ret = efi_root_node_register();
61 if (ret != EFI_SUCCESS)
64 /* Initialize EFI driver uclass */
65 ret = efi_driver_init();
66 if (ret != EFI_SUCCESS)
69 ret = efi_console_register();
70 if (ret != EFI_SUCCESS)
72 #ifdef CONFIG_PARTITIONS
73 ret = efi_disk_register();
74 if (ret != EFI_SUCCESS)
77 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
78 ret = efi_gop_register();
79 if (ret != EFI_SUCCESS)
83 ret = efi_net_register();
84 if (ret != EFI_SUCCESS)
87 #ifdef CONFIG_GENERATE_ACPI_TABLE
88 ret = efi_acpi_register();
89 if (ret != EFI_SUCCESS)
92 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
93 ret = efi_smbios_register();
94 if (ret != EFI_SUCCESS)
97 ret = efi_watchdog_register();
98 if (ret != EFI_SUCCESS)
101 /* Initialize EFI runtime services */
102 ret = efi_reset_system_init();
103 if (ret != EFI_SUCCESS)
107 efi_obj_list_initialized = ret;
112 * Allow unaligned memory access.
114 * This routine is overridden by architectures providing this feature.
116 void __weak allow_unaligned(void)
121 * Set the load options of an image from an environment variable.
123 * @loaded_image_info: the image
124 * @env_var: name of the environment variable
126 static void set_load_options(struct efi_loaded_image *loaded_image_info,
130 const char *env = env_get(env_var);
133 loaded_image_info->load_options = NULL;
134 loaded_image_info->load_options_size = 0;
137 size = utf8_utf16_strlen(env) + 1;
138 loaded_image_info->load_options = calloc(size, sizeof(u16));
139 if (!loaded_image_info->load_options) {
140 printf("ERROR: Out of memory\n");
143 pos = loaded_image_info->load_options;
144 utf8_utf16_strcpy(&pos, env);
145 loaded_image_info->load_options_size = size * 2;
149 * copy_fdt() - Copy the device tree to a new location available to EFI
151 * The FDT is relocated into a suitable location within the EFI memory map.
152 * An additional 12KB is added to the space in case the device tree needs to be
153 * expanded later with fdt_open_into().
155 * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated
157 * @fdt_sizep: Returns new size of FDT, including
158 * @return new relocated address of FDT
160 static efi_status_t copy_fdt(ulong *fdt_addrp, ulong *fdt_sizep)
162 unsigned long fdt_ram_start = -1L, fdt_pages;
163 efi_status_t ret = 0;
169 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
170 u64 ram_start = gd->bd->bi_dram[i].start;
171 u64 ram_size = gd->bd->bi_dram[i].size;
176 if (ram_start < fdt_ram_start)
177 fdt_ram_start = ram_start;
181 * Give us at least 4KB of breathing room in case the device tree needs
182 * to be expanded later. Round up to the nearest EFI page boundary.
184 fdt = map_sysmem(*fdt_addrp, 0);
185 fdt_size = fdt_totalsize(fdt);
186 fdt_size += 4096 * 3;
187 fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE);
188 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
190 /* Safe fdt location is at 127MB */
191 new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size;
192 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
193 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
195 if (ret != EFI_SUCCESS) {
196 /* If we can't put it there, put it somewhere */
197 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
198 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
199 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
201 if (ret != EFI_SUCCESS) {
202 printf("ERROR: Failed to reserve space for FDT\n");
207 new_fdt = map_sysmem(new_fdt_addr, fdt_size);
208 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
209 fdt_set_totalsize(new_fdt, fdt_size);
211 *fdt_addrp = new_fdt_addr;
212 *fdt_sizep = fdt_size;
217 static efi_status_t efi_do_enter(
218 efi_handle_t image_handle, struct efi_system_table *st,
219 EFIAPI efi_status_t (*entry)(
220 efi_handle_t image_handle,
221 struct efi_system_table *st))
223 efi_status_t ret = EFI_LOAD_ERROR;
226 ret = entry(image_handle, st);
227 st->boottime->exit(image_handle, ret, 0, NULL);
232 static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
233 efi_handle_t image_handle, struct efi_system_table *st),
234 efi_handle_t image_handle, struct efi_system_table *st)
236 /* Enable caches again */
239 return efi_do_enter(image_handle, st, entry);
243 #ifdef CONFIG_ARMV7_NONSEC
244 static bool is_nonsec;
246 static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
247 efi_handle_t image_handle, struct efi_system_table *st),
248 efi_handle_t image_handle, struct efi_system_table *st)
250 /* Enable caches again */
255 return efi_do_enter(image_handle, st, entry);
260 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
262 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
263 * ignored because this is not critical and we would rather continue to try to
266 * @fdt: Pointer to device tree
268 static void efi_carve_out_dt_rsv(void *fdt)
271 uint64_t addr, size, pages;
273 nr_rsv = fdt_num_mem_rsv(fdt);
275 /* Look for an existing entry and add it to the efi mem map. */
276 for (i = 0; i < nr_rsv; i++) {
277 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
280 pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT;
281 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
283 printf("FDT memrsv map %d: Failed to add to map\n", i);
287 static efi_status_t efi_install_fdt(ulong fdt_addr)
289 bootm_headers_t img = { 0 };
290 ulong fdt_pages, fdt_size, fdt_start;
294 fdt = map_sysmem(fdt_addr, 0);
295 if (fdt_check_header(fdt)) {
296 printf("ERROR: invalid device tree\n");
297 return EFI_INVALID_PARAMETER;
300 /* Prepare fdt for payload */
301 ret = copy_fdt(&fdt_addr, &fdt_size);
306 fdt = map_sysmem(fdt_addr, 0);
307 fdt_size = fdt_totalsize(fdt);
308 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
309 printf("ERROR: failed to process device tree\n");
310 return EFI_LOAD_ERROR;
313 efi_carve_out_dt_rsv(fdt);
315 /* Link to it in the efi tables */
316 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
317 if (ret != EFI_SUCCESS)
318 return EFI_OUT_OF_RESOURCES;
320 /* And reserve the space in the memory map */
321 fdt_start = fdt_addr;
322 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
324 ret = efi_add_memory_map(fdt_start, fdt_pages,
325 EFI_BOOT_SERVICES_DATA, true);
331 * do_bootefi_exec() - execute EFI binary
333 * @efi: address of the binary
334 * @device_path: path of the device from which the binary was loaded
335 * @image_path: device path of the binary
336 * Return: status code
338 * Load the EFI binary into a newly assigned memory unwinding the relocation
339 * information, install the loaded image protocol, and call the binary.
341 static efi_status_t do_bootefi_exec(void *efi,
342 struct efi_device_path *device_path,
343 struct efi_device_path *image_path)
345 efi_handle_t mem_handle = NULL;
346 struct efi_device_path *memdp = NULL;
348 struct efi_loaded_image_obj *image_handle = NULL;
349 struct efi_loaded_image *loaded_image_info = NULL;
351 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
352 struct efi_system_table *st);
355 * Special case for efi payload not loaded from disk, such as
356 * 'bootefi hello' or for example payload loaded directly into
357 * memory via jtag, etc:
359 if (!device_path && !image_path) {
360 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
361 /* actual addresses filled in after efi_load_pe() */
362 memdp = efi_dp_from_mem(0, 0, 0);
363 device_path = image_path = memdp;
365 * Grub expects that the device path of the loaded image is
366 * installed on a handle.
368 ret = efi_create_handle(&mem_handle);
369 if (ret != EFI_SUCCESS)
371 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
373 if (ret != EFI_SUCCESS)
376 assert(device_path && image_path);
379 ret = efi_setup_loaded_image(device_path, image_path, &image_handle,
381 if (ret != EFI_SUCCESS)
384 /* Transfer environment variable bootargs as load options */
385 set_load_options(loaded_image_info, "bootargs");
386 /* Load the EFI payload */
387 entry = efi_load_pe(image_handle, efi, loaded_image_info);
389 ret = EFI_LOAD_ERROR;
394 struct efi_device_path_memory *mdp = (void *)memdp;
395 mdp->memory_type = loaded_image_info->image_code_type;
396 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
397 mdp->end_address = mdp->start_address +
398 loaded_image_info->image_size;
401 /* we don't support much: */
402 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
403 "{ro,boot}(blob)0000000000000000");
405 /* Call our payload! */
406 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
408 if (setjmp(&image_handle->exit_jmp)) {
409 ret = image_handle->exit_status;
414 /* On AArch64 we need to make sure we call our payload in < EL3 */
415 if (current_el() == 3) {
417 dcache_disable(); /* flush cache before switch to EL2 */
419 /* Move into EL2 and keep running there */
420 armv8_switch_to_el2((ulong)entry,
422 (ulong)&systab, 0, (ulong)efi_run_in_el2,
425 /* Should never reach here, efi exits with longjmp */
430 #ifdef CONFIG_ARMV7_NONSEC
431 if (armv7_boot_nonsec() && !is_nonsec) {
432 dcache_disable(); /* flush cache before switch to HYP */
435 secure_ram_addr(_do_nonsec_entry)(
438 (uintptr_t)image_handle,
441 /* Should never reach here, efi exits with longjmp */
446 ret = efi_do_enter(image_handle, &systab, entry);
449 /* image has returned, loaded-image obj goes *poof*: */
451 efi_delete_handle(&image_handle->parent);
453 efi_delete_handle(mem_handle);
458 static int do_bootefi_bootmgr_exec(void)
460 struct efi_device_path *device_path, *file_path;
464 addr = efi_bootmgr_load(&device_path, &file_path);
468 printf("## Starting EFI application at %p ...\n", addr);
469 r = do_bootefi_exec(addr, device_path, file_path);
470 printf("## Application terminated, r = %lu\n",
471 r & ~EFI_ERROR_MASK);
473 if (r != EFI_SUCCESS)
479 /* Interpreter command to boot an arbitrary EFI image from memory */
480 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
485 unsigned long fdt_addr;
487 /* Allow unaligned memory access */
490 /* Initialize EFI drivers */
491 r = efi_init_obj_list();
492 if (r != EFI_SUCCESS) {
493 printf("Error: Cannot set up EFI drivers, r = %lu\n",
494 r & ~EFI_ERROR_MASK);
495 return CMD_RET_FAILURE;
499 return CMD_RET_USAGE;
502 fdt_addr = simple_strtoul(argv[2], NULL, 16);
503 if (!fdt_addr && *argv[2] != '0')
504 return CMD_RET_USAGE;
505 /* Install device tree */
506 r = efi_install_fdt(fdt_addr);
507 if (r != EFI_SUCCESS) {
508 printf("ERROR: failed to install device tree\n");
509 return CMD_RET_FAILURE;
512 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
513 efi_install_configuration_table(&efi_guid_fdt, NULL);
514 printf("WARNING: booting without device tree\n");
516 #ifdef CONFIG_CMD_BOOTEFI_HELLO
517 if (!strcmp(argv[1], "hello")) {
518 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
520 saddr = env_get("loadaddr");
522 addr = simple_strtoul(saddr, NULL, 16);
524 addr = CONFIG_SYS_LOAD_ADDR;
525 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
528 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
529 if (!strcmp(argv[1], "selftest")) {
530 struct efi_loaded_image_obj *image_handle;
531 struct efi_loaded_image *loaded_image_info;
533 /* Construct a dummy device path. */
534 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
535 (uintptr_t)&efi_selftest,
536 (uintptr_t)&efi_selftest);
537 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
539 r = efi_setup_loaded_image(bootefi_device_path,
540 bootefi_image_path, &image_handle,
542 if (r != EFI_SUCCESS)
543 return CMD_RET_FAILURE;
546 /* Transfer environment variable efi_selftest as load options */
547 set_load_options(loaded_image_info, "efi_selftest");
548 /* Execute the test */
549 r = efi_selftest(image_handle, &systab);
551 free(loaded_image_info->load_options);
552 efi_delete_handle(&image_handle->parent);
553 return r != EFI_SUCCESS;
556 if (!strcmp(argv[1], "bootmgr")) {
557 return do_bootefi_bootmgr_exec();
561 addr = simple_strtoul(saddr, NULL, 16);
562 /* Check that a numeric value was passed */
563 if (!addr && *saddr != '0')
564 return CMD_RET_USAGE;
568 printf("## Starting EFI application at %08lx ...\n", addr);
569 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
571 printf("## Application terminated, r = %lu\n",
572 r & ~EFI_ERROR_MASK);
574 if (r != EFI_SUCCESS)
580 #ifdef CONFIG_SYS_LONGHELP
581 static char bootefi_help_text[] =
582 "<image address> [fdt address]\n"
583 " - boot EFI payload stored at address <image address>.\n"
584 " If specified, the device tree located at <fdt address> gets\n"
585 " exposed as EFI configuration table.\n"
586 #ifdef CONFIG_CMD_BOOTEFI_HELLO
588 " - boot a sample Hello World application stored within U-Boot\n"
590 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
591 "bootefi selftest [fdt address]\n"
592 " - boot an EFI selftest application stored within U-Boot\n"
593 " Use environment variable efi_selftest to select a single test.\n"
594 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
596 "bootefi bootmgr [fdt addr]\n"
597 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
599 " If specified, the device tree located at <fdt address> gets\n"
600 " exposed as EFI configuration table.\n";
604 bootefi, 3, 0, do_bootefi,
605 "Boots an EFI payload from memory",
609 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
611 char filename[32] = { 0 }; /* dp->str is u16[32] long */
614 /* efi_set_bootdev is typically called repeatedly, recover memory */
615 efi_free_pool(bootefi_device_path);
616 efi_free_pool(bootefi_image_path);
617 /* If blk_get_device_part_str fails, avoid duplicate free. */
618 bootefi_device_path = NULL;
619 bootefi_image_path = NULL;
621 if (strcmp(dev, "Net")) {
622 struct blk_desc *desc;
623 disk_partition_t fs_partition;
626 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
631 bootefi_device_path = efi_dp_from_part(desc, part);
634 bootefi_device_path = efi_dp_from_eth();
641 if (strcmp(dev, "Net")) {
642 /* Add leading / to fs paths, because they're absolute */
643 snprintf(filename, sizeof(filename), "/%s", path);
645 snprintf(filename, sizeof(filename), "%s", path);
647 /* DOS style file path: */
649 while ((s = strchr(s, '/')))
651 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);