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 * Return: status code
159 static efi_status_t
copy_fdt(ulong
*fdt_addrp
)
161 unsigned long fdt_ram_start
= -1L, fdt_pages
;
162 efi_status_t ret
= 0;
168 for (i
= 0; i
< CONFIG_NR_DRAM_BANKS
; i
++) {
169 u64 ram_start
= gd
->bd
->bi_dram
[i
].start
;
170 u64 ram_size
= gd
->bd
->bi_dram
[i
].size
;
175 if (ram_start
< fdt_ram_start
)
176 fdt_ram_start
= ram_start
;
180 * Give us at least 4KB of breathing room in case the device tree needs
181 * to be expanded later. Round up to the nearest EFI page boundary.
183 fdt
= map_sysmem(*fdt_addrp
, 0);
184 fdt_size
= fdt_totalsize(fdt
);
185 fdt_size
+= 4096 * 3;
186 fdt_size
= ALIGN(fdt_size
+ EFI_PAGE_SIZE
- 1, EFI_PAGE_SIZE
);
187 fdt_pages
= fdt_size
>> EFI_PAGE_SHIFT
;
189 /* Safe fdt location is at 127MB */
190 new_fdt_addr
= fdt_ram_start
+ (127 * 1024 * 1024) + fdt_size
;
191 ret
= efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS
,
192 EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
194 if (ret
!= EFI_SUCCESS
) {
195 /* If we can't put it there, put it somewhere */
196 new_fdt_addr
= (ulong
)memalign(EFI_PAGE_SIZE
, fdt_size
);
197 ret
= efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS
,
198 EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
200 if (ret
!= EFI_SUCCESS
) {
201 printf("ERROR: Failed to reserve space for FDT\n");
206 new_fdt
= map_sysmem(new_fdt_addr
, fdt_size
);
207 memcpy(new_fdt
, fdt
, fdt_totalsize(fdt
));
208 fdt_set_totalsize(new_fdt
, fdt_size
);
210 *fdt_addrp
= new_fdt_addr
;
215 static efi_status_t
efi_do_enter(
216 efi_handle_t image_handle
, struct efi_system_table
*st
,
217 EFIAPI
efi_status_t (*entry
)(
218 efi_handle_t image_handle
,
219 struct efi_system_table
*st
))
221 efi_status_t ret
= EFI_LOAD_ERROR
;
224 ret
= entry(image_handle
, st
);
225 st
->boottime
->exit(image_handle
, ret
, 0, NULL
);
230 static efi_status_t
efi_run_in_el2(EFIAPI
efi_status_t (*entry
)(
231 efi_handle_t image_handle
, struct efi_system_table
*st
),
232 efi_handle_t image_handle
, struct efi_system_table
*st
)
234 /* Enable caches again */
237 return efi_do_enter(image_handle
, st
, entry
);
241 #ifdef CONFIG_ARMV7_NONSEC
242 static bool is_nonsec
;
244 static efi_status_t
efi_run_in_hyp(EFIAPI
efi_status_t (*entry
)(
245 efi_handle_t image_handle
, struct efi_system_table
*st
),
246 efi_handle_t image_handle
, struct efi_system_table
*st
)
248 /* Enable caches again */
253 return efi_do_enter(image_handle
, st
, entry
);
258 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
260 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
261 * ignored because this is not critical and we would rather continue to try to
264 * @fdt: Pointer to device tree
266 static void efi_carve_out_dt_rsv(void *fdt
)
269 uint64_t addr
, size
, pages
;
271 nr_rsv
= fdt_num_mem_rsv(fdt
);
273 /* Look for an existing entry and add it to the efi mem map. */
274 for (i
= 0; i
< nr_rsv
; i
++) {
275 if (fdt_get_mem_rsv(fdt
, i
, &addr
, &size
) != 0)
279 * Do not carve out the device tree. It is already marked as
280 * EFI_RUNTIME_SERVICES_DATA
282 if (addr
== (uintptr_t)fdt
)
285 pages
= ALIGN(size
+ (addr
& EFI_PAGE_MASK
), EFI_PAGE_SIZE
) >>
287 addr
&= ~EFI_PAGE_MASK
;
288 if (!efi_add_memory_map(addr
, pages
, EFI_RESERVED_MEMORY_TYPE
,
290 printf("FDT memrsv map %d: Failed to add to map\n", i
);
294 static efi_status_t
efi_install_fdt(ulong fdt_addr
)
296 bootm_headers_t img
= { 0 };
300 fdt
= map_sysmem(fdt_addr
, 0);
301 if (fdt_check_header(fdt
)) {
302 printf("ERROR: invalid device tree\n");
303 return EFI_INVALID_PARAMETER
;
306 /* Prepare fdt for payload */
307 ret
= copy_fdt(&fdt_addr
);
312 fdt
= map_sysmem(fdt_addr
, 0);
313 if (image_setup_libfdt(&img
, fdt
, 0, NULL
)) {
314 printf("ERROR: failed to process device tree\n");
315 return EFI_LOAD_ERROR
;
318 efi_carve_out_dt_rsv(fdt
);
320 /* Link to it in the efi tables */
321 ret
= efi_install_configuration_table(&efi_guid_fdt
, fdt
);
322 if (ret
!= EFI_SUCCESS
)
323 return EFI_OUT_OF_RESOURCES
;
328 static efi_status_t
bootefi_run_prepare(const char *load_options_path
,
329 struct efi_device_path
*device_path
,
330 struct efi_device_path
*image_path
,
331 struct efi_loaded_image_obj
**image_objp
,
332 struct efi_loaded_image
**loaded_image_infop
)
336 ret
= efi_setup_loaded_image(device_path
, image_path
, image_objp
,
338 if (ret
!= EFI_SUCCESS
)
341 /* Transfer environment variable as load options */
342 set_load_options(*loaded_image_infop
, load_options_path
);
348 * bootefi_run_finish() - finish up after running an EFI test
350 * @loaded_image_info: Pointer to a struct which holds the loaded image info
351 * @image_objj: Pointer to a struct which holds the loaded image object
353 static void bootefi_run_finish(struct efi_loaded_image_obj
*image_obj
,
354 struct efi_loaded_image
*loaded_image_info
)
357 free(loaded_image_info
->load_options
);
358 efi_delete_handle(&image_obj
->header
);
362 * do_bootefi_exec() - execute EFI binary
364 * @efi: address of the binary
365 * @device_path: path of the device from which the binary was loaded
366 * @image_path: device path of the binary
367 * Return: status code
369 * Load the EFI binary into a newly assigned memory unwinding the relocation
370 * information, install the loaded image protocol, and call the binary.
372 static efi_status_t
do_bootefi_exec(void *efi
,
373 struct efi_device_path
*device_path
,
374 struct efi_device_path
*image_path
)
376 efi_handle_t mem_handle
= NULL
;
377 struct efi_device_path
*memdp
= NULL
;
379 struct efi_loaded_image_obj
*image_obj
= NULL
;
380 struct efi_loaded_image
*loaded_image_info
= NULL
;
382 EFIAPI
efi_status_t (*entry
)(efi_handle_t image_handle
,
383 struct efi_system_table
*st
);
386 * Special case for efi payload not loaded from disk, such as
387 * 'bootefi hello' or for example payload loaded directly into
388 * memory via JTAG, etc:
390 if (!device_path
&& !image_path
) {
391 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
392 /* actual addresses filled in after efi_load_pe() */
393 memdp
= efi_dp_from_mem(0, 0, 0);
394 device_path
= image_path
= memdp
;
396 * Grub expects that the device path of the loaded image is
397 * installed on a handle.
399 ret
= efi_create_handle(&mem_handle
);
400 if (ret
!= EFI_SUCCESS
)
401 return ret
; /* TODO: leaks device_path */
402 ret
= efi_add_protocol(mem_handle
, &efi_guid_device_path
,
404 if (ret
!= EFI_SUCCESS
)
405 goto err_add_protocol
;
407 assert(device_path
&& image_path
);
410 ret
= bootefi_run_prepare("bootargs", device_path
, image_path
,
411 &image_obj
, &loaded_image_info
);
415 /* Load the EFI payload */
416 entry
= efi_load_pe(image_obj
, efi
, loaded_image_info
);
418 ret
= EFI_LOAD_ERROR
;
423 struct efi_device_path_memory
*mdp
= (void *)memdp
;
424 mdp
->memory_type
= loaded_image_info
->image_code_type
;
425 mdp
->start_address
= (uintptr_t)loaded_image_info
->image_base
;
426 mdp
->end_address
= mdp
->start_address
+
427 loaded_image_info
->image_size
;
430 /* we don't support much: */
431 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
432 "{ro,boot}(blob)0000000000000000");
434 /* Call our payload! */
435 debug("%s:%d Jumping to 0x%lx\n", __func__
, __LINE__
, (long)entry
);
437 if (setjmp(&image_obj
->exit_jmp
)) {
438 ret
= image_obj
->exit_status
;
443 /* On AArch64 we need to make sure we call our payload in < EL3 */
444 if (current_el() == 3) {
446 dcache_disable(); /* flush cache before switch to EL2 */
448 /* Move into EL2 and keep running there */
449 armv8_switch_to_el2((ulong
)entry
,
450 (ulong
)&image_obj
->header
,
451 (ulong
)&systab
, 0, (ulong
)efi_run_in_el2
,
454 /* Should never reach here, efi exits with longjmp */
459 #ifdef CONFIG_ARMV7_NONSEC
460 if (armv7_boot_nonsec() && !is_nonsec
) {
461 dcache_disable(); /* flush cache before switch to HYP */
464 secure_ram_addr(_do_nonsec_entry
)(
467 (uintptr_t)&image_obj
->header
,
470 /* Should never reach here, efi exits with longjmp */
475 ret
= efi_do_enter(&image_obj
->header
, &systab
, entry
);
478 /* image has returned, loaded-image obj goes *poof*: */
479 bootefi_run_finish(image_obj
, loaded_image_info
);
483 efi_delete_handle(mem_handle
);
488 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
490 * bootefi_test_prepare() - prepare to run an EFI test
492 * This sets things up so we can call EFI functions. This involves preparing
493 * the 'gd' pointer and setting up the load ed image data structures.
495 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
496 * loaded image object. This struct will be inited by this function before
498 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
499 * info. This struct will be inited by this function before use.
500 * @path: File path to the test being run (often just the test name with a
501 * backslash before it
502 * @test_func: Address of the test function that is being run
503 * @load_options_path: U-Boot environment variable to use as load options
504 * @return 0 if OK, -ve on error
506 static efi_status_t bootefi_test_prepare
507 (struct efi_loaded_image_obj
**image_objp
,
508 struct efi_loaded_image
**loaded_image_infop
, const char *path
,
509 ulong test_func
, const char *load_options_path
)
511 /* Construct a dummy device path */
512 bootefi_device_path
= efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE
,
513 (uintptr_t)test_func
,
514 (uintptr_t)test_func
);
515 if (!bootefi_device_path
)
516 return EFI_OUT_OF_RESOURCES
;
517 bootefi_image_path
= efi_dp_from_file(NULL
, 0, path
);
518 if (!bootefi_image_path
)
519 return EFI_OUT_OF_RESOURCES
;
521 return bootefi_run_prepare(load_options_path
, bootefi_device_path
,
522 bootefi_image_path
, image_objp
,
526 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
528 static int do_bootefi_bootmgr_exec(void)
530 struct efi_device_path
*device_path
, *file_path
;
534 addr
= efi_bootmgr_load(&device_path
, &file_path
);
538 printf("## Starting EFI application at %p ...\n", addr
);
539 r
= do_bootefi_exec(addr
, device_path
, file_path
);
540 printf("## Application terminated, r = %lu\n",
541 r
& ~EFI_ERROR_MASK
);
543 if (r
!= EFI_SUCCESS
)
549 /* Interpreter command to boot an arbitrary EFI image from memory */
550 static int do_bootefi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
555 unsigned long fdt_addr
;
557 /* Allow unaligned memory access */
560 /* Initialize EFI drivers */
561 r
= efi_init_obj_list();
562 if (r
!= EFI_SUCCESS
) {
563 printf("Error: Cannot set up EFI drivers, r = %lu\n",
564 r
& ~EFI_ERROR_MASK
);
565 return CMD_RET_FAILURE
;
569 return CMD_RET_USAGE
;
572 fdt_addr
= simple_strtoul(argv
[2], NULL
, 16);
573 if (!fdt_addr
&& *argv
[2] != '0')
574 return CMD_RET_USAGE
;
575 /* Install device tree */
576 r
= efi_install_fdt(fdt_addr
);
577 if (r
!= EFI_SUCCESS
) {
578 printf("ERROR: failed to install device tree\n");
579 return CMD_RET_FAILURE
;
582 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
583 efi_install_configuration_table(&efi_guid_fdt
, NULL
);
584 printf("WARNING: booting without device tree\n");
586 #ifdef CONFIG_CMD_BOOTEFI_HELLO
587 if (!strcmp(argv
[1], "hello")) {
588 ulong size
= __efi_helloworld_end
- __efi_helloworld_begin
;
590 saddr
= env_get("loadaddr");
592 addr
= simple_strtoul(saddr
, NULL
, 16);
594 addr
= CONFIG_SYS_LOAD_ADDR
;
595 memcpy(map_sysmem(addr
, size
), __efi_helloworld_begin
, size
);
598 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
599 if (!strcmp(argv
[1], "selftest")) {
600 struct efi_loaded_image_obj
*image_obj
;
601 struct efi_loaded_image
*loaded_image_info
;
603 if (bootefi_test_prepare(&image_obj
, &loaded_image_info
,
604 "\\selftest", (uintptr_t)&efi_selftest
,
606 return CMD_RET_FAILURE
;
608 /* Execute the test */
609 r
= efi_selftest(&image_obj
->header
, &systab
);
610 bootefi_run_finish(image_obj
, loaded_image_info
);
611 return r
!= EFI_SUCCESS
;
614 if (!strcmp(argv
[1], "bootmgr")) {
615 return do_bootefi_bootmgr_exec();
619 addr
= simple_strtoul(saddr
, NULL
, 16);
620 /* Check that a numeric value was passed */
621 if (!addr
&& *saddr
!= '0')
622 return CMD_RET_USAGE
;
626 printf("## Starting EFI application at %08lx ...\n", addr
);
627 r
= do_bootefi_exec(map_sysmem(addr
, 0), bootefi_device_path
,
629 printf("## Application terminated, r = %lu\n",
630 r
& ~EFI_ERROR_MASK
);
632 if (r
!= EFI_SUCCESS
)
638 #ifdef CONFIG_SYS_LONGHELP
639 static char bootefi_help_text
[] =
640 "<image address> [fdt address]\n"
641 " - boot EFI payload stored at address <image address>.\n"
642 " If specified, the device tree located at <fdt address> gets\n"
643 " exposed as EFI configuration table.\n"
644 #ifdef CONFIG_CMD_BOOTEFI_HELLO
646 " - boot a sample Hello World application stored within U-Boot\n"
648 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
649 "bootefi selftest [fdt address]\n"
650 " - boot an EFI selftest application stored within U-Boot\n"
651 " Use environment variable efi_selftest to select a single test.\n"
652 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
654 "bootefi bootmgr [fdt addr]\n"
655 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
657 " If specified, the device tree located at <fdt address> gets\n"
658 " exposed as EFI configuration table.\n";
662 bootefi
, 3, 0, do_bootefi
,
663 "Boots an EFI payload from memory",
667 void efi_set_bootdev(const char *dev
, const char *devnr
, const char *path
)
669 struct efi_device_path
*device
, *image
;
672 /* efi_set_bootdev is typically called repeatedly, recover memory */
673 efi_free_pool(bootefi_device_path
);
674 efi_free_pool(bootefi_image_path
);
676 ret
= efi_dp_from_name(dev
, devnr
, path
, &device
, &image
);
677 if (ret
== EFI_SUCCESS
) {
678 bootefi_device_path
= device
;
679 bootefi_image_path
= image
;
681 bootefi_device_path
= NULL
;
682 bootefi_image_path
= NULL
;