Simon Glass | acfa9bd | 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 | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 22 | #include <net.h> |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 23 | #include <pxe_utils.h> |
Simon Glass | 2984d21 | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 24 | #include <linux/sizes.h> |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 25 | |
| 26 | #define EFI_DIRNAME "efi/boot/" |
| 27 | |
| 28 | /** |
| 29 | * get_efi_leafname() - Get the leaf name for the EFI file we expect |
| 30 | * |
| 31 | * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi". |
| 32 | * Must have at least 16 bytes of space |
| 33 | * @max_len: Length of @str, must be >=16 |
| 34 | */ |
| 35 | static int get_efi_leafname(char *str, int max_len) |
| 36 | { |
| 37 | const char *base; |
| 38 | |
| 39 | if (max_len < 16) |
| 40 | return log_msg_ret("spc", -ENOSPC); |
| 41 | if (IS_ENABLED(CONFIG_ARM64)) |
| 42 | base = "bootaa64"; |
| 43 | else if (IS_ENABLED(CONFIG_ARM)) |
| 44 | base = "bootarm"; |
| 45 | else if (IS_ENABLED(CONFIG_X86_RUN_32BIT)) |
| 46 | base = "bootia32"; |
| 47 | else if (IS_ENABLED(CONFIG_X86_RUN_64BIT)) |
| 48 | base = "bootx64"; |
| 49 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) |
| 50 | base = "bootriscv32"; |
| 51 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) |
| 52 | base = "bootriscv64"; |
| 53 | else if (IS_ENABLED(CONFIG_SANDBOX)) |
| 54 | base = "bootsbox"; |
| 55 | else |
| 56 | return -EINVAL; |
| 57 | |
| 58 | strcpy(str, base); |
| 59 | strcat(str, ".efi"); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 64 | static int get_efi_pxe_arch(void) |
| 65 | { |
| 66 | /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */ |
| 67 | if (IS_ENABLED(CONFIG_ARM64)) |
| 68 | return 0xb; |
| 69 | else if (IS_ENABLED(CONFIG_ARM)) |
| 70 | return 0xa; |
| 71 | else if (IS_ENABLED(CONFIG_X86_64)) |
| 72 | return 0x6; |
| 73 | else if (IS_ENABLED(CONFIG_X86)) |
| 74 | return 0x7; |
| 75 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) |
| 76 | return 0x19; |
| 77 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) |
| 78 | return 0x1b; |
| 79 | else if (IS_ENABLED(CONFIG_SANDBOX)) |
| 80 | return 0; /* not used */ |
| 81 | |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
| 85 | static int get_efi_pxe_vci(char *str, int max_len) |
| 86 | { |
| 87 | int ret; |
| 88 | |
| 89 | ret = get_efi_pxe_arch(); |
| 90 | if (ret < 0) |
| 91 | return ret; |
| 92 | |
| 93 | snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Simon Glass | 146242c | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 98 | /** |
| 99 | * bootmeth_uses_network() - check if the media device is Ethernet |
| 100 | * |
| 101 | * @bflow: Bootflow to check |
| 102 | * Returns: true if the media device is Ethernet, else false |
| 103 | */ |
| 104 | static bool bootmeth_uses_network(struct bootflow *bflow) |
| 105 | { |
| 106 | const struct udevice *media = dev_get_parent(bflow->dev); |
| 107 | |
| 108 | return IS_ENABLED(CONFIG_CMD_DHCP) && |
| 109 | device_get_uclass_id(media) == UCLASS_ETH; |
| 110 | } |
| 111 | |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 112 | static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow) |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 113 | { |
| 114 | const struct udevice *media_dev; |
| 115 | int size = bflow->size; |
| 116 | const char *dev_name; |
| 117 | char devnum_str[9]; |
| 118 | char dirname[200]; |
| 119 | char *last_slash; |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * This is a horrible hack to tell EFI about this boot device. Once we |
| 123 | * unify EFI with the rest of U-Boot we can clean this up. The same hack |
| 124 | * exists in multiple places, e.g. in the fs, tftp and load commands. |
| 125 | * |
| 126 | * Once we can clean up the EFI code to make proper use of driver model, |
| 127 | * this can go away. |
| 128 | */ |
| 129 | media_dev = dev_get_parent(bflow->dev); |
Mathew McBride | 484e701 | 2023-04-24 13:49:50 +1200 | [diff] [blame] | 130 | snprintf(devnum_str, sizeof(devnum_str), "%x:%x", |
| 131 | desc ? desc->devnum : dev_seq(media_dev), |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 132 | bflow->part); |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 133 | |
| 134 | strlcpy(dirname, bflow->fname, sizeof(dirname)); |
| 135 | last_slash = strrchr(dirname, '/'); |
| 136 | if (last_slash) |
| 137 | *last_slash = '\0'; |
| 138 | |
| 139 | log_debug("setting bootdev %s, %s, %s, %p, %x\n", |
| 140 | dev_get_uclass_name(media_dev), devnum_str, bflow->fname, |
| 141 | bflow->buf, size); |
| 142 | dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ? |
| 143 | "usb" : dev_get_uclass_name(media_dev); |
| 144 | efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size); |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 145 | } |
| 146 | |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 147 | static int efiload_read_file(struct bootflow *bflow, ulong addr) |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 148 | { |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 149 | struct blk_desc *desc = NULL; |
| 150 | loff_t bytes_read; |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 151 | int ret; |
| 152 | |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 153 | if (bflow->blk) |
| 154 | desc = dev_get_uclass_plat(bflow->blk); |
| 155 | ret = bootmeth_setup_fs(bflow, desc); |
| 156 | if (ret) |
| 157 | return log_msg_ret("set", ret); |
| 158 | |
| 159 | ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read); |
Simon Glass | d80bb4f | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 160 | if (ret) |
| 161 | return log_msg_ret("read", ret); |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 162 | bflow->buf = map_sysmem(addr, bflow->size); |
Simon Glass | 741d1e9 | 2023-11-15 18:35:23 -0700 | [diff] [blame] | 163 | bflow->flags |= BOOTFLOWF_STATIC_BUF; |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 164 | |
| 165 | set_efi_bootdev(desc, bflow); |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) |
| 171 | { |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 172 | /* This only works on block and network devices */ |
| 173 | if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter)) |
| 174 | return log_msg_ret("blk", -ENOTSUPP); |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 175 | |
Simon Glass | d9f4857 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 176 | /* This works on block devices and network devices */ |
| 177 | if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY) |
| 178 | return log_msg_ret("pxe", -ENOTSUPP); |
| 179 | |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 183 | /** |
| 184 | * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file |
| 185 | * |
| 186 | * @fname: Place to put filename |
| 187 | * @size: Max size of filename |
| 188 | * @seq: Sequence number, to cycle through options (0=first) |
| 189 | * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist, |
| 190 | * -EINVAL if there are no more options, -EALREADY if the control FDT should be |
| 191 | * used |
| 192 | */ |
| 193 | static int distro_efi_get_fdt_name(char *fname, int size, int seq) |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 194 | { |
| 195 | const char *fdt_fname; |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 196 | const char *prefix; |
| 197 | |
| 198 | /* select the prefix */ |
| 199 | switch (seq) { |
| 200 | case 0: |
| 201 | /* this is the default */ |
| 202 | prefix = "/dtb"; |
| 203 | break; |
| 204 | case 1: |
| 205 | prefix = ""; |
| 206 | break; |
| 207 | case 2: |
| 208 | prefix = "/dtb/current"; |
| 209 | break; |
| 210 | default: |
| 211 | return log_msg_ret("pref", -EINVAL); |
| 212 | } |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 213 | |
| 214 | fdt_fname = env_get("fdtfile"); |
| 215 | if (fdt_fname) { |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 216 | snprintf(fname, size, "%s/%s", prefix, fdt_fname); |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 217 | log_debug("Using device tree: %s\n", fname); |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 218 | } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) { |
| 219 | strcpy(fname, "<prior>"); |
| 220 | return log_msg_ret("pref", -EALREADY); |
| 221 | /* Use this fallback only for 32-bit ARM */ |
| 222 | } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) { |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 223 | const char *soc = env_get("soc"); |
| 224 | const char *board = env_get("board"); |
| 225 | const char *boardver = env_get("boardver"); |
| 226 | |
| 227 | /* cf the code in label_boot() which seems very complex */ |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 228 | snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix, |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 229 | soc ? soc : "", soc ? "-" : "", board ? board : "", |
| 230 | boardver ? boardver : ""); |
| 231 | log_debug("Using default device tree: %s\n", fname); |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 232 | } else { |
| 233 | return log_msg_ret("env", -ENOENT); |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 234 | } |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 235 | |
| 236 | return 0; |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 239 | /* |
| 240 | * distro_efi_try_bootflow_files() - Check that files are present |
| 241 | * |
| 242 | * This reads any FDT file and checks whether the bootflow file is present, for |
| 243 | * later reading. We avoid reading the bootflow now, since it is likely large, |
| 244 | * it may take a long time and we want to avoid needing to allocate memory for |
| 245 | * it |
| 246 | * |
| 247 | * @dev: bootmeth device to use |
| 248 | * @bflow: bootflow to update |
| 249 | */ |
| 250 | static int distro_efi_try_bootflow_files(struct udevice *dev, |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 251 | struct bootflow *bflow) |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 252 | { |
| 253 | struct blk_desc *desc = NULL; |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 254 | ulong fdt_addr, size; |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 255 | char fname[256]; |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 256 | int ret, seq; |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 257 | |
| 258 | /* We require a partition table */ |
| 259 | if (!bflow->part) |
| 260 | return -ENOENT; |
| 261 | |
| 262 | strcpy(fname, EFI_DIRNAME); |
| 263 | ret = get_efi_leafname(fname + strlen(fname), |
| 264 | sizeof(fname) - strlen(fname)); |
| 265 | if (ret) |
| 266 | return log_msg_ret("leaf", ret); |
| 267 | |
| 268 | if (bflow->blk) |
| 269 | desc = dev_get_uclass_plat(bflow->blk); |
| 270 | ret = bootmeth_try_file(bflow, desc, NULL, fname); |
| 271 | if (ret) |
| 272 | return log_msg_ret("try", ret); |
| 273 | |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 274 | /* Since we can access the file, let's call it ready */ |
| 275 | bflow->state = BOOTFLOWST_READY; |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 276 | |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 277 | fdt_addr = env_get_hex("fdt_addr_r", 0); |
| 278 | |
| 279 | /* try the various available names */ |
| 280 | ret = -ENOENT; |
Simon Glass | 9f83516 | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 281 | *fname = '\0'; |
| 282 | for (seq = 0; ret == -ENOENT; seq++) { |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 283 | ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq); |
Simon Glass | 9f83516 | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 284 | if (ret == -EALREADY) |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 285 | bflow->flags = BOOTFLOWF_USE_PRIOR_FDT; |
Simon Glass | 2984d21 | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 286 | if (!ret) { |
| 287 | /* Limit FDT files to 4MB */ |
| 288 | size = SZ_4M; |
Simon Glass | 9f83516 | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 289 | ret = bootmeth_common_read_file(dev, bflow, fname, |
| 290 | fdt_addr, &size); |
Simon Glass | 2984d21 | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 291 | } |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Simon Glass | 9f83516 | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 294 | if (*fname) { |
| 295 | bflow->fdt_fname = strdup(fname); |
| 296 | if (!bflow->fdt_fname) |
| 297 | return log_msg_ret("fil", -ENOMEM); |
| 298 | } |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 299 | |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 300 | if (!ret) { |
| 301 | bflow->fdt_size = size; |
| 302 | bflow->fdt_addr = fdt_addr; |
| 303 | |
| 304 | /* |
| 305 | * TODO: Apply extension overlay |
| 306 | * |
| 307 | * Here we need to load and apply the extension overlay. This is |
| 308 | * not implemented. See do_extension_apply(). The extension |
| 309 | * stuff needs an implementation in boot/extension.c so it is |
| 310 | * separate from the command code. Really the extension stuff |
| 311 | * should use the device tree and a uclass / driver interface |
| 312 | * rather than implementing its own list |
| 313 | */ |
| 314 | } else { |
| 315 | log_debug("No device tree available\n"); |
| 316 | } |
| 317 | |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 321 | static int distro_efi_read_bootflow_net(struct bootflow *bflow) |
| 322 | { |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 323 | char file_addr[17], fname[256]; |
| 324 | char *tftp_argv[] = {"tftp", file_addr, fname, NULL}; |
| 325 | struct cmd_tbl cmdtp = {}; /* dummy */ |
| 326 | const char *addr_str, *fdt_addr_str; |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 327 | int ret, arch, size; |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 328 | ulong addr, fdt_addr; |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 329 | char str[36]; |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 330 | |
| 331 | ret = get_efi_pxe_vci(str, sizeof(str)); |
| 332 | if (ret) |
| 333 | return log_msg_ret("vci", ret); |
| 334 | ret = get_efi_pxe_arch(); |
| 335 | if (ret < 0) |
| 336 | return log_msg_ret("arc", ret); |
| 337 | arch = ret; |
| 338 | |
| 339 | ret = env_set("bootp_vci", str); |
| 340 | if (ret) |
| 341 | return log_msg_ret("vcs", ret); |
Shantur Rathore | c5440a6 | 2023-11-19 16:54:58 +0000 | [diff] [blame^] | 342 | ret = env_set_hex("bootp_arch", arch); |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 343 | if (ret) |
| 344 | return log_msg_ret("ars", ret); |
| 345 | |
| 346 | /* figure out the load address */ |
| 347 | addr_str = env_get("kernel_addr_r"); |
| 348 | addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; |
| 349 | |
| 350 | /* clear any previous bootfile */ |
| 351 | env_set("bootfile", NULL); |
| 352 | |
| 353 | /* read the kernel */ |
| 354 | ret = dhcp_run(addr, NULL, true); |
| 355 | if (ret) |
| 356 | return log_msg_ret("dhc", ret); |
| 357 | |
| 358 | size = env_get_hex("filesize", -1); |
| 359 | if (size <= 0) |
| 360 | return log_msg_ret("sz", -EINVAL); |
| 361 | bflow->size = size; |
| 362 | |
| 363 | /* do the hideous EFI hack */ |
| 364 | efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0), |
| 365 | bflow->size); |
| 366 | |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 367 | /* read the DT file also */ |
| 368 | fdt_addr_str = env_get("fdt_addr_r"); |
| 369 | if (!fdt_addr_str) |
| 370 | return log_msg_ret("fdt", -EINVAL); |
| 371 | fdt_addr = hextoul(fdt_addr_str, NULL); |
| 372 | sprintf(file_addr, "%lx", fdt_addr); |
| 373 | |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 374 | /* We only allow the first prefix with PXE */ |
| 375 | ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0); |
| 376 | if (ret) |
| 377 | return log_msg_ret("nam", ret); |
| 378 | |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 379 | bflow->fdt_fname = strdup(fname); |
| 380 | if (!bflow->fdt_fname) |
| 381 | return log_msg_ret("fil", -ENOMEM); |
| 382 | |
| 383 | if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) { |
| 384 | bflow->fdt_size = env_get_hex("filesize", 0); |
| 385 | bflow->fdt_addr = fdt_addr; |
| 386 | } else { |
| 387 | log_debug("No device tree available\n"); |
| 388 | } |
| 389 | |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 390 | bflow->state = BOOTFLOWST_READY; |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 396 | { |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 397 | int ret; |
| 398 | |
Simon Glass | 146242c | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 399 | if (bootmeth_uses_network(bflow)) { |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 400 | /* we only support reading from one device, so ignore 'dev' */ |
| 401 | ret = distro_efi_read_bootflow_net(bflow); |
| 402 | if (ret) |
| 403 | return log_msg_ret("net", ret); |
| 404 | } else { |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 405 | ret = distro_efi_try_bootflow_files(dev, bflow); |
Simon Glass | 3e18860 | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 406 | if (ret) |
| 407 | return log_msg_ret("blk", ret); |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Bin Meng | 6072703 | 2023-08-03 17:30:05 +0800 | [diff] [blame] | 413 | static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 414 | { |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 415 | ulong kernel, fdt; |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 416 | char cmd[50]; |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 417 | int ret; |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 418 | |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 419 | kernel = env_get_hex("kernel_addr_r", 0); |
Simon Glass | 146242c | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 420 | if (!bootmeth_uses_network(bflow)) { |
Simon Glass | 6a8c2f9 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 421 | ret = efiload_read_file(bflow, kernel); |
| 422 | if (ret) |
| 423 | return log_msg_ret("read", ret); |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * use the provided device tree if available, else fall back to |
| 427 | * the control FDT |
| 428 | */ |
| 429 | if (bflow->fdt_fname) |
| 430 | fdt = bflow->fdt_addr; |
| 431 | else |
| 432 | fdt = (ulong)map_to_sysmem(gd->fdt_blob); |
| 433 | } else { |
| 434 | /* |
| 435 | * This doesn't actually work for network devices: |
| 436 | * |
| 437 | * do_bootefi_image() No UEFI binary known at 0x02080000 |
| 438 | * |
| 439 | * But this is the same behaviour for distro boot, so it can be |
| 440 | * fixed here. |
| 441 | */ |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 442 | fdt = env_get_hex("fdt_addr_r", 0); |
| 443 | } |
| 444 | |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 445 | /* |
| 446 | * At some point we can add a real interface to bootefi so we can call |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 447 | * this directly. For now, go through the CLI, like distro boot. |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 448 | */ |
Simon Glass | e890e8c | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 449 | snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", kernel, fdt); |
Simon Glass | acfa9bd | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 450 | if (run_command(cmd, 0)) |
| 451 | return log_msg_ret("run", -EINVAL); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int distro_bootmeth_efi_bind(struct udevice *dev) |
| 457 | { |
| 458 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 459 | |
| 460 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 461 | "EFI boot from an .efi file" : "EFI"; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static struct bootmeth_ops distro_efi_bootmeth_ops = { |
| 467 | .check = distro_efi_check, |
| 468 | .read_bootflow = distro_efi_read_bootflow, |
| 469 | .read_file = bootmeth_common_read_file, |
| 470 | .boot = distro_efi_boot, |
| 471 | }; |
| 472 | |
| 473 | static const struct udevice_id distro_efi_bootmeth_ids[] = { |
| 474 | { .compatible = "u-boot,distro-efi" }, |
| 475 | { } |
| 476 | }; |
| 477 | |
| 478 | U_BOOT_DRIVER(bootmeth_efi) = { |
| 479 | .name = "bootmeth_efi", |
| 480 | .id = UCLASS_BOOTMETH, |
| 481 | .of_match = distro_efi_bootmeth_ids, |
| 482 | .ops = &distro_efi_bootmeth_ops, |
| 483 | .bind = distro_bootmeth_efi_bind, |
| 484 | }; |