blob: 631a25d76e9e0418ea21fc02be5112e778e0d67d [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark9975fe92017-09-13 18:05:38 -04002/*
Heinrich Schuchardte1fec152018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clark9975fe92017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark9975fe92017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clark9975fe92017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013#include <malloc.h>
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +090014#include <efi_default_filename.h>
Rob Clark9975fe92017-09-13 18:05:38 -040015#include <efi_loader.h>
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020016#include <efi_variable.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090017#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040018
19static const struct efi_boot_services *bs;
20static const struct efi_runtime_services *rs;
21
Rob Clark9975fe92017-09-13 18:05:38 -040022/*
23 * bootmgr implements the logic of trying to find a payload to boot
24 * based on the BootOrder + BootXXXX variables, and then loading it.
25 *
26 * TODO detecting a special key held (f9?) and displaying a boot menu
27 * like you would get on a PC would be clever.
28 *
29 * TODO if we had a way to write and persist variables after the OS
30 * has started, we'd also want to check OsIndications to see if we
31 * should do normal or recovery boot.
32 */
33
Heinrich Schuchardt1064d042020-08-07 17:47:13 +020034/**
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +090035 * expand_media_path() - expand a device path for default file name
36 * @device_path: device path to check against
37 *
38 * If @device_path is a media or disk partition which houses a file
39 * system, this function returns a full device path which contains
40 * an architecture-specific default file name for removable media.
41 *
42 * Return: a newly allocated device path
43 */
44static
45struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
46{
47 struct efi_device_path *dp, *full_path;
48 efi_handle_t handle;
49 efi_status_t ret;
50
51 if (!device_path)
52 return NULL;
53
54 /*
55 * If device_path is a (removable) media or partition which provides
56 * simple file system protocol, append a default file name to support
57 * booting from removable media.
58 */
59 dp = device_path;
60 ret = EFI_CALL(efi_locate_device_path(
61 &efi_simple_file_system_protocol_guid,
62 &dp, &handle));
63 if (ret == EFI_SUCCESS) {
64 if (dp->type == DEVICE_PATH_TYPE_END) {
65 dp = efi_dp_from_file(NULL, 0,
66 "/EFI/BOOT/" BOOTEFI_NAME);
67 full_path = efi_dp_append(device_path, dp);
68 efi_free_pool(dp);
69 } else {
70 full_path = efi_dp_dup(device_path);
71 }
72 } else {
73 full_path = efi_dp_dup(device_path);
74 }
75
76 return full_path;
77}
78
79/**
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020080 * try_load_entry() - try to load image for boot option
81 *
Rob Clark9975fe92017-09-13 18:05:38 -040082 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020083 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -040084 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020085 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020086 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
87 * @handle: on return handle for the newly installed image
88 * @load_options: load options set on the loaded image protocol
89 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -040090 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020091static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
92 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -040093{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090094 struct efi_load_option lo;
Heinrich Schuchardt4f419962022-04-25 23:35:01 +020095 u16 varname[9];
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090096 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020097 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090098 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -040099
Heinrich Schuchardt4f419962022-04-25 23:35:01 +0200100 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clark9975fe92017-09-13 18:05:38 -0400101
Ilias Apalodimasf4dc1bc2021-03-27 10:56:07 +0200102 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clark9975fe92017-09-13 18:05:38 -0400103 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900104 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400105
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200106 ret = efi_deserialize_load_option(&lo, load_option, &size);
107 if (ret != EFI_SUCCESS) {
108 log_warning("Invalid load option for %ls\n", varname);
109 goto error;
110 }
Rob Clark9975fe92017-09-13 18:05:38 -0400111
112 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +0900113 struct efi_device_path *file_path;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900114 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400115
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200116 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
117 lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400118
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +0900119 file_path = expand_media_path(lo.file_path);
120 ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900121 NULL, 0, handle));
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +0900122 efi_free_pool(file_path);
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200123 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200124 log_warning("Loading %ls '%ls' failed\n",
125 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400126 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200127 }
Rob Clark9975fe92017-09-13 18:05:38 -0400128
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900129 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
130 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass156ccbc2022-01-23 12:55:12 -0700131 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200132 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200133 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200134 if (ret != EFI_SUCCESS)
135 goto unload;
136 /* try to register load file2 for initrd's */
137 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
138 ret = efi_initrd_register();
139 if (ret != EFI_SUCCESS)
140 goto unload;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900141 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900142
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200143 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900144 } else {
145 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400146 }
147
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200148 /* Set load options */
149 if (size) {
150 *load_options = malloc(size);
151 if (!*load_options) {
152 ret = EFI_OUT_OF_RESOURCES;
153 goto error;
154 }
155 memcpy(*load_options, lo.optional_data, size);
156 ret = efi_set_load_options(*handle, size, *load_options);
157 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100158 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200159 }
160
Rob Clark9975fe92017-09-13 18:05:38 -0400161error:
162 free(load_option);
163
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900164 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200165
166unload:
167 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
168 log_err("Unloading image failed\n");
169 free(load_option);
170
171 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400172}
173
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200174/**
175 * efi_bootmgr_load() - try to load from BootNext or BootOrder
176 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900177 * Attempt to load from BootNext or in the order specified by BootOrder
178 * EFI variable, the available load-options, finding and returning
179 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200180 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200181 * @handle: on return handle for the newly installed image
182 * @load_options: load options set on the loaded image protocol
183 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400184 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200185efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400186{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900187 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200188 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400189 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900190 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400191
Rob Clark9975fe92017-09-13 18:05:38 -0400192 bs = systab.boottime;
193 rs = systab.runtime;
194
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900195 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900196 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700197 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200198 &efi_global_variable_guid,
199 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900200 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
201 /* BootNext does exist here */
202 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200203 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900204
205 /* delete BootNext */
Simon Glass156ccbc2022-01-23 12:55:12 -0700206 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200207 &efi_global_variable_guid,
208 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900209
210 /* load BootNext */
211 if (ret == EFI_SUCCESS) {
212 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200213 ret = try_load_entry(bootnext, handle,
214 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900215 if (ret == EFI_SUCCESS)
216 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200217 log_warning(
218 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900219 }
220 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200221 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900222 }
223 }
224
225 /* BootOrder */
Simon Glass156ccbc2022-01-23 12:55:12 -0700226 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100227 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200228 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900229 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400230 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100231 }
Rob Clark9975fe92017-09-13 18:05:38 -0400232
233 num = size / sizeof(uint16_t);
234 for (i = 0; i < num; i++) {
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200235 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200236 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900237 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400238 break;
239 }
240
241 free(bootorder);
242
243error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900244 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400245}