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 <efi_variable.h>
16 #include <asm/unaligned.h>
18 static const struct efi_boot_services *bs;
19 static const struct efi_runtime_services *rs;
22 * bootmgr implements the logic of trying to find a payload to boot
23 * based on the BootOrder + BootXXXX variables, and then loading it.
25 * TODO detecting a special key held (f9?) and displaying a boot menu
26 * like you would get on a PC would be clever.
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.
34 * efi_set_load_options() - set the load options of a loaded image
36 * @handle: the image handle
37 * @load_options_size: size of load options
38 * @load_options: pointer to load options
41 efi_status_t efi_set_load_options(efi_handle_t handle,
42 efi_uintn_t load_options_size,
45 struct efi_loaded_image *loaded_image_info;
48 ret = EFI_CALL(systab.boottime->open_protocol(
50 &efi_guid_loaded_image,
51 (void **)&loaded_image_info,
53 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
54 if (ret != EFI_SUCCESS)
55 return EFI_INVALID_PARAMETER;
57 loaded_image_info->load_options = load_options;
58 loaded_image_info->load_options_size = load_options_size;
60 return EFI_CALL(systab.boottime->close_protocol(handle,
61 &efi_guid_loaded_image,
67 * efi_deserialize_load_option() - parse serialized data
69 * Parse serialized data describing a load option and transform it to the
70 * efi_load_option structure.
72 * @lo: pointer to target
73 * @data: serialized data
74 * @size: size of the load option, on return size of the optional data
77 efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
83 if (*size < len + 2 * sizeof(u16))
84 return EFI_INVALID_PARAMETER;
85 lo->attributes = get_unaligned_le32(data);
90 lo->file_path_length = get_unaligned_le16(data);
94 lo->label = (u16 *)data;
95 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
97 return EFI_INVALID_PARAMETER;
98 len = (len + 1) * sizeof(u16);
100 return EFI_INVALID_PARAMETER;
104 len = lo->file_path_length;
106 return EFI_INVALID_PARAMETER;
107 lo->file_path = (struct efi_device_path *)data;
108 if (efi_dp_check_length(lo->file_path, len) < 0)
109 return EFI_INVALID_PARAMETER;
113 lo->optional_data = data;
119 * efi_serialize_load_option() - serialize load option
121 * Serialize efi_load_option structure into byte stream for BootXXXX.
123 * @data: buffer for serialized data
125 * Return: size of allocated buffer
127 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
129 unsigned long label_len;
133 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
136 size = sizeof(lo->attributes);
137 size += sizeof(lo->file_path_length);
139 size += lo->file_path_length;
140 if (lo->optional_data)
141 size += (utf8_utf16_strlen((const char *)lo->optional_data)
149 memcpy(p, &lo->attributes, sizeof(lo->attributes));
150 p += sizeof(lo->attributes);
152 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
153 p += sizeof(lo->file_path_length);
155 memcpy(p, lo->label, label_len);
158 memcpy(p, lo->file_path, lo->file_path_length);
159 p += lo->file_path_length;
161 if (lo->optional_data) {
162 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
163 p += sizeof(u16); /* size of trailing \0 */
169 * get_var() - get UEFI variable
171 * It is the caller's duty to free the returned buffer.
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
178 static void *get_var(u16 *name, const efi_guid_t *vendor,
185 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
186 if (ret == EFI_BUFFER_TOO_SMALL) {
188 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
191 if (ret != EFI_SUCCESS) {
201 * try_load_entry() - try to load image for boot option
203 * Attempt to load load-option number 'n', returning device_path and file_path
204 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
205 * and that the specified file to boot exists.
207 * @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
212 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
215 struct efi_load_option lo;
216 u16 varname[] = L"Boot0000";
217 u16 hexmap[] = L"0123456789ABCDEF";
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];
227 load_option = get_var(varname, &efi_global_variable_guid, &size);
229 return EFI_LOAD_ERROR;
231 ret = efi_deserialize_load_option(&lo, load_option, &size);
232 if (ret != EFI_SUCCESS) {
233 log_warning("Invalid load option for %ls\n", varname);
237 if (lo.attributes & LOAD_OPTION_ACTIVE) {
240 log_debug("%s: trying to load \"%ls\" from %pD\n",
241 __func__, lo.label, lo.file_path);
243 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
245 if (ret != EFI_SUCCESS) {
246 log_warning("Loading %ls '%ls' failed\n",
251 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
252 EFI_VARIABLE_RUNTIME_ACCESS;
253 ret = efi_set_variable_int(L"BootCurrent",
254 &efi_global_variable_guid,
255 attributes, sizeof(n), &n, false);
256 if (ret != EFI_SUCCESS) {
257 if (EFI_CALL(efi_unload_image(*handle))
259 log_err("Unloading image failed\n");
263 log_info("Booting: %ls\n", lo.label);
265 ret = EFI_LOAD_ERROR;
268 /* Set load options */
270 *load_options = malloc(size);
271 if (!*load_options) {
272 ret = EFI_OUT_OF_RESOURCES;
275 memcpy(*load_options, lo.optional_data, size);
276 ret = efi_set_load_options(*handle, size, *load_options);
288 * efi_bootmgr_load() - try to load from BootNext or BootOrder
290 * 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.
294 * @handle: on return handle for the newly installed image
295 * @load_options: load options set on the loaded image protocol
296 * Return: status code
298 efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
300 u16 bootnext, *bootorder;
305 bs = systab.boottime;
309 size = sizeof(bootnext);
310 ret = efi_get_variable_int(L"BootNext",
311 &efi_global_variable_guid,
312 NULL, &size, &bootnext, NULL);
313 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
314 /* BootNext does exist here */
315 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
316 log_err("BootNext must be 16-bit integer\n");
318 /* delete BootNext */
319 ret = efi_set_variable_int(L"BootNext",
320 &efi_global_variable_guid,
324 if (ret == EFI_SUCCESS) {
325 if (size == sizeof(u16)) {
326 ret = try_load_entry(bootnext, handle,
328 if (ret == EFI_SUCCESS)
331 "Loading from BootNext failed, falling back to BootOrder\n");
334 log_err("Deleting BootNext failed\n");
339 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
341 log_info("BootOrder not defined\n");
346 num = size / sizeof(uint16_t);
347 for (i = 0; i < num; i++) {
348 log_debug("%s trying to load Boot%04X\n", __func__,
350 ret = try_load_entry(bootorder[i], handle, load_options);
351 if (ret == EFI_SUCCESS)