xref: /openbmc/u-boot/cmd/bootefi.c (revision 51c533fd)
1b9939336SAlexander Graf /*
2b9939336SAlexander Graf  *  EFI application loader
3b9939336SAlexander Graf  *
4b9939336SAlexander Graf  *  Copyright (c) 2016 Alexander Graf
5b9939336SAlexander Graf  *
6b9939336SAlexander Graf  *  SPDX-License-Identifier:     GPL-2.0+
7b9939336SAlexander Graf  */
8b9939336SAlexander Graf 
9b9939336SAlexander Graf #include <common.h>
10b9939336SAlexander Graf #include <command.h>
119d922450SSimon Glass #include <dm.h>
12b9939336SAlexander Graf #include <efi_loader.h>
13b9939336SAlexander Graf #include <errno.h>
14b9939336SAlexander Graf #include <libfdt.h>
15b9939336SAlexander Graf #include <libfdt_env.h>
16ad0c1a3dSAlexander Graf #include <memalign.h>
170d9d501fSAlexander Graf #include <asm/global_data.h>
18e275458cSSimon Glass #include <asm-generic/sections.h>
19e275458cSSimon Glass #include <linux/linkage.h>
200d9d501fSAlexander Graf 
210d9d501fSAlexander Graf DECLARE_GLOBAL_DATA_PTR;
22b9939336SAlexander Graf 
237cbc1241SHeinrich Schuchardt static uint8_t efi_obj_list_initalized;
247cbc1241SHeinrich Schuchardt 
25b9939336SAlexander Graf /*
26b9939336SAlexander Graf  * When booting using the "bootefi" command, we don't know which
27b9939336SAlexander Graf  * physical device the file came from. So we create a pseudo-device
28b9939336SAlexander Graf  * called "bootefi" with the device path /bootefi.
29b9939336SAlexander Graf  *
30b9939336SAlexander Graf  * In addition to the originating device we also declare the file path
31b9939336SAlexander Graf  * of "bootefi" based loads to be /bootefi.
32b9939336SAlexander Graf  */
330f4060ebSAlexander Graf static struct efi_device_path_file_path bootefi_image_path[] = {
34b9939336SAlexander Graf 	{
35b9939336SAlexander Graf 		.dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
36b9939336SAlexander Graf 		.dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
370f4060ebSAlexander Graf 		.dp.length = sizeof(bootefi_image_path[0]),
38b9939336SAlexander Graf 		.str = { 'b','o','o','t','e','f','i' },
39b9939336SAlexander Graf 	}, {
40b9939336SAlexander Graf 		.dp.type = DEVICE_PATH_TYPE_END,
41b9939336SAlexander Graf 		.dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
420f4060ebSAlexander Graf 		.dp.length = sizeof(bootefi_image_path[0]),
43b9939336SAlexander Graf 	}
44b9939336SAlexander Graf };
45b9939336SAlexander Graf 
46c07ad7c0SAlexander Graf static struct efi_device_path_file_path bootefi_device_path[] = {
47c07ad7c0SAlexander Graf 	{
48c07ad7c0SAlexander Graf 		.dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
49c07ad7c0SAlexander Graf 		.dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
50c07ad7c0SAlexander Graf 		.dp.length = sizeof(bootefi_image_path[0]),
51c07ad7c0SAlexander Graf 		.str = { 'b','o','o','t','e','f','i' },
52c07ad7c0SAlexander Graf 	}, {
53c07ad7c0SAlexander Graf 		.dp.type = DEVICE_PATH_TYPE_END,
54c07ad7c0SAlexander Graf 		.dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
55c07ad7c0SAlexander Graf 		.dp.length = sizeof(bootefi_image_path[0]),
56c07ad7c0SAlexander Graf 	}
57c07ad7c0SAlexander Graf };
58c07ad7c0SAlexander Graf 
59b9939336SAlexander Graf /* The EFI loaded_image interface for the image executed via "bootefi" */
60b9939336SAlexander Graf static struct efi_loaded_image loaded_image_info = {
61c07ad7c0SAlexander Graf 	.device_handle = bootefi_device_path,
620f4060ebSAlexander Graf 	.file_path = bootefi_image_path,
63b9939336SAlexander Graf };
64b9939336SAlexander Graf 
65b9939336SAlexander Graf /* The EFI object struct for the image executed via "bootefi" */
66b9939336SAlexander Graf static struct efi_object loaded_image_info_obj = {
67b9939336SAlexander Graf 	.handle = &loaded_image_info,
68b9939336SAlexander Graf 	.protocols = {
69b9939336SAlexander Graf 		{
70b9939336SAlexander Graf 			/*
71b9939336SAlexander Graf 			 * When asking for the loaded_image interface, just
72b9939336SAlexander Graf 			 * return handle which points to loaded_image_info
73b9939336SAlexander Graf 			 */
74b9939336SAlexander Graf 			.guid = &efi_guid_loaded_image,
75b5349f74Sxypron.glpk@gmx.de 			.protocol_interface = &loaded_image_info,
76b9939336SAlexander Graf 		},
77b9939336SAlexander Graf 		{
78b9939336SAlexander Graf 			/*
79b9939336SAlexander Graf 			 * When asking for the device path interface, return
80c07ad7c0SAlexander Graf 			 * bootefi_device_path
81b9939336SAlexander Graf 			 */
82b9939336SAlexander Graf 			.guid = &efi_guid_device_path,
83b5349f74Sxypron.glpk@gmx.de 			.protocol_interface = bootefi_device_path,
84b9939336SAlexander Graf 		},
8588adae5eSxypron.glpk@gmx.de 		{
8688adae5eSxypron.glpk@gmx.de 			.guid = &efi_guid_console_control,
8788adae5eSxypron.glpk@gmx.de 			.protocol_interface = (void *) &efi_console_control
8888adae5eSxypron.glpk@gmx.de 		},
89cc5b7081Sxypron.glpk@gmx.de 		{
90cc5b7081Sxypron.glpk@gmx.de 			.guid = &efi_guid_device_path_to_text_protocol,
91cc5b7081Sxypron.glpk@gmx.de 			.protocol_interface = (void *) &efi_device_path_to_text
92cc5b7081Sxypron.glpk@gmx.de 		},
93b9939336SAlexander Graf 	},
94b9939336SAlexander Graf };
95b9939336SAlexander Graf 
96b9939336SAlexander Graf /* The EFI object struct for the device the "bootefi" image was loaded from */
97b9939336SAlexander Graf static struct efi_object bootefi_device_obj = {
98c07ad7c0SAlexander Graf 	.handle = bootefi_device_path,
99b9939336SAlexander Graf 	.protocols = {
100b9939336SAlexander Graf 		{
101b9939336SAlexander Graf 			/* When asking for the device path interface, return
102c07ad7c0SAlexander Graf 			 * bootefi_device_path */
103b9939336SAlexander Graf 			.guid = &efi_guid_device_path,
104b5349f74Sxypron.glpk@gmx.de 			.protocol_interface = bootefi_device_path
105b9939336SAlexander Graf 		}
106b9939336SAlexander Graf 	},
107b9939336SAlexander Graf };
108b9939336SAlexander Graf 
1097cbc1241SHeinrich Schuchardt /* Initialize and populate EFI object list */
1107cbc1241SHeinrich Schuchardt static void efi_init_obj_list(void)
1117cbc1241SHeinrich Schuchardt {
1127cbc1241SHeinrich Schuchardt 	efi_obj_list_initalized = 1;
1137cbc1241SHeinrich Schuchardt 
1147cbc1241SHeinrich Schuchardt 	list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
1157cbc1241SHeinrich Schuchardt 	list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
1167cbc1241SHeinrich Schuchardt 	efi_console_register();
1177cbc1241SHeinrich Schuchardt #ifdef CONFIG_PARTITIONS
1187cbc1241SHeinrich Schuchardt 	efi_disk_register();
1197cbc1241SHeinrich Schuchardt #endif
1207cbc1241SHeinrich Schuchardt #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
1217cbc1241SHeinrich Schuchardt 	efi_gop_register();
1227cbc1241SHeinrich Schuchardt #endif
1237cbc1241SHeinrich Schuchardt #ifdef CONFIG_NET
1247cbc1241SHeinrich Schuchardt 	void *nethandle = loaded_image_info.device_handle;
1257cbc1241SHeinrich Schuchardt 	efi_net_register(&nethandle);
1267cbc1241SHeinrich Schuchardt 
1277cbc1241SHeinrich Schuchardt 	if (!memcmp(bootefi_device_path[0].str, "N\0e\0t", 6))
1287cbc1241SHeinrich Schuchardt 		loaded_image_info.device_handle = nethandle;
1297cbc1241SHeinrich Schuchardt 	else
1307cbc1241SHeinrich Schuchardt 		loaded_image_info.device_handle = bootefi_device_path;
1317cbc1241SHeinrich Schuchardt #endif
1327cbc1241SHeinrich Schuchardt #ifdef CONFIG_GENERATE_SMBIOS_TABLE
1337cbc1241SHeinrich Schuchardt 	efi_smbios_register();
1347cbc1241SHeinrich Schuchardt #endif
1357cbc1241SHeinrich Schuchardt 
1367cbc1241SHeinrich Schuchardt 	/* Initialize EFI runtime services */
1377cbc1241SHeinrich Schuchardt 	efi_reset_system_init();
1387cbc1241SHeinrich Schuchardt 	efi_get_time_init();
1397cbc1241SHeinrich Schuchardt }
1407cbc1241SHeinrich Schuchardt 
1410d9d501fSAlexander Graf static void *copy_fdt(void *fdt)
1420d9d501fSAlexander Graf {
1430d9d501fSAlexander Graf 	u64 fdt_size = fdt_totalsize(fdt);
144ad0c1a3dSAlexander Graf 	unsigned long fdt_ram_start = -1L, fdt_pages;
145ad0c1a3dSAlexander Graf 	u64 new_fdt_addr;
1460d9d501fSAlexander Graf 	void *new_fdt;
147ad0c1a3dSAlexander Graf 	int i;
1480d9d501fSAlexander Graf 
149ad0c1a3dSAlexander Graf         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
150ad0c1a3dSAlexander Graf                 u64 ram_start = gd->bd->bi_dram[i].start;
151ad0c1a3dSAlexander Graf                 u64 ram_size = gd->bd->bi_dram[i].size;
1520d9d501fSAlexander Graf 
153ad0c1a3dSAlexander Graf 		if (!ram_size)
154ad0c1a3dSAlexander Graf 			continue;
155ad0c1a3dSAlexander Graf 
156ad0c1a3dSAlexander Graf 		if (ram_start < fdt_ram_start)
157ad0c1a3dSAlexander Graf 			fdt_ram_start = ram_start;
158ad0c1a3dSAlexander Graf 	}
159ad0c1a3dSAlexander Graf 
160ad0c1a3dSAlexander Graf 	/* Give us at least 4kb breathing room */
161a44bffccSxypron.glpk@gmx.de 	fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
162ad0c1a3dSAlexander Graf 	fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
163ad0c1a3dSAlexander Graf 
164ad0c1a3dSAlexander Graf 	/* Safe fdt location is at 128MB */
165ad0c1a3dSAlexander Graf 	new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
166ad0c1a3dSAlexander Graf 	if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
167ad0c1a3dSAlexander Graf 			       &new_fdt_addr) != EFI_SUCCESS) {
168ad0c1a3dSAlexander Graf 		/* If we can't put it there, put it somewhere */
169a44bffccSxypron.glpk@gmx.de 		new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
17085a6e9b3SAlexander Graf 		if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
17185a6e9b3SAlexander Graf 				       &new_fdt_addr) != EFI_SUCCESS) {
17285a6e9b3SAlexander Graf 			printf("ERROR: Failed to reserve space for FDT\n");
17385a6e9b3SAlexander Graf 			return NULL;
174ad0c1a3dSAlexander Graf 		}
17585a6e9b3SAlexander Graf 	}
17685a6e9b3SAlexander Graf 
177ad0c1a3dSAlexander Graf 	new_fdt = (void*)(ulong)new_fdt_addr;
1780d9d501fSAlexander Graf 	memcpy(new_fdt, fdt, fdt_totalsize(fdt));
1790d9d501fSAlexander Graf 	fdt_set_totalsize(new_fdt, fdt_size);
1800d9d501fSAlexander Graf 
1810d9d501fSAlexander Graf 	return new_fdt;
1820d9d501fSAlexander Graf }
1830d9d501fSAlexander Graf 
184b06d8ac3Sxypron.glpk@gmx.de static ulong efi_do_enter(void *image_handle,
185b06d8ac3Sxypron.glpk@gmx.de 			  struct efi_system_table *st,
186b06d8ac3Sxypron.glpk@gmx.de 			  asmlinkage ulong (*entry)(void *image_handle,
187b06d8ac3Sxypron.glpk@gmx.de 				struct efi_system_table *st))
188b06d8ac3Sxypron.glpk@gmx.de {
189b06d8ac3Sxypron.glpk@gmx.de 	efi_status_t ret = EFI_LOAD_ERROR;
190b06d8ac3Sxypron.glpk@gmx.de 
191b06d8ac3Sxypron.glpk@gmx.de 	if (entry)
192b06d8ac3Sxypron.glpk@gmx.de 		ret = entry(image_handle, st);
193b06d8ac3Sxypron.glpk@gmx.de 	st->boottime->exit(image_handle, ret, 0, NULL);
194b06d8ac3Sxypron.glpk@gmx.de 	return ret;
195b06d8ac3Sxypron.glpk@gmx.de }
196b06d8ac3Sxypron.glpk@gmx.de 
197ec6617c3SAlison Wang #ifdef CONFIG_ARM64
198b06d8ac3Sxypron.glpk@gmx.de static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)(
199b06d8ac3Sxypron.glpk@gmx.de 			void *image_handle, struct efi_system_table *st),
200b06d8ac3Sxypron.glpk@gmx.de 			void *image_handle, struct efi_system_table *st)
201ec6617c3SAlison Wang {
202ec6617c3SAlison Wang 	/* Enable caches again */
203ec6617c3SAlison Wang 	dcache_enable();
204ec6617c3SAlison Wang 
205b06d8ac3Sxypron.glpk@gmx.de 	return efi_do_enter(image_handle, st, entry);
206ec6617c3SAlison Wang }
207ec6617c3SAlison Wang #endif
208ec6617c3SAlison Wang 
209b9939336SAlexander Graf /*
210b9939336SAlexander Graf  * Load an EFI payload into a newly allocated piece of memory, register all
211b9939336SAlexander Graf  * EFI objects it would want to access and jump to it.
212b9939336SAlexander Graf  */
2131c39809bSAlexander Graf static unsigned long do_bootefi_exec(void *efi, void *fdt)
214b9939336SAlexander Graf {
215e275458cSSimon Glass 	ulong (*entry)(void *image_handle, struct efi_system_table *st)
216e275458cSSimon Glass 		asmlinkage;
217b9939336SAlexander Graf 	ulong fdt_pages, fdt_size, fdt_start, fdt_end;
218f4f9993fSAlexander Graf 	const efi_guid_t fdt_guid = EFI_FDT_GUID;
219dea2174dSAlexander Graf 	bootm_headers_t img = { 0 };
220b9939336SAlexander Graf 
221b9939336SAlexander Graf 	/*
222b9939336SAlexander Graf 	 * gd lives in a fixed register which may get clobbered while we execute
223b9939336SAlexander Graf 	 * the payload. So save it here and restore it on every callback entry
224b9939336SAlexander Graf 	 */
225b9939336SAlexander Graf 	efi_save_gd();
226b9939336SAlexander Graf 
2271c39809bSAlexander Graf 	if (fdt && !fdt_check_header(fdt)) {
228dea2174dSAlexander Graf 		/* Prepare fdt for payload */
2290d9d501fSAlexander Graf 		fdt = copy_fdt(fdt);
2300d9d501fSAlexander Graf 
2310d9d501fSAlexander Graf 		if (image_setup_libfdt(&img, fdt, 0, NULL)) {
232dea2174dSAlexander Graf 			printf("ERROR: Failed to process device tree\n");
233dea2174dSAlexander Graf 			return -EINVAL;
234dea2174dSAlexander Graf 		}
235dea2174dSAlexander Graf 
236dea2174dSAlexander Graf 		/* Link to it in the efi tables */
237f4f9993fSAlexander Graf 		efi_install_configuration_table(&fdt_guid, fdt);
238b9939336SAlexander Graf 
239b9939336SAlexander Graf 		/* And reserve the space in the memory map */
2400d9d501fSAlexander Graf 		fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
2410d9d501fSAlexander Graf 		fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
242b9939336SAlexander Graf 		fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
243b9939336SAlexander Graf 		fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
244b9939336SAlexander Graf 		/* Give a bootloader the chance to modify the device tree */
245b9939336SAlexander Graf 		fdt_pages += 2;
246b9939336SAlexander Graf 		efi_add_memory_map(fdt_start, fdt_pages,
247b9939336SAlexander Graf 				   EFI_BOOT_SERVICES_DATA, true);
248b9939336SAlexander Graf 	} else {
2491c39809bSAlexander Graf 		printf("WARNING: Invalid device tree, expect boot to fail\n");
250f4f9993fSAlexander Graf 		efi_install_configuration_table(&fdt_guid, NULL);
251b9939336SAlexander Graf 	}
252b9939336SAlexander Graf 
253b9939336SAlexander Graf 	/* Load the EFI payload */
254b9939336SAlexander Graf 	entry = efi_load_pe(efi, &loaded_image_info);
255b9939336SAlexander Graf 	if (!entry)
256b9939336SAlexander Graf 		return -ENOENT;
257b9939336SAlexander Graf 
258b9939336SAlexander Graf 	/* Initialize and populate EFI object list */
2597cbc1241SHeinrich Schuchardt 	if (!efi_obj_list_initalized)
2607cbc1241SHeinrich Schuchardt 		efi_init_obj_list();
26180a4800eSAlexander Graf 
262b9939336SAlexander Graf 	/* Call our payload! */
263edcef3baSAlexander Graf 	debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
264a86aeaf2SAlexander Graf 
265a86aeaf2SAlexander Graf 	if (setjmp(&loaded_image_info.exit_jmp)) {
2661da1bac4Sxypron.glpk@gmx.de 		return loaded_image_info.exit_status;
267a86aeaf2SAlexander Graf 	}
268a86aeaf2SAlexander Graf 
26969bd459dSAlexander Graf #ifdef CONFIG_ARM64
27069bd459dSAlexander Graf 	/* On AArch64 we need to make sure we call our payload in < EL3 */
27169bd459dSAlexander Graf 	if (current_el() == 3) {
27269bd459dSAlexander Graf 		smp_kick_all_cpus();
27369bd459dSAlexander Graf 		dcache_disable();	/* flush cache before switch to EL2 */
274ec6617c3SAlison Wang 
275ec6617c3SAlison Wang 		/* Move into EL2 and keep running there */
276ec6617c3SAlison Wang 		armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
2777c5e1febSAlison Wang 				    (ulong)&systab, 0, (ulong)efi_run_in_el2,
278ec6617c3SAlison Wang 				    ES_TO_AARCH64);
279ec6617c3SAlison Wang 
280ec6617c3SAlison Wang 		/* Should never reach here, efi exits with longjmp */
281ec6617c3SAlison Wang 		while (1) { }
28269bd459dSAlexander Graf 	}
28369bd459dSAlexander Graf #endif
28469bd459dSAlexander Graf 
285b06d8ac3Sxypron.glpk@gmx.de 	return efi_do_enter(&loaded_image_info, &systab, entry);
286b9939336SAlexander Graf }
287b9939336SAlexander Graf 
288b9939336SAlexander Graf 
289b9939336SAlexander Graf /* Interpreter command to boot an arbitrary EFI image from memory */
290b9939336SAlexander Graf static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
291b9939336SAlexander Graf {
2921c39809bSAlexander Graf 	char *saddr, *sfdt;
2931c39809bSAlexander Graf 	unsigned long addr, fdt_addr = 0;
2941da1bac4Sxypron.glpk@gmx.de 	unsigned long r;
295b9939336SAlexander Graf 
296b9939336SAlexander Graf 	if (argc < 2)
2973c1dcef6SBin Meng 		return CMD_RET_USAGE;
298c7ae3dfdSSimon Glass #ifdef CONFIG_CMD_BOOTEFI_HELLO
299c7ae3dfdSSimon Glass 	if (!strcmp(argv[1], "hello")) {
300c7ae3dfdSSimon Glass 		ulong size = __efi_hello_world_end - __efi_hello_world_begin;
301c7ae3dfdSSimon Glass 
302*51c533fdSHeinrich Schuchardt 		saddr = env_get("loadaddr");
303*51c533fdSHeinrich Schuchardt 		if (saddr)
304*51c533fdSHeinrich Schuchardt 			addr = simple_strtoul(saddr, NULL, 16);
305*51c533fdSHeinrich Schuchardt 		else
306c7ae3dfdSSimon Glass 			addr = CONFIG_SYS_LOAD_ADDR;
307c7ae3dfdSSimon Glass 		memcpy((char *)addr, __efi_hello_world_begin, size);
308c7ae3dfdSSimon Glass 	} else
309c7ae3dfdSSimon Glass #endif
310c7ae3dfdSSimon Glass 	{
311b9939336SAlexander Graf 		saddr = argv[1];
312b9939336SAlexander Graf 
313b9939336SAlexander Graf 		addr = simple_strtoul(saddr, NULL, 16);
314b9939336SAlexander Graf 
3151c39809bSAlexander Graf 		if (argc > 2) {
3161c39809bSAlexander Graf 			sfdt = argv[2];
3171c39809bSAlexander Graf 			fdt_addr = simple_strtoul(sfdt, NULL, 16);
3181c39809bSAlexander Graf 		}
319c7ae3dfdSSimon Glass 	}
3201c39809bSAlexander Graf 
3215ee31bafSSimon Glass 	printf("## Starting EFI application at %08lx ...\n", addr);
3221c39809bSAlexander Graf 	r = do_bootefi_exec((void *)addr, (void*)fdt_addr);
3231da1bac4Sxypron.glpk@gmx.de 	printf("## Application terminated, r = %lu\n",
3241da1bac4Sxypron.glpk@gmx.de 	       r & ~EFI_ERROR_MASK);
325b9939336SAlexander Graf 
3261da1bac4Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
3271da1bac4Sxypron.glpk@gmx.de 		return 1;
3281da1bac4Sxypron.glpk@gmx.de 	else
3291da1bac4Sxypron.glpk@gmx.de 		return 0;
330b9939336SAlexander Graf }
331b9939336SAlexander Graf 
332b9939336SAlexander Graf #ifdef CONFIG_SYS_LONGHELP
333b9939336SAlexander Graf static char bootefi_help_text[] =
3341c39809bSAlexander Graf 	"<image address> [fdt address]\n"
3351c39809bSAlexander Graf 	"  - boot EFI payload stored at address <image address>.\n"
3361c39809bSAlexander Graf 	"    If specified, the device tree located at <fdt address> gets\n"
337c7ae3dfdSSimon Glass 	"    exposed as EFI configuration table.\n"
338c7ae3dfdSSimon Glass #ifdef CONFIG_CMD_BOOTEFI_HELLO
339c7ae3dfdSSimon Glass 	"hello\n"
340c7ae3dfdSSimon Glass 	"  - boot a sample Hello World application stored within U-Boot"
341c7ae3dfdSSimon Glass #endif
342c7ae3dfdSSimon Glass 	;
343b9939336SAlexander Graf #endif
344b9939336SAlexander Graf 
345b9939336SAlexander Graf U_BOOT_CMD(
3461c39809bSAlexander Graf 	bootefi, 3, 0, do_bootefi,
34792dfd922SSergey Kubushyn 	"Boots an EFI payload from memory",
348b9939336SAlexander Graf 	bootefi_help_text
349b9939336SAlexander Graf );
3500f4060ebSAlexander Graf 
351c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
3520f4060ebSAlexander Graf {
3538c3df0bfSAlexander Graf 	__maybe_unused struct blk_desc *desc;
354ecbe1a07SAlexander Graf 	char devname[32] = { 0 }; /* dp->str is u16[32] long */
3553e433e96SRob Clark 	char *colon, *s;
3560f4060ebSAlexander Graf 
3571acc0087SPatrick Delaunay #if defined(CONFIG_BLK) || CONFIG_IS_ENABLED(ISO_PARTITION)
358f9d334bdSAlexander Graf 	desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
359f9d334bdSAlexander Graf #endif
360f9d334bdSAlexander Graf 
361f9d334bdSAlexander Graf #ifdef CONFIG_BLK
362f9d334bdSAlexander Graf 	if (desc) {
363f9d334bdSAlexander Graf 		snprintf(devname, sizeof(devname), "%s", desc->bdev->name);
364f9d334bdSAlexander Graf 	} else
365f9d334bdSAlexander Graf #endif
366f9d334bdSAlexander Graf 
367f9d334bdSAlexander Graf 	{
3680f4060ebSAlexander Graf 		/* Assemble the condensed device name we use in efi_disk.c */
3690f4060ebSAlexander Graf 		snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
370f9d334bdSAlexander Graf 	}
371f9d334bdSAlexander Graf 
3720f4060ebSAlexander Graf 	colon = strchr(devname, ':');
3738c3df0bfSAlexander Graf 
3741acc0087SPatrick Delaunay #if CONFIG_IS_ENABLED(ISO_PARTITION)
3758c3df0bfSAlexander Graf 	/* For ISOs we create partition block devices */
3768c3df0bfSAlexander Graf 	if (desc && (desc->type != DEV_TYPE_UNKNOWN) &&
3778c3df0bfSAlexander Graf 	    (desc->part_type == PART_TYPE_ISO)) {
3788c3df0bfSAlexander Graf 		if (!colon)
379f9d334bdSAlexander Graf 			snprintf(devname, sizeof(devname), "%s:1", devname);
380f9d334bdSAlexander Graf 
3818c3df0bfSAlexander Graf 		colon = NULL;
3828c3df0bfSAlexander Graf 	}
3838c3df0bfSAlexander Graf #endif
3848c3df0bfSAlexander Graf 
3850f4060ebSAlexander Graf 	if (colon)
3860f4060ebSAlexander Graf 		*colon = '\0';
3870f4060ebSAlexander Graf 
388c07ad7c0SAlexander Graf 	/* Patch bootefi_device_path to the target device */
389c07ad7c0SAlexander Graf 	memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str));
390c07ad7c0SAlexander Graf 	ascii2unicode(bootefi_device_path[0].str, devname);
391c07ad7c0SAlexander Graf 
392c07ad7c0SAlexander Graf 	/* Patch bootefi_image_path to the target file path */
3930f4060ebSAlexander Graf 	memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
39449271666SAlexander Graf 	if (strcmp(dev, "Net")) {
39549271666SAlexander Graf 		/* Add leading / to fs paths, because they're absolute */
39649271666SAlexander Graf 		snprintf(devname, sizeof(devname), "/%s", path);
39749271666SAlexander Graf 	} else {
398c07ad7c0SAlexander Graf 		snprintf(devname, sizeof(devname), "%s", path);
39949271666SAlexander Graf 	}
4003e433e96SRob Clark 	/* DOS style file path: */
4013e433e96SRob Clark 	s = devname;
4023e433e96SRob Clark 	while ((s = strchr(s, '/')))
4033e433e96SRob Clark 		*s++ = '\\';
4040f4060ebSAlexander Graf 	ascii2unicode(bootefi_image_path[0].str, devname);
4050f4060ebSAlexander Graf }
406