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
;
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
;
68 CHAR16
*entry_oneshot
;
73 static VOID
cursor_left(UINTN
*cursor
, UINTN
*first
) {
76 else if ((*first
) > 0)
80 static VOID
cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
) {
81 if ((*cursor
)+1 < x_max
)
83 else if ((*first
) + (*cursor
) < len
)
87 static BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
, UINTN y_pos
) {
100 size
= StrLen(line_in
) + 1024;
101 line
= AllocatePool(size
* sizeof(CHAR16
));
102 StrCpy(line
, line_in
);
104 print
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
106 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, TRUE
);
121 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
122 while (clear
> 0 && i
< x_max
-1) {
128 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_pos
);
129 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, print
);
130 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
132 err
= console_key_read(&key
, TRUE
);
137 case KEYPRESS(0, SCAN_ESC
, 0):
138 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'c'):
139 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'g'):
140 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('c')):
141 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('g')):
145 case KEYPRESS(0, SCAN_HOME
, 0):
146 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'a'):
147 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('a')):
148 /* beginning-of-line */
153 case KEYPRESS(0, SCAN_END
, 0):
154 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'e'):
155 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('e')):
157 cursor
= len
- first
;
158 if (cursor
+1 >= x_max
) {
160 first
= len
- (x_max
-1);
164 case KEYPRESS(0, SCAN_DOWN
, 0):
165 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'f'):
166 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_RIGHT
, 0):
168 while (line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
169 cursor_right(&cursor
, &first
, x_max
, len
);
170 while (line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
171 cursor_right(&cursor
, &first
, x_max
, len
);
172 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
175 case KEYPRESS(0, SCAN_UP
, 0):
176 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'b'):
177 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_LEFT
, 0):
179 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
180 cursor_left(&cursor
, &first
);
181 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ')
182 cursor_left(&cursor
, &first
);
184 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ')
185 cursor_left(&cursor
, &first
);
186 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
189 case KEYPRESS(0, SCAN_RIGHT
, 0):
190 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'f'):
191 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('f')):
193 if (first
+ cursor
== len
)
195 cursor_right(&cursor
, &first
, x_max
, len
);
196 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
199 case KEYPRESS(0, SCAN_LEFT
, 0):
200 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'b'):
201 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('b')):
203 cursor_left(&cursor
, &first
);
204 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
207 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'd'):
210 for (i
= first
+ cursor
; i
< len
&& line
[i
] == ' '; i
++)
212 for (; i
< len
&& line
[i
] != ' '; i
++)
215 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
216 line
[i
] = line
[i
+ clear
];
221 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'w'):
222 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('w')):
223 case KEYPRESS(EFI_ALT_PRESSED
, 0, CHAR_BACKSPACE
):
224 /* backward-kill-word */
226 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
227 cursor_left(&cursor
, &first
);
229 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ') {
230 cursor_left(&cursor
, &first
);
234 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ') {
235 cursor_left(&cursor
, &first
);
238 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
240 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
241 line
[i
] = line
[i
+ clear
];
246 case KEYPRESS(0, SCAN_DELETE
, 0):
247 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'd'):
248 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('d')):
251 if (first
+ cursor
== len
)
253 for (i
= first
+ cursor
; i
< len
; i
++)
259 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'k'):
260 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('k')):
262 line
[first
+ cursor
] = '\0';
263 clear
= len
- (first
+ cursor
);
264 len
= first
+ cursor
;
267 case KEYPRESS(0, 0, CHAR_LINEFEED
):
268 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
269 if (StrCmp(line
, line_in
) != 0) {
277 case KEYPRESS(0, 0, CHAR_BACKSPACE
):
280 if (first
== 0 && cursor
== 0)
282 for (i
= first
+ cursor
-1; i
< len
; i
++)
288 if (cursor
> 0 || first
== 0)
290 /* show full line if it fits */
296 /* jump left to see what we delete */
306 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
307 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
310 for (i
= len
; i
> first
+ cursor
; i
--)
312 line
[first
+ cursor
] = KEYCHAR(key
);
315 if (cursor
+1 < x_max
)
317 else if (first
+ cursor
< len
)
323 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
329 static UINTN
entry_lookup_key(Config
*config
, UINTN start
, CHAR16 key
) {
335 /* select entry by number key */
336 if (key
>= '1' && key
<= '9') {
338 if (i
> config
->entry_count
)
339 i
= config
->entry_count
;
343 /* find matching key in config entries */
344 for (i
= start
; i
< config
->entry_count
; i
++)
345 if (config
->entries
[i
]->key
== key
)
348 for (i
= 0; i
< start
; i
++)
349 if (config
->entries
[i
]->key
== key
)
355 static VOID
print_status(Config
*config
, CHAR16
*loaded_image_path
) {
364 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
365 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
367 Print(L
"systemd-boot version: " VERSION
"\n");
368 Print(L
"architecture: " EFI_MACHINE_TYPE_NAME
"\n");
369 Print(L
"loaded image: %s\n", loaded_image_path
);
370 Print(L
"UEFI specification: %d.%02d\n", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
371 Print(L
"firmware vendor: %s\n", ST
->FirmwareVendor
);
372 Print(L
"firmware version: %d.%02d\n", ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
374 if (uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x
, &y
) == EFI_SUCCESS
)
375 Print(L
"console size: %d x %d\n", x
, y
);
377 if (efivar_get_raw(&global_guid
, L
"SecureBoot", &b
, &size
) == EFI_SUCCESS
) {
378 Print(L
"SecureBoot: %s\n", yes_no(*b
> 0));
382 if (efivar_get_raw(&global_guid
, L
"SetupMode", &b
, &size
) == EFI_SUCCESS
) {
383 Print(L
"SetupMode: %s\n", *b
> 0 ? L
"setup" : L
"user");
387 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
388 Print(L
"OsIndicationsSupported: %d\n", (UINT64
)*b
);
393 Print(L
"timeout: %d\n", config
->timeout_sec
);
394 if (config
->timeout_sec_efivar
>= 0)
395 Print(L
"timeout (EFI var): %d\n", config
->timeout_sec_efivar
);
396 Print(L
"timeout (config): %d\n", config
->timeout_sec_config
);
397 if (config
->entry_default_pattern
)
398 Print(L
"default pattern: '%s'\n", config
->entry_default_pattern
);
399 Print(L
"editor: %s\n", yes_no(!config
->no_editor
));
402 Print(L
"config entry count: %d\n", config
->entry_count
);
403 Print(L
"entry selected idx: %d\n", config
->idx_default
);
404 if (config
->idx_default_efivar
>= 0)
405 Print(L
"entry EFI var idx: %d\n", config
->idx_default_efivar
);
408 if (efivar_get_int(L
"LoaderConfigTimeout", &i
) == EFI_SUCCESS
)
409 Print(L
"LoaderConfigTimeout: %d\n", i
);
410 if (config
->entry_oneshot
)
411 Print(L
"LoaderEntryOneShot: %s\n", config
->entry_oneshot
);
412 if (efivar_get(L
"LoaderDevicePartUUID", &s
) == EFI_SUCCESS
) {
413 Print(L
"LoaderDevicePartUUID: %s\n", s
);
416 if (efivar_get(L
"LoaderEntryDefault", &s
) == EFI_SUCCESS
) {
417 Print(L
"LoaderEntryDefault: %s\n", s
);
421 Print(L
"\n--- press key ---\n\n");
422 console_key_read(&key
, TRUE
);
424 for (i
= 0; i
< config
->entry_count
; i
++) {
427 if (key
== KEYPRESS(0, SCAN_ESC
, 0) || key
== KEYPRESS(0, 0, 'q'))
430 entry
= config
->entries
[i
];
431 Print(L
"config entry: %d/%d\n", i
+1, config
->entry_count
);
433 Print(L
"file '%s'\n", entry
->file
);
434 Print(L
"title show '%s'\n", entry
->title_show
);
436 Print(L
"title '%s'\n", entry
->title
);
438 Print(L
"version '%s'\n", entry
->version
);
439 if (entry
->machine_id
)
440 Print(L
"machine-id '%s'\n", entry
->machine_id
);
442 EFI_DEVICE_PATH
*device_path
;
445 device_path
= DevicePathFromHandle(entry
->device
);
447 str
= DevicePathToStr(device_path
);
448 Print(L
"device handle '%s'\n", str
);
453 Print(L
"loader '%s'\n", entry
->loader
);
455 Print(L
"options '%s'\n", entry
->options
);
456 Print(L
"auto-select %s\n", yes_no(!entry
->no_autoselect
));
458 Print(L
"internal call yes\n");
460 Print(L
"\n--- press key ---\n\n");
461 console_key_read(&key
, TRUE
);
464 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
467 static BOOLEAN
menu_run(Config
*config
, ConfigEntry
**chosen_entry
, CHAR16
*loaded_image_path
) {
471 UINTN idx_highlight_prev
;
487 BOOLEAN exit
= FALSE
;
489 BOOLEAN wait
= FALSE
;
491 graphics_mode(FALSE
);
492 uefi_call_wrapper(ST
->ConIn
->Reset
, 2, ST
->ConIn
, FALSE
);
493 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
494 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
496 /* draw a single character to make ClearScreen work on some firmware */
497 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
" ");
498 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
500 err
= uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
501 if (EFI_ERROR(err
)) {
506 /* we check 10 times per second for a keystroke */
507 if (config
->timeout_sec
> 0)
508 timeout_remain
= config
->timeout_sec
* 10;
512 idx_highlight
= config
->idx_default
;
513 idx_highlight_prev
= 0;
515 visible_max
= y_max
- 2;
517 if ((UINTN
)config
->idx_default
>= visible_max
)
518 idx_first
= config
->idx_default
-1;
522 idx_last
= idx_first
+ visible_max
-1;
527 /* length of the longest entry */
529 for (i
= 0; i
< config
->entry_count
; i
++) {
532 entry_len
= StrLen(config
->entries
[i
]->title_show
);
533 if (line_width
< entry_len
)
534 line_width
= entry_len
;
536 if (line_width
> x_max
-6)
537 line_width
= x_max
-6;
539 /* offsets to center the entries on the screen */
540 x_start
= (x_max
- (line_width
)) / 2;
541 if (config
->entry_count
< visible_max
)
542 y_start
= ((visible_max
- config
->entry_count
) / 2) + 1;
546 /* menu entries title lines */
547 lines
= AllocatePool(sizeof(CHAR16
*) * config
->entry_count
);
548 for (i
= 0; i
< config
->entry_count
; i
++) {
551 lines
[i
] = AllocatePool(((x_max
+1) * sizeof(CHAR16
)));
552 for (j
= 0; j
< x_start
; j
++)
555 for (k
= 0; config
->entries
[i
]->title_show
[k
] != '\0' && j
< x_max
; j
++, k
++)
556 lines
[i
][j
] = config
->entries
[i
]->title_show
[k
];
558 for (; j
< x_max
; j
++)
560 lines
[i
][x_max
] = '\0';
564 clearline
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
565 for (i
= 0; i
< x_max
; i
++)
573 for (i
= 0; i
< config
->entry_count
; i
++) {
574 if (i
< idx_first
|| i
> idx_last
)
576 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ i
- idx_first
);
577 if (i
== idx_highlight
)
578 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
579 EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
581 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
582 EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
583 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[i
]);
584 if ((INTN
)i
== config
->idx_default_efivar
) {
585 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ i
- idx_first
);
586 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
590 } else if (highlight
) {
591 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight_prev
- idx_first
);
592 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
593 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight_prev
]);
594 if ((INTN
)idx_highlight_prev
== config
->idx_default_efivar
) {
595 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight_prev
- idx_first
);
596 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
599 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight
- idx_first
);
600 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
601 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight
]);
602 if ((INTN
)idx_highlight
== config
->idx_default_efivar
) {
603 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight
- idx_first
);
604 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
609 if (timeout_remain
> 0) {
611 status
= PoolPrint(L
"Boot in %d sec.", (timeout_remain
+ 5) / 10);
614 /* print status at last line of screen */
620 len
= StrLen(status
);
622 x
= (x_max
- len
) / 2;
625 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
626 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
627 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+ (x_max
- x
));
628 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, status
);
629 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1 + x
+ len
);
632 err
= console_key_read(&key
, wait
);
633 if (EFI_ERROR(err
)) {
634 /* timeout reached */
635 if (timeout_remain
== 0) {
640 /* sleep and update status */
641 if (timeout_remain
> 0) {
642 uefi_call_wrapper(BS
->Stall
, 1, 100 * 1000);
647 /* timeout disabled, wait for next key */
654 /* clear status after keystroke */
658 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
659 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
660 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
663 idx_highlight_prev
= idx_highlight
;
666 case KEYPRESS(0, SCAN_UP
, 0):
667 case KEYPRESS(0, 0, 'k'):
668 if (idx_highlight
> 0)
672 case KEYPRESS(0, SCAN_DOWN
, 0):
673 case KEYPRESS(0, 0, 'j'):
674 if (idx_highlight
< config
->entry_count
-1)
678 case KEYPRESS(0, SCAN_HOME
, 0):
679 case KEYPRESS(EFI_ALT_PRESSED
, 0, '<'):
680 if (idx_highlight
> 0) {
686 case KEYPRESS(0, SCAN_END
, 0):
687 case KEYPRESS(EFI_ALT_PRESSED
, 0, '>'):
688 if (idx_highlight
< config
->entry_count
-1) {
690 idx_highlight
= config
->entry_count
-1;
694 case KEYPRESS(0, SCAN_PAGE_UP
, 0):
695 if (idx_highlight
> visible_max
)
696 idx_highlight
-= visible_max
;
701 case KEYPRESS(0, SCAN_PAGE_DOWN
, 0):
702 idx_highlight
+= visible_max
;
703 if (idx_highlight
> config
->entry_count
-1)
704 idx_highlight
= config
->entry_count
-1;
707 case KEYPRESS(0, 0, CHAR_LINEFEED
):
708 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
712 case KEYPRESS(0, SCAN_F1
, 0):
713 case KEYPRESS(0, 0, 'h'):
714 case KEYPRESS(0, 0, '?'):
715 status
= StrDuplicate(L
"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
718 case KEYPRESS(0, 0, 'Q'):
723 case KEYPRESS(0, 0, 'd'):
724 if (config
->idx_default_efivar
!= (INTN
)idx_highlight
) {
725 /* store the selected entry in a persistent EFI variable */
726 efivar_set(L
"LoaderEntryDefault", config
->entries
[idx_highlight
]->file
, TRUE
);
727 config
->idx_default_efivar
= idx_highlight
;
728 status
= StrDuplicate(L
"Default boot entry selected.");
730 /* clear the default entry EFI variable */
731 efivar_set(L
"LoaderEntryDefault", NULL
, TRUE
);
732 config
->idx_default_efivar
= -1;
733 status
= StrDuplicate(L
"Default boot entry cleared.");
738 case KEYPRESS(0, 0, '-'):
739 case KEYPRESS(0, 0, 'T'):
740 if (config
->timeout_sec_efivar
> 0) {
741 config
->timeout_sec_efivar
--;
742 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
743 if (config
->timeout_sec_efivar
> 0)
744 status
= PoolPrint(L
"Menu timeout set to %d sec.", config
->timeout_sec_efivar
);
746 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
747 } else if (config
->timeout_sec_efivar
<= 0){
748 config
->timeout_sec_efivar
= -1;
749 efivar_set(L
"LoaderConfigTimeout", NULL
, TRUE
);
750 if (config
->timeout_sec_config
> 0)
751 status
= PoolPrint(L
"Menu timeout of %d sec is defined by configuration file.",
752 config
->timeout_sec_config
);
754 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
758 case KEYPRESS(0, 0, '+'):
759 case KEYPRESS(0, 0, 't'):
760 if (config
->timeout_sec_efivar
== -1 && config
->timeout_sec_config
== 0)
761 config
->timeout_sec_efivar
++;
762 config
->timeout_sec_efivar
++;
763 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
764 if (config
->timeout_sec_efivar
> 0)
765 status
= PoolPrint(L
"Menu timeout set to %d sec.",
766 config
->timeout_sec_efivar
);
768 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
771 case KEYPRESS(0, 0, 'e'):
772 /* only the options of configured entries can be edited */
773 if (config
->no_editor
|| config
->entries
[idx_highlight
]->type
== LOADER_UNDEFINED
)
775 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
776 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
777 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
778 if (line_edit(config
->entries
[idx_highlight
]->options
, &config
->options_edit
, x_max
-1, y_max
-1))
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);
784 case KEYPRESS(0, 0, 'v'):
785 status
= PoolPrint(L
"systemd-boot " VERSION
" (" EFI_MACHINE_TYPE_NAME
"), UEFI Specification %d.%02d, Vendor %s %d.%02d",
786 ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff,
787 ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
790 case KEYPRESS(0, 0, 'P'):
791 print_status(config
, loaded_image_path
);
795 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'l'):
796 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('l')):
801 /* jump with a hotkey directly to a matching entry */
802 idx
= entry_lookup_key(config
, idx_highlight
+1, KEYCHAR(key
));
809 if (idx_highlight
> idx_last
) {
810 idx_last
= idx_highlight
;
811 idx_first
= 1 + idx_highlight
- visible_max
;
813 } else if (idx_highlight
< idx_first
) {
814 idx_first
= idx_highlight
;
815 idx_last
= idx_highlight
+ visible_max
-1;
819 if (!refresh
&& idx_highlight
!= idx_highlight_prev
)
823 *chosen_entry
= config
->entries
[idx_highlight
];
825 for (i
= 0; i
< config
->entry_count
; i
++)
830 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_WHITE
|EFI_BACKGROUND_BLACK
);
831 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
835 static VOID
config_add_entry(Config
*config
, ConfigEntry
*entry
) {
836 if ((config
->entry_count
& 15) == 0) {
839 i
= config
->entry_count
+ 16;
840 if (config
->entry_count
== 0)
841 config
->entries
= AllocatePool(sizeof(VOID
*) * i
);
843 config
->entries
= ReallocatePool(config
->entries
,
844 sizeof(VOID
*) * config
->entry_count
, sizeof(VOID
*) * i
);
846 config
->entries
[config
->entry_count
++] = entry
;
849 static VOID
config_entry_free(ConfigEntry
*entry
) {
850 FreePool(entry
->title_show
);
851 FreePool(entry
->title
);
852 FreePool(entry
->machine_id
);
853 FreePool(entry
->loader
);
854 FreePool(entry
->options
);
857 static BOOLEAN
is_digit(CHAR16 c
) {
858 return (c
>= '0') && (c
<= '9');
861 static UINTN
c_order(CHAR16 c
) {
866 else if ((c
>= 'a') && (c
<= 'z'))
872 static INTN
str_verscmp(CHAR16
*s1
, CHAR16
*s2
) {
879 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
882 order
= c_order(*s1
) - c_order(*s2
);
895 while (is_digit(*s1
) && is_digit(*s2
)) {
911 return StrCmp(os1
, os2
);
914 static CHAR8
*line_get_key_value(CHAR8
*content
, CHAR8
*sep
, UINTN
*pos
, CHAR8
**key_ret
, CHAR8
**value_ret
) {
920 line
= content
+ *pos
;
925 while (line
[linelen
] && !strchra((CHAR8
*)"\n\r", line
[linelen
]))
928 /* move pos to next line */
938 line
[linelen
] = '\0';
940 /* remove leading whitespace */
941 while (strchra((CHAR8
*)" \t", *line
)) {
946 /* remove trailing whitespace */
947 while (linelen
> 0 && strchra(sep
, line
[linelen
-1]))
949 line
[linelen
] = '\0';
954 /* split key/value */
956 while (*value
&& !strchra(sep
, *value
))
962 while (*value
&& strchra(sep
, *value
))
966 if (value
[0] == '\"' && line
[linelen
-1] == '\"') {
968 line
[linelen
-1] = '\0';
976 static VOID
config_defaults_load_from_file(Config
*config
, CHAR8
*content
) {
982 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
983 if (strcmpa((CHAR8
*)"timeout", key
) == 0) {
986 s
= stra_to_str(value
);
987 config
->timeout_sec_config
= Atoi(s
);
988 config
->timeout_sec
= config
->timeout_sec_config
;
993 if (strcmpa((CHAR8
*)"default", key
) == 0) {
994 FreePool(config
->entry_default_pattern
);
995 config
->entry_default_pattern
= stra_to_str(value
);
996 StrLwr(config
->entry_default_pattern
);
1000 if (strcmpa((CHAR8
*)"editor", key
) == 0) {
1003 if (EFI_ERROR(parse_boolean(value
, &on
)))
1005 config
->no_editor
= !on
;
1010 static VOID
config_entry_add_from_file(Config
*config
, EFI_HANDLE
*device
, CHAR16
*file
, CHAR8
*content
, CHAR16
*loaded_image_path
) {
1016 CHAR16
*initrd
= NULL
;
1018 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1021 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1022 if (strcmpa((CHAR8
*)"title", key
) == 0) {
1023 FreePool(entry
->title
);
1024 entry
->title
= stra_to_str(value
);
1028 if (strcmpa((CHAR8
*)"version", key
) == 0) {
1029 FreePool(entry
->version
);
1030 entry
->version
= stra_to_str(value
);
1034 if (strcmpa((CHAR8
*)"machine-id", key
) == 0) {
1035 FreePool(entry
->machine_id
);
1036 entry
->machine_id
= stra_to_str(value
);
1040 if (strcmpa((CHAR8
*)"linux", key
) == 0) {
1041 FreePool(entry
->loader
);
1042 entry
->type
= LOADER_LINUX
;
1043 entry
->loader
= stra_to_path(value
);
1048 if (strcmpa((CHAR8
*)"efi", key
) == 0) {
1049 entry
->type
= LOADER_EFI
;
1050 FreePool(entry
->loader
);
1051 entry
->loader
= stra_to_path(value
);
1053 /* do not add an entry for ourselves */
1054 if (StriCmp(entry
->loader
, loaded_image_path
) == 0) {
1055 entry
->type
= LOADER_UNDEFINED
;
1061 if (strcmpa((CHAR8
*)"architecture", key
) == 0) {
1062 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1063 if (strcmpa((CHAR8
*)EFI_MACHINE_TYPE_NAME
, value
) != 0) {
1064 entry
->type
= LOADER_UNDEFINED
;
1070 if (strcmpa((CHAR8
*)"initrd", key
) == 0) {
1073 new = stra_to_path(value
);
1077 s
= PoolPrint(L
"%s initrd=%s", initrd
, new);
1081 initrd
= PoolPrint(L
"initrd=%s", new);
1086 if (strcmpa((CHAR8
*)"options", key
) == 0) {
1089 new = stra_to_str(value
);
1090 if (entry
->options
) {
1093 s
= PoolPrint(L
"%s %s", entry
->options
, new);
1094 FreePool(entry
->options
);
1097 entry
->options
= new;
1105 if (entry
->type
== LOADER_UNDEFINED
) {
1106 config_entry_free(entry
);
1112 /* add initrd= to options */
1113 if (entry
->type
== LOADER_LINUX
&& initrd
) {
1114 if (entry
->options
) {
1117 s
= PoolPrint(L
"%s %s", initrd
, entry
->options
);
1118 FreePool(entry
->options
);
1121 entry
->options
= initrd
;
1127 entry
->device
= device
;
1128 entry
->file
= StrDuplicate(file
);
1129 len
= StrLen(entry
->file
);
1130 /* remove ".conf" */
1132 entry
->file
[len
- 5] = '\0';
1133 StrLwr(entry
->file
);
1135 config_add_entry(config
, entry
);
1138 static VOID
config_load_defaults(Config
*config
, EFI_FILE
*root_dir
) {
1139 CHAR8
*content
= NULL
;
1144 len
= file_read(root_dir
, L
"\\loader\\loader.conf", 0, 0, &content
);
1146 config_defaults_load_from_file(config
, content
);
1149 err
= efivar_get_int(L
"LoaderConfigTimeout", &sec
);
1150 if (!EFI_ERROR(err
)) {
1151 config
->timeout_sec_efivar
= sec
;
1152 config
->timeout_sec
= sec
;
1154 config
->timeout_sec_efivar
= -1;
1157 static VOID
config_load_entries(Config
*config
, EFI_HANDLE
*device
, EFI_FILE
*root_dir
, CHAR16
*loaded_image_path
) {
1158 EFI_FILE_HANDLE entries_dir
;
1161 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &entries_dir
, L
"\\loader\\entries", EFI_FILE_MODE_READ
, 0ULL);
1162 if (!EFI_ERROR(err
)) {
1167 CHAR8
*content
= NULL
;
1170 bufsize
= sizeof(buf
);
1171 err
= uefi_call_wrapper(entries_dir
->Read
, 3, entries_dir
, &bufsize
, buf
);
1172 if (bufsize
== 0 || EFI_ERROR(err
))
1175 f
= (EFI_FILE_INFO
*) buf
;
1176 if (f
->FileName
[0] == '.')
1178 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1181 len
= StrLen(f
->FileName
);
1184 if (StriCmp(f
->FileName
+ len
- 5, L
".conf") != 0)
1186 if (StrnCmp(f
->FileName
, L
"auto-", 5) == 0)
1189 len
= file_read(entries_dir
, f
->FileName
, 0, 0, &content
);
1191 config_entry_add_from_file(config
, device
, f
->FileName
, content
, loaded_image_path
);
1194 uefi_call_wrapper(entries_dir
->Close
, 1, entries_dir
);
1198 static VOID
config_sort_entries(Config
*config
) {
1201 for (i
= 1; i
< config
->entry_count
; i
++) {
1206 for (k
= 0; k
< config
->entry_count
- i
; k
++) {
1209 if (str_verscmp(config
->entries
[k
]->file
, config
->entries
[k
+1]->file
) <= 0)
1211 entry
= config
->entries
[k
];
1212 config
->entries
[k
] = config
->entries
[k
+1];
1213 config
->entries
[k
+1] = entry
;
1221 static VOID
config_default_entry_select(Config
*config
) {
1227 * The EFI variable to specify a boot entry for the next, and only the
1228 * next reboot. The variable is always cleared directly after it is read.
1230 err
= efivar_get(L
"LoaderEntryOneShot", &var
);
1231 if (!EFI_ERROR(err
)) {
1232 BOOLEAN found
= FALSE
;
1234 for (i
= 0; i
< config
->entry_count
; i
++) {
1235 if (StrCmp(config
->entries
[i
]->file
, var
) == 0) {
1236 config
->idx_default
= i
;
1242 config
->entry_oneshot
= StrDuplicate(var
);
1243 efivar_set(L
"LoaderEntryOneShot", NULL
, TRUE
);
1250 * The EFI variable to select the default boot entry overrides the
1251 * configured pattern. The variable can be set and cleared by pressing
1252 * the 'd' key in the loader selection menu, the entry is marked with
1255 err
= efivar_get(L
"LoaderEntryDefault", &var
);
1256 if (!EFI_ERROR(err
)) {
1257 BOOLEAN found
= FALSE
;
1259 for (i
= 0; i
< config
->entry_count
; i
++) {
1260 if (StrCmp(config
->entries
[i
]->file
, var
) == 0) {
1261 config
->idx_default
= i
;
1262 config
->idx_default_efivar
= i
;
1271 config
->idx_default_efivar
= -1;
1273 if (config
->entry_count
== 0)
1277 * Match the pattern from the end of the list to the start, find last
1278 * entry (largest number) matching the given pattern.
1280 if (config
->entry_default_pattern
) {
1281 i
= config
->entry_count
;
1283 if (config
->entries
[i
]->no_autoselect
)
1285 if (MetaiMatch(config
->entries
[i
]->file
, config
->entry_default_pattern
)) {
1286 config
->idx_default
= i
;
1292 /* select the last suitable entry */
1293 i
= config
->entry_count
;
1295 if (config
->entries
[i
]->no_autoselect
)
1297 config
->idx_default
= i
;
1301 /* no entry found */
1302 config
->idx_default
= -1;
1305 /* generate a unique title, avoiding non-distinguishable menu entries */
1306 static VOID
config_title_generate(Config
*config
) {
1311 for (i
= 0; i
< config
->entry_count
; i
++) {
1314 FreePool(config
->entries
[i
]->title_show
);
1315 title
= config
->entries
[i
]->title
;
1317 title
= config
->entries
[i
]->file
;
1318 config
->entries
[i
]->title_show
= StrDuplicate(title
);
1322 for (i
= 0; i
< config
->entry_count
; i
++) {
1323 for (k
= 0; k
< config
->entry_count
; k
++) {
1326 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1330 config
->entries
[i
]->non_unique
= TRUE
;
1331 config
->entries
[k
]->non_unique
= TRUE
;
1337 /* add version to non-unique titles */
1338 for (i
= 0; i
< config
->entry_count
; i
++) {
1341 if (!config
->entries
[i
]->non_unique
)
1343 if (!config
->entries
[i
]->version
)
1346 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->version
);
1347 FreePool(config
->entries
[i
]->title_show
);
1348 config
->entries
[i
]->title_show
= s
;
1349 config
->entries
[i
]->non_unique
= FALSE
;
1353 for (i
= 0; i
< config
->entry_count
; i
++) {
1354 for (k
= 0; k
< config
->entry_count
; k
++) {
1357 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1361 config
->entries
[i
]->non_unique
= TRUE
;
1362 config
->entries
[k
]->non_unique
= TRUE
;
1368 /* add machine-id to non-unique titles */
1369 for (i
= 0; i
< config
->entry_count
; i
++) {
1373 if (!config
->entries
[i
]->non_unique
)
1375 if (!config
->entries
[i
]->machine_id
)
1378 m
= StrDuplicate(config
->entries
[i
]->machine_id
);
1380 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, m
);
1381 FreePool(config
->entries
[i
]->title_show
);
1382 config
->entries
[i
]->title_show
= s
;
1383 config
->entries
[i
]->non_unique
= FALSE
;
1388 for (i
= 0; i
< config
->entry_count
; i
++) {
1389 for (k
= 0; k
< config
->entry_count
; k
++) {
1392 if (StrCmp(config
->entries
[i
]->title_show
, config
->entries
[k
]->title_show
) != 0)
1396 config
->entries
[i
]->non_unique
= TRUE
;
1397 config
->entries
[k
]->non_unique
= TRUE
;
1403 /* add file name to non-unique titles */
1404 for (i
= 0; i
< config
->entry_count
; i
++) {
1407 if (!config
->entries
[i
]->non_unique
)
1409 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->file
);
1410 FreePool(config
->entries
[i
]->title_show
);
1411 config
->entries
[i
]->title_show
= s
;
1412 config
->entries
[i
]->non_unique
= FALSE
;
1416 static BOOLEAN
config_entry_add_call(Config
*config
, CHAR16
*title
, EFI_STATUS (*call
)(VOID
)) {
1419 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1420 entry
->title
= StrDuplicate(title
);
1422 entry
->no_autoselect
= TRUE
;
1423 config_add_entry(config
, entry
);
1427 static ConfigEntry
*config_entry_add_loader(Config
*config
, EFI_HANDLE
*device
,
1428 enum loader_type type
,CHAR16
*file
, CHAR16 key
, CHAR16
*title
, CHAR16
*loader
) {
1431 entry
= AllocateZeroPool(sizeof(ConfigEntry
));
1433 entry
->title
= StrDuplicate(title
);
1434 entry
->device
= device
;
1435 entry
->loader
= StrDuplicate(loader
);
1436 entry
->file
= StrDuplicate(file
);
1437 StrLwr(entry
->file
);
1439 config_add_entry(config
, entry
);
1444 static BOOLEAN
config_entry_add_loader_auto(Config
*config
, EFI_HANDLE
*device
, EFI_FILE
*root_dir
, CHAR16
*loaded_image_path
,
1445 CHAR16
*file
, CHAR16 key
, CHAR16
*title
, CHAR16
*loader
) {
1446 EFI_FILE_HANDLE handle
;
1450 /* do not add an entry for ourselves */
1451 if (loaded_image_path
&& StriCmp(loader
, loaded_image_path
) == 0)
1454 /* check existence */
1455 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, loader
, EFI_FILE_MODE_READ
, 0ULL);
1458 uefi_call_wrapper(handle
->Close
, 1, handle
);
1460 entry
= config_entry_add_loader(config
, device
, LOADER_UNDEFINED
, file
, key
, title
, loader
);
1464 /* do not boot right away into auto-detected entries */
1465 entry
->no_autoselect
= TRUE
;
1470 static VOID
config_entry_add_osx(Config
*config
) {
1472 UINTN handle_count
= 0;
1473 EFI_HANDLE
*handles
= NULL
;
1475 err
= LibLocateHandle(ByProtocol
, &FileSystemProtocol
, NULL
, &handle_count
, &handles
);
1476 if (!EFI_ERROR(err
)) {
1479 for (i
= 0; i
< handle_count
; i
++) {
1483 root
= LibOpenRoot(handles
[i
]);
1486 found
= config_entry_add_loader_auto(config
, handles
[i
], root
, NULL
, L
"auto-osx", 'a', L
"OS X",
1487 L
"\\System\\Library\\CoreServices\\boot.efi");
1488 uefi_call_wrapper(root
->Close
, 1, root
);
1497 static VOID
config_entry_add_linux( Config
*config
, EFI_LOADED_IMAGE
*loaded_image
, EFI_FILE
*root_dir
) {
1498 EFI_FILE_HANDLE linux_dir
;
1502 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &linux_dir
, L
"\\EFI\\Linux", EFI_FILE_MODE_READ
, 0ULL);
1503 if (!EFI_ERROR(err
)) {
1508 CHAR8
*sections
[] = {
1510 (UINT8
*)".cmdline",
1513 UINTN offs
[ELEMENTSOF(sections
)-1] = {};
1514 UINTN szs
[ELEMENTSOF(sections
)-1] = {};
1515 UINTN addrs
[ELEMENTSOF(sections
)-1] = {};
1516 CHAR8
*content
= NULL
;
1521 CHAR16
*os_name
= NULL
;
1522 CHAR16
*os_id
= NULL
;
1523 CHAR16
*os_version
= NULL
;
1524 CHAR16
*os_build
= NULL
;
1526 bufsize
= sizeof(buf
);
1527 err
= uefi_call_wrapper(linux_dir
->Read
, 3, linux_dir
, &bufsize
, buf
);
1528 if (bufsize
== 0 || EFI_ERROR(err
))
1531 f
= (EFI_FILE_INFO
*) buf
;
1532 if (f
->FileName
[0] == '.')
1534 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1536 len
= StrLen(f
->FileName
);
1539 if (StriCmp(f
->FileName
+ len
- 4, L
".efi") != 0)
1542 /* look for .osrel and .cmdline sections in the .efi binary */
1543 err
= pefile_locate_sections(linux_dir
, f
->FileName
, sections
, addrs
, offs
, szs
);
1547 len
= file_read(linux_dir
, f
->FileName
, offs
[0], szs
[0], &content
);
1551 /* read properties from the embedded os-release file */
1553 while ((line
= line_get_key_value(content
, (CHAR8
*)"=", &pos
, &key
, &value
))) {
1554 if (strcmpa((CHAR8
*)"PRETTY_NAME", key
) == 0) {
1556 os_name
= stra_to_str(value
);
1560 if (strcmpa((CHAR8
*)"ID", key
) == 0) {
1562 os_id
= stra_to_str(value
);
1566 if (strcmpa((CHAR8
*)"VERSION_ID", key
) == 0) {
1567 FreePool(os_version
);
1568 os_version
= stra_to_str(value
);
1572 if (strcmpa((CHAR8
*)"BUILD_ID", key
) == 0) {
1574 os_build
= stra_to_str(value
);
1579 if (os_name
&& os_id
&& (os_version
|| os_build
)) {
1584 conf
= PoolPrint(L
"%s-%s", os_id
, os_version
? : os_build
);
1585 path
= PoolPrint(L
"\\EFI\\Linux\\%s", f
->FileName
);
1586 entry
= config_entry_add_loader(config
, loaded_image
->DeviceHandle
, LOADER_LINUX
, conf
, 'l', os_name
, path
);
1589 /* read the embedded cmdline file */
1590 len
= file_read(linux_dir
, f
->FileName
, offs
[1], szs
[1] - 1 , &content
);
1592 cmdline
= stra_to_str(content
);
1593 entry
->options
= cmdline
;
1603 FreePool(os_version
);
1607 uefi_call_wrapper(linux_dir
->Close
, 1, linux_dir
);
1611 static EFI_STATUS
image_start(EFI_HANDLE parent_image
, const Config
*config
, const ConfigEntry
*entry
) {
1613 EFI_DEVICE_PATH
*path
;
1617 path
= FileDevicePath(entry
->device
, entry
->loader
);
1619 Print(L
"Error getting device path.");
1620 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1621 return EFI_INVALID_PARAMETER
;
1624 err
= uefi_call_wrapper(BS
->LoadImage
, 6, FALSE
, parent_image
, path
, NULL
, 0, &image
);
1625 if (EFI_ERROR(err
)) {
1626 Print(L
"Error loading %s: %r", entry
->loader
, err
);
1627 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1631 if (config
->options_edit
)
1632 options
= config
->options_edit
;
1633 else if (entry
->options
)
1634 options
= entry
->options
;
1638 EFI_LOADED_IMAGE
*loaded_image
;
1640 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
1641 parent_image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1642 if (EFI_ERROR(err
)) {
1643 Print(L
"Error getting LoadedImageProtocol handle: %r", err
);
1644 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1647 loaded_image
->LoadOptions
= options
;
1648 loaded_image
->LoadOptionsSize
= (StrLen(loaded_image
->LoadOptions
)+1) * sizeof(CHAR16
);
1651 efivar_set_time_usec(L
"LoaderTimeExecUSec", 0);
1652 err
= uefi_call_wrapper(BS
->StartImage
, 3, image
, NULL
, NULL
);
1654 uefi_call_wrapper(BS
->UnloadImage
, 1, image
);
1660 static EFI_STATUS
reboot_into_firmware(VOID
) {
1666 osind
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
1668 err
= efivar_get_raw(&global_guid
, L
"OsIndications", &b
, &size
);
1669 if (!EFI_ERROR(err
))
1670 osind
|= (UINT64
)*b
;
1673 err
= efivar_set_raw(&global_guid
, L
"OsIndications", (CHAR8
*)&osind
, sizeof(UINT64
), TRUE
);
1677 err
= uefi_call_wrapper(RT
->ResetSystem
, 4, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1678 Print(L
"Error calling ResetSystem: %r", err
);
1679 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1683 static VOID
config_free(Config
*config
) {
1686 for (i
= 0; i
< config
->entry_count
; i
++)
1687 config_entry_free(config
->entries
[i
]);
1688 FreePool(config
->entries
);
1689 FreePool(config
->entry_default_pattern
);
1690 FreePool(config
->options_edit
);
1691 FreePool(config
->entry_oneshot
);
1694 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*sys_table
) {
1698 EFI_LOADED_IMAGE
*loaded_image
;
1700 CHAR16
*loaded_image_path
;
1704 BOOLEAN menu
= FALSE
;
1707 InitializeLib(image
, sys_table
);
1708 init_usec
= time_usec();
1709 efivar_set_time_usec(L
"LoaderTimeInitUSec", init_usec
);
1710 efivar_set(L
"LoaderInfo", L
"systemd-boot " VERSION
, FALSE
);
1711 s
= PoolPrint(L
"%s %d.%02d", ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
1712 efivar_set(L
"LoaderFirmwareInfo", s
, FALSE
);
1714 s
= PoolPrint(L
"UEFI %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
1715 efivar_set(L
"LoaderFirmwareType", s
, FALSE
);
1718 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
1719 image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1720 if (EFI_ERROR(err
)) {
1721 Print(L
"Error getting a LoadedImageProtocol handle: %r ", err
);
1722 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1726 /* export the device path this image is started from */
1727 if (disk_get_part_uuid(loaded_image
->DeviceHandle
, uuid
) == EFI_SUCCESS
)
1728 efivar_set(L
"LoaderDevicePartUUID", uuid
, FALSE
);
1730 root_dir
= LibOpenRoot(loaded_image
->DeviceHandle
);
1732 Print(L
"Unable to open root directory: %r ", err
);
1733 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1734 return EFI_LOAD_ERROR
;
1738 /* the filesystem path to this image, to prevent adding ourselves to the menu */
1739 loaded_image_path
= DevicePathToStr(loaded_image
->FilePath
);
1740 efivar_set(L
"LoaderImageIdentifier", loaded_image_path
, FALSE
);
1742 ZeroMem(&config
, sizeof(Config
));
1743 config_load_defaults(&config
, root_dir
);
1745 /* scan /EFI/Linux/ directory */
1746 config_entry_add_linux(&config
, loaded_image
, root_dir
);
1748 /* scan /loader/entries/\*.conf files */
1749 config_load_entries(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
);
1751 /* sort entries after version number */
1752 config_sort_entries(&config
);
1754 /* if we find some well-known loaders, add them to the end of the list */
1755 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1756 L
"auto-windows", 'w', L
"Windows Boot Manager", L
"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
1757 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1758 L
"auto-efi-shell", 's', L
"EFI Shell", L
"\\shell" EFI_MACHINE_TYPE_NAME
".efi");
1759 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
1760 L
"auto-efi-default", '\0', L
"EFI Default Loader", L
"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME
".efi");
1761 config_entry_add_osx(&config
);
1763 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
1764 UINT64 osind
= (UINT64
)*b
;
1766 if (osind
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
)
1767 config_entry_add_call(&config
, L
"Reboot Into Firmware Interface", reboot_into_firmware
);
1771 if (config
.entry_count
== 0) {
1772 Print(L
"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
1773 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1777 config_title_generate(&config
);
1779 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/
1780 config_default_entry_select(&config
);
1782 /* if no configured entry to select from was found, enable the menu */
1783 if (config
.idx_default
== -1) {
1784 config
.idx_default
= 0;
1785 if (config
.timeout_sec
== 0)
1786 config
.timeout_sec
= 10;
1789 /* select entry or show menu when key is pressed or timeout is set */
1790 if (config
.timeout_sec
== 0) {
1793 err
= console_key_read(&key
, FALSE
);
1794 if (!EFI_ERROR(err
)) {
1797 /* find matching key in config entries */
1798 idx
= entry_lookup_key(&config
, config
.idx_default
, KEYCHAR(key
));
1800 config
.idx_default
= idx
;
1810 entry
= config
.entries
[config
.idx_default
];
1812 efivar_set_time_usec(L
"LoaderTimeMenuUSec", 0);
1813 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 0, 0x10000, 0, NULL
);
1814 if (!menu_run(&config
, &entry
, loaded_image_path
))
1817 /* run special entry like "reboot" */
1824 /* export the selected boot entry to the system */
1825 efivar_set(L
"LoaderEntrySelected", entry
->file
, FALSE
);
1827 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 5 * 60, 0x10000, 0, NULL
);
1828 err
= image_start(image
, &config
, entry
);
1829 if (EFI_ERROR(err
)) {
1830 graphics_mode(FALSE
);
1831 Print(L
"\nFailed to execute %s (%s): %r\n", entry
->title
, entry
->loader
, err
);
1832 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1837 config
.timeout_sec
= 0;
1841 FreePool(loaded_image_path
);
1842 config_free(&config
);
1843 uefi_call_wrapper(root_dir
->Close
, 1, root_dir
);
1844 uefi_call_wrapper(BS
->CloseProtocol
, 4, image
, &LoadedImageProtocol
, image
, NULL
);