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 /* Initialize EFI driver uclass */
38 efi_console_register();
39 #ifdef CONFIG_PARTITIONS
42 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
48 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
49 efi_smbios_register();
51 efi_watchdog_register();
53 /* Initialize EFI runtime services */
54 efi_reset_system_init();
59 * Set the load options of an image from an environment variable.
61 * @loaded_image_info: the image
62 * @env_var: name of the environment variable
64 static void set_load_options(struct efi_loaded_image
*loaded_image_info
,
68 const char *env
= env_get(env_var
);
70 loaded_image_info
->load_options
= NULL
;
71 loaded_image_info
->load_options_size
= 0;
74 size
= strlen(env
) + 1;
75 loaded_image_info
->load_options
= calloc(size
, sizeof(u16
));
76 if (!loaded_image_info
->load_options
) {
77 printf("ERROR: Out of memory\n");
80 utf8_to_utf16(loaded_image_info
->load_options
, (u8
*)env
, size
);
81 loaded_image_info
->load_options_size
= size
* 2;
84 static void *copy_fdt(void *fdt
)
86 u64 fdt_size
= fdt_totalsize(fdt
);
87 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
;
103 /* Give us at least 4kb breathing room */
104 fdt_size
= ALIGN(fdt_size
+ 4096, EFI_PAGE_SIZE
);
105 fdt_pages
= fdt_size
>> EFI_PAGE_SHIFT
;
107 /* Safe fdt location is at 128MB */
108 new_fdt_addr
= fdt_ram_start
+ (128 * 1024 * 1024) + fdt_size
;
109 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
110 &new_fdt_addr
) != EFI_SUCCESS
) {
111 /* If we can't put it there, put it somewhere */
112 new_fdt_addr
= (ulong
)memalign(EFI_PAGE_SIZE
, fdt_size
);
113 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA
, fdt_pages
,
114 &new_fdt_addr
) != EFI_SUCCESS
) {
115 printf("ERROR: Failed to reserve space for FDT\n");
120 new_fdt
= (void*)(ulong
)new_fdt_addr
;
121 memcpy(new_fdt
, fdt
, fdt_totalsize(fdt
));
122 fdt_set_totalsize(new_fdt
, fdt_size
);
127 static efi_status_t
efi_do_enter(
128 efi_handle_t image_handle
, struct efi_system_table
*st
,
129 EFIAPI
efi_status_t (*entry
)(
130 efi_handle_t image_handle
,
131 struct efi_system_table
*st
))
133 efi_status_t ret
= EFI_LOAD_ERROR
;
136 ret
= entry(image_handle
, st
);
137 st
->boottime
->exit(image_handle
, ret
, 0, NULL
);
142 static efi_status_t
efi_run_in_el2(EFIAPI
efi_status_t (*entry
)(
143 efi_handle_t image_handle
, struct efi_system_table
*st
),
144 efi_handle_t image_handle
, struct efi_system_table
*st
)
146 /* Enable caches again */
149 return efi_do_enter(image_handle
, st
, entry
);
154 * Load an EFI payload into a newly allocated piece of memory, register all
155 * EFI objects it would want to access and jump to it.
157 static efi_status_t
do_bootefi_exec(void *efi
, void *fdt
,
158 struct efi_device_path
*device_path
,
159 struct efi_device_path
*image_path
)
161 struct efi_loaded_image loaded_image_info
= {};
162 struct efi_object loaded_image_info_obj
= {};
163 struct efi_device_path
*memdp
= NULL
;
166 EFIAPI
efi_status_t (*entry
)(efi_handle_t image_handle
,
167 struct efi_system_table
*st
);
168 ulong fdt_pages
, fdt_size
, fdt_start
, fdt_end
;
169 const efi_guid_t fdt_guid
= EFI_FDT_GUID
;
170 bootm_headers_t img
= { 0 };
173 * Special case for efi payload not loaded from disk, such as
174 * 'bootefi hello' or for example payload loaded directly into
175 * memory via jtag/etc:
177 if (!device_path
&& !image_path
) {
178 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
179 /* actual addresses filled in after efi_load_pe() */
180 memdp
= efi_dp_from_mem(0, 0, 0);
181 device_path
= image_path
= memdp
;
183 assert(device_path
&& image_path
);
186 /* Initialize and populate EFI object list */
187 if (!efi_obj_list_initalized
)
190 efi_setup_loaded_image(&loaded_image_info
, &loaded_image_info_obj
,
191 device_path
, image_path
);
194 * gd lives in a fixed register which may get clobbered while we execute
195 * the payload. So save it here and restore it on every callback entry
199 if (fdt
&& !fdt_check_header(fdt
)) {
200 /* Prepare fdt for payload */
203 if (image_setup_libfdt(&img
, fdt
, 0, NULL
)) {
204 printf("ERROR: Failed to process device tree\n");
208 /* Link to it in the efi tables */
209 efi_install_configuration_table(&fdt_guid
, fdt
);
211 /* And reserve the space in the memory map */
212 fdt_start
= ((ulong
)fdt
) & ~EFI_PAGE_MASK
;
213 fdt_end
= ((ulong
)fdt
) + fdt_totalsize(fdt
);
214 fdt_size
= (fdt_end
- fdt_start
) + EFI_PAGE_MASK
;
215 fdt_pages
= fdt_size
>> EFI_PAGE_SHIFT
;
216 /* Give a bootloader the chance to modify the device tree */
218 efi_add_memory_map(fdt_start
, fdt_pages
,
219 EFI_BOOT_SERVICES_DATA
, true);
221 printf("WARNING: Invalid device tree, expect boot to fail\n");
222 efi_install_configuration_table(&fdt_guid
, NULL
);
225 /* Transfer environment variable bootargs as load options */
226 set_load_options(&loaded_image_info
, "bootargs");
227 /* Load the EFI payload */
228 entry
= efi_load_pe(efi
, &loaded_image_info
);
235 struct efi_device_path_memory
*mdp
= (void *)memdp
;
236 mdp
->memory_type
= loaded_image_info
.image_code_type
;
237 mdp
->start_address
= (uintptr_t)loaded_image_info
.image_base
;
238 mdp
->end_address
= mdp
->start_address
+
239 loaded_image_info
.image_size
;
242 /* we don't support much: */
243 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
244 "{ro,boot}(blob)0000000000000000");
246 /* Call our payload! */
247 debug("%s:%d Jumping to 0x%lx\n", __func__
, __LINE__
, (long)entry
);
249 if (setjmp(&loaded_image_info
.exit_jmp
)) {
250 ret
= loaded_image_info
.exit_status
;
255 /* On AArch64 we need to make sure we call our payload in < EL3 */
256 if (current_el() == 3) {
258 dcache_disable(); /* flush cache before switch to EL2 */
260 /* Move into EL2 and keep running there */
261 armv8_switch_to_el2((ulong
)entry
,
262 (ulong
)&loaded_image_info_obj
.handle
,
263 (ulong
)&systab
, 0, (ulong
)efi_run_in_el2
,
266 /* Should never reach here, efi exits with longjmp */
271 ret
= efi_do_enter(loaded_image_info_obj
.handle
, &systab
, entry
);
274 /* image has returned, loaded-image obj goes *poof*: */
275 list_del(&loaded_image_info_obj
.link
);
280 static int do_bootefi_bootmgr_exec(unsigned long fdt_addr
)
282 struct efi_device_path
*device_path
, *file_path
;
286 /* Initialize and populate EFI object list */
287 if (!efi_obj_list_initalized
)
291 * gd lives in a fixed register which may get clobbered while we execute
292 * the payload. So save it here and restore it on every callback entry
296 addr
= efi_bootmgr_load(&device_path
, &file_path
);
300 printf("## Starting EFI application at %p ...\n", addr
);
301 r
= do_bootefi_exec(addr
, (void *)fdt_addr
, device_path
, file_path
);
302 printf("## Application terminated, r = %lu\n",
303 r
& ~EFI_ERROR_MASK
);
305 if (r
!= EFI_SUCCESS
)
311 /* Interpreter command to boot an arbitrary EFI image from memory */
312 static int do_bootefi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
315 unsigned long addr
, fdt_addr
= 0;
319 return CMD_RET_USAGE
;
320 #ifdef CONFIG_CMD_BOOTEFI_HELLO
321 if (!strcmp(argv
[1], "hello")) {
322 ulong size
= __efi_helloworld_end
- __efi_helloworld_begin
;
324 saddr
= env_get("loadaddr");
326 addr
= simple_strtoul(saddr
, NULL
, 16);
328 addr
= CONFIG_SYS_LOAD_ADDR
;
329 memcpy((char *)addr
, __efi_helloworld_begin
, size
);
332 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
333 if (!strcmp(argv
[1], "selftest")) {
334 struct efi_loaded_image loaded_image_info
= {};
335 struct efi_object loaded_image_info_obj
= {};
337 /* Construct a dummy device path. */
338 bootefi_device_path
= efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE
,
339 (uintptr_t)&efi_selftest
,
340 (uintptr_t)&efi_selftest
);
341 bootefi_image_path
= efi_dp_from_file(NULL
, 0, "\\selftest");
343 efi_setup_loaded_image(&loaded_image_info
,
344 &loaded_image_info_obj
,
345 bootefi_device_path
, bootefi_image_path
);
347 * gd lives in a fixed register which may get clobbered while we
348 * execute the payload. So save it here and restore it on every
352 /* Initialize and populate EFI object list */
353 if (!efi_obj_list_initalized
)
355 /* Transfer environment variable efi_selftest as load options */
356 set_load_options(&loaded_image_info
, "efi_selftest");
357 /* Execute the test */
358 r
= efi_selftest(loaded_image_info_obj
.handle
, &systab
);
360 free(loaded_image_info
.load_options
);
361 list_del(&loaded_image_info_obj
.link
);
362 return r
!= EFI_SUCCESS
;
365 if (!strcmp(argv
[1], "bootmgr")) {
366 unsigned long fdt_addr
= 0;
369 fdt_addr
= simple_strtoul(argv
[2], NULL
, 16);
371 return do_bootefi_bootmgr_exec(fdt_addr
);
375 addr
= simple_strtoul(saddr
, NULL
, 16);
376 /* Check that a numeric value was passed */
377 if (!addr
&& *saddr
!= '0')
378 return CMD_RET_USAGE
;
382 fdt_addr
= simple_strtoul(sfdt
, NULL
, 16);
386 printf("## Starting EFI application at %08lx ...\n", addr
);
387 r
= do_bootefi_exec((void *)addr
, (void *)fdt_addr
,
388 bootefi_device_path
, bootefi_image_path
);
389 printf("## Application terminated, r = %lu\n",
390 r
& ~EFI_ERROR_MASK
);
392 if (r
!= EFI_SUCCESS
)
398 #ifdef CONFIG_SYS_LONGHELP
399 static char bootefi_help_text
[] =
400 "<image address> [fdt address]\n"
401 " - boot EFI payload stored at address <image address>.\n"
402 " If specified, the device tree located at <fdt address> gets\n"
403 " exposed as EFI configuration table.\n"
404 #ifdef CONFIG_CMD_BOOTEFI_HELLO
406 " - boot a sample Hello World application stored within U-Boot\n"
408 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
410 " - boot an EFI selftest application stored within U-Boot\n"
411 " Use environment variable efi_selftest to select a single test.\n"
412 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
414 "bootefi bootmgr [fdt addr]\n"
415 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
417 " If specified, the device tree located at <fdt address> gets\n"
418 " exposed as EFI configuration table.\n";
422 bootefi
, 3, 0, do_bootefi
,
423 "Boots an EFI payload from memory",
427 static int parse_partnum(const char *devnr
)
429 const char *str
= strchr(devnr
, ':');
432 return simple_strtoul(str
, NULL
, 16);
437 void efi_set_bootdev(const char *dev
, const char *devnr
, const char *path
)
439 char filename
[32] = { 0 }; /* dp->str is u16[32] long */
442 if (strcmp(dev
, "Net")) {
443 struct blk_desc
*desc
;
446 desc
= blk_get_dev(dev
, simple_strtol(devnr
, NULL
, 10));
449 part
= parse_partnum(devnr
);
451 bootefi_device_path
= efi_dp_from_part(desc
, part
);
454 bootefi_device_path
= efi_dp_from_eth();
461 if (strcmp(dev
, "Net")) {
462 /* Add leading / to fs paths, because they're absolute */
463 snprintf(filename
, sizeof(filename
), "/%s", path
);
465 snprintf(filename
, sizeof(filename
), "%s", path
);
467 /* DOS style file path: */
469 while ((s
= strchr(s
, '/')))
471 bootefi_image_path
= efi_dp_from_file(NULL
, 0, filename
);