1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
11 #include <efi_loader.h>
12 #include <asm/unaligned.h>
14 static const struct efi_boot_services *bs;
15 static const struct efi_runtime_services *rs;
18 * bootmgr implements the logic of trying to find a payload to boot
19 * based on the BootOrder + BootXXXX variables, and then loading it.
21 * TODO detecting a special key held (f9?) and displaying a boot menu
22 * like you would get on a PC would be clever.
24 * TODO if we had a way to write and persist variables after the OS
25 * has started, we'd also want to check OsIndications to see if we
26 * should do normal or recovery boot.
31 * efi_deserialize_load_option() - parse serialized data
33 * Parse serialized data describing a load option and transform it to the
34 * efi_load_option structure.
36 * @lo: pointer to target
37 * @data: serialized data
39 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
41 lo->attributes = get_unaligned_le32(data);
44 lo->file_path_length = get_unaligned_le16(data);
48 lo->label = (u16 *)data;
49 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
52 lo->file_path = (struct efi_device_path *)data;
53 data += lo->file_path_length;
55 lo->optional_data = data;
59 * efi_serialize_load_option() - serialize load option
61 * Serialize efi_load_option structure into byte stream for BootXXXX.
63 * @data: buffer for serialized data
65 * Return: size of allocated buffer
67 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
69 unsigned long label_len;
73 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
76 size = sizeof(lo->attributes);
77 size += sizeof(lo->file_path_length);
79 size += lo->file_path_length;
80 if (lo->optional_data)
81 size += (utf8_utf16_strlen((const char *)lo->optional_data)
89 memcpy(p, &lo->attributes, sizeof(lo->attributes));
90 p += sizeof(lo->attributes);
92 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
93 p += sizeof(lo->file_path_length);
95 memcpy(p, lo->label, label_len);
98 memcpy(p, lo->file_path, lo->file_path_length);
99 p += lo->file_path_length;
101 if (lo->optional_data) {
102 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
103 p += sizeof(u16); /* size of trailing \0 */
109 * get_var() - get UEFI variable
111 * It is the caller's duty to free the returned buffer.
113 * @name: name of variable
114 * @vendor: vendor GUID of variable
115 * @size: size of allocated buffer
116 * Return: buffer with variable data or NULL
118 static void *get_var(u16 *name, const efi_guid_t *vendor,
121 efi_guid_t *v = (efi_guid_t *)vendor;
126 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
127 if (ret == EFI_BUFFER_TOO_SMALL) {
129 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
132 if (ret != EFI_SUCCESS) {
142 * try_load_entry() - try to load image for boot option
144 * Attempt to load load-option number 'n', returning device_path and file_path
145 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
146 * and that the specified file to boot exists.
148 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
149 * @handle: on return handle for the newly installed image
150 * Return: status code
152 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
154 struct efi_load_option lo;
155 u16 varname[] = L"Boot0000";
156 u16 hexmap[] = L"0123456789ABCDEF";
161 varname[4] = hexmap[(n & 0xf000) >> 12];
162 varname[5] = hexmap[(n & 0x0f00) >> 8];
163 varname[6] = hexmap[(n & 0x00f0) >> 4];
164 varname[7] = hexmap[(n & 0x000f) >> 0];
166 load_option = get_var(varname, &efi_global_variable_guid, &size);
168 return EFI_LOAD_ERROR;
170 efi_deserialize_load_option(&lo, load_option);
172 if (lo.attributes & LOAD_OPTION_ACTIVE) {
175 debug("%s: trying to load \"%ls\" from %pD\n",
176 __func__, lo.label, lo.file_path);
178 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
180 if (ret != EFI_SUCCESS) {
181 printf("Loading from Boot%04X '%ls' failed\n", n,
186 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
187 EFI_VARIABLE_RUNTIME_ACCESS;
189 ret = EFI_CALL(efi_set_variable(
191 (efi_guid_t *)&efi_global_variable_guid,
192 attributes, size, &n));
193 if (ret != EFI_SUCCESS) {
194 if (EFI_CALL(efi_unload_image(*handle))
196 printf("Unloading image failed\n");
200 printf("Booting: %ls\n", lo.label);
202 ret = EFI_LOAD_ERROR;
212 * efi_bootmgr_load() - try to load from BootNext or BootOrder
214 * Attempt to load from BootNext or in the order specified by BootOrder
215 * EFI variable, the available load-options, finding and returning
216 * the first one that can be loaded successfully.
218 * @handle: on return handle for the newly installed image
219 * Return: status code
221 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
223 u16 bootnext, *bootorder;
228 bs = systab.boottime;
233 size = sizeof(bootnext);
234 ret = EFI_CALL(efi_get_variable(L"BootNext",
235 (efi_guid_t *)&efi_global_variable_guid,
236 NULL, &size, &bootnext));
237 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
238 /* BootNext does exist here */
239 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
240 printf("BootNext must be 16-bit integer\n");
242 /* delete BootNext */
243 ret = EFI_CALL(efi_set_variable(
245 (efi_guid_t *)&efi_global_variable_guid,
246 EFI_VARIABLE_NON_VOLATILE, 0,
250 if (ret == EFI_SUCCESS) {
251 if (size == sizeof(u16)) {
252 ret = try_load_entry(bootnext, handle);
253 if (ret == EFI_SUCCESS)
255 printf("Loading from BootNext failed, falling back to BootOrder\n");
258 printf("Deleting BootNext failed\n");
263 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
265 printf("BootOrder not defined\n");
270 num = size / sizeof(uint16_t);
271 for (i = 0; i < num; i++) {
272 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
273 ret = try_load_entry(bootorder[i], handle);
274 if (ret == EFI_SUCCESS)