blob: b9437a81c642351ddc1da3b6e873b744e875ff54 [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>
Simon Glass37972f42025-05-24 11:28:21 -060015#include <efi_device_path.h>
Tom Rinic31301c2025-05-15 17:31:50 -060016#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040018#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090019#include <net.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040020#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020021#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090022#include <asm/unaligned.h>
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +053023#include <linux/kernel.h>
24#include <linux/sizes.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040025
26static const struct efi_boot_services *bs;
27static const struct efi_runtime_services *rs;
28
Masahisa Kojima949c4412023-11-10 13:25:40 +090029/**
30 * struct uridp_context - uri device path resource
31 *
32 * @image_size: image size
33 * @image_addr: image address
34 * @loaded_dp: pointer to loaded device path
35 * @ramdisk_blk_dev: pointer to the ramdisk blk device
36 * @mem_handle: efi_handle to the loaded PE-COFF image
37 */
38struct uridp_context {
39 ulong image_size;
40 ulong image_addr;
41 struct efi_device_path *loaded_dp;
42 struct udevice *ramdisk_blk_dev;
43 efi_handle_t mem_handle;
44};
45
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090046const efi_guid_t efi_guid_bootmenu_auto_generated =
47 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
48
Rob Clarkc84c1102017-09-13 18:05:38 -040049/*
50 * bootmgr implements the logic of trying to find a payload to boot
51 * based on the BootOrder + BootXXXX variables, and then loading it.
52 *
53 * TODO detecting a special key held (f9?) and displaying a boot menu
54 * like you would get on a PC would be clever.
55 *
56 * TODO if we had a way to write and persist variables after the OS
57 * has started, we'd also want to check OsIndications to see if we
58 * should do normal or recovery boot.
59 */
60
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020061/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090062 * expand_media_path() - expand a device path for default file name
63 * @device_path: device path to check against
64 *
65 * If @device_path is a media or disk partition which houses a file
66 * system, this function returns a full device path which contains
67 * an architecture-specific default file name for removable media.
68 *
69 * Return: a newly allocated device path
70 */
71static
72struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
73{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020074 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090075 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090076
77 if (!device_path)
78 return NULL;
79
80 /*
81 * If device_path is a (removable) media or partition which provides
82 * simple file system protocol, append a default file name to support
83 * booting from removable media.
84 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020085 handle = efi_dp_find_obj(device_path,
86 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000087 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000088 if (rem->type == DEVICE_PATH_TYPE_END) {
Simon Glass3b1e60b2024-11-07 14:31:43 -070089 char fname[30];
90
91 snprintf(fname, sizeof(fname), "/EFI/BOOT/%s",
92 efi_get_basename());
93 full_path = efi_dp_from_file(device_path, fname);
94
AKASHI Takahirofd972f52022-04-28 17:09:39 +090095 } else {
96 full_path = efi_dp_dup(device_path);
97 }
98 } else {
99 full_path = efi_dp_dup(device_path);
100 }
101
102 return full_path;
103}
104
105/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900106 * try_load_from_file_path() - try to load a file
107 *
108 * Given a file media path iterate through a list of handles and try to
109 * to load the file from each of them until the first success.
110 *
111 * @fs_handles: array of handles with the simple file protocol
112 * @num: number of handles in fs_handles
113 * @fp: file path to open
114 * @handle: on return pointer to handle for loaded image
115 * @removable: if true only consider removable media, else only non-removable
116 */
117static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
118 efi_uintn_t num,
119 struct efi_device_path *fp,
120 efi_handle_t *handle,
121 bool removable)
122{
123 struct efi_handler *handler;
124 struct efi_device_path *dp;
125 int i;
126 efi_status_t ret;
127
128 for (i = 0; i < num; i++) {
129 if (removable != efi_disk_is_removable(fs_handles[i]))
130 continue;
131
132 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
133 &handler);
134 if (ret != EFI_SUCCESS)
135 continue;
136
137 dp = handler->protocol_interface;
138 if (!dp)
139 continue;
140
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200141 dp = efi_dp_concat(dp, fp, 0);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900142 if (!dp)
143 continue;
144
145 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
146 handle));
147 efi_free_pool(dp);
148 if (ret == EFI_SUCCESS)
149 return ret;
150 }
151
152 return EFI_NOT_FOUND;
153}
154
155/**
156 * try_load_from_short_path
157 * @fp: file path
158 * @handle: pointer to handle for newly installed image
159 *
160 * Enumerate all the devices which support file system operations,
161 * prepend its media device path to the file path, @fp, and
162 * try to load the file.
163 * This function should be called when handling a short-form path
164 * which is starting with a file device path.
165 *
166 * Return: status code
167 */
168static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
169 efi_handle_t *handle)
170{
171 efi_handle_t *fs_handles;
172 efi_uintn_t num;
173 efi_status_t ret;
174
175 ret = EFI_CALL(efi_locate_handle_buffer(
176 BY_PROTOCOL,
177 &efi_simple_file_system_protocol_guid,
178 NULL,
179 &num, &fs_handles));
180 if (ret != EFI_SUCCESS)
181 return ret;
182 if (!num)
183 return EFI_NOT_FOUND;
184
185 /* removable media first */
186 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
187 if (ret == EFI_SUCCESS)
188 goto out;
189
190 /* fixed media */
191 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
192 if (ret == EFI_SUCCESS)
193 goto out;
194
195out:
196 return ret;
197}
198
199/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900200 * mount_image() - mount the image with blkmap
201 *
202 * @lo_label: u16 label string of load option
203 * @addr: image address
204 * @size: image size
205 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
206 */
207static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
208{
209 int err;
210 struct blkmap *bm;
211 struct udevice *bm_dev;
212 char *label = NULL, *p;
213
214 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
215 if (!label)
216 return NULL;
217
218 p = label;
219 utf16_utf8_strcpy(&p, lo_label);
220 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
221 if (err) {
222 efi_free_pool(label);
223 return NULL;
224 }
225 bm = dev_get_plat(bm_dev);
226
227 efi_free_pool(label);
228
229 return bm->blk;
230}
231
232/**
233 * search_default_file() - search default file
234 *
235 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
236 * @loaded_dp: pointer to default file device path
237 * Return: status code
238 */
239static efi_status_t search_default_file(struct udevice *dev,
240 struct efi_device_path **loaded_dp)
241{
242 efi_status_t ret;
243 efi_handle_t handle;
244 u16 *default_file_name = NULL;
245 struct efi_file_handle *root, *f;
246 struct efi_device_path *dp = NULL, *fp = NULL;
247 struct efi_simple_file_system_protocol *file_system;
248 struct efi_device_path *device_path, *full_path = NULL;
249
250 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
251 log_warning("DM_TAG_EFI not found\n");
252 return EFI_INVALID_PARAMETER;
253 }
254
255 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
256 (void **)&device_path, efi_root, NULL,
257 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
258 if (ret != EFI_SUCCESS)
259 return ret;
260
261 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
262 (void **)&file_system, efi_root, NULL,
263 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
264 if (ret != EFI_SUCCESS)
265 return ret;
266
267 ret = EFI_CALL(file_system->open_volume(file_system, &root));
268 if (ret != EFI_SUCCESS)
269 return ret;
270
271 full_path = expand_media_path(device_path);
272 ret = efi_dp_split_file_path(full_path, &dp, &fp);
273 if (ret != EFI_SUCCESS)
274 goto err;
275
276 default_file_name = efi_dp_str(fp);
277 efi_free_pool(dp);
278 efi_free_pool(fp);
279 if (!default_file_name) {
280 ret = EFI_OUT_OF_RESOURCES;
281 goto err;
282 }
283
284 ret = EFI_CALL(root->open(root, &f, default_file_name,
285 EFI_FILE_MODE_READ, 0));
286 efi_free_pool(default_file_name);
287 if (ret != EFI_SUCCESS)
288 goto err;
289
290 EFI_CALL(f->close(f));
291 EFI_CALL(root->close(root));
292
293 *loaded_dp = full_path;
294
295 return EFI_SUCCESS;
296
297err:
298 EFI_CALL(root->close(root));
299 efi_free_pool(full_path);
300
301 return ret;
302}
303
304/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900305 * fill_default_file_path() - get fallback boot device path for block device
306 *
307 * Provide the device path to the fallback UEFI boot file, e.g.
308 * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900309 *
310 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900311 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900312 * Return: status code
313 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900314static efi_status_t fill_default_file_path(struct udevice *blk,
315 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900316{
317 efi_status_t ret;
318 struct udevice *partition;
319
320 /* image that has no partition table but a file system */
321 ret = search_default_file(blk, dp);
322 if (ret == EFI_SUCCESS)
323 return ret;
324
325 /* try the partitions */
326 device_foreach_child(partition, blk) {
327 enum uclass_id id;
328
329 id = device_get_uclass_id(partition);
330 if (id != UCLASS_PARTITION)
331 continue;
332
333 ret = search_default_file(partition, dp);
334 if (ret == EFI_SUCCESS)
335 return ret;
336 }
337
338 return EFI_NOT_FOUND;
339}
340
341/**
342 * prepare_loaded_image() - prepare ramdisk for downloaded image
343 *
344 * @label: label of load option
345 * @addr: image address
346 * @size: image size
347 * @dp: pointer to default file device path
348 * @blk: pointer to created blk udevice
349 * Return: status code
350 */
351static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
352 struct efi_device_path **dp,
353 struct udevice **blk)
354{
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530355 u64 pages;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900356 efi_status_t ret;
357 struct udevice *ramdisk_blk;
358
359 ramdisk_blk = mount_image(label, addr, size);
360 if (!ramdisk_blk)
361 return EFI_LOAD_ERROR;
362
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900363 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900364 if (ret != EFI_SUCCESS) {
365 log_info("Cannot boot from downloaded image\n");
366 goto err;
367 }
368
369 /*
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530370 * Linux supports 'pmem' which allows OS installers to find, reclaim
371 * the mounted images and continue the installation since the contents
372 * of the pmem region are treated as local media.
373 *
374 * The memory regions used for it needs to be carved out of the EFI
375 * memory map.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900376 */
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530377 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
378 ret = efi_update_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY,
379 false, true);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900380 if (ret != EFI_SUCCESS) {
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530381 log_err("Failed to reserve memory\n");
Masahisa Kojima949c4412023-11-10 13:25:40 +0900382 goto err;
383 }
384
385 *blk = ramdisk_blk;
386
387 return EFI_SUCCESS;
388
389err:
390 if (blkmap_destroy(ramdisk_blk->parent))
391 log_err("Destroying blkmap failed\n");
392
393 return ret;
394}
395
396/**
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300397 * efi_bootmgr_release_uridp() - cleanup uri device path resource
Masahisa Kojima949c4412023-11-10 13:25:40 +0900398 *
399 * @ctx: event context
400 * Return: status code
401 */
Ilias Apalodimas55c05712024-10-26 10:37:32 +0300402static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900403{
404 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300405 efi_status_t ret2 = EFI_SUCCESS;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900406
407 if (!ctx)
408 return ret;
409
410 /* cleanup for iso or img image */
411 if (ctx->ramdisk_blk_dev) {
412 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
413 EFI_CONVENTIONAL_MEMORY);
414 if (ret != EFI_SUCCESS)
415 log_err("Reclaiming memory failed\n");
416
417 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
418 log_err("Destroying blkmap failed\n");
419 ret = EFI_DEVICE_ERROR;
420 }
421 }
422
423 /* cleanup for PE-COFF image */
424 if (ctx->mem_handle) {
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300425 ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle,
426 &efi_guid_device_path,
427 ctx->loaded_dp,
428 NULL);
429 if (ret2 != EFI_SUCCESS)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900430 log_err("Uninstall device_path protocol failed\n");
431 }
432
433 efi_free_pool(ctx->loaded_dp);
434 free(ctx);
435
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300436 return ret == EFI_SUCCESS ? ret2 : ret;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900437}
438
439/**
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300440 * efi_bootmgr_http_return() - return to efibootmgr callback
Masahisa Kojima949c4412023-11-10 13:25:40 +0900441 *
442 * @event: the event for which this notification function is registered
443 * @context: event context
444 */
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300445static void EFIAPI efi_bootmgr_http_return(struct efi_event *event,
446 void *context)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900447{
448 efi_status_t ret;
449
450 EFI_ENTRY("%p, %p", event, context);
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300451 ret = efi_bootmgr_release_uridp(context);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900452 EFI_EXIT(ret);
453}
454
455/**
456 * try_load_from_uri_path() - Handle the URI device path
457 *
458 * @uridp: uri device path
459 * @lo_label: label of load option
460 * @handle: pointer to handle for newly installed image
461 * Return: status code
462 */
463static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
464 u16 *lo_label,
465 efi_handle_t *handle)
466{
467 char *s;
468 int err;
469 int uri_len;
470 efi_status_t ret;
471 void *source_buffer;
472 efi_uintn_t source_size;
473 struct uridp_context *ctx;
474 struct udevice *blk = NULL;
475 struct efi_event *event = NULL;
476 efi_handle_t mem_handle = NULL;
477 struct efi_device_path *loaded_dp;
478 static ulong image_size, image_addr;
479
480 ctx = calloc(1, sizeof(struct uridp_context));
481 if (!ctx)
482 return EFI_OUT_OF_RESOURCES;
483
Ilias Apalodimasabddf282025-05-23 16:04:04 +0300484 s = env_get("ipaddr");
485 if (!s && dhcp_run(0, NULL, false)) {
486 log_err("Error: Can't find a valid IP address\n");
487 ret = EFI_DEVICE_ERROR;
488 goto err;
489 }
490
Masahisa Kojima949c4412023-11-10 13:25:40 +0900491 s = env_get("loadaddr");
492 if (!s) {
493 log_err("Error: loadaddr is not set\n");
494 ret = EFI_INVALID_PARAMETER;
495 goto err;
496 }
497
498 image_addr = hextoul(s, NULL);
Adriano Cordovab479fc42024-12-04 00:05:16 -0300499 err = wget_do_request(image_addr, uridp->uri);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900500 if (err < 0) {
501 ret = EFI_INVALID_PARAMETER;
502 goto err;
503 }
504
505 image_size = env_get_hex("filesize", 0);
506 if (!image_size) {
507 ret = EFI_INVALID_PARAMETER;
508 goto err;
509 }
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530510 /*
511 * Depending on the kernel configuration, pmem memory areas must be
512 * page aligned or 2MiB aligned. PowerPC is an exception here and
513 * requires 16MiB alignment, but since we don't have EFI support for
514 * it, limit the alignment to 2MiB.
515 */
516 image_size = ALIGN(image_size, SZ_2M);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900517
518 /*
519 * If the file extension is ".iso" or ".img", mount it and try to load
520 * the default file.
521 * If the file is PE-COFF image, load the downloaded file.
522 */
523 uri_len = strlen(uridp->uri);
524 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
525 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
526 ret = prepare_loaded_image(lo_label, image_addr, image_size,
527 &loaded_dp, &blk);
528 if (ret != EFI_SUCCESS)
529 goto err;
530
531 source_buffer = NULL;
532 source_size = 0;
533 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
534 /*
535 * loaded_dp must exist until efi application returns,
536 * will be freed in return_to_efibootmgr event callback.
537 */
538 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Simon Glass37972f42025-05-24 11:28:21 -0600539 image_addr, image_size);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900540 ret = efi_install_multiple_protocol_interfaces(
541 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
542 if (ret != EFI_SUCCESS)
543 goto err;
544
545 source_buffer = (void *)image_addr;
546 source_size = image_size;
547 } else {
548 log_err("Error: file type is not supported\n");
549 ret = EFI_UNSUPPORTED;
550 goto err;
551 }
552
553 ctx->image_size = image_size;
554 ctx->image_addr = image_addr;
555 ctx->loaded_dp = loaded_dp;
556 ctx->ramdisk_blk_dev = blk;
557 ctx->mem_handle = mem_handle;
558
559 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
560 source_size, handle));
561 if (ret != EFI_SUCCESS)
562 goto err;
563
564 /* create event for cleanup when the image returns or error occurs */
565 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300566 efi_bootmgr_http_return, ctx,
Masahisa Kojima949c4412023-11-10 13:25:40 +0900567 &efi_guid_event_group_return_to_efibootmgr,
568 &event);
569 if (ret != EFI_SUCCESS) {
570 log_err("Creating event failed\n");
571 goto err;
572 }
573
574 return ret;
575
576err:
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300577 efi_bootmgr_release_uridp(ctx);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900578
579 return ret;
580}
581
582/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900583 * try_load_from_media() - load file from media
584 *
585 * @file_path: file path
586 * @handle_img: on return handle for the newly installed image
587 *
588 * If @file_path contains a file name, load the file.
589 * If @file_path does not have a file name, search the architecture-specific
590 * fallback boot file and load it.
591 * TODO: If the FilePathList[0] device does not support
592 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
593 * call EFI_BOOT_SERVICES.ConnectController()
594 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
595 * not based on EFI_BLOCK_IO_PROTOCOL
596 *
597 * Return: status code
598 */
599static efi_status_t try_load_from_media(struct efi_device_path *file_path,
600 efi_handle_t *handle_img)
601{
602 efi_handle_t handle_blkdev;
603 efi_status_t ret = EFI_SUCCESS;
604 struct efi_device_path *rem, *dp = NULL;
605 struct efi_device_path *final_dp = file_path;
606
607 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
608 if (handle_blkdev) {
609 if (rem->type == DEVICE_PATH_TYPE_END) {
610 /* no file name present, try default file */
611 ret = fill_default_file_path(handle_blkdev->dev, &dp);
612 if (ret != EFI_SUCCESS)
613 return ret;
614
615 final_dp = dp;
616 }
617 }
618
619 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
620
621 efi_free_pool(dp);
622
623 return ret;
624}
625
626/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200627 * try_load_entry() - try to load image for boot option
628 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400629 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200630 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400631 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200632 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200633 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
634 * @handle: on return handle for the newly installed image
635 * @load_options: load options set on the loaded image protocol
636 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400637 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200638static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
639 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400640{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900641 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200642 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900643 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200644 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900645 efi_status_t ret;
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200646 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400647
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200648 *handle = NULL;
649 *load_options = NULL;
Rob Clarkc84c1102017-09-13 18:05:38 -0400650
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200651 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200652 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400653 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900654 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400655
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200656 ret = efi_deserialize_load_option(&lo, load_option, &size);
657 if (ret != EFI_SUCCESS) {
658 log_warning("Invalid load option for %ls\n", varname);
659 goto error;
660 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400661
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200662 if (!(lo.attributes & LOAD_OPTION_ACTIVE)) {
663 ret = EFI_LOAD_ERROR;
664 goto error;
665 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400666
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200667 log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400668
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200669 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
670 /* file_path doesn't contain a device path */
671 ret = try_load_from_short_path(lo.file_path, handle);
672 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
673 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
674 ret = try_load_from_uri_path(
675 (struct efi_device_path_uri *)lo.file_path,
676 lo.label, handle);
677 else
678 ret = EFI_LOAD_ERROR;
679 } else {
680 ret = try_load_from_media(lo.file_path, handle);
681 }
682 if (ret != EFI_SUCCESS) {
683 log_warning("Loading %ls '%ls' failed\n",
684 varname, lo.label);
685 goto error;
686 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400687
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200688 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
689 EFI_VARIABLE_RUNTIME_ACCESS;
690 ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid,
691 attributes, sizeof(n), &n, false);
692 if (ret != EFI_SUCCESS)
693 goto error;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900694
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200695 /* try to register load file2 for initrd's */
696 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
Adriano Cordova02d049d2025-03-19 11:44:59 -0300697 ret = efi_initrd_register(NULL);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200698 if (ret != EFI_SUCCESS)
699 goto error;
Rob Clarkc84c1102017-09-13 18:05:38 -0400700 }
701
Ilias Apalodimas2f304322025-03-28 14:58:18 +0200702 log_info("Booting: Label: %ls Device path: %pD\n", lo.label, lo.file_path);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200703
704 /* Ignore the optional data in auto-generated boot options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900705 if (size >= sizeof(efi_guid_t) &&
706 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
707 size = 0;
708
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200709 /* Set optional data in loaded file protocol */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200710 if (size) {
711 *load_options = malloc(size);
712 if (!*load_options) {
713 ret = EFI_OUT_OF_RESOURCES;
714 goto error;
715 }
716 memcpy(*load_options, lo.optional_data, size);
717 ret = efi_set_load_options(*handle, size, *load_options);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200718 if (ret != EFI_SUCCESS)
719 free(load_options);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200720 }
721
Rob Clarkc84c1102017-09-13 18:05:38 -0400722error:
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200723 if (ret != EFI_SUCCESS && *handle &&
724 EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200725 log_err("Unloading image failed\n");
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200726
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200727 free(load_option);
728
729 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400730}
731
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200732/**
733 * efi_bootmgr_load() - try to load from BootNext or BootOrder
734 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900735 * Attempt to load from BootNext or in the order specified by BootOrder
736 * EFI variable, the available load-options, finding and returning
737 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200738 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200739 * @handle: on return handle for the newly installed image
740 * @load_options: load options set on the loaded image protocol
741 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400742 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200743efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400744{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900745 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200746 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400747 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900748 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400749
Rob Clarkc84c1102017-09-13 18:05:38 -0400750 bs = systab.boottime;
751 rs = systab.runtime;
752
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900753 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900754 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700755 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200756 &efi_global_variable_guid,
757 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900758 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
759 /* BootNext does exist here */
760 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200761 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900762
763 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700764 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200765 &efi_global_variable_guid,
766 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900767
768 /* load BootNext */
769 if (ret == EFI_SUCCESS) {
770 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200771 ret = try_load_entry(bootnext, handle,
772 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900773 if (ret == EFI_SUCCESS)
774 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200775 log_warning(
776 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900777 }
778 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200779 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900780 }
781 }
782
783 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700784 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100785 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200786 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900787 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400788 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100789 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400790
791 num = size / sizeof(uint16_t);
792 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200793 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200794 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900795 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400796 break;
797 }
798
799 free(bootorder);
800
801error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900802 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400803}
Raymond Mao70a76c52023-06-19 14:22:58 -0700804
805/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900806 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700807 *
808 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900809 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900810 * @handles: pointer to block device handles
811 * @count: On entry number of handles to block devices.
812 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900813 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700814 * Return: status code
815 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900816static efi_status_t
817efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900818 efi_uintn_t index, efi_handle_t *handles,
819 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700820{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900821 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700822 struct efi_handler *handler;
823 efi_status_t ret = EFI_SUCCESS;
824
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900825 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700826 u16 *p;
827 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
828 char *optional_data;
829 struct efi_load_option lo;
830 char buf[BOOTMENU_DEVICE_NAME_MAX];
831 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700832 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900833 struct efi_block_io *blkio;
834
835 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
836 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700837
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900838 if (blkio->media->logical_partition)
839 continue;
840
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900841 if (removable != (blkio->media->removable_media != 0))
842 continue;
843
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900844 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700845 if (ret != EFI_SUCCESS)
846 continue;
847 ret = efi_protocol_open(handler, (void **)&device_path,
848 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
849 if (ret != EFI_SUCCESS)
850 continue;
851
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900852 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700853 if (ret != EFI_SUCCESS)
854 continue;
855
856 p = dev_name;
857 utf8_utf16_strncpy(&p, buf, strlen(buf));
858
Raymond Maob5542a62023-06-19 14:23:01 -0700859 /* prefer to short form device path */
860 short_dp = efi_dp_shorten(device_path);
861 if (short_dp)
862 device_path = short_dp;
863
Raymond Mao70a76c52023-06-19 14:22:58 -0700864 lo.label = dev_name;
865 lo.attributes = LOAD_OPTION_ACTIVE;
866 lo.file_path = device_path;
Simon Glassc88552c2025-05-24 11:28:23 -0600867 lo.file_path_length = efi_dp_size(device_path) +
868 sizeof(EFI_DP_END);
Raymond Mao70a76c52023-06-19 14:22:58 -0700869 /*
870 * Set the dedicated guid to optional_data, it is used to identify
871 * the boot option that automatically generated by the bootmenu.
872 * efi_serialize_load_option() expects optional_data is null-terminated
873 * utf8 string, so set the "1234567" string to allocate enough space
874 * to store guid, instead of realloc the load_option.
875 */
876 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900877 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
878 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700879 ret = EFI_OUT_OF_RESOURCES;
880 goto out;
881 }
882 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900883 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700884 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900885 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900886
887 if (num >= *count)
888 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700889 }
890
891out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900892 *count = num;
893
Raymond Mao70a76c52023-06-19 14:22:58 -0700894 return ret;
895}
896
897/**
898 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
899 *
900 * @opt: pointer to the media boot option structure
901 * @count: number of media boot option structure
902 * Return: status code
903 */
904static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
905 efi_status_t count)
906{
907 efi_uintn_t size;
908 void *load_option;
909 u32 i, list_size = 0;
910 struct efi_load_option lo;
911 u16 *var_name16 = NULL;
912 u16 varname[] = u"Boot####";
913 efi_status_t ret = EFI_SUCCESS;
914 u16 *delete_index_list = NULL, *p;
915 efi_uintn_t buf_size;
916
917 buf_size = 128;
918 var_name16 = malloc(buf_size);
919 if (!var_name16)
920 return EFI_OUT_OF_RESOURCES;
921
922 var_name16[0] = 0;
923 for (;;) {
924 int index;
925 efi_guid_t guid;
926 efi_uintn_t tmp;
927
928 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
929 if (ret == EFI_NOT_FOUND) {
930 /*
931 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
932 * This should be treated as success.
933 */
934 ret = EFI_SUCCESS;
935 break;
936 }
937
938 if (ret != EFI_SUCCESS)
939 goto out;
940
941 if (!efi_varname_is_load_option(var_name16, &index))
942 continue;
943
944 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
945 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
946 if (!load_option)
947 continue;
948
949 tmp = size;
950 ret = efi_deserialize_load_option(&lo, load_option, &size);
951 if (ret != EFI_SUCCESS)
952 goto next;
953
954 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
955 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
956 for (i = 0; i < count; i++) {
957 if (opt[i].size == tmp &&
958 memcmp(opt[i].lo, load_option, tmp) == 0) {
959 opt[i].exist = true;
960 break;
961 }
962 }
963
964 /*
965 * The entire list of variables must be retrieved by
966 * efi_get_next_variable_name_int() before deleting the invalid
967 * boot option, just save the index here.
968 */
969 if (i == count) {
970 p = realloc(delete_index_list, sizeof(u32) *
971 (list_size + 1));
972 if (!p) {
973 ret = EFI_OUT_OF_RESOURCES;
974 goto out;
975 }
976 delete_index_list = p;
977 delete_index_list[list_size++] = index;
978 }
979 }
980next:
981 free(load_option);
982 }
983
984 /* delete all invalid boot options */
985 for (i = 0; i < list_size; i++) {
986 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
987 if (ret != EFI_SUCCESS)
988 goto out;
989 }
990
991out:
992 free(var_name16);
993 free(delete_index_list);
994
995 return ret;
996}
997
998/**
999 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
1000 *
1001 * @buf: pointer to the buffer to store boot option variable name
1002 * @buf_size: buffer size
1003 * @index: pointer to store the index in the BootOrder variable
1004 * Return: status code
1005 */
1006efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1007 unsigned int *index)
1008{
1009 u32 i;
1010 efi_status_t ret;
1011 efi_uintn_t size;
1012
1013 if (buf_size < u16_strsize(u"Boot####"))
1014 return EFI_BUFFER_TOO_SMALL;
1015
1016 for (i = 0; i <= 0xFFFF; i++) {
1017 size = 0;
1018 efi_create_indexed_name(buf, buf_size, "Boot", i);
1019 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1020 NULL, &size, NULL, NULL);
1021 if (ret == EFI_BUFFER_TOO_SMALL)
1022 continue;
1023 else
1024 break;
1025 }
1026
1027 if (i > 0xFFFF)
1028 return EFI_OUT_OF_RESOURCES;
1029
1030 *index = i;
1031
1032 return EFI_SUCCESS;
1033}
1034
1035/**
1036 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1037 *
1038 * @index: "Boot####" index to append to BootOrder variable
1039 * Return: status code
1040 */
1041efi_status_t efi_bootmgr_append_bootorder(u16 index)
1042{
1043 u16 *bootorder;
1044 efi_status_t ret;
1045 u16 *new_bootorder = NULL;
1046 efi_uintn_t last, size, new_size;
1047
1048 /* append new boot option */
1049 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1050 last = size / sizeof(u16);
1051 new_size = size + sizeof(u16);
1052 new_bootorder = calloc(1, new_size);
1053 if (!new_bootorder) {
1054 ret = EFI_OUT_OF_RESOURCES;
1055 goto out;
1056 }
1057 memcpy(new_bootorder, bootorder, size);
1058 new_bootorder[last] = index;
1059
1060 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1061 EFI_VARIABLE_NON_VOLATILE |
1062 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1063 EFI_VARIABLE_RUNTIME_ACCESS,
1064 new_size, new_bootorder, false);
1065 if (ret != EFI_SUCCESS)
1066 goto out;
1067
1068out:
1069 free(bootorder);
1070 free(new_bootorder);
1071
1072 return ret;
1073}
1074
1075/**
1076 * efi_bootmgr_delete_boot_option() - delete selected boot option
1077 *
1078 * @boot_index: boot option index to delete
1079 * Return: status code
1080 */
1081efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1082{
1083 u16 *bootorder;
1084 u16 varname[9];
1085 efi_status_t ret;
1086 unsigned int index;
1087 efi_uintn_t num, size;
1088
1089 efi_create_indexed_name(varname, sizeof(varname),
1090 "Boot", boot_index);
1091 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1092 0, 0, NULL, false);
1093 if (ret != EFI_SUCCESS) {
1094 log_err("delete boot option(%ls) failed\n", varname);
1095 return ret;
1096 }
1097
1098 /* update BootOrder if necessary */
1099 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1100 if (!bootorder)
1101 return EFI_SUCCESS;
1102
1103 num = size / sizeof(u16);
1104 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1105 return EFI_SUCCESS;
1106
1107 memmove(&bootorder[index], &bootorder[index + 1],
1108 (num - index - 1) * sizeof(u16));
1109 size -= sizeof(u16);
1110 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1111 EFI_VARIABLE_NON_VOLATILE |
1112 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1113 EFI_VARIABLE_RUNTIME_ACCESS,
1114 size, bootorder, false);
1115
1116 return ret;
1117}
1118
1119/**
1120 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1121 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001122 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001123 * This function also provide the BOOT#### variable maintenance for
1124 * the media device entries.
1125 * - Automatically create the BOOT#### variable for the newly detected device,
1126 * this BOOT#### variable is distinguished by the special GUID
1127 * stored in the EFI_LOAD_OPTION.optional_data
1128 * - If the device is not attached to the system, the associated BOOT#### variable
1129 * is automatically deleted.
1130 *
1131 * Return: status code
1132 */
1133efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1134{
1135 u32 i;
1136 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001137 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001138 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001139 struct eficonfig_media_boot_option *opt = NULL;
1140
1141 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001142 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001143 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001144 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001145 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001146 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001147
1148 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001149 if (!opt) {
1150 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001151 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001152 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001153
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001154 /* parse removable block io followed by fixed block io */
1155 num = count;
1156 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001157 if (ret != EFI_SUCCESS)
1158 goto out;
1159
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001160 total = num;
1161 num = count;
1162 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1163 if (ret != EFI_SUCCESS)
1164 goto out;
1165
1166 total = num;
1167
Raymond Mao70a76c52023-06-19 14:22:58 -07001168 /*
1169 * System hardware configuration may vary depending on the user setup.
1170 * The boot option is automatically added by the bootmenu.
1171 * If the device is not attached to the system, the boot option needs
1172 * to be deleted.
1173 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001174 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001175 if (ret != EFI_SUCCESS)
1176 goto out;
1177
1178 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001179 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001180 u32 boot_index;
1181 u16 var_name[9];
1182
1183 if (!opt[i].exist) {
1184 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1185 &boot_index);
1186 if (ret != EFI_SUCCESS)
1187 goto out;
1188
1189 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1190 EFI_VARIABLE_NON_VOLATILE |
1191 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1192 EFI_VARIABLE_RUNTIME_ACCESS,
1193 opt[i].size, opt[i].lo, false);
1194 if (ret != EFI_SUCCESS)
1195 goto out;
1196
1197 ret = efi_bootmgr_append_bootorder(boot_index);
1198 if (ret != EFI_SUCCESS) {
1199 efi_set_variable_int(var_name, &efi_global_variable_guid,
1200 0, 0, NULL, false);
1201 goto out;
1202 }
1203 }
1204 }
1205
1206out:
1207 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001208 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001209 free(opt[i].lo);
1210 }
1211 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001212 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001213
Raymond Maoa35784d2023-06-19 14:22:59 -07001214 if (ret == EFI_NOT_FOUND)
1215 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001216 return ret;
1217}
1218
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001219/**
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001220 * load_fdt_from_load_option - load device-tree from load option
1221 *
1222 * @fdt: pointer to loaded device-tree or NULL
1223 * Return: status code
1224 */
1225static efi_status_t load_fdt_from_load_option(void **fdt)
1226{
1227 struct efi_device_path *dp = NULL;
1228 struct efi_file_handle *f = NULL;
1229 efi_uintn_t filesize;
1230 efi_status_t ret;
1231
1232 *fdt = NULL;
1233
1234 dp = efi_get_dp_from_boot(&efi_guid_fdt);
1235 if (!dp)
1236 return EFI_SUCCESS;
1237
1238 /* Open file */
1239 f = efi_file_from_path(dp);
1240 if (!f) {
1241 log_err("Can't find %pD specified in Boot####\n", dp);
1242 ret = EFI_NOT_FOUND;
1243 goto out;
1244 }
1245
1246 /* Get file size */
1247 ret = efi_file_size(f, &filesize);
1248 if (ret != EFI_SUCCESS)
1249 goto out;
1250
1251 *fdt = calloc(1, filesize);
1252 if (!*fdt) {
1253 log_err("Out of memory\n");
1254 ret = EFI_OUT_OF_RESOURCES;
1255 goto out;
1256 }
1257 ret = EFI_CALL(f->read(f, &filesize, *fdt));
1258 if (ret != EFI_SUCCESS) {
1259 log_err("Can't read fdt\n");
1260 free(*fdt);
1261 *fdt = NULL;
1262 }
1263
1264out:
1265 efi_free_pool(dp);
1266 if (f)
1267 EFI_CALL(f->close(f));
1268
1269 return ret;
1270}
1271
1272/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001273 * efi_bootmgr_run() - execute EFI boot manager
1274 * @fdt: Flat device tree
1275 *
1276 * Invoke EFI boot manager and execute a binary depending on
1277 * boot options. If @fdt is not NULL, it will be passed to
1278 * the executed binary.
1279 *
1280 * Return: status code
1281 */
1282efi_status_t efi_bootmgr_run(void *fdt)
1283{
1284 efi_handle_t handle;
1285 void *load_options;
1286 efi_status_t ret;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001287 void *fdt_lo, *fdt_distro = NULL;
1288 efi_uintn_t fdt_size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001289
1290 /* Initialize EFI drivers */
1291 ret = efi_init_obj_list();
1292 if (ret != EFI_SUCCESS) {
1293 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1294 ret & ~EFI_ERROR_MASK);
1295 return CMD_RET_FAILURE;
1296 }
1297
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001298 ret = efi_bootmgr_load(&handle, &load_options);
1299 if (ret != EFI_SUCCESS) {
1300 log_notice("EFI boot manager: Cannot load any image\n");
1301 return ret;
1302 }
1303
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001304 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
1305 ret = load_fdt_from_load_option(&fdt_lo);
1306 if (ret != EFI_SUCCESS)
1307 return ret;
1308 if (fdt_lo)
1309 fdt = fdt_lo;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001310 if (!fdt) {
Heinrich Schuchardt8257f162024-07-13 13:30:29 +02001311 efi_load_distro_fdt(handle, &fdt_distro, &fdt_size);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001312 fdt = fdt_distro;
1313 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001314 }
1315
1316 /*
1317 * Needed in ACPI case to create reservations based on
1318 * control device-tree.
1319 */
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001320 ret = efi_install_fdt(fdt);
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001321
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001322 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001323 free(fdt_lo);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001324 if (fdt_distro)
1325 efi_free_pages((uintptr_t)fdt_distro,
1326 efi_size_in_pages(fdt_size));
1327 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001328
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001329 if (ret != EFI_SUCCESS) {
1330 if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
1331 free(load_options);
1332 else
1333 log_err("Unloading image failed\n");
1334
1335 return ret;
1336 }
1337
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001338 return do_bootefi_exec(handle, load_options);
1339}