blob: 94c3a18bfc646a4cc82d3da25f41031201e47a1f [file] [log] [blame]
Simon Glassacfa9bd2022-04-24 23:31:17 -06001// 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>
Simon Glass3e188602023-01-17 10:47:55 -070022#include <net.h>
Simon Glassacfa9bd2022-04-24 23:31:17 -060023#include <pxe_utils.h>
Simon Glass2984d212023-07-26 21:01:24 -060024#include <linux/sizes.h>
Simon Glassacfa9bd2022-04-24 23:31:17 -060025
26#define EFI_DIRNAME "efi/boot/"
27
28/**
29 * get_efi_leafname() - Get the leaf name for the EFI file we expect
30 *
31 * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi".
32 * Must have at least 16 bytes of space
33 * @max_len: Length of @str, must be >=16
34 */
35static int get_efi_leafname(char *str, int max_len)
36{
37 const char *base;
38
39 if (max_len < 16)
40 return log_msg_ret("spc", -ENOSPC);
41 if (IS_ENABLED(CONFIG_ARM64))
42 base = "bootaa64";
43 else if (IS_ENABLED(CONFIG_ARM))
44 base = "bootarm";
45 else if (IS_ENABLED(CONFIG_X86_RUN_32BIT))
46 base = "bootia32";
47 else if (IS_ENABLED(CONFIG_X86_RUN_64BIT))
48 base = "bootx64";
49 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
50 base = "bootriscv32";
51 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
52 base = "bootriscv64";
53 else if (IS_ENABLED(CONFIG_SANDBOX))
54 base = "bootsbox";
55 else
56 return -EINVAL;
57
58 strcpy(str, base);
59 strcat(str, ".efi");
60
61 return 0;
62}
63
Simon Glass3e188602023-01-17 10:47:55 -070064static int get_efi_pxe_arch(void)
65{
66 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
67 if (IS_ENABLED(CONFIG_ARM64))
68 return 0xb;
69 else if (IS_ENABLED(CONFIG_ARM))
70 return 0xa;
71 else if (IS_ENABLED(CONFIG_X86_64))
72 return 0x6;
73 else if (IS_ENABLED(CONFIG_X86))
74 return 0x7;
75 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
76 return 0x19;
77 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
78 return 0x1b;
79 else if (IS_ENABLED(CONFIG_SANDBOX))
80 return 0; /* not used */
81
82 return -EINVAL;
83}
84
85static int get_efi_pxe_vci(char *str, int max_len)
86{
87 int ret;
88
89 ret = get_efi_pxe_arch();
90 if (ret < 0)
91 return ret;
92
93 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
94
95 return 0;
96}
97
Simon Glass146242c2023-07-26 21:01:22 -060098/**
99 * bootmeth_uses_network() - check if the media device is Ethernet
100 *
101 * @bflow: Bootflow to check
102 * Returns: true if the media device is Ethernet, else false
103 */
104static bool bootmeth_uses_network(struct bootflow *bflow)
105{
106 const struct udevice *media = dev_get_parent(bflow->dev);
107
108 return IS_ENABLED(CONFIG_CMD_DHCP) &&
109 device_get_uclass_id(media) == UCLASS_ETH;
110}
111
Simon Glassd80bb4f2023-04-24 13:49:46 +1200112static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600113{
114 const struct udevice *media_dev;
115 int size = bflow->size;
116 const char *dev_name;
117 char devnum_str[9];
118 char dirname[200];
119 char *last_slash;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600120
121 /*
122 * This is a horrible hack to tell EFI about this boot device. Once we
123 * unify EFI with the rest of U-Boot we can clean this up. The same hack
124 * exists in multiple places, e.g. in the fs, tftp and load commands.
125 *
126 * Once we can clean up the EFI code to make proper use of driver model,
127 * this can go away.
128 */
129 media_dev = dev_get_parent(bflow->dev);
Mathew McBride484e7012023-04-24 13:49:50 +1200130 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
131 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassd80bb4f2023-04-24 13:49:46 +1200132 bflow->part);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600133
134 strlcpy(dirname, bflow->fname, sizeof(dirname));
135 last_slash = strrchr(dirname, '/');
136 if (last_slash)
137 *last_slash = '\0';
138
139 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
140 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
141 bflow->buf, size);
142 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
143 "usb" : dev_get_uclass_name(media_dev);
144 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassd80bb4f2023-04-24 13:49:46 +1200145}
146
Simon Glass6a8c2f92023-07-26 21:01:23 -0600147static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassd80bb4f2023-04-24 13:49:46 +1200148{
Simon Glass6a8c2f92023-07-26 21:01:23 -0600149 struct blk_desc *desc = NULL;
150 loff_t bytes_read;
Simon Glassd80bb4f2023-04-24 13:49:46 +1200151 int ret;
152
Simon Glass6a8c2f92023-07-26 21:01:23 -0600153 if (bflow->blk)
154 desc = dev_get_uclass_plat(bflow->blk);
155 ret = bootmeth_setup_fs(bflow, desc);
156 if (ret)
157 return log_msg_ret("set", ret);
158
159 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
Simon Glassd80bb4f2023-04-24 13:49:46 +1200160 if (ret)
161 return log_msg_ret("read", ret);
Simon Glass6a8c2f92023-07-26 21:01:23 -0600162 bflow->buf = map_sysmem(addr, bflow->size);
163
164 set_efi_bootdev(desc, bflow);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600165
166 return 0;
167}
168
169static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
170{
Simon Glass3e188602023-01-17 10:47:55 -0700171 /* This only works on block and network devices */
172 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
173 return log_msg_ret("blk", -ENOTSUPP);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600174
Simon Glassd9f48572023-01-17 10:48:05 -0700175 /* This works on block devices and network devices */
176 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
177 return log_msg_ret("pxe", -ENOTSUPP);
178
Simon Glassacfa9bd2022-04-24 23:31:17 -0600179 return 0;
180}
181
Simon Glass47dd6b42023-02-22 12:17:04 -0700182/**
183 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
184 *
185 * @fname: Place to put filename
186 * @size: Max size of filename
187 * @seq: Sequence number, to cycle through options (0=first)
188 * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
189 * -EINVAL if there are no more options, -EALREADY if the control FDT should be
190 * used
191 */
192static int distro_efi_get_fdt_name(char *fname, int size, int seq)
Simon Glasse890e8c2023-01-17 10:47:57 -0700193{
194 const char *fdt_fname;
Simon Glass47dd6b42023-02-22 12:17:04 -0700195 const char *prefix;
196
197 /* select the prefix */
198 switch (seq) {
199 case 0:
200 /* this is the default */
201 prefix = "/dtb";
202 break;
203 case 1:
204 prefix = "";
205 break;
206 case 2:
207 prefix = "/dtb/current";
208 break;
Caleb Connolly6f386d02023-10-17 11:55:36 +0100209 case 3:
210 prefix = "/dtbs";
211 break;
Simon Glass47dd6b42023-02-22 12:17:04 -0700212 default:
213 return log_msg_ret("pref", -EINVAL);
214 }
Simon Glasse890e8c2023-01-17 10:47:57 -0700215
216 fdt_fname = env_get("fdtfile");
217 if (fdt_fname) {
Simon Glass47dd6b42023-02-22 12:17:04 -0700218 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
Simon Glasse890e8c2023-01-17 10:47:57 -0700219 log_debug("Using device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700220 } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
221 strcpy(fname, "<prior>");
222 return log_msg_ret("pref", -EALREADY);
223 /* Use this fallback only for 32-bit ARM */
224 } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
Simon Glasse890e8c2023-01-17 10:47:57 -0700225 const char *soc = env_get("soc");
226 const char *board = env_get("board");
227 const char *boardver = env_get("boardver");
228
229 /* cf the code in label_boot() which seems very complex */
Simon Glass47dd6b42023-02-22 12:17:04 -0700230 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
Simon Glasse890e8c2023-01-17 10:47:57 -0700231 soc ? soc : "", soc ? "-" : "", board ? board : "",
232 boardver ? boardver : "");
233 log_debug("Using default device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700234 } else {
235 return log_msg_ret("env", -ENOENT);
Simon Glasse890e8c2023-01-17 10:47:57 -0700236 }
Simon Glass47dd6b42023-02-22 12:17:04 -0700237
238 return 0;
Simon Glasse890e8c2023-01-17 10:47:57 -0700239}
240
Simon Glass6a8c2f92023-07-26 21:01:23 -0600241/*
242 * distro_efi_try_bootflow_files() - Check that files are present
243 *
244 * This reads any FDT file and checks whether the bootflow file is present, for
245 * later reading. We avoid reading the bootflow now, since it is likely large,
246 * it may take a long time and we want to avoid needing to allocate memory for
247 * it
248 *
249 * @dev: bootmeth device to use
250 * @bflow: bootflow to update
251 */
252static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass3e188602023-01-17 10:47:55 -0700253 struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600254{
255 struct blk_desc *desc = NULL;
Simon Glasse890e8c2023-01-17 10:47:57 -0700256 ulong fdt_addr, size;
Simon Glass3e188602023-01-17 10:47:55 -0700257 char fname[256];
Simon Glass47dd6b42023-02-22 12:17:04 -0700258 int ret, seq;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600259
260 /* We require a partition table */
261 if (!bflow->part)
262 return -ENOENT;
263
264 strcpy(fname, EFI_DIRNAME);
265 ret = get_efi_leafname(fname + strlen(fname),
266 sizeof(fname) - strlen(fname));
267 if (ret)
268 return log_msg_ret("leaf", ret);
269
270 if (bflow->blk)
271 desc = dev_get_uclass_plat(bflow->blk);
272 ret = bootmeth_try_file(bflow, desc, NULL, fname);
273 if (ret)
274 return log_msg_ret("try", ret);
275
Simon Glass6a8c2f92023-07-26 21:01:23 -0600276 /* Since we can access the file, let's call it ready */
277 bflow->state = BOOTFLOWST_READY;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600278
Simon Glass47dd6b42023-02-22 12:17:04 -0700279 fdt_addr = env_get_hex("fdt_addr_r", 0);
280
281 /* try the various available names */
282 ret = -ENOENT;
Simon Glass9f835162023-04-24 13:49:44 +1200283 *fname = '\0';
284 for (seq = 0; ret == -ENOENT; seq++) {
Simon Glass47dd6b42023-02-22 12:17:04 -0700285 ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
Simon Glass9f835162023-04-24 13:49:44 +1200286 if (ret == -EALREADY)
Simon Glass47dd6b42023-02-22 12:17:04 -0700287 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass2984d212023-07-26 21:01:24 -0600288 if (!ret) {
289 /* Limit FDT files to 4MB */
290 size = SZ_4M;
Simon Glass9f835162023-04-24 13:49:44 +1200291 ret = bootmeth_common_read_file(dev, bflow, fname,
292 fdt_addr, &size);
Simon Glass2984d212023-07-26 21:01:24 -0600293 }
Simon Glass47dd6b42023-02-22 12:17:04 -0700294 }
295
Simon Glass9f835162023-04-24 13:49:44 +1200296 if (*fname) {
297 bflow->fdt_fname = strdup(fname);
298 if (!bflow->fdt_fname)
299 return log_msg_ret("fil", -ENOMEM);
300 }
Simon Glasse890e8c2023-01-17 10:47:57 -0700301
Simon Glasse890e8c2023-01-17 10:47:57 -0700302 if (!ret) {
303 bflow->fdt_size = size;
304 bflow->fdt_addr = fdt_addr;
305
306 /*
307 * TODO: Apply extension overlay
308 *
309 * Here we need to load and apply the extension overlay. This is
310 * not implemented. See do_extension_apply(). The extension
311 * stuff needs an implementation in boot/extension.c so it is
312 * separate from the command code. Really the extension stuff
313 * should use the device tree and a uclass / driver interface
314 * rather than implementing its own list
315 */
316 } else {
317 log_debug("No device tree available\n");
318 }
319
Simon Glassacfa9bd2022-04-24 23:31:17 -0600320 return 0;
321}
322
Simon Glass3e188602023-01-17 10:47:55 -0700323static int distro_efi_read_bootflow_net(struct bootflow *bflow)
324{
Simon Glasse890e8c2023-01-17 10:47:57 -0700325 char file_addr[17], fname[256];
326 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
327 struct cmd_tbl cmdtp = {}; /* dummy */
328 const char *addr_str, *fdt_addr_str;
Simon Glass3e188602023-01-17 10:47:55 -0700329 int ret, arch, size;
Simon Glasse890e8c2023-01-17 10:47:57 -0700330 ulong addr, fdt_addr;
Simon Glass3e188602023-01-17 10:47:55 -0700331 char str[36];
Simon Glass3e188602023-01-17 10:47:55 -0700332
333 ret = get_efi_pxe_vci(str, sizeof(str));
334 if (ret)
335 return log_msg_ret("vci", ret);
336 ret = get_efi_pxe_arch();
337 if (ret < 0)
338 return log_msg_ret("arc", ret);
339 arch = ret;
340
341 ret = env_set("bootp_vci", str);
342 if (ret)
343 return log_msg_ret("vcs", ret);
344 ret = env_set_ulong("bootp_arch", arch);
345 if (ret)
346 return log_msg_ret("ars", ret);
347
348 /* figure out the load address */
349 addr_str = env_get("kernel_addr_r");
350 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
351
352 /* clear any previous bootfile */
353 env_set("bootfile", NULL);
354
355 /* read the kernel */
356 ret = dhcp_run(addr, NULL, true);
357 if (ret)
358 return log_msg_ret("dhc", ret);
359
360 size = env_get_hex("filesize", -1);
361 if (size <= 0)
362 return log_msg_ret("sz", -EINVAL);
363 bflow->size = size;
364
365 /* do the hideous EFI hack */
366 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
367 bflow->size);
368
Simon Glasse890e8c2023-01-17 10:47:57 -0700369 /* read the DT file also */
370 fdt_addr_str = env_get("fdt_addr_r");
371 if (!fdt_addr_str)
372 return log_msg_ret("fdt", -EINVAL);
373 fdt_addr = hextoul(fdt_addr_str, NULL);
374 sprintf(file_addr, "%lx", fdt_addr);
375
Simon Glass47dd6b42023-02-22 12:17:04 -0700376 /* We only allow the first prefix with PXE */
377 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
378 if (ret)
379 return log_msg_ret("nam", ret);
380
Simon Glasse890e8c2023-01-17 10:47:57 -0700381 bflow->fdt_fname = strdup(fname);
382 if (!bflow->fdt_fname)
383 return log_msg_ret("fil", -ENOMEM);
384
385 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
386 bflow->fdt_size = env_get_hex("filesize", 0);
387 bflow->fdt_addr = fdt_addr;
388 } else {
389 log_debug("No device tree available\n");
390 }
391
Simon Glass3e188602023-01-17 10:47:55 -0700392 bflow->state = BOOTFLOWST_READY;
393
394 return 0;
395}
396
397static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
398{
Simon Glass3e188602023-01-17 10:47:55 -0700399 int ret;
400
Simon Glass146242c2023-07-26 21:01:22 -0600401 if (bootmeth_uses_network(bflow)) {
Simon Glass3e188602023-01-17 10:47:55 -0700402 /* we only support reading from one device, so ignore 'dev' */
403 ret = distro_efi_read_bootflow_net(bflow);
404 if (ret)
405 return log_msg_ret("net", ret);
406 } else {
Simon Glass6a8c2f92023-07-26 21:01:23 -0600407 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass3e188602023-01-17 10:47:55 -0700408 if (ret)
409 return log_msg_ret("blk", ret);
410 }
411
412 return 0;
413}
414
Bin Meng60727032023-08-03 17:30:05 +0800415static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600416{
Simon Glasse890e8c2023-01-17 10:47:57 -0700417 ulong kernel, fdt;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600418 char cmd[50];
Simon Glass6a8c2f92023-07-26 21:01:23 -0600419 int ret;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600420
Simon Glass6a8c2f92023-07-26 21:01:23 -0600421 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass146242c2023-07-26 21:01:22 -0600422 if (!bootmeth_uses_network(bflow)) {
Simon Glass6a8c2f92023-07-26 21:01:23 -0600423 ret = efiload_read_file(bflow, kernel);
424 if (ret)
425 return log_msg_ret("read", ret);
Simon Glasse890e8c2023-01-17 10:47:57 -0700426
427 /*
428 * use the provided device tree if available, else fall back to
429 * the control FDT
430 */
431 if (bflow->fdt_fname)
432 fdt = bflow->fdt_addr;
433 else
434 fdt = (ulong)map_to_sysmem(gd->fdt_blob);
435 } else {
436 /*
437 * This doesn't actually work for network devices:
438 *
439 * do_bootefi_image() No UEFI binary known at 0x02080000
440 *
441 * But this is the same behaviour for distro boot, so it can be
442 * fixed here.
443 */
Simon Glasse890e8c2023-01-17 10:47:57 -0700444 fdt = env_get_hex("fdt_addr_r", 0);
445 }
446
Simon Glassacfa9bd2022-04-24 23:31:17 -0600447 /*
448 * At some point we can add a real interface to bootefi so we can call
Simon Glasse890e8c2023-01-17 10:47:57 -0700449 * this directly. For now, go through the CLI, like distro boot.
Simon Glassacfa9bd2022-04-24 23:31:17 -0600450 */
Simon Glasse890e8c2023-01-17 10:47:57 -0700451 snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600452 if (run_command(cmd, 0))
453 return log_msg_ret("run", -EINVAL);
454
455 return 0;
456}
457
458static int distro_bootmeth_efi_bind(struct udevice *dev)
459{
460 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
461
462 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
463 "EFI boot from an .efi file" : "EFI";
464
465 return 0;
466}
467
468static struct bootmeth_ops distro_efi_bootmeth_ops = {
469 .check = distro_efi_check,
470 .read_bootflow = distro_efi_read_bootflow,
471 .read_file = bootmeth_common_read_file,
472 .boot = distro_efi_boot,
473};
474
475static const struct udevice_id distro_efi_bootmeth_ids[] = {
476 { .compatible = "u-boot,distro-efi" },
477 { }
478};
479
480U_BOOT_DRIVER(bootmeth_efi) = {
481 .name = "bootmeth_efi",
482 .id = UCLASS_BOOTMETH,
483 .of_match = distro_efi_bootmeth_ids,
484 .ops = &distro_efi_bootmeth_ops,
485 .bind = distro_bootmeth_efi_bind,
486};