Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 3 | * EFI boot manager |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 10 | #include <blk.h> |
| 11 | #include <blkmap.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 12 | #include <charset.h> |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 3b1e60b | 2024-11-07 14:31:43 -0700 | [diff] [blame] | 14 | #include <efi.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 16 | #include <malloc.h> |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 17 | #include <net.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 18 | #include <efi_loader.h> |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 19 | #include <efi_variable.h> |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 20 | #include <asm/unaligned.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 21 | |
| 22 | static const struct efi_boot_services *bs; |
| 23 | static const struct efi_runtime_services *rs; |
| 24 | |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 25 | /** |
| 26 | * struct uridp_context - uri device path resource |
| 27 | * |
| 28 | * @image_size: image size |
| 29 | * @image_addr: image address |
| 30 | * @loaded_dp: pointer to loaded device path |
| 31 | * @ramdisk_blk_dev: pointer to the ramdisk blk device |
| 32 | * @mem_handle: efi_handle to the loaded PE-COFF image |
| 33 | */ |
| 34 | struct uridp_context { |
| 35 | ulong image_size; |
| 36 | ulong image_addr; |
| 37 | struct efi_device_path *loaded_dp; |
| 38 | struct udevice *ramdisk_blk_dev; |
| 39 | efi_handle_t mem_handle; |
| 40 | }; |
| 41 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 42 | const efi_guid_t efi_guid_bootmenu_auto_generated = |
| 43 | EFICONFIG_AUTO_GENERATED_ENTRY_GUID; |
| 44 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 45 | /* |
| 46 | * bootmgr implements the logic of trying to find a payload to boot |
| 47 | * based on the BootOrder + BootXXXX variables, and then loading it. |
| 48 | * |
| 49 | * TODO detecting a special key held (f9?) and displaying a boot menu |
| 50 | * like you would get on a PC would be clever. |
| 51 | * |
| 52 | * TODO if we had a way to write and persist variables after the OS |
| 53 | * has started, we'd also want to check OsIndications to see if we |
| 54 | * should do normal or recovery boot. |
| 55 | */ |
| 56 | |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 57 | /** |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 58 | * expand_media_path() - expand a device path for default file name |
| 59 | * @device_path: device path to check against |
| 60 | * |
| 61 | * If @device_path is a media or disk partition which houses a file |
| 62 | * system, this function returns a full device path which contains |
| 63 | * an architecture-specific default file name for removable media. |
| 64 | * |
| 65 | * Return: a newly allocated device path |
| 66 | */ |
| 67 | static |
| 68 | struct efi_device_path *expand_media_path(struct efi_device_path *device_path) |
| 69 | { |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 70 | struct efi_device_path *rem, *full_path; |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 71 | efi_handle_t handle; |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 72 | |
| 73 | if (!device_path) |
| 74 | return NULL; |
| 75 | |
| 76 | /* |
| 77 | * If device_path is a (removable) media or partition which provides |
| 78 | * simple file system protocol, append a default file name to support |
| 79 | * booting from removable media. |
| 80 | */ |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 81 | handle = efi_dp_find_obj(device_path, |
| 82 | &efi_simple_file_system_protocol_guid, &rem); |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 83 | if (handle) { |
Heinrich Schuchardt | 0628e1c | 2022-06-11 05:22:07 +0000 | [diff] [blame] | 84 | if (rem->type == DEVICE_PATH_TYPE_END) { |
Simon Glass | 3b1e60b | 2024-11-07 14:31:43 -0700 | [diff] [blame] | 85 | char fname[30]; |
| 86 | |
| 87 | snprintf(fname, sizeof(fname), "/EFI/BOOT/%s", |
| 88 | efi_get_basename()); |
| 89 | full_path = efi_dp_from_file(device_path, fname); |
| 90 | |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 91 | } else { |
| 92 | full_path = efi_dp_dup(device_path); |
| 93 | } |
| 94 | } else { |
| 95 | full_path = efi_dp_dup(device_path); |
| 96 | } |
| 97 | |
| 98 | return full_path; |
| 99 | } |
| 100 | |
| 101 | /** |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 102 | * try_load_from_file_path() - try to load a file |
| 103 | * |
| 104 | * Given a file media path iterate through a list of handles and try to |
| 105 | * to load the file from each of them until the first success. |
| 106 | * |
| 107 | * @fs_handles: array of handles with the simple file protocol |
| 108 | * @num: number of handles in fs_handles |
| 109 | * @fp: file path to open |
| 110 | * @handle: on return pointer to handle for loaded image |
| 111 | * @removable: if true only consider removable media, else only non-removable |
| 112 | */ |
| 113 | static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles, |
| 114 | efi_uintn_t num, |
| 115 | struct efi_device_path *fp, |
| 116 | efi_handle_t *handle, |
| 117 | bool removable) |
| 118 | { |
| 119 | struct efi_handler *handler; |
| 120 | struct efi_device_path *dp; |
| 121 | int i; |
| 122 | efi_status_t ret; |
| 123 | |
| 124 | for (i = 0; i < num; i++) { |
| 125 | if (removable != efi_disk_is_removable(fs_handles[i])) |
| 126 | continue; |
| 127 | |
| 128 | ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path, |
| 129 | &handler); |
| 130 | if (ret != EFI_SUCCESS) |
| 131 | continue; |
| 132 | |
| 133 | dp = handler->protocol_interface; |
| 134 | if (!dp) |
| 135 | continue; |
| 136 | |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 137 | dp = efi_dp_concat(dp, fp, 0); |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 138 | if (!dp) |
| 139 | continue; |
| 140 | |
| 141 | ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0, |
| 142 | handle)); |
| 143 | efi_free_pool(dp); |
| 144 | if (ret == EFI_SUCCESS) |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | return EFI_NOT_FOUND; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * try_load_from_short_path |
| 153 | * @fp: file path |
| 154 | * @handle: pointer to handle for newly installed image |
| 155 | * |
| 156 | * Enumerate all the devices which support file system operations, |
| 157 | * prepend its media device path to the file path, @fp, and |
| 158 | * try to load the file. |
| 159 | * This function should be called when handling a short-form path |
| 160 | * which is starting with a file device path. |
| 161 | * |
| 162 | * Return: status code |
| 163 | */ |
| 164 | static efi_status_t try_load_from_short_path(struct efi_device_path *fp, |
| 165 | efi_handle_t *handle) |
| 166 | { |
| 167 | efi_handle_t *fs_handles; |
| 168 | efi_uintn_t num; |
| 169 | efi_status_t ret; |
| 170 | |
| 171 | ret = EFI_CALL(efi_locate_handle_buffer( |
| 172 | BY_PROTOCOL, |
| 173 | &efi_simple_file_system_protocol_guid, |
| 174 | NULL, |
| 175 | &num, &fs_handles)); |
| 176 | if (ret != EFI_SUCCESS) |
| 177 | return ret; |
| 178 | if (!num) |
| 179 | return EFI_NOT_FOUND; |
| 180 | |
| 181 | /* removable media first */ |
| 182 | ret = try_load_from_file_path(fs_handles, num, fp, handle, true); |
| 183 | if (ret == EFI_SUCCESS) |
| 184 | goto out; |
| 185 | |
| 186 | /* fixed media */ |
| 187 | ret = try_load_from_file_path(fs_handles, num, fp, handle, false); |
| 188 | if (ret == EFI_SUCCESS) |
| 189 | goto out; |
| 190 | |
| 191 | out: |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | /** |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 196 | * mount_image() - mount the image with blkmap |
| 197 | * |
| 198 | * @lo_label: u16 label string of load option |
| 199 | * @addr: image address |
| 200 | * @size: image size |
| 201 | * Return: pointer to the UCLASS_BLK udevice, NULL if failed |
| 202 | */ |
| 203 | static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size) |
| 204 | { |
| 205 | int err; |
| 206 | struct blkmap *bm; |
| 207 | struct udevice *bm_dev; |
| 208 | char *label = NULL, *p; |
| 209 | |
| 210 | label = efi_alloc(utf16_utf8_strlen(lo_label) + 1); |
| 211 | if (!label) |
| 212 | return NULL; |
| 213 | |
| 214 | p = label; |
| 215 | utf16_utf8_strcpy(&p, lo_label); |
| 216 | err = blkmap_create_ramdisk(label, addr, size, &bm_dev); |
| 217 | if (err) { |
| 218 | efi_free_pool(label); |
| 219 | return NULL; |
| 220 | } |
| 221 | bm = dev_get_plat(bm_dev); |
| 222 | |
| 223 | efi_free_pool(label); |
| 224 | |
| 225 | return bm->blk; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * search_default_file() - search default file |
| 230 | * |
| 231 | * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice |
| 232 | * @loaded_dp: pointer to default file device path |
| 233 | * Return: status code |
| 234 | */ |
| 235 | static efi_status_t search_default_file(struct udevice *dev, |
| 236 | struct efi_device_path **loaded_dp) |
| 237 | { |
| 238 | efi_status_t ret; |
| 239 | efi_handle_t handle; |
| 240 | u16 *default_file_name = NULL; |
| 241 | struct efi_file_handle *root, *f; |
| 242 | struct efi_device_path *dp = NULL, *fp = NULL; |
| 243 | struct efi_simple_file_system_protocol *file_system; |
| 244 | struct efi_device_path *device_path, *full_path = NULL; |
| 245 | |
| 246 | if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { |
| 247 | log_warning("DM_TAG_EFI not found\n"); |
| 248 | return EFI_INVALID_PARAMETER; |
| 249 | } |
| 250 | |
| 251 | ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, |
| 252 | (void **)&device_path, efi_root, NULL, |
| 253 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 254 | if (ret != EFI_SUCCESS) |
| 255 | return ret; |
| 256 | |
| 257 | ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, |
| 258 | (void **)&file_system, efi_root, NULL, |
| 259 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 260 | if (ret != EFI_SUCCESS) |
| 261 | return ret; |
| 262 | |
| 263 | ret = EFI_CALL(file_system->open_volume(file_system, &root)); |
| 264 | if (ret != EFI_SUCCESS) |
| 265 | return ret; |
| 266 | |
| 267 | full_path = expand_media_path(device_path); |
| 268 | ret = efi_dp_split_file_path(full_path, &dp, &fp); |
| 269 | if (ret != EFI_SUCCESS) |
| 270 | goto err; |
| 271 | |
| 272 | default_file_name = efi_dp_str(fp); |
| 273 | efi_free_pool(dp); |
| 274 | efi_free_pool(fp); |
| 275 | if (!default_file_name) { |
| 276 | ret = EFI_OUT_OF_RESOURCES; |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | ret = EFI_CALL(root->open(root, &f, default_file_name, |
| 281 | EFI_FILE_MODE_READ, 0)); |
| 282 | efi_free_pool(default_file_name); |
| 283 | if (ret != EFI_SUCCESS) |
| 284 | goto err; |
| 285 | |
| 286 | EFI_CALL(f->close(f)); |
| 287 | EFI_CALL(root->close(root)); |
| 288 | |
| 289 | *loaded_dp = full_path; |
| 290 | |
| 291 | return EFI_SUCCESS; |
| 292 | |
| 293 | err: |
| 294 | EFI_CALL(root->close(root)); |
| 295 | efi_free_pool(full_path); |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | /** |
Masahisa Kojima | 54ce3dd | 2024-01-12 09:19:21 +0900 | [diff] [blame] | 301 | * fill_default_file_path() - get fallback boot device path for block device |
| 302 | * |
| 303 | * Provide the device path to the fallback UEFI boot file, e.g. |
| 304 | * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk. |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 305 | * |
| 306 | * @blk: pointer to the UCLASS_BLK udevice |
Masahisa Kojima | 54ce3dd | 2024-01-12 09:19:21 +0900 | [diff] [blame] | 307 | * @dp: pointer to store the fallback boot device path |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 308 | * Return: status code |
| 309 | */ |
Masahisa Kojima | 54ce3dd | 2024-01-12 09:19:21 +0900 | [diff] [blame] | 310 | static efi_status_t fill_default_file_path(struct udevice *blk, |
| 311 | struct efi_device_path **dp) |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 312 | { |
| 313 | efi_status_t ret; |
| 314 | struct udevice *partition; |
| 315 | |
| 316 | /* image that has no partition table but a file system */ |
| 317 | ret = search_default_file(blk, dp); |
| 318 | if (ret == EFI_SUCCESS) |
| 319 | return ret; |
| 320 | |
| 321 | /* try the partitions */ |
| 322 | device_foreach_child(partition, blk) { |
| 323 | enum uclass_id id; |
| 324 | |
| 325 | id = device_get_uclass_id(partition); |
| 326 | if (id != UCLASS_PARTITION) |
| 327 | continue; |
| 328 | |
| 329 | ret = search_default_file(partition, dp); |
| 330 | if (ret == EFI_SUCCESS) |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | return EFI_NOT_FOUND; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * prepare_loaded_image() - prepare ramdisk for downloaded image |
| 339 | * |
| 340 | * @label: label of load option |
| 341 | * @addr: image address |
| 342 | * @size: image size |
| 343 | * @dp: pointer to default file device path |
| 344 | * @blk: pointer to created blk udevice |
| 345 | * Return: status code |
| 346 | */ |
| 347 | static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, |
| 348 | struct efi_device_path **dp, |
| 349 | struct udevice **blk) |
| 350 | { |
| 351 | efi_status_t ret; |
| 352 | struct udevice *ramdisk_blk; |
| 353 | |
| 354 | ramdisk_blk = mount_image(label, addr, size); |
| 355 | if (!ramdisk_blk) |
| 356 | return EFI_LOAD_ERROR; |
| 357 | |
Masahisa Kojima | 54ce3dd | 2024-01-12 09:19:21 +0900 | [diff] [blame] | 358 | ret = fill_default_file_path(ramdisk_blk, dp); |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 359 | if (ret != EFI_SUCCESS) { |
| 360 | log_info("Cannot boot from downloaded image\n"); |
| 361 | goto err; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * TODO: expose the ramdisk to OS. |
| 366 | * Need to pass the ramdisk information by the architecture-specific |
| 367 | * methods such as 'pmem' device-tree node. |
| 368 | */ |
| 369 | ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); |
| 370 | if (ret != EFI_SUCCESS) { |
| 371 | log_err("Memory reservation failed\n"); |
| 372 | goto err; |
| 373 | } |
| 374 | |
| 375 | *blk = ramdisk_blk; |
| 376 | |
| 377 | return EFI_SUCCESS; |
| 378 | |
| 379 | err: |
| 380 | if (blkmap_destroy(ramdisk_blk->parent)) |
| 381 | log_err("Destroying blkmap failed\n"); |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | /** |
Ilias Apalodimas | 32833cf | 2024-08-12 23:56:36 +0300 | [diff] [blame] | 387 | * efi_bootmgr_release_uridp() - cleanup uri device path resource |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 388 | * |
| 389 | * @ctx: event context |
| 390 | * Return: status code |
| 391 | */ |
Ilias Apalodimas | 55c0571 | 2024-10-26 10:37:32 +0300 | [diff] [blame] | 392 | static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx) |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 393 | { |
| 394 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 868937f | 2024-08-12 23:56:38 +0300 | [diff] [blame] | 395 | efi_status_t ret2 = EFI_SUCCESS; |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 396 | |
| 397 | if (!ctx) |
| 398 | return ret; |
| 399 | |
| 400 | /* cleanup for iso or img image */ |
| 401 | if (ctx->ramdisk_blk_dev) { |
| 402 | ret = efi_add_memory_map(ctx->image_addr, ctx->image_size, |
| 403 | EFI_CONVENTIONAL_MEMORY); |
| 404 | if (ret != EFI_SUCCESS) |
| 405 | log_err("Reclaiming memory failed\n"); |
| 406 | |
| 407 | if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) { |
| 408 | log_err("Destroying blkmap failed\n"); |
| 409 | ret = EFI_DEVICE_ERROR; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* cleanup for PE-COFF image */ |
| 414 | if (ctx->mem_handle) { |
Ilias Apalodimas | 868937f | 2024-08-12 23:56:38 +0300 | [diff] [blame] | 415 | ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle, |
| 416 | &efi_guid_device_path, |
| 417 | ctx->loaded_dp, |
| 418 | NULL); |
| 419 | if (ret2 != EFI_SUCCESS) |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 420 | log_err("Uninstall device_path protocol failed\n"); |
| 421 | } |
| 422 | |
| 423 | efi_free_pool(ctx->loaded_dp); |
| 424 | free(ctx); |
| 425 | |
Ilias Apalodimas | 868937f | 2024-08-12 23:56:38 +0300 | [diff] [blame] | 426 | return ret == EFI_SUCCESS ? ret2 : ret; |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
Ilias Apalodimas | fe14afc | 2024-08-12 23:56:37 +0300 | [diff] [blame] | 430 | * efi_bootmgr_http_return() - return to efibootmgr callback |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 431 | * |
| 432 | * @event: the event for which this notification function is registered |
| 433 | * @context: event context |
| 434 | */ |
Ilias Apalodimas | fe14afc | 2024-08-12 23:56:37 +0300 | [diff] [blame] | 435 | static void EFIAPI efi_bootmgr_http_return(struct efi_event *event, |
| 436 | void *context) |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 437 | { |
| 438 | efi_status_t ret; |
| 439 | |
| 440 | EFI_ENTRY("%p, %p", event, context); |
Ilias Apalodimas | 32833cf | 2024-08-12 23:56:36 +0300 | [diff] [blame] | 441 | ret = efi_bootmgr_release_uridp(context); |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 442 | EFI_EXIT(ret); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * try_load_from_uri_path() - Handle the URI device path |
| 447 | * |
| 448 | * @uridp: uri device path |
| 449 | * @lo_label: label of load option |
| 450 | * @handle: pointer to handle for newly installed image |
| 451 | * Return: status code |
| 452 | */ |
| 453 | static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, |
| 454 | u16 *lo_label, |
| 455 | efi_handle_t *handle) |
| 456 | { |
| 457 | char *s; |
| 458 | int err; |
| 459 | int uri_len; |
| 460 | efi_status_t ret; |
| 461 | void *source_buffer; |
| 462 | efi_uintn_t source_size; |
| 463 | struct uridp_context *ctx; |
| 464 | struct udevice *blk = NULL; |
| 465 | struct efi_event *event = NULL; |
| 466 | efi_handle_t mem_handle = NULL; |
| 467 | struct efi_device_path *loaded_dp; |
| 468 | static ulong image_size, image_addr; |
| 469 | |
| 470 | ctx = calloc(1, sizeof(struct uridp_context)); |
| 471 | if (!ctx) |
| 472 | return EFI_OUT_OF_RESOURCES; |
| 473 | |
| 474 | s = env_get("loadaddr"); |
| 475 | if (!s) { |
| 476 | log_err("Error: loadaddr is not set\n"); |
| 477 | ret = EFI_INVALID_PARAMETER; |
| 478 | goto err; |
| 479 | } |
| 480 | |
| 481 | image_addr = hextoul(s, NULL); |
| 482 | err = wget_with_dns(image_addr, uridp->uri); |
| 483 | if (err < 0) { |
| 484 | ret = EFI_INVALID_PARAMETER; |
| 485 | goto err; |
| 486 | } |
| 487 | |
| 488 | image_size = env_get_hex("filesize", 0); |
| 489 | if (!image_size) { |
| 490 | ret = EFI_INVALID_PARAMETER; |
| 491 | goto err; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * If the file extension is ".iso" or ".img", mount it and try to load |
| 496 | * the default file. |
| 497 | * If the file is PE-COFF image, load the downloaded file. |
| 498 | */ |
| 499 | uri_len = strlen(uridp->uri); |
| 500 | if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || |
| 501 | !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { |
| 502 | ret = prepare_loaded_image(lo_label, image_addr, image_size, |
| 503 | &loaded_dp, &blk); |
| 504 | if (ret != EFI_SUCCESS) |
| 505 | goto err; |
| 506 | |
| 507 | source_buffer = NULL; |
| 508 | source_size = 0; |
| 509 | } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { |
| 510 | /* |
| 511 | * loaded_dp must exist until efi application returns, |
| 512 | * will be freed in return_to_efibootmgr event callback. |
| 513 | */ |
| 514 | loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
| 515 | (uintptr_t)image_addr, image_size); |
| 516 | ret = efi_install_multiple_protocol_interfaces( |
| 517 | &mem_handle, &efi_guid_device_path, loaded_dp, NULL); |
| 518 | if (ret != EFI_SUCCESS) |
| 519 | goto err; |
| 520 | |
| 521 | source_buffer = (void *)image_addr; |
| 522 | source_size = image_size; |
| 523 | } else { |
| 524 | log_err("Error: file type is not supported\n"); |
| 525 | ret = EFI_UNSUPPORTED; |
| 526 | goto err; |
| 527 | } |
| 528 | |
| 529 | ctx->image_size = image_size; |
| 530 | ctx->image_addr = image_addr; |
| 531 | ctx->loaded_dp = loaded_dp; |
| 532 | ctx->ramdisk_blk_dev = blk; |
| 533 | ctx->mem_handle = mem_handle; |
| 534 | |
| 535 | ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, |
| 536 | source_size, handle)); |
| 537 | if (ret != EFI_SUCCESS) |
| 538 | goto err; |
| 539 | |
| 540 | /* create event for cleanup when the image returns or error occurs */ |
| 541 | ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Ilias Apalodimas | fe14afc | 2024-08-12 23:56:37 +0300 | [diff] [blame] | 542 | efi_bootmgr_http_return, ctx, |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 543 | &efi_guid_event_group_return_to_efibootmgr, |
| 544 | &event); |
| 545 | if (ret != EFI_SUCCESS) { |
| 546 | log_err("Creating event failed\n"); |
| 547 | goto err; |
| 548 | } |
| 549 | |
| 550 | return ret; |
| 551 | |
| 552 | err: |
Ilias Apalodimas | 32833cf | 2024-08-12 23:56:36 +0300 | [diff] [blame] | 553 | efi_bootmgr_release_uridp(ctx); |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | /** |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 559 | * try_load_from_media() - load file from media |
| 560 | * |
| 561 | * @file_path: file path |
| 562 | * @handle_img: on return handle for the newly installed image |
| 563 | * |
| 564 | * If @file_path contains a file name, load the file. |
| 565 | * If @file_path does not have a file name, search the architecture-specific |
| 566 | * fallback boot file and load it. |
| 567 | * TODO: If the FilePathList[0] device does not support |
| 568 | * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL, |
| 569 | * call EFI_BOOT_SERVICES.ConnectController() |
| 570 | * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL |
| 571 | * not based on EFI_BLOCK_IO_PROTOCOL |
| 572 | * |
| 573 | * Return: status code |
| 574 | */ |
| 575 | static efi_status_t try_load_from_media(struct efi_device_path *file_path, |
| 576 | efi_handle_t *handle_img) |
| 577 | { |
| 578 | efi_handle_t handle_blkdev; |
| 579 | efi_status_t ret = EFI_SUCCESS; |
| 580 | struct efi_device_path *rem, *dp = NULL; |
| 581 | struct efi_device_path *final_dp = file_path; |
| 582 | |
| 583 | handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem); |
| 584 | if (handle_blkdev) { |
| 585 | if (rem->type == DEVICE_PATH_TYPE_END) { |
| 586 | /* no file name present, try default file */ |
| 587 | ret = fill_default_file_path(handle_blkdev->dev, &dp); |
| 588 | if (ret != EFI_SUCCESS) |
| 589 | return ret; |
| 590 | |
| 591 | final_dp = dp; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img)); |
| 596 | |
| 597 | efi_free_pool(dp); |
| 598 | |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | /** |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 603 | * try_load_entry() - try to load image for boot option |
| 604 | * |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 605 | * Attempt to load load-option number 'n', returning device_path and file_path |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 606 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 607 | * and that the specified file to boot exists. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 608 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 609 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 610 | * @handle: on return handle for the newly installed image |
| 611 | * @load_options: load options set on the loaded image protocol |
| 612 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 613 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 614 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, |
| 615 | void **load_options) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 616 | { |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 617 | struct efi_load_option lo; |
Heinrich Schuchardt | c79cebe | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 618 | u16 varname[9]; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 619 | void *load_option; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 620 | efi_uintn_t size; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 621 | efi_status_t ret; |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 622 | u32 attributes; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 623 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 624 | *handle = NULL; |
| 625 | *load_options = NULL; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 626 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 627 | efi_create_indexed_name(varname, sizeof(varname), "Boot", n); |
Ilias Apalodimas | fc4ca6b | 2021-03-27 10:56:07 +0200 | [diff] [blame] | 628 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 629 | if (!load_option) |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 630 | return EFI_LOAD_ERROR; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 631 | |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 632 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 633 | if (ret != EFI_SUCCESS) { |
| 634 | log_warning("Invalid load option for %ls\n", varname); |
| 635 | goto error; |
| 636 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 637 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 638 | if (!(lo.attributes & LOAD_OPTION_ACTIVE)) { |
| 639 | ret = EFI_LOAD_ERROR; |
| 640 | goto error; |
| 641 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 642 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 643 | log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 644 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 645 | if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { |
| 646 | /* file_path doesn't contain a device path */ |
| 647 | ret = try_load_from_short_path(lo.file_path, handle); |
| 648 | } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { |
| 649 | if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) |
| 650 | ret = try_load_from_uri_path( |
| 651 | (struct efi_device_path_uri *)lo.file_path, |
| 652 | lo.label, handle); |
| 653 | else |
| 654 | ret = EFI_LOAD_ERROR; |
| 655 | } else { |
| 656 | ret = try_load_from_media(lo.file_path, handle); |
| 657 | } |
| 658 | if (ret != EFI_SUCCESS) { |
| 659 | log_warning("Loading %ls '%ls' failed\n", |
| 660 | varname, lo.label); |
| 661 | goto error; |
| 662 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 663 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 664 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 665 | EFI_VARIABLE_RUNTIME_ACCESS; |
| 666 | ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid, |
| 667 | attributes, sizeof(n), &n, false); |
| 668 | if (ret != EFI_SUCCESS) |
| 669 | goto error; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 670 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 671 | /* try to register load file2 for initrd's */ |
| 672 | if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) { |
| 673 | ret = efi_initrd_register(); |
| 674 | if (ret != EFI_SUCCESS) |
| 675 | goto error; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 678 | log_info("Booting: %ls\n", lo.label); |
| 679 | |
| 680 | /* Ignore the optional data in auto-generated boot options */ |
Masahisa Kojima | 767a9e6 | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 681 | if (size >= sizeof(efi_guid_t) && |
| 682 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) |
| 683 | size = 0; |
| 684 | |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 685 | /* Set optional data in loaded file protocol */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 686 | if (size) { |
| 687 | *load_options = malloc(size); |
| 688 | if (!*load_options) { |
| 689 | ret = EFI_OUT_OF_RESOURCES; |
| 690 | goto error; |
| 691 | } |
| 692 | memcpy(*load_options, lo.optional_data, size); |
| 693 | ret = efi_set_load_options(*handle, size, *load_options); |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 694 | if (ret != EFI_SUCCESS) |
| 695 | free(load_options); |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 698 | error: |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 699 | if (ret != EFI_SUCCESS && *handle && |
| 700 | EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS) |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 701 | log_err("Unloading image failed\n"); |
Heinrich Schuchardt | d41005b | 2024-04-22 10:41:00 +0200 | [diff] [blame] | 702 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 703 | free(load_option); |
| 704 | |
| 705 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 708 | /** |
| 709 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 710 | * |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 711 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 712 | * EFI variable, the available load-options, finding and returning |
| 713 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 714 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 715 | * @handle: on return handle for the newly installed image |
| 716 | * @load_options: load options set on the loaded image protocol |
| 717 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 718 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 719 | efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 720 | { |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 721 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 722 | efi_uintn_t size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 723 | int i, num; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 724 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 725 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 726 | bs = systab.boottime; |
| 727 | rs = systab.runtime; |
| 728 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 729 | /* BootNext */ |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 730 | size = sizeof(bootnext); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 731 | ret = efi_get_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 732 | &efi_global_variable_guid, |
| 733 | NULL, &size, &bootnext, NULL); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 734 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 735 | /* BootNext does exist here */ |
| 736 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 737 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 738 | |
| 739 | /* delete BootNext */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 740 | ret = efi_set_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 741 | &efi_global_variable_guid, |
| 742 | 0, 0, NULL, false); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 743 | |
| 744 | /* load BootNext */ |
| 745 | if (ret == EFI_SUCCESS) { |
| 746 | if (size == sizeof(u16)) { |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 747 | ret = try_load_entry(bootnext, handle, |
| 748 | load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 749 | if (ret == EFI_SUCCESS) |
| 750 | return ret; |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 751 | log_warning( |
| 752 | "Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 753 | } |
| 754 | } else { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 755 | log_err("Deleting BootNext failed\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | /* BootOrder */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 760 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 761 | if (!bootorder) { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 762 | log_info("BootOrder not defined\n"); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 763 | ret = EFI_NOT_FOUND; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 764 | goto error; |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 765 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 766 | |
| 767 | num = size / sizeof(uint16_t); |
| 768 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | e07fe5c | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 769 | log_debug("trying to load Boot%04X\n", bootorder[i]); |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 770 | ret = try_load_entry(bootorder[i], handle, load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 771 | if (ret == EFI_SUCCESS) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 772 | break; |
| 773 | } |
| 774 | |
| 775 | free(bootorder); |
| 776 | |
| 777 | error: |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 778 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 779 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 780 | |
| 781 | /** |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 782 | * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 783 | * |
| 784 | * @opt: pointer to the media boot option structure |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 785 | * @index: index of the opt array to store the boot option |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 786 | * @handles: pointer to block device handles |
| 787 | * @count: On entry number of handles to block devices. |
| 788 | * On exit number of boot options. |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 789 | * @removable: flag to parse removable only |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 790 | * Return: status code |
| 791 | */ |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 792 | static efi_status_t |
| 793 | efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt, |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 794 | efi_uintn_t index, efi_handle_t *handles, |
| 795 | efi_uintn_t *count, bool removable) |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 796 | { |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 797 | u32 i, num = index; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 798 | struct efi_handler *handler; |
| 799 | efi_status_t ret = EFI_SUCCESS; |
| 800 | |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 801 | for (i = 0; i < *count; i++) { |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 802 | u16 *p; |
| 803 | u16 dev_name[BOOTMENU_DEVICE_NAME_MAX]; |
| 804 | char *optional_data; |
| 805 | struct efi_load_option lo; |
| 806 | char buf[BOOTMENU_DEVICE_NAME_MAX]; |
| 807 | struct efi_device_path *device_path; |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 808 | struct efi_device_path *short_dp; |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 809 | struct efi_block_io *blkio; |
| 810 | |
| 811 | ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler); |
| 812 | blkio = handler->protocol_interface; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 813 | |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 814 | if (blkio->media->logical_partition) |
| 815 | continue; |
| 816 | |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 817 | if (removable != (blkio->media->removable_media != 0)) |
| 818 | continue; |
| 819 | |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 820 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 821 | if (ret != EFI_SUCCESS) |
| 822 | continue; |
| 823 | ret = efi_protocol_open(handler, (void **)&device_path, |
| 824 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 825 | if (ret != EFI_SUCCESS) |
| 826 | continue; |
| 827 | |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 828 | ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 829 | if (ret != EFI_SUCCESS) |
| 830 | continue; |
| 831 | |
| 832 | p = dev_name; |
| 833 | utf8_utf16_strncpy(&p, buf, strlen(buf)); |
| 834 | |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 835 | /* prefer to short form device path */ |
| 836 | short_dp = efi_dp_shorten(device_path); |
| 837 | if (short_dp) |
| 838 | device_path = short_dp; |
| 839 | |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 840 | lo.label = dev_name; |
| 841 | lo.attributes = LOAD_OPTION_ACTIVE; |
| 842 | lo.file_path = device_path; |
| 843 | lo.file_path_length = efi_dp_size(device_path) + sizeof(END); |
| 844 | /* |
| 845 | * Set the dedicated guid to optional_data, it is used to identify |
| 846 | * the boot option that automatically generated by the bootmenu. |
| 847 | * efi_serialize_load_option() expects optional_data is null-terminated |
| 848 | * utf8 string, so set the "1234567" string to allocate enough space |
| 849 | * to store guid, instead of realloc the load_option. |
| 850 | */ |
| 851 | lo.optional_data = "1234567"; |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 852 | opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo); |
| 853 | if (!opt[num].size) { |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 854 | ret = EFI_OUT_OF_RESOURCES; |
| 855 | goto out; |
| 856 | } |
| 857 | /* set the guid */ |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 858 | optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567")); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 859 | memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t)); |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 860 | num++; |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 861 | |
| 862 | if (num >= *count) |
| 863 | break; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | out: |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 867 | *count = num; |
| 868 | |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option |
| 874 | * |
| 875 | * @opt: pointer to the media boot option structure |
| 876 | * @count: number of media boot option structure |
| 877 | * Return: status code |
| 878 | */ |
| 879 | static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt, |
| 880 | efi_status_t count) |
| 881 | { |
| 882 | efi_uintn_t size; |
| 883 | void *load_option; |
| 884 | u32 i, list_size = 0; |
| 885 | struct efi_load_option lo; |
| 886 | u16 *var_name16 = NULL; |
| 887 | u16 varname[] = u"Boot####"; |
| 888 | efi_status_t ret = EFI_SUCCESS; |
| 889 | u16 *delete_index_list = NULL, *p; |
| 890 | efi_uintn_t buf_size; |
| 891 | |
| 892 | buf_size = 128; |
| 893 | var_name16 = malloc(buf_size); |
| 894 | if (!var_name16) |
| 895 | return EFI_OUT_OF_RESOURCES; |
| 896 | |
| 897 | var_name16[0] = 0; |
| 898 | for (;;) { |
| 899 | int index; |
| 900 | efi_guid_t guid; |
| 901 | efi_uintn_t tmp; |
| 902 | |
| 903 | ret = efi_next_variable_name(&buf_size, &var_name16, &guid); |
| 904 | if (ret == EFI_NOT_FOUND) { |
| 905 | /* |
| 906 | * EFI_NOT_FOUND indicates we retrieved all EFI variables. |
| 907 | * This should be treated as success. |
| 908 | */ |
| 909 | ret = EFI_SUCCESS; |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | if (ret != EFI_SUCCESS) |
| 914 | goto out; |
| 915 | |
| 916 | if (!efi_varname_is_load_option(var_name16, &index)) |
| 917 | continue; |
| 918 | |
| 919 | efi_create_indexed_name(varname, sizeof(varname), "Boot", index); |
| 920 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 921 | if (!load_option) |
| 922 | continue; |
| 923 | |
| 924 | tmp = size; |
| 925 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 926 | if (ret != EFI_SUCCESS) |
| 927 | goto next; |
| 928 | |
| 929 | if (size >= sizeof(efi_guid_bootmenu_auto_generated) && |
| 930 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { |
| 931 | for (i = 0; i < count; i++) { |
| 932 | if (opt[i].size == tmp && |
| 933 | memcmp(opt[i].lo, load_option, tmp) == 0) { |
| 934 | opt[i].exist = true; |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * The entire list of variables must be retrieved by |
| 941 | * efi_get_next_variable_name_int() before deleting the invalid |
| 942 | * boot option, just save the index here. |
| 943 | */ |
| 944 | if (i == count) { |
| 945 | p = realloc(delete_index_list, sizeof(u32) * |
| 946 | (list_size + 1)); |
| 947 | if (!p) { |
| 948 | ret = EFI_OUT_OF_RESOURCES; |
| 949 | goto out; |
| 950 | } |
| 951 | delete_index_list = p; |
| 952 | delete_index_list[list_size++] = index; |
| 953 | } |
| 954 | } |
| 955 | next: |
| 956 | free(load_option); |
| 957 | } |
| 958 | |
| 959 | /* delete all invalid boot options */ |
| 960 | for (i = 0; i < list_size; i++) { |
| 961 | ret = efi_bootmgr_delete_boot_option(delete_index_list[i]); |
| 962 | if (ret != EFI_SUCCESS) |
| 963 | goto out; |
| 964 | } |
| 965 | |
| 966 | out: |
| 967 | free(var_name16); |
| 968 | free(delete_index_list); |
| 969 | |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index |
| 975 | * |
| 976 | * @buf: pointer to the buffer to store boot option variable name |
| 977 | * @buf_size: buffer size |
| 978 | * @index: pointer to store the index in the BootOrder variable |
| 979 | * Return: status code |
| 980 | */ |
| 981 | efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size, |
| 982 | unsigned int *index) |
| 983 | { |
| 984 | u32 i; |
| 985 | efi_status_t ret; |
| 986 | efi_uintn_t size; |
| 987 | |
| 988 | if (buf_size < u16_strsize(u"Boot####")) |
| 989 | return EFI_BUFFER_TOO_SMALL; |
| 990 | |
| 991 | for (i = 0; i <= 0xFFFF; i++) { |
| 992 | size = 0; |
| 993 | efi_create_indexed_name(buf, buf_size, "Boot", i); |
| 994 | ret = efi_get_variable_int(buf, &efi_global_variable_guid, |
| 995 | NULL, &size, NULL, NULL); |
| 996 | if (ret == EFI_BUFFER_TOO_SMALL) |
| 997 | continue; |
| 998 | else |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | if (i > 0xFFFF) |
| 1003 | return EFI_OUT_OF_RESOURCES; |
| 1004 | |
| 1005 | *index = i; |
| 1006 | |
| 1007 | return EFI_SUCCESS; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable |
| 1012 | * |
| 1013 | * @index: "Boot####" index to append to BootOrder variable |
| 1014 | * Return: status code |
| 1015 | */ |
| 1016 | efi_status_t efi_bootmgr_append_bootorder(u16 index) |
| 1017 | { |
| 1018 | u16 *bootorder; |
| 1019 | efi_status_t ret; |
| 1020 | u16 *new_bootorder = NULL; |
| 1021 | efi_uintn_t last, size, new_size; |
| 1022 | |
| 1023 | /* append new boot option */ |
| 1024 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 1025 | last = size / sizeof(u16); |
| 1026 | new_size = size + sizeof(u16); |
| 1027 | new_bootorder = calloc(1, new_size); |
| 1028 | if (!new_bootorder) { |
| 1029 | ret = EFI_OUT_OF_RESOURCES; |
| 1030 | goto out; |
| 1031 | } |
| 1032 | memcpy(new_bootorder, bootorder, size); |
| 1033 | new_bootorder[last] = index; |
| 1034 | |
| 1035 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1036 | EFI_VARIABLE_NON_VOLATILE | |
| 1037 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1038 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1039 | new_size, new_bootorder, false); |
| 1040 | if (ret != EFI_SUCCESS) |
| 1041 | goto out; |
| 1042 | |
| 1043 | out: |
| 1044 | free(bootorder); |
| 1045 | free(new_bootorder); |
| 1046 | |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * efi_bootmgr_delete_boot_option() - delete selected boot option |
| 1052 | * |
| 1053 | * @boot_index: boot option index to delete |
| 1054 | * Return: status code |
| 1055 | */ |
| 1056 | efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index) |
| 1057 | { |
| 1058 | u16 *bootorder; |
| 1059 | u16 varname[9]; |
| 1060 | efi_status_t ret; |
| 1061 | unsigned int index; |
| 1062 | efi_uintn_t num, size; |
| 1063 | |
| 1064 | efi_create_indexed_name(varname, sizeof(varname), |
| 1065 | "Boot", boot_index); |
| 1066 | ret = efi_set_variable_int(varname, &efi_global_variable_guid, |
| 1067 | 0, 0, NULL, false); |
| 1068 | if (ret != EFI_SUCCESS) { |
| 1069 | log_err("delete boot option(%ls) failed\n", varname); |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | /* update BootOrder if necessary */ |
| 1074 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 1075 | if (!bootorder) |
| 1076 | return EFI_SUCCESS; |
| 1077 | |
| 1078 | num = size / sizeof(u16); |
| 1079 | if (!efi_search_bootorder(bootorder, num, boot_index, &index)) |
| 1080 | return EFI_SUCCESS; |
| 1081 | |
| 1082 | memmove(&bootorder[index], &bootorder[index + 1], |
| 1083 | (num - index - 1) * sizeof(u16)); |
| 1084 | size -= sizeof(u16); |
| 1085 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1086 | EFI_VARIABLE_NON_VOLATILE | |
| 1087 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1088 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1089 | size, bootorder, false); |
| 1090 | |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option |
| 1096 | * |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 1097 | * This function enumerates all BlockIo devices and add the boot option for it. |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1098 | * This function also provide the BOOT#### variable maintenance for |
| 1099 | * the media device entries. |
| 1100 | * - Automatically create the BOOT#### variable for the newly detected device, |
| 1101 | * this BOOT#### variable is distinguished by the special GUID |
| 1102 | * stored in the EFI_LOAD_OPTION.optional_data |
| 1103 | * - If the device is not attached to the system, the associated BOOT#### variable |
| 1104 | * is automatically deleted. |
| 1105 | * |
| 1106 | * Return: status code |
| 1107 | */ |
| 1108 | efi_status_t efi_bootmgr_update_media_device_boot_option(void) |
| 1109 | { |
| 1110 | u32 i; |
| 1111 | efi_status_t ret; |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1112 | efi_uintn_t count, num, total; |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 1113 | efi_handle_t *handles = NULL; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1114 | struct eficonfig_media_boot_option *opt = NULL; |
| 1115 | |
| 1116 | ret = efi_locate_handle_buffer_int(BY_PROTOCOL, |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 1117 | &efi_block_io_guid, |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1118 | NULL, &count, |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 1119 | (efi_handle_t **)&handles); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1120 | if (ret != EFI_SUCCESS) |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1121 | goto out; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1122 | |
| 1123 | opt = calloc(count, sizeof(struct eficonfig_media_boot_option)); |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1124 | if (!opt) { |
| 1125 | ret = EFI_OUT_OF_RESOURCES; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1126 | goto out; |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1127 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1128 | |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1129 | /* parse removable block io followed by fixed block io */ |
| 1130 | num = count; |
| 1131 | ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1132 | if (ret != EFI_SUCCESS) |
| 1133 | goto out; |
| 1134 | |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1135 | total = num; |
| 1136 | num = count; |
| 1137 | ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false); |
| 1138 | if (ret != EFI_SUCCESS) |
| 1139 | goto out; |
| 1140 | |
| 1141 | total = num; |
| 1142 | |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | * System hardware configuration may vary depending on the user setup. |
| 1145 | * The boot option is automatically added by the bootmenu. |
| 1146 | * If the device is not attached to the system, the boot option needs |
| 1147 | * to be deleted. |
| 1148 | */ |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1149 | ret = efi_bootmgr_delete_invalid_boot_option(opt, total); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1150 | if (ret != EFI_SUCCESS) |
| 1151 | goto out; |
| 1152 | |
| 1153 | /* add non-existent boot option */ |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1154 | for (i = 0; i < total; i++) { |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1155 | u32 boot_index; |
| 1156 | u16 var_name[9]; |
| 1157 | |
| 1158 | if (!opt[i].exist) { |
| 1159 | ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name), |
| 1160 | &boot_index); |
| 1161 | if (ret != EFI_SUCCESS) |
| 1162 | goto out; |
| 1163 | |
| 1164 | ret = efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 1165 | EFI_VARIABLE_NON_VOLATILE | |
| 1166 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1167 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1168 | opt[i].size, opt[i].lo, false); |
| 1169 | if (ret != EFI_SUCCESS) |
| 1170 | goto out; |
| 1171 | |
| 1172 | ret = efi_bootmgr_append_bootorder(boot_index); |
| 1173 | if (ret != EFI_SUCCESS) { |
| 1174 | efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 1175 | 0, 0, NULL, false); |
| 1176 | goto out; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | out: |
| 1182 | if (opt) { |
Masahisa Kojima | e0d1424 | 2024-01-12 09:19:23 +0900 | [diff] [blame] | 1183 | for (i = 0; i < total; i++) |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1184 | free(opt[i].lo); |
| 1185 | } |
| 1186 | free(opt); |
Masahisa Kojima | 19410bb | 2024-01-12 09:19:22 +0900 | [diff] [blame] | 1187 | efi_free_pool(handles); |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1188 | |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1189 | if (ret == EFI_NOT_FOUND) |
| 1190 | return EFI_SUCCESS; |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1191 | return ret; |
| 1192 | } |
| 1193 | |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1194 | /** |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1195 | * load_fdt_from_load_option - load device-tree from load option |
| 1196 | * |
| 1197 | * @fdt: pointer to loaded device-tree or NULL |
| 1198 | * Return: status code |
| 1199 | */ |
| 1200 | static efi_status_t load_fdt_from_load_option(void **fdt) |
| 1201 | { |
| 1202 | struct efi_device_path *dp = NULL; |
| 1203 | struct efi_file_handle *f = NULL; |
| 1204 | efi_uintn_t filesize; |
| 1205 | efi_status_t ret; |
| 1206 | |
| 1207 | *fdt = NULL; |
| 1208 | |
| 1209 | dp = efi_get_dp_from_boot(&efi_guid_fdt); |
| 1210 | if (!dp) |
| 1211 | return EFI_SUCCESS; |
| 1212 | |
| 1213 | /* Open file */ |
| 1214 | f = efi_file_from_path(dp); |
| 1215 | if (!f) { |
| 1216 | log_err("Can't find %pD specified in Boot####\n", dp); |
| 1217 | ret = EFI_NOT_FOUND; |
| 1218 | goto out; |
| 1219 | } |
| 1220 | |
| 1221 | /* Get file size */ |
| 1222 | ret = efi_file_size(f, &filesize); |
| 1223 | if (ret != EFI_SUCCESS) |
| 1224 | goto out; |
| 1225 | |
| 1226 | *fdt = calloc(1, filesize); |
| 1227 | if (!*fdt) { |
| 1228 | log_err("Out of memory\n"); |
| 1229 | ret = EFI_OUT_OF_RESOURCES; |
| 1230 | goto out; |
| 1231 | } |
| 1232 | ret = EFI_CALL(f->read(f, &filesize, *fdt)); |
| 1233 | if (ret != EFI_SUCCESS) { |
| 1234 | log_err("Can't read fdt\n"); |
| 1235 | free(*fdt); |
| 1236 | *fdt = NULL; |
| 1237 | } |
| 1238 | |
| 1239 | out: |
| 1240 | efi_free_pool(dp); |
| 1241 | if (f) |
| 1242 | EFI_CALL(f->close(f)); |
| 1243 | |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
| 1247 | /** |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1248 | * efi_bootmgr_run() - execute EFI boot manager |
| 1249 | * @fdt: Flat device tree |
| 1250 | * |
| 1251 | * Invoke EFI boot manager and execute a binary depending on |
| 1252 | * boot options. If @fdt is not NULL, it will be passed to |
| 1253 | * the executed binary. |
| 1254 | * |
| 1255 | * Return: status code |
| 1256 | */ |
| 1257 | efi_status_t efi_bootmgr_run(void *fdt) |
| 1258 | { |
| 1259 | efi_handle_t handle; |
| 1260 | void *load_options; |
| 1261 | efi_status_t ret; |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 1262 | void *fdt_lo, *fdt_distro = NULL; |
| 1263 | efi_uintn_t fdt_size; |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1264 | |
| 1265 | /* Initialize EFI drivers */ |
| 1266 | ret = efi_init_obj_list(); |
| 1267 | if (ret != EFI_SUCCESS) { |
| 1268 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1269 | ret & ~EFI_ERROR_MASK); |
| 1270 | return CMD_RET_FAILURE; |
| 1271 | } |
| 1272 | |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1273 | ret = efi_bootmgr_load(&handle, &load_options); |
| 1274 | if (ret != EFI_SUCCESS) { |
| 1275 | log_notice("EFI boot manager: Cannot load any image\n"); |
| 1276 | return ret; |
| 1277 | } |
| 1278 | |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1279 | if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) { |
| 1280 | ret = load_fdt_from_load_option(&fdt_lo); |
| 1281 | if (ret != EFI_SUCCESS) |
| 1282 | return ret; |
| 1283 | if (fdt_lo) |
| 1284 | fdt = fdt_lo; |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 1285 | if (!fdt) { |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 1286 | efi_load_distro_fdt(handle, &fdt_distro, &fdt_size); |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 1287 | fdt = fdt_distro; |
| 1288 | } |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Needed in ACPI case to create reservations based on |
| 1293 | * control device-tree. |
| 1294 | */ |
Heinrich Schuchardt | 00df4ad | 2024-04-22 11:03:10 +0200 | [diff] [blame] | 1295 | ret = efi_install_fdt(fdt); |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1296 | |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 1297 | if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) { |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1298 | free(fdt_lo); |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 1299 | if (fdt_distro) |
| 1300 | efi_free_pages((uintptr_t)fdt_distro, |
| 1301 | efi_size_in_pages(fdt_size)); |
| 1302 | } |
Heinrich Schuchardt | dfeb042 | 2024-04-26 16:13:17 +0200 | [diff] [blame] | 1303 | |
Heinrich Schuchardt | 00df4ad | 2024-04-22 11:03:10 +0200 | [diff] [blame] | 1304 | if (ret != EFI_SUCCESS) { |
| 1305 | if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS) |
| 1306 | free(load_options); |
| 1307 | else |
| 1308 | log_err("Unloading image failed\n"); |
| 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1313 | return do_bootefi_exec(handle, load_options); |
| 1314 | } |