blob: b112f5d81efdb99acbf8dc8d5ca5905cecd22fe6 [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
8#include <common.h>
9#include <charset.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Rob Clark9975fe92017-09-13 18:05:38 -040011#include <malloc.h>
12#include <efi_loader.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090013#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040014
15static const struct efi_boot_services *bs;
16static const struct efi_runtime_services *rs;
17
Rob Clark9975fe92017-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 Schuchardt15621aa2019-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
39 */
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090040void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
Rob Clark9975fe92017-09-13 18:05:38 -040041{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090042 lo->attributes = get_unaligned_le32(data);
43 data += sizeof(u32);
Rob Clark9975fe92017-09-13 18:05:38 -040044
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090045 lo->file_path_length = get_unaligned_le16(data);
46 data += sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040047
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090048 /* FIXME */
49 lo->label = (u16 *)data;
50 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040051
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090052 /* FIXME */
53 lo->file_path = (struct efi_device_path *)data;
54 data += lo->file_path_length;
Rob Clark9975fe92017-09-13 18:05:38 -040055
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090056 lo->optional_data = data;
57}
58
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020059/**
60 * efi_serialize_load_option() - serialize load option
61 *
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090062 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020063 *
64 * @data: buffer for serialized data
65 * @lo: load option
66 * Return: size of allocated buffer
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090067 */
68unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
69{
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020070 unsigned long label_len;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090071 unsigned long size;
72 u8 *p;
73
74 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090075
76 /* total size */
77 size = sizeof(lo->attributes);
78 size += sizeof(lo->file_path_length);
79 size += label_len;
80 size += lo->file_path_length;
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020081 if (lo->optional_data)
82 size += (utf8_utf16_strlen((const char *)lo->optional_data)
83 + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090084 p = malloc(size);
85 if (!p)
86 return 0;
87
88 /* copy data */
89 *data = p;
90 memcpy(p, &lo->attributes, sizeof(lo->attributes));
91 p += sizeof(lo->attributes);
92
93 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
94 p += sizeof(lo->file_path_length);
95
96 memcpy(p, lo->label, label_len);
97 p += label_len;
98
99 memcpy(p, lo->file_path, lo->file_path_length);
100 p += lo->file_path_length;
101
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200102 if (lo->optional_data) {
103 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
104 p += sizeof(u16); /* size of trailing \0 */
105 }
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900106 return size;
Rob Clark9975fe92017-09-13 18:05:38 -0400107}
108
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200109/**
110 * get_var() - get UEFI variable
111 *
112 * It is the caller's duty to free the returned buffer.
113 *
114 * @name: name of variable
115 * @vendor: vendor GUID of variable
116 * @size: size of allocated buffer
117 * Return: buffer with variable data or NULL
118 */
Rob Clark9975fe92017-09-13 18:05:38 -0400119static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200120 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -0400121{
122 efi_guid_t *v = (efi_guid_t *)vendor;
123 efi_status_t ret;
124 void *buf = NULL;
125
126 *size = 0;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200127 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400128 if (ret == EFI_BUFFER_TOO_SMALL) {
129 buf = malloc(*size);
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200130 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400131 }
132
133 if (ret != EFI_SUCCESS) {
134 free(buf);
135 *size = 0;
136 return NULL;
137 }
138
139 return buf;
140}
141
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200142/**
143 * try_load_entry() - try to load image for boot option
144 *
Rob Clark9975fe92017-09-13 18:05:38 -0400145 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200146 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -0400147 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200148 *
149 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
150 * @handle: on return handle for the newly installed image
151 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400152 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900153static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400154{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900155 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -0400156 u16 varname[] = L"Boot0000";
157 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900158 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200159 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900160 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400161
162 varname[4] = hexmap[(n & 0xf000) >> 12];
163 varname[5] = hexmap[(n & 0x0f00) >> 8];
164 varname[6] = hexmap[(n & 0x00f0) >> 4];
165 varname[7] = hexmap[(n & 0x000f) >> 0];
166
167 load_option = get_var(varname, &efi_global_variable_guid, &size);
168 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900169 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400170
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900171 efi_deserialize_load_option(&lo, load_option);
Rob Clark9975fe92017-09-13 18:05:38 -0400172
173 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900174 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400175
Heinrich Schuchardt4a8b5e72018-01-26 07:20:54 +0100176 debug("%s: trying to load \"%ls\" from %pD\n",
177 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400178
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900179 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
180 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200181 if (ret != EFI_SUCCESS) {
182 printf("Loading from Boot%04X '%ls' failed\n", n,
183 lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400184 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200185 }
Rob Clark9975fe92017-09-13 18:05:38 -0400186
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900187 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
188 EFI_VARIABLE_RUNTIME_ACCESS;
189 size = sizeof(n);
190 ret = EFI_CALL(efi_set_variable(
191 L"BootCurrent",
192 (efi_guid_t *)&efi_global_variable_guid,
193 attributes, size, &n));
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900194 if (ret != EFI_SUCCESS) {
195 if (EFI_CALL(efi_unload_image(*handle))
196 != EFI_SUCCESS)
197 printf("Unloading image failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900198 goto error;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900199 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900200
Rob Clark9975fe92017-09-13 18:05:38 -0400201 printf("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900202 } else {
203 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400204 }
205
206error:
207 free(load_option);
208
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900209 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400210}
211
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200212/**
213 * efi_bootmgr_load() - try to load from BootNext or BootOrder
214 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900215 * Attempt to load from BootNext or in the order specified by BootOrder
216 * EFI variable, the available load-options, finding and returning
217 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200218 *
219 * @handle: on return handle for the newly installed image
220 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400221 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900222efi_status_t efi_bootmgr_load(efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400223{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900224 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200225 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400226 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900227 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400228
Rob Clark9975fe92017-09-13 18:05:38 -0400229 bs = systab.boottime;
230 rs = systab.runtime;
231
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900232 /* BootNext */
233 bootnext = 0;
234 size = sizeof(bootnext);
235 ret = EFI_CALL(efi_get_variable(L"BootNext",
236 (efi_guid_t *)&efi_global_variable_guid,
237 NULL, &size, &bootnext));
238 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
239 /* BootNext does exist here */
240 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
241 printf("BootNext must be 16-bit integer\n");
242
243 /* delete BootNext */
244 ret = EFI_CALL(efi_set_variable(
245 L"BootNext",
246 (efi_guid_t *)&efi_global_variable_guid,
AKASHI Takahiro366161c2019-06-04 15:52:09 +0900247 EFI_VARIABLE_NON_VOLATILE, 0,
248 &bootnext));
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900249
250 /* load BootNext */
251 if (ret == EFI_SUCCESS) {
252 if (size == sizeof(u16)) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900253 ret = try_load_entry(bootnext, handle);
254 if (ret == EFI_SUCCESS)
255 return ret;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200256 printf("Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900257 }
258 } else {
259 printf("Deleting BootNext failed\n");
260 }
261 }
262
263 /* BootOrder */
Rob Clark9975fe92017-09-13 18:05:38 -0400264 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100265 if (!bootorder) {
266 printf("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900267 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400268 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100269 }
Rob Clark9975fe92017-09-13 18:05:38 -0400270
271 num = size / sizeof(uint16_t);
272 for (i = 0; i < num; i++) {
273 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900274 ret = try_load_entry(bootorder[i], handle);
275 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400276 break;
277 }
278
279 free(bootorder);
280
281error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900282 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400283}