2 * EFI application loader
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <efi_loader.h>
15 #include <libfdt_env.h>
17 #include <asm/global_data.h>
18 #include <asm-generic/sections.h>
19 #include <linux/linkage.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 static uint8_t efi_obj_list_initalized;
25 static struct efi_device_path *bootefi_image_path;
26 static struct efi_device_path *bootefi_device_path;
28 /* Initialize and populate EFI object list */
29 static void efi_init_obj_list(void)
31 efi_obj_list_initalized = 1;
33 efi_console_register();
34 #ifdef CONFIG_PARTITIONS
37 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
43 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
44 efi_smbios_register();
47 /* Initialize EFI runtime services */
48 efi_reset_system_init();
52 static void *copy_fdt(void *fdt)
54 u64 fdt_size = fdt_totalsize(fdt);
55 unsigned long fdt_ram_start = -1L, fdt_pages;
60 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
61 u64 ram_start = gd->bd->bi_dram[i].start;
62 u64 ram_size = gd->bd->bi_dram[i].size;
67 if (ram_start < fdt_ram_start)
68 fdt_ram_start = ram_start;
71 /* Give us at least 4kb breathing room */
72 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
73 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
75 /* Safe fdt location is at 128MB */
76 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
77 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
78 &new_fdt_addr) != EFI_SUCCESS) {
79 /* If we can't put it there, put it somewhere */
80 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
81 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
82 &new_fdt_addr) != EFI_SUCCESS) {
83 printf("ERROR: Failed to reserve space for FDT\n");
88 new_fdt = (void*)(ulong)new_fdt_addr;
89 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
90 fdt_set_totalsize(new_fdt, fdt_size);
95 static ulong efi_do_enter(void *image_handle,
96 struct efi_system_table *st,
97 asmlinkage ulong (*entry)(void *image_handle,
98 struct efi_system_table *st))
100 efi_status_t ret = EFI_LOAD_ERROR;
103 ret = entry(image_handle, st);
104 st->boottime->exit(image_handle, ret, 0, NULL);
109 static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)(
110 void *image_handle, struct efi_system_table *st),
111 void *image_handle, struct efi_system_table *st)
113 /* Enable caches again */
116 return efi_do_enter(image_handle, st, entry);
121 * Load an EFI payload into a newly allocated piece of memory, register all
122 * EFI objects it would want to access and jump to it.
124 static unsigned long do_bootefi_exec(void *efi, void *fdt,
125 struct efi_device_path *device_path,
126 struct efi_device_path *image_path)
128 struct efi_loaded_image loaded_image_info = {};
129 struct efi_object loaded_image_info_obj = {};
132 ulong (*entry)(void *image_handle, struct efi_system_table *st)
134 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
135 const efi_guid_t fdt_guid = EFI_FDT_GUID;
136 bootm_headers_t img = { 0 };
138 /* Initialize and populate EFI object list */
139 if (!efi_obj_list_initalized)
142 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
143 device_path, image_path);
146 * gd lives in a fixed register which may get clobbered while we execute
147 * the payload. So save it here and restore it on every callback entry
151 if (fdt && !fdt_check_header(fdt)) {
152 /* Prepare fdt for payload */
155 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
156 printf("ERROR: Failed to process device tree\n");
160 /* Link to it in the efi tables */
161 efi_install_configuration_table(&fdt_guid, fdt);
163 /* And reserve the space in the memory map */
164 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
165 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
166 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
167 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
168 /* Give a bootloader the chance to modify the device tree */
170 efi_add_memory_map(fdt_start, fdt_pages,
171 EFI_BOOT_SERVICES_DATA, true);
173 printf("WARNING: Invalid device tree, expect boot to fail\n");
174 efi_install_configuration_table(&fdt_guid, NULL);
177 /* Load the EFI payload */
178 entry = efi_load_pe(efi, &loaded_image_info);
184 /* we don't support much: */
185 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
186 "{ro,boot}(blob)0000000000000000");
188 /* Call our payload! */
189 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
191 if (setjmp(&loaded_image_info.exit_jmp)) {
192 ret = loaded_image_info.exit_status;
198 /* On AArch64 we need to make sure we call our payload in < EL3 */
199 if (current_el() == 3) {
201 dcache_disable(); /* flush cache before switch to EL2 */
203 /* Move into EL2 and keep running there */
204 armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
205 (ulong)&systab, 0, (ulong)efi_run_in_el2,
208 /* Should never reach here, efi exits with longjmp */
213 ret = efi_do_enter(&loaded_image_info, &systab, entry);
216 /* image has returned, loaded-image obj goes *poof*: */
217 list_del(&loaded_image_info_obj.link);
222 /* Interpreter command to boot an arbitrary EFI image from memory */
223 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
226 unsigned long addr, fdt_addr = 0;
230 return CMD_RET_USAGE;
231 #ifdef CONFIG_CMD_BOOTEFI_HELLO
232 if (!strcmp(argv[1], "hello")) {
233 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
235 saddr = env_get("loadaddr");
237 addr = simple_strtoul(saddr, NULL, 16);
239 addr = CONFIG_SYS_LOAD_ADDR;
240 memcpy((char *)addr, __efi_helloworld_begin, size);
243 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
244 if (!strcmp(argv[1], "selftest")) {
246 * gd lives in a fixed register which may get clobbered while we
247 * execute the payload. So save it here and restore it on every
251 /* Initialize and populate EFI object list */
252 if (!efi_obj_list_initalized)
254 loaded_image_info.device_handle = bootefi_device_path;
255 loaded_image_info.file_path = bootefi_image_path;
256 return efi_selftest(&loaded_image_info, &systab);
262 addr = simple_strtoul(saddr, NULL, 16);
266 fdt_addr = simple_strtoul(sfdt, NULL, 16);
270 printf("## Starting EFI application at %08lx ...\n", addr);
271 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
272 bootefi_device_path, bootefi_image_path);
273 printf("## Application terminated, r = %lu\n",
274 r & ~EFI_ERROR_MASK);
276 if (r != EFI_SUCCESS)
282 #ifdef CONFIG_SYS_LONGHELP
283 static char bootefi_help_text[] =
284 "<image address> [fdt address]\n"
285 " - boot EFI payload stored at address <image address>.\n"
286 " If specified, the device tree located at <fdt address> gets\n"
287 " exposed as EFI configuration table.\n"
288 #ifdef CONFIG_CMD_BOOTEFI_HELLO
290 " - boot a sample Hello World application stored within U-Boot\n"
292 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
294 " - boot an EFI selftest application stored within U-Boot\n"
300 bootefi, 3, 0, do_bootefi,
301 "Boots an EFI payload from memory",
305 static int parse_partnum(const char *devnr)
307 const char *str = strchr(devnr, ':');
310 return simple_strtoul(str, NULL, 16);
315 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
317 char filename[32] = { 0 }; /* dp->str is u16[32] long */
320 if (strcmp(dev, "Net")) {
321 struct blk_desc *desc;
324 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
325 part = parse_partnum(devnr);
327 bootefi_device_path = efi_dp_from_part(desc, part);
330 bootefi_device_path = efi_dp_from_eth();
334 if (strcmp(dev, "Net")) {
335 /* Add leading / to fs paths, because they're absolute */
336 snprintf(filename, sizeof(filename), "/%s", path);
338 snprintf(filename, sizeof(filename), "%s", path);
340 /* DOS style file path: */
342 while ((s = strchr(s, '/')))
344 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);