blob: 6a97ac02ff5ccc1fb60bafca254388d3f322bf34 [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 Glassacfa9bd2022-04-24 23:31:17 -060097static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow)
98{
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;
105 int ret;
106
107 ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000);
108 if (ret)
109 return log_msg_ret("read", ret);
110
111 /*
112 * This is a horrible hack to tell EFI about this boot device. Once we
113 * unify EFI with the rest of U-Boot we can clean this up. The same hack
114 * exists in multiple places, e.g. in the fs, tftp and load commands.
115 *
116 * Once we can clean up the EFI code to make proper use of driver model,
117 * this can go away.
118 */
119 media_dev = dev_get_parent(bflow->dev);
120 snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev));
121
122 strlcpy(dirname, bflow->fname, sizeof(dirname));
123 last_slash = strrchr(dirname, '/');
124 if (last_slash)
125 *last_slash = '\0';
126
127 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
128 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
129 bflow->buf, size);
130 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
131 "usb" : dev_get_uclass_name(media_dev);
132 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
133
134 return 0;
135}
136
137static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
138{
Simon Glass3e188602023-01-17 10:47:55 -0700139 /* This only works on block and network devices */
140 if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter))
141 return log_msg_ret("blk", -ENOTSUPP);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600142
Simon Glassd9f48572023-01-17 10:48:05 -0700143 /* This works on block devices and network devices */
144 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
145 return log_msg_ret("pxe", -ENOTSUPP);
146
Simon Glassacfa9bd2022-04-24 23:31:17 -0600147 return 0;
148}
149
Simon Glass47dd6b42023-02-22 12:17:04 -0700150/**
151 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
152 *
153 * @fname: Place to put filename
154 * @size: Max size of filename
155 * @seq: Sequence number, to cycle through options (0=first)
156 * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
157 * -EINVAL if there are no more options, -EALREADY if the control FDT should be
158 * used
159 */
160static int distro_efi_get_fdt_name(char *fname, int size, int seq)
Simon Glasse890e8c2023-01-17 10:47:57 -0700161{
162 const char *fdt_fname;
Simon Glass47dd6b42023-02-22 12:17:04 -0700163 const char *prefix;
164
165 /* select the prefix */
166 switch (seq) {
167 case 0:
168 /* this is the default */
169 prefix = "/dtb";
170 break;
171 case 1:
172 prefix = "";
173 break;
174 case 2:
175 prefix = "/dtb/current";
176 break;
177 default:
178 return log_msg_ret("pref", -EINVAL);
179 }
Simon Glasse890e8c2023-01-17 10:47:57 -0700180
181 fdt_fname = env_get("fdtfile");
182 if (fdt_fname) {
Simon Glass47dd6b42023-02-22 12:17:04 -0700183 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
Simon Glasse890e8c2023-01-17 10:47:57 -0700184 log_debug("Using device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700185 } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
186 strcpy(fname, "<prior>");
187 return log_msg_ret("pref", -EALREADY);
188 /* Use this fallback only for 32-bit ARM */
189 } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
Simon Glasse890e8c2023-01-17 10:47:57 -0700190 const char *soc = env_get("soc");
191 const char *board = env_get("board");
192 const char *boardver = env_get("boardver");
193
194 /* cf the code in label_boot() which seems very complex */
Simon Glass47dd6b42023-02-22 12:17:04 -0700195 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
Simon Glasse890e8c2023-01-17 10:47:57 -0700196 soc ? soc : "", soc ? "-" : "", board ? board : "",
197 boardver ? boardver : "");
198 log_debug("Using default device tree: %s\n", fname);
Simon Glass47dd6b42023-02-22 12:17:04 -0700199 } else {
200 return log_msg_ret("env", -ENOENT);
Simon Glasse890e8c2023-01-17 10:47:57 -0700201 }
Simon Glass47dd6b42023-02-22 12:17:04 -0700202
203 return 0;
Simon Glasse890e8c2023-01-17 10:47:57 -0700204}
205
Simon Glass3e188602023-01-17 10:47:55 -0700206static int distro_efi_read_bootflow_file(struct udevice *dev,
207 struct bootflow *bflow)
Simon Glassacfa9bd2022-04-24 23:31:17 -0600208{
209 struct blk_desc *desc = NULL;
Simon Glasse890e8c2023-01-17 10:47:57 -0700210 ulong fdt_addr, size;
Simon Glass3e188602023-01-17 10:47:55 -0700211 char fname[256];
Simon Glass47dd6b42023-02-22 12:17:04 -0700212 int ret, seq;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600213
214 /* We require a partition table */
215 if (!bflow->part)
216 return -ENOENT;
217
218 strcpy(fname, EFI_DIRNAME);
219 ret = get_efi_leafname(fname + strlen(fname),
220 sizeof(fname) - strlen(fname));
221 if (ret)
222 return log_msg_ret("leaf", ret);
223
224 if (bflow->blk)
225 desc = dev_get_uclass_plat(bflow->blk);
226 ret = bootmeth_try_file(bflow, desc, NULL, fname);
227 if (ret)
228 return log_msg_ret("try", ret);
229
230 ret = efiload_read_file(desc, bflow);
231 if (ret)
232 return log_msg_ret("read", -EINVAL);
233
Simon Glass47dd6b42023-02-22 12:17:04 -0700234 fdt_addr = env_get_hex("fdt_addr_r", 0);
235
236 /* try the various available names */
237 ret = -ENOENT;
238 for (seq = 0; ret; seq++) {
239 ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
240 if (ret == -EALREADY) {
241 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
242 break;
243 }
244 if (ret)
245 return log_msg_ret("nam", ret);
246 ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr,
247 &size);
248 }
249
Simon Glasse890e8c2023-01-17 10:47:57 -0700250 bflow->fdt_fname = strdup(fname);
251 if (!bflow->fdt_fname)
252 return log_msg_ret("fil", -ENOMEM);
253
Simon Glasse890e8c2023-01-17 10:47:57 -0700254 if (!ret) {
255 bflow->fdt_size = size;
256 bflow->fdt_addr = fdt_addr;
257
258 /*
259 * TODO: Apply extension overlay
260 *
261 * Here we need to load and apply the extension overlay. This is
262 * not implemented. See do_extension_apply(). The extension
263 * stuff needs an implementation in boot/extension.c so it is
264 * separate from the command code. Really the extension stuff
265 * should use the device tree and a uclass / driver interface
266 * rather than implementing its own list
267 */
268 } else {
269 log_debug("No device tree available\n");
270 }
271
Simon Glassacfa9bd2022-04-24 23:31:17 -0600272 return 0;
273}
274
Simon Glass3e188602023-01-17 10:47:55 -0700275static int distro_efi_read_bootflow_net(struct bootflow *bflow)
276{
Simon Glasse890e8c2023-01-17 10:47:57 -0700277 char file_addr[17], fname[256];
278 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
279 struct cmd_tbl cmdtp = {}; /* dummy */
280 const char *addr_str, *fdt_addr_str;
Simon Glass3e188602023-01-17 10:47:55 -0700281 int ret, arch, size;
Simon Glasse890e8c2023-01-17 10:47:57 -0700282 ulong addr, fdt_addr;
Simon Glass3e188602023-01-17 10:47:55 -0700283 char str[36];
Simon Glass3e188602023-01-17 10:47:55 -0700284
285 ret = get_efi_pxe_vci(str, sizeof(str));
286 if (ret)
287 return log_msg_ret("vci", ret);
288 ret = get_efi_pxe_arch();
289 if (ret < 0)
290 return log_msg_ret("arc", ret);
291 arch = ret;
292
293 ret = env_set("bootp_vci", str);
294 if (ret)
295 return log_msg_ret("vcs", ret);
296 ret = env_set_ulong("bootp_arch", arch);
297 if (ret)
298 return log_msg_ret("ars", ret);
299
300 /* figure out the load address */
301 addr_str = env_get("kernel_addr_r");
302 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
303
304 /* clear any previous bootfile */
305 env_set("bootfile", NULL);
306
307 /* read the kernel */
308 ret = dhcp_run(addr, NULL, true);
309 if (ret)
310 return log_msg_ret("dhc", ret);
311
312 size = env_get_hex("filesize", -1);
313 if (size <= 0)
314 return log_msg_ret("sz", -EINVAL);
315 bflow->size = size;
316
317 /* do the hideous EFI hack */
318 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
319 bflow->size);
320
Simon Glasse890e8c2023-01-17 10:47:57 -0700321 /* read the DT file also */
322 fdt_addr_str = env_get("fdt_addr_r");
323 if (!fdt_addr_str)
324 return log_msg_ret("fdt", -EINVAL);
325 fdt_addr = hextoul(fdt_addr_str, NULL);
326 sprintf(file_addr, "%lx", fdt_addr);
327
Simon Glass47dd6b42023-02-22 12:17:04 -0700328 /* We only allow the first prefix with PXE */
329 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
330 if (ret)
331 return log_msg_ret("nam", ret);
332
Simon Glasse890e8c2023-01-17 10:47:57 -0700333 bflow->fdt_fname = strdup(fname);
334 if (!bflow->fdt_fname)
335 return log_msg_ret("fil", -ENOMEM);
336
337 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
338 bflow->fdt_size = env_get_hex("filesize", 0);
339 bflow->fdt_addr = fdt_addr;
340 } else {
341 log_debug("No device tree available\n");
342 }
343
Simon Glass3e188602023-01-17 10:47:55 -0700344 bflow->state = BOOTFLOWST_READY;
345
346 return 0;
347}
348
349static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
350{
351 const struct udevice *media = dev_get_parent(bflow->dev);
352 int ret;
353
354 if (IS_ENABLED(CONFIG_CMD_DHCP) &&
355 device_get_uclass_id(media) == UCLASS_ETH) {
356 /* we only support reading from one device, so ignore 'dev' */
357 ret = distro_efi_read_bootflow_net(bflow);
358 if (ret)
359 return log_msg_ret("net", ret);
360 } else {
361 ret = distro_efi_read_bootflow_file(dev, bflow);
362 if (ret)
363 return log_msg_ret("blk", ret);
364 }
365
366 return 0;
367}
368
Simon Glassacfa9bd2022-04-24 23:31:17 -0600369int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
370{
Simon Glasse890e8c2023-01-17 10:47:57 -0700371 ulong kernel, fdt;
Simon Glassacfa9bd2022-04-24 23:31:17 -0600372 char cmd[50];
373
Simon Glasse890e8c2023-01-17 10:47:57 -0700374 /* A non-zero buffer indicates the kernel is there */
375 if (bflow->buf) {
376 kernel = (ulong)map_to_sysmem(bflow->buf);
377
378 /*
379 * use the provided device tree if available, else fall back to
380 * the control FDT
381 */
382 if (bflow->fdt_fname)
383 fdt = bflow->fdt_addr;
384 else
385 fdt = (ulong)map_to_sysmem(gd->fdt_blob);
386 } else {
387 /*
388 * This doesn't actually work for network devices:
389 *
390 * do_bootefi_image() No UEFI binary known at 0x02080000
391 *
392 * But this is the same behaviour for distro boot, so it can be
393 * fixed here.
394 */
395 kernel = env_get_hex("kernel_addr_r", 0);
396 fdt = env_get_hex("fdt_addr_r", 0);
397 }
398
Simon Glassacfa9bd2022-04-24 23:31:17 -0600399 /*
400 * At some point we can add a real interface to bootefi so we can call
Simon Glasse890e8c2023-01-17 10:47:57 -0700401 * this directly. For now, go through the CLI, like distro boot.
Simon Glassacfa9bd2022-04-24 23:31:17 -0600402 */
Simon Glasse890e8c2023-01-17 10:47:57 -0700403 snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
Simon Glassacfa9bd2022-04-24 23:31:17 -0600404 if (run_command(cmd, 0))
405 return log_msg_ret("run", -EINVAL);
406
407 return 0;
408}
409
410static int distro_bootmeth_efi_bind(struct udevice *dev)
411{
412 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
413
414 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
415 "EFI boot from an .efi file" : "EFI";
416
417 return 0;
418}
419
420static struct bootmeth_ops distro_efi_bootmeth_ops = {
421 .check = distro_efi_check,
422 .read_bootflow = distro_efi_read_bootflow,
423 .read_file = bootmeth_common_read_file,
424 .boot = distro_efi_boot,
425};
426
427static const struct udevice_id distro_efi_bootmeth_ids[] = {
428 { .compatible = "u-boot,distro-efi" },
429 { }
430};
431
432U_BOOT_DRIVER(bootmeth_efi) = {
433 .name = "bootmeth_efi",
434 .id = UCLASS_BOOTMETH,
435 .of_match = distro_efi_bootmeth_ids,
436 .ops = &distro_efi_bootmeth_ops,
437 .bind = distro_bootmeth_efi_bind,
438};