1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
8 #define LOG_CATEGORY LOGC_EFI
14 #include <efi_loader.h>
15 #include <asm/unaligned.h>
17 static const struct efi_boot_services *bs;
18 static const struct efi_runtime_services *rs;
21 * bootmgr implements the logic of trying to find a payload to boot
22 * based on the BootOrder + BootXXXX variables, and then loading it.
24 * TODO detecting a special key held (f9?) and displaying a boot menu
25 * like you would get on a PC would be clever.
27 * TODO if we had a way to write and persist variables after the OS
28 * has started, we'd also want to check OsIndications to see if we
29 * should do normal or recovery boot.
34 * efi_deserialize_load_option() - parse serialized data
36 * Parse serialized data describing a load option and transform it to the
37 * efi_load_option structure.
39 * @lo: pointer to target
40 * @data: serialized data
41 * @size: size of the load option, on return size of the optional data
44 efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
50 if (*size < len + 2 * sizeof(u16))
51 return EFI_INVALID_PARAMETER;
52 lo->attributes = get_unaligned_le32(data);
57 lo->file_path_length = get_unaligned_le16(data);
61 lo->label = (u16 *)data;
62 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
64 return EFI_INVALID_PARAMETER;
65 len = (len + 1) * sizeof(u16);
67 return EFI_INVALID_PARAMETER;
71 len = lo->file_path_length;
73 return EFI_INVALID_PARAMETER;
74 lo->file_path = (struct efi_device_path *)data;
76 * TODO: validate device path. There should be an end node within
77 * the indicated file_path_length.
82 lo->optional_data = data;
88 * efi_serialize_load_option() - serialize load option
90 * Serialize efi_load_option structure into byte stream for BootXXXX.
92 * @data: buffer for serialized data
94 * Return: size of allocated buffer
96 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
98 unsigned long label_len;
102 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
105 size = sizeof(lo->attributes);
106 size += sizeof(lo->file_path_length);
108 size += lo->file_path_length;
109 if (lo->optional_data)
110 size += (utf8_utf16_strlen((const char *)lo->optional_data)
118 memcpy(p, &lo->attributes, sizeof(lo->attributes));
119 p += sizeof(lo->attributes);
121 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
122 p += sizeof(lo->file_path_length);
124 memcpy(p, lo->label, label_len);
127 memcpy(p, lo->file_path, lo->file_path_length);
128 p += lo->file_path_length;
130 if (lo->optional_data) {
131 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
132 p += sizeof(u16); /* size of trailing \0 */
138 * get_var() - get UEFI variable
140 * It is the caller's duty to free the returned buffer.
142 * @name: name of variable
143 * @vendor: vendor GUID of variable
144 * @size: size of allocated buffer
145 * Return: buffer with variable data or NULL
147 static void *get_var(u16 *name, const efi_guid_t *vendor,
150 efi_guid_t *v = (efi_guid_t *)vendor;
155 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
156 if (ret == EFI_BUFFER_TOO_SMALL) {
158 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
161 if (ret != EFI_SUCCESS) {
171 * try_load_entry() - try to load image for boot option
173 * Attempt to load load-option number 'n', returning device_path and file_path
174 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
175 * and that the specified file to boot exists.
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
181 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
183 struct efi_load_option lo;
184 u16 varname[] = L"Boot0000";
185 u16 hexmap[] = L"0123456789ABCDEF";
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];
195 load_option = get_var(varname, &efi_global_variable_guid, &size);
197 return EFI_LOAD_ERROR;
199 ret = efi_deserialize_load_option(&lo, load_option, &size);
200 if (ret != EFI_SUCCESS) {
201 log_warning("Invalid load option for %ls\n", varname);
205 if (lo.attributes & LOAD_OPTION_ACTIVE) {
208 log_debug("%s: trying to load \"%ls\" from %pD\n",
209 __func__, lo.label, lo.file_path);
211 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
213 if (ret != EFI_SUCCESS) {
214 log_warning("Loading %ls '%ls' failed\n",
219 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
220 EFI_VARIABLE_RUNTIME_ACCESS;
222 ret = EFI_CALL(efi_set_variable(
224 (efi_guid_t *)&efi_global_variable_guid,
225 attributes, size, &n));
226 if (ret != EFI_SUCCESS) {
227 if (EFI_CALL(efi_unload_image(*handle))
229 log_err("Unloading image failed\n");
233 log_info("Booting: %ls\n", lo.label);
235 ret = EFI_LOAD_ERROR;
245 * efi_bootmgr_load() - try to load from BootNext or BootOrder
247 * Attempt to load from BootNext or in the order specified by BootOrder
248 * EFI variable, the available load-options, finding and returning
249 * the first one that can be loaded successfully.
251 * @handle: on return handle for the newly installed image
252 * Return: status code
254 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
256 u16 bootnext, *bootorder;
261 bs = systab.boottime;
266 size = sizeof(bootnext);
267 ret = EFI_CALL(efi_get_variable(L"BootNext",
268 (efi_guid_t *)&efi_global_variable_guid,
269 NULL, &size, &bootnext));
270 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
271 /* BootNext does exist here */
272 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
273 log_err("BootNext must be 16-bit integer\n");
275 /* delete BootNext */
276 ret = EFI_CALL(efi_set_variable(
278 (efi_guid_t *)&efi_global_variable_guid,
279 EFI_VARIABLE_NON_VOLATILE, 0,
283 if (ret == EFI_SUCCESS) {
284 if (size == sizeof(u16)) {
285 ret = try_load_entry(bootnext, handle);
286 if (ret == EFI_SUCCESS)
289 "Loading from BootNext failed, falling back to BootOrder\n");
292 log_err("Deleting BootNext failed\n");
297 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
299 log_info("BootOrder not defined\n");
304 num = size / sizeof(uint16_t);
305 for (i = 0; i < num; i++) {
306 log_debug("%s trying to load Boot%04X\n", __func__,
308 ret = try_load_entry(bootorder[i], handle);
309 if (ret == EFI_SUCCESS)