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 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
| 13 | #include <bootmeth.h> |
| 14 | #include <command.h> |
| 15 | #include <dm.h> |
Heinrich Schuchardt | 4226d9d | 2024-04-03 23:39:48 +0200 | [diff] [blame] | 16 | #include <efi_default_filename.h> |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 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> |
Simon Glass | 02fafec | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 24 | #include <linux/sizes.h> |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 25 | |
Heinrich Schuchardt | 4226d9d | 2024-04-03 23:39:48 +0200 | [diff] [blame] | 26 | #define EFI_DIRNAME "/EFI/BOOT/" |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 27 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 28 | static int get_efi_pxe_arch(void) |
| 29 | { |
| 30 | /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */ |
| 31 | if (IS_ENABLED(CONFIG_ARM64)) |
| 32 | return 0xb; |
| 33 | else if (IS_ENABLED(CONFIG_ARM)) |
| 34 | return 0xa; |
| 35 | else if (IS_ENABLED(CONFIG_X86_64)) |
| 36 | return 0x6; |
| 37 | else if (IS_ENABLED(CONFIG_X86)) |
| 38 | return 0x7; |
| 39 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) |
| 40 | return 0x19; |
| 41 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) |
| 42 | return 0x1b; |
| 43 | else if (IS_ENABLED(CONFIG_SANDBOX)) |
| 44 | return 0; /* not used */ |
| 45 | |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
| 49 | static int get_efi_pxe_vci(char *str, int max_len) |
| 50 | { |
| 51 | int ret; |
| 52 | |
| 53 | ret = get_efi_pxe_arch(); |
| 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | |
| 57 | snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | 47fc734 | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 62 | /** |
| 63 | * bootmeth_uses_network() - check if the media device is Ethernet |
| 64 | * |
| 65 | * @bflow: Bootflow to check |
| 66 | * Returns: true if the media device is Ethernet, else false |
| 67 | */ |
| 68 | static bool bootmeth_uses_network(struct bootflow *bflow) |
| 69 | { |
| 70 | const struct udevice *media = dev_get_parent(bflow->dev); |
| 71 | |
| 72 | return IS_ENABLED(CONFIG_CMD_DHCP) && |
| 73 | device_get_uclass_id(media) == UCLASS_ETH; |
| 74 | } |
| 75 | |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 76 | static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow) |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 77 | { |
| 78 | const struct udevice *media_dev; |
| 79 | int size = bflow->size; |
| 80 | const char *dev_name; |
| 81 | char devnum_str[9]; |
| 82 | char dirname[200]; |
| 83 | char *last_slash; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 84 | |
| 85 | /* |
| 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. |
| 89 | * |
| 90 | * Once we can clean up the EFI code to make proper use of driver model, |
| 91 | * this can go away. |
| 92 | */ |
| 93 | media_dev = dev_get_parent(bflow->dev); |
Mathew McBride | 63c5e5c | 2023-04-24 13:49:50 +1200 | [diff] [blame] | 94 | snprintf(devnum_str, sizeof(devnum_str), "%x:%x", |
| 95 | desc ? desc->devnum : dev_seq(media_dev), |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 96 | bflow->part); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 97 | |
| 98 | strlcpy(dirname, bflow->fname, sizeof(dirname)); |
| 99 | last_slash = strrchr(dirname, '/'); |
| 100 | if (last_slash) |
| 101 | *last_slash = '\0'; |
| 102 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 103 | dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ? |
Heinrich Schuchardt | 9c8cf10 | 2024-08-07 02:13:45 +0200 | [diff] [blame] | 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); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 107 | efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size); |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 108 | } |
| 109 | |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 110 | static int efiload_read_file(struct bootflow *bflow, ulong addr) |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 111 | { |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 112 | struct blk_desc *desc = NULL; |
| 113 | loff_t bytes_read; |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 114 | int ret; |
| 115 | |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 116 | if (bflow->blk) |
| 117 | desc = dev_get_uclass_plat(bflow->blk); |
| 118 | ret = bootmeth_setup_fs(bflow, desc); |
| 119 | if (ret) |
| 120 | return log_msg_ret("set", ret); |
| 121 | |
| 122 | ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read); |
Simon Glass | acad362 | 2023-04-24 13:49:46 +1200 | [diff] [blame] | 123 | if (ret) |
| 124 | return log_msg_ret("read", ret); |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 125 | bflow->buf = map_sysmem(addr, bflow->size); |
| 126 | |
| 127 | set_efi_bootdev(desc, bflow); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) |
| 133 | { |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 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); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 137 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 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); |
| 141 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 145 | /* |
| 146 | * distro_efi_try_bootflow_files() - Check that files are present |
| 147 | * |
| 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 |
| 151 | * it |
| 152 | * |
| 153 | * @dev: bootmeth device to use |
| 154 | * @bflow: bootflow to update |
| 155 | */ |
| 156 | static int distro_efi_try_bootflow_files(struct udevice *dev, |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 157 | struct bootflow *bflow) |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 158 | { |
| 159 | struct blk_desc *desc = NULL; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 160 | ulong fdt_addr, size; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 161 | char fname[256]; |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 162 | int ret, seq; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 163 | |
| 164 | /* We require a partition table */ |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 165 | if (!bflow->part) { |
| 166 | log_debug("no partitions\n"); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 167 | return -ENOENT; |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 168 | } |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 169 | |
| 170 | strcpy(fname, EFI_DIRNAME); |
Heinrich Schuchardt | 4226d9d | 2024-04-03 23:39:48 +0200 | [diff] [blame] | 171 | strcat(fname, BOOTEFI_NAME); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 172 | |
| 173 | if (bflow->blk) |
| 174 | desc = dev_get_uclass_plat(bflow->blk); |
| 175 | ret = bootmeth_try_file(bflow, desc, NULL, fname); |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 176 | if (ret) { |
| 177 | log_debug("File '%s' not found\n", fname); |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 178 | return log_msg_ret("try", ret); |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 179 | } |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 180 | |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 181 | /* Since we can access the file, let's call it ready */ |
| 182 | bflow->state = BOOTFLOWST_READY; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 183 | |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 184 | fdt_addr = env_get_hex("fdt_addr_r", 0); |
| 185 | |
| 186 | /* try the various available names */ |
| 187 | ret = -ENOENT; |
Simon Glass | 40516be | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 188 | *fname = '\0'; |
| 189 | for (seq = 0; ret == -ENOENT; seq++) { |
Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 190 | ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq); |
Simon Glass | 40516be | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 191 | if (ret == -EALREADY) |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 192 | bflow->flags = BOOTFLOWF_USE_PRIOR_FDT; |
Simon Glass | 02fafec | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 193 | if (!ret) { |
| 194 | /* Limit FDT files to 4MB */ |
| 195 | size = SZ_4M; |
Simon Glass | 40516be | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 196 | ret = bootmeth_common_read_file(dev, bflow, fname, |
| 197 | fdt_addr, &size); |
Simon Glass | 02fafec | 2023-07-26 21:01:24 -0600 | [diff] [blame] | 198 | } |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Simon Glass | 40516be | 2023-04-24 13:49:44 +1200 | [diff] [blame] | 201 | if (*fname) { |
| 202 | bflow->fdt_fname = strdup(fname); |
| 203 | if (!bflow->fdt_fname) |
| 204 | return log_msg_ret("fil", -ENOMEM); |
| 205 | } |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 206 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 207 | if (!ret) { |
| 208 | bflow->fdt_size = size; |
| 209 | bflow->fdt_addr = fdt_addr; |
| 210 | |
| 211 | /* |
| 212 | * TODO: Apply extension overlay |
| 213 | * |
| 214 | * Here we need to load and apply the extension overlay. This is |
| 215 | * not implemented. See do_extension_apply(). The extension |
| 216 | * stuff needs an implementation in boot/extension.c so it is |
| 217 | * separate from the command code. Really the extension stuff |
| 218 | * should use the device tree and a uclass / driver interface |
| 219 | * rather than implementing its own list |
| 220 | */ |
| 221 | } else { |
| 222 | log_debug("No device tree available\n"); |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 223 | bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 229 | static int distro_efi_read_bootflow_net(struct bootflow *bflow) |
| 230 | { |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 231 | char file_addr[17], fname[256]; |
| 232 | char *tftp_argv[] = {"tftp", file_addr, fname, NULL}; |
| 233 | struct cmd_tbl cmdtp = {}; /* dummy */ |
Shantur Rathore | 1c34f9d | 2023-11-19 16:54:59 +0000 | [diff] [blame] | 234 | const char *addr_str, *fdt_addr_str, *bootfile_name; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 235 | int ret, arch, size; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 236 | ulong addr, fdt_addr; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 237 | char str[36]; |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 238 | |
| 239 | ret = get_efi_pxe_vci(str, sizeof(str)); |
| 240 | if (ret) |
| 241 | return log_msg_ret("vci", ret); |
| 242 | ret = get_efi_pxe_arch(); |
| 243 | if (ret < 0) |
| 244 | return log_msg_ret("arc", ret); |
| 245 | arch = ret; |
| 246 | |
| 247 | ret = env_set("bootp_vci", str); |
| 248 | if (ret) |
| 249 | return log_msg_ret("vcs", ret); |
Shantur Rathore | b4c1252 | 2023-11-19 16:54:58 +0000 | [diff] [blame] | 250 | ret = env_set_hex("bootp_arch", arch); |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 251 | if (ret) |
| 252 | return log_msg_ret("ars", ret); |
| 253 | |
| 254 | /* figure out the load address */ |
| 255 | addr_str = env_get("kernel_addr_r"); |
| 256 | addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; |
| 257 | |
| 258 | /* clear any previous bootfile */ |
| 259 | env_set("bootfile", NULL); |
| 260 | |
| 261 | /* read the kernel */ |
| 262 | ret = dhcp_run(addr, NULL, true); |
| 263 | if (ret) |
| 264 | return log_msg_ret("dhc", ret); |
| 265 | |
| 266 | size = env_get_hex("filesize", -1); |
| 267 | if (size <= 0) |
| 268 | return log_msg_ret("sz", -EINVAL); |
| 269 | bflow->size = size; |
| 270 | |
Simon Glass | 5b0d914 | 2024-07-17 09:31:03 +0100 | [diff] [blame] | 271 | /* bootfile should be setup by dhcp */ |
Shantur Rathore | 1c34f9d | 2023-11-19 16:54:59 +0000 | [diff] [blame] | 272 | bootfile_name = env_get("bootfile"); |
| 273 | if (!bootfile_name) |
| 274 | return log_msg_ret("bootfile_name", ret); |
| 275 | bflow->fname = strdup(bootfile_name); |
| 276 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 277 | /* do the hideous EFI hack */ |
| 278 | efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0), |
| 279 | bflow->size); |
| 280 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 281 | /* read the DT file also */ |
| 282 | fdt_addr_str = env_get("fdt_addr_r"); |
| 283 | if (!fdt_addr_str) |
| 284 | return log_msg_ret("fdt", -EINVAL); |
| 285 | fdt_addr = hextoul(fdt_addr_str, NULL); |
| 286 | sprintf(file_addr, "%lx", fdt_addr); |
| 287 | |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 288 | /* We only allow the first prefix with PXE */ |
Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 289 | ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0); |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 290 | if (ret) |
| 291 | return log_msg_ret("nam", ret); |
| 292 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 293 | bflow->fdt_fname = strdup(fname); |
| 294 | if (!bflow->fdt_fname) |
| 295 | return log_msg_ret("fil", -ENOMEM); |
| 296 | |
| 297 | if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) { |
| 298 | bflow->fdt_size = env_get_hex("filesize", 0); |
| 299 | bflow->fdt_addr = fdt_addr; |
| 300 | } else { |
| 301 | log_debug("No device tree available\n"); |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 302 | bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 305 | bflow->state = BOOTFLOWST_READY; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 311 | { |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 312 | int ret; |
| 313 | |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 314 | log_debug("dev='%s', part=%d\n", bflow->dev->name, bflow->part); |
| 315 | |
Shantur Rathore | b45d7e2 | 2023-11-19 16:55:01 +0000 | [diff] [blame] | 316 | /* |
| 317 | * bootmeth_efi doesn't allocate any buffer neither for blk nor net device |
| 318 | * set flag to avoid freeing static buffer. |
| 319 | */ |
| 320 | bflow->flags |= BOOTFLOWF_STATIC_BUF; |
| 321 | |
Simon Glass | 47fc734 | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 322 | if (bootmeth_uses_network(bflow)) { |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 323 | /* we only support reading from one device, so ignore 'dev' */ |
| 324 | ret = distro_efi_read_bootflow_net(bflow); |
| 325 | if (ret) |
| 326 | return log_msg_ret("net", ret); |
| 327 | } else { |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 328 | ret = distro_efi_try_bootflow_files(dev, bflow); |
Simon Glass | 9e08caa | 2023-01-17 10:47:55 -0700 | [diff] [blame] | 329 | if (ret) |
| 330 | return log_msg_ret("blk", ret); |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Bin Meng | 79eb5f7 | 2023-08-03 17:30:05 +0800 | [diff] [blame] | 336 | static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 337 | { |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 338 | ulong kernel, fdt; |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 339 | int ret; |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 340 | |
Simon Glass | b3445e0 | 2024-09-26 23:59:37 +0200 | [diff] [blame] | 341 | log_debug("distro EFI boot\n"); |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 342 | kernel = env_get_hex("kernel_addr_r", 0); |
Simon Glass | 47fc734 | 2023-07-26 21:01:22 -0600 | [diff] [blame] | 343 | if (!bootmeth_uses_network(bflow)) { |
Simon Glass | 4aec190 | 2023-07-26 21:01:23 -0600 | [diff] [blame] | 344 | ret = efiload_read_file(bflow, kernel); |
| 345 | if (ret) |
| 346 | return log_msg_ret("read", ret); |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 349 | * use the provided device tree if not using the built-in fdt |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 350 | */ |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 351 | if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT) |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 352 | fdt = bflow->fdt_addr; |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 353 | |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 354 | } else { |
| 355 | /* |
| 356 | * This doesn't actually work for network devices: |
| 357 | * |
| 358 | * do_bootefi_image() No UEFI binary known at 0x02080000 |
| 359 | * |
| 360 | * But this is the same behaviour for distro boot, so it can be |
| 361 | * fixed here. |
| 362 | */ |
Simon Glass | 56bb4e4 | 2023-01-17 10:47:57 -0700 | [diff] [blame] | 363 | fdt = env_get_hex("fdt_addr_r", 0); |
| 364 | } |
| 365 | |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 366 | if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) { |
| 367 | log_debug("Booting with built-in fdt\n"); |
Heinrich Schuchardt | 8ff257c | 2023-12-22 16:01:56 +0100 | [diff] [blame] | 368 | if (efi_binary_run(map_sysmem(kernel, 0), bflow->size, |
Tom Rini | 08ccd54 | 2023-12-18 08:31:50 -0500 | [diff] [blame] | 369 | EFI_FDT_USE_INTERNAL)) |
| 370 | return log_msg_ret("run", -EINVAL); |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 371 | } else { |
| 372 | log_debug("Booting with external fdt\n"); |
Heinrich Schuchardt | 8ff257c | 2023-12-22 16:01:56 +0100 | [diff] [blame] | 373 | if (efi_binary_run(map_sysmem(kernel, 0), bflow->size, |
Tom Rini | 08ccd54 | 2023-12-18 08:31:50 -0500 | [diff] [blame] | 374 | map_sysmem(fdt, 0))) |
| 375 | return log_msg_ret("run", -EINVAL); |
Shantur Rathore | aab9cdb | 2023-11-19 16:55:00 +0000 | [diff] [blame] | 376 | } |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int distro_bootmeth_efi_bind(struct udevice *dev) |
| 382 | { |
| 383 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 384 | |
| 385 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 386 | "EFI boot from an .efi file" : "EFI"; |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static struct bootmeth_ops distro_efi_bootmeth_ops = { |
| 392 | .check = distro_efi_check, |
| 393 | .read_bootflow = distro_efi_read_bootflow, |
| 394 | .read_file = bootmeth_common_read_file, |
| 395 | .boot = distro_efi_boot, |
| 396 | }; |
| 397 | |
| 398 | static const struct udevice_id distro_efi_bootmeth_ids[] = { |
| 399 | { .compatible = "u-boot,distro-efi" }, |
| 400 | { } |
| 401 | }; |
| 402 | |
Simon Glass | e5cb018 | 2024-07-17 09:30:59 +0100 | [diff] [blame] | 403 | /* Put a number before 'efi' to provide a default ordering */ |
Heinrich Schuchardt | dc1f006 | 2024-04-03 20:34:15 +0200 | [diff] [blame] | 404 | U_BOOT_DRIVER(bootmeth_4efi) = { |
Simon Glass | 66f6255 | 2022-04-24 23:31:17 -0600 | [diff] [blame] | 405 | .name = "bootmeth_efi", |
| 406 | .id = UCLASS_BOOTMETH, |
| 407 | .of_match = distro_efi_bootmeth_ids, |
| 408 | .ops = &distro_efi_bootmeth_ops, |
| 409 | .bind = distro_bootmeth_efi_bind, |
| 410 | }; |