blob: aebc5207fc01b043b2379da8c574b79cc4e690b2 [file] [log] [blame]
Simon Glass66f62552022-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>
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020017#include <efi_default_filename.h>
Simon Glass66f62552022-04-24 23:31:17 -060018#include <efi_loader.h>
19#include <fs.h>
20#include <malloc.h>
21#include <mapmem.h>
22#include <mmc.h>
Simon Glass9e08caa2023-01-17 10:47:55 -070023#include <net.h>
Simon Glass66f62552022-04-24 23:31:17 -060024#include <pxe_utils.h>
Simon Glass02fafec2023-07-26 21:01:24 -060025#include <linux/sizes.h>
Simon Glass66f62552022-04-24 23:31:17 -060026
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020027#define EFI_DIRNAME "/EFI/BOOT/"
Simon Glass66f62552022-04-24 23:31:17 -060028
Simon Glass9e08caa2023-01-17 10:47:55 -070029static int get_efi_pxe_arch(void)
30{
31 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
32 if (IS_ENABLED(CONFIG_ARM64))
33 return 0xb;
34 else if (IS_ENABLED(CONFIG_ARM))
35 return 0xa;
36 else if (IS_ENABLED(CONFIG_X86_64))
37 return 0x6;
38 else if (IS_ENABLED(CONFIG_X86))
39 return 0x7;
40 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
41 return 0x19;
42 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
43 return 0x1b;
44 else if (IS_ENABLED(CONFIG_SANDBOX))
45 return 0; /* not used */
46
47 return -EINVAL;
48}
49
50static int get_efi_pxe_vci(char *str, int max_len)
51{
52 int ret;
53
54 ret = get_efi_pxe_arch();
55 if (ret < 0)
56 return ret;
57
58 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
59
60 return 0;
61}
62
Simon Glass47fc7342023-07-26 21:01:22 -060063/**
64 * bootmeth_uses_network() - check if the media device is Ethernet
65 *
66 * @bflow: Bootflow to check
67 * Returns: true if the media device is Ethernet, else false
68 */
69static bool bootmeth_uses_network(struct bootflow *bflow)
70{
71 const struct udevice *media = dev_get_parent(bflow->dev);
72
73 return IS_ENABLED(CONFIG_CMD_DHCP) &&
74 device_get_uclass_id(media) == UCLASS_ETH;
75}
76
Simon Glassacad3622023-04-24 13:49:46 +120077static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -060078{
79 const struct udevice *media_dev;
80 int size = bflow->size;
81 const char *dev_name;
82 char devnum_str[9];
83 char dirname[200];
84 char *last_slash;
Simon Glass66f62552022-04-24 23:31:17 -060085
86 /*
87 * This is a horrible hack to tell EFI about this boot device. Once we
88 * unify EFI with the rest of U-Boot we can clean this up. The same hack
89 * exists in multiple places, e.g. in the fs, tftp and load commands.
90 *
91 * Once we can clean up the EFI code to make proper use of driver model,
92 * this can go away.
93 */
94 media_dev = dev_get_parent(bflow->dev);
Mathew McBride63c5e5c2023-04-24 13:49:50 +120095 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
96 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassacad3622023-04-24 13:49:46 +120097 bflow->part);
Simon Glass66f62552022-04-24 23:31:17 -060098
99 strlcpy(dirname, bflow->fname, sizeof(dirname));
100 last_slash = strrchr(dirname, '/');
101 if (last_slash)
102 *last_slash = '\0';
103
104 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
105 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
106 bflow->buf, size);
107 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
108 "usb" : dev_get_uclass_name(media_dev);
109 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassacad3622023-04-24 13:49:46 +1200110}
111
Simon Glass4aec1902023-07-26 21:01:23 -0600112static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassacad3622023-04-24 13:49:46 +1200113{
Simon Glass4aec1902023-07-26 21:01:23 -0600114 struct blk_desc *desc = NULL;
115 loff_t bytes_read;
Simon Glassacad3622023-04-24 13:49:46 +1200116 int ret;
117
Simon Glass4aec1902023-07-26 21:01:23 -0600118 if (bflow->blk)
119 desc = dev_get_uclass_plat(bflow->blk);
120 ret = bootmeth_setup_fs(bflow, desc);
121 if (ret)
122 return log_msg_ret("set", ret);
123
124 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
Simon Glassacad3622023-04-24 13:49:46 +1200125 if (ret)
126 return log_msg_ret("read", ret);
Simon Glass4aec1902023-07-26 21:01:23 -0600127 bflow->buf = map_sysmem(addr, bflow->size);
128
129 set_efi_bootdev(desc, bflow);
Simon Glass66f62552022-04-24 23:31:17 -0600130
131 return 0;
132}
133
134static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
135{
Simon Glass9e08caa2023-01-17 10:47:55 -0700136 /* This only works on block and network devices */
137 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
138 return log_msg_ret("blk", -ENOTSUPP);
Simon Glass66f62552022-04-24 23:31:17 -0600139
Simon Glasse22fe922023-01-17 10:48:05 -0700140 /* This works on block devices and network devices */
141 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
142 return log_msg_ret("pxe", -ENOTSUPP);
143
Simon Glass66f62552022-04-24 23:31:17 -0600144 return 0;
145}
146
Simon Glassca84dc82023-02-22 12:17:04 -0700147/**
148 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
149 *
150 * @fname: Place to put filename
151 * @size: Max size of filename
152 * @seq: Sequence number, to cycle through options (0=first)
153 * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
154 * -EINVAL if there are no more options, -EALREADY if the control FDT should be
155 * used
156 */
157static int distro_efi_get_fdt_name(char *fname, int size, int seq)
Simon Glass56bb4e42023-01-17 10:47:57 -0700158{
159 const char *fdt_fname;
Simon Glassca84dc82023-02-22 12:17:04 -0700160 const char *prefix;
161
162 /* select the prefix */
163 switch (seq) {
164 case 0:
165 /* this is the default */
166 prefix = "/dtb";
167 break;
168 case 1:
169 prefix = "";
170 break;
171 case 2:
172 prefix = "/dtb/current";
173 break;
174 default:
175 return log_msg_ret("pref", -EINVAL);
176 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700177
178 fdt_fname = env_get("fdtfile");
179 if (fdt_fname) {
Simon Glassca84dc82023-02-22 12:17:04 -0700180 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
Simon Glass56bb4e42023-01-17 10:47:57 -0700181 log_debug("Using device tree: %s\n", fname);
Simon Glassca84dc82023-02-22 12:17:04 -0700182 } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
183 strcpy(fname, "<prior>");
184 return log_msg_ret("pref", -EALREADY);
185 /* Use this fallback only for 32-bit ARM */
186 } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
Simon Glass56bb4e42023-01-17 10:47:57 -0700187 const char *soc = env_get("soc");
188 const char *board = env_get("board");
189 const char *boardver = env_get("boardver");
190
191 /* cf the code in label_boot() which seems very complex */
Simon Glassca84dc82023-02-22 12:17:04 -0700192 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
Simon Glass56bb4e42023-01-17 10:47:57 -0700193 soc ? soc : "", soc ? "-" : "", board ? board : "",
194 boardver ? boardver : "");
195 log_debug("Using default device tree: %s\n", fname);
Simon Glassca84dc82023-02-22 12:17:04 -0700196 } else {
197 return log_msg_ret("env", -ENOENT);
Simon Glass56bb4e42023-01-17 10:47:57 -0700198 }
Simon Glassca84dc82023-02-22 12:17:04 -0700199
200 return 0;
Simon Glass56bb4e42023-01-17 10:47:57 -0700201}
202
Simon Glass4aec1902023-07-26 21:01:23 -0600203/*
204 * distro_efi_try_bootflow_files() - Check that files are present
205 *
206 * This reads any FDT file and checks whether the bootflow file is present, for
207 * later reading. We avoid reading the bootflow now, since it is likely large,
208 * it may take a long time and we want to avoid needing to allocate memory for
209 * it
210 *
211 * @dev: bootmeth device to use
212 * @bflow: bootflow to update
213 */
214static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass9e08caa2023-01-17 10:47:55 -0700215 struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600216{
217 struct blk_desc *desc = NULL;
Simon Glass56bb4e42023-01-17 10:47:57 -0700218 ulong fdt_addr, size;
Simon Glass9e08caa2023-01-17 10:47:55 -0700219 char fname[256];
Simon Glassca84dc82023-02-22 12:17:04 -0700220 int ret, seq;
Simon Glass66f62552022-04-24 23:31:17 -0600221
222 /* We require a partition table */
223 if (!bflow->part)
224 return -ENOENT;
225
226 strcpy(fname, EFI_DIRNAME);
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +0200227 strcat(fname, BOOTEFI_NAME);
Simon Glass66f62552022-04-24 23:31:17 -0600228
229 if (bflow->blk)
230 desc = dev_get_uclass_plat(bflow->blk);
231 ret = bootmeth_try_file(bflow, desc, NULL, fname);
232 if (ret)
233 return log_msg_ret("try", ret);
234
Simon Glass4aec1902023-07-26 21:01:23 -0600235 /* Since we can access the file, let's call it ready */
236 bflow->state = BOOTFLOWST_READY;
Simon Glass66f62552022-04-24 23:31:17 -0600237
Simon Glassca84dc82023-02-22 12:17:04 -0700238 fdt_addr = env_get_hex("fdt_addr_r", 0);
239
240 /* try the various available names */
241 ret = -ENOENT;
Simon Glass40516be2023-04-24 13:49:44 +1200242 *fname = '\0';
243 for (seq = 0; ret == -ENOENT; seq++) {
Simon Glassca84dc82023-02-22 12:17:04 -0700244 ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
Simon Glass40516be2023-04-24 13:49:44 +1200245 if (ret == -EALREADY)
Simon Glassca84dc82023-02-22 12:17:04 -0700246 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass02fafec2023-07-26 21:01:24 -0600247 if (!ret) {
248 /* Limit FDT files to 4MB */
249 size = SZ_4M;
Simon Glass40516be2023-04-24 13:49:44 +1200250 ret = bootmeth_common_read_file(dev, bflow, fname,
251 fdt_addr, &size);
Simon Glass02fafec2023-07-26 21:01:24 -0600252 }
Simon Glassca84dc82023-02-22 12:17:04 -0700253 }
254
Simon Glass40516be2023-04-24 13:49:44 +1200255 if (*fname) {
256 bflow->fdt_fname = strdup(fname);
257 if (!bflow->fdt_fname)
258 return log_msg_ret("fil", -ENOMEM);
259 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700260
Simon Glass56bb4e42023-01-17 10:47:57 -0700261 if (!ret) {
262 bflow->fdt_size = size;
263 bflow->fdt_addr = fdt_addr;
264
265 /*
266 * TODO: Apply extension overlay
267 *
268 * Here we need to load and apply the extension overlay. This is
269 * not implemented. See do_extension_apply(). The extension
270 * stuff needs an implementation in boot/extension.c so it is
271 * separate from the command code. Really the extension stuff
272 * should use the device tree and a uclass / driver interface
273 * rather than implementing its own list
274 */
275 } else {
276 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000277 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700278 }
279
Simon Glass66f62552022-04-24 23:31:17 -0600280 return 0;
281}
282
Simon Glass9e08caa2023-01-17 10:47:55 -0700283static int distro_efi_read_bootflow_net(struct bootflow *bflow)
284{
Simon Glass56bb4e42023-01-17 10:47:57 -0700285 char file_addr[17], fname[256];
286 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
287 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000288 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass9e08caa2023-01-17 10:47:55 -0700289 int ret, arch, size;
Simon Glass56bb4e42023-01-17 10:47:57 -0700290 ulong addr, fdt_addr;
Simon Glass9e08caa2023-01-17 10:47:55 -0700291 char str[36];
Simon Glass9e08caa2023-01-17 10:47:55 -0700292
293 ret = get_efi_pxe_vci(str, sizeof(str));
294 if (ret)
295 return log_msg_ret("vci", ret);
296 ret = get_efi_pxe_arch();
297 if (ret < 0)
298 return log_msg_ret("arc", ret);
299 arch = ret;
300
301 ret = env_set("bootp_vci", str);
302 if (ret)
303 return log_msg_ret("vcs", ret);
Shantur Rathoreb4c12522023-11-19 16:54:58 +0000304 ret = env_set_hex("bootp_arch", arch);
Simon Glass9e08caa2023-01-17 10:47:55 -0700305 if (ret)
306 return log_msg_ret("ars", ret);
307
308 /* figure out the load address */
309 addr_str = env_get("kernel_addr_r");
310 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
311
312 /* clear any previous bootfile */
313 env_set("bootfile", NULL);
314
315 /* read the kernel */
316 ret = dhcp_run(addr, NULL, true);
317 if (ret)
318 return log_msg_ret("dhc", ret);
319
320 size = env_get_hex("filesize", -1);
321 if (size <= 0)
322 return log_msg_ret("sz", -EINVAL);
323 bflow->size = size;
324
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000325 /* bootfile should be setup by dhcp*/
326 bootfile_name = env_get("bootfile");
327 if (!bootfile_name)
328 return log_msg_ret("bootfile_name", ret);
329 bflow->fname = strdup(bootfile_name);
330
Simon Glass9e08caa2023-01-17 10:47:55 -0700331 /* do the hideous EFI hack */
332 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
333 bflow->size);
334
Simon Glass56bb4e42023-01-17 10:47:57 -0700335 /* read the DT file also */
336 fdt_addr_str = env_get("fdt_addr_r");
337 if (!fdt_addr_str)
338 return log_msg_ret("fdt", -EINVAL);
339 fdt_addr = hextoul(fdt_addr_str, NULL);
340 sprintf(file_addr, "%lx", fdt_addr);
341
Simon Glassca84dc82023-02-22 12:17:04 -0700342 /* We only allow the first prefix with PXE */
343 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
344 if (ret)
345 return log_msg_ret("nam", ret);
346
Simon Glass56bb4e42023-01-17 10:47:57 -0700347 bflow->fdt_fname = strdup(fname);
348 if (!bflow->fdt_fname)
349 return log_msg_ret("fil", -ENOMEM);
350
351 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
352 bflow->fdt_size = env_get_hex("filesize", 0);
353 bflow->fdt_addr = fdt_addr;
354 } else {
355 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000356 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700357 }
358
Simon Glass9e08caa2023-01-17 10:47:55 -0700359 bflow->state = BOOTFLOWST_READY;
360
361 return 0;
362}
363
364static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
365{
Simon Glass9e08caa2023-01-17 10:47:55 -0700366 int ret;
367
Shantur Rathoreb45d7e22023-11-19 16:55:01 +0000368 /*
369 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
370 * set flag to avoid freeing static buffer.
371 */
372 bflow->flags |= BOOTFLOWF_STATIC_BUF;
373
Simon Glass47fc7342023-07-26 21:01:22 -0600374 if (bootmeth_uses_network(bflow)) {
Simon Glass9e08caa2023-01-17 10:47:55 -0700375 /* we only support reading from one device, so ignore 'dev' */
376 ret = distro_efi_read_bootflow_net(bflow);
377 if (ret)
378 return log_msg_ret("net", ret);
379 } else {
Simon Glass4aec1902023-07-26 21:01:23 -0600380 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass9e08caa2023-01-17 10:47:55 -0700381 if (ret)
382 return log_msg_ret("blk", ret);
383 }
384
385 return 0;
386}
387
Bin Meng79eb5f72023-08-03 17:30:05 +0800388static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600389{
Simon Glass56bb4e42023-01-17 10:47:57 -0700390 ulong kernel, fdt;
Simon Glass4aec1902023-07-26 21:01:23 -0600391 int ret;
Simon Glass66f62552022-04-24 23:31:17 -0600392
Simon Glass4aec1902023-07-26 21:01:23 -0600393 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass47fc7342023-07-26 21:01:22 -0600394 if (!bootmeth_uses_network(bflow)) {
Simon Glass4aec1902023-07-26 21:01:23 -0600395 ret = efiload_read_file(bflow, kernel);
396 if (ret)
397 return log_msg_ret("read", ret);
Simon Glass56bb4e42023-01-17 10:47:57 -0700398
399 /*
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000400 * use the provided device tree if not using the built-in fdt
Simon Glass56bb4e42023-01-17 10:47:57 -0700401 */
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000402 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glass56bb4e42023-01-17 10:47:57 -0700403 fdt = bflow->fdt_addr;
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000404
Simon Glass56bb4e42023-01-17 10:47:57 -0700405 } else {
406 /*
407 * This doesn't actually work for network devices:
408 *
409 * do_bootefi_image() No UEFI binary known at 0x02080000
410 *
411 * But this is the same behaviour for distro boot, so it can be
412 * fixed here.
413 */
Simon Glass56bb4e42023-01-17 10:47:57 -0700414 fdt = env_get_hex("fdt_addr_r", 0);
415 }
416
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000417 if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) {
418 log_debug("Booting with built-in fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100419 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500420 EFI_FDT_USE_INTERNAL))
421 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000422 } else {
423 log_debug("Booting with external fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100424 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500425 map_sysmem(fdt, 0)))
426 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000427 }
Simon Glass66f62552022-04-24 23:31:17 -0600428
429 return 0;
430}
431
432static int distro_bootmeth_efi_bind(struct udevice *dev)
433{
434 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
435
436 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
437 "EFI boot from an .efi file" : "EFI";
438
439 return 0;
440}
441
442static struct bootmeth_ops distro_efi_bootmeth_ops = {
443 .check = distro_efi_check,
444 .read_bootflow = distro_efi_read_bootflow,
445 .read_file = bootmeth_common_read_file,
446 .boot = distro_efi_boot,
447};
448
449static const struct udevice_id distro_efi_bootmeth_ids[] = {
450 { .compatible = "u-boot,distro-efi" },
451 { }
452};
453
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200454U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glass66f62552022-04-24 23:31:17 -0600455 .name = "bootmeth_efi",
456 .id = UCLASS_BOOTMETH,
457 .of_match = distro_efi_bootmeth_ids,
458 .ops = &distro_efi_bootmeth_ops,
459 .bind = distro_bootmeth_efi_bind,
460};