blob: af31fbfc85db52ebc94fdedfbe83f0fc9e09dafc [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>
24
25#define EFI_DIRNAME "efi/boot/"
26
27/**
28 * get_efi_leafname() - Get the leaf name for the EFI file we expect
29 *
30 * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi".
31 * Must have at least 16 bytes of space
32 * @max_len: Length of @str, must be >=16
33 */
34static int get_efi_leafname(char *str, int max_len)
35{
36 const char *base;
37
38 if (max_len < 16)
39 return log_msg_ret("spc", -ENOSPC);
40 if (IS_ENABLED(CONFIG_ARM64))
41 base = "bootaa64";
42 else if (IS_ENABLED(CONFIG_ARM))
43 base = "bootarm";
44 else if (IS_ENABLED(CONFIG_X86_RUN_32BIT))
45 base = "bootia32";
46 else if (IS_ENABLED(CONFIG_X86_RUN_64BIT))
47 base = "bootx64";
48 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
49 base = "bootriscv32";
50 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
51 base = "bootriscv64";
52 else if (IS_ENABLED(CONFIG_SANDBOX))
53 base = "bootsbox";
54 else
55 return -EINVAL;
56
57 strcpy(str, base);
58 strcat(str, ".efi");
59
60 return 0;
61}
62
Simon Glass3e188602023-01-17 10:47:55 -070063static int get_efi_pxe_arch(void)
64{
65 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
66 if (IS_ENABLED(CONFIG_ARM64))
67 return 0xb;
68 else if (IS_ENABLED(CONFIG_ARM))
69 return 0xa;
70 else if (IS_ENABLED(CONFIG_X86_64))
71 return 0x6;
72 else if (IS_ENABLED(CONFIG_X86))
73 return 0x7;
74 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
75 return 0x19;
76 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
77 return 0x1b;
78 else if (IS_ENABLED(CONFIG_SANDBOX))
79 return 0; /* not used */
80
81 return -EINVAL;
82}
83
84static int get_efi_pxe_vci(char *str, int max_len)
85{
86 int ret;
87
88 ret = get_efi_pxe_arch();
89 if (ret < 0)
90 return ret;
91
92 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
93
94 return 0;
95}
96
Simon Glassd80bb4f2023-04-24 13:49:46 +120097static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -060098{
99 const struct udevice *media_dev;
100 int size = bflow->size;
101 const char *dev_name;
102 char devnum_str[9];
103 char dirname[200];
104 char *last_slash;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600105
106 /*
107 * This is a horrible hack to tell EFI about this boot device. Once we
108 * unify EFI with the rest of U-Boot we can clean this up. The same hack
109 * exists in multiple places, e.g. in the fs, tftp and load commands.
110 *
111 * Once we can clean up the EFI code to make proper use of driver model,
112 * this can go away.
113 */
114 media_dev = dev_get_parent(bflow->dev);
Mathew McBride484e7012023-04-24 13:49:50 +1200115 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
116 desc ? desc->devnum : dev_seq(media_dev),
Simon Glassd80bb4f2023-04-24 13:49:46 +1200117 bflow->part);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600118
119 strlcpy(dirname, bflow->fname, sizeof(dirname));
120 last_slash = strrchr(dirname, '/');
121 if (last_slash)
122 *last_slash = '\0';
123
124 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
125 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
126 bflow->buf, size);
127 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
128 "usb" : dev_get_uclass_name(media_dev);
129 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
Simon Glassd80bb4f2023-04-24 13:49:46 +1200130}
131
132static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow)
133{
134 int ret;
135
136 ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000);
137 if (ret)
138 return log_msg_ret("read", ret);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600139
140 return 0;
141}
142
143static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
144{
Simon Glass3e188602023-01-17 10:47:55 -0700145 /* This only works on block and network devices */
146 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
147 return log_msg_ret("blk", -ENOTSUPP);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600148
Simon Glassd9f48572023-01-17 10:48:05 -0700149 /* This works on block devices and network devices */
150 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
151 return log_msg_ret("pxe", -ENOTSUPP);
152
Simon Glassacfa9bd2022-04-24 23:31:17 -0600153 return 0;
154}
155
Simon Glass47dd6b42023-02-22 12:17:04 -0700156/**
157 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
158 *
159 * @fname: Place to put filename
160 * @size: Max size of filename
161 * @seq: Sequence number, to cycle through options (0=first)
162 * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
163 * -EINVAL if there are no more options, -EALREADY if the control FDT should be
164 * used
165 */
166static int distro_efi_get_fdt_name(char *fname, int size, int seq)
Simon Glasse890e8c2023-01-17 10:47:57 -0700167{
168 const char *fdt_fname;
Simon Glass47dd6b42023-02-22 12:17:04 -0700169 const char *prefix;
170
171 /* select the prefix */
172 switch (seq) {
173 case 0:
174 /* this is the default */
175 prefix = "/dtb";
176 break;
177 case 1:
178 prefix = "";
179 break;
180 case 2:
181 prefix = "/dtb/current";
182 break;
183 default:
184 return log_msg_ret("pref", -EINVAL);
185 }
Simon Glasse890e8c2023-01-17 10:47:57 -0700186
187 fdt_fname = env_get("fdtfile");
188 if (fdt_fname) {
Simon Glass47dd6b42023-02-22 12:17:04 -0700189 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
Simon Glasse890e8c2023-01-17 10:47:57 -0700190 log_debug("Using device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700191 } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
192 strcpy(fname, "<prior>");
193 return log_msg_ret("pref", -EALREADY);
194 /* Use this fallback only for 32-bit ARM */
195 } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
Simon Glasse890e8c2023-01-17 10:47:57 -0700196 const char *soc = env_get("soc");
197 const char *board = env_get("board");
198 const char *boardver = env_get("boardver");
199
200 /* cf the code in label_boot() which seems very complex */
Simon Glass47dd6b42023-02-22 12:17:04 -0700201 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
Simon Glasse890e8c2023-01-17 10:47:57 -0700202 soc ? soc : "", soc ? "-" : "", board ? board : "",
203 boardver ? boardver : "");
204 log_debug("Using default device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700205 } else {
206 return log_msg_ret("env", -ENOENT);
Simon Glasse890e8c2023-01-17 10:47:57 -0700207 }
Simon Glass47dd6b42023-02-22 12:17:04 -0700208
209 return 0;
Simon Glasse890e8c2023-01-17 10:47:57 -0700210}
211
Simon Glass3e188602023-01-17 10:47:55 -0700212static int distro_efi_read_bootflow_file(struct udevice *dev,
213 struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600214{
215 struct blk_desc *desc = NULL;
Simon Glasse890e8c2023-01-17 10:47:57 -0700216 ulong fdt_addr, size;
Simon Glass3e188602023-01-17 10:47:55 -0700217 char fname[256];
Simon Glass47dd6b42023-02-22 12:17:04 -0700218 int ret, seq;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600219
220 /* We require a partition table */
221 if (!bflow->part)
222 return -ENOENT;
223
224 strcpy(fname, EFI_DIRNAME);
225 ret = get_efi_leafname(fname + strlen(fname),
226 sizeof(fname) - strlen(fname));
227 if (ret)
228 return log_msg_ret("leaf", ret);
229
230 if (bflow->blk)
231 desc = dev_get_uclass_plat(bflow->blk);
232 ret = bootmeth_try_file(bflow, desc, NULL, fname);
233 if (ret)
234 return log_msg_ret("try", ret);
235
236 ret = efiload_read_file(desc, bflow);
237 if (ret)
Simon Glassc8894342023-05-10 16:34:26 -0600238 return log_msg_ret("read", ret);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600239
Simon Glass47dd6b42023-02-22 12:17:04 -0700240 fdt_addr = env_get_hex("fdt_addr_r", 0);
241
242 /* try the various available names */
243 ret = -ENOENT;
Simon Glass9f835162023-04-24 13:49:44 +1200244 *fname = '\0';
245 for (seq = 0; ret == -ENOENT; seq++) {
Simon Glass47dd6b42023-02-22 12:17:04 -0700246 ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
Simon Glass9f835162023-04-24 13:49:44 +1200247 if (ret == -EALREADY)
Simon Glass47dd6b42023-02-22 12:17:04 -0700248 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
Simon Glass9f835162023-04-24 13:49:44 +1200249 if (!ret)
250 ret = bootmeth_common_read_file(dev, bflow, fname,
251 fdt_addr, &size);
Simon Glass47dd6b42023-02-22 12:17:04 -0700252 }
253
Simon Glass9f835162023-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 Glasse890e8c2023-01-17 10:47:57 -0700259
Simon Glasse890e8c2023-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");
276 }
277
Simon Glassacfa9bd2022-04-24 23:31:17 -0600278 return 0;
279}
280
Simon Glass3e188602023-01-17 10:47:55 -0700281static int distro_efi_read_bootflow_net(struct bootflow *bflow)
282{
Simon Glasse890e8c2023-01-17 10:47:57 -0700283 char file_addr[17], fname[256];
284 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
285 struct cmd_tbl cmdtp = {}; /* dummy */
286 const char *addr_str, *fdt_addr_str;
Simon Glass3e188602023-01-17 10:47:55 -0700287 int ret, arch, size;
Simon Glasse890e8c2023-01-17 10:47:57 -0700288 ulong addr, fdt_addr;
Simon Glass3e188602023-01-17 10:47:55 -0700289 char str[36];
Simon Glass3e188602023-01-17 10:47:55 -0700290
291 ret = get_efi_pxe_vci(str, sizeof(str));
292 if (ret)
293 return log_msg_ret("vci", ret);
294 ret = get_efi_pxe_arch();
295 if (ret < 0)
296 return log_msg_ret("arc", ret);
297 arch = ret;
298
299 ret = env_set("bootp_vci", str);
300 if (ret)
301 return log_msg_ret("vcs", ret);
302 ret = env_set_ulong("bootp_arch", arch);
303 if (ret)
304 return log_msg_ret("ars", ret);
305
306 /* figure out the load address */
307 addr_str = env_get("kernel_addr_r");
308 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
309
310 /* clear any previous bootfile */
311 env_set("bootfile", NULL);
312
313 /* read the kernel */
314 ret = dhcp_run(addr, NULL, true);
315 if (ret)
316 return log_msg_ret("dhc", ret);
317
318 size = env_get_hex("filesize", -1);
319 if (size <= 0)
320 return log_msg_ret("sz", -EINVAL);
321 bflow->size = size;
322
323 /* do the hideous EFI hack */
324 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
325 bflow->size);
326
Simon Glasse890e8c2023-01-17 10:47:57 -0700327 /* read the DT file also */
328 fdt_addr_str = env_get("fdt_addr_r");
329 if (!fdt_addr_str)
330 return log_msg_ret("fdt", -EINVAL);
331 fdt_addr = hextoul(fdt_addr_str, NULL);
332 sprintf(file_addr, "%lx", fdt_addr);
333
Simon Glass47dd6b42023-02-22 12:17:04 -0700334 /* We only allow the first prefix with PXE */
335 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
336 if (ret)
337 return log_msg_ret("nam", ret);
338
Simon Glasse890e8c2023-01-17 10:47:57 -0700339 bflow->fdt_fname = strdup(fname);
340 if (!bflow->fdt_fname)
341 return log_msg_ret("fil", -ENOMEM);
342
343 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
344 bflow->fdt_size = env_get_hex("filesize", 0);
345 bflow->fdt_addr = fdt_addr;
346 } else {
347 log_debug("No device tree available\n");
348 }
349
Simon Glass3e188602023-01-17 10:47:55 -0700350 bflow->state = BOOTFLOWST_READY;
351
352 return 0;
353}
354
355static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
356{
357 const struct udevice *media = dev_get_parent(bflow->dev);
358 int ret;
359
360 if (IS_ENABLED(CONFIG_CMD_DHCP) &&
361 device_get_uclass_id(media) == UCLASS_ETH) {
362 /* we only support reading from one device, so ignore 'dev' */
363 ret = distro_efi_read_bootflow_net(bflow);
364 if (ret)
365 return log_msg_ret("net", ret);
366 } else {
367 ret = distro_efi_read_bootflow_file(dev, bflow);
368 if (ret)
369 return log_msg_ret("blk", ret);
370 }
371
372 return 0;
373}
374
Simon Glassacfa9bd2022-04-24 23:31:17 -0600375int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
376{
Simon Glasse890e8c2023-01-17 10:47:57 -0700377 ulong kernel, fdt;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600378 char cmd[50];
379
Simon Glasse890e8c2023-01-17 10:47:57 -0700380 /* A non-zero buffer indicates the kernel is there */
381 if (bflow->buf) {
Simon Glassd80bb4f2023-04-24 13:49:46 +1200382 /* Set the EFI bootdev again, since reading an FDT loses it! */
383 if (bflow->blk) {
384 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
385
386 set_efi_bootdev(desc, bflow);
387 }
388
Simon Glasse890e8c2023-01-17 10:47:57 -0700389 kernel = (ulong)map_to_sysmem(bflow->buf);
390
391 /*
392 * use the provided device tree if available, else fall back to
393 * the control FDT
394 */
395 if (bflow->fdt_fname)
396 fdt = bflow->fdt_addr;
397 else
398 fdt = (ulong)map_to_sysmem(gd->fdt_blob);
399 } else {
400 /*
401 * This doesn't actually work for network devices:
402 *
403 * do_bootefi_image() No UEFI binary known at 0x02080000
404 *
405 * But this is the same behaviour for distro boot, so it can be
406 * fixed here.
407 */
408 kernel = env_get_hex("kernel_addr_r", 0);
409 fdt = env_get_hex("fdt_addr_r", 0);
410 }
411
Simon Glassacfa9bd2022-04-24 23:31:17 -0600412 /*
413 * At some point we can add a real interface to bootefi so we can call
Simon Glasse890e8c2023-01-17 10:47:57 -0700414 * this directly. For now, go through the CLI, like distro boot.
Simon Glassacfa9bd2022-04-24 23:31:17 -0600415 */
Simon Glasse890e8c2023-01-17 10:47:57 -0700416 snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600417 if (run_command(cmd, 0))
418 return log_msg_ret("run", -EINVAL);
419
420 return 0;
421}
422
423static int distro_bootmeth_efi_bind(struct udevice *dev)
424{
425 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
426
427 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
428 "EFI boot from an .efi file" : "EFI";
429
430 return 0;
431}
432
433static struct bootmeth_ops distro_efi_bootmeth_ops = {
434 .check = distro_efi_check,
435 .read_bootflow = distro_efi_read_bootflow,
436 .read_file = bootmeth_common_read_file,
437 .boot = distro_efi_boot,
438};
439
440static const struct udevice_id distro_efi_bootmeth_ids[] = {
441 { .compatible = "u-boot,distro-efi" },
442 { }
443};
444
445U_BOOT_DRIVER(bootmeth_efi) = {
446 .name = "bootmeth_efi",
447 .id = UCLASS_BOOTMETH,
448 .of_match = distro_efi_bootmeth_ids,
449 .ops = &distro_efi_bootmeth_ops,
450 .bind = distro_bootmeth_efi_bind,
451};