blob: 1a3461f5a9d03cbda1a18ce3123e3e1e1e526809 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040017#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090018#include <net.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040019#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020020#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090021#include <asm/unaligned.h>
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +053022#include <linux/kernel.h>
23#include <linux/sizes.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040024
25static const struct efi_boot_services *bs;
26static const struct efi_runtime_services *rs;
27
Masahisa Kojima949c4412023-11-10 13:25:40 +090028/**
29 * struct uridp_context - uri device path resource
30 *
31 * @image_size: image size
32 * @image_addr: image address
33 * @loaded_dp: pointer to loaded device path
34 * @ramdisk_blk_dev: pointer to the ramdisk blk device
35 * @mem_handle: efi_handle to the loaded PE-COFF image
36 */
37struct uridp_context {
38 ulong image_size;
39 ulong image_addr;
40 struct efi_device_path *loaded_dp;
41 struct udevice *ramdisk_blk_dev;
42 efi_handle_t mem_handle;
43};
44
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090045const efi_guid_t efi_guid_bootmenu_auto_generated =
46 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
47
Rob Clarkc84c1102017-09-13 18:05:38 -040048/*
49 * bootmgr implements the logic of trying to find a payload to boot
50 * based on the BootOrder + BootXXXX variables, and then loading it.
51 *
52 * TODO detecting a special key held (f9?) and displaying a boot menu
53 * like you would get on a PC would be clever.
54 *
55 * TODO if we had a way to write and persist variables after the OS
56 * has started, we'd also want to check OsIndications to see if we
57 * should do normal or recovery boot.
58 */
59
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020060/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090061 * expand_media_path() - expand a device path for default file name
62 * @device_path: device path to check against
63 *
64 * If @device_path is a media or disk partition which houses a file
65 * system, this function returns a full device path which contains
66 * an architecture-specific default file name for removable media.
67 *
68 * Return: a newly allocated device path
69 */
70static
71struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
72{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020073 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090074 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090075
76 if (!device_path)
77 return NULL;
78
79 /*
80 * If device_path is a (removable) media or partition which provides
81 * simple file system protocol, append a default file name to support
82 * booting from removable media.
83 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020084 handle = efi_dp_find_obj(device_path,
85 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000086 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000087 if (rem->type == DEVICE_PATH_TYPE_END) {
Simon Glass3b1e60b2024-11-07 14:31:43 -070088 char fname[30];
89
90 snprintf(fname, sizeof(fname), "/EFI/BOOT/%s",
91 efi_get_basename());
92 full_path = efi_dp_from_file(device_path, fname);
93
AKASHI Takahirofd972f52022-04-28 17:09:39 +090094 } else {
95 full_path = efi_dp_dup(device_path);
96 }
97 } else {
98 full_path = efi_dp_dup(device_path);
99 }
100
101 return full_path;
102}
103
104/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900105 * try_load_from_file_path() - try to load a file
106 *
107 * Given a file media path iterate through a list of handles and try to
108 * to load the file from each of them until the first success.
109 *
110 * @fs_handles: array of handles with the simple file protocol
111 * @num: number of handles in fs_handles
112 * @fp: file path to open
113 * @handle: on return pointer to handle for loaded image
114 * @removable: if true only consider removable media, else only non-removable
115 */
116static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
117 efi_uintn_t num,
118 struct efi_device_path *fp,
119 efi_handle_t *handle,
120 bool removable)
121{
122 struct efi_handler *handler;
123 struct efi_device_path *dp;
124 int i;
125 efi_status_t ret;
126
127 for (i = 0; i < num; i++) {
128 if (removable != efi_disk_is_removable(fs_handles[i]))
129 continue;
130
131 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
132 &handler);
133 if (ret != EFI_SUCCESS)
134 continue;
135
136 dp = handler->protocol_interface;
137 if (!dp)
138 continue;
139
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200140 dp = efi_dp_concat(dp, fp, 0);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900141 if (!dp)
142 continue;
143
144 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
145 handle));
146 efi_free_pool(dp);
147 if (ret == EFI_SUCCESS)
148 return ret;
149 }
150
151 return EFI_NOT_FOUND;
152}
153
154/**
155 * try_load_from_short_path
156 * @fp: file path
157 * @handle: pointer to handle for newly installed image
158 *
159 * Enumerate all the devices which support file system operations,
160 * prepend its media device path to the file path, @fp, and
161 * try to load the file.
162 * This function should be called when handling a short-form path
163 * which is starting with a file device path.
164 *
165 * Return: status code
166 */
167static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
168 efi_handle_t *handle)
169{
170 efi_handle_t *fs_handles;
171 efi_uintn_t num;
172 efi_status_t ret;
173
174 ret = EFI_CALL(efi_locate_handle_buffer(
175 BY_PROTOCOL,
176 &efi_simple_file_system_protocol_guid,
177 NULL,
178 &num, &fs_handles));
179 if (ret != EFI_SUCCESS)
180 return ret;
181 if (!num)
182 return EFI_NOT_FOUND;
183
184 /* removable media first */
185 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
186 if (ret == EFI_SUCCESS)
187 goto out;
188
189 /* fixed media */
190 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
191 if (ret == EFI_SUCCESS)
192 goto out;
193
194out:
195 return ret;
196}
197
198/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900199 * mount_image() - mount the image with blkmap
200 *
201 * @lo_label: u16 label string of load option
202 * @addr: image address
203 * @size: image size
204 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
205 */
206static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
207{
208 int err;
209 struct blkmap *bm;
210 struct udevice *bm_dev;
211 char *label = NULL, *p;
212
213 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
214 if (!label)
215 return NULL;
216
217 p = label;
218 utf16_utf8_strcpy(&p, lo_label);
219 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
220 if (err) {
221 efi_free_pool(label);
222 return NULL;
223 }
224 bm = dev_get_plat(bm_dev);
225
226 efi_free_pool(label);
227
228 return bm->blk;
229}
230
231/**
232 * search_default_file() - search default file
233 *
234 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
235 * @loaded_dp: pointer to default file device path
236 * Return: status code
237 */
238static efi_status_t search_default_file(struct udevice *dev,
239 struct efi_device_path **loaded_dp)
240{
241 efi_status_t ret;
242 efi_handle_t handle;
243 u16 *default_file_name = NULL;
244 struct efi_file_handle *root, *f;
245 struct efi_device_path *dp = NULL, *fp = NULL;
246 struct efi_simple_file_system_protocol *file_system;
247 struct efi_device_path *device_path, *full_path = NULL;
248
249 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
250 log_warning("DM_TAG_EFI not found\n");
251 return EFI_INVALID_PARAMETER;
252 }
253
254 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
255 (void **)&device_path, efi_root, NULL,
256 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
257 if (ret != EFI_SUCCESS)
258 return ret;
259
260 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
261 (void **)&file_system, efi_root, NULL,
262 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
263 if (ret != EFI_SUCCESS)
264 return ret;
265
266 ret = EFI_CALL(file_system->open_volume(file_system, &root));
267 if (ret != EFI_SUCCESS)
268 return ret;
269
270 full_path = expand_media_path(device_path);
271 ret = efi_dp_split_file_path(full_path, &dp, &fp);
272 if (ret != EFI_SUCCESS)
273 goto err;
274
275 default_file_name = efi_dp_str(fp);
276 efi_free_pool(dp);
277 efi_free_pool(fp);
278 if (!default_file_name) {
279 ret = EFI_OUT_OF_RESOURCES;
280 goto err;
281 }
282
283 ret = EFI_CALL(root->open(root, &f, default_file_name,
284 EFI_FILE_MODE_READ, 0));
285 efi_free_pool(default_file_name);
286 if (ret != EFI_SUCCESS)
287 goto err;
288
289 EFI_CALL(f->close(f));
290 EFI_CALL(root->close(root));
291
292 *loaded_dp = full_path;
293
294 return EFI_SUCCESS;
295
296err:
297 EFI_CALL(root->close(root));
298 efi_free_pool(full_path);
299
300 return ret;
301}
302
303/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900304 * fill_default_file_path() - get fallback boot device path for block device
305 *
306 * Provide the device path to the fallback UEFI boot file, e.g.
307 * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900308 *
309 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900310 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900311 * Return: status code
312 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900313static efi_status_t fill_default_file_path(struct udevice *blk,
314 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900315{
316 efi_status_t ret;
317 struct udevice *partition;
318
319 /* image that has no partition table but a file system */
320 ret = search_default_file(blk, dp);
321 if (ret == EFI_SUCCESS)
322 return ret;
323
324 /* try the partitions */
325 device_foreach_child(partition, blk) {
326 enum uclass_id id;
327
328 id = device_get_uclass_id(partition);
329 if (id != UCLASS_PARTITION)
330 continue;
331
332 ret = search_default_file(partition, dp);
333 if (ret == EFI_SUCCESS)
334 return ret;
335 }
336
337 return EFI_NOT_FOUND;
338}
339
340/**
341 * prepare_loaded_image() - prepare ramdisk for downloaded image
342 *
343 * @label: label of load option
344 * @addr: image address
345 * @size: image size
346 * @dp: pointer to default file device path
347 * @blk: pointer to created blk udevice
348 * Return: status code
349 */
350static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
351 struct efi_device_path **dp,
352 struct udevice **blk)
353{
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530354 u64 pages;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900355 efi_status_t ret;
356 struct udevice *ramdisk_blk;
357
358 ramdisk_blk = mount_image(label, addr, size);
359 if (!ramdisk_blk)
360 return EFI_LOAD_ERROR;
361
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900362 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900363 if (ret != EFI_SUCCESS) {
364 log_info("Cannot boot from downloaded image\n");
365 goto err;
366 }
367
368 /*
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530369 * Linux supports 'pmem' which allows OS installers to find, reclaim
370 * the mounted images and continue the installation since the contents
371 * of the pmem region are treated as local media.
372 *
373 * The memory regions used for it needs to be carved out of the EFI
374 * memory map.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900375 */
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530376 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
377 ret = efi_update_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY,
378 false, true);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900379 if (ret != EFI_SUCCESS) {
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530380 log_err("Failed to reserve memory\n");
Masahisa Kojima949c4412023-11-10 13:25:40 +0900381 goto err;
382 }
383
384 *blk = ramdisk_blk;
385
386 return EFI_SUCCESS;
387
388err:
389 if (blkmap_destroy(ramdisk_blk->parent))
390 log_err("Destroying blkmap failed\n");
391
392 return ret;
393}
394
395/**
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300396 * efi_bootmgr_release_uridp() - cleanup uri device path resource
Masahisa Kojima949c4412023-11-10 13:25:40 +0900397 *
398 * @ctx: event context
399 * Return: status code
400 */
Ilias Apalodimas55c05712024-10-26 10:37:32 +0300401static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900402{
403 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300404 efi_status_t ret2 = EFI_SUCCESS;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900405
406 if (!ctx)
407 return ret;
408
409 /* cleanup for iso or img image */
410 if (ctx->ramdisk_blk_dev) {
411 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
412 EFI_CONVENTIONAL_MEMORY);
413 if (ret != EFI_SUCCESS)
414 log_err("Reclaiming memory failed\n");
415
416 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
417 log_err("Destroying blkmap failed\n");
418 ret = EFI_DEVICE_ERROR;
419 }
420 }
421
422 /* cleanup for PE-COFF image */
423 if (ctx->mem_handle) {
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300424 ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle,
425 &efi_guid_device_path,
426 ctx->loaded_dp,
427 NULL);
428 if (ret2 != EFI_SUCCESS)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900429 log_err("Uninstall device_path protocol failed\n");
430 }
431
432 efi_free_pool(ctx->loaded_dp);
433 free(ctx);
434
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300435 return ret == EFI_SUCCESS ? ret2 : ret;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900436}
437
438/**
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300439 * efi_bootmgr_http_return() - return to efibootmgr callback
Masahisa Kojima949c4412023-11-10 13:25:40 +0900440 *
441 * @event: the event for which this notification function is registered
442 * @context: event context
443 */
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300444static void EFIAPI efi_bootmgr_http_return(struct efi_event *event,
445 void *context)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900446{
447 efi_status_t ret;
448
449 EFI_ENTRY("%p, %p", event, context);
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300450 ret = efi_bootmgr_release_uridp(context);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900451 EFI_EXIT(ret);
452}
453
454/**
455 * try_load_from_uri_path() - Handle the URI device path
456 *
457 * @uridp: uri device path
458 * @lo_label: label of load option
459 * @handle: pointer to handle for newly installed image
460 * Return: status code
461 */
462static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
463 u16 *lo_label,
464 efi_handle_t *handle)
465{
466 char *s;
467 int err;
468 int uri_len;
469 efi_status_t ret;
470 void *source_buffer;
471 efi_uintn_t source_size;
472 struct uridp_context *ctx;
473 struct udevice *blk = NULL;
474 struct efi_event *event = NULL;
475 efi_handle_t mem_handle = NULL;
476 struct efi_device_path *loaded_dp;
477 static ulong image_size, image_addr;
478
479 ctx = calloc(1, sizeof(struct uridp_context));
480 if (!ctx)
481 return EFI_OUT_OF_RESOURCES;
482
Ilias Apalodimasabddf282025-05-23 16:04:04 +0300483 s = env_get("ipaddr");
484 if (!s && dhcp_run(0, NULL, false)) {
485 log_err("Error: Can't find a valid IP address\n");
486 ret = EFI_DEVICE_ERROR;
487 goto err;
488 }
489
Masahisa Kojima949c4412023-11-10 13:25:40 +0900490 s = env_get("loadaddr");
491 if (!s) {
492 log_err("Error: loadaddr is not set\n");
493 ret = EFI_INVALID_PARAMETER;
494 goto err;
495 }
496
497 image_addr = hextoul(s, NULL);
Adriano Cordovab479fc42024-12-04 00:05:16 -0300498 err = wget_do_request(image_addr, uridp->uri);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900499 if (err < 0) {
500 ret = EFI_INVALID_PARAMETER;
501 goto err;
502 }
503
504 image_size = env_get_hex("filesize", 0);
505 if (!image_size) {
506 ret = EFI_INVALID_PARAMETER;
507 goto err;
508 }
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530509 /*
510 * Depending on the kernel configuration, pmem memory areas must be
511 * page aligned or 2MiB aligned. PowerPC is an exception here and
512 * requires 16MiB alignment, but since we don't have EFI support for
513 * it, limit the alignment to 2MiB.
514 */
515 image_size = ALIGN(image_size, SZ_2M);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900516
517 /*
518 * If the file extension is ".iso" or ".img", mount it and try to load
519 * the default file.
520 * If the file is PE-COFF image, load the downloaded file.
521 */
522 uri_len = strlen(uridp->uri);
523 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
524 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
525 ret = prepare_loaded_image(lo_label, image_addr, image_size,
526 &loaded_dp, &blk);
527 if (ret != EFI_SUCCESS)
528 goto err;
529
530 source_buffer = NULL;
531 source_size = 0;
532 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
533 /*
534 * loaded_dp must exist until efi application returns,
535 * will be freed in return_to_efibootmgr event callback.
536 */
537 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Simon Glass37972f42025-05-24 11:28:21 -0600538 image_addr, image_size);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900539 ret = efi_install_multiple_protocol_interfaces(
540 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
541 if (ret != EFI_SUCCESS)
542 goto err;
543
544 source_buffer = (void *)image_addr;
545 source_size = image_size;
546 } else {
547 log_err("Error: file type is not supported\n");
548 ret = EFI_UNSUPPORTED;
549 goto err;
550 }
551
552 ctx->image_size = image_size;
553 ctx->image_addr = image_addr;
554 ctx->loaded_dp = loaded_dp;
555 ctx->ramdisk_blk_dev = blk;
556 ctx->mem_handle = mem_handle;
557
558 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
559 source_size, handle));
560 if (ret != EFI_SUCCESS)
561 goto err;
562
563 /* create event for cleanup when the image returns or error occurs */
564 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300565 efi_bootmgr_http_return, ctx,
Masahisa Kojima949c4412023-11-10 13:25:40 +0900566 &efi_guid_event_group_return_to_efibootmgr,
567 &event);
568 if (ret != EFI_SUCCESS) {
569 log_err("Creating event failed\n");
570 goto err;
571 }
572
573 return ret;
574
575err:
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300576 efi_bootmgr_release_uridp(ctx);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900577
578 return ret;
579}
580
581/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900582 * try_load_from_media() - load file from media
583 *
584 * @file_path: file path
585 * @handle_img: on return handle for the newly installed image
586 *
587 * If @file_path contains a file name, load the file.
588 * If @file_path does not have a file name, search the architecture-specific
589 * fallback boot file and load it.
590 * TODO: If the FilePathList[0] device does not support
591 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
592 * call EFI_BOOT_SERVICES.ConnectController()
593 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
594 * not based on EFI_BLOCK_IO_PROTOCOL
595 *
596 * Return: status code
597 */
598static efi_status_t try_load_from_media(struct efi_device_path *file_path,
599 efi_handle_t *handle_img)
600{
601 efi_handle_t handle_blkdev;
602 efi_status_t ret = EFI_SUCCESS;
603 struct efi_device_path *rem, *dp = NULL;
604 struct efi_device_path *final_dp = file_path;
605
606 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
607 if (handle_blkdev) {
608 if (rem->type == DEVICE_PATH_TYPE_END) {
609 /* no file name present, try default file */
610 ret = fill_default_file_path(handle_blkdev->dev, &dp);
611 if (ret != EFI_SUCCESS)
612 return ret;
613
614 final_dp = dp;
615 }
616 }
617
618 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
619
620 efi_free_pool(dp);
621
622 return ret;
623}
624
625/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200626 * try_load_entry() - try to load image for boot option
627 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400628 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200629 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400630 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200631 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200632 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
633 * @handle: on return handle for the newly installed image
634 * @load_options: load options set on the loaded image protocol
635 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400636 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200637static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
638 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400639{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900640 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200641 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900642 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200643 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900644 efi_status_t ret;
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200645 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400646
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200647 *handle = NULL;
648 *load_options = NULL;
Rob Clarkc84c1102017-09-13 18:05:38 -0400649
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200650 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200651 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400652 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900653 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400654
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200655 ret = efi_deserialize_load_option(&lo, load_option, &size);
656 if (ret != EFI_SUCCESS) {
657 log_warning("Invalid load option for %ls\n", varname);
658 goto error;
659 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400660
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200661 if (!(lo.attributes & LOAD_OPTION_ACTIVE)) {
662 ret = EFI_LOAD_ERROR;
663 goto error;
664 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400665
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200666 log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400667
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200668 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
669 /* file_path doesn't contain a device path */
670 ret = try_load_from_short_path(lo.file_path, handle);
671 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
672 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
673 ret = try_load_from_uri_path(
674 (struct efi_device_path_uri *)lo.file_path,
675 lo.label, handle);
676 else
677 ret = EFI_LOAD_ERROR;
678 } else {
679 ret = try_load_from_media(lo.file_path, handle);
680 }
681 if (ret != EFI_SUCCESS) {
682 log_warning("Loading %ls '%ls' failed\n",
683 varname, lo.label);
684 goto error;
685 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400686
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200687 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
688 EFI_VARIABLE_RUNTIME_ACCESS;
689 ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid,
690 attributes, sizeof(n), &n, false);
691 if (ret != EFI_SUCCESS)
692 goto error;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900693
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200694 /* try to register load file2 for initrd's */
695 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
Adriano Cordova02d049d2025-03-19 11:44:59 -0300696 ret = efi_initrd_register(NULL);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200697 if (ret != EFI_SUCCESS)
698 goto error;
Rob Clarkc84c1102017-09-13 18:05:38 -0400699 }
700
Ilias Apalodimas2f304322025-03-28 14:58:18 +0200701 log_info("Booting: Label: %ls Device path: %pD\n", lo.label, lo.file_path);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200702
703 /* Ignore the optional data in auto-generated boot options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900704 if (size >= sizeof(efi_guid_t) &&
705 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
706 size = 0;
707
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200708 /* Set optional data in loaded file protocol */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200709 if (size) {
710 *load_options = malloc(size);
711 if (!*load_options) {
712 ret = EFI_OUT_OF_RESOURCES;
713 goto error;
714 }
715 memcpy(*load_options, lo.optional_data, size);
716 ret = efi_set_load_options(*handle, size, *load_options);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200717 if (ret != EFI_SUCCESS)
718 free(load_options);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200719 }
720
Rob Clarkc84c1102017-09-13 18:05:38 -0400721error:
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200722 if (ret != EFI_SUCCESS && *handle &&
723 EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200724 log_err("Unloading image failed\n");
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200725
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200726 free(load_option);
727
728 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400729}
730
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200731/**
732 * efi_bootmgr_load() - try to load from BootNext or BootOrder
733 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900734 * Attempt to load from BootNext or in the order specified by BootOrder
735 * EFI variable, the available load-options, finding and returning
736 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200737 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200738 * @handle: on return handle for the newly installed image
739 * @load_options: load options set on the loaded image protocol
740 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400741 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200742efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400743{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900744 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200745 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400746 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900747 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400748
Rob Clarkc84c1102017-09-13 18:05:38 -0400749 bs = systab.boottime;
750 rs = systab.runtime;
751
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900752 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900753 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700754 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200755 &efi_global_variable_guid,
756 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900757 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
758 /* BootNext does exist here */
759 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200760 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900761
762 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700763 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200764 &efi_global_variable_guid,
765 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900766
767 /* load BootNext */
768 if (ret == EFI_SUCCESS) {
769 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200770 ret = try_load_entry(bootnext, handle,
771 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900772 if (ret == EFI_SUCCESS)
773 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200774 log_warning(
775 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900776 }
777 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200778 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900779 }
780 }
781
782 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700783 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100784 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200785 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900786 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400787 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100788 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400789
790 num = size / sizeof(uint16_t);
791 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200792 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200793 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900794 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400795 break;
796 }
797
798 free(bootorder);
799
800error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900801 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400802}
Raymond Mao70a76c52023-06-19 14:22:58 -0700803
804/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900805 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700806 *
807 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900808 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900809 * @handles: pointer to block device handles
810 * @count: On entry number of handles to block devices.
811 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900812 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700813 * Return: status code
814 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900815static efi_status_t
816efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900817 efi_uintn_t index, efi_handle_t *handles,
818 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700819{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900820 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700821 struct efi_handler *handler;
822 efi_status_t ret = EFI_SUCCESS;
823
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900824 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700825 u16 *p;
826 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
827 char *optional_data;
828 struct efi_load_option lo;
829 char buf[BOOTMENU_DEVICE_NAME_MAX];
830 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700831 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900832 struct efi_block_io *blkio;
833
834 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
835 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700836
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900837 if (blkio->media->logical_partition)
838 continue;
839
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900840 if (removable != (blkio->media->removable_media != 0))
841 continue;
842
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900843 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700844 if (ret != EFI_SUCCESS)
845 continue;
846 ret = efi_protocol_open(handler, (void **)&device_path,
847 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
848 if (ret != EFI_SUCCESS)
849 continue;
850
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900851 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700852 if (ret != EFI_SUCCESS)
853 continue;
854
855 p = dev_name;
856 utf8_utf16_strncpy(&p, buf, strlen(buf));
857
Raymond Maob5542a62023-06-19 14:23:01 -0700858 /* prefer to short form device path */
859 short_dp = efi_dp_shorten(device_path);
860 if (short_dp)
861 device_path = short_dp;
862
Raymond Mao70a76c52023-06-19 14:22:58 -0700863 lo.label = dev_name;
864 lo.attributes = LOAD_OPTION_ACTIVE;
865 lo.file_path = device_path;
Simon Glassc88552c2025-05-24 11:28:23 -0600866 lo.file_path_length = efi_dp_size(device_path) +
867 sizeof(EFI_DP_END);
Raymond Mao70a76c52023-06-19 14:22:58 -0700868 /*
869 * Set the dedicated guid to optional_data, it is used to identify
870 * the boot option that automatically generated by the bootmenu.
871 * efi_serialize_load_option() expects optional_data is null-terminated
872 * utf8 string, so set the "1234567" string to allocate enough space
873 * to store guid, instead of realloc the load_option.
874 */
875 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900876 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
877 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700878 ret = EFI_OUT_OF_RESOURCES;
879 goto out;
880 }
881 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900882 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700883 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900884 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900885
886 if (num >= *count)
887 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700888 }
889
890out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900891 *count = num;
892
Raymond Mao70a76c52023-06-19 14:22:58 -0700893 return ret;
894}
895
896/**
897 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
898 *
899 * @opt: pointer to the media boot option structure
900 * @count: number of media boot option structure
901 * Return: status code
902 */
903static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
904 efi_status_t count)
905{
906 efi_uintn_t size;
907 void *load_option;
908 u32 i, list_size = 0;
909 struct efi_load_option lo;
910 u16 *var_name16 = NULL;
911 u16 varname[] = u"Boot####";
912 efi_status_t ret = EFI_SUCCESS;
913 u16 *delete_index_list = NULL, *p;
914 efi_uintn_t buf_size;
915
916 buf_size = 128;
917 var_name16 = malloc(buf_size);
918 if (!var_name16)
919 return EFI_OUT_OF_RESOURCES;
920
921 var_name16[0] = 0;
922 for (;;) {
923 int index;
924 efi_guid_t guid;
925 efi_uintn_t tmp;
926
927 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
928 if (ret == EFI_NOT_FOUND) {
929 /*
930 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
931 * This should be treated as success.
932 */
933 ret = EFI_SUCCESS;
934 break;
935 }
936
937 if (ret != EFI_SUCCESS)
938 goto out;
939
940 if (!efi_varname_is_load_option(var_name16, &index))
941 continue;
942
943 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
944 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
945 if (!load_option)
946 continue;
947
948 tmp = size;
949 ret = efi_deserialize_load_option(&lo, load_option, &size);
950 if (ret != EFI_SUCCESS)
951 goto next;
952
953 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
954 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
955 for (i = 0; i < count; i++) {
956 if (opt[i].size == tmp &&
957 memcmp(opt[i].lo, load_option, tmp) == 0) {
958 opt[i].exist = true;
959 break;
960 }
961 }
962
963 /*
964 * The entire list of variables must be retrieved by
965 * efi_get_next_variable_name_int() before deleting the invalid
966 * boot option, just save the index here.
967 */
968 if (i == count) {
969 p = realloc(delete_index_list, sizeof(u32) *
970 (list_size + 1));
971 if (!p) {
972 ret = EFI_OUT_OF_RESOURCES;
973 goto out;
974 }
975 delete_index_list = p;
976 delete_index_list[list_size++] = index;
977 }
978 }
979next:
980 free(load_option);
981 }
982
983 /* delete all invalid boot options */
984 for (i = 0; i < list_size; i++) {
985 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
986 if (ret != EFI_SUCCESS)
987 goto out;
988 }
989
990out:
991 free(var_name16);
992 free(delete_index_list);
993
994 return ret;
995}
996
997/**
998 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
999 *
1000 * @buf: pointer to the buffer to store boot option variable name
1001 * @buf_size: buffer size
1002 * @index: pointer to store the index in the BootOrder variable
1003 * Return: status code
1004 */
1005efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1006 unsigned int *index)
1007{
1008 u32 i;
1009 efi_status_t ret;
1010 efi_uintn_t size;
1011
1012 if (buf_size < u16_strsize(u"Boot####"))
1013 return EFI_BUFFER_TOO_SMALL;
1014
1015 for (i = 0; i <= 0xFFFF; i++) {
1016 size = 0;
1017 efi_create_indexed_name(buf, buf_size, "Boot", i);
1018 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1019 NULL, &size, NULL, NULL);
1020 if (ret == EFI_BUFFER_TOO_SMALL)
1021 continue;
1022 else
1023 break;
1024 }
1025
1026 if (i > 0xFFFF)
1027 return EFI_OUT_OF_RESOURCES;
1028
1029 *index = i;
1030
1031 return EFI_SUCCESS;
1032}
1033
1034/**
1035 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1036 *
1037 * @index: "Boot####" index to append to BootOrder variable
1038 * Return: status code
1039 */
1040efi_status_t efi_bootmgr_append_bootorder(u16 index)
1041{
1042 u16 *bootorder;
1043 efi_status_t ret;
1044 u16 *new_bootorder = NULL;
1045 efi_uintn_t last, size, new_size;
1046
1047 /* append new boot option */
1048 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1049 last = size / sizeof(u16);
1050 new_size = size + sizeof(u16);
1051 new_bootorder = calloc(1, new_size);
1052 if (!new_bootorder) {
1053 ret = EFI_OUT_OF_RESOURCES;
1054 goto out;
1055 }
1056 memcpy(new_bootorder, bootorder, size);
1057 new_bootorder[last] = index;
1058
1059 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1060 EFI_VARIABLE_NON_VOLATILE |
1061 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1062 EFI_VARIABLE_RUNTIME_ACCESS,
1063 new_size, new_bootorder, false);
1064 if (ret != EFI_SUCCESS)
1065 goto out;
1066
1067out:
1068 free(bootorder);
1069 free(new_bootorder);
1070
1071 return ret;
1072}
1073
1074/**
1075 * efi_bootmgr_delete_boot_option() - delete selected boot option
1076 *
1077 * @boot_index: boot option index to delete
1078 * Return: status code
1079 */
1080efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1081{
1082 u16 *bootorder;
1083 u16 varname[9];
1084 efi_status_t ret;
1085 unsigned int index;
1086 efi_uintn_t num, size;
1087
1088 efi_create_indexed_name(varname, sizeof(varname),
1089 "Boot", boot_index);
1090 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1091 0, 0, NULL, false);
1092 if (ret != EFI_SUCCESS) {
1093 log_err("delete boot option(%ls) failed\n", varname);
1094 return ret;
1095 }
1096
1097 /* update BootOrder if necessary */
1098 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1099 if (!bootorder)
1100 return EFI_SUCCESS;
1101
1102 num = size / sizeof(u16);
1103 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1104 return EFI_SUCCESS;
1105
1106 memmove(&bootorder[index], &bootorder[index + 1],
1107 (num - index - 1) * sizeof(u16));
1108 size -= sizeof(u16);
1109 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1110 EFI_VARIABLE_NON_VOLATILE |
1111 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1112 EFI_VARIABLE_RUNTIME_ACCESS,
1113 size, bootorder, false);
1114
1115 return ret;
1116}
1117
1118/**
1119 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1120 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001121 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001122 * This function also provide the BOOT#### variable maintenance for
1123 * the media device entries.
1124 * - Automatically create the BOOT#### variable for the newly detected device,
1125 * this BOOT#### variable is distinguished by the special GUID
1126 * stored in the EFI_LOAD_OPTION.optional_data
1127 * - If the device is not attached to the system, the associated BOOT#### variable
1128 * is automatically deleted.
1129 *
1130 * Return: status code
1131 */
1132efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1133{
1134 u32 i;
1135 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001136 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001137 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001138 struct eficonfig_media_boot_option *opt = NULL;
1139
1140 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001141 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001142 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001143 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001144 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001145 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001146
1147 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001148 if (!opt) {
1149 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001150 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001151 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001152
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001153 /* parse removable block io followed by fixed block io */
1154 num = count;
1155 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001156 if (ret != EFI_SUCCESS)
1157 goto out;
1158
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001159 total = num;
1160 num = count;
1161 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1162 if (ret != EFI_SUCCESS)
1163 goto out;
1164
1165 total = num;
1166
Raymond Mao70a76c52023-06-19 14:22:58 -07001167 /*
1168 * System hardware configuration may vary depending on the user setup.
1169 * The boot option is automatically added by the bootmenu.
1170 * If the device is not attached to the system, the boot option needs
1171 * to be deleted.
1172 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001173 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001174 if (ret != EFI_SUCCESS)
1175 goto out;
1176
1177 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001178 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001179 u32 boot_index;
1180 u16 var_name[9];
1181
1182 if (!opt[i].exist) {
1183 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1184 &boot_index);
1185 if (ret != EFI_SUCCESS)
1186 goto out;
1187
1188 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1189 EFI_VARIABLE_NON_VOLATILE |
1190 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1191 EFI_VARIABLE_RUNTIME_ACCESS,
1192 opt[i].size, opt[i].lo, false);
1193 if (ret != EFI_SUCCESS)
1194 goto out;
1195
1196 ret = efi_bootmgr_append_bootorder(boot_index);
1197 if (ret != EFI_SUCCESS) {
1198 efi_set_variable_int(var_name, &efi_global_variable_guid,
1199 0, 0, NULL, false);
1200 goto out;
1201 }
1202 }
1203 }
1204
1205out:
1206 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001207 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001208 free(opt[i].lo);
1209 }
1210 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001211 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001212
Raymond Maoa35784d2023-06-19 14:22:59 -07001213 if (ret == EFI_NOT_FOUND)
1214 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001215 return ret;
1216}
1217
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001218/**
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001219 * load_fdt_from_load_option - load device-tree from load option
1220 *
1221 * @fdt: pointer to loaded device-tree or NULL
1222 * Return: status code
1223 */
1224static efi_status_t load_fdt_from_load_option(void **fdt)
1225{
1226 struct efi_device_path *dp = NULL;
1227 struct efi_file_handle *f = NULL;
1228 efi_uintn_t filesize;
1229 efi_status_t ret;
1230
1231 *fdt = NULL;
1232
1233 dp = efi_get_dp_from_boot(&efi_guid_fdt);
1234 if (!dp)
1235 return EFI_SUCCESS;
1236
1237 /* Open file */
1238 f = efi_file_from_path(dp);
1239 if (!f) {
1240 log_err("Can't find %pD specified in Boot####\n", dp);
1241 ret = EFI_NOT_FOUND;
1242 goto out;
1243 }
1244
1245 /* Get file size */
1246 ret = efi_file_size(f, &filesize);
1247 if (ret != EFI_SUCCESS)
1248 goto out;
1249
1250 *fdt = calloc(1, filesize);
1251 if (!*fdt) {
1252 log_err("Out of memory\n");
1253 ret = EFI_OUT_OF_RESOURCES;
1254 goto out;
1255 }
1256 ret = EFI_CALL(f->read(f, &filesize, *fdt));
1257 if (ret != EFI_SUCCESS) {
1258 log_err("Can't read fdt\n");
1259 free(*fdt);
1260 *fdt = NULL;
1261 }
1262
1263out:
1264 efi_free_pool(dp);
1265 if (f)
1266 EFI_CALL(f->close(f));
1267
1268 return ret;
1269}
1270
1271/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001272 * efi_bootmgr_run() - execute EFI boot manager
1273 * @fdt: Flat device tree
1274 *
1275 * Invoke EFI boot manager and execute a binary depending on
1276 * boot options. If @fdt is not NULL, it will be passed to
1277 * the executed binary.
1278 *
1279 * Return: status code
1280 */
1281efi_status_t efi_bootmgr_run(void *fdt)
1282{
1283 efi_handle_t handle;
1284 void *load_options;
1285 efi_status_t ret;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001286 void *fdt_lo, *fdt_distro = NULL;
1287 efi_uintn_t fdt_size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001288
1289 /* Initialize EFI drivers */
1290 ret = efi_init_obj_list();
1291 if (ret != EFI_SUCCESS) {
1292 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1293 ret & ~EFI_ERROR_MASK);
1294 return CMD_RET_FAILURE;
1295 }
1296
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001297 ret = efi_bootmgr_load(&handle, &load_options);
1298 if (ret != EFI_SUCCESS) {
1299 log_notice("EFI boot manager: Cannot load any image\n");
1300 return ret;
1301 }
1302
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001303 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
1304 ret = load_fdt_from_load_option(&fdt_lo);
1305 if (ret != EFI_SUCCESS)
1306 return ret;
1307 if (fdt_lo)
1308 fdt = fdt_lo;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001309 if (!fdt) {
Heinrich Schuchardt8257f162024-07-13 13:30:29 +02001310 efi_load_distro_fdt(handle, &fdt_distro, &fdt_size);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001311 fdt = fdt_distro;
1312 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001313 }
1314
1315 /*
1316 * Needed in ACPI case to create reservations based on
1317 * control device-tree.
1318 */
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001319 ret = efi_install_fdt(fdt);
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001320
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001321 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001322 free(fdt_lo);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001323 if (fdt_distro)
1324 efi_free_pages((uintptr_t)fdt_distro,
1325 efi_size_in_pages(fdt_size));
1326 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001327
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001328 if (ret != EFI_SUCCESS) {
1329 if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
1330 free(load_options);
1331 else
1332 log_err("Unloading image failed\n");
1333
1334 return ret;
1335 }
1336
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001337 return do_bootefi_exec(handle, load_options);
1338}