]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0fa2cac4 KS |
2 | |
3 | #include <efi.h> | |
fa2a3bbd | 4 | #include <efigpt.h> |
0fa2cac4 KS |
5 | #include <efilib.h> |
6 | ||
0fa2cac4 | 7 | #include "console.h" |
fa2a3bbd | 8 | #include "crc32.h" |
8110e144 | 9 | #include "disk.h" |
cf0fbc49 | 10 | #include "graphics.h" |
0fa2cac4 | 11 | #include "linux.h" |
91b08bb0 | 12 | #include "loader-features.h" |
92ed3bb4 | 13 | #include "measure.h" |
d4cbada2 | 14 | #include "pe.h" |
e4dcf7aa | 15 | #include "random-seed.h" |
b2bb40ce | 16 | #include "shim.h" |
d4cbada2 | 17 | #include "util.h" |
0fa2cac4 KS |
18 | |
19 | #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI | |
20 | #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL | |
21 | #endif | |
22 | ||
23 | /* magic string to find in the binary image */ | |
681bd2c5 | 24 | static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####"; |
0fa2cac4 KS |
25 | |
26 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; | |
27 | ||
28 | enum loader_type { | |
29 | LOADER_UNDEFINED, | |
30 | LOADER_EFI, | |
f538cc65 | 31 | LOADER_LINUX, |
0fa2cac4 KS |
32 | }; |
33 | ||
34 | typedef struct { | |
6cd12ebc | 35 | CHAR16 *id; /* The unique identifier for this entry */ |
0fa2cac4 KS |
36 | CHAR16 *title_show; |
37 | CHAR16 *title; | |
38 | CHAR16 *version; | |
39 | CHAR16 *machine_id; | |
40 | EFI_HANDLE *device; | |
41 | enum loader_type type; | |
42 | CHAR16 *loader; | |
43 | CHAR16 *options; | |
0fa2cac4 KS |
44 | CHAR16 key; |
45 | EFI_STATUS (*call)(VOID); | |
46 | BOOLEAN no_autoselect; | |
47 | BOOLEAN non_unique; | |
f538cc65 LP |
48 | UINTN tries_done; |
49 | UINTN tries_left; | |
50 | CHAR16 *path; | |
51 | CHAR16 *current_name; | |
52 | CHAR16 *next_name; | |
0fa2cac4 KS |
53 | } ConfigEntry; |
54 | ||
55 | typedef struct { | |
56 | ConfigEntry **entries; | |
57 | UINTN entry_count; | |
58 | INTN idx_default; | |
59 | INTN idx_default_efivar; | |
60 | UINTN timeout_sec; | |
61 | UINTN timeout_sec_config; | |
62 | INTN timeout_sec_efivar; | |
63 | CHAR16 *entry_default_pattern; | |
0fa2cac4 KS |
64 | CHAR16 *entry_oneshot; |
65 | CHAR16 *options_edit; | |
c1d4e298 JJ |
66 | BOOLEAN editor; |
67 | BOOLEAN auto_entries; | |
68 | BOOLEAN auto_firmware; | |
fe2579dd | 69 | BOOLEAN force_menu; |
68d4b8ac HDA |
70 | UINTN console_mode; |
71 | enum console_mode_change_type console_mode_change; | |
e4dcf7aa | 72 | RandomSeedMode random_seed_mode; |
0fa2cac4 KS |
73 | } Config; |
74 | ||
52efd56a | 75 | static VOID cursor_left(UINTN *cursor, UINTN *first) { |
0fa2cac4 KS |
76 | if ((*cursor) > 0) |
77 | (*cursor)--; | |
78 | else if ((*first) > 0) | |
79 | (*first)--; | |
80 | } | |
81 | ||
580fe4df LP |
82 | static VOID cursor_right( |
83 | UINTN *cursor, | |
84 | UINTN *first, | |
85 | UINTN x_max, | |
86 | UINTN len) { | |
87 | ||
0fa2cac4 KS |
88 | if ((*cursor)+1 < x_max) |
89 | (*cursor)++; | |
90 | else if ((*first) + (*cursor) < len) | |
91 | (*first)++; | |
92 | } | |
93 | ||
580fe4df LP |
94 | static BOOLEAN line_edit( |
95 | CHAR16 *line_in, | |
96 | CHAR16 **line_out, | |
97 | UINTN x_max, | |
98 | UINTN y_pos) { | |
99 | ||
a42d7cf1 ZJS |
100 | _cleanup_freepool_ CHAR16 *line = NULL, *print = NULL; |
101 | UINTN size, len, first, cursor, clear; | |
102 | BOOLEAN exit, enter; | |
0fa2cac4 KS |
103 | |
104 | if (!line_in) | |
105 | line_in = L""; | |
106 | size = StrLen(line_in) + 1024; | |
107 | line = AllocatePool(size * sizeof(CHAR16)); | |
108 | StrCpy(line, line_in); | |
109 | len = StrLen(line); | |
110 | print = AllocatePool((x_max+1) * sizeof(CHAR16)); | |
111 | ||
112 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE); | |
113 | ||
114 | first = 0; | |
115 | cursor = 0; | |
116 | clear = 0; | |
117 | enter = FALSE; | |
118 | exit = FALSE; | |
119 | while (!exit) { | |
120 | EFI_STATUS err; | |
121 | UINT64 key; | |
122 | UINTN i; | |
123 | ||
124 | i = len - first; | |
125 | if (i >= x_max-1) | |
126 | i = x_max-1; | |
127 | CopyMem(print, line + first, i * sizeof(CHAR16)); | |
128 | while (clear > 0 && i < x_max-1) { | |
129 | clear--; | |
130 | print[i++] = ' '; | |
131 | } | |
132 | print[i] = '\0'; | |
133 | ||
134 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos); | |
135 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print); | |
136 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
137 | ||
138 | err = console_key_read(&key, TRUE); | |
139 | if (EFI_ERROR(err)) | |
140 | continue; | |
141 | ||
142 | switch (key) { | |
143 | case KEYPRESS(0, SCAN_ESC, 0): | |
144 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'): | |
145 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'): | |
146 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')): | |
147 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')): | |
148 | exit = TRUE; | |
149 | break; | |
150 | ||
151 | case KEYPRESS(0, SCAN_HOME, 0): | |
152 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'): | |
153 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')): | |
154 | /* beginning-of-line */ | |
155 | cursor = 0; | |
156 | first = 0; | |
157 | continue; | |
158 | ||
159 | case KEYPRESS(0, SCAN_END, 0): | |
160 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'): | |
161 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')): | |
162 | /* end-of-line */ | |
163 | cursor = len - first; | |
164 | if (cursor+1 >= x_max) { | |
165 | cursor = x_max-1; | |
166 | first = len - (x_max-1); | |
167 | } | |
168 | continue; | |
169 | ||
170 | case KEYPRESS(0, SCAN_DOWN, 0): | |
171 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'): | |
172 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0): | |
173 | /* forward-word */ | |
977f65f0 | 174 | while (line[first + cursor] == ' ') |
0fa2cac4 KS |
175 | cursor_right(&cursor, &first, x_max, len); |
176 | while (line[first + cursor] && line[first + cursor] != ' ') | |
177 | cursor_right(&cursor, &first, x_max, len); | |
178 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
179 | continue; | |
180 | ||
181 | case KEYPRESS(0, SCAN_UP, 0): | |
182 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'): | |
183 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0): | |
184 | /* backward-word */ | |
185 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { | |
186 | cursor_left(&cursor, &first); | |
187 | while ((first + cursor) > 0 && line[first + cursor] == ' ') | |
188 | cursor_left(&cursor, &first); | |
189 | } | |
190 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') | |
191 | cursor_left(&cursor, &first); | |
192 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
193 | continue; | |
194 | ||
195 | case KEYPRESS(0, SCAN_RIGHT, 0): | |
196 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'): | |
197 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')): | |
198 | /* forward-char */ | |
199 | if (first + cursor == len) | |
200 | continue; | |
201 | cursor_right(&cursor, &first, x_max, len); | |
202 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
203 | continue; | |
204 | ||
205 | case KEYPRESS(0, SCAN_LEFT, 0): | |
206 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'): | |
207 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')): | |
208 | /* backward-char */ | |
209 | cursor_left(&cursor, &first); | |
210 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
211 | continue; | |
212 | ||
213 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'): | |
214 | /* kill-word */ | |
215 | clear = 0; | |
216 | for (i = first + cursor; i < len && line[i] == ' '; i++) | |
217 | clear++; | |
218 | for (; i < len && line[i] != ' '; i++) | |
219 | clear++; | |
220 | ||
221 | for (i = first + cursor; i + clear < len; i++) | |
222 | line[i] = line[i + clear]; | |
223 | len -= clear; | |
224 | line[len] = '\0'; | |
225 | continue; | |
226 | ||
227 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'): | |
228 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')): | |
229 | case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE): | |
230 | /* backward-kill-word */ | |
231 | clear = 0; | |
232 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { | |
233 | cursor_left(&cursor, &first); | |
234 | clear++; | |
235 | while ((first + cursor) > 0 && line[first + cursor] == ' ') { | |
236 | cursor_left(&cursor, &first); | |
237 | clear++; | |
238 | } | |
239 | } | |
240 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') { | |
241 | cursor_left(&cursor, &first); | |
242 | clear++; | |
243 | } | |
244 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
245 | ||
246 | for (i = first + cursor; i + clear < len; i++) | |
247 | line[i] = line[i + clear]; | |
248 | len -= clear; | |
249 | line[len] = '\0'; | |
250 | continue; | |
251 | ||
252 | case KEYPRESS(0, SCAN_DELETE, 0): | |
253 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'): | |
254 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')): | |
255 | if (len == 0) | |
256 | continue; | |
257 | if (first + cursor == len) | |
258 | continue; | |
259 | for (i = first + cursor; i < len; i++) | |
260 | line[i] = line[i+1]; | |
261 | clear = 1; | |
262 | len--; | |
263 | continue; | |
264 | ||
265 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'): | |
266 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')): | |
267 | /* kill-line */ | |
268 | line[first + cursor] = '\0'; | |
269 | clear = len - (first + cursor); | |
270 | len = first + cursor; | |
271 | continue; | |
272 | ||
273 | case KEYPRESS(0, 0, CHAR_LINEFEED): | |
274 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): | |
c1db999e LP |
275 | if (StrCmp(line, line_in) != 0) |
276 | *line_out = TAKE_PTR(line); | |
0fa2cac4 KS |
277 | enter = TRUE; |
278 | exit = TRUE; | |
279 | break; | |
280 | ||
281 | case KEYPRESS(0, 0, CHAR_BACKSPACE): | |
282 | if (len == 0) | |
283 | continue; | |
284 | if (first == 0 && cursor == 0) | |
285 | continue; | |
286 | for (i = first + cursor-1; i < len; i++) | |
287 | line[i] = line[i+1]; | |
288 | clear = 1; | |
289 | len--; | |
290 | if (cursor > 0) | |
291 | cursor--; | |
292 | if (cursor > 0 || first == 0) | |
293 | continue; | |
294 | /* show full line if it fits */ | |
295 | if (len < x_max) { | |
296 | cursor = first; | |
297 | first = 0; | |
298 | continue; | |
299 | } | |
300 | /* jump left to see what we delete */ | |
301 | if (first > 10) { | |
302 | first -= 10; | |
303 | cursor = 10; | |
304 | } else { | |
305 | cursor = first; | |
306 | first = 0; | |
307 | } | |
308 | continue; | |
309 | ||
310 | case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'): | |
311 | case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff): | |
312 | if (len+1 == size) | |
313 | continue; | |
314 | for (i = len; i > first + cursor; i--) | |
315 | line[i] = line[i-1]; | |
316 | line[first + cursor] = KEYCHAR(key); | |
317 | len++; | |
318 | line[len] = '\0'; | |
319 | if (cursor+1 < x_max) | |
320 | cursor++; | |
321 | else if (first + cursor < len) | |
322 | first++; | |
323 | continue; | |
324 | } | |
325 | } | |
326 | ||
327 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); | |
0fa2cac4 KS |
328 | return enter; |
329 | } | |
330 | ||
331 | static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) { | |
332 | UINTN i; | |
333 | ||
334 | if (key == 0) | |
335 | return -1; | |
336 | ||
337 | /* select entry by number key */ | |
338 | if (key >= '1' && key <= '9') { | |
339 | i = key - '0'; | |
340 | if (i > config->entry_count) | |
341 | i = config->entry_count; | |
342 | return i-1; | |
343 | } | |
344 | ||
345 | /* find matching key in config entries */ | |
346 | for (i = start; i < config->entry_count; i++) | |
347 | if (config->entries[i]->key == key) | |
348 | return i; | |
349 | ||
350 | for (i = 0; i < start; i++) | |
351 | if (config->entries[i]->key == key) | |
352 | return i; | |
353 | ||
354 | return -1; | |
355 | } | |
356 | ||
7361099e | 357 | static VOID print_status(Config *config, CHAR16 *loaded_image_path) { |
0fa2cac4 KS |
358 | UINT64 key; |
359 | UINTN i; | |
a42d7cf1 ZJS |
360 | _cleanup_freepool_ CHAR8 *bootvar = NULL, *modevar = NULL, *indvar = NULL; |
361 | _cleanup_freepool_ CHAR16 *partstr = NULL, *defaultstr = NULL; | |
362 | UINTN x, y, size; | |
0fa2cac4 KS |
363 | |
364 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
365 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
366 | ||
681bd2c5 | 367 | Print(L"systemd-boot version: " GIT_VERSION "\n"); |
0fa2cac4 KS |
368 | Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n"); |
369 | Print(L"loaded image: %s\n", loaded_image_path); | |
370 | Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); | |
371 | Print(L"firmware vendor: %s\n", ST->FirmwareVendor); | |
372 | Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); | |
373 | ||
374 | if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS) | |
375 | Print(L"console size: %d x %d\n", x, y); | |
376 | ||
a42d7cf1 ZJS |
377 | if (efivar_get_raw(&global_guid, L"SecureBoot", &bootvar, &size) == EFI_SUCCESS) |
378 | Print(L"SecureBoot: %s\n", yes_no(*bootvar > 0)); | |
0fa2cac4 | 379 | |
a42d7cf1 ZJS |
380 | if (efivar_get_raw(&global_guid, L"SetupMode", &modevar, &size) == EFI_SUCCESS) |
381 | Print(L"SetupMode: %s\n", *modevar > 0 ? L"setup" : L"user"); | |
0fa2cac4 | 382 | |
b2bb40ce MR |
383 | if (shim_loaded()) |
384 | Print(L"Shim: present\n"); | |
385 | ||
a42d7cf1 ZJS |
386 | if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &indvar, &size) == EFI_SUCCESS) |
387 | Print(L"OsIndicationsSupported: %d\n", (UINT64)*indvar); | |
c1d4e298 JJ |
388 | |
389 | Print(L"\n--- press key ---\n\n"); | |
390 | console_key_read(&key, TRUE); | |
0fa2cac4 | 391 | |
8ac69471 | 392 | Print(L"timeout: %u\n", config->timeout_sec); |
0fa2cac4 KS |
393 | if (config->timeout_sec_efivar >= 0) |
394 | Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar); | |
8ac69471 | 395 | Print(L"timeout (config): %u\n", config->timeout_sec_config); |
0fa2cac4 KS |
396 | if (config->entry_default_pattern) |
397 | Print(L"default pattern: '%s'\n", config->entry_default_pattern); | |
c1d4e298 JJ |
398 | Print(L"editor: %s\n", yes_no(config->editor)); |
399 | Print(L"auto-entries: %s\n", yes_no(config->auto_entries)); | |
400 | Print(L"auto-firmware: %s\n", yes_no(config->auto_firmware)); | |
e4dcf7aa LP |
401 | |
402 | switch (config->random_seed_mode) { | |
403 | case RANDOM_SEED_OFF: | |
404 | Print(L"random-seed-mode: off\n"); | |
405 | break; | |
406 | case RANDOM_SEED_WITH_SYSTEM_TOKEN: | |
407 | Print(L"random-seed-node: with-system-token\n"); | |
408 | break; | |
409 | case RANDOM_SEED_ALWAYS: | |
410 | Print(L"random-seed-node: always\n"); | |
411 | break; | |
412 | default: | |
413 | ; | |
414 | } | |
415 | ||
0fa2cac4 KS |
416 | Print(L"\n"); |
417 | ||
418 | Print(L"config entry count: %d\n", config->entry_count); | |
419 | Print(L"entry selected idx: %d\n", config->idx_default); | |
420 | if (config->idx_default_efivar >= 0) | |
421 | Print(L"entry EFI var idx: %d\n", config->idx_default_efivar); | |
422 | Print(L"\n"); | |
423 | ||
424 | if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS) | |
8ac69471 LP |
425 | Print(L"LoaderConfigTimeout: %u\n", i); |
426 | ||
0fa2cac4 KS |
427 | if (config->entry_oneshot) |
428 | Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot); | |
a42d7cf1 ZJS |
429 | if (efivar_get(L"LoaderDevicePartUUID", &partstr) == EFI_SUCCESS) |
430 | Print(L"LoaderDevicePartUUID: %s\n", partstr); | |
431 | if (efivar_get(L"LoaderEntryDefault", &defaultstr) == EFI_SUCCESS) | |
432 | Print(L"LoaderEntryDefault: %s\n", defaultstr); | |
0fa2cac4 KS |
433 | |
434 | Print(L"\n--- press key ---\n\n"); | |
435 | console_key_read(&key, TRUE); | |
436 | ||
437 | for (i = 0; i < config->entry_count; i++) { | |
438 | ConfigEntry *entry; | |
439 | ||
440 | if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q')) | |
441 | break; | |
442 | ||
443 | entry = config->entries[i]; | |
0fa2cac4 | 444 | Print(L"config entry: %d/%d\n", i+1, config->entry_count); |
081cc95f LP |
445 | if (entry->id) |
446 | Print(L"id '%s'\n", entry->id); | |
0fa2cac4 KS |
447 | Print(L"title show '%s'\n", entry->title_show); |
448 | if (entry->title) | |
449 | Print(L"title '%s'\n", entry->title); | |
450 | if (entry->version) | |
451 | Print(L"version '%s'\n", entry->version); | |
452 | if (entry->machine_id) | |
453 | Print(L"machine-id '%s'\n", entry->machine_id); | |
454 | if (entry->device) { | |
455 | EFI_DEVICE_PATH *device_path; | |
0fa2cac4 KS |
456 | |
457 | device_path = DevicePathFromHandle(entry->device); | |
458 | if (device_path) { | |
a42d7cf1 ZJS |
459 | _cleanup_freepool_ CHAR16 *str; |
460 | ||
0fa2cac4 KS |
461 | str = DevicePathToStr(device_path); |
462 | Print(L"device handle '%s'\n", str); | |
0fa2cac4 KS |
463 | } |
464 | } | |
465 | if (entry->loader) | |
466 | Print(L"loader '%s'\n", entry->loader); | |
467 | if (entry->options) | |
468 | Print(L"options '%s'\n", entry->options); | |
4db7e6d7 | 469 | Print(L"auto-select %s\n", yes_no(!entry->no_autoselect)); |
0fa2cac4 KS |
470 | if (entry->call) |
471 | Print(L"internal call yes\n"); | |
472 | ||
f538cc65 LP |
473 | if (entry->tries_left != (UINTN) -1) |
474 | Print(L"counting boots yes\n" | |
475 | "tries done %u\n" | |
476 | "tries left %u\n" | |
477 | "current path %s\\%s\n" | |
478 | "next path %s\\%s\n", | |
479 | entry->tries_done, | |
480 | entry->tries_left, | |
481 | entry->path, entry->current_name, | |
482 | entry->path, entry->next_name); | |
483 | ||
0fa2cac4 KS |
484 | Print(L"\n--- press key ---\n\n"); |
485 | console_key_read(&key, TRUE); | |
486 | } | |
487 | ||
488 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
489 | } | |
490 | ||
580fe4df LP |
491 | static BOOLEAN menu_run( |
492 | Config *config, | |
493 | ConfigEntry **chosen_entry, | |
494 | CHAR16 *loaded_image_path) { | |
495 | ||
0fa2cac4 KS |
496 | EFI_STATUS err; |
497 | UINTN visible_max; | |
498 | UINTN idx_highlight; | |
499 | UINTN idx_highlight_prev; | |
500 | UINTN idx_first; | |
501 | UINTN idx_last; | |
502 | BOOLEAN refresh; | |
503 | BOOLEAN highlight; | |
504 | UINTN i; | |
505 | UINTN line_width; | |
506 | CHAR16 **lines; | |
507 | UINTN x_start; | |
508 | UINTN y_start; | |
509 | UINTN x_max; | |
510 | UINTN y_max; | |
511 | CHAR16 *status; | |
512 | CHAR16 *clearline; | |
513 | INTN timeout_remain; | |
514 | INT16 idx; | |
515 | BOOLEAN exit = FALSE; | |
516 | BOOLEAN run = TRUE; | |
517 | BOOLEAN wait = FALSE; | |
518 | ||
519 | graphics_mode(FALSE); | |
520 | uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE); | |
521 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); | |
522 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
523 | ||
524 | /* draw a single character to make ClearScreen work on some firmware */ | |
525 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" "); | |
68d4b8ac HDA |
526 | |
527 | if (config->console_mode_change != CONSOLE_MODE_KEEP) { | |
528 | err = console_set_mode(&config->console_mode, config->console_mode_change); | |
d9690d8f JJ |
529 | if (EFI_ERROR(err)) { |
530 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
531 | Print(L"Error switching console mode to %ld: %r.\r", (UINT64)config->console_mode, err); | |
532 | } | |
533 | } else | |
68d4b8ac HDA |
534 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); |
535 | ||
0fa2cac4 KS |
536 | err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max); |
537 | if (EFI_ERROR(err)) { | |
538 | x_max = 80; | |
539 | y_max = 25; | |
540 | } | |
541 | ||
542 | /* we check 10 times per second for a keystroke */ | |
543 | if (config->timeout_sec > 0) | |
544 | timeout_remain = config->timeout_sec * 10; | |
545 | else | |
546 | timeout_remain = -1; | |
547 | ||
548 | idx_highlight = config->idx_default; | |
549 | idx_highlight_prev = 0; | |
550 | ||
551 | visible_max = y_max - 2; | |
552 | ||
553 | if ((UINTN)config->idx_default >= visible_max) | |
554 | idx_first = config->idx_default-1; | |
555 | else | |
556 | idx_first = 0; | |
557 | ||
558 | idx_last = idx_first + visible_max-1; | |
559 | ||
560 | refresh = TRUE; | |
561 | highlight = FALSE; | |
562 | ||
563 | /* length of the longest entry */ | |
564 | line_width = 5; | |
565 | for (i = 0; i < config->entry_count; i++) { | |
566 | UINTN entry_len; | |
567 | ||
568 | entry_len = StrLen(config->entries[i]->title_show); | |
569 | if (line_width < entry_len) | |
570 | line_width = entry_len; | |
571 | } | |
572 | if (line_width > x_max-6) | |
573 | line_width = x_max-6; | |
574 | ||
575 | /* offsets to center the entries on the screen */ | |
576 | x_start = (x_max - (line_width)) / 2; | |
577 | if (config->entry_count < visible_max) | |
578 | y_start = ((visible_max - config->entry_count) / 2) + 1; | |
579 | else | |
580 | y_start = 0; | |
581 | ||
582 | /* menu entries title lines */ | |
583 | lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count); | |
584 | for (i = 0; i < config->entry_count; i++) { | |
585 | UINTN j, k; | |
586 | ||
587 | lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16))); | |
588 | for (j = 0; j < x_start; j++) | |
589 | lines[i][j] = ' '; | |
590 | ||
591 | for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++) | |
592 | lines[i][j] = config->entries[i]->title_show[k]; | |
593 | ||
594 | for (; j < x_max; j++) | |
595 | lines[i][j] = ' '; | |
596 | lines[i][x_max] = '\0'; | |
597 | } | |
598 | ||
599 | status = NULL; | |
600 | clearline = AllocatePool((x_max+1) * sizeof(CHAR16)); | |
601 | for (i = 0; i < x_max; i++) | |
602 | clearline[i] = ' '; | |
603 | clearline[i] = 0; | |
604 | ||
605 | while (!exit) { | |
606 | UINT64 key; | |
607 | ||
608 | if (refresh) { | |
609 | for (i = 0; i < config->entry_count; i++) { | |
610 | if (i < idx_first || i > idx_last) | |
611 | continue; | |
612 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first); | |
613 | if (i == idx_highlight) | |
614 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, | |
615 | EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); | |
616 | else | |
617 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, | |
618 | EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
619 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]); | |
620 | if ((INTN)i == config->idx_default_efivar) { | |
621 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first); | |
622 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
623 | } | |
624 | } | |
625 | refresh = FALSE; | |
626 | } else if (highlight) { | |
627 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first); | |
628 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
629 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]); | |
630 | if ((INTN)idx_highlight_prev == config->idx_default_efivar) { | |
631 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first); | |
632 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
633 | } | |
634 | ||
635 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first); | |
636 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); | |
637 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]); | |
638 | if ((INTN)idx_highlight == config->idx_default_efivar) { | |
639 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first); | |
640 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
641 | } | |
642 | highlight = FALSE; | |
643 | } | |
644 | ||
645 | if (timeout_remain > 0) { | |
646 | FreePool(status); | |
647 | status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10); | |
648 | } | |
649 | ||
650 | /* print status at last line of screen */ | |
651 | if (status) { | |
652 | UINTN len; | |
653 | UINTN x; | |
654 | ||
655 | /* center line */ | |
656 | len = StrLen(status); | |
657 | if (len < x_max) | |
658 | x = (x_max - len) / 2; | |
659 | else | |
660 | x = 0; | |
661 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
662 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
663 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x)); | |
664 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status); | |
665 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len); | |
666 | } | |
667 | ||
668 | err = console_key_read(&key, wait); | |
669 | if (EFI_ERROR(err)) { | |
670 | /* timeout reached */ | |
671 | if (timeout_remain == 0) { | |
672 | exit = TRUE; | |
673 | break; | |
674 | } | |
675 | ||
676 | /* sleep and update status */ | |
677 | if (timeout_remain > 0) { | |
678 | uefi_call_wrapper(BS->Stall, 1, 100 * 1000); | |
679 | timeout_remain--; | |
680 | continue; | |
681 | } | |
682 | ||
683 | /* timeout disabled, wait for next key */ | |
684 | wait = TRUE; | |
685 | continue; | |
686 | } | |
687 | ||
688 | timeout_remain = -1; | |
689 | ||
690 | /* clear status after keystroke */ | |
691 | if (status) { | |
692 | FreePool(status); | |
693 | status = NULL; | |
694 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
695 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
696 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
697 | } | |
698 | ||
699 | idx_highlight_prev = idx_highlight; | |
700 | ||
701 | switch (key) { | |
702 | case KEYPRESS(0, SCAN_UP, 0): | |
703 | case KEYPRESS(0, 0, 'k'): | |
704 | if (idx_highlight > 0) | |
705 | idx_highlight--; | |
706 | break; | |
707 | ||
708 | case KEYPRESS(0, SCAN_DOWN, 0): | |
709 | case KEYPRESS(0, 0, 'j'): | |
710 | if (idx_highlight < config->entry_count-1) | |
711 | idx_highlight++; | |
712 | break; | |
713 | ||
714 | case KEYPRESS(0, SCAN_HOME, 0): | |
715 | case KEYPRESS(EFI_ALT_PRESSED, 0, '<'): | |
716 | if (idx_highlight > 0) { | |
717 | refresh = TRUE; | |
718 | idx_highlight = 0; | |
719 | } | |
720 | break; | |
721 | ||
722 | case KEYPRESS(0, SCAN_END, 0): | |
723 | case KEYPRESS(EFI_ALT_PRESSED, 0, '>'): | |
724 | if (idx_highlight < config->entry_count-1) { | |
725 | refresh = TRUE; | |
726 | idx_highlight = config->entry_count-1; | |
727 | } | |
728 | break; | |
729 | ||
730 | case KEYPRESS(0, SCAN_PAGE_UP, 0): | |
731 | if (idx_highlight > visible_max) | |
732 | idx_highlight -= visible_max; | |
733 | else | |
734 | idx_highlight = 0; | |
735 | break; | |
736 | ||
737 | case KEYPRESS(0, SCAN_PAGE_DOWN, 0): | |
738 | idx_highlight += visible_max; | |
739 | if (idx_highlight > config->entry_count-1) | |
740 | idx_highlight = config->entry_count-1; | |
741 | break; | |
742 | ||
743 | case KEYPRESS(0, 0, CHAR_LINEFEED): | |
744 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): | |
745 | exit = TRUE; | |
746 | break; | |
747 | ||
748 | case KEYPRESS(0, SCAN_F1, 0): | |
749 | case KEYPRESS(0, 0, 'h'): | |
750 | case KEYPRESS(0, 0, '?'): | |
751 | status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp"); | |
752 | break; | |
753 | ||
754 | case KEYPRESS(0, 0, 'Q'): | |
755 | exit = TRUE; | |
756 | run = FALSE; | |
757 | break; | |
758 | ||
759 | case KEYPRESS(0, 0, 'd'): | |
760 | if (config->idx_default_efivar != (INTN)idx_highlight) { | |
761 | /* store the selected entry in a persistent EFI variable */ | |
081cc95f | 762 | efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->id, TRUE); |
0fa2cac4 KS |
763 | config->idx_default_efivar = idx_highlight; |
764 | status = StrDuplicate(L"Default boot entry selected."); | |
765 | } else { | |
766 | /* clear the default entry EFI variable */ | |
767 | efivar_set(L"LoaderEntryDefault", NULL, TRUE); | |
768 | config->idx_default_efivar = -1; | |
769 | status = StrDuplicate(L"Default boot entry cleared."); | |
770 | } | |
771 | refresh = TRUE; | |
772 | break; | |
773 | ||
774 | case KEYPRESS(0, 0, '-'): | |
775 | case KEYPRESS(0, 0, 'T'): | |
776 | if (config->timeout_sec_efivar > 0) { | |
777 | config->timeout_sec_efivar--; | |
778 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); | |
779 | if (config->timeout_sec_efivar > 0) | |
780 | status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar); | |
781 | else | |
782 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
783 | } else if (config->timeout_sec_efivar <= 0){ | |
784 | config->timeout_sec_efivar = -1; | |
785 | efivar_set(L"LoaderConfigTimeout", NULL, TRUE); | |
786 | if (config->timeout_sec_config > 0) | |
787 | status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.", | |
788 | config->timeout_sec_config); | |
789 | else | |
790 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
791 | } | |
792 | break; | |
793 | ||
794 | case KEYPRESS(0, 0, '+'): | |
795 | case KEYPRESS(0, 0, 't'): | |
796 | if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0) | |
797 | config->timeout_sec_efivar++; | |
798 | config->timeout_sec_efivar++; | |
799 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); | |
800 | if (config->timeout_sec_efivar > 0) | |
801 | status = PoolPrint(L"Menu timeout set to %d sec.", | |
802 | config->timeout_sec_efivar); | |
803 | else | |
804 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
805 | break; | |
806 | ||
807 | case KEYPRESS(0, 0, 'e'): | |
808 | /* only the options of configured entries can be edited */ | |
c1d4e298 | 809 | if (!config->editor || config->entries[idx_highlight]->type == LOADER_UNDEFINED) |
0fa2cac4 KS |
810 | break; |
811 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
812 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
813 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
814 | if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1)) | |
815 | exit = TRUE; | |
816 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
817 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
818 | break; | |
819 | ||
820 | case KEYPRESS(0, 0, 'v'): | |
681bd2c5 | 821 | status = PoolPrint(L"systemd-boot " GIT_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d", |
0fa2cac4 KS |
822 | ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff, |
823 | ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); | |
824 | break; | |
825 | ||
826 | case KEYPRESS(0, 0, 'P'): | |
7361099e | 827 | print_status(config, loaded_image_path); |
0fa2cac4 KS |
828 | refresh = TRUE; |
829 | break; | |
830 | ||
831 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'): | |
832 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')): | |
833 | refresh = TRUE; | |
834 | break; | |
835 | ||
836 | default: | |
837 | /* jump with a hotkey directly to a matching entry */ | |
838 | idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key)); | |
839 | if (idx < 0) | |
840 | break; | |
841 | idx_highlight = idx; | |
842 | refresh = TRUE; | |
843 | } | |
844 | ||
845 | if (idx_highlight > idx_last) { | |
846 | idx_last = idx_highlight; | |
847 | idx_first = 1 + idx_highlight - visible_max; | |
848 | refresh = TRUE; | |
56c70b32 | 849 | } else if (idx_highlight < idx_first) { |
0fa2cac4 KS |
850 | idx_first = idx_highlight; |
851 | idx_last = idx_highlight + visible_max-1; | |
852 | refresh = TRUE; | |
853 | } | |
854 | ||
0fa2cac4 KS |
855 | if (!refresh && idx_highlight != idx_highlight_prev) |
856 | highlight = TRUE; | |
857 | } | |
858 | ||
859 | *chosen_entry = config->entries[idx_highlight]; | |
860 | ||
861 | for (i = 0; i < config->entry_count; i++) | |
862 | FreePool(lines[i]); | |
863 | FreePool(lines); | |
864 | FreePool(clearline); | |
865 | ||
866 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK); | |
867 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
868 | return run; | |
869 | } | |
870 | ||
871 | static VOID config_add_entry(Config *config, ConfigEntry *entry) { | |
872 | if ((config->entry_count & 15) == 0) { | |
873 | UINTN i; | |
874 | ||
875 | i = config->entry_count + 16; | |
876 | if (config->entry_count == 0) | |
877 | config->entries = AllocatePool(sizeof(VOID *) * i); | |
878 | else | |
879 | config->entries = ReallocatePool(config->entries, | |
880 | sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i); | |
881 | } | |
882 | config->entries[config->entry_count++] = entry; | |
883 | } | |
884 | ||
885 | static VOID config_entry_free(ConfigEntry *entry) { | |
42cf81c2 LP |
886 | if (!entry) |
887 | return; | |
888 | ||
889 | FreePool(entry->id); | |
0fa2cac4 KS |
890 | FreePool(entry->title_show); |
891 | FreePool(entry->title); | |
42cf81c2 | 892 | FreePool(entry->version); |
0fa2cac4 KS |
893 | FreePool(entry->machine_id); |
894 | FreePool(entry->loader); | |
895 | FreePool(entry->options); | |
f538cc65 LP |
896 | FreePool(entry->path); |
897 | FreePool(entry->current_name); | |
898 | FreePool(entry->next_name); | |
42cf81c2 | 899 | FreePool(entry); |
0fa2cac4 KS |
900 | } |
901 | ||
52efd56a | 902 | static BOOLEAN is_digit(CHAR16 c) { |
0fa2cac4 KS |
903 | return (c >= '0') && (c <= '9'); |
904 | } | |
905 | ||
52efd56a | 906 | static UINTN c_order(CHAR16 c) { |
0fa2cac4 KS |
907 | if (c == '\0') |
908 | return 0; | |
909 | if (is_digit(c)) | |
910 | return 0; | |
911 | else if ((c >= 'a') && (c <= 'z')) | |
912 | return c; | |
913 | else | |
914 | return c + 0x10000; | |
915 | } | |
916 | ||
52efd56a | 917 | static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) { |
0fa2cac4 KS |
918 | CHAR16 *os1 = s1; |
919 | CHAR16 *os2 = s2; | |
920 | ||
921 | while (*s1 || *s2) { | |
922 | INTN first; | |
923 | ||
924 | while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) { | |
925 | INTN order; | |
926 | ||
927 | order = c_order(*s1) - c_order(*s2); | |
64e7e27c | 928 | if (order != 0) |
0fa2cac4 KS |
929 | return order; |
930 | s1++; | |
931 | s2++; | |
932 | } | |
933 | ||
934 | while (*s1 == '0') | |
935 | s1++; | |
936 | while (*s2 == '0') | |
937 | s2++; | |
938 | ||
939 | first = 0; | |
940 | while (is_digit(*s1) && is_digit(*s2)) { | |
941 | if (first == 0) | |
942 | first = *s1 - *s2; | |
943 | s1++; | |
944 | s2++; | |
945 | } | |
946 | ||
947 | if (is_digit(*s1)) | |
948 | return 1; | |
949 | if (is_digit(*s2)) | |
950 | return -1; | |
951 | ||
64e7e27c | 952 | if (first != 0) |
0fa2cac4 KS |
953 | return first; |
954 | } | |
955 | ||
956 | return StrCmp(os1, os2); | |
957 | } | |
958 | ||
580fe4df LP |
959 | static CHAR8 *line_get_key_value( |
960 | CHAR8 *content, | |
961 | CHAR8 *sep, | |
962 | UINTN *pos, | |
963 | CHAR8 **key_ret, | |
964 | CHAR8 **value_ret) { | |
965 | ||
0fa2cac4 KS |
966 | CHAR8 *line; |
967 | UINTN linelen; | |
968 | CHAR8 *value; | |
969 | ||
970 | skip: | |
971 | line = content + *pos; | |
972 | if (*line == '\0') | |
973 | return NULL; | |
974 | ||
975 | linelen = 0; | |
976 | while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen])) | |
977 | linelen++; | |
978 | ||
979 | /* move pos to next line */ | |
980 | *pos += linelen; | |
981 | if (content[*pos]) | |
982 | (*pos)++; | |
983 | ||
984 | /* empty line */ | |
985 | if (linelen == 0) | |
986 | goto skip; | |
987 | ||
988 | /* terminate line */ | |
989 | line[linelen] = '\0'; | |
990 | ||
991 | /* remove leading whitespace */ | |
992 | while (strchra((CHAR8 *)" \t", *line)) { | |
993 | line++; | |
994 | linelen--; | |
995 | } | |
996 | ||
997 | /* remove trailing whitespace */ | |
d949c467 | 998 | while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1])) |
0fa2cac4 KS |
999 | linelen--; |
1000 | line[linelen] = '\0'; | |
1001 | ||
1002 | if (*line == '#') | |
1003 | goto skip; | |
1004 | ||
1005 | /* split key/value */ | |
1006 | value = line; | |
1007 | while (*value && !strchra(sep, *value)) | |
1008 | value++; | |
1009 | if (*value == '\0') | |
1010 | goto skip; | |
1011 | *value = '\0'; | |
1012 | value++; | |
1013 | while (*value && strchra(sep, *value)) | |
1014 | value++; | |
1015 | ||
1016 | /* unquote */ | |
e768a4f0 | 1017 | if (value[0] == '"' && line[linelen-1] == '"') { |
0fa2cac4 KS |
1018 | value++; |
1019 | line[linelen-1] = '\0'; | |
1020 | } | |
1021 | ||
1022 | *key_ret = line; | |
1023 | *value_ret = value; | |
1024 | return line; | |
1025 | } | |
1026 | ||
1027 | static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) { | |
1028 | CHAR8 *line; | |
1029 | UINTN pos = 0; | |
1030 | CHAR8 *key, *value; | |
1031 | ||
0fa2cac4 KS |
1032 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { |
1033 | if (strcmpa((CHAR8 *)"timeout", key) == 0) { | |
a42d7cf1 | 1034 | _cleanup_freepool_ CHAR16 *s = NULL; |
0fa2cac4 KS |
1035 | |
1036 | s = stra_to_str(value); | |
1037 | config->timeout_sec_config = Atoi(s); | |
1038 | config->timeout_sec = config->timeout_sec_config; | |
0fa2cac4 KS |
1039 | continue; |
1040 | } | |
1041 | ||
1042 | if (strcmpa((CHAR8 *)"default", key) == 0) { | |
1043 | FreePool(config->entry_default_pattern); | |
1044 | config->entry_default_pattern = stra_to_str(value); | |
1045 | StrLwr(config->entry_default_pattern); | |
1046 | continue; | |
1047 | } | |
4db7e6d7 KS |
1048 | |
1049 | if (strcmpa((CHAR8 *)"editor", key) == 0) { | |
1050 | BOOLEAN on; | |
1051 | ||
1052 | if (EFI_ERROR(parse_boolean(value, &on))) | |
1053 | continue; | |
7c122dfe | 1054 | |
c1d4e298 | 1055 | config->editor = on; |
7c122dfe | 1056 | continue; |
c1d4e298 JJ |
1057 | } |
1058 | ||
1059 | if (strcmpa((CHAR8 *)"auto-entries", key) == 0) { | |
1060 | BOOLEAN on; | |
1061 | ||
1062 | if (EFI_ERROR(parse_boolean(value, &on))) | |
1063 | continue; | |
7c122dfe | 1064 | |
c1d4e298 | 1065 | config->auto_entries = on; |
7c122dfe | 1066 | continue; |
c1d4e298 JJ |
1067 | } |
1068 | ||
1069 | if (strcmpa((CHAR8 *)"auto-firmware", key) == 0) { | |
1070 | BOOLEAN on; | |
1071 | ||
1072 | if (EFI_ERROR(parse_boolean(value, &on))) | |
1073 | continue; | |
7c122dfe | 1074 | |
c1d4e298 | 1075 | config->auto_firmware = on; |
7c122dfe | 1076 | continue; |
4db7e6d7 | 1077 | } |
68d4b8ac HDA |
1078 | |
1079 | if (strcmpa((CHAR8 *)"console-mode", key) == 0) { | |
68d4b8ac HDA |
1080 | if (strcmpa((CHAR8 *)"auto", value) == 0) |
1081 | config->console_mode_change = CONSOLE_MODE_AUTO; | |
1082 | else if (strcmpa((CHAR8 *)"max", value) == 0) | |
1083 | config->console_mode_change = CONSOLE_MODE_MAX; | |
1084 | else if (strcmpa((CHAR8 *)"keep", value) == 0) | |
1085 | config->console_mode_change = CONSOLE_MODE_KEEP; | |
1086 | else { | |
a42d7cf1 ZJS |
1087 | _cleanup_freepool_ CHAR16 *s = NULL; |
1088 | ||
68d4b8ac HDA |
1089 | s = stra_to_str(value); |
1090 | config->console_mode = Atoi(s); | |
1091 | config->console_mode_change = CONSOLE_MODE_SET; | |
68d4b8ac HDA |
1092 | } |
1093 | ||
1094 | continue; | |
1095 | } | |
e4dcf7aa LP |
1096 | |
1097 | if (strcmpa((CHAR8*) "random-seed-mode", key) == 0) { | |
1098 | if (strcmpa((CHAR8*) "off", value) == 0) | |
1099 | config->random_seed_mode = RANDOM_SEED_OFF; | |
1100 | else if (strcmpa((CHAR8*) "with-system-token", value) == 0) | |
1101 | config->random_seed_mode = RANDOM_SEED_WITH_SYSTEM_TOKEN; | |
1102 | else if (strcmpa((CHAR8*) "always", value) == 0) | |
1103 | config->random_seed_mode = RANDOM_SEED_ALWAYS; | |
1104 | else { | |
1105 | BOOLEAN on; | |
1106 | ||
1107 | if (EFI_ERROR(parse_boolean(value, &on))) | |
1108 | continue; | |
1109 | ||
1110 | config->random_seed_mode = on ? RANDOM_SEED_ALWAYS : RANDOM_SEED_OFF; | |
1111 | } | |
1112 | } | |
0fa2cac4 KS |
1113 | } |
1114 | } | |
1115 | ||
f538cc65 LP |
1116 | static VOID config_entry_parse_tries( |
1117 | ConfigEntry *entry, | |
1118 | CHAR16 *path, | |
1119 | CHAR16 *file, | |
1120 | CHAR16 *suffix) { | |
1121 | ||
1122 | UINTN left = (UINTN) -1, done = (UINTN) -1, factor = 1, i, next_left, next_done; | |
1123 | _cleanup_freepool_ CHAR16 *prefix = NULL; | |
1124 | ||
1125 | /* | |
1126 | * Parses a suffix of two counters (one going down, one going up) in the form "+LEFT-DONE" from the end of the | |
1127 | * filename (but before the .efi/.conf suffix), where the "-DONE" part is optional and may be left out (in | |
1128 | * which case that counter as assumed to be zero, i.e. the missing part is synonymous to "-0"). | |
1129 | * | |
1130 | * Names we grok, and the series they result in: | |
1131 | * | |
1132 | * foobar+3.efi → foobar+2-1.efi → foobar+1-2.efi → foobar+0-3.efi → STOP! | |
1133 | * foobar+4-0.efi → foobar+3-1.efi → foobar+2-2.efi → foobar+1-3.efi → foobar+0-4.efi → STOP! | |
1134 | */ | |
1135 | ||
1136 | i = StrLen(file); | |
1137 | ||
1138 | /* Chop off any suffix such as ".conf" or ".efi" */ | |
1139 | if (suffix) { | |
1140 | UINTN suffix_length; | |
1141 | ||
1142 | suffix_length = StrLen(suffix); | |
1143 | if (i < suffix_length) | |
1144 | return; | |
1145 | ||
1146 | i -= suffix_length; | |
1147 | } | |
1148 | ||
1149 | /* Go backwards through the string and parse everything we encounter */ | |
1150 | for (;;) { | |
1151 | if (i == 0) | |
1152 | return; | |
1153 | ||
1154 | i--; | |
1155 | ||
1156 | switch (file[i]) { | |
1157 | ||
1158 | case '+': | |
1159 | if (left == (UINTN) -1) /* didn't read at least one digit for 'left'? */ | |
1160 | return; | |
1161 | ||
1162 | if (done == (UINTN) -1) /* no 'done' counter? If so, it's equivalent to 0 */ | |
1163 | done = 0; | |
1164 | ||
1165 | goto good; | |
1166 | ||
1167 | case '-': | |
1168 | if (left == (UINTN) -1) /* didn't parse any digit yet? */ | |
1169 | return; | |
1170 | ||
1171 | if (done != (UINTN) -1) /* already encountered a dash earlier? */ | |
1172 | return; | |
1173 | ||
1174 | /* So we encountered a dash. This means this counter is of the form +LEFT-DONE. Let's assign | |
1175 | * what we already parsed to 'done', and start fresh for the 'left' part. */ | |
1176 | ||
1177 | done = left; | |
1178 | left = (UINTN) -1; | |
1179 | factor = 1; | |
1180 | break; | |
1181 | ||
1182 | case '0'...'9': { | |
1183 | UINTN new_factor; | |
1184 | ||
1185 | if (left == (UINTN) -1) | |
1186 | left = file[i] - '0'; | |
1187 | else { | |
1188 | UINTN new_left, digit; | |
1189 | ||
1190 | digit = file[i] - '0'; | |
1191 | if (digit > (UINTN) -1 / factor) /* overflow check */ | |
1192 | return; | |
1193 | ||
1194 | new_left = left + digit * factor; | |
1195 | if (new_left < left) /* overflow check */ | |
1196 | return; | |
1197 | ||
1198 | if (new_left == (UINTN) -1) /* don't allow us to be confused */ | |
1199 | return; | |
1200 | } | |
1201 | ||
1202 | new_factor = factor * 10; | |
1203 | if (new_factor < factor) /* overflow chck */ | |
1204 | return; | |
1205 | ||
1206 | factor = new_factor; | |
1207 | break; | |
1208 | } | |
1209 | ||
1210 | default: | |
1211 | return; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | good: | |
1216 | entry->tries_left = left; | |
1217 | entry->tries_done = done; | |
1218 | ||
1219 | entry->path = StrDuplicate(path); | |
1220 | entry->current_name = StrDuplicate(file); | |
1221 | ||
1222 | next_left = left <= 0 ? 0 : left - 1; | |
1223 | next_done = done >= (UINTN) -2 ? (UINTN) -2 : done + 1; | |
1224 | ||
1225 | prefix = StrDuplicate(file); | |
1226 | prefix[i] = 0; | |
1227 | ||
1228 | entry->next_name = PoolPrint(L"%s+%u-%u%s", prefix, next_left, next_done, suffix ?: L""); | |
1229 | } | |
1230 | ||
1231 | static VOID config_entry_bump_counters( | |
1232 | ConfigEntry *entry, | |
1233 | EFI_FILE_HANDLE root_dir) { | |
1234 | ||
1235 | _cleanup_freepool_ CHAR16* old_path = NULL, *new_path = NULL; | |
1236 | _cleanup_(FileHandleClosep) EFI_FILE_HANDLE handle = NULL; | |
1237 | static EFI_GUID EfiFileInfoGuid = EFI_FILE_INFO_ID; | |
1238 | _cleanup_freepool_ EFI_FILE_INFO *file_info = NULL; | |
1239 | UINTN file_info_size, a, b; | |
1240 | EFI_STATUS r; | |
1241 | ||
1242 | if (entry->tries_left == (UINTN) -1) | |
1243 | return; | |
1244 | ||
1245 | if (!entry->path || !entry->current_name || !entry->next_name) | |
1246 | return; | |
1247 | ||
1248 | old_path = PoolPrint(L"%s\\%s", entry->path, entry->current_name); | |
1249 | ||
1250 | r = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, old_path, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0ULL); | |
1251 | if (EFI_ERROR(r)) | |
1252 | return; | |
1253 | ||
1254 | a = StrLen(entry->current_name); | |
1255 | b = StrLen(entry->next_name); | |
1256 | ||
1257 | file_info_size = OFFSETOF(EFI_FILE_INFO, FileName) + (a > b ? a : b) + 1; | |
1258 | ||
1259 | for (;;) { | |
1260 | file_info = AllocatePool(file_info_size); | |
1261 | ||
1262 | r = uefi_call_wrapper(handle->GetInfo, 4, handle, &EfiFileInfoGuid, &file_info_size, file_info); | |
1263 | if (!EFI_ERROR(r)) | |
1264 | break; | |
1265 | ||
1266 | if (r != EFI_BUFFER_TOO_SMALL || file_info_size * 2 < file_info_size) { | |
1267 | Print(L"\nFailed to get file info for '%s': %r\n", old_path, r); | |
1268 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1269 | return; | |
1270 | } | |
1271 | ||
1272 | file_info_size *= 2; | |
1273 | FreePool(file_info); | |
1274 | } | |
1275 | ||
1276 | /* And rename the file */ | |
1277 | StrCpy(file_info->FileName, entry->next_name); | |
1278 | r = uefi_call_wrapper(handle->SetInfo, 4, handle, &EfiFileInfoGuid, file_info_size, file_info); | |
1279 | if (EFI_ERROR(r)) { | |
1280 | Print(L"\nFailed to rename '%s' to '%s', ignoring: %r\n", old_path, entry->next_name, r); | |
1281 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1282 | return; | |
1283 | } | |
1284 | ||
1285 | /* Flush everything to disk, just in case… */ | |
1286 | (void) uefi_call_wrapper(handle->Flush, 1, handle); | |
1287 | ||
1288 | /* Let's tell the OS that we renamed this file, so that it knows what to rename to the counter-less name on | |
1289 | * success */ | |
1290 | new_path = PoolPrint(L"%s\\%s", entry->path, entry->next_name); | |
1291 | efivar_set(L"LoaderBootCountPath", new_path, FALSE); | |
1292 | ||
1293 | /* If the file we just renamed is the loader path, then let's update that. */ | |
1294 | if (StrCmp(entry->loader, old_path) == 0) { | |
1295 | FreePool(entry->loader); | |
c1db999e | 1296 | entry->loader = TAKE_PTR(new_path); |
f538cc65 LP |
1297 | } |
1298 | } | |
1299 | ||
580fe4df LP |
1300 | static VOID config_entry_add_from_file( |
1301 | Config *config, | |
1302 | EFI_HANDLE *device, | |
f9f1d01c | 1303 | EFI_FILE *root_dir, |
f538cc65 | 1304 | CHAR16 *path, |
580fe4df LP |
1305 | CHAR16 *file, |
1306 | CHAR8 *content, | |
1307 | CHAR16 *loaded_image_path) { | |
1308 | ||
0fa2cac4 KS |
1309 | ConfigEntry *entry; |
1310 | CHAR8 *line; | |
1311 | UINTN pos = 0; | |
1312 | CHAR8 *key, *value; | |
f9f1d01c JJ |
1313 | EFI_STATUS err; |
1314 | EFI_FILE_HANDLE handle; | |
a42d7cf1 | 1315 | _cleanup_freepool_ CHAR16 *initrd = NULL; |
0fa2cac4 | 1316 | |
f538cc65 LP |
1317 | entry = AllocatePool(sizeof(ConfigEntry)); |
1318 | ||
1319 | *entry = (ConfigEntry) { | |
1320 | .tries_done = (UINTN) -1, | |
1321 | .tries_left = (UINTN) -1, | |
1322 | }; | |
0fa2cac4 | 1323 | |
0fa2cac4 KS |
1324 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { |
1325 | if (strcmpa((CHAR8 *)"title", key) == 0) { | |
1326 | FreePool(entry->title); | |
1327 | entry->title = stra_to_str(value); | |
1328 | continue; | |
1329 | } | |
1330 | ||
1331 | if (strcmpa((CHAR8 *)"version", key) == 0) { | |
1332 | FreePool(entry->version); | |
1333 | entry->version = stra_to_str(value); | |
1334 | continue; | |
1335 | } | |
1336 | ||
1337 | if (strcmpa((CHAR8 *)"machine-id", key) == 0) { | |
1338 | FreePool(entry->machine_id); | |
1339 | entry->machine_id = stra_to_str(value); | |
1340 | continue; | |
1341 | } | |
1342 | ||
1343 | if (strcmpa((CHAR8 *)"linux", key) == 0) { | |
1344 | FreePool(entry->loader); | |
1345 | entry->type = LOADER_LINUX; | |
1346 | entry->loader = stra_to_path(value); | |
1347 | entry->key = 'l'; | |
1348 | continue; | |
1349 | } | |
1350 | ||
1351 | if (strcmpa((CHAR8 *)"efi", key) == 0) { | |
1352 | entry->type = LOADER_EFI; | |
1353 | FreePool(entry->loader); | |
1354 | entry->loader = stra_to_path(value); | |
1355 | ||
1356 | /* do not add an entry for ourselves */ | |
17ad0f0f | 1357 | if (loaded_image_path && StriCmp(entry->loader, loaded_image_path) == 0) { |
0fa2cac4 KS |
1358 | entry->type = LOADER_UNDEFINED; |
1359 | break; | |
1360 | } | |
1361 | continue; | |
1362 | } | |
1363 | ||
1364 | if (strcmpa((CHAR8 *)"architecture", key) == 0) { | |
1365 | /* do not add an entry for an EFI image of architecture not matching with that of the image */ | |
1366 | if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) { | |
1367 | entry->type = LOADER_UNDEFINED; | |
1368 | break; | |
1369 | } | |
1370 | continue; | |
1371 | } | |
1372 | ||
1373 | if (strcmpa((CHAR8 *)"initrd", key) == 0) { | |
a42d7cf1 | 1374 | _cleanup_freepool_ CHAR16 *new = NULL; |
0fa2cac4 KS |
1375 | |
1376 | new = stra_to_path(value); | |
1377 | if (initrd) { | |
1378 | CHAR16 *s; | |
1379 | ||
1380 | s = PoolPrint(L"%s initrd=%s", initrd, new); | |
1381 | FreePool(initrd); | |
1382 | initrd = s; | |
1383 | } else | |
1384 | initrd = PoolPrint(L"initrd=%s", new); | |
a42d7cf1 | 1385 | |
0fa2cac4 KS |
1386 | continue; |
1387 | } | |
1388 | ||
1389 | if (strcmpa((CHAR8 *)"options", key) == 0) { | |
a42d7cf1 | 1390 | _cleanup_freepool_ CHAR16 *new = NULL; |
0fa2cac4 KS |
1391 | |
1392 | new = stra_to_str(value); | |
1393 | if (entry->options) { | |
1394 | CHAR16 *s; | |
1395 | ||
1396 | s = PoolPrint(L"%s %s", entry->options, new); | |
1397 | FreePool(entry->options); | |
1398 | entry->options = s; | |
c1db999e LP |
1399 | } else |
1400 | entry->options = TAKE_PTR(new); | |
a42d7cf1 | 1401 | |
0fa2cac4 KS |
1402 | continue; |
1403 | } | |
0fa2cac4 KS |
1404 | } |
1405 | ||
1406 | if (entry->type == LOADER_UNDEFINED) { | |
1407 | config_entry_free(entry); | |
0fa2cac4 KS |
1408 | return; |
1409 | } | |
1410 | ||
f9f1d01c JJ |
1411 | /* check existence */ |
1412 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, entry->loader, EFI_FILE_MODE_READ, 0ULL); | |
1413 | if (EFI_ERROR(err)) { | |
1414 | config_entry_free(entry); | |
1415 | return; | |
1416 | } | |
1417 | uefi_call_wrapper(handle->Close, 1, handle); | |
1418 | ||
0fa2cac4 KS |
1419 | /* add initrd= to options */ |
1420 | if (entry->type == LOADER_LINUX && initrd) { | |
1421 | if (entry->options) { | |
1422 | CHAR16 *s; | |
1423 | ||
1424 | s = PoolPrint(L"%s %s", initrd, entry->options); | |
1425 | FreePool(entry->options); | |
1426 | entry->options = s; | |
c1db999e LP |
1427 | } else |
1428 | entry->options = TAKE_PTR(initrd); | |
0fa2cac4 | 1429 | } |
0fa2cac4 | 1430 | |
0fa2cac4 | 1431 | entry->device = device; |
081cc95f | 1432 | entry->id = StrDuplicate(file); |
081cc95f | 1433 | StrLwr(entry->id); |
0fa2cac4 KS |
1434 | |
1435 | config_add_entry(config, entry); | |
f538cc65 LP |
1436 | |
1437 | config_entry_parse_tries(entry, path, file, L".conf"); | |
0fa2cac4 KS |
1438 | } |
1439 | ||
540536ff | 1440 | static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) { |
a42d7cf1 | 1441 | _cleanup_freepool_ CHAR8 *content = NULL; |
0fa2cac4 | 1442 | UINTN sec; |
540536ff | 1443 | EFI_STATUS err; |
0fa2cac4 | 1444 | |
aec1443a LP |
1445 | *config = (Config) { |
1446 | .editor = TRUE, | |
1447 | .auto_entries = TRUE, | |
1448 | .auto_firmware = TRUE, | |
e4dcf7aa | 1449 | .random_seed_mode = RANDOM_SEED_WITH_SYSTEM_TOKEN, |
aec1443a | 1450 | }; |
c1d4e298 | 1451 | |
33d4ba32 JJ |
1452 | err = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content, NULL); |
1453 | if (!EFI_ERROR(err)) | |
0fa2cac4 | 1454 | config_defaults_load_from_file(config, content); |
0fa2cac4 KS |
1455 | |
1456 | err = efivar_get_int(L"LoaderConfigTimeout", &sec); | |
1457 | if (!EFI_ERROR(err)) { | |
2366d923 | 1458 | config->timeout_sec_efivar = sec > INTN_MAX ? INTN_MAX : sec; |
0fa2cac4 KS |
1459 | config->timeout_sec = sec; |
1460 | } else | |
1461 | config->timeout_sec_efivar = -1; | |
fe2579dd LP |
1462 | |
1463 | err = efivar_get_int(L"LoaderConfigTimeoutOneShot", &sec); | |
1464 | if (!EFI_ERROR(err)) { | |
1465 | /* Unset variable now, after all it's "one shot". */ | |
1466 | (void) efivar_set(L"LoaderConfigTimeoutOneShot", NULL, TRUE); | |
1467 | ||
1468 | config->timeout_sec = sec; | |
1469 | config->force_menu = TRUE; /* force the menu when this is set */ | |
1470 | } | |
540536ff KS |
1471 | } |
1472 | ||
580fe4df LP |
1473 | static VOID config_load_entries( |
1474 | Config *config, | |
1475 | EFI_HANDLE *device, | |
1476 | EFI_FILE *root_dir, | |
1477 | CHAR16 *loaded_image_path) { | |
1478 | ||
540536ff KS |
1479 | EFI_FILE_HANDLE entries_dir; |
1480 | EFI_STATUS err; | |
0fa2cac4 KS |
1481 | |
1482 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL); | |
1483 | if (!EFI_ERROR(err)) { | |
1484 | for (;;) { | |
1485 | CHAR16 buf[256]; | |
1486 | UINTN bufsize; | |
1487 | EFI_FILE_INFO *f; | |
a42d7cf1 | 1488 | _cleanup_freepool_ CHAR8 *content = NULL; |
0fa2cac4 KS |
1489 | UINTN len; |
1490 | ||
1491 | bufsize = sizeof(buf); | |
1492 | err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf); | |
1493 | if (bufsize == 0 || EFI_ERROR(err)) | |
1494 | break; | |
1495 | ||
1496 | f = (EFI_FILE_INFO *) buf; | |
1497 | if (f->FileName[0] == '.') | |
1498 | continue; | |
1499 | if (f->Attribute & EFI_FILE_DIRECTORY) | |
1500 | continue; | |
9d3dec15 | 1501 | |
0fa2cac4 KS |
1502 | len = StrLen(f->FileName); |
1503 | if (len < 6) | |
1504 | continue; | |
1505 | if (StriCmp(f->FileName + len - 5, L".conf") != 0) | |
1506 | continue; | |
9d3dec15 KS |
1507 | if (StrnCmp(f->FileName, L"auto-", 5) == 0) |
1508 | continue; | |
0fa2cac4 | 1509 | |
33d4ba32 JJ |
1510 | err = file_read(entries_dir, f->FileName, 0, 0, &content, NULL); |
1511 | if (!EFI_ERROR(err)) | |
f9f1d01c | 1512 | config_entry_add_from_file(config, device, root_dir, L"\\loader\\entries", f->FileName, content, loaded_image_path); |
0fa2cac4 KS |
1513 | } |
1514 | uefi_call_wrapper(entries_dir->Close, 1, entries_dir); | |
1515 | } | |
540536ff KS |
1516 | } |
1517 | ||
f538cc65 LP |
1518 | static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) { |
1519 | INTN r; | |
1520 | ||
1521 | /* Order entries that have no tries left to the end of the list */ | |
1522 | if (a->tries_left != 0 && b->tries_left == 0) | |
1523 | return -1; | |
1524 | if (a->tries_left == 0 && b->tries_left != 0) | |
1525 | return 1; | |
1526 | ||
1527 | r = str_verscmp(a->id, b->id); | |
1528 | if (r != 0) | |
1529 | return r; | |
1530 | ||
1531 | if (a->tries_left == (UINTN) -1 || | |
1532 | b->tries_left == (UINTN) -1) | |
1533 | return 0; | |
1534 | ||
1535 | /* If both items have boot counting, and otherwise are identical, put the entry with more tries left first */ | |
1536 | if (a->tries_left > b->tries_left) | |
1537 | return -1; | |
1538 | if (a->tries_left < b->tries_left) | |
1539 | return 1; | |
1540 | ||
1541 | /* If they have the same number of tries left, then let the one win which was tried fewer times so far */ | |
1542 | if (a->tries_done < b->tries_done) | |
1543 | return -1; | |
1544 | if (a->tries_done > b->tries_done) | |
1545 | return 1; | |
1546 | ||
1547 | return 0; | |
1548 | } | |
1549 | ||
540536ff KS |
1550 | static VOID config_sort_entries(Config *config) { |
1551 | UINTN i; | |
0fa2cac4 | 1552 | |
0fa2cac4 KS |
1553 | for (i = 1; i < config->entry_count; i++) { |
1554 | BOOLEAN more; | |
1555 | UINTN k; | |
1556 | ||
1557 | more = FALSE; | |
1558 | for (k = 0; k < config->entry_count - i; k++) { | |
1559 | ConfigEntry *entry; | |
1560 | ||
f538cc65 | 1561 | if (config_entry_compare(config->entries[k], config->entries[k+1]) <= 0) |
0fa2cac4 | 1562 | continue; |
f538cc65 | 1563 | |
0fa2cac4 KS |
1564 | entry = config->entries[k]; |
1565 | config->entries[k] = config->entries[k+1]; | |
1566 | config->entries[k+1] = entry; | |
1567 | more = TRUE; | |
1568 | } | |
1569 | if (!more) | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | ||
535610b5 LP |
1574 | static INTN config_entry_find(Config *config, CHAR16 *id) { |
1575 | UINTN i; | |
1576 | ||
1577 | for (i = 0; i < config->entry_count; i++) | |
1578 | if (StrCmp(config->entries[i]->id, id) == 0) | |
1579 | return (INTN) i; | |
1580 | ||
1581 | return -1; | |
1582 | } | |
1583 | ||
0fa2cac4 | 1584 | static VOID config_default_entry_select(Config *config) { |
a42d7cf1 | 1585 | _cleanup_freepool_ CHAR16 *entry_oneshot = NULL, *entry_default = NULL; |
0fa2cac4 | 1586 | EFI_STATUS err; |
535610b5 | 1587 | INTN i; |
0fa2cac4 KS |
1588 | |
1589 | /* | |
1590 | * The EFI variable to specify a boot entry for the next, and only the | |
1591 | * next reboot. The variable is always cleared directly after it is read. | |
1592 | */ | |
a42d7cf1 | 1593 | err = efivar_get(L"LoaderEntryOneShot", &entry_oneshot); |
0fa2cac4 | 1594 | if (!EFI_ERROR(err)) { |
0fa2cac4 | 1595 | |
a42d7cf1 | 1596 | config->entry_oneshot = StrDuplicate(entry_oneshot); |
0fa2cac4 | 1597 | efivar_set(L"LoaderEntryOneShot", NULL, TRUE); |
535610b5 LP |
1598 | |
1599 | i = config_entry_find(config, entry_oneshot); | |
1600 | if (i >= 0) { | |
1601 | config->idx_default = i; | |
0fa2cac4 | 1602 | return; |
535610b5 | 1603 | } |
0fa2cac4 KS |
1604 | } |
1605 | ||
1606 | /* | |
1607 | * The EFI variable to select the default boot entry overrides the | |
1608 | * configured pattern. The variable can be set and cleared by pressing | |
1609 | * the 'd' key in the loader selection menu, the entry is marked with | |
1610 | * an '*'. | |
1611 | */ | |
a42d7cf1 | 1612 | err = efivar_get(L"LoaderEntryDefault", &entry_default); |
0fa2cac4 | 1613 | if (!EFI_ERROR(err)) { |
535610b5 LP |
1614 | |
1615 | i = config_entry_find(config, entry_default); | |
1616 | if (i >= 0) { | |
1617 | config->idx_default = i; | |
1618 | config->idx_default_efivar = i; | |
1619 | return; | |
1620 | } | |
0fa2cac4 KS |
1621 | } |
1622 | config->idx_default_efivar = -1; | |
1623 | ||
1624 | if (config->entry_count == 0) | |
1625 | return; | |
1626 | ||
1627 | /* | |
1628 | * Match the pattern from the end of the list to the start, find last | |
1629 | * entry (largest number) matching the given pattern. | |
1630 | */ | |
1631 | if (config->entry_default_pattern) { | |
1632 | i = config->entry_count; | |
1633 | while (i--) { | |
1634 | if (config->entries[i]->no_autoselect) | |
1635 | continue; | |
081cc95f | 1636 | if (MetaiMatch(config->entries[i]->id, config->entry_default_pattern)) { |
0fa2cac4 KS |
1637 | config->idx_default = i; |
1638 | return; | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | /* select the last suitable entry */ | |
1644 | i = config->entry_count; | |
1645 | while (i--) { | |
1646 | if (config->entries[i]->no_autoselect) | |
1647 | continue; | |
1648 | config->idx_default = i; | |
1649 | return; | |
1650 | } | |
1651 | ||
1652 | /* no entry found */ | |
1653 | config->idx_default = -1; | |
1654 | } | |
1655 | ||
a2fa605a ZJS |
1656 | static BOOLEAN find_nonunique(ConfigEntry **entries, UINTN entry_count) { |
1657 | BOOLEAN non_unique = FALSE; | |
1658 | UINTN i, k; | |
1659 | ||
1660 | for (i = 0; i < entry_count; i++) | |
1661 | entries[i]->non_unique = FALSE; | |
1662 | ||
1663 | for (i = 0; i < entry_count; i++) | |
1664 | for (k = 0; k < entry_count; k++) { | |
1665 | if (i == k) | |
1666 | continue; | |
1667 | if (StrCmp(entries[i]->title_show, entries[k]->title_show) != 0) | |
1668 | continue; | |
1669 | ||
1670 | non_unique = entries[i]->non_unique = entries[k]->non_unique = TRUE; | |
1671 | } | |
1672 | ||
1673 | return non_unique; | |
1674 | } | |
1675 | ||
0fa2cac4 KS |
1676 | /* generate a unique title, avoiding non-distinguishable menu entries */ |
1677 | static VOID config_title_generate(Config *config) { | |
a2fa605a | 1678 | UINTN i; |
0fa2cac4 KS |
1679 | |
1680 | /* set title */ | |
1681 | for (i = 0; i < config->entry_count; i++) { | |
1682 | CHAR16 *title; | |
1683 | ||
1684 | FreePool(config->entries[i]->title_show); | |
1685 | title = config->entries[i]->title; | |
1686 | if (!title) | |
081cc95f | 1687 | title = config->entries[i]->id; |
0fa2cac4 KS |
1688 | config->entries[i]->title_show = StrDuplicate(title); |
1689 | } | |
1690 | ||
a2fa605a | 1691 | if (!find_nonunique(config->entries, config->entry_count)) |
0fa2cac4 KS |
1692 | return; |
1693 | ||
1694 | /* add version to non-unique titles */ | |
1695 | for (i = 0; i < config->entry_count; i++) { | |
1696 | CHAR16 *s; | |
1697 | ||
1698 | if (!config->entries[i]->non_unique) | |
1699 | continue; | |
1700 | if (!config->entries[i]->version) | |
1701 | continue; | |
1702 | ||
1703 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version); | |
1704 | FreePool(config->entries[i]->title_show); | |
1705 | config->entries[i]->title_show = s; | |
0fa2cac4 KS |
1706 | } |
1707 | ||
a2fa605a | 1708 | if (!find_nonunique(config->entries, config->entry_count)) |
0fa2cac4 KS |
1709 | return; |
1710 | ||
1711 | /* add machine-id to non-unique titles */ | |
1712 | for (i = 0; i < config->entry_count; i++) { | |
1713 | CHAR16 *s; | |
a42d7cf1 | 1714 | _cleanup_freepool_ CHAR16 *m = NULL; |
0fa2cac4 KS |
1715 | |
1716 | if (!config->entries[i]->non_unique) | |
1717 | continue; | |
1718 | if (!config->entries[i]->machine_id) | |
1719 | continue; | |
1720 | ||
1721 | m = StrDuplicate(config->entries[i]->machine_id); | |
1722 | m[8] = '\0'; | |
1723 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m); | |
1724 | FreePool(config->entries[i]->title_show); | |
1725 | config->entries[i]->title_show = s; | |
0fa2cac4 KS |
1726 | } |
1727 | ||
a2fa605a | 1728 | if (!find_nonunique(config->entries, config->entry_count)) |
0fa2cac4 KS |
1729 | return; |
1730 | ||
1731 | /* add file name to non-unique titles */ | |
1732 | for (i = 0; i < config->entry_count; i++) { | |
1733 | CHAR16 *s; | |
1734 | ||
1735 | if (!config->entries[i]->non_unique) | |
1736 | continue; | |
081cc95f | 1737 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->id); |
0fa2cac4 KS |
1738 | FreePool(config->entries[i]->title_show); |
1739 | config->entries[i]->title_show = s; | |
1740 | config->entries[i]->non_unique = FALSE; | |
1741 | } | |
1742 | } | |
1743 | ||
580fe4df LP |
1744 | static BOOLEAN config_entry_add_call( |
1745 | Config *config, | |
7f1ef125 | 1746 | CHAR16 *id, |
580fe4df LP |
1747 | CHAR16 *title, |
1748 | EFI_STATUS (*call)(VOID)) { | |
1749 | ||
0fa2cac4 KS |
1750 | ConfigEntry *entry; |
1751 | ||
f538cc65 LP |
1752 | entry = AllocatePool(sizeof(ConfigEntry)); |
1753 | *entry = (ConfigEntry) { | |
7f1ef125 | 1754 | .id = StrDuplicate(id), |
f538cc65 LP |
1755 | .title = StrDuplicate(title), |
1756 | .call = call, | |
1757 | .no_autoselect = TRUE, | |
1758 | .tries_done = (UINTN) -1, | |
1759 | .tries_left = (UINTN) -1, | |
1760 | }; | |
1761 | ||
0fa2cac4 KS |
1762 | config_add_entry(config, entry); |
1763 | return TRUE; | |
1764 | } | |
1765 | ||
580fe4df LP |
1766 | static ConfigEntry *config_entry_add_loader( |
1767 | Config *config, | |
1768 | EFI_HANDLE *device, | |
1769 | enum loader_type type, | |
081cc95f | 1770 | CHAR16 *id, |
580fe4df LP |
1771 | CHAR16 key, |
1772 | CHAR16 *title, | |
10d0024a SM |
1773 | CHAR16 *loader, |
1774 | CHAR16 *version) { | |
580fe4df | 1775 | |
0fa2cac4 KS |
1776 | ConfigEntry *entry; |
1777 | ||
f538cc65 LP |
1778 | entry = AllocatePool(sizeof(ConfigEntry)); |
1779 | *entry = (ConfigEntry) { | |
1780 | .type = type, | |
1781 | .title = StrDuplicate(title), | |
10d0024a | 1782 | .version = StrDuplicate(version), |
f538cc65 LP |
1783 | .device = device, |
1784 | .loader = StrDuplicate(loader), | |
1785 | .id = StrDuplicate(id), | |
1786 | .key = key, | |
1787 | .tries_done = (UINTN) -1, | |
1788 | .tries_left = (UINTN) -1, | |
1789 | }; | |
1790 | ||
081cc95f | 1791 | StrLwr(entry->id); |
0fa2cac4 | 1792 | |
f538cc65 | 1793 | config_add_entry(config, entry); |
0fa2cac4 KS |
1794 | return entry; |
1795 | } | |
1796 | ||
580fe4df LP |
1797 | static BOOLEAN config_entry_add_loader_auto( |
1798 | Config *config, | |
1799 | EFI_HANDLE *device, | |
1800 | EFI_FILE *root_dir, | |
1801 | CHAR16 *loaded_image_path, | |
081cc95f | 1802 | CHAR16 *id, |
580fe4df LP |
1803 | CHAR16 key, |
1804 | CHAR16 *title, | |
1805 | CHAR16 *loader) { | |
1806 | ||
0fa2cac4 KS |
1807 | EFI_FILE_HANDLE handle; |
1808 | ConfigEntry *entry; | |
1809 | EFI_STATUS err; | |
1810 | ||
c1d4e298 JJ |
1811 | if (!config->auto_entries) |
1812 | return FALSE; | |
1813 | ||
0fa2cac4 | 1814 | /* do not add an entry for ourselves */ |
4c8c9f9f JJ |
1815 | if (loaded_image_path) { |
1816 | UINTN len; | |
a42d7cf1 | 1817 | _cleanup_freepool_ CHAR8 *content = NULL; |
4c8c9f9f JJ |
1818 | |
1819 | if (StriCmp(loader, loaded_image_path) == 0) | |
1820 | return FALSE; | |
1821 | ||
1822 | /* look for systemd-boot magic string */ | |
1823 | err = file_read(root_dir, loader, 0, 100*1024, &content, &len); | |
1824 | if (!EFI_ERROR(err)) { | |
1825 | CHAR8 *start = content; | |
1826 | CHAR8 *last = content + len - sizeof(magic) - 1; | |
1827 | ||
1828 | for (; start <= last; start++) | |
a42d7cf1 | 1829 | if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0) |
4c8c9f9f | 1830 | return FALSE; |
4c8c9f9f JJ |
1831 | } |
1832 | } | |
0fa2cac4 KS |
1833 | |
1834 | /* check existence */ | |
1835 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL); | |
1836 | if (EFI_ERROR(err)) | |
1837 | return FALSE; | |
1838 | uefi_call_wrapper(handle->Close, 1, handle); | |
1839 | ||
10d0024a | 1840 | entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, id, key, title, loader, NULL); |
0fa2cac4 KS |
1841 | if (!entry) |
1842 | return FALSE; | |
1843 | ||
1844 | /* do not boot right away into auto-detected entries */ | |
1845 | entry->no_autoselect = TRUE; | |
1846 | ||
0fa2cac4 KS |
1847 | return TRUE; |
1848 | } | |
1849 | ||
1850 | static VOID config_entry_add_osx(Config *config) { | |
1851 | EFI_STATUS err; | |
1852 | UINTN handle_count = 0; | |
a42d7cf1 | 1853 | _cleanup_freepool_ EFI_HANDLE *handles = NULL; |
0fa2cac4 | 1854 | |
c1d4e298 JJ |
1855 | if (!config->auto_entries) |
1856 | return; | |
1857 | ||
0fa2cac4 KS |
1858 | err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles); |
1859 | if (!EFI_ERROR(err)) { | |
1860 | UINTN i; | |
1861 | ||
1862 | for (i = 0; i < handle_count; i++) { | |
1863 | EFI_FILE *root; | |
1864 | BOOLEAN found; | |
1865 | ||
1866 | root = LibOpenRoot(handles[i]); | |
1867 | if (!root) | |
1868 | continue; | |
b3f76b0d | 1869 | found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"macOS", |
0fa2cac4 KS |
1870 | L"\\System\\Library\\CoreServices\\boot.efi"); |
1871 | uefi_call_wrapper(root->Close, 1, root); | |
1872 | if (found) | |
1873 | break; | |
1874 | } | |
0fa2cac4 KS |
1875 | } |
1876 | } | |
1877 | ||
580fe4df LP |
1878 | static VOID config_entry_add_linux( |
1879 | Config *config, | |
c8ad5996 | 1880 | EFI_HANDLE *device, |
580fe4df LP |
1881 | EFI_FILE *root_dir) { |
1882 | ||
0fa2cac4 KS |
1883 | EFI_FILE_HANDLE linux_dir; |
1884 | EFI_STATUS err; | |
b1da143f | 1885 | ConfigEntry *entry; |
0fa2cac4 KS |
1886 | |
1887 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL); | |
12643e7c ZJS |
1888 | if (EFI_ERROR(err)) |
1889 | return; | |
0fa2cac4 | 1890 | |
12643e7c ZJS |
1891 | for (;;) { |
1892 | CHAR16 buf[256]; | |
1893 | UINTN bufsize = sizeof buf; | |
1894 | EFI_FILE_INFO *f; | |
1895 | CHAR8 *sections[] = { | |
1896 | (UINT8 *)".osrel", | |
1897 | (UINT8 *)".cmdline", | |
1898 | NULL | |
1899 | }; | |
1900 | UINTN offs[ELEMENTSOF(sections)-1] = {}; | |
1901 | UINTN szs[ELEMENTSOF(sections)-1] = {}; | |
1902 | UINTN addrs[ELEMENTSOF(sections)-1] = {}; | |
1903 | CHAR8 *content = NULL; | |
1904 | UINTN len; | |
1905 | CHAR8 *line; | |
1906 | UINTN pos = 0; | |
1907 | CHAR8 *key, *value; | |
10d0024a | 1908 | CHAR16 *os_name_pretty = NULL; |
12643e7c ZJS |
1909 | CHAR16 *os_name = NULL; |
1910 | CHAR16 *os_id = NULL; | |
1911 | CHAR16 *os_version = NULL; | |
10d0024a SM |
1912 | CHAR16 *os_version_id = NULL; |
1913 | CHAR16 *os_build_id = NULL; | |
12643e7c ZJS |
1914 | |
1915 | err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf); | |
1916 | if (bufsize == 0 || EFI_ERROR(err)) | |
1917 | break; | |
0fa2cac4 | 1918 | |
12643e7c ZJS |
1919 | f = (EFI_FILE_INFO *) buf; |
1920 | if (f->FileName[0] == '.') | |
1921 | continue; | |
1922 | if (f->Attribute & EFI_FILE_DIRECTORY) | |
1923 | continue; | |
1924 | len = StrLen(f->FileName); | |
1925 | if (len < 5) | |
1926 | continue; | |
1927 | if (StriCmp(f->FileName + len - 4, L".efi") != 0) | |
1928 | continue; | |
65901c0f SM |
1929 | if (StrnCmp(f->FileName, L"auto-", 5) == 0) |
1930 | continue; | |
0fa2cac4 | 1931 | |
12643e7c ZJS |
1932 | /* look for .osrel and .cmdline sections in the .efi binary */ |
1933 | err = pe_file_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs); | |
1934 | if (EFI_ERROR(err)) | |
1935 | continue; | |
0fa2cac4 | 1936 | |
12643e7c ZJS |
1937 | err = file_read(linux_dir, f->FileName, offs[0], szs[0], &content, NULL); |
1938 | if (EFI_ERROR(err)) | |
1939 | continue; | |
0fa2cac4 | 1940 | |
12643e7c | 1941 | /* read properties from the embedded os-release file */ |
12643e7c ZJS |
1942 | while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) { |
1943 | if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) { | |
10d0024a SM |
1944 | FreePool(os_name_pretty); |
1945 | os_name_pretty = stra_to_str(value); | |
1946 | continue; | |
1947 | } | |
1948 | ||
1949 | if (strcmpa((CHAR8 *)"NAME", key) == 0) { | |
12643e7c ZJS |
1950 | FreePool(os_name); |
1951 | os_name = stra_to_str(value); | |
1952 | continue; | |
1953 | } | |
0fa2cac4 | 1954 | |
12643e7c ZJS |
1955 | if (strcmpa((CHAR8 *)"ID", key) == 0) { |
1956 | FreePool(os_id); | |
1957 | os_id = stra_to_str(value); | |
1958 | continue; | |
1959 | } | |
59658d19 | 1960 | |
12643e7c ZJS |
1961 | if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) { |
1962 | FreePool(os_version); | |
1963 | os_version = stra_to_str(value); | |
1964 | continue; | |
0fa2cac4 KS |
1965 | } |
1966 | ||
10d0024a SM |
1967 | if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) { |
1968 | FreePool(os_version_id); | |
1969 | os_version_id = stra_to_str(value); | |
1970 | continue; | |
1971 | } | |
1972 | ||
12643e7c | 1973 | if (strcmpa((CHAR8 *)"BUILD_ID", key) == 0) { |
10d0024a SM |
1974 | FreePool(os_build_id); |
1975 | os_build_id = stra_to_str(value); | |
12643e7c | 1976 | continue; |
0fa2cac4 | 1977 | } |
12643e7c ZJS |
1978 | } |
1979 | ||
10d0024a | 1980 | if ((os_name_pretty || os_name) && os_id && (os_version || os_version_id || os_build_id)) { |
7fa23ab6 | 1981 | _cleanup_freepool_ CHAR16 *path = NULL; |
12643e7c | 1982 | |
12643e7c | 1983 | path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName); |
2214cfbf | 1984 | |
10d0024a SM |
1985 | entry = config_entry_add_loader(config, device, LOADER_LINUX, f->FileName, 'l', |
1986 | os_name_pretty ? : (os_name ? : os_id), path, | |
1987 | os_version ? : (os_version_id ? : os_build_id)); | |
0fa2cac4 KS |
1988 | |
1989 | FreePool(content); | |
12643e7c | 1990 | content = NULL; |
2214cfbf | 1991 | |
12643e7c | 1992 | /* read the embedded cmdline file */ |
d3a27ed4 | 1993 | err = file_read(linux_dir, f->FileName, offs[1], szs[1], &content, NULL); |
12643e7c | 1994 | if (!EFI_ERROR(err)) { |
d3a27ed4 ZJS |
1995 | |
1996 | /* chomp the newline */ | |
1997 | if (content[szs[1]-1] == '\n') | |
1998 | content[szs[1]-1] = '\0'; | |
1999 | ||
2000 | entry->options = stra_to_str(content); | |
12643e7c | 2001 | } |
f538cc65 LP |
2002 | |
2003 | config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi"); | |
0fa2cac4 | 2004 | } |
12643e7c | 2005 | |
10d0024a | 2006 | FreePool(os_name_pretty); |
12643e7c ZJS |
2007 | FreePool(os_name); |
2008 | FreePool(os_id); | |
2009 | FreePool(os_version); | |
10d0024a SM |
2010 | FreePool(os_version_id); |
2011 | FreePool(os_build_id); | |
12643e7c | 2012 | FreePool(content); |
0fa2cac4 | 2013 | } |
12643e7c ZJS |
2014 | |
2015 | uefi_call_wrapper(linux_dir->Close, 1, linux_dir); | |
0fa2cac4 KS |
2016 | } |
2017 | ||
fa2a3bbd LP |
2018 | /* Note that this is in GUID format, i.e. the first 32bit, and the following pair of 16bit are byteswapped. */ |
2019 | static const UINT8 xbootldr_guid[16] = { | |
2020 | 0xff, 0xc2, 0x13, 0xbc, 0xe6, 0x59, 0x62, 0x42, 0xa3, 0x52, 0xb2, 0x75, 0xfd, 0x6f, 0x71, 0x72 | |
2021 | }; | |
2022 | ||
2023 | EFI_DEVICE_PATH *path_parent(EFI_DEVICE_PATH *path, EFI_DEVICE_PATH *node) { | |
2024 | EFI_DEVICE_PATH *parent; | |
2025 | UINTN len; | |
2026 | ||
2027 | len = (UINT8*) NextDevicePathNode(node) - (UINT8*) path; | |
2028 | parent = (EFI_DEVICE_PATH*) AllocatePool(len + sizeof(EFI_DEVICE_PATH)); | |
2029 | CopyMem(parent, path, len); | |
2030 | CopyMem((UINT8*) parent + len, EndDevicePath, sizeof(EFI_DEVICE_PATH)); | |
2031 | ||
2032 | return parent; | |
2033 | } | |
2034 | ||
2035 | static VOID config_load_xbootldr( | |
2036 | Config *config, | |
2037 | EFI_HANDLE *device) { | |
2038 | ||
2039 | EFI_DEVICE_PATH *partition_path, *node, *disk_path, *copy; | |
2040 | UINT32 found_partition_number = (UINT32) -1; | |
2041 | UINT64 found_partition_start = (UINT64) -1; | |
2042 | UINT64 found_partition_size = (UINT64) -1; | |
2043 | UINT8 found_partition_signature[16] = {}; | |
2044 | EFI_HANDLE new_device; | |
2045 | EFI_FILE *root_dir; | |
2046 | EFI_STATUS r; | |
2047 | ||
2048 | partition_path = DevicePathFromHandle(device); | |
2049 | if (!partition_path) | |
2050 | return; | |
2051 | ||
2052 | for (node = partition_path; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) { | |
2053 | EFI_HANDLE disk_handle; | |
2054 | EFI_BLOCK_IO *block_io; | |
2055 | EFI_DEVICE_PATH *p; | |
2056 | UINTN nr; | |
2057 | ||
2058 | /* First, Let's look for the SCSI/SATA/USB/… device path node, i.e. one above the media | |
2059 | * devices */ | |
2060 | if (DevicePathType(node) != MESSAGING_DEVICE_PATH) | |
2061 | continue; | |
2062 | ||
2063 | /* Determine the device path one level up */ | |
2064 | disk_path = path_parent(partition_path, node); | |
2065 | p = disk_path; | |
2066 | r = uefi_call_wrapper(BS->LocateDevicePath, 3, &BlockIoProtocol, &p, &disk_handle); | |
2067 | if (EFI_ERROR(r)) | |
2068 | continue; | |
2069 | ||
2070 | r = uefi_call_wrapper(BS->HandleProtocol, 3, disk_handle, &BlockIoProtocol, (VOID **)&block_io); | |
2071 | if (EFI_ERROR(r)) | |
2072 | continue; | |
2073 | ||
2074 | /* Filter out some block devices early. (We only care about block devices that aren't | |
2075 | * partitions themselves — we look for GPT partition tables to parse after all —, and only | |
2076 | * those which contain a medium and have at least 2 blocks.) */ | |
2077 | if (block_io->Media->LogicalPartition || | |
2078 | !block_io->Media->MediaPresent || | |
2079 | block_io->Media->LastBlock <= 1) | |
2080 | continue; | |
2081 | ||
2082 | /* Try both copies of the GPT header, in case one is corrupted */ | |
2083 | for (nr = 0; nr < 2; nr++) { | |
2084 | _cleanup_freepool_ EFI_PARTITION_ENTRY* entries = NULL; | |
2085 | union { | |
2086 | EFI_PARTITION_TABLE_HEADER gpt_header; | |
2087 | uint8_t space[((sizeof(EFI_PARTITION_TABLE_HEADER) + 511) / 512) * 512]; | |
2088 | } gpt_header_buffer; | |
ff19ad88 | 2089 | const EFI_PARTITION_TABLE_HEADER *h = &gpt_header_buffer.gpt_header; |
fa2a3bbd LP |
2090 | UINT64 where; |
2091 | UINTN i, sz; | |
2092 | UINT32 c; | |
2093 | ||
2094 | if (nr == 0) | |
ff19ad88 ZJS |
2095 | /* Read the first copy at LBA 1 */ |
2096 | where = 1; | |
fa2a3bbd | 2097 | else |
ff19ad88 ZJS |
2098 | /* Read the second copy at the very last LBA of this block device */ |
2099 | where = block_io->Media->LastBlock; | |
fa2a3bbd LP |
2100 | |
2101 | /* Read the GPT header */ | |
ff19ad88 ZJS |
2102 | r = uefi_call_wrapper(block_io->ReadBlocks, 5, |
2103 | block_io, | |
2104 | block_io->Media->MediaId, | |
2105 | where, | |
2106 | sizeof(gpt_header_buffer), &gpt_header_buffer); | |
fa2a3bbd LP |
2107 | if (EFI_ERROR(r)) |
2108 | continue; | |
2109 | ||
2110 | /* Some superficial validation of the GPT header */ | |
ff19ad88 ZJS |
2111 | c = CompareMem(&h->Header.Signature, "EFI PART", sizeof(h->Header.Signature)); |
2112 | if (c != 0) | |
fa2a3bbd LP |
2113 | continue; |
2114 | ||
ff19ad88 ZJS |
2115 | if (h->Header.HeaderSize < 92 || |
2116 | h->Header.HeaderSize > 512) | |
fa2a3bbd LP |
2117 | continue; |
2118 | ||
ff19ad88 | 2119 | if (h->Header.Revision != 0x00010000U) |
fa2a3bbd LP |
2120 | continue; |
2121 | ||
2122 | /* Calculate CRC check */ | |
ff19ad88 ZJS |
2123 | c = ~crc32_exclude_offset((UINT32) -1, |
2124 | (const UINT8*) &gpt_header_buffer, | |
2125 | h->Header.HeaderSize, | |
2126 | OFFSETOF(EFI_PARTITION_TABLE_HEADER, Header.CRC32), | |
2127 | sizeof(h->Header.CRC32)); | |
2128 | if (c != h->Header.CRC32) | |
fa2a3bbd LP |
2129 | continue; |
2130 | ||
ff19ad88 | 2131 | if (h->MyLBA != where) |
fa2a3bbd LP |
2132 | continue; |
2133 | ||
ff19ad88 | 2134 | if (h->SizeOfPartitionEntry < sizeof(EFI_PARTITION_ENTRY)) |
fa2a3bbd LP |
2135 | continue; |
2136 | ||
ff19ad88 ZJS |
2137 | if (h->NumberOfPartitionEntries <= 0 || |
2138 | h->NumberOfPartitionEntries > 1024) | |
fa2a3bbd LP |
2139 | continue; |
2140 | ||
7a2cb022 ZJS |
2141 | if (h->SizeOfPartitionEntry > UINTN_MAX / h->NumberOfPartitionEntries) /* overflow check */ |
2142 | continue; | |
2143 | ||
fa2a3bbd | 2144 | /* Now load the GPT entry table */ |
7a2cb022 | 2145 | sz = ALIGN_TO((UINTN) h->SizeOfPartitionEntry * (UINTN) h->NumberOfPartitionEntries, 512); |
fa2a3bbd LP |
2146 | entries = AllocatePool(sz); |
2147 | ||
ff19ad88 ZJS |
2148 | r = uefi_call_wrapper(block_io->ReadBlocks, 5, |
2149 | block_io, | |
2150 | block_io->Media->MediaId, | |
2151 | h->PartitionEntryLBA, | |
2152 | sz, entries); | |
fa2a3bbd LP |
2153 | if (EFI_ERROR(r)) |
2154 | continue; | |
2155 | ||
2156 | /* Calculate CRC of entries array, too */ | |
2157 | c = ~crc32((UINT32) -1, entries, sz); | |
ff19ad88 | 2158 | if (c != h->PartitionEntryArrayCRC32) |
fa2a3bbd LP |
2159 | continue; |
2160 | ||
ff19ad88 | 2161 | for (i = 0; i < h->NumberOfPartitionEntries; i++) { |
fa2a3bbd LP |
2162 | EFI_PARTITION_ENTRY *entry; |
2163 | ||
ff19ad88 | 2164 | entry = (EFI_PARTITION_ENTRY*) ((UINT8*) entries + h->SizeOfPartitionEntry * i); |
fa2a3bbd LP |
2165 | |
2166 | if (CompareMem(&entry->PartitionTypeGUID, xbootldr_guid, 16) == 0) { | |
2167 | UINT64 end; | |
2168 | ||
2169 | /* Let's use memcpy(), in case the structs are not aligned (they really should be though) */ | |
2170 | CopyMem(&found_partition_start, &entry->StartingLBA, sizeof(found_partition_start)); | |
2171 | CopyMem(&end, &entry->EndingLBA, sizeof(end)); | |
2172 | ||
2173 | if (end < found_partition_start) /* Bogus? */ | |
2174 | continue; | |
2175 | ||
2176 | found_partition_size = end - found_partition_start + 1; | |
2177 | CopyMem(found_partition_signature, &entry->UniquePartitionGUID, sizeof(found_partition_signature)); | |
2178 | ||
2179 | found_partition_number = i + 1; | |
2180 | goto found; | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | break; /* This GPT was fully valid, but we didn't find what we are looking for. This | |
2185 | * means there's no reason to check the second copy of the GPT header */ | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | return; /* Not found */ | |
2190 | ||
2191 | found: | |
2192 | copy = DuplicateDevicePath(partition_path); | |
2193 | ||
2194 | /* Patch in the data we found */ | |
2195 | for (node = copy; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) { | |
2196 | HARDDRIVE_DEVICE_PATH *hd; | |
2197 | ||
2198 | if (DevicePathType(node) != MEDIA_DEVICE_PATH) | |
2199 | continue; | |
2200 | ||
2201 | if (DevicePathSubType(node) != MEDIA_HARDDRIVE_DP) | |
2202 | continue; | |
2203 | ||
2204 | hd = (HARDDRIVE_DEVICE_PATH*) node; | |
2205 | hd->PartitionNumber = found_partition_number; | |
2206 | hd->PartitionStart = found_partition_start; | |
2207 | hd->PartitionSize = found_partition_size; | |
2208 | CopyMem(hd->Signature, found_partition_signature, sizeof(hd->Signature)); | |
2209 | hd->MBRType = MBR_TYPE_EFI_PARTITION_TABLE_HEADER; | |
2210 | hd->SignatureType = SIGNATURE_TYPE_GUID; | |
2211 | } | |
2212 | ||
2213 | r = uefi_call_wrapper(BS->LocateDevicePath, 3, &BlockIoProtocol, ©, &new_device); | |
2214 | if (EFI_ERROR(r)) | |
2215 | return; | |
2216 | ||
2217 | root_dir = LibOpenRoot(new_device); | |
2218 | if (!root_dir) | |
2219 | return; | |
2220 | ||
2221 | config_entry_add_linux(config, new_device, root_dir); | |
2222 | config_load_entries(config, new_device, root_dir, NULL); | |
2223 | } | |
2224 | ||
580fe4df LP |
2225 | static EFI_STATUS image_start( |
2226 | EFI_HANDLE parent_image, | |
2227 | const Config *config, | |
2228 | const ConfigEntry *entry) { | |
2229 | ||
0fa2cac4 | 2230 | EFI_HANDLE image; |
a42d7cf1 | 2231 | _cleanup_freepool_ EFI_DEVICE_PATH *path = NULL; |
0fa2cac4 KS |
2232 | CHAR16 *options; |
2233 | EFI_STATUS err; | |
2234 | ||
2235 | path = FileDevicePath(entry->device, entry->loader); | |
2236 | if (!path) { | |
2237 | Print(L"Error getting device path."); | |
2238 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
2239 | return EFI_INVALID_PARAMETER; | |
2240 | } | |
2241 | ||
2242 | err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image); | |
2243 | if (EFI_ERROR(err)) { | |
2244 | Print(L"Error loading %s: %r", entry->loader, err); | |
2245 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
a42d7cf1 | 2246 | return err; |
0fa2cac4 KS |
2247 | } |
2248 | ||
2249 | if (config->options_edit) | |
2250 | options = config->options_edit; | |
2251 | else if (entry->options) | |
2252 | options = entry->options; | |
2253 | else | |
2254 | options = NULL; | |
2255 | if (options) { | |
2256 | EFI_LOADED_IMAGE *loaded_image; | |
2257 | ||
2258 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, | |
2259 | parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); | |
2260 | if (EFI_ERROR(err)) { | |
2261 | Print(L"Error getting LoadedImageProtocol handle: %r", err); | |
2262 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
2263 | goto out_unload; | |
2264 | } | |
2265 | loaded_image->LoadOptions = options; | |
2266 | loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16); | |
92ed3bb4 | 2267 | |
349cc4a5 | 2268 | #if ENABLE_TPM |
7db5706e | 2269 | /* Try to log any options to the TPM, especially to catch manually edited options */ |
92ed3bb4 | 2270 | err = tpm_log_event(SD_TPM_PCR, |
33de6b57 | 2271 | (EFI_PHYSICAL_ADDRESS) (UINTN) loaded_image->LoadOptions, |
92ed3bb4 HH |
2272 | loaded_image->LoadOptionsSize, loaded_image->LoadOptions); |
2273 | if (EFI_ERROR(err)) { | |
2274 | Print(L"Unable to add image options measurement: %r", err); | |
522aa9f5 | 2275 | uefi_call_wrapper(BS->Stall, 1, 200 * 1000); |
92ed3bb4 HH |
2276 | } |
2277 | #endif | |
0fa2cac4 KS |
2278 | } |
2279 | ||
2280 | efivar_set_time_usec(L"LoaderTimeExecUSec", 0); | |
2281 | err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL); | |
2282 | out_unload: | |
2283 | uefi_call_wrapper(BS->UnloadImage, 1, image); | |
0fa2cac4 KS |
2284 | return err; |
2285 | } | |
2286 | ||
2287 | static EFI_STATUS reboot_into_firmware(VOID) { | |
a42d7cf1 | 2288 | _cleanup_freepool_ CHAR8 *b = NULL; |
0fa2cac4 KS |
2289 | UINTN size; |
2290 | UINT64 osind; | |
2291 | EFI_STATUS err; | |
2292 | ||
2293 | osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI; | |
2294 | ||
2295 | err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size); | |
2296 | if (!EFI_ERROR(err)) | |
2297 | osind |= (UINT64)*b; | |
0fa2cac4 | 2298 | |
f82ecab0 | 2299 | err = efivar_set_raw(&global_guid, L"OsIndications", &osind, sizeof(UINT64), TRUE); |
0fa2cac4 KS |
2300 | if (EFI_ERROR(err)) |
2301 | return err; | |
2302 | ||
2303 | err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL); | |
2304 | Print(L"Error calling ResetSystem: %r", err); | |
2305 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
2306 | return err; | |
2307 | } | |
2308 | ||
2309 | static VOID config_free(Config *config) { | |
2310 | UINTN i; | |
2311 | ||
2312 | for (i = 0; i < config->entry_count; i++) | |
2313 | config_entry_free(config->entries[i]); | |
2314 | FreePool(config->entries); | |
2315 | FreePool(config->entry_default_pattern); | |
2316 | FreePool(config->options_edit); | |
2317 | FreePool(config->entry_oneshot); | |
0fa2cac4 KS |
2318 | } |
2319 | ||
0e2bc732 LP |
2320 | static VOID config_write_entries_to_variable(Config *config) { |
2321 | _cleanup_freepool_ CHAR16 *buffer = NULL; | |
2322 | UINTN i, sz = 0; | |
2323 | CHAR16 *p; | |
2324 | ||
2325 | for (i = 0; i < config->entry_count; i++) | |
2326 | sz += StrLen(config->entries[i]->id) + 1; | |
2327 | ||
2328 | p = buffer = AllocatePool(sz * sizeof(CHAR16)); | |
2329 | ||
2330 | for (i = 0; i < config->entry_count; i++) { | |
2331 | UINTN l; | |
2332 | ||
2333 | l = StrLen(config->entries[i]->id) + 1; | |
2334 | CopyMem(p, config->entries[i]->id, l * sizeof(CHAR16)); | |
2335 | ||
2336 | p += l; | |
2337 | } | |
2338 | ||
2339 | /* Store the full list of discovered entries. */ | |
2340 | (void) efivar_set_raw(&loader_guid, L"LoaderEntries", buffer, (UINT8*) p - (UINT8*) buffer, FALSE); | |
2341 | } | |
2342 | ||
0fa2cac4 | 2343 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { |
5dd5f7cf | 2344 | static const UINT64 loader_features = |
91b08bb0 ZJS |
2345 | EFI_LOADER_FEATURE_CONFIG_TIMEOUT | |
2346 | EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT | | |
2347 | EFI_LOADER_FEATURE_ENTRY_DEFAULT | | |
2348 | EFI_LOADER_FEATURE_ENTRY_ONESHOT | | |
2349 | EFI_LOADER_FEATURE_BOOT_COUNTING | | |
3a76fbe0 | 2350 | EFI_LOADER_FEATURE_XBOOTLDR | |
e4dcf7aa | 2351 | EFI_LOADER_FEATURE_RANDOM_SEED | |
5dd5f7cf LP |
2352 | 0; |
2353 | ||
a42d7cf1 | 2354 | _cleanup_freepool_ CHAR16 *infostr = NULL, *typestr = NULL; |
0fa2cac4 KS |
2355 | CHAR8 *b; |
2356 | UINTN size; | |
2357 | EFI_LOADED_IMAGE *loaded_image; | |
2358 | EFI_FILE *root_dir; | |
2359 | CHAR16 *loaded_image_path; | |
0fa2cac4 KS |
2360 | EFI_STATUS err; |
2361 | Config config; | |
2362 | UINT64 init_usec; | |
2363 | BOOLEAN menu = FALSE; | |
8110e144 | 2364 | CHAR16 uuid[37]; |
0fa2cac4 KS |
2365 | |
2366 | InitializeLib(image, sys_table); | |
2367 | init_usec = time_usec(); | |
2368 | efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec); | |
681bd2c5 | 2369 | efivar_set(L"LoaderInfo", L"systemd-boot " GIT_VERSION, FALSE); |
a42d7cf1 ZJS |
2370 | |
2371 | infostr = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); | |
2372 | efivar_set(L"LoaderFirmwareInfo", infostr, FALSE); | |
2373 | ||
2374 | typestr = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); | |
2375 | efivar_set(L"LoaderFirmwareType", typestr, FALSE); | |
0fa2cac4 | 2376 | |
5dd5f7cf LP |
2377 | (void) efivar_set_raw(&loader_guid, L"LoaderFeatures", &loader_features, sizeof(loader_features), FALSE); |
2378 | ||
0fa2cac4 KS |
2379 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, |
2380 | image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); | |
2381 | if (EFI_ERROR(err)) { | |
67633c07 | 2382 | Print(L"Error getting a LoadedImageProtocol handle: %r", err); |
0fa2cac4 KS |
2383 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); |
2384 | return err; | |
2385 | } | |
2386 | ||
2387 | /* export the device path this image is started from */ | |
8110e144 KS |
2388 | if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS) |
2389 | efivar_set(L"LoaderDevicePartUUID", uuid, FALSE); | |
0fa2cac4 KS |
2390 | |
2391 | root_dir = LibOpenRoot(loaded_image->DeviceHandle); | |
2392 | if (!root_dir) { | |
5d875897 | 2393 | Print(L"Unable to open root directory."); |
0fa2cac4 KS |
2394 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); |
2395 | return EFI_LOAD_ERROR; | |
2396 | } | |
2397 | ||
b2bb40ce MR |
2398 | if (secure_boot_enabled() && shim_loaded()) { |
2399 | err = security_policy_install(); | |
2400 | if (EFI_ERROR(err)) { | |
2401 | Print(L"Error installing security policy: %r ", err); | |
2402 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
2403 | return err; | |
2404 | } | |
2405 | } | |
0fa2cac4 KS |
2406 | |
2407 | /* the filesystem path to this image, to prevent adding ourselves to the menu */ | |
2408 | loaded_image_path = DevicePathToStr(loaded_image->FilePath); | |
2409 | efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE); | |
2410 | ||
540536ff | 2411 | config_load_defaults(&config, root_dir); |
0fa2cac4 | 2412 | |
540536ff | 2413 | /* scan /EFI/Linux/ directory */ |
c8ad5996 | 2414 | config_entry_add_linux(&config, loaded_image->DeviceHandle, root_dir); |
540536ff KS |
2415 | |
2416 | /* scan /loader/entries/\*.conf files */ | |
2417 | config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path); | |
2418 | ||
fa2a3bbd LP |
2419 | /* Similar, but on any XBOOTLDR partition */ |
2420 | config_load_xbootldr(&config, loaded_image->DeviceHandle); | |
2421 | ||
540536ff KS |
2422 | /* sort entries after version number */ |
2423 | config_sort_entries(&config); | |
2424 | ||
2425 | /* if we find some well-known loaders, add them to the end of the list */ | |
4c8c9f9f | 2426 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL, |
0fa2cac4 | 2427 | L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi"); |
4c8c9f9f | 2428 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL, |
0fa2cac4 KS |
2429 | L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi"); |
2430 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, | |
2431 | L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi"); | |
2432 | config_entry_add_osx(&config); | |
0fa2cac4 | 2433 | |
c1d4e298 | 2434 | if (config.auto_firmware && efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) { |
0fa2cac4 KS |
2435 | UINT64 osind = (UINT64)*b; |
2436 | ||
2437 | if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) | |
ff19ad88 ZJS |
2438 | config_entry_add_call(&config, |
2439 | L"auto-reboot-to-firmware-setup", | |
2440 | L"Reboot Into Firmware Interface", | |
2441 | reboot_into_firmware); | |
0fa2cac4 KS |
2442 | FreePool(b); |
2443 | } | |
2444 | ||
2445 | if (config.entry_count == 0) { | |
2446 | Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed."); | |
2447 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
2448 | goto out; | |
2449 | } | |
2450 | ||
0e2bc732 LP |
2451 | config_write_entries_to_variable(&config); |
2452 | ||
0fa2cac4 KS |
2453 | config_title_generate(&config); |
2454 | ||
13e785f7 | 2455 | /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */ |
0fa2cac4 KS |
2456 | config_default_entry_select(&config); |
2457 | ||
2458 | /* if no configured entry to select from was found, enable the menu */ | |
2459 | if (config.idx_default == -1) { | |
2460 | config.idx_default = 0; | |
2461 | if (config.timeout_sec == 0) | |
2462 | config.timeout_sec = 10; | |
2463 | } | |
2464 | ||
2465 | /* select entry or show menu when key is pressed or timeout is set */ | |
fe2579dd LP |
2466 | if (config.force_menu || config.timeout_sec > 0) |
2467 | menu = TRUE; | |
2468 | else { | |
0fa2cac4 KS |
2469 | UINT64 key; |
2470 | ||
2471 | err = console_key_read(&key, FALSE); | |
26601a2a LB |
2472 | |
2473 | if (err == EFI_NOT_READY) { | |
2474 | uefi_call_wrapper(BS->Stall, 1, 100 * 1000); | |
2475 | err = console_key_read(&key, FALSE); | |
2476 | } | |
2477 | ||
0fa2cac4 KS |
2478 | if (!EFI_ERROR(err)) { |
2479 | INT16 idx; | |
2480 | ||
2481 | /* find matching key in config entries */ | |
2482 | idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key)); | |
2483 | if (idx >= 0) | |
2484 | config.idx_default = idx; | |
2485 | else | |
2486 | menu = TRUE; | |
2487 | } | |
fe2579dd | 2488 | } |
0fa2cac4 KS |
2489 | |
2490 | for (;;) { | |
2491 | ConfigEntry *entry; | |
2492 | ||
2493 | entry = config.entries[config.idx_default]; | |
2494 | if (menu) { | |
2495 | efivar_set_time_usec(L"LoaderTimeMenuUSec", 0); | |
2496 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL); | |
7361099e | 2497 | if (!menu_run(&config, &entry, loaded_image_path)) |
0fa2cac4 | 2498 | break; |
996daf2f | 2499 | } |
0fa2cac4 | 2500 | |
996daf2f LP |
2501 | /* run special entry like "reboot" */ |
2502 | if (entry->call) { | |
2503 | entry->call(); | |
2504 | continue; | |
0fa2cac4 KS |
2505 | } |
2506 | ||
f538cc65 LP |
2507 | config_entry_bump_counters(entry, root_dir); |
2508 | ||
e4dcf7aa LP |
2509 | /* Export the selected boot entry to the system */ |
2510 | (VOID) efivar_set(L"LoaderEntrySelected", entry->id, FALSE); | |
2511 | ||
2512 | /* Optionally, read a random seed off the ESP and pass it to the OS */ | |
2513 | (VOID) process_random_seed(root_dir, config.random_seed_mode); | |
0fa2cac4 KS |
2514 | |
2515 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL); | |
2516 | err = image_start(image, &config, entry); | |
20b1538d | 2517 | if (EFI_ERROR(err)) { |
37fa3690 | 2518 | graphics_mode(FALSE); |
20b1538d | 2519 | Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err); |
0fa2cac4 KS |
2520 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); |
2521 | goto out; | |
2522 | } | |
2523 | ||
2524 | menu = TRUE; | |
2525 | config.timeout_sec = 0; | |
2526 | } | |
2527 | err = EFI_SUCCESS; | |
2528 | out: | |
2529 | FreePool(loaded_image_path); | |
2530 | config_free(&config); | |
2531 | uefi_call_wrapper(root_dir->Close, 1, root_dir); | |
2532 | uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL); | |
2533 | return err; | |
2534 | } |