1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot via EFI
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEGORY UCLASS_BOOTSTD
17 #include <efi_loader.h>
23 #include <pxe_utils.h>
25 #define EFI_DIRNAME "efi/boot/"
28 * get_efi_leafname() - Get the leaf name for the EFI file we expect
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
34 static int get_efi_leafname(char *str, int max_len)
39 return log_msg_ret("spc", -ENOSPC);
40 if (IS_ENABLED(CONFIG_ARM64))
42 else if (IS_ENABLED(CONFIG_ARM))
44 else if (IS_ENABLED(CONFIG_X86_RUN_32BIT))
46 else if (IS_ENABLED(CONFIG_X86_RUN_64BIT))
48 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
50 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
52 else if (IS_ENABLED(CONFIG_SANDBOX))
63 static int get_efi_pxe_arch(void)
65 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
66 if (IS_ENABLED(CONFIG_ARM64))
68 else if (IS_ENABLED(CONFIG_ARM))
70 else if (IS_ENABLED(CONFIG_X86_64))
72 else if (IS_ENABLED(CONFIG_X86))
74 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
76 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
78 else if (IS_ENABLED(CONFIG_SANDBOX))
79 return 0; /* not used */
84 static int get_efi_pxe_vci(char *str, int max_len)
88 ret = get_efi_pxe_arch();
92 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
97 static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow)
99 const struct udevice *media_dev;
100 int size = bflow->size;
101 const char *dev_name;
107 ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000);
109 return log_msg_ret("read", ret);
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.
116 * Once we can clean up the EFI code to make proper use of driver model,
119 media_dev = dev_get_parent(bflow->dev);
120 snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev));
122 strlcpy(dirname, bflow->fname, sizeof(dirname));
123 last_slash = strrchr(dirname, '/');
127 log_debug("setting bootdev %s, %s, %s, %p, %x\n",
128 dev_get_uclass_name(media_dev), devnum_str, bflow->fname,
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);
137 static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
139 /* 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);
143 /* This works on block devices and network devices */
144 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
145 return log_msg_ret("pxe", -ENOTSUPP);
151 * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
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
160 static int distro_efi_get_fdt_name(char *fname, int size, int seq)
162 const char *fdt_fname;
165 /* select the prefix */
168 /* this is the default */
175 prefix = "/dtb/current";
178 return log_msg_ret("pref", -EINVAL);
181 fdt_fname = env_get("fdtfile");
183 snprintf(fname, size, "%s/%s", prefix, fdt_fname);
184 log_debug("Using device tree: %s\n", fname);
185 } 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)) {
190 const char *soc = env_get("soc");
191 const char *board = env_get("board");
192 const char *boardver = env_get("boardver");
194 /* cf the code in label_boot() which seems very complex */
195 snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
196 soc ? soc : "", soc ? "-" : "", board ? board : "",
197 boardver ? boardver : "");
198 log_debug("Using default device tree: %s\n", fname);
200 return log_msg_ret("env", -ENOENT);
206 static int distro_efi_read_bootflow_file(struct udevice *dev,
207 struct bootflow *bflow)
209 struct blk_desc *desc = NULL;
210 ulong fdt_addr, size;
214 /* We require a partition table */
218 strcpy(fname, EFI_DIRNAME);
219 ret = get_efi_leafname(fname + strlen(fname),
220 sizeof(fname) - strlen(fname));
222 return log_msg_ret("leaf", ret);
225 desc = dev_get_uclass_plat(bflow->blk);
226 ret = bootmeth_try_file(bflow, desc, NULL, fname);
228 return log_msg_ret("try", ret);
230 ret = efiload_read_file(desc, bflow);
232 return log_msg_ret("read", -EINVAL);
234 fdt_addr = env_get_hex("fdt_addr_r", 0);
236 /* try the various available names */
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;
245 return log_msg_ret("nam", ret);
246 ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr,
250 bflow->fdt_fname = strdup(fname);
251 if (!bflow->fdt_fname)
252 return log_msg_ret("fil", -ENOMEM);
255 bflow->fdt_size = size;
256 bflow->fdt_addr = fdt_addr;
259 * TODO: Apply extension overlay
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
269 log_debug("No device tree available\n");
275 static int distro_efi_read_bootflow_net(struct bootflow *bflow)
277 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;
282 ulong addr, fdt_addr;
285 ret = get_efi_pxe_vci(str, sizeof(str));
287 return log_msg_ret("vci", ret);
288 ret = get_efi_pxe_arch();
290 return log_msg_ret("arc", ret);
293 ret = env_set("bootp_vci", str);
295 return log_msg_ret("vcs", ret);
296 ret = env_set_ulong("bootp_arch", arch);
298 return log_msg_ret("ars", ret);
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;
304 /* clear any previous bootfile */
305 env_set("bootfile", NULL);
307 /* read the kernel */
308 ret = dhcp_run(addr, NULL, true);
310 return log_msg_ret("dhc", ret);
312 size = env_get_hex("filesize", -1);
314 return log_msg_ret("sz", -EINVAL);
317 /* do the hideous EFI hack */
318 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
321 /* read the DT file also */
322 fdt_addr_str = env_get("fdt_addr_r");
324 return log_msg_ret("fdt", -EINVAL);
325 fdt_addr = hextoul(fdt_addr_str, NULL);
326 sprintf(file_addr, "%lx", fdt_addr);
328 /* We only allow the first prefix with PXE */
329 ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
331 return log_msg_ret("nam", ret);
333 bflow->fdt_fname = strdup(fname);
334 if (!bflow->fdt_fname)
335 return log_msg_ret("fil", -ENOMEM);
337 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
338 bflow->fdt_size = env_get_hex("filesize", 0);
339 bflow->fdt_addr = fdt_addr;
341 log_debug("No device tree available\n");
344 bflow->state = BOOTFLOWST_READY;
349 static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
351 const struct udevice *media = dev_get_parent(bflow->dev);
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);
359 return log_msg_ret("net", ret);
361 ret = distro_efi_read_bootflow_file(dev, bflow);
363 return log_msg_ret("blk", ret);
369 int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
374 /* A non-zero buffer indicates the kernel is there */
376 kernel = (ulong)map_to_sysmem(bflow->buf);
379 * use the provided device tree if available, else fall back to
382 if (bflow->fdt_fname)
383 fdt = bflow->fdt_addr;
385 fdt = (ulong)map_to_sysmem(gd->fdt_blob);
388 * This doesn't actually work for network devices:
390 * do_bootefi_image() No UEFI binary known at 0x02080000
392 * But this is the same behaviour for distro boot, so it can be
395 kernel = env_get_hex("kernel_addr_r", 0);
396 fdt = env_get_hex("fdt_addr_r", 0);
400 * At some point we can add a real interface to bootefi so we can call
401 * this directly. For now, go through the CLI, like distro boot.
403 snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
404 if (run_command(cmd, 0))
405 return log_msg_ret("run", -EINVAL);
410 static int distro_bootmeth_efi_bind(struct udevice *dev)
412 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
414 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
415 "EFI boot from an .efi file" : "EFI";
420 static 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,
427 static const struct udevice_id distro_efi_bootmeth_ids[] = {
428 { .compatible = "u-boot,distro-efi" },
432 U_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,