1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
12 #include <efi_loader.h>
13 #include <asm/unaligned.h>
15 static const struct efi_boot_services *bs;
16 static const struct efi_runtime_services *rs;
19 * bootmgr implements the logic of trying to find a payload to boot
20 * based on the BootOrder + BootXXXX variables, and then loading it.
22 * TODO detecting a special key held (f9?) and displaying a boot menu
23 * like you would get on a PC would be clever.
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.
32 * efi_deserialize_load_option() - parse serialized data
34 * Parse serialized data describing a load option and transform it to the
35 * efi_load_option structure.
37 * @lo: pointer to target
38 * @data: serialized data
39 * @size: size of the load option, on return size of the optional data
42 efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
48 if (*size < len + 2 * sizeof(u16))
49 return EFI_INVALID_PARAMETER;
50 lo->attributes = get_unaligned_le32(data);
55 lo->file_path_length = get_unaligned_le16(data);
59 lo->label = (u16 *)data;
60 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
62 return EFI_INVALID_PARAMETER;
63 len = (len + 1) * sizeof(u16);
65 return EFI_INVALID_PARAMETER;
69 len = lo->file_path_length;
71 return EFI_INVALID_PARAMETER;
72 lo->file_path = (struct efi_device_path *)data;
74 * TODO: validate device path. There should be an end node within
75 * the indicated file_path_length.
80 lo->optional_data = data;
86 * efi_serialize_load_option() - serialize load option
88 * Serialize efi_load_option structure into byte stream for BootXXXX.
90 * @data: buffer for serialized data
92 * Return: size of allocated buffer
94 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
96 unsigned long label_len;
100 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
103 size = sizeof(lo->attributes);
104 size += sizeof(lo->file_path_length);
106 size += lo->file_path_length;
107 if (lo->optional_data)
108 size += (utf8_utf16_strlen((const char *)lo->optional_data)
116 memcpy(p, &lo->attributes, sizeof(lo->attributes));
117 p += sizeof(lo->attributes);
119 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
120 p += sizeof(lo->file_path_length);
122 memcpy(p, lo->label, label_len);
125 memcpy(p, lo->file_path, lo->file_path_length);
126 p += lo->file_path_length;
128 if (lo->optional_data) {
129 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
130 p += sizeof(u16); /* size of trailing \0 */
136 * get_var() - get UEFI variable
138 * It is the caller's duty to free the returned buffer.
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
145 static void *get_var(u16 *name, const efi_guid_t *vendor,
148 efi_guid_t *v = (efi_guid_t *)vendor;
153 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
154 if (ret == EFI_BUFFER_TOO_SMALL) {
156 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
159 if (ret != EFI_SUCCESS) {
169 * try_load_entry() - try to load image for boot option
171 * Attempt to load load-option number 'n', returning device_path and file_path
172 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
173 * and that the specified file to boot exists.
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
179 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
181 struct efi_load_option lo;
182 u16 varname[] = L"Boot0000";
183 u16 hexmap[] = L"0123456789ABCDEF";
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];
193 load_option = get_var(varname, &efi_global_variable_guid, &size);
195 return EFI_LOAD_ERROR;
197 ret = efi_deserialize_load_option(&lo, load_option, &size);
198 if (ret != EFI_SUCCESS) {
199 log_warning("Invalid load option for %ls\n", varname);
203 if (lo.attributes & LOAD_OPTION_ACTIVE) {
206 debug("%s: trying to load \"%ls\" from %pD\n",
207 __func__, lo.label, lo.file_path);
209 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
211 if (ret != EFI_SUCCESS) {
212 printf("Loading from Boot%04X '%ls' failed\n", n,
217 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
218 EFI_VARIABLE_RUNTIME_ACCESS;
220 ret = EFI_CALL(efi_set_variable(
222 (efi_guid_t *)&efi_global_variable_guid,
223 attributes, size, &n));
224 if (ret != EFI_SUCCESS) {
225 if (EFI_CALL(efi_unload_image(*handle))
227 printf("Unloading image failed\n");
231 printf("Booting: %ls\n", lo.label);
233 ret = EFI_LOAD_ERROR;
243 * efi_bootmgr_load() - try to load from BootNext or BootOrder
245 * 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.
249 * @handle: on return handle for the newly installed image
250 * Return: status code
252 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
254 u16 bootnext, *bootorder;
259 bs = systab.boottime;
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");
273 /* delete BootNext */
274 ret = EFI_CALL(efi_set_variable(
276 (efi_guid_t *)&efi_global_variable_guid,
277 EFI_VARIABLE_NON_VOLATILE, 0,
281 if (ret == EFI_SUCCESS) {
282 if (size == sizeof(u16)) {
283 ret = try_load_entry(bootnext, handle);
284 if (ret == EFI_SUCCESS)
286 printf("Loading from BootNext failed, falling back to BootOrder\n");
289 printf("Deleting BootNext failed\n");
294 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
296 printf("BootOrder not defined\n");
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]);
304 ret = try_load_entry(bootorder[i], handle);
305 if (ret == EFI_SUCCESS)