1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application loader
5 * Copyright (c) 2016 Alexander Graf
8 #define LOG_CATEGORY LOGC_EFI
14 #include <efi_loader.h>
15 #include <efi_selftest.h>
21 #include <linux/libfdt.h>
22 #include <linux/libfdt_env.h>
25 #include <asm-generic/sections.h>
26 #include <linux/linkage.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 static struct efi_device_path *bootefi_image_path;
31 static struct efi_device_path *bootefi_device_path;
32 static void *image_addr;
33 static size_t image_size;
36 * efi_clear_bootdev() - clear boot device
38 static void efi_clear_bootdev(void)
40 efi_free_pool(bootefi_device_path);
41 efi_free_pool(bootefi_image_path);
42 bootefi_device_path = NULL;
43 bootefi_image_path = NULL;
49 * efi_set_bootdev() - set boot device
51 * This function is called when a file is loaded, e.g. via the 'load' command.
52 * We use the path to this file to inform the UEFI binary about the boot device.
54 * @dev: device, e.g. "MMC"
55 * @devnr: number of the device, e.g. "1:2"
56 * @path: path to file loaded
57 * @buffer: buffer with file loaded
58 * @buffer_size: size of file loaded
60 void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
61 void *buffer, size_t buffer_size)
63 struct efi_device_path *device, *image;
66 /* Forget overwritten image */
67 if (buffer + buffer_size >= image_addr &&
68 image_addr + image_size >= buffer)
71 /* Remember only PE-COFF and FIT images */
72 if (efi_check_pe(buffer, buffer_size, NULL) != EFI_SUCCESS) {
74 if (!fit_check_format(buffer))
77 * FIT images of type EFI_OS are started via command bootm.
78 * We should not use their boot device with the bootefi command.
87 /* efi_set_bootdev() is typically called repeatedly, recover memory */
91 image_size = buffer_size;
93 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
94 if (ret == EFI_SUCCESS) {
95 bootefi_device_path = device;
97 /* FIXME: image should not contain device */
98 struct efi_device_path *image_tmp = image;
100 efi_dp_split_file_path(image, &device, &image);
101 efi_free_pool(image_tmp);
103 bootefi_image_path = image;
110 * efi_env_set_load_options() - set load options from environment variable
112 * @handle: the image handle
113 * @env_var: name of the environment variable
114 * @load_options: pointer to load options (output)
115 * Return: status code
117 static efi_status_t efi_env_set_load_options(efi_handle_t handle,
121 const char *env = env_get(env_var);
126 *load_options = NULL;
129 size = sizeof(u16) * (utf8_utf16_strlen(env) + 1);
130 pos = calloc(size, 1);
132 return EFI_OUT_OF_RESOURCES;
134 utf8_utf16_strcpy(&pos, env);
135 ret = efi_set_load_options(handle, size, *load_options);
136 if (ret != EFI_SUCCESS) {
138 *load_options = NULL;
143 #if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
146 * copy_fdt() - Copy the device tree to a new location available to EFI
148 * The FDT is copied to a suitable location within the EFI memory map.
149 * Additional 12 KiB are added to the space in case the device tree needs to be
150 * expanded later with fdt_open_into().
152 * @fdtp: On entry a pointer to the flattened device tree.
153 * On exit a pointer to the copy of the flattened device tree.
155 * Return: status code
157 static efi_status_t copy_fdt(void **fdtp)
159 unsigned long fdt_ram_start = -1L, fdt_pages;
160 efi_status_t ret = 0;
166 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
167 u64 ram_start = gd->bd->bi_dram[i].start;
168 u64 ram_size = gd->bd->bi_dram[i].size;
173 if (ram_start < fdt_ram_start)
174 fdt_ram_start = ram_start;
178 * Give us at least 12 KiB of breathing room in case the device tree
179 * needs to be expanded later.
182 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
183 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
186 * Safe fdt location is at 127 MiB.
187 * On the sandbox convert from the sandbox address space.
189 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
191 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
192 EFI_ACPI_RECLAIM_MEMORY, 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_ACPI_RECLAIM_MEMORY, fdt_pages,
200 if (ret != EFI_SUCCESS) {
201 log_err("ERROR: Failed to reserve space for FDT\n");
205 new_fdt = (void *)(uintptr_t)new_fdt_addr;
206 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
207 fdt_set_totalsize(new_fdt, fdt_size);
209 *fdtp = (void *)(uintptr_t)new_fdt_addr;
215 * efi_reserve_memory() - add reserved memory to memory map
217 * @addr: start address of the reserved memory range
218 * @size: size of the reserved memory range
219 * @nomap: indicates that the memory range shall not be accessed by the
222 static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
227 /* Convert from sandbox address space. */
228 addr = (uintptr_t)map_sysmem(addr, 0);
231 type = EFI_RESERVED_MEMORY_TYPE;
233 type = EFI_BOOT_SERVICES_DATA;
235 ret = efi_add_memory_map(addr, size, type);
236 if (ret != EFI_SUCCESS)
237 log_err("Reserved memory mapping failed addr %llx size %llx\n",
242 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
244 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
245 * ignored because this is not critical and we would rather continue to try to
248 * @fdt: Pointer to device tree
250 static void efi_carve_out_dt_rsv(void *fdt)
254 int nodeoffset, subnode;
256 nr_rsv = fdt_num_mem_rsv(fdt);
258 /* Look for an existing entry and add it to the efi mem map. */
259 for (i = 0; i < nr_rsv; i++) {
260 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
262 efi_reserve_memory(addr, size, false);
265 /* process reserved-memory */
266 nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
267 if (nodeoffset >= 0) {
268 subnode = fdt_first_subnode(fdt, nodeoffset);
269 while (subnode >= 0) {
273 /* check if this subnode has a reg property */
274 fdt_addr = fdtdec_get_addr_size_auto_parent(
275 fdt, nodeoffset, subnode,
276 "reg", 0, &fdt_size, false);
278 * The /reserved-memory node may have children with
279 * a size instead of a reg property.
281 if (fdt_addr != FDT_ADDR_T_NONE &&
282 fdtdec_get_is_enabled(fdt, subnode)) {
285 nomap = !!fdt_getprop(fdt, subnode, "no-map",
287 efi_reserve_memory(fdt_addr, fdt_size, nomap);
289 subnode = fdt_next_subnode(fdt, subnode);
295 * get_config_table() - get configuration table
297 * @guid: GUID of the configuration table
298 * Return: pointer to configuration table or NULL
300 static void *get_config_table(const efi_guid_t *guid)
304 for (i = 0; i < systab.nr_tables; i++) {
305 if (!guidcmp(guid, &systab.tables[i].guid))
306 return systab.tables[i].table;
311 #endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
314 * efi_install_fdt() - install device tree
316 * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
317 * address will will be installed as configuration table, otherwise the device
318 * tree located at the address indicated by environment variable fdt_addr or as
319 * fallback fdtcontroladdr will be used.
321 * On architectures using ACPI tables device trees shall not be installed as
322 * configuration table.
324 * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
325 * the hardware device tree as indicated by environment variable
326 * fdt_addr or as fallback the internal device tree as indicated by
327 * the environment variable fdtcontroladdr
328 * Return: status code
330 efi_status_t efi_install_fdt(void *fdt)
333 * The EBBR spec requires that we have either an FDT or an ACPI table
336 #if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
338 log_err("ERROR: can't have ACPI table and device tree.\n");
339 return EFI_LOAD_ERROR;
342 bootm_headers_t img = { 0 };
345 if (fdt == EFI_FDT_USE_INTERNAL) {
349 /* Look for device tree that is already installed */
350 if (get_config_table(&efi_guid_fdt))
352 /* Check if there is a hardware device tree */
353 fdt_opt = env_get("fdt_addr");
354 /* Use our own device tree as fallback */
356 fdt_opt = env_get("fdtcontroladdr");
358 log_err("ERROR: need device tree\n");
359 return EFI_NOT_FOUND;
362 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
364 log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
365 return EFI_LOAD_ERROR;
367 fdt = map_sysmem(fdt_addr, 0);
370 /* Install device tree */
371 if (fdt_check_header(fdt)) {
372 log_err("ERROR: invalid device tree\n");
373 return EFI_LOAD_ERROR;
376 /* Prepare device tree for payload */
377 ret = copy_fdt(&fdt);
379 log_err("ERROR: out of memory\n");
380 return EFI_OUT_OF_RESOURCES;
383 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
384 log_err("ERROR: failed to process device tree\n");
385 return EFI_LOAD_ERROR;
388 /* Create memory reservations as indicated by the device tree */
389 efi_carve_out_dt_rsv(fdt);
391 /* Install device tree as UEFI table */
392 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
393 if (ret != EFI_SUCCESS) {
394 log_err("ERROR: failed to install device tree\n");
397 #endif /* GENERATE_ACPI_TABLE */
403 * do_bootefi_exec() - execute EFI binary
405 * The image indicated by @handle is started. When it returns the allocated
406 * memory for the @load_options is freed.
408 * @handle: handle of loaded image
409 * @load_options: load options
410 * Return: status code
412 * Load the EFI binary into a newly assigned memory unwinding the relocation
413 * information, install the loaded image protocol, and call the binary.
415 static efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options)
418 efi_uintn_t exit_data_size = 0;
419 u16 *exit_data = NULL;
421 /* Call our payload! */
422 ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data));
423 if (ret != EFI_SUCCESS) {
424 log_err("## Application failed, r = %lu\n",
425 ret & ~EFI_ERROR_MASK);
427 log_err("## %ls\n", exit_data);
428 efi_free_pool(exit_data);
440 * do_efibootmgr() - execute EFI boot manager
442 * Return: status code
444 static int do_efibootmgr(void)
450 ret = efi_bootmgr_load(&handle, &load_options);
451 if (ret != EFI_SUCCESS) {
452 log_notice("EFI boot manager: Cannot load any image\n");
453 return CMD_RET_FAILURE;
456 ret = do_bootefi_exec(handle, load_options);
458 if (ret != EFI_SUCCESS)
459 return CMD_RET_FAILURE;
461 return CMD_RET_SUCCESS;
465 * do_bootefi_image() - execute EFI binary
467 * Set up memory image for the binary to be loaded, prepare device path, and
468 * then call do_bootefi_exec() to execute it.
470 * @image_opt: string of image start address
471 * Return: status code
473 static int do_bootefi_image(const char *image_opt)
476 unsigned long addr, size;
479 #ifdef CONFIG_CMD_BOOTEFI_HELLO
480 if (!strcmp(image_opt, "hello")) {
481 image_buf = __efi_helloworld_begin;
482 size = __efi_helloworld_end - __efi_helloworld_begin;
487 addr = strtoul(image_opt, NULL, 16);
488 /* Check that a numeric value was passed */
490 return CMD_RET_USAGE;
492 image_buf = map_sysmem(addr, 0);
494 if (image_buf != image_addr) {
495 log_err("No UEFI binary known at %s\n", image_opt);
496 return CMD_RET_FAILURE;
500 ret = efi_run_image(image_buf, size);
502 if (ret != EFI_SUCCESS)
503 return CMD_RET_FAILURE;
505 return CMD_RET_SUCCESS;
509 * efi_run_image() - run loaded UEFI image
511 * @source_buffer: memory address of the UEFI image
512 * @source_size: size of the UEFI image
513 * Return: status code
515 efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
517 efi_handle_t mem_handle = NULL, handle;
518 struct efi_device_path *file_path = NULL;
519 struct efi_device_path *msg_path;
523 if (!bootefi_device_path || !bootefi_image_path) {
525 * Special case for efi payload not loaded from disk,
526 * such as 'bootefi hello' or for example payload
527 * loaded directly into memory via JTAG, etc:
529 file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
530 (uintptr_t)source_buffer,
533 * Make sure that device for device_path exist
534 * in load_image(). Otherwise, shell and grub will fail.
536 ret = efi_create_handle(&mem_handle);
537 if (ret != EFI_SUCCESS)
540 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
542 if (ret != EFI_SUCCESS)
544 msg_path = file_path;
546 file_path = efi_dp_append(bootefi_device_path,
548 msg_path = bootefi_image_path;
551 log_info("Booting %pD\n", msg_path);
553 ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
554 source_size, &handle));
555 if (ret != EFI_SUCCESS) {
556 log_err("Loading image failed\n");
560 /* Transfer environment variable as load options */
561 ret = efi_env_set_load_options(handle, "bootargs", &load_options);
562 if (ret != EFI_SUCCESS)
565 ret = do_bootefi_exec(handle, load_options);
568 efi_delete_handle(mem_handle);
569 efi_free_pool(file_path);
573 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
574 static efi_status_t bootefi_run_prepare(const char *load_options_path,
575 struct efi_device_path *device_path,
576 struct efi_device_path *image_path,
577 struct efi_loaded_image_obj **image_objp,
578 struct efi_loaded_image **loaded_image_infop)
583 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
585 if (ret != EFI_SUCCESS)
588 /* Transfer environment variable as load options */
589 return efi_env_set_load_options((efi_handle_t)*image_objp,
595 * bootefi_test_prepare() - prepare to run an EFI test
597 * Prepare to run a test as if it were provided by a loaded image.
599 * @image_objp: pointer to be set to the loaded image handle
600 * @loaded_image_infop: pointer to be set to the loaded image protocol
601 * @path: dummy file path used to construct the device path
602 * set in the loaded image protocol
603 * @load_options_path: name of a U-Boot environment variable. Its value is
604 * set as load options in the loaded image protocol.
605 * Return: status code
607 static efi_status_t bootefi_test_prepare
608 (struct efi_loaded_image_obj **image_objp,
609 struct efi_loaded_image **loaded_image_infop, const char *path,
610 const char *load_options_path)
614 /* Construct a dummy device path */
615 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
616 if (!bootefi_device_path)
617 return EFI_OUT_OF_RESOURCES;
619 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
620 if (!bootefi_image_path) {
621 ret = EFI_OUT_OF_RESOURCES;
625 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
626 bootefi_image_path, image_objp,
628 if (ret == EFI_SUCCESS)
637 * bootefi_run_finish() - finish up after running an EFI test
639 * @loaded_image_info: Pointer to a struct which holds the loaded image info
640 * @image_obj: Pointer to a struct which holds the loaded image object
642 static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
643 struct efi_loaded_image *loaded_image_info)
646 free(loaded_image_info->load_options);
647 efi_delete_handle(&image_obj->header);
651 * do_efi_selftest() - execute EFI selftest
653 * Return: status code
655 static int do_efi_selftest(void)
657 struct efi_loaded_image_obj *image_obj;
658 struct efi_loaded_image *loaded_image_info;
661 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
662 "\\selftest", "efi_selftest");
663 if (ret != EFI_SUCCESS)
664 return CMD_RET_FAILURE;
666 /* Execute the test */
667 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
668 bootefi_run_finish(image_obj, loaded_image_info);
670 return ret != EFI_SUCCESS;
672 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
675 * do_bootefi() - execute `bootefi` command
677 * @cmdtp: table entry describing command
678 * @flag: bitmap indicating how the command was invoked
679 * @argc: number of arguments
680 * @argv: command line arguments
681 * Return: status code
683 static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
690 return CMD_RET_USAGE;
692 /* Initialize EFI drivers */
693 ret = efi_init_obj_list();
694 if (ret != EFI_SUCCESS) {
695 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
696 ret & ~EFI_ERROR_MASK);
697 return CMD_RET_FAILURE;
703 fdt_addr = simple_strtoul(argv[2], NULL, 16);
704 fdt = map_sysmem(fdt_addr, 0);
706 fdt = EFI_FDT_USE_INTERNAL;
708 ret = efi_install_fdt(fdt);
709 if (ret == EFI_INVALID_PARAMETER)
710 return CMD_RET_USAGE;
711 else if (ret != EFI_SUCCESS)
712 return CMD_RET_FAILURE;
714 if (!strcmp(argv[1], "bootmgr"))
715 return do_efibootmgr();
716 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
717 else if (!strcmp(argv[1], "selftest"))
718 return do_efi_selftest();
721 return do_bootefi_image(argv[1]);
724 #ifdef CONFIG_SYS_LONGHELP
725 static char bootefi_help_text[] =
726 "<image address> [fdt address]\n"
727 " - boot EFI payload stored at address <image address>.\n"
728 " If specified, the device tree located at <fdt address> gets\n"
729 " exposed as EFI configuration table.\n"
730 #ifdef CONFIG_CMD_BOOTEFI_HELLO
732 " - boot a sample Hello World application stored within U-Boot\n"
734 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
735 "bootefi selftest [fdt address]\n"
736 " - boot an EFI selftest application stored within U-Boot\n"
737 " Use environment variable efi_selftest to select a single test.\n"
738 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
740 "bootefi bootmgr [fdt address]\n"
741 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
743 " If specified, the device tree located at <fdt address> gets\n"
744 " exposed as EFI configuration table.\n";
748 bootefi, 3, 0, do_bootefi,
749 "Boots an EFI payload from memory",