blob: e144b3e7f43b13ea63192586e8570880d45395c3 [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
8#include <common.h>
9#include <charset.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040011#include <malloc.h>
12#include <efi_loader.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090013#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040014
15static const struct efi_boot_services *bs;
16static const struct efi_runtime_services *rs;
17
Rob Clarkc84c1102017-09-13 18:05:38 -040018/*
19 * bootmgr implements the logic of trying to find a payload to boot
20 * based on the BootOrder + BootXXXX variables, and then loading it.
21 *
22 * TODO detecting a special key held (f9?) and displaying a boot menu
23 * like you would get on a PC would be clever.
24 *
25 * TODO if we had a way to write and persist variables after the OS
26 * has started, we'd also want to check OsIndications to see if we
27 * should do normal or recovery boot.
28 */
29
30
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020031/**
32 * efi_deserialize_load_option() - parse serialized data
33 *
34 * Parse serialized data describing a load option and transform it to the
35 * efi_load_option structure.
36 *
37 * @lo: pointer to target
38 * @data: serialized data
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020039 * @size: size of the load option, on return size of the optional data
40 * Return: status code
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020041 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020042efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
43 efi_uintn_t *size)
AKASHI Takahirobd237742018-11-05 18:06:41 +090044{
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020045 efi_uintn_t len;
46
47 len = sizeof(u32);
48 if (*size < len + 2 * sizeof(u16))
49 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +090050 lo->attributes = get_unaligned_le32(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020051 data += len;
52 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090053
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020054 len = sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +090055 lo->file_path_length = get_unaligned_le16(data);
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020056 data += len;
57 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090058
AKASHI Takahirobd237742018-11-05 18:06:41 +090059 lo->label = (u16 *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020060 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
61 if (lo->label[len])
62 return EFI_INVALID_PARAMETER;
63 len = (len + 1) * sizeof(u16);
64 if (*size < len)
65 return EFI_INVALID_PARAMETER;
66 data += len;
67 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090068
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020069 len = lo->file_path_length;
70 if (*size < len)
71 return EFI_INVALID_PARAMETER;
AKASHI Takahirobd237742018-11-05 18:06:41 +090072 lo->file_path = (struct efi_device_path *)data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020073 /*
74 * TODO: validate device path. There should be an end node within
75 * the indicated file_path_length.
76 */
77 data += len;
78 *size -= len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090079
80 lo->optional_data = data;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020081
82 return EFI_SUCCESS;
AKASHI Takahirobd237742018-11-05 18:06:41 +090083}
84
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020085/**
86 * efi_serialize_load_option() - serialize load option
87 *
AKASHI Takahirobd237742018-11-05 18:06:41 +090088 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020089 *
90 * @data: buffer for serialized data
91 * @lo: load option
92 * Return: size of allocated buffer
Rob Clarkc84c1102017-09-13 18:05:38 -040093 */
AKASHI Takahirobd237742018-11-05 18:06:41 +090094unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
Rob Clarkc84c1102017-09-13 18:05:38 -040095{
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +020096 unsigned long label_len;
AKASHI Takahirobd237742018-11-05 18:06:41 +090097 unsigned long size;
98 u8 *p;
99
100 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clarkc84c1102017-09-13 18:05:38 -0400101
AKASHI Takahirobd237742018-11-05 18:06:41 +0900102 /* total size */
103 size = sizeof(lo->attributes);
104 size += sizeof(lo->file_path_length);
105 size += label_len;
106 size += lo->file_path_length;
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200107 if (lo->optional_data)
108 size += (utf8_utf16_strlen((const char *)lo->optional_data)
109 + 1) * sizeof(u16);
AKASHI Takahirobd237742018-11-05 18:06:41 +0900110 p = malloc(size);
111 if (!p)
112 return 0;
Rob Clarkc84c1102017-09-13 18:05:38 -0400113
AKASHI Takahirobd237742018-11-05 18:06:41 +0900114 /* copy data */
115 *data = p;
116 memcpy(p, &lo->attributes, sizeof(lo->attributes));
117 p += sizeof(lo->attributes);
Rob Clarkc84c1102017-09-13 18:05:38 -0400118
AKASHI Takahirobd237742018-11-05 18:06:41 +0900119 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
120 p += sizeof(lo->file_path_length);
Rob Clarkc84c1102017-09-13 18:05:38 -0400121
AKASHI Takahirobd237742018-11-05 18:06:41 +0900122 memcpy(p, lo->label, label_len);
123 p += label_len;
124
125 memcpy(p, lo->file_path, lo->file_path_length);
126 p += lo->file_path_length;
127
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200128 if (lo->optional_data) {
129 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
130 p += sizeof(u16); /* size of trailing \0 */
131 }
AKASHI Takahirobd237742018-11-05 18:06:41 +0900132 return size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400133}
134
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200135/**
136 * get_var() - get UEFI variable
137 *
138 * It is the caller's duty to free the returned buffer.
139 *
140 * @name: name of variable
141 * @vendor: vendor GUID of variable
142 * @size: size of allocated buffer
143 * Return: buffer with variable data or NULL
144 */
Rob Clarkc84c1102017-09-13 18:05:38 -0400145static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200146 efi_uintn_t *size)
Rob Clarkc84c1102017-09-13 18:05:38 -0400147{
148 efi_guid_t *v = (efi_guid_t *)vendor;
149 efi_status_t ret;
150 void *buf = NULL;
151
152 *size = 0;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200153 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clarkc84c1102017-09-13 18:05:38 -0400154 if (ret == EFI_BUFFER_TOO_SMALL) {
155 buf = malloc(*size);
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200156 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clarkc84c1102017-09-13 18:05:38 -0400157 }
158
159 if (ret != EFI_SUCCESS) {
160 free(buf);
161 *size = 0;
162 return NULL;
163 }
164
165 return buf;
166}
167
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200168/**
169 * try_load_entry() - try to load image for boot option
170 *
Rob Clarkc84c1102017-09-13 18:05:38 -0400171 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200172 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -0400173 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200174 *
175 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
176 * @handle: on return handle for the newly installed image
177 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400178 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900179static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
Rob Clarkc84c1102017-09-13 18:05:38 -0400180{
AKASHI Takahirobd237742018-11-05 18:06:41 +0900181 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -0400182 u16 varname[] = L"Boot0000";
183 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900184 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200185 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900186 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400187
188 varname[4] = hexmap[(n & 0xf000) >> 12];
189 varname[5] = hexmap[(n & 0x0f00) >> 8];
190 varname[6] = hexmap[(n & 0x00f0) >> 4];
191 varname[7] = hexmap[(n & 0x000f) >> 0];
192
193 load_option = get_var(varname, &efi_global_variable_guid, &size);
194 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900195 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400196
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200197 ret = efi_deserialize_load_option(&lo, load_option, &size);
198 if (ret != EFI_SUCCESS) {
199 log_warning("Invalid load option for %ls\n", varname);
200 goto error;
201 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400202
203 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900204 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400205
Heinrich Schuchardt253aa642018-01-26 07:20:54 +0100206 debug("%s: trying to load \"%ls\" from %pD\n",
207 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400208
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900209 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
210 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200211 if (ret != EFI_SUCCESS) {
212 printf("Loading from Boot%04X '%ls' failed\n", n,
213 lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400214 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200215 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400216
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900217 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
218 EFI_VARIABLE_RUNTIME_ACCESS;
219 size = sizeof(n);
220 ret = EFI_CALL(efi_set_variable(
221 L"BootCurrent",
222 (efi_guid_t *)&efi_global_variable_guid,
223 attributes, size, &n));
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900224 if (ret != EFI_SUCCESS) {
225 if (EFI_CALL(efi_unload_image(*handle))
226 != EFI_SUCCESS)
227 printf("Unloading image failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900228 goto error;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900229 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900230
Rob Clarkc84c1102017-09-13 18:05:38 -0400231 printf("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900232 } else {
233 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400234 }
235
236error:
237 free(load_option);
238
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900239 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400240}
241
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200242/**
243 * efi_bootmgr_load() - try to load from BootNext or BootOrder
244 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900245 * Attempt to load from BootNext or in the order specified by BootOrder
246 * EFI variable, the available load-options, finding and returning
247 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200248 *
249 * @handle: on return handle for the newly installed image
250 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400251 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900252efi_status_t efi_bootmgr_load(efi_handle_t *handle)
Rob Clarkc84c1102017-09-13 18:05:38 -0400253{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900254 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200255 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400256 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900257 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400258
Rob Clarkc84c1102017-09-13 18:05:38 -0400259 bs = systab.boottime;
260 rs = systab.runtime;
261
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900262 /* BootNext */
263 bootnext = 0;
264 size = sizeof(bootnext);
265 ret = EFI_CALL(efi_get_variable(L"BootNext",
266 (efi_guid_t *)&efi_global_variable_guid,
267 NULL, &size, &bootnext));
268 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
269 /* BootNext does exist here */
270 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
271 printf("BootNext must be 16-bit integer\n");
272
273 /* delete BootNext */
274 ret = EFI_CALL(efi_set_variable(
275 L"BootNext",
276 (efi_guid_t *)&efi_global_variable_guid,
AKASHI Takahirofd93b312019-06-04 15:52:09 +0900277 EFI_VARIABLE_NON_VOLATILE, 0,
278 &bootnext));
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900279
280 /* load BootNext */
281 if (ret == EFI_SUCCESS) {
282 if (size == sizeof(u16)) {
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900283 ret = try_load_entry(bootnext, handle);
284 if (ret == EFI_SUCCESS)
285 return ret;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200286 printf("Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900287 }
288 } else {
289 printf("Deleting BootNext failed\n");
290 }
291 }
292
293 /* BootOrder */
Rob Clarkc84c1102017-09-13 18:05:38 -0400294 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100295 if (!bootorder) {
296 printf("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900297 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400298 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100299 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400300
301 num = size / sizeof(uint16_t);
302 for (i = 0; i < num; i++) {
303 debug("%s: trying to load Boot%04X\n", __func__, 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}