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);
146 static void distro_efi_get_fdt_name(char *fname, int size)
148 const char *fdt_fname;
150 fdt_fname = env_get("fdtfile");
152 snprintf(fname, size, "dtb/%s", fdt_fname);
153 log_debug("Using device tree: %s\n", fname);
155 const char *soc = env_get("soc");
156 const char *board = env_get("board");
157 const char *boardver = env_get("boardver");
159 /* cf the code in label_boot() which seems very complex */
160 snprintf(fname, size, "dtb/%s%s%s%s.dtb",
161 soc ? soc : "", soc ? "-" : "", board ? board : "",
162 boardver ? boardver : "");
163 log_debug("Using default device tree: %s\n", fname);
167 static int distro_efi_read_bootflow_file(struct udevice *dev,
168 struct bootflow *bflow)
170 struct blk_desc *desc = NULL;
171 ulong fdt_addr, size;
175 /* We require a partition table */
179 strcpy(fname, EFI_DIRNAME);
180 ret = get_efi_leafname(fname + strlen(fname),
181 sizeof(fname) - strlen(fname));
183 return log_msg_ret("leaf", ret);
186 desc = dev_get_uclass_plat(bflow->blk);
187 ret = bootmeth_try_file(bflow, desc, NULL, fname);
189 return log_msg_ret("try", ret);
191 ret = efiload_read_file(desc, bflow);
193 return log_msg_ret("read", -EINVAL);
195 distro_efi_get_fdt_name(fname, sizeof(fname));
196 bflow->fdt_fname = strdup(fname);
197 if (!bflow->fdt_fname)
198 return log_msg_ret("fil", -ENOMEM);
200 fdt_addr = env_get_hex("fdt_addr_r", 0);
201 ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr, &size);
203 bflow->fdt_size = size;
204 bflow->fdt_addr = fdt_addr;
207 * TODO: Apply extension overlay
209 * Here we need to load and apply the extension overlay. This is
210 * not implemented. See do_extension_apply(). The extension
211 * stuff needs an implementation in boot/extension.c so it is
212 * separate from the command code. Really the extension stuff
213 * should use the device tree and a uclass / driver interface
214 * rather than implementing its own list
217 log_debug("No device tree available\n");
223 static int distro_efi_read_bootflow_net(struct bootflow *bflow)
225 char file_addr[17], fname[256];
226 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
227 struct cmd_tbl cmdtp = {}; /* dummy */
228 const char *addr_str, *fdt_addr_str;
230 ulong addr, fdt_addr;
233 ret = get_efi_pxe_vci(str, sizeof(str));
235 return log_msg_ret("vci", ret);
236 ret = get_efi_pxe_arch();
238 return log_msg_ret("arc", ret);
241 ret = env_set("bootp_vci", str);
243 return log_msg_ret("vcs", ret);
244 ret = env_set_ulong("bootp_arch", arch);
246 return log_msg_ret("ars", ret);
248 /* figure out the load address */
249 addr_str = env_get("kernel_addr_r");
250 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
252 /* clear any previous bootfile */
253 env_set("bootfile", NULL);
255 /* read the kernel */
256 ret = dhcp_run(addr, NULL, true);
258 return log_msg_ret("dhc", ret);
260 size = env_get_hex("filesize", -1);
262 return log_msg_ret("sz", -EINVAL);
265 /* do the hideous EFI hack */
266 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
269 /* read the DT file also */
270 fdt_addr_str = env_get("fdt_addr_r");
272 return log_msg_ret("fdt", -EINVAL);
273 fdt_addr = hextoul(fdt_addr_str, NULL);
274 sprintf(file_addr, "%lx", fdt_addr);
276 distro_efi_get_fdt_name(fname, sizeof(fname));
277 bflow->fdt_fname = strdup(fname);
278 if (!bflow->fdt_fname)
279 return log_msg_ret("fil", -ENOMEM);
281 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
282 bflow->fdt_size = env_get_hex("filesize", 0);
283 bflow->fdt_addr = fdt_addr;
285 log_debug("No device tree available\n");
288 bflow->state = BOOTFLOWST_READY;
293 static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
295 const struct udevice *media = dev_get_parent(bflow->dev);
298 if (IS_ENABLED(CONFIG_CMD_DHCP) &&
299 device_get_uclass_id(media) == UCLASS_ETH) {
300 /* we only support reading from one device, so ignore 'dev' */
301 ret = distro_efi_read_bootflow_net(bflow);
303 return log_msg_ret("net", ret);
305 ret = distro_efi_read_bootflow_file(dev, bflow);
307 return log_msg_ret("blk", ret);
313 int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
318 /* A non-zero buffer indicates the kernel is there */
320 kernel = (ulong)map_to_sysmem(bflow->buf);
323 * use the provided device tree if available, else fall back to
326 if (bflow->fdt_fname)
327 fdt = bflow->fdt_addr;
329 fdt = (ulong)map_to_sysmem(gd->fdt_blob);
332 * This doesn't actually work for network devices:
334 * do_bootefi_image() No UEFI binary known at 0x02080000
336 * But this is the same behaviour for distro boot, so it can be
339 kernel = env_get_hex("kernel_addr_r", 0);
340 fdt = env_get_hex("fdt_addr_r", 0);
344 * At some point we can add a real interface to bootefi so we can call
345 * this directly. For now, go through the CLI, like distro boot.
347 snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt);
348 if (run_command(cmd, 0))
349 return log_msg_ret("run", -EINVAL);
354 static int distro_bootmeth_efi_bind(struct udevice *dev)
356 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
358 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
359 "EFI boot from an .efi file" : "EFI";
364 static struct bootmeth_ops distro_efi_bootmeth_ops = {
365 .check = distro_efi_check,
366 .read_bootflow = distro_efi_read_bootflow,
367 .read_file = bootmeth_common_read_file,
368 .boot = distro_efi_boot,
371 static const struct udevice_id distro_efi_bootmeth_ids[] = {
372 { .compatible = "u-boot,distro-efi" },
376 U_BOOT_DRIVER(bootmeth_efi) = {
377 .name = "bootmeth_efi",
378 .id = UCLASS_BOOTMETH,
379 .of_match = distro_efi_bootmeth_ids,
380 .ops = &distro_efi_bootmeth_ops,
381 .bind = distro_bootmeth_efi_bind,