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 copied to a suitable location within the EFI memory map.
152 * Additional 12 KiB are added to the space in case the device tree needs to be
153 * expanded later with fdt_open_into().
155 * @fdtp: On entry a pointer to the flattened device tree.
156 * On exit a pointer to the copy of the flattened device tree.
158 * Return: status code
160 static efi_status_t
copy_fdt(void **fdtp
)
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 12 KiB of breathing room in case the device tree
182 * needs to be expanded later.
185 fdt_pages
= efi_size_in_pages(fdt_totalsize(fdt
) + 0x3000);
186 fdt_size
= fdt_pages
<< EFI_PAGE_SHIFT
;
189 * Safe fdt location is at 127 MiB.
190 * On the sandbox convert from the sandbox address space.
192 new_fdt_addr
= (uintptr_t)map_sysmem(fdt_ram_start
+ 0x7f00000 +
194 ret
= efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS
,
195 EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
197 if (ret
!= EFI_SUCCESS
) {
198 /* If we can't put it there, put it somewhere */
199 new_fdt_addr
= (ulong
)memalign(EFI_PAGE_SIZE
, fdt_size
);
200 ret
= efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS
,
201 EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
203 if (ret
!= EFI_SUCCESS
) {
204 printf("ERROR: Failed to reserve space for FDT\n");
208 new_fdt
= (void *)(uintptr_t)new_fdt_addr
;
209 memcpy(new_fdt
, fdt
, fdt_totalsize(fdt
));
210 fdt_set_totalsize(new_fdt
, fdt_size
);
212 *fdtp
= (void *)(uintptr_t)new_fdt_addr
;
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 /* Convert from sandbox address space. */
281 addr
= (uintptr_t)map_sysmem(addr
, 0);
283 pages
= efi_size_in_pages(size
+ (addr
& EFI_PAGE_MASK
));
284 addr
&= ~EFI_PAGE_MASK
;
285 if (!efi_add_memory_map(addr
, pages
, EFI_RESERVED_MEMORY_TYPE
,
287 printf("FDT memrsv map %d: Failed to add to map\n", i
);
291 static efi_status_t
efi_install_fdt(ulong fdt_addr
)
293 bootm_headers_t img
= { 0 };
297 fdt
= map_sysmem(fdt_addr
, 0);
298 if (fdt_check_header(fdt
)) {
299 printf("ERROR: invalid device tree\n");
300 return EFI_INVALID_PARAMETER
;
303 /* Create memory reservation as indicated by the device tree */
304 efi_carve_out_dt_rsv(fdt
);
306 /* Prepare fdt for payload */
307 ret
= copy_fdt(&fdt
);
311 if (image_setup_libfdt(&img
, fdt
, 0, NULL
)) {
312 printf("ERROR: failed to process device tree\n");
313 return EFI_LOAD_ERROR
;
316 /* Link to it in the efi tables */
317 ret
= efi_install_configuration_table(&efi_guid_fdt
, fdt
);
318 if (ret
!= EFI_SUCCESS
)
319 return EFI_OUT_OF_RESOURCES
;
324 static efi_status_t
bootefi_run_prepare(const char *load_options_path
,
325 struct efi_device_path
*device_path
,
326 struct efi_device_path
*image_path
,
327 struct efi_loaded_image_obj
**image_objp
,
328 struct efi_loaded_image
**loaded_image_infop
)
332 ret
= efi_setup_loaded_image(device_path
, image_path
, image_objp
,
334 if (ret
!= EFI_SUCCESS
)
337 /* Transfer environment variable as load options */
338 set_load_options(*loaded_image_infop
, load_options_path
);
344 * bootefi_run_finish() - finish up after running an EFI test
346 * @loaded_image_info: Pointer to a struct which holds the loaded image info
347 * @image_objj: Pointer to a struct which holds the loaded image object
349 static void bootefi_run_finish(struct efi_loaded_image_obj
*image_obj
,
350 struct efi_loaded_image
*loaded_image_info
)
353 free(loaded_image_info
->load_options
);
354 efi_delete_handle(&image_obj
->header
);
358 * do_bootefi_exec() - execute EFI binary
360 * @efi: address of the binary
361 * @device_path: path of the device from which the binary was loaded
362 * @image_path: device path of the binary
363 * Return: status code
365 * Load the EFI binary into a newly assigned memory unwinding the relocation
366 * information, install the loaded image protocol, and call the binary.
368 static efi_status_t
do_bootefi_exec(void *efi
,
369 struct efi_device_path
*device_path
,
370 struct efi_device_path
*image_path
)
372 efi_handle_t mem_handle
= NULL
;
373 struct efi_device_path
*memdp
= NULL
;
375 struct efi_loaded_image_obj
*image_obj
= NULL
;
376 struct efi_loaded_image
*loaded_image_info
= NULL
;
378 EFIAPI
efi_status_t (*entry
)(efi_handle_t image_handle
,
379 struct efi_system_table
*st
);
382 * Special case for efi payload not loaded from disk, such as
383 * 'bootefi hello' or for example payload loaded directly into
384 * memory via JTAG, etc:
386 if (!device_path
&& !image_path
) {
387 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
388 /* actual addresses filled in after efi_load_pe() */
389 memdp
= efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE
, 0, 0);
390 device_path
= image_path
= memdp
;
392 * Grub expects that the device path of the loaded image is
393 * installed on a handle.
395 ret
= efi_create_handle(&mem_handle
);
396 if (ret
!= EFI_SUCCESS
)
397 return ret
; /* TODO: leaks device_path */
398 ret
= efi_add_protocol(mem_handle
, &efi_guid_device_path
,
400 if (ret
!= EFI_SUCCESS
)
401 goto err_add_protocol
;
403 assert(device_path
&& image_path
);
406 ret
= bootefi_run_prepare("bootargs", device_path
, image_path
,
407 &image_obj
, &loaded_image_info
);
411 /* Load the EFI payload */
412 entry
= efi_load_pe(image_obj
, efi
, loaded_image_info
);
414 ret
= EFI_LOAD_ERROR
;
419 struct efi_device_path_memory
*mdp
= (void *)memdp
;
420 mdp
->memory_type
= loaded_image_info
->image_code_type
;
421 mdp
->start_address
= (uintptr_t)loaded_image_info
->image_base
;
422 mdp
->end_address
= mdp
->start_address
+
423 loaded_image_info
->image_size
;
426 /* we don't support much: */
427 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
428 "{ro,boot}(blob)0000000000000000");
430 /* Call our payload! */
431 debug("%s: Jumping to 0x%p\n", __func__
, entry
);
433 if (setjmp(&image_obj
->exit_jmp
)) {
434 ret
= image_obj
->exit_status
;
439 /* On AArch64 we need to make sure we call our payload in < EL3 */
440 if (current_el() == 3) {
442 dcache_disable(); /* flush cache before switch to EL2 */
444 /* Move into EL2 and keep running there */
445 armv8_switch_to_el2((ulong
)entry
,
446 (ulong
)&image_obj
->header
,
447 (ulong
)&systab
, 0, (ulong
)efi_run_in_el2
,
450 /* Should never reach here, efi exits with longjmp */
455 #ifdef CONFIG_ARMV7_NONSEC
456 if (armv7_boot_nonsec() && !is_nonsec
) {
457 dcache_disable(); /* flush cache before switch to HYP */
460 secure_ram_addr(_do_nonsec_entry
)(
463 (uintptr_t)&image_obj
->header
,
466 /* Should never reach here, efi exits with longjmp */
471 ret
= efi_do_enter(&image_obj
->header
, &systab
, entry
);
474 /* image has returned, loaded-image obj goes *poof*: */
475 bootefi_run_finish(image_obj
, loaded_image_info
);
479 efi_delete_handle(mem_handle
);
484 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
486 * bootefi_test_prepare() - prepare to run an EFI test
488 * This sets things up so we can call EFI functions. This involves preparing
489 * the 'gd' pointer and setting up the load ed image data structures.
491 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
492 * loaded image object. This struct will be inited by this function before
494 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
495 * info. This struct will be inited by this function before use.
496 * @path: File path to the test being run (often just the test name with a
497 * backslash before it
498 * @test_func: Address of the test function that is being run
499 * @load_options_path: U-Boot environment variable to use as load options
500 * @return 0 if OK, -ve on error
502 static efi_status_t bootefi_test_prepare
503 (struct efi_loaded_image_obj
**image_objp
,
504 struct efi_loaded_image
**loaded_image_infop
, const char *path
,
505 ulong test_func
, const char *load_options_path
)
507 /* Construct a dummy device path */
508 bootefi_device_path
= efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE
,
509 (uintptr_t)test_func
,
510 (uintptr_t)test_func
);
511 if (!bootefi_device_path
)
512 return EFI_OUT_OF_RESOURCES
;
513 bootefi_image_path
= efi_dp_from_file(NULL
, 0, path
);
514 if (!bootefi_image_path
)
515 return EFI_OUT_OF_RESOURCES
;
517 return bootefi_run_prepare(load_options_path
, bootefi_device_path
,
518 bootefi_image_path
, image_objp
,
522 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
524 static int do_bootefi_bootmgr_exec(void)
526 struct efi_device_path
*device_path
, *file_path
;
530 addr
= efi_bootmgr_load(&device_path
, &file_path
);
534 printf("## Starting EFI application at %p ...\n", addr
);
535 r
= do_bootefi_exec(addr
, device_path
, file_path
);
536 printf("## Application terminated, r = %lu\n",
537 r
& ~EFI_ERROR_MASK
);
539 if (r
!= EFI_SUCCESS
)
545 /* Interpreter command to boot an arbitrary EFI image from memory */
546 static int do_bootefi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
551 unsigned long fdt_addr
;
553 /* Allow unaligned memory access */
556 /* Initialize EFI drivers */
557 r
= efi_init_obj_list();
558 if (r
!= EFI_SUCCESS
) {
559 printf("Error: Cannot set up EFI drivers, r = %lu\n",
560 r
& ~EFI_ERROR_MASK
);
561 return CMD_RET_FAILURE
;
565 return CMD_RET_USAGE
;
568 fdt_addr
= simple_strtoul(argv
[2], NULL
, 16);
569 if (!fdt_addr
&& *argv
[2] != '0')
570 return CMD_RET_USAGE
;
571 /* Install device tree */
572 r
= efi_install_fdt(fdt_addr
);
573 if (r
!= EFI_SUCCESS
) {
574 printf("ERROR: failed to install device tree\n");
575 return CMD_RET_FAILURE
;
578 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
579 efi_install_configuration_table(&efi_guid_fdt
, NULL
);
580 printf("WARNING: booting without device tree\n");
582 #ifdef CONFIG_CMD_BOOTEFI_HELLO
583 if (!strcmp(argv
[1], "hello")) {
584 ulong size
= __efi_helloworld_end
- __efi_helloworld_begin
;
586 saddr
= env_get("loadaddr");
588 addr
= simple_strtoul(saddr
, NULL
, 16);
590 addr
= CONFIG_SYS_LOAD_ADDR
;
591 memcpy(map_sysmem(addr
, size
), __efi_helloworld_begin
, size
);
594 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
595 if (!strcmp(argv
[1], "selftest")) {
596 struct efi_loaded_image_obj
*image_obj
;
597 struct efi_loaded_image
*loaded_image_info
;
599 if (bootefi_test_prepare(&image_obj
, &loaded_image_info
,
600 "\\selftest", (uintptr_t)&efi_selftest
,
602 return CMD_RET_FAILURE
;
604 /* Execute the test */
605 r
= efi_selftest(&image_obj
->header
, &systab
);
606 bootefi_run_finish(image_obj
, loaded_image_info
);
607 return r
!= EFI_SUCCESS
;
610 if (!strcmp(argv
[1], "bootmgr")) {
611 return do_bootefi_bootmgr_exec();
615 addr
= simple_strtoul(saddr
, NULL
, 16);
616 /* Check that a numeric value was passed */
617 if (!addr
&& *saddr
!= '0')
618 return CMD_RET_USAGE
;
622 printf("## Starting EFI application at %08lx ...\n", addr
);
623 r
= do_bootefi_exec(map_sysmem(addr
, 0), bootefi_device_path
,
625 printf("## Application terminated, r = %lu\n",
626 r
& ~EFI_ERROR_MASK
);
628 if (r
!= EFI_SUCCESS
)
634 #ifdef CONFIG_SYS_LONGHELP
635 static char bootefi_help_text
[] =
636 "<image address> [fdt address]\n"
637 " - boot EFI payload stored at address <image address>.\n"
638 " If specified, the device tree located at <fdt address> gets\n"
639 " exposed as EFI configuration table.\n"
640 #ifdef CONFIG_CMD_BOOTEFI_HELLO
642 " - boot a sample Hello World application stored within U-Boot\n"
644 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
645 "bootefi selftest [fdt address]\n"
646 " - boot an EFI selftest application stored within U-Boot\n"
647 " Use environment variable efi_selftest to select a single test.\n"
648 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
650 "bootefi bootmgr [fdt addr]\n"
651 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
653 " If specified, the device tree located at <fdt address> gets\n"
654 " exposed as EFI configuration table.\n";
658 bootefi
, 3, 0, do_bootefi
,
659 "Boots an EFI payload from memory",
663 void efi_set_bootdev(const char *dev
, const char *devnr
, const char *path
)
665 struct efi_device_path
*device
, *image
;
668 /* efi_set_bootdev is typically called repeatedly, recover memory */
669 efi_free_pool(bootefi_device_path
);
670 efi_free_pool(bootefi_image_path
);
672 ret
= efi_dp_from_name(dev
, devnr
, path
, &device
, &image
);
673 if (ret
== EFI_SUCCESS
) {
674 bootefi_device_path
= device
;
675 bootefi_image_path
= image
;
677 bootefi_device_path
= NULL
;
678 bootefi_image_path
= NULL
;