blob: 4b24b41047238a10af2e7525e6e4c6ef6cf338f1 [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
Rob Clarkc84c1102017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040013#include <malloc.h>
AKASHI Takahirofd972f52022-04-28 17:09:39 +090014#include <efi_default_filename.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040015#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020016#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090017#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040018
19static const struct efi_boot_services *bs;
20static const struct efi_runtime_services *rs;
21
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090022const efi_guid_t efi_guid_bootmenu_auto_generated =
23 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
24
Rob Clarkc84c1102017-09-13 18:05:38 -040025/*
26 * bootmgr implements the logic of trying to find a payload to boot
27 * based on the BootOrder + BootXXXX variables, and then loading it.
28 *
29 * TODO detecting a special key held (f9?) and displaying a boot menu
30 * like you would get on a PC would be clever.
31 *
32 * TODO if we had a way to write and persist variables after the OS
33 * has started, we'd also want to check OsIndications to see if we
34 * should do normal or recovery boot.
35 */
36
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020037/**
AKASHI Takahirofd972f52022-04-28 17:09:39 +090038 * expand_media_path() - expand a device path for default file name
39 * @device_path: device path to check against
40 *
41 * If @device_path is a media or disk partition which houses a file
42 * system, this function returns a full device path which contains
43 * an architecture-specific default file name for removable media.
44 *
45 * Return: a newly allocated device path
46 */
47static
48struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
49{
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000050 struct efi_device_path *dp, *rem, *full_path;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090051 efi_handle_t handle;
AKASHI Takahirofd972f52022-04-28 17:09:39 +090052
53 if (!device_path)
54 return NULL;
55
56 /*
57 * If device_path is a (removable) media or partition which provides
58 * simple file system protocol, append a default file name to support
59 * booting from removable media.
60 */
61 dp = device_path;
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000062 handle = efi_dp_find_obj(dp, &efi_simple_file_system_protocol_guid,
63 &rem);
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +000064 if (handle) {
Heinrich Schuchardt0628e1c2022-06-11 05:22:07 +000065 if (rem->type == DEVICE_PATH_TYPE_END) {
AKASHI Takahirofd972f52022-04-28 17:09:39 +090066 dp = efi_dp_from_file(NULL, 0,
67 "/EFI/BOOT/" BOOTEFI_NAME);
68 full_path = efi_dp_append(device_path, dp);
69 efi_free_pool(dp);
70 } else {
71 full_path = efi_dp_dup(device_path);
72 }
73 } else {
74 full_path = efi_dp_dup(device_path);
75 }
76
77 return full_path;
78}
79
80/**
AKASHI Takahirodac4d092022-05-12 11:29:02 +090081 * try_load_from_file_path() - try to load a file
82 *
83 * Given a file media path iterate through a list of handles and try to
84 * to load the file from each of them until the first success.
85 *
86 * @fs_handles: array of handles with the simple file protocol
87 * @num: number of handles in fs_handles
88 * @fp: file path to open
89 * @handle: on return pointer to handle for loaded image
90 * @removable: if true only consider removable media, else only non-removable
91 */
92static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
93 efi_uintn_t num,
94 struct efi_device_path *fp,
95 efi_handle_t *handle,
96 bool removable)
97{
98 struct efi_handler *handler;
99 struct efi_device_path *dp;
100 int i;
101 efi_status_t ret;
102
103 for (i = 0; i < num; i++) {
104 if (removable != efi_disk_is_removable(fs_handles[i]))
105 continue;
106
107 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
108 &handler);
109 if (ret != EFI_SUCCESS)
110 continue;
111
112 dp = handler->protocol_interface;
113 if (!dp)
114 continue;
115
116 dp = efi_dp_append(dp, fp);
117 if (!dp)
118 continue;
119
120 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
121 handle));
122 efi_free_pool(dp);
123 if (ret == EFI_SUCCESS)
124 return ret;
125 }
126
127 return EFI_NOT_FOUND;
128}
129
130/**
131 * try_load_from_short_path
132 * @fp: file path
133 * @handle: pointer to handle for newly installed image
134 *
135 * Enumerate all the devices which support file system operations,
136 * prepend its media device path to the file path, @fp, and
137 * try to load the file.
138 * This function should be called when handling a short-form path
139 * which is starting with a file device path.
140 *
141 * Return: status code
142 */
143static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
144 efi_handle_t *handle)
145{
146 efi_handle_t *fs_handles;
147 efi_uintn_t num;
148 efi_status_t ret;
149
150 ret = EFI_CALL(efi_locate_handle_buffer(
151 BY_PROTOCOL,
152 &efi_simple_file_system_protocol_guid,
153 NULL,
154 &num, &fs_handles));
155 if (ret != EFI_SUCCESS)
156 return ret;
157 if (!num)
158 return EFI_NOT_FOUND;
159
160 /* removable media first */
161 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
162 if (ret == EFI_SUCCESS)
163 goto out;
164
165 /* fixed media */
166 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
167 if (ret == EFI_SUCCESS)
168 goto out;
169
170out:
171 return ret;
172}
173
174/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200175 * try_load_entry() - try to load image for boot option
176 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400177 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200178 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400179 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200180 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200181 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
182 * @handle: on return handle for the newly installed image
183 * @load_options: load options set on the loaded image protocol
184 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400185 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200186static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
187 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400188{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900189 struct efi_load_option lo;
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200190 u16 varname[9];
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900191 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200192 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900193 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400194
Heinrich Schuchardtc79cebe2022-04-25 23:35:01 +0200195 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clarkc84c1102017-09-13 18:05:38 -0400196
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200197 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -0400198 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900199 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400200
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200201 ret = efi_deserialize_load_option(&lo, load_option, &size);
202 if (ret != EFI_SUCCESS) {
203 log_warning("Invalid load option for %ls\n", varname);
204 goto error;
205 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400206
207 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahirofd972f52022-04-28 17:09:39 +0900208 struct efi_device_path *file_path;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900209 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400210
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200211 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
212 lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400213
AKASHI Takahirodac4d092022-05-12 11:29:02 +0900214 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
215 /* file_path doesn't contain a device path */
216 ret = try_load_from_short_path(lo.file_path, handle);
217 } else {
218 file_path = expand_media_path(lo.file_path);
219 ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
220 NULL, 0, handle));
221 efi_free_pool(file_path);
222 }
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200223 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200224 log_warning("Loading %ls '%ls' failed\n",
225 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400226 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200227 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400228
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900229 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
230 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass90975372022-01-23 12:55:12 -0700231 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200232 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200233 attributes, sizeof(n), &n, false);
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200234 if (ret != EFI_SUCCESS)
235 goto unload;
236 /* try to register load file2 for initrd's */
237 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
238 ret = efi_initrd_register();
239 if (ret != EFI_SUCCESS)
240 goto unload;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900241 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900242
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200243 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900244 } else {
245 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400246 }
247
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200248 /* Set load options */
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900249 if (size >= sizeof(efi_guid_t) &&
250 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
251 size = 0;
252
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200253 if (size) {
254 *load_options = malloc(size);
255 if (!*load_options) {
256 ret = EFI_OUT_OF_RESOURCES;
257 goto error;
258 }
259 memcpy(*load_options, lo.optional_data, size);
260 ret = efi_set_load_options(*handle, size, *load_options);
261 } else {
Heinrich Schuchardt7c2a8592020-12-27 15:46:00 +0100262 *load_options = NULL;
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200263 }
264
Rob Clarkc84c1102017-09-13 18:05:38 -0400265error:
266 free(load_option);
267
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900268 return ret;
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200269
270unload:
271 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
272 log_err("Unloading image failed\n");
273 free(load_option);
274
275 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400276}
277
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200278/**
279 * efi_bootmgr_load() - try to load from BootNext or BootOrder
280 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900281 * Attempt to load from BootNext or in the order specified by BootOrder
282 * EFI variable, the available load-options, finding and returning
283 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200284 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200285 * @handle: on return handle for the newly installed image
286 * @load_options: load options set on the loaded image protocol
287 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400288 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200289efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400290{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900291 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200292 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400293 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900294 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400295
Rob Clarkc84c1102017-09-13 18:05:38 -0400296 bs = systab.boottime;
297 rs = systab.runtime;
298
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900299 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900300 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700301 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200302 &efi_global_variable_guid,
303 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900304 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
305 /* BootNext does exist here */
306 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200307 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900308
309 /* delete BootNext */
Simon Glass90975372022-01-23 12:55:12 -0700310 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200311 &efi_global_variable_guid,
312 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900313
314 /* load BootNext */
315 if (ret == EFI_SUCCESS) {
316 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200317 ret = try_load_entry(bootnext, handle,
318 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900319 if (ret == EFI_SUCCESS)
320 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200321 log_warning(
322 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900323 }
324 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200325 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900326 }
327 }
328
329 /* BootOrder */
Simon Glass90975372022-01-23 12:55:12 -0700330 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100331 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200332 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900333 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400334 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100335 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400336
337 num = size / sizeof(uint16_t);
338 for (i = 0; i < num; i++) {
Heinrich Schuchardte07fe5c2022-04-29 07:15:04 +0200339 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200340 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900341 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400342 break;
343 }
344
345 free(bootorder);
346
347error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900348 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400349}