blob: 4ac519228a694239ffc781ab732c2ecba99ff3e2 [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 Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040015#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090016#include <net.h>
AKASHI Takahirofd972f52022-04-28 17:09:39 +090017#include <efi_default_filename.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040018#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020019#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090020#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040021
22static const struct efi_boot_services *bs;
23static const struct efi_runtime_services *rs;
24
Masahisa Kojima949c4412023-11-10 13:25:40 +090025/**
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 */
34struct 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 Kojimac5ff0a02022-09-12 17:33:50 +090042const efi_guid_t efi_guid_bootmenu_auto_generated =
43 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
44
Rob Clarkc84c1102017-09-13 18:05:38 -040045/*
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 Schuchardt25c6be52020-08-07 17:47:13 +020057/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090058 * 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 */
67static
68struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
69{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020070 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090071 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090072
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 Schuchardtff08eac2023-05-13 10:36:21 +020081 handle = efi_dp_find_obj(device_path,
82 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000083 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000084 if (rem->type == DEVICE_PATH_TYPE_END) {
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020085 full_path = efi_dp_from_file(device_path,
86 "/EFI/BOOT/" BOOTEFI_NAME);
AKASHI Takahirofd972f52022-04-28 17:09:39 +090087 } 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 Takahirodac4d092022-05-12 11:29:02 +090098 * 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 */
109static 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 Apalodimas5dcefa72024-01-08 10:55:33 +0200133 dp = efi_dp_concat(dp, fp, false);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900134 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 */
160static 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
187out:
188 return ret;
189}
190
191/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900192 * 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 */
199static 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 */
231static 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
289err:
290 EFI_CALL(root->close(root));
291 efi_free_pool(full_path);
292
293 return ret;
294}
295
296/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900297 * 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 Kojima949c4412023-11-10 13:25:40 +0900301 *
302 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900303 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900304 * Return: status code
305 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900306static efi_status_t fill_default_file_path(struct udevice *blk,
307 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900308{
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 */
343static 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 Kojima54ce3dd2024-01-12 09:19:21 +0900354 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900355 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
375err:
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 */
388efi_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 */
429static 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 */
447static 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
546err:
547 efi_bootmgr_release_uridp_resource(ctx);
548
549 return ret;
550}
551
552/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900553 * try_load_from_media() - load file from media
554 *
555 * @file_path: file path
556 * @handle_img: on return handle for the newly installed image
557 *
558 * If @file_path contains a file name, load the file.
559 * If @file_path does not have a file name, search the architecture-specific
560 * fallback boot file and load it.
561 * TODO: If the FilePathList[0] device does not support
562 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
563 * call EFI_BOOT_SERVICES.ConnectController()
564 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
565 * not based on EFI_BLOCK_IO_PROTOCOL
566 *
567 * Return: status code
568 */
569static efi_status_t try_load_from_media(struct efi_device_path *file_path,
570 efi_handle_t *handle_img)
571{
572 efi_handle_t handle_blkdev;
573 efi_status_t ret = EFI_SUCCESS;
574 struct efi_device_path *rem, *dp = NULL;
575 struct efi_device_path *final_dp = file_path;
576
577 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
578 if (handle_blkdev) {
579 if (rem->type == DEVICE_PATH_TYPE_END) {
580 /* no file name present, try default file */
581 ret = fill_default_file_path(handle_blkdev->dev, &dp);
582 if (ret != EFI_SUCCESS)
583 return ret;
584
585 final_dp = dp;
586 }
587 }
588
589 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
590
591 efi_free_pool(dp);
592
593 return ret;
594}
595
596/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200597 * try_load_entry() - try to load image for boot option
598 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400599 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200600 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400601 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200602 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200603 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
604 * @handle: on return handle for the newly installed image
605 * @load_options: load options set on the loaded image protocol
606 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400607 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200608static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
609 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400610{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900611 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200612 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900613 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200614 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900615 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400616
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200617 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clarkc84c1102017-09-13 18:05:38 -0400618
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200619 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400620 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900621 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400622
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200623 ret = efi_deserialize_load_option(&lo, load_option, &size);
624 if (ret != EFI_SUCCESS) {
625 log_warning("Invalid load option for %ls\n", varname);
626 goto error;
627 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400628
629 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900630 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400631
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200632 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
633 lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400634
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900635 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
636 /* file_path doesn't contain a device path */
637 ret = try_load_from_short_path(lo.file_path, handle);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900638 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
639 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
640 ret = try_load_from_uri_path(
641 (struct efi_device_path_uri *)lo.file_path,
642 lo.label, handle);
643 else
644 ret = EFI_LOAD_ERROR;
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900645 } else {
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900646 ret = try_load_from_media(lo.file_path, handle);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900647 }
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200648 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200649 log_warning("Loading %ls '%ls' failed\n",
650 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400651 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200652 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400653
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900654 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
655 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass90975372022-01-23 12:55:12 -0700656 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200657 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200658 attributes, sizeof(n), &n, false);
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200659 if (ret != EFI_SUCCESS)
660 goto unload;
661 /* try to register load file2 for initrd's */
662 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
663 ret = efi_initrd_register();
664 if (ret != EFI_SUCCESS)
665 goto unload;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900666 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900667
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200668 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900669 } else {
670 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400671 }
672
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200673 /* Set load options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900674 if (size >= sizeof(efi_guid_t) &&
675 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
676 size = 0;
677
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200678 if (size) {
679 *load_options = malloc(size);
680 if (!*load_options) {
681 ret = EFI_OUT_OF_RESOURCES;
682 goto error;
683 }
684 memcpy(*load_options, lo.optional_data, size);
685 ret = efi_set_load_options(*handle, size, *load_options);
686 } else {
Heinrich Schuchardt7c2a8592020-12-27 15:46:00 +0100687 *load_options = NULL;
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200688 }
689
Rob Clarkc84c1102017-09-13 18:05:38 -0400690error:
691 free(load_option);
692
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900693 return ret;
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200694
695unload:
696 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
697 log_err("Unloading image failed\n");
698 free(load_option);
699
700 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400701}
702
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200703/**
704 * efi_bootmgr_load() - try to load from BootNext or BootOrder
705 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900706 * Attempt to load from BootNext or in the order specified by BootOrder
707 * EFI variable, the available load-options, finding and returning
708 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200709 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200710 * @handle: on return handle for the newly installed image
711 * @load_options: load options set on the loaded image protocol
712 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400713 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200714efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400715{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900716 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200717 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400718 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900719 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400720
Rob Clarkc84c1102017-09-13 18:05:38 -0400721 bs = systab.boottime;
722 rs = systab.runtime;
723
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900724 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900725 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700726 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200727 &efi_global_variable_guid,
728 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900729 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
730 /* BootNext does exist here */
731 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200732 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900733
734 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700735 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200736 &efi_global_variable_guid,
737 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900738
739 /* load BootNext */
740 if (ret == EFI_SUCCESS) {
741 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200742 ret = try_load_entry(bootnext, handle,
743 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900744 if (ret == EFI_SUCCESS)
745 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200746 log_warning(
747 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900748 }
749 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200750 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900751 }
752 }
753
754 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700755 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100756 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200757 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900758 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400759 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100760 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400761
762 num = size / sizeof(uint16_t);
763 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200764 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200765 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900766 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400767 break;
768 }
769
770 free(bootorder);
771
772error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900773 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400774}
Raymond Mao70a76c52023-06-19 14:22:58 -0700775
776/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900777 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700778 *
779 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900780 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900781 * @handles: pointer to block device handles
782 * @count: On entry number of handles to block devices.
783 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900784 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700785 * Return: status code
786 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900787static efi_status_t
788efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900789 efi_uintn_t index, efi_handle_t *handles,
790 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700791{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900792 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700793 struct efi_handler *handler;
794 efi_status_t ret = EFI_SUCCESS;
795
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900796 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700797 u16 *p;
798 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
799 char *optional_data;
800 struct efi_load_option lo;
801 char buf[BOOTMENU_DEVICE_NAME_MAX];
802 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700803 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900804 struct efi_block_io *blkio;
805
806 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
807 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700808
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900809 if (blkio->media->logical_partition)
810 continue;
811
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900812 if (removable != (blkio->media->removable_media != 0))
813 continue;
814
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900815 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700816 if (ret != EFI_SUCCESS)
817 continue;
818 ret = efi_protocol_open(handler, (void **)&device_path,
819 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
820 if (ret != EFI_SUCCESS)
821 continue;
822
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900823 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700824 if (ret != EFI_SUCCESS)
825 continue;
826
827 p = dev_name;
828 utf8_utf16_strncpy(&p, buf, strlen(buf));
829
Raymond Maob5542a62023-06-19 14:23:01 -0700830 /* prefer to short form device path */
831 short_dp = efi_dp_shorten(device_path);
832 if (short_dp)
833 device_path = short_dp;
834
Raymond Mao70a76c52023-06-19 14:22:58 -0700835 lo.label = dev_name;
836 lo.attributes = LOAD_OPTION_ACTIVE;
837 lo.file_path = device_path;
838 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
839 /*
840 * Set the dedicated guid to optional_data, it is used to identify
841 * the boot option that automatically generated by the bootmenu.
842 * efi_serialize_load_option() expects optional_data is null-terminated
843 * utf8 string, so set the "1234567" string to allocate enough space
844 * to store guid, instead of realloc the load_option.
845 */
846 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900847 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
848 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700849 ret = EFI_OUT_OF_RESOURCES;
850 goto out;
851 }
852 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900853 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700854 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900855 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900856
857 if (num >= *count)
858 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700859 }
860
861out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900862 *count = num;
863
Raymond Mao70a76c52023-06-19 14:22:58 -0700864 return ret;
865}
866
867/**
868 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
869 *
870 * @opt: pointer to the media boot option structure
871 * @count: number of media boot option structure
872 * Return: status code
873 */
874static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
875 efi_status_t count)
876{
877 efi_uintn_t size;
878 void *load_option;
879 u32 i, list_size = 0;
880 struct efi_load_option lo;
881 u16 *var_name16 = NULL;
882 u16 varname[] = u"Boot####";
883 efi_status_t ret = EFI_SUCCESS;
884 u16 *delete_index_list = NULL, *p;
885 efi_uintn_t buf_size;
886
887 buf_size = 128;
888 var_name16 = malloc(buf_size);
889 if (!var_name16)
890 return EFI_OUT_OF_RESOURCES;
891
892 var_name16[0] = 0;
893 for (;;) {
894 int index;
895 efi_guid_t guid;
896 efi_uintn_t tmp;
897
898 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
899 if (ret == EFI_NOT_FOUND) {
900 /*
901 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
902 * This should be treated as success.
903 */
904 ret = EFI_SUCCESS;
905 break;
906 }
907
908 if (ret != EFI_SUCCESS)
909 goto out;
910
911 if (!efi_varname_is_load_option(var_name16, &index))
912 continue;
913
914 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
915 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
916 if (!load_option)
917 continue;
918
919 tmp = size;
920 ret = efi_deserialize_load_option(&lo, load_option, &size);
921 if (ret != EFI_SUCCESS)
922 goto next;
923
924 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
925 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
926 for (i = 0; i < count; i++) {
927 if (opt[i].size == tmp &&
928 memcmp(opt[i].lo, load_option, tmp) == 0) {
929 opt[i].exist = true;
930 break;
931 }
932 }
933
934 /*
935 * The entire list of variables must be retrieved by
936 * efi_get_next_variable_name_int() before deleting the invalid
937 * boot option, just save the index here.
938 */
939 if (i == count) {
940 p = realloc(delete_index_list, sizeof(u32) *
941 (list_size + 1));
942 if (!p) {
943 ret = EFI_OUT_OF_RESOURCES;
944 goto out;
945 }
946 delete_index_list = p;
947 delete_index_list[list_size++] = index;
948 }
949 }
950next:
951 free(load_option);
952 }
953
954 /* delete all invalid boot options */
955 for (i = 0; i < list_size; i++) {
956 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
957 if (ret != EFI_SUCCESS)
958 goto out;
959 }
960
961out:
962 free(var_name16);
963 free(delete_index_list);
964
965 return ret;
966}
967
968/**
969 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
970 *
971 * @buf: pointer to the buffer to store boot option variable name
972 * @buf_size: buffer size
973 * @index: pointer to store the index in the BootOrder variable
974 * Return: status code
975 */
976efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
977 unsigned int *index)
978{
979 u32 i;
980 efi_status_t ret;
981 efi_uintn_t size;
982
983 if (buf_size < u16_strsize(u"Boot####"))
984 return EFI_BUFFER_TOO_SMALL;
985
986 for (i = 0; i <= 0xFFFF; i++) {
987 size = 0;
988 efi_create_indexed_name(buf, buf_size, "Boot", i);
989 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
990 NULL, &size, NULL, NULL);
991 if (ret == EFI_BUFFER_TOO_SMALL)
992 continue;
993 else
994 break;
995 }
996
997 if (i > 0xFFFF)
998 return EFI_OUT_OF_RESOURCES;
999
1000 *index = i;
1001
1002 return EFI_SUCCESS;
1003}
1004
1005/**
1006 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1007 *
1008 * @index: "Boot####" index to append to BootOrder variable
1009 * Return: status code
1010 */
1011efi_status_t efi_bootmgr_append_bootorder(u16 index)
1012{
1013 u16 *bootorder;
1014 efi_status_t ret;
1015 u16 *new_bootorder = NULL;
1016 efi_uintn_t last, size, new_size;
1017
1018 /* append new boot option */
1019 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1020 last = size / sizeof(u16);
1021 new_size = size + sizeof(u16);
1022 new_bootorder = calloc(1, new_size);
1023 if (!new_bootorder) {
1024 ret = EFI_OUT_OF_RESOURCES;
1025 goto out;
1026 }
1027 memcpy(new_bootorder, bootorder, size);
1028 new_bootorder[last] = index;
1029
1030 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1031 EFI_VARIABLE_NON_VOLATILE |
1032 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1033 EFI_VARIABLE_RUNTIME_ACCESS,
1034 new_size, new_bootorder, false);
1035 if (ret != EFI_SUCCESS)
1036 goto out;
1037
1038out:
1039 free(bootorder);
1040 free(new_bootorder);
1041
1042 return ret;
1043}
1044
1045/**
1046 * efi_bootmgr_delete_boot_option() - delete selected boot option
1047 *
1048 * @boot_index: boot option index to delete
1049 * Return: status code
1050 */
1051efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1052{
1053 u16 *bootorder;
1054 u16 varname[9];
1055 efi_status_t ret;
1056 unsigned int index;
1057 efi_uintn_t num, size;
1058
1059 efi_create_indexed_name(varname, sizeof(varname),
1060 "Boot", boot_index);
1061 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1062 0, 0, NULL, false);
1063 if (ret != EFI_SUCCESS) {
1064 log_err("delete boot option(%ls) failed\n", varname);
1065 return ret;
1066 }
1067
1068 /* update BootOrder if necessary */
1069 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1070 if (!bootorder)
1071 return EFI_SUCCESS;
1072
1073 num = size / sizeof(u16);
1074 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1075 return EFI_SUCCESS;
1076
1077 memmove(&bootorder[index], &bootorder[index + 1],
1078 (num - index - 1) * sizeof(u16));
1079 size -= sizeof(u16);
1080 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1081 EFI_VARIABLE_NON_VOLATILE |
1082 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1083 EFI_VARIABLE_RUNTIME_ACCESS,
1084 size, bootorder, false);
1085
1086 return ret;
1087}
1088
1089/**
1090 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1091 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001092 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001093 * This function also provide the BOOT#### variable maintenance for
1094 * the media device entries.
1095 * - Automatically create the BOOT#### variable for the newly detected device,
1096 * this BOOT#### variable is distinguished by the special GUID
1097 * stored in the EFI_LOAD_OPTION.optional_data
1098 * - If the device is not attached to the system, the associated BOOT#### variable
1099 * is automatically deleted.
1100 *
1101 * Return: status code
1102 */
1103efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1104{
1105 u32 i;
1106 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001107 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001108 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001109 struct eficonfig_media_boot_option *opt = NULL;
1110
1111 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001112 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001113 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001114 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001115 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001116 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001117
1118 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001119 if (!opt) {
1120 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001121 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001122 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001123
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001124 /* parse removable block io followed by fixed block io */
1125 num = count;
1126 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001127 if (ret != EFI_SUCCESS)
1128 goto out;
1129
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001130 total = num;
1131 num = count;
1132 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1133 if (ret != EFI_SUCCESS)
1134 goto out;
1135
1136 total = num;
1137
Raymond Mao70a76c52023-06-19 14:22:58 -07001138 /*
1139 * System hardware configuration may vary depending on the user setup.
1140 * The boot option is automatically added by the bootmenu.
1141 * If the device is not attached to the system, the boot option needs
1142 * to be deleted.
1143 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001144 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001145 if (ret != EFI_SUCCESS)
1146 goto out;
1147
1148 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001149 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001150 u32 boot_index;
1151 u16 var_name[9];
1152
1153 if (!opt[i].exist) {
1154 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1155 &boot_index);
1156 if (ret != EFI_SUCCESS)
1157 goto out;
1158
1159 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1160 EFI_VARIABLE_NON_VOLATILE |
1161 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1162 EFI_VARIABLE_RUNTIME_ACCESS,
1163 opt[i].size, opt[i].lo, false);
1164 if (ret != EFI_SUCCESS)
1165 goto out;
1166
1167 ret = efi_bootmgr_append_bootorder(boot_index);
1168 if (ret != EFI_SUCCESS) {
1169 efi_set_variable_int(var_name, &efi_global_variable_guid,
1170 0, 0, NULL, false);
1171 goto out;
1172 }
1173 }
1174 }
1175
1176out:
1177 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001178 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001179 free(opt[i].lo);
1180 }
1181 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001182 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001183
Raymond Maoa35784d2023-06-19 14:22:59 -07001184 if (ret == EFI_NOT_FOUND)
1185 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001186 return ret;
1187}
1188
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001189/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001190 * efi_bootmgr_run() - execute EFI boot manager
1191 * @fdt: Flat device tree
1192 *
1193 * Invoke EFI boot manager and execute a binary depending on
1194 * boot options. If @fdt is not NULL, it will be passed to
1195 * the executed binary.
1196 *
1197 * Return: status code
1198 */
1199efi_status_t efi_bootmgr_run(void *fdt)
1200{
1201 efi_handle_t handle;
1202 void *load_options;
1203 efi_status_t ret;
1204
1205 /* Initialize EFI drivers */
1206 ret = efi_init_obj_list();
1207 if (ret != EFI_SUCCESS) {
1208 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1209 ret & ~EFI_ERROR_MASK);
1210 return CMD_RET_FAILURE;
1211 }
1212
1213 ret = efi_install_fdt(fdt);
1214 if (ret != EFI_SUCCESS)
1215 return ret;
1216
1217 ret = efi_bootmgr_load(&handle, &load_options);
1218 if (ret != EFI_SUCCESS) {
1219 log_notice("EFI boot manager: Cannot load any image\n");
1220 return ret;
1221 }
1222
1223 return do_bootefi_exec(handle, load_options);
1224}