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
40 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
42 lo->attributes = get_unaligned_le32(data);
45 lo->file_path_length = get_unaligned_le16(data);
49 lo->label = (u16 *)data;
50 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
53 lo->file_path = (struct efi_device_path *)data;
54 data += lo->file_path_length;
56 lo->optional_data = data;
60 * efi_serialize_load_option() - serialize load option
62 * Serialize efi_load_option structure into byte stream for BootXXXX.
64 * @data: buffer for serialized data
66 * Return: size of allocated buffer
68 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
70 unsigned long label_len;
74 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
77 size = sizeof(lo->attributes);
78 size += sizeof(lo->file_path_length);
80 size += lo->file_path_length;
81 if (lo->optional_data)
82 size += (utf8_utf16_strlen((const char *)lo->optional_data)
90 memcpy(p, &lo->attributes, sizeof(lo->attributes));
91 p += sizeof(lo->attributes);
93 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
94 p += sizeof(lo->file_path_length);
96 memcpy(p, lo->label, label_len);
99 memcpy(p, lo->file_path, lo->file_path_length);
100 p += lo->file_path_length;
102 if (lo->optional_data) {
103 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
104 p += sizeof(u16); /* size of trailing \0 */
110 * get_var() - get UEFI variable
112 * It is the caller's duty to free the returned buffer.
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
119 static void *get_var(u16 *name, const efi_guid_t *vendor,
122 efi_guid_t *v = (efi_guid_t *)vendor;
127 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
128 if (ret == EFI_BUFFER_TOO_SMALL) {
130 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
133 if (ret != EFI_SUCCESS) {
143 * try_load_entry() - try to load image for boot option
145 * Attempt to load load-option number 'n', returning device_path and file_path
146 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
147 * and that the specified file to boot exists.
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
153 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
155 struct efi_load_option lo;
156 u16 varname[] = L"Boot0000";
157 u16 hexmap[] = L"0123456789ABCDEF";
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];
167 load_option = get_var(varname, &efi_global_variable_guid, &size);
169 return EFI_LOAD_ERROR;
171 efi_deserialize_load_option(&lo, load_option);
173 if (lo.attributes & LOAD_OPTION_ACTIVE) {
176 debug("%s: trying to load \"%ls\" from %pD\n",
177 __func__, lo.label, lo.file_path);
179 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
181 if (ret != EFI_SUCCESS) {
182 printf("Loading from Boot%04X '%ls' failed\n", n,
187 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
188 EFI_VARIABLE_RUNTIME_ACCESS;
190 ret = EFI_CALL(efi_set_variable(
192 (efi_guid_t *)&efi_global_variable_guid,
193 attributes, size, &n));
194 if (ret != EFI_SUCCESS) {
195 if (EFI_CALL(efi_unload_image(*handle))
197 printf("Unloading image failed\n");
201 printf("Booting: %ls\n", lo.label);
203 ret = EFI_LOAD_ERROR;
213 * efi_bootmgr_load() - try to load from BootNext or BootOrder
215 * 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.
219 * @handle: on return handle for the newly installed image
220 * Return: status code
222 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
224 u16 bootnext, *bootorder;
229 bs = systab.boottime;
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");
243 /* delete BootNext */
244 ret = EFI_CALL(efi_set_variable(
246 (efi_guid_t *)&efi_global_variable_guid,
247 EFI_VARIABLE_NON_VOLATILE, 0,
251 if (ret == EFI_SUCCESS) {
252 if (size == sizeof(u16)) {
253 ret = try_load_entry(bootnext, handle);
254 if (ret == EFI_SUCCESS)
256 printf("Loading from BootNext failed, falling back to BootOrder\n");
259 printf("Deleting BootNext failed\n");
264 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
266 printf("BootOrder not defined\n");
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]);
274 ret = try_load_entry(bootorder[i], handle);
275 if (ret == EFI_SUCCESS)