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 static struct efi_device_path *bootefi_image_path;
32 static struct efi_device_path *bootefi_device_path;
35 * Allow unaligned memory access.
37 * This routine is overridden by architectures providing this feature.
39 void __weak allow_unaligned(void)
44 * Set the load options of an image from an environment variable.
46 * @loaded_image_info: the image
47 * @env_var: name of the environment variable
49 static void set_load_options(struct efi_loaded_image *loaded_image_info,
53 const char *env = env_get(env_var);
56 loaded_image_info->load_options = NULL;
57 loaded_image_info->load_options_size = 0;
60 size = utf8_utf16_strlen(env) + 1;
61 loaded_image_info->load_options = calloc(size, sizeof(u16));
62 if (!loaded_image_info->load_options) {
63 printf("ERROR: Out of memory\n");
66 pos = loaded_image_info->load_options;
67 utf8_utf16_strcpy(&pos, env);
68 loaded_image_info->load_options_size = size * 2;
72 * copy_fdt() - Copy the device tree to a new location available to EFI
74 * The FDT is copied to a suitable location within the EFI memory map.
75 * Additional 12 KiB are added to the space in case the device tree needs to be
76 * expanded later with fdt_open_into().
78 * @fdtp: On entry a pointer to the flattened device tree.
79 * On exit a pointer to the copy of the flattened device tree.
83 static efi_status_t copy_fdt(void **fdtp)
85 unsigned long fdt_ram_start = -1L, fdt_pages;
92 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
93 u64 ram_start = gd->bd->bi_dram[i].start;
94 u64 ram_size = gd->bd->bi_dram[i].size;
99 if (ram_start < fdt_ram_start)
100 fdt_ram_start = ram_start;
104 * Give us at least 12 KiB of breathing room in case the device tree
105 * needs to be expanded later.
108 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
109 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
112 * Safe fdt location is at 127 MiB.
113 * On the sandbox convert from the sandbox address space.
115 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
117 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
118 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
120 if (ret != EFI_SUCCESS) {
121 /* If we can't put it there, put it somewhere */
122 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
123 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
124 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
126 if (ret != EFI_SUCCESS) {
127 printf("ERROR: Failed to reserve space for FDT\n");
131 new_fdt = (void *)(uintptr_t)new_fdt_addr;
132 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
133 fdt_set_totalsize(new_fdt, fdt_size);
135 *fdtp = (void *)(uintptr_t)new_fdt_addr;
140 static efi_status_t efi_do_enter(
141 efi_handle_t image_handle, struct efi_system_table *st,
142 EFIAPI efi_status_t (*entry)(
143 efi_handle_t image_handle,
144 struct efi_system_table *st))
146 efi_status_t ret = EFI_LOAD_ERROR;
149 ret = entry(image_handle, st);
150 st->boottime->exit(image_handle, ret, 0, NULL);
155 static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
156 efi_handle_t image_handle, struct efi_system_table *st),
157 efi_handle_t image_handle, struct efi_system_table *st)
159 /* Enable caches again */
162 return efi_do_enter(image_handle, st, entry);
166 #ifdef CONFIG_ARMV7_NONSEC
167 static bool is_nonsec;
169 static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
170 efi_handle_t image_handle, struct efi_system_table *st),
171 efi_handle_t image_handle, struct efi_system_table *st)
173 /* Enable caches again */
178 return efi_do_enter(image_handle, st, entry);
183 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
185 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
186 * ignored because this is not critical and we would rather continue to try to
189 * @fdt: Pointer to device tree
191 static void efi_carve_out_dt_rsv(void *fdt)
194 uint64_t addr, size, pages;
196 nr_rsv = fdt_num_mem_rsv(fdt);
198 /* Look for an existing entry and add it to the efi mem map. */
199 for (i = 0; i < nr_rsv; i++) {
200 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
203 /* Convert from sandbox address space. */
204 addr = (uintptr_t)map_sysmem(addr, 0);
206 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
207 addr &= ~EFI_PAGE_MASK;
208 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
210 printf("FDT memrsv map %d: Failed to add to map\n", i);
214 static efi_status_t efi_install_fdt(ulong fdt_addr)
216 bootm_headers_t img = { 0 };
220 fdt = map_sysmem(fdt_addr, 0);
221 if (fdt_check_header(fdt)) {
222 printf("ERROR: invalid device tree\n");
223 return EFI_INVALID_PARAMETER;
226 /* Create memory reservation as indicated by the device tree */
227 efi_carve_out_dt_rsv(fdt);
229 /* Prepare fdt for payload */
230 ret = copy_fdt(&fdt);
234 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
235 printf("ERROR: failed to process device tree\n");
236 return EFI_LOAD_ERROR;
239 /* Link to it in the efi tables */
240 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
241 if (ret != EFI_SUCCESS)
242 return EFI_OUT_OF_RESOURCES;
247 static efi_status_t bootefi_run_prepare(const char *load_options_path,
248 struct efi_device_path *device_path,
249 struct efi_device_path *image_path,
250 struct efi_loaded_image_obj **image_objp,
251 struct efi_loaded_image **loaded_image_infop)
255 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
257 if (ret != EFI_SUCCESS)
260 /* Transfer environment variable as load options */
261 set_load_options(*loaded_image_infop, load_options_path);
267 * bootefi_run_finish() - finish up after running an EFI test
269 * @loaded_image_info: Pointer to a struct which holds the loaded image info
270 * @image_objj: Pointer to a struct which holds the loaded image object
272 static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
273 struct efi_loaded_image *loaded_image_info)
276 free(loaded_image_info->load_options);
277 efi_delete_handle(&image_obj->header);
281 * do_bootefi_exec() - execute EFI binary
283 * @efi: address of the binary
284 * @device_path: path of the device from which the binary was loaded
285 * @image_path: device path of the binary
286 * Return: status code
288 * Load the EFI binary into a newly assigned memory unwinding the relocation
289 * information, install the loaded image protocol, and call the binary.
291 static efi_status_t do_bootefi_exec(void *efi,
292 struct efi_device_path *device_path,
293 struct efi_device_path *image_path)
295 efi_handle_t mem_handle = NULL;
296 struct efi_device_path *memdp = NULL;
298 struct efi_loaded_image_obj *image_obj = NULL;
299 struct efi_loaded_image *loaded_image_info = NULL;
301 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
302 struct efi_system_table *st);
305 * Special case for efi payload not loaded from disk, such as
306 * 'bootefi hello' or for example payload loaded directly into
307 * memory via JTAG, etc:
309 if (!device_path && !image_path) {
310 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
311 /* actual addresses filled in after efi_load_pe() */
312 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
313 device_path = image_path = memdp;
315 * Grub expects that the device path of the loaded image is
316 * installed on a handle.
318 ret = efi_create_handle(&mem_handle);
319 if (ret != EFI_SUCCESS)
320 return ret; /* TODO: leaks device_path */
321 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
323 if (ret != EFI_SUCCESS)
324 goto err_add_protocol;
326 assert(device_path && image_path);
329 ret = bootefi_run_prepare("bootargs", device_path, image_path,
330 &image_obj, &loaded_image_info);
334 /* Load the EFI payload */
335 entry = efi_load_pe(image_obj, efi, loaded_image_info);
337 ret = EFI_LOAD_ERROR;
342 struct efi_device_path_memory *mdp = (void *)memdp;
343 mdp->memory_type = loaded_image_info->image_code_type;
344 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
345 mdp->end_address = mdp->start_address +
346 loaded_image_info->image_size;
349 /* we don't support much: */
350 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
351 "{ro,boot}(blob)0000000000000000");
353 /* Call our payload! */
354 debug("%s: Jumping to 0x%p\n", __func__, entry);
356 if (setjmp(&image_obj->exit_jmp)) {
357 ret = image_obj->exit_status;
362 /* On AArch64 we need to make sure we call our payload in < EL3 */
363 if (current_el() == 3) {
365 dcache_disable(); /* flush cache before switch to EL2 */
367 /* Move into EL2 and keep running there */
368 armv8_switch_to_el2((ulong)entry,
369 (ulong)&image_obj->header,
370 (ulong)&systab, 0, (ulong)efi_run_in_el2,
373 /* Should never reach here, efi exits with longjmp */
378 #ifdef CONFIG_ARMV7_NONSEC
379 if (armv7_boot_nonsec() && !is_nonsec) {
380 dcache_disable(); /* flush cache before switch to HYP */
383 secure_ram_addr(_do_nonsec_entry)(
386 (uintptr_t)&image_obj->header,
389 /* Should never reach here, efi exits with longjmp */
394 ret = efi_do_enter(&image_obj->header, &systab, entry);
397 /* image has returned, loaded-image obj goes *poof*: */
398 bootefi_run_finish(image_obj, loaded_image_info);
402 efi_delete_handle(mem_handle);
407 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
409 * bootefi_test_prepare() - prepare to run an EFI test
411 * This sets things up so we can call EFI functions. This involves preparing
412 * the 'gd' pointer and setting up the load ed image data structures.
414 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
415 * loaded image object. This struct will be inited by this function before
417 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
418 * info. This struct will be inited by this function before use.
419 * @path: File path to the test being run (often just the test name with a
420 * backslash before it
421 * @test_func: Address of the test function that is being run
422 * @load_options_path: U-Boot environment variable to use as load options
423 * @return 0 if OK, -ve on error
425 static efi_status_t bootefi_test_prepare
426 (struct efi_loaded_image_obj **image_objp,
427 struct efi_loaded_image **loaded_image_infop, const char *path,
428 ulong test_func, const char *load_options_path)
430 /* Construct a dummy device path */
431 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
432 (uintptr_t)test_func,
433 (uintptr_t)test_func);
434 if (!bootefi_device_path)
435 return EFI_OUT_OF_RESOURCES;
436 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
437 if (!bootefi_image_path)
438 return EFI_OUT_OF_RESOURCES;
440 return bootefi_run_prepare(load_options_path, bootefi_device_path,
441 bootefi_image_path, image_objp,
445 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
447 static int do_bootefi_bootmgr_exec(void)
449 struct efi_device_path *device_path, *file_path;
453 addr = efi_bootmgr_load(&device_path, &file_path);
457 printf("## Starting EFI application at %p ...\n", addr);
458 r = do_bootefi_exec(addr, device_path, file_path);
459 printf("## Application terminated, r = %lu\n",
460 r & ~EFI_ERROR_MASK);
462 if (r != EFI_SUCCESS)
468 /* Interpreter command to boot an arbitrary EFI image from memory */
469 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
474 unsigned long fdt_addr;
476 /* Allow unaligned memory access */
479 /* Initialize EFI drivers */
480 r = efi_init_obj_list();
481 if (r != EFI_SUCCESS) {
482 printf("Error: Cannot set up EFI drivers, r = %lu\n",
483 r & ~EFI_ERROR_MASK);
484 return CMD_RET_FAILURE;
488 return CMD_RET_USAGE;
491 fdt_addr = simple_strtoul(argv[2], NULL, 16);
492 if (!fdt_addr && *argv[2] != '0')
493 return CMD_RET_USAGE;
494 /* Install device tree */
495 r = efi_install_fdt(fdt_addr);
496 if (r != EFI_SUCCESS) {
497 printf("ERROR: failed to install device tree\n");
498 return CMD_RET_FAILURE;
501 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
502 efi_install_configuration_table(&efi_guid_fdt, NULL);
503 printf("WARNING: booting without device tree\n");
505 #ifdef CONFIG_CMD_BOOTEFI_HELLO
506 if (!strcmp(argv[1], "hello")) {
507 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
509 saddr = env_get("loadaddr");
511 addr = simple_strtoul(saddr, NULL, 16);
513 addr = CONFIG_SYS_LOAD_ADDR;
514 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
517 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
518 if (!strcmp(argv[1], "selftest")) {
519 struct efi_loaded_image_obj *image_obj;
520 struct efi_loaded_image *loaded_image_info;
522 if (bootefi_test_prepare(&image_obj, &loaded_image_info,
523 "\\selftest", (uintptr_t)&efi_selftest,
525 return CMD_RET_FAILURE;
527 /* Execute the test */
528 r = efi_selftest(&image_obj->header, &systab);
529 bootefi_run_finish(image_obj, loaded_image_info);
530 return r != EFI_SUCCESS;
533 if (!strcmp(argv[1], "bootmgr")) {
534 return do_bootefi_bootmgr_exec();
538 addr = simple_strtoul(saddr, NULL, 16);
539 /* Check that a numeric value was passed */
540 if (!addr && *saddr != '0')
541 return CMD_RET_USAGE;
545 printf("## Starting EFI application at %08lx ...\n", addr);
546 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
548 printf("## Application terminated, r = %lu\n",
549 r & ~EFI_ERROR_MASK);
551 if (r != EFI_SUCCESS)
557 #ifdef CONFIG_SYS_LONGHELP
558 static char bootefi_help_text[] =
559 "<image address> [fdt address]\n"
560 " - boot EFI payload stored at address <image address>.\n"
561 " If specified, the device tree located at <fdt address> gets\n"
562 " exposed as EFI configuration table.\n"
563 #ifdef CONFIG_CMD_BOOTEFI_HELLO
565 " - boot a sample Hello World application stored within U-Boot\n"
567 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
568 "bootefi selftest [fdt address]\n"
569 " - boot an EFI selftest application stored within U-Boot\n"
570 " Use environment variable efi_selftest to select a single test.\n"
571 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
573 "bootefi bootmgr [fdt addr]\n"
574 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
576 " If specified, the device tree located at <fdt address> gets\n"
577 " exposed as EFI configuration table.\n";
581 bootefi, 3, 0, do_bootefi,
582 "Boots an EFI payload from memory",
586 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
588 struct efi_device_path *device, *image;
591 /* efi_set_bootdev is typically called repeatedly, recover memory */
592 efi_free_pool(bootefi_device_path);
593 efi_free_pool(bootefi_image_path);
595 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
596 if (ret == EFI_SUCCESS) {
597 bootefi_device_path = device;
598 bootefi_image_path = image;
600 bootefi_device_path = NULL;
601 bootefi_image_path = NULL;