blob: ede9116b3cecf05a58f5a33c44d0d8400d13e18a [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
Masahisa Kojima87d79142022-09-12 17:33:50 +090022const efi_guid_t efi_guid_bootmenu_auto_generated =
23 EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
24
Rob Clark9975fe92017-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 Schuchardt1064d042020-08-07 17:47:13 +020037/**
AKASHI Takahiro4e65ca02022-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 Schuchardt178667b2022-06-11 05:22:07 +000050 struct efi_device_path *dp, *rem, *full_path;
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +090051 efi_handle_t handle;
AKASHI Takahiro4e65ca02022-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 Schuchardt178667b2022-06-11 05:22:07 +000062 handle = efi_dp_find_obj(dp, &efi_simple_file_system_protocol_guid,
63 &rem);
Heinrich Schuchardt72fa9cd2022-06-11 05:22:08 +000064 if (handle) {
Heinrich Schuchardt178667b2022-06-11 05:22:07 +000065 if (rem->type == DEVICE_PATH_TYPE_END) {
AKASHI Takahiro4e65ca02022-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 Takahiro57ad6242022-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 Schuchardt15621aa2019-07-14 13:20:28 +0200175 * try_load_entry() - try to load image for boot option
176 *
Rob Clark9975fe92017-09-13 18:05:38 -0400177 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200178 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -0400179 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200180 *
Heinrich Schuchardt0ad64002020-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 Clark9975fe92017-09-13 18:05:38 -0400185 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200186static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
187 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400188{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900189 struct efi_load_option lo;
Heinrich Schuchardt4f419962022-04-25 23:35:01 +0200190 u16 varname[9];
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900191 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200192 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900193 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400194
Heinrich Schuchardt4f419962022-04-25 23:35:01 +0200195 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clark9975fe92017-09-13 18:05:38 -0400196
Ilias Apalodimasf4dc1bc2021-03-27 10:56:07 +0200197 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clark9975fe92017-09-13 18:05:38 -0400198 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900199 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400200
Heinrich Schuchardt0e69bcf2020-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 Clark9975fe92017-09-13 18:05:38 -0400206
207 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +0900208 struct efi_device_path *file_path;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900209 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400210
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200211 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
212 lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400213
AKASHI Takahiro57ad6242022-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 Takahiro94e6e552019-05-29 20:54:25 +0200223 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200224 log_warning("Loading %ls '%ls' failed\n",
225 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400226 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200227 }
Rob Clark9975fe92017-09-13 18:05:38 -0400228
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900229 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
230 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass156ccbc2022-01-23 12:55:12 -0700231 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200232 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200233 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-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 Takahiro6b95b382019-04-19 12:22:35 +0900241 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900242
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200243 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900244 } else {
245 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400246 }
247
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200248 /* Set load options */
249 if (size) {
250 *load_options = malloc(size);
251 if (!*load_options) {
252 ret = EFI_OUT_OF_RESOURCES;
253 goto error;
254 }
255 memcpy(*load_options, lo.optional_data, size);
256 ret = efi_set_load_options(*handle, size, *load_options);
257 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100258 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200259 }
260
Rob Clark9975fe92017-09-13 18:05:38 -0400261error:
262 free(load_option);
263
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900264 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200265
266unload:
267 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
268 log_err("Unloading image failed\n");
269 free(load_option);
270
271 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400272}
273
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200274/**
275 * efi_bootmgr_load() - try to load from BootNext or BootOrder
276 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900277 * Attempt to load from BootNext or in the order specified by BootOrder
278 * EFI variable, the available load-options, finding and returning
279 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200280 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200281 * @handle: on return handle for the newly installed image
282 * @load_options: load options set on the loaded image protocol
283 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400284 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200285efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400286{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900287 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200288 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400289 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900290 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400291
Rob Clark9975fe92017-09-13 18:05:38 -0400292 bs = systab.boottime;
293 rs = systab.runtime;
294
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900295 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900296 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700297 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200298 &efi_global_variable_guid,
299 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900300 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
301 /* BootNext does exist here */
302 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200303 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900304
305 /* delete BootNext */
Simon Glass156ccbc2022-01-23 12:55:12 -0700306 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200307 &efi_global_variable_guid,
308 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900309
310 /* load BootNext */
311 if (ret == EFI_SUCCESS) {
312 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200313 ret = try_load_entry(bootnext, handle,
314 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900315 if (ret == EFI_SUCCESS)
316 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200317 log_warning(
318 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900319 }
320 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200321 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900322 }
323 }
324
325 /* BootOrder */
Simon Glass156ccbc2022-01-23 12:55:12 -0700326 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100327 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200328 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900329 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400330 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100331 }
Rob Clark9975fe92017-09-13 18:05:38 -0400332
333 num = size / sizeof(uint16_t);
334 for (i = 0; i < num; i++) {
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200335 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200336 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900337 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400338 break;
339 }
340
341 free(bootorder);
342
343error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900344 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400345}