1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application loader
5 * Copyright (c) 2016 Alexander Graf
13 #include <efi_loader.h>
14 #include <efi_selftest.h>
16 #include <linux/libfdt.h>
17 #include <linux/libfdt_env.h>
20 #include <asm/global_data.h>
21 #include <asm-generic/sections.h>
22 #include <asm-generic/unaligned.h>
23 #include <linux/linkage.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static struct efi_device_path *bootefi_image_path;
28 static struct efi_device_path *bootefi_device_path;
31 * Allow unaligned memory access.
33 * This routine is overridden by architectures providing this feature.
35 void __weak allow_unaligned(void)
40 * Set the load options of an image from an environment variable.
42 * @handle: the image handle
43 * @env_var: name of the environment variable
46 static efi_status_t set_load_options(efi_handle_t handle, const char *env_var)
48 struct efi_loaded_image *loaded_image_info;
50 const char *env = env_get(env_var);
54 ret = EFI_CALL(systab.boottime->open_protocol(
56 &efi_guid_loaded_image,
57 (void **)&loaded_image_info,
59 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
60 if (ret != EFI_SUCCESS)
61 return EFI_INVALID_PARAMETER;
63 loaded_image_info->load_options = NULL;
64 loaded_image_info->load_options_size = 0;
68 size = utf8_utf16_strlen(env) + 1;
69 loaded_image_info->load_options = calloc(size, sizeof(u16));
70 if (!loaded_image_info->load_options) {
71 printf("ERROR: Out of memory\n");
72 EFI_CALL(systab.boottime->close_protocol(handle,
73 &efi_guid_loaded_image,
75 return EFI_OUT_OF_RESOURCES;
77 pos = loaded_image_info->load_options;
78 utf8_utf16_strcpy(&pos, env);
79 loaded_image_info->load_options_size = size * 2;
82 return EFI_CALL(systab.boottime->close_protocol(handle,
83 &efi_guid_loaded_image,
88 * copy_fdt() - Copy the device tree to a new location available to EFI
90 * The FDT is copied to a suitable location within the EFI memory map.
91 * Additional 12 KiB are added to the space in case the device tree needs to be
92 * expanded later with fdt_open_into().
94 * @fdtp: On entry a pointer to the flattened device tree.
95 * On exit a pointer to the copy of the flattened device tree.
99 static efi_status_t copy_fdt(void **fdtp)
101 unsigned long fdt_ram_start = -1L, fdt_pages;
102 efi_status_t ret = 0;
108 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
109 u64 ram_start = gd->bd->bi_dram[i].start;
110 u64 ram_size = gd->bd->bi_dram[i].size;
115 if (ram_start < fdt_ram_start)
116 fdt_ram_start = ram_start;
120 * Give us at least 12 KiB of breathing room in case the device tree
121 * needs to be expanded later.
124 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
125 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
128 * Safe fdt location is at 127 MiB.
129 * On the sandbox convert from the sandbox address space.
131 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
133 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
134 EFI_BOOT_SERVICES_DATA, fdt_pages,
136 if (ret != EFI_SUCCESS) {
137 /* If we can't put it there, put it somewhere */
138 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
139 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
140 EFI_BOOT_SERVICES_DATA, fdt_pages,
142 if (ret != EFI_SUCCESS) {
143 printf("ERROR: Failed to reserve space for FDT\n");
147 new_fdt = (void *)(uintptr_t)new_fdt_addr;
148 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
149 fdt_set_totalsize(new_fdt, fdt_size);
151 *fdtp = (void *)(uintptr_t)new_fdt_addr;
157 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
159 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
160 * ignored because this is not critical and we would rather continue to try to
163 * @fdt: Pointer to device tree
165 static void efi_carve_out_dt_rsv(void *fdt)
168 uint64_t addr, size, pages;
170 nr_rsv = fdt_num_mem_rsv(fdt);
172 /* Look for an existing entry and add it to the efi mem map. */
173 for (i = 0; i < nr_rsv; i++) {
174 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
177 /* Convert from sandbox address space. */
178 addr = (uintptr_t)map_sysmem(addr, 0);
180 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
181 addr &= ~EFI_PAGE_MASK;
182 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
184 printf("FDT memrsv map %d: Failed to add to map\n", i);
188 static efi_status_t efi_install_fdt(ulong fdt_addr)
190 bootm_headers_t img = { 0 };
194 fdt = map_sysmem(fdt_addr, 0);
195 if (fdt_check_header(fdt)) {
196 printf("ERROR: invalid device tree\n");
197 return EFI_INVALID_PARAMETER;
200 /* Create memory reservation as indicated by the device tree */
201 efi_carve_out_dt_rsv(fdt);
203 /* Prepare fdt for payload */
204 ret = copy_fdt(&fdt);
208 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
209 printf("ERROR: failed to process device tree\n");
210 return EFI_LOAD_ERROR;
213 /* Link to it in the efi tables */
214 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
215 if (ret != EFI_SUCCESS)
216 return EFI_OUT_OF_RESOURCES;
222 * efi_process_fdt() - process fdt passed by a command argument
223 * @fdt_opt: pointer to argument
224 * Return: status code
226 * If specified, fdt will be installed as configuration table,
227 * otherwise no fdt will be passed.
229 static efi_status_t efi_process_fdt(const char *fdt_opt)
231 unsigned long fdt_addr;
235 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
236 if (!fdt_addr && *fdt_opt != '0')
237 return EFI_INVALID_PARAMETER;
239 /* Install device tree */
240 ret = efi_install_fdt(fdt_addr);
241 if (ret != EFI_SUCCESS) {
242 printf("ERROR: failed to install device tree\n");
246 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
247 efi_install_configuration_table(&efi_guid_fdt, NULL);
253 static efi_status_t bootefi_run_prepare(const char *load_options_path,
254 struct efi_device_path *device_path,
255 struct efi_device_path *image_path,
256 struct efi_loaded_image_obj **image_objp,
257 struct efi_loaded_image **loaded_image_infop)
261 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
263 if (ret != EFI_SUCCESS)
266 /* Transfer environment variable as load options */
267 return set_load_options((efi_handle_t)*image_objp, load_options_path);
271 * bootefi_run_finish() - finish up after running an EFI test
273 * @loaded_image_info: Pointer to a struct which holds the loaded image info
274 * @image_objj: Pointer to a struct which holds the loaded image object
276 static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
277 struct efi_loaded_image *loaded_image_info)
280 free(loaded_image_info->load_options);
281 efi_delete_handle(&image_obj->header);
285 * do_bootefi_exec() - execute EFI binary
287 * @efi: address of the binary
288 * @device_path: path of the device from which the binary was loaded
289 * @image_path: device path of the binary
290 * Return: status code
292 * Load the EFI binary into a newly assigned memory unwinding the relocation
293 * information, install the loaded image protocol, and call the binary.
295 static efi_status_t do_bootefi_exec(void *efi,
296 struct efi_device_path *device_path,
297 struct efi_device_path *image_path)
299 efi_handle_t mem_handle = NULL;
300 struct efi_device_path *memdp = NULL;
302 struct efi_loaded_image_obj *image_obj = NULL;
303 struct efi_loaded_image *loaded_image_info = NULL;
306 * Special case for efi payload not loaded from disk, such as
307 * 'bootefi hello' or for example payload loaded directly into
308 * memory via JTAG, etc:
310 if (!device_path && !image_path) {
311 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
312 /* actual addresses filled in after efi_load_pe() */
313 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
314 device_path = image_path = memdp;
316 * Grub expects that the device path of the loaded image is
317 * installed on a handle.
319 ret = efi_create_handle(&mem_handle);
320 if (ret != EFI_SUCCESS)
321 return ret; /* TODO: leaks device_path */
322 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
324 if (ret != EFI_SUCCESS)
325 goto err_add_protocol;
327 assert(device_path && image_path);
330 ret = bootefi_run_prepare("bootargs", device_path, image_path,
331 &image_obj, &loaded_image_info);
335 /* Load the EFI payload */
336 ret = efi_load_pe(image_obj, efi, loaded_image_info);
337 if (ret != EFI_SUCCESS)
341 struct efi_device_path_memory *mdp = (void *)memdp;
342 mdp->memory_type = loaded_image_info->image_code_type;
343 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
344 mdp->end_address = mdp->start_address +
345 loaded_image_info->image_size;
348 /* we don't support much: */
349 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
350 "{ro,boot}(blob)0000000000000000");
352 /* Call our payload! */
353 debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry);
354 ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL));
357 /* image has returned, loaded-image obj goes *poof*: */
358 bootefi_run_finish(image_obj, loaded_image_info);
362 efi_delete_handle(mem_handle);
367 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
369 * bootefi_test_prepare() - prepare to run an EFI test
371 * Prepare to run a test as if it were provided by a loaded image.
373 * @image_objp: pointer to be set to the loaded image handle
374 * @loaded_image_infop: pointer to be set to the loaded image protocol
375 * @path: dummy file path used to construct the device path
376 * set in the loaded image protocol
377 * @load_options_path: name of a U-Boot environment variable. Its value is
378 * set as load options in the loaded image protocol.
379 * Return: status code
381 static efi_status_t bootefi_test_prepare
382 (struct efi_loaded_image_obj **image_objp,
383 struct efi_loaded_image **loaded_image_infop, const char *path,
384 const char *load_options_path)
388 /* Construct a dummy device path */
389 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
390 if (!bootefi_device_path)
391 return EFI_OUT_OF_RESOURCES;
393 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
394 if (!bootefi_image_path) {
395 ret = EFI_OUT_OF_RESOURCES;
399 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
400 bootefi_image_path, image_objp,
402 if (ret == EFI_SUCCESS)
405 efi_free_pool(bootefi_image_path);
406 bootefi_image_path = NULL;
408 efi_free_pool(bootefi_device_path);
409 bootefi_device_path = NULL;
413 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
415 static int do_bootefi_bootmgr_exec(void)
417 struct efi_device_path *device_path, *file_path;
421 addr = efi_bootmgr_load(&device_path, &file_path);
425 printf("## Starting EFI application at %p ...\n", addr);
426 r = do_bootefi_exec(addr, device_path, file_path);
427 printf("## Application terminated, r = %lu\n",
428 r & ~EFI_ERROR_MASK);
430 if (r != EFI_SUCCESS)
436 /* Interpreter command to boot an arbitrary EFI image from memory */
437 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
443 /* Allow unaligned memory access */
446 switch_to_non_secure_mode();
448 /* Initialize EFI drivers */
449 r = efi_init_obj_list();
450 if (r != EFI_SUCCESS) {
451 printf("Error: Cannot set up EFI drivers, r = %lu\n",
452 r & ~EFI_ERROR_MASK);
453 return CMD_RET_FAILURE;
457 return CMD_RET_USAGE;
459 r = efi_process_fdt(argc > 2 ? argv[2] : NULL);
460 if (r == EFI_INVALID_PARAMETER)
461 return CMD_RET_USAGE;
462 else if (r != EFI_SUCCESS)
463 return CMD_RET_FAILURE;
465 #ifdef CONFIG_CMD_BOOTEFI_HELLO
466 if (!strcmp(argv[1], "hello")) {
467 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
469 saddr = env_get("loadaddr");
471 addr = simple_strtoul(saddr, NULL, 16);
473 addr = CONFIG_SYS_LOAD_ADDR;
474 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
477 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
478 if (!strcmp(argv[1], "selftest")) {
479 struct efi_loaded_image_obj *image_obj;
480 struct efi_loaded_image *loaded_image_info;
482 r = bootefi_test_prepare(&image_obj, &loaded_image_info,
483 "\\selftest", "efi_selftest");
484 if (r != EFI_SUCCESS)
485 return CMD_RET_FAILURE;
487 /* Execute the test */
488 r = EFI_CALL(efi_selftest(&image_obj->header, &systab));
489 bootefi_run_finish(image_obj, loaded_image_info);
490 return r != EFI_SUCCESS;
493 if (!strcmp(argv[1], "bootmgr")) {
494 return do_bootefi_bootmgr_exec();
498 addr = simple_strtoul(saddr, NULL, 16);
499 /* Check that a numeric value was passed */
500 if (!addr && *saddr != '0')
501 return CMD_RET_USAGE;
505 printf("## Starting EFI application at %08lx ...\n", addr);
506 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
508 printf("## Application terminated, r = %lu\n",
509 r & ~EFI_ERROR_MASK);
511 if (r != EFI_SUCCESS)
517 #ifdef CONFIG_SYS_LONGHELP
518 static char bootefi_help_text[] =
519 "<image address> [fdt address]\n"
520 " - boot EFI payload stored at address <image address>.\n"
521 " If specified, the device tree located at <fdt address> gets\n"
522 " exposed as EFI configuration table.\n"
523 #ifdef CONFIG_CMD_BOOTEFI_HELLO
525 " - boot a sample Hello World application stored within U-Boot\n"
527 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
528 "bootefi selftest [fdt address]\n"
529 " - boot an EFI selftest application stored within U-Boot\n"
530 " Use environment variable efi_selftest to select a single test.\n"
531 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
533 "bootefi bootmgr [fdt addr]\n"
534 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
536 " If specified, the device tree located at <fdt address> gets\n"
537 " exposed as EFI configuration table.\n";
541 bootefi, 3, 0, do_bootefi,
542 "Boots an EFI payload from memory",
546 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
548 struct efi_device_path *device, *image;
551 /* efi_set_bootdev is typically called repeatedly, recover memory */
552 efi_free_pool(bootefi_device_path);
553 efi_free_pool(bootefi_image_path);
555 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
556 if (ret == EFI_SUCCESS) {
557 bootefi_device_path = device;
558 bootefi_image_path = image;
560 bootefi_device_path = NULL;
561 bootefi_image_path = NULL;