Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | // SPDX-License-Identifier: GPL-2.0+ /* * Bootmethod for distro boot via EFI * * Copyright 2021 Google LLC * Written by Simon Glass <sjg@chromium.org> */ #define LOG_CATEGORY UCLASS_BOOTSTD #include <bootdev.h> #include <bootflow.h> #include <bootmeth.h> #include <command.h> #include <dm.h> #include <efi.h> #include <efi_loader.h> #include <env.h> #include <extension_board.h> #include <fs.h> #include <malloc.h> #include <mapmem.h> #include <mmc.h> #include <net.h> #include <pxe_utils.h> #include <linux/sizes.h> #define EFI_DIRNAME "/EFI/BOOT/" static int get_efi_pxe_vci(char *str, int max_len) { int ret; ret = efi_get_pxe_arch(); if (ret < 0) return ret; snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret); return 0; } /** * bootmeth_uses_network() - check if the media device is Ethernet * * @bflow: Bootflow to check * Returns: true if the media device is Ethernet, else false */ static bool bootmeth_uses_network(struct bootflow *bflow) { const struct udevice *media = dev_get_parent(bflow->dev); return IS_ENABLED(CONFIG_CMD_DHCP) && device_get_uclass_id(media) == UCLASS_ETH; } static int efiload_read_file(struct bootflow *bflow, ulong addr) { struct blk_desc *desc = NULL; ulong size; int ret; if (bflow->blk) desc = dev_get_uclass_plat(bflow->blk); size = SZ_1G; ret = bootmeth_common_read_file(bflow->method, bflow, bflow->fname, addr, BFI_EFI, &size); if (ret) return log_msg_ret("rdf", ret); bflow->buf = map_sysmem(addr, bflow->size); return 0; } static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) { /* This only works on block and network devices */ if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter)) return log_msg_ret("blk", -ENOTSUPP); /* This works on block devices and network devices */ if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY) return log_msg_ret("pxe", -ENOTSUPP); return 0; } /* * distro_efi_try_bootflow_files() - Check that files are present * * This reads any FDT file and checks whether the bootflow file is present, for * later reading. We avoid reading the bootflow now, since it is likely large, * it may take a long time and we want to avoid needing to allocate memory for * it * * @dev: bootmeth device to use * @bflow: bootflow to update */ static int distro_efi_try_bootflow_files(struct udevice *dev, struct bootflow *bflow) { ulong fdt_addr, size, overlay_addr; const struct extension *extension; struct fdt_header *working_fdt; struct blk_desc *desc = NULL; struct alist *extension_list; char fname[256]; int ret, seq; /* We require a partition table */ if (!bflow->part) { log_debug("no partitions\n"); return -ENOENT; } strcpy(fname, EFI_DIRNAME); strcat(fname, efi_get_basename()); if (bflow->blk) desc = dev_get_uclass_plat(bflow->blk); ret = bootmeth_try_file(bflow, desc, NULL, fname); if (ret) { log_debug("File '%s' not found\n", fname); return log_msg_ret("try", ret); } /* Since we can access the file, let's call it ready */ bflow->state = BOOTFLOWST_READY; fdt_addr = env_get_hex("fdt_addr_r", 0); /* try the various available names */ ret = -ENOENT; *fname = '\0'; for (seq = 0; ret == -ENOENT; seq++) { ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq); if (ret == -EALREADY) bflow->flags = BOOTFLOWF_USE_PRIOR_FDT; if (!ret) { /* Limit FDT files to 4MB */ size = SZ_4M; ret = bootmeth_common_read_file(dev, bflow, fname, fdt_addr, (enum bootflow_img_t)IH_TYPE_FLATDT, &size); } } if (*fname) { bflow->fdt_fname = strdup(fname); if (!bflow->fdt_fname) return log_msg_ret("fil", -ENOMEM); } if (ret) { log_debug("No device tree available\n"); bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; return 0; } bflow->fdt_size = size; bflow->fdt_addr = fdt_addr; if (!CONFIG_IS_ENABLED(SUPPORT_EXTENSION_SCAN)) return 0; ret = extension_scan(); if (ret < 0) return 0; extension_list = extension_get_list(); if (!extension_list) return 0; working_fdt = map_sysmem(fdt_addr, 0); if (fdt_check_header(working_fdt)) return 0; overlay_addr = env_get_hex("extension_overlay_addr", 0); if (!overlay_addr) { log_debug("Environment extension_overlay_addr is missing\n"); return 0; } alist_for_each(extension, extension_list) { char *overlay_file; int len; len = sizeof(EFI_DIRNAME) + strlen(extension->overlay); overlay_file = calloc(1, len); if (!overlay_file) return -ENOMEM; snprintf(overlay_file, len, "%s%s", EFI_DIRNAME, extension->overlay); ret = bootmeth_common_read_file(dev, bflow, overlay_file, overlay_addr, (enum bootflow_img_t)IH_TYPE_FLATDT, &size); if (ret) { log_debug("Failed loading overlay %s\n", overlay_file); free(overlay_file); continue; } ret = extension_apply(working_fdt, size); if (ret) { log_debug("Failed applying overlay %s\n", overlay_file); free(overlay_file); continue; } bflow->fdt_size += size; free(overlay_file); } return 0; } static int distro_efi_read_bootflow_net(struct bootflow *bflow) { char file_addr[17], fname[256]; char *tftp_argv[] = {"tftp", file_addr, fname, NULL}; struct cmd_tbl cmdtp = {}; /* dummy */ const char *addr_str, *fdt_addr_str, *bootfile_name; int ret, arch, size; ulong addr, fdt_addr; char str[36]; ret = get_efi_pxe_vci(str, sizeof(str)); if (ret) return log_msg_ret("vci", ret); ret = efi_get_pxe_arch(); if (ret < 0) return log_msg_ret("arc", ret); arch = ret; ret = env_set("bootp_vci", str); if (ret) return log_msg_ret("vcs", ret); ret = env_set_hex("bootp_arch", arch); if (ret) return log_msg_ret("ars", ret); /* figure out the load address */ addr_str = env_get("kernel_addr_r"); addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; /* clear any previous bootfile */ env_set("bootfile", NULL); /* read the kernel */ ret = dhcp_run(addr, NULL, true); if (ret) return log_msg_ret("dhc", ret); size = env_get_hex("filesize", -1); if (size <= 0) return log_msg_ret("sz", -EINVAL); bflow->size = size; bflow->buf = map_sysmem(addr, size); /* bootfile should be setup by dhcp */ bootfile_name = env_get("bootfile"); if (!bootfile_name) return log_msg_ret("bootfile_name", ret); bflow->fname = strdup(bootfile_name); if (!bflow->fname) return log_msg_ret("fi0", -ENOMEM); /* read the DT file also */ fdt_addr_str = env_get("fdt_addr_r"); if (!fdt_addr_str) return log_msg_ret("fdt", -EINVAL); fdt_addr = hextoul(fdt_addr_str, NULL); sprintf(file_addr, "%lx", fdt_addr); /* We only allow the first prefix with PXE */ ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0); if (ret) return log_msg_ret("nam", ret); bflow->fdt_fname = strdup(fname); if (!bflow->fdt_fname) return log_msg_ret("fil", -ENOMEM); if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) { bflow->fdt_size = env_get_hex("filesize", 0); bflow->fdt_addr = fdt_addr; } else { log_debug("No device tree available\n"); bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; } bflow->state = BOOTFLOWST_READY; return 0; } static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) { int ret; log_debug("dev='%s', part=%d\n", bflow->dev->name, bflow->part); /* * bootmeth_efi doesn't allocate any buffer neither for blk nor net device * set flag to avoid freeing static buffer. */ bflow->flags |= BOOTFLOWF_STATIC_BUF; if (bootmeth_uses_network(bflow)) { /* we only support reading from one device, so ignore 'dev' */ ret = distro_efi_read_bootflow_net(bflow); if (ret) return log_msg_ret("net", ret); } else { ret = distro_efi_try_bootflow_files(dev, bflow); if (ret) return log_msg_ret("blk", ret); } return 0; } static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) { ulong kernel, fdt; int ret; log_debug("distro EFI boot\n"); kernel = env_get_hex("kernel_addr_r", 0); if (!bootmeth_uses_network(bflow)) { ret = efiload_read_file(bflow, kernel); if (ret) return log_msg_ret("read", ret); /* * use the provided device tree if not using the built-in fdt */ if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT) fdt = bflow->fdt_addr; } if (efi_bootflow_run(bflow)) return log_msg_ret("run", -EINVAL); return 0; } static int distro_bootmeth_efi_bind(struct udevice *dev) { struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? "EFI boot from an .efi file" : "EFI"; return 0; } static struct bootmeth_ops distro_efi_bootmeth_ops = { .check = distro_efi_check, .read_bootflow = distro_efi_read_bootflow, .read_file = bootmeth_common_read_file, .boot = distro_efi_boot, }; static const struct udevice_id distro_efi_bootmeth_ids[] = { { .compatible = "u-boot,distro-efi" }, { } }; /* Put a number before 'efi' to provide a default ordering */ U_BOOT_DRIVER(bootmeth_4efi) = { .name = "bootmeth_efi", .id = UCLASS_BOOTMETH, .of_match = distro_efi_bootmeth_ids, .ops = &distro_efi_bootmeth_ops, .bind = distro_bootmeth_efi_bind, }; |