1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * Copyright (C) 2012-2015 Kay Sievers <kay@vrfy.org>
14 * Copyright (C) 2012-2015 Harald Hoyer <harald@redhat.com>
29 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
30 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
33 /* magic string to find in the binary image */
34 static const char __attribute__((used
)) magic
[] = "#### LoaderInfo: systemd-boot " PACKAGE_VERSION
" ####";
36 static const EFI_GUID global_guid
= EFI_GLOBAL_VARIABLE
;
51 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
) {
77 else if ((*first
) > 0)
81 static VOID
cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
) {
82 if ((*cursor
)+1 < x_max
)
84 else if ((*first
) + (*cursor
) < len
)
88 static BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
, UINTN y_pos
) {
101 size
= StrLen(line_in
) + 1024;
102 line
= AllocatePool(size
* sizeof(CHAR16
));
103 StrCpy(line
, line_in
);
105 print
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
107 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, TRUE
);
122 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
123 while (clear
> 0 && i
< x_max
-1) {
129 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_pos
);
130 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, print
);
131 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
133 err
= console_key_read(&key
, TRUE
);
138 case KEYPRESS(0, SCAN_ESC
, 0):
139 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'c'):
140 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'g'):
141 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('c')):
142 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('g')):
146 case KEYPRESS(0, SCAN_HOME
, 0):
147 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'a'):
148 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('a')):
149 /* beginning-of-line */
154 case KEYPRESS(0, SCAN_END
, 0):
155 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'e'):
156 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('e')):
158 cursor
= len
- first
;
159 if (cursor
+1 >= x_max
) {
161 first
= len
- (x_max
-1);
165 case KEYPRESS(0, SCAN_DOWN
, 0):
166 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'f'):
167 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_RIGHT
, 0):
169 while (line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
170 cursor_right(&cursor
, &first
, x_max
, len
);
171 while (line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
172 cursor_right(&cursor
, &first
, x_max
, len
);
173 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
176 case KEYPRESS(0, SCAN_UP
, 0):
177 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'b'):
178 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_LEFT
, 0):
180 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
181 cursor_left(&cursor
, &first
);
182 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ')
183 cursor_left(&cursor
, &first
);
185 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ')
186 cursor_left(&cursor
, &first
);
187 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
190 case KEYPRESS(0, SCAN_RIGHT
, 0):
191 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'f'):
192 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('f')):
194 if (first
+ cursor
== len
)
196 cursor_right(&cursor
, &first
, x_max
, len
);
197 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
200 case KEYPRESS(0, SCAN_LEFT
, 0):
201 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'b'):
202 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('b')):
204 cursor_left(&cursor
, &first
);
205 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
208 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'd'):
211 for (i
= first
+ cursor
; i
< len
&& line
[i
] == ' '; i
++)
213 for (; i
< len
&& line
[i
] != ' '; i
++)
216 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
217 line
[i
] = line
[i
+ clear
];
222 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'w'):
223 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('w')):
224 case KEYPRESS(EFI_ALT_PRESSED
, 0, CHAR_BACKSPACE
):
225 /* backward-kill-word */
227 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
228 cursor_left(&cursor
, &first
);
230 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ') {
231 cursor_left(&cursor
, &first
);
235 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ') {
236 cursor_left(&cursor
, &first
);
239 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
241 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
242 line
[i
] = line
[i
+ clear
];
247 case KEYPRESS(0, SCAN_DELETE
, 0):
248 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'd'):
249 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('d')):
252 if (first
+ cursor
== len
)
254 for (i
= first
+ cursor
; i
< len
; i
++)
260 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'k'):
261 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('k')):
263 line
[first
+ cursor
] = '\0';
264 clear
= len
- (first
+ cursor
);
265 len
= first
+ cursor
;
268 case KEYPRESS(0, 0, CHAR_LINEFEED
):
269 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
270 if (StrCmp(line
, line_in
) != 0) {
278 case KEYPRESS(0, 0, CHAR_BACKSPACE
):
281 if (first
== 0 && cursor
== 0)
283 for (i
= first
+ cursor
-1; i
< len
; i
++)
289 if (cursor
> 0 || first
== 0)
291 /* show full line if it fits */
297 /* jump left to see what we delete */
307 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
308 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
311 for (i
= len
; i
> first
+ cursor
; i
--)
313 line
[first
+ cursor
] = KEYCHAR(key
);
316 if (cursor
+1 < x_max
)
318 else if (first
+ cursor
< len
)
324 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
330 static UINTN
entry_lookup_key(Config
*config
, UINTN start
, CHAR16 key
) {
336 /* select entry by number key */
337 if (key
>= '1' && key
<= '9') {
339 if (i
> config
->entry_count
)
340 i
= config
->entry_count
;
344 /* find matching key in config entries */
345 for (i
= start
; i
< config
->entry_count
; i
++)
346 if (config
->entries
[i
]->key
== key
)
349 for (i
= 0; i
< start
; i
++)
350 if (config
->entries
[i
]->key
== key
)
356 static VOID
print_status(Config
*config
, CHAR16
*loaded_image_path
) {
365 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
366 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
368 Print(L
"systemd-boot version: " PACKAGE_VERSION
"\n");
369 Print(L
"architecture: " EFI_MACHINE_TYPE_NAME
"\n");
370 Print(L
"loaded image: %s\n", loaded_image_path
);
371 Print(L
"UEFI specification: %d.%02d\n", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
372 Print(L
"firmware vendor: %s\n", ST
->FirmwareVendor
);
373 Print(L
"firmware version: %d.%02d\n", ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
375 if (uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x
, &y
) == EFI_SUCCESS
)
376 Print(L
"console size: %d x %d\n", x
, y
);
378 if (efivar_get_raw(&global_guid
, L
"SecureBoot", &b
, &size
) == EFI_SUCCESS
) {
379 Print(L
"SecureBoot: %s\n", yes_no(*b
> 0));
383 if (efivar_get_raw(&global_guid
, L
"SetupMode", &b
, &size
) == EFI_SUCCESS
) {
384 Print(L
"SetupMode: %s\n", *b
> 0 ? L
"setup" : L
"user");
389 Print(L
"Shim: present\n");
391 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
392 Print(L
"OsIndicationsSupported: %d\n", (UINT64
)*b
);
397 Print(L
"timeout: %d\n", config
->timeout_sec
);
398 if (config
->timeout_sec_efivar
>= 0)
399 Print(L
"timeout (EFI var): %d\n", config
->timeout_sec_efivar
);
400 Print(L
"timeout (config): %d\n", config
->timeout_sec_config
);
401 if (config
->entry_default_pattern
)
402 Print(L
"default pattern: '%s'\n", config
->entry_default_pattern
);
403 Print(L
"editor: %s\n", yes_no(!config
->no_editor
));
406 Print(L
"config entry count: %d\n", config
->entry_count
);
407 Print(L
"entry selected idx: %d\n", config
->idx_default
);
408 if (config
->idx_default_efivar
>= 0)
409 Print(L
"entry EFI var idx: %d\n", config
->idx_default_efivar
);
412 if (efivar_get_int(L
"LoaderConfigTimeout", &i
) == EFI_SUCCESS
)
413 Print(L
"LoaderConfigTimeout: %d\n", i
);
414 if (config
->entry_oneshot
)
415 Print(L
"LoaderEntryOneShot: %s\n", config
->entry_oneshot
);
416 if (efivar_get(L
"LoaderDevicePartUUID", &s
) == EFI_SUCCESS
) {
417 Print(L
"LoaderDevicePartUUID: %s\n", s
);
420 if (efivar_get(L
"LoaderEntryDefault", &s
) == EFI_SUCCESS
) {
421 Print(L
"LoaderEntryDefault: %s\n", s
);
425 Print(L
"\n--- press key ---\n\n");
426 console_key_read(&key
, TRUE
);
428 for (i
= 0; i
< config
->entry_count
; i
++) {
431 if (key
== KEYPRESS(0, SCAN_ESC
, 0) || key
== KEYPRESS(0, 0, 'q'))
434 entry
= config
->entries
[i
];
435 Print(L
"config entry: %d/%d\n", i
+1, config
->entry_count
);
437 Print(L
"file '%s'\n", entry
->file
);
438 Print(L
"title show '%s'\n", entry
->title_show
);
440 Print(L
"title '%s'\n", entry
->title
);
442 Print(L
"version '%s'\n", entry
->version
);
443 if (entry
->machine_id
)
444 Print(L
"machine-id '%s'\n", entry
->machine_id
);
446 EFI_DEVICE_PATH
*device_path
;
449 device_path
= DevicePathFromHandle(entry
->device
);
451 str
= DevicePathToStr(device_path
);
452 Print(L
"device handle '%s'\n", str
);
457 Print(L
"loader '%s'\n", entry
->loader
);
459 Print(L
"options '%s'\n", entry
->options
);
460 Print(L
"auto-select %s\n", yes_no(!entry
->no_autoselect
));
462 Print(L
"internal call yes\n");
464 Print(L
"\n--- press key ---\n\n");
465 console_key_read(&key
, TRUE
);
468 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
471 static BOOLEAN
menu_run(Config
*config
, ConfigEntry
**chosen_entry
, CHAR16
*loaded_image_path
) {
475 UINTN idx_highlight_prev
;
491 BOOLEAN exit
= FALSE
;
493 BOOLEAN wait
= FALSE
;
495 graphics_mode(FALSE
);
496 uefi_call_wrapper(ST
->ConIn
->Reset
, 2, ST
->ConIn
, FALSE
);
497 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
498 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
500 /* draw a single character to make ClearScreen work on some firmware */
501 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
" ");
502 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
504 err
= uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
505 if (EFI_ERROR(err
)) {
510 /* we check 10 times per second for a keystroke */
511 if (config
->timeout_sec
> 0)
512 timeout_remain
= config
->timeout_sec
* 10;
516 idx_highlight
= config
->idx_default
;
517 idx_highlight_prev
= 0;
519 visible_max
= y_max
- 2;
521 if ((UINTN
)config
->idx_default
>= visible_max
)
522 idx_first
= config
->idx_default
-1;
526 idx_last
= idx_first
+ visible_max
-1;
531 /* length of the longest entry */
533 for (i
= 0; i
< config
->entry_count
; i
++) {
536 entry_len
= StrLen(config
->entries
[i
]->title_show
);
537 if (line_width
< entry_len
)
538 line_width
= entry_len
;
540 if (line_width
> x_max
-6)
541 line_width
= x_max
-6;
543 /* offsets to center the entries on the screen */
544 x_start
= (x_max
- (line_width
)) / 2;
545 if (config
->entry_count
< visible_max
)
546 y_start
= ((visible_max
- config
->entry_count
) / 2) + 1;
550 /* menu entries title lines */
551 lines
= AllocatePool(sizeof(CHAR16
*) * config
->entry_count
);
552 for (i
= 0; i
< config
->entry_count
; i
++) {
555 lines
[i
] = AllocatePool(((x_max
+1) * sizeof(CHAR16
)));
556 for (j
= 0; j
< x_start
; j
++)
559 for (k
= 0; config
->entries
[i
]->title_show
[k
] != '\0' && j
< x_max
; j
++, k
++)
560 lines
[i
][j
] = config
->entries
[i
]->title_show
[k
];
562 for (; j
< x_max
; j
++)
564 lines
[i
][x_max
] = '\0';
568 clearline
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
569 for (i
= 0; i
< x_max
; i
++)
577 for (i
= 0; i
< config
->entry_count
; i
++) {
578 if (i
< idx_first
|| i
> idx_last
)
580 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ i
- idx_first
);
581 if (i
== idx_highlight
)
582 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
583 EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
585 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
586 EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
587 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[i
]);
588 if ((INTN
)i
== config
->idx_default_efivar
) {
589 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ i
- idx_first
);
590 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
594 } else if (highlight
) {
595 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight_prev
- idx_first
);
596 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
597 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight_prev
]);
598 if ((INTN
)idx_highlight_prev
== config
->idx_default_efivar
) {
599 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight_prev
- idx_first
);
600 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
603 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight
- idx_first
);
604 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
605 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight
]);
606 if ((INTN
)idx_highlight
== config
->idx_default_efivar
) {
607 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight
- idx_first
);
608 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
613 if (timeout_remain
> 0) {
615 status
= PoolPrint(L
"Boot in %d sec.", (timeout_remain
+ 5) / 10);
618 /* print status at last line of screen */
624 len
= StrLen(status
);
626 x
= (x_max
- len
) / 2;
629 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
630 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
631 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+ (x_max
- x
));
632 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, status
);
633 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1 + x
+ len
);
636 err
= console_key_read(&key
, wait
);
637 if (EFI_ERROR(err
)) {
638 /* timeout reached */
639 if (timeout_remain
== 0) {
644 /* sleep and update status */
645 if (timeout_remain
> 0) {
646 uefi_call_wrapper(BS
->Stall
, 1, 100 * 1000);
651 /* timeout disabled, wait for next key */
658 /* clear status after keystroke */
662 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
663 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
664 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
667 idx_highlight_prev
= idx_highlight
;
670 case KEYPRESS(0, SCAN_UP
, 0):
671 case KEYPRESS(0, 0, 'k'):
672 if (idx_highlight
> 0)
676 case KEYPRESS(0, SCAN_DOWN
, 0):
677 case KEYPRESS(0, 0, 'j'):
678 if (idx_highlight
< config
->entry_count
-1)
682 case KEYPRESS(0, SCAN_HOME
, 0):
683 case KEYPRESS(EFI_ALT_PRESSED
, 0, '<'):
684 if (idx_highlight
> 0) {
690 case KEYPRESS(0, SCAN_END
, 0):
691 case KEYPRESS(EFI_ALT_PRESSED
, 0, '>'):
692 if (idx_highlight
< config
->entry_count
-1) {
694 idx_highlight
= config
->entry_count
-1;
698 case KEYPRESS(0, SCAN_PAGE_UP
, 0):
699 if (idx_highlight
> visible_max
)
700 idx_highlight
-= visible_max
;
705 case KEYPRESS(0, SCAN_PAGE_DOWN
, 0):
706 idx_highlight
+= visible_max
;
707 if (idx_highlight
> config
->entry_count
-1)
708 idx_highlight
= config
->entry_count
-1;
711 case KEYPRESS(0, 0, CHAR_LINEFEED
):
712 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
716 case KEYPRESS(0, SCAN_F1
, 0):
717 case KEYPRESS(0, 0, 'h'):
718 case KEYPRESS(0, 0, '?'):
719 status
= StrDuplicate(L
"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
722 case KEYPRESS(0, 0, 'Q'):
727 case KEYPRESS(0, 0, 'd'):
728 if (config
->idx_default_efivar
!= (INTN
)idx_highlight
) {
729 /* store the selected entry in a persistent EFI variable */
730 efivar_set(L
"LoaderEntryDefault", config
->entries
[idx_highlight
]->file
, TRUE
);
731 config
->idx_default_efivar
= idx_highlight
;
732 status
= StrDuplicate(L
"Default boot entry selected.");
734 /* clear the default entry EFI variable */
735 efivar_set(L
"LoaderEntryDefault", NULL
, TRUE
);
736 config
->idx_default_efivar
= -1;
737 status
= StrDuplicate(L
"Default boot entry cleared.");
742 case KEYPRESS(0, 0, '-'):
743 case KEYPRESS(0, 0, 'T'):
744 if (config
->timeout_sec_efivar
> 0) {
745 config
->timeout_sec_efivar
--;
746 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
747 if (config
->timeout_sec_efivar
> 0)
748 status
= PoolPrint(L
"Menu timeout set to %d sec.", config
->timeout_sec_efivar
);
750 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
751 } else if (config
->timeout_sec_efivar
<= 0){
752 config
->timeout_sec_efivar
= -1;
753 efivar_set(L
"LoaderConfigTimeout", NULL
, TRUE
);
754 if (config
->timeout_sec_config
> 0)
755 status
= PoolPrint(L
"Menu timeout of %d sec is defined by configuration file.",
756 config
->timeout_sec_config
);
758 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
762 case KEYPRESS(0, 0, '+'):
763 case KEYPRESS(0, 0, 't'):
764 if (config
->timeout_sec_efivar
== -1 && config
->timeout_sec_config
== 0)
765 config
->timeout_sec_efivar
++;
766 config
->timeout_sec_efivar
++;
767 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
768 if (config
->timeout_sec_efivar
> 0)
769 status
= PoolPrint(L
"Menu timeout set to %d sec.",
770 config
->timeout_sec_efivar
);
772 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
775 case KEYPRESS(0, 0, 'e'):
776 /* only the options of configured entries can be edited */
777 if (config
->no_editor
|| config
->entries
[idx_highlight
]->type
== LOADER_UNDEFINED
)
779 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
780 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
781 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
782 if (line_edit(config
->entries
[idx_highlight
]->options
, &config
->options_edit
, x_max
-1, y_max
-1))
784 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
785 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
788 case KEYPRESS(0, 0, 'v'):
789 status
= PoolPrint(L
"systemd-boot " PACKAGE_VERSION
" (" EFI_MACHINE_TYPE_NAME
"), UEFI Specification %d.%02d, Vendor %s %d.%02d",
790 ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff,
791 ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
794 case KEYPRESS(0, 0, 'P'):
795 print_status(config
, loaded_image_path
);
799 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'l'):
800 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('l')):
805 /* jump with a hotkey directly to a matching entry */
806 idx
= entry_lookup_key(config
, idx_highlight
+1, KEYCHAR(key
));
813 if (idx_highlight
> idx_last
) {
814 idx_last
= idx_highlight
;
815 idx_first
= 1 + idx_highlight
- visible_max
;
817 } else if (idx_highlight
< idx_first
) {
818 idx_first
= idx_highlight
;
819 idx_last
= idx_highlight
+ visible_max
-1;
823 if (!refresh
&& idx_highlight
!= idx_highlight_prev
)
827 *chosen_entry
= config
->entries
[idx_highlight
];
829 for (i
= 0; i
< config
->entry_count
; i
++)
834 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_WHITE
|EFI_BACKGROUND_BLACK
);
835 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
839 static VOID
config_add_entry(Config
*config
, ConfigEntry
*entry
) {
840 if ((config
->entry_count
& 15) == 0) {
843 i
= config
->entry_count
+ 16;
844 if (config
->entry_count
== 0)
845 config
->entries
= AllocatePool(sizeof(VOID
*) * i
);
847 config
->entries
= ReallocatePool(config
->entries
,
848 sizeof(VOID
*) * config
->entry_count
, sizeof(VOID
*) * i
);
850 config
->entries
[config
->entry_count
++] = entry
;
853 static VOID
config_entry_free(ConfigEntry
*entry
) {
854 FreePool(entry
->title_show
);
855 FreePool(entry
->title
);
856 FreePool(entry
->machine_id
);
857 FreePool(entry
->loader
);
858 FreePool(entry
->options
);
861 static BOOLEAN
is_digit(CHAR16 c
) {
862 return (c
>= '0') && (c
<= '9');
865 static UINTN
c_order(CHAR16 c
) {
870 else if ((c
>= 'a') && (c
<= 'z'))
876 static INTN
str_verscmp(CHAR16
*s1
, CHAR16
*s2
) {
883 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
886 order
= c_order(*s1
) - c_order(*s2
);
899 while (is_digit(*s1
) && is_digit(*s2
)) {
915 return StrCmp(os1
, os2
);
918 static CHAR8
*line_get_key_value(CHAR8
*content
, CHAR8
*sep
, UINTN
*pos
, CHAR8
**key_ret
, CHAR8
**value_ret
) {
924 line
= content
+ *pos
;
929 while (line
[linelen
] && !strchra((CHAR8
*)"\n\r", line
[linelen
]))
932 /* move pos to next line */
942 line
[linelen
] = '\0';
944 /* remove leading whitespace */
945 while (strchra((CHAR8
*)" \t", *line
)) {
950 /* remove trailing whitespace */
951 while (linelen
> 0 && strchra(sep
, line
[linelen
-1]))
953 line
[linelen
] = '\0';
958 /* split key/value */
960 while (*value
&& !strchra(sep
, *value
))
966 while (*value
&& strchra(sep
, *value
))
970 if (value
[0] == '\"' && line
[linelen
-1] == '\"') {
972 line
[linelen
-1] = '\0';
980 static VOID
config_defaults_load_from_file(Config
*config
, CHAR8
*content
) {
986 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
987 if (strcmpa((CHAR8
*)"timeout", key
) == 0) {
990 s
= stra_to_str(value
);
991 config
->timeout_sec_config
= Atoi(s
);
992 config
->timeout_sec
= config
->timeout_sec_config
;
997 if (strcmpa((CHAR8
*)"default", key
) == 0) {
998 FreePool(config
->entry_default_pattern
);
999 config
->entry_default_pattern
= stra_to_str(value
);
1000 StrLwr(config
->entry_default_pattern
);
1004 if (strcmpa((CHAR8
*)"editor", key
) == 0) {
1007 if (EFI_ERROR(parse_boolean(value
, &on
)))
1009 config
->no_editor
= !on
;
1014 static VOID
config_entry_add_from_file(Config
*config
, EFI_HANDLE
*device
, CHAR16
*file
, CHAR8
*content
, CHAR16
*loaded_image_path
) {
1020 CHAR16
*initrd
= NULL
;
1022 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1025 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1026 if (strcmpa((CHAR8
*)"title", key
) == 0) {
1027 FreePool(entry
->title
);
1028 entry
->title
= stra_to_str(value
);
1032 if (strcmpa((CHAR8
*)"version", key
) == 0) {
1033 FreePool(entry
->version
);
1034 entry
->version
= stra_to_str(value
);
1038 if (strcmpa((CHAR8
*)"machine-id", key
) == 0) {
1039 FreePool(entry
->machine_id
);
1040 entry
->machine_id
= stra_to_str(value
);
1044 if (strcmpa((CHAR8
*)"linux", key
) == 0) {
1045 FreePool(entry
->loader
);
1046 entry
->type
= LOADER_LINUX
;
1047 entry
->loader
= stra_to_path(value
);
1052 if (strcmpa((CHAR8
*)"efi", key
) == 0) {
1053 entry
->type
= LOADER_EFI
;
1054 FreePool(entry
->loader
);
1055 entry
->loader
= stra_to_path(value
);
1057 /* do not add an entry for ourselves */
1058 if (StriCmp(entry
->loader
, loaded_image_path
) == 0) {
1059 entry
->type
= LOADER_UNDEFINED
;
1065 if (strcmpa((CHAR8
*)"architecture", key
) == 0) {
1066 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1067 if (strcmpa((CHAR8
*)EFI_MACHINE_TYPE_NAME
, value
) != 0) {
1068 entry
->type
= LOADER_UNDEFINED
;
1074 if (strcmpa((CHAR8
*)"initrd", key
) == 0) {
1077 new = stra_to_path(value
);
1081 s
= PoolPrint(L
"%s initrd=%s", initrd
, new);
1085 initrd
= PoolPrint(L
"initrd=%s", new);
1090 if (strcmpa((CHAR8
*)"options", key
) == 0) {
1093 new = stra_to_str(value
);
1094 if (entry
->options
) {
1097 s
= PoolPrint(L
"%s %s", entry
->options
, new);
1098 FreePool(entry
->options
);
1101 entry
->options
= new;
1109 if (entry
->type
== LOADER_UNDEFINED
) {
1110 config_entry_free(entry
);
1116 /* add initrd= to options */
1117 if (entry
->type
== LOADER_LINUX
&& initrd
) {
1118 if (entry
->options
) {
1121 s
= PoolPrint(L
"%s %s", initrd
, entry
->options
);
1122 FreePool(entry
->options
);
1125 entry
->options
= initrd
;
1131 entry
->device
= device
;
1132 entry
->file
= StrDuplicate(file
);
1133 len
= StrLen(entry
->file
);
1134 /* remove ".conf" */
1136 entry
->file
[len
- 5] = '\0';
1137 StrLwr(entry
->file
);
1139 config_add_entry(config
, entry
);
1142 static VOID
config_load_defaults(Config
*config
, EFI_FILE
*root_dir
) {
1143 CHAR8
*content
= NULL
;
1148 len
= file_read(root_dir
, L
"\\loader\\loader.conf", 0, 0, &content
);
1150 config_defaults_load_from_file(config
, content
);
1153 err
= efivar_get_int(L
"LoaderConfigTimeout", &sec
);
1154 if (!EFI_ERROR(err
)) {
1155 config
->timeout_sec_efivar
= sec
;
1156 config
->timeout_sec
= sec
;
1158 config
->timeout_sec_efivar
= -1;
1161 static VOID
config_load_entries(Config
*config
, EFI_HANDLE
*device
, EFI_FILE
*root_dir
, CHAR16
*loaded_image_path
) {
1162 EFI_FILE_HANDLE entries_dir
;
1165 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &entries_dir
, L
"\\loader\\entries", EFI_FILE_MODE_READ
, 0ULL);
1166 if (!EFI_ERROR(err
)) {
1171 CHAR8
*content
= NULL
;
1174 bufsize
= sizeof(buf
);
1175 err
= uefi_call_wrapper(entries_dir
->Read
, 3, entries_dir
, &bufsize
, buf
);
1176 if (bufsize
== 0 || EFI_ERROR(err
))
1179 f
= (EFI_FILE_INFO
*) buf
;
1180 if (f
->FileName
[0] == '.')
1182 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1185 len
= StrLen(f
->FileName
);
1188 if (StriCmp(f
->FileName
+ len
- 5, L
".conf") != 0)
1190 if (StrnCmp(f
->FileName
, L
"auto-", 5) == 0)
1193 len
= file_read(entries_dir
, f
->FileName
, 0, 0, &content
);
1195 config_entry_add_from_file(config
, device
, f
->FileName
, content
, loaded_image_path
);
1198 uefi_call_wrapper(entries_dir
->Close
, 1, entries_dir
);
1202 static VOID
config_sort_entries(Config
*config
) {
1205 for (i
= 1; i
< config
->entry_count
; i
++) {
1210 for (k
= 0; k
< config
->entry_count
- i
; k
++) {
1213 if (str_verscmp(config
->entries
[k
]->file
, config
->entries
[k
+1]->file
) <= 0)
1215 entry
= config
->entries
[k
];
1216 config
->entries
[k
] = config
->entries
[k
+1];
1217 config
->entries
[k
+1] = entry
;
1225 static VOID
config_default_entry_select(Config
*config
) {
1231 * The EFI variable to specify a boot entry for the next, and only the
1232 * next reboot. The variable is always cleared directly after it is read.
1234 err
= efivar_get(L
"LoaderEntryOneShot", &var
);
1235 if (!EFI_ERROR(err
)) {
1236 BOOLEAN found
= FALSE
;
1238 for (i
= 0; i
< config
->entry_count
; i
++) {
1239 if (StrCmp(config
->entries
[i
]->file
, var
) == 0) {
1240 config
->idx_default
= i
;
1246 config
->entry_oneshot
= StrDuplicate(var
);
1247 efivar_set(L
"LoaderEntryOneShot", NULL
, TRUE
);
1254 * The EFI variable to select the default boot entry overrides the
1255 * configured pattern. The variable can be set and cleared by pressing
1256 * the 'd' key in the loader selection menu, the entry is marked with
1259 err
= efivar_get(L
"LoaderEntryDefault", &var
);
1260 if (!EFI_ERROR(err
)) {
1261 BOOLEAN found
= FALSE
;
1263 for (i
= 0; i
< config
->entry_count
; i
++) {
1264 if (StrCmp(config
->entries
[i
]->file
, var
) == 0) {
1265 config
->idx_default
= i
;
1266 config
->idx_default_efivar
= i
;
1275 config
->idx_default_efivar
= -1;
1277 if (config
->entry_count
== 0)
1281 * Match the pattern from the end of the list to the start, find last
1282 * entry (largest number) matching the given pattern.
1284 if (config
->entry_default_pattern
) {
1285 i
= config
->entry_count
;
1287 if (config
->entries
[i
]->no_autoselect
)
1289 if (MetaiMatch(config
->entries
[i
]->file
, config
->entry_default_pattern
)) {
1290 config
->idx_default
= i
;
1296 /* select the last suitable entry */
1297 i
= config
->entry_count
;
1299 if (config
->entries
[i
]->no_autoselect
)
1301 config
->idx_default
= i
;
1305 /* no entry found */
1306 config
->idx_default
= -1;
1309 /* generate a unique title, avoiding non-distinguishable menu entries */
1310 static VOID
config_title_generate(Config
*config
) {
1315 for (i
= 0; i
< config
->entry_count
; i
++) {
1318 FreePool(config
->entries
[i
]->title_show
);
1319 title
= config
->entries
[i
]->title
;
1321 title
= config
->entries
[i
]->file
;
1322 config
->entries
[i
]->title_show
= StrDuplicate(title
);
1326 for (i
= 0; i
< config
->entry_count
; i
++) {
1327 for (k
= 0; k
< config
->entry_count
; k
++) {
1330 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1334 config
->entries
[i
]->non_unique
= TRUE
;
1335 config
->entries
[k
]->non_unique
= TRUE
;
1341 /* add version to non-unique titles */
1342 for (i
= 0; i
< config
->entry_count
; i
++) {
1345 if (!config
->entries
[i
]->non_unique
)
1347 if (!config
->entries
[i
]->version
)
1350 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->version
);
1351 FreePool(config
->entries
[i
]->title_show
);
1352 config
->entries
[i
]->title_show
= s
;
1353 config
->entries
[i
]->non_unique
= FALSE
;
1357 for (i
= 0; i
< config
->entry_count
; i
++) {
1358 for (k
= 0; k
< config
->entry_count
; k
++) {
1361 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1365 config
->entries
[i
]->non_unique
= TRUE
;
1366 config
->entries
[k
]->non_unique
= TRUE
;
1372 /* add machine-id to non-unique titles */
1373 for (i
= 0; i
< config
->entry_count
; i
++) {
1377 if (!config
->entries
[i
]->non_unique
)
1379 if (!config
->entries
[i
]->machine_id
)
1382 m
= StrDuplicate(config
->entries
[i
]->machine_id
);
1384 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, m
);
1385 FreePool(config
->entries
[i
]->title_show
);
1386 config
->entries
[i
]->title_show
= s
;
1387 config
->entries
[i
]->non_unique
= FALSE
;
1392 for (i
= 0; i
< config
->entry_count
; i
++) {
1393 for (k
= 0; k
< config
->entry_count
; k
++) {
1396 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1400 config
->entries
[i
]->non_unique
= TRUE
;
1401 config
->entries
[k
]->non_unique
= TRUE
;
1407 /* add file name to non-unique titles */
1408 for (i
= 0; i
< config
->entry_count
; i
++) {
1411 if (!config
->entries
[i
]->non_unique
)
1413 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->file
);
1414 FreePool(config
->entries
[i
]->title_show
);
1415 config
->entries
[i
]->title_show
= s
;
1416 config
->entries
[i
]->non_unique
= FALSE
;
1420 static BOOLEAN
config_entry_add_call(Config
*config
, CHAR16
*title
, EFI_STATUS (*call
)(VOID
)) {
1423 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1424 entry
->title
= StrDuplicate(title
);
1426 entry
->no_autoselect
= TRUE
;
1427 config_add_entry(config
, entry
);
1431 static ConfigEntry
*config_entry_add_loader(Config
*config
, EFI_HANDLE
*device
,
1432 enum loader_type type
,CHAR16
*file
, CHAR16 key
, CHAR16
*title
, CHAR16
*loader
) {
1435 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1437 entry
->title
= StrDuplicate(title
);
1438 entry
->device
= device
;
1439 entry
->loader
= StrDuplicate(loader
);
1440 entry
->file
= StrDuplicate(file
);
1441 StrLwr(entry
->file
);
1443 config_add_entry(config
, entry
);
1448 static BOOLEAN
config_entry_add_loader_auto(Config
*config
, EFI_HANDLE
*device
, EFI_FILE
*root_dir
, CHAR16
*loaded_image_path
,
1449 CHAR16
*file
, CHAR16 key
, CHAR16
*title
, CHAR16
*loader
) {
1450 EFI_FILE_HANDLE handle
;
1454 /* do not add an entry for ourselves */
1455 if (loaded_image_path
&& StriCmp(loader
, loaded_image_path
) == 0)
1458 /* check existence */
1459 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, loader
, EFI_FILE_MODE_READ
, 0ULL);
1462 uefi_call_wrapper(handle
->Close
, 1, handle
);
1464 entry
= config_entry_add_loader(config
, device
, LOADER_UNDEFINED
, file
, key
, title
, loader
);
1468 /* do not boot right away into auto-detected entries */
1469 entry
->no_autoselect
= TRUE
;
1474 static VOID
config_entry_add_osx(Config
*config
) {
1476 UINTN handle_count
= 0;
1477 EFI_HANDLE
*handles
= NULL
;
1479 err
= LibLocateHandle(ByProtocol
, &FileSystemProtocol
, NULL
, &handle_count
, &handles
);
1480 if (!EFI_ERROR(err
)) {
1483 for (i
= 0; i
< handle_count
; i
++) {
1487 root
= LibOpenRoot(handles
[i
]);
1490 found
= config_entry_add_loader_auto(config
, handles
[i
], root
, NULL
, L
"auto-osx", 'a', L
"macOS",
1491 L
"\\System\\Library\\CoreServices\\boot.efi");
1492 uefi_call_wrapper(root
->Close
, 1, root
);
1501 static VOID
config_entry_add_linux( Config
*config
, EFI_LOADED_IMAGE
*loaded_image
, EFI_FILE
*root_dir
) {
1502 EFI_FILE_HANDLE linux_dir
;
1506 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &linux_dir
, L
"\\EFI\\Linux", EFI_FILE_MODE_READ
, 0ULL);
1507 if (!EFI_ERROR(err
)) {
1512 CHAR8
*sections
[] = {
1514 (UINT8
*)".cmdline",
1517 UINTN offs
[ELEMENTSOF(sections
)-1] = {};
1518 UINTN szs
[ELEMENTSOF(sections
)-1] = {};
1519 UINTN addrs
[ELEMENTSOF(sections
)-1] = {};
1520 CHAR8
*content
= NULL
;
1525 CHAR16
*os_name
= NULL
;
1526 CHAR16
*os_id
= NULL
;
1527 CHAR16
*os_version
= NULL
;
1528 CHAR16
*os_build
= NULL
;
1530 bufsize
= sizeof(buf
);
1531 err
= uefi_call_wrapper(linux_dir
->Read
, 3, linux_dir
, &bufsize
, buf
);
1532 if (bufsize
== 0 || EFI_ERROR(err
))
1535 f
= (EFI_FILE_INFO
*) buf
;
1536 if (f
->FileName
[0] == '.')
1538 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1540 len
= StrLen(f
->FileName
);
1543 if (StriCmp(f
->FileName
+ len
- 4, L
".efi") != 0)
1546 /* look for .osrel and .cmdline sections in the .efi binary */
1547 err
= pe_file_locate_sections(linux_dir
, f
->FileName
, sections
, addrs
, offs
, szs
);
1551 len
= file_read(linux_dir
, f
->FileName
, offs
[0], szs
[0], &content
);
1555 /* read properties from the embedded os-release file */
1557 while ((line
= line_get_key_value(content
, (CHAR8
*)"=", &pos
, &key
, &value
))) {
1558 if (strcmpa((CHAR8
*)"PRETTY_NAME", key
) == 0) {
1560 os_name
= stra_to_str(value
);
1564 if (strcmpa((CHAR8
*)"ID", key
) == 0) {
1566 os_id
= stra_to_str(value
);
1570 if (strcmpa((CHAR8
*)"VERSION_ID", key
) == 0) {
1571 FreePool(os_version
);
1572 os_version
= stra_to_str(value
);
1576 if (strcmpa((CHAR8
*)"BUILD_ID", key
) == 0) {
1578 os_build
= stra_to_str(value
);
1583 if (os_name
&& os_id
&& (os_version
|| os_build
)) {
1588 conf
= PoolPrint(L
"%s-%s", os_id
, os_version
? : os_build
);
1589 path
= PoolPrint(L
"\\EFI\\Linux\\%s", f
->FileName
);
1590 entry
= config_entry_add_loader(config
, loaded_image
->DeviceHandle
, LOADER_LINUX
, conf
, 'l', os_name
, path
);
1593 /* read the embedded cmdline file */
1594 len
= file_read(linux_dir
, f
->FileName
, offs
[1], szs
[1] - 1 , &content
);
1596 cmdline
= stra_to_str(content
);
1597 entry
->options
= cmdline
;
1607 FreePool(os_version
);
1611 uefi_call_wrapper(linux_dir
->Close
, 1, linux_dir
);
1615 static EFI_STATUS
image_start(EFI_HANDLE parent_image
, const Config
*config
, const ConfigEntry
*entry
) {
1617 EFI_DEVICE_PATH
*path
;
1621 path
= FileDevicePath(entry
->device
, entry
->loader
);
1623 Print(L
"Error getting device path.");
1624 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1625 return EFI_INVALID_PARAMETER
;
1628 err
= uefi_call_wrapper(BS
->LoadImage
, 6, FALSE
, parent_image
, path
, NULL
, 0, &image
);
1629 if (EFI_ERROR(err
)) {
1630 Print(L
"Error loading %s: %r", entry
->loader
, err
);
1631 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1635 if (config
->options_edit
)
1636 options
= config
->options_edit
;
1637 else if (entry
->options
)
1638 options
= entry
->options
;
1642 EFI_LOADED_IMAGE
*loaded_image
;
1644 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
1645 parent_image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1646 if (EFI_ERROR(err
)) {
1647 Print(L
"Error getting LoadedImageProtocol handle: %r", err
);
1648 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1651 loaded_image
->LoadOptions
= options
;
1652 loaded_image
->LoadOptionsSize
= (StrLen(loaded_image
->LoadOptions
)+1) * sizeof(CHAR16
);
1655 /* Try to log any options to the TPM, especially to catch manually edited options */
1656 err
= tpm_log_event(SD_TPM_PCR
,
1657 (EFI_PHYSICAL_ADDRESS
) loaded_image
->LoadOptions
,
1658 loaded_image
->LoadOptionsSize
, loaded_image
->LoadOptions
);
1659 if (EFI_ERROR(err
)) {
1660 Print(L
"Unable to add image options measurement: %r", err
);
1661 uefi_call_wrapper(BS
->Stall
, 1, 200 * 1000);
1666 efivar_set_time_usec(L
"LoaderTimeExecUSec", 0);
1667 err
= uefi_call_wrapper(BS
->StartImage
, 3, image
, NULL
, NULL
);
1669 uefi_call_wrapper(BS
->UnloadImage
, 1, image
);
1675 static EFI_STATUS
reboot_into_firmware(VOID
) {
1681 osind
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
1683 err
= efivar_get_raw(&global_guid
, L
"OsIndications", &b
, &size
);
1684 if (!EFI_ERROR(err
))
1685 osind
|= (UINT64
)*b
;
1688 err
= efivar_set_raw(&global_guid
, L
"OsIndications", (CHAR8
*)&osind
, sizeof(UINT64
), TRUE
);
1692 err
= uefi_call_wrapper(RT
->ResetSystem
, 4, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1693 Print(L
"Error calling ResetSystem: %r", err
);
1694 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1698 static VOID
config_free(Config
*config
) {
1701 for (i
= 0; i
< config
->entry_count
; i
++)
1702 config_entry_free(config
->entries
[i
]);
1703 FreePool(config
->entries
);
1704 FreePool(config
->entry_default_pattern
);
1705 FreePool(config
->options_edit
);
1706 FreePool(config
->entry_oneshot
);
1709 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*sys_table
) {
1713 EFI_LOADED_IMAGE
*loaded_image
;
1715 CHAR16
*loaded_image_path
;
1719 BOOLEAN menu
= FALSE
;
1722 InitializeLib(image
, sys_table
);
1723 init_usec
= time_usec();
1724 efivar_set_time_usec(L
"LoaderTimeInitUSec", init_usec
);
1725 efivar_set(L
"LoaderInfo", L
"systemd-boot " PACKAGE_VERSION
, FALSE
);
1726 s
= PoolPrint(L
"%s %d.%02d", ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
1727 efivar_set(L
"LoaderFirmwareInfo", s
, FALSE
);
1729 s
= PoolPrint(L
"UEFI %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
1730 efivar_set(L
"LoaderFirmwareType", s
, FALSE
);
1733 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
1734 image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1735 if (EFI_ERROR(err
)) {
1736 Print(L
"Error getting a LoadedImageProtocol handle: %r ", err
);
1737 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1741 /* export the device path this image is started from */
1742 if (disk_get_part_uuid(loaded_image
->DeviceHandle
, uuid
) == EFI_SUCCESS
)
1743 efivar_set(L
"LoaderDevicePartUUID", uuid
, FALSE
);
1745 root_dir
= LibOpenRoot(loaded_image
->DeviceHandle
);
1747 Print(L
"Unable to open root directory: %r ", err
);
1748 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1749 return EFI_LOAD_ERROR
;
1752 if (secure_boot_enabled() && shim_loaded()) {
1753 err
= security_policy_install();
1754 if (EFI_ERROR(err
)) {
1755 Print(L
"Error installing security policy: %r ", err
);
1756 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1761 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1762 loaded_image_path
= DevicePathToStr(loaded_image
->FilePath
);
1763 efivar_set(L
"LoaderImageIdentifier", loaded_image_path
, FALSE
);
1765 ZeroMem(&config
, sizeof(Config
));
1766 config_load_defaults(&config
, root_dir
);
1768 /* scan /EFI/Linux/ directory */
1769 config_entry_add_linux(&config
, loaded_image
, root_dir
);
1771 /* scan /loader/entries/\*.conf files */
1772 config_load_entries(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
);
1774 /* sort entries after version number */
1775 config_sort_entries(&config
);
1777 /* if we find some well-known loaders, add them to the end of the list */
1778 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1779 L
"auto-windows", 'w', L
"Windows Boot Manager", L
"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1780 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1781 L
"auto-efi-shell", 's', L
"EFI Shell", L
"\\shell" EFI_MACHINE_TYPE_NAME
".efi");
1782 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1783 L
"auto-efi-default", '\0', L
"EFI Default Loader", L
"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME
".efi");
1784 config_entry_add_osx(&config
);
1786 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
1787 UINT64 osind
= (UINT64
)*b
;
1789 if (osind
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
)
1790 config_entry_add_call(&config
, L
"Reboot Into Firmware Interface", reboot_into_firmware
);
1794 if (config
.entry_count
== 0) {
1795 Print(L
"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1796 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1800 config_title_generate(&config
);
1802 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */
1803 config_default_entry_select(&config
);
1805 /* if no configured entry to select from was found, enable the menu */
1806 if (config
.idx_default
== -1) {
1807 config
.idx_default
= 0;
1808 if (config
.timeout_sec
== 0)
1809 config
.timeout_sec
= 10;
1812 /* select entry or show menu when key is pressed or timeout is set */
1813 if (config
.timeout_sec
== 0) {
1816 err
= console_key_read(&key
, FALSE
);
1817 if (!EFI_ERROR(err
)) {
1820 /* find matching key in config entries */
1821 idx
= entry_lookup_key(&config
, config
.idx_default
, KEYCHAR(key
));
1823 config
.idx_default
= idx
;
1833 entry
= config
.entries
[config
.idx_default
];
1835 efivar_set_time_usec(L
"LoaderTimeMenuUSec", 0);
1836 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 0, 0x10000, 0, NULL
);
1837 if (!menu_run(&config
, &entry
, loaded_image_path
))
1840 /* run special entry like "reboot" */
1847 /* export the selected boot entry to the system */
1848 efivar_set(L
"LoaderEntrySelected", entry
->file
, FALSE
);
1850 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 5 * 60, 0x10000, 0, NULL
);
1851 err
= image_start(image
, &config
, entry
);
1852 if (EFI_ERROR(err
)) {
1853 graphics_mode(FALSE
);
1854 Print(L
"\nFailed to execute %s (%s): %r\n", entry
->title
, entry
->loader
, err
);
1855 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1860 config
.timeout_sec
= 0;
1864 FreePool(loaded_image_path
);
1865 config_free(&config
);
1866 uefi_call_wrapper(root_dir
->Close
, 1, root_dir
);
1867 uefi_call_wrapper(BS
->CloseProtocol
, 4, image
, &LoadedImageProtocol
, image
, NULL
);