blob: e03198b57a87ecf3946a3bd17050cf601540c2f6 [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
33
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020034/**
35 * efi_deserialize_load_option() - parse serialized data
36 *
37 * Parse serialized data describing a load option and transform it to the
38 * efi_load_option structure.
39 *
40 * @lo: pointer to target
41 * @data: serialized data
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020042 * @size: size of the load option, on return size of the optional data
43 * Return: status code
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020044 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020045efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
46 efi_uintn_t *size)
AKASHI Takahirobd237742018-11-05 18:06:41 +090047{
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020048 efi_uintn_t len;
49
50 len = sizeof(u32);
51 if (*size < len + 2 * sizeof(u16))
52 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +090053 lo->attributes = get_unaligned_le32(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020054 data += len;
55 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090056
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020057 len = sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +090058 lo->file_path_length = get_unaligned_le16(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020059 data += len;
60 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090061
AKASHI Takahirobd237742018-11-05 18:06:41 +090062 lo->label = (u16 *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020063 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
64 if (lo->label[len])
65 return EFI_INVALID_PARAMETER;
66 len = (len + 1) * sizeof(u16);
67 if (*size < len)
68 return EFI_INVALID_PARAMETER;
69 data += len;
70 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090071
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020072 len = lo->file_path_length;
73 if (*size < len)
74 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +090075 lo->file_path = (struct efi_device_path *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020076 /*
77 * TODO: validate device path. There should be an end node within
78 * the indicated file_path_length.
79 */
80 data += len;
81 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090082
83 lo->optional_data = data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020084
85 return EFI_SUCCESS;
AKASHI Takahirobd237742018-11-05 18:06:41 +090086}
87
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020088/**
89 * efi_serialize_load_option() - serialize load option
90 *
AKASHI Takahirobd237742018-11-05 18:06:41 +090091 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020092 *
93 * @data: buffer for serialized data
94 * @lo: load option
95 * Return: size of allocated buffer
Rob Clarkc84c1102017-09-13 18:05:38 -040096 */
AKASHI Takahirobd237742018-11-05 18:06:41 +090097unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
Rob Clarkc84c1102017-09-13 18:05:38 -040098{
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +020099 unsigned long label_len;
AKASHI Takahirobd237742018-11-05 18:06:41 +0900100 unsigned long size;
101 u8 *p;
102
103 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clarkc84c1102017-09-13 18:05:38 -0400104
AKASHI Takahirobd237742018-11-05 18:06:41 +0900105 /* total size */
106 size = sizeof(lo->attributes);
107 size += sizeof(lo->file_path_length);
108 size += label_len;
109 size += lo->file_path_length;
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200110 if (lo->optional_data)
111 size += (utf8_utf16_strlen((const char *)lo->optional_data)
112 + 1) * sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +0900113 p = malloc(size);
114 if (!p)
115 return 0;
Rob Clarkc84c1102017-09-13 18:05:38 -0400116
AKASHI Takahirobd237742018-11-05 18:06:41 +0900117 /* copy data */
118 *data = p;
119 memcpy(p, &lo->attributes, sizeof(lo->attributes));
120 p += sizeof(lo->attributes);
Rob Clarkc84c1102017-09-13 18:05:38 -0400121
AKASHI Takahirobd237742018-11-05 18:06:41 +0900122 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
123 p += sizeof(lo->file_path_length);
Rob Clarkc84c1102017-09-13 18:05:38 -0400124
AKASHI Takahirobd237742018-11-05 18:06:41 +0900125 memcpy(p, lo->label, label_len);
126 p += label_len;
127
128 memcpy(p, lo->file_path, lo->file_path_length);
129 p += lo->file_path_length;
130
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200131 if (lo->optional_data) {
132 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
133 p += sizeof(u16); /* size of trailing \0 */
134 }
AKASHI Takahirobd237742018-11-05 18:06:41 +0900135 return size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400136}
137
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200138/**
139 * get_var() - get UEFI variable
140 *
141 * It is the caller's duty to free the returned buffer.
142 *
143 * @name: name of variable
144 * @vendor: vendor GUID of variable
145 * @size: size of allocated buffer
146 * Return: buffer with variable data or NULL
147 */
Rob Clarkc84c1102017-09-13 18:05:38 -0400148static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200149 efi_uintn_t *size)
Rob Clarkc84c1102017-09-13 18:05:38 -0400150{
Rob Clarkc84c1102017-09-13 18:05:38 -0400151 efi_status_t ret;
152 void *buf = NULL;
153
154 *size = 0;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200155 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400156 if (ret == EFI_BUFFER_TOO_SMALL) {
157 buf = malloc(*size);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200158 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -0400159 }
160
161 if (ret != EFI_SUCCESS) {
162 free(buf);
163 *size = 0;
164 return NULL;
165 }
166
167 return buf;
168}
169
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200170/**
171 * try_load_entry() - try to load image for boot option
172 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400173 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200174 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400175 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200176 *
177 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
178 * @handle: on return handle for the newly installed image
179 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400180 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900181static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
Rob Clarkc84c1102017-09-13 18:05:38 -0400182{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900183 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -0400184 u16 varname[] = L"Boot0000";
185 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900186 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200187 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900188 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400189
190 varname[4] = hexmap[(n & 0xf000) >> 12];
191 varname[5] = hexmap[(n & 0x0f00) >> 8];
192 varname[6] = hexmap[(n & 0x00f0) >> 4];
193 varname[7] = hexmap[(n & 0x000f) >> 0];
194
195 load_option = get_var(varname, &efi_global_variable_guid, &size);
196 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900197 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400198
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200199 ret = efi_deserialize_load_option(&lo, load_option, &size);
200 if (ret != EFI_SUCCESS) {
201 log_warning("Invalid load option for %ls\n", varname);
202 goto error;
203 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400204
205 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900206 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400207
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200208 log_debug("%s: trying to load \"%ls\" from %pD\n",
209 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400210
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900211 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
212 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200213 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200214 log_warning("Loading %ls '%ls' failed\n",
215 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400216 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200217 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400218
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900219 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
220 EFI_VARIABLE_RUNTIME_ACCESS;
221 size = sizeof(n);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200222 ret = efi_set_variable_int(L"BootCurrent",
223 &efi_global_variable_guid,
224 attributes, size, &n, false);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900225 if (ret != EFI_SUCCESS) {
226 if (EFI_CALL(efi_unload_image(*handle))
227 != EFI_SUCCESS)
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200228 log_err("Unloading image failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900229 goto error;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900230 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900231
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200232 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900233 } else {
234 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400235 }
236
237error:
238 free(load_option);
239
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900240 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400241}
242
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200243/**
244 * efi_bootmgr_load() - try to load from BootNext or BootOrder
245 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900246 * Attempt to load from BootNext or in the order specified by BootOrder
247 * EFI variable, the available load-options, finding and returning
248 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200249 *
250 * @handle: on return handle for the newly installed image
251 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400252 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900253efi_status_t efi_bootmgr_load(efi_handle_t *handle)
Rob Clarkc84c1102017-09-13 18:05:38 -0400254{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900255 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200256 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400257 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900258 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400259
Rob Clarkc84c1102017-09-13 18:05:38 -0400260 bs = systab.boottime;
261 rs = systab.runtime;
262
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900263 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900264 size = sizeof(bootnext);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200265 ret = efi_get_variable_int(L"BootNext",
266 &efi_global_variable_guid,
267 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900268 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
269 /* BootNext does exist here */
270 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200271 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900272
273 /* delete BootNext */
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200274 ret = efi_set_variable_int(L"BootNext",
275 &efi_global_variable_guid,
276 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900277
278 /* load BootNext */
279 if (ret == EFI_SUCCESS) {
280 if (size == sizeof(u16)) {
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900281 ret = try_load_entry(bootnext, handle);
282 if (ret == EFI_SUCCESS)
283 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200284 log_warning(
285 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900286 }
287 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200288 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900289 }
290 }
291
292 /* BootOrder */
Rob Clarkc84c1102017-09-13 18:05:38 -0400293 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100294 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200295 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900296 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400297 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100298 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400299
300 num = size / sizeof(uint16_t);
301 for (i = 0; i < num; i++) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200302 log_debug("%s trying to load Boot%04X\n", __func__,
303 bootorder[i]);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900304 ret = try_load_entry(bootorder[i], handle);
305 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400306 break;
307 }
308
309 free(bootorder);
310
311error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900312 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400313}