blob: 4233d36b72288c7fb611826aacc65eff2892693a [file] [log] [blame]
Alexander Grafe2b04f22016-03-10 00:27:20 +01001/*
2 * EFI application loader
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +02009#include <charset.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010010#include <common.h>
11#include <command.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010013#include <efi_loader.h>
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020014#include <efi_selftest.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010015#include <errno.h>
16#include <libfdt.h>
17#include <libfdt_env.h>
Alexander Graffdbae012016-04-11 23:51:01 +020018#include <memalign.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020019#include <asm/global_data.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060020#include <asm-generic/sections.h>
21#include <linux/linkage.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020022
23DECLARE_GLOBAL_DATA_PTR;
Alexander Grafe2b04f22016-03-10 00:27:20 +010024
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020025static uint8_t efi_obj_list_initalized;
26
Rob Clarkf8db9222017-09-13 18:05:33 -040027static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
Alexander Grafe2b04f22016-03-10 00:27:20 +010029
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020030/* Initialize and populate EFI object list */
31static void efi_init_obj_list(void)
32{
33 efi_obj_list_initalized = 1;
34
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010035 /* Initialize EFI driver uclass */
36 efi_driver_init();
37
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020038 efi_console_register();
39#ifdef CONFIG_PARTITIONS
40 efi_disk_register();
41#endif
42#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
43 efi_gop_register();
44#endif
45#ifdef CONFIG_NET
Rob Clarkf8db9222017-09-13 18:05:33 -040046 efi_net_register();
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020047#endif
48#ifdef CONFIG_GENERATE_SMBIOS_TABLE
49 efi_smbios_register();
50#endif
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020051 efi_watchdog_register();
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020052
53 /* Initialize EFI runtime services */
54 efi_reset_system_init();
55 efi_get_time_init();
56}
57
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020058/*
59 * Set the load options of an image from an environment variable.
60 *
61 * @loaded_image_info: the image
62 * @env_var: name of the environment variable
63 */
64static void set_load_options(struct efi_loaded_image *loaded_image_info,
65 const char *env_var)
66{
67 size_t size;
68 const char *env = env_get(env_var);
69
70 loaded_image_info->load_options = NULL;
71 loaded_image_info->load_options_size = 0;
72 if (!env)
73 return;
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");
78 return;
79 }
80 utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
81 loaded_image_info->load_options_size = size * 2;
82}
83
Alexander Graf2ff5cc32016-04-11 16:55:26 +020084static void *copy_fdt(void *fdt)
85{
86 u64 fdt_size = fdt_totalsize(fdt);
Alexander Graffdbae012016-04-11 23:51:01 +020087 unsigned long fdt_ram_start = -1L, fdt_pages;
88 u64 new_fdt_addr;
Alexander Graf2ff5cc32016-04-11 16:55:26 +020089 void *new_fdt;
Alexander Graffdbae012016-04-11 23:51:01 +020090 int i;
Alexander Graf2ff5cc32016-04-11 16:55:26 +020091
Alexander Graffdbae012016-04-11 23:51:01 +020092 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;
Alexander Graf2ff5cc32016-04-11 16:55:26 +020095
Alexander Graffdbae012016-04-11 23:51:01 +020096 if (!ram_size)
97 continue;
98
99 if (ram_start < fdt_ram_start)
100 fdt_ram_start = ram_start;
101 }
102
103 /* Give us at least 4kb breathing room */
xypron.glpk@gmx.def6b99122017-08-11 21:19:25 +0200104 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
Alexander Graffdbae012016-04-11 23:51:01 +0200105 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
106
107 /* Safe fdt location is at 128MB */
108 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
Heinrich Schuchardt5731fd42018-01-18 23:21:22 +0100109 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
Alexander Graffdbae012016-04-11 23:51:01 +0200110 &new_fdt_addr) != EFI_SUCCESS) {
111 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.def6b99122017-08-11 21:19:25 +0200112 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Heinrich Schuchardt5731fd42018-01-18 23:21:22 +0100113 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
Alexander Graf9b9b7162017-07-03 13:32:35 +0200114 &new_fdt_addr) != EFI_SUCCESS) {
115 printf("ERROR: Failed to reserve space for FDT\n");
116 return NULL;
117 }
Alexander Graffdbae012016-04-11 23:51:01 +0200118 }
Alexander Graf9b9b7162017-07-03 13:32:35 +0200119
Alexander Graffdbae012016-04-11 23:51:01 +0200120 new_fdt = (void*)(ulong)new_fdt_addr;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200121 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
122 fdt_set_totalsize(new_fdt, fdt_size);
123
124 return new_fdt;
125}
126
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200127static efi_status_t efi_do_enter(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100128 efi_handle_t image_handle, struct efi_system_table *st,
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100129 EFIAPI efi_status_t (*entry)(
130 efi_handle_t image_handle,
131 struct efi_system_table *st))
xypron.glpk@gmx.de1e5afc62017-07-04 23:15:21 +0200132{
133 efi_status_t ret = EFI_LOAD_ERROR;
134
135 if (entry)
136 ret = entry(image_handle, st);
137 st->boottime->exit(image_handle, ret, 0, NULL);
138 return ret;
139}
140
Alison Wang73818d52016-11-10 10:49:03 +0800141#ifdef CONFIG_ARM64
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100142static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100143 efi_handle_t image_handle, struct efi_system_table *st),
144 efi_handle_t image_handle, struct efi_system_table *st)
Alison Wang73818d52016-11-10 10:49:03 +0800145{
146 /* Enable caches again */
147 dcache_enable();
148
xypron.glpk@gmx.de1e5afc62017-07-04 23:15:21 +0200149 return efi_do_enter(image_handle, st, entry);
Alison Wang73818d52016-11-10 10:49:03 +0800150}
151#endif
152
Alexander Grafe2b04f22016-03-10 00:27:20 +0100153/*
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.
156 */
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200157static efi_status_t do_bootefi_exec(void *efi, void *fdt,
158 struct efi_device_path *device_path,
159 struct efi_device_path *image_path)
Alexander Grafe2b04f22016-03-10 00:27:20 +0100160{
Rob Clarkf8db9222017-09-13 18:05:33 -0400161 struct efi_loaded_image loaded_image_info = {};
162 struct efi_object loaded_image_info_obj = {};
Rob Clark18ceba72017-10-10 08:23:06 -0400163 struct efi_device_path *memdp = NULL;
Rob Clarkf8db9222017-09-13 18:05:33 -0400164 ulong ret;
165
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100166 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
167 struct efi_system_table *st);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100168 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
Alexander Graf5fb802a2017-07-26 13:41:05 +0200169 const efi_guid_t fdt_guid = EFI_FDT_GUID;
Alexander Grafc81b3472016-03-04 01:10:13 +0100170 bootm_headers_t img = { 0 };
Alexander Grafe2b04f22016-03-10 00:27:20 +0100171
Rob Clark18ceba72017-10-10 08:23:06 -0400172 /*
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:
176 */
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;
182 } else {
183 assert(device_path && image_path);
184 }
185
Rob Clarkf8db9222017-09-13 18:05:33 -0400186 /* Initialize and populate EFI object list */
187 if (!efi_obj_list_initalized)
188 efi_init_obj_list();
189
190 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
191 device_path, image_path);
192
Alexander Grafe2b04f22016-03-10 00:27:20 +0100193 /*
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
196 */
197 efi_save_gd();
198
Alexander Graf54c1e832016-04-14 16:07:53 +0200199 if (fdt && !fdt_check_header(fdt)) {
Alexander Grafc81b3472016-03-04 01:10:13 +0100200 /* Prepare fdt for payload */
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200201 fdt = copy_fdt(fdt);
202
203 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Alexander Grafc81b3472016-03-04 01:10:13 +0100204 printf("ERROR: Failed to process device tree\n");
205 return -EINVAL;
206 }
207
208 /* Link to it in the efi tables */
Alexander Graf5fb802a2017-07-26 13:41:05 +0200209 efi_install_configuration_table(&fdt_guid, fdt);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100210
211 /* And reserve the space in the memory map */
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200212 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
213 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100214 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 */
217 fdt_pages += 2;
218 efi_add_memory_map(fdt_start, fdt_pages,
219 EFI_BOOT_SERVICES_DATA, true);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100220 } else {
Alexander Graf54c1e832016-04-14 16:07:53 +0200221 printf("WARNING: Invalid device tree, expect boot to fail\n");
Alexander Graf5fb802a2017-07-26 13:41:05 +0200222 efi_install_configuration_table(&fdt_guid, NULL);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100223 }
224
Heinrich Schuchardta4589e22017-10-18 18:13:15 +0200225 /* Transfer environment variable bootargs as load options */
226 set_load_options(&loaded_image_info, "bootargs");
Alexander Grafe2b04f22016-03-10 00:27:20 +0100227 /* Load the EFI payload */
228 entry = efi_load_pe(efi, &loaded_image_info);
Rob Clarkf8db9222017-09-13 18:05:33 -0400229 if (!entry) {
230 ret = -ENOENT;
231 goto exit;
232 }
Alexander Graf77256092016-08-16 21:08:45 +0200233
Rob Clark18ceba72017-10-10 08:23:06 -0400234 if (memdp) {
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;
240 }
241
Rob Clark15f3d742017-09-13 18:05:37 -0400242 /* we don't support much: */
243 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
244 "{ro,boot}(blob)0000000000000000");
245
Alexander Grafe2b04f22016-03-10 00:27:20 +0100246 /* Call our payload! */
Alexander Graf62a67482016-06-02 11:38:27 +0200247 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
Alexander Graf988c0662016-05-20 23:28:23 +0200248
249 if (setjmp(&loaded_image_info.exit_jmp)) {
Rob Clarkf8db9222017-09-13 18:05:33 -0400250 ret = loaded_image_info.exit_status;
Rob Clarkf8db9222017-09-13 18:05:33 -0400251 goto exit;
Alexander Graf988c0662016-05-20 23:28:23 +0200252 }
253
Alexander Grafbfd199c2016-11-17 01:02:58 +0100254#ifdef CONFIG_ARM64
255 /* On AArch64 we need to make sure we call our payload in < EL3 */
256 if (current_el() == 3) {
257 smp_kick_all_cpus();
258 dcache_disable(); /* flush cache before switch to EL2 */
Alison Wang73818d52016-11-10 10:49:03 +0800259
260 /* Move into EL2 and keep running there */
Heinrich Schuchardt754d4972017-11-26 14:05:22 +0100261 armv8_switch_to_el2((ulong)entry,
262 (ulong)&loaded_image_info_obj.handle,
Alison Wangeb2088d2017-01-17 09:39:17 +0800263 (ulong)&systab, 0, (ulong)efi_run_in_el2,
Alison Wang73818d52016-11-10 10:49:03 +0800264 ES_TO_AARCH64);
265
266 /* Should never reach here, efi exits with longjmp */
267 while (1) { }
Alexander Grafbfd199c2016-11-17 01:02:58 +0100268 }
269#endif
270
Heinrich Schuchardt754d4972017-11-26 14:05:22 +0100271 ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
Rob Clarkf8db9222017-09-13 18:05:33 -0400272
273exit:
274 /* image has returned, loaded-image obj goes *poof*: */
275 list_del(&loaded_image_info_obj.link);
276
277 return ret;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100278}
279
Rob Clarkc84c1102017-09-13 18:05:38 -0400280static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
281{
282 struct efi_device_path *device_path, *file_path;
283 void *addr;
284 efi_status_t r;
285
286 /* Initialize and populate EFI object list */
287 if (!efi_obj_list_initalized)
288 efi_init_obj_list();
289
290 /*
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
293 */
294 efi_save_gd();
295
296 addr = efi_bootmgr_load(&device_path, &file_path);
297 if (!addr)
298 return 1;
299
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);
304
305 if (r != EFI_SUCCESS)
306 return 1;
307
308 return 0;
309}
310
Alexander Grafe2b04f22016-03-10 00:27:20 +0100311/* Interpreter command to boot an arbitrary EFI image from memory */
312static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
313{
Alexander Graf54c1e832016-04-14 16:07:53 +0200314 char *saddr, *sfdt;
315 unsigned long addr, fdt_addr = 0;
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200316 efi_status_t r;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100317
318 if (argc < 2)
Bin Mengda800ff2016-08-13 04:02:06 -0700319 return CMD_RET_USAGE;
Simon Glassfac4ced2016-11-07 08:47:08 -0700320#ifdef CONFIG_CMD_BOOTEFI_HELLO
321 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt7804ae92017-09-05 03:19:37 +0200322 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100323
Heinrich Schuchardte6a9ab52017-08-28 18:54:30 +0200324 saddr = env_get("loadaddr");
325 if (saddr)
326 addr = simple_strtoul(saddr, NULL, 16);
327 else
328 addr = CONFIG_SYS_LOAD_ADDR;
Heinrich Schuchardt7804ae92017-09-05 03:19:37 +0200329 memcpy((char *)addr, __efi_helloworld_begin, size);
Simon Glassfac4ced2016-11-07 08:47:08 -0700330 } else
331#endif
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200332#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
333 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardt389f2512017-09-20 22:54:58 +0200334 struct efi_loaded_image loaded_image_info = {};
335 struct efi_object loaded_image_info_obj = {};
336
Heinrich Schuchardta809bc22017-10-18 18:13:09 +0200337 /* 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");
342
Heinrich Schuchardt389f2512017-09-20 22:54:58 +0200343 efi_setup_loaded_image(&loaded_image_info,
344 &loaded_image_info_obj,
345 bootefi_device_path, bootefi_image_path);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200346 /*
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
349 * callback entry
350 */
351 efi_save_gd();
352 /* Initialize and populate EFI object list */
353 if (!efi_obj_list_initalized)
354 efi_init_obj_list();
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200355 /* Transfer environment variable efi_selftest as load options */
356 set_load_options(&loaded_image_info, "efi_selftest");
357 /* Execute the test */
Heinrich Schuchardt754d4972017-11-26 14:05:22 +0100358 r = efi_selftest(loaded_image_info_obj.handle, &systab);
Heinrich Schuchardtd2d90b42017-10-18 18:13:14 +0200359 efi_restore_gd();
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200360 free(loaded_image_info.load_options);
Heinrich Schuchardtd2d90b42017-10-18 18:13:14 +0200361 list_del(&loaded_image_info_obj.link);
362 return r != EFI_SUCCESS;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200363 } else
364#endif
Rob Clarkc84c1102017-09-13 18:05:38 -0400365 if (!strcmp(argv[1], "bootmgr")) {
366 unsigned long fdt_addr = 0;
367
368 if (argc > 2)
369 fdt_addr = simple_strtoul(argv[2], NULL, 16);
370
371 return do_bootefi_bootmgr_exec(fdt_addr);
372 } else {
Simon Glassfac4ced2016-11-07 08:47:08 -0700373 saddr = argv[1];
Alexander Grafe2b04f22016-03-10 00:27:20 +0100374
Simon Glassfac4ced2016-11-07 08:47:08 -0700375 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardtaa6fd9a2018-01-24 20:33:54 +0100376 /* Check that a numeric value was passed */
377 if (!addr && *saddr != '0')
378 return CMD_RET_USAGE;
Simon Glassfac4ced2016-11-07 08:47:08 -0700379
380 if (argc > 2) {
381 sfdt = argv[2];
382 fdt_addr = simple_strtoul(sfdt, NULL, 16);
383 }
Alexander Graf54c1e832016-04-14 16:07:53 +0200384 }
385
Simon Glass1a5490e2016-11-07 08:47:05 -0700386 printf("## Starting EFI application at %08lx ...\n", addr);
Rob Clarkf8db9222017-09-13 18:05:33 -0400387 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
388 bootefi_device_path, bootefi_image_path);
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200389 printf("## Application terminated, r = %lu\n",
390 r & ~EFI_ERROR_MASK);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100391
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200392 if (r != EFI_SUCCESS)
393 return 1;
394 else
395 return 0;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100396}
397
398#ifdef CONFIG_SYS_LONGHELP
399static char bootefi_help_text[] =
Alexander Graf54c1e832016-04-14 16:07:53 +0200400 "<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"
Simon Glassfac4ced2016-11-07 08:47:08 -0700403 " exposed as EFI configuration table.\n"
404#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200405 "bootefi hello\n"
406 " - boot a sample Hello World application stored within U-Boot\n"
407#endif
408#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
409 "bootefi selftest\n"
410 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200411 " Use environment variable efi_selftest to select a single test.\n"
412 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700413#endif
Rob Clarkc84c1102017-09-13 18:05:38 -0400414 "bootmgr [fdt addr]\n"
415 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
416 "\n"
417 " If specified, the device tree located at <fdt address> gets\n"
418 " exposed as EFI configuration table.\n";
Alexander Grafe2b04f22016-03-10 00:27:20 +0100419#endif
420
421U_BOOT_CMD(
Alexander Graf54c1e832016-04-14 16:07:53 +0200422 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn268f19e2016-06-07 11:14:31 -0700423 "Boots an EFI payload from memory",
Alexander Grafe2b04f22016-03-10 00:27:20 +0100424 bootefi_help_text
425);
Alexander Graf8f19f262016-03-04 01:10:14 +0100426
Rob Clarkf8db9222017-09-13 18:05:33 -0400427static int parse_partnum(const char *devnr)
Alexander Graf8f19f262016-03-04 01:10:14 +0100428{
Rob Clarkf8db9222017-09-13 18:05:33 -0400429 const char *str = strchr(devnr, ':');
430 if (str) {
431 str++;
432 return simple_strtoul(str, NULL, 16);
Alexander Graf34118e72016-08-05 14:49:53 +0200433 }
Rob Clarkf8db9222017-09-13 18:05:33 -0400434 return 0;
435}
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200436
Rob Clarkf8db9222017-09-13 18:05:33 -0400437void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
438{
439 char filename[32] = { 0 }; /* dp->str is u16[32] long */
440 char *s;
Alexander Graf34118e72016-08-05 14:49:53 +0200441
Rob Clarkf8db9222017-09-13 18:05:33 -0400442 if (strcmp(dev, "Net")) {
443 struct blk_desc *desc;
444 int part;
Alexander Grafeb8d82c2016-04-11 16:16:18 +0200445
Rob Clarkf8db9222017-09-13 18:05:33 -0400446 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
Stefan Roesedf769762017-11-17 08:47:09 +0100447 if (!desc)
448 return;
Rob Clarkf8db9222017-09-13 18:05:33 -0400449 part = parse_partnum(devnr);
Alexander Graf8f19f262016-03-04 01:10:14 +0100450
Rob Clarkf8db9222017-09-13 18:05:33 -0400451 bootefi_device_path = efi_dp_from_part(desc, part);
452 } else {
453#ifdef CONFIG_NET
454 bootefi_device_path = efi_dp_from_eth();
455#endif
456 }
Alexander Grafc49a1b82016-04-11 16:16:19 +0200457
Rob Clarkc84c1102017-09-13 18:05:38 -0400458 if (!path)
459 return;
460
Alexander Graf45e437d2016-07-21 01:44:46 +0200461 if (strcmp(dev, "Net")) {
462 /* Add leading / to fs paths, because they're absolute */
Rob Clarkf8db9222017-09-13 18:05:33 -0400463 snprintf(filename, sizeof(filename), "/%s", path);
Alexander Graf45e437d2016-07-21 01:44:46 +0200464 } else {
Rob Clarkf8db9222017-09-13 18:05:33 -0400465 snprintf(filename, sizeof(filename), "%s", path);
Alexander Graf45e437d2016-07-21 01:44:46 +0200466 }
Rob Clark65336522017-07-24 07:59:09 -0400467 /* DOS style file path: */
Rob Clarkf8db9222017-09-13 18:05:33 -0400468 s = filename;
Rob Clark65336522017-07-24 07:59:09 -0400469 while ((s = strchr(s, '/')))
470 *s++ = '\\';
Rob Clarkf8db9222017-09-13 18:05:33 -0400471 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
Alexander Graf8f19f262016-03-04 01:10:14 +0100472}