blob: 51213c0293c307b843b4a1da819d9141632b9d20 [file] [log] [blame]
Alexander Grafb9939332016-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 Schuchardtd78e40d2017-10-18 18:13:13 +02009#include <charset.h>
Alexander Grafb9939332016-03-10 00:27:20 +010010#include <common.h>
11#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Alexander Grafb9939332016-03-10 00:27:20 +010013#include <efi_loader.h>
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020014#include <efi_selftest.h>
Alexander Grafb9939332016-03-10 00:27:20 +010015#include <errno.h>
16#include <libfdt.h>
17#include <libfdt_env.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020018#include <memalign.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020019#include <asm/global_data.h>
Simon Glasse2754582016-09-25 15:27:32 -060020#include <asm-generic/sections.h>
21#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020022
23DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010024
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020025static uint8_t efi_obj_list_initalized;
26
Rob Clark95c55532017-09-13 18:05:33 -040027static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
Alexander Grafb9939332016-03-10 00:27:20 +010029
Heinrich Schuchardt7cbc1242017-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 Schuchardt05ef48a2018-01-21 19:29:30 +010035 /* Initialize EFI driver uclass */
36 efi_driver_init();
37
Heinrich Schuchardt7cbc1242017-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 Clark95c55532017-09-13 18:05:33 -040046 efi_net_register();
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020047#endif
48#ifdef CONFIG_GENERATE_SMBIOS_TABLE
49 efi_smbios_register();
50#endif
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +020051 efi_watchdog_register();
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020052
53 /* Initialize EFI runtime services */
54 efi_reset_system_init();
55 efi_get_time_init();
56}
57
Heinrich Schuchardtd78e40d2017-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 Graf0d9d5012016-04-11 16:55:26 +020084static void *copy_fdt(void *fdt)
85{
86 u64 fdt_size = fdt_totalsize(fdt);
Alexander Grafad0c1a32016-04-11 23:51:01 +020087 unsigned long fdt_ram_start = -1L, fdt_pages;
88 u64 new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +020089 void *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +020090 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +020091
Alexander Grafad0c1a32016-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 Graf0d9d5012016-04-11 16:55:26 +020095
Alexander Grafad0c1a32016-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.dea44bffc2017-08-11 21:19:25 +0200104 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
Alexander Grafad0c1a32016-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 Schuchardt17ff6f02018-01-18 23:21:22 +0100109 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
Alexander Grafad0c1a32016-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.dea44bffc2017-08-11 21:19:25 +0200112 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Heinrich Schuchardt17ff6f02018-01-18 23:21:22 +0100113 if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
Alexander Graf85a6e9b2017-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 Grafad0c1a32016-04-11 23:51:01 +0200118 }
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200119
Alexander Grafad0c1a32016-04-11 23:51:01 +0200120 new_fdt = (void*)(ulong)new_fdt_addr;
Alexander Graf0d9d5012016-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 Schuchardt3eb08412017-10-18 18:13:08 +0200127static efi_status_t efi_do_enter(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100128 efi_handle_t image_handle, struct efi_system_table *st,
129 asmlinkage ulong (*entry)(efi_handle_t image_handle,
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200130 struct efi_system_table *st))
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +0200131{
132 efi_status_t ret = EFI_LOAD_ERROR;
133
134 if (entry)
135 ret = entry(image_handle, st);
136 st->boottime->exit(image_handle, ret, 0, NULL);
137 return ret;
138}
139
Alison Wangec6617c2016-11-10 10:49:03 +0800140#ifdef CONFIG_ARM64
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200141static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100142 efi_handle_t image_handle, struct efi_system_table *st),
143 efi_handle_t image_handle, struct efi_system_table *st)
Alison Wangec6617c2016-11-10 10:49:03 +0800144{
145 /* Enable caches again */
146 dcache_enable();
147
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +0200148 return efi_do_enter(image_handle, st, entry);
Alison Wangec6617c2016-11-10 10:49:03 +0800149}
150#endif
151
Alexander Grafb9939332016-03-10 00:27:20 +0100152/*
153 * Load an EFI payload into a newly allocated piece of memory, register all
154 * EFI objects it would want to access and jump to it.
155 */
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200156static efi_status_t do_bootefi_exec(void *efi, void *fdt,
157 struct efi_device_path *device_path,
158 struct efi_device_path *image_path)
Alexander Grafb9939332016-03-10 00:27:20 +0100159{
Rob Clark95c55532017-09-13 18:05:33 -0400160 struct efi_loaded_image loaded_image_info = {};
161 struct efi_object loaded_image_info_obj = {};
Rob Clarkbf192732017-10-10 08:23:06 -0400162 struct efi_device_path *memdp = NULL;
Rob Clark95c55532017-09-13 18:05:33 -0400163 ulong ret;
164
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100165 ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st)
Simon Glasse2754582016-09-25 15:27:32 -0600166 asmlinkage;
Alexander Grafb9939332016-03-10 00:27:20 +0100167 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
Alexander Graff4f99932017-07-26 13:41:05 +0200168 const efi_guid_t fdt_guid = EFI_FDT_GUID;
Alexander Grafdea21742016-03-04 01:10:13 +0100169 bootm_headers_t img = { 0 };
Alexander Grafb9939332016-03-10 00:27:20 +0100170
Rob Clarkbf192732017-10-10 08:23:06 -0400171 /*
172 * Special case for efi payload not loaded from disk, such as
173 * 'bootefi hello' or for example payload loaded directly into
174 * memory via jtag/etc:
175 */
176 if (!device_path && !image_path) {
177 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
178 /* actual addresses filled in after efi_load_pe() */
179 memdp = efi_dp_from_mem(0, 0, 0);
180 device_path = image_path = memdp;
181 } else {
182 assert(device_path && image_path);
183 }
184
Rob Clark95c55532017-09-13 18:05:33 -0400185 /* Initialize and populate EFI object list */
186 if (!efi_obj_list_initalized)
187 efi_init_obj_list();
188
189 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
190 device_path, image_path);
191
Alexander Grafb9939332016-03-10 00:27:20 +0100192 /*
193 * gd lives in a fixed register which may get clobbered while we execute
194 * the payload. So save it here and restore it on every callback entry
195 */
196 efi_save_gd();
197
Alexander Graf1c398092016-04-14 16:07:53 +0200198 if (fdt && !fdt_check_header(fdt)) {
Alexander Grafdea21742016-03-04 01:10:13 +0100199 /* Prepare fdt for payload */
Alexander Graf0d9d5012016-04-11 16:55:26 +0200200 fdt = copy_fdt(fdt);
201
202 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Alexander Grafdea21742016-03-04 01:10:13 +0100203 printf("ERROR: Failed to process device tree\n");
204 return -EINVAL;
205 }
206
207 /* Link to it in the efi tables */
Alexander Graff4f99932017-07-26 13:41:05 +0200208 efi_install_configuration_table(&fdt_guid, fdt);
Alexander Grafb9939332016-03-10 00:27:20 +0100209
210 /* And reserve the space in the memory map */
Alexander Graf0d9d5012016-04-11 16:55:26 +0200211 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
212 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
Alexander Grafb9939332016-03-10 00:27:20 +0100213 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
214 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
215 /* Give a bootloader the chance to modify the device tree */
216 fdt_pages += 2;
217 efi_add_memory_map(fdt_start, fdt_pages,
218 EFI_BOOT_SERVICES_DATA, true);
Alexander Grafb9939332016-03-10 00:27:20 +0100219 } else {
Alexander Graf1c398092016-04-14 16:07:53 +0200220 printf("WARNING: Invalid device tree, expect boot to fail\n");
Alexander Graff4f99932017-07-26 13:41:05 +0200221 efi_install_configuration_table(&fdt_guid, NULL);
Alexander Grafb9939332016-03-10 00:27:20 +0100222 }
223
Heinrich Schuchardtb57f48a2017-10-18 18:13:15 +0200224 /* Transfer environment variable bootargs as load options */
225 set_load_options(&loaded_image_info, "bootargs");
Alexander Grafb9939332016-03-10 00:27:20 +0100226 /* Load the EFI payload */
227 entry = efi_load_pe(efi, &loaded_image_info);
Rob Clark95c55532017-09-13 18:05:33 -0400228 if (!entry) {
229 ret = -ENOENT;
230 goto exit;
231 }
Alexander Graf80a48002016-08-16 21:08:45 +0200232
Rob Clarkbf192732017-10-10 08:23:06 -0400233 if (memdp) {
234 struct efi_device_path_memory *mdp = (void *)memdp;
235 mdp->memory_type = loaded_image_info.image_code_type;
236 mdp->start_address = (uintptr_t)loaded_image_info.image_base;
237 mdp->end_address = mdp->start_address +
238 loaded_image_info.image_size;
239 }
240
Rob Clarkad644e72017-09-13 18:05:37 -0400241 /* we don't support much: */
242 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
243 "{ro,boot}(blob)0000000000000000");
244
Alexander Grafb9939332016-03-10 00:27:20 +0100245 /* Call our payload! */
Alexander Grafedcef3b2016-06-02 11:38:27 +0200246 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200247
248 if (setjmp(&loaded_image_info.exit_jmp)) {
Rob Clark95c55532017-09-13 18:05:33 -0400249 ret = loaded_image_info.exit_status;
Rob Clark95c55532017-09-13 18:05:33 -0400250 goto exit;
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200251 }
252
Alexander Graf69bd4592016-11-17 01:02:58 +0100253#ifdef CONFIG_ARM64
254 /* On AArch64 we need to make sure we call our payload in < EL3 */
255 if (current_el() == 3) {
256 smp_kick_all_cpus();
257 dcache_disable(); /* flush cache before switch to EL2 */
Alison Wangec6617c2016-11-10 10:49:03 +0800258
259 /* Move into EL2 and keep running there */
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +0100260 armv8_switch_to_el2((ulong)entry,
261 (ulong)&loaded_image_info_obj.handle,
Alison Wang7c5e1fe2017-01-17 09:39:17 +0800262 (ulong)&systab, 0, (ulong)efi_run_in_el2,
Alison Wangec6617c2016-11-10 10:49:03 +0800263 ES_TO_AARCH64);
264
265 /* Should never reach here, efi exits with longjmp */
266 while (1) { }
Alexander Graf69bd4592016-11-17 01:02:58 +0100267 }
268#endif
269
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +0100270 ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
Rob Clark95c55532017-09-13 18:05:33 -0400271
272exit:
273 /* image has returned, loaded-image obj goes *poof*: */
274 list_del(&loaded_image_info_obj.link);
275
276 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100277}
278
Rob Clark9975fe92017-09-13 18:05:38 -0400279static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
280{
281 struct efi_device_path *device_path, *file_path;
282 void *addr;
283 efi_status_t r;
284
285 /* Initialize and populate EFI object list */
286 if (!efi_obj_list_initalized)
287 efi_init_obj_list();
288
289 /*
290 * gd lives in a fixed register which may get clobbered while we execute
291 * the payload. So save it here and restore it on every callback entry
292 */
293 efi_save_gd();
294
295 addr = efi_bootmgr_load(&device_path, &file_path);
296 if (!addr)
297 return 1;
298
299 printf("## Starting EFI application at %p ...\n", addr);
300 r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
301 printf("## Application terminated, r = %lu\n",
302 r & ~EFI_ERROR_MASK);
303
304 if (r != EFI_SUCCESS)
305 return 1;
306
307 return 0;
308}
309
Alexander Grafb9939332016-03-10 00:27:20 +0100310/* Interpreter command to boot an arbitrary EFI image from memory */
311static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
312{
Alexander Graf1c398092016-04-14 16:07:53 +0200313 char *saddr, *sfdt;
314 unsigned long addr, fdt_addr = 0;
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200315 efi_status_t r;
Alexander Grafb9939332016-03-10 00:27:20 +0100316
317 if (argc < 2)
Bin Meng3c1dcef2016-08-13 04:02:06 -0700318 return CMD_RET_USAGE;
Simon Glassc7ae3df2016-11-07 08:47:08 -0700319#ifdef CONFIG_CMD_BOOTEFI_HELLO
320 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200321 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafb9939332016-03-10 00:27:20 +0100322
Heinrich Schuchardt51c533f2017-08-28 18:54:30 +0200323 saddr = env_get("loadaddr");
324 if (saddr)
325 addr = simple_strtoul(saddr, NULL, 16);
326 else
327 addr = CONFIG_SYS_LOAD_ADDR;
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200328 memcpy((char *)addr, __efi_helloworld_begin, size);
Simon Glassc7ae3df2016-11-07 08:47:08 -0700329 } else
330#endif
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200331#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
332 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardt7aca68c2017-09-20 22:54:58 +0200333 struct efi_loaded_image loaded_image_info = {};
334 struct efi_object loaded_image_info_obj = {};
335
Heinrich Schuchardtf972dc12017-10-18 18:13:09 +0200336 /* Construct a dummy device path. */
337 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
338 (uintptr_t)&efi_selftest,
339 (uintptr_t)&efi_selftest);
340 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
341
Heinrich Schuchardt7aca68c2017-09-20 22:54:58 +0200342 efi_setup_loaded_image(&loaded_image_info,
343 &loaded_image_info_obj,
344 bootefi_device_path, bootefi_image_path);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200345 /*
346 * gd lives in a fixed register which may get clobbered while we
347 * execute the payload. So save it here and restore it on every
348 * callback entry
349 */
350 efi_save_gd();
351 /* Initialize and populate EFI object list */
352 if (!efi_obj_list_initalized)
353 efi_init_obj_list();
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200354 /* Transfer environment variable efi_selftest as load options */
355 set_load_options(&loaded_image_info, "efi_selftest");
356 /* Execute the test */
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +0100357 r = efi_selftest(loaded_image_info_obj.handle, &systab);
Heinrich Schuchardtc2b53902017-10-18 18:13:14 +0200358 efi_restore_gd();
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200359 free(loaded_image_info.load_options);
Heinrich Schuchardtc2b53902017-10-18 18:13:14 +0200360 list_del(&loaded_image_info_obj.link);
361 return r != EFI_SUCCESS;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200362 } else
363#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400364 if (!strcmp(argv[1], "bootmgr")) {
365 unsigned long fdt_addr = 0;
366
367 if (argc > 2)
368 fdt_addr = simple_strtoul(argv[2], NULL, 16);
369
370 return do_bootefi_bootmgr_exec(fdt_addr);
371 } else {
Simon Glassc7ae3df2016-11-07 08:47:08 -0700372 saddr = argv[1];
Alexander Grafb9939332016-03-10 00:27:20 +0100373
Simon Glassc7ae3df2016-11-07 08:47:08 -0700374 addr = simple_strtoul(saddr, NULL, 16);
375
376 if (argc > 2) {
377 sfdt = argv[2];
378 fdt_addr = simple_strtoul(sfdt, NULL, 16);
379 }
Alexander Graf1c398092016-04-14 16:07:53 +0200380 }
381
Simon Glass5ee31ba2016-11-07 08:47:05 -0700382 printf("## Starting EFI application at %08lx ...\n", addr);
Rob Clark95c55532017-09-13 18:05:33 -0400383 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
384 bootefi_device_path, bootefi_image_path);
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200385 printf("## Application terminated, r = %lu\n",
386 r & ~EFI_ERROR_MASK);
Alexander Grafb9939332016-03-10 00:27:20 +0100387
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200388 if (r != EFI_SUCCESS)
389 return 1;
390 else
391 return 0;
Alexander Grafb9939332016-03-10 00:27:20 +0100392}
393
394#ifdef CONFIG_SYS_LONGHELP
395static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200396 "<image address> [fdt address]\n"
397 " - boot EFI payload stored at address <image address>.\n"
398 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700399 " exposed as EFI configuration table.\n"
400#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200401 "bootefi hello\n"
402 " - boot a sample Hello World application stored within U-Boot\n"
403#endif
404#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
405 "bootefi selftest\n"
406 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200407 " Use environment variable efi_selftest to select a single test.\n"
408 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700409#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400410 "bootmgr [fdt addr]\n"
411 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
412 "\n"
413 " If specified, the device tree located at <fdt address> gets\n"
414 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100415#endif
416
417U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200418 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700419 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100420 bootefi_help_text
421);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100422
Rob Clark95c55532017-09-13 18:05:33 -0400423static int parse_partnum(const char *devnr)
424{
425 const char *str = strchr(devnr, ':');
426 if (str) {
427 str++;
428 return simple_strtoul(str, NULL, 16);
429 }
430 return 0;
431}
432
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200433void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100434{
Rob Clark95c55532017-09-13 18:05:33 -0400435 char filename[32] = { 0 }; /* dp->str is u16[32] long */
436 char *s;
Alexander Graf0f4060e2016-03-04 01:10:14 +0100437
Rob Clark95c55532017-09-13 18:05:33 -0400438 if (strcmp(dev, "Net")) {
439 struct blk_desc *desc;
440 int part;
441
442 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
Stefan Roese8300be62017-11-17 08:47:09 +0100443 if (!desc)
444 return;
Rob Clark95c55532017-09-13 18:05:33 -0400445 part = parse_partnum(devnr);
446
447 bootefi_device_path = efi_dp_from_part(desc, part);
448 } else {
449#ifdef CONFIG_NET
450 bootefi_device_path = efi_dp_from_eth();
Alexander Graff9d334b2016-08-05 14:49:53 +0200451#endif
Alexander Graff9d334b2016-08-05 14:49:53 +0200452 }
453
Rob Clark9975fe92017-09-13 18:05:38 -0400454 if (!path)
455 return;
456
Alexander Graf49271662016-07-21 01:44:46 +0200457 if (strcmp(dev, "Net")) {
458 /* Add leading / to fs paths, because they're absolute */
Rob Clark95c55532017-09-13 18:05:33 -0400459 snprintf(filename, sizeof(filename), "/%s", path);
Alexander Graf49271662016-07-21 01:44:46 +0200460 } else {
Rob Clark95c55532017-09-13 18:05:33 -0400461 snprintf(filename, sizeof(filename), "%s", path);
Alexander Graf49271662016-07-21 01:44:46 +0200462 }
Rob Clark3e433e92017-07-24 07:59:09 -0400463 /* DOS style file path: */
Rob Clark95c55532017-09-13 18:05:33 -0400464 s = filename;
Rob Clark3e433e92017-07-24 07:59:09 -0400465 while ((s = strchr(s, '/')))
466 *s++ = '\\';
Rob Clark95c55532017-09-13 18:05:33 -0400467 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100468}