1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot via EFI
5 * Copyright 2021 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
16 #include <efi_default_filename.h>
17 #include <efi_loader.h>
23 #include <pxe_utils.h>
24 #include <linux/sizes.h>
26 #define EFI_DIRNAME "/EFI/BOOT/"
28 static int get_efi_pxe_arch(void)
30 /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */
31 if (IS_ENABLED(CONFIG_ARM64))
33 else if (IS_ENABLED(CONFIG_ARM))
35 else if (IS_ENABLED(CONFIG_X86_64))
37 else if (IS_ENABLED(CONFIG_X86))
39 else if (IS_ENABLED(CONFIG_ARCH_RV32I))
41 else if (IS_ENABLED(CONFIG_ARCH_RV64I))
43 else if (IS_ENABLED(CONFIG_SANDBOX))
44 return 0; /* not used */
49 static int get_efi_pxe_vci(char *str, int max_len)
53 ret = get_efi_pxe_arch();
57 snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret);
63 * bootmeth_uses_network() - check if the media device is Ethernet
65 * @bflow: Bootflow to check
66 * Returns: true if the media device is Ethernet, else false
68 static bool bootmeth_uses_network(struct bootflow *bflow)
70 const struct udevice *media = dev_get_parent(bflow->dev);
72 return IS_ENABLED(CONFIG_CMD_DHCP) &&
73 device_get_uclass_id(media) == UCLASS_ETH;
76 static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow)
78 const struct udevice *media_dev;
79 int size = bflow->size;
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.
90 * Once we can clean up the EFI code to make proper use of driver model,
93 media_dev = dev_get_parent(bflow->dev);
94 snprintf(devnum_str, sizeof(devnum_str), "%x:%x",
95 desc ? desc->devnum : dev_seq(media_dev),
98 strlcpy(dirname, bflow->fname, sizeof(dirname));
99 last_slash = strrchr(dirname, '/');
103 dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ?
104 "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);
107 efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size);
110 static int efiload_read_file(struct bootflow *bflow, ulong addr)
112 struct blk_desc *desc = NULL;
117 desc = dev_get_uclass_plat(bflow->blk);
118 ret = bootmeth_setup_fs(bflow, desc);
120 return log_msg_ret("set", ret);
122 ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read);
124 return log_msg_ret("read", ret);
125 bflow->buf = map_sysmem(addr, bflow->size);
127 set_efi_bootdev(desc, bflow);
132 static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter)
134 /* 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);
138 /* This works on block devices and network devices */
139 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
140 return log_msg_ret("pxe", -ENOTSUPP);
146 * distro_efi_try_bootflow_files() - Check that files are present
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
153 * @dev: bootmeth device to use
154 * @bflow: bootflow to update
156 static int distro_efi_try_bootflow_files(struct udevice *dev,
157 struct bootflow *bflow)
159 struct blk_desc *desc = NULL;
160 ulong fdt_addr, size;
164 /* We require a partition table */
168 strcpy(fname, EFI_DIRNAME);
169 strcat(fname, BOOTEFI_NAME);
172 desc = dev_get_uclass_plat(bflow->blk);
173 ret = bootmeth_try_file(bflow, desc, NULL, fname);
175 return log_msg_ret("try", ret);
177 /* Since we can access the file, let's call it ready */
178 bflow->state = BOOTFLOWST_READY;
180 fdt_addr = env_get_hex("fdt_addr_r", 0);
182 /* try the various available names */
185 for (seq = 0; ret == -ENOENT; seq++) {
186 ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
187 if (ret == -EALREADY)
188 bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
190 /* Limit FDT files to 4MB */
192 ret = bootmeth_common_read_file(dev, bflow, fname,
198 bflow->fdt_fname = strdup(fname);
199 if (!bflow->fdt_fname)
200 return log_msg_ret("fil", -ENOMEM);
204 bflow->fdt_size = size;
205 bflow->fdt_addr = fdt_addr;
208 * TODO: Apply extension overlay
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
218 log_debug("No device tree available\n");
219 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
225 static int distro_efi_read_bootflow_net(struct bootflow *bflow)
227 char file_addr[17], fname[256];
228 char *tftp_argv[] = {"tftp", file_addr, fname, NULL};
229 struct cmd_tbl cmdtp = {}; /* dummy */
230 const char *addr_str, *fdt_addr_str, *bootfile_name;
232 ulong addr, fdt_addr;
235 ret = get_efi_pxe_vci(str, sizeof(str));
237 return log_msg_ret("vci", ret);
238 ret = get_efi_pxe_arch();
240 return log_msg_ret("arc", ret);
243 ret = env_set("bootp_vci", str);
245 return log_msg_ret("vcs", ret);
246 ret = env_set_hex("bootp_arch", arch);
248 return log_msg_ret("ars", ret);
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;
254 /* clear any previous bootfile */
255 env_set("bootfile", NULL);
257 /* read the kernel */
258 ret = dhcp_run(addr, NULL, true);
260 return log_msg_ret("dhc", ret);
262 size = env_get_hex("filesize", -1);
264 return log_msg_ret("sz", -EINVAL);
267 /* bootfile should be setup by dhcp */
268 bootfile_name = env_get("bootfile");
270 return log_msg_ret("bootfile_name", ret);
271 bflow->fname = strdup(bootfile_name);
273 /* do the hideous EFI hack */
274 efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0),
277 /* read the DT file also */
278 fdt_addr_str = env_get("fdt_addr_r");
280 return log_msg_ret("fdt", -EINVAL);
281 fdt_addr = hextoul(fdt_addr_str, NULL);
282 sprintf(file_addr, "%lx", fdt_addr);
284 /* We only allow the first prefix with PXE */
285 ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
287 return log_msg_ret("nam", ret);
289 bflow->fdt_fname = strdup(fname);
290 if (!bflow->fdt_fname)
291 return log_msg_ret("fil", -ENOMEM);
293 if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) {
294 bflow->fdt_size = env_get_hex("filesize", 0);
295 bflow->fdt_addr = fdt_addr;
297 log_debug("No device tree available\n");
298 bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT;
301 bflow->state = BOOTFLOWST_READY;
306 static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow)
311 * bootmeth_efi doesn't allocate any buffer neither for blk nor net device
312 * set flag to avoid freeing static buffer.
314 bflow->flags |= BOOTFLOWF_STATIC_BUF;
316 if (bootmeth_uses_network(bflow)) {
317 /* we only support reading from one device, so ignore 'dev' */
318 ret = distro_efi_read_bootflow_net(bflow);
320 return log_msg_ret("net", ret);
322 ret = distro_efi_try_bootflow_files(dev, bflow);
324 return log_msg_ret("blk", ret);
330 static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow)
335 kernel = env_get_hex("kernel_addr_r", 0);
336 if (!bootmeth_uses_network(bflow)) {
337 ret = efiload_read_file(bflow, kernel);
339 return log_msg_ret("read", ret);
342 * use the provided device tree if not using the built-in fdt
344 if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT)
345 fdt = bflow->fdt_addr;
349 * This doesn't actually work for network devices:
351 * do_bootefi_image() No UEFI binary known at 0x02080000
353 * But this is the same behaviour for distro boot, so it can be
356 fdt = env_get_hex("fdt_addr_r", 0);
359 if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) {
360 log_debug("Booting with built-in fdt\n");
361 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
362 EFI_FDT_USE_INTERNAL))
363 return log_msg_ret("run", -EINVAL);
365 log_debug("Booting with external fdt\n");
366 if (efi_binary_run(map_sysmem(kernel, 0), bflow->size,
368 return log_msg_ret("run", -EINVAL);
374 static int distro_bootmeth_efi_bind(struct udevice *dev)
376 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
378 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
379 "EFI boot from an .efi file" : "EFI";
384 static 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,
391 static const struct udevice_id distro_efi_bootmeth_ids[] = {
392 { .compatible = "u-boot,distro-efi" },
396 /* Put a number before 'efi' to provide a default ordering */
397 U_BOOT_DRIVER(bootmeth_4efi) = {
398 .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,