blob: 298149bcc3322a7d568a7ad50f968bb3fc35d883 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkc84c1102017-09-13 18:05:38 -04002/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clarkc84c1102017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clarkc84c1102017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt273df962020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Masahisa Kojima949c4412023-11-10 13:25:40 +090010#include <blk.h>
11#include <blkmap.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040012#include <charset.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090013#include <dm.h>
Simon Glass3b1e60b2024-11-07 14:31:43 -070014#include <efi.h>
Tom Rinic31301c2025-05-15 17:31:50 -060015#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040017#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090018#include <net.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040019#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020020#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090021#include <asm/unaligned.h>
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +053022#include <linux/kernel.h>
23#include <linux/sizes.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040024
25static const struct efi_boot_services *bs;
26static const struct efi_runtime_services *rs;
27
Masahisa Kojima949c4412023-11-10 13:25:40 +090028/**
29 * struct uridp_context - uri device path resource
30 *
31 * @image_size: image size
32 * @image_addr: image address
33 * @loaded_dp: pointer to loaded device path
34 * @ramdisk_blk_dev: pointer to the ramdisk blk device
35 * @mem_handle: efi_handle to the loaded PE-COFF image
36 */
37struct uridp_context {
38 ulong image_size;
39 ulong image_addr;
40 struct efi_device_path *loaded_dp;
41 struct udevice *ramdisk_blk_dev;
42 efi_handle_t mem_handle;
43};
44
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090045const efi_guid_t efi_guid_bootmenu_auto_generated =
46 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
47
Rob Clarkc84c1102017-09-13 18:05:38 -040048/*
49 * bootmgr implements the logic of trying to find a payload to boot
50 * based on the BootOrder + BootXXXX variables, and then loading it.
51 *
52 * TODO detecting a special key held (f9?) and displaying a boot menu
53 * like you would get on a PC would be clever.
54 *
55 * TODO if we had a way to write and persist variables after the OS
56 * has started, we'd also want to check OsIndications to see if we
57 * should do normal or recovery boot.
58 */
59
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020060/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090061 * expand_media_path() - expand a device path for default file name
62 * @device_path: device path to check against
63 *
64 * If @device_path is a media or disk partition which houses a file
65 * system, this function returns a full device path which contains
66 * an architecture-specific default file name for removable media.
67 *
68 * Return: a newly allocated device path
69 */
70static
71struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
72{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020073 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090074 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090075
76 if (!device_path)
77 return NULL;
78
79 /*
80 * If device_path is a (removable) media or partition which provides
81 * simple file system protocol, append a default file name to support
82 * booting from removable media.
83 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020084 handle = efi_dp_find_obj(device_path,
85 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000086 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000087 if (rem->type == DEVICE_PATH_TYPE_END) {
Simon Glass3b1e60b2024-11-07 14:31:43 -070088 char fname[30];
89
90 snprintf(fname, sizeof(fname), "/EFI/BOOT/%s",
91 efi_get_basename());
92 full_path = efi_dp_from_file(device_path, fname);
93
AKASHI Takahirofd972f52022-04-28 17:09:39 +090094 } else {
95 full_path = efi_dp_dup(device_path);
96 }
97 } else {
98 full_path = efi_dp_dup(device_path);
99 }
100
101 return full_path;
102}
103
104/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900105 * try_load_from_file_path() - try to load a file
106 *
107 * Given a file media path iterate through a list of handles and try to
108 * to load the file from each of them until the first success.
109 *
110 * @fs_handles: array of handles with the simple file protocol
111 * @num: number of handles in fs_handles
112 * @fp: file path to open
113 * @handle: on return pointer to handle for loaded image
114 * @removable: if true only consider removable media, else only non-removable
115 */
116static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
117 efi_uintn_t num,
118 struct efi_device_path *fp,
119 efi_handle_t *handle,
120 bool removable)
121{
122 struct efi_handler *handler;
123 struct efi_device_path *dp;
124 int i;
125 efi_status_t ret;
126
127 for (i = 0; i < num; i++) {
128 if (removable != efi_disk_is_removable(fs_handles[i]))
129 continue;
130
131 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
132 &handler);
133 if (ret != EFI_SUCCESS)
134 continue;
135
136 dp = handler->protocol_interface;
137 if (!dp)
138 continue;
139
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200140 dp = efi_dp_concat(dp, fp, 0);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900141 if (!dp)
142 continue;
143
144 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
145 handle));
146 efi_free_pool(dp);
147 if (ret == EFI_SUCCESS)
148 return ret;
149 }
150
151 return EFI_NOT_FOUND;
152}
153
154/**
155 * try_load_from_short_path
156 * @fp: file path
157 * @handle: pointer to handle for newly installed image
158 *
159 * Enumerate all the devices which support file system operations,
160 * prepend its media device path to the file path, @fp, and
161 * try to load the file.
162 * This function should be called when handling a short-form path
163 * which is starting with a file device path.
164 *
165 * Return: status code
166 */
167static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
168 efi_handle_t *handle)
169{
170 efi_handle_t *fs_handles;
171 efi_uintn_t num;
172 efi_status_t ret;
173
174 ret = EFI_CALL(efi_locate_handle_buffer(
175 BY_PROTOCOL,
176 &efi_simple_file_system_protocol_guid,
177 NULL,
178 &num, &fs_handles));
179 if (ret != EFI_SUCCESS)
180 return ret;
181 if (!num)
182 return EFI_NOT_FOUND;
183
184 /* removable media first */
185 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
186 if (ret == EFI_SUCCESS)
187 goto out;
188
189 /* fixed media */
190 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
191 if (ret == EFI_SUCCESS)
192 goto out;
193
194out:
195 return ret;
196}
197
198/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900199 * mount_image() - mount the image with blkmap
200 *
201 * @lo_label: u16 label string of load option
202 * @addr: image address
203 * @size: image size
204 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
205 */
206static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
207{
208 int err;
209 struct blkmap *bm;
210 struct udevice *bm_dev;
211 char *label = NULL, *p;
212
213 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
214 if (!label)
215 return NULL;
216
217 p = label;
218 utf16_utf8_strcpy(&p, lo_label);
219 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
220 if (err) {
221 efi_free_pool(label);
222 return NULL;
223 }
224 bm = dev_get_plat(bm_dev);
225
226 efi_free_pool(label);
227
228 return bm->blk;
229}
230
231/**
232 * search_default_file() - search default file
233 *
234 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
235 * @loaded_dp: pointer to default file device path
236 * Return: status code
237 */
238static efi_status_t search_default_file(struct udevice *dev,
239 struct efi_device_path **loaded_dp)
240{
241 efi_status_t ret;
242 efi_handle_t handle;
243 u16 *default_file_name = NULL;
244 struct efi_file_handle *root, *f;
245 struct efi_device_path *dp = NULL, *fp = NULL;
246 struct efi_simple_file_system_protocol *file_system;
247 struct efi_device_path *device_path, *full_path = NULL;
248
249 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
250 log_warning("DM_TAG_EFI not found\n");
251 return EFI_INVALID_PARAMETER;
252 }
253
254 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
255 (void **)&device_path, efi_root, NULL,
256 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
257 if (ret != EFI_SUCCESS)
258 return ret;
259
260 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
261 (void **)&file_system, efi_root, NULL,
262 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
263 if (ret != EFI_SUCCESS)
264 return ret;
265
266 ret = EFI_CALL(file_system->open_volume(file_system, &root));
267 if (ret != EFI_SUCCESS)
268 return ret;
269
270 full_path = expand_media_path(device_path);
271 ret = efi_dp_split_file_path(full_path, &dp, &fp);
272 if (ret != EFI_SUCCESS)
273 goto err;
274
275 default_file_name = efi_dp_str(fp);
276 efi_free_pool(dp);
277 efi_free_pool(fp);
278 if (!default_file_name) {
279 ret = EFI_OUT_OF_RESOURCES;
280 goto err;
281 }
282
283 ret = EFI_CALL(root->open(root, &f, default_file_name,
284 EFI_FILE_MODE_READ, 0));
285 efi_free_pool(default_file_name);
286 if (ret != EFI_SUCCESS)
287 goto err;
288
289 EFI_CALL(f->close(f));
290 EFI_CALL(root->close(root));
291
292 *loaded_dp = full_path;
293
294 return EFI_SUCCESS;
295
296err:
297 EFI_CALL(root->close(root));
298 efi_free_pool(full_path);
299
300 return ret;
301}
302
303/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900304 * fill_default_file_path() - get fallback boot device path for block device
305 *
306 * Provide the device path to the fallback UEFI boot file, e.g.
307 * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900308 *
309 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900310 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900311 * Return: status code
312 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900313static efi_status_t fill_default_file_path(struct udevice *blk,
314 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900315{
316 efi_status_t ret;
317 struct udevice *partition;
318
319 /* image that has no partition table but a file system */
320 ret = search_default_file(blk, dp);
321 if (ret == EFI_SUCCESS)
322 return ret;
323
324 /* try the partitions */
325 device_foreach_child(partition, blk) {
326 enum uclass_id id;
327
328 id = device_get_uclass_id(partition);
329 if (id != UCLASS_PARTITION)
330 continue;
331
332 ret = search_default_file(partition, dp);
333 if (ret == EFI_SUCCESS)
334 return ret;
335 }
336
337 return EFI_NOT_FOUND;
338}
339
340/**
341 * prepare_loaded_image() - prepare ramdisk for downloaded image
342 *
343 * @label: label of load option
344 * @addr: image address
345 * @size: image size
346 * @dp: pointer to default file device path
347 * @blk: pointer to created blk udevice
348 * Return: status code
349 */
350static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
351 struct efi_device_path **dp,
352 struct udevice **blk)
353{
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530354 u64 pages;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900355 efi_status_t ret;
356 struct udevice *ramdisk_blk;
357
358 ramdisk_blk = mount_image(label, addr, size);
359 if (!ramdisk_blk)
360 return EFI_LOAD_ERROR;
361
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900362 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900363 if (ret != EFI_SUCCESS) {
364 log_info("Cannot boot from downloaded image\n");
365 goto err;
366 }
367
368 /*
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530369 * Linux supports 'pmem' which allows OS installers to find, reclaim
370 * the mounted images and continue the installation since the contents
371 * of the pmem region are treated as local media.
372 *
373 * The memory regions used for it needs to be carved out of the EFI
374 * memory map.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900375 */
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530376 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
377 ret = efi_update_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY,
378 false, true);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900379 if (ret != EFI_SUCCESS) {
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530380 log_err("Failed to reserve memory\n");
Masahisa Kojima949c4412023-11-10 13:25:40 +0900381 goto err;
382 }
383
384 *blk = ramdisk_blk;
385
386 return EFI_SUCCESS;
387
388err:
389 if (blkmap_destroy(ramdisk_blk->parent))
390 log_err("Destroying blkmap failed\n");
391
392 return ret;
393}
394
395/**
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300396 * efi_bootmgr_release_uridp() - cleanup uri device path resource
Masahisa Kojima949c4412023-11-10 13:25:40 +0900397 *
398 * @ctx: event context
399 * Return: status code
400 */
Ilias Apalodimas55c05712024-10-26 10:37:32 +0300401static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900402{
403 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300404 efi_status_t ret2 = EFI_SUCCESS;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900405
406 if (!ctx)
407 return ret;
408
409 /* cleanup for iso or img image */
410 if (ctx->ramdisk_blk_dev) {
411 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
412 EFI_CONVENTIONAL_MEMORY);
413 if (ret != EFI_SUCCESS)
414 log_err("Reclaiming memory failed\n");
415
416 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
417 log_err("Destroying blkmap failed\n");
418 ret = EFI_DEVICE_ERROR;
419 }
420 }
421
422 /* cleanup for PE-COFF image */
423 if (ctx->mem_handle) {
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300424 ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle,
425 &efi_guid_device_path,
426 ctx->loaded_dp,
427 NULL);
428 if (ret2 != EFI_SUCCESS)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900429 log_err("Uninstall device_path protocol failed\n");
430 }
431
432 efi_free_pool(ctx->loaded_dp);
433 free(ctx);
434
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300435 return ret == EFI_SUCCESS ? ret2 : ret;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900436}
437
438/**
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300439 * efi_bootmgr_http_return() - return to efibootmgr callback
Masahisa Kojima949c4412023-11-10 13:25:40 +0900440 *
441 * @event: the event for which this notification function is registered
442 * @context: event context
443 */
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300444static void EFIAPI efi_bootmgr_http_return(struct efi_event *event,
445 void *context)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900446{
447 efi_status_t ret;
448
449 EFI_ENTRY("%p, %p", event, context);
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300450 ret = efi_bootmgr_release_uridp(context);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900451 EFI_EXIT(ret);
452}
453
454/**
455 * try_load_from_uri_path() - Handle the URI device path
456 *
457 * @uridp: uri device path
458 * @lo_label: label of load option
459 * @handle: pointer to handle for newly installed image
460 * Return: status code
461 */
462static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
463 u16 *lo_label,
464 efi_handle_t *handle)
465{
466 char *s;
467 int err;
468 int uri_len;
469 efi_status_t ret;
470 void *source_buffer;
471 efi_uintn_t source_size;
472 struct uridp_context *ctx;
473 struct udevice *blk = NULL;
474 struct efi_event *event = NULL;
475 efi_handle_t mem_handle = NULL;
476 struct efi_device_path *loaded_dp;
477 static ulong image_size, image_addr;
478
479 ctx = calloc(1, sizeof(struct uridp_context));
480 if (!ctx)
481 return EFI_OUT_OF_RESOURCES;
482
483 s = env_get("loadaddr");
484 if (!s) {
485 log_err("Error: loadaddr is not set\n");
486 ret = EFI_INVALID_PARAMETER;
487 goto err;
488 }
489
490 image_addr = hextoul(s, NULL);
Adriano Cordovab479fc42024-12-04 00:05:16 -0300491 err = wget_do_request(image_addr, uridp->uri);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900492 if (err < 0) {
493 ret = EFI_INVALID_PARAMETER;
494 goto err;
495 }
496
497 image_size = env_get_hex("filesize", 0);
498 if (!image_size) {
499 ret = EFI_INVALID_PARAMETER;
500 goto err;
501 }
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530502 /*
503 * Depending on the kernel configuration, pmem memory areas must be
504 * page aligned or 2MiB aligned. PowerPC is an exception here and
505 * requires 16MiB alignment, but since we don't have EFI support for
506 * it, limit the alignment to 2MiB.
507 */
508 image_size = ALIGN(image_size, SZ_2M);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900509
510 /*
511 * If the file extension is ".iso" or ".img", mount it and try to load
512 * the default file.
513 * If the file is PE-COFF image, load the downloaded file.
514 */
515 uri_len = strlen(uridp->uri);
516 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
517 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
518 ret = prepare_loaded_image(lo_label, image_addr, image_size,
519 &loaded_dp, &blk);
520 if (ret != EFI_SUCCESS)
521 goto err;
522
523 source_buffer = NULL;
524 source_size = 0;
525 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
526 /*
527 * loaded_dp must exist until efi application returns,
528 * will be freed in return_to_efibootmgr event callback.
529 */
530 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
531 (uintptr_t)image_addr, image_size);
532 ret = efi_install_multiple_protocol_interfaces(
533 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
534 if (ret != EFI_SUCCESS)
535 goto err;
536
537 source_buffer = (void *)image_addr;
538 source_size = image_size;
539 } else {
540 log_err("Error: file type is not supported\n");
541 ret = EFI_UNSUPPORTED;
542 goto err;
543 }
544
545 ctx->image_size = image_size;
546 ctx->image_addr = image_addr;
547 ctx->loaded_dp = loaded_dp;
548 ctx->ramdisk_blk_dev = blk;
549 ctx->mem_handle = mem_handle;
550
551 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
552 source_size, handle));
553 if (ret != EFI_SUCCESS)
554 goto err;
555
556 /* create event for cleanup when the image returns or error occurs */
557 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300558 efi_bootmgr_http_return, ctx,
Masahisa Kojima949c4412023-11-10 13:25:40 +0900559 &efi_guid_event_group_return_to_efibootmgr,
560 &event);
561 if (ret != EFI_SUCCESS) {
562 log_err("Creating event failed\n");
563 goto err;
564 }
565
566 return ret;
567
568err:
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300569 efi_bootmgr_release_uridp(ctx);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900570
571 return ret;
572}
573
574/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900575 * try_load_from_media() - load file from media
576 *
577 * @file_path: file path
578 * @handle_img: on return handle for the newly installed image
579 *
580 * If @file_path contains a file name, load the file.
581 * If @file_path does not have a file name, search the architecture-specific
582 * fallback boot file and load it.
583 * TODO: If the FilePathList[0] device does not support
584 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
585 * call EFI_BOOT_SERVICES.ConnectController()
586 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
587 * not based on EFI_BLOCK_IO_PROTOCOL
588 *
589 * Return: status code
590 */
591static efi_status_t try_load_from_media(struct efi_device_path *file_path,
592 efi_handle_t *handle_img)
593{
594 efi_handle_t handle_blkdev;
595 efi_status_t ret = EFI_SUCCESS;
596 struct efi_device_path *rem, *dp = NULL;
597 struct efi_device_path *final_dp = file_path;
598
599 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
600 if (handle_blkdev) {
601 if (rem->type == DEVICE_PATH_TYPE_END) {
602 /* no file name present, try default file */
603 ret = fill_default_file_path(handle_blkdev->dev, &dp);
604 if (ret != EFI_SUCCESS)
605 return ret;
606
607 final_dp = dp;
608 }
609 }
610
611 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
612
613 efi_free_pool(dp);
614
615 return ret;
616}
617
618/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200619 * try_load_entry() - try to load image for boot option
620 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400621 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200622 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400623 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200624 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200625 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
626 * @handle: on return handle for the newly installed image
627 * @load_options: load options set on the loaded image protocol
628 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400629 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200630static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
631 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400632{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900633 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200634 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900635 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200636 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900637 efi_status_t ret;
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200638 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400639
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200640 *handle = NULL;
641 *load_options = NULL;
Rob Clarkc84c1102017-09-13 18:05:38 -0400642
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200643 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200644 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400645 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900646 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400647
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200648 ret = efi_deserialize_load_option(&lo, load_option, &size);
649 if (ret != EFI_SUCCESS) {
650 log_warning("Invalid load option for %ls\n", varname);
651 goto error;
652 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400653
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200654 if (!(lo.attributes & LOAD_OPTION_ACTIVE)) {
655 ret = EFI_LOAD_ERROR;
656 goto error;
657 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400658
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200659 log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400660
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200661 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
662 /* file_path doesn't contain a device path */
663 ret = try_load_from_short_path(lo.file_path, handle);
664 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
665 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
666 ret = try_load_from_uri_path(
667 (struct efi_device_path_uri *)lo.file_path,
668 lo.label, handle);
669 else
670 ret = EFI_LOAD_ERROR;
671 } else {
672 ret = try_load_from_media(lo.file_path, handle);
673 }
674 if (ret != EFI_SUCCESS) {
675 log_warning("Loading %ls '%ls' failed\n",
676 varname, lo.label);
677 goto error;
678 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400679
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200680 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
681 EFI_VARIABLE_RUNTIME_ACCESS;
682 ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid,
683 attributes, sizeof(n), &n, false);
684 if (ret != EFI_SUCCESS)
685 goto error;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900686
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200687 /* try to register load file2 for initrd's */
688 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
Adriano Cordova02d049d2025-03-19 11:44:59 -0300689 ret = efi_initrd_register(NULL);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200690 if (ret != EFI_SUCCESS)
691 goto error;
Rob Clarkc84c1102017-09-13 18:05:38 -0400692 }
693
Ilias Apalodimas2f304322025-03-28 14:58:18 +0200694 log_info("Booting: Label: %ls Device path: %pD\n", lo.label, lo.file_path);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200695
696 /* Ignore the optional data in auto-generated boot options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900697 if (size >= sizeof(efi_guid_t) &&
698 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
699 size = 0;
700
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200701 /* Set optional data in loaded file protocol */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200702 if (size) {
703 *load_options = malloc(size);
704 if (!*load_options) {
705 ret = EFI_OUT_OF_RESOURCES;
706 goto error;
707 }
708 memcpy(*load_options, lo.optional_data, size);
709 ret = efi_set_load_options(*handle, size, *load_options);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200710 if (ret != EFI_SUCCESS)
711 free(load_options);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200712 }
713
Rob Clarkc84c1102017-09-13 18:05:38 -0400714error:
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200715 if (ret != EFI_SUCCESS && *handle &&
716 EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200717 log_err("Unloading image failed\n");
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200718
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200719 free(load_option);
720
721 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400722}
723
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200724/**
725 * efi_bootmgr_load() - try to load from BootNext or BootOrder
726 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900727 * Attempt to load from BootNext or in the order specified by BootOrder
728 * EFI variable, the available load-options, finding and returning
729 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200730 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200731 * @handle: on return handle for the newly installed image
732 * @load_options: load options set on the loaded image protocol
733 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400734 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200735efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400736{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900737 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200738 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400739 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900740 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400741
Rob Clarkc84c1102017-09-13 18:05:38 -0400742 bs = systab.boottime;
743 rs = systab.runtime;
744
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900745 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900746 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700747 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200748 &efi_global_variable_guid,
749 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900750 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
751 /* BootNext does exist here */
752 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200753 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900754
755 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700756 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200757 &efi_global_variable_guid,
758 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900759
760 /* load BootNext */
761 if (ret == EFI_SUCCESS) {
762 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200763 ret = try_load_entry(bootnext, handle,
764 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900765 if (ret == EFI_SUCCESS)
766 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200767 log_warning(
768 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900769 }
770 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200771 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900772 }
773 }
774
775 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700776 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100777 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200778 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900779 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400780 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100781 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400782
783 num = size / sizeof(uint16_t);
784 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200785 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200786 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900787 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400788 break;
789 }
790
791 free(bootorder);
792
793error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900794 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400795}
Raymond Mao70a76c52023-06-19 14:22:58 -0700796
797/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900798 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700799 *
800 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900801 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900802 * @handles: pointer to block device handles
803 * @count: On entry number of handles to block devices.
804 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900805 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700806 * Return: status code
807 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900808static efi_status_t
809efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900810 efi_uintn_t index, efi_handle_t *handles,
811 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700812{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900813 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700814 struct efi_handler *handler;
815 efi_status_t ret = EFI_SUCCESS;
816
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900817 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700818 u16 *p;
819 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
820 char *optional_data;
821 struct efi_load_option lo;
822 char buf[BOOTMENU_DEVICE_NAME_MAX];
823 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700824 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900825 struct efi_block_io *blkio;
826
827 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
828 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700829
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900830 if (blkio->media->logical_partition)
831 continue;
832
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900833 if (removable != (blkio->media->removable_media != 0))
834 continue;
835
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900836 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700837 if (ret != EFI_SUCCESS)
838 continue;
839 ret = efi_protocol_open(handler, (void **)&device_path,
840 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
841 if (ret != EFI_SUCCESS)
842 continue;
843
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900844 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700845 if (ret != EFI_SUCCESS)
846 continue;
847
848 p = dev_name;
849 utf8_utf16_strncpy(&p, buf, strlen(buf));
850
Raymond Maob5542a62023-06-19 14:23:01 -0700851 /* prefer to short form device path */
852 short_dp = efi_dp_shorten(device_path);
853 if (short_dp)
854 device_path = short_dp;
855
Raymond Mao70a76c52023-06-19 14:22:58 -0700856 lo.label = dev_name;
857 lo.attributes = LOAD_OPTION_ACTIVE;
858 lo.file_path = device_path;
859 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
860 /*
861 * Set the dedicated guid to optional_data, it is used to identify
862 * the boot option that automatically generated by the bootmenu.
863 * efi_serialize_load_option() expects optional_data is null-terminated
864 * utf8 string, so set the "1234567" string to allocate enough space
865 * to store guid, instead of realloc the load_option.
866 */
867 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900868 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
869 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700870 ret = EFI_OUT_OF_RESOURCES;
871 goto out;
872 }
873 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900874 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700875 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900876 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900877
878 if (num >= *count)
879 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700880 }
881
882out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900883 *count = num;
884
Raymond Mao70a76c52023-06-19 14:22:58 -0700885 return ret;
886}
887
888/**
889 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
890 *
891 * @opt: pointer to the media boot option structure
892 * @count: number of media boot option structure
893 * Return: status code
894 */
895static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
896 efi_status_t count)
897{
898 efi_uintn_t size;
899 void *load_option;
900 u32 i, list_size = 0;
901 struct efi_load_option lo;
902 u16 *var_name16 = NULL;
903 u16 varname[] = u"Boot####";
904 efi_status_t ret = EFI_SUCCESS;
905 u16 *delete_index_list = NULL, *p;
906 efi_uintn_t buf_size;
907
908 buf_size = 128;
909 var_name16 = malloc(buf_size);
910 if (!var_name16)
911 return EFI_OUT_OF_RESOURCES;
912
913 var_name16[0] = 0;
914 for (;;) {
915 int index;
916 efi_guid_t guid;
917 efi_uintn_t tmp;
918
919 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
920 if (ret == EFI_NOT_FOUND) {
921 /*
922 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
923 * This should be treated as success.
924 */
925 ret = EFI_SUCCESS;
926 break;
927 }
928
929 if (ret != EFI_SUCCESS)
930 goto out;
931
932 if (!efi_varname_is_load_option(var_name16, &index))
933 continue;
934
935 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
936 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
937 if (!load_option)
938 continue;
939
940 tmp = size;
941 ret = efi_deserialize_load_option(&lo, load_option, &size);
942 if (ret != EFI_SUCCESS)
943 goto next;
944
945 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
946 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
947 for (i = 0; i < count; i++) {
948 if (opt[i].size == tmp &&
949 memcmp(opt[i].lo, load_option, tmp) == 0) {
950 opt[i].exist = true;
951 break;
952 }
953 }
954
955 /*
956 * The entire list of variables must be retrieved by
957 * efi_get_next_variable_name_int() before deleting the invalid
958 * boot option, just save the index here.
959 */
960 if (i == count) {
961 p = realloc(delete_index_list, sizeof(u32) *
962 (list_size + 1));
963 if (!p) {
964 ret = EFI_OUT_OF_RESOURCES;
965 goto out;
966 }
967 delete_index_list = p;
968 delete_index_list[list_size++] = index;
969 }
970 }
971next:
972 free(load_option);
973 }
974
975 /* delete all invalid boot options */
976 for (i = 0; i < list_size; i++) {
977 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
978 if (ret != EFI_SUCCESS)
979 goto out;
980 }
981
982out:
983 free(var_name16);
984 free(delete_index_list);
985
986 return ret;
987}
988
989/**
990 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
991 *
992 * @buf: pointer to the buffer to store boot option variable name
993 * @buf_size: buffer size
994 * @index: pointer to store the index in the BootOrder variable
995 * Return: status code
996 */
997efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
998 unsigned int *index)
999{
1000 u32 i;
1001 efi_status_t ret;
1002 efi_uintn_t size;
1003
1004 if (buf_size < u16_strsize(u"Boot####"))
1005 return EFI_BUFFER_TOO_SMALL;
1006
1007 for (i = 0; i <= 0xFFFF; i++) {
1008 size = 0;
1009 efi_create_indexed_name(buf, buf_size, "Boot", i);
1010 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1011 NULL, &size, NULL, NULL);
1012 if (ret == EFI_BUFFER_TOO_SMALL)
1013 continue;
1014 else
1015 break;
1016 }
1017
1018 if (i > 0xFFFF)
1019 return EFI_OUT_OF_RESOURCES;
1020
1021 *index = i;
1022
1023 return EFI_SUCCESS;
1024}
1025
1026/**
1027 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1028 *
1029 * @index: "Boot####" index to append to BootOrder variable
1030 * Return: status code
1031 */
1032efi_status_t efi_bootmgr_append_bootorder(u16 index)
1033{
1034 u16 *bootorder;
1035 efi_status_t ret;
1036 u16 *new_bootorder = NULL;
1037 efi_uintn_t last, size, new_size;
1038
1039 /* append new boot option */
1040 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1041 last = size / sizeof(u16);
1042 new_size = size + sizeof(u16);
1043 new_bootorder = calloc(1, new_size);
1044 if (!new_bootorder) {
1045 ret = EFI_OUT_OF_RESOURCES;
1046 goto out;
1047 }
1048 memcpy(new_bootorder, bootorder, size);
1049 new_bootorder[last] = index;
1050
1051 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1052 EFI_VARIABLE_NON_VOLATILE |
1053 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1054 EFI_VARIABLE_RUNTIME_ACCESS,
1055 new_size, new_bootorder, false);
1056 if (ret != EFI_SUCCESS)
1057 goto out;
1058
1059out:
1060 free(bootorder);
1061 free(new_bootorder);
1062
1063 return ret;
1064}
1065
1066/**
1067 * efi_bootmgr_delete_boot_option() - delete selected boot option
1068 *
1069 * @boot_index: boot option index to delete
1070 * Return: status code
1071 */
1072efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1073{
1074 u16 *bootorder;
1075 u16 varname[9];
1076 efi_status_t ret;
1077 unsigned int index;
1078 efi_uintn_t num, size;
1079
1080 efi_create_indexed_name(varname, sizeof(varname),
1081 "Boot", boot_index);
1082 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1083 0, 0, NULL, false);
1084 if (ret != EFI_SUCCESS) {
1085 log_err("delete boot option(%ls) failed\n", varname);
1086 return ret;
1087 }
1088
1089 /* update BootOrder if necessary */
1090 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1091 if (!bootorder)
1092 return EFI_SUCCESS;
1093
1094 num = size / sizeof(u16);
1095 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1096 return EFI_SUCCESS;
1097
1098 memmove(&bootorder[index], &bootorder[index + 1],
1099 (num - index - 1) * sizeof(u16));
1100 size -= sizeof(u16);
1101 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1102 EFI_VARIABLE_NON_VOLATILE |
1103 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1104 EFI_VARIABLE_RUNTIME_ACCESS,
1105 size, bootorder, false);
1106
1107 return ret;
1108}
1109
1110/**
1111 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1112 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001113 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001114 * This function also provide the BOOT#### variable maintenance for
1115 * the media device entries.
1116 * - Automatically create the BOOT#### variable for the newly detected device,
1117 * this BOOT#### variable is distinguished by the special GUID
1118 * stored in the EFI_LOAD_OPTION.optional_data
1119 * - If the device is not attached to the system, the associated BOOT#### variable
1120 * is automatically deleted.
1121 *
1122 * Return: status code
1123 */
1124efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1125{
1126 u32 i;
1127 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001128 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001129 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001130 struct eficonfig_media_boot_option *opt = NULL;
1131
1132 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001133 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001134 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001135 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001136 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001137 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001138
1139 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001140 if (!opt) {
1141 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001142 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001143 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001144
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001145 /* parse removable block io followed by fixed block io */
1146 num = count;
1147 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001148 if (ret != EFI_SUCCESS)
1149 goto out;
1150
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001151 total = num;
1152 num = count;
1153 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1154 if (ret != EFI_SUCCESS)
1155 goto out;
1156
1157 total = num;
1158
Raymond Mao70a76c52023-06-19 14:22:58 -07001159 /*
1160 * System hardware configuration may vary depending on the user setup.
1161 * The boot option is automatically added by the bootmenu.
1162 * If the device is not attached to the system, the boot option needs
1163 * to be deleted.
1164 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001165 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001166 if (ret != EFI_SUCCESS)
1167 goto out;
1168
1169 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001170 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001171 u32 boot_index;
1172 u16 var_name[9];
1173
1174 if (!opt[i].exist) {
1175 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1176 &boot_index);
1177 if (ret != EFI_SUCCESS)
1178 goto out;
1179
1180 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1181 EFI_VARIABLE_NON_VOLATILE |
1182 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1183 EFI_VARIABLE_RUNTIME_ACCESS,
1184 opt[i].size, opt[i].lo, false);
1185 if (ret != EFI_SUCCESS)
1186 goto out;
1187
1188 ret = efi_bootmgr_append_bootorder(boot_index);
1189 if (ret != EFI_SUCCESS) {
1190 efi_set_variable_int(var_name, &efi_global_variable_guid,
1191 0, 0, NULL, false);
1192 goto out;
1193 }
1194 }
1195 }
1196
1197out:
1198 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001199 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001200 free(opt[i].lo);
1201 }
1202 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001203 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001204
Raymond Maoa35784d2023-06-19 14:22:59 -07001205 if (ret == EFI_NOT_FOUND)
1206 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001207 return ret;
1208}
1209
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001210/**
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001211 * load_fdt_from_load_option - load device-tree from load option
1212 *
1213 * @fdt: pointer to loaded device-tree or NULL
1214 * Return: status code
1215 */
1216static efi_status_t load_fdt_from_load_option(void **fdt)
1217{
1218 struct efi_device_path *dp = NULL;
1219 struct efi_file_handle *f = NULL;
1220 efi_uintn_t filesize;
1221 efi_status_t ret;
1222
1223 *fdt = NULL;
1224
1225 dp = efi_get_dp_from_boot(&efi_guid_fdt);
1226 if (!dp)
1227 return EFI_SUCCESS;
1228
1229 /* Open file */
1230 f = efi_file_from_path(dp);
1231 if (!f) {
1232 log_err("Can't find %pD specified in Boot####\n", dp);
1233 ret = EFI_NOT_FOUND;
1234 goto out;
1235 }
1236
1237 /* Get file size */
1238 ret = efi_file_size(f, &filesize);
1239 if (ret != EFI_SUCCESS)
1240 goto out;
1241
1242 *fdt = calloc(1, filesize);
1243 if (!*fdt) {
1244 log_err("Out of memory\n");
1245 ret = EFI_OUT_OF_RESOURCES;
1246 goto out;
1247 }
1248 ret = EFI_CALL(f->read(f, &filesize, *fdt));
1249 if (ret != EFI_SUCCESS) {
1250 log_err("Can't read fdt\n");
1251 free(*fdt);
1252 *fdt = NULL;
1253 }
1254
1255out:
1256 efi_free_pool(dp);
1257 if (f)
1258 EFI_CALL(f->close(f));
1259
1260 return ret;
1261}
1262
1263/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001264 * efi_bootmgr_run() - execute EFI boot manager
1265 * @fdt: Flat device tree
1266 *
1267 * Invoke EFI boot manager and execute a binary depending on
1268 * boot options. If @fdt is not NULL, it will be passed to
1269 * the executed binary.
1270 *
1271 * Return: status code
1272 */
1273efi_status_t efi_bootmgr_run(void *fdt)
1274{
1275 efi_handle_t handle;
1276 void *load_options;
1277 efi_status_t ret;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001278 void *fdt_lo, *fdt_distro = NULL;
1279 efi_uintn_t fdt_size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001280
1281 /* Initialize EFI drivers */
1282 ret = efi_init_obj_list();
1283 if (ret != EFI_SUCCESS) {
1284 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1285 ret & ~EFI_ERROR_MASK);
1286 return CMD_RET_FAILURE;
1287 }
1288
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001289 ret = efi_bootmgr_load(&handle, &load_options);
1290 if (ret != EFI_SUCCESS) {
1291 log_notice("EFI boot manager: Cannot load any image\n");
1292 return ret;
1293 }
1294
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001295 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
1296 ret = load_fdt_from_load_option(&fdt_lo);
1297 if (ret != EFI_SUCCESS)
1298 return ret;
1299 if (fdt_lo)
1300 fdt = fdt_lo;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001301 if (!fdt) {
Heinrich Schuchardt8257f162024-07-13 13:30:29 +02001302 efi_load_distro_fdt(handle, &fdt_distro, &fdt_size);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001303 fdt = fdt_distro;
1304 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001305 }
1306
1307 /*
1308 * Needed in ACPI case to create reservations based on
1309 * control device-tree.
1310 */
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001311 ret = efi_install_fdt(fdt);
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001312
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001313 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001314 free(fdt_lo);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001315 if (fdt_distro)
1316 efi_free_pages((uintptr_t)fdt_distro,
1317 efi_size_in_pages(fdt_size));
1318 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001319
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001320 if (ret != EFI_SUCCESS) {
1321 if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
1322 free(load_options);
1323 else
1324 log_err("Unloading image failed\n");
1325
1326 return ret;
1327 }
1328
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001329 return do_bootefi_exec(handle, load_options);
1330}