2 * EFI application loader
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <efi_loader.h>
14 #include <efi_selftest.h>
17 #include <libfdt_env.h>
19 #include <asm/global_data.h>
20 #include <asm-generic/sections.h>
21 #include <linux/linkage.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static uint8_t efi_obj_list_initalized;
27 static struct efi_device_path *bootefi_image_path;
28 static struct efi_device_path *bootefi_device_path;
30 /* Initialize and populate EFI object list */
31 static void efi_init_obj_list(void)
33 efi_obj_list_initalized = 1;
35 efi_console_register();
36 #ifdef CONFIG_PARTITIONS
39 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
45 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
46 efi_smbios_register();
48 efi_watchdog_register();
50 /* Initialize EFI runtime services */
51 efi_reset_system_init();
56 * Set the load options of an image from an environment variable.
58 * @loaded_image_info: the image
59 * @env_var: name of the environment variable
61 static void set_load_options(struct efi_loaded_image *loaded_image_info,
65 const char *env = env_get(env_var);
67 loaded_image_info->load_options = NULL;
68 loaded_image_info->load_options_size = 0;
71 size = strlen(env) + 1;
72 loaded_image_info->load_options = calloc(size, sizeof(u16));
73 if (!loaded_image_info->load_options) {
74 printf("ERROR: Out of memory\n");
77 utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
78 loaded_image_info->load_options_size = size * 2;
81 static void *copy_fdt(void *fdt)
83 u64 fdt_size = fdt_totalsize(fdt);
84 unsigned long fdt_ram_start = -1L, fdt_pages;
89 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
90 u64 ram_start = gd->bd->bi_dram[i].start;
91 u64 ram_size = gd->bd->bi_dram[i].size;
96 if (ram_start < fdt_ram_start)
97 fdt_ram_start = ram_start;
100 /* Give us at least 4kb breathing room */
101 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
102 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
104 /* Safe fdt location is at 128MB */
105 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
106 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
107 &new_fdt_addr) != EFI_SUCCESS) {
108 /* If we can't put it there, put it somewhere */
109 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
110 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
111 &new_fdt_addr) != EFI_SUCCESS) {
112 printf("ERROR: Failed to reserve space for FDT\n");
117 new_fdt = (void*)(ulong)new_fdt_addr;
118 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
119 fdt_set_totalsize(new_fdt, fdt_size);
124 static efi_status_t efi_do_enter(
125 efi_handle_t image_handle, struct efi_system_table *st,
126 asmlinkage ulong (*entry)(efi_handle_t image_handle,
127 struct efi_system_table *st))
129 efi_status_t ret = EFI_LOAD_ERROR;
132 ret = entry(image_handle, st);
133 st->boottime->exit(image_handle, ret, 0, NULL);
138 static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)(
139 efi_handle_t image_handle, struct efi_system_table *st),
140 efi_handle_t image_handle, struct efi_system_table *st)
142 /* Enable caches again */
145 return efi_do_enter(image_handle, st, entry);
150 * Load an EFI payload into a newly allocated piece of memory, register all
151 * EFI objects it would want to access and jump to it.
153 static efi_status_t do_bootefi_exec(void *efi, void *fdt,
154 struct efi_device_path *device_path,
155 struct efi_device_path *image_path)
157 struct efi_loaded_image loaded_image_info = {};
158 struct efi_object loaded_image_info_obj = {};
159 struct efi_device_path *memdp = NULL;
162 ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st)
164 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
165 const efi_guid_t fdt_guid = EFI_FDT_GUID;
166 bootm_headers_t img = { 0 };
169 * Special case for efi payload not loaded from disk, such as
170 * 'bootefi hello' or for example payload loaded directly into
171 * memory via jtag/etc:
173 if (!device_path && !image_path) {
174 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
175 /* actual addresses filled in after efi_load_pe() */
176 memdp = efi_dp_from_mem(0, 0, 0);
177 device_path = image_path = memdp;
179 assert(device_path && image_path);
182 /* Initialize and populate EFI object list */
183 if (!efi_obj_list_initalized)
186 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
187 device_path, image_path);
190 * gd lives in a fixed register which may get clobbered while we execute
191 * the payload. So save it here and restore it on every callback entry
195 if (fdt && !fdt_check_header(fdt)) {
196 /* Prepare fdt for payload */
199 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
200 printf("ERROR: Failed to process device tree\n");
204 /* Link to it in the efi tables */
205 efi_install_configuration_table(&fdt_guid, fdt);
207 /* And reserve the space in the memory map */
208 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
209 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
210 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
211 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
212 /* Give a bootloader the chance to modify the device tree */
214 efi_add_memory_map(fdt_start, fdt_pages,
215 EFI_BOOT_SERVICES_DATA, true);
217 printf("WARNING: Invalid device tree, expect boot to fail\n");
218 efi_install_configuration_table(&fdt_guid, NULL);
221 /* Transfer environment variable bootargs as load options */
222 set_load_options(&loaded_image_info, "bootargs");
223 /* Load the EFI payload */
224 entry = efi_load_pe(efi, &loaded_image_info);
231 struct efi_device_path_memory *mdp = (void *)memdp;
232 mdp->memory_type = loaded_image_info.image_code_type;
233 mdp->start_address = (uintptr_t)loaded_image_info.image_base;
234 mdp->end_address = mdp->start_address +
235 loaded_image_info.image_size;
238 /* we don't support much: */
239 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
240 "{ro,boot}(blob)0000000000000000");
242 /* Call our payload! */
243 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
245 if (setjmp(&loaded_image_info.exit_jmp)) {
246 ret = loaded_image_info.exit_status;
251 /* On AArch64 we need to make sure we call our payload in < EL3 */
252 if (current_el() == 3) {
254 dcache_disable(); /* flush cache before switch to EL2 */
256 /* Move into EL2 and keep running there */
257 armv8_switch_to_el2((ulong)entry,
258 (ulong)&loaded_image_info_obj.handle,
259 (ulong)&systab, 0, (ulong)efi_run_in_el2,
262 /* Should never reach here, efi exits with longjmp */
267 ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
270 /* image has returned, loaded-image obj goes *poof*: */
271 list_del(&loaded_image_info_obj.link);
276 static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
278 struct efi_device_path *device_path, *file_path;
282 /* Initialize and populate EFI object list */
283 if (!efi_obj_list_initalized)
287 * gd lives in a fixed register which may get clobbered while we execute
288 * the payload. So save it here and restore it on every callback entry
292 addr = efi_bootmgr_load(&device_path, &file_path);
296 printf("## Starting EFI application at %p ...\n", addr);
297 r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
298 printf("## Application terminated, r = %lu\n",
299 r & ~EFI_ERROR_MASK);
301 if (r != EFI_SUCCESS)
307 /* Interpreter command to boot an arbitrary EFI image from memory */
308 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
311 unsigned long addr, fdt_addr = 0;
315 return CMD_RET_USAGE;
316 #ifdef CONFIG_CMD_BOOTEFI_HELLO
317 if (!strcmp(argv[1], "hello")) {
318 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
320 saddr = env_get("loadaddr");
322 addr = simple_strtoul(saddr, NULL, 16);
324 addr = CONFIG_SYS_LOAD_ADDR;
325 memcpy((char *)addr, __efi_helloworld_begin, size);
328 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
329 if (!strcmp(argv[1], "selftest")) {
330 struct efi_loaded_image loaded_image_info = {};
331 struct efi_object loaded_image_info_obj = {};
333 /* Construct a dummy device path. */
334 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
335 (uintptr_t)&efi_selftest,
336 (uintptr_t)&efi_selftest);
337 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
339 efi_setup_loaded_image(&loaded_image_info,
340 &loaded_image_info_obj,
341 bootefi_device_path, bootefi_image_path);
343 * gd lives in a fixed register which may get clobbered while we
344 * execute the payload. So save it here and restore it on every
348 /* Initialize and populate EFI object list */
349 if (!efi_obj_list_initalized)
351 /* Transfer environment variable efi_selftest as load options */
352 set_load_options(&loaded_image_info, "efi_selftest");
353 /* Execute the test */
354 r = efi_selftest(loaded_image_info_obj.handle, &systab);
356 free(loaded_image_info.load_options);
357 list_del(&loaded_image_info_obj.link);
358 return r != EFI_SUCCESS;
361 if (!strcmp(argv[1], "bootmgr")) {
362 unsigned long fdt_addr = 0;
365 fdt_addr = simple_strtoul(argv[2], NULL, 16);
367 return do_bootefi_bootmgr_exec(fdt_addr);
371 addr = simple_strtoul(saddr, NULL, 16);
375 fdt_addr = simple_strtoul(sfdt, NULL, 16);
379 printf("## Starting EFI application at %08lx ...\n", addr);
380 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
381 bootefi_device_path, bootefi_image_path);
382 printf("## Application terminated, r = %lu\n",
383 r & ~EFI_ERROR_MASK);
385 if (r != EFI_SUCCESS)
391 #ifdef CONFIG_SYS_LONGHELP
392 static char bootefi_help_text[] =
393 "<image address> [fdt address]\n"
394 " - boot EFI payload stored at address <image address>.\n"
395 " If specified, the device tree located at <fdt address> gets\n"
396 " exposed as EFI configuration table.\n"
397 #ifdef CONFIG_CMD_BOOTEFI_HELLO
399 " - boot a sample Hello World application stored within U-Boot\n"
401 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
403 " - boot an EFI selftest application stored within U-Boot\n"
404 " Use environment variable efi_selftest to select a single test.\n"
405 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
407 "bootmgr [fdt addr]\n"
408 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
410 " If specified, the device tree located at <fdt address> gets\n"
411 " exposed as EFI configuration table.\n";
415 bootefi, 3, 0, do_bootefi,
416 "Boots an EFI payload from memory",
420 static int parse_partnum(const char *devnr)
422 const char *str = strchr(devnr, ':');
425 return simple_strtoul(str, NULL, 16);
430 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
432 char filename[32] = { 0 }; /* dp->str is u16[32] long */
435 if (strcmp(dev, "Net")) {
436 struct blk_desc *desc;
439 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
442 part = parse_partnum(devnr);
444 bootefi_device_path = efi_dp_from_part(desc, part);
447 bootefi_device_path = efi_dp_from_eth();
454 if (strcmp(dev, "Net")) {
455 /* Add leading / to fs paths, because they're absolute */
456 snprintf(filename, sizeof(filename), "/%s", path);
458 snprintf(filename, sizeof(filename), "%s", path);
460 /* DOS style file path: */
462 while ((s = strchr(s, '/')))
464 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);