]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/boot.c
72a946b1ad35cbbe3279e1f3c4cd161588ea50c0
[thirdparty/systemd.git] / src / boot / efi / boot.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /*
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * Copyright (C) 2012-2015 Kay Sievers <kay@vrfy.org>
14 * Copyright (C) 2012-2015 Harald Hoyer <harald@redhat.com>
15 */
16
17 #include <efi.h>
18 #include <efilib.h>
19
20 #include "console.h"
21 #include "disk.h"
22 #include "graphics.h"
23 #include "linux.h"
24 #include "measure.h"
25 #include "pe.h"
26 #include "shim.h"
27 #include "util.h"
28
29 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
30 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
31 #endif
32
33 /* magic string to find in the binary image */
34 static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " PACKAGE_VERSION " ####";
35
36 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
37
38 enum loader_type {
39 LOADER_UNDEFINED,
40 LOADER_EFI,
41 LOADER_LINUX
42 };
43
44 typedef struct {
45 CHAR16 *file;
46 CHAR16 *title_show;
47 CHAR16 *title;
48 CHAR16 *version;
49 CHAR16 *machine_id;
50 EFI_HANDLE *device;
51 enum loader_type type;
52 CHAR16 *loader;
53 CHAR16 *options;
54 CHAR16 key;
55 EFI_STATUS (*call)(VOID);
56 BOOLEAN no_autoselect;
57 BOOLEAN non_unique;
58 } ConfigEntry;
59
60 typedef struct {
61 ConfigEntry **entries;
62 UINTN entry_count;
63 INTN idx_default;
64 INTN idx_default_efivar;
65 UINTN timeout_sec;
66 UINTN timeout_sec_config;
67 INTN timeout_sec_efivar;
68 CHAR16 *entry_default_pattern;
69 CHAR16 *entry_oneshot;
70 CHAR16 *options_edit;
71 BOOLEAN editor;
72 BOOLEAN auto_entries;
73 BOOLEAN auto_firmware;
74 UINTN console_mode;
75 enum console_mode_change_type console_mode_change;
76 } Config;
77
78 static VOID cursor_left(UINTN *cursor, UINTN *first) {
79 if ((*cursor) > 0)
80 (*cursor)--;
81 else if ((*first) > 0)
82 (*first)--;
83 }
84
85 static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len) {
86 if ((*cursor)+1 < x_max)
87 (*cursor)++;
88 else if ((*first) + (*cursor) < len)
89 (*first)++;
90 }
91
92 static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
93 _cleanup_freepool_ CHAR16 *line = NULL, *print = NULL;
94 UINTN size, len, first, cursor, clear;
95 BOOLEAN exit, enter;
96
97 if (!line_in)
98 line_in = L"";
99 size = StrLen(line_in) + 1024;
100 line = AllocatePool(size * sizeof(CHAR16));
101 StrCpy(line, line_in);
102 len = StrLen(line);
103 print = AllocatePool((x_max+1) * sizeof(CHAR16));
104
105 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
106
107 first = 0;
108 cursor = 0;
109 clear = 0;
110 enter = FALSE;
111 exit = FALSE;
112 while (!exit) {
113 EFI_STATUS err;
114 UINT64 key;
115 UINTN i;
116
117 i = len - first;
118 if (i >= x_max-1)
119 i = x_max-1;
120 CopyMem(print, line + first, i * sizeof(CHAR16));
121 while (clear > 0 && i < x_max-1) {
122 clear--;
123 print[i++] = ' ';
124 }
125 print[i] = '\0';
126
127 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
128 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
129 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
130
131 err = console_key_read(&key, TRUE);
132 if (EFI_ERROR(err))
133 continue;
134
135 switch (key) {
136 case KEYPRESS(0, SCAN_ESC, 0):
137 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'):
138 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'):
139 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')):
140 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')):
141 exit = TRUE;
142 break;
143
144 case KEYPRESS(0, SCAN_HOME, 0):
145 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'):
146 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')):
147 /* beginning-of-line */
148 cursor = 0;
149 first = 0;
150 continue;
151
152 case KEYPRESS(0, SCAN_END, 0):
153 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'):
154 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')):
155 /* end-of-line */
156 cursor = len - first;
157 if (cursor+1 >= x_max) {
158 cursor = x_max-1;
159 first = len - (x_max-1);
160 }
161 continue;
162
163 case KEYPRESS(0, SCAN_DOWN, 0):
164 case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'):
165 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0):
166 /* forward-word */
167 while (line[first + cursor] == ' ')
168 cursor_right(&cursor, &first, x_max, len);
169 while (line[first + cursor] && line[first + cursor] != ' ')
170 cursor_right(&cursor, &first, x_max, len);
171 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
172 continue;
173
174 case KEYPRESS(0, SCAN_UP, 0):
175 case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'):
176 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0):
177 /* backward-word */
178 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
179 cursor_left(&cursor, &first);
180 while ((first + cursor) > 0 && line[first + cursor] == ' ')
181 cursor_left(&cursor, &first);
182 }
183 while ((first + cursor) > 0 && line[first + cursor-1] != ' ')
184 cursor_left(&cursor, &first);
185 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
186 continue;
187
188 case KEYPRESS(0, SCAN_RIGHT, 0):
189 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'):
190 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')):
191 /* forward-char */
192 if (first + cursor == len)
193 continue;
194 cursor_right(&cursor, &first, x_max, len);
195 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
196 continue;
197
198 case KEYPRESS(0, SCAN_LEFT, 0):
199 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'):
200 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')):
201 /* backward-char */
202 cursor_left(&cursor, &first);
203 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
204 continue;
205
206 case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
207 /* kill-word */
208 clear = 0;
209 for (i = first + cursor; i < len && line[i] == ' '; i++)
210 clear++;
211 for (; i < len && line[i] != ' '; i++)
212 clear++;
213
214 for (i = first + cursor; i + clear < len; i++)
215 line[i] = line[i + clear];
216 len -= clear;
217 line[len] = '\0';
218 continue;
219
220 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'):
221 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')):
222 case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE):
223 /* backward-kill-word */
224 clear = 0;
225 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
226 cursor_left(&cursor, &first);
227 clear++;
228 while ((first + cursor) > 0 && line[first + cursor] == ' ') {
229 cursor_left(&cursor, &first);
230 clear++;
231 }
232 }
233 while ((first + cursor) > 0 && line[first + cursor-1] != ' ') {
234 cursor_left(&cursor, &first);
235 clear++;
236 }
237 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
238
239 for (i = first + cursor; i + clear < len; i++)
240 line[i] = line[i + clear];
241 len -= clear;
242 line[len] = '\0';
243 continue;
244
245 case KEYPRESS(0, SCAN_DELETE, 0):
246 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'):
247 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')):
248 if (len == 0)
249 continue;
250 if (first + cursor == len)
251 continue;
252 for (i = first + cursor; i < len; i++)
253 line[i] = line[i+1];
254 clear = 1;
255 len--;
256 continue;
257
258 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'):
259 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')):
260 /* kill-line */
261 line[first + cursor] = '\0';
262 clear = len - (first + cursor);
263 len = first + cursor;
264 continue;
265
266 case KEYPRESS(0, 0, CHAR_LINEFEED):
267 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
268 if (StrCmp(line, line_in) != 0) {
269 *line_out = line;
270 line = NULL;
271 }
272 enter = TRUE;
273 exit = TRUE;
274 break;
275
276 case KEYPRESS(0, 0, CHAR_BACKSPACE):
277 if (len == 0)
278 continue;
279 if (first == 0 && cursor == 0)
280 continue;
281 for (i = first + cursor-1; i < len; i++)
282 line[i] = line[i+1];
283 clear = 1;
284 len--;
285 if (cursor > 0)
286 cursor--;
287 if (cursor > 0 || first == 0)
288 continue;
289 /* show full line if it fits */
290 if (len < x_max) {
291 cursor = first;
292 first = 0;
293 continue;
294 }
295 /* jump left to see what we delete */
296 if (first > 10) {
297 first -= 10;
298 cursor = 10;
299 } else {
300 cursor = first;
301 first = 0;
302 }
303 continue;
304
305 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
306 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
307 if (len+1 == size)
308 continue;
309 for (i = len; i > first + cursor; i--)
310 line[i] = line[i-1];
311 line[first + cursor] = KEYCHAR(key);
312 len++;
313 line[len] = '\0';
314 if (cursor+1 < x_max)
315 cursor++;
316 else if (first + cursor < len)
317 first++;
318 continue;
319 }
320 }
321
322 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
323 return enter;
324 }
325
326 static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
327 UINTN i;
328
329 if (key == 0)
330 return -1;
331
332 /* select entry by number key */
333 if (key >= '1' && key <= '9') {
334 i = key - '0';
335 if (i > config->entry_count)
336 i = config->entry_count;
337 return i-1;
338 }
339
340 /* find matching key in config entries */
341 for (i = start; i < config->entry_count; i++)
342 if (config->entries[i]->key == key)
343 return i;
344
345 for (i = 0; i < start; i++)
346 if (config->entries[i]->key == key)
347 return i;
348
349 return -1;
350 }
351
352 static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
353 UINT64 key;
354 UINTN i;
355 _cleanup_freepool_ CHAR8 *bootvar = NULL, *modevar = NULL, *indvar = NULL;
356 _cleanup_freepool_ CHAR16 *partstr = NULL, *defaultstr = NULL;
357 UINTN x, y, size;
358
359 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
360 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
361
362 Print(L"systemd-boot version: " PACKAGE_VERSION "\n");
363 Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n");
364 Print(L"loaded image: %s\n", loaded_image_path);
365 Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
366 Print(L"firmware vendor: %s\n", ST->FirmwareVendor);
367 Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
368
369 if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS)
370 Print(L"console size: %d x %d\n", x, y);
371
372 if (efivar_get_raw(&global_guid, L"SecureBoot", &bootvar, &size) == EFI_SUCCESS)
373 Print(L"SecureBoot: %s\n", yes_no(*bootvar > 0));
374
375 if (efivar_get_raw(&global_guid, L"SetupMode", &modevar, &size) == EFI_SUCCESS)
376 Print(L"SetupMode: %s\n", *modevar > 0 ? L"setup" : L"user");
377
378 if (shim_loaded())
379 Print(L"Shim: present\n");
380
381 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &indvar, &size) == EFI_SUCCESS)
382 Print(L"OsIndicationsSupported: %d\n", (UINT64)*indvar);
383
384 Print(L"\n--- press key ---\n\n");
385 console_key_read(&key, TRUE);
386
387 Print(L"timeout: %d\n", config->timeout_sec);
388 if (config->timeout_sec_efivar >= 0)
389 Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
390 Print(L"timeout (config): %d\n", config->timeout_sec_config);
391 if (config->entry_default_pattern)
392 Print(L"default pattern: '%s'\n", config->entry_default_pattern);
393 Print(L"editor: %s\n", yes_no(config->editor));
394 Print(L"auto-entries: %s\n", yes_no(config->auto_entries));
395 Print(L"auto-firmware: %s\n", yes_no(config->auto_firmware));
396 Print(L"\n");
397
398 Print(L"config entry count: %d\n", config->entry_count);
399 Print(L"entry selected idx: %d\n", config->idx_default);
400 if (config->idx_default_efivar >= 0)
401 Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
402 Print(L"\n");
403
404 if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
405 Print(L"LoaderConfigTimeout: %d\n", i);
406 if (config->entry_oneshot)
407 Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
408 if (efivar_get(L"LoaderDevicePartUUID", &partstr) == EFI_SUCCESS)
409 Print(L"LoaderDevicePartUUID: %s\n", partstr);
410 if (efivar_get(L"LoaderEntryDefault", &defaultstr) == EFI_SUCCESS)
411 Print(L"LoaderEntryDefault: %s\n", defaultstr);
412
413 Print(L"\n--- press key ---\n\n");
414 console_key_read(&key, TRUE);
415
416 for (i = 0; i < config->entry_count; i++) {
417 ConfigEntry *entry;
418
419 if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
420 break;
421
422 entry = config->entries[i];
423 Print(L"config entry: %d/%d\n", i+1, config->entry_count);
424 if (entry->file)
425 Print(L"file '%s'\n", entry->file);
426 Print(L"title show '%s'\n", entry->title_show);
427 if (entry->title)
428 Print(L"title '%s'\n", entry->title);
429 if (entry->version)
430 Print(L"version '%s'\n", entry->version);
431 if (entry->machine_id)
432 Print(L"machine-id '%s'\n", entry->machine_id);
433 if (entry->device) {
434 EFI_DEVICE_PATH *device_path;
435
436 device_path = DevicePathFromHandle(entry->device);
437 if (device_path) {
438 _cleanup_freepool_ CHAR16 *str;
439
440 str = DevicePathToStr(device_path);
441 Print(L"device handle '%s'\n", str);
442 }
443 }
444 if (entry->loader)
445 Print(L"loader '%s'\n", entry->loader);
446 if (entry->options)
447 Print(L"options '%s'\n", entry->options);
448 Print(L"auto-select %s\n", yes_no(!entry->no_autoselect));
449 if (entry->call)
450 Print(L"internal call yes\n");
451
452 Print(L"\n--- press key ---\n\n");
453 console_key_read(&key, TRUE);
454 }
455
456 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
457 }
458
459 static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, CHAR16 *loaded_image_path) {
460 EFI_STATUS err;
461 UINTN visible_max;
462 UINTN idx_highlight;
463 UINTN idx_highlight_prev;
464 UINTN idx_first;
465 UINTN idx_last;
466 BOOLEAN refresh;
467 BOOLEAN highlight;
468 UINTN i;
469 UINTN line_width;
470 CHAR16 **lines;
471 UINTN x_start;
472 UINTN y_start;
473 UINTN x_max;
474 UINTN y_max;
475 CHAR16 *status;
476 CHAR16 *clearline;
477 INTN timeout_remain;
478 INT16 idx;
479 BOOLEAN exit = FALSE;
480 BOOLEAN run = TRUE;
481 BOOLEAN wait = FALSE;
482 BOOLEAN cleared_screen = FALSE;
483
484 graphics_mode(FALSE);
485 uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
486 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
487 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
488
489 /* draw a single character to make ClearScreen work on some firmware */
490 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" ");
491
492 if (config->console_mode_change != CONSOLE_MODE_KEEP) {
493 err = console_set_mode(&config->console_mode, config->console_mode_change);
494 if (!EFI_ERROR(err))
495 cleared_screen = TRUE;
496 }
497
498 if (!cleared_screen)
499 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
500
501 if (config->console_mode_change != CONSOLE_MODE_KEEP && EFI_ERROR(err))
502 Print(L"Error switching console mode to %ld: %r.\r", (UINT64)config->console_mode, err);
503
504 err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
505 if (EFI_ERROR(err)) {
506 x_max = 80;
507 y_max = 25;
508 }
509
510 /* we check 10 times per second for a keystroke */
511 if (config->timeout_sec > 0)
512 timeout_remain = config->timeout_sec * 10;
513 else
514 timeout_remain = -1;
515
516 idx_highlight = config->idx_default;
517 idx_highlight_prev = 0;
518
519 visible_max = y_max - 2;
520
521 if ((UINTN)config->idx_default >= visible_max)
522 idx_first = config->idx_default-1;
523 else
524 idx_first = 0;
525
526 idx_last = idx_first + visible_max-1;
527
528 refresh = TRUE;
529 highlight = FALSE;
530
531 /* length of the longest entry */
532 line_width = 5;
533 for (i = 0; i < config->entry_count; i++) {
534 UINTN entry_len;
535
536 entry_len = StrLen(config->entries[i]->title_show);
537 if (line_width < entry_len)
538 line_width = entry_len;
539 }
540 if (line_width > x_max-6)
541 line_width = x_max-6;
542
543 /* offsets to center the entries on the screen */
544 x_start = (x_max - (line_width)) / 2;
545 if (config->entry_count < visible_max)
546 y_start = ((visible_max - config->entry_count) / 2) + 1;
547 else
548 y_start = 0;
549
550 /* menu entries title lines */
551 lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
552 for (i = 0; i < config->entry_count; i++) {
553 UINTN j, k;
554
555 lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
556 for (j = 0; j < x_start; j++)
557 lines[i][j] = ' ';
558
559 for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
560 lines[i][j] = config->entries[i]->title_show[k];
561
562 for (; j < x_max; j++)
563 lines[i][j] = ' ';
564 lines[i][x_max] = '\0';
565 }
566
567 status = NULL;
568 clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
569 for (i = 0; i < x_max; i++)
570 clearline[i] = ' ';
571 clearline[i] = 0;
572
573 while (!exit) {
574 UINT64 key;
575
576 if (refresh) {
577 for (i = 0; i < config->entry_count; i++) {
578 if (i < idx_first || i > idx_last)
579 continue;
580 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
581 if (i == idx_highlight)
582 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
583 EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
584 else
585 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
586 EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
587 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
588 if ((INTN)i == config->idx_default_efivar) {
589 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
590 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
591 }
592 }
593 refresh = FALSE;
594 } else if (highlight) {
595 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
596 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
597 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
598 if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
599 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
600 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
601 }
602
603 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
604 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
605 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
606 if ((INTN)idx_highlight == config->idx_default_efivar) {
607 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
608 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
609 }
610 highlight = FALSE;
611 }
612
613 if (timeout_remain > 0) {
614 FreePool(status);
615 status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
616 }
617
618 /* print status at last line of screen */
619 if (status) {
620 UINTN len;
621 UINTN x;
622
623 /* center line */
624 len = StrLen(status);
625 if (len < x_max)
626 x = (x_max - len) / 2;
627 else
628 x = 0;
629 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
630 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
631 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
632 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
633 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
634 }
635
636 err = console_key_read(&key, wait);
637 if (EFI_ERROR(err)) {
638 /* timeout reached */
639 if (timeout_remain == 0) {
640 exit = TRUE;
641 break;
642 }
643
644 /* sleep and update status */
645 if (timeout_remain > 0) {
646 uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
647 timeout_remain--;
648 continue;
649 }
650
651 /* timeout disabled, wait for next key */
652 wait = TRUE;
653 continue;
654 }
655
656 timeout_remain = -1;
657
658 /* clear status after keystroke */
659 if (status) {
660 FreePool(status);
661 status = NULL;
662 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
663 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
664 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
665 }
666
667 idx_highlight_prev = idx_highlight;
668
669 switch (key) {
670 case KEYPRESS(0, SCAN_UP, 0):
671 case KEYPRESS(0, 0, 'k'):
672 if (idx_highlight > 0)
673 idx_highlight--;
674 break;
675
676 case KEYPRESS(0, SCAN_DOWN, 0):
677 case KEYPRESS(0, 0, 'j'):
678 if (idx_highlight < config->entry_count-1)
679 idx_highlight++;
680 break;
681
682 case KEYPRESS(0, SCAN_HOME, 0):
683 case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
684 if (idx_highlight > 0) {
685 refresh = TRUE;
686 idx_highlight = 0;
687 }
688 break;
689
690 case KEYPRESS(0, SCAN_END, 0):
691 case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
692 if (idx_highlight < config->entry_count-1) {
693 refresh = TRUE;
694 idx_highlight = config->entry_count-1;
695 }
696 break;
697
698 case KEYPRESS(0, SCAN_PAGE_UP, 0):
699 if (idx_highlight > visible_max)
700 idx_highlight -= visible_max;
701 else
702 idx_highlight = 0;
703 break;
704
705 case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
706 idx_highlight += visible_max;
707 if (idx_highlight > config->entry_count-1)
708 idx_highlight = config->entry_count-1;
709 break;
710
711 case KEYPRESS(0, 0, CHAR_LINEFEED):
712 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
713 exit = TRUE;
714 break;
715
716 case KEYPRESS(0, SCAN_F1, 0):
717 case KEYPRESS(0, 0, 'h'):
718 case KEYPRESS(0, 0, '?'):
719 status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
720 break;
721
722 case KEYPRESS(0, 0, 'Q'):
723 exit = TRUE;
724 run = FALSE;
725 break;
726
727 case KEYPRESS(0, 0, 'd'):
728 if (config->idx_default_efivar != (INTN)idx_highlight) {
729 /* store the selected entry in a persistent EFI variable */
730 efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE);
731 config->idx_default_efivar = idx_highlight;
732 status = StrDuplicate(L"Default boot entry selected.");
733 } else {
734 /* clear the default entry EFI variable */
735 efivar_set(L"LoaderEntryDefault", NULL, TRUE);
736 config->idx_default_efivar = -1;
737 status = StrDuplicate(L"Default boot entry cleared.");
738 }
739 refresh = TRUE;
740 break;
741
742 case KEYPRESS(0, 0, '-'):
743 case KEYPRESS(0, 0, 'T'):
744 if (config->timeout_sec_efivar > 0) {
745 config->timeout_sec_efivar--;
746 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
747 if (config->timeout_sec_efivar > 0)
748 status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
749 else
750 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
751 } else if (config->timeout_sec_efivar <= 0){
752 config->timeout_sec_efivar = -1;
753 efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
754 if (config->timeout_sec_config > 0)
755 status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
756 config->timeout_sec_config);
757 else
758 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
759 }
760 break;
761
762 case KEYPRESS(0, 0, '+'):
763 case KEYPRESS(0, 0, 't'):
764 if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
765 config->timeout_sec_efivar++;
766 config->timeout_sec_efivar++;
767 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
768 if (config->timeout_sec_efivar > 0)
769 status = PoolPrint(L"Menu timeout set to %d sec.",
770 config->timeout_sec_efivar);
771 else
772 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
773 break;
774
775 case KEYPRESS(0, 0, 'e'):
776 /* only the options of configured entries can be edited */
777 if (!config->editor || config->entries[idx_highlight]->type == LOADER_UNDEFINED)
778 break;
779 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
780 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
781 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
782 if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
783 exit = TRUE;
784 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
785 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
786 break;
787
788 case KEYPRESS(0, 0, 'v'):
789 status = PoolPrint(L"systemd-boot " PACKAGE_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
790 ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
791 ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
792 break;
793
794 case KEYPRESS(0, 0, 'P'):
795 print_status(config, loaded_image_path);
796 refresh = TRUE;
797 break;
798
799 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
800 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
801 refresh = TRUE;
802 break;
803
804 default:
805 /* jump with a hotkey directly to a matching entry */
806 idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
807 if (idx < 0)
808 break;
809 idx_highlight = idx;
810 refresh = TRUE;
811 }
812
813 if (idx_highlight > idx_last) {
814 idx_last = idx_highlight;
815 idx_first = 1 + idx_highlight - visible_max;
816 refresh = TRUE;
817 } else if (idx_highlight < idx_first) {
818 idx_first = idx_highlight;
819 idx_last = idx_highlight + visible_max-1;
820 refresh = TRUE;
821 }
822
823 if (!refresh && idx_highlight != idx_highlight_prev)
824 highlight = TRUE;
825 }
826
827 *chosen_entry = config->entries[idx_highlight];
828
829 for (i = 0; i < config->entry_count; i++)
830 FreePool(lines[i]);
831 FreePool(lines);
832 FreePool(clearline);
833
834 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
835 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
836 return run;
837 }
838
839 static VOID config_add_entry(Config *config, ConfigEntry *entry) {
840 if ((config->entry_count & 15) == 0) {
841 UINTN i;
842
843 i = config->entry_count + 16;
844 if (config->entry_count == 0)
845 config->entries = AllocatePool(sizeof(VOID *) * i);
846 else
847 config->entries = ReallocatePool(config->entries,
848 sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
849 }
850 config->entries[config->entry_count++] = entry;
851 }
852
853 static VOID config_entry_free(ConfigEntry *entry) {
854 FreePool(entry->title_show);
855 FreePool(entry->title);
856 FreePool(entry->machine_id);
857 FreePool(entry->loader);
858 FreePool(entry->options);
859 }
860
861 static BOOLEAN is_digit(CHAR16 c) {
862 return (c >= '0') && (c <= '9');
863 }
864
865 static UINTN c_order(CHAR16 c) {
866 if (c == '\0')
867 return 0;
868 if (is_digit(c))
869 return 0;
870 else if ((c >= 'a') && (c <= 'z'))
871 return c;
872 else
873 return c + 0x10000;
874 }
875
876 static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) {
877 CHAR16 *os1 = s1;
878 CHAR16 *os2 = s2;
879
880 while (*s1 || *s2) {
881 INTN first;
882
883 while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
884 INTN order;
885
886 order = c_order(*s1) - c_order(*s2);
887 if (order)
888 return order;
889 s1++;
890 s2++;
891 }
892
893 while (*s1 == '0')
894 s1++;
895 while (*s2 == '0')
896 s2++;
897
898 first = 0;
899 while (is_digit(*s1) && is_digit(*s2)) {
900 if (first == 0)
901 first = *s1 - *s2;
902 s1++;
903 s2++;
904 }
905
906 if (is_digit(*s1))
907 return 1;
908 if (is_digit(*s2))
909 return -1;
910
911 if (first)
912 return first;
913 }
914
915 return StrCmp(os1, os2);
916 }
917
918 static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
919 CHAR8 *line;
920 UINTN linelen;
921 CHAR8 *value;
922
923 skip:
924 line = content + *pos;
925 if (*line == '\0')
926 return NULL;
927
928 linelen = 0;
929 while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
930 linelen++;
931
932 /* move pos to next line */
933 *pos += linelen;
934 if (content[*pos])
935 (*pos)++;
936
937 /* empty line */
938 if (linelen == 0)
939 goto skip;
940
941 /* terminate line */
942 line[linelen] = '\0';
943
944 /* remove leading whitespace */
945 while (strchra((CHAR8 *)" \t", *line)) {
946 line++;
947 linelen--;
948 }
949
950 /* remove trailing whitespace */
951 while (linelen > 0 && strchra(sep, line[linelen-1]))
952 linelen--;
953 line[linelen] = '\0';
954
955 if (*line == '#')
956 goto skip;
957
958 /* split key/value */
959 value = line;
960 while (*value && !strchra(sep, *value))
961 value++;
962 if (*value == '\0')
963 goto skip;
964 *value = '\0';
965 value++;
966 while (*value && strchra(sep, *value))
967 value++;
968
969 /* unquote */
970 if (value[0] == '\"' && line[linelen-1] == '\"') {
971 value++;
972 line[linelen-1] = '\0';
973 }
974
975 *key_ret = line;
976 *value_ret = value;
977 return line;
978 }
979
980 static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
981 CHAR8 *line;
982 UINTN pos = 0;
983 CHAR8 *key, *value;
984
985 line = content;
986 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
987 if (strcmpa((CHAR8 *)"timeout", key) == 0) {
988 _cleanup_freepool_ CHAR16 *s = NULL;
989
990 s = stra_to_str(value);
991 config->timeout_sec_config = Atoi(s);
992 config->timeout_sec = config->timeout_sec_config;
993 continue;
994 }
995
996 if (strcmpa((CHAR8 *)"default", key) == 0) {
997 FreePool(config->entry_default_pattern);
998 config->entry_default_pattern = stra_to_str(value);
999 StrLwr(config->entry_default_pattern);
1000 continue;
1001 }
1002
1003 if (strcmpa((CHAR8 *)"editor", key) == 0) {
1004 BOOLEAN on;
1005
1006 if (EFI_ERROR(parse_boolean(value, &on)))
1007 continue;
1008 config->editor = on;
1009 }
1010
1011 if (strcmpa((CHAR8 *)"auto-entries", key) == 0) {
1012 BOOLEAN on;
1013
1014 if (EFI_ERROR(parse_boolean(value, &on)))
1015 continue;
1016 config->auto_entries = on;
1017 }
1018
1019 if (strcmpa((CHAR8 *)"auto-firmware", key) == 0) {
1020 BOOLEAN on;
1021
1022 if (EFI_ERROR(parse_boolean(value, &on)))
1023 continue;
1024 config->auto_firmware = on;
1025 }
1026
1027 if (strcmpa((CHAR8 *)"console-mode", key) == 0) {
1028 if (strcmpa((CHAR8 *)"auto", value) == 0)
1029 config->console_mode_change = CONSOLE_MODE_AUTO;
1030 else if (strcmpa((CHAR8 *)"max", value) == 0)
1031 config->console_mode_change = CONSOLE_MODE_MAX;
1032 else if (strcmpa((CHAR8 *)"keep", value) == 0)
1033 config->console_mode_change = CONSOLE_MODE_KEEP;
1034 else {
1035 _cleanup_freepool_ CHAR16 *s = NULL;
1036
1037 s = stra_to_str(value);
1038 config->console_mode = Atoi(s);
1039 config->console_mode_change = CONSOLE_MODE_SET;
1040 }
1041
1042 continue;
1043 }
1044 }
1045 }
1046
1047 static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
1048 ConfigEntry *entry;
1049 CHAR8 *line;
1050 UINTN pos = 0;
1051 CHAR8 *key, *value;
1052 UINTN len;
1053 _cleanup_freepool_ CHAR16 *initrd = NULL;
1054
1055 entry = AllocateZeroPool(sizeof(ConfigEntry));
1056
1057 line = content;
1058 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1059 if (strcmpa((CHAR8 *)"title", key) == 0) {
1060 FreePool(entry->title);
1061 entry->title = stra_to_str(value);
1062 continue;
1063 }
1064
1065 if (strcmpa((CHAR8 *)"version", key) == 0) {
1066 FreePool(entry->version);
1067 entry->version = stra_to_str(value);
1068 continue;
1069 }
1070
1071 if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
1072 FreePool(entry->machine_id);
1073 entry->machine_id = stra_to_str(value);
1074 continue;
1075 }
1076
1077 if (strcmpa((CHAR8 *)"linux", key) == 0) {
1078 FreePool(entry->loader);
1079 entry->type = LOADER_LINUX;
1080 entry->loader = stra_to_path(value);
1081 entry->key = 'l';
1082 continue;
1083 }
1084
1085 if (strcmpa((CHAR8 *)"efi", key) == 0) {
1086 entry->type = LOADER_EFI;
1087 FreePool(entry->loader);
1088 entry->loader = stra_to_path(value);
1089
1090 /* do not add an entry for ourselves */
1091 if (StriCmp(entry->loader, loaded_image_path) == 0) {
1092 entry->type = LOADER_UNDEFINED;
1093 break;
1094 }
1095 continue;
1096 }
1097
1098 if (strcmpa((CHAR8 *)"architecture", key) == 0) {
1099 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1100 if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) {
1101 entry->type = LOADER_UNDEFINED;
1102 break;
1103 }
1104 continue;
1105 }
1106
1107 if (strcmpa((CHAR8 *)"initrd", key) == 0) {
1108 _cleanup_freepool_ CHAR16 *new = NULL;
1109
1110 new = stra_to_path(value);
1111 if (initrd) {
1112 CHAR16 *s;
1113
1114 s = PoolPrint(L"%s initrd=%s", initrd, new);
1115 FreePool(initrd);
1116 initrd = s;
1117 } else
1118 initrd = PoolPrint(L"initrd=%s", new);
1119
1120 continue;
1121 }
1122
1123 if (strcmpa((CHAR8 *)"options", key) == 0) {
1124 _cleanup_freepool_ CHAR16 *new = NULL;
1125
1126 new = stra_to_str(value);
1127 if (entry->options) {
1128 CHAR16 *s;
1129
1130 s = PoolPrint(L"%s %s", entry->options, new);
1131 FreePool(entry->options);
1132 entry->options = s;
1133 } else {
1134 entry->options = new;
1135 new = NULL;
1136 }
1137
1138 continue;
1139 }
1140 }
1141
1142 if (entry->type == LOADER_UNDEFINED) {
1143 config_entry_free(entry);
1144 FreePool(entry);
1145 return;
1146 }
1147
1148 /* add initrd= to options */
1149 if (entry->type == LOADER_LINUX && initrd) {
1150 if (entry->options) {
1151 CHAR16 *s;
1152
1153 s = PoolPrint(L"%s %s", initrd, entry->options);
1154 FreePool(entry->options);
1155 entry->options = s;
1156 } else {
1157 entry->options = initrd;
1158 initrd = NULL;
1159 }
1160 }
1161
1162 entry->device = device;
1163 entry->file = StrDuplicate(file);
1164 len = StrLen(entry->file);
1165 /* remove ".conf" */
1166 if (len > 5)
1167 entry->file[len - 5] = '\0';
1168 StrLwr(entry->file);
1169
1170 config_add_entry(config, entry);
1171 }
1172
1173 static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) {
1174 _cleanup_freepool_ CHAR8 *content = NULL;
1175 UINTN sec;
1176 EFI_STATUS err;
1177
1178 config->editor = TRUE;
1179 config->auto_entries = TRUE;
1180 config->auto_firmware = TRUE;
1181
1182 err = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content, NULL);
1183 if (!EFI_ERROR(err))
1184 config_defaults_load_from_file(config, content);
1185
1186 err = efivar_get_int(L"LoaderConfigTimeout", &sec);
1187 if (!EFI_ERROR(err)) {
1188 config->timeout_sec_efivar = sec;
1189 config->timeout_sec = sec;
1190 } else
1191 config->timeout_sec_efivar = -1;
1192 }
1193
1194 static VOID config_load_entries(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
1195 EFI_FILE_HANDLE entries_dir;
1196 EFI_STATUS err;
1197
1198 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
1199 if (!EFI_ERROR(err)) {
1200 for (;;) {
1201 CHAR16 buf[256];
1202 UINTN bufsize;
1203 EFI_FILE_INFO *f;
1204 _cleanup_freepool_ CHAR8 *content = NULL;
1205 UINTN len;
1206
1207 bufsize = sizeof(buf);
1208 err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
1209 if (bufsize == 0 || EFI_ERROR(err))
1210 break;
1211
1212 f = (EFI_FILE_INFO *) buf;
1213 if (f->FileName[0] == '.')
1214 continue;
1215 if (f->Attribute & EFI_FILE_DIRECTORY)
1216 continue;
1217
1218 len = StrLen(f->FileName);
1219 if (len < 6)
1220 continue;
1221 if (StriCmp(f->FileName + len - 5, L".conf") != 0)
1222 continue;
1223 if (StrnCmp(f->FileName, L"auto-", 5) == 0)
1224 continue;
1225
1226 err = file_read(entries_dir, f->FileName, 0, 0, &content, NULL);
1227 if (!EFI_ERROR(err))
1228 config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path);
1229 }
1230 uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
1231 }
1232 }
1233
1234 static VOID config_sort_entries(Config *config) {
1235 UINTN i;
1236
1237 for (i = 1; i < config->entry_count; i++) {
1238 BOOLEAN more;
1239 UINTN k;
1240
1241 more = FALSE;
1242 for (k = 0; k < config->entry_count - i; k++) {
1243 ConfigEntry *entry;
1244
1245 if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0)
1246 continue;
1247 entry = config->entries[k];
1248 config->entries[k] = config->entries[k+1];
1249 config->entries[k+1] = entry;
1250 more = TRUE;
1251 }
1252 if (!more)
1253 break;
1254 }
1255 }
1256
1257 static VOID config_default_entry_select(Config *config) {
1258 _cleanup_freepool_ CHAR16 *entry_oneshot = NULL, *entry_default = NULL;
1259 EFI_STATUS err;
1260 UINTN i;
1261
1262 /*
1263 * The EFI variable to specify a boot entry for the next, and only the
1264 * next reboot. The variable is always cleared directly after it is read.
1265 */
1266 err = efivar_get(L"LoaderEntryOneShot", &entry_oneshot);
1267 if (!EFI_ERROR(err)) {
1268 BOOLEAN found = FALSE;
1269
1270 for (i = 0; i < config->entry_count; i++)
1271 if (StrCmp(config->entries[i]->file, entry_oneshot) == 0) {
1272 config->idx_default = i;
1273 found = TRUE;
1274 break;
1275 }
1276
1277 config->entry_oneshot = StrDuplicate(entry_oneshot);
1278 efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
1279 if (found)
1280 return;
1281 }
1282
1283 /*
1284 * The EFI variable to select the default boot entry overrides the
1285 * configured pattern. The variable can be set and cleared by pressing
1286 * the 'd' key in the loader selection menu, the entry is marked with
1287 * an '*'.
1288 */
1289 err = efivar_get(L"LoaderEntryDefault", &entry_default);
1290 if (!EFI_ERROR(err)) {
1291 for (i = 0; i < config->entry_count; i++)
1292 if (StrCmp(config->entries[i]->file, entry_default) == 0) {
1293 config->idx_default = i;
1294 config->idx_default_efivar = i;
1295 return;
1296 }
1297 }
1298 config->idx_default_efivar = -1;
1299
1300 if (config->entry_count == 0)
1301 return;
1302
1303 /*
1304 * Match the pattern from the end of the list to the start, find last
1305 * entry (largest number) matching the given pattern.
1306 */
1307 if (config->entry_default_pattern) {
1308 i = config->entry_count;
1309 while (i--) {
1310 if (config->entries[i]->no_autoselect)
1311 continue;
1312 if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) {
1313 config->idx_default = i;
1314 return;
1315 }
1316 }
1317 }
1318
1319 /* select the last suitable entry */
1320 i = config->entry_count;
1321 while (i--) {
1322 if (config->entries[i]->no_autoselect)
1323 continue;
1324 config->idx_default = i;
1325 return;
1326 }
1327
1328 /* no entry found */
1329 config->idx_default = -1;
1330 }
1331
1332 static BOOLEAN find_nonunique(ConfigEntry **entries, UINTN entry_count) {
1333 BOOLEAN non_unique = FALSE;
1334 UINTN i, k;
1335
1336 for (i = 0; i < entry_count; i++)
1337 entries[i]->non_unique = FALSE;
1338
1339 for (i = 0; i < entry_count; i++)
1340 for (k = 0; k < entry_count; k++) {
1341 if (i == k)
1342 continue;
1343 if (StrCmp(entries[i]->title_show, entries[k]->title_show) != 0)
1344 continue;
1345
1346 non_unique = entries[i]->non_unique = entries[k]->non_unique = TRUE;
1347 }
1348
1349 return non_unique;
1350 }
1351
1352 /* generate a unique title, avoiding non-distinguishable menu entries */
1353 static VOID config_title_generate(Config *config) {
1354 UINTN i;
1355
1356 /* set title */
1357 for (i = 0; i < config->entry_count; i++) {
1358 CHAR16 *title;
1359
1360 FreePool(config->entries[i]->title_show);
1361 title = config->entries[i]->title;
1362 if (!title)
1363 title = config->entries[i]->file;
1364 config->entries[i]->title_show = StrDuplicate(title);
1365 }
1366
1367 if (!find_nonunique(config->entries, config->entry_count))
1368 return;
1369
1370 /* add version to non-unique titles */
1371 for (i = 0; i < config->entry_count; i++) {
1372 CHAR16 *s;
1373
1374 if (!config->entries[i]->non_unique)
1375 continue;
1376 if (!config->entries[i]->version)
1377 continue;
1378
1379 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
1380 FreePool(config->entries[i]->title_show);
1381 config->entries[i]->title_show = s;
1382 }
1383
1384 if (!find_nonunique(config->entries, config->entry_count))
1385 return;
1386
1387 /* add machine-id to non-unique titles */
1388 for (i = 0; i < config->entry_count; i++) {
1389 CHAR16 *s;
1390 _cleanup_freepool_ CHAR16 *m = NULL;
1391
1392 if (!config->entries[i]->non_unique)
1393 continue;
1394 if (!config->entries[i]->machine_id)
1395 continue;
1396
1397 m = StrDuplicate(config->entries[i]->machine_id);
1398 m[8] = '\0';
1399 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
1400 FreePool(config->entries[i]->title_show);
1401 config->entries[i]->title_show = s;
1402 }
1403
1404 if (!find_nonunique(config->entries, config->entry_count))
1405 return;
1406
1407 /* add file name to non-unique titles */
1408 for (i = 0; i < config->entry_count; i++) {
1409 CHAR16 *s;
1410
1411 if (!config->entries[i]->non_unique)
1412 continue;
1413 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file);
1414 FreePool(config->entries[i]->title_show);
1415 config->entries[i]->title_show = s;
1416 config->entries[i]->non_unique = FALSE;
1417 }
1418 }
1419
1420 static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) {
1421 ConfigEntry *entry;
1422
1423 entry = AllocateZeroPool(sizeof(ConfigEntry));
1424 entry->title = StrDuplicate(title);
1425 entry->call = call;
1426 entry->no_autoselect = TRUE;
1427 config_add_entry(config, entry);
1428 return TRUE;
1429 }
1430
1431 static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device,
1432 enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1433 ConfigEntry *entry;
1434
1435 entry = AllocateZeroPool(sizeof(ConfigEntry));
1436 entry->type = type;
1437 entry->title = StrDuplicate(title);
1438 entry->device = device;
1439 entry->loader = StrDuplicate(loader);
1440 entry->file = StrDuplicate(file);
1441 StrLwr(entry->file);
1442 entry->key = key;
1443 config_add_entry(config, entry);
1444
1445 return entry;
1446 }
1447
1448 static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1449 CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1450 EFI_FILE_HANDLE handle;
1451 ConfigEntry *entry;
1452 EFI_STATUS err;
1453
1454 if (!config->auto_entries)
1455 return FALSE;
1456
1457 /* do not add an entry for ourselves */
1458 if (loaded_image_path) {
1459 UINTN len;
1460 _cleanup_freepool_ CHAR8 *content = NULL;
1461
1462 if (StriCmp(loader, loaded_image_path) == 0)
1463 return FALSE;
1464
1465 /* look for systemd-boot magic string */
1466 err = file_read(root_dir, loader, 0, 100*1024, &content, &len);
1467 if (!EFI_ERROR(err)) {
1468 CHAR8 *start = content;
1469 CHAR8 *last = content + len - sizeof(magic) - 1;
1470
1471 for (; start <= last; start++)
1472 if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0)
1473 return FALSE;
1474 }
1475 }
1476
1477 /* check existence */
1478 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
1479 if (EFI_ERROR(err))
1480 return FALSE;
1481 uefi_call_wrapper(handle->Close, 1, handle);
1482
1483 entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader);
1484 if (!entry)
1485 return FALSE;
1486
1487 /* do not boot right away into auto-detected entries */
1488 entry->no_autoselect = TRUE;
1489
1490 return TRUE;
1491 }
1492
1493 static VOID config_entry_add_osx(Config *config) {
1494 EFI_STATUS err;
1495 UINTN handle_count = 0;
1496 _cleanup_freepool_ EFI_HANDLE *handles = NULL;
1497
1498 if (!config->auto_entries)
1499 return;
1500
1501 err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
1502 if (!EFI_ERROR(err)) {
1503 UINTN i;
1504
1505 for (i = 0; i < handle_count; i++) {
1506 EFI_FILE *root;
1507 BOOLEAN found;
1508
1509 root = LibOpenRoot(handles[i]);
1510 if (!root)
1511 continue;
1512 found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"macOS",
1513 L"\\System\\Library\\CoreServices\\boot.efi");
1514 uefi_call_wrapper(root->Close, 1, root);
1515 if (found)
1516 break;
1517 }
1518 }
1519 }
1520
1521 static VOID config_entry_add_linux(Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) {
1522 EFI_FILE_HANDLE linux_dir;
1523 EFI_STATUS err;
1524 ConfigEntry *entry;
1525
1526 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL);
1527 if (EFI_ERROR(err))
1528 return;
1529
1530 for (;;) {
1531 CHAR16 buf[256];
1532 UINTN bufsize = sizeof buf;
1533 EFI_FILE_INFO *f;
1534 CHAR8 *sections[] = {
1535 (UINT8 *)".osrel",
1536 (UINT8 *)".cmdline",
1537 NULL
1538 };
1539 UINTN offs[ELEMENTSOF(sections)-1] = {};
1540 UINTN szs[ELEMENTSOF(sections)-1] = {};
1541 UINTN addrs[ELEMENTSOF(sections)-1] = {};
1542 CHAR8 *content = NULL;
1543 UINTN len;
1544 CHAR8 *line;
1545 UINTN pos = 0;
1546 CHAR8 *key, *value;
1547 CHAR16 *os_name = NULL;
1548 CHAR16 *os_id = NULL;
1549 CHAR16 *os_version = NULL;
1550 CHAR16 *os_build = NULL;
1551
1552 err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf);
1553 if (bufsize == 0 || EFI_ERROR(err))
1554 break;
1555
1556 f = (EFI_FILE_INFO *) buf;
1557 if (f->FileName[0] == '.')
1558 continue;
1559 if (f->Attribute & EFI_FILE_DIRECTORY)
1560 continue;
1561 len = StrLen(f->FileName);
1562 if (len < 5)
1563 continue;
1564 if (StriCmp(f->FileName + len - 4, L".efi") != 0)
1565 continue;
1566
1567 /* look for .osrel and .cmdline sections in the .efi binary */
1568 err = pe_file_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs);
1569 if (EFI_ERROR(err))
1570 continue;
1571
1572 err = file_read(linux_dir, f->FileName, offs[0], szs[0], &content, NULL);
1573 if (EFI_ERROR(err))
1574 continue;
1575
1576 /* read properties from the embedded os-release file */
1577 line = content;
1578 while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
1579 if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) {
1580 FreePool(os_name);
1581 os_name = stra_to_str(value);
1582 continue;
1583 }
1584
1585 if (strcmpa((CHAR8 *)"ID", key) == 0) {
1586 FreePool(os_id);
1587 os_id = stra_to_str(value);
1588 continue;
1589 }
1590
1591 if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) {
1592 FreePool(os_version);
1593 os_version = stra_to_str(value);
1594 continue;
1595 }
1596
1597 if (strcmpa((CHAR8 *)"BUILD_ID", key) == 0) {
1598 FreePool(os_build);
1599 os_build = stra_to_str(value);
1600 continue;
1601 }
1602 }
1603
1604 if (os_name && os_id && (os_version || os_build)) {
1605 CHAR16 *conf;
1606 CHAR16 *path;
1607 CHAR16 *cmdline;
1608
1609 conf = PoolPrint(L"%s-%s", os_id, os_version ? : os_build);
1610 path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName);
1611 entry = config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path);
1612
1613 FreePool(content);
1614 content = NULL;
1615 /* read the embedded cmdline file */
1616 err = file_read(linux_dir, f->FileName, offs[1], szs[1] - 1, &content, NULL);
1617 if (!EFI_ERROR(err)) {
1618 cmdline = stra_to_str(content);
1619 entry->options = cmdline;
1620 cmdline = NULL;
1621 }
1622 FreePool(cmdline);
1623 FreePool(conf);
1624 FreePool(path);
1625 }
1626
1627 FreePool(os_name);
1628 FreePool(os_id);
1629 FreePool(os_version);
1630 FreePool(os_build);
1631 FreePool(content);
1632 }
1633
1634 uefi_call_wrapper(linux_dir->Close, 1, linux_dir);
1635 }
1636
1637 static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) {
1638 EFI_HANDLE image;
1639 _cleanup_freepool_ EFI_DEVICE_PATH *path = NULL;
1640 CHAR16 *options;
1641 EFI_STATUS err;
1642
1643 path = FileDevicePath(entry->device, entry->loader);
1644 if (!path) {
1645 Print(L"Error getting device path.");
1646 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1647 return EFI_INVALID_PARAMETER;
1648 }
1649
1650 err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
1651 if (EFI_ERROR(err)) {
1652 Print(L"Error loading %s: %r", entry->loader, err);
1653 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1654 return err;
1655 }
1656
1657 if (config->options_edit)
1658 options = config->options_edit;
1659 else if (entry->options)
1660 options = entry->options;
1661 else
1662 options = NULL;
1663 if (options) {
1664 EFI_LOADED_IMAGE *loaded_image;
1665
1666 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1667 parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1668 if (EFI_ERROR(err)) {
1669 Print(L"Error getting LoadedImageProtocol handle: %r", err);
1670 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1671 goto out_unload;
1672 }
1673 loaded_image->LoadOptions = options;
1674 loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
1675
1676 #if ENABLE_TPM
1677 /* Try to log any options to the TPM, especially to catch manually edited options */
1678 err = tpm_log_event(SD_TPM_PCR,
1679 (EFI_PHYSICAL_ADDRESS) (UINTN) loaded_image->LoadOptions,
1680 loaded_image->LoadOptionsSize, loaded_image->LoadOptions);
1681 if (EFI_ERROR(err)) {
1682 Print(L"Unable to add image options measurement: %r", err);
1683 uefi_call_wrapper(BS->Stall, 1, 200 * 1000);
1684 }
1685 #endif
1686 }
1687
1688 efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
1689 err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
1690 out_unload:
1691 uefi_call_wrapper(BS->UnloadImage, 1, image);
1692 return err;
1693 }
1694
1695 static EFI_STATUS reboot_into_firmware(VOID) {
1696 _cleanup_freepool_ CHAR8 *b = NULL;
1697 UINTN size;
1698 UINT64 osind;
1699 EFI_STATUS err;
1700
1701 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
1702
1703 err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
1704 if (!EFI_ERROR(err))
1705 osind |= (UINT64)*b;
1706
1707 err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
1708 if (EFI_ERROR(err))
1709 return err;
1710
1711 err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
1712 Print(L"Error calling ResetSystem: %r", err);
1713 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1714 return err;
1715 }
1716
1717 static VOID config_free(Config *config) {
1718 UINTN i;
1719
1720 for (i = 0; i < config->entry_count; i++)
1721 config_entry_free(config->entries[i]);
1722 FreePool(config->entries);
1723 FreePool(config->entry_default_pattern);
1724 FreePool(config->options_edit);
1725 FreePool(config->entry_oneshot);
1726 }
1727
1728 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
1729 _cleanup_freepool_ CHAR16 *infostr = NULL, *typestr = NULL;
1730 CHAR8 *b;
1731 UINTN size;
1732 EFI_LOADED_IMAGE *loaded_image;
1733 EFI_FILE *root_dir;
1734 CHAR16 *loaded_image_path;
1735 EFI_STATUS err;
1736 Config config;
1737 UINT64 init_usec;
1738 BOOLEAN menu = FALSE;
1739 CHAR16 uuid[37];
1740
1741 InitializeLib(image, sys_table);
1742 init_usec = time_usec();
1743 efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
1744 efivar_set(L"LoaderInfo", L"systemd-boot " PACKAGE_VERSION, FALSE);
1745
1746 infostr = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1747 efivar_set(L"LoaderFirmwareInfo", infostr, FALSE);
1748
1749 typestr = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
1750 efivar_set(L"LoaderFirmwareType", typestr, FALSE);
1751
1752 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1753 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1754 if (EFI_ERROR(err)) {
1755 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
1756 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1757 return err;
1758 }
1759
1760 /* export the device path this image is started from */
1761 if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS)
1762 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
1763
1764 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
1765 if (!root_dir) {
1766 Print(L"Unable to open root directory: %r ", err);
1767 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1768 return EFI_LOAD_ERROR;
1769 }
1770
1771 if (secure_boot_enabled() && shim_loaded()) {
1772 err = security_policy_install();
1773 if (EFI_ERROR(err)) {
1774 Print(L"Error installing security policy: %r ", err);
1775 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1776 return err;
1777 }
1778 }
1779
1780 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1781 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
1782 efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
1783
1784 ZeroMem(&config, sizeof(Config));
1785 config_load_defaults(&config, root_dir);
1786
1787 /* scan /EFI/Linux/ directory */
1788 config_entry_add_linux(&config, loaded_image, root_dir);
1789
1790 /* scan /loader/entries/\*.conf files */
1791 config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
1792
1793 /* sort entries after version number */
1794 config_sort_entries(&config);
1795
1796 /* if we find some well-known loaders, add them to the end of the list */
1797 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
1798 L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1799 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
1800 L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
1801 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1802 L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");
1803 config_entry_add_osx(&config);
1804
1805 if (config.auto_firmware && efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
1806 UINT64 osind = (UINT64)*b;
1807
1808 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
1809 config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
1810 FreePool(b);
1811 }
1812
1813 if (config.entry_count == 0) {
1814 Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1815 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1816 goto out;
1817 }
1818
1819 config_title_generate(&config);
1820
1821 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */
1822 config_default_entry_select(&config);
1823
1824 /* if no configured entry to select from was found, enable the menu */
1825 if (config.idx_default == -1) {
1826 config.idx_default = 0;
1827 if (config.timeout_sec == 0)
1828 config.timeout_sec = 10;
1829 }
1830
1831 /* select entry or show menu when key is pressed or timeout is set */
1832 if (config.timeout_sec == 0) {
1833 UINT64 key;
1834
1835 err = console_key_read(&key, FALSE);
1836 if (!EFI_ERROR(err)) {
1837 INT16 idx;
1838
1839 /* find matching key in config entries */
1840 idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
1841 if (idx >= 0)
1842 config.idx_default = idx;
1843 else
1844 menu = TRUE;
1845 }
1846 } else
1847 menu = TRUE;
1848
1849 for (;;) {
1850 ConfigEntry *entry;
1851
1852 entry = config.entries[config.idx_default];
1853 if (menu) {
1854 efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
1855 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
1856 if (!menu_run(&config, &entry, loaded_image_path))
1857 break;
1858
1859 /* run special entry like "reboot" */
1860 if (entry->call) {
1861 entry->call();
1862 continue;
1863 }
1864 }
1865
1866 /* export the selected boot entry to the system */
1867 efivar_set(L"LoaderEntrySelected", entry->file, FALSE);
1868
1869 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
1870 err = image_start(image, &config, entry);
1871 if (EFI_ERROR(err)) {
1872 graphics_mode(FALSE);
1873 Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err);
1874 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1875 goto out;
1876 }
1877
1878 menu = TRUE;
1879 config.timeout_sec = 0;
1880 }
1881 err = EFI_SUCCESS;
1882 out:
1883 FreePool(loaded_image_path);
1884 config_free(&config);
1885 uefi_call_wrapper(root_dir->Close, 1, root_dir);
1886 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);
1887 return err;
1888 }