]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/bootefi.c
fs: update fs_dev_part in fs_set_blk_dev_with_part()
[thirdparty/u-boot.git] / cmd / bootefi.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
b9939336
AG
2/*
3 * EFI application loader
4 *
5 * Copyright (c) 2016 Alexander Graf
b9939336
AG
6 */
7
d78e40d6 8#include <charset.h>
b9939336
AG
9#include <common.h>
10#include <command.h>
9d922450 11#include <dm.h>
b9939336 12#include <efi_loader.h>
d78e40d6 13#include <efi_selftest.h>
b9939336 14#include <errno.h>
b08c8c48
MY
15#include <linux/libfdt.h>
16#include <linux/libfdt_env.h>
354264b3 17#include <mapmem.h>
ad0c1a3d 18#include <memalign.h>
0d9d501f 19#include <asm/global_data.h>
e275458c 20#include <asm-generic/sections.h>
c3b11dea 21#include <asm-generic/unaligned.h>
e275458c 22#include <linux/linkage.h>
0d9d501f 23
dc500c36
MK
24#ifdef CONFIG_ARMV7_NONSEC
25#include <asm/armv7.h>
26#include <asm/secure.h>
27#endif
28
0d9d501f 29DECLARE_GLOBAL_DATA_PTR;
b9939336 30
fc225e60
HS
31#define OBJ_LIST_NOT_INITIALIZED 1
32
33static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
7cbc1241 34
95c5553e
RC
35static struct efi_device_path *bootefi_image_path;
36static struct efi_device_path *bootefi_device_path;
b9939336 37
7cbc1241 38/* Initialize and populate EFI object list */
fc225e60 39efi_status_t efi_init_obj_list(void)
7cbc1241 40{
fc225e60
HS
41 efi_status_t ret = EFI_SUCCESS;
42
1e1e1c27
HS
43 /*
44 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
45 * As this register may be overwritten by an EFI payload we save it here
46 * and restore it on every callback entered.
47 */
48 efi_save_gd();
49
098a6cdd 50 /* Initialize once only */
fc225e60
HS
51 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
52 return efi_obj_list_initialized;
7cbc1241 53
640adadf
HS
54 /* Initialize system table */
55 ret = efi_initialize_system_table();
56 if (ret != EFI_SUCCESS)
57 goto out;
58
4e6b5d65
HS
59 /* Initialize root node */
60 ret = efi_root_node_register();
61 if (ret != EFI_SUCCESS)
62 goto out;
63
05ef48a2 64 /* Initialize EFI driver uclass */
fc225e60
HS
65 ret = efi_driver_init();
66 if (ret != EFI_SUCCESS)
67 goto out;
05ef48a2 68
fc225e60
HS
69 ret = efi_console_register();
70 if (ret != EFI_SUCCESS)
71 goto out;
7cbc1241 72#ifdef CONFIG_PARTITIONS
fc225e60
HS
73 ret = efi_disk_register();
74 if (ret != EFI_SUCCESS)
75 goto out;
7cbc1241
HS
76#endif
77#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
fc225e60
HS
78 ret = efi_gop_register();
79 if (ret != EFI_SUCCESS)
80 goto out;
7cbc1241 81#endif
092f2f35 82#ifdef CONFIG_NET
fc225e60
HS
83 ret = efi_net_register();
84 if (ret != EFI_SUCCESS)
85 goto out;
7cbc1241 86#endif
86df34d4
BM
87#ifdef CONFIG_GENERATE_ACPI_TABLE
88 ret = efi_acpi_register();
89 if (ret != EFI_SUCCESS)
90 goto out;
91#endif
7cbc1241 92#ifdef CONFIG_GENERATE_SMBIOS_TABLE
fc225e60
HS
93 ret = efi_smbios_register();
94 if (ret != EFI_SUCCESS)
95 goto out;
7cbc1241 96#endif
fc225e60
HS
97 ret = efi_watchdog_register();
98 if (ret != EFI_SUCCESS)
99 goto out;
7cbc1241
HS
100
101 /* Initialize EFI runtime services */
fc225e60
HS
102 ret = efi_reset_system_init();
103 if (ret != EFI_SUCCESS)
104 goto out;
fc225e60
HS
105
106out:
107 efi_obj_list_initialized = ret;
108 return ret;
7cbc1241
HS
109}
110
c3b11dea
HS
111/*
112 * Allow unaligned memory access.
113 *
114 * This routine is overridden by architectures providing this feature.
115 */
116void __weak allow_unaligned(void)
117{
118}
119
d78e40d6
HS
120/*
121 * Set the load options of an image from an environment variable.
122 *
123 * @loaded_image_info: the image
124 * @env_var: name of the environment variable
125 */
126static void set_load_options(struct efi_loaded_image *loaded_image_info,
127 const char *env_var)
128{
129 size_t size;
130 const char *env = env_get(env_var);
7086a71a 131 u16 *pos;
d78e40d6
HS
132
133 loaded_image_info->load_options = NULL;
134 loaded_image_info->load_options_size = 0;
135 if (!env)
136 return;
7086a71a 137 size = utf8_utf16_strlen(env) + 1;
d78e40d6
HS
138 loaded_image_info->load_options = calloc(size, sizeof(u16));
139 if (!loaded_image_info->load_options) {
140 printf("ERROR: Out of memory\n");
141 return;
142 }
7086a71a
HS
143 pos = loaded_image_info->load_options;
144 utf8_utf16_strcpy(&pos, env);
d78e40d6
HS
145 loaded_image_info->load_options_size = size * 2;
146}
147
9dff4900
SG
148/**
149 * copy_fdt() - Copy the device tree to a new location available to EFI
150 *
151 * The FDT is relocated into a suitable location within the EFI memory map.
152 * An additional 12KB is added to the space in case the device tree needs to be
153 * expanded later with fdt_open_into().
154 *
155 * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated
156 * FDT start
157 * @fdt_sizep: Returns new size of FDT, including
158 * @return new relocated address of FDT
159 */
160static efi_status_t copy_fdt(ulong *fdt_addrp, ulong *fdt_sizep)
0d9d501f 161{
ad0c1a3d 162 unsigned long fdt_ram_start = -1L, fdt_pages;
9dff4900
SG
163 efi_status_t ret = 0;
164 void *fdt, *new_fdt;
ad0c1a3d 165 u64 new_fdt_addr;
9dff4900 166 uint fdt_size;
ad0c1a3d 167 int i;
0d9d501f 168
9dff4900
SG
169 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
170 u64 ram_start = gd->bd->bi_dram[i].start;
171 u64 ram_size = gd->bd->bi_dram[i].size;
0d9d501f 172
ad0c1a3d
AG
173 if (!ram_size)
174 continue;
175
176 if (ram_start < fdt_ram_start)
177 fdt_ram_start = ram_start;
178 }
179
bc9a638a
SG
180 /*
181 * Give us at least 4KB of breathing room in case the device tree needs
182 * to be expanded later. Round up to the nearest EFI page boundary.
183 */
9dff4900
SG
184 fdt = map_sysmem(*fdt_addrp, 0);
185 fdt_size = fdt_totalsize(fdt);
186 fdt_size += 4096 * 3;
bc9a638a 187 fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE);
ad0c1a3d
AG
188 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
189
baf70c02
SG
190 /* Safe fdt location is at 127MB */
191 new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size;
9dff4900
SG
192 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
193 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
194 &new_fdt_addr);
195 if (ret != EFI_SUCCESS) {
ad0c1a3d 196 /* If we can't put it there, put it somewhere */
a44bffcc 197 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
9dff4900
SG
198 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
199 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
200 &new_fdt_addr);
201 if (ret != EFI_SUCCESS) {
85a6e9b3 202 printf("ERROR: Failed to reserve space for FDT\n");
9dff4900 203 goto done;
85a6e9b3 204 }
ad0c1a3d 205 }
85a6e9b3 206
9dff4900 207 new_fdt = map_sysmem(new_fdt_addr, fdt_size);
0d9d501f
AG
208 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
209 fdt_set_totalsize(new_fdt, fdt_size);
210
9dff4900
SG
211 *fdt_addrp = new_fdt_addr;
212 *fdt_sizep = fdt_size;
213done:
214 return ret;
0d9d501f
AG
215}
216
3eb0841b 217static efi_status_t efi_do_enter(
2074f700 218 efi_handle_t image_handle, struct efi_system_table *st,
c6fa5df6
AG
219 EFIAPI efi_status_t (*entry)(
220 efi_handle_t image_handle,
221 struct efi_system_table *st))
b06d8ac3
HS
222{
223 efi_status_t ret = EFI_LOAD_ERROR;
224
225 if (entry)
226 ret = entry(image_handle, st);
227 st->boottime->exit(image_handle, ret, 0, NULL);
228 return ret;
229}
230
ec6617c3 231#ifdef CONFIG_ARM64
c6fa5df6 232static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
2074f700
HS
233 efi_handle_t image_handle, struct efi_system_table *st),
234 efi_handle_t image_handle, struct efi_system_table *st)
ec6617c3
AW
235{
236 /* Enable caches again */
237 dcache_enable();
238
b06d8ac3 239 return efi_do_enter(image_handle, st, entry);
ec6617c3
AW
240}
241#endif
242
dc500c36 243#ifdef CONFIG_ARMV7_NONSEC
f17f2001
MK
244static bool is_nonsec;
245
dc500c36
MK
246static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
247 efi_handle_t image_handle, struct efi_system_table *st),
248 efi_handle_t image_handle, struct efi_system_table *st)
249{
250 /* Enable caches again */
251 dcache_enable();
252
f17f2001
MK
253 is_nonsec = true;
254
dc500c36
MK
255 return efi_do_enter(image_handle, st, entry);
256}
257#endif
258
416e07e2
SG
259/*
260 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
261 *
262 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
263 * ignored because this is not critical and we would rather continue to try to
264 * boot.
265 *
266 * @fdt: Pointer to device tree
267 */
268static void efi_carve_out_dt_rsv(void *fdt)
806d2fa8
AG
269{
270 int nr_rsv, i;
271 uint64_t addr, size, pages;
272
273 nr_rsv = fdt_num_mem_rsv(fdt);
274
275 /* Look for an existing entry and add it to the efi mem map. */
276 for (i = 0; i < nr_rsv; i++) {
277 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
278 continue;
279
280 pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT;
416e07e2
SG
281 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
282 false))
283 printf("FDT memrsv map %d: Failed to add to map\n", i);
806d2fa8 284 }
806d2fa8
AG
285}
286
9dff4900 287static efi_status_t efi_install_fdt(ulong fdt_addr)
bc4f9133
HS
288{
289 bootm_headers_t img = { 0 };
9dff4900 290 ulong fdt_pages, fdt_size, fdt_start;
bc4f9133 291 efi_status_t ret;
9dff4900 292 void *fdt;
bc4f9133 293
9dff4900 294 fdt = map_sysmem(fdt_addr, 0);
bc4f9133
HS
295 if (fdt_check_header(fdt)) {
296 printf("ERROR: invalid device tree\n");
297 return EFI_INVALID_PARAMETER;
298 }
299
300 /* Prepare fdt for payload */
9dff4900
SG
301 ret = copy_fdt(&fdt_addr, &fdt_size);
302 if (ret)
303 return ret;
bc4f9133 304
9dff4900
SG
305 unmap_sysmem(fdt);
306 fdt = map_sysmem(fdt_addr, 0);
307 fdt_size = fdt_totalsize(fdt);
bc4f9133
HS
308 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
309 printf("ERROR: failed to process device tree\n");
310 return EFI_LOAD_ERROR;
311 }
312
416e07e2 313 efi_carve_out_dt_rsv(fdt);
806d2fa8 314
bc4f9133
HS
315 /* Link to it in the efi tables */
316 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
317 if (ret != EFI_SUCCESS)
318 return EFI_OUT_OF_RESOURCES;
319
320 /* And reserve the space in the memory map */
9dff4900 321 fdt_start = fdt_addr;
bc4f9133 322 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
9dff4900 323
bc4f9133
HS
324 ret = efi_add_memory_map(fdt_start, fdt_pages,
325 EFI_BOOT_SERVICES_DATA, true);
9dff4900 326
bc4f9133
HS
327 return ret;
328}
329
c982874e
HS
330/**
331 * do_bootefi_exec() - execute EFI binary
332 *
333 * @efi: address of the binary
334 * @device_path: path of the device from which the binary was loaded
335 * @image_path: device path of the binary
336 * Return: status code
337 *
338 * Load the EFI binary into a newly assigned memory unwinding the relocation
339 * information, install the loaded image protocol, and call the binary.
b9939336 340 */
bc4f9133 341static efi_status_t do_bootefi_exec(void *efi,
3eb0841b
HS
342 struct efi_device_path *device_path,
343 struct efi_device_path *image_path)
b9939336 344{
8887acc6 345 efi_handle_t mem_handle = NULL;
bf19273e 346 struct efi_device_path *memdp = NULL;
45204b10 347 efi_status_t ret;
faea1041 348 struct efi_loaded_image_obj *image_obj = NULL;
c982874e 349 struct efi_loaded_image *loaded_image_info = NULL;
95c5553e 350
c6fa5df6
AG
351 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
352 struct efi_system_table *st);
b9939336 353
bf19273e
RC
354 /*
355 * Special case for efi payload not loaded from disk, such as
356 * 'bootefi hello' or for example payload loaded directly into
8887acc6 357 * memory via jtag, etc:
bf19273e
RC
358 */
359 if (!device_path && !image_path) {
360 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
361 /* actual addresses filled in after efi_load_pe() */
362 memdp = efi_dp_from_mem(0, 0, 0);
363 device_path = image_path = memdp;
8887acc6
HS
364 /*
365 * Grub expects that the device path of the loaded image is
366 * installed on a handle.
367 */
368 ret = efi_create_handle(&mem_handle);
369 if (ret != EFI_SUCCESS)
370 goto exit;
371 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
58bc69d2
AG
372 device_path);
373 if (ret != EFI_SUCCESS)
374 goto exit;
bf19273e
RC
375 } else {
376 assert(device_path && image_path);
377 }
378
faea1041 379 ret = efi_setup_loaded_image(device_path, image_path, &image_obj,
c982874e
HS
380 &loaded_image_info);
381 if (ret != EFI_SUCCESS)
382 goto exit;
95c5553e 383
b57f48a8 384 /* Transfer environment variable bootargs as load options */
c982874e 385 set_load_options(loaded_image_info, "bootargs");
b9939336 386 /* Load the EFI payload */
faea1041 387 entry = efi_load_pe(image_obj, efi, loaded_image_info);
95c5553e 388 if (!entry) {
45204b10 389 ret = EFI_LOAD_ERROR;
95c5553e
RC
390 goto exit;
391 }
80a4800e 392
bf19273e
RC
393 if (memdp) {
394 struct efi_device_path_memory *mdp = (void *)memdp;
c982874e
HS
395 mdp->memory_type = loaded_image_info->image_code_type;
396 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
bf19273e 397 mdp->end_address = mdp->start_address +
c982874e 398 loaded_image_info->image_size;
bf19273e
RC
399 }
400
ad644e7c
RC
401 /* we don't support much: */
402 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
403 "{ro,boot}(blob)0000000000000000");
404
b9939336 405 /* Call our payload! */
edcef3ba 406 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
a86aeaf2 407
faea1041
HS
408 if (setjmp(&image_obj->exit_jmp)) {
409 ret = image_obj->exit_status;
95c5553e 410 goto exit;
a86aeaf2
AG
411 }
412
69bd459d
AG
413#ifdef CONFIG_ARM64
414 /* On AArch64 we need to make sure we call our payload in < EL3 */
415 if (current_el() == 3) {
416 smp_kick_all_cpus();
417 dcache_disable(); /* flush cache before switch to EL2 */
ec6617c3
AW
418
419 /* Move into EL2 and keep running there */
ea54ad59 420 armv8_switch_to_el2((ulong)entry,
d39646a3 421 (ulong)&image_obj->header,
7c5e1feb 422 (ulong)&systab, 0, (ulong)efi_run_in_el2,
ec6617c3
AW
423 ES_TO_AARCH64);
424
425 /* Should never reach here, efi exits with longjmp */
426 while (1) { }
69bd459d
AG
427 }
428#endif
429
dc500c36 430#ifdef CONFIG_ARMV7_NONSEC
f17f2001 431 if (armv7_boot_nonsec() && !is_nonsec) {
dc500c36
MK
432 dcache_disable(); /* flush cache before switch to HYP */
433
434 armv7_init_nonsec();
435 secure_ram_addr(_do_nonsec_entry)(
436 efi_run_in_hyp,
437 (uintptr_t)entry,
d39646a3 438 (uintptr_t)&image_obj->header,
dc500c36
MK
439 (uintptr_t)&systab);
440
441 /* Should never reach here, efi exits with longjmp */
442 while (1) { }
443 }
444#endif
445
d39646a3 446 ret = efi_do_enter(&image_obj->header, &systab, entry);
95c5553e
RC
447
448exit:
449 /* image has returned, loaded-image obj goes *poof*: */
faea1041 450 if (image_obj)
d39646a3 451 efi_delete_handle(&image_obj->header);
8887acc6
HS
452 if (mem_handle)
453 efi_delete_handle(mem_handle);
95c5553e
RC
454
455 return ret;
b9939336
AG
456}
457
bc4f9133 458static int do_bootefi_bootmgr_exec(void)
9975fe96
RC
459{
460 struct efi_device_path *device_path, *file_path;
461 void *addr;
462 efi_status_t r;
463
9975fe96
RC
464 addr = efi_bootmgr_load(&device_path, &file_path);
465 if (!addr)
466 return 1;
467
468 printf("## Starting EFI application at %p ...\n", addr);
bc4f9133 469 r = do_bootefi_exec(addr, device_path, file_path);
9975fe96
RC
470 printf("## Application terminated, r = %lu\n",
471 r & ~EFI_ERROR_MASK);
472
473 if (r != EFI_SUCCESS)
474 return 1;
475
476 return 0;
477}
478
b9939336
AG
479/* Interpreter command to boot an arbitrary EFI image from memory */
480static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
481{
bc4f9133
HS
482 unsigned long addr;
483 char *saddr;
3eb0841b 484 efi_status_t r;
354264b3 485 unsigned long fdt_addr;
b9939336 486
c3b11dea
HS
487 /* Allow unaligned memory access */
488 allow_unaligned();
489
fc225e60
HS
490 /* Initialize EFI drivers */
491 r = efi_init_obj_list();
492 if (r != EFI_SUCCESS) {
493 printf("Error: Cannot set up EFI drivers, r = %lu\n",
494 r & ~EFI_ERROR_MASK);
495 return CMD_RET_FAILURE;
496 }
497
b9939336 498 if (argc < 2)
3c1dcef6 499 return CMD_RET_USAGE;
bc4f9133
HS
500
501 if (argc > 2) {
354264b3 502 fdt_addr = simple_strtoul(argv[2], NULL, 16);
bc4f9133
HS
503 if (!fdt_addr && *argv[2] != '0')
504 return CMD_RET_USAGE;
505 /* Install device tree */
9dff4900 506 r = efi_install_fdt(fdt_addr);
bc4f9133
HS
507 if (r != EFI_SUCCESS) {
508 printf("ERROR: failed to install device tree\n");
509 return CMD_RET_FAILURE;
510 }
511 } else {
512 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
513 efi_install_configuration_table(&efi_guid_fdt, NULL);
514 printf("WARNING: booting without device tree\n");
515 }
c7ae3dfd
SG
516#ifdef CONFIG_CMD_BOOTEFI_HELLO
517 if (!strcmp(argv[1], "hello")) {
5e44489b 518 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
b9939336 519
51c533fd
HS
520 saddr = env_get("loadaddr");
521 if (saddr)
522 addr = simple_strtoul(saddr, NULL, 16);
523 else
524 addr = CONFIG_SYS_LOAD_ADDR;
354264b3 525 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
c7ae3dfd 526 } else
623b3a57
HS
527#endif
528#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
529 if (!strcmp(argv[1], "selftest")) {
faea1041 530 struct efi_loaded_image_obj *image_obj;
c982874e 531 struct efi_loaded_image *loaded_image_info;
7aca68ca 532
f972dc14
HS
533 /* Construct a dummy device path. */
534 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
535 (uintptr_t)&efi_selftest,
536 (uintptr_t)&efi_selftest);
537 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
538
c982874e 539 r = efi_setup_loaded_image(bootefi_device_path,
faea1041 540 bootefi_image_path, &image_obj,
c982874e
HS
541 &loaded_image_info);
542 if (r != EFI_SUCCESS)
543 return CMD_RET_FAILURE;
544
623b3a57 545 efi_save_gd();
d78e40d6 546 /* Transfer environment variable efi_selftest as load options */
c982874e 547 set_load_options(loaded_image_info, "efi_selftest");
d78e40d6 548 /* Execute the test */
d39646a3 549 r = efi_selftest(&image_obj->header, &systab);
c2b53902 550 efi_restore_gd();
c982874e 551 free(loaded_image_info->load_options);
d39646a3 552 efi_delete_handle(&image_obj->header);
c2b53902 553 return r != EFI_SUCCESS;
623b3a57 554 } else
c7ae3dfd 555#endif
9975fe96 556 if (!strcmp(argv[1], "bootmgr")) {
bc4f9133 557 return do_bootefi_bootmgr_exec();
9975fe96 558 } else {
c7ae3dfd 559 saddr = argv[1];
b9939336 560
c7ae3dfd 561 addr = simple_strtoul(saddr, NULL, 16);
49db1cb8
HS
562 /* Check that a numeric value was passed */
563 if (!addr && *saddr != '0')
564 return CMD_RET_USAGE;
c7ae3dfd 565
1c39809b
AG
566 }
567
5ee31baf 568 printf("## Starting EFI application at %08lx ...\n", addr);
354264b3 569 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
bc4f9133 570 bootefi_image_path);
1da1bac4
HS
571 printf("## Application terminated, r = %lu\n",
572 r & ~EFI_ERROR_MASK);
b9939336 573
1da1bac4
HS
574 if (r != EFI_SUCCESS)
575 return 1;
576 else
577 return 0;
b9939336
AG
578}
579
580#ifdef CONFIG_SYS_LONGHELP
581static char bootefi_help_text[] =
1c39809b
AG
582 "<image address> [fdt address]\n"
583 " - boot EFI payload stored at address <image address>.\n"
584 " If specified, the device tree located at <fdt address> gets\n"
c7ae3dfd
SG
585 " exposed as EFI configuration table.\n"
586#ifdef CONFIG_CMD_BOOTEFI_HELLO
623b3a57
HS
587 "bootefi hello\n"
588 " - boot a sample Hello World application stored within U-Boot\n"
589#endif
590#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
bc4f9133 591 "bootefi selftest [fdt address]\n"
623b3a57 592 " - boot an EFI selftest application stored within U-Boot\n"
d78e40d6
HS
593 " Use environment variable efi_selftest to select a single test.\n"
594 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
c7ae3dfd 595#endif
f623e07f 596 "bootefi bootmgr [fdt addr]\n"
9975fe96
RC
597 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
598 "\n"
599 " If specified, the device tree located at <fdt address> gets\n"
600 " exposed as EFI configuration table.\n";
b9939336
AG
601#endif
602
603U_BOOT_CMD(
1c39809b 604 bootefi, 3, 0, do_bootefi,
92dfd922 605 "Boots an EFI payload from memory",
b9939336
AG
606 bootefi_help_text
607);
0f4060eb 608
95c5553e
RC
609void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
610{
611 char filename[32] = { 0 }; /* dp->str is u16[32] long */
612 char *s;
f9d334bd 613
79276eb2
HS
614 /* efi_set_bootdev is typically called repeatedly, recover memory */
615 efi_free_pool(bootefi_device_path);
616 efi_free_pool(bootefi_image_path);
617 /* If blk_get_device_part_str fails, avoid duplicate free. */
618 bootefi_device_path = NULL;
619 bootefi_image_path = NULL;
620
95c5553e
RC
621 if (strcmp(dev, "Net")) {
622 struct blk_desc *desc;
2db1eba1 623 disk_partition_t fs_partition;
95c5553e 624 int part;
8c3df0bf 625
2db1eba1
HS
626 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
627 1);
628 if (part < 0)
8300be61 629 return;
0f4060eb 630
95c5553e
RC
631 bootefi_device_path = efi_dp_from_part(desc, part);
632 } else {
092f2f35 633#ifdef CONFIG_NET
95c5553e
RC
634 bootefi_device_path = efi_dp_from_eth();
635#endif
636 }
c07ad7c0 637
9975fe96
RC
638 if (!path)
639 return;
640
49271666
AG
641 if (strcmp(dev, "Net")) {
642 /* Add leading / to fs paths, because they're absolute */
95c5553e 643 snprintf(filename, sizeof(filename), "/%s", path);
49271666 644 } else {
95c5553e 645 snprintf(filename, sizeof(filename), "%s", path);
49271666 646 }
3e433e96 647 /* DOS style file path: */
95c5553e 648 s = filename;
3e433e96
RC
649 while ((s = strchr(s, '/')))
650 *s++ = '\\';
95c5553e 651 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
0f4060eb 652}