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>
28 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
29 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
32 /* magic string to find in the binary image */
33 static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " VERSION " ####";
35 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
50 enum loader_type type;
55 EFI_STATUS (*call)(VOID);
56 BOOLEAN no_autoselect;
61 ConfigEntry **entries;
64 INTN idx_default_efivar;
66 UINTN timeout_sec_config;
67 INTN timeout_sec_efivar;
68 CHAR16 *entry_default_pattern;
69 CHAR16 *entry_oneshot;
74 static VOID cursor_left(UINTN *cursor, UINTN *first)
78 else if ((*first) > 0)
82 static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
84 if ((*cursor)+1 < x_max)
86 else if ((*first) + (*cursor) < len)
90 static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
103 size = StrLen(line_in) + 1024;
104 line = AllocatePool(size * sizeof(CHAR16));
105 StrCpy(line, line_in);
107 print = AllocatePool((x_max+1) * sizeof(CHAR16));
109 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
124 CopyMem(print, line + first, i * sizeof(CHAR16));
125 while (clear > 0 && i < x_max-1) {
131 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
132 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
133 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
135 err = console_key_read(&key, TRUE);
140 case KEYPRESS(0, SCAN_ESC, 0):
141 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'):
142 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'):
143 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')):
144 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')):
148 case KEYPRESS(0, SCAN_HOME, 0):
149 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'):
150 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')):
151 /* beginning-of-line */
156 case KEYPRESS(0, SCAN_END, 0):
157 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'):
158 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')):
160 cursor = len - first;
161 if (cursor+1 >= x_max) {
163 first = len - (x_max-1);
167 case KEYPRESS(0, SCAN_DOWN, 0):
168 case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'):
169 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0):
171 while (line[first + cursor] && line[first + cursor] == ' ')
172 cursor_right(&cursor, &first, x_max, len);
173 while (line[first + cursor] && line[first + cursor] != ' ')
174 cursor_right(&cursor, &first, x_max, len);
175 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
178 case KEYPRESS(0, SCAN_UP, 0):
179 case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'):
180 case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0):
182 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
183 cursor_left(&cursor, &first);
184 while ((first + cursor) > 0 && line[first + cursor] == ' ')
185 cursor_left(&cursor, &first);
187 while ((first + cursor) > 0 && line[first + cursor-1] != ' ')
188 cursor_left(&cursor, &first);
189 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
192 case KEYPRESS(0, SCAN_RIGHT, 0):
193 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'):
194 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')):
196 if (first + cursor == len)
198 cursor_right(&cursor, &first, x_max, len);
199 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
202 case KEYPRESS(0, SCAN_LEFT, 0):
203 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'):
204 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')):
206 cursor_left(&cursor, &first);
207 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
210 case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
213 for (i = first + cursor; i < len && line[i] == ' '; i++)
215 for (; i < len && line[i] != ' '; i++)
218 for (i = first + cursor; i + clear < len; i++)
219 line[i] = line[i + clear];
224 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'):
225 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')):
226 case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE):
227 /* backward-kill-word */
229 if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
230 cursor_left(&cursor, &first);
232 while ((first + cursor) > 0 && line[first + cursor] == ' ') {
233 cursor_left(&cursor, &first);
237 while ((first + cursor) > 0 && line[first + cursor-1] != ' ') {
238 cursor_left(&cursor, &first);
241 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
243 for (i = first + cursor; i + clear < len; i++)
244 line[i] = line[i + clear];
249 case KEYPRESS(0, SCAN_DELETE, 0):
250 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'):
251 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')):
254 if (first + cursor == len)
256 for (i = first + cursor; i < len; i++)
262 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'):
263 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')):
265 line[first + cursor] = '\0';
266 clear = len - (first + cursor);
267 len = first + cursor;
270 case KEYPRESS(0, 0, CHAR_LINEFEED):
271 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
272 if (StrCmp(line, line_in) != 0) {
280 case KEYPRESS(0, 0, CHAR_BACKSPACE):
283 if (first == 0 && cursor == 0)
285 for (i = first + cursor-1; i < len; i++)
291 if (cursor > 0 || first == 0)
293 /* show full line if it fits */
299 /* jump left to see what we delete */
309 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
310 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
313 for (i = len; i > first + cursor; i--)
315 line[first + cursor] = KEYCHAR(key);
318 if (cursor+1 < x_max)
320 else if (first + cursor < len)
326 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
332 static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
338 /* select entry by number key */
339 if (key >= '1' && key <= '9') {
341 if (i > config->entry_count)
342 i = config->entry_count;
346 /* find matching key in config entries */
347 for (i = start; i < config->entry_count; i++)
348 if (config->entries[i]->key == key)
351 for (i = 0; i < start; i++)
352 if (config->entries[i]->key == key)
358 static VOID print_status(Config *config, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
368 const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = NULL;
370 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
371 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
373 Print(L"systemd-boot version: " VERSION "\n");
374 Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n");
375 Print(L"loaded image: %s\n", loaded_image_path);
376 Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
377 Print(L"firmware vendor: %s\n", ST->FirmwareVendor);
378 Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
380 if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS)
381 Print(L"console size: %d x %d\n", x, y);
383 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
384 Print(L"SecureBoot: %s\n", *b > 0 ? L"enabled" : L"disabled");
388 if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) {
389 Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user");
393 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
394 Print(L"OsIndicationsSupported: %d\n", (UINT64)*b);
399 Print(L"timeout: %d\n", config->timeout_sec);
400 if (config->timeout_sec_efivar >= 0)
401 Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
402 Print(L"timeout (config): %d\n", config->timeout_sec_config);
403 if (config->entry_default_pattern)
404 Print(L"default pattern: '%s'\n", config->entry_default_pattern);
407 Print(L"config entry count: %d\n", config->entry_count);
408 Print(L"entry selected idx: %d\n", config->idx_default);
409 if (config->idx_default_efivar >= 0)
410 Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
413 if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
414 Print(L"LoaderConfigTimeout: %d\n", i);
415 if (config->entry_oneshot)
416 Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
417 if (efivar_get(L"LoaderDeviceIdentifier", &s) == EFI_SUCCESS) {
418 Print(L"LoaderDeviceIdentifier: %s\n", s);
421 if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) {
422 Print(L"LoaderDevicePartUUID: %s\n", s);
425 if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) {
426 Print(L"LoaderEntryDefault: %s\n", s);
430 Print(L"\n--- press key ---\n\n");
431 console_key_read(&key, TRUE);
433 for (i = 0; i < config->entry_count; i++) {
436 if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
439 entry = config->entries[i];
442 UINT8 *content = NULL;
445 len = file_read(root_dir, entry->splash, 0, 0, &content);
448 static const EFI_GRAPHICS_OUTPUT_BLT_PIXEL colors[] = {
449 { .Red = 0xff, .Green = 0xff, .Blue = 0xff },
450 { .Red = 0xc0, .Green = 0xc0, .Blue = 0xc0 },
451 { .Red = 0xff, .Green = 0, .Blue = 0 },
452 { .Red = 0, .Green = 0xff, .Blue = 0 },
453 { .Red = 0, .Green = 0, .Blue = 0xff },
454 { .Red = 0, .Green = 0, .Blue = 0 },
457 err = graphics_splash(content, len, pixel);
461 /* 'b' rotates through background colors */
462 console_key_read(&key, TRUE);
463 if (key != KEYPRESS(0, 0, 'b'))
465 pixel = &colors[color++];
466 if (color == ELEMENTSOF(colors))
472 graphics_mode(FALSE);
473 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
476 Print(L"config entry: %d/%d\n", i+1, config->entry_count);
478 Print(L"file '%s'\n", entry->file);
479 Print(L"title show '%s'\n", entry->title_show);
481 Print(L"title '%s'\n", entry->title);
483 Print(L"version '%s'\n", entry->version);
484 if (entry->machine_id)
485 Print(L"machine-id '%s'\n", entry->machine_id);
487 EFI_DEVICE_PATH *device_path;
490 device_path = DevicePathFromHandle(entry->device);
492 str = DevicePathToStr(device_path);
493 Print(L"device handle '%s'\n", str);
498 Print(L"loader '%s'\n", entry->loader);
500 Print(L"options '%s'\n", entry->options);
502 Print(L"splash '%s'\n", entry->splash);
503 Print(L"auto-select %s\n", entry->no_autoselect ? L"no" : L"yes");
505 Print(L"internal call yes\n");
507 Print(L"\n--- press key ---\n\n");
508 console_key_read(&key, TRUE);
511 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
514 static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
518 UINTN idx_highlight_prev;
534 BOOLEAN exit = FALSE;
536 BOOLEAN wait = FALSE;
538 graphics_mode(FALSE);
539 uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
540 uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
541 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
543 /* draw a single character to make ClearScreen work on some firmware */
544 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" ");
545 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
547 err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
548 if (EFI_ERROR(err)) {
553 /* we check 10 times per second for a keystroke */
554 if (config->timeout_sec > 0)
555 timeout_remain = config->timeout_sec * 10;
559 idx_highlight = config->idx_default;
560 idx_highlight_prev = 0;
562 visible_max = y_max - 2;
564 if ((UINTN)config->idx_default >= visible_max)
565 idx_first = config->idx_default-1;
569 idx_last = idx_first + visible_max-1;
574 /* length of the longest entry */
576 for (i = 0; i < config->entry_count; i++) {
579 entry_len = StrLen(config->entries[i]->title_show);
580 if (line_width < entry_len)
581 line_width = entry_len;
583 if (line_width > x_max-6)
584 line_width = x_max-6;
586 /* offsets to center the entries on the screen */
587 x_start = (x_max - (line_width)) / 2;
588 if (config->entry_count < visible_max)
589 y_start = ((visible_max - config->entry_count) / 2) + 1;
593 /* menu entries title lines */
594 lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
595 for (i = 0; i < config->entry_count; i++) {
598 lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
599 for (j = 0; j < x_start; j++)
602 for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
603 lines[i][j] = config->entries[i]->title_show[k];
605 for (; j < x_max; j++)
607 lines[i][x_max] = '\0';
611 clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
612 for (i = 0; i < x_max; i++)
620 for (i = 0; i < config->entry_count; i++) {
621 if (i < idx_first || i > idx_last)
623 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
624 if (i == idx_highlight)
625 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
626 EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
628 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
629 EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
630 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
631 if ((INTN)i == config->idx_default_efivar) {
632 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
633 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
637 } else if (highlight) {
638 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
639 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
640 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
641 if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
642 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
643 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
646 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
647 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
648 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
649 if ((INTN)idx_highlight == config->idx_default_efivar) {
650 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
651 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
656 if (timeout_remain > 0) {
658 status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
661 /* print status at last line of screen */
667 len = StrLen(status);
669 x = (x_max - len) / 2;
672 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
673 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
674 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
675 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
676 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
679 err = console_key_read(&key, wait);
680 if (EFI_ERROR(err)) {
681 /* timeout reached */
682 if (timeout_remain == 0) {
687 /* sleep and update status */
688 if (timeout_remain > 0) {
689 uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
694 /* timeout disabled, wait for next key */
701 /* clear status after keystroke */
705 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
706 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
707 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
710 idx_highlight_prev = idx_highlight;
713 case KEYPRESS(0, SCAN_UP, 0):
714 case KEYPRESS(0, 0, 'k'):
715 if (idx_highlight > 0)
719 case KEYPRESS(0, SCAN_DOWN, 0):
720 case KEYPRESS(0, 0, 'j'):
721 if (idx_highlight < config->entry_count-1)
725 case KEYPRESS(0, SCAN_HOME, 0):
726 case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
727 if (idx_highlight > 0) {
733 case KEYPRESS(0, SCAN_END, 0):
734 case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
735 if (idx_highlight < config->entry_count-1) {
737 idx_highlight = config->entry_count-1;
741 case KEYPRESS(0, SCAN_PAGE_UP, 0):
742 if (idx_highlight > visible_max)
743 idx_highlight -= visible_max;
748 case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
749 idx_highlight += visible_max;
750 if (idx_highlight > config->entry_count-1)
751 idx_highlight = config->entry_count-1;
754 case KEYPRESS(0, 0, CHAR_LINEFEED):
755 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
759 case KEYPRESS(0, SCAN_F1, 0):
760 case KEYPRESS(0, 0, 'h'):
761 case KEYPRESS(0, 0, '?'):
762 status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
765 case KEYPRESS(0, 0, 'Q'):
770 case KEYPRESS(0, 0, 'd'):
771 if (config->idx_default_efivar != (INTN)idx_highlight) {
772 /* store the selected entry in a persistent EFI variable */
773 efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE);
774 config->idx_default_efivar = idx_highlight;
775 status = StrDuplicate(L"Default boot entry selected.");
777 /* clear the default entry EFI variable */
778 efivar_set(L"LoaderEntryDefault", NULL, TRUE);
779 config->idx_default_efivar = -1;
780 status = StrDuplicate(L"Default boot entry cleared.");
785 case KEYPRESS(0, 0, '-'):
786 case KEYPRESS(0, 0, 'T'):
787 if (config->timeout_sec_efivar > 0) {
788 config->timeout_sec_efivar--;
789 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
790 if (config->timeout_sec_efivar > 0)
791 status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
793 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
794 } else if (config->timeout_sec_efivar <= 0){
795 config->timeout_sec_efivar = -1;
796 efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
797 if (config->timeout_sec_config > 0)
798 status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
799 config->timeout_sec_config);
801 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
805 case KEYPRESS(0, 0, '+'):
806 case KEYPRESS(0, 0, 't'):
807 if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
808 config->timeout_sec_efivar++;
809 config->timeout_sec_efivar++;
810 efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
811 if (config->timeout_sec_efivar > 0)
812 status = PoolPrint(L"Menu timeout set to %d sec.",
813 config->timeout_sec_efivar);
815 status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
818 case KEYPRESS(0, 0, 'e'):
819 /* only the options of configured entries can be edited */
820 if (config->entries[idx_highlight]->type == LOADER_UNDEFINED)
822 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
823 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
824 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
825 if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
827 uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
828 uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
831 case KEYPRESS(0, 0, 'v'):
832 status = PoolPrint(L"systemd-boot " VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
833 ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
834 ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
837 case KEYPRESS(0, 0, 'P'):
838 print_status(config, root_dir, loaded_image_path);
842 case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
843 case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
848 /* jump with a hotkey directly to a matching entry */
849 idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
856 if (idx_highlight > idx_last) {
857 idx_last = idx_highlight;
858 idx_first = 1 + idx_highlight - visible_max;
861 if (idx_highlight < idx_first) {
862 idx_first = idx_highlight;
863 idx_last = idx_highlight + visible_max-1;
867 idx_last = idx_first + visible_max-1;
869 if (!refresh && idx_highlight != idx_highlight_prev)
873 *chosen_entry = config->entries[idx_highlight];
875 for (i = 0; i < config->entry_count; i++)
880 uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
881 uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
885 static VOID config_add_entry(Config *config, ConfigEntry *entry) {
886 if ((config->entry_count & 15) == 0) {
889 i = config->entry_count + 16;
890 if (config->entry_count == 0)
891 config->entries = AllocatePool(sizeof(VOID *) * i);
893 config->entries = ReallocatePool(config->entries,
894 sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
896 config->entries[config->entry_count++] = entry;
899 static VOID config_entry_free(ConfigEntry *entry) {
900 FreePool(entry->title_show);
901 FreePool(entry->title);
902 FreePool(entry->machine_id);
903 FreePool(entry->loader);
904 FreePool(entry->options);
907 static BOOLEAN is_digit(CHAR16 c)
909 return (c >= '0') && (c <= '9');
912 static UINTN c_order(CHAR16 c)
918 else if ((c >= 'a') && (c <= 'z'))
924 static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2)
932 while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
935 order = c_order(*s1) - c_order(*s2);
948 while (is_digit(*s1) && is_digit(*s2)) {
964 return StrCmp(os1, os2);
967 static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
973 line = content + *pos;
978 while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
981 /* move pos to next line */
991 line[linelen] = '\0';
993 /* remove leading whitespace */
994 while (strchra((CHAR8 *)" \t", *line)) {
999 /* remove trailing whitespace */
1000 while (linelen > 0 && strchra(sep, line[linelen-1]))
1002 line[linelen] = '\0';
1007 /* split key/value */
1009 while (*value && !strchra(sep, *value))
1015 while (*value && strchra(sep, *value))
1019 if (value[0] == '\"' && line[linelen-1] == '\"') {
1021 line[linelen-1] = '\0';
1029 static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
1035 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1036 if (strcmpa((CHAR8 *)"timeout", key) == 0) {
1039 s = stra_to_str(value);
1040 config->timeout_sec_config = Atoi(s);
1041 config->timeout_sec = config->timeout_sec_config;
1046 if (strcmpa((CHAR8 *)"default", key) == 0) {
1047 FreePool(config->entry_default_pattern);
1048 config->entry_default_pattern = stra_to_str(value);
1049 StrLwr(config->entry_default_pattern);
1055 static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
1061 CHAR16 *initrd = NULL;
1063 entry = AllocateZeroPool(sizeof(ConfigEntry));
1066 while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
1067 if (strcmpa((CHAR8 *)"title", key) == 0) {
1068 FreePool(entry->title);
1069 entry->title = stra_to_str(value);
1073 if (strcmpa((CHAR8 *)"version", key) == 0) {
1074 FreePool(entry->version);
1075 entry->version = stra_to_str(value);
1079 if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
1080 FreePool(entry->machine_id);
1081 entry->machine_id = stra_to_str(value);
1085 if (strcmpa((CHAR8 *)"linux", key) == 0) {
1086 FreePool(entry->loader);
1087 entry->type = LOADER_LINUX;
1088 entry->loader = stra_to_path(value);
1093 if (strcmpa((CHAR8 *)"efi", key) == 0) {
1094 entry->type = LOADER_EFI;
1095 FreePool(entry->loader);
1096 entry->loader = stra_to_path(value);
1098 /* do not add an entry for ourselves */
1099 if (StriCmp(entry->loader, loaded_image_path) == 0) {
1100 entry->type = LOADER_UNDEFINED;
1106 if (strcmpa((CHAR8 *)"architecture", key) == 0) {
1107 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1108 if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) {
1109 entry->type = LOADER_UNDEFINED;
1115 if (strcmpa((CHAR8 *)"initrd", key) == 0) {
1118 new = stra_to_path(value);
1122 s = PoolPrint(L"%s initrd=%s", initrd, new);
1126 initrd = PoolPrint(L"initrd=%s", new);
1131 if (strcmpa((CHAR8 *)"options", key) == 0) {
1134 new = stra_to_str(value);
1135 if (entry->options) {
1138 s = PoolPrint(L"%s %s", entry->options, new);
1139 FreePool(entry->options);
1142 entry->options = new;
1149 if (strcmpa((CHAR8 *)"splash", key) == 0) {
1150 FreePool(entry->splash);
1151 entry->splash = stra_to_path(value);
1156 if (entry->type == LOADER_UNDEFINED) {
1157 config_entry_free(entry);
1163 /* add initrd= to options */
1164 if (entry->type == LOADER_LINUX && initrd) {
1165 if (entry->options) {
1168 s = PoolPrint(L"%s %s", initrd, entry->options);
1169 FreePool(entry->options);
1172 entry->options = initrd;
1178 if (entry->machine_id) {
1181 /* append additional options from EFI variables for this machine-id */
1182 var = PoolPrint(L"LoaderEntryOptions-%s", entry->machine_id);
1186 if (efivar_get(var, &s) == EFI_SUCCESS) {
1187 if (entry->options) {
1190 s2 = PoolPrint(L"%s %s", entry->options, s);
1191 FreePool(entry->options);
1192 entry->options = s2;
1199 var = PoolPrint(L"LoaderEntryOptionsOneShot-%s", entry->machine_id);
1203 if (efivar_get(var, &s) == EFI_SUCCESS) {
1204 if (entry->options) {
1207 s2 = PoolPrint(L"%s %s", entry->options, s);
1208 FreePool(entry->options);
1209 entry->options = s2;
1212 efivar_set(var, NULL, TRUE);
1218 entry->device = device;
1219 entry->file = StrDuplicate(file);
1220 len = StrLen(entry->file);
1221 /* remove ".conf" */
1223 entry->file[len - 5] = '\0';
1224 StrLwr(entry->file);
1226 config_add_entry(config, entry);
1229 static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) {
1230 EFI_FILE_HANDLE entries_dir;
1232 CHAR8 *content = NULL;
1237 len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content);
1239 config_defaults_load_from_file(config, content);
1242 err = efivar_get_int(L"LoaderConfigTimeout", &sec);
1243 if (!EFI_ERROR(err)) {
1244 config->timeout_sec_efivar = sec;
1245 config->timeout_sec = sec;
1247 config->timeout_sec_efivar = -1;
1249 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
1250 if (!EFI_ERROR(err)) {
1255 CHAR8 *content = NULL;
1258 bufsize = sizeof(buf);
1259 err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
1260 if (bufsize == 0 || EFI_ERROR(err))
1263 f = (EFI_FILE_INFO *) buf;
1264 if (f->FileName[0] == '.')
1266 if (f->Attribute & EFI_FILE_DIRECTORY)
1268 len = StrLen(f->FileName);
1271 if (StriCmp(f->FileName + len - 5, L".conf") != 0)
1274 len = file_read(entries_dir, f->FileName, 0, 0, &content);
1276 config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path);
1279 uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
1282 /* sort entries after version number */
1283 for (i = 1; i < config->entry_count; i++) {
1288 for (k = 0; k < config->entry_count - i; k++) {
1291 if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0)
1293 entry = config->entries[k];
1294 config->entries[k] = config->entries[k+1];
1295 config->entries[k+1] = entry;
1303 static VOID config_default_entry_select(Config *config) {
1309 * The EFI variable to specify a boot entry for the next, and only the
1310 * next reboot. The variable is always cleared directly after it is read.
1312 err = efivar_get(L"LoaderEntryOneShot", &var);
1313 if (!EFI_ERROR(err)) {
1314 BOOLEAN found = FALSE;
1316 for (i = 0; i < config->entry_count; i++) {
1317 if (StrCmp(config->entries[i]->file, var) == 0) {
1318 config->idx_default = i;
1324 config->entry_oneshot = StrDuplicate(var);
1325 efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
1332 * The EFI variable to select the default boot entry overrides the
1333 * configured pattern. The variable can be set and cleared by pressing
1334 * the 'd' key in the loader selection menu, the entry is marked with
1337 err = efivar_get(L"LoaderEntryDefault", &var);
1338 if (!EFI_ERROR(err)) {
1339 BOOLEAN found = FALSE;
1341 for (i = 0; i < config->entry_count; i++) {
1342 if (StrCmp(config->entries[i]->file, var) == 0) {
1343 config->idx_default = i;
1344 config->idx_default_efivar = i;
1353 config->idx_default_efivar = -1;
1355 if (config->entry_count == 0)
1359 * Match the pattern from the end of the list to the start, find last
1360 * entry (largest number) matching the given pattern.
1362 if (config->entry_default_pattern) {
1363 i = config->entry_count;
1365 if (config->entries[i]->no_autoselect)
1367 if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) {
1368 config->idx_default = i;
1374 /* select the last suitable entry */
1375 i = config->entry_count;
1377 if (config->entries[i]->no_autoselect)
1379 config->idx_default = i;
1383 /* no entry found */
1384 config->idx_default = -1;
1387 /* generate a unique title, avoiding non-distinguishable menu entries */
1388 static VOID config_title_generate(Config *config) {
1393 for (i = 0; i < config->entry_count; i++) {
1396 FreePool(config->entries[i]->title_show);
1397 title = config->entries[i]->title;
1399 title = config->entries[i]->file;
1400 config->entries[i]->title_show = StrDuplicate(title);
1404 for (i = 0; i < config->entry_count; i++) {
1405 for (k = 0; k < config->entry_count; k++) {
1408 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1412 config->entries[i]->non_unique = TRUE;
1413 config->entries[k]->non_unique = TRUE;
1419 /* add version to non-unique titles */
1420 for (i = 0; i < config->entry_count; i++) {
1423 if (!config->entries[i]->non_unique)
1425 if (!config->entries[i]->version)
1428 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
1429 FreePool(config->entries[i]->title_show);
1430 config->entries[i]->title_show = s;
1431 config->entries[i]->non_unique = FALSE;
1435 for (i = 0; i < config->entry_count; i++) {
1436 for (k = 0; k < config->entry_count; k++) {
1439 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1443 config->entries[i]->non_unique = TRUE;
1444 config->entries[k]->non_unique = TRUE;
1450 /* add machine-id to non-unique titles */
1451 for (i = 0; i < config->entry_count; i++) {
1455 if (!config->entries[i]->non_unique)
1457 if (!config->entries[i]->machine_id)
1460 m = StrDuplicate(config->entries[i]->machine_id);
1462 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
1463 FreePool(config->entries[i]->title_show);
1464 config->entries[i]->title_show = s;
1465 config->entries[i]->non_unique = FALSE;
1470 for (i = 0; i < config->entry_count; i++) {
1471 for (k = 0; k < config->entry_count; k++) {
1474 if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0)
1478 config->entries[i]->non_unique = TRUE;
1479 config->entries[k]->non_unique = TRUE;
1485 /* add file name to non-unique titles */
1486 for (i = 0; i < config->entry_count; i++) {
1489 if (!config->entries[i]->non_unique)
1491 s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file);
1492 FreePool(config->entries[i]->title_show);
1493 config->entries[i]->title_show = s;
1494 config->entries[i]->non_unique = FALSE;
1498 static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) {
1501 entry = AllocateZeroPool(sizeof(ConfigEntry));
1502 entry->title = StrDuplicate(title);
1504 entry->no_autoselect = TRUE;
1505 config_add_entry(config, entry);
1509 static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device,
1510 enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1513 entry = AllocateZeroPool(sizeof(ConfigEntry));
1515 entry->title = StrDuplicate(title);
1516 entry->device = device;
1517 entry->loader = StrDuplicate(loader);
1518 entry->file = StrDuplicate(file);
1519 StrLwr(entry->file);
1521 config_add_entry(config, entry);
1526 static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path,
1527 CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) {
1528 EFI_FILE_HANDLE handle;
1532 /* do not add an entry for ourselves */
1533 if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0)
1536 /* check existence */
1537 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
1540 uefi_call_wrapper(handle->Close, 1, handle);
1542 entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader);
1546 /* do not boot right away into auto-detected entries */
1547 entry->no_autoselect = TRUE;
1549 /* export identifiers of automatically added entries */
1550 if (config->entries_auto) {
1553 s = PoolPrint(L"%s %s", config->entries_auto, file);
1554 FreePool(config->entries_auto);
1555 config->entries_auto = s;
1557 config->entries_auto = StrDuplicate(file);
1562 static VOID config_entry_add_osx(Config *config) {
1564 UINTN handle_count = 0;
1565 EFI_HANDLE *handles = NULL;
1567 err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
1568 if (!EFI_ERROR(err)) {
1571 for (i = 0; i < handle_count; i++) {
1575 root = LibOpenRoot(handles[i]);
1578 found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"OS X",
1579 L"\\System\\Library\\CoreServices\\boot.efi");
1580 uefi_call_wrapper(root->Close, 1, root);
1589 static VOID config_entry_add_linux( Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) {
1590 EFI_FILE_HANDLE linux_dir;
1593 err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL);
1594 if (!EFI_ERROR(err)) {
1599 CHAR8 *sections[] = {
1603 UINTN offs[ELEMENTSOF(sections)-1] = {};
1604 UINTN szs[ELEMENTSOF(sections)-1] = {};
1605 UINTN addrs[ELEMENTSOF(sections)-1] = {};
1606 CHAR8 *content = NULL;
1611 CHAR16 *os_name = NULL;
1612 CHAR16 *os_id = NULL;
1613 CHAR16 *os_version = NULL;
1615 bufsize = sizeof(buf);
1616 err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf);
1617 if (bufsize == 0 || EFI_ERROR(err))
1620 f = (EFI_FILE_INFO *) buf;
1621 if (f->FileName[0] == '.')
1623 if (f->Attribute & EFI_FILE_DIRECTORY)
1625 len = StrLen(f->FileName);
1628 if (StriCmp(f->FileName + len - 4, L".efi") != 0)
1631 /* look for an .osrel section in the .efi binary */
1632 err = pefile_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs);
1636 len = file_read(linux_dir, f->FileName, offs[0], szs[0], &content);
1640 /* read properties from the embedded os-release file */
1642 while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
1643 if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) {
1644 os_name = stra_to_str(value);
1648 if (strcmpa((CHAR8 *)"ID", key) == 0) {
1649 os_id = stra_to_str(value);
1653 if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) {
1654 os_version = stra_to_str(value);
1659 if (os_name && os_id && os_version) {
1663 conf = PoolPrint(L"%s-%s", os_id, os_version);
1664 path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName);
1665 config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path);
1670 FreePool(os_version);
1675 uefi_call_wrapper(linux_dir->Close, 1, linux_dir);
1679 static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) {
1681 EFI_DEVICE_PATH *path;
1685 path = FileDevicePath(entry->device, entry->loader);
1687 Print(L"Error getting device path.");
1688 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1689 return EFI_INVALID_PARAMETER;
1692 err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
1693 if (EFI_ERROR(err)) {
1694 Print(L"Error loading %s: %r", entry->loader, err);
1695 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1699 if (config->options_edit)
1700 options = config->options_edit;
1701 else if (entry->options)
1702 options = entry->options;
1706 EFI_LOADED_IMAGE *loaded_image;
1708 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1709 parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1710 if (EFI_ERROR(err)) {
1711 Print(L"Error getting LoadedImageProtocol handle: %r", err);
1712 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1715 loaded_image->LoadOptions = options;
1716 loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
1719 efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
1720 err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
1722 uefi_call_wrapper(BS->UnloadImage, 1, image);
1728 static EFI_STATUS reboot_into_firmware(VOID) {
1734 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
1736 err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
1737 if (!EFI_ERROR(err))
1738 osind |= (UINT64)*b;
1741 err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
1745 err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
1746 Print(L"Error calling ResetSystem: %r", err);
1747 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1751 static VOID config_free(Config *config) {
1754 for (i = 0; i < config->entry_count; i++)
1755 config_entry_free(config->entries[i]);
1756 FreePool(config->entries);
1757 FreePool(config->entry_default_pattern);
1758 FreePool(config->options_edit);
1759 FreePool(config->entry_oneshot);
1760 FreePool(config->entries_auto);
1763 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
1767 EFI_LOADED_IMAGE *loaded_image;
1769 CHAR16 *loaded_image_path;
1770 EFI_DEVICE_PATH *device_path;
1774 BOOLEAN menu = FALSE;
1776 InitializeLib(image, sys_table);
1777 init_usec = time_usec();
1778 efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
1779 efivar_set(L"LoaderInfo", L"systemd-boot " VERSION, FALSE);
1780 s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1781 efivar_set(L"LoaderFirmwareInfo", s, FALSE);
1783 s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
1784 efivar_set(L"LoaderFirmwareType", s, FALSE);
1787 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
1788 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1789 if (EFI_ERROR(err)) {
1790 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
1791 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1795 /* export the device path this image is started from */
1796 device_path = DevicePathFromHandle(loaded_image->DeviceHandle);
1799 EFI_DEVICE_PATH *path, *paths;
1801 str = DevicePathToStr(device_path);
1802 efivar_set(L"LoaderDeviceIdentifier", str, FALSE);
1805 paths = UnpackDevicePath(device_path);
1806 for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
1807 HARDDRIVE_DEVICE_PATH *drive;
1810 if (DevicePathType(path) != MEDIA_DEVICE_PATH)
1812 if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP)
1814 drive = (HARDDRIVE_DEVICE_PATH *)path;
1815 if (drive->SignatureType != SIGNATURE_TYPE_GUID)
1818 GuidToString(uuid, (EFI_GUID *)&drive->Signature);
1819 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
1825 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
1827 Print(L"Unable to open root directory: %r ", err);
1828 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1829 return EFI_LOAD_ERROR;
1833 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1834 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
1835 efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
1837 /* scan "\loader\entries\*.conf" files */
1838 ZeroMem(&config, sizeof(Config));
1839 config_load(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
1841 /* if we find some well-known loaders, add them to the end of the list */
1842 config_entry_add_linux(&config, loaded_image, root_dir);
1843 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1844 L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1845 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1846 L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
1847 config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
1848 L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");
1849 config_entry_add_osx(&config);
1850 efivar_set(L"LoaderEntriesAuto", config.entries_auto, FALSE);
1852 if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
1853 UINT64 osind = (UINT64)*b;
1855 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
1856 config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
1860 if (config.entry_count == 0) {
1861 Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1862 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1866 config_title_generate(&config);
1868 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/
1869 config_default_entry_select(&config);
1871 /* if no configured entry to select from was found, enable the menu */
1872 if (config.idx_default == -1) {
1873 config.idx_default = 0;
1874 if (config.timeout_sec == 0)
1875 config.timeout_sec = 10;
1878 /* select entry or show menu when key is pressed or timeout is set */
1879 if (config.timeout_sec == 0) {
1882 err = console_key_read(&key, FALSE);
1883 if (!EFI_ERROR(err)) {
1886 /* find matching key in config entries */
1887 idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
1889 config.idx_default = idx;
1899 entry = config.entries[config.idx_default];
1901 efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
1902 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
1903 if (!menu_run(&config, &entry, root_dir, loaded_image_path))
1906 /* run special entry like "reboot" */
1911 } else if (entry->splash) {
1912 UINT8 *content = NULL;
1915 len = file_read(root_dir, entry->splash, 0, 0, &content);
1917 graphics_splash(content, len, NULL);
1922 /* export the selected boot entry to the system */
1923 efivar_set(L"LoaderEntrySelected", entry->file, FALSE);
1925 uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
1926 err = image_start(image, &config, entry);
1927 if (EFI_ERROR(err)) {
1928 graphics_mode(FALSE);
1929 Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err);
1930 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
1935 config.timeout_sec = 0;
1939 FreePool(loaded_image_path);
1940 config_free(&config);
1941 uefi_call_wrapper(root_dir->Close, 1, root_dir);
1942 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);