blob: 93f65905300fa2dae6a8085a78810b11a7a857cf [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/**
AKASHI Takahiro57ad6242022-05-12 11:29:02 +090080 * try_load_from_file_path() - try to load a file
81 *
82 * Given a file media path iterate through a list of handles and try to
83 * to load the file from each of them until the first success.
84 *
85 * @fs_handles: array of handles with the simple file protocol
86 * @num: number of handles in fs_handles
87 * @fp: file path to open
88 * @handle: on return pointer to handle for loaded image
89 * @removable: if true only consider removable media, else only non-removable
90 */
91static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
92 efi_uintn_t num,
93 struct efi_device_path *fp,
94 efi_handle_t *handle,
95 bool removable)
96{
97 struct efi_handler *handler;
98 struct efi_device_path *dp;
99 int i;
100 efi_status_t ret;
101
102 for (i = 0; i < num; i++) {
103 if (removable != efi_disk_is_removable(fs_handles[i]))
104 continue;
105
106 ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
107 &handler);
108 if (ret != EFI_SUCCESS)
109 continue;
110
111 dp = handler->protocol_interface;
112 if (!dp)
113 continue;
114
115 dp = efi_dp_append(dp, fp);
116 if (!dp)
117 continue;
118
119 ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
120 handle));
121 efi_free_pool(dp);
122 if (ret == EFI_SUCCESS)
123 return ret;
124 }
125
126 return EFI_NOT_FOUND;
127}
128
129/**
130 * try_load_from_short_path
131 * @fp: file path
132 * @handle: pointer to handle for newly installed image
133 *
134 * Enumerate all the devices which support file system operations,
135 * prepend its media device path to the file path, @fp, and
136 * try to load the file.
137 * This function should be called when handling a short-form path
138 * which is starting with a file device path.
139 *
140 * Return: status code
141 */
142static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
143 efi_handle_t *handle)
144{
145 efi_handle_t *fs_handles;
146 efi_uintn_t num;
147 efi_status_t ret;
148
149 ret = EFI_CALL(efi_locate_handle_buffer(
150 BY_PROTOCOL,
151 &efi_simple_file_system_protocol_guid,
152 NULL,
153 &num, &fs_handles));
154 if (ret != EFI_SUCCESS)
155 return ret;
156 if (!num)
157 return EFI_NOT_FOUND;
158
159 /* removable media first */
160 ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
161 if (ret == EFI_SUCCESS)
162 goto out;
163
164 /* fixed media */
165 ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
166 if (ret == EFI_SUCCESS)
167 goto out;
168
169out:
170 return ret;
171}
172
173/**
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200174 * try_load_entry() - try to load image for boot option
175 *
Rob Clark9975fe92017-09-13 18:05:38 -0400176 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200177 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -0400178 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200179 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200180 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
181 * @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 +0200185static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
186 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400187{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900188 struct efi_load_option lo;
Heinrich Schuchardt4f419962022-04-25 23:35:01 +0200189 u16 varname[9];
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900190 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200191 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900192 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400193
Heinrich Schuchardt4f419962022-04-25 23:35:01 +0200194 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clark9975fe92017-09-13 18:05:38 -0400195
Ilias Apalodimasf4dc1bc2021-03-27 10:56:07 +0200196 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clark9975fe92017-09-13 18:05:38 -0400197 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900198 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400199
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200200 ret = efi_deserialize_load_option(&lo, load_option, &size);
201 if (ret != EFI_SUCCESS) {
202 log_warning("Invalid load option for %ls\n", varname);
203 goto error;
204 }
Rob Clark9975fe92017-09-13 18:05:38 -0400205
206 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro4e65ca02022-04-28 17:09:39 +0900207 struct efi_device_path *file_path;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900208 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400209
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200210 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
211 lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400212
AKASHI Takahiro57ad6242022-05-12 11:29:02 +0900213 if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
214 /* file_path doesn't contain a device path */
215 ret = try_load_from_short_path(lo.file_path, handle);
216 } else {
217 file_path = expand_media_path(lo.file_path);
218 ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
219 NULL, 0, handle));
220 efi_free_pool(file_path);
221 }
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200222 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200223 log_warning("Loading %ls '%ls' failed\n",
224 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400225 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200226 }
Rob Clark9975fe92017-09-13 18:05:38 -0400227
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900228 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
229 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass156ccbc2022-01-23 12:55:12 -0700230 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200231 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200232 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200233 if (ret != EFI_SUCCESS)
234 goto unload;
235 /* try to register load file2 for initrd's */
236 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
237 ret = efi_initrd_register();
238 if (ret != EFI_SUCCESS)
239 goto unload;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900240 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900241
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200242 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900243 } else {
244 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400245 }
246
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200247 /* Set load options */
248 if (size) {
249 *load_options = malloc(size);
250 if (!*load_options) {
251 ret = EFI_OUT_OF_RESOURCES;
252 goto error;
253 }
254 memcpy(*load_options, lo.optional_data, size);
255 ret = efi_set_load_options(*handle, size, *load_options);
256 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100257 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200258 }
259
Rob Clark9975fe92017-09-13 18:05:38 -0400260error:
261 free(load_option);
262
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900263 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200264
265unload:
266 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
267 log_err("Unloading image failed\n");
268 free(load_option);
269
270 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400271}
272
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200273/**
274 * efi_bootmgr_load() - try to load from BootNext or BootOrder
275 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900276 * Attempt to load from BootNext or in the order specified by BootOrder
277 * EFI variable, the available load-options, finding and returning
278 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200279 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200280 * @handle: on return handle for the newly installed image
281 * @load_options: load options set on the loaded image protocol
282 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400283 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200284efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400285{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900286 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200287 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400288 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900289 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400290
Rob Clark9975fe92017-09-13 18:05:38 -0400291 bs = systab.boottime;
292 rs = systab.runtime;
293
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900294 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900295 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700296 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200297 &efi_global_variable_guid,
298 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900299 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
300 /* BootNext does exist here */
301 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200302 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900303
304 /* delete BootNext */
Simon Glass156ccbc2022-01-23 12:55:12 -0700305 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200306 &efi_global_variable_guid,
307 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900308
309 /* load BootNext */
310 if (ret == EFI_SUCCESS) {
311 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200312 ret = try_load_entry(bootnext, handle,
313 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900314 if (ret == EFI_SUCCESS)
315 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200316 log_warning(
317 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900318 }
319 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200320 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900321 }
322 }
323
324 /* BootOrder */
Simon Glass156ccbc2022-01-23 12:55:12 -0700325 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100326 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200327 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900328 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400329 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100330 }
Rob Clark9975fe92017-09-13 18:05:38 -0400331
332 num = size / sizeof(uint16_t);
333 for (i = 0; i < num; i++) {
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200334 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200335 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900336 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400337 break;
338 }
339
340 free(bootorder);
341
342error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900343 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400344}