blob: 8c51a6ef2edfd4551777ec8fdc48044699eba354 [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>
Rob Clarkc84c1102017-09-13 18:05:38 -040021
22static const struct efi_boot_services *bs;
23static const struct efi_runtime_services *rs;
24
Masahisa Kojima949c4412023-11-10 13:25:40 +090025/**
26 * struct uridp_context - uri device path resource
27 *
28 * @image_size: image size
29 * @image_addr: image address
30 * @loaded_dp: pointer to loaded device path
31 * @ramdisk_blk_dev: pointer to the ramdisk blk device
32 * @mem_handle: efi_handle to the loaded PE-COFF image
33 */
34struct uridp_context {
35 ulong image_size;
36 ulong image_addr;
37 struct efi_device_path *loaded_dp;
38 struct udevice *ramdisk_blk_dev;
39 efi_handle_t mem_handle;
40};
41
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090042const efi_guid_t efi_guid_bootmenu_auto_generated =
43 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
44
Rob Clarkc84c1102017-09-13 18:05:38 -040045/*
46 * bootmgr implements the logic of trying to find a payload to boot
47 * based on the BootOrder + BootXXXX variables, and then loading it.
48 *
49 * TODO detecting a special key held (f9?) and displaying a boot menu
50 * like you would get on a PC would be clever.
51 *
52 * TODO if we had a way to write and persist variables after the OS
53 * has started, we'd also want to check OsIndications to see if we
54 * should do normal or recovery boot.
55 */
56
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020057/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090058 * expand_media_path() - expand a device path for default file name
59 * @device_path: device path to check against
60 *
61 * If @device_path is a media or disk partition which houses a file
62 * system, this function returns a full device path which contains
63 * an architecture-specific default file name for removable media.
64 *
65 * Return: a newly allocated device path
66 */
67static
68struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
69{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020070 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090071 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090072
73 if (!device_path)
74 return NULL;
75
76 /*
77 * If device_path is a (removable) media or partition which provides
78 * simple file system protocol, append a default file name to support
79 * booting from removable media.
80 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020081 handle = efi_dp_find_obj(device_path,
82 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000083 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000084 if (rem->type == DEVICE_PATH_TYPE_END) {
Simon Glass3b1e60b2024-11-07 14:31:43 -070085 char fname[30];
86
87 snprintf(fname, sizeof(fname), "/EFI/BOOT/%s",
88 efi_get_basename());
89 full_path = efi_dp_from_file(device_path, fname);
90
AKASHI Takahirofd972f52022-04-28 17:09:39 +090091 } else {
92 full_path = efi_dp_dup(device_path);
93 }
94 } else {
95 full_path = efi_dp_dup(device_path);
96 }
97
98 return full_path;
99}
100
101/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900102 * try_load_from_file_path() - try to load a file
103 *
104 * Given a file media path iterate through a list of handles and try to
105 * to load the file from each of them until the first success.
106 *
107 * @fs_handles: array of handles with the simple file protocol
108 * @num: number of handles in fs_handles
109 * @fp: file path to open
110 * @handle: on return pointer to handle for loaded image
111 * @removable: if true only consider removable media, else only non-removable
112 */
113static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
114 efi_uintn_t num,
115 struct efi_device_path *fp,
116 efi_handle_t *handle,
117 bool removable)
118{
119 struct efi_handler *handler;
120 struct efi_device_path *dp;
121 int i;
122 efi_status_t ret;
123
124 for (i = 0; i < num; i++) {
125 if (removable != efi_disk_is_removable(fs_handles[i]))
126 continue;
127
128 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
129 &handler);
130 if (ret != EFI_SUCCESS)
131 continue;
132
133 dp = handler->protocol_interface;
134 if (!dp)
135 continue;
136
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200137 dp = efi_dp_concat(dp, fp, 0);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900138 if (!dp)
139 continue;
140
141 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
142 handle));
143 efi_free_pool(dp);
144 if (ret == EFI_SUCCESS)
145 return ret;
146 }
147
148 return EFI_NOT_FOUND;
149}
150
151/**
152 * try_load_from_short_path
153 * @fp: file path
154 * @handle: pointer to handle for newly installed image
155 *
156 * Enumerate all the devices which support file system operations,
157 * prepend its media device path to the file path, @fp, and
158 * try to load the file.
159 * This function should be called when handling a short-form path
160 * which is starting with a file device path.
161 *
162 * Return: status code
163 */
164static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
165 efi_handle_t *handle)
166{
167 efi_handle_t *fs_handles;
168 efi_uintn_t num;
169 efi_status_t ret;
170
171 ret = EFI_CALL(efi_locate_handle_buffer(
172 BY_PROTOCOL,
173 &efi_simple_file_system_protocol_guid,
174 NULL,
175 &num, &fs_handles));
176 if (ret != EFI_SUCCESS)
177 return ret;
178 if (!num)
179 return EFI_NOT_FOUND;
180
181 /* removable media first */
182 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
183 if (ret == EFI_SUCCESS)
184 goto out;
185
186 /* fixed media */
187 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
188 if (ret == EFI_SUCCESS)
189 goto out;
190
191out:
192 return ret;
193}
194
195/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900196 * mount_image() - mount the image with blkmap
197 *
198 * @lo_label: u16 label string of load option
199 * @addr: image address
200 * @size: image size
201 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
202 */
203static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
204{
205 int err;
206 struct blkmap *bm;
207 struct udevice *bm_dev;
208 char *label = NULL, *p;
209
210 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
211 if (!label)
212 return NULL;
213
214 p = label;
215 utf16_utf8_strcpy(&p, lo_label);
216 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
217 if (err) {
218 efi_free_pool(label);
219 return NULL;
220 }
221 bm = dev_get_plat(bm_dev);
222
223 efi_free_pool(label);
224
225 return bm->blk;
226}
227
228/**
229 * search_default_file() - search default file
230 *
231 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
232 * @loaded_dp: pointer to default file device path
233 * Return: status code
234 */
235static efi_status_t search_default_file(struct udevice *dev,
236 struct efi_device_path **loaded_dp)
237{
238 efi_status_t ret;
239 efi_handle_t handle;
240 u16 *default_file_name = NULL;
241 struct efi_file_handle *root, *f;
242 struct efi_device_path *dp = NULL, *fp = NULL;
243 struct efi_simple_file_system_protocol *file_system;
244 struct efi_device_path *device_path, *full_path = NULL;
245
246 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
247 log_warning("DM_TAG_EFI not found\n");
248 return EFI_INVALID_PARAMETER;
249 }
250
251 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
252 (void **)&device_path, efi_root, NULL,
253 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
254 if (ret != EFI_SUCCESS)
255 return ret;
256
257 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
258 (void **)&file_system, efi_root, NULL,
259 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
260 if (ret != EFI_SUCCESS)
261 return ret;
262
263 ret = EFI_CALL(file_system->open_volume(file_system, &root));
264 if (ret != EFI_SUCCESS)
265 return ret;
266
267 full_path = expand_media_path(device_path);
268 ret = efi_dp_split_file_path(full_path, &dp, &fp);
269 if (ret != EFI_SUCCESS)
270 goto err;
271
272 default_file_name = efi_dp_str(fp);
273 efi_free_pool(dp);
274 efi_free_pool(fp);
275 if (!default_file_name) {
276 ret = EFI_OUT_OF_RESOURCES;
277 goto err;
278 }
279
280 ret = EFI_CALL(root->open(root, &f, default_file_name,
281 EFI_FILE_MODE_READ, 0));
282 efi_free_pool(default_file_name);
283 if (ret != EFI_SUCCESS)
284 goto err;
285
286 EFI_CALL(f->close(f));
287 EFI_CALL(root->close(root));
288
289 *loaded_dp = full_path;
290
291 return EFI_SUCCESS;
292
293err:
294 EFI_CALL(root->close(root));
295 efi_free_pool(full_path);
296
297 return ret;
298}
299
300/**
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900301 * fill_default_file_path() - get fallback boot device path for block device
302 *
303 * Provide the device path to the fallback UEFI boot file, e.g.
304 * EFI/BOOT/BOOTAA64.EFI if that file exists on the block device @blk.
Masahisa Kojima949c4412023-11-10 13:25:40 +0900305 *
306 * @blk: pointer to the UCLASS_BLK udevice
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900307 * @dp: pointer to store the fallback boot device path
Masahisa Kojima949c4412023-11-10 13:25:40 +0900308 * Return: status code
309 */
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900310static efi_status_t fill_default_file_path(struct udevice *blk,
311 struct efi_device_path **dp)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900312{
313 efi_status_t ret;
314 struct udevice *partition;
315
316 /* image that has no partition table but a file system */
317 ret = search_default_file(blk, dp);
318 if (ret == EFI_SUCCESS)
319 return ret;
320
321 /* try the partitions */
322 device_foreach_child(partition, blk) {
323 enum uclass_id id;
324
325 id = device_get_uclass_id(partition);
326 if (id != UCLASS_PARTITION)
327 continue;
328
329 ret = search_default_file(partition, dp);
330 if (ret == EFI_SUCCESS)
331 return ret;
332 }
333
334 return EFI_NOT_FOUND;
335}
336
337/**
338 * prepare_loaded_image() - prepare ramdisk for downloaded image
339 *
340 * @label: label of load option
341 * @addr: image address
342 * @size: image size
343 * @dp: pointer to default file device path
344 * @blk: pointer to created blk udevice
345 * Return: status code
346 */
347static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
348 struct efi_device_path **dp,
349 struct udevice **blk)
350{
351 efi_status_t ret;
352 struct udevice *ramdisk_blk;
353
354 ramdisk_blk = mount_image(label, addr, size);
355 if (!ramdisk_blk)
356 return EFI_LOAD_ERROR;
357
Masahisa Kojima54ce3dd2024-01-12 09:19:21 +0900358 ret = fill_default_file_path(ramdisk_blk, dp);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900359 if (ret != EFI_SUCCESS) {
360 log_info("Cannot boot from downloaded image\n");
361 goto err;
362 }
363
364 /*
365 * TODO: expose the ramdisk to OS.
366 * Need to pass the ramdisk information by the architecture-specific
367 * methods such as 'pmem' device-tree node.
368 */
369 ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE);
370 if (ret != EFI_SUCCESS) {
371 log_err("Memory reservation failed\n");
372 goto err;
373 }
374
375 *blk = ramdisk_blk;
376
377 return EFI_SUCCESS;
378
379err:
380 if (blkmap_destroy(ramdisk_blk->parent))
381 log_err("Destroying blkmap failed\n");
382
383 return ret;
384}
385
386/**
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300387 * efi_bootmgr_release_uridp() - cleanup uri device path resource
Masahisa Kojima949c4412023-11-10 13:25:40 +0900388 *
389 * @ctx: event context
390 * Return: status code
391 */
Ilias Apalodimas55c05712024-10-26 10:37:32 +0300392static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900393{
394 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300395 efi_status_t ret2 = EFI_SUCCESS;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900396
397 if (!ctx)
398 return ret;
399
400 /* cleanup for iso or img image */
401 if (ctx->ramdisk_blk_dev) {
402 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
403 EFI_CONVENTIONAL_MEMORY);
404 if (ret != EFI_SUCCESS)
405 log_err("Reclaiming memory failed\n");
406
407 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
408 log_err("Destroying blkmap failed\n");
409 ret = EFI_DEVICE_ERROR;
410 }
411 }
412
413 /* cleanup for PE-COFF image */
414 if (ctx->mem_handle) {
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300415 ret2 = efi_uninstall_multiple_protocol_interfaces(ctx->mem_handle,
416 &efi_guid_device_path,
417 ctx->loaded_dp,
418 NULL);
419 if (ret2 != EFI_SUCCESS)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900420 log_err("Uninstall device_path protocol failed\n");
421 }
422
423 efi_free_pool(ctx->loaded_dp);
424 free(ctx);
425
Ilias Apalodimas868937f2024-08-12 23:56:38 +0300426 return ret == EFI_SUCCESS ? ret2 : ret;
Masahisa Kojima949c4412023-11-10 13:25:40 +0900427}
428
429/**
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300430 * efi_bootmgr_http_return() - return to efibootmgr callback
Masahisa Kojima949c4412023-11-10 13:25:40 +0900431 *
432 * @event: the event for which this notification function is registered
433 * @context: event context
434 */
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300435static void EFIAPI efi_bootmgr_http_return(struct efi_event *event,
436 void *context)
Masahisa Kojima949c4412023-11-10 13:25:40 +0900437{
438 efi_status_t ret;
439
440 EFI_ENTRY("%p, %p", event, context);
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300441 ret = efi_bootmgr_release_uridp(context);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900442 EFI_EXIT(ret);
443}
444
445/**
446 * try_load_from_uri_path() - Handle the URI device path
447 *
448 * @uridp: uri device path
449 * @lo_label: label of load option
450 * @handle: pointer to handle for newly installed image
451 * Return: status code
452 */
453static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
454 u16 *lo_label,
455 efi_handle_t *handle)
456{
457 char *s;
458 int err;
459 int uri_len;
460 efi_status_t ret;
461 void *source_buffer;
462 efi_uintn_t source_size;
463 struct uridp_context *ctx;
464 struct udevice *blk = NULL;
465 struct efi_event *event = NULL;
466 efi_handle_t mem_handle = NULL;
467 struct efi_device_path *loaded_dp;
468 static ulong image_size, image_addr;
469
470 ctx = calloc(1, sizeof(struct uridp_context));
471 if (!ctx)
472 return EFI_OUT_OF_RESOURCES;
473
474 s = env_get("loadaddr");
475 if (!s) {
476 log_err("Error: loadaddr is not set\n");
477 ret = EFI_INVALID_PARAMETER;
478 goto err;
479 }
480
481 image_addr = hextoul(s, NULL);
482 err = wget_with_dns(image_addr, uridp->uri);
483 if (err < 0) {
484 ret = EFI_INVALID_PARAMETER;
485 goto err;
486 }
487
488 image_size = env_get_hex("filesize", 0);
489 if (!image_size) {
490 ret = EFI_INVALID_PARAMETER;
491 goto err;
492 }
493
494 /*
495 * If the file extension is ".iso" or ".img", mount it and try to load
496 * the default file.
497 * If the file is PE-COFF image, load the downloaded file.
498 */
499 uri_len = strlen(uridp->uri);
500 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
501 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
502 ret = prepare_loaded_image(lo_label, image_addr, image_size,
503 &loaded_dp, &blk);
504 if (ret != EFI_SUCCESS)
505 goto err;
506
507 source_buffer = NULL;
508 source_size = 0;
509 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
510 /*
511 * loaded_dp must exist until efi application returns,
512 * will be freed in return_to_efibootmgr event callback.
513 */
514 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
515 (uintptr_t)image_addr, image_size);
516 ret = efi_install_multiple_protocol_interfaces(
517 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
518 if (ret != EFI_SUCCESS)
519 goto err;
520
521 source_buffer = (void *)image_addr;
522 source_size = image_size;
523 } else {
524 log_err("Error: file type is not supported\n");
525 ret = EFI_UNSUPPORTED;
526 goto err;
527 }
528
529 ctx->image_size = image_size;
530 ctx->image_addr = image_addr;
531 ctx->loaded_dp = loaded_dp;
532 ctx->ramdisk_blk_dev = blk;
533 ctx->mem_handle = mem_handle;
534
535 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
536 source_size, handle));
537 if (ret != EFI_SUCCESS)
538 goto err;
539
540 /* create event for cleanup when the image returns or error occurs */
541 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Ilias Apalodimasfe14afc2024-08-12 23:56:37 +0300542 efi_bootmgr_http_return, ctx,
Masahisa Kojima949c4412023-11-10 13:25:40 +0900543 &efi_guid_event_group_return_to_efibootmgr,
544 &event);
545 if (ret != EFI_SUCCESS) {
546 log_err("Creating event failed\n");
547 goto err;
548 }
549
550 return ret;
551
552err:
Ilias Apalodimas32833cf2024-08-12 23:56:36 +0300553 efi_bootmgr_release_uridp(ctx);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900554
555 return ret;
556}
557
558/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900559 * try_load_from_media() - load file from media
560 *
561 * @file_path: file path
562 * @handle_img: on return handle for the newly installed image
563 *
564 * If @file_path contains a file name, load the file.
565 * If @file_path does not have a file name, search the architecture-specific
566 * fallback boot file and load it.
567 * TODO: If the FilePathList[0] device does not support
568 * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL but supports EFI_BLOCK_IO_PROTOCOL,
569 * call EFI_BOOT_SERVICES.ConnectController()
570 * TODO: FilePathList[0] device supports the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
571 * not based on EFI_BLOCK_IO_PROTOCOL
572 *
573 * Return: status code
574 */
575static efi_status_t try_load_from_media(struct efi_device_path *file_path,
576 efi_handle_t *handle_img)
577{
578 efi_handle_t handle_blkdev;
579 efi_status_t ret = EFI_SUCCESS;
580 struct efi_device_path *rem, *dp = NULL;
581 struct efi_device_path *final_dp = file_path;
582
583 handle_blkdev = efi_dp_find_obj(file_path, &efi_block_io_guid, &rem);
584 if (handle_blkdev) {
585 if (rem->type == DEVICE_PATH_TYPE_END) {
586 /* no file name present, try default file */
587 ret = fill_default_file_path(handle_blkdev->dev, &dp);
588 if (ret != EFI_SUCCESS)
589 return ret;
590
591 final_dp = dp;
592 }
593 }
594
595 ret = EFI_CALL(efi_load_image(true, efi_root, final_dp, NULL, 0, handle_img));
596
597 efi_free_pool(dp);
598
599 return ret;
600}
601
602/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200603 * try_load_entry() - try to load image for boot option
604 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400605 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200606 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400607 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200608 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200609 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
610 * @handle: on return handle for the newly installed image
611 * @load_options: load options set on the loaded image protocol
612 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400613 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200614static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
615 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400616{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900617 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200618 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900619 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200620 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900621 efi_status_t ret;
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200622 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400623
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200624 *handle = NULL;
625 *load_options = NULL;
Rob Clarkc84c1102017-09-13 18:05:38 -0400626
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200627 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200628 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400629 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900630 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400631
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200632 ret = efi_deserialize_load_option(&lo, load_option, &size);
633 if (ret != EFI_SUCCESS) {
634 log_warning("Invalid load option for %ls\n", varname);
635 goto error;
636 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400637
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200638 if (!(lo.attributes & LOAD_OPTION_ACTIVE)) {
639 ret = EFI_LOAD_ERROR;
640 goto error;
641 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400642
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200643 log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400644
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200645 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
646 /* file_path doesn't contain a device path */
647 ret = try_load_from_short_path(lo.file_path, handle);
648 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
649 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
650 ret = try_load_from_uri_path(
651 (struct efi_device_path_uri *)lo.file_path,
652 lo.label, handle);
653 else
654 ret = EFI_LOAD_ERROR;
655 } else {
656 ret = try_load_from_media(lo.file_path, handle);
657 }
658 if (ret != EFI_SUCCESS) {
659 log_warning("Loading %ls '%ls' failed\n",
660 varname, lo.label);
661 goto error;
662 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400663
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200664 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
665 EFI_VARIABLE_RUNTIME_ACCESS;
666 ret = efi_set_variable_int(u"BootCurrent", &efi_global_variable_guid,
667 attributes, sizeof(n), &n, false);
668 if (ret != EFI_SUCCESS)
669 goto error;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900670
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200671 /* try to register load file2 for initrd's */
672 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
673 ret = efi_initrd_register();
674 if (ret != EFI_SUCCESS)
675 goto error;
Rob Clarkc84c1102017-09-13 18:05:38 -0400676 }
677
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200678 log_info("Booting: %ls\n", lo.label);
679
680 /* Ignore the optional data in auto-generated boot options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900681 if (size >= sizeof(efi_guid_t) &&
682 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
683 size = 0;
684
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200685 /* Set optional data in loaded file protocol */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200686 if (size) {
687 *load_options = malloc(size);
688 if (!*load_options) {
689 ret = EFI_OUT_OF_RESOURCES;
690 goto error;
691 }
692 memcpy(*load_options, lo.optional_data, size);
693 ret = efi_set_load_options(*handle, size, *load_options);
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200694 if (ret != EFI_SUCCESS)
695 free(load_options);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200696 }
697
Rob Clarkc84c1102017-09-13 18:05:38 -0400698error:
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200699 if (ret != EFI_SUCCESS && *handle &&
700 EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200701 log_err("Unloading image failed\n");
Heinrich Schuchardtd41005b2024-04-22 10:41:00 +0200702
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200703 free(load_option);
704
705 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400706}
707
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200708/**
709 * efi_bootmgr_load() - try to load from BootNext or BootOrder
710 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900711 * Attempt to load from BootNext or in the order specified by BootOrder
712 * EFI variable, the available load-options, finding and returning
713 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200714 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200715 * @handle: on return handle for the newly installed image
716 * @load_options: load options set on the loaded image protocol
717 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400718 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200719efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400720{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900721 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200722 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400723 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900724 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400725
Rob Clarkc84c1102017-09-13 18:05:38 -0400726 bs = systab.boottime;
727 rs = systab.runtime;
728
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900729 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900730 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700731 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200732 &efi_global_variable_guid,
733 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900734 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
735 /* BootNext does exist here */
736 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200737 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900738
739 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700740 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200741 &efi_global_variable_guid,
742 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900743
744 /* load BootNext */
745 if (ret == EFI_SUCCESS) {
746 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200747 ret = try_load_entry(bootnext, handle,
748 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900749 if (ret == EFI_SUCCESS)
750 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200751 log_warning(
752 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900753 }
754 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200755 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900756 }
757 }
758
759 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700760 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100761 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200762 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900763 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400764 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100765 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400766
767 num = size / sizeof(uint16_t);
768 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200769 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200770 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900771 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400772 break;
773 }
774
775 free(bootorder);
776
777error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900778 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400779}
Raymond Mao70a76c52023-06-19 14:22:58 -0700780
781/**
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900782 * efi_bootmgr_enumerate_boot_options() - enumerate the possible bootable media
Raymond Mao70a76c52023-06-19 14:22:58 -0700783 *
784 * @opt: pointer to the media boot option structure
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900785 * @index: index of the opt array to store the boot option
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900786 * @handles: pointer to block device handles
787 * @count: On entry number of handles to block devices.
788 * On exit number of boot options.
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900789 * @removable: flag to parse removable only
Raymond Mao70a76c52023-06-19 14:22:58 -0700790 * Return: status code
791 */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900792static efi_status_t
793efi_bootmgr_enumerate_boot_options(struct eficonfig_media_boot_option *opt,
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900794 efi_uintn_t index, efi_handle_t *handles,
795 efi_uintn_t *count, bool removable)
Raymond Mao70a76c52023-06-19 14:22:58 -0700796{
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900797 u32 i, num = index;
Raymond Mao70a76c52023-06-19 14:22:58 -0700798 struct efi_handler *handler;
799 efi_status_t ret = EFI_SUCCESS;
800
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900801 for (i = 0; i < *count; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700802 u16 *p;
803 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
804 char *optional_data;
805 struct efi_load_option lo;
806 char buf[BOOTMENU_DEVICE_NAME_MAX];
807 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700808 struct efi_device_path *short_dp;
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900809 struct efi_block_io *blkio;
810
811 ret = efi_search_protocol(handles[i], &efi_block_io_guid, &handler);
812 blkio = handler->protocol_interface;
Raymond Mao70a76c52023-06-19 14:22:58 -0700813
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900814 if (blkio->media->logical_partition)
815 continue;
816
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900817 if (removable != (blkio->media->removable_media != 0))
818 continue;
819
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900820 ret = efi_search_protocol(handles[i], &efi_guid_device_path, &handler);
Raymond Mao70a76c52023-06-19 14:22:58 -0700821 if (ret != EFI_SUCCESS)
822 continue;
823 ret = efi_protocol_open(handler, (void **)&device_path,
824 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
825 if (ret != EFI_SUCCESS)
826 continue;
827
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900828 ret = efi_disk_get_device_name(handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
Raymond Mao70a76c52023-06-19 14:22:58 -0700829 if (ret != EFI_SUCCESS)
830 continue;
831
832 p = dev_name;
833 utf8_utf16_strncpy(&p, buf, strlen(buf));
834
Raymond Maob5542a62023-06-19 14:23:01 -0700835 /* prefer to short form device path */
836 short_dp = efi_dp_shorten(device_path);
837 if (short_dp)
838 device_path = short_dp;
839
Raymond Mao70a76c52023-06-19 14:22:58 -0700840 lo.label = dev_name;
841 lo.attributes = LOAD_OPTION_ACTIVE;
842 lo.file_path = device_path;
843 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
844 /*
845 * Set the dedicated guid to optional_data, it is used to identify
846 * the boot option that automatically generated by the bootmenu.
847 * efi_serialize_load_option() expects optional_data is null-terminated
848 * utf8 string, so set the "1234567" string to allocate enough space
849 * to store guid, instead of realloc the load_option.
850 */
851 lo.optional_data = "1234567";
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900852 opt[num].size = efi_serialize_load_option(&lo, (u8 **)&opt[num].lo);
853 if (!opt[num].size) {
Raymond Mao70a76c52023-06-19 14:22:58 -0700854 ret = EFI_OUT_OF_RESOURCES;
855 goto out;
856 }
857 /* set the guid */
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900858 optional_data = (char *)opt[num].lo + (opt[num].size - u16_strsize(u"1234567"));
Raymond Mao70a76c52023-06-19 14:22:58 -0700859 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900860 num++;
Masahisa Kojimae0d14242024-01-12 09:19:23 +0900861
862 if (num >= *count)
863 break;
Raymond Mao70a76c52023-06-19 14:22:58 -0700864 }
865
866out:
Masahisa Kojima19410bb2024-01-12 09:19:22 +0900867 *count = num;
868
Raymond Mao70a76c52023-06-19 14:22:58 -0700869 return ret;
870}
871
872/**
873 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
874 *
875 * @opt: pointer to the media boot option structure
876 * @count: number of media boot option structure
877 * Return: status code
878 */
879static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
880 efi_status_t count)
881{
882 efi_uintn_t size;
883 void *load_option;
884 u32 i, list_size = 0;
885 struct efi_load_option lo;
886 u16 *var_name16 = NULL;
887 u16 varname[] = u"Boot####";
888 efi_status_t ret = EFI_SUCCESS;
889 u16 *delete_index_list = NULL, *p;
890 efi_uintn_t buf_size;
891
892 buf_size = 128;
893 var_name16 = malloc(buf_size);
894 if (!var_name16)
895 return EFI_OUT_OF_RESOURCES;
896
897 var_name16[0] = 0;
898 for (;;) {
899 int index;
900 efi_guid_t guid;
901 efi_uintn_t tmp;
902
903 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
904 if (ret == EFI_NOT_FOUND) {
905 /*
906 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
907 * This should be treated as success.
908 */
909 ret = EFI_SUCCESS;
910 break;
911 }
912
913 if (ret != EFI_SUCCESS)
914 goto out;
915
916 if (!efi_varname_is_load_option(var_name16, &index))
917 continue;
918
919 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
920 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
921 if (!load_option)
922 continue;
923
924 tmp = size;
925 ret = efi_deserialize_load_option(&lo, load_option, &size);
926 if (ret != EFI_SUCCESS)
927 goto next;
928
929 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
930 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
931 for (i = 0; i < count; i++) {
932 if (opt[i].size == tmp &&
933 memcmp(opt[i].lo, load_option, tmp) == 0) {
934 opt[i].exist = true;
935 break;
936 }
937 }
938
939 /*
940 * The entire list of variables must be retrieved by
941 * efi_get_next_variable_name_int() before deleting the invalid
942 * boot option, just save the index here.
943 */
944 if (i == count) {
945 p = realloc(delete_index_list, sizeof(u32) *
946 (list_size + 1));
947 if (!p) {
948 ret = EFI_OUT_OF_RESOURCES;
949 goto out;
950 }
951 delete_index_list = p;
952 delete_index_list[list_size++] = index;
953 }
954 }
955next:
956 free(load_option);
957 }
958
959 /* delete all invalid boot options */
960 for (i = 0; i < list_size; i++) {
961 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
962 if (ret != EFI_SUCCESS)
963 goto out;
964 }
965
966out:
967 free(var_name16);
968 free(delete_index_list);
969
970 return ret;
971}
972
973/**
974 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
975 *
976 * @buf: pointer to the buffer to store boot option variable name
977 * @buf_size: buffer size
978 * @index: pointer to store the index in the BootOrder variable
979 * Return: status code
980 */
981efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
982 unsigned int *index)
983{
984 u32 i;
985 efi_status_t ret;
986 efi_uintn_t size;
987
988 if (buf_size < u16_strsize(u"Boot####"))
989 return EFI_BUFFER_TOO_SMALL;
990
991 for (i = 0; i <= 0xFFFF; i++) {
992 size = 0;
993 efi_create_indexed_name(buf, buf_size, "Boot", i);
994 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
995 NULL, &size, NULL, NULL);
996 if (ret == EFI_BUFFER_TOO_SMALL)
997 continue;
998 else
999 break;
1000 }
1001
1002 if (i > 0xFFFF)
1003 return EFI_OUT_OF_RESOURCES;
1004
1005 *index = i;
1006
1007 return EFI_SUCCESS;
1008}
1009
1010/**
1011 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
1012 *
1013 * @index: "Boot####" index to append to BootOrder variable
1014 * Return: status code
1015 */
1016efi_status_t efi_bootmgr_append_bootorder(u16 index)
1017{
1018 u16 *bootorder;
1019 efi_status_t ret;
1020 u16 *new_bootorder = NULL;
1021 efi_uintn_t last, size, new_size;
1022
1023 /* append new boot option */
1024 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1025 last = size / sizeof(u16);
1026 new_size = size + sizeof(u16);
1027 new_bootorder = calloc(1, new_size);
1028 if (!new_bootorder) {
1029 ret = EFI_OUT_OF_RESOURCES;
1030 goto out;
1031 }
1032 memcpy(new_bootorder, bootorder, size);
1033 new_bootorder[last] = index;
1034
1035 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1036 EFI_VARIABLE_NON_VOLATILE |
1037 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1038 EFI_VARIABLE_RUNTIME_ACCESS,
1039 new_size, new_bootorder, false);
1040 if (ret != EFI_SUCCESS)
1041 goto out;
1042
1043out:
1044 free(bootorder);
1045 free(new_bootorder);
1046
1047 return ret;
1048}
1049
1050/**
1051 * efi_bootmgr_delete_boot_option() - delete selected boot option
1052 *
1053 * @boot_index: boot option index to delete
1054 * Return: status code
1055 */
1056efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
1057{
1058 u16 *bootorder;
1059 u16 varname[9];
1060 efi_status_t ret;
1061 unsigned int index;
1062 efi_uintn_t num, size;
1063
1064 efi_create_indexed_name(varname, sizeof(varname),
1065 "Boot", boot_index);
1066 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1067 0, 0, NULL, false);
1068 if (ret != EFI_SUCCESS) {
1069 log_err("delete boot option(%ls) failed\n", varname);
1070 return ret;
1071 }
1072
1073 /* update BootOrder if necessary */
1074 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1075 if (!bootorder)
1076 return EFI_SUCCESS;
1077
1078 num = size / sizeof(u16);
1079 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1080 return EFI_SUCCESS;
1081
1082 memmove(&bootorder[index], &bootorder[index + 1],
1083 (num - index - 1) * sizeof(u16));
1084 size -= sizeof(u16);
1085 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1086 EFI_VARIABLE_NON_VOLATILE |
1087 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1088 EFI_VARIABLE_RUNTIME_ACCESS,
1089 size, bootorder, false);
1090
1091 return ret;
1092}
1093
1094/**
1095 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1096 *
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001097 * This function enumerates all BlockIo devices and add the boot option for it.
Raymond Mao70a76c52023-06-19 14:22:58 -07001098 * This function also provide the BOOT#### variable maintenance for
1099 * the media device entries.
1100 * - Automatically create the BOOT#### variable for the newly detected device,
1101 * this BOOT#### variable is distinguished by the special GUID
1102 * stored in the EFI_LOAD_OPTION.optional_data
1103 * - If the device is not attached to the system, the associated BOOT#### variable
1104 * is automatically deleted.
1105 *
1106 * Return: status code
1107 */
1108efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1109{
1110 u32 i;
1111 efi_status_t ret;
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001112 efi_uintn_t count, num, total;
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001113 efi_handle_t *handles = NULL;
Raymond Mao70a76c52023-06-19 14:22:58 -07001114 struct eficonfig_media_boot_option *opt = NULL;
1115
1116 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001117 &efi_block_io_guid,
Raymond Mao70a76c52023-06-19 14:22:58 -07001118 NULL, &count,
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001119 (efi_handle_t **)&handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001120 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001121 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001122
1123 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001124 if (!opt) {
1125 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001126 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001127 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001128
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001129 /* parse removable block io followed by fixed block io */
1130 num = count;
1131 ret = efi_bootmgr_enumerate_boot_options(opt, 0, handles, &num, true);
Raymond Mao70a76c52023-06-19 14:22:58 -07001132 if (ret != EFI_SUCCESS)
1133 goto out;
1134
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001135 total = num;
1136 num = count;
1137 ret = efi_bootmgr_enumerate_boot_options(opt, total, handles, &num, false);
1138 if (ret != EFI_SUCCESS)
1139 goto out;
1140
1141 total = num;
1142
Raymond Mao70a76c52023-06-19 14:22:58 -07001143 /*
1144 * System hardware configuration may vary depending on the user setup.
1145 * The boot option is automatically added by the bootmenu.
1146 * If the device is not attached to the system, the boot option needs
1147 * to be deleted.
1148 */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001149 ret = efi_bootmgr_delete_invalid_boot_option(opt, total);
Raymond Mao70a76c52023-06-19 14:22:58 -07001150 if (ret != EFI_SUCCESS)
1151 goto out;
1152
1153 /* add non-existent boot option */
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001154 for (i = 0; i < total; i++) {
Raymond Mao70a76c52023-06-19 14:22:58 -07001155 u32 boot_index;
1156 u16 var_name[9];
1157
1158 if (!opt[i].exist) {
1159 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1160 &boot_index);
1161 if (ret != EFI_SUCCESS)
1162 goto out;
1163
1164 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1165 EFI_VARIABLE_NON_VOLATILE |
1166 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1167 EFI_VARIABLE_RUNTIME_ACCESS,
1168 opt[i].size, opt[i].lo, false);
1169 if (ret != EFI_SUCCESS)
1170 goto out;
1171
1172 ret = efi_bootmgr_append_bootorder(boot_index);
1173 if (ret != EFI_SUCCESS) {
1174 efi_set_variable_int(var_name, &efi_global_variable_guid,
1175 0, 0, NULL, false);
1176 goto out;
1177 }
1178 }
1179 }
1180
1181out:
1182 if (opt) {
Masahisa Kojimae0d14242024-01-12 09:19:23 +09001183 for (i = 0; i < total; i++)
Raymond Mao70a76c52023-06-19 14:22:58 -07001184 free(opt[i].lo);
1185 }
1186 free(opt);
Masahisa Kojima19410bb2024-01-12 09:19:22 +09001187 efi_free_pool(handles);
Raymond Mao70a76c52023-06-19 14:22:58 -07001188
Raymond Maoa35784d2023-06-19 14:22:59 -07001189 if (ret == EFI_NOT_FOUND)
1190 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001191 return ret;
1192}
1193
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001194/**
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001195 * load_fdt_from_load_option - load device-tree from load option
1196 *
1197 * @fdt: pointer to loaded device-tree or NULL
1198 * Return: status code
1199 */
1200static efi_status_t load_fdt_from_load_option(void **fdt)
1201{
1202 struct efi_device_path *dp = NULL;
1203 struct efi_file_handle *f = NULL;
1204 efi_uintn_t filesize;
1205 efi_status_t ret;
1206
1207 *fdt = NULL;
1208
1209 dp = efi_get_dp_from_boot(&efi_guid_fdt);
1210 if (!dp)
1211 return EFI_SUCCESS;
1212
1213 /* Open file */
1214 f = efi_file_from_path(dp);
1215 if (!f) {
1216 log_err("Can't find %pD specified in Boot####\n", dp);
1217 ret = EFI_NOT_FOUND;
1218 goto out;
1219 }
1220
1221 /* Get file size */
1222 ret = efi_file_size(f, &filesize);
1223 if (ret != EFI_SUCCESS)
1224 goto out;
1225
1226 *fdt = calloc(1, filesize);
1227 if (!*fdt) {
1228 log_err("Out of memory\n");
1229 ret = EFI_OUT_OF_RESOURCES;
1230 goto out;
1231 }
1232 ret = EFI_CALL(f->read(f, &filesize, *fdt));
1233 if (ret != EFI_SUCCESS) {
1234 log_err("Can't read fdt\n");
1235 free(*fdt);
1236 *fdt = NULL;
1237 }
1238
1239out:
1240 efi_free_pool(dp);
1241 if (f)
1242 EFI_CALL(f->close(f));
1243
1244 return ret;
1245}
1246
1247/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001248 * efi_bootmgr_run() - execute EFI boot manager
1249 * @fdt: Flat device tree
1250 *
1251 * Invoke EFI boot manager and execute a binary depending on
1252 * boot options. If @fdt is not NULL, it will be passed to
1253 * the executed binary.
1254 *
1255 * Return: status code
1256 */
1257efi_status_t efi_bootmgr_run(void *fdt)
1258{
1259 efi_handle_t handle;
1260 void *load_options;
1261 efi_status_t ret;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001262 void *fdt_lo, *fdt_distro = NULL;
1263 efi_uintn_t fdt_size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001264
1265 /* Initialize EFI drivers */
1266 ret = efi_init_obj_list();
1267 if (ret != EFI_SUCCESS) {
1268 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1269 ret & ~EFI_ERROR_MASK);
1270 return CMD_RET_FAILURE;
1271 }
1272
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001273 ret = efi_bootmgr_load(&handle, &load_options);
1274 if (ret != EFI_SUCCESS) {
1275 log_notice("EFI boot manager: Cannot load any image\n");
1276 return ret;
1277 }
1278
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001279 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
1280 ret = load_fdt_from_load_option(&fdt_lo);
1281 if (ret != EFI_SUCCESS)
1282 return ret;
1283 if (fdt_lo)
1284 fdt = fdt_lo;
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001285 if (!fdt) {
Heinrich Schuchardt8257f162024-07-13 13:30:29 +02001286 efi_load_distro_fdt(handle, &fdt_distro, &fdt_size);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001287 fdt = fdt_distro;
1288 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001289 }
1290
1291 /*
1292 * Needed in ACPI case to create reservations based on
1293 * control device-tree.
1294 */
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001295 ret = efi_install_fdt(fdt);
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001296
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001297 if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001298 free(fdt_lo);
Heinrich Schuchardtd9b23632024-04-26 16:13:21 +02001299 if (fdt_distro)
1300 efi_free_pages((uintptr_t)fdt_distro,
1301 efi_size_in_pages(fdt_size));
1302 }
Heinrich Schuchardtdfeb0422024-04-26 16:13:17 +02001303
Heinrich Schuchardt00df4ad2024-04-22 11:03:10 +02001304 if (ret != EFI_SUCCESS) {
1305 if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
1306 free(load_options);
1307 else
1308 log_err("Unloading image failed\n");
1309
1310 return ret;
1311 }
1312
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001313 return do_bootefi_exec(handle, load_options);
1314}