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 | |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 133 | dp = efi_dp_concat(dp, fp, false); |
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 | /** |
| 383 | * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource |
| 384 | * |
| 385 | * @ctx: event context |
| 386 | * Return: status code |
| 387 | */ |
| 388 | efi_status_t efi_bootmgr_release_uridp_resource(struct uridp_context *ctx) |
| 389 | { |
| 390 | efi_status_t ret = EFI_SUCCESS; |
| 391 | |
| 392 | if (!ctx) |
| 393 | return ret; |
| 394 | |
| 395 | /* cleanup for iso or img image */ |
| 396 | if (ctx->ramdisk_blk_dev) { |
| 397 | ret = efi_add_memory_map(ctx->image_addr, ctx->image_size, |
| 398 | EFI_CONVENTIONAL_MEMORY); |
| 399 | if (ret != EFI_SUCCESS) |
| 400 | log_err("Reclaiming memory failed\n"); |
| 401 | |
| 402 | if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) { |
| 403 | log_err("Destroying blkmap failed\n"); |
| 404 | ret = EFI_DEVICE_ERROR; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* cleanup for PE-COFF image */ |
| 409 | if (ctx->mem_handle) { |
| 410 | ret = efi_uninstall_multiple_protocol_interfaces( |
| 411 | ctx->mem_handle, &efi_guid_device_path, ctx->loaded_dp, |
| 412 | NULL); |
| 413 | if (ret != EFI_SUCCESS) |
| 414 | log_err("Uninstall device_path protocol failed\n"); |
| 415 | } |
| 416 | |
| 417 | efi_free_pool(ctx->loaded_dp); |
| 418 | free(ctx); |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * efi_bootmgr_image_return_notify() - return to efibootmgr callback |
| 425 | * |
| 426 | * @event: the event for which this notification function is registered |
| 427 | * @context: event context |
| 428 | */ |
| 429 | static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, |
| 430 | void *context) |
| 431 | { |
| 432 | efi_status_t ret; |
| 433 | |
| 434 | EFI_ENTRY("%p, %p", event, context); |
| 435 | ret = efi_bootmgr_release_uridp_resource(context); |
| 436 | EFI_EXIT(ret); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * try_load_from_uri_path() - Handle the URI device path |
| 441 | * |
| 442 | * @uridp: uri device path |
| 443 | * @lo_label: label of load option |
| 444 | * @handle: pointer to handle for newly installed image |
| 445 | * Return: status code |
| 446 | */ |
| 447 | static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, |
| 448 | u16 *lo_label, |
| 449 | efi_handle_t *handle) |
| 450 | { |
| 451 | char *s; |
| 452 | int err; |
| 453 | int uri_len; |
| 454 | efi_status_t ret; |
| 455 | void *source_buffer; |
| 456 | efi_uintn_t source_size; |
| 457 | struct uridp_context *ctx; |
| 458 | struct udevice *blk = NULL; |
| 459 | struct efi_event *event = NULL; |
| 460 | efi_handle_t mem_handle = NULL; |
| 461 | struct efi_device_path *loaded_dp; |
| 462 | static ulong image_size, image_addr; |
| 463 | |
| 464 | ctx = calloc(1, sizeof(struct uridp_context)); |
| 465 | if (!ctx) |
| 466 | return EFI_OUT_OF_RESOURCES; |
| 467 | |
| 468 | s = env_get("loadaddr"); |
| 469 | if (!s) { |
| 470 | log_err("Error: loadaddr is not set\n"); |
| 471 | ret = EFI_INVALID_PARAMETER; |
| 472 | goto err; |
| 473 | } |
| 474 | |
| 475 | image_addr = hextoul(s, NULL); |
| 476 | err = wget_with_dns(image_addr, uridp->uri); |
| 477 | if (err < 0) { |
| 478 | ret = EFI_INVALID_PARAMETER; |
| 479 | goto err; |
| 480 | } |
| 481 | |
| 482 | image_size = env_get_hex("filesize", 0); |
| 483 | if (!image_size) { |
| 484 | ret = EFI_INVALID_PARAMETER; |
| 485 | goto err; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * If the file extension is ".iso" or ".img", mount it and try to load |
| 490 | * the default file. |
| 491 | * If the file is PE-COFF image, load the downloaded file. |
| 492 | */ |
| 493 | uri_len = strlen(uridp->uri); |
| 494 | if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || |
| 495 | !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { |
| 496 | ret = prepare_loaded_image(lo_label, image_addr, image_size, |
| 497 | &loaded_dp, &blk); |
| 498 | if (ret != EFI_SUCCESS) |
| 499 | goto err; |
| 500 | |
| 501 | source_buffer = NULL; |
| 502 | source_size = 0; |
| 503 | } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { |
| 504 | /* |
| 505 | * loaded_dp must exist until efi application returns, |
| 506 | * will be freed in return_to_efibootmgr event callback. |
| 507 | */ |
| 508 | loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
| 509 | (uintptr_t)image_addr, image_size); |
| 510 | ret = efi_install_multiple_protocol_interfaces( |
| 511 | &mem_handle, &efi_guid_device_path, loaded_dp, NULL); |
| 512 | if (ret != EFI_SUCCESS) |
| 513 | goto err; |
| 514 | |
| 515 | source_buffer = (void *)image_addr; |
| 516 | source_size = image_size; |
| 517 | } else { |
| 518 | log_err("Error: file type is not supported\n"); |
| 519 | ret = EFI_UNSUPPORTED; |
| 520 | goto err; |
| 521 | } |
| 522 | |
| 523 | ctx->image_size = image_size; |
| 524 | ctx->image_addr = image_addr; |
| 525 | ctx->loaded_dp = loaded_dp; |
| 526 | ctx->ramdisk_blk_dev = blk; |
| 527 | ctx->mem_handle = mem_handle; |
| 528 | |
| 529 | ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, |
| 530 | source_size, handle)); |
| 531 | if (ret != EFI_SUCCESS) |
| 532 | goto err; |
| 533 | |
| 534 | /* create event for cleanup when the image returns or error occurs */ |
| 535 | ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
| 536 | efi_bootmgr_image_return_notify, ctx, |
| 537 | &efi_guid_event_group_return_to_efibootmgr, |
| 538 | &event); |
| 539 | if (ret != EFI_SUCCESS) { |
| 540 | log_err("Creating event failed\n"); |
| 541 | goto err; |
| 542 | } |
| 543 | |
| 544 | return ret; |
| 545 | |
| 546 | err: |
| 547 | efi_bootmgr_release_uridp_resource(ctx); |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /** |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 553 | * try_load_entry() - try to load image for boot option |
| 554 | * |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 555 | * 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] | 556 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 557 | * and that the specified file to boot exists. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 558 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 559 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 560 | * @handle: on return handle for the newly installed image |
| 561 | * @load_options: load options set on the loaded image protocol |
| 562 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 563 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 564 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, |
| 565 | void **load_options) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 566 | { |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 567 | struct efi_load_option lo; |
Heinrich Schuchardt | c79cebe | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 568 | u16 varname[9]; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 569 | void *load_option; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 570 | efi_uintn_t size; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 571 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 572 | |
Heinrich Schuchardt | c79cebe | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 573 | efi_create_indexed_name(varname, sizeof(varname), "Boot", n); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 574 | |
Ilias Apalodimas | fc4ca6b | 2021-03-27 10:56:07 +0200 | [diff] [blame] | 575 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 576 | if (!load_option) |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 577 | return EFI_LOAD_ERROR; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 578 | |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 579 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 580 | if (ret != EFI_SUCCESS) { |
| 581 | log_warning("Invalid load option for %ls\n", varname); |
| 582 | goto error; |
| 583 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 584 | |
| 585 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 586 | struct efi_device_path *file_path; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 587 | u32 attributes; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 588 | |
Heinrich Schuchardt | e07fe5c | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 589 | log_debug("trying to load \"%ls\" from %pD\n", lo.label, |
| 590 | lo.file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 591 | |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 592 | if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { |
| 593 | /* file_path doesn't contain a device path */ |
| 594 | ret = try_load_from_short_path(lo.file_path, handle); |
Masahisa Kojima | 949c441 | 2023-11-10 13:25:40 +0900 | [diff] [blame] | 595 | } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { |
| 596 | if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) |
| 597 | ret = try_load_from_uri_path( |
| 598 | (struct efi_device_path_uri *)lo.file_path, |
| 599 | lo.label, handle); |
| 600 | else |
| 601 | ret = EFI_LOAD_ERROR; |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 602 | } else { |
| 603 | file_path = expand_media_path(lo.file_path); |
| 604 | ret = EFI_CALL(efi_load_image(true, efi_root, file_path, |
| 605 | NULL, 0, handle)); |
| 606 | efi_free_pool(file_path); |
| 607 | } |
AKASHI Takahiro | 15db9ce | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 608 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 609 | log_warning("Loading %ls '%ls' failed\n", |
| 610 | varname, lo.label); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 611 | goto error; |
AKASHI Takahiro | 15db9ce | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 612 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 613 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 614 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 615 | EFI_VARIABLE_RUNTIME_ACCESS; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 616 | ret = efi_set_variable_int(u"BootCurrent", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 617 | &efi_global_variable_guid, |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 618 | attributes, sizeof(n), &n, false); |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 619 | if (ret != EFI_SUCCESS) |
| 620 | goto unload; |
| 621 | /* try to register load file2 for initrd's */ |
| 622 | if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) { |
| 623 | ret = efi_initrd_register(); |
| 624 | if (ret != EFI_SUCCESS) |
| 625 | goto unload; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 626 | } |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 627 | |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 628 | log_info("Booting: %ls\n", lo.label); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 629 | } else { |
| 630 | ret = EFI_LOAD_ERROR; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 631 | } |
| 632 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 633 | /* Set load options */ |
Masahisa Kojima | 767a9e6 | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 634 | if (size >= sizeof(efi_guid_t) && |
| 635 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) |
| 636 | size = 0; |
| 637 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 638 | if (size) { |
| 639 | *load_options = malloc(size); |
| 640 | if (!*load_options) { |
| 641 | ret = EFI_OUT_OF_RESOURCES; |
| 642 | goto error; |
| 643 | } |
| 644 | memcpy(*load_options, lo.optional_data, size); |
| 645 | ret = efi_set_load_options(*handle, size, *load_options); |
| 646 | } else { |
Heinrich Schuchardt | 7c2a859 | 2020-12-27 15:46:00 +0100 | [diff] [blame] | 647 | *load_options = NULL; |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 650 | error: |
| 651 | free(load_option); |
| 652 | |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 653 | return ret; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 654 | |
| 655 | unload: |
| 656 | if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS) |
| 657 | log_err("Unloading image failed\n"); |
| 658 | free(load_option); |
| 659 | |
| 660 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 661 | } |
| 662 | |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 663 | /** |
| 664 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 665 | * |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 666 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 667 | * EFI variable, the available load-options, finding and returning |
| 668 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 669 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 670 | * @handle: on return handle for the newly installed image |
| 671 | * @load_options: load options set on the loaded image protocol |
| 672 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 673 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 674 | 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] | 675 | { |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 676 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 677 | efi_uintn_t size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 678 | int i, num; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 679 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 680 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 681 | bs = systab.boottime; |
| 682 | rs = systab.runtime; |
| 683 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 684 | /* BootNext */ |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 685 | size = sizeof(bootnext); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 686 | ret = efi_get_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 687 | &efi_global_variable_guid, |
| 688 | NULL, &size, &bootnext, NULL); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 689 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 690 | /* BootNext does exist here */ |
| 691 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 692 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 693 | |
| 694 | /* delete BootNext */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 695 | ret = efi_set_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 696 | &efi_global_variable_guid, |
| 697 | 0, 0, NULL, false); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 698 | |
| 699 | /* load BootNext */ |
| 700 | if (ret == EFI_SUCCESS) { |
| 701 | if (size == sizeof(u16)) { |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 702 | ret = try_load_entry(bootnext, handle, |
| 703 | load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 704 | if (ret == EFI_SUCCESS) |
| 705 | return ret; |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 706 | log_warning( |
| 707 | "Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 708 | } |
| 709 | } else { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 710 | log_err("Deleting BootNext failed\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | /* BootOrder */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 715 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 716 | if (!bootorder) { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 717 | log_info("BootOrder not defined\n"); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 718 | ret = EFI_NOT_FOUND; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 719 | goto error; |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 720 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 721 | |
| 722 | num = size / sizeof(uint16_t); |
| 723 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | e07fe5c | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 724 | log_debug("trying to load Boot%04X\n", bootorder[i]); |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 725 | ret = try_load_entry(bootorder[i], handle, load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 726 | if (ret == EFI_SUCCESS) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | |
| 730 | free(bootorder); |
| 731 | |
| 732 | error: |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 733 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 734 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 735 | |
| 736 | /** |
| 737 | * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media |
| 738 | * |
| 739 | * @opt: pointer to the media boot option structure |
| 740 | * @volume_handles: pointer to the efi handles |
| 741 | * @count: number of efi handle |
| 742 | * Return: status code |
| 743 | */ |
| 744 | static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt, |
| 745 | efi_handle_t *volume_handles, |
| 746 | efi_status_t count) |
| 747 | { |
| 748 | u32 i; |
| 749 | struct efi_handler *handler; |
| 750 | efi_status_t ret = EFI_SUCCESS; |
| 751 | |
| 752 | for (i = 0; i < count; i++) { |
| 753 | u16 *p; |
| 754 | u16 dev_name[BOOTMENU_DEVICE_NAME_MAX]; |
| 755 | char *optional_data; |
| 756 | struct efi_load_option lo; |
| 757 | char buf[BOOTMENU_DEVICE_NAME_MAX]; |
| 758 | struct efi_device_path *device_path; |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 759 | struct efi_device_path *short_dp; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 760 | |
| 761 | ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler); |
| 762 | if (ret != EFI_SUCCESS) |
| 763 | continue; |
| 764 | ret = efi_protocol_open(handler, (void **)&device_path, |
| 765 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 766 | if (ret != EFI_SUCCESS) |
| 767 | continue; |
| 768 | |
| 769 | ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX); |
| 770 | if (ret != EFI_SUCCESS) |
| 771 | continue; |
| 772 | |
| 773 | p = dev_name; |
| 774 | utf8_utf16_strncpy(&p, buf, strlen(buf)); |
| 775 | |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 776 | /* prefer to short form device path */ |
| 777 | short_dp = efi_dp_shorten(device_path); |
| 778 | if (short_dp) |
| 779 | device_path = short_dp; |
| 780 | |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 781 | lo.label = dev_name; |
| 782 | lo.attributes = LOAD_OPTION_ACTIVE; |
| 783 | lo.file_path = device_path; |
| 784 | lo.file_path_length = efi_dp_size(device_path) + sizeof(END); |
| 785 | /* |
| 786 | * Set the dedicated guid to optional_data, it is used to identify |
| 787 | * the boot option that automatically generated by the bootmenu. |
| 788 | * efi_serialize_load_option() expects optional_data is null-terminated |
| 789 | * utf8 string, so set the "1234567" string to allocate enough space |
| 790 | * to store guid, instead of realloc the load_option. |
| 791 | */ |
| 792 | lo.optional_data = "1234567"; |
| 793 | opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo); |
| 794 | if (!opt[i].size) { |
| 795 | ret = EFI_OUT_OF_RESOURCES; |
| 796 | goto out; |
| 797 | } |
| 798 | /* set the guid */ |
| 799 | optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567")); |
| 800 | memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t)); |
| 801 | } |
| 802 | |
| 803 | out: |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option |
| 809 | * |
| 810 | * @opt: pointer to the media boot option structure |
| 811 | * @count: number of media boot option structure |
| 812 | * Return: status code |
| 813 | */ |
| 814 | static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt, |
| 815 | efi_status_t count) |
| 816 | { |
| 817 | efi_uintn_t size; |
| 818 | void *load_option; |
| 819 | u32 i, list_size = 0; |
| 820 | struct efi_load_option lo; |
| 821 | u16 *var_name16 = NULL; |
| 822 | u16 varname[] = u"Boot####"; |
| 823 | efi_status_t ret = EFI_SUCCESS; |
| 824 | u16 *delete_index_list = NULL, *p; |
| 825 | efi_uintn_t buf_size; |
| 826 | |
| 827 | buf_size = 128; |
| 828 | var_name16 = malloc(buf_size); |
| 829 | if (!var_name16) |
| 830 | return EFI_OUT_OF_RESOURCES; |
| 831 | |
| 832 | var_name16[0] = 0; |
| 833 | for (;;) { |
| 834 | int index; |
| 835 | efi_guid_t guid; |
| 836 | efi_uintn_t tmp; |
| 837 | |
| 838 | ret = efi_next_variable_name(&buf_size, &var_name16, &guid); |
| 839 | if (ret == EFI_NOT_FOUND) { |
| 840 | /* |
| 841 | * EFI_NOT_FOUND indicates we retrieved all EFI variables. |
| 842 | * This should be treated as success. |
| 843 | */ |
| 844 | ret = EFI_SUCCESS; |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | if (ret != EFI_SUCCESS) |
| 849 | goto out; |
| 850 | |
| 851 | if (!efi_varname_is_load_option(var_name16, &index)) |
| 852 | continue; |
| 853 | |
| 854 | efi_create_indexed_name(varname, sizeof(varname), "Boot", index); |
| 855 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 856 | if (!load_option) |
| 857 | continue; |
| 858 | |
| 859 | tmp = size; |
| 860 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 861 | if (ret != EFI_SUCCESS) |
| 862 | goto next; |
| 863 | |
| 864 | if (size >= sizeof(efi_guid_bootmenu_auto_generated) && |
| 865 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { |
| 866 | for (i = 0; i < count; i++) { |
| 867 | if (opt[i].size == tmp && |
| 868 | memcmp(opt[i].lo, load_option, tmp) == 0) { |
| 869 | opt[i].exist = true; |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * The entire list of variables must be retrieved by |
| 876 | * efi_get_next_variable_name_int() before deleting the invalid |
| 877 | * boot option, just save the index here. |
| 878 | */ |
| 879 | if (i == count) { |
| 880 | p = realloc(delete_index_list, sizeof(u32) * |
| 881 | (list_size + 1)); |
| 882 | if (!p) { |
| 883 | ret = EFI_OUT_OF_RESOURCES; |
| 884 | goto out; |
| 885 | } |
| 886 | delete_index_list = p; |
| 887 | delete_index_list[list_size++] = index; |
| 888 | } |
| 889 | } |
| 890 | next: |
| 891 | free(load_option); |
| 892 | } |
| 893 | |
| 894 | /* delete all invalid boot options */ |
| 895 | for (i = 0; i < list_size; i++) { |
| 896 | ret = efi_bootmgr_delete_boot_option(delete_index_list[i]); |
| 897 | if (ret != EFI_SUCCESS) |
| 898 | goto out; |
| 899 | } |
| 900 | |
| 901 | out: |
| 902 | free(var_name16); |
| 903 | free(delete_index_list); |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index |
| 910 | * |
| 911 | * @buf: pointer to the buffer to store boot option variable name |
| 912 | * @buf_size: buffer size |
| 913 | * @index: pointer to store the index in the BootOrder variable |
| 914 | * Return: status code |
| 915 | */ |
| 916 | efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size, |
| 917 | unsigned int *index) |
| 918 | { |
| 919 | u32 i; |
| 920 | efi_status_t ret; |
| 921 | efi_uintn_t size; |
| 922 | |
| 923 | if (buf_size < u16_strsize(u"Boot####")) |
| 924 | return EFI_BUFFER_TOO_SMALL; |
| 925 | |
| 926 | for (i = 0; i <= 0xFFFF; i++) { |
| 927 | size = 0; |
| 928 | efi_create_indexed_name(buf, buf_size, "Boot", i); |
| 929 | ret = efi_get_variable_int(buf, &efi_global_variable_guid, |
| 930 | NULL, &size, NULL, NULL); |
| 931 | if (ret == EFI_BUFFER_TOO_SMALL) |
| 932 | continue; |
| 933 | else |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | if (i > 0xFFFF) |
| 938 | return EFI_OUT_OF_RESOURCES; |
| 939 | |
| 940 | *index = i; |
| 941 | |
| 942 | return EFI_SUCCESS; |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable |
| 947 | * |
| 948 | * @index: "Boot####" index to append to BootOrder variable |
| 949 | * Return: status code |
| 950 | */ |
| 951 | efi_status_t efi_bootmgr_append_bootorder(u16 index) |
| 952 | { |
| 953 | u16 *bootorder; |
| 954 | efi_status_t ret; |
| 955 | u16 *new_bootorder = NULL; |
| 956 | efi_uintn_t last, size, new_size; |
| 957 | |
| 958 | /* append new boot option */ |
| 959 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 960 | last = size / sizeof(u16); |
| 961 | new_size = size + sizeof(u16); |
| 962 | new_bootorder = calloc(1, new_size); |
| 963 | if (!new_bootorder) { |
| 964 | ret = EFI_OUT_OF_RESOURCES; |
| 965 | goto out; |
| 966 | } |
| 967 | memcpy(new_bootorder, bootorder, size); |
| 968 | new_bootorder[last] = index; |
| 969 | |
| 970 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 971 | EFI_VARIABLE_NON_VOLATILE | |
| 972 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 973 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 974 | new_size, new_bootorder, false); |
| 975 | if (ret != EFI_SUCCESS) |
| 976 | goto out; |
| 977 | |
| 978 | out: |
| 979 | free(bootorder); |
| 980 | free(new_bootorder); |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * efi_bootmgr_delete_boot_option() - delete selected boot option |
| 987 | * |
| 988 | * @boot_index: boot option index to delete |
| 989 | * Return: status code |
| 990 | */ |
| 991 | efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index) |
| 992 | { |
| 993 | u16 *bootorder; |
| 994 | u16 varname[9]; |
| 995 | efi_status_t ret; |
| 996 | unsigned int index; |
| 997 | efi_uintn_t num, size; |
| 998 | |
| 999 | efi_create_indexed_name(varname, sizeof(varname), |
| 1000 | "Boot", boot_index); |
| 1001 | ret = efi_set_variable_int(varname, &efi_global_variable_guid, |
| 1002 | 0, 0, NULL, false); |
| 1003 | if (ret != EFI_SUCCESS) { |
| 1004 | log_err("delete boot option(%ls) failed\n", varname); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | /* update BootOrder if necessary */ |
| 1009 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 1010 | if (!bootorder) |
| 1011 | return EFI_SUCCESS; |
| 1012 | |
| 1013 | num = size / sizeof(u16); |
| 1014 | if (!efi_search_bootorder(bootorder, num, boot_index, &index)) |
| 1015 | return EFI_SUCCESS; |
| 1016 | |
| 1017 | memmove(&bootorder[index], &bootorder[index + 1], |
| 1018 | (num - index - 1) * sizeof(u16)); |
| 1019 | size -= sizeof(u16); |
| 1020 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1021 | EFI_VARIABLE_NON_VOLATILE | |
| 1022 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1023 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1024 | size, bootorder, false); |
| 1025 | |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option |
| 1031 | * |
| 1032 | * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL |
| 1033 | * and generate the bootmenu entries. |
| 1034 | * This function also provide the BOOT#### variable maintenance for |
| 1035 | * the media device entries. |
| 1036 | * - Automatically create the BOOT#### variable for the newly detected device, |
| 1037 | * this BOOT#### variable is distinguished by the special GUID |
| 1038 | * stored in the EFI_LOAD_OPTION.optional_data |
| 1039 | * - If the device is not attached to the system, the associated BOOT#### variable |
| 1040 | * is automatically deleted. |
| 1041 | * |
| 1042 | * Return: status code |
| 1043 | */ |
| 1044 | efi_status_t efi_bootmgr_update_media_device_boot_option(void) |
| 1045 | { |
| 1046 | u32 i; |
| 1047 | efi_status_t ret; |
| 1048 | efi_uintn_t count; |
| 1049 | efi_handle_t *volume_handles = NULL; |
| 1050 | struct eficonfig_media_boot_option *opt = NULL; |
| 1051 | |
| 1052 | ret = efi_locate_handle_buffer_int(BY_PROTOCOL, |
| 1053 | &efi_simple_file_system_protocol_guid, |
| 1054 | NULL, &count, |
| 1055 | (efi_handle_t **)&volume_handles); |
| 1056 | if (ret != EFI_SUCCESS) |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1057 | goto out; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1058 | |
| 1059 | opt = calloc(count, sizeof(struct eficonfig_media_boot_option)); |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1060 | if (!opt) { |
| 1061 | ret = EFI_OUT_OF_RESOURCES; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1062 | goto out; |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1063 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 1064 | |
| 1065 | /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */ |
| 1066 | ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count); |
| 1067 | if (ret != EFI_SUCCESS) |
| 1068 | goto out; |
| 1069 | |
| 1070 | /* |
| 1071 | * System hardware configuration may vary depending on the user setup. |
| 1072 | * The boot option is automatically added by the bootmenu. |
| 1073 | * If the device is not attached to the system, the boot option needs |
| 1074 | * to be deleted. |
| 1075 | */ |
| 1076 | ret = efi_bootmgr_delete_invalid_boot_option(opt, count); |
| 1077 | if (ret != EFI_SUCCESS) |
| 1078 | goto out; |
| 1079 | |
| 1080 | /* add non-existent boot option */ |
| 1081 | for (i = 0; i < count; i++) { |
| 1082 | u32 boot_index; |
| 1083 | u16 var_name[9]; |
| 1084 | |
| 1085 | if (!opt[i].exist) { |
| 1086 | ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name), |
| 1087 | &boot_index); |
| 1088 | if (ret != EFI_SUCCESS) |
| 1089 | goto out; |
| 1090 | |
| 1091 | ret = efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 1092 | EFI_VARIABLE_NON_VOLATILE | |
| 1093 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1094 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1095 | opt[i].size, opt[i].lo, false); |
| 1096 | if (ret != EFI_SUCCESS) |
| 1097 | goto out; |
| 1098 | |
| 1099 | ret = efi_bootmgr_append_bootorder(boot_index); |
| 1100 | if (ret != EFI_SUCCESS) { |
| 1101 | efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 1102 | 0, 0, NULL, false); |
| 1103 | goto out; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | out: |
| 1109 | if (opt) { |
| 1110 | for (i = 0; i < count; i++) |
| 1111 | free(opt[i].lo); |
| 1112 | } |
| 1113 | free(opt); |
| 1114 | efi_free_pool(volume_handles); |
| 1115 | |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 1116 | if (ret == EFI_NOT_FOUND) |
| 1117 | return EFI_SUCCESS; |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1118 | return ret; |
| 1119 | } |
| 1120 | |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1121 | /** |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 1122 | * efi_bootmgr_run() - execute EFI boot manager |
| 1123 | * @fdt: Flat device tree |
| 1124 | * |
| 1125 | * Invoke EFI boot manager and execute a binary depending on |
| 1126 | * boot options. If @fdt is not NULL, it will be passed to |
| 1127 | * the executed binary. |
| 1128 | * |
| 1129 | * Return: status code |
| 1130 | */ |
| 1131 | efi_status_t efi_bootmgr_run(void *fdt) |
| 1132 | { |
| 1133 | efi_handle_t handle; |
| 1134 | void *load_options; |
| 1135 | efi_status_t ret; |
| 1136 | |
| 1137 | /* Initialize EFI drivers */ |
| 1138 | ret = efi_init_obj_list(); |
| 1139 | if (ret != EFI_SUCCESS) { |
| 1140 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1141 | ret & ~EFI_ERROR_MASK); |
| 1142 | return CMD_RET_FAILURE; |
| 1143 | } |
| 1144 | |
| 1145 | ret = efi_install_fdt(fdt); |
| 1146 | if (ret != EFI_SUCCESS) |
| 1147 | return ret; |
| 1148 | |
| 1149 | ret = efi_bootmgr_load(&handle, &load_options); |
| 1150 | if (ret != EFI_SUCCESS) { |
| 1151 | log_notice("EFI boot manager: Cannot load any image\n"); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | return do_bootefi_exec(handle, load_options); |
| 1156 | } |