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.
30 /* Parse serialized data and transform it into efi_load_option structure */
31 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
33 lo->attributes = get_unaligned_le32(data);
36 lo->file_path_length = get_unaligned_le16(data);
40 lo->label = (u16 *)data;
41 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
44 lo->file_path = (struct efi_device_path *)data;
45 data += lo->file_path_length;
47 lo->optional_data = data;
51 * Serialize efi_load_option structure into byte stream for BootXXXX.
52 * Return a size of allocated data.
54 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
56 unsigned long label_len;
60 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
63 size = sizeof(lo->attributes);
64 size += sizeof(lo->file_path_length);
66 size += lo->file_path_length;
67 if (lo->optional_data)
68 size += (utf8_utf16_strlen((const char *)lo->optional_data)
76 memcpy(p, &lo->attributes, sizeof(lo->attributes));
77 p += sizeof(lo->attributes);
79 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
80 p += sizeof(lo->file_path_length);
82 memcpy(p, lo->label, label_len);
85 memcpy(p, lo->file_path, lo->file_path_length);
86 p += lo->file_path_length;
88 if (lo->optional_data) {
89 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
90 p += sizeof(u16); /* size of trailing \0 */
95 /* free() the result */
96 static void *get_var(u16 *name, const efi_guid_t *vendor,
99 efi_guid_t *v = (efi_guid_t *)vendor;
104 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
105 if (ret == EFI_BUFFER_TOO_SMALL) {
107 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
110 if (ret != EFI_SUCCESS) {
120 * Attempt to load load-option number 'n', returning device_path and file_path
121 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
122 * and that the specified file to boot exists.
124 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
126 struct efi_load_option lo;
127 u16 varname[] = L"Boot0000";
128 u16 hexmap[] = L"0123456789ABCDEF";
133 varname[4] = hexmap[(n & 0xf000) >> 12];
134 varname[5] = hexmap[(n & 0x0f00) >> 8];
135 varname[6] = hexmap[(n & 0x00f0) >> 4];
136 varname[7] = hexmap[(n & 0x000f) >> 0];
138 load_option = get_var(varname, &efi_global_variable_guid, &size);
140 return EFI_LOAD_ERROR;
142 efi_deserialize_load_option(&lo, load_option);
144 if (lo.attributes & LOAD_OPTION_ACTIVE) {
147 debug("%s: trying to load \"%ls\" from %pD\n",
148 __func__, lo.label, lo.file_path);
150 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
152 if (ret != EFI_SUCCESS)
155 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
156 EFI_VARIABLE_RUNTIME_ACCESS;
158 ret = EFI_CALL(efi_set_variable(
160 (efi_guid_t *)&efi_global_variable_guid,
161 attributes, size, &n));
162 if (ret != EFI_SUCCESS) {
163 if (EFI_CALL(efi_unload_image(*handle))
165 printf("Unloading image failed\n");
169 printf("Booting: %ls\n", lo.label);
171 ret = EFI_LOAD_ERROR;
181 * Attempt to load from BootNext or in the order specified by BootOrder
182 * EFI variable, the available load-options, finding and returning
183 * the first one that can be loaded successfully.
185 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
187 u16 bootnext, *bootorder;
192 bs = systab.boottime;
197 size = sizeof(bootnext);
198 ret = EFI_CALL(efi_get_variable(L"BootNext",
199 (efi_guid_t *)&efi_global_variable_guid,
200 NULL, &size, &bootnext));
201 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
202 /* BootNext does exist here */
203 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
204 printf("BootNext must be 16-bit integer\n");
206 /* delete BootNext */
207 ret = EFI_CALL(efi_set_variable(
209 (efi_guid_t *)&efi_global_variable_guid,
213 if (ret == EFI_SUCCESS) {
214 if (size == sizeof(u16)) {
215 ret = try_load_entry(bootnext, handle);
216 if (ret == EFI_SUCCESS)
220 printf("Deleting BootNext failed\n");
225 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
227 printf("BootOrder not defined\n");
232 num = size / sizeof(uint16_t);
233 for (i = 0; i < num; i++) {
234 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
235 ret = try_load_entry(bootorder[i], handle);
236 if (ret == EFI_SUCCESS)