2 * Simple UEFI boot loader which executes configured EFI images, where the
3 * default entry is selected by a configured pattern (glob) or an on-screen
6 * All gummiboot code is LGPL not GPL, to stay out of politics and to give
7 * the freedom of copying code from programs to possible future libraries.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
20 * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
22 * "Any intelligent fool can make things bigger, more complex, and more violent."
29 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
30 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
33 #ifndef EFI_SECURITY_VIOLATION
34 #define EFI_SECURITY_VIOLATION EFIERR(26)
37 /* magic string to find in the binary image */
38 static const char __attribute__((used)) magic[] = "#### LoaderInfo: gummiboot " VERSION " ####";
41 * Allocated random UUID, intended to be shared across tools that implement
42 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
43 * associated EFI variables.
45 static const EFI_GUID loader_guid = { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };
47 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
62 enum loader_type type;
65 EFI_STATUS (*call)(void);
66 BOOLEAN no_autoselect;
71 ConfigEntry **entries;
74 INTN idx_default_efivar;
76 UINTN timeout_sec_config;
77 INTN timeout_sec_efivar;
78 CHAR16 *entry_default_pattern;
79 CHAR16 *entry_oneshot;
84 static CHAR16 *stra_to_str(CHAR8 *stra);
87 static UINT64 ticks_read(void) {
89 __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
93 static void cpuid_read(UINT32 info, UINT32 *eax, UINT32 *ebx, UINT32 *ecx, UINT32 *edx) {
100 :"+a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
105 static UINT64 cpufreq_read(void) {
106 UINT32 eax, ebx, ecx, edx;
109 CHAR8 s[4 * 4 * 3 + 1];
120 for (i = 0; i < 3; i++) {
121 cpuid_read(0x80000002 + i, &eax, &ebx, &ecx, &edx);
127 brand.s[4 * 4 * 3] = '\0';
131 * “x.xxyHz” or “xxxxyHz”, where y=M,G,T
132 * from CPUID brand string:
133 * Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz
135 * http://www.intel.com/content/dam/www/public/us/en/documents/
136 * application-notes/processor-identification-cpuid-instruction-note.pdf
139 for (i = 4; i < (4 * 4 * 3) - 2; i++) {
140 if (brand.s[i+1] == 'H' && brand.s[i+2] == 'z') {
169 str = stra_to_str(s);
170 usec = Atoi(str) * scale;
175 static UINT64 time_usec(void) {
179 ticks = ticks_read();
183 cpufreq = cpufreq_read();
187 return 1000 * 1000 * ticks / cpufreq;
190 static UINT64 time_usec(void) { return 0; }
193 static EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size, BOOLEAN persistent) {
196 flags = EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
198 flags |= EFI_VARIABLE_NON_VOLATILE;
200 return uefi_call_wrapper(RT->SetVariable, 5, name, (EFI_GUID *)vendor, flags, size, buf);
203 static EFI_STATUS efivar_set(CHAR16 *name, CHAR16 *value, BOOLEAN persistent) {
204 return efivar_set_raw(&loader_guid, name, (CHAR8 *)value, value ? (StrLen(value)+1) * sizeof(CHAR16) : 0, persistent);
207 static EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 **buffer, UINTN *size) {
212 l = sizeof(CHAR16 *) * EFI_MAXIMUM_VARIABLE_SIZE;
213 buf = AllocatePool(l);
215 return EFI_OUT_OF_RESOURCES;
217 err = uefi_call_wrapper(RT->GetVariable, 5, name, (EFI_GUID *)vendor, NULL, &l, buf);
218 if (EFI_ERROR(err) == EFI_SUCCESS) {
228 static EFI_STATUS efivar_get(CHAR16 *name, CHAR16 **value) {
234 err = efivar_get_raw(&loader_guid, name, &buf, &size);
235 if (EFI_ERROR(err) != EFI_SUCCESS)
238 val = StrDuplicate((CHAR16 *)buf);
241 return EFI_OUT_OF_RESOURCES;
248 static EFI_STATUS efivar_set_int(CHAR16 *name, UINTN i, BOOLEAN persistent) {
251 SPrint(str, 32, L"%d", i);
252 return efivar_set(name, str, persistent);
255 static EFI_STATUS efivar_get_int(CHAR16 *name, UINTN *i) {
259 err = efivar_get(name, &val);
260 if (EFI_ERROR(err) == EFI_SUCCESS) {
267 static VOID efivar_set_time_usec(CHAR16 *name, UINT64 usec) {
275 SPrint(str, 32, L"%ld", usec);
276 efivar_set(name, str, FALSE);
279 /* remap EFI unicode and EFI scan code pair */
280 #define CHAR_CTRL(c) ((c) - 'a' + 1)
281 static void key_remap(EFI_INPUT_KEY *key, const EFI_INPUT_KEY *map) {
284 for (i = 0; ; i += 2) {
285 /* map unicode char to scan code*/
286 if (map[i].UnicodeChar > 0) {
287 if (key->UnicodeChar != map[i].UnicodeChar)
289 key->ScanCode = map[i+1].ScanCode ;
290 key->UnicodeChar = 0;
294 /* map scan code to unicode char */
295 if (map[i].ScanCode > 0) {
296 if (key->ScanCode != map[i].ScanCode)
299 key->UnicodeChar = map[i+1].UnicodeChar;
307 static void cursor_left(UINTN *cursor, UINTN *first)
311 else if ((*first) > 0)
315 static void cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
317 if ((*cursor)+1 < x_max)
319 else if ((*first) + (*cursor) < len)
323 static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
332 static const EFI_INPUT_KEY keymap[] = {
333 { 0, CHAR_CTRL('a') }, { SCAN_HOME, 0 },
334 { 0, CHAR_CTRL('e') }, { SCAN_END, 0 },
335 { 0, CHAR_CTRL('f') }, { SCAN_DOWN, 0 },
336 { 0, CHAR_CTRL('b') }, { SCAN_UP, 0 }
341 size = StrLen(line_in) + 1024;
342 line = AllocatePool(size * sizeof(CHAR16));
343 StrCpy(line, line_in);
345 print = AllocatePool((x_max+1) * sizeof(CHAR16));
347 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
362 CopyMem(print, line + first, i * sizeof(CHAR16));
366 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
367 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
368 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
370 uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index);
371 err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
375 key_remap(&key, keymap);
377 switch (key.ScanCode) {
387 if (cursor+1 >= x_max) {
389 first = len - (x_max-1);
393 while((first + cursor) && line[first + cursor] == ' ')
394 cursor_left(&cursor, &first);
395 while((first + cursor) && line[first + cursor] != ' ')
396 cursor_left(&cursor, &first);
397 while((first + cursor) && line[first + cursor] == ' ')
398 cursor_left(&cursor, &first);
399 if (first + cursor != len && first + cursor)
400 cursor_right(&cursor, &first, x_max, len);
401 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
404 while(line[first + cursor] && line[first + cursor] == ' ')
405 cursor_right(&cursor, &first, x_max, len);
406 while(line[first + cursor] && line[first + cursor] != ' ')
407 cursor_right(&cursor, &first, x_max, len);
408 while(line[first + cursor] && line[first + cursor] == ' ')
409 cursor_right(&cursor, &first, x_max, len);
410 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
413 if (first + cursor == len)
415 cursor_right(&cursor, &first, x_max, len);
416 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
419 cursor_left(&cursor, &first);
420 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
425 if (first + cursor == len)
427 for (i = first + cursor; i < len; i++)
434 switch (key.UnicodeChar) {
436 case CHAR_CARRIAGE_RETURN:
437 if (StrCmp(line, line_in) != 0) {
447 if (first == 0 && cursor == 0)
449 for (i = first + cursor-1; i < len; i++)
454 if (cursor > 0 || first == 0)
456 /* show full line if it fits */
462 /* jump left to see what we delete */
473 case 0x80 ... 0xffff:
476 for (i = len; i > first + cursor; i--)
478 line[first + cursor] = key.UnicodeChar;
481 if (cursor+1 < x_max)
483 else if (first + cursor < len)
489 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
495 static VOID dump_status(Config *config, CHAR16 *loaded_image_path) {
503 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
504 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
506 Print(L"gummiboot version: " VERSION "\n");
507 Print(L"loaded image: %s\n", loaded_image_path);
508 Print(L"UEFI version: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
509 Print(L"firmware vendor: %s\n", ST->FirmwareVendor);
510 Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
511 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
512 Print(L"SecureBoot: %s\n", *b > 0 ? L"enabled" : L"disabled");
516 if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) {
517 Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user");
521 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
522 Print(L"OsIndicationsSupported: %d\n", (UINT64)*b);
527 Print(L"timeout: %d\n", config->timeout_sec);
528 if (config->timeout_sec_efivar >= 0)
529 Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
530 Print(L"timeout (config): %d\n", config->timeout_sec_config);
531 if (config->entry_default_pattern)
532 Print(L"default pattern: '%s'\n", config->entry_default_pattern);
535 Print(L"config entry count: %d\n", config->entry_count);
536 Print(L"entry selected idx: %d\n", config->idx_default);
537 if (config->idx_default_efivar >= 0)
538 Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
541 if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
542 Print(L"LoaderConfigTimeout: %d\n", i);
543 if (config->entry_oneshot)
544 Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
545 if (efivar_get(L"LoaderDeviceIdentifier", &s) == EFI_SUCCESS) {
546 Print(L"LoaderDeviceIdentifier: %s\n", s);
549 if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) {
550 Print(L"LoaderDevicePartUUID: %s\n", s);
553 if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) {
554 Print(L"LoaderEntryDefault: %s\n", s);
558 Print(L"\n--- press key ---\n\n");
559 uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index);
560 uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
562 for (i = 0; i < config->entry_count; i++) {
565 entry = config->entries[i];
566 Print(L"config entry: %d/%d\n", i+1, config->entry_count);
568 Print(L"file '%s'\n", entry->file);
569 Print(L"title show '%s'\n", entry->title_show);
571 Print(L"title '%s'\n", entry->title);
573 Print(L"version '%s'\n", entry->version);
574 if (entry->machine_id)
575 Print(L"machine-id '%s'\n", entry->machine_id);
577 EFI_DEVICE_PATH *device_path;
580 device_path = DevicePathFromHandle(entry->device);
582 str = DevicePathToStr(device_path);
583 Print(L"device handle '%s'\n", str);
588 Print(L"loader '%s'\n", entry->loader);
590 Print(L"options '%s'\n", entry->options);
591 Print(L"auto-select %s\n", entry->no_autoselect ? L"no" : L"yes");
593 Print(L"internal call yes\n");
594 Print(L"\n--- press key ---\n\n");
595 uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index);
596 uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
597 if (key.ScanCode == SCAN_ESC)
601 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
604 static EFI_STATUS console_text_mode(VOID) {
605 #define EFI_CONSOLE_CONTROL_PROTOCOL_GUID \
606 { 0xf42f7782, 0x12e, 0x4c12, { 0x99, 0x56, 0x49, 0xf9, 0x43, 0x4, 0xf7, 0x21 }};
608 struct _EFI_CONSOLE_CONTROL_PROTOCOL;
611 EfiConsoleControlScreenText,
612 EfiConsoleControlScreenGraphics,
613 EfiConsoleControlScreenMaxValue,
614 } EFI_CONSOLE_CONTROL_SCREEN_MODE;
616 typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE)(
617 struct _EFI_CONSOLE_CONTROL_PROTOCOL *This,
618 EFI_CONSOLE_CONTROL_SCREEN_MODE *Mode,
623 typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE)(
624 struct _EFI_CONSOLE_CONTROL_PROTOCOL *This,
625 EFI_CONSOLE_CONTROL_SCREEN_MODE Mode
628 typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN)(
629 struct _EFI_CONSOLE_CONTROL_PROTOCOL *This,
633 typedef struct _EFI_CONSOLE_CONTROL_PROTOCOL {
634 EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE GetMode;
635 EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE SetMode;
636 EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN LockStdIn;
637 } EFI_CONSOLE_CONTROL_PROTOCOL;
639 EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
640 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
643 err = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **)&ConsoleControl);
646 return uefi_call_wrapper(ConsoleControl->SetMode, 2, ConsoleControl, EfiConsoleControlScreenText);
649 static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, CHAR16 *loaded_image_path) {
653 UINTN idx_highlight_prev;
668 BOOLEAN exit = FALSE;
670 static const EFI_INPUT_KEY keymap[] = {
671 { 0, 'j' }, { SCAN_DOWN, 0 },
672 { 0, 'k' }, { SCAN_UP, 0 },
673 { SCAN_F1, 0 }, { 0, 'h' },
678 uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
679 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
680 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
681 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
683 err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
684 if (EFI_ERROR(err)) {
689 /* we check 10 times per second for a keystroke */
690 if (config->timeout_sec > 0)
691 timeout_remain = config->timeout_sec * 10;
695 idx_highlight = config->idx_default;
696 idx_highlight_prev = 0;
698 visible_max = y_max - 2;
700 if ((UINTN)config->idx_default >= visible_max)
701 idx_first = config->idx_default-1;
705 idx_last = idx_first + visible_max-1;
710 /* length of the longest entry */
712 for (i = 0; i < config->entry_count; i++) {
715 entry_len = StrLen(config->entries[i]->title_show);
716 if (line_width < entry_len)
717 line_width = entry_len;
719 if (line_width > x_max-6)
720 line_width = x_max-6;
722 /* offsets to center the entries on the screen */
723 x_start = (x_max - (line_width)) / 2;
724 if (config->entry_count < visible_max)
725 y_start = ((visible_max - config->entry_count) / 2) + 1;
729 /* menu entries title lines */
730 lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
731 for (i = 0; i < config->entry_count; i++) {
734 lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
735 for (j = 0; j < x_start; j++)
738 for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
739 lines[i][j] = config->entries[i]->title_show[k];
741 for (; j < x_max; j++)
743 lines[i][x_max] = '\0';
747 clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
748 for (i = 0; i < x_max; i++)
756 for (i = 0; i < config->entry_count; i++) {
757 if (i < idx_first || i > idx_last)
759 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
760 if (i == idx_highlight)
761 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
762 EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
764 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
765 EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
766 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
767 if ((INTN)i == config->idx_default_efivar) {
768 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
769 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
773 } else if (highlight) {
774 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
775 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
776 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
777 if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
778 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
779 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
782 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
783 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
784 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
785 if ((INTN)idx_highlight == config->idx_default_efivar) {
786 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
787 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
792 if (timeout_remain > 0) {
794 status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
797 /* print status at last line of screen */
803 len = StrLen(status);
805 x = (x_max - len) / 2;
808 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
809 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
810 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
811 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
812 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
815 err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
816 if (err != EFI_SUCCESS) {
819 if (timeout_remain == 0) {
823 if (timeout_remain > 0) {
824 uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
828 uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index);
833 /* clear status after keystroke */
837 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
838 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
839 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
842 idx_highlight_prev = idx_highlight;
844 key_remap(&key, keymap);
846 switch (key.ScanCode) {
848 if (idx_highlight > 0)
852 if (idx_highlight < config->entry_count-1)
856 if (idx_highlight > 0) {
862 if (idx_highlight < config->entry_count-1) {
864 idx_highlight = config->entry_count-1;
868 if (idx_highlight > visible_max)
869 idx_highlight -= visible_max;
874 idx_highlight += visible_max;
875 if (idx_highlight > config->entry_count-1)
876 idx_highlight = config->entry_count-1;
880 if (idx_highlight > idx_last) {
881 idx_last = idx_highlight;
882 idx_first = 1 + idx_highlight - visible_max;
885 if (idx_highlight < idx_first) {
886 idx_first = idx_highlight;
887 idx_last = idx_highlight + visible_max-1;
890 idx_last = idx_first + visible_max-1;
892 switch (key.UnicodeChar) {
894 case CHAR_CARRIAGE_RETURN:
898 status = StrDuplicate(L"(d)efault, (+/-)timeout, (e)dit, (v)ersion (q)uit (*)dump (h)elp");
905 if (config->idx_default_efivar != (INTN)idx_highlight) {
906 /* store the selected entry in a persistent EFI variable */
907 efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE);
908 config->idx_default_efivar = idx_highlight;
909 status = StrDuplicate(L"Default boot entry selected.");
911 /* clear the default entry EFI variable */
912 efivar_set(L"LoaderEntryDefault", NULL, TRUE);
913 config->idx_default_efivar = -1;
914 status = StrDuplicate(L"Default boot entry cleared.");
919 if (config->timeout_sec_efivar > 0) {
920 config->timeout_sec_efivar--;
921 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
922 if (config->timeout_sec_efivar > 0)
923 status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
925 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
926 } else if (config->timeout_sec_efivar <= 0){
927 config->timeout_sec_efivar = -1;
928 efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
929 if (config->timeout_sec_config > 0)
930 status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
931 config->timeout_sec_config);
933 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
937 if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
938 config->timeout_sec_efivar++;
939 config->timeout_sec_efivar++;
940 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
941 if (config->timeout_sec_efivar > 0)
942 status = PoolPrint(L"Menu timeout set to %d sec.",
943 config->timeout_sec_efivar);
945 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
948 /* only the options of configured entries can be edited */
949 if (config->entries[idx_highlight]->type == LOADER_UNDEFINED)
951 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
952 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
953 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
954 if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
956 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
957 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
960 status = PoolPrint(L"gummiboot " VERSION ", UEFI %d.%02d, %s %d.%02d",
961 ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
962 ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
965 dump_status(config, loaded_image_path);
970 if (!refresh && idx_highlight != idx_highlight_prev)
974 *chosen_entry = config->entries[idx_highlight];
976 for (i = 0; i < config->entry_count; i++)
981 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
982 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
986 static VOID config_add_entry(Config *config, ConfigEntry *entry) {
987 if ((config->entry_count & 15) == 0) {
990 i = config->entry_count + 16;
991 if (config->entry_count == 0)
992 config->entries = AllocatePool(sizeof(VOID *) * i);
994 config->entries = ReallocatePool(config->entries,
995 sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
997 config->entries[config->entry_count++] = entry;
1000 static VOID config_entry_free(ConfigEntry *entry) {
1001 FreePool(entry->title_show);
1002 FreePool(entry->title);
1003 FreePool(entry->machine_id);
1004 FreePool(entry->loader);
1005 FreePool(entry->options);
1008 static BOOLEAN is_digit(CHAR16 c)
1010 return (c >= '0') && (c <= '9');
1013 static UINTN c_order(CHAR16 c)
1019 else if ((c >= 'a') && (c <= 'z'))
1025 static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2)
1030 while (*s1 || *s2) {
1033 while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
1036 order = c_order(*s1) - c_order(*s2);
1049 while (is_digit(*s1) && is_digit(*s2)) {
1065 return StrCmp(os1, os2);
1068 static INTN utf8_to_16(CHAR8 *stra, CHAR16 *c) {
1075 else if ((stra[0] & 0xe0) == 0xc0)
1077 else if ((stra[0] & 0xf0) == 0xe0)
1079 else if ((stra[0] & 0xf8) == 0xf0)
1081 else if ((stra[0] & 0xfc) == 0xf8)
1083 else if ((stra[0] & 0xfe) == 0xfc)
1093 unichar = stra[0] & 0x1f;
1096 unichar = stra[0] & 0x0f;
1099 unichar = stra[0] & 0x07;
1102 unichar = stra[0] & 0x03;
1105 unichar = stra[0] & 0x01;
1109 for (i = 1; i < len; i++) {
1110 if ((stra[i] & 0xc0) != 0x80)
1113 unichar |= stra[i] & 0x3f;
1120 static CHAR16 *stra_to_str(CHAR8 *stra) {
1126 len = strlena(stra);
1127 str = AllocatePool((len + 1) * sizeof(CHAR16));
1134 utf8len = utf8_to_16(stra + i, str + strlen);
1136 /* invalid utf8 sequence, skip the garbage */
1148 static CHAR16 *stra_to_path(CHAR8 *stra) {
1154 len = strlena(stra);
1155 str = AllocatePool((len + 2) * sizeof(CHAR16));
1163 utf8len = utf8_to_16(stra + i, str + strlen);
1165 /* invalid utf8 sequence, skip the garbage */
1170 if (str[strlen] == '/')
1172 if (str[strlen] == '\\' && str[strlen-1] == '\\') {
1173 /* skip double slashes */
1185 static CHAR8 *strchra(CHAR8 *s, CHAR8 c) {
1193 static CHAR8 *line_get_key_value(CHAR8 *content, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
1199 line = content + *pos;
1204 while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
1207 /* move pos to next line */
1216 /* terminate line */
1217 line[linelen] = '\0';
1219 /* remove leading whitespace */
1220 while (strchra((CHAR8 *)" \t", *line)) {
1225 /* remove trailing whitespace */
1226 while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1]))
1228 line[linelen] = '\0';
1233 /* split key/value */
1235 while (*value && !strchra((CHAR8 *)" \t", *value))
1241 while (*value && strchra((CHAR8 *)" \t", *value))
1249 static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
1255 while ((line = line_get_key_value(content, &pos, &key, &value))) {
1256 if (strcmpa((CHAR8 *)"timeout", key) == 0) {
1259 s = stra_to_str(value);
1260 config->timeout_sec_config = Atoi(s);
1261 config->timeout_sec = config->timeout_sec_config;
1265 if (strcmpa((CHAR8 *)"default", key) == 0) {
1266 config->entry_default_pattern = stra_to_str(value);
1267 StrLwr(config->entry_default_pattern);
1273 static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
1279 CHAR16 *initrd = NULL;
1281 entry = AllocateZeroPool(sizeof(ConfigEntry));
1284 while ((line = line_get_key_value(content, &pos, &key, &value))) {
1285 if (strcmpa((CHAR8 *)"title", key) == 0) {
1286 FreePool(entry->title);
1287 entry->title = stra_to_str(value);
1291 if (strcmpa((CHAR8 *)"version", key) == 0) {
1292 FreePool(entry->version);
1293 entry->version = stra_to_str(value);
1297 if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
1298 FreePool(entry->machine_id);
1299 entry->machine_id = stra_to_str(value);
1303 if (strcmpa((CHAR8 *)"linux", key) == 0) {
1304 FreePool(entry->loader);
1305 entry->type = LOADER_LINUX;
1306 entry->loader = stra_to_path(value);
1310 if (strcmpa((CHAR8 *)"efi", key) == 0) {
1311 entry->type = LOADER_EFI;
1312 FreePool(entry->loader);
1313 entry->loader = stra_to_path(value);
1315 /* do not add an entry for ourselves */
1316 if (StriCmp(entry->loader, loaded_image_path) == 0) {
1317 entry->type = LOADER_UNDEFINED;
1323 if (strcmpa((CHAR8 *)"initrd", key) == 0) {
1326 new = stra_to_path(value);
1330 s = PoolPrint(L"%s initrd=%s", initrd, new);
1334 initrd = PoolPrint(L"initrd=%s", new);
1339 if (strcmpa((CHAR8 *)"options", key) == 0) {
1342 new = stra_to_str(value);
1343 if (entry->options) {
1346 s = PoolPrint(L"%s %s", entry->options, new);
1347 FreePool(entry->options);
1350 entry->options = new;
1358 if (entry->type == LOADER_UNDEFINED) {
1359 config_entry_free(entry);
1365 /* add initrd= to options */
1366 if (entry->type == LOADER_LINUX && initrd) {
1367 if (entry->options) {
1370 s = PoolPrint(L"%s %s", initrd, entry->options);
1371 FreePool(entry->options);
1374 entry->options = initrd;
1380 if (entry->machine_id) {
1383 /* append additional options from EFI variables for this machine-id */
1384 var = PoolPrint(L"LoaderEntryOptions-%s", entry->machine_id);
1388 if (efivar_get(var, &s) == EFI_SUCCESS) {
1389 if (entry->options) {
1392 s2 = PoolPrint(L"%s %s", entry->options, s);
1393 FreePool(entry->options);
1394 entry->options = s2;
1401 var = PoolPrint(L"LoaderEntryOptionsOneShot-%s", entry->machine_id);
1405 if (efivar_get(var, &s) == EFI_SUCCESS) {
1406 if (entry->options) {
1409 s2 = PoolPrint(L"%s %s", entry->options, s);
1410 FreePool(entry->options);
1411 entry->options = s2;
1414 efivar_set(var, NULL, TRUE);
1420 entry->device = device;
1421 entry->file = StrDuplicate(file);
1422 len = StrLen(entry->file);
1423 /* remove ".conf" */
1425 entry->file[len - 5] = '\0';
1426 StrLwr(entry->file);
1428 config_add_entry(config, entry);
1431 static UINTN file_read(EFI_FILE_HANDLE dir, CHAR16 *name, CHAR8 **content) {
1432 EFI_FILE_HANDLE handle;
1433 EFI_FILE_INFO *info;
1439 err = uefi_call_wrapper(dir->Open, 5, dir, &handle, name, EFI_FILE_MODE_READ, 0ULL);
1443 info = LibFileInfo(handle);
1444 buflen = info->FileSize+1;
1445 buf = AllocatePool(buflen);
1447 err = uefi_call_wrapper(handle->Read, 3, handle, &buflen, buf);
1448 if (EFI_ERROR(err) == EFI_SUCCESS) {
1456 uefi_call_wrapper(handle->Close, 1, handle);
1461 static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
1462 EFI_FILE_HANDLE entries_dir;
1464 CHAR8 *content = NULL;
1469 len = file_read(root_dir, L"\\loader\\loader.conf", &content);
1471 config_defaults_load_from_file(config, content);
1474 err = efivar_get_int(L"LoaderConfigTimeout", &sec);
1475 if (EFI_ERROR(err) == EFI_SUCCESS) {
1476 config->timeout_sec_efivar = sec;
1477 config->timeout_sec = sec;
1479 config->timeout_sec_efivar = -1;
1481 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
1482 if (EFI_ERROR(err) == EFI_SUCCESS) {
1487 CHAR8 *content = NULL;
1490 bufsize = sizeof(buf);
1491 err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
1492 if (bufsize == 0 || EFI_ERROR(err))
1495 f = (EFI_FILE_INFO *) buf;
1496 if (f->FileName[0] == '.')
1498 if (f->Attribute & EFI_FILE_DIRECTORY)
1500 len = StrLen(f->FileName);
1503 if (StriCmp(f->FileName + len - 5, L".conf") != 0)
1506 len = file_read(entries_dir, f->FileName, &content);
1508 config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path);
1511 uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
1514 /* sort entries after version number */
1515 for (i = 1; i < config->entry_count; i++) {
1520 for (k = 0; k < config->entry_count - i; k++) {
1523 if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0)
1525 entry = config->entries[k];
1526 config->entries[k] = config->entries[k+1];
1527 config->entries[k+1] = entry;
1535 static VOID config_default_entry_select(Config *config) {
1541 * The EFI variable to specify a boot entry for the next, and only the
1542 * next reboot. The variable is always cleared directly after it is read.
1544 err = efivar_get(L"LoaderEntryOneShot", &var);
1545 if (EFI_ERROR(err) == EFI_SUCCESS) {
1546 BOOLEAN found = FALSE;
1548 for (i = 0; i < config->entry_count; i++) {
1549 if (StrCmp(config->entries[i]->file, var) == 0) {
1550 config->idx_default = i;
1556 config->entry_oneshot = StrDuplicate(var);
1557 efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
1564 * The EFI variable to select the default boot entry overrides the
1565 * configured pattern. The variable can be set and cleared by pressing
1566 * the 'd' key in the loader selection menu, the entry is marked with
1569 err = efivar_get(L"LoaderEntryDefault", &var);
1570 if (EFI_ERROR(err) == EFI_SUCCESS) {
1571 BOOLEAN found = FALSE;
1573 for (i = 0; i < config->entry_count; i++) {
1574 if (StrCmp(config->entries[i]->file, var) == 0) {
1575 config->idx_default = i;
1576 config->idx_default_efivar = i;
1585 config->idx_default_efivar = -1;
1587 if (config->entry_count == 0)
1591 * Match the pattern from the end of the list to the start, find last
1592 * entry (largest number) matching the given pattern.
1594 if (config->entry_default_pattern) {
1595 i = config->entry_count;
1597 if (config->entries[i]->no_autoselect)
1599 if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) {
1600 config->idx_default = i;
1606 /* select the last suitable entry */
1607 i = config->entry_count;
1609 if (config->entries[i]->no_autoselect)
1611 config->idx_default = i;
1615 /* no entry found */
1616 config->idx_default = -1;
1619 /* generate a unique title, avoiding non-distinguishable menu entries */
1620 static VOID config_title_generate(Config *config) {
1625 for (i = 0; i < config->entry_count; i++) {
1628 FreePool(config->entries[i]->title_show);
1629 title = config->entries[i]->title;
1631 title = config->entries[i]->file;
1632 config->entries[i]->title_show = StrDuplicate(title);
1636 for (i = 0; i < config->entry_count; i++) {
1637 for (k = 0; k < config->entry_count; k++) {
1640 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1644 config->entries[i]->non_unique = TRUE;
1645 config->entries[k]->non_unique = TRUE;
1651 /* add version to non-unique titles */
1652 for (i = 0; i < config->entry_count; i++) {
1655 if (!config->entries[i]->non_unique)
1657 if (!config->entries[i]->version)
1660 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
1661 FreePool(config->entries[i]->title_show);
1662 config->entries[i]->title_show = s;
1663 config->entries[i]->non_unique = FALSE;
1667 for (i = 0; i < config->entry_count; i++) {
1668 for (k = 0; k < config->entry_count; k++) {
1671 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1675 config->entries[i]->non_unique = TRUE;
1676 config->entries[k]->non_unique = TRUE;
1682 /* add machine-id to non-unique titles */
1683 for (i = 0; i < config->entry_count; i++) {
1687 if (!config->entries[i]->non_unique)
1689 if (!config->entries[i]->machine_id)
1692 m = StrDuplicate(config->entries[i]->machine_id);
1694 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
1695 FreePool(config->entries[i]->title_show);
1696 config->entries[i]->title_show = s;
1697 config->entries[i]->non_unique = FALSE;
1702 for (i = 0; i < config->entry_count; i++) {
1703 for (k = 0; k < config->entry_count; k++) {
1706 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1710 config->entries[i]->non_unique = TRUE;
1711 config->entries[k]->non_unique = TRUE;
1717 /* add file name to non-unique titles */
1718 for (i = 0; i < config->entry_count; i++) {
1721 if (!config->entries[i]->non_unique)
1723 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file);
1724 FreePool(config->entries[i]->title_show);
1725 config->entries[i]->title_show = s;
1726 config->entries[i]->non_unique = FALSE;
1730 static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(void)) {
1733 entry = AllocateZeroPool(sizeof(ConfigEntry));
1734 entry->title = StrDuplicate(title);
1736 entry->no_autoselect = TRUE;
1737 config_add_entry(config, entry);
1741 static BOOLEAN config_entry_add_loader(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1742 CHAR16 *file, CHAR16 *title, CHAR16 *loader) {
1743 EFI_FILE_HANDLE handle;
1747 /* do not add an entry for ourselves */
1748 if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0)
1751 /* check existence */
1752 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
1755 uefi_call_wrapper(handle->Close, 1, handle);
1757 entry = AllocateZeroPool(sizeof(ConfigEntry));
1758 entry->title = StrDuplicate(title);
1759 entry->device = device;
1760 entry->loader = StrDuplicate(loader);
1761 entry->file = StrDuplicate(file);
1762 StrLwr(entry->file);
1763 entry->no_autoselect = TRUE;
1764 config_add_entry(config, entry);
1768 static VOID config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1769 CHAR16 *file, CHAR16 *title, CHAR16 *loader) {
1770 if (!config_entry_add_loader(config, device, root_dir, loaded_image_path, file, title, loader))
1773 /* export identifiers of automatically added entries */
1774 if (config->entries_auto) {
1777 s = PoolPrint(L"%s %s", config->entries_auto, file);
1778 FreePool(config->entries_auto);
1779 config->entries_auto = s;
1781 config->entries_auto = StrDuplicate(file);
1784 static VOID config_entry_add_osx(Config *config) {
1786 UINTN handle_count = 0;
1787 EFI_HANDLE *handles = NULL;
1789 err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
1790 if (EFI_ERROR(err) == EFI_SUCCESS) {
1793 for (i = 0; i < handle_count; i++) {
1796 root = LibOpenRoot(handles[i]);
1799 config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", L"OS X",
1800 L"\\System\\Library\\CoreServices\\boot.efi");
1801 uefi_call_wrapper(root->Close, 1, root);
1808 static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) {
1811 EFI_DEVICE_PATH *path;
1814 path = FileDevicePath(entry->device, entry->loader);
1816 Print(L"Error getting device path.");
1817 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1818 return EFI_INVALID_PARAMETER;
1821 err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
1822 if (EFI_ERROR(err)) {
1823 Print(L"Error loading %s: %r", entry->loader, err);
1824 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1828 if (config->options_edit)
1829 options = config->options_edit;
1830 else if (entry->options)
1831 options = entry->options;
1835 EFI_LOADED_IMAGE *loaded_image;
1837 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (void **)&loaded_image,
1838 parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1839 if (EFI_ERROR(err)) {
1840 Print(L"Error getting LoadedImageProtocol handle: %r", err);
1841 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1844 loaded_image->LoadOptions = options;
1845 loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
1848 efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
1849 err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
1851 uefi_call_wrapper(BS->UnloadImage, 1, image);
1857 static EFI_STATUS reboot_into_firmware(VOID) {
1863 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
1865 err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
1866 if (err == EFI_SUCCESS)
1867 osind |= (UINT64)*b;
1870 err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
1871 if (err != EFI_SUCCESS)
1874 err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
1875 Print(L"Error calling ResetSystem: %r", err);
1876 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1880 static VOID config_free(Config *config) {
1883 for (i = 0; i < config->entry_count; i++)
1884 config_entry_free(config->entries[i]);
1885 FreePool(config->entries);
1886 FreePool(config->entry_default_pattern);
1887 FreePool(config->options_edit);
1888 FreePool(config->entry_oneshot);
1889 FreePool(config->entries_auto);
1892 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
1896 EFI_LOADED_IMAGE *loaded_image;
1898 CHAR16 *loaded_image_path;
1899 EFI_DEVICE_PATH *device_path;
1903 BOOLEAN menu = FALSE;
1905 InitializeLib(image, sys_table);
1906 init_usec = time_usec();
1907 efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
1908 efivar_set(L"LoaderInfo", L"gummiboot " VERSION, FALSE);
1909 s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1910 efivar_set(L"LoaderFirmwareInfo", s, FALSE);
1912 s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
1913 efivar_set(L"LoaderFirmwareType", s, FALSE);
1916 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (void **)&loaded_image,
1917 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1918 if (EFI_ERROR(err)) {
1919 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
1920 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1924 /* export the device path this image is started from */
1925 device_path = DevicePathFromHandle(loaded_image->DeviceHandle);
1928 EFI_DEVICE_PATH *path, *paths;
1930 str = DevicePathToStr(device_path);
1931 efivar_set(L"LoaderDeviceIdentifier", str, FALSE);
1934 paths = UnpackDevicePath(device_path);
1935 for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
1936 HARDDRIVE_DEVICE_PATH *drive;
1939 if (DevicePathType(path) != MEDIA_DEVICE_PATH)
1941 if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP)
1943 drive = (HARDDRIVE_DEVICE_PATH *)path;
1944 if (drive->SignatureType != SIGNATURE_TYPE_GUID)
1947 GuidToString(uuid, (EFI_GUID *)&drive->Signature);
1948 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
1954 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
1956 Print(L"Unable to open root directory: %r ", err);
1957 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1958 return EFI_LOAD_ERROR;
1961 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1962 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
1963 efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
1965 /* scan "\loader\entries\*.conf" files */
1966 ZeroMem(&config, sizeof(Config));
1967 config_load(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
1969 /* if we find some well-known loaders, add them to the end of the list */
1970 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1971 L"auto-windows", L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1972 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1973 L"auto-efi-shell", L"EFI Shell", L"\\shellx64.efi");
1974 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1975 L"auto-efi-default", L"EFI Default Loader", L"\\EFI\\BOOT\\BOOTX64.EFI");
1976 config_entry_add_osx(&config);
1977 efivar_set(L"LoaderEntriesAuto", config.entries_auto, FALSE);
1979 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
1980 UINT64 osind = (UINT64)*b;
1982 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
1983 config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
1987 if (config.entry_count == 0) {
1988 Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1989 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1993 config_title_generate(&config);
1995 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/
1996 config_default_entry_select(&config);
1998 /* if no configured entry to select from was found, enable the menu */
1999 if (config.idx_default == -1) {
2000 config.idx_default = 0;
2001 if (config.timeout_sec == 0)
2002 config.timeout_sec = 10;
2005 /* show menu when key is pressed or timeout is set */
2006 if (config.timeout_sec == 0) {
2009 err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
2010 if (err == EFI_SUCCESS)
2018 entry = config.entries[config.idx_default];
2020 efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
2021 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
2022 if (!menu_run(&config, &entry, loaded_image_path))
2025 /* run special entry like "reboot" */
2032 /* export the selected boot entry to the system */
2033 efivar_set(L"LoaderEntrySelected", entry->file, FALSE);
2035 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
2036 err = image_start(image, &config, entry);
2038 if (err == EFI_ACCESS_DENIED || err == EFI_SECURITY_VIOLATION) {
2039 /* Platform is secure boot and requested image isn't
2040 * trusted. Need to go back to prior boot system and
2041 * install more keys or hashes. Signal failure by
2042 * returning the error */
2043 Print(L"\nImage %s gives a security error\n", entry->title);
2044 Print(L"Please enrol the hash or signature of %s\n", entry->loader);
2045 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
2050 config.timeout_sec = 0;
2054 FreePool(loaded_image_path);
2055 config_free(&config);
2056 uefi_call_wrapper(root_dir->Close, 1, root_dir);
2057 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);