blob: c7035c0d0c4e2fed5a0eeae63fdf8ffd40e54905 [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
Simon Glass66f62552022-04-24 23:31:17 -060011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
14#include <command.h>
15#include <dm.h>
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020016#include <efi_default_filename.h>
Simon Glass66f62552022-04-24 23:31:17 -060017#include <efi_loader.h>
18#include <fs.h>
19#include <malloc.h>
20#include <mapmem.h>
21#include <mmc.h>
Simon Glass9e08caa2023-01-17 10:47:55 -070022#include <net.h>
Simon Glass66f62552022-04-24 23:31:17 -060023#include <pxe_utils.h>
Simon Glass02fafec2023-07-26 21:01:24 -060024#include <linux/sizes.h>
Simon Glass66f62552022-04-24 23:31:17 -060025
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020026#define EFI_DIRNAME "/EFI/BOOT/"
Simon Glass66f62552022-04-24 23:31:17 -060027
Simon Glass9e08caa2023-01-17 10:47:55 -070028static int get_efi_pxe_arch(void)
29{
30 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
31 if (IS_ENABLED(CONFIG_ARM64))
32 return 0xb;
33 else if (IS_ENABLED(CONFIG_ARM))
34 return 0xa;
35 else if (IS_ENABLED(CONFIG_X86_64))
36 return 0x6;
37 else if (IS_ENABLED(CONFIG_X86))
38 return 0x7;
39 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
40 return 0x19;
41 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
42 return 0x1b;
43 else if (IS_ENABLED(CONFIG_SANDBOX))
44 return 0; /* not used */
45
46 return -EINVAL;
47}
48
49static int get_efi_pxe_vci(char *str, int max_len)
50{
51 int ret;
52
53 ret = get_efi_pxe_arch();
54 if (ret < 0)
55 return ret;
56
57 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
58
59 return 0;
60}
61
Simon Glass47fc7342023-07-26 21:01:22 -060062/**
63 * bootmeth_uses_network() - check if the media device is Ethernet
64 *
65 * @bflow: Bootflow to check
66 * Returns: true if the media device is Ethernet, else false
67 */
68static bool bootmeth_uses_network(struct bootflow *bflow)
69{
70 const struct udevice *media = dev_get_parent(bflow->dev);
71
72 return IS_ENABLED(CONFIG_CMD_DHCP) &&
73 device_get_uclass_id(media) == UCLASS_ETH;
74}
75
Simon Glassacad3622023-04-24 13:49:46 +120076static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -060077{
78 const struct udevice *media_dev;
79 int size = bflow->size;
80 const char *dev_name;
81 char devnum_str[9];
82 char dirname[200];
83 char *last_slash;
Simon Glass66f62552022-04-24 23:31:17 -060084
85 /*
86 * This is a horrible hack to tell EFI about this boot device. Once we
87 * unify EFI with the rest of U-Boot we can clean this up. The same hack
88 * exists in multiple places, e.g. in the fs, tftp and load commands.
89 *
90 * Once we can clean up the EFI code to make proper use of driver model,
91 * this can go away.
92 */
93 media_dev = dev_get_parent(bflow->dev);
Mathew McBride63c5e5c2023-04-24 13:49:50 +120094 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
95 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassacad3622023-04-24 13:49:46 +120096 bflow->part);
Simon Glass66f62552022-04-24 23:31:17 -060097
98 strlcpy(dirname, bflow->fname, sizeof(dirname));
99 last_slash = strrchr(dirname, '/');
100 if (last_slash)
101 *last_slash = '\0';
102
103 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
104 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
105 bflow->buf, size);
106 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
107 "usb" : dev_get_uclass_name(media_dev);
108 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassacad3622023-04-24 13:49:46 +1200109}
110
Simon Glass4aec1902023-07-26 21:01:23 -0600111static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassacad3622023-04-24 13:49:46 +1200112{
Simon Glass4aec1902023-07-26 21:01:23 -0600113 struct blk_desc *desc = NULL;
114 loff_t bytes_read;
Simon Glassacad3622023-04-24 13:49:46 +1200115 int ret;
116
Simon Glass4aec1902023-07-26 21:01:23 -0600117 if (bflow->blk)
118 desc = dev_get_uclass_plat(bflow->blk);
119 ret = bootmeth_setup_fs(bflow, desc);
120 if (ret)
121 return log_msg_ret("set", ret);
122
123 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
Simon Glassacad3622023-04-24 13:49:46 +1200124 if (ret)
125 return log_msg_ret("read", ret);
Simon Glass4aec1902023-07-26 21:01:23 -0600126 bflow->buf = map_sysmem(addr, bflow->size);
127
128 set_efi_bootdev(desc, bflow);
Simon Glass66f62552022-04-24 23:31:17 -0600129
130 return 0;
131}
132
133static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
134{
Simon Glass9e08caa2023-01-17 10:47:55 -0700135 /* This only works on block and network devices */
136 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
137 return log_msg_ret("blk", -ENOTSUPP);
Simon Glass66f62552022-04-24 23:31:17 -0600138
Simon Glasse22fe922023-01-17 10:48:05 -0700139 /* This works on block devices and network devices */
140 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
141 return log_msg_ret("pxe", -ENOTSUPP);
142
Simon Glass66f62552022-04-24 23:31:17 -0600143 return 0;
144}
145
Simon Glassca84dc82023-02-22 12:17:04 -0700146/**
147 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
148 *
149 * @fname: Place to put filename
150 * @size: Max size of filename
151 * @seq: Sequence number, to cycle through options (0=first)
152 * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
153 * -EINVAL if there are no more options, -EALREADY if the control FDT should be
154 * used
155 */
156static int distro_efi_get_fdt_name(char *fname, int size, int seq)
Simon Glass56bb4e42023-01-17 10:47:57 -0700157{
158 const char *fdt_fname;
Simon Glassca84dc82023-02-22 12:17:04 -0700159 const char *prefix;
160
161 /* select the prefix */
162 switch (seq) {
163 case 0:
164 /* this is the default */
165 prefix = "/dtb";
166 break;
167 case 1:
168 prefix = "";
169 break;
170 case 2:
171 prefix = "/dtb/current";
172 break;
173 default:
174 return log_msg_ret("pref", -EINVAL);
175 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700176
177 fdt_fname = env_get("fdtfile");
178 if (fdt_fname) {
Simon Glassca84dc82023-02-22 12:17:04 -0700179 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
Simon Glass56bb4e42023-01-17 10:47:57 -0700180 log_debug("Using device tree: %s\n", fname);
Simon Glassca84dc82023-02-22 12:17:04 -0700181 } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
182 strcpy(fname, "<prior>");
183 return log_msg_ret("pref", -EALREADY);
184 /* Use this fallback only for 32-bit ARM */
185 } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
Simon Glass56bb4e42023-01-17 10:47:57 -0700186 const char *soc = env_get("soc");
187 const char *board = env_get("board");
188 const char *boardver = env_get("boardver");
189
190 /* cf the code in label_boot() which seems very complex */
Simon Glassca84dc82023-02-22 12:17:04 -0700191 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
Simon Glass56bb4e42023-01-17 10:47:57 -0700192 soc ? soc : "", soc ? "-" : "", board ? board : "",
193 boardver ? boardver : "");
194 log_debug("Using default device tree: %s\n", fname);
Simon Glassca84dc82023-02-22 12:17:04 -0700195 } else {
196 return log_msg_ret("env", -ENOENT);
Simon Glass56bb4e42023-01-17 10:47:57 -0700197 }
Simon Glassca84dc82023-02-22 12:17:04 -0700198
199 return 0;
Simon Glass56bb4e42023-01-17 10:47:57 -0700200}
201
Simon Glass4aec1902023-07-26 21:01:23 -0600202/*
203 * distro_efi_try_bootflow_files() - Check that files are present
204 *
205 * This reads any FDT file and checks whether the bootflow file is present, for
206 * later reading. We avoid reading the bootflow now, since it is likely large,
207 * it may take a long time and we want to avoid needing to allocate memory for
208 * it
209 *
210 * @dev: bootmeth device to use
211 * @bflow: bootflow to update
212 */
213static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass9e08caa2023-01-17 10:47:55 -0700214 struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600215{
216 struct blk_desc *desc = NULL;
Simon Glass56bb4e42023-01-17 10:47:57 -0700217 ulong fdt_addr, size;
Simon Glass9e08caa2023-01-17 10:47:55 -0700218 char fname[256];
Simon Glassca84dc82023-02-22 12:17:04 -0700219 int ret, seq;
Simon Glass66f62552022-04-24 23:31:17 -0600220
221 /* We require a partition table */
222 if (!bflow->part)
223 return -ENOENT;
224
225 strcpy(fname, EFI_DIRNAME);
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +0200226 strcat(fname, BOOTEFI_NAME);
Simon Glass66f62552022-04-24 23:31:17 -0600227
228 if (bflow->blk)
229 desc = dev_get_uclass_plat(bflow->blk);
230 ret = bootmeth_try_file(bflow, desc, NULL, fname);
231 if (ret)
232 return log_msg_ret("try", ret);
233
Simon Glass4aec1902023-07-26 21:01:23 -0600234 /* Since we can access the file, let's call it ready */
235 bflow->state = BOOTFLOWST_READY;
Simon Glass66f62552022-04-24 23:31:17 -0600236
Simon Glassca84dc82023-02-22 12:17:04 -0700237 fdt_addr = env_get_hex("fdt_addr_r", 0);
238
239 /* try the various available names */
240 ret = -ENOENT;
Simon Glass40516be2023-04-24 13:49:44 +1200241 *fname = '\0';
242 for (seq = 0; ret == -ENOENT; seq++) {
Simon Glassca84dc82023-02-22 12:17:04 -0700243 ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
Simon Glass40516be2023-04-24 13:49:44 +1200244 if (ret == -EALREADY)
Simon Glassca84dc82023-02-22 12:17:04 -0700245 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass02fafec2023-07-26 21:01:24 -0600246 if (!ret) {
247 /* Limit FDT files to 4MB */
248 size = SZ_4M;
Simon Glass40516be2023-04-24 13:49:44 +1200249 ret = bootmeth_common_read_file(dev, bflow, fname,
250 fdt_addr, &size);
Simon Glass02fafec2023-07-26 21:01:24 -0600251 }
Simon Glassca84dc82023-02-22 12:17:04 -0700252 }
253
Simon Glass40516be2023-04-24 13:49:44 +1200254 if (*fname) {
255 bflow->fdt_fname = strdup(fname);
256 if (!bflow->fdt_fname)
257 return log_msg_ret("fil", -ENOMEM);
258 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700259
Simon Glass56bb4e42023-01-17 10:47:57 -0700260 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");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000276 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700277 }
278
Simon Glass66f62552022-04-24 23:31:17 -0600279 return 0;
280}
281
Simon Glass9e08caa2023-01-17 10:47:55 -0700282static int distro_efi_read_bootflow_net(struct bootflow *bflow)
283{
Simon Glass56bb4e42023-01-17 10:47:57 -0700284 char file_addr[17], fname[256];
285 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
286 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000287 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass9e08caa2023-01-17 10:47:55 -0700288 int ret, arch, size;
Simon Glass56bb4e42023-01-17 10:47:57 -0700289 ulong addr, fdt_addr;
Simon Glass9e08caa2023-01-17 10:47:55 -0700290 char str[36];
Simon Glass9e08caa2023-01-17 10:47:55 -0700291
292 ret = get_efi_pxe_vci(str, sizeof(str));
293 if (ret)
294 return log_msg_ret("vci", ret);
295 ret = get_efi_pxe_arch();
296 if (ret < 0)
297 return log_msg_ret("arc", ret);
298 arch = ret;
299
300 ret = env_set("bootp_vci", str);
301 if (ret)
302 return log_msg_ret("vcs", ret);
Shantur Rathoreb4c12522023-11-19 16:54:58 +0000303 ret = env_set_hex("bootp_arch", arch);
Simon Glass9e08caa2023-01-17 10:47:55 -0700304 if (ret)
305 return log_msg_ret("ars", ret);
306
307 /* figure out the load address */
308 addr_str = env_get("kernel_addr_r");
309 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
310
311 /* clear any previous bootfile */
312 env_set("bootfile", NULL);
313
314 /* read the kernel */
315 ret = dhcp_run(addr, NULL, true);
316 if (ret)
317 return log_msg_ret("dhc", ret);
318
319 size = env_get_hex("filesize", -1);
320 if (size <= 0)
321 return log_msg_ret("sz", -EINVAL);
322 bflow->size = size;
323
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000324 /* bootfile should be setup by dhcp*/
325 bootfile_name = env_get("bootfile");
326 if (!bootfile_name)
327 return log_msg_ret("bootfile_name", ret);
328 bflow->fname = strdup(bootfile_name);
329
Simon Glass9e08caa2023-01-17 10:47:55 -0700330 /* do the hideous EFI hack */
331 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
332 bflow->size);
333
Simon Glass56bb4e42023-01-17 10:47:57 -0700334 /* read the DT file also */
335 fdt_addr_str = env_get("fdt_addr_r");
336 if (!fdt_addr_str)
337 return log_msg_ret("fdt", -EINVAL);
338 fdt_addr = hextoul(fdt_addr_str, NULL);
339 sprintf(file_addr, "%lx", fdt_addr);
340
Simon Glassca84dc82023-02-22 12:17:04 -0700341 /* We only allow the first prefix with PXE */
342 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
343 if (ret)
344 return log_msg_ret("nam", ret);
345
Simon Glass56bb4e42023-01-17 10:47:57 -0700346 bflow->fdt_fname = strdup(fname);
347 if (!bflow->fdt_fname)
348 return log_msg_ret("fil", -ENOMEM);
349
350 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
351 bflow->fdt_size = env_get_hex("filesize", 0);
352 bflow->fdt_addr = fdt_addr;
353 } else {
354 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000355 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700356 }
357
Simon Glass9e08caa2023-01-17 10:47:55 -0700358 bflow->state = BOOTFLOWST_READY;
359
360 return 0;
361}
362
363static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
364{
Simon Glass9e08caa2023-01-17 10:47:55 -0700365 int ret;
366
Shantur Rathoreb45d7e22023-11-19 16:55:01 +0000367 /*
368 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
369 * set flag to avoid freeing static buffer.
370 */
371 bflow->flags |= BOOTFLOWF_STATIC_BUF;
372
Simon Glass47fc7342023-07-26 21:01:22 -0600373 if (bootmeth_uses_network(bflow)) {
Simon Glass9e08caa2023-01-17 10:47:55 -0700374 /* we only support reading from one device, so ignore 'dev' */
375 ret = distro_efi_read_bootflow_net(bflow);
376 if (ret)
377 return log_msg_ret("net", ret);
378 } else {
Simon Glass4aec1902023-07-26 21:01:23 -0600379 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass9e08caa2023-01-17 10:47:55 -0700380 if (ret)
381 return log_msg_ret("blk", ret);
382 }
383
384 return 0;
385}
386
Bin Meng79eb5f72023-08-03 17:30:05 +0800387static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600388{
Simon Glass56bb4e42023-01-17 10:47:57 -0700389 ulong kernel, fdt;
Simon Glass4aec1902023-07-26 21:01:23 -0600390 int ret;
Simon Glass66f62552022-04-24 23:31:17 -0600391
Simon Glass4aec1902023-07-26 21:01:23 -0600392 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass47fc7342023-07-26 21:01:22 -0600393 if (!bootmeth_uses_network(bflow)) {
Simon Glass4aec1902023-07-26 21:01:23 -0600394 ret = efiload_read_file(bflow, kernel);
395 if (ret)
396 return log_msg_ret("read", ret);
Simon Glass56bb4e42023-01-17 10:47:57 -0700397
398 /*
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000399 * use the provided device tree if not using the built-in fdt
Simon Glass56bb4e42023-01-17 10:47:57 -0700400 */
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000401 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glass56bb4e42023-01-17 10:47:57 -0700402 fdt = bflow->fdt_addr;
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000403
Simon Glass56bb4e42023-01-17 10:47:57 -0700404 } else {
405 /*
406 * This doesn't actually work for network devices:
407 *
408 * do_bootefi_image() No UEFI binary known at 0x02080000
409 *
410 * But this is the same behaviour for distro boot, so it can be
411 * fixed here.
412 */
Simon Glass56bb4e42023-01-17 10:47:57 -0700413 fdt = env_get_hex("fdt_addr_r", 0);
414 }
415
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000416 if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) {
417 log_debug("Booting with built-in fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100418 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500419 EFI_FDT_USE_INTERNAL))
420 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000421 } else {
422 log_debug("Booting with external fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100423 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500424 map_sysmem(fdt, 0)))
425 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000426 }
Simon Glass66f62552022-04-24 23:31:17 -0600427
428 return 0;
429}
430
431static int distro_bootmeth_efi_bind(struct udevice *dev)
432{
433 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
434
435 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
436 "EFI boot from an .efi file" : "EFI";
437
438 return 0;
439}
440
441static struct bootmeth_ops distro_efi_bootmeth_ops = {
442 .check = distro_efi_check,
443 .read_bootflow = distro_efi_read_bootflow,
444 .read_file = bootmeth_common_read_file,
445 .boot = distro_efi_boot,
446};
447
448static const struct udevice_id distro_efi_bootmeth_ids[] = {
449 { .compatible = "u-boot,distro-efi" },
450 { }
451};
452
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200453U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glass66f62552022-04-24 23:31:17 -0600454 .name = "bootmeth_efi",
455 .id = UCLASS_BOOTMETH,
456 .of_match = distro_efi_bootmeth_ids,
457 .ops = &distro_efi_bootmeth_ops,
458 .bind = distro_bootmeth_efi_bind,
459};