1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Bootmethod for distro boot via EFI
4  *
5  * Copyright 2021 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #define LOG_CATEGORY UCLASS_BOOTSTD
10 
11 #include <common.h>
12 #include <bootdev.h>
13 #include <bootflow.h>
14 #include <bootmeth.h>
15 #include <command.h>
16 #include <dm.h>
17 #include <efi_loader.h>
18 #include <fs.h>
19 #include <malloc.h>
20 #include <mapmem.h>
21 #include <mmc.h>
22 #include <net.h>
23 #include <pxe_utils.h>
24 
25 #define EFI_DIRNAME	"efi/boot/"
26 
27 /**
28  * get_efi_leafname() - Get the leaf name for the EFI file we expect
29  *
30  * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi".
31  *	Must have at least 16 bytes of space
32  * @max_len: Length of @str, must be >=16
33  */
get_efi_leafname(char * str,int max_len)34 static int get_efi_leafname(char *str, int max_len)
35 {
36 	const char *base;
37 
38 	if (max_len < 16)
39 		return log_msg_ret("spc", -ENOSPC);
40 	if (IS_ENABLED(CONFIG_ARM64))
41 		base = "bootaa64";
42 	else if (IS_ENABLED(CONFIG_ARM))
43 		base = "bootarm";
44 	else if (IS_ENABLED(CONFIG_X86_RUN_32BIT))
45 		base = "bootia32";
46 	else if (IS_ENABLED(CONFIG_X86_RUN_64BIT))
47 		base = "bootx64";
48 	else if (IS_ENABLED(CONFIG_ARCH_RV32I))
49 		base = "bootriscv32";
50 	else if (IS_ENABLED(CONFIG_ARCH_RV64I))
51 		base = "bootriscv64";
52 	else if (IS_ENABLED(CONFIG_SANDBOX))
53 		base = "bootsbox";
54 	else
55 		return -EINVAL;
56 
57 	strcpy(str, base);
58 	strcat(str, ".efi");
59 
60 	return 0;
61 }
62 
get_efi_pxe_arch(void)63 static int get_efi_pxe_arch(void)
64 {
65 	/* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
66 	if (IS_ENABLED(CONFIG_ARM64))
67 		return 0xb;
68 	else if (IS_ENABLED(CONFIG_ARM))
69 		return 0xa;
70 	else if (IS_ENABLED(CONFIG_X86_64))
71 		return 0x6;
72 	else if (IS_ENABLED(CONFIG_X86))
73 		return 0x7;
74 	else if (IS_ENABLED(CONFIG_ARCH_RV32I))
75 		return 0x19;
76 	else if (IS_ENABLED(CONFIG_ARCH_RV64I))
77 		return 0x1b;
78 	else if (IS_ENABLED(CONFIG_SANDBOX))
79 		return 0;	/* not used */
80 
81 	return -EINVAL;
82 }
83 
get_efi_pxe_vci(char * str,int max_len)84 static int get_efi_pxe_vci(char *str, int max_len)
85 {
86 	int ret;
87 
88 	ret = get_efi_pxe_arch();
89 	if (ret < 0)
90 		return ret;
91 
92 	snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
93 
94 	return 0;
95 }
96 
set_efi_bootdev(struct blk_desc * desc,struct bootflow * bflow)97 static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
98 {
99 	const struct udevice *media_dev;
100 	int size = bflow->size;
101 	const char *dev_name;
102 	char devnum_str[9];
103 	char dirname[200];
104 	char *last_slash;
105 
106 	/*
107 	 * This is a horrible hack to tell EFI about this boot device. Once we
108 	 * unify EFI with the rest of U-Boot we can clean this up. The same hack
109 	 * exists in multiple places, e.g. in the fs, tftp and load commands.
110 	 *
111 	 * Once we can clean up the EFI code to make proper use of driver model,
112 	 * this can go away.
113 	 */
114 	media_dev = dev_get_parent(bflow->dev);
115 	snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
116 		 desc ? desc->devnum : dev_seq(media_dev),
117 		 bflow->part);
118 
119 	strlcpy(dirname, bflow->fname, sizeof(dirname));
120 	last_slash = strrchr(dirname, '/');
121 	if (last_slash)
122 		*last_slash = '\0';
123 
124 	log_debug("setting bootdev %s, %s, %s, %p, %x\n",
125 		  dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
126 		  bflow->buf, size);
127 	dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
128 		 "usb" : dev_get_uclass_name(media_dev);
129 	efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
130 }
131 
efiload_read_file(struct blk_desc * desc,struct bootflow * bflow)132 static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow)
133 {
134 	int ret;
135 
136 	ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000);
137 	if (ret)
138 		return log_msg_ret("read", ret);
139 
140 	return 0;
141 }
142 
distro_efi_check(struct udevice * dev,struct bootflow_iter * iter)143 static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
144 {
145 	/* This only works on block and network devices */
146 	if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
147 		return log_msg_ret("blk", -ENOTSUPP);
148 
149 	/* This works on block devices and network devices */
150 	if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
151 		return log_msg_ret("pxe", -ENOTSUPP);
152 
153 	return 0;
154 }
155 
156 /**
157  * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
158  *
159  * @fname: Place to put filename
160  * @size: Max size of filename
161  * @seq: Sequence number, to cycle through options (0=first)
162  * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
163  * -EINVAL if there are no more options, -EALREADY if the control FDT should be
164  * used
165  */
distro_efi_get_fdt_name(char * fname,int size,int seq)166 static int distro_efi_get_fdt_name(char *fname, int size, int seq)
167 {
168 	const char *fdt_fname;
169 	const char *prefix;
170 
171 	/* select the prefix */
172 	switch (seq) {
173 	case 0:
174 		/* this is the default */
175 		prefix = "/dtb";
176 		break;
177 	case 1:
178 		prefix = "";
179 		break;
180 	case 2:
181 		prefix = "/dtb/current";
182 		break;
183 	default:
184 		return log_msg_ret("pref", -EINVAL);
185 	}
186 
187 	fdt_fname = env_get("fdtfile");
188 	if (fdt_fname) {
189 		snprintf(fname, size, "%s/%s", prefix, fdt_fname);
190 		log_debug("Using device tree: %s\n", fname);
191 	} else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
192 		strcpy(fname, "<prior>");
193 		return log_msg_ret("pref", -EALREADY);
194 	/* Use this fallback only for 32-bit ARM */
195 	} else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
196 		const char *soc = env_get("soc");
197 		const char *board = env_get("board");
198 		const char *boardver = env_get("boardver");
199 
200 		/* cf the code in label_boot() which seems very complex */
201 		snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
202 			 soc ? soc : "", soc ? "-" : "", board ? board : "",
203 			 boardver ? boardver : "");
204 		log_debug("Using default device tree: %s\n", fname);
205 	} else {
206 		return log_msg_ret("env", -ENOENT);
207 	}
208 
209 	return 0;
210 }
211 
distro_efi_read_bootflow_file(struct udevice * dev,struct bootflow * bflow)212 static int distro_efi_read_bootflow_file(struct udevice *dev,
213 					 struct bootflow *bflow)
214 {
215 	struct blk_desc *desc = NULL;
216 	ulong fdt_addr, size;
217 	char fname[256];
218 	int ret, seq;
219 
220 	/* We require a partition table */
221 	if (!bflow->part)
222 		return -ENOENT;
223 
224 	strcpy(fname, EFI_DIRNAME);
225 	ret = get_efi_leafname(fname + strlen(fname),
226 			       sizeof(fname) - strlen(fname));
227 	if (ret)
228 		return log_msg_ret("leaf", ret);
229 
230 	if (bflow->blk)
231 		 desc = dev_get_uclass_plat(bflow->blk);
232 	ret = bootmeth_try_file(bflow, desc, NULL, fname);
233 	if (ret)
234 		return log_msg_ret("try", ret);
235 
236 	ret = efiload_read_file(desc, bflow);
237 	if (ret)
238 		return log_msg_ret("read", ret);
239 
240 	fdt_addr = env_get_hex("fdt_addr_r", 0);
241 
242 	/* try the various available names */
243 	ret = -ENOENT;
244 	*fname = '\0';
245 	for (seq = 0; ret == -ENOENT; seq++) {
246 		ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
247 		if (ret == -EALREADY)
248 			bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
249 		if (!ret)
250 			ret = bootmeth_common_read_file(dev, bflow, fname,
251 							fdt_addr, &size);
252 	}
253 
254 	if (*fname) {
255 		bflow->fdt_fname = strdup(fname);
256 		if (!bflow->fdt_fname)
257 			return log_msg_ret("fil", -ENOMEM);
258 	}
259 
260 	if (!ret) {
261 		bflow->fdt_size = size;
262 		bflow->fdt_addr = fdt_addr;
263 
264 		/*
265 		 * TODO: Apply extension overlay
266 		 *
267 		 * Here we need to load and apply the extension overlay. This is
268 		 * not implemented. See do_extension_apply(). The extension
269 		 * stuff needs an implementation in boot/extension.c so it is
270 		 * separate from the command code. Really the extension stuff
271 		 * should use the device tree and a uclass / driver interface
272 		 * rather than implementing its own list
273 		 */
274 	} else {
275 		log_debug("No device tree available\n");
276 	}
277 
278 	return 0;
279 }
280 
distro_efi_read_bootflow_net(struct bootflow * bflow)281 static int distro_efi_read_bootflow_net(struct bootflow *bflow)
282 {
283 	char file_addr[17], fname[256];
284 	char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
285 	struct cmd_tbl cmdtp = {};	/* dummy */
286 	const char *addr_str, *fdt_addr_str;
287 	int ret, arch, size;
288 	ulong addr, fdt_addr;
289 	char str[36];
290 
291 	ret = get_efi_pxe_vci(str, sizeof(str));
292 	if (ret)
293 		return log_msg_ret("vci", ret);
294 	ret = get_efi_pxe_arch();
295 	if (ret < 0)
296 		return log_msg_ret("arc", ret);
297 	arch = ret;
298 
299 	ret = env_set("bootp_vci", str);
300 	if (ret)
301 		return log_msg_ret("vcs", ret);
302 	ret = env_set_ulong("bootp_arch", arch);
303 	if (ret)
304 		return log_msg_ret("ars", ret);
305 
306 	/* figure out the load address */
307 	addr_str = env_get("kernel_addr_r");
308 	addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
309 
310 	/* clear any previous bootfile */
311 	env_set("bootfile", NULL);
312 
313 	/* read the kernel */
314 	ret = dhcp_run(addr, NULL, true);
315 	if (ret)
316 		return log_msg_ret("dhc", ret);
317 
318 	size = env_get_hex("filesize", -1);
319 	if (size <= 0)
320 		return log_msg_ret("sz", -EINVAL);
321 	bflow->size = size;
322 
323 	/* do the hideous EFI hack */
324 	efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
325 			bflow->size);
326 
327 	/* read the DT file also */
328 	fdt_addr_str = env_get("fdt_addr_r");
329 	if (!fdt_addr_str)
330 		return log_msg_ret("fdt", -EINVAL);
331 	fdt_addr = hextoul(fdt_addr_str, NULL);
332 	sprintf(file_addr, "%lx", fdt_addr);
333 
334 	/* We only allow the first prefix with PXE */
335 	ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
336 	if (ret)
337 		return log_msg_ret("nam", ret);
338 
339 	bflow->fdt_fname = strdup(fname);
340 	if (!bflow->fdt_fname)
341 		return log_msg_ret("fil", -ENOMEM);
342 
343 	if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
344 		bflow->fdt_size = env_get_hex("filesize", 0);
345 		bflow->fdt_addr = fdt_addr;
346 	} else {
347 		log_debug("No device tree available\n");
348 	}
349 
350 	bflow->state = BOOTFLOWST_READY;
351 
352 	return 0;
353 }
354 
distro_efi_read_bootflow(struct udevice * dev,struct bootflow * bflow)355 static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
356 {
357 	const struct udevice *media = dev_get_parent(bflow->dev);
358 	int ret;
359 
360 	if (IS_ENABLED(CONFIG_CMD_DHCP) &&
361 	    device_get_uclass_id(media) == UCLASS_ETH) {
362 		/* we only support reading from one device, so ignore 'dev' */
363 		ret = distro_efi_read_bootflow_net(bflow);
364 		if (ret)
365 			return log_msg_ret("net", ret);
366 	} else {
367 		ret = distro_efi_read_bootflow_file(dev, bflow);
368 		if (ret)
369 			return log_msg_ret("blk", ret);
370 	}
371 
372 	return 0;
373 }
374 
distro_efi_boot(struct udevice * dev,struct bootflow * bflow)375 int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
376 {
377 	ulong kernel, fdt;
378 	char cmd[50];
379 
380 	/* A non-zero buffer indicates the kernel is there */
381 	if (bflow->buf) {
382 		/* Set the EFI bootdev again, since reading an FDT loses it! */
383 		if (bflow->blk) {
384 			struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
385 
386 			set_efi_bootdev(desc, bflow);
387 		}
388 
389 		kernel = (ulong)map_to_sysmem(bflow->buf);
390 
391 		/*
392 		 * use the provided device tree if available, else fall back to
393 		 * the control FDT
394 		 */
395 		if (bflow->fdt_fname)
396 			fdt = bflow->fdt_addr;
397 		else
398 			fdt = (ulong)map_to_sysmem(gd->fdt_blob);
399 	} else {
400 		/*
401 		 * This doesn't actually work for network devices:
402 		 *
403 		 * do_bootefi_image() No UEFI binary known at 0x02080000
404 		 *
405 		 * But this is the same behaviour for distro boot, so it can be
406 		 * fixed here.
407 		 */
408 		kernel = env_get_hex("kernel_addr_r", 0);
409 		fdt = env_get_hex("fdt_addr_r", 0);
410 	}
411 
412 	/*
413 	 * At some point we can add a real interface to bootefi so we can call
414 	 * this directly. For now, go through the CLI, like distro boot.
415 	 */
416 	snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
417 	if (run_command(cmd, 0))
418 		return log_msg_ret("run", -EINVAL);
419 
420 	return 0;
421 }
422 
distro_bootmeth_efi_bind(struct udevice * dev)423 static int distro_bootmeth_efi_bind(struct udevice *dev)
424 {
425 	struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
426 
427 	plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
428 		"EFI boot from an .efi file" : "EFI";
429 
430 	return 0;
431 }
432 
433 static struct bootmeth_ops distro_efi_bootmeth_ops = {
434 	.check		= distro_efi_check,
435 	.read_bootflow	= distro_efi_read_bootflow,
436 	.read_file	= bootmeth_common_read_file,
437 	.boot		= distro_efi_boot,
438 };
439 
440 static const struct udevice_id distro_efi_bootmeth_ids[] = {
441 	{ .compatible = "u-boot,distro-efi" },
442 	{ }
443 };
444 
445 U_BOOT_DRIVER(bootmeth_efi) = {
446 	.name		= "bootmeth_efi",
447 	.id		= UCLASS_BOOTMETH,
448 	.of_match	= distro_efi_bootmeth_ids,
449 	.ops		= &distro_efi_bootmeth_ops,
450 	.bind		= distro_bootmeth_efi_bind,
451 };
452