1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * Copyright (C) 2012-2015 Kay Sievers <kay@vrfy.org>
15 * Copyright (C) 2012-2015 Harald Hoyer <harald@redhat.com>
27 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
28 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
31 /* magic string to find in the binary image */
32 static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " VERSION " ####";
34 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
49 enum loader_type type;
53 EFI_STATUS (*call)(VOID);
54 BOOLEAN no_autoselect;
59 ConfigEntry **entries;
62 INTN idx_default_efivar;
64 UINTN timeout_sec_config;
65 INTN timeout_sec_efivar;
66 CHAR16 *entry_default_pattern;
67 CHAR16 *entry_oneshot;
71 static VOID cursor_left(UINTN *cursor, UINTN *first)
75 else if ((*first) > 0)
79 static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
81 if ((*cursor)+1 < x_max)
83 else if ((*first) + (*cursor) < len)
87 static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
100 size = StrLen(line_in) + 1024;
101 line = AllocatePool(size * sizeof(CHAR16));
102 StrCpy(line, line_in);
104 print = AllocatePool((x_max+1) * sizeof(CHAR16));
106 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
121 CopyMem(print, line + first, i * sizeof(CHAR16));
122 while (clear > 0 && i < x_max-1) {
128 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
129 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
130 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
132 err = console_key_read(&key, TRUE);
137 case KEYPRESS(0, SCAN_ESC, 0):
138 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'):
139 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'):
140 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')):
141 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')):
145 case KEYPRESS(0, SCAN_HOME, 0):
146 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'):
147 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')):
148 /* beginning-of-line */
153 case KEYPRESS(0, SCAN_END, 0):
154 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'):
155 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')):
157 cursor = len - first;
158 if (cursor+1 >= x_max) {
160 first = len - (x_max-1);
164 case KEYPRESS(0, SCAN_DOWN, 0):
165 case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'):
166 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0):
168 while (line[first + cursor] && line[first + cursor] == ' ')
169 cursor_right(&cursor, &first, x_max, len);
170 while (line[first + cursor] && line[first + cursor] != ' ')
171 cursor_right(&cursor, &first, x_max, len);
172 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
175 case KEYPRESS(0, SCAN_UP, 0):
176 case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'):
177 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0):
179 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
180 cursor_left(&cursor, &first);
181 while ((first + cursor) > 0 && line[first + cursor] == ' ')
182 cursor_left(&cursor, &first);
184 while ((first + cursor) > 0 && line[first + cursor-1] != ' ')
185 cursor_left(&cursor, &first);
186 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
189 case KEYPRESS(0, SCAN_RIGHT, 0):
190 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'):
191 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')):
193 if (first + cursor == len)
195 cursor_right(&cursor, &first, x_max, len);
196 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
199 case KEYPRESS(0, SCAN_LEFT, 0):
200 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'):
201 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')):
203 cursor_left(&cursor, &first);
204 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
207 case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
210 for (i = first + cursor; i < len && line[i] == ' '; i++)
212 for (; i < len && line[i] != ' '; i++)
215 for (i = first + cursor; i + clear < len; i++)
216 line[i] = line[i + clear];
221 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'):
222 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')):
223 case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE):
224 /* backward-kill-word */
226 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
227 cursor_left(&cursor, &first);
229 while ((first + cursor) > 0 && line[first + cursor] == ' ') {
230 cursor_left(&cursor, &first);
234 while ((first + cursor) > 0 && line[first + cursor-1] != ' ') {
235 cursor_left(&cursor, &first);
238 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
240 for (i = first + cursor; i + clear < len; i++)
241 line[i] = line[i + clear];
246 case KEYPRESS(0, SCAN_DELETE, 0):
247 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'):
248 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')):
251 if (first + cursor == len)
253 for (i = first + cursor; i < len; i++)
259 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'):
260 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')):
262 line[first + cursor] = '\0';
263 clear = len - (first + cursor);
264 len = first + cursor;
267 case KEYPRESS(0, 0, CHAR_LINEFEED):
268 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
269 if (StrCmp(line, line_in) != 0) {
277 case KEYPRESS(0, 0, CHAR_BACKSPACE):
280 if (first == 0 && cursor == 0)
282 for (i = first + cursor-1; i < len; i++)
288 if (cursor > 0 || first == 0)
290 /* show full line if it fits */
296 /* jump left to see what we delete */
306 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
307 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
310 for (i = len; i > first + cursor; i--)
312 line[first + cursor] = KEYCHAR(key);
315 if (cursor+1 < x_max)
317 else if (first + cursor < len)
323 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
329 static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
335 /* select entry by number key */
336 if (key >= '1' && key <= '9') {
338 if (i > config->entry_count)
339 i = config->entry_count;
343 /* find matching key in config entries */
344 for (i = start; i < config->entry_count; i++)
345 if (config->entries[i]->key == key)
348 for (i = 0; i < start; i++)
349 if (config->entries[i]->key == key)
355 static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
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);
367 Print(L"systemd-boot version: " VERSION "\n");
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);
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);
377 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
378 Print(L"SecureBoot: %s\n", *b > 0 ? L"enabled" : L"disabled");
382 if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) {
383 Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user");
387 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
388 Print(L"OsIndicationsSupported: %d\n", (UINT64)*b);
393 Print(L"timeout: %d\n", config->timeout_sec);
394 if (config->timeout_sec_efivar >= 0)
395 Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
396 Print(L"timeout (config): %d\n", config->timeout_sec_config);
397 if (config->entry_default_pattern)
398 Print(L"default pattern: '%s'\n", config->entry_default_pattern);
401 Print(L"config entry count: %d\n", config->entry_count);
402 Print(L"entry selected idx: %d\n", config->idx_default);
403 if (config->idx_default_efivar >= 0)
404 Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
407 if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
408 Print(L"LoaderConfigTimeout: %d\n", i);
409 if (config->entry_oneshot)
410 Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
411 if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) {
412 Print(L"LoaderDevicePartUUID: %s\n", s);
415 if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) {
416 Print(L"LoaderEntryDefault: %s\n", s);
420 Print(L"\n--- press key ---\n\n");
421 console_key_read(&key, TRUE);
423 for (i = 0; i < config->entry_count; i++) {
426 if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
429 entry = config->entries[i];
430 Print(L"config entry: %d/%d\n", i+1, config->entry_count);
432 Print(L"file '%s'\n", entry->file);
433 Print(L"title show '%s'\n", entry->title_show);
435 Print(L"title '%s'\n", entry->title);
437 Print(L"version '%s'\n", entry->version);
438 if (entry->machine_id)
439 Print(L"machine-id '%s'\n", entry->machine_id);
441 EFI_DEVICE_PATH *device_path;
444 device_path = DevicePathFromHandle(entry->device);
446 str = DevicePathToStr(device_path);
447 Print(L"device handle '%s'\n", str);
452 Print(L"loader '%s'\n", entry->loader);
454 Print(L"options '%s'\n", entry->options);
455 Print(L"auto-select %s\n", entry->no_autoselect ? L"no" : L"yes");
457 Print(L"internal call yes\n");
459 Print(L"\n--- press key ---\n\n");
460 console_key_read(&key, TRUE);
463 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
466 static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, CHAR16 *loaded_image_path) {
470 UINTN idx_highlight_prev;
486 BOOLEAN exit = FALSE;
488 BOOLEAN wait = FALSE;
490 graphics_mode(FALSE);
491 uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
492 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
493 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
495 /* draw a single character to make ClearScreen work on some firmware */
496 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" ");
497 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
499 err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
500 if (EFI_ERROR(err)) {
505 /* we check 10 times per second for a keystroke */
506 if (config->timeout_sec > 0)
507 timeout_remain = config->timeout_sec * 10;
511 idx_highlight = config->idx_default;
512 idx_highlight_prev = 0;
514 visible_max = y_max - 2;
516 if ((UINTN)config->idx_default >= visible_max)
517 idx_first = config->idx_default-1;
521 idx_last = idx_first + visible_max-1;
526 /* length of the longest entry */
528 for (i = 0; i < config->entry_count; i++) {
531 entry_len = StrLen(config->entries[i]->title_show);
532 if (line_width < entry_len)
533 line_width = entry_len;
535 if (line_width > x_max-6)
536 line_width = x_max-6;
538 /* offsets to center the entries on the screen */
539 x_start = (x_max - (line_width)) / 2;
540 if (config->entry_count < visible_max)
541 y_start = ((visible_max - config->entry_count) / 2) + 1;
545 /* menu entries title lines */
546 lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
547 for (i = 0; i < config->entry_count; i++) {
550 lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
551 for (j = 0; j < x_start; j++)
554 for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
555 lines[i][j] = config->entries[i]->title_show[k];
557 for (; j < x_max; j++)
559 lines[i][x_max] = '\0';
563 clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
564 for (i = 0; i < x_max; i++)
572 for (i = 0; i < config->entry_count; i++) {
573 if (i < idx_first || i > idx_last)
575 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
576 if (i == idx_highlight)
577 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
578 EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
580 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
581 EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
582 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
583 if ((INTN)i == config->idx_default_efivar) {
584 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
585 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
589 } else if (highlight) {
590 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
591 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
592 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
593 if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
594 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
595 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
598 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
599 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
600 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
601 if ((INTN)idx_highlight == config->idx_default_efivar) {
602 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
603 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
608 if (timeout_remain > 0) {
610 status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
613 /* print status at last line of screen */
619 len = StrLen(status);
621 x = (x_max - len) / 2;
624 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
625 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
626 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
627 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
628 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
631 err = console_key_read(&key, wait);
632 if (EFI_ERROR(err)) {
633 /* timeout reached */
634 if (timeout_remain == 0) {
639 /* sleep and update status */
640 if (timeout_remain > 0) {
641 uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
646 /* timeout disabled, wait for next key */
653 /* clear status after keystroke */
657 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
658 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
659 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
662 idx_highlight_prev = idx_highlight;
665 case KEYPRESS(0, SCAN_UP, 0):
666 case KEYPRESS(0, 0, 'k'):
667 if (idx_highlight > 0)
671 case KEYPRESS(0, SCAN_DOWN, 0):
672 case KEYPRESS(0, 0, 'j'):
673 if (idx_highlight < config->entry_count-1)
677 case KEYPRESS(0, SCAN_HOME, 0):
678 case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
679 if (idx_highlight > 0) {
685 case KEYPRESS(0, SCAN_END, 0):
686 case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
687 if (idx_highlight < config->entry_count-1) {
689 idx_highlight = config->entry_count-1;
693 case KEYPRESS(0, SCAN_PAGE_UP, 0):
694 if (idx_highlight > visible_max)
695 idx_highlight -= visible_max;
700 case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
701 idx_highlight += visible_max;
702 if (idx_highlight > config->entry_count-1)
703 idx_highlight = config->entry_count-1;
706 case KEYPRESS(0, 0, CHAR_LINEFEED):
707 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
711 case KEYPRESS(0, SCAN_F1, 0):
712 case KEYPRESS(0, 0, 'h'):
713 case KEYPRESS(0, 0, '?'):
714 status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
717 case KEYPRESS(0, 0, 'Q'):
722 case KEYPRESS(0, 0, 'd'):
723 if (config->idx_default_efivar != (INTN)idx_highlight) {
724 /* store the selected entry in a persistent EFI variable */
725 efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE);
726 config->idx_default_efivar = idx_highlight;
727 status = StrDuplicate(L"Default boot entry selected.");
729 /* clear the default entry EFI variable */
730 efivar_set(L"LoaderEntryDefault", NULL, TRUE);
731 config->idx_default_efivar = -1;
732 status = StrDuplicate(L"Default boot entry cleared.");
737 case KEYPRESS(0, 0, '-'):
738 case KEYPRESS(0, 0, 'T'):
739 if (config->timeout_sec_efivar > 0) {
740 config->timeout_sec_efivar--;
741 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
742 if (config->timeout_sec_efivar > 0)
743 status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
745 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
746 } else if (config->timeout_sec_efivar <= 0){
747 config->timeout_sec_efivar = -1;
748 efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
749 if (config->timeout_sec_config > 0)
750 status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
751 config->timeout_sec_config);
753 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
757 case KEYPRESS(0, 0, '+'):
758 case KEYPRESS(0, 0, 't'):
759 if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
760 config->timeout_sec_efivar++;
761 config->timeout_sec_efivar++;
762 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
763 if (config->timeout_sec_efivar > 0)
764 status = PoolPrint(L"Menu timeout set to %d sec.",
765 config->timeout_sec_efivar);
767 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
770 case KEYPRESS(0, 0, 'e'):
771 /* only the options of configured entries can be edited */
772 if (config->entries[idx_highlight]->type == LOADER_UNDEFINED)
774 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
775 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
776 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
777 if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
779 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
780 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
783 case KEYPRESS(0, 0, 'v'):
784 status = PoolPrint(L"systemd-boot " VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
785 ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
786 ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
789 case KEYPRESS(0, 0, 'P'):
790 print_status(config, loaded_image_path);
794 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
795 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
800 /* jump with a hotkey directly to a matching entry */
801 idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
808 if (idx_highlight > idx_last) {
809 idx_last = idx_highlight;
810 idx_first = 1 + idx_highlight - visible_max;
812 } else if (idx_highlight < idx_first) {
813 idx_first = idx_highlight;
814 idx_last = idx_highlight + visible_max-1;
818 if (!refresh && idx_highlight != idx_highlight_prev)
822 *chosen_entry = config->entries[idx_highlight];
824 for (i = 0; i < config->entry_count; i++)
829 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
830 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
834 static VOID config_add_entry(Config *config, ConfigEntry *entry) {
835 if ((config->entry_count & 15) == 0) {
838 i = config->entry_count + 16;
839 if (config->entry_count == 0)
840 config->entries = AllocatePool(sizeof(VOID *) * i);
842 config->entries = ReallocatePool(config->entries,
843 sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
845 config->entries[config->entry_count++] = entry;
848 static VOID config_entry_free(ConfigEntry *entry) {
849 FreePool(entry->title_show);
850 FreePool(entry->title);
851 FreePool(entry->machine_id);
852 FreePool(entry->loader);
853 FreePool(entry->options);
856 static BOOLEAN is_digit(CHAR16 c)
858 return (c >= '0') && (c <= '9');
861 static UINTN c_order(CHAR16 c)
867 else if ((c >= 'a') && (c <= 'z'))
873 static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2)
881 while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
884 order = c_order(*s1) - c_order(*s2);
897 while (is_digit(*s1) && is_digit(*s2)) {
913 return StrCmp(os1, os2);
916 static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
922 line = content + *pos;
927 while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
930 /* move pos to next line */
940 line[linelen] = '\0';
942 /* remove leading whitespace */
943 while (strchra((CHAR8 *)" \t", *line)) {
948 /* remove trailing whitespace */
949 while (linelen > 0 && strchra(sep, line[linelen-1]))
951 line[linelen] = '\0';
956 /* split key/value */
958 while (*value && !strchra(sep, *value))
964 while (*value && strchra(sep, *value))
968 if (value[0] == '\"' && line[linelen-1] == '\"') {
970 line[linelen-1] = '\0';
978 static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
984 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
985 if (strcmpa((CHAR8 *)"timeout", key) == 0) {
988 s = stra_to_str(value);
989 config->timeout_sec_config = Atoi(s);
990 config->timeout_sec = config->timeout_sec_config;
995 if (strcmpa((CHAR8 *)"default", key) == 0) {
996 FreePool(config->entry_default_pattern);
997 config->entry_default_pattern = stra_to_str(value);
998 StrLwr(config->entry_default_pattern);
1004 static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
1010 CHAR16 *initrd = NULL;
1012 entry = AllocateZeroPool(sizeof(ConfigEntry));
1015 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1016 if (strcmpa((CHAR8 *)"title", key) == 0) {
1017 FreePool(entry->title);
1018 entry->title = stra_to_str(value);
1022 if (strcmpa((CHAR8 *)"version", key) == 0) {
1023 FreePool(entry->version);
1024 entry->version = stra_to_str(value);
1028 if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
1029 FreePool(entry->machine_id);
1030 entry->machine_id = stra_to_str(value);
1034 if (strcmpa((CHAR8 *)"linux", key) == 0) {
1035 FreePool(entry->loader);
1036 entry->type = LOADER_LINUX;
1037 entry->loader = stra_to_path(value);
1042 if (strcmpa((CHAR8 *)"efi", key) == 0) {
1043 entry->type = LOADER_EFI;
1044 FreePool(entry->loader);
1045 entry->loader = stra_to_path(value);
1047 /* do not add an entry for ourselves */
1048 if (StriCmp(entry->loader, loaded_image_path) == 0) {
1049 entry->type = LOADER_UNDEFINED;
1055 if (strcmpa((CHAR8 *)"architecture", key) == 0) {
1056 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1057 if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) {
1058 entry->type = LOADER_UNDEFINED;
1064 if (strcmpa((CHAR8 *)"initrd", key) == 0) {
1067 new = stra_to_path(value);
1071 s = PoolPrint(L"%s initrd=%s", initrd, new);
1075 initrd = PoolPrint(L"initrd=%s", new);
1080 if (strcmpa((CHAR8 *)"options", key) == 0) {
1083 new = stra_to_str(value);
1084 if (entry->options) {
1087 s = PoolPrint(L"%s %s", entry->options, new);
1088 FreePool(entry->options);
1091 entry->options = new;
1099 if (entry->type == LOADER_UNDEFINED) {
1100 config_entry_free(entry);
1106 /* add initrd= to options */
1107 if (entry->type == LOADER_LINUX && initrd) {
1108 if (entry->options) {
1111 s = PoolPrint(L"%s %s", initrd, entry->options);
1112 FreePool(entry->options);
1115 entry->options = initrd;
1121 entry->device = device;
1122 entry->file = StrDuplicate(file);
1123 len = StrLen(entry->file);
1124 /* remove ".conf" */
1126 entry->file[len - 5] = '\0';
1127 StrLwr(entry->file);
1129 config_add_entry(config, entry);
1132 static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
1133 EFI_FILE_HANDLE entries_dir;
1135 CHAR8 *content = NULL;
1140 len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content);
1142 config_defaults_load_from_file(config, content);
1145 err = efivar_get_int(L"LoaderConfigTimeout", &sec);
1146 if (!EFI_ERROR(err)) {
1147 config->timeout_sec_efivar = sec;
1148 config->timeout_sec = sec;
1150 config->timeout_sec_efivar = -1;
1152 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
1153 if (!EFI_ERROR(err)) {
1158 CHAR8 *content = NULL;
1161 bufsize = sizeof(buf);
1162 err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
1163 if (bufsize == 0 || EFI_ERROR(err))
1166 f = (EFI_FILE_INFO *) buf;
1167 if (f->FileName[0] == '.')
1169 if (f->Attribute & EFI_FILE_DIRECTORY)
1172 len = StrLen(f->FileName);
1175 if (StriCmp(f->FileName + len - 5, L".conf") != 0)
1177 if (StrnCmp(f->FileName, L"auto-", 5) == 0)
1180 len = file_read(entries_dir, f->FileName, 0, 0, &content);
1182 config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path);
1185 uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
1188 /* sort entries after version number */
1189 for (i = 1; i < config->entry_count; i++) {
1194 for (k = 0; k < config->entry_count - i; k++) {
1197 if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0)
1199 entry = config->entries[k];
1200 config->entries[k] = config->entries[k+1];
1201 config->entries[k+1] = entry;
1209 static VOID config_default_entry_select(Config *config) {
1215 * The EFI variable to specify a boot entry for the next, and only the
1216 * next reboot. The variable is always cleared directly after it is read.
1218 err = efivar_get(L"LoaderEntryOneShot", &var);
1219 if (!EFI_ERROR(err)) {
1220 BOOLEAN found = FALSE;
1222 for (i = 0; i < config->entry_count; i++) {
1223 if (StrCmp(config->entries[i]->file, var) == 0) {
1224 config->idx_default = i;
1230 config->entry_oneshot = StrDuplicate(var);
1231 efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
1238 * The EFI variable to select the default boot entry overrides the
1239 * configured pattern. The variable can be set and cleared by pressing
1240 * the 'd' key in the loader selection menu, the entry is marked with
1243 err = efivar_get(L"LoaderEntryDefault", &var);
1244 if (!EFI_ERROR(err)) {
1245 BOOLEAN found = FALSE;
1247 for (i = 0; i < config->entry_count; i++) {
1248 if (StrCmp(config->entries[i]->file, var) == 0) {
1249 config->idx_default = i;
1250 config->idx_default_efivar = i;
1259 config->idx_default_efivar = -1;
1261 if (config->entry_count == 0)
1265 * Match the pattern from the end of the list to the start, find last
1266 * entry (largest number) matching the given pattern.
1268 if (config->entry_default_pattern) {
1269 i = config->entry_count;
1271 if (config->entries[i]->no_autoselect)
1273 if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) {
1274 config->idx_default = i;
1280 /* select the last suitable entry */
1281 i = config->entry_count;
1283 if (config->entries[i]->no_autoselect)
1285 config->idx_default = i;
1289 /* no entry found */
1290 config->idx_default = -1;
1293 /* generate a unique title, avoiding non-distinguishable menu entries */
1294 static VOID config_title_generate(Config *config) {
1299 for (i = 0; i < config->entry_count; i++) {
1302 FreePool(config->entries[i]->title_show);
1303 title = config->entries[i]->title;
1305 title = config->entries[i]->file;
1306 config->entries[i]->title_show = StrDuplicate(title);
1310 for (i = 0; i < config->entry_count; i++) {
1311 for (k = 0; k < config->entry_count; k++) {
1314 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1318 config->entries[i]->non_unique = TRUE;
1319 config->entries[k]->non_unique = TRUE;
1325 /* add version to non-unique titles */
1326 for (i = 0; i < config->entry_count; i++) {
1329 if (!config->entries[i]->non_unique)
1331 if (!config->entries[i]->version)
1334 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
1335 FreePool(config->entries[i]->title_show);
1336 config->entries[i]->title_show = s;
1337 config->entries[i]->non_unique = FALSE;
1341 for (i = 0; i < config->entry_count; i++) {
1342 for (k = 0; k < config->entry_count; k++) {
1345 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1349 config->entries[i]->non_unique = TRUE;
1350 config->entries[k]->non_unique = TRUE;
1356 /* add machine-id to non-unique titles */
1357 for (i = 0; i < config->entry_count; i++) {
1361 if (!config->entries[i]->non_unique)
1363 if (!config->entries[i]->machine_id)
1366 m = StrDuplicate(config->entries[i]->machine_id);
1368 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
1369 FreePool(config->entries[i]->title_show);
1370 config->entries[i]->title_show = s;
1371 config->entries[i]->non_unique = FALSE;
1376 for (i = 0; i < config->entry_count; i++) {
1377 for (k = 0; k < config->entry_count; k++) {
1380 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1384 config->entries[i]->non_unique = TRUE;
1385 config->entries[k]->non_unique = TRUE;
1391 /* add file name to non-unique titles */
1392 for (i = 0; i < config->entry_count; i++) {
1395 if (!config->entries[i]->non_unique)
1397 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file);
1398 FreePool(config->entries[i]->title_show);
1399 config->entries[i]->title_show = s;
1400 config->entries[i]->non_unique = FALSE;
1404 static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) {
1407 entry = AllocateZeroPool(sizeof(ConfigEntry));
1408 entry->title = StrDuplicate(title);
1410 entry->no_autoselect = TRUE;
1411 config_add_entry(config, entry);
1415 static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device,
1416 enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1419 entry = AllocateZeroPool(sizeof(ConfigEntry));
1421 entry->title = StrDuplicate(title);
1422 entry->device = device;
1423 entry->loader = StrDuplicate(loader);
1424 entry->file = StrDuplicate(file);
1425 StrLwr(entry->file);
1427 config_add_entry(config, entry);
1432 static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1433 CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1434 EFI_FILE_HANDLE handle;
1438 /* do not add an entry for ourselves */
1439 if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0)
1442 /* check existence */
1443 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
1446 uefi_call_wrapper(handle->Close, 1, handle);
1448 entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader);
1452 /* do not boot right away into auto-detected entries */
1453 entry->no_autoselect = TRUE;
1458 static VOID config_entry_add_osx(Config *config) {
1460 UINTN handle_count = 0;
1461 EFI_HANDLE *handles = NULL;
1463 err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
1464 if (!EFI_ERROR(err)) {
1467 for (i = 0; i < handle_count; i++) {
1471 root = LibOpenRoot(handles[i]);
1474 found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"OS X",
1475 L"\\System\\Library\\CoreServices\\boot.efi");
1476 uefi_call_wrapper(root->Close, 1, root);
1485 static VOID config_entry_add_linux( Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) {
1486 EFI_FILE_HANDLE linux_dir;
1489 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL);
1490 if (!EFI_ERROR(err)) {
1495 CHAR8 *sections[] = {
1499 UINTN offs[ELEMENTSOF(sections)-1] = {};
1500 UINTN szs[ELEMENTSOF(sections)-1] = {};
1501 UINTN addrs[ELEMENTSOF(sections)-1] = {};
1502 CHAR8 *content = NULL;
1507 CHAR16 *os_name = NULL;
1508 CHAR16 *os_id = NULL;
1509 CHAR16 *os_version = NULL;
1511 bufsize = sizeof(buf);
1512 err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf);
1513 if (bufsize == 0 || EFI_ERROR(err))
1516 f = (EFI_FILE_INFO *) buf;
1517 if (f->FileName[0] == '.')
1519 if (f->Attribute & EFI_FILE_DIRECTORY)
1521 len = StrLen(f->FileName);
1524 if (StriCmp(f->FileName + len - 4, L".efi") != 0)
1527 /* look for an .osrel section in the .efi binary */
1528 err = pefile_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs);
1532 len = file_read(linux_dir, f->FileName, offs[0], szs[0], &content);
1536 /* read properties from the embedded os-release file */
1538 while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
1539 if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) {
1540 os_name = stra_to_str(value);
1544 if (strcmpa((CHAR8 *)"ID", key) == 0) {
1545 os_id = stra_to_str(value);
1549 if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) {
1550 os_version = stra_to_str(value);
1555 if (os_name && os_id && os_version) {
1559 conf = PoolPrint(L"%s-%s", os_id, os_version);
1560 path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName);
1561 config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path);
1566 FreePool(os_version);
1571 uefi_call_wrapper(linux_dir->Close, 1, linux_dir);
1575 static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) {
1577 EFI_DEVICE_PATH *path;
1581 path = FileDevicePath(entry->device, entry->loader);
1583 Print(L"Error getting device path.");
1584 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1585 return EFI_INVALID_PARAMETER;
1588 err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
1589 if (EFI_ERROR(err)) {
1590 Print(L"Error loading %s: %r", entry->loader, err);
1591 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1595 if (config->options_edit)
1596 options = config->options_edit;
1597 else if (entry->options)
1598 options = entry->options;
1602 EFI_LOADED_IMAGE *loaded_image;
1604 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1605 parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1606 if (EFI_ERROR(err)) {
1607 Print(L"Error getting LoadedImageProtocol handle: %r", err);
1608 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1611 loaded_image->LoadOptions = options;
1612 loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
1615 efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
1616 err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
1618 uefi_call_wrapper(BS->UnloadImage, 1, image);
1624 static EFI_STATUS reboot_into_firmware(VOID) {
1630 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
1632 err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
1633 if (!EFI_ERROR(err))
1634 osind |= (UINT64)*b;
1637 err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
1641 err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
1642 Print(L"Error calling ResetSystem: %r", err);
1643 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1647 static VOID config_free(Config *config) {
1650 for (i = 0; i < config->entry_count; i++)
1651 config_entry_free(config->entries[i]);
1652 FreePool(config->entries);
1653 FreePool(config->entry_default_pattern);
1654 FreePool(config->options_edit);
1655 FreePool(config->entry_oneshot);
1658 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
1662 EFI_LOADED_IMAGE *loaded_image;
1664 CHAR16 *loaded_image_path;
1665 EFI_DEVICE_PATH *device_path;
1669 BOOLEAN menu = FALSE;
1671 InitializeLib(image, sys_table);
1672 init_usec = time_usec();
1673 efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
1674 efivar_set(L"LoaderInfo", L"systemd-boot " VERSION, FALSE);
1675 s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1676 efivar_set(L"LoaderFirmwareInfo", s, FALSE);
1678 s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
1679 efivar_set(L"LoaderFirmwareType", s, FALSE);
1682 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1683 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1684 if (EFI_ERROR(err)) {
1685 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
1686 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1690 /* export the device path this image is started from */
1691 device_path = DevicePathFromHandle(loaded_image->DeviceHandle);
1693 EFI_DEVICE_PATH *path, *paths;
1695 paths = UnpackDevicePath(device_path);
1696 for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
1697 HARDDRIVE_DEVICE_PATH *drive;
1700 if (DevicePathType(path) != MEDIA_DEVICE_PATH)
1702 if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP)
1704 drive = (HARDDRIVE_DEVICE_PATH *)path;
1705 if (drive->SignatureType != SIGNATURE_TYPE_GUID)
1708 GuidToString(uuid, (EFI_GUID *)&drive->Signature);
1709 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
1715 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
1717 Print(L"Unable to open root directory: %r ", err);
1718 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1719 return EFI_LOAD_ERROR;
1723 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1724 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
1725 efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
1727 /* scan "\loader\entries\*.conf" files */
1728 ZeroMem(&config, sizeof(Config));
1729 config_load(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
1731 /* if we find some well-known loaders, add them to the end of the list */
1732 config_entry_add_linux(&config, loaded_image, root_dir);
1733 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1734 L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1735 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1736 L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
1737 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1738 L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");
1739 config_entry_add_osx(&config);
1741 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
1742 UINT64 osind = (UINT64)*b;
1744 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
1745 config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
1749 if (config.entry_count == 0) {
1750 Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1751 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1755 config_title_generate(&config);
1757 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/
1758 config_default_entry_select(&config);
1760 /* if no configured entry to select from was found, enable the menu */
1761 if (config.idx_default == -1) {
1762 config.idx_default = 0;
1763 if (config.timeout_sec == 0)
1764 config.timeout_sec = 10;
1767 /* select entry or show menu when key is pressed or timeout is set */
1768 if (config.timeout_sec == 0) {
1771 err = console_key_read(&key, FALSE);
1772 if (!EFI_ERROR(err)) {
1775 /* find matching key in config entries */
1776 idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
1778 config.idx_default = idx;
1788 entry = config.entries[config.idx_default];
1790 efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
1791 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
1792 if (!menu_run(&config, &entry, loaded_image_path))
1795 /* run special entry like "reboot" */
1802 /* export the selected boot entry to the system */
1803 efivar_set(L"LoaderEntrySelected", entry->file, FALSE);
1805 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
1806 err = image_start(image, &config, entry);
1807 if (EFI_ERROR(err)) {
1808 graphics_mode(FALSE);
1809 Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err);
1810 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1815 config.timeout_sec = 0;
1819 FreePool(loaded_image_path);
1820 config_free(&config);
1821 uefi_call_wrapper(root_dir->Close, 1, root_dir);
1822 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);