blob: 6b41c0999f1cf7f067191ee68c56a2574b29ceb2 [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
Simon Glassacfa9bd2022-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 Schuchardt92f4cb62024-04-03 23:39:48 +020016#include <efi_default_filename.h>
Simon Glassacfa9bd2022-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 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
Heinrich Schuchardt92f4cb62024-04-03 23:39:48 +020026#define EFI_DIRNAME "/EFI/BOOT/"
Simon Glassacfa9bd2022-04-24 23:31:17 -060027
Simon Glass3e188602023-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 Glass146242c2023-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 Glassd80bb4f2023-04-24 13:49:46 +120076static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glassacfa9bd2022-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 Glassacfa9bd2022-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 McBride484e7012023-04-24 13:49:50 +120094 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
95 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassd80bb4f2023-04-24 13:49:46 +120096 bflow->part);
Simon Glassacfa9bd2022-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
Simon Glassacfa9bd2022-04-24 23:31:17 -0600103 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
Heinrich Schuchardted3ce652024-08-07 02:13:45 +0200104 "usb" : blk_get_uclass_name(device_get_uclass_id(media_dev));
105 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
106 dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600107 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassd80bb4f2023-04-24 13:49:46 +1200108}
109
Simon Glass6a8c2f92023-07-26 21:01:23 -0600110static int efiload_read_file(struct bootflow *bflow, ulong addr)
Simon Glassd80bb4f2023-04-24 13:49:46 +1200111{
Simon Glass6a8c2f92023-07-26 21:01:23 -0600112 struct blk_desc *desc = NULL;
113 loff_t bytes_read;
Simon Glassd80bb4f2023-04-24 13:49:46 +1200114 int ret;
115
Simon Glass6a8c2f92023-07-26 21:01:23 -0600116 if (bflow->blk)
117 desc = dev_get_uclass_plat(bflow->blk);
118 ret = bootmeth_setup_fs(bflow, desc);
119 if (ret)
120 return log_msg_ret("set", ret);
121
122 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
Simon Glassd80bb4f2023-04-24 13:49:46 +1200123 if (ret)
124 return log_msg_ret("read", ret);
Simon Glass6a8c2f92023-07-26 21:01:23 -0600125 bflow->buf = map_sysmem(addr, bflow->size);
126
127 set_efi_bootdev(desc, bflow);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600128
129 return 0;
130}
131
132static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
133{
Simon Glass3e188602023-01-17 10:47:55 -0700134 /* This only works on block and network devices */
135 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
136 return log_msg_ret("blk", -ENOTSUPP);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600137
Simon Glassd9f48572023-01-17 10:48:05 -0700138 /* This works on block devices and network devices */
139 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
140 return log_msg_ret("pxe", -ENOTSUPP);
141
Simon Glassacfa9bd2022-04-24 23:31:17 -0600142 return 0;
143}
144
Simon Glass6a8c2f92023-07-26 21:01:23 -0600145/*
146 * distro_efi_try_bootflow_files() - Check that files are present
147 *
148 * This reads any FDT file and checks whether the bootflow file is present, for
149 * later reading. We avoid reading the bootflow now, since it is likely large,
150 * it may take a long time and we want to avoid needing to allocate memory for
151 * it
152 *
153 * @dev: bootmeth device to use
154 * @bflow: bootflow to update
155 */
156static int distro_efi_try_bootflow_files(struct udevice *dev,
Simon Glass3e188602023-01-17 10:47:55 -0700157 struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600158{
159 struct blk_desc *desc = NULL;
Simon Glasse890e8c2023-01-17 10:47:57 -0700160 ulong fdt_addr, size;
Simon Glass3e188602023-01-17 10:47:55 -0700161 char fname[256];
Simon Glass47dd6b42023-02-22 12:17:04 -0700162 int ret, seq;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600163
164 /* We require a partition table */
165 if (!bflow->part)
166 return -ENOENT;
167
168 strcpy(fname, EFI_DIRNAME);
Heinrich Schuchardt92f4cb62024-04-03 23:39:48 +0200169 strcat(fname, BOOTEFI_NAME);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600170
171 if (bflow->blk)
172 desc = dev_get_uclass_plat(bflow->blk);
173 ret = bootmeth_try_file(bflow, desc, NULL, fname);
174 if (ret)
175 return log_msg_ret("try", ret);
176
Simon Glass6a8c2f92023-07-26 21:01:23 -0600177 /* Since we can access the file, let's call it ready */
178 bflow->state = BOOTFLOWST_READY;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600179
Simon Glass47dd6b42023-02-22 12:17:04 -0700180 fdt_addr = env_get_hex("fdt_addr_r", 0);
181
182 /* try the various available names */
183 ret = -ENOENT;
Simon Glass9f835162023-04-24 13:49:44 +1200184 *fname = '\0';
185 for (seq = 0; ret == -ENOENT; seq++) {
Heinrich Schuchardtc9463852024-04-26 16:13:18 +0200186 ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
Simon Glass9f835162023-04-24 13:49:44 +1200187 if (ret == -EALREADY)
Simon Glass47dd6b42023-02-22 12:17:04 -0700188 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass2984d212023-07-26 21:01:24 -0600189 if (!ret) {
190 /* Limit FDT files to 4MB */
191 size = SZ_4M;
Simon Glass9f835162023-04-24 13:49:44 +1200192 ret = bootmeth_common_read_file(dev, bflow, fname,
193 fdt_addr, &size);
Simon Glass2984d212023-07-26 21:01:24 -0600194 }
Simon Glass47dd6b42023-02-22 12:17:04 -0700195 }
196
Simon Glass9f835162023-04-24 13:49:44 +1200197 if (*fname) {
198 bflow->fdt_fname = strdup(fname);
199 if (!bflow->fdt_fname)
200 return log_msg_ret("fil", -ENOMEM);
201 }
Simon Glasse890e8c2023-01-17 10:47:57 -0700202
Simon Glasse890e8c2023-01-17 10:47:57 -0700203 if (!ret) {
204 bflow->fdt_size = size;
205 bflow->fdt_addr = fdt_addr;
206
207 /*
208 * TODO: Apply extension overlay
209 *
210 * Here we need to load and apply the extension overlay. This is
211 * not implemented. See do_extension_apply(). The extension
212 * stuff needs an implementation in boot/extension.c so it is
213 * separate from the command code. Really the extension stuff
214 * should use the device tree and a uclass / driver interface
215 * rather than implementing its own list
216 */
217 } else {
218 log_debug("No device tree available\n");
Shantur Rathore184fc032023-11-19 16:55:00 +0000219 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glasse890e8c2023-01-17 10:47:57 -0700220 }
221
Simon Glassacfa9bd2022-04-24 23:31:17 -0600222 return 0;
223}
224
Simon Glass3e188602023-01-17 10:47:55 -0700225static int distro_efi_read_bootflow_net(struct bootflow *bflow)
226{
Simon Glasse890e8c2023-01-17 10:47:57 -0700227 char file_addr[17], fname[256];
228 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
229 struct cmd_tbl cmdtp = {}; /* dummy */
Shantur Rathoree31317e2023-11-19 16:54:59 +0000230 const char *addr_str, *fdt_addr_str, *bootfile_name;
Simon Glass3e188602023-01-17 10:47:55 -0700231 int ret, arch, size;
Simon Glasse890e8c2023-01-17 10:47:57 -0700232 ulong addr, fdt_addr;
Simon Glass3e188602023-01-17 10:47:55 -0700233 char str[36];
Simon Glass3e188602023-01-17 10:47:55 -0700234
235 ret = get_efi_pxe_vci(str, sizeof(str));
236 if (ret)
237 return log_msg_ret("vci", ret);
238 ret = get_efi_pxe_arch();
239 if (ret < 0)
240 return log_msg_ret("arc", ret);
241 arch = ret;
242
243 ret = env_set("bootp_vci", str);
244 if (ret)
245 return log_msg_ret("vcs", ret);
Shantur Rathorec5440a62023-11-19 16:54:58 +0000246 ret = env_set_hex("bootp_arch", arch);
Simon Glass3e188602023-01-17 10:47:55 -0700247 if (ret)
248 return log_msg_ret("ars", ret);
249
250 /* figure out the load address */
251 addr_str = env_get("kernel_addr_r");
252 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
253
254 /* clear any previous bootfile */
255 env_set("bootfile", NULL);
256
257 /* read the kernel */
258 ret = dhcp_run(addr, NULL, true);
259 if (ret)
260 return log_msg_ret("dhc", ret);
261
262 size = env_get_hex("filesize", -1);
263 if (size <= 0)
264 return log_msg_ret("sz", -EINVAL);
265 bflow->size = size;
266
Simon Glass6b387c92024-07-17 09:31:03 +0100267 /* bootfile should be setup by dhcp */
Shantur Rathoree31317e2023-11-19 16:54:59 +0000268 bootfile_name = env_get("bootfile");
269 if (!bootfile_name)
270 return log_msg_ret("bootfile_name", ret);
271 bflow->fname = strdup(bootfile_name);
272
Simon Glass3e188602023-01-17 10:47:55 -0700273 /* do the hideous EFI hack */
274 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
275 bflow->size);
276
Simon Glasse890e8c2023-01-17 10:47:57 -0700277 /* read the DT file also */
278 fdt_addr_str = env_get("fdt_addr_r");
279 if (!fdt_addr_str)
280 return log_msg_ret("fdt", -EINVAL);
281 fdt_addr = hextoul(fdt_addr_str, NULL);
282 sprintf(file_addr, "%lx", fdt_addr);
283
Simon Glass47dd6b42023-02-22 12:17:04 -0700284 /* We only allow the first prefix with PXE */
Heinrich Schuchardtc9463852024-04-26 16:13:18 +0200285 ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
Simon Glass47dd6b42023-02-22 12:17:04 -0700286 if (ret)
287 return log_msg_ret("nam", ret);
288
Simon Glasse890e8c2023-01-17 10:47:57 -0700289 bflow->fdt_fname = strdup(fname);
290 if (!bflow->fdt_fname)
291 return log_msg_ret("fil", -ENOMEM);
292
293 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
294 bflow->fdt_size = env_get_hex("filesize", 0);
295 bflow->fdt_addr = fdt_addr;
296 } else {
297 log_debug("No device tree available\n");
Shantur Rathore184fc032023-11-19 16:55:00 +0000298 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
Simon Glasse890e8c2023-01-17 10:47:57 -0700299 }
300
Simon Glass3e188602023-01-17 10:47:55 -0700301 bflow->state = BOOTFLOWST_READY;
302
303 return 0;
304}
305
306static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
307{
Simon Glass3e188602023-01-17 10:47:55 -0700308 int ret;
309
Shantur Rathore37503b02023-11-19 16:55:01 +0000310 /*
311 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
312 * set flag to avoid freeing static buffer.
313 */
314 bflow->flags |= BOOTFLOWF_STATIC_BUF;
315
Simon Glass146242c2023-07-26 21:01:22 -0600316 if (bootmeth_uses_network(bflow)) {
Simon Glass3e188602023-01-17 10:47:55 -0700317 /* we only support reading from one device, so ignore 'dev' */
318 ret = distro_efi_read_bootflow_net(bflow);
319 if (ret)
320 return log_msg_ret("net", ret);
321 } else {
Simon Glass6a8c2f92023-07-26 21:01:23 -0600322 ret = distro_efi_try_bootflow_files(dev, bflow);
Simon Glass3e188602023-01-17 10:47:55 -0700323 if (ret)
324 return log_msg_ret("blk", ret);
325 }
326
327 return 0;
328}
329
Bin Meng60727032023-08-03 17:30:05 +0800330static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600331{
Simon Glasse890e8c2023-01-17 10:47:57 -0700332 ulong kernel, fdt;
Simon Glass6a8c2f92023-07-26 21:01:23 -0600333 int ret;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600334
Simon Glass6a8c2f92023-07-26 21:01:23 -0600335 kernel = env_get_hex("kernel_addr_r", 0);
Simon Glass146242c2023-07-26 21:01:22 -0600336 if (!bootmeth_uses_network(bflow)) {
Simon Glass6a8c2f92023-07-26 21:01:23 -0600337 ret = efiload_read_file(bflow, kernel);
338 if (ret)
339 return log_msg_ret("read", ret);
Simon Glasse890e8c2023-01-17 10:47:57 -0700340
341 /*
Shantur Rathore184fc032023-11-19 16:55:00 +0000342 * use the provided device tree if not using the built-in fdt
Simon Glasse890e8c2023-01-17 10:47:57 -0700343 */
Shantur Rathore184fc032023-11-19 16:55:00 +0000344 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
Simon Glasse890e8c2023-01-17 10:47:57 -0700345 fdt = bflow->fdt_addr;
Shantur Rathore184fc032023-11-19 16:55:00 +0000346
Simon Glasse890e8c2023-01-17 10:47:57 -0700347 } else {
348 /*
349 * This doesn't actually work for network devices:
350 *
351 * do_bootefi_image() No UEFI binary known at 0x02080000
352 *
353 * But this is the same behaviour for distro boot, so it can be
354 * fixed here.
355 */
Simon Glasse890e8c2023-01-17 10:47:57 -0700356 fdt = env_get_hex("fdt_addr_r", 0);
357 }
358
Shantur Rathore184fc032023-11-19 16:55:00 +0000359 if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) {
360 log_debug("Booting with built-in fdt\n");
Heinrich Schuchardt4b151562023-12-22 16:01:56 +0100361 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini1373ffd2023-12-18 08:31:50 -0500362 EFI_FDT_USE_INTERNAL))
363 return log_msg_ret("run", -EINVAL);
Shantur Rathore184fc032023-11-19 16:55:00 +0000364 } else {
365 log_debug("Booting with external fdt\n");
Heinrich Schuchardt4b151562023-12-22 16:01:56 +0100366 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
Tom Rini1373ffd2023-12-18 08:31:50 -0500367 map_sysmem(fdt, 0)))
368 return log_msg_ret("run", -EINVAL);
Shantur Rathore184fc032023-11-19 16:55:00 +0000369 }
Simon Glassacfa9bd2022-04-24 23:31:17 -0600370
371 return 0;
372}
373
374static int distro_bootmeth_efi_bind(struct udevice *dev)
375{
376 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
377
378 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
379 "EFI boot from an .efi file" : "EFI";
380
381 return 0;
382}
383
384static struct bootmeth_ops distro_efi_bootmeth_ops = {
385 .check = distro_efi_check,
386 .read_bootflow = distro_efi_read_bootflow,
387 .read_file = bootmeth_common_read_file,
388 .boot = distro_efi_boot,
389};
390
391static const struct udevice_id distro_efi_bootmeth_ids[] = {
392 { .compatible = "u-boot,distro-efi" },
393 { }
394};
395
Simon Glasscfed2212024-07-17 09:30:59 +0100396/* Put a number before 'efi' to provide a default ordering */
Heinrich Schuchardt08c51a72024-04-03 20:34:15 +0200397U_BOOT_DRIVER(bootmeth_4efi) = {
Simon Glassacfa9bd2022-04-24 23:31:17 -0600398 .name = "bootmeth_efi",
399 .id = UCLASS_BOOTMETH,
400 .of_match = distro_efi_bootmeth_ids,
401 .ops = &distro_efi_bootmeth_ops,
402 .bind = distro_bootmeth_efi_bind,
403};