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