blob: 61dc72a23da87af7bbd3c157849119f975189bf6 [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>
14#include <efi_loader.h>
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clark9975fe92017-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 Schuchardt1064d042020-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 Clark9975fe92017-09-13 18:05:38 -040065
Heinrich Schuchardt15621aa2019-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 Schuchardt0e69bcf2020-05-31 22:46:09 +020074 * @size: size of the load option, on return size of the optional data
75 * Return: status code
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020076 */
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020077efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
78 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -040079{
Heinrich Schuchardt0e69bcf2020-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 Takahiro1a82b342018-11-05 18:06:41 +090085 lo->attributes = get_unaligned_le32(data);
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020086 data += len;
87 *size -= len;
Rob Clark9975fe92017-09-13 18:05:38 -040088
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020089 len = sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090090 lo->file_path_length = get_unaligned_le16(data);
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020091 data += len;
92 *size -= len;
Rob Clark9975fe92017-09-13 18:05:38 -040093
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090094 lo->label = (u16 *)data;
Heinrich Schuchardt0e69bcf2020-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;
Rob Clark9975fe92017-09-13 18:05:38 -0400103
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200104 len = lo->file_path_length;
105 if (*size < len)
106 return EFI_INVALID_PARAMETER;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900107 lo->file_path = (struct efi_device_path *)data;
Heinrich Schuchardt15d8f002020-08-23 10:59:17 +0200108 if (efi_dp_check_length(lo->file_path, len) < 0)
109 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200110 data += len;
111 *size -= len;
Rob Clark9975fe92017-09-13 18:05:38 -0400112
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900113 lo->optional_data = data;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200114
115 return EFI_SUCCESS;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900116}
117
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200118/**
119 * efi_serialize_load_option() - serialize load option
120 *
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900121 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200122 *
123 * @data: buffer for serialized data
124 * @lo: load option
125 * Return: size of allocated buffer
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900126 */
127unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
128{
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200129 unsigned long label_len;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900130 unsigned long size;
131 u8 *p;
132
133 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900134
135 /* 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 Schuchardt39a1ff82019-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 Takahiro1a82b342018-11-05 18:06:41 +0900143 p = malloc(size);
144 if (!p)
145 return 0;
146
147 /* copy data */
148 *data = p;
149 memcpy(p, &lo->attributes, sizeof(lo->attributes));
150 p += sizeof(lo->attributes);
151
152 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
153 p += sizeof(lo->file_path_length);
154
155 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 Schuchardt39a1ff82019-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 Takahiro1a82b342018-11-05 18:06:41 +0900165 return size;
Rob Clark9975fe92017-09-13 18:05:38 -0400166}
167
Heinrich Schuchardt15621aa2019-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 Clark9975fe92017-09-13 18:05:38 -0400178static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200179 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -0400180{
Rob Clark9975fe92017-09-13 18:05:38 -0400181 efi_status_t ret;
182 void *buf = NULL;
183
184 *size = 0;
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200185 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clark9975fe92017-09-13 18:05:38 -0400186 if (ret == EFI_BUFFER_TOO_SMALL) {
187 buf = malloc(*size);
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200188 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clark9975fe92017-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 Schuchardt15621aa2019-07-14 13:20:28 +0200200/**
201 * try_load_entry() - try to load image for boot option
202 *
Rob Clark9975fe92017-09-13 18:05:38 -0400203 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200204 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -0400205 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200206 *
Heinrich Schuchardt0ad64002020-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 Clark9975fe92017-09-13 18:05:38 -0400211 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200212static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
213 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400214{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900215 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -0400216 u16 varname[] = L"Boot0000";
217 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900218 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200219 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900220 efi_status_t ret;
Rob Clark9975fe92017-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 Takahiro6b95b382019-04-19 12:22:35 +0900229 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400230
Heinrich Schuchardt0e69bcf2020-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 Clark9975fe92017-09-13 18:05:38 -0400236
237 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900238 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400239
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200240 log_debug("%s: trying to load \"%ls\" from %pD\n",
241 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400242
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900243 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
244 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200245 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200246 log_warning("Loading %ls '%ls' failed\n",
247 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400248 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200249 }
Rob Clark9975fe92017-09-13 18:05:38 -0400250
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900251 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
252 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200253 ret = efi_set_variable_int(L"BootCurrent",
254 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200255 attributes, sizeof(n), &n, false);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900256 if (ret != EFI_SUCCESS) {
257 if (EFI_CALL(efi_unload_image(*handle))
258 != EFI_SUCCESS)
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200259 log_err("Unloading image failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900260 goto error;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900261 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900262
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200263 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900264 } else {
265 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400266 }
267
Heinrich Schuchardt0ad64002020-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 Clark9975fe92017-09-13 18:05:38 -0400281error:
282 free(load_option);
283
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900284 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400285}
286
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200287/**
288 * efi_bootmgr_load() - try to load from BootNext or BootOrder
289 *
AKASHI Takahiro37279ad2019-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 Schuchardt15621aa2019-07-14 13:20:28 +0200293 *
Heinrich Schuchardt0ad64002020-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 Clark9975fe92017-09-13 18:05:38 -0400297 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200298efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400299{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900300 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200301 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400302 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900303 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400304
Rob Clark9975fe92017-09-13 18:05:38 -0400305 bs = systab.boottime;
306 rs = systab.runtime;
307
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900308 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900309 size = sizeof(bootnext);
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200310 ret = efi_get_variable_int(L"BootNext",
311 &efi_global_variable_guid,
312 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-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 Schuchardt7a373e52020-05-31 10:07:31 +0200316 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900317
318 /* delete BootNext */
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200319 ret = efi_set_variable_int(L"BootNext",
320 &efi_global_variable_guid,
321 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900322
323 /* load BootNext */
324 if (ret == EFI_SUCCESS) {
325 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200326 ret = try_load_entry(bootnext, handle,
327 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900328 if (ret == EFI_SUCCESS)
329 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200330 log_warning(
331 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900332 }
333 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200334 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900335 }
336 }
337
338 /* BootOrder */
Rob Clark9975fe92017-09-13 18:05:38 -0400339 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100340 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200341 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900342 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400343 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100344 }
Rob Clark9975fe92017-09-13 18:05:38 -0400345
346 num = size / sizeof(uint16_t);
347 for (i = 0; i < num; i++) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200348 log_debug("%s trying to load Boot%04X\n", __func__,
349 bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200350 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900351 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400352 break;
353 }
354
355 free(bootorder);
356
357error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900358 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400359}