blob: 68d7db5ea303c94307a7f8ddf2b89dcdf6f07d2f [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkc84c1102017-09-13 18:05:38 -04002/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clarkc84c1102017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clarkc84c1102017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt273df962020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Masahisa Kojima949c4412023-11-10 13:25:40 +090010#include <blk.h>
11#include <blkmap.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040012#include <charset.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040015#include <malloc.h>
Masahisa Kojima949c4412023-11-10 13:25:40 +090016#include <net.h>
AKASHI Takahirofd972f52022-04-28 17:09:39 +090017#include <efi_default_filename.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040018#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020019#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090020#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040021
22static const struct efi_boot_services *bs;
23static const struct efi_runtime_services *rs;
24
Masahisa Kojima949c4412023-11-10 13:25:40 +090025/**
26 * struct uridp_context - uri device path resource
27 *
28 * @image_size: image size
29 * @image_addr: image address
30 * @loaded_dp: pointer to loaded device path
31 * @ramdisk_blk_dev: pointer to the ramdisk blk device
32 * @mem_handle: efi_handle to the loaded PE-COFF image
33 */
34struct uridp_context {
35 ulong image_size;
36 ulong image_addr;
37 struct efi_device_path *loaded_dp;
38 struct udevice *ramdisk_blk_dev;
39 efi_handle_t mem_handle;
40};
41
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090042const efi_guid_t efi_guid_bootmenu_auto_generated =
43 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
44
Rob Clarkc84c1102017-09-13 18:05:38 -040045/*
46 * bootmgr implements the logic of trying to find a payload to boot
47 * based on the BootOrder + BootXXXX variables, and then loading it.
48 *
49 * TODO detecting a special key held (f9?) and displaying a boot menu
50 * like you would get on a PC would be clever.
51 *
52 * TODO if we had a way to write and persist variables after the OS
53 * has started, we'd also want to check OsIndications to see if we
54 * should do normal or recovery boot.
55 */
56
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020057/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090058 * expand_media_path() - expand a device path for default file name
59 * @device_path: device path to check against
60 *
61 * If @device_path is a media or disk partition which houses a file
62 * system, this function returns a full device path which contains
63 * an architecture-specific default file name for removable media.
64 *
65 * Return: a newly allocated device path
66 */
67static
68struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
69{
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020070 struct efi_device_path *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090071 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090072
73 if (!device_path)
74 return NULL;
75
76 /*
77 * If device_path is a (removable) media or partition which provides
78 * simple file system protocol, append a default file name to support
79 * booting from removable media.
80 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020081 handle = efi_dp_find_obj(device_path,
82 &efi_simple_file_system_protocol_guid, &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000083 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000084 if (rem->type == DEVICE_PATH_TYPE_END) {
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +020085 full_path = efi_dp_from_file(device_path,
86 "/EFI/BOOT/" BOOTEFI_NAME);
AKASHI Takahirofd972f52022-04-28 17:09:39 +090087 } else {
88 full_path = efi_dp_dup(device_path);
89 }
90 } else {
91 full_path = efi_dp_dup(device_path);
92 }
93
94 return full_path;
95}
96
97/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +090098 * try_load_from_file_path() - try to load a file
99 *
100 * Given a file media path iterate through a list of handles and try to
101 * to load the file from each of them until the first success.
102 *
103 * @fs_handles: array of handles with the simple file protocol
104 * @num: number of handles in fs_handles
105 * @fp: file path to open
106 * @handle: on return pointer to handle for loaded image
107 * @removable: if true only consider removable media, else only non-removable
108 */
109static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
110 efi_uintn_t num,
111 struct efi_device_path *fp,
112 efi_handle_t *handle,
113 bool removable)
114{
115 struct efi_handler *handler;
116 struct efi_device_path *dp;
117 int i;
118 efi_status_t ret;
119
120 for (i = 0; i < num; i++) {
121 if (removable != efi_disk_is_removable(fs_handles[i]))
122 continue;
123
124 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
125 &handler);
126 if (ret != EFI_SUCCESS)
127 continue;
128
129 dp = handler->protocol_interface;
130 if (!dp)
131 continue;
132
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200133 dp = efi_dp_concat(dp, fp, false);
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900134 if (!dp)
135 continue;
136
137 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
138 handle));
139 efi_free_pool(dp);
140 if (ret == EFI_SUCCESS)
141 return ret;
142 }
143
144 return EFI_NOT_FOUND;
145}
146
147/**
148 * try_load_from_short_path
149 * @fp: file path
150 * @handle: pointer to handle for newly installed image
151 *
152 * Enumerate all the devices which support file system operations,
153 * prepend its media device path to the file path, @fp, and
154 * try to load the file.
155 * This function should be called when handling a short-form path
156 * which is starting with a file device path.
157 *
158 * Return: status code
159 */
160static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
161 efi_handle_t *handle)
162{
163 efi_handle_t *fs_handles;
164 efi_uintn_t num;
165 efi_status_t ret;
166
167 ret = EFI_CALL(efi_locate_handle_buffer(
168 BY_PROTOCOL,
169 &efi_simple_file_system_protocol_guid,
170 NULL,
171 &num, &fs_handles));
172 if (ret != EFI_SUCCESS)
173 return ret;
174 if (!num)
175 return EFI_NOT_FOUND;
176
177 /* removable media first */
178 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
179 if (ret == EFI_SUCCESS)
180 goto out;
181
182 /* fixed media */
183 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
184 if (ret == EFI_SUCCESS)
185 goto out;
186
187out:
188 return ret;
189}
190
191/**
Masahisa Kojima949c4412023-11-10 13:25:40 +0900192 * mount_image() - mount the image with blkmap
193 *
194 * @lo_label: u16 label string of load option
195 * @addr: image address
196 * @size: image size
197 * Return: pointer to the UCLASS_BLK udevice, NULL if failed
198 */
199static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size)
200{
201 int err;
202 struct blkmap *bm;
203 struct udevice *bm_dev;
204 char *label = NULL, *p;
205
206 label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
207 if (!label)
208 return NULL;
209
210 p = label;
211 utf16_utf8_strcpy(&p, lo_label);
212 err = blkmap_create_ramdisk(label, addr, size, &bm_dev);
213 if (err) {
214 efi_free_pool(label);
215 return NULL;
216 }
217 bm = dev_get_plat(bm_dev);
218
219 efi_free_pool(label);
220
221 return bm->blk;
222}
223
224/**
225 * search_default_file() - search default file
226 *
227 * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
228 * @loaded_dp: pointer to default file device path
229 * Return: status code
230 */
231static efi_status_t search_default_file(struct udevice *dev,
232 struct efi_device_path **loaded_dp)
233{
234 efi_status_t ret;
235 efi_handle_t handle;
236 u16 *default_file_name = NULL;
237 struct efi_file_handle *root, *f;
238 struct efi_device_path *dp = NULL, *fp = NULL;
239 struct efi_simple_file_system_protocol *file_system;
240 struct efi_device_path *device_path, *full_path = NULL;
241
242 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) {
243 log_warning("DM_TAG_EFI not found\n");
244 return EFI_INVALID_PARAMETER;
245 }
246
247 ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path,
248 (void **)&device_path, efi_root, NULL,
249 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
250 if (ret != EFI_SUCCESS)
251 return ret;
252
253 ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid,
254 (void **)&file_system, efi_root, NULL,
255 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
256 if (ret != EFI_SUCCESS)
257 return ret;
258
259 ret = EFI_CALL(file_system->open_volume(file_system, &root));
260 if (ret != EFI_SUCCESS)
261 return ret;
262
263 full_path = expand_media_path(device_path);
264 ret = efi_dp_split_file_path(full_path, &dp, &fp);
265 if (ret != EFI_SUCCESS)
266 goto err;
267
268 default_file_name = efi_dp_str(fp);
269 efi_free_pool(dp);
270 efi_free_pool(fp);
271 if (!default_file_name) {
272 ret = EFI_OUT_OF_RESOURCES;
273 goto err;
274 }
275
276 ret = EFI_CALL(root->open(root, &f, default_file_name,
277 EFI_FILE_MODE_READ, 0));
278 efi_free_pool(default_file_name);
279 if (ret != EFI_SUCCESS)
280 goto err;
281
282 EFI_CALL(f->close(f));
283 EFI_CALL(root->close(root));
284
285 *loaded_dp = full_path;
286
287 return EFI_SUCCESS;
288
289err:
290 EFI_CALL(root->close(root));
291 efi_free_pool(full_path);
292
293 return ret;
294}
295
296/**
297 * check_disk_has_default_file() - load the default file
298 *
299 * @blk: pointer to the UCLASS_BLK udevice
300 * @dp: pointer to default file device path
301 * Return: status code
302 */
303static efi_status_t check_disk_has_default_file(struct udevice *blk,
304 struct efi_device_path **dp)
305{
306 efi_status_t ret;
307 struct udevice *partition;
308
309 /* image that has no partition table but a file system */
310 ret = search_default_file(blk, dp);
311 if (ret == EFI_SUCCESS)
312 return ret;
313
314 /* try the partitions */
315 device_foreach_child(partition, blk) {
316 enum uclass_id id;
317
318 id = device_get_uclass_id(partition);
319 if (id != UCLASS_PARTITION)
320 continue;
321
322 ret = search_default_file(partition, dp);
323 if (ret == EFI_SUCCESS)
324 return ret;
325 }
326
327 return EFI_NOT_FOUND;
328}
329
330/**
331 * prepare_loaded_image() - prepare ramdisk for downloaded image
332 *
333 * @label: label of load option
334 * @addr: image address
335 * @size: image size
336 * @dp: pointer to default file device path
337 * @blk: pointer to created blk udevice
338 * Return: status code
339 */
340static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
341 struct efi_device_path **dp,
342 struct udevice **blk)
343{
344 efi_status_t ret;
345 struct udevice *ramdisk_blk;
346
347 ramdisk_blk = mount_image(label, addr, size);
348 if (!ramdisk_blk)
349 return EFI_LOAD_ERROR;
350
351 ret = check_disk_has_default_file(ramdisk_blk, dp);
352 if (ret != EFI_SUCCESS) {
353 log_info("Cannot boot from downloaded image\n");
354 goto err;
355 }
356
357 /*
358 * TODO: expose the ramdisk to OS.
359 * Need to pass the ramdisk information by the architecture-specific
360 * methods such as 'pmem' device-tree node.
361 */
362 ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE);
363 if (ret != EFI_SUCCESS) {
364 log_err("Memory reservation failed\n");
365 goto err;
366 }
367
368 *blk = ramdisk_blk;
369
370 return EFI_SUCCESS;
371
372err:
373 if (blkmap_destroy(ramdisk_blk->parent))
374 log_err("Destroying blkmap failed\n");
375
376 return ret;
377}
378
379/**
380 * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource
381 *
382 * @ctx: event context
383 * Return: status code
384 */
385efi_status_t efi_bootmgr_release_uridp_resource(struct uridp_context *ctx)
386{
387 efi_status_t ret = EFI_SUCCESS;
388
389 if (!ctx)
390 return ret;
391
392 /* cleanup for iso or img image */
393 if (ctx->ramdisk_blk_dev) {
394 ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
395 EFI_CONVENTIONAL_MEMORY);
396 if (ret != EFI_SUCCESS)
397 log_err("Reclaiming memory failed\n");
398
399 if (blkmap_destroy(ctx->ramdisk_blk_dev->parent)) {
400 log_err("Destroying blkmap failed\n");
401 ret = EFI_DEVICE_ERROR;
402 }
403 }
404
405 /* cleanup for PE-COFF image */
406 if (ctx->mem_handle) {
407 ret = efi_uninstall_multiple_protocol_interfaces(
408 ctx->mem_handle, &efi_guid_device_path, ctx->loaded_dp,
409 NULL);
410 if (ret != EFI_SUCCESS)
411 log_err("Uninstall device_path protocol failed\n");
412 }
413
414 efi_free_pool(ctx->loaded_dp);
415 free(ctx);
416
417 return ret;
418}
419
420/**
421 * efi_bootmgr_image_return_notify() - return to efibootmgr callback
422 *
423 * @event: the event for which this notification function is registered
424 * @context: event context
425 */
426static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event,
427 void *context)
428{
429 efi_status_t ret;
430
431 EFI_ENTRY("%p, %p", event, context);
432 ret = efi_bootmgr_release_uridp_resource(context);
433 EFI_EXIT(ret);
434}
435
436/**
437 * try_load_from_uri_path() - Handle the URI device path
438 *
439 * @uridp: uri device path
440 * @lo_label: label of load option
441 * @handle: pointer to handle for newly installed image
442 * Return: status code
443 */
444static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
445 u16 *lo_label,
446 efi_handle_t *handle)
447{
448 char *s;
449 int err;
450 int uri_len;
451 efi_status_t ret;
452 void *source_buffer;
453 efi_uintn_t source_size;
454 struct uridp_context *ctx;
455 struct udevice *blk = NULL;
456 struct efi_event *event = NULL;
457 efi_handle_t mem_handle = NULL;
458 struct efi_device_path *loaded_dp;
459 static ulong image_size, image_addr;
460
461 ctx = calloc(1, sizeof(struct uridp_context));
462 if (!ctx)
463 return EFI_OUT_OF_RESOURCES;
464
465 s = env_get("loadaddr");
466 if (!s) {
467 log_err("Error: loadaddr is not set\n");
468 ret = EFI_INVALID_PARAMETER;
469 goto err;
470 }
471
472 image_addr = hextoul(s, NULL);
473 err = wget_with_dns(image_addr, uridp->uri);
474 if (err < 0) {
475 ret = EFI_INVALID_PARAMETER;
476 goto err;
477 }
478
479 image_size = env_get_hex("filesize", 0);
480 if (!image_size) {
481 ret = EFI_INVALID_PARAMETER;
482 goto err;
483 }
484
485 /*
486 * If the file extension is ".iso" or ".img", mount it and try to load
487 * the default file.
488 * If the file is PE-COFF image, load the downloaded file.
489 */
490 uri_len = strlen(uridp->uri);
491 if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
492 !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
493 ret = prepare_loaded_image(lo_label, image_addr, image_size,
494 &loaded_dp, &blk);
495 if (ret != EFI_SUCCESS)
496 goto err;
497
498 source_buffer = NULL;
499 source_size = 0;
500 } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
501 /*
502 * loaded_dp must exist until efi application returns,
503 * will be freed in return_to_efibootmgr event callback.
504 */
505 loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
506 (uintptr_t)image_addr, image_size);
507 ret = efi_install_multiple_protocol_interfaces(
508 &mem_handle, &efi_guid_device_path, loaded_dp, NULL);
509 if (ret != EFI_SUCCESS)
510 goto err;
511
512 source_buffer = (void *)image_addr;
513 source_size = image_size;
514 } else {
515 log_err("Error: file type is not supported\n");
516 ret = EFI_UNSUPPORTED;
517 goto err;
518 }
519
520 ctx->image_size = image_size;
521 ctx->image_addr = image_addr;
522 ctx->loaded_dp = loaded_dp;
523 ctx->ramdisk_blk_dev = blk;
524 ctx->mem_handle = mem_handle;
525
526 ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer,
527 source_size, handle));
528 if (ret != EFI_SUCCESS)
529 goto err;
530
531 /* create event for cleanup when the image returns or error occurs */
532 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
533 efi_bootmgr_image_return_notify, ctx,
534 &efi_guid_event_group_return_to_efibootmgr,
535 &event);
536 if (ret != EFI_SUCCESS) {
537 log_err("Creating event failed\n");
538 goto err;
539 }
540
541 return ret;
542
543err:
544 efi_bootmgr_release_uridp_resource(ctx);
545
546 return ret;
547}
548
549/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200550 * try_load_entry() - try to load image for boot option
551 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400552 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200553 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400554 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200555 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200556 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
557 * @handle: on return handle for the newly installed image
558 * @load_options: load options set on the loaded image protocol
559 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400560 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200561static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
562 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400563{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900564 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200565 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900566 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200567 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900568 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400569
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200570 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clarkc84c1102017-09-13 18:05:38 -0400571
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200572 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400573 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900574 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400575
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200576 ret = efi_deserialize_load_option(&lo, load_option, &size);
577 if (ret != EFI_SUCCESS) {
578 log_warning("Invalid load option for %ls\n", varname);
579 goto error;
580 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400581
582 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahirofd972f52022-04-28 17:09:39 +0900583 struct efi_device_path *file_path;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900584 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400585
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200586 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
587 lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400588
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900589 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
590 /* file_path doesn't contain a device path */
591 ret = try_load_from_short_path(lo.file_path, handle);
Masahisa Kojima949c4412023-11-10 13:25:40 +0900592 } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
593 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
594 ret = try_load_from_uri_path(
595 (struct efi_device_path_uri *)lo.file_path,
596 lo.label, handle);
597 else
598 ret = EFI_LOAD_ERROR;
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900599 } else {
600 file_path = expand_media_path(lo.file_path);
601 ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
602 NULL, 0, handle));
603 efi_free_pool(file_path);
604 }
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200605 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200606 log_warning("Loading %ls '%ls' failed\n",
607 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400608 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200609 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400610
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900611 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
612 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass90975372022-01-23 12:55:12 -0700613 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200614 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200615 attributes, sizeof(n), &n, false);
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200616 if (ret != EFI_SUCCESS)
617 goto unload;
618 /* try to register load file2 for initrd's */
619 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
620 ret = efi_initrd_register();
621 if (ret != EFI_SUCCESS)
622 goto unload;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900623 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900624
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200625 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900626 } else {
627 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400628 }
629
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200630 /* Set load options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900631 if (size >= sizeof(efi_guid_t) &&
632 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
633 size = 0;
634
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200635 if (size) {
636 *load_options = malloc(size);
637 if (!*load_options) {
638 ret = EFI_OUT_OF_RESOURCES;
639 goto error;
640 }
641 memcpy(*load_options, lo.optional_data, size);
642 ret = efi_set_load_options(*handle, size, *load_options);
643 } else {
Heinrich Schuchardt7c2a8592020-12-27 15:46:00 +0100644 *load_options = NULL;
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200645 }
646
Rob Clarkc84c1102017-09-13 18:05:38 -0400647error:
648 free(load_option);
649
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900650 return ret;
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200651
652unload:
653 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
654 log_err("Unloading image failed\n");
655 free(load_option);
656
657 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400658}
659
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200660/**
661 * efi_bootmgr_load() - try to load from BootNext or BootOrder
662 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900663 * Attempt to load from BootNext or in the order specified by BootOrder
664 * EFI variable, the available load-options, finding and returning
665 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200666 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200667 * @handle: on return handle for the newly installed image
668 * @load_options: load options set on the loaded image protocol
669 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400670 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200671efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400672{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900673 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200674 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400675 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900676 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400677
Rob Clarkc84c1102017-09-13 18:05:38 -0400678 bs = systab.boottime;
679 rs = systab.runtime;
680
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900681 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900682 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700683 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200684 &efi_global_variable_guid,
685 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900686 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
687 /* BootNext does exist here */
688 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200689 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900690
691 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700692 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200693 &efi_global_variable_guid,
694 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900695
696 /* load BootNext */
697 if (ret == EFI_SUCCESS) {
698 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200699 ret = try_load_entry(bootnext, handle,
700 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900701 if (ret == EFI_SUCCESS)
702 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200703 log_warning(
704 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900705 }
706 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200707 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900708 }
709 }
710
711 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700712 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100713 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200714 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900715 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400716 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100717 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400718
719 num = size / sizeof(uint16_t);
720 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200721 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200722 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900723 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400724 break;
725 }
726
727 free(bootorder);
728
729error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900730 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400731}
Raymond Mao70a76c52023-06-19 14:22:58 -0700732
733/**
734 * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media
735 *
736 * @opt: pointer to the media boot option structure
737 * @volume_handles: pointer to the efi handles
738 * @count: number of efi handle
739 * Return: status code
740 */
741static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
742 efi_handle_t *volume_handles,
743 efi_status_t count)
744{
745 u32 i;
746 struct efi_handler *handler;
747 efi_status_t ret = EFI_SUCCESS;
748
749 for (i = 0; i < count; i++) {
750 u16 *p;
751 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
752 char *optional_data;
753 struct efi_load_option lo;
754 char buf[BOOTMENU_DEVICE_NAME_MAX];
755 struct efi_device_path *device_path;
Raymond Maob5542a62023-06-19 14:23:01 -0700756 struct efi_device_path *short_dp;
Raymond Mao70a76c52023-06-19 14:22:58 -0700757
758 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
759 if (ret != EFI_SUCCESS)
760 continue;
761 ret = efi_protocol_open(handler, (void **)&device_path,
762 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
763 if (ret != EFI_SUCCESS)
764 continue;
765
766 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
767 if (ret != EFI_SUCCESS)
768 continue;
769
770 p = dev_name;
771 utf8_utf16_strncpy(&p, buf, strlen(buf));
772
Raymond Maob5542a62023-06-19 14:23:01 -0700773 /* prefer to short form device path */
774 short_dp = efi_dp_shorten(device_path);
775 if (short_dp)
776 device_path = short_dp;
777
Raymond Mao70a76c52023-06-19 14:22:58 -0700778 lo.label = dev_name;
779 lo.attributes = LOAD_OPTION_ACTIVE;
780 lo.file_path = device_path;
781 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
782 /*
783 * Set the dedicated guid to optional_data, it is used to identify
784 * the boot option that automatically generated by the bootmenu.
785 * efi_serialize_load_option() expects optional_data is null-terminated
786 * utf8 string, so set the "1234567" string to allocate enough space
787 * to store guid, instead of realloc the load_option.
788 */
789 lo.optional_data = "1234567";
790 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
791 if (!opt[i].size) {
792 ret = EFI_OUT_OF_RESOURCES;
793 goto out;
794 }
795 /* set the guid */
796 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
797 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
798 }
799
800out:
801 return ret;
802}
803
804/**
805 * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
806 *
807 * @opt: pointer to the media boot option structure
808 * @count: number of media boot option structure
809 * Return: status code
810 */
811static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
812 efi_status_t count)
813{
814 efi_uintn_t size;
815 void *load_option;
816 u32 i, list_size = 0;
817 struct efi_load_option lo;
818 u16 *var_name16 = NULL;
819 u16 varname[] = u"Boot####";
820 efi_status_t ret = EFI_SUCCESS;
821 u16 *delete_index_list = NULL, *p;
822 efi_uintn_t buf_size;
823
824 buf_size = 128;
825 var_name16 = malloc(buf_size);
826 if (!var_name16)
827 return EFI_OUT_OF_RESOURCES;
828
829 var_name16[0] = 0;
830 for (;;) {
831 int index;
832 efi_guid_t guid;
833 efi_uintn_t tmp;
834
835 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
836 if (ret == EFI_NOT_FOUND) {
837 /*
838 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
839 * This should be treated as success.
840 */
841 ret = EFI_SUCCESS;
842 break;
843 }
844
845 if (ret != EFI_SUCCESS)
846 goto out;
847
848 if (!efi_varname_is_load_option(var_name16, &index))
849 continue;
850
851 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
852 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
853 if (!load_option)
854 continue;
855
856 tmp = size;
857 ret = efi_deserialize_load_option(&lo, load_option, &size);
858 if (ret != EFI_SUCCESS)
859 goto next;
860
861 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
862 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
863 for (i = 0; i < count; i++) {
864 if (opt[i].size == tmp &&
865 memcmp(opt[i].lo, load_option, tmp) == 0) {
866 opt[i].exist = true;
867 break;
868 }
869 }
870
871 /*
872 * The entire list of variables must be retrieved by
873 * efi_get_next_variable_name_int() before deleting the invalid
874 * boot option, just save the index here.
875 */
876 if (i == count) {
877 p = realloc(delete_index_list, sizeof(u32) *
878 (list_size + 1));
879 if (!p) {
880 ret = EFI_OUT_OF_RESOURCES;
881 goto out;
882 }
883 delete_index_list = p;
884 delete_index_list[list_size++] = index;
885 }
886 }
887next:
888 free(load_option);
889 }
890
891 /* delete all invalid boot options */
892 for (i = 0; i < list_size; i++) {
893 ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
894 if (ret != EFI_SUCCESS)
895 goto out;
896 }
897
898out:
899 free(var_name16);
900 free(delete_index_list);
901
902 return ret;
903}
904
905/**
906 * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
907 *
908 * @buf: pointer to the buffer to store boot option variable name
909 * @buf_size: buffer size
910 * @index: pointer to store the index in the BootOrder variable
911 * Return: status code
912 */
913efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
914 unsigned int *index)
915{
916 u32 i;
917 efi_status_t ret;
918 efi_uintn_t size;
919
920 if (buf_size < u16_strsize(u"Boot####"))
921 return EFI_BUFFER_TOO_SMALL;
922
923 for (i = 0; i <= 0xFFFF; i++) {
924 size = 0;
925 efi_create_indexed_name(buf, buf_size, "Boot", i);
926 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
927 NULL, &size, NULL, NULL);
928 if (ret == EFI_BUFFER_TOO_SMALL)
929 continue;
930 else
931 break;
932 }
933
934 if (i > 0xFFFF)
935 return EFI_OUT_OF_RESOURCES;
936
937 *index = i;
938
939 return EFI_SUCCESS;
940}
941
942/**
943 * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
944 *
945 * @index: "Boot####" index to append to BootOrder variable
946 * Return: status code
947 */
948efi_status_t efi_bootmgr_append_bootorder(u16 index)
949{
950 u16 *bootorder;
951 efi_status_t ret;
952 u16 *new_bootorder = NULL;
953 efi_uintn_t last, size, new_size;
954
955 /* append new boot option */
956 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
957 last = size / sizeof(u16);
958 new_size = size + sizeof(u16);
959 new_bootorder = calloc(1, new_size);
960 if (!new_bootorder) {
961 ret = EFI_OUT_OF_RESOURCES;
962 goto out;
963 }
964 memcpy(new_bootorder, bootorder, size);
965 new_bootorder[last] = index;
966
967 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
968 EFI_VARIABLE_NON_VOLATILE |
969 EFI_VARIABLE_BOOTSERVICE_ACCESS |
970 EFI_VARIABLE_RUNTIME_ACCESS,
971 new_size, new_bootorder, false);
972 if (ret != EFI_SUCCESS)
973 goto out;
974
975out:
976 free(bootorder);
977 free(new_bootorder);
978
979 return ret;
980}
981
982/**
983 * efi_bootmgr_delete_boot_option() - delete selected boot option
984 *
985 * @boot_index: boot option index to delete
986 * Return: status code
987 */
988efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
989{
990 u16 *bootorder;
991 u16 varname[9];
992 efi_status_t ret;
993 unsigned int index;
994 efi_uintn_t num, size;
995
996 efi_create_indexed_name(varname, sizeof(varname),
997 "Boot", boot_index);
998 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
999 0, 0, NULL, false);
1000 if (ret != EFI_SUCCESS) {
1001 log_err("delete boot option(%ls) failed\n", varname);
1002 return ret;
1003 }
1004
1005 /* update BootOrder if necessary */
1006 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1007 if (!bootorder)
1008 return EFI_SUCCESS;
1009
1010 num = size / sizeof(u16);
1011 if (!efi_search_bootorder(bootorder, num, boot_index, &index))
1012 return EFI_SUCCESS;
1013
1014 memmove(&bootorder[index], &bootorder[index + 1],
1015 (num - index - 1) * sizeof(u16));
1016 size -= sizeof(u16);
1017 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1018 EFI_VARIABLE_NON_VOLATILE |
1019 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1020 EFI_VARIABLE_RUNTIME_ACCESS,
1021 size, bootorder, false);
1022
1023 return ret;
1024}
1025
1026/**
1027 * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
1028 *
1029 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
1030 * and generate the bootmenu entries.
1031 * This function also provide the BOOT#### variable maintenance for
1032 * the media device entries.
1033 * - Automatically create the BOOT#### variable for the newly detected device,
1034 * this BOOT#### variable is distinguished by the special GUID
1035 * stored in the EFI_LOAD_OPTION.optional_data
1036 * - If the device is not attached to the system, the associated BOOT#### variable
1037 * is automatically deleted.
1038 *
1039 * Return: status code
1040 */
1041efi_status_t efi_bootmgr_update_media_device_boot_option(void)
1042{
1043 u32 i;
1044 efi_status_t ret;
1045 efi_uintn_t count;
1046 efi_handle_t *volume_handles = NULL;
1047 struct eficonfig_media_boot_option *opt = NULL;
1048
1049 ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
1050 &efi_simple_file_system_protocol_guid,
1051 NULL, &count,
1052 (efi_handle_t **)&volume_handles);
1053 if (ret != EFI_SUCCESS)
Raymond Maoa35784d2023-06-19 14:22:59 -07001054 goto out;
Raymond Mao70a76c52023-06-19 14:22:58 -07001055
1056 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
Raymond Maoa35784d2023-06-19 14:22:59 -07001057 if (!opt) {
1058 ret = EFI_OUT_OF_RESOURCES;
Raymond Mao70a76c52023-06-19 14:22:58 -07001059 goto out;
Raymond Maoa35784d2023-06-19 14:22:59 -07001060 }
Raymond Mao70a76c52023-06-19 14:22:58 -07001061
1062 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
1063 ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count);
1064 if (ret != EFI_SUCCESS)
1065 goto out;
1066
1067 /*
1068 * System hardware configuration may vary depending on the user setup.
1069 * The boot option is automatically added by the bootmenu.
1070 * If the device is not attached to the system, the boot option needs
1071 * to be deleted.
1072 */
1073 ret = efi_bootmgr_delete_invalid_boot_option(opt, count);
1074 if (ret != EFI_SUCCESS)
1075 goto out;
1076
1077 /* add non-existent boot option */
1078 for (i = 0; i < count; i++) {
1079 u32 boot_index;
1080 u16 var_name[9];
1081
1082 if (!opt[i].exist) {
1083 ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
1084 &boot_index);
1085 if (ret != EFI_SUCCESS)
1086 goto out;
1087
1088 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
1089 EFI_VARIABLE_NON_VOLATILE |
1090 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1091 EFI_VARIABLE_RUNTIME_ACCESS,
1092 opt[i].size, opt[i].lo, false);
1093 if (ret != EFI_SUCCESS)
1094 goto out;
1095
1096 ret = efi_bootmgr_append_bootorder(boot_index);
1097 if (ret != EFI_SUCCESS) {
1098 efi_set_variable_int(var_name, &efi_global_variable_guid,
1099 0, 0, NULL, false);
1100 goto out;
1101 }
1102 }
1103 }
1104
1105out:
1106 if (opt) {
1107 for (i = 0; i < count; i++)
1108 free(opt[i].lo);
1109 }
1110 free(opt);
1111 efi_free_pool(volume_handles);
1112
Raymond Maoa35784d2023-06-19 14:22:59 -07001113 if (ret == EFI_NOT_FOUND)
1114 return EFI_SUCCESS;
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001115 return ret;
1116}
1117
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001118/**
AKASHI Takahiro7b061922023-11-21 10:29:44 +09001119 * efi_bootmgr_run() - execute EFI boot manager
1120 * @fdt: Flat device tree
1121 *
1122 * Invoke EFI boot manager and execute a binary depending on
1123 * boot options. If @fdt is not NULL, it will be passed to
1124 * the executed binary.
1125 *
1126 * Return: status code
1127 */
1128efi_status_t efi_bootmgr_run(void *fdt)
1129{
1130 efi_handle_t handle;
1131 void *load_options;
1132 efi_status_t ret;
1133
1134 /* Initialize EFI drivers */
1135 ret = efi_init_obj_list();
1136 if (ret != EFI_SUCCESS) {
1137 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1138 ret & ~EFI_ERROR_MASK);
1139 return CMD_RET_FAILURE;
1140 }
1141
1142 ret = efi_install_fdt(fdt);
1143 if (ret != EFI_SUCCESS)
1144 return ret;
1145
1146 ret = efi_bootmgr_load(&handle, &load_options);
1147 if (ret != EFI_SUCCESS) {
1148 log_notice("EFI boot manager: Cannot load any image\n");
1149 return ret;
1150 }
1151
1152 return do_bootefi_exec(handle, load_options);
1153}