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: sd-boot " VERSION " ####";
34 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
49 enum loader_type type;
54 EFI_STATUS (*call)(VOID);
55 BOOLEAN no_autoselect;
60 ConfigEntry **entries;
63 INTN idx_default_efivar;
65 UINTN timeout_sec_config;
66 INTN timeout_sec_efivar;
67 CHAR16 *entry_default_pattern;
69 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background;
70 CHAR16 *entry_oneshot;
75 static VOID cursor_left(UINTN *cursor, UINTN *first)
79 else if ((*first) > 0)
83 static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
85 if ((*cursor)+1 < x_max)
87 else if ((*first) + (*cursor) < len)
91 static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
104 size = StrLen(line_in) + 1024;
105 line = AllocatePool(size * sizeof(CHAR16));
106 StrCpy(line, line_in);
108 print = AllocatePool((x_max+1) * sizeof(CHAR16));
110 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
125 CopyMem(print, line + first, i * sizeof(CHAR16));
126 while (clear > 0 && i < x_max-1) {
132 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
133 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
134 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
136 err = console_key_read(&key, TRUE);
141 case KEYPRESS(0, SCAN_ESC, 0):
142 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'):
143 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'):
144 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')):
145 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')):
149 case KEYPRESS(0, SCAN_HOME, 0):
150 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'):
151 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')):
152 /* beginning-of-line */
157 case KEYPRESS(0, SCAN_END, 0):
158 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'):
159 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')):
161 cursor = len - first;
162 if (cursor+1 >= x_max) {
164 first = len - (x_max-1);
168 case KEYPRESS(0, SCAN_DOWN, 0):
169 case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'):
170 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0):
172 while (line[first + cursor] && line[first + cursor] == ' ')
173 cursor_right(&cursor, &first, x_max, len);
174 while (line[first + cursor] && line[first + cursor] != ' ')
175 cursor_right(&cursor, &first, x_max, len);
176 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
179 case KEYPRESS(0, SCAN_UP, 0):
180 case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'):
181 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0):
183 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
184 cursor_left(&cursor, &first);
185 while ((first + cursor) > 0 && line[first + cursor] == ' ')
186 cursor_left(&cursor, &first);
188 while ((first + cursor) > 0 && line[first + cursor-1] != ' ')
189 cursor_left(&cursor, &first);
190 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
193 case KEYPRESS(0, SCAN_RIGHT, 0):
194 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'):
195 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')):
197 if (first + cursor == len)
199 cursor_right(&cursor, &first, x_max, len);
200 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
203 case KEYPRESS(0, SCAN_LEFT, 0):
204 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'):
205 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')):
207 cursor_left(&cursor, &first);
208 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
211 case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
214 for (i = first + cursor; i < len && line[i] == ' '; i++)
216 for (; i < len && line[i] != ' '; i++)
219 for (i = first + cursor; i + clear < len; i++)
220 line[i] = line[i + clear];
225 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'):
226 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')):
227 case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE):
228 /* backward-kill-word */
230 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
231 cursor_left(&cursor, &first);
233 while ((first + cursor) > 0 && line[first + cursor] == ' ') {
234 cursor_left(&cursor, &first);
238 while ((first + cursor) > 0 && line[first + cursor-1] != ' ') {
239 cursor_left(&cursor, &first);
242 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
244 for (i = first + cursor; i + clear < len; i++)
245 line[i] = line[i + clear];
250 case KEYPRESS(0, SCAN_DELETE, 0):
251 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'):
252 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')):
255 if (first + cursor == len)
257 for (i = first + cursor; i < len; i++)
263 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'):
264 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')):
266 line[first + cursor] = '\0';
267 clear = len - (first + cursor);
268 len = first + cursor;
271 case KEYPRESS(0, 0, CHAR_LINEFEED):
272 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
273 if (StrCmp(line, line_in) != 0) {
281 case KEYPRESS(0, 0, CHAR_BACKSPACE):
284 if (first == 0 && cursor == 0)
286 for (i = first + cursor-1; i < len; i++)
292 if (cursor > 0 || first == 0)
294 /* show full line if it fits */
300 /* jump left to see what we delete */
310 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
311 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
314 for (i = len; i > first + cursor; i--)
316 line[first + cursor] = KEYCHAR(key);
319 if (cursor+1 < x_max)
321 else if (first + cursor < len)
327 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
333 static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
339 /* select entry by number key */
340 if (key >= '1' && key <= '9') {
342 if (i > config->entry_count)
343 i = config->entry_count;
347 /* find matching key in config entries */
348 for (i = start; i < config->entry_count; i++)
349 if (config->entries[i]->key == key)
352 for (i = 0; i < start; i++)
353 if (config->entries[i]->key == key)
359 static VOID print_status(Config *config, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
369 const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = config->background;
371 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
372 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
374 /* show splash and wait for key */
376 static const EFI_GRAPHICS_OUTPUT_BLT_PIXEL colors[] = {
377 { .Red = 0xff, .Green = 0xff, .Blue = 0xff },
378 { .Red = 0xc0, .Green = 0xc0, .Blue = 0xc0 },
379 { .Red = 0xff, .Green = 0, .Blue = 0 },
380 { .Red = 0, .Green = 0xff, .Blue = 0 },
381 { .Red = 0, .Green = 0, .Blue = 0xff },
382 { .Red = 0, .Green = 0, .Blue = 0 },
387 err = graphics_splash(root_dir, config->splash, pixel);
389 err = graphics_splash(root_dir, L"\\EFI\\systemd\\splash.bmp", pixel);
393 /* 'b' rotates through background colors */
394 console_key_read(&key, TRUE);
395 if (key == KEYPRESS(0, 0, 'b')) {
396 pixel = &colors[color++];
397 if (color == ELEMENTSOF(colors))
403 graphics_mode(FALSE);
404 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
408 Print(L"sd-boot version: " VERSION "\n");
409 Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n");
410 Print(L"loaded image: %s\n", loaded_image_path);
411 Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
412 Print(L"firmware vendor: %s\n", ST->FirmwareVendor);
413 Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
415 if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS)
416 Print(L"console size: %d x %d\n", x, y);
418 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
419 Print(L"SecureBoot: %s\n", *b > 0 ? L"enabled" : L"disabled");
423 if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) {
424 Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user");
428 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
429 Print(L"OsIndicationsSupported: %d\n", (UINT64)*b);
434 Print(L"timeout: %d\n", config->timeout_sec);
435 if (config->timeout_sec_efivar >= 0)
436 Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
437 Print(L"timeout (config): %d\n", config->timeout_sec_config);
438 if (config->entry_default_pattern)
439 Print(L"default pattern: '%s'\n", config->entry_default_pattern);
441 Print(L"splash '%s'\n", config->splash);
442 if (config->background)
443 Print(L"background '#%02x%02x%02x'\n",
444 config->background->Red,
445 config->background->Green,
446 config->background->Blue);
449 Print(L"config entry count: %d\n", config->entry_count);
450 Print(L"entry selected idx: %d\n", config->idx_default);
451 if (config->idx_default_efivar >= 0)
452 Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
455 if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
456 Print(L"LoaderConfigTimeout: %d\n", i);
457 if (config->entry_oneshot)
458 Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
459 if (efivar_get(L"LoaderDeviceIdentifier", &s) == EFI_SUCCESS) {
460 Print(L"LoaderDeviceIdentifier: %s\n", s);
463 if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) {
464 Print(L"LoaderDevicePartUUID: %s\n", s);
467 if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) {
468 Print(L"LoaderEntryDefault: %s\n", s);
472 Print(L"\n--- press key ---\n\n");
473 console_key_read(&key, TRUE);
475 for (i = 0; i < config->entry_count; i++) {
478 if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
481 entry = config->entries[i];
484 err = graphics_splash(root_dir, entry->splash, config->background);
485 if (!EFI_ERROR(err)) {
486 console_key_read(&key, TRUE);
487 graphics_mode(FALSE);
491 Print(L"config entry: %d/%d\n", i+1, config->entry_count);
493 Print(L"file '%s'\n", entry->file);
494 Print(L"title show '%s'\n", entry->title_show);
496 Print(L"title '%s'\n", entry->title);
498 Print(L"version '%s'\n", entry->version);
499 if (entry->machine_id)
500 Print(L"machine-id '%s'\n", entry->machine_id);
502 EFI_DEVICE_PATH *device_path;
505 device_path = DevicePathFromHandle(entry->device);
507 str = DevicePathToStr(device_path);
508 Print(L"device handle '%s'\n", str);
513 Print(L"loader '%s'\n", entry->loader);
515 Print(L"options '%s'\n", entry->options);
517 Print(L"splash '%s'\n", entry->splash);
518 Print(L"auto-select %s\n", entry->no_autoselect ? L"no" : L"yes");
520 Print(L"internal call yes\n");
522 Print(L"\n--- press key ---\n\n");
523 console_key_read(&key, TRUE);
526 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
529 static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
533 UINTN idx_highlight_prev;
549 BOOLEAN exit = FALSE;
551 BOOLEAN wait = FALSE;
553 graphics_mode(FALSE);
554 uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
555 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
556 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
558 /* draw a single character to make ClearScreen work on some firmware */
559 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" ");
560 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
562 err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
563 if (EFI_ERROR(err)) {
568 /* we check 10 times per second for a keystroke */
569 if (config->timeout_sec > 0)
570 timeout_remain = config->timeout_sec * 10;
574 idx_highlight = config->idx_default;
575 idx_highlight_prev = 0;
577 visible_max = y_max - 2;
579 if ((UINTN)config->idx_default >= visible_max)
580 idx_first = config->idx_default-1;
584 idx_last = idx_first + visible_max-1;
589 /* length of the longest entry */
591 for (i = 0; i < config->entry_count; i++) {
594 entry_len = StrLen(config->entries[i]->title_show);
595 if (line_width < entry_len)
596 line_width = entry_len;
598 if (line_width > x_max-6)
599 line_width = x_max-6;
601 /* offsets to center the entries on the screen */
602 x_start = (x_max - (line_width)) / 2;
603 if (config->entry_count < visible_max)
604 y_start = ((visible_max - config->entry_count) / 2) + 1;
608 /* menu entries title lines */
609 lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
610 for (i = 0; i < config->entry_count; i++) {
613 lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
614 for (j = 0; j < x_start; j++)
617 for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
618 lines[i][j] = config->entries[i]->title_show[k];
620 for (; j < x_max; j++)
622 lines[i][x_max] = '\0';
626 clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
627 for (i = 0; i < x_max; i++)
635 for (i = 0; i < config->entry_count; i++) {
636 if (i < idx_first || i > idx_last)
638 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
639 if (i == idx_highlight)
640 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
641 EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
643 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
644 EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
645 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
646 if ((INTN)i == config->idx_default_efivar) {
647 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
648 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
652 } else if (highlight) {
653 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
654 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
655 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
656 if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
657 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
658 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
661 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
662 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
663 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
664 if ((INTN)idx_highlight == config->idx_default_efivar) {
665 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
666 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
671 if (timeout_remain > 0) {
673 status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
676 /* print status at last line of screen */
682 len = StrLen(status);
684 x = (x_max - len) / 2;
687 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
688 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
689 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
690 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
691 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
694 err = console_key_read(&key, wait);
695 if (EFI_ERROR(err)) {
696 /* timeout reached */
697 if (timeout_remain == 0) {
702 /* sleep and update status */
703 if (timeout_remain > 0) {
704 uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
709 /* timeout disabled, wait for next key */
716 /* clear status after keystroke */
720 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
721 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
722 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
725 idx_highlight_prev = idx_highlight;
728 case KEYPRESS(0, SCAN_UP, 0):
729 case KEYPRESS(0, 0, 'k'):
730 if (idx_highlight > 0)
734 case KEYPRESS(0, SCAN_DOWN, 0):
735 case KEYPRESS(0, 0, 'j'):
736 if (idx_highlight < config->entry_count-1)
740 case KEYPRESS(0, SCAN_HOME, 0):
741 case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
742 if (idx_highlight > 0) {
748 case KEYPRESS(0, SCAN_END, 0):
749 case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
750 if (idx_highlight < config->entry_count-1) {
752 idx_highlight = config->entry_count-1;
756 case KEYPRESS(0, SCAN_PAGE_UP, 0):
757 if (idx_highlight > visible_max)
758 idx_highlight -= visible_max;
763 case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
764 idx_highlight += visible_max;
765 if (idx_highlight > config->entry_count-1)
766 idx_highlight = config->entry_count-1;
769 case KEYPRESS(0, 0, CHAR_LINEFEED):
770 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
774 case KEYPRESS(0, SCAN_F1, 0):
775 case KEYPRESS(0, 0, 'h'):
776 case KEYPRESS(0, 0, '?'):
777 status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
780 case KEYPRESS(0, 0, 'Q'):
785 case KEYPRESS(0, 0, 'd'):
786 if (config->idx_default_efivar != (INTN)idx_highlight) {
787 /* store the selected entry in a persistent EFI variable */
788 efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE);
789 config->idx_default_efivar = idx_highlight;
790 status = StrDuplicate(L"Default boot entry selected.");
792 /* clear the default entry EFI variable */
793 efivar_set(L"LoaderEntryDefault", NULL, TRUE);
794 config->idx_default_efivar = -1;
795 status = StrDuplicate(L"Default boot entry cleared.");
800 case KEYPRESS(0, 0, '-'):
801 case KEYPRESS(0, 0, 'T'):
802 if (config->timeout_sec_efivar > 0) {
803 config->timeout_sec_efivar--;
804 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
805 if (config->timeout_sec_efivar > 0)
806 status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
808 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
809 } else if (config->timeout_sec_efivar <= 0){
810 config->timeout_sec_efivar = -1;
811 efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
812 if (config->timeout_sec_config > 0)
813 status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
814 config->timeout_sec_config);
816 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
820 case KEYPRESS(0, 0, '+'):
821 case KEYPRESS(0, 0, 't'):
822 if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
823 config->timeout_sec_efivar++;
824 config->timeout_sec_efivar++;
825 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
826 if (config->timeout_sec_efivar > 0)
827 status = PoolPrint(L"Menu timeout set to %d sec.",
828 config->timeout_sec_efivar);
830 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
833 case KEYPRESS(0, 0, 'e'):
834 /* only the options of configured entries can be edited */
835 if (config->entries[idx_highlight]->type == LOADER_UNDEFINED)
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);
840 if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
842 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
843 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
846 case KEYPRESS(0, 0, 'v'):
847 status = PoolPrint(L"sd-boot " VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
848 ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
849 ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
852 case KEYPRESS(0, 0, 'P'):
853 print_status(config, root_dir, loaded_image_path);
857 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
858 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
863 /* jump with a hotkey directly to a matching entry */
864 idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
871 if (idx_highlight > idx_last) {
872 idx_last = idx_highlight;
873 idx_first = 1 + idx_highlight - visible_max;
876 if (idx_highlight < idx_first) {
877 idx_first = idx_highlight;
878 idx_last = idx_highlight + visible_max-1;
882 idx_last = idx_first + visible_max-1;
884 if (!refresh && idx_highlight != idx_highlight_prev)
888 *chosen_entry = config->entries[idx_highlight];
890 for (i = 0; i < config->entry_count; i++)
895 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
896 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
900 static VOID config_add_entry(Config *config, ConfigEntry *entry) {
901 if ((config->entry_count & 15) == 0) {
904 i = config->entry_count + 16;
905 if (config->entry_count == 0)
906 config->entries = AllocatePool(sizeof(VOID *) * i);
908 config->entries = ReallocatePool(config->entries,
909 sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
911 config->entries[config->entry_count++] = entry;
914 static VOID config_entry_free(ConfigEntry *entry) {
915 FreePool(entry->title_show);
916 FreePool(entry->title);
917 FreePool(entry->machine_id);
918 FreePool(entry->loader);
919 FreePool(entry->options);
922 static BOOLEAN is_digit(CHAR16 c)
924 return (c >= '0') && (c <= '9');
927 static UINTN c_order(CHAR16 c)
933 else if ((c >= 'a') && (c <= 'z'))
939 static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2)
947 while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
950 order = c_order(*s1) - c_order(*s2);
963 while (is_digit(*s1) && is_digit(*s2)) {
979 return StrCmp(os1, os2);
982 static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
988 line = content + *pos;
993 while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
996 /* move pos to next line */
1005 /* terminate line */
1006 line[linelen] = '\0';
1008 /* remove leading whitespace */
1009 while (strchra((CHAR8 *)" \t", *line)) {
1014 /* remove trailing whitespace */
1015 while (linelen > 0 && strchra(sep, line[linelen-1]))
1017 line[linelen] = '\0';
1022 /* split key/value */
1024 while (*value && !strchra(sep, *value))
1030 while (*value && strchra(sep, *value))
1034 if (value[0] == '\"' && line[linelen-1] == '\"') {
1036 line[linelen-1] = '\0';
1044 static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
1050 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1051 if (strcmpa((CHAR8 *)"timeout", key) == 0) {
1054 s = stra_to_str(value);
1055 config->timeout_sec_config = Atoi(s);
1056 config->timeout_sec = config->timeout_sec_config;
1061 if (strcmpa((CHAR8 *)"default", key) == 0) {
1062 FreePool(config->entry_default_pattern);
1063 config->entry_default_pattern = stra_to_str(value);
1064 StrLwr(config->entry_default_pattern);
1068 if (strcmpa((CHAR8 *)"splash", key) == 0) {
1069 FreePool(config->splash);
1070 config->splash = stra_to_path(value);
1074 if (strcmpa((CHAR8 *)"background", key) == 0) {
1077 /* accept #RRGGBB hex notation */
1078 if (value[0] != '#')
1080 if (value[7] != '\0')
1083 FreePool(config->background);
1084 config->background = AllocateZeroPool(sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
1085 if (!config->background)
1091 config->background->Red = xtoi(c);
1095 config->background->Green = xtoi(c);
1099 config->background->Blue = xtoi(c);
1105 static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
1111 CHAR16 *initrd = NULL;
1113 entry = AllocateZeroPool(sizeof(ConfigEntry));
1116 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1117 if (strcmpa((CHAR8 *)"title", key) == 0) {
1118 FreePool(entry->title);
1119 entry->title = stra_to_str(value);
1123 if (strcmpa((CHAR8 *)"version", key) == 0) {
1124 FreePool(entry->version);
1125 entry->version = stra_to_str(value);
1129 if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
1130 FreePool(entry->machine_id);
1131 entry->machine_id = stra_to_str(value);
1135 if (strcmpa((CHAR8 *)"linux", key) == 0) {
1136 FreePool(entry->loader);
1137 entry->type = LOADER_LINUX;
1138 entry->loader = stra_to_path(value);
1143 if (strcmpa((CHAR8 *)"efi", key) == 0) {
1144 entry->type = LOADER_EFI;
1145 FreePool(entry->loader);
1146 entry->loader = stra_to_path(value);
1148 /* do not add an entry for ourselves */
1149 if (StriCmp(entry->loader, loaded_image_path) == 0) {
1150 entry->type = LOADER_UNDEFINED;
1156 if (strcmpa((CHAR8 *)"architecture", key) == 0) {
1157 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1158 if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) {
1159 entry->type = LOADER_UNDEFINED;
1165 if (strcmpa((CHAR8 *)"initrd", key) == 0) {
1168 new = stra_to_path(value);
1172 s = PoolPrint(L"%s initrd=%s", initrd, new);
1176 initrd = PoolPrint(L"initrd=%s", new);
1181 if (strcmpa((CHAR8 *)"options", key) == 0) {
1184 new = stra_to_str(value);
1185 if (entry->options) {
1188 s = PoolPrint(L"%s %s", entry->options, new);
1189 FreePool(entry->options);
1192 entry->options = new;
1199 if (strcmpa((CHAR8 *)"splash", key) == 0) {
1200 FreePool(entry->splash);
1201 entry->splash = stra_to_path(value);
1206 if (entry->type == LOADER_UNDEFINED) {
1207 config_entry_free(entry);
1213 /* add initrd= to options */
1214 if (entry->type == LOADER_LINUX && initrd) {
1215 if (entry->options) {
1218 s = PoolPrint(L"%s %s", initrd, entry->options);
1219 FreePool(entry->options);
1222 entry->options = initrd;
1228 if (entry->machine_id) {
1231 /* append additional options from EFI variables for this machine-id */
1232 var = PoolPrint(L"LoaderEntryOptions-%s", entry->machine_id);
1236 if (efivar_get(var, &s) == EFI_SUCCESS) {
1237 if (entry->options) {
1240 s2 = PoolPrint(L"%s %s", entry->options, s);
1241 FreePool(entry->options);
1242 entry->options = s2;
1249 var = PoolPrint(L"LoaderEntryOptionsOneShot-%s", entry->machine_id);
1253 if (efivar_get(var, &s) == EFI_SUCCESS) {
1254 if (entry->options) {
1257 s2 = PoolPrint(L"%s %s", entry->options, s);
1258 FreePool(entry->options);
1259 entry->options = s2;
1262 efivar_set(var, NULL, TRUE);
1268 entry->device = device;
1269 entry->file = StrDuplicate(file);
1270 len = StrLen(entry->file);
1271 /* remove ".conf" */
1273 entry->file[len - 5] = '\0';
1274 StrLwr(entry->file);
1276 config_add_entry(config, entry);
1279 static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
1280 EFI_FILE_HANDLE entries_dir;
1282 CHAR8 *content = NULL;
1287 len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content);
1289 config_defaults_load_from_file(config, content);
1292 err = efivar_get_int(L"LoaderConfigTimeout", &sec);
1293 if (!EFI_ERROR(err)) {
1294 config->timeout_sec_efivar = sec;
1295 config->timeout_sec = sec;
1297 config->timeout_sec_efivar = -1;
1299 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
1300 if (!EFI_ERROR(err)) {
1305 CHAR8 *content = NULL;
1308 bufsize = sizeof(buf);
1309 err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
1310 if (bufsize == 0 || EFI_ERROR(err))
1313 f = (EFI_FILE_INFO *) buf;
1314 if (f->FileName[0] == '.')
1316 if (f->Attribute & EFI_FILE_DIRECTORY)
1318 len = StrLen(f->FileName);
1321 if (StriCmp(f->FileName + len - 5, L".conf") != 0)
1324 len = file_read(entries_dir, f->FileName, 0, 0, &content);
1326 config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path);
1329 uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
1332 /* sort entries after version number */
1333 for (i = 1; i < config->entry_count; i++) {
1338 for (k = 0; k < config->entry_count - i; k++) {
1341 if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0)
1343 entry = config->entries[k];
1344 config->entries[k] = config->entries[k+1];
1345 config->entries[k+1] = entry;
1353 static VOID config_default_entry_select(Config *config) {
1359 * The EFI variable to specify a boot entry for the next, and only the
1360 * next reboot. The variable is always cleared directly after it is read.
1362 err = efivar_get(L"LoaderEntryOneShot", &var);
1363 if (!EFI_ERROR(err)) {
1364 BOOLEAN found = FALSE;
1366 for (i = 0; i < config->entry_count; i++) {
1367 if (StrCmp(config->entries[i]->file, var) == 0) {
1368 config->idx_default = i;
1374 config->entry_oneshot = StrDuplicate(var);
1375 efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
1382 * The EFI variable to select the default boot entry overrides the
1383 * configured pattern. The variable can be set and cleared by pressing
1384 * the 'd' key in the loader selection menu, the entry is marked with
1387 err = efivar_get(L"LoaderEntryDefault", &var);
1388 if (!EFI_ERROR(err)) {
1389 BOOLEAN found = FALSE;
1391 for (i = 0; i < config->entry_count; i++) {
1392 if (StrCmp(config->entries[i]->file, var) == 0) {
1393 config->idx_default = i;
1394 config->idx_default_efivar = i;
1403 config->idx_default_efivar = -1;
1405 if (config->entry_count == 0)
1409 * Match the pattern from the end of the list to the start, find last
1410 * entry (largest number) matching the given pattern.
1412 if (config->entry_default_pattern) {
1413 i = config->entry_count;
1415 if (config->entries[i]->no_autoselect)
1417 if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) {
1418 config->idx_default = i;
1424 /* select the last suitable entry */
1425 i = config->entry_count;
1427 if (config->entries[i]->no_autoselect)
1429 config->idx_default = i;
1433 /* no entry found */
1434 config->idx_default = -1;
1437 /* generate a unique title, avoiding non-distinguishable menu entries */
1438 static VOID config_title_generate(Config *config) {
1443 for (i = 0; i < config->entry_count; i++) {
1446 FreePool(config->entries[i]->title_show);
1447 title = config->entries[i]->title;
1449 title = config->entries[i]->file;
1450 config->entries[i]->title_show = StrDuplicate(title);
1454 for (i = 0; i < config->entry_count; i++) {
1455 for (k = 0; k < config->entry_count; k++) {
1458 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1462 config->entries[i]->non_unique = TRUE;
1463 config->entries[k]->non_unique = TRUE;
1469 /* add version to non-unique titles */
1470 for (i = 0; i < config->entry_count; i++) {
1473 if (!config->entries[i]->non_unique)
1475 if (!config->entries[i]->version)
1478 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
1479 FreePool(config->entries[i]->title_show);
1480 config->entries[i]->title_show = s;
1481 config->entries[i]->non_unique = FALSE;
1485 for (i = 0; i < config->entry_count; i++) {
1486 for (k = 0; k < config->entry_count; k++) {
1489 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1493 config->entries[i]->non_unique = TRUE;
1494 config->entries[k]->non_unique = TRUE;
1500 /* add machine-id to non-unique titles */
1501 for (i = 0; i < config->entry_count; i++) {
1505 if (!config->entries[i]->non_unique)
1507 if (!config->entries[i]->machine_id)
1510 m = StrDuplicate(config->entries[i]->machine_id);
1512 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
1513 FreePool(config->entries[i]->title_show);
1514 config->entries[i]->title_show = s;
1515 config->entries[i]->non_unique = FALSE;
1520 for (i = 0; i < config->entry_count; i++) {
1521 for (k = 0; k < config->entry_count; k++) {
1524 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1528 config->entries[i]->non_unique = TRUE;
1529 config->entries[k]->non_unique = TRUE;
1535 /* add file name to non-unique titles */
1536 for (i = 0; i < config->entry_count; i++) {
1539 if (!config->entries[i]->non_unique)
1541 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file);
1542 FreePool(config->entries[i]->title_show);
1543 config->entries[i]->title_show = s;
1544 config->entries[i]->non_unique = FALSE;
1548 static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) {
1551 entry = AllocateZeroPool(sizeof(ConfigEntry));
1552 entry->title = StrDuplicate(title);
1554 entry->no_autoselect = TRUE;
1555 config_add_entry(config, entry);
1559 static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device,
1560 enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1563 entry = AllocateZeroPool(sizeof(ConfigEntry));
1565 entry->title = StrDuplicate(title);
1566 entry->device = device;
1567 entry->loader = StrDuplicate(loader);
1568 entry->file = StrDuplicate(file);
1569 StrLwr(entry->file);
1571 config_add_entry(config, entry);
1576 static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1577 CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1578 EFI_FILE_HANDLE handle;
1582 /* do not add an entry for ourselves */
1583 if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0)
1586 /* check existence */
1587 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
1590 uefi_call_wrapper(handle->Close, 1, handle);
1592 entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader);
1596 /* do not boot right away into auto-detected entries */
1597 entry->no_autoselect = TRUE;
1599 /* do not show a splash; they do not need one, or they draw their own */
1600 entry->splash = StrDuplicate(L"");
1602 /* export identifiers of automatically added entries */
1603 if (config->entries_auto) {
1606 s = PoolPrint(L"%s %s", config->entries_auto, file);
1607 FreePool(config->entries_auto);
1608 config->entries_auto = s;
1610 config->entries_auto = StrDuplicate(file);
1615 static VOID config_entry_add_osx(Config *config) {
1617 UINTN handle_count = 0;
1618 EFI_HANDLE *handles = NULL;
1620 err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
1621 if (!EFI_ERROR(err)) {
1624 for (i = 0; i < handle_count; i++) {
1628 root = LibOpenRoot(handles[i]);
1631 found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"OS X",
1632 L"\\System\\Library\\CoreServices\\boot.efi");
1633 uefi_call_wrapper(root->Close, 1, root);
1642 static VOID config_entry_add_linux( Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) {
1643 EFI_FILE_HANDLE linux_dir;
1646 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL);
1647 if (!EFI_ERROR(err)) {
1652 CHAR8 *sections[] = {
1656 UINTN offs[ELEMENTSOF(sections)-1] = {};
1657 UINTN szs[ELEMENTSOF(sections)-1] = {};
1658 UINTN addrs[ELEMENTSOF(sections)-1] = {};
1659 CHAR8 *content = NULL;
1664 CHAR16 *os_name = NULL;
1665 CHAR16 *os_id = NULL;
1666 CHAR16 *os_version = NULL;
1668 bufsize = sizeof(buf);
1669 err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf);
1670 if (bufsize == 0 || EFI_ERROR(err))
1673 f = (EFI_FILE_INFO *) buf;
1674 if (f->FileName[0] == '.')
1676 if (f->Attribute & EFI_FILE_DIRECTORY)
1678 len = StrLen(f->FileName);
1681 if (StriCmp(f->FileName + len - 4, L".efi") != 0)
1684 /* look for an .osrel section in the .efi binary */
1685 err = pefile_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs);
1689 len = file_read(linux_dir, f->FileName, offs[0], szs[0], &content);
1693 /* read properties from the embedded os-release file */
1695 while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
1696 if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) {
1697 os_name = stra_to_str(value);
1701 if (strcmpa((CHAR8 *)"ID", key) == 0) {
1702 os_id = stra_to_str(value);
1706 if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) {
1707 os_version = stra_to_str(value);
1712 if (os_name && os_id && os_version) {
1716 conf = PoolPrint(L"%s-%s", os_id, os_version);
1717 path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName);
1718 config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path);
1723 FreePool(os_version);
1728 uefi_call_wrapper(linux_dir->Close, 1, linux_dir);
1732 static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) {
1734 EFI_DEVICE_PATH *path;
1738 path = FileDevicePath(entry->device, entry->loader);
1740 Print(L"Error getting device path.");
1741 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1742 return EFI_INVALID_PARAMETER;
1745 err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
1746 if (EFI_ERROR(err)) {
1747 Print(L"Error loading %s: %r", entry->loader, err);
1748 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1752 if (config->options_edit)
1753 options = config->options_edit;
1754 else if (entry->options)
1755 options = entry->options;
1759 EFI_LOADED_IMAGE *loaded_image;
1761 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1762 parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1763 if (EFI_ERROR(err)) {
1764 Print(L"Error getting LoadedImageProtocol handle: %r", err);
1765 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1768 loaded_image->LoadOptions = options;
1769 loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
1772 efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
1773 err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
1775 uefi_call_wrapper(BS->UnloadImage, 1, image);
1781 static EFI_STATUS reboot_into_firmware(VOID) {
1787 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
1789 err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
1790 if (!EFI_ERROR(err))
1791 osind |= (UINT64)*b;
1794 err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
1798 err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
1799 Print(L"Error calling ResetSystem: %r", err);
1800 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1804 static VOID config_free(Config *config) {
1807 for (i = 0; i < config->entry_count; i++)
1808 config_entry_free(config->entries[i]);
1809 FreePool(config->entries);
1810 FreePool(config->entry_default_pattern);
1811 FreePool(config->options_edit);
1812 FreePool(config->entry_oneshot);
1813 FreePool(config->entries_auto);
1814 FreePool(config->splash);
1815 FreePool(config->background);
1818 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
1822 EFI_LOADED_IMAGE *loaded_image;
1824 CHAR16 *loaded_image_path;
1825 EFI_DEVICE_PATH *device_path;
1829 BOOLEAN menu = FALSE;
1831 InitializeLib(image, sys_table);
1832 init_usec = time_usec();
1833 efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
1834 efivar_set(L"LoaderInfo", L"sd-boot " VERSION, FALSE);
1835 s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1836 efivar_set(L"LoaderFirmwareInfo", s, FALSE);
1838 s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
1839 efivar_set(L"LoaderFirmwareType", s, FALSE);
1842 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1843 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1844 if (EFI_ERROR(err)) {
1845 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
1846 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1850 /* export the device path this image is started from */
1851 device_path = DevicePathFromHandle(loaded_image->DeviceHandle);
1854 EFI_DEVICE_PATH *path, *paths;
1856 str = DevicePathToStr(device_path);
1857 efivar_set(L"LoaderDeviceIdentifier", str, FALSE);
1860 paths = UnpackDevicePath(device_path);
1861 for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
1862 HARDDRIVE_DEVICE_PATH *drive;
1865 if (DevicePathType(path) != MEDIA_DEVICE_PATH)
1867 if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP)
1869 drive = (HARDDRIVE_DEVICE_PATH *)path;
1870 if (drive->SignatureType != SIGNATURE_TYPE_GUID)
1873 GuidToString(uuid, (EFI_GUID *)&drive->Signature);
1874 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
1880 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
1882 Print(L"Unable to open root directory: %r ", err);
1883 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1884 return EFI_LOAD_ERROR;
1888 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1889 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
1890 efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
1892 /* scan "\loader\entries\*.conf" files */
1893 ZeroMem(&config, sizeof(Config));
1894 config_load(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
1896 if (!config.background) {
1897 config.background = AllocateZeroPool(sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
1898 if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) {
1899 config.background->Red = 0xc0;
1900 config.background->Green = 0xc0;
1901 config.background->Blue = 0xc0;
1905 /* if we find some well-known loaders, add them to the end of the list */
1906 config_entry_add_linux(&config, loaded_image, root_dir);
1907 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1908 L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1909 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1910 L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
1911 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1912 L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");
1913 config_entry_add_osx(&config);
1914 efivar_set(L"LoaderEntriesAuto", config.entries_auto, FALSE);
1916 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
1917 UINT64 osind = (UINT64)*b;
1919 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
1920 config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
1924 if (config.entry_count == 0) {
1925 Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1926 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1930 config_title_generate(&config);
1932 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/
1933 config_default_entry_select(&config);
1935 /* if no configured entry to select from was found, enable the menu */
1936 if (config.idx_default == -1) {
1937 config.idx_default = 0;
1938 if (config.timeout_sec == 0)
1939 config.timeout_sec = 10;
1942 /* select entry or show menu when key is pressed or timeout is set */
1943 if (config.timeout_sec == 0) {
1946 err = console_key_read(&key, FALSE);
1947 if (!EFI_ERROR(err)) {
1950 /* find matching key in config entries */
1951 idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
1953 config.idx_default = idx;
1963 entry = config.entries[config.idx_default];
1965 efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
1966 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
1967 if (!menu_run(&config, &entry, root_dir, loaded_image_path))
1970 /* run special entry like "reboot" */
1976 err = EFI_NOT_FOUND;
1978 /* splash from entry file */
1979 if (entry->splash) {
1980 /* some entries disable the splash because they draw their own */
1981 if (entry->splash[0] == '\0')
1984 err = graphics_splash(root_dir, entry->splash, config.background);
1987 /* splash from config file */
1988 if (EFI_ERROR(err) && config.splash)
1989 err = graphics_splash(root_dir, config.splash, config.background);
1991 /* default splash */
1993 graphics_splash(root_dir, L"\\EFI\\systemd\\splash.bmp", config.background);
1996 /* export the selected boot entry to the system */
1997 efivar_set(L"LoaderEntrySelected", entry->file, FALSE);
1999 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
2000 err = image_start(image, &config, entry);
2002 if (err == EFI_ACCESS_DENIED || err == EFI_SECURITY_VIOLATION) {
2003 /* Platform is secure boot and requested image isn't
2004 * trusted. Need to go back to prior boot system and
2005 * install more keys or hashes. Signal failure by
2006 * returning the error */
2007 Print(L"\nImage %s gives a security error\n", entry->title);
2008 Print(L"Please enrol the hash or signature of %s\n", entry->loader);
2009 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
2014 config.timeout_sec = 0;
2018 FreePool(loaded_image_path);
2019 config_free(&config);
2020 uefi_call_wrapper(root_dir->Close, 1, root_dir);
2021 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);