Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 1 | // 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 Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 22 | #include <net.h> |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 23 | #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 | */ |
| 34 | static 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 Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 63 | static 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 | |
| 84 | static 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 Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 97 | static 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 | |
| 137 | static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) |
| 138 | { |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 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); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 142 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame^] | 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); |
| 146 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 150 | static void distro_efi_get_fdt_name(char *fname, int size) |
| 151 | { |
| 152 | const char *fdt_fname; |
| 153 | |
| 154 | fdt_fname = env_get("fdtfile"); |
| 155 | if (fdt_fname) { |
| 156 | snprintf(fname, size, "dtb/%s", fdt_fname); |
| 157 | log_debug("Using device tree: %s\n", fname); |
| 158 | } else { |
| 159 | const char *soc = env_get("soc"); |
| 160 | const char *board = env_get("board"); |
| 161 | const char *boardver = env_get("boardver"); |
| 162 | |
| 163 | /* cf the code in label_boot() which seems very complex */ |
| 164 | snprintf(fname, size, "dtb/%s%s%s%s.dtb", |
| 165 | soc ? soc : "", soc ? "-" : "", board ? board : "", |
| 166 | boardver ? boardver : ""); |
| 167 | log_debug("Using default device tree: %s\n", fname); |
| 168 | } |
| 169 | } |
| 170 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 171 | static int distro_efi_read_bootflow_file(struct udevice *dev, |
| 172 | struct bootflow *bflow) |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 173 | { |
| 174 | struct blk_desc *desc = NULL; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 175 | ulong fdt_addr, size; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 176 | char fname[256]; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 177 | int ret; |
| 178 | |
| 179 | /* We require a partition table */ |
| 180 | if (!bflow->part) |
| 181 | return -ENOENT; |
| 182 | |
| 183 | strcpy(fname, EFI_DIRNAME); |
| 184 | ret = get_efi_leafname(fname + strlen(fname), |
| 185 | sizeof(fname) - strlen(fname)); |
| 186 | if (ret) |
| 187 | return log_msg_ret("leaf", ret); |
| 188 | |
| 189 | if (bflow->blk) |
| 190 | desc = dev_get_uclass_plat(bflow->blk); |
| 191 | ret = bootmeth_try_file(bflow, desc, NULL, fname); |
| 192 | if (ret) |
| 193 | return log_msg_ret("try", ret); |
| 194 | |
| 195 | ret = efiload_read_file(desc, bflow); |
| 196 | if (ret) |
| 197 | return log_msg_ret("read", -EINVAL); |
| 198 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 199 | distro_efi_get_fdt_name(fname, sizeof(fname)); |
| 200 | bflow->fdt_fname = strdup(fname); |
| 201 | if (!bflow->fdt_fname) |
| 202 | return log_msg_ret("fil", -ENOMEM); |
| 203 | |
| 204 | fdt_addr = env_get_hex("fdt_addr_r", 0); |
| 205 | ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr, &size); |
| 206 | if (!ret) { |
| 207 | bflow->fdt_size = size; |
| 208 | bflow->fdt_addr = fdt_addr; |
| 209 | |
| 210 | /* |
| 211 | * TODO: Apply extension overlay |
| 212 | * |
| 213 | * Here we need to load and apply the extension overlay. This is |
| 214 | * not implemented. See do_extension_apply(). The extension |
| 215 | * stuff needs an implementation in boot/extension.c so it is |
| 216 | * separate from the command code. Really the extension stuff |
| 217 | * should use the device tree and a uclass / driver interface |
| 218 | * rather than implementing its own list |
| 219 | */ |
| 220 | } else { |
| 221 | log_debug("No device tree available\n"); |
| 222 | } |
| 223 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 227 | static int distro_efi_read_bootflow_net(struct bootflow *bflow) |
| 228 | { |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 229 | char file_addr[17], fname[256]; |
| 230 | char *tftp_argv[] = {"tftp", file_addr, fname, NULL}; |
| 231 | struct cmd_tbl cmdtp = {}; /* dummy */ |
| 232 | const char *addr_str, *fdt_addr_str; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 233 | int ret, arch, size; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 234 | ulong addr, fdt_addr; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 235 | char str[36]; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 236 | |
| 237 | ret = get_efi_pxe_vci(str, sizeof(str)); |
| 238 | if (ret) |
| 239 | return log_msg_ret("vci", ret); |
| 240 | ret = get_efi_pxe_arch(); |
| 241 | if (ret < 0) |
| 242 | return log_msg_ret("arc", ret); |
| 243 | arch = ret; |
| 244 | |
| 245 | ret = env_set("bootp_vci", str); |
| 246 | if (ret) |
| 247 | return log_msg_ret("vcs", ret); |
| 248 | ret = env_set_ulong("bootp_arch", arch); |
| 249 | if (ret) |
| 250 | return log_msg_ret("ars", ret); |
| 251 | |
| 252 | /* figure out the load address */ |
| 253 | addr_str = env_get("kernel_addr_r"); |
| 254 | addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; |
| 255 | |
| 256 | /* clear any previous bootfile */ |
| 257 | env_set("bootfile", NULL); |
| 258 | |
| 259 | /* read the kernel */ |
| 260 | ret = dhcp_run(addr, NULL, true); |
| 261 | if (ret) |
| 262 | return log_msg_ret("dhc", ret); |
| 263 | |
| 264 | size = env_get_hex("filesize", -1); |
| 265 | if (size <= 0) |
| 266 | return log_msg_ret("sz", -EINVAL); |
| 267 | bflow->size = size; |
| 268 | |
| 269 | /* do the hideous EFI hack */ |
| 270 | efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0), |
| 271 | bflow->size); |
| 272 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 273 | /* read the DT file also */ |
| 274 | fdt_addr_str = env_get("fdt_addr_r"); |
| 275 | if (!fdt_addr_str) |
| 276 | return log_msg_ret("fdt", -EINVAL); |
| 277 | fdt_addr = hextoul(fdt_addr_str, NULL); |
| 278 | sprintf(file_addr, "%lx", fdt_addr); |
| 279 | |
| 280 | distro_efi_get_fdt_name(fname, sizeof(fname)); |
| 281 | bflow->fdt_fname = strdup(fname); |
| 282 | if (!bflow->fdt_fname) |
| 283 | return log_msg_ret("fil", -ENOMEM); |
| 284 | |
| 285 | if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) { |
| 286 | bflow->fdt_size = env_get_hex("filesize", 0); |
| 287 | bflow->fdt_addr = fdt_addr; |
| 288 | } else { |
| 289 | log_debug("No device tree available\n"); |
| 290 | } |
| 291 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 292 | bflow->state = BOOTFLOWST_READY; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 298 | { |
| 299 | const struct udevice *media = dev_get_parent(bflow->dev); |
| 300 | int ret; |
| 301 | |
| 302 | if (IS_ENABLED(CONFIG_CMD_DHCP) && |
| 303 | device_get_uclass_id(media) == UCLASS_ETH) { |
| 304 | /* we only support reading from one device, so ignore 'dev' */ |
| 305 | ret = distro_efi_read_bootflow_net(bflow); |
| 306 | if (ret) |
| 307 | return log_msg_ret("net", ret); |
| 308 | } else { |
| 309 | ret = distro_efi_read_bootflow_file(dev, bflow); |
| 310 | if (ret) |
| 311 | return log_msg_ret("blk", ret); |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 317 | int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) |
| 318 | { |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 319 | ulong kernel, fdt; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 320 | char cmd[50]; |
| 321 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 322 | /* A non-zero buffer indicates the kernel is there */ |
| 323 | if (bflow->buf) { |
| 324 | kernel = (ulong)map_to_sysmem(bflow->buf); |
| 325 | |
| 326 | /* |
| 327 | * use the provided device tree if available, else fall back to |
| 328 | * the control FDT |
| 329 | */ |
| 330 | if (bflow->fdt_fname) |
| 331 | fdt = bflow->fdt_addr; |
| 332 | else |
| 333 | fdt = (ulong)map_to_sysmem(gd->fdt_blob); |
| 334 | } else { |
| 335 | /* |
| 336 | * This doesn't actually work for network devices: |
| 337 | * |
| 338 | * do_bootefi_image() No UEFI binary known at 0x02080000 |
| 339 | * |
| 340 | * But this is the same behaviour for distro boot, so it can be |
| 341 | * fixed here. |
| 342 | */ |
| 343 | kernel = env_get_hex("kernel_addr_r", 0); |
| 344 | fdt = env_get_hex("fdt_addr_r", 0); |
| 345 | } |
| 346 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 347 | /* |
| 348 | * At some point we can add a real interface to bootefi so we can call |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 349 | * this directly. For now, go through the CLI, like distro boot. |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 350 | */ |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 351 | snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 352 | if (run_command(cmd, 0)) |
| 353 | return log_msg_ret("run", -EINVAL); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int distro_bootmeth_efi_bind(struct udevice *dev) |
| 359 | { |
| 360 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 361 | |
| 362 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 363 | "EFI boot from an .efi file" : "EFI"; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static struct bootmeth_ops distro_efi_bootmeth_ops = { |
| 369 | .check = distro_efi_check, |
| 370 | .read_bootflow = distro_efi_read_bootflow, |
| 371 | .read_file = bootmeth_common_read_file, |
| 372 | .boot = distro_efi_boot, |
| 373 | }; |
| 374 | |
| 375 | static const struct udevice_id distro_efi_bootmeth_ids[] = { |
| 376 | { .compatible = "u-boot,distro-efi" }, |
| 377 | { } |
| 378 | }; |
| 379 | |
| 380 | U_BOOT_DRIVER(bootmeth_efi) = { |
| 381 | .name = "bootmeth_efi", |
| 382 | .id = UCLASS_BOOTMETH, |
| 383 | .of_match = distro_efi_bootmeth_ids, |
| 384 | .ops = &distro_efi_bootmeth_ops, |
| 385 | .bind = distro_bootmeth_efi_bind, |
| 386 | }; |