1
2
3
4
5
6
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <common.h>
11#include <charset.h>
12#include <log.h>
13#include <malloc.h>
14#include <efi_loader.h>
15#include <efi_variable.h>
16#include <asm/unaligned.h>
17
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41efi_status_t efi_set_load_options(efi_handle_t handle,
42 efi_uintn_t load_options_size,
43 void *load_options)
44{
45 struct efi_loaded_image *loaded_image_info;
46 efi_status_t ret;
47
48 ret = EFI_CALL(systab.boottime->open_protocol(
49 handle,
50 &efi_guid_loaded_image,
51 (void **)&loaded_image_info,
52 efi_root, NULL,
53 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
54 if (ret != EFI_SUCCESS)
55 return EFI_INVALID_PARAMETER;
56
57 loaded_image_info->load_options = load_options;
58 loaded_image_info->load_options_size = load_options_size;
59
60 return EFI_CALL(systab.boottime->close_protocol(handle,
61 &efi_guid_loaded_image,
62 efi_root, NULL));
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
78 efi_uintn_t *size)
79{
80 efi_uintn_t len;
81
82 len = sizeof(u32);
83 if (*size < len + 2 * sizeof(u16))
84 return EFI_INVALID_PARAMETER;
85 lo->attributes = get_unaligned_le32(data);
86 data += len;
87 *size -= len;
88
89 len = sizeof(u16);
90 lo->file_path_length = get_unaligned_le16(data);
91 data += len;
92 *size -= len;
93
94 lo->label = (u16 *)data;
95 len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
96 if (lo->label[len])
97 return EFI_INVALID_PARAMETER;
98 len = (len + 1) * sizeof(u16);
99 if (*size < len)
100 return EFI_INVALID_PARAMETER;
101 data += len;
102 *size -= len;
103
104 len = lo->file_path_length;
105 if (*size < len)
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;
110 data += len;
111 *size -= len;
112
113 lo->optional_data = data;
114
115 return EFI_SUCCESS;
116}
117
118
119
120
121
122
123
124
125
126
127unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
128{
129 unsigned long label_len;
130 unsigned long size;
131 u8 *p;
132
133 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
134
135
136 size = sizeof(lo->attributes);
137 size += sizeof(lo->file_path_length);
138 size += label_len;
139 size += lo->file_path_length;
140 if (lo->optional_data)
141 size += (utf8_utf16_strlen((const char *)lo->optional_data)
142 + 1) * sizeof(u16);
143 p = malloc(size);
144 if (!p)
145 return 0;
146
147
148 *data = p;
149 memcpy(p, &lo->attributes, sizeof(lo->attributes));
150 p += sizeof(lo->attributes);
151
152 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
153 p += sizeof(lo->file_path_length);
154
155 memcpy(p, lo->label, label_len);
156 p += label_len;
157
158 memcpy(p, lo->file_path, lo->file_path_length);
159 p += lo->file_path_length;
160
161 if (lo->optional_data) {
162 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
163 p += sizeof(u16);
164 }
165 return size;
166}
167
168
169
170
171
172
173
174
175
176
177
178static void *get_var(u16 *name, const efi_guid_t *vendor,
179 efi_uintn_t *size)
180{
181 efi_status_t ret;
182 void *buf = NULL;
183
184 *size = 0;
185 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
186 if (ret == EFI_BUFFER_TOO_SMALL) {
187 buf = malloc(*size);
188 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
189 }
190
191 if (ret != EFI_SUCCESS) {
192 free(buf);
193 *size = 0;
194 return NULL;
195 }
196
197 return buf;
198}
199
200
201
202
203
204
205
206
207
208
209
210
211
212static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
213 void **load_options)
214{
215 struct efi_load_option lo;
216 u16 varname[] = L"Boot0000";
217 u16 hexmap[] = L"0123456789ABCDEF";
218 void *load_option;
219 efi_uintn_t size;
220 efi_status_t ret;
221
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];
226
227 load_option = get_var(varname, &efi_global_variable_guid, &size);
228 if (!load_option)
229 return EFI_LOAD_ERROR;
230
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);
234 goto error;
235 }
236
237 if (lo.attributes & LOAD_OPTION_ACTIVE) {
238 u32 attributes;
239
240 log_debug("%s: trying to load \"%ls\" from %pD\n",
241 __func__, lo.label, lo.file_path);
242
243 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
244 NULL, 0, handle));
245 if (ret != EFI_SUCCESS) {
246 log_warning("Loading %ls '%ls' failed\n",
247 varname, lo.label);
248 goto error;
249 }
250
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))
258 != EFI_SUCCESS)
259 log_err("Unloading image failed\n");
260 goto error;
261 }
262
263 log_info("Booting: %ls\n", lo.label);
264 } else {
265 ret = EFI_LOAD_ERROR;
266 }
267
268
269 if (size) {
270 *load_options = malloc(size);
271 if (!*load_options) {
272 ret = EFI_OUT_OF_RESOURCES;
273 goto error;
274 }
275 memcpy(*load_options, lo.optional_data, size);
276 ret = efi_set_load_options(*handle, size, *load_options);
277 } else {
278 *load_options = NULL;
279 }
280
281error:
282 free(load_option);
283
284 return ret;
285}
286
287
288
289
290
291
292
293
294
295
296
297
298efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
299{
300 u16 bootnext, *bootorder;
301 efi_uintn_t size;
302 int i, num;
303 efi_status_t ret;
304
305 bs = systab.boottime;
306 rs = systab.runtime;
307
308
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
315 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
316 log_err("BootNext must be 16-bit integer\n");
317
318
319 ret = efi_set_variable_int(L"BootNext",
320 &efi_global_variable_guid,
321 0, 0, NULL, false);
322
323
324 if (ret == EFI_SUCCESS) {
325 if (size == sizeof(u16)) {
326 ret = try_load_entry(bootnext, handle,
327 load_options);
328 if (ret == EFI_SUCCESS)
329 return ret;
330 log_warning(
331 "Loading from BootNext failed, falling back to BootOrder\n");
332 }
333 } else {
334 log_err("Deleting BootNext failed\n");
335 }
336 }
337
338
339 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
340 if (!bootorder) {
341 log_info("BootOrder not defined\n");
342 ret = EFI_NOT_FOUND;
343 goto error;
344 }
345
346 num = size / sizeof(uint16_t);
347 for (i = 0; i < num; i++) {
348 log_debug("%s trying to load Boot%04X\n", __func__,
349 bootorder[i]);
350 ret = try_load_entry(bootorder[i], handle, load_options);
351 if (ret == EFI_SUCCESS)
352 break;
353 }
354
355 free(bootorder);
356
357error:
358 return ret;
359}
360