blob: f9534ef85edb55782f572219db3e295ebb0cf647 [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 Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040016#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090017#include <net.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040018#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020019#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090020#include <asm/unaligned.h>
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +053021#include <linux/kernel.h>
22#include <linux/sizes.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040023
24static const struct efi_boot_services *bs;
25static const struct efi_runtime_services *rs;
26
Masahisa Kojima949c4412023-11-10 13:25:40 +090027/**
28 * struct uridp_context - uri device path resource
29 *
30 * @image_size: image size
31 * @image_addr: image address
32 * @loaded_dp: pointer to loaded device path
33 * @ramdisk_blk_dev: pointer to the ramdisk blk device
34 * @mem_handle: efi_handle to the loaded PE-COFF image
35 */
36struct uridp_context {
37 ulong image_size;
38 ulong image_addr;
39 struct efi_device_path *loaded_dp;
40 struct udevice *ramdisk_blk_dev;
41 efi_handle_t mem_handle;
42};
43
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090044const efi_guid_t efi_guid_bootmenu_auto_generated =
45 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
46
Rob Clarkc84c1102017-09-13 18:05:38 -040047/*
48 * bootmgr implements the logic of trying to find a payload to boot
49 * based on the BootOrder + BootXXXX variables, and then loading it.
50 *
51 * TODO detecting a special key held (f9?) and displaying a boot menu
52 * like you would get on a PC would be clever.
53 *
54 * TODO if we had a way to write and persist variables after the OS
55 * has started, we'd also want to check OsIndications to see if we
56 * should do normal or recovery boot.
57 */
58
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020059/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090060 * expand_media_path() - expand a device path for default file name
61 * @device_path: device path to check against
62 *
63 * If @device_path is a media or disk partition which houses a file
64 * system, this function returns a full device path which contains
65 * an architecture-specific default file name for removable media.
66 *
67 * Return: a newly allocated device path
68 */
69static
70struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
71{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020072 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090073 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090074
75 if (!device_path)
76 return NULL;
77
78 /*
79 * If device_path is a (removable) media or partition which provides
80 * simple file system protocol, append a default file name to support
81 * booting from removable media.
82 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020083 handle = efi_dp_find_obj(device_path,
84 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000085 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000086 if (rem->type == DEVICE_PATH_TYPE_END) {
Simon Glass3b1e60b2024-11-07 14:31:43 -070087 char fname[30];
88
89 snprintf(fname, sizeof(fname), "/EFI/BOOT/%s",
90 efi_get_basename());
91 full_path = efi_dp_from_file(device_path, fname);
92
AKASHI Takahirofd972f52022-04-28 17:09:39 +090093 } else {
94 full_path = efi_dp_dup(device_path);
95 }
96 } else {
97 full_path = efi_dp_dup(device_path);
98 }
99
100 return full_path;
101}
102
103/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900104 * try_load_from_file_path() - try to load a file
105 *
106 * Given a file media path iterate through a list of handles and try to
107 * to load the file from each of them until the first success.
108 *
109 * @fs_handles: array of handles with the simple file protocol
110 * @num: number of handles in fs_handles
111 * @fp: file path to open
112 * @handle: on return pointer to handle for loaded image
113 * @removable: if true only consider removable media, else only non-removable
114 */
115static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
116 efi_uintn_t num,
117 struct efi_device_path *fp,
118 efi_handle_t *handle,
119 bool removable)
120{
121 struct efi_handler *handler;
122 struct efi_device_path *dp;
123 int i;
124 efi_status_t ret;
125
126 for (i = 0; i < num; i++) {
127 if (removable != efi_disk_is_removable(fs_handles[i]))
128 continue;
129
130 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
131 &handler);
132 if (ret != EFI_SUCCESS)
133 continue;
134
135 dp = handler->protocol_interface;
136 if (!dp)
137 continue;
138
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200139 dp = efi_dp_concat(dp, fp, 0);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900140 if (!dp)
141 continue;
142
143 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
144 handle));
145 efi_free_pool(dp);
146 if (ret == EFI_SUCCESS)
147 return ret;
148 }
149
150 return EFI_NOT_FOUND;
151}
152
153/**
154 * try_load_from_short_path
155 * @fp: file path
156 * @handle: pointer to handle for newly installed image
157 *
158 * Enumerate all the devices which support file system operations,
159 * prepend its media device path to the file path, @fp, and
160 * try to load the file.
161 * This function should be called when handling a short-form path
162 * which is starting with a file device path.
163 *
164 * Return: status code
165 */
166static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
167 efi_handle_t *handle)
168{
169 efi_handle_t *fs_handles;
170 efi_uintn_t num;
171 efi_status_t ret;
172
173 ret = EFI_CALL(efi_locate_handle_buffer(
174 BY_PROTOCOL,
175 &efi_simple_file_system_protocol_guid,
176 NULL,
177 &num, &fs_handles));
178 if (ret != EFI_SUCCESS)
179 return ret;
180 if (!num)
181 return EFI_NOT_FOUND;
182
183 /* removable media first */
184 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
185 if (ret == EFI_SUCCESS)
186 goto out;
187
188 /* fixed media */
189 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
190 if (ret == EFI_SUCCESS)
191 goto out;
192
193out:
194 return ret;
195}
196
197/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900198 * mount_image() - mount the image with blkmap
199 *
200 * @lo_label: u16 label string of load option
201 * @addr: image address
202 * @size: image size
203 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
204 */
205static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
206{
207 int err;
208 struct blkmap *bm;
209 struct udevice *bm_dev;
210 char *label = NULL, *p;
211
212 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
213 if (!label)
214 return NULL;
215
216 p = label;
217 utf16_utf8_strcpy(&p, lo_label);
218 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
219 if (err) {
220 efi_free_pool(label);
221 return NULL;
222 }
223 bm = dev_get_plat(bm_dev);
224
225 efi_free_pool(label);
226
227 return bm->blk;
228}
229
230/**
231 * search_default_file() - search default file
232 *
233 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
234 * @loaded_dp: pointer to default file device path
235 * Return: status code
236 */
237static efi_status_t search_default_file(struct udevice *dev,
238 struct efi_device_path **loaded_dp)
239{
240 efi_status_t ret;
241 efi_handle_t handle;
242 u16 *default_file_name = NULL;
243 struct efi_file_handle *root, *f;
244 struct efi_device_path *dp = NULL, *fp = NULL;
245 struct efi_simple_file_system_protocol *file_system;
246 struct efi_device_path *device_path, *full_path = NULL;
247
248 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
249 log_warning("DM_TAG_EFI not found\n");
250 return EFI_INVALID_PARAMETER;
251 }
252
253 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
254 (void **)&device_path, efi_root, NULL,
255 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
256 if (ret != EFI_SUCCESS)
257 return ret;
258
259 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
260 (void **)&file_system, efi_root, NULL,
261 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
262 if (ret != EFI_SUCCESS)
263 return ret;
264
265 ret = EFI_CALL(file_system->open_volume(file_system, &root));
266 if (ret != EFI_SUCCESS)
267 return ret;
268
269 full_path = expand_media_path(device_path);
270 ret = efi_dp_split_file_path(full_path, &dp, &fp);
271 if (ret != EFI_SUCCESS)
272 goto err;
273
274 default_file_name = efi_dp_str(fp);
275 efi_free_pool(dp);
276 efi_free_pool(fp);
277 if (!default_file_name) {
278 ret = EFI_OUT_OF_RESOURCES;
279 goto err;
280 }
281
282 ret = EFI_CALL(root->open(root, &f, default_file_name,
283 EFI_FILE_MODE_READ, 0));
284 efi_free_pool(default_file_name);
285 if (ret != EFI_SUCCESS)
286 goto err;
287
288 EFI_CALL(f->close(f));
289 EFI_CALL(root->close(root));
290
291 *loaded_dp = full_path;
292
293 return EFI_SUCCESS;
294
295err:
296 EFI_CALL(root->close(root));
297 efi_free_pool(full_path);
298
299 return ret;
300}
301
302/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900303 * fill_default_file_path() - get fallback boot device path for block device
304 *
305 * Provide the device path to the fallback UEFI boot file, e.g.
306 * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900307 *
308 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900309 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900310 * Return: status code
311 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900312static efi_status_t fill_default_file_path(struct udevice *blk,
313 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900314{
315 efi_status_t ret;
316 struct udevice *partition;
317
318 /* image that has no partition table but a file system */
319 ret = search_default_file(blk, dp);
320 if (ret == EFI_SUCCESS)
321 return ret;
322
323 /* try the partitions */
324 device_foreach_child(partition, blk) {
325 enum uclass_id id;
326
327 id = device_get_uclass_id(partition);
328 if (id != UCLASS_PARTITION)
329 continue;
330
331 ret = search_default_file(partition, dp);
332 if (ret == EFI_SUCCESS)
333 return ret;
334 }
335
336 return EFI_NOT_FOUND;
337}
338
339/**
340 * prepare_loaded_image() - prepare ramdisk for downloaded image
341 *
342 * @label: label of load option
343 * @addr: image address
344 * @size: image size
345 * @dp: pointer to default file device path
346 * @blk: pointer to created blk udevice
347 * Return: status code
348 */
349static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
350 struct efi_device_path **dp,
351 struct udevice **blk)
352{
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530353 u64 pages;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900354 efi_status_t ret;
355 struct udevice *ramdisk_blk;
356
357 ramdisk_blk = mount_image(label, addr, size);
358 if (!ramdisk_blk)
359 return EFI_LOAD_ERROR;
360
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900361 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900362 if (ret != EFI_SUCCESS) {
363 log_info("Cannot boot from downloaded image\n");
364 goto err;
365 }
366
367 /*
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530368 * Linux supports 'pmem' which allows OS installers to find, reclaim
369 * the mounted images and continue the installation since the contents
370 * of the pmem region are treated as local media.
371 *
372 * The memory regions used for it needs to be carved out of the EFI
373 * memory map.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900374 */
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530375 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
376 ret = efi_update_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY,
377 false, true);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900378 if (ret != EFI_SUCCESS) {
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530379 log_err("Failed to reserve memory\n");
Masahisa Kojima949c4412023-11-10 13:25:40 +0900380 goto err;
381 }
382
383 *blk = ramdisk_blk;
384
385 return EFI_SUCCESS;
386
387err:
388 if (blkmap_destroy(ramdisk_blk->parent))
389 log_err("Destroying blkmap failed\n");
390
391 return ret;
392}
393
394/**
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300395 * efi_bootmgr_release_uridp() - cleanup uri device path resource
Masahisa Kojima949c4412023-11-10 13:25:40 +0900396 *
397 * @ctx: event context
398 * Return: status code
399 */
Ilias Apalodimas55c05712024-10-26 10:37:32 +0300400static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900401{
402 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300403 efi_status_t ret2 = EFI_SUCCESS;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900404
405 if (!ctx)
406 return ret;
407
408 /* cleanup for iso or img image */
409 if (ctx->ramdisk_blk_dev) {
410 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
411 EFI_CONVENTIONAL_MEMORY);
412 if (ret != EFI_SUCCESS)
413 log_err("Reclaiming memory failed\n");
414
415 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
416 log_err("Destroying blkmap failed\n");
417 ret = EFI_DEVICE_ERROR;
418 }
419 }
420
421 /* cleanup for PE-COFF image */
422 if (ctx->mem_handle) {
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300423 ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle,
424 &efi_guid_device_path,
425 ctx->loaded_dp,
426 NULL);
427 if (ret2 != EFI_SUCCESS)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900428 log_err("Uninstall device_path protocol failed\n");
429 }
430
431 efi_free_pool(ctx->loaded_dp);
432 free(ctx);
433
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300434 return ret == EFI_SUCCESS ? ret2 : ret;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900435}
436
437/**
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300438 * efi_bootmgr_http_return() - return to efibootmgr callback
Masahisa Kojima949c4412023-11-10 13:25:40 +0900439 *
440 * @event: the event for which this notification function is registered
441 * @context: event context
442 */
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300443static void EFIAPI efi_bootmgr_http_return(struct efi_event *event,
444 void *context)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900445{
446 efi_status_t ret;
447
448 EFI_ENTRY("%p, %p", event, context);
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300449 ret = efi_bootmgr_release_uridp(context);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900450 EFI_EXIT(ret);
451}
452
453/**
454 * try_load_from_uri_path() - Handle the URI device path
455 *
456 * @uridp: uri device path
457 * @lo_label: label of load option
458 * @handle: pointer to handle for newly installed image
459 * Return: status code
460 */
461static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
462 u16 *lo_label,
463 efi_handle_t *handle)
464{
465 char *s;
466 int err;
467 int uri_len;
468 efi_status_t ret;
469 void *source_buffer;
470 efi_uintn_t source_size;
471 struct uridp_context *ctx;
472 struct udevice *blk = NULL;
473 struct efi_event *event = NULL;
474 efi_handle_t mem_handle = NULL;
475 struct efi_device_path *loaded_dp;
476 static ulong image_size, image_addr;
477
478 ctx = calloc(1, sizeof(struct uridp_context));
479 if (!ctx)
480 return EFI_OUT_OF_RESOURCES;
481
482 s = env_get("loadaddr");
483 if (!s) {
484 log_err("Error: loadaddr is not set\n");
485 ret = EFI_INVALID_PARAMETER;
486 goto err;
487 }
488
489 image_addr = hextoul(s, NULL);
Adriano Cordovab479fc42024-12-04 00:05:16 -0300490 err = wget_do_request(image_addr, uridp->uri);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900491 if (err < 0) {
492 ret = EFI_INVALID_PARAMETER;
493 goto err;
494 }
495
496 image_size = env_get_hex("filesize", 0);
497 if (!image_size) {
498 ret = EFI_INVALID_PARAMETER;
499 goto err;
500 }
Ilias Apalodimas3a8f9332025-03-17 14:03:59 +0530501 /*
502 * Depending on the kernel configuration, pmem memory areas must be
503 * page aligned or 2MiB aligned. PowerPC is an exception here and
504 * requires 16MiB alignment, but since we don't have EFI support for
505 * it, limit the alignment to 2MiB.
506 */
507 image_size = ALIGN(image_size, SZ_2M);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900508
509 /*
510 * If the file extension is ".iso" or ".img", mount it and try to load
511 * the default file.
512 * If the file is PE-COFF image, load the downloaded file.
513 */
514 uri_len = strlen(uridp->uri);
515 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
516 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
517 ret = prepare_loaded_image(lo_label, image_addr, image_size,
518 &loaded_dp, &blk);
519 if (ret != EFI_SUCCESS)
520 goto err;
521
522 source_buffer = NULL;
523 source_size = 0;
524 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
525 /*
526 * loaded_dp must exist until efi application returns,
527 * will be freed in return_to_efibootmgr event callback.
528 */
529 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
530 (uintptr_t)image_addr, image_size);
531 ret = efi_install_multiple_protocol_interfaces(
532 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
533 if (ret != EFI_SUCCESS)
534 goto err;
535
536 source_buffer = (void *)image_addr;
537 source_size = image_size;
538 } else {
539 log_err("Error: file type is not supported\n");
540 ret = EFI_UNSUPPORTED;
541 goto err;
542 }
543
544 ctx->image_size = image_size;
545 ctx->image_addr = image_addr;
546 ctx->loaded_dp = loaded_dp;
547 ctx->ramdisk_blk_dev = blk;
548 ctx->mem_handle = mem_handle;
549
550 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
551 source_size, handle));
552 if (ret != EFI_SUCCESS)
553 goto err;
554
555 /* create event for cleanup when the image returns or error occurs */
556 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300557 efi_bootmgr_http_return, ctx,
Masahisa Kojima949c4412023-11-10 13:25:40 +0900558 &efi_guid_event_group_return_to_efibootmgr,
559 &event);
560 if (ret != EFI_SUCCESS) {
561 log_err("Creating event failed\n");
562 goto err;
563 }
564
565 return ret;
566
567err:
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300568 efi_bootmgr_release_uridp(ctx);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900569
570 return ret;
571}
572
573/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900574 * try_load_from_media() - load file from media
575 *
576 * @file_path: file path
577 * @handle_img: on return handle for the newly installed image
578 *
579 * If @file_path contains a file name, load the file.
580 * If @file_path does not have a file name, search the architecture-specific
581 * fallback boot file and load it.
582 * TODO: If the FilePathList[0] device does not support
583 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
584 * call EFI_BOOT_SERVICES.ConnectController()
585 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
586 * not based on EFI_BLOCK_IO_PROTOCOL
587 *
588 * Return: status code
589 */
590static efi_status_t try_load_from_media(struct efi_device_path *file_path,
591 efi_handle_t *handle_img)
592{
593 efi_handle_t handle_blkdev;
594 efi_status_t ret = EFI_SUCCESS;
595 struct efi_device_path *rem, *dp = NULL;
596 struct efi_device_path *final_dp = file_path;
597
598 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
599 if (handle_blkdev) {
600 if (rem->type == DEVICE_PATH_TYPE_END) {
601 /* no file name present, try default file */
602 ret = fill_default_file_path(handle_blkdev->dev, &dp);
603 if (ret != EFI_SUCCESS)
604 return ret;
605
606 final_dp = dp;
607 }
608 }
609
610 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
611
612 efi_free_pool(dp);
613
614 return ret;
615}
616
617/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200618 * try_load_entry() - try to load image for boot option
619 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400620 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200621 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400622 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200623 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200624 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
625 * @handle: on return handle for the newly installed image
626 * @load_options: load options set on the loaded image protocol
627 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400628 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200629static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
630 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400631{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900632 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200633 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900634 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200635 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900636 efi_status_t ret;
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200637 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400638
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200639 *handle = NULL;
640 *load_options = NULL;
Rob Clarkc84c1102017-09-13 18:05:38 -0400641
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200642 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200643 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400644 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900645 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400646
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200647 ret = efi_deserialize_load_option(&lo, load_option, &size);
648 if (ret != EFI_SUCCESS) {
649 log_warning("Invalid load option for %ls\n", varname);
650 goto error;
651 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400652
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200653 if (!(lo.attributes & LOAD_OPTION_ACTIVE)) {
654 ret = EFI_LOAD_ERROR;
655 goto error;
656 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400657
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200658 log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400659
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200660 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
661 /* file_path doesn't contain a device path */
662 ret = try_load_from_short_path(lo.file_path, handle);
663 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
664 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
665 ret = try_load_from_uri_path(
666 (struct efi_device_path_uri *)lo.file_path,
667 lo.label, handle);
668 else
669 ret = EFI_LOAD_ERROR;
670 } else {
671 ret = try_load_from_media(lo.file_path, handle);
672 }
673 if (ret != EFI_SUCCESS) {
674 log_warning("Loading %ls '%ls' failed\n",
675 varname, lo.label);
676 goto error;
677 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400678
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200679 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
680 EFI_VARIABLE_RUNTIME_ACCESS;
681 ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid,
682 attributes, sizeof(n), &n, false);
683 if (ret != EFI_SUCCESS)
684 goto error;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900685
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200686 /* try to register load file2 for initrd's */
687 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
688 ret = efi_initrd_register();
689 if (ret != EFI_SUCCESS)
690 goto error;
Rob Clarkc84c1102017-09-13 18:05:38 -0400691 }
692
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200693 log_info("Booting: %ls\n", lo.label);
694
695 /* Ignore the optional data in auto-generated boot options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900696 if (size >= sizeof(efi_guid_t) &&
697 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
698 size = 0;
699
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200700 /* Set optional data in loaded file protocol */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200701 if (size) {
702 *load_options = malloc(size);
703 if (!*load_options) {
704 ret = EFI_OUT_OF_RESOURCES;
705 goto error;
706 }
707 memcpy(*load_options, lo.optional_data, size);
708 ret = efi_set_load_options(*handle, size, *load_options);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200709 if (ret != EFI_SUCCESS)
710 free(load_options);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200711 }
712
Rob Clarkc84c1102017-09-13 18:05:38 -0400713error:
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200714 if (ret != EFI_SUCCESS && *handle &&
715 EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200716 log_err("Unloading image failed\n");
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200717
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200718 free(load_option);
719
720 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400721}
722
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200723/**
724 * efi_bootmgr_load() - try to load from BootNext or BootOrder
725 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900726 * Attempt to load from BootNext or in the order specified by BootOrder
727 * EFI variable, the available load-options, finding and returning
728 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200729 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200730 * @handle: on return handle for the newly installed image
731 * @load_options: load options set on the loaded image protocol
732 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400733 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200734efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400735{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900736 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200737 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400738 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900739 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400740
Rob Clarkc84c1102017-09-13 18:05:38 -0400741 bs = systab.boottime;
742 rs = systab.runtime;
743
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900744 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900745 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700746 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200747 &efi_global_variable_guid,
748 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900749 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
750 /* BootNext does exist here */
751 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200752 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900753
754 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700755 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200756 &efi_global_variable_guid,
757 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900758
759 /* load BootNext */
760 if (ret == EFI_SUCCESS) {
761 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200762 ret = try_load_entry(bootnext, handle,
763 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900764 if (ret == EFI_SUCCESS)
765 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200766 log_warning(
767 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900768 }
769 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200770 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900771 }
772 }
773
774 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700775 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100776 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200777 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900778 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400779 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100780 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400781
782 num = size / sizeof(uint16_t);
783 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200784 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200785 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900786 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400787 break;
788 }
789
790 free(bootorder);
791
792error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900793 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400794}
Raymond Mao70a76c52023-06-19 14:22:58 -0700795
796/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900797 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700798 *
799 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900800 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900801 * @handles: pointer to block device handles
802 * @count: On entry number of handles to block devices.
803 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900804 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700805 * Return: status code
806 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900807static efi_status_t
808efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900809 efi_uintn_t index, efi_handle_t *handles,
810 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700811{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900812 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700813 struct efi_handler *handler;
814 efi_status_t ret = EFI_SUCCESS;
815
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900816 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700817 u16 *p;
818 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
819 char *optional_data;
820 struct efi_load_option lo;
821 char buf[BOOTMENU_DEVICE_NAME_MAX];
822 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700823 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900824 struct efi_block_io *blkio;
825
826 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
827 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700828
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900829 if (blkio->media->logical_partition)
830 continue;
831
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900832 if (removable != (blkio->media->removable_media != 0))
833 continue;
834
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900835 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700836 if (ret != EFI_SUCCESS)
837 continue;
838 ret = efi_protocol_open(handler, (void **)&device_path,
839 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
840 if (ret != EFI_SUCCESS)
841 continue;
842
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900843 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700844 if (ret != EFI_SUCCESS)
845 continue;
846
847 p = dev_name;
848 utf8_utf16_strncpy(&p, buf, strlen(buf));
849
Raymond Maob5542a62023-06-19 14:23:01 -0700850 /* prefer to short form device path */
851 short_dp = efi_dp_shorten(device_path);
852 if (short_dp)
853 device_path = short_dp;
854
Raymond Mao70a76c52023-06-19 14:22:58 -0700855 lo.label = dev_name;
856 lo.attributes = LOAD_OPTION_ACTIVE;
857 lo.file_path = device_path;
858 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
859 /*
860 * Set the dedicated guid to optional_data, it is used to identify
861 * the boot option that automatically generated by the bootmenu.
862 * efi_serialize_load_option() expects optional_data is null-terminated
863 * utf8 string, so set the "1234567" string to allocate enough space
864 * to store guid, instead of realloc the load_option.
865 */
866 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900867 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
868 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700869 ret = EFI_OUT_OF_RESOURCES;
870 goto out;
871 }
872 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900873 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700874 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900875 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900876
877 if (num >= *count)
878 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700879 }
880
881out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900882 *count = num;
883
Raymond Mao70a76c52023-06-19 14:22:58 -0700884 return ret;
885}
886
887/**
888 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
889 *
890 * @opt: pointer to the media boot option structure
891 * @count: number of media boot option structure
892 * Return: status code
893 */
894static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
895 efi_status_t count)
896{
897 efi_uintn_t size;
898 void *load_option;
899 u32 i, list_size = 0;
900 struct efi_load_option lo;
901 u16 *var_name16 = NULL;
902 u16 varname[] = u"Boot####";
903 efi_status_t ret = EFI_SUCCESS;
904 u16 *delete_index_list = NULL, *p;
905 efi_uintn_t buf_size;
906
907 buf_size = 128;
908 var_name16 = malloc(buf_size);
909 if (!var_name16)
910 return EFI_OUT_OF_RESOURCES;
911
912 var_name16[0] = 0;
913 for (;;) {
914 int index;
915 efi_guid_t guid;
916 efi_uintn_t tmp;
917
918 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
919 if (ret == EFI_NOT_FOUND) {
920 /*
921 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
922 * This should be treated as success.
923 */
924 ret = EFI_SUCCESS;
925 break;
926 }
927
928 if (ret != EFI_SUCCESS)
929 goto out;
930
931 if (!efi_varname_is_load_option(var_name16, &index))
932 continue;
933
934 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
935 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
936 if (!load_option)
937 continue;
938
939 tmp = size;
940 ret = efi_deserialize_load_option(&lo, load_option, &size);
941 if (ret != EFI_SUCCESS)
942 goto next;
943
944 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
945 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
946 for (i = 0; i < count; i++) {
947 if (opt[i].size == tmp &&
948 memcmp(opt[i].lo, load_option, tmp) == 0) {
949 opt[i].exist = true;
950 break;
951 }
952 }
953
954 /*
955 * The entire list of variables must be retrieved by
956 * efi_get_next_variable_name_int() before deleting the invalid
957 * boot option, just save the index here.
958 */
959 if (i == count) {
960 p = realloc(delete_index_list, sizeof(u32) *
961 (list_size + 1));
962 if (!p) {
963 ret = EFI_OUT_OF_RESOURCES;
964 goto out;
965 }
966 delete_index_list = p;
967 delete_index_list[list_size++] = index;
968 }
969 }
970next:
971 free(load_option);
972 }
973
974 /* delete all invalid boot options */
975 for (i = 0; i < list_size; i++) {
976 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
977 if (ret != EFI_SUCCESS)
978 goto out;
979 }
980
981out:
982 free(var_name16);
983 free(delete_index_list);
984
985 return ret;
986}
987
988/**
989 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
990 *
991 * @buf: pointer to the buffer to store boot option variable name
992 * @buf_size: buffer size
993 * @index: pointer to store the index in the BootOrder variable
994 * Return: status code
995 */
996efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
997 unsigned int *index)
998{
999 u32 i;
1000 efi_status_t ret;
1001 efi_uintn_t size;
1002
1003 if (buf_size < u16_strsize(u"Boot####"))
1004 return EFI_BUFFER_TOO_SMALL;
1005
1006 for (i = 0; i <= 0xFFFF; i++) {
1007 size = 0;
1008 efi_create_indexed_name(buf, buf_size, "Boot", i);
1009 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1010 NULL, &size, NULL, NULL);
1011 if (ret == EFI_BUFFER_TOO_SMALL)
1012 continue;
1013 else
1014 break;
1015 }
1016
1017 if (i > 0xFFFF)
1018 return EFI_OUT_OF_RESOURCES;
1019
1020 *index = i;
1021
1022 return EFI_SUCCESS;
1023}
1024
1025/**
1026 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1027 *
1028 * @index: "Boot####" index to append to BootOrder variable
1029 * Return: status code
1030 */
1031efi_status_t efi_bootmgr_append_bootorder(u16 index)
1032{
1033 u16 *bootorder;
1034 efi_status_t ret;
1035 u16 *new_bootorder = NULL;
1036 efi_uintn_t last, size, new_size;
1037
1038 /* append new boot option */
1039 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1040 last = size / sizeof(u16);
1041 new_size = size + sizeof(u16);
1042 new_bootorder = calloc(1, new_size);
1043 if (!new_bootorder) {
1044 ret = EFI_OUT_OF_RESOURCES;
1045 goto out;
1046 }
1047 memcpy(new_bootorder, bootorder, size);
1048 new_bootorder[last] = index;
1049
1050 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1051 EFI_VARIABLE_NON_VOLATILE |
1052 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1053 EFI_VARIABLE_RUNTIME_ACCESS,
1054 new_size, new_bootorder, false);
1055 if (ret != EFI_SUCCESS)
1056 goto out;
1057
1058out:
1059 free(bootorder);
1060 free(new_bootorder);
1061
1062 return ret;
1063}
1064
1065/**
1066 * efi_bootmgr_delete_boot_option() - delete selected boot option
1067 *
1068 * @boot_index: boot option index to delete
1069 * Return: status code
1070 */
1071efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1072{
1073 u16 *bootorder;
1074 u16 varname[9];
1075 efi_status_t ret;
1076 unsigned int index;
1077 efi_uintn_t num, size;
1078
1079 efi_create_indexed_name(varname, sizeof(varname),
1080 "Boot", boot_index);
1081 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1082 0, 0, NULL, false);
1083 if (ret != EFI_SUCCESS) {
1084 log_err("delete boot option(%ls) failed\n", varname);
1085 return ret;
1086 }
1087
1088 /* update BootOrder if necessary */
1089 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1090 if (!bootorder)
1091 return EFI_SUCCESS;
1092
1093 num = size / sizeof(u16);
1094 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1095 return EFI_SUCCESS;
1096
1097 memmove(&bootorder[index], &bootorder[index + 1],
1098 (num - index - 1) * sizeof(u16));
1099 size -= sizeof(u16);
1100 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1101 EFI_VARIABLE_NON_VOLATILE |
1102 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1103 EFI_VARIABLE_RUNTIME_ACCESS,
1104 size, bootorder, false);
1105
1106 return ret;
1107}
1108
1109/**
1110 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1111 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001112 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001113 * This function also provide the BOOT#### variable maintenance for
1114 * the media device entries.
1115 * - Automatically create the BOOT#### variable for the newly detected device,
1116 * this BOOT#### variable is distinguished by the special GUID
1117 * stored in the EFI_LOAD_OPTION.optional_data
1118 * - If the device is not attached to the system, the associated BOOT#### variable
1119 * is automatically deleted.
1120 *
1121 * Return: status code
1122 */
1123efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1124{
1125 u32 i;
1126 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001127 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001128 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001129 struct eficonfig_media_boot_option *opt = NULL;
1130
1131 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001132 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001133 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001134 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001135 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001136 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001137
1138 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001139 if (!opt) {
1140 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001141 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001142 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001143
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001144 /* parse removable block io followed by fixed block io */
1145 num = count;
1146 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001147 if (ret != EFI_SUCCESS)
1148 goto out;
1149
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001150 total = num;
1151 num = count;
1152 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1153 if (ret != EFI_SUCCESS)
1154 goto out;
1155
1156 total = num;
1157
Raymond Mao70a76c52023-06-19 14:22:58 -07001158 /*
1159 * System hardware configuration may vary depending on the user setup.
1160 * The boot option is automatically added by the bootmenu.
1161 * If the device is not attached to the system, the boot option needs
1162 * to be deleted.
1163 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001164 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001165 if (ret != EFI_SUCCESS)
1166 goto out;
1167
1168 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001169 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001170 u32 boot_index;
1171 u16 var_name[9];
1172
1173 if (!opt[i].exist) {
1174 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1175 &boot_index);
1176 if (ret != EFI_SUCCESS)
1177 goto out;
1178
1179 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1180 EFI_VARIABLE_NON_VOLATILE |
1181 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1182 EFI_VARIABLE_RUNTIME_ACCESS,
1183 opt[i].size, opt[i].lo, false);
1184 if (ret != EFI_SUCCESS)
1185 goto out;
1186
1187 ret = efi_bootmgr_append_bootorder(boot_index);
1188 if (ret != EFI_SUCCESS) {
1189 efi_set_variable_int(var_name, &efi_global_variable_guid,
1190 0, 0, NULL, false);
1191 goto out;
1192 }
1193 }
1194 }
1195
1196out:
1197 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001198 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001199 free(opt[i].lo);
1200 }
1201 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001202 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001203
Raymond Maoa35784d2023-06-19 14:22:59 -07001204 if (ret == EFI_NOT_FOUND)
1205 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001206 return ret;
1207}
1208
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001209/**
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001210 * load_fdt_from_load_option - load device-tree from load option
1211 *
1212 * @fdt: pointer to loaded device-tree or NULL
1213 * Return: status code
1214 */
1215static efi_status_t load_fdt_from_load_option(void **fdt)
1216{
1217 struct efi_device_path *dp = NULL;
1218 struct efi_file_handle *f = NULL;
1219 efi_uintn_t filesize;
1220 efi_status_t ret;
1221
1222 *fdt = NULL;
1223
1224 dp = efi_get_dp_from_boot(&efi_guid_fdt);
1225 if (!dp)
1226 return EFI_SUCCESS;
1227
1228 /* Open file */
1229 f = efi_file_from_path(dp);
1230 if (!f) {
1231 log_err("Can't find %pD specified in Boot####\n", dp);
1232 ret = EFI_NOT_FOUND;
1233 goto out;
1234 }
1235
1236 /* Get file size */
1237 ret = efi_file_size(f, &filesize);
1238 if (ret != EFI_SUCCESS)
1239 goto out;
1240
1241 *fdt = calloc(1, filesize);
1242 if (!*fdt) {
1243 log_err("Out of memory\n");
1244 ret = EFI_OUT_OF_RESOURCES;
1245 goto out;
1246 }
1247 ret = EFI_CALL(f->read(f, &filesize, *fdt));
1248 if (ret != EFI_SUCCESS) {
1249 log_err("Can't read fdt\n");
1250 free(*fdt);
1251 *fdt = NULL;
1252 }
1253
1254out:
1255 efi_free_pool(dp);
1256 if (f)
1257 EFI_CALL(f->close(f));
1258
1259 return ret;
1260}
1261
1262/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001263 * efi_bootmgr_run() - execute EFI boot manager
1264 * @fdt: Flat device tree
1265 *
1266 * Invoke EFI boot manager and execute a binary depending on
1267 * boot options. If @fdt is not NULL, it will be passed to
1268 * the executed binary.
1269 *
1270 * Return: status code
1271 */
1272efi_status_t efi_bootmgr_run(void *fdt)
1273{
1274 efi_handle_t handle;
1275 void *load_options;
1276 efi_status_t ret;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001277 void *fdt_lo, *fdt_distro = NULL;
1278 efi_uintn_t fdt_size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001279
1280 /* Initialize EFI drivers */
1281 ret = efi_init_obj_list();
1282 if (ret != EFI_SUCCESS) {
1283 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1284 ret & ~EFI_ERROR_MASK);
1285 return CMD_RET_FAILURE;
1286 }
1287
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001288 ret = efi_bootmgr_load(&handle, &load_options);
1289 if (ret != EFI_SUCCESS) {
1290 log_notice("EFI boot manager: Cannot load any image\n");
1291 return ret;
1292 }
1293
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001294 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
1295 ret = load_fdt_from_load_option(&fdt_lo);
1296 if (ret != EFI_SUCCESS)
1297 return ret;
1298 if (fdt_lo)
1299 fdt = fdt_lo;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001300 if (!fdt) {
Heinrich Schuchardt8257f162024-07-13 13:30:29 +02001301 efi_load_distro_fdt(handle, &fdt_distro, &fdt_size);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001302 fdt = fdt_distro;
1303 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001304 }
1305
1306 /*
1307 * Needed in ACPI case to create reservations based on
1308 * control device-tree.
1309 */
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001310 ret = efi_install_fdt(fdt);
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001311
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001312 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001313 free(fdt_lo);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001314 if (fdt_distro)
1315 efi_free_pages((uintptr_t)fdt_distro,
1316 efi_size_in_pages(fdt_size));
1317 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001318
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001319 if (ret != EFI_SUCCESS) {
1320 if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
1321 free(load_options);
1322 else
1323 log_err("Unloading image failed\n");
1324
1325 return ret;
1326 }
1327
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001328 return do_bootefi_exec(handle, load_options);
1329}