blob: f836aa655f573b8d4a933bf8a2f4e2ec188c02e2 [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>
Simon Glass3b1e60b2024-11-07 14:31:43 -070016#include <efi.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_vci(char *str, int max_len)
29{
30 int ret;
31
Simon Glass1824bf62024-11-07 14:31:44 -070032 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -070033 if (ret < 0)
34 return ret;
35
36 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
37
38 return 0;
39}
40
Simon Glass47fc7342023-07-26 21:01:22 -060041/**
42 * bootmeth_uses_network() - check if the media device is Ethernet
43 *
44 * @bflow: Bootflow to check
45 * Returns: true if the media device is Ethernet, else false
46 */
47static bool bootmeth_uses_network(struct bootflow *bflow)
48{
49 const struct udevice *media = dev_get_parent(bflow->dev);
50
51 return IS_ENABLED(CONFIG_CMD_DHCP) &&
52 device_get_uclass_id(media) == UCLASS_ETH;
53}
54
Simon Glassacad3622023-04-24 13:49:46 +120055static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -060056{
57 const struct udevice *media_dev;
58 int size = bflow->size;
59 const char *dev_name;
60 char devnum_str[9];
61 char dirname[200];
62 char *last_slash;
Simon Glass66f62552022-04-24 23:31:17 -060063
64 /*
65 * This is a horrible hack to tell EFI about this boot device. Once we
66 * unify EFI with the rest of U-Boot we can clean this up. The same hack
67 * exists in multiple places, e.g. in the fs, tftp and load commands.
68 *
69 * Once we can clean up the EFI code to make proper use of driver model,
70 * this can go away.
71 */
72 media_dev = dev_get_parent(bflow->dev);
Mathew McBride63c5e5c2023-04-24 13:49:50 +120073 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
74 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassacad3622023-04-24 13:49:46 +120075 bflow->part);
Simon Glass66f62552022-04-24 23:31:17 -060076
77 strlcpy(dirname, bflow->fname, sizeof(dirname));
78 last_slash = strrchr(dirname, '/');
79 if (last_slash)
80 *last_slash = '\0';
81
Simon Glass66f62552022-04-24 23:31:17 -060082 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
Heinrich Schuchardt9c8cf102024-08-07 02:13:45 +020083 "usb" : blk_get_uclass_name(device_get_uclass_id(media_dev));
84 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
85 dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glass66f62552022-04-24 23:31:17 -060086 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassacad3622023-04-24 13:49:46 +120087}
88
Simon Glass4aec1902023-07-26 21:01:23 -060089static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassacad3622023-04-24 13:49:46 +120090{
Simon Glass4aec1902023-07-26 21:01:23 -060091 struct blk_desc *desc = NULL;
92 loff_t bytes_read;
Simon Glassacad3622023-04-24 13:49:46 +120093 int ret;
94
Simon Glass4aec1902023-07-26 21:01:23 -060095 if (bflow->blk)
96 desc = dev_get_uclass_plat(bflow->blk);
97 ret = bootmeth_setup_fs(bflow, desc);
98 if (ret)
99 return log_msg_ret("set", ret);
100
101 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
Simon Glassacad3622023-04-24 13:49:46 +1200102 if (ret)
103 return log_msg_ret("read", ret);
Simon Glass4aec1902023-07-26 21:01:23 -0600104 bflow->buf = map_sysmem(addr, bflow->size);
105
106 set_efi_bootdev(desc, bflow);
Simon Glass66f62552022-04-24 23:31:17 -0600107
108 return 0;
109}
110
111static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
112{
Simon Glass9e08caa2023-01-17 10:47:55 -0700113 /* This only works on block and network devices */
114 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
115 return log_msg_ret("blk", -ENOTSUPP);
Simon Glass66f62552022-04-24 23:31:17 -0600116
Simon Glasse22fe922023-01-17 10:48:05 -0700117 /* This works on block devices and network devices */
118 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
119 return log_msg_ret("pxe", -ENOTSUPP);
120
Simon Glass66f62552022-04-24 23:31:17 -0600121 return 0;
122}
123
Simon Glass4aec1902023-07-26 21:01:23 -0600124/*
125 * distro_efi_try_bootflow_files() - Check that files are present
126 *
127 * This reads any FDT file and checks whether the bootflow file is present, for
128 * later reading. We avoid reading the bootflow now, since it is likely large,
129 * it may take a long time and we want to avoid needing to allocate memory for
130 * it
131 *
132 * @dev: bootmeth device to use
133 * @bflow: bootflow to update
134 */
135static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass9e08caa2023-01-17 10:47:55 -0700136 struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600137{
138 struct blk_desc *desc = NULL;
Simon Glass56bb4e42023-01-17 10:47:57 -0700139 ulong fdt_addr, size;
Simon Glass9e08caa2023-01-17 10:47:55 -0700140 char fname[256];
Simon Glassca84dc82023-02-22 12:17:04 -0700141 int ret, seq;
Simon Glass66f62552022-04-24 23:31:17 -0600142
143 /* We require a partition table */
Simon Glassb3445e02024-09-26 23:59:37 +0200144 if (!bflow->part) {
145 log_debug("no partitions\n");
Simon Glass66f62552022-04-24 23:31:17 -0600146 return -ENOENT;
Simon Glassb3445e02024-09-26 23:59:37 +0200147 }
Simon Glass66f62552022-04-24 23:31:17 -0600148
149 strcpy(fname, EFI_DIRNAME);
Simon Glass3b1e60b2024-11-07 14:31:43 -0700150 strcat(fname, efi_get_basename());
Simon Glass66f62552022-04-24 23:31:17 -0600151
152 if (bflow->blk)
153 desc = dev_get_uclass_plat(bflow->blk);
154 ret = bootmeth_try_file(bflow, desc, NULL, fname);
Simon Glassb3445e02024-09-26 23:59:37 +0200155 if (ret) {
156 log_debug("File '%s' not found\n", fname);
Simon Glass66f62552022-04-24 23:31:17 -0600157 return log_msg_ret("try", ret);
Simon Glassb3445e02024-09-26 23:59:37 +0200158 }
Simon Glass66f62552022-04-24 23:31:17 -0600159
Simon Glass4aec1902023-07-26 21:01:23 -0600160 /* Since we can access the file, let's call it ready */
161 bflow->state = BOOTFLOWST_READY;
Simon Glass66f62552022-04-24 23:31:17 -0600162
Simon Glassca84dc82023-02-22 12:17:04 -0700163 fdt_addr = env_get_hex("fdt_addr_r", 0);
164
165 /* try the various available names */
166 ret = -ENOENT;
Simon Glass40516be2023-04-24 13:49:44 +1200167 *fname = '\0';
168 for (seq = 0; ret == -ENOENT; seq++) {
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200169 ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
Simon Glass40516be2023-04-24 13:49:44 +1200170 if (ret == -EALREADY)
Simon Glassca84dc82023-02-22 12:17:04 -0700171 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass02fafec2023-07-26 21:01:24 -0600172 if (!ret) {
173 /* Limit FDT files to 4MB */
174 size = SZ_4M;
Simon Glass40516be2023-04-24 13:49:44 +1200175 ret = bootmeth_common_read_file(dev, bflow, fname,
176 fdt_addr, &size);
Simon Glass02fafec2023-07-26 21:01:24 -0600177 }
Simon Glassca84dc82023-02-22 12:17:04 -0700178 }
179
Simon Glass40516be2023-04-24 13:49:44 +1200180 if (*fname) {
181 bflow->fdt_fname = strdup(fname);
182 if (!bflow->fdt_fname)
183 return log_msg_ret("fil", -ENOMEM);
184 }
Simon Glass56bb4e42023-01-17 10:47:57 -0700185
Simon Glass56bb4e42023-01-17 10:47:57 -0700186 if (!ret) {
187 bflow->fdt_size = size;
188 bflow->fdt_addr = fdt_addr;
189
190 /*
191 * TODO: Apply extension overlay
192 *
193 * Here we need to load and apply the extension overlay. This is
194 * not implemented. See do_extension_apply(). The extension
195 * stuff needs an implementation in boot/extension.c so it is
196 * separate from the command code. Really the extension stuff
197 * should use the device tree and a uclass / driver interface
198 * rather than implementing its own list
199 */
200 } else {
201 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000202 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700203 }
204
Simon Glass66f62552022-04-24 23:31:17 -0600205 return 0;
206}
207
Simon Glass9e08caa2023-01-17 10:47:55 -0700208static int distro_efi_read_bootflow_net(struct bootflow *bflow)
209{
Simon Glass56bb4e42023-01-17 10:47:57 -0700210 char file_addr[17], fname[256];
211 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
212 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000213 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass9e08caa2023-01-17 10:47:55 -0700214 int ret, arch, size;
Simon Glass56bb4e42023-01-17 10:47:57 -0700215 ulong addr, fdt_addr;
Simon Glass9e08caa2023-01-17 10:47:55 -0700216 char str[36];
Simon Glass9e08caa2023-01-17 10:47:55 -0700217
218 ret = get_efi_pxe_vci(str, sizeof(str));
219 if (ret)
220 return log_msg_ret("vci", ret);
Simon Glass1824bf62024-11-07 14:31:44 -0700221 ret = efi_get_pxe_arch();
Simon Glass9e08caa2023-01-17 10:47:55 -0700222 if (ret < 0)
223 return log_msg_ret("arc", ret);
224 arch = ret;
225
226 ret = env_set("bootp_vci", str);
227 if (ret)
228 return log_msg_ret("vcs", ret);
Shantur Rathoreb4c12522023-11-19 16:54:58 +0000229 ret = env_set_hex("bootp_arch", arch);
Simon Glass9e08caa2023-01-17 10:47:55 -0700230 if (ret)
231 return log_msg_ret("ars", ret);
232
233 /* figure out the load address */
234 addr_str = env_get("kernel_addr_r");
235 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
236
237 /* clear any previous bootfile */
238 env_set("bootfile", NULL);
239
240 /* read the kernel */
241 ret = dhcp_run(addr, NULL, true);
242 if (ret)
243 return log_msg_ret("dhc", ret);
244
245 size = env_get_hex("filesize", -1);
246 if (size <= 0)
247 return log_msg_ret("sz", -EINVAL);
248 bflow->size = size;
249
Simon Glass5b0d9142024-07-17 09:31:03 +0100250 /* bootfile should be setup by dhcp */
Shantur Rathore1c34f9d2023-11-19 16:54:59 +0000251 bootfile_name = env_get("bootfile");
252 if (!bootfile_name)
253 return log_msg_ret("bootfile_name", ret);
254 bflow->fname = strdup(bootfile_name);
255
Simon Glass9e08caa2023-01-17 10:47:55 -0700256 /* do the hideous EFI hack */
257 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
258 bflow->size);
259
Simon Glass56bb4e42023-01-17 10:47:57 -0700260 /* read the DT file also */
261 fdt_addr_str = env_get("fdt_addr_r");
262 if (!fdt_addr_str)
263 return log_msg_ret("fdt", -EINVAL);
264 fdt_addr = hextoul(fdt_addr_str, NULL);
265 sprintf(file_addr, "%lx", fdt_addr);
266
Simon Glassca84dc82023-02-22 12:17:04 -0700267 /* We only allow the first prefix with PXE */
Heinrich Schuchardt1a41ca42024-04-26 16:13:18 +0200268 ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
Simon Glassca84dc82023-02-22 12:17:04 -0700269 if (ret)
270 return log_msg_ret("nam", ret);
271
Simon Glass56bb4e42023-01-17 10:47:57 -0700272 bflow->fdt_fname = strdup(fname);
273 if (!bflow->fdt_fname)
274 return log_msg_ret("fil", -ENOMEM);
275
276 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
277 bflow->fdt_size = env_get_hex("filesize", 0);
278 bflow->fdt_addr = fdt_addr;
279 } else {
280 log_debug("No device tree available\n");
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000281 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glass56bb4e42023-01-17 10:47:57 -0700282 }
283
Simon Glass9e08caa2023-01-17 10:47:55 -0700284 bflow->state = BOOTFLOWST_READY;
285
286 return 0;
287}
288
289static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
290{
Simon Glass9e08caa2023-01-17 10:47:55 -0700291 int ret;
292
Simon Glassb3445e02024-09-26 23:59:37 +0200293 log_debug("dev='%s', part=%d\n", bflow->dev->name, bflow->part);
294
Shantur Rathoreb45d7e22023-11-19 16:55:01 +0000295 /*
296 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
297 * set flag to avoid freeing static buffer.
298 */
299 bflow->flags |= BOOTFLOWF_STATIC_BUF;
300
Simon Glass47fc7342023-07-26 21:01:22 -0600301 if (bootmeth_uses_network(bflow)) {
Simon Glass9e08caa2023-01-17 10:47:55 -0700302 /* we only support reading from one device, so ignore 'dev' */
303 ret = distro_efi_read_bootflow_net(bflow);
304 if (ret)
305 return log_msg_ret("net", ret);
306 } else {
Simon Glass4aec1902023-07-26 21:01:23 -0600307 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass9e08caa2023-01-17 10:47:55 -0700308 if (ret)
309 return log_msg_ret("blk", ret);
310 }
311
312 return 0;
313}
314
Bin Meng79eb5f72023-08-03 17:30:05 +0800315static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass66f62552022-04-24 23:31:17 -0600316{
Simon Glass56bb4e42023-01-17 10:47:57 -0700317 ulong kernel, fdt;
Simon Glass4aec1902023-07-26 21:01:23 -0600318 int ret;
Simon Glass66f62552022-04-24 23:31:17 -0600319
Simon Glassb3445e02024-09-26 23:59:37 +0200320 log_debug("distro EFI boot\n");
Simon Glass4aec1902023-07-26 21:01:23 -0600321 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass47fc7342023-07-26 21:01:22 -0600322 if (!bootmeth_uses_network(bflow)) {
Simon Glass4aec1902023-07-26 21:01:23 -0600323 ret = efiload_read_file(bflow, kernel);
324 if (ret)
325 return log_msg_ret("read", ret);
Simon Glass56bb4e42023-01-17 10:47:57 -0700326
327 /*
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000328 * use the provided device tree if not using the built-in fdt
Simon Glass56bb4e42023-01-17 10:47:57 -0700329 */
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000330 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glass56bb4e42023-01-17 10:47:57 -0700331 fdt = bflow->fdt_addr;
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000332
Simon Glass56bb4e42023-01-17 10:47:57 -0700333 } else {
334 /*
335 * This doesn't actually work for network devices:
336 *
337 * do_bootefi_image() No UEFI binary known at 0x02080000
338 *
339 * But this is the same behaviour for distro boot, so it can be
340 * fixed here.
341 */
Simon Glass56bb4e42023-01-17 10:47:57 -0700342 fdt = env_get_hex("fdt_addr_r", 0);
343 }
344
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000345 if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) {
346 log_debug("Booting with built-in fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100347 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500348 EFI_FDT_USE_INTERNAL))
349 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000350 } else {
351 log_debug("Booting with external fdt\n");
Heinrich Schuchardt8ff257c2023-12-22 16:01:56 +0100352 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini08ccd542023-12-18 08:31:50 -0500353 map_sysmem(fdt, 0)))
354 return log_msg_ret("run", -EINVAL);
Shantur Rathoreaab9cdb2023-11-19 16:55:00 +0000355 }
Simon Glass66f62552022-04-24 23:31:17 -0600356
357 return 0;
358}
359
360static int distro_bootmeth_efi_bind(struct udevice *dev)
361{
362 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
363
364 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
365 "EFI boot from an .efi file" : "EFI";
366
367 return 0;
368}
369
370static struct bootmeth_ops distro_efi_bootmeth_ops = {
371 .check = distro_efi_check,
372 .read_bootflow = distro_efi_read_bootflow,
373 .read_file = bootmeth_common_read_file,
374 .boot = distro_efi_boot,
375};
376
377static const struct udevice_id distro_efi_bootmeth_ids[] = {
378 { .compatible = "u-boot,distro-efi" },
379 { }
380};
381
Simon Glasse5cb0182024-07-17 09:30:59 +0100382/* Put a number before 'efi' to provide a default ordering */
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200383U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glass66f62552022-04-24 23:31:17 -0600384 .name = "bootmeth_efi",
385 .id = UCLASS_BOOTMETH,
386 .of_match = distro_efi_bootmeth_ids,
387 .ops = &distro_efi_bootmeth_ops,
388 .bind = distro_bootmeth_efi_bind,
389};