blob: 61dc72a23da87af7bbd3c157849119f975189bf6 [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 Schuchardt55a61122020-08-23 10:59:17 +0200108 if (efi_dp_check_length(lo->file_path, len) < 0)
109 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200110 data += len;
111 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900112
113 lo->optional_data = data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200114
115 return EFI_SUCCESS;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900116}
117
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200118/**
119 * efi_serialize_load_option() - serialize load option
120 *
AKASHI Takahirobd237742018-11-05 18:06:41 +0900121 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200122 *
123 * @data: buffer for serialized data
124 * @lo: load option
125 * Return: size of allocated buffer
Rob Clarkc84c1102017-09-13 18:05:38 -0400126 */
AKASHI Takahirobd237742018-11-05 18:06:41 +0900127unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
Rob Clarkc84c1102017-09-13 18:05:38 -0400128{
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200129 unsigned long label_len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900130 unsigned long size;
131 u8 *p;
132
133 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clarkc84c1102017-09-13 18:05:38 -0400134
AKASHI Takahirobd237742018-11-05 18:06:41 +0900135 /* total size */
136 size = sizeof(lo->attributes);
137 size += sizeof(lo->file_path_length);
138 size += label_len;
139 size += lo->file_path_length;
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200140 if (lo->optional_data)
141 size += (utf8_utf16_strlen((const char *)lo->optional_data)
142 + 1) * sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +0900143 p = malloc(size);
144 if (!p)
145 return 0;
Rob Clarkc84c1102017-09-13 18:05:38 -0400146
AKASHI Takahirobd237742018-11-05 18:06:41 +0900147 /* copy data */
148 *data = p;
149 memcpy(p, &lo->attributes, sizeof(lo->attributes));
150 p += sizeof(lo->attributes);
Rob Clarkc84c1102017-09-13 18:05:38 -0400151
AKASHI Takahirobd237742018-11-05 18:06:41 +0900152 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
153 p += sizeof(lo->file_path_length);
Rob Clarkc84c1102017-09-13 18:05:38 -0400154
AKASHI Takahirobd237742018-11-05 18:06:41 +0900155 memcpy(p, lo->label, label_len);
156 p += label_len;
157
158 memcpy(p, lo->file_path, lo->file_path_length);
159 p += lo->file_path_length;
160
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200161 if (lo->optional_data) {
162 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
163 p += sizeof(u16); /* size of trailing \0 */
164 }
AKASHI Takahirobd237742018-11-05 18:06:41 +0900165 return size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400166}
167
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200168/**
169 * get_var() - get UEFI variable
170 *
171 * It is the caller's duty to free the returned buffer.
172 *
173 * @name: name of variable
174 * @vendor: vendor GUID of variable
175 * @size: size of allocated buffer
176 * Return: buffer with variable data or NULL
177 */
Rob Clarkc84c1102017-09-13 18:05:38 -0400178static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200179 efi_uintn_t *size)
Rob Clarkc84c1102017-09-13 18:05:38 -0400180{
Rob Clarkc84c1102017-09-13 18:05:38 -0400181 efi_status_t ret;
182 void *buf = NULL;
183
184 *size = 0;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200185 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400186 if (ret == EFI_BUFFER_TOO_SMALL) {
187 buf = malloc(*size);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200188 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400189 }
190
191 if (ret != EFI_SUCCESS) {
192 free(buf);
193 *size = 0;
194 return NULL;
195 }
196
197 return buf;
198}
199
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200200/**
201 * try_load_entry() - try to load image for boot option
202 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400203 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200204 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400205 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200206 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200207 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
208 * @handle: on return handle for the newly installed image
209 * @load_options: load options set on the loaded image protocol
210 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400211 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200212static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
213 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400214{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900215 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -0400216 u16 varname[] = L"Boot0000";
217 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900218 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200219 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900220 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400221
222 varname[4] = hexmap[(n & 0xf000) >> 12];
223 varname[5] = hexmap[(n & 0x0f00) >> 8];
224 varname[6] = hexmap[(n & 0x00f0) >> 4];
225 varname[7] = hexmap[(n & 0x000f) >> 0];
226
227 load_option = get_var(varname, &efi_global_variable_guid, &size);
228 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900229 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400230
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200231 ret = efi_deserialize_load_option(&lo, load_option, &size);
232 if (ret != EFI_SUCCESS) {
233 log_warning("Invalid load option for %ls\n", varname);
234 goto error;
235 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400236
237 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900238 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400239
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200240 log_debug("%s: trying to load \"%ls\" from %pD\n",
241 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400242
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900243 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
244 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200245 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200246 log_warning("Loading %ls '%ls' failed\n",
247 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400248 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200249 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400250
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900251 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
252 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200253 ret = efi_set_variable_int(L"BootCurrent",
254 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200255 attributes, sizeof(n), &n, false);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900256 if (ret != EFI_SUCCESS) {
257 if (EFI_CALL(efi_unload_image(*handle))
258 != EFI_SUCCESS)
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200259 log_err("Unloading image failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900260 goto error;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900261 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900262
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200263 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900264 } else {
265 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400266 }
267
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200268 /* Set load options */
269 if (size) {
270 *load_options = malloc(size);
271 if (!*load_options) {
272 ret = EFI_OUT_OF_RESOURCES;
273 goto error;
274 }
275 memcpy(*load_options, lo.optional_data, size);
276 ret = efi_set_load_options(*handle, size, *load_options);
277 } else {
278 load_options = NULL;
279 }
280
Rob Clarkc84c1102017-09-13 18:05:38 -0400281error:
282 free(load_option);
283
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900284 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400285}
286
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200287/**
288 * efi_bootmgr_load() - try to load from BootNext or BootOrder
289 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900290 * Attempt to load from BootNext or in the order specified by BootOrder
291 * EFI variable, the available load-options, finding and returning
292 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200293 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200294 * @handle: on return handle for the newly installed image
295 * @load_options: load options set on the loaded image protocol
296 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400297 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200298efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400299{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900300 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200301 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400302 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900303 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400304
Rob Clarkc84c1102017-09-13 18:05:38 -0400305 bs = systab.boottime;
306 rs = systab.runtime;
307
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900308 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900309 size = sizeof(bootnext);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200310 ret = efi_get_variable_int(L"BootNext",
311 &efi_global_variable_guid,
312 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900313 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
314 /* BootNext does exist here */
315 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200316 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900317
318 /* delete BootNext */
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200319 ret = efi_set_variable_int(L"BootNext",
320 &efi_global_variable_guid,
321 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900322
323 /* load BootNext */
324 if (ret == EFI_SUCCESS) {
325 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200326 ret = try_load_entry(bootnext, handle,
327 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900328 if (ret == EFI_SUCCESS)
329 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200330 log_warning(
331 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900332 }
333 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200334 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900335 }
336 }
337
338 /* BootOrder */
Rob Clarkc84c1102017-09-13 18:05:38 -0400339 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100340 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200341 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900342 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400343 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100344 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400345
346 num = size / sizeof(uint16_t);
347 for (i = 0; i < num; i++) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200348 log_debug("%s trying to load Boot%04X\n", __func__,
349 bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200350 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900351 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400352 break;
353 }
354
355 free(bootorder);
356
357error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900358 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400359}