1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application loader
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7 
8 #define LOG_CATEGORY LOGC_EFI
9 
10 #include <command.h>
11 #include <efi.h>
12 #include <efi_device_path.h>
13 #include <efi_loader.h>
14 #include <exports.h>
15 #include <log.h>
16 #include <malloc.h>
17 #include <mapmem.h>
18 #include <vsprintf.h>
19 #include <asm-generic/sections.h>
20 #include <asm/global_data.h>
21 #include <linux/string.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 static struct efi_device_path *test_image_path;
26 static struct efi_device_path *test_device_path;
27 
bootefi_run_prepare(const char * load_options_path,struct efi_device_path * device_path,struct efi_device_path * image_path,struct efi_loaded_image_obj ** image_objp,struct efi_loaded_image ** loaded_image_infop)28 static efi_status_t bootefi_run_prepare(const char *load_options_path,
29 		struct efi_device_path *device_path,
30 		struct efi_device_path *image_path,
31 		struct efi_loaded_image_obj **image_objp,
32 		struct efi_loaded_image **loaded_image_infop)
33 {
34 	efi_status_t ret;
35 	u16 *load_options;
36 
37 	ret = efi_setup_loaded_image(device_path, image_path, image_objp,
38 				     loaded_image_infop);
39 	if (ret != EFI_SUCCESS)
40 		return ret;
41 
42 	(*image_objp)->auth_status = EFI_IMAGE_AUTH_PASSED;
43 	(*image_objp)->entry = efi_selftest;
44 
45 	/* Transfer environment variable as load options */
46 	return efi_env_set_load_options((efi_handle_t)*image_objp,
47 					load_options_path,
48 					&load_options);
49 }
50 
51 /**
52  * bootefi_test_prepare() - prepare to run an EFI test
53  *
54  * Prepare to run a test as if it were provided by a loaded image.
55  *
56  * @image_objp:		pointer to be set to the loaded image handle
57  * @loaded_image_infop:	pointer to be set to the loaded image protocol
58  * @path:		dummy file path used to construct the device path
59  *			set in the loaded image protocol
60  * @load_options_path:	name of a U-Boot environment variable. Its value is
61  *			set as load options in the loaded image protocol.
62  * Return:		status code
63  */
bootefi_test_prepare(struct efi_loaded_image_obj ** image_objp,struct efi_loaded_image ** loaded_image_infop,const char * path,const char * load_options_path)64 static efi_status_t bootefi_test_prepare
65 		(struct efi_loaded_image_obj **image_objp,
66 		 struct efi_loaded_image **loaded_image_infop, const char *path,
67 		 const char *load_options_path)
68 {
69 	efi_status_t ret;
70 
71 	/* Construct a dummy device path */
72 	test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
73 	if (!test_device_path)
74 		return EFI_OUT_OF_RESOURCES;
75 
76 	test_image_path = efi_dp_from_file(NULL, path);
77 	if (!test_image_path) {
78 		ret = EFI_OUT_OF_RESOURCES;
79 		goto failure;
80 	}
81 
82 	ret = bootefi_run_prepare(load_options_path, test_device_path,
83 				  test_image_path, image_objp,
84 				  loaded_image_infop);
85 	if (ret == EFI_SUCCESS)
86 		return ret;
87 
88 failure:
89 	efi_free_pool(test_device_path);
90 	efi_free_pool(test_image_path);
91 	/* TODO: not sure calling clear function is necessary */
92 	efi_clear_bootdev();
93 	return ret;
94 }
95 
96 /**
97  * do_efi_selftest() - execute EFI selftest
98  *
99  * Return:	status code
100  */
do_efi_selftest(void)101 static int do_efi_selftest(void)
102 {
103 	struct efi_loaded_image_obj *image_obj;
104 	struct efi_loaded_image *loaded_image_info;
105 	efi_status_t ret;
106 
107 	ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
108 				   "\\selftest", "efi_selftest");
109 	if (ret != EFI_SUCCESS)
110 		return CMD_RET_FAILURE;
111 
112 	/* Execute the test */
113 	ret = do_bootefi_exec(&image_obj->header,
114 			      loaded_image_info->load_options);
115 	efi_free_pool(test_device_path);
116 	efi_free_pool(test_image_path);
117 	if (ret != EFI_SUCCESS)
118 		efi_delete_handle(&image_obj->header);
119 	else
120 		ret = efi_delete_handle(&image_obj->header);
121 
122 	return ret != EFI_SUCCESS;
123 }
124 
125 /**
126  * do_bootefi() - execute `bootefi` command
127  *
128  * @cmdtp:	table entry describing command
129  * @flag:	bitmap indicating how the command was invoked
130  * @argc:	number of arguments
131  * @argv:	command line arguments
132  * Return:	status code
133  */
do_bootefi(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])134 static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
135 		      char *const argv[])
136 {
137 	efi_status_t ret;
138 	char *p;
139 	void *fdt, *initrd = NULL, *image_buf;
140 	unsigned long addr, size, rd_len = 0, fdt_addr = 0;
141 	void *image_addr;
142 	size_t image_size;
143 	int fdt_arg = 2;
144 
145 	if (argc < 2)
146 		return CMD_RET_USAGE;
147 
148 	if (argc > 2) {
149 		ulong rd_addr = 0;
150 		char *end = strchr(argv[2], ':');
151 
152 		if (end) {
153 			rd_addr = hextoul(argv[2], NULL);
154 			if (!rd_addr)
155 				return CMD_RET_USAGE;
156 
157 			rd_len = hextoul(++end, NULL);
158 			initrd = map_sysmem(rd_addr, rd_len);
159 			++fdt_arg;
160 		}
161 	}
162 
163 	if (argc > fdt_arg + 1)
164 		return CMD_RET_USAGE;
165 	if (argc == fdt_arg + 1)
166 		fdt_addr = hextoul(argv[fdt_arg], NULL);
167 
168 	if (fdt_addr)
169 		fdt = map_sysmem(fdt_addr, 0);
170 	else
171 		fdt = EFI_FDT_USE_INTERNAL;
172 
173 	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
174 	    !strcmp(argv[1], "bootmgr")) {
175 		ret = efi_bootmgr_run(fdt);
176 
177 		if (ret != EFI_SUCCESS)
178 			return CMD_RET_FAILURE;
179 
180 		return CMD_RET_SUCCESS;
181 	}
182 
183 	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_SELFTEST) &&
184 	    !strcmp(argv[1], "selftest")) {
185 		/* Initialize EFI drivers */
186 		ret = efi_init_obj_list();
187 		if (ret != EFI_SUCCESS) {
188 			log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
189 				ret & ~EFI_ERROR_MASK);
190 			return CMD_RET_FAILURE;
191 		}
192 
193 		ret = efi_install_fdt(fdt);
194 		if (ret != EFI_SUCCESS)
195 			return CMD_RET_FAILURE;
196 
197 		return do_efi_selftest();
198 	}
199 
200 	if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY))
201 		return CMD_RET_SUCCESS;
202 
203 	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) &&
204 	    !strcmp(argv[1], "hello")) {
205 		image_buf = __efi_helloworld_begin;
206 		size = __efi_helloworld_end - __efi_helloworld_begin;
207 		/* TODO: not sure calling clear function is necessary */
208 		efi_clear_bootdev();
209 	} else {
210 		addr = strtoul(argv[1], NULL, 16);
211 		/* Check that a numeric value was passed */
212 		if (!addr)
213 			return CMD_RET_USAGE;
214 		image_buf = map_sysmem(addr, 0);
215 
216 		p  = strchr(argv[1], ':');
217 		if (p) {
218 			size = strtoul(++p, NULL, 16);
219 			if (!size)
220 				return CMD_RET_USAGE;
221 			efi_clear_bootdev();
222 		} else {
223 			/* Image should be already loaded */
224 			efi_get_image_parameters(&image_addr, &image_size);
225 
226 			if (image_buf != image_addr) {
227 				log_err("No UEFI binary known at %s\n",
228 					argv[1]);
229 				return CMD_RET_FAILURE;
230 			}
231 			size = image_size;
232 		}
233 	}
234 
235 	ret = efi_binary_run(image_buf, size, fdt, initrd, rd_len);
236 
237 	if (ret != EFI_SUCCESS)
238 		return CMD_RET_FAILURE;
239 
240 	return CMD_RET_SUCCESS;
241 }
242 
243 U_BOOT_LONGHELP(bootefi,
244 	"<image address>[:<size>] [<initrd address>:<size>] [<fdt address>]\n"
245 	"  - boot EFI payload\n"
246 #ifdef CONFIG_CMD_BOOTEFI_HELLO
247 	"bootefi hello\n"
248 	"  - boot a sample Hello World application stored within U-Boot\n"
249 #endif
250 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
251 	"bootefi selftest [fdt address]\n"
252 	"  - boot an EFI selftest application stored within U-Boot\n"
253 	"    Use environment variable efi_selftest to select a single test.\n"
254 	"    Use 'setenv efi_selftest list' to enumerate all tests.\n"
255 #endif
256 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
257 	"bootefi bootmgr [fdt address]\n"
258 	"  - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
259 	"\n"
260 	"    If specified, the device tree located at <fdt address> gets\n"
261 	"    exposed as EFI configuration table.\n"
262 #endif
263 	);
264 
265 U_BOOT_CMD(
266 	bootefi, 4, 0, do_bootefi,
267 	"Boots an EFI payload from memory",
268 	bootefi_help_text
269 );
270