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