blob: 1e06e60963926ea8ea272fd3344534ca4919261b [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>
14#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clarkc84c1102017-09-13 18:05:38 -040021/*
22 * bootmgr implements the logic of trying to find a payload to boot
23 * based on the BootOrder + BootXXXX variables, and then loading it.
24 *
25 * TODO detecting a special key held (f9?) and displaying a boot menu
26 * like you would get on a PC would be clever.
27 *
28 * TODO if we had a way to write and persist variables after the OS
29 * has started, we'd also want to check OsIndications to see if we
30 * should do normal or recovery boot.
31 */
32
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020033/**
34 * efi_set_load_options() - set the load options of a loaded image
35 *
36 * @handle: the image handle
37 * @load_options_size: size of load options
38 * @load_options: pointer to load options
39 * Return: status code
40 */
41efi_status_t efi_set_load_options(efi_handle_t handle,
42 efi_uintn_t load_options_size,
43 void *load_options)
44{
45 struct efi_loaded_image *loaded_image_info;
46 efi_status_t ret;
47
48 ret = EFI_CALL(systab.boottime->open_protocol(
49 handle,
50 &efi_guid_loaded_image,
51 (void **)&loaded_image_info,
52 efi_root, NULL,
53 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
54 if (ret != EFI_SUCCESS)
55 return EFI_INVALID_PARAMETER;
56
57 loaded_image_info->load_options = load_options;
58 loaded_image_info->load_options_size = load_options_size;
59
60 return EFI_CALL(systab.boottime->close_protocol(handle,
61 &efi_guid_loaded_image,
62 efi_root, NULL));
63}
64
Rob Clarkc84c1102017-09-13 18:05:38 -040065
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020066/**
67 * efi_deserialize_load_option() - parse serialized data
68 *
69 * Parse serialized data describing a load option and transform it to the
70 * efi_load_option structure.
71 *
72 * @lo: pointer to target
73 * @data: serialized data
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020074 * @size: size of the load option, on return size of the optional data
75 * Return: status code
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020076 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020077efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
78 efi_uintn_t *size)
AKASHI Takahirobd237742018-11-05 18:06:41 +090079{
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020080 efi_uintn_t len;
81
82 len = sizeof(u32);
83 if (*size < len + 2 * sizeof(u16))
84 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +090085 lo->attributes = get_unaligned_le32(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020086 data += len;
87 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090088
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020089 len = sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +090090 lo->file_path_length = get_unaligned_le16(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020091 data += len;
92 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090093
AKASHI Takahirobd237742018-11-05 18:06:41 +090094 lo->label = (u16 *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020095 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
96 if (lo->label[len])
97 return EFI_INVALID_PARAMETER;
98 len = (len + 1) * sizeof(u16);
99 if (*size < len)
100 return EFI_INVALID_PARAMETER;
101 data += len;
102 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900103
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200104 len = lo->file_path_length;
105 if (*size < len)
106 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900107 lo->file_path = (struct efi_device_path *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200108 /*
109 * TODO: validate device path. There should be an end node within
110 * the indicated file_path_length.
111 */
112 data += len;
113 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900114
115 lo->optional_data = data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200116
117 return EFI_SUCCESS;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900118}
119
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200120/**
121 * efi_serialize_load_option() - serialize load option
122 *
AKASHI Takahirobd237742018-11-05 18:06:41 +0900123 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200124 *
125 * @data: buffer for serialized data
126 * @lo: load option
127 * Return: size of allocated buffer
Rob Clarkc84c1102017-09-13 18:05:38 -0400128 */
AKASHI Takahirobd237742018-11-05 18:06:41 +0900129unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
Rob Clarkc84c1102017-09-13 18:05:38 -0400130{
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200131 unsigned long label_len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900132 unsigned long size;
133 u8 *p;
134
135 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clarkc84c1102017-09-13 18:05:38 -0400136
AKASHI Takahirobd237742018-11-05 18:06:41 +0900137 /* total size */
138 size = sizeof(lo->attributes);
139 size += sizeof(lo->file_path_length);
140 size += label_len;
141 size += lo->file_path_length;
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200142 if (lo->optional_data)
143 size += (utf8_utf16_strlen((const char *)lo->optional_data)
144 + 1) * sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +0900145 p = malloc(size);
146 if (!p)
147 return 0;
Rob Clarkc84c1102017-09-13 18:05:38 -0400148
AKASHI Takahirobd237742018-11-05 18:06:41 +0900149 /* copy data */
150 *data = p;
151 memcpy(p, &lo->attributes, sizeof(lo->attributes));
152 p += sizeof(lo->attributes);
Rob Clarkc84c1102017-09-13 18:05:38 -0400153
AKASHI Takahirobd237742018-11-05 18:06:41 +0900154 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
155 p += sizeof(lo->file_path_length);
Rob Clarkc84c1102017-09-13 18:05:38 -0400156
AKASHI Takahirobd237742018-11-05 18:06:41 +0900157 memcpy(p, lo->label, label_len);
158 p += label_len;
159
160 memcpy(p, lo->file_path, lo->file_path_length);
161 p += lo->file_path_length;
162
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200163 if (lo->optional_data) {
164 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
165 p += sizeof(u16); /* size of trailing \0 */
166 }
AKASHI Takahirobd237742018-11-05 18:06:41 +0900167 return size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400168}
169
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200170/**
171 * get_var() - get UEFI variable
172 *
173 * It is the caller's duty to free the returned buffer.
174 *
175 * @name: name of variable
176 * @vendor: vendor GUID of variable
177 * @size: size of allocated buffer
178 * Return: buffer with variable data or NULL
179 */
Rob Clarkc84c1102017-09-13 18:05:38 -0400180static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200181 efi_uintn_t *size)
Rob Clarkc84c1102017-09-13 18:05:38 -0400182{
Rob Clarkc84c1102017-09-13 18:05:38 -0400183 efi_status_t ret;
184 void *buf = NULL;
185
186 *size = 0;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200187 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400188 if (ret == EFI_BUFFER_TOO_SMALL) {
189 buf = malloc(*size);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200190 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400191 }
192
193 if (ret != EFI_SUCCESS) {
194 free(buf);
195 *size = 0;
196 return NULL;
197 }
198
199 return buf;
200}
201
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200202/**
203 * try_load_entry() - try to load image for boot option
204 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400205 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200206 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400207 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200208 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200209 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
210 * @handle: on return handle for the newly installed image
211 * @load_options: load options set on the loaded image protocol
212 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400213 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200214static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
215 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400216{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900217 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -0400218 u16 varname[] = L"Boot0000";
219 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900220 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200221 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900222 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400223
224 varname[4] = hexmap[(n & 0xf000) >> 12];
225 varname[5] = hexmap[(n & 0x0f00) >> 8];
226 varname[6] = hexmap[(n & 0x00f0) >> 4];
227 varname[7] = hexmap[(n & 0x000f) >> 0];
228
229 load_option = get_var(varname, &efi_global_variable_guid, &size);
230 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900231 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400232
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200233 ret = efi_deserialize_load_option(&lo, load_option, &size);
234 if (ret != EFI_SUCCESS) {
235 log_warning("Invalid load option for %ls\n", varname);
236 goto error;
237 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400238
239 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900240 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400241
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200242 log_debug("%s: trying to load \"%ls\" from %pD\n",
243 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400244
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900245 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
246 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200247 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200248 log_warning("Loading %ls '%ls' failed\n",
249 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400250 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200251 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400252
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900253 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
254 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200255 ret = efi_set_variable_int(L"BootCurrent",
256 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200257 attributes, sizeof(n), &n, false);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900258 if (ret != EFI_SUCCESS) {
259 if (EFI_CALL(efi_unload_image(*handle))
260 != EFI_SUCCESS)
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200261 log_err("Unloading image failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900262 goto error;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900263 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900264
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200265 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900266 } else {
267 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400268 }
269
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200270 /* Set load options */
271 if (size) {
272 *load_options = malloc(size);
273 if (!*load_options) {
274 ret = EFI_OUT_OF_RESOURCES;
275 goto error;
276 }
277 memcpy(*load_options, lo.optional_data, size);
278 ret = efi_set_load_options(*handle, size, *load_options);
279 } else {
280 load_options = NULL;
281 }
282
Rob Clarkc84c1102017-09-13 18:05:38 -0400283error:
284 free(load_option);
285
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900286 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400287}
288
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200289/**
290 * efi_bootmgr_load() - try to load from BootNext or BootOrder
291 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900292 * Attempt to load from BootNext or in the order specified by BootOrder
293 * EFI variable, the available load-options, finding and returning
294 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200295 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200296 * @handle: on return handle for the newly installed image
297 * @load_options: load options set on the loaded image protocol
298 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400299 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200300efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400301{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900302 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200303 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400304 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900305 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400306
Rob Clarkc84c1102017-09-13 18:05:38 -0400307 bs = systab.boottime;
308 rs = systab.runtime;
309
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900310 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900311 size = sizeof(bootnext);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200312 ret = efi_get_variable_int(L"BootNext",
313 &efi_global_variable_guid,
314 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900315 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
316 /* BootNext does exist here */
317 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200318 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900319
320 /* delete BootNext */
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200321 ret = efi_set_variable_int(L"BootNext",
322 &efi_global_variable_guid,
323 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900324
325 /* load BootNext */
326 if (ret == EFI_SUCCESS) {
327 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200328 ret = try_load_entry(bootnext, handle,
329 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900330 if (ret == EFI_SUCCESS)
331 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200332 log_warning(
333 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900334 }
335 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200336 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900337 }
338 }
339
340 /* BootOrder */
Rob Clarkc84c1102017-09-13 18:05:38 -0400341 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100342 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200343 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900344 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400345 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100346 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400347
348 num = size / sizeof(uint16_t);
349 for (i = 0; i < num; i++) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200350 log_debug("%s trying to load Boot%04X\n", __func__,
351 bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200352 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900353 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400354 break;
355 }
356
357 free(bootorder);
358
359error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900360 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400361}