1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "loader-features.h"
18 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
19 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
22 /* magic string to find in the binary image */
23 static const char __attribute__((used
)) magic
[] = "#### LoaderInfo: systemd-boot " GIT_VERSION
" ####";
25 static const EFI_GUID global_guid
= EFI_GLOBAL_VARIABLE
;
34 CHAR16
*id
; /* The identifier for this entry (note that this id is not necessarily unique though!) */
40 enum loader_type type
;
44 EFI_STATUS (*call
)(VOID
);
45 BOOLEAN no_autoselect
;
55 ConfigEntry
**entries
;
58 INTN idx_default_efivar
;
60 UINTN timeout_sec_config
;
61 INTN timeout_sec_efivar
;
62 CHAR16
*entry_default_pattern
;
63 CHAR16
*entry_oneshot
;
67 BOOLEAN auto_firmware
;
70 enum console_mode_change_type console_mode_change
;
73 static VOID
cursor_left(UINTN
*cursor
, UINTN
*first
) {
76 else if ((*first
) > 0)
80 static VOID
cursor_right(
86 if ((*cursor
)+1 < x_max
)
88 else if ((*first
) + (*cursor
) < len
)
92 static BOOLEAN
line_edit(
98 _cleanup_freepool_ CHAR16
*line
= NULL
, *print
= NULL
;
99 UINTN size
, len
, first
, cursor
, clear
;
104 size
= StrLen(line_in
) + 1024;
105 line
= AllocatePool(size
* sizeof(CHAR16
));
106 StrCpy(line
, line_in
);
108 print
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
110 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, TRUE
);
125 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
126 while (clear
> 0 && i
< x_max
-1) {
132 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_pos
);
133 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, print
);
134 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
136 err
= console_key_read(&key
, TRUE
);
141 case KEYPRESS(0, SCAN_ESC
, 0):
142 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'c'):
143 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'g'):
144 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('c')):
145 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('g')):
149 case KEYPRESS(0, SCAN_HOME
, 0):
150 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'a'):
151 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('a')):
152 /* beginning-of-line */
157 case KEYPRESS(0, SCAN_END
, 0):
158 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'e'):
159 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('e')):
161 cursor
= len
- first
;
162 if (cursor
+1 >= x_max
) {
164 first
= len
- (x_max
-1);
168 case KEYPRESS(0, SCAN_DOWN
, 0):
169 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'f'):
170 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_RIGHT
, 0):
172 while (line
[first
+ cursor
] == ' ')
173 cursor_right(&cursor
, &first
, x_max
, len
);
174 while (line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
175 cursor_right(&cursor
, &first
, x_max
, len
);
176 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
179 case KEYPRESS(0, SCAN_UP
, 0):
180 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'b'):
181 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_LEFT
, 0):
183 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
184 cursor_left(&cursor
, &first
);
185 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ')
186 cursor_left(&cursor
, &first
);
188 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ')
189 cursor_left(&cursor
, &first
);
190 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
193 case KEYPRESS(0, SCAN_RIGHT
, 0):
194 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'f'):
195 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('f')):
197 if (first
+ cursor
== len
)
199 cursor_right(&cursor
, &first
, x_max
, len
);
200 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
203 case KEYPRESS(0, SCAN_LEFT
, 0):
204 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'b'):
205 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('b')):
207 cursor_left(&cursor
, &first
);
208 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
211 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'd'):
214 for (i
= first
+ cursor
; i
< len
&& line
[i
] == ' '; i
++)
216 for (; i
< len
&& line
[i
] != ' '; i
++)
219 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
220 line
[i
] = line
[i
+ clear
];
225 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'w'):
226 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('w')):
227 case KEYPRESS(EFI_ALT_PRESSED
, 0, CHAR_BACKSPACE
):
228 /* backward-kill-word */
230 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
231 cursor_left(&cursor
, &first
);
233 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ') {
234 cursor_left(&cursor
, &first
);
238 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ') {
239 cursor_left(&cursor
, &first
);
242 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
244 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
245 line
[i
] = line
[i
+ clear
];
250 case KEYPRESS(0, SCAN_DELETE
, 0):
251 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'd'):
252 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('d')):
255 if (first
+ cursor
== len
)
257 for (i
= first
+ cursor
; i
< len
; i
++)
263 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'k'):
264 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('k')):
266 line
[first
+ cursor
] = '\0';
267 clear
= len
- (first
+ cursor
);
268 len
= first
+ cursor
;
271 case KEYPRESS(0, 0, CHAR_LINEFEED
):
272 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
273 if (StrCmp(line
, line_in
) != 0)
274 *line_out
= TAKE_PTR(line
);
279 case KEYPRESS(0, 0, CHAR_BACKSPACE
):
282 if (first
== 0 && cursor
== 0)
284 for (i
= first
+ cursor
-1; i
< len
; i
++)
290 if (cursor
> 0 || first
== 0)
292 /* show full line if it fits */
298 /* jump left to see what we delete */
308 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
309 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
312 for (i
= len
; i
> first
+ cursor
; i
--)
314 line
[first
+ cursor
] = KEYCHAR(key
);
317 if (cursor
+1 < x_max
)
319 else if (first
+ cursor
< len
)
325 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
) {
358 _cleanup_freepool_ CHAR8
*bootvar
= NULL
, *modevar
= NULL
, *indvar
= NULL
;
359 _cleanup_freepool_ CHAR16
*partstr
= NULL
, *defaultstr
= NULL
;
362 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
363 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
365 Print(L
"systemd-boot version: " GIT_VERSION
"\n");
366 Print(L
"architecture: " EFI_MACHINE_TYPE_NAME
"\n");
367 Print(L
"loaded image: %s\n", loaded_image_path
);
368 Print(L
"UEFI specification: %d.%02d\n", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
369 Print(L
"firmware vendor: %s\n", ST
->FirmwareVendor
);
370 Print(L
"firmware version: %d.%02d\n", ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
372 if (uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x
, &y
) == EFI_SUCCESS
)
373 Print(L
"console size: %d x %d\n", x
, y
);
375 if (efivar_get_raw(&global_guid
, L
"SecureBoot", &bootvar
, &size
) == EFI_SUCCESS
)
376 Print(L
"SecureBoot: %s\n", yes_no(*bootvar
> 0));
378 if (efivar_get_raw(&global_guid
, L
"SetupMode", &modevar
, &size
) == EFI_SUCCESS
)
379 Print(L
"SetupMode: %s\n", *modevar
> 0 ? L
"setup" : L
"user");
382 Print(L
"Shim: present\n");
384 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &indvar
, &size
) == EFI_SUCCESS
)
385 Print(L
"OsIndicationsSupported: %d\n", (UINT64
)*indvar
);
387 Print(L
"\n--- press key ---\n\n");
388 console_key_read(&key
, TRUE
);
390 Print(L
"timeout: %u\n", config
->timeout_sec
);
391 if (config
->timeout_sec_efivar
>= 0)
392 Print(L
"timeout (EFI var): %d\n", config
->timeout_sec_efivar
);
393 Print(L
"timeout (config): %u\n", config
->timeout_sec_config
);
394 if (config
->entry_default_pattern
)
395 Print(L
"default pattern: '%s'\n", config
->entry_default_pattern
);
396 Print(L
"editor: %s\n", yes_no(config
->editor
));
397 Print(L
"auto-entries: %s\n", yes_no(config
->auto_entries
));
398 Print(L
"auto-firmware: %s\n", yes_no(config
->auto_firmware
));
401 Print(L
"config entry count: %d\n", config
->entry_count
);
402 Print(L
"entry selected idx: %d\n", config
->idx_default
);
403 if (config
->idx_default_efivar
>= 0)
404 Print(L
"entry EFI var idx: %d\n", config
->idx_default_efivar
);
407 if (efivar_get_int(L
"LoaderConfigTimeout", &i
) == EFI_SUCCESS
)
408 Print(L
"LoaderConfigTimeout: %u\n", i
);
410 if (config
->entry_oneshot
)
411 Print(L
"LoaderEntryOneShot: %s\n", config
->entry_oneshot
);
412 if (efivar_get(L
"LoaderDevicePartUUID", &partstr
) == EFI_SUCCESS
)
413 Print(L
"LoaderDevicePartUUID: %s\n", partstr
);
414 if (efivar_get(L
"LoaderEntryDefault", &defaultstr
) == EFI_SUCCESS
)
415 Print(L
"LoaderEntryDefault: %s\n", defaultstr
);
417 Print(L
"\n--- press key ---\n\n");
418 console_key_read(&key
, TRUE
);
420 for (i
= 0; i
< config
->entry_count
; i
++) {
423 if (key
== KEYPRESS(0, SCAN_ESC
, 0) || key
== KEYPRESS(0, 0, 'q'))
426 entry
= config
->entries
[i
];
427 Print(L
"config entry: %d/%d\n", i
+1, config
->entry_count
);
429 Print(L
"id '%s'\n", entry
->id
);
430 Print(L
"title show '%s'\n", entry
->title_show
);
432 Print(L
"title '%s'\n", entry
->title
);
434 Print(L
"version '%s'\n", entry
->version
);
435 if (entry
->machine_id
)
436 Print(L
"machine-id '%s'\n", entry
->machine_id
);
438 EFI_DEVICE_PATH
*device_path
;
440 device_path
= DevicePathFromHandle(entry
->device
);
442 _cleanup_freepool_ CHAR16
*str
;
444 str
= DevicePathToStr(device_path
);
445 Print(L
"device handle '%s'\n", str
);
449 Print(L
"loader '%s'\n", entry
->loader
);
451 Print(L
"options '%s'\n", entry
->options
);
452 Print(L
"auto-select %s\n", yes_no(!entry
->no_autoselect
));
454 Print(L
"internal call yes\n");
456 if (entry
->tries_left
!= (UINTN
) -1)
457 Print(L
"counting boots yes\n"
460 "current path %s\\%s\n"
461 "next path %s\\%s\n",
464 entry
->path
, entry
->current_name
,
465 entry
->path
, entry
->next_name
);
467 Print(L
"\n--- press key ---\n\n");
468 console_key_read(&key
, TRUE
);
471 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
474 static BOOLEAN
menu_run(
476 ConfigEntry
**chosen_entry
,
477 CHAR16
*loaded_image_path
) {
482 UINTN idx_highlight_prev
;
498 BOOLEAN exit
= FALSE
;
500 BOOLEAN wait
= FALSE
;
501 BOOLEAN cleared_screen
= FALSE
;
503 graphics_mode(FALSE
);
504 uefi_call_wrapper(ST
->ConIn
->Reset
, 2, ST
->ConIn
, FALSE
);
505 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
506 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
508 /* draw a single character to make ClearScreen work on some firmware */
509 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
" ");
511 if (config
->console_mode_change
!= CONSOLE_MODE_KEEP
) {
512 err
= console_set_mode(&config
->console_mode
, config
->console_mode_change
);
514 cleared_screen
= TRUE
;
518 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
520 if (config
->console_mode_change
!= CONSOLE_MODE_KEEP
&& EFI_ERROR(err
))
521 Print(L
"Error switching console mode to %ld: %r.\r", (UINT64
)config
->console_mode
, err
);
523 err
= uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
524 if (EFI_ERROR(err
)) {
529 /* we check 10 times per second for a keystroke */
530 if (config
->timeout_sec
> 0)
531 timeout_remain
= config
->timeout_sec
* 10;
535 idx_highlight
= config
->idx_default
;
536 idx_highlight_prev
= 0;
538 visible_max
= y_max
- 2;
540 if ((UINTN
)config
->idx_default
>= visible_max
)
541 idx_first
= config
->idx_default
-1;
545 idx_last
= idx_first
+ visible_max
-1;
550 /* length of the longest entry */
552 for (i
= 0; i
< config
->entry_count
; i
++) {
555 entry_len
= StrLen(config
->entries
[i
]->title_show
);
556 if (line_width
< entry_len
)
557 line_width
= entry_len
;
559 if (line_width
> x_max
-6)
560 line_width
= x_max
-6;
562 /* offsets to center the entries on the screen */
563 x_start
= (x_max
- (line_width
)) / 2;
564 if (config
->entry_count
< visible_max
)
565 y_start
= ((visible_max
- config
->entry_count
) / 2) + 1;
569 /* menu entries title lines */
570 lines
= AllocatePool(sizeof(CHAR16
*) * config
->entry_count
);
571 for (i
= 0; i
< config
->entry_count
; i
++) {
574 lines
[i
] = AllocatePool(((x_max
+1) * sizeof(CHAR16
)));
575 for (j
= 0; j
< x_start
; j
++)
578 for (k
= 0; config
->entries
[i
]->title_show
[k
] != '\0' && j
< x_max
; j
++, k
++)
579 lines
[i
][j
] = config
->entries
[i
]->title_show
[k
];
581 for (; j
< x_max
; j
++)
583 lines
[i
][x_max
] = '\0';
587 clearline
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
588 for (i
= 0; i
< x_max
; i
++)
596 for (i
= 0; i
< config
->entry_count
; i
++) {
597 if (i
< idx_first
|| i
> idx_last
)
599 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ i
- idx_first
);
600 if (i
== idx_highlight
)
601 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
602 EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
604 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
605 EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
606 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[i
]);
607 if ((INTN
)i
== config
->idx_default_efivar
) {
608 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ i
- idx_first
);
609 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
613 } else if (highlight
) {
614 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight_prev
- idx_first
);
615 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
616 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight_prev
]);
617 if ((INTN
)idx_highlight_prev
== config
->idx_default_efivar
) {
618 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight_prev
- idx_first
);
619 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
622 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight
- idx_first
);
623 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
624 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight
]);
625 if ((INTN
)idx_highlight
== config
->idx_default_efivar
) {
626 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight
- idx_first
);
627 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
632 if (timeout_remain
> 0) {
634 status
= PoolPrint(L
"Boot in %d sec.", (timeout_remain
+ 5) / 10);
637 /* print status at last line of screen */
643 len
= StrLen(status
);
645 x
= (x_max
- len
) / 2;
648 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
649 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
650 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+ (x_max
- x
));
651 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, status
);
652 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1 + x
+ len
);
655 err
= console_key_read(&key
, wait
);
656 if (EFI_ERROR(err
)) {
657 /* timeout reached */
658 if (timeout_remain
== 0) {
663 /* sleep and update status */
664 if (timeout_remain
> 0) {
665 uefi_call_wrapper(BS
->Stall
, 1, 100 * 1000);
670 /* timeout disabled, wait for next key */
677 /* clear status after keystroke */
681 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
682 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
683 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
686 idx_highlight_prev
= idx_highlight
;
689 case KEYPRESS(0, SCAN_UP
, 0):
690 case KEYPRESS(0, 0, 'k'):
691 if (idx_highlight
> 0)
695 case KEYPRESS(0, SCAN_DOWN
, 0):
696 case KEYPRESS(0, 0, 'j'):
697 if (idx_highlight
< config
->entry_count
-1)
701 case KEYPRESS(0, SCAN_HOME
, 0):
702 case KEYPRESS(EFI_ALT_PRESSED
, 0, '<'):
703 if (idx_highlight
> 0) {
709 case KEYPRESS(0, SCAN_END
, 0):
710 case KEYPRESS(EFI_ALT_PRESSED
, 0, '>'):
711 if (idx_highlight
< config
->entry_count
-1) {
713 idx_highlight
= config
->entry_count
-1;
717 case KEYPRESS(0, SCAN_PAGE_UP
, 0):
718 if (idx_highlight
> visible_max
)
719 idx_highlight
-= visible_max
;
724 case KEYPRESS(0, SCAN_PAGE_DOWN
, 0):
725 idx_highlight
+= visible_max
;
726 if (idx_highlight
> config
->entry_count
-1)
727 idx_highlight
= config
->entry_count
-1;
730 case KEYPRESS(0, 0, CHAR_LINEFEED
):
731 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
735 case KEYPRESS(0, SCAN_F1
, 0):
736 case KEYPRESS(0, 0, 'h'):
737 case KEYPRESS(0, 0, '?'):
738 status
= StrDuplicate(L
"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
741 case KEYPRESS(0, 0, 'Q'):
746 case KEYPRESS(0, 0, 'd'):
747 if (config
->idx_default_efivar
!= (INTN
)idx_highlight
) {
748 /* store the selected entry in a persistent EFI variable */
749 efivar_set(L
"LoaderEntryDefault", config
->entries
[idx_highlight
]->id
, TRUE
);
750 config
->idx_default_efivar
= idx_highlight
;
751 status
= StrDuplicate(L
"Default boot entry selected.");
753 /* clear the default entry EFI variable */
754 efivar_set(L
"LoaderEntryDefault", NULL
, TRUE
);
755 config
->idx_default_efivar
= -1;
756 status
= StrDuplicate(L
"Default boot entry cleared.");
761 case KEYPRESS(0, 0, '-'):
762 case KEYPRESS(0, 0, 'T'):
763 if (config
->timeout_sec_efivar
> 0) {
764 config
->timeout_sec_efivar
--;
765 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
766 if (config
->timeout_sec_efivar
> 0)
767 status
= PoolPrint(L
"Menu timeout set to %d sec.", config
->timeout_sec_efivar
);
769 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
770 } else if (config
->timeout_sec_efivar
<= 0){
771 config
->timeout_sec_efivar
= -1;
772 efivar_set(L
"LoaderConfigTimeout", NULL
, TRUE
);
773 if (config
->timeout_sec_config
> 0)
774 status
= PoolPrint(L
"Menu timeout of %d sec is defined by configuration file.",
775 config
->timeout_sec_config
);
777 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
781 case KEYPRESS(0, 0, '+'):
782 case KEYPRESS(0, 0, 't'):
783 if (config
->timeout_sec_efivar
== -1 && config
->timeout_sec_config
== 0)
784 config
->timeout_sec_efivar
++;
785 config
->timeout_sec_efivar
++;
786 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
787 if (config
->timeout_sec_efivar
> 0)
788 status
= PoolPrint(L
"Menu timeout set to %d sec.",
789 config
->timeout_sec_efivar
);
791 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
794 case KEYPRESS(0, 0, 'e'):
795 /* only the options of configured entries can be edited */
796 if (!config
->editor
|| config
->entries
[idx_highlight
]->type
== LOADER_UNDEFINED
)
798 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
799 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
800 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
801 if (line_edit(config
->entries
[idx_highlight
]->options
, &config
->options_edit
, x_max
-1, y_max
-1))
803 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
804 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
807 case KEYPRESS(0, 0, 'v'):
808 status
= PoolPrint(L
"systemd-boot " GIT_VERSION
" (" EFI_MACHINE_TYPE_NAME
"), UEFI Specification %d.%02d, Vendor %s %d.%02d",
809 ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff,
810 ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
813 case KEYPRESS(0, 0, 'P'):
814 print_status(config
, loaded_image_path
);
818 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'l'):
819 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('l')):
824 /* jump with a hotkey directly to a matching entry */
825 idx
= entry_lookup_key(config
, idx_highlight
+1, KEYCHAR(key
));
832 if (idx_highlight
> idx_last
) {
833 idx_last
= idx_highlight
;
834 idx_first
= 1 + idx_highlight
- visible_max
;
836 } else if (idx_highlight
< idx_first
) {
837 idx_first
= idx_highlight
;
838 idx_last
= idx_highlight
+ visible_max
-1;
842 if (!refresh
&& idx_highlight
!= idx_highlight_prev
)
846 *chosen_entry
= config
->entries
[idx_highlight
];
848 for (i
= 0; i
< config
->entry_count
; i
++)
853 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_WHITE
|EFI_BACKGROUND_BLACK
);
854 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
858 static VOID
config_add_entry(Config
*config
, ConfigEntry
*entry
) {
859 if ((config
->entry_count
& 15) == 0) {
862 i
= config
->entry_count
+ 16;
863 if (config
->entry_count
== 0)
864 config
->entries
= AllocatePool(sizeof(VOID
*) * i
);
866 config
->entries
= ReallocatePool(config
->entries
,
867 sizeof(VOID
*) * config
->entry_count
, sizeof(VOID
*) * i
);
869 config
->entries
[config
->entry_count
++] = entry
;
872 static VOID
config_entry_free(ConfigEntry
*entry
) {
877 FreePool(entry
->title_show
);
878 FreePool(entry
->title
);
879 FreePool(entry
->version
);
880 FreePool(entry
->machine_id
);
881 FreePool(entry
->loader
);
882 FreePool(entry
->options
);
883 FreePool(entry
->path
);
884 FreePool(entry
->current_name
);
885 FreePool(entry
->next_name
);
889 static BOOLEAN
is_digit(CHAR16 c
) {
890 return (c
>= '0') && (c
<= '9');
893 static UINTN
c_order(CHAR16 c
) {
898 else if ((c
>= 'a') && (c
<= 'z'))
904 static INTN
str_verscmp(CHAR16
*s1
, CHAR16
*s2
) {
911 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
914 order
= c_order(*s1
) - c_order(*s2
);
927 while (is_digit(*s1
) && is_digit(*s2
)) {
943 return StrCmp(os1
, os2
);
946 static CHAR8
*line_get_key_value(
958 line
= content
+ *pos
;
963 while (line
[linelen
] && !strchra((CHAR8
*)"\n\r", line
[linelen
]))
966 /* move pos to next line */
976 line
[linelen
] = '\0';
978 /* remove leading whitespace */
979 while (strchra((CHAR8
*)" \t", *line
)) {
984 /* remove trailing whitespace */
985 while (linelen
> 0 && strchra((CHAR8
*)" \t", line
[linelen
-1]))
987 line
[linelen
] = '\0';
992 /* split key/value */
994 while (*value
&& !strchra(sep
, *value
))
1000 while (*value
&& strchra(sep
, *value
))
1004 if (value
[0] == '"' && line
[linelen
-1] == '"') {
1006 line
[linelen
-1] = '\0';
1014 static VOID
config_defaults_load_from_file(Config
*config
, CHAR8
*content
) {
1019 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1020 if (strcmpa((CHAR8
*)"timeout", key
) == 0) {
1021 _cleanup_freepool_ CHAR16
*s
= NULL
;
1023 s
= stra_to_str(value
);
1024 config
->timeout_sec_config
= Atoi(s
);
1025 config
->timeout_sec
= config
->timeout_sec_config
;
1029 if (strcmpa((CHAR8
*)"default", key
) == 0) {
1030 FreePool(config
->entry_default_pattern
);
1031 config
->entry_default_pattern
= stra_to_str(value
);
1032 StrLwr(config
->entry_default_pattern
);
1036 if (strcmpa((CHAR8
*)"editor", key
) == 0) {
1039 if (EFI_ERROR(parse_boolean(value
, &on
)))
1041 config
->editor
= on
;
1044 if (strcmpa((CHAR8
*)"auto-entries", key
) == 0) {
1047 if (EFI_ERROR(parse_boolean(value
, &on
)))
1049 config
->auto_entries
= on
;
1052 if (strcmpa((CHAR8
*)"auto-firmware", key
) == 0) {
1055 if (EFI_ERROR(parse_boolean(value
, &on
)))
1057 config
->auto_firmware
= on
;
1060 if (strcmpa((CHAR8
*)"console-mode", key
) == 0) {
1061 if (strcmpa((CHAR8
*)"auto", value
) == 0)
1062 config
->console_mode_change
= CONSOLE_MODE_AUTO
;
1063 else if (strcmpa((CHAR8
*)"max", value
) == 0)
1064 config
->console_mode_change
= CONSOLE_MODE_MAX
;
1065 else if (strcmpa((CHAR8
*)"keep", value
) == 0)
1066 config
->console_mode_change
= CONSOLE_MODE_KEEP
;
1068 _cleanup_freepool_ CHAR16
*s
= NULL
;
1070 s
= stra_to_str(value
);
1071 config
->console_mode
= Atoi(s
);
1072 config
->console_mode_change
= CONSOLE_MODE_SET
;
1080 static VOID
config_entry_parse_tries(
1086 UINTN left
= (UINTN
) -1, done
= (UINTN
) -1, factor
= 1, i
, next_left
, next_done
;
1087 _cleanup_freepool_ CHAR16
*prefix
= NULL
;
1090 * Parses a suffix of two counters (one going down, one going up) in the form "+LEFT-DONE" from the end of the
1091 * filename (but before the .efi/.conf suffix), where the "-DONE" part is optional and may be left out (in
1092 * which case that counter as assumed to be zero, i.e. the missing part is synonymous to "-0").
1094 * Names we grok, and the series they result in:
1096 * foobar+3.efi → foobar+2-1.efi → foobar+1-2.efi → foobar+0-3.efi → STOP!
1097 * foobar+4-0.efi → foobar+3-1.efi → foobar+2-2.efi → foobar+1-3.efi → foobar+0-4.efi → STOP!
1102 /* Chop off any suffix such as ".conf" or ".efi" */
1104 UINTN suffix_length
;
1106 suffix_length
= StrLen(suffix
);
1107 if (i
< suffix_length
)
1113 /* Go backwards through the string and parse everything we encounter */
1123 if (left
== (UINTN
) -1) /* didn't read at least one digit for 'left'? */
1126 if (done
== (UINTN
) -1) /* no 'done' counter? If so, it's equivalent to 0 */
1132 if (left
== (UINTN
) -1) /* didn't parse any digit yet? */
1135 if (done
!= (UINTN
) -1) /* already encountered a dash earlier? */
1138 /* So we encountered a dash. This means this counter is of the form +LEFT-DONE. Let's assign
1139 * what we already parsed to 'done', and start fresh for the 'left' part. */
1149 if (left
== (UINTN
) -1)
1150 left
= file
[i
] - '0';
1152 UINTN new_left
, digit
;
1154 digit
= file
[i
] - '0';
1155 if (digit
> (UINTN
) -1 / factor
) /* overflow check */
1158 new_left
= left
+ digit
* factor
;
1159 if (new_left
< left
) /* overflow check */
1162 if (new_left
== (UINTN
) -1) /* don't allow us to be confused */
1166 new_factor
= factor
* 10;
1167 if (new_factor
< factor
) /* overflow chck */
1170 factor
= new_factor
;
1180 entry
->tries_left
= left
;
1181 entry
->tries_done
= done
;
1183 entry
->path
= StrDuplicate(path
);
1184 entry
->current_name
= StrDuplicate(file
);
1186 next_left
= left
<= 0 ? 0 : left
- 1;
1187 next_done
= done
>= (UINTN
) -2 ? (UINTN
) -2 : done
+ 1;
1189 prefix
= StrDuplicate(file
);
1192 entry
->next_name
= PoolPrint(L
"%s+%u-%u%s", prefix
, next_left
, next_done
, suffix
?: L
"");
1195 static VOID
config_entry_bump_counters(
1197 EFI_FILE_HANDLE root_dir
) {
1199 _cleanup_freepool_ CHAR16
* old_path
= NULL
, *new_path
= NULL
;
1200 _cleanup_(FileHandleClosep
) EFI_FILE_HANDLE handle
= NULL
;
1201 static EFI_GUID EfiFileInfoGuid
= EFI_FILE_INFO_ID
;
1202 _cleanup_freepool_ EFI_FILE_INFO
*file_info
= NULL
;
1203 UINTN file_info_size
, a
, b
;
1206 if (entry
->tries_left
== (UINTN
) -1)
1209 if (!entry
->path
|| !entry
->current_name
|| !entry
->next_name
)
1212 old_path
= PoolPrint(L
"%s\\%s", entry
->path
, entry
->current_name
);
1214 r
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, old_path
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0ULL);
1218 a
= StrLen(entry
->current_name
);
1219 b
= StrLen(entry
->next_name
);
1221 file_info_size
= OFFSETOF(EFI_FILE_INFO
, FileName
) + (a
> b
? a
: b
) + 1;
1224 file_info
= AllocatePool(file_info_size
);
1226 r
= uefi_call_wrapper(handle
->GetInfo
, 4, handle
, &EfiFileInfoGuid
, &file_info_size
, file_info
);
1230 if (r
!= EFI_BUFFER_TOO_SMALL
|| file_info_size
* 2 < file_info_size
) {
1231 Print(L
"\nFailed to get file info for '%s': %r\n", old_path
, r
);
1232 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1236 file_info_size
*= 2;
1237 FreePool(file_info
);
1240 /* And rename the file */
1241 StrCpy(file_info
->FileName
, entry
->next_name
);
1242 r
= uefi_call_wrapper(handle
->SetInfo
, 4, handle
, &EfiFileInfoGuid
, file_info_size
, file_info
);
1244 Print(L
"\nFailed to rename '%s' to '%s', ignoring: %r\n", old_path
, entry
->next_name
, r
);
1245 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1249 /* Flush everything to disk, just in case… */
1250 (void) uefi_call_wrapper(handle
->Flush
, 1, handle
);
1252 /* Let's tell the OS that we renamed this file, so that it knows what to rename to the counter-less name on
1254 new_path
= PoolPrint(L
"%s\\%s", entry
->path
, entry
->next_name
);
1255 efivar_set(L
"LoaderBootCountPath", new_path
, FALSE
);
1257 /* If the file we just renamed is the loader path, then let's update that. */
1258 if (StrCmp(entry
->loader
, old_path
) == 0) {
1259 FreePool(entry
->loader
);
1260 entry
->loader
= TAKE_PTR(new_path
);
1264 static VOID
config_entry_add_from_file(
1270 CHAR16
*loaded_image_path
) {
1277 _cleanup_freepool_ CHAR16
*initrd
= NULL
;
1279 entry
= AllocatePool(sizeof(ConfigEntry
));
1281 *entry
= (ConfigEntry
) {
1282 .tries_done
= (UINTN
) -1,
1283 .tries_left
= (UINTN
) -1,
1286 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1287 if (strcmpa((CHAR8
*)"title", key
) == 0) {
1288 FreePool(entry
->title
);
1289 entry
->title
= stra_to_str(value
);
1293 if (strcmpa((CHAR8
*)"version", key
) == 0) {
1294 FreePool(entry
->version
);
1295 entry
->version
= stra_to_str(value
);
1299 if (strcmpa((CHAR8
*)"machine-id", key
) == 0) {
1300 FreePool(entry
->machine_id
);
1301 entry
->machine_id
= stra_to_str(value
);
1305 if (strcmpa((CHAR8
*)"linux", key
) == 0) {
1306 FreePool(entry
->loader
);
1307 entry
->type
= LOADER_LINUX
;
1308 entry
->loader
= stra_to_path(value
);
1313 if (strcmpa((CHAR8
*)"efi", key
) == 0) {
1314 entry
->type
= LOADER_EFI
;
1315 FreePool(entry
->loader
);
1316 entry
->loader
= stra_to_path(value
);
1318 /* do not add an entry for ourselves */
1319 if (loaded_image_path
&& StriCmp(entry
->loader
, loaded_image_path
) == 0) {
1320 entry
->type
= LOADER_UNDEFINED
;
1326 if (strcmpa((CHAR8
*)"architecture", key
) == 0) {
1327 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1328 if (strcmpa((CHAR8
*)EFI_MACHINE_TYPE_NAME
, value
) != 0) {
1329 entry
->type
= LOADER_UNDEFINED
;
1335 if (strcmpa((CHAR8
*)"initrd", key
) == 0) {
1336 _cleanup_freepool_ CHAR16
*new = NULL
;
1338 new = stra_to_path(value
);
1342 s
= PoolPrint(L
"%s initrd=%s", initrd
, new);
1346 initrd
= PoolPrint(L
"initrd=%s", new);
1351 if (strcmpa((CHAR8
*)"options", key
) == 0) {
1352 _cleanup_freepool_ CHAR16
*new = NULL
;
1354 new = stra_to_str(value
);
1355 if (entry
->options
) {
1358 s
= PoolPrint(L
"%s %s", entry
->options
, new);
1359 FreePool(entry
->options
);
1362 entry
->options
= TAKE_PTR(new);
1368 if (entry
->type
== LOADER_UNDEFINED
) {
1369 config_entry_free(entry
);
1373 /* add initrd= to options */
1374 if (entry
->type
== LOADER_LINUX
&& initrd
) {
1375 if (entry
->options
) {
1378 s
= PoolPrint(L
"%s %s", initrd
, entry
->options
);
1379 FreePool(entry
->options
);
1382 entry
->options
= TAKE_PTR(initrd
);
1385 entry
->device
= device
;
1386 entry
->id
= StrDuplicate(file
);
1387 len
= StrLen(entry
->id
);
1388 /* remove ".conf" */
1390 entry
->id
[len
- 5] = '\0';
1393 config_add_entry(config
, entry
);
1395 config_entry_parse_tries(entry
, path
, file
, L
".conf");
1398 static VOID
config_load_defaults(Config
*config
, EFI_FILE
*root_dir
) {
1399 _cleanup_freepool_ CHAR8
*content
= NULL
;
1403 *config
= (Config
) {
1405 .auto_entries
= TRUE
,
1406 .auto_firmware
= TRUE
,
1409 err
= file_read(root_dir
, L
"\\loader\\loader.conf", 0, 0, &content
, NULL
);
1410 if (!EFI_ERROR(err
))
1411 config_defaults_load_from_file(config
, content
);
1413 err
= efivar_get_int(L
"LoaderConfigTimeout", &sec
);
1414 if (!EFI_ERROR(err
)) {
1415 config
->timeout_sec_efivar
= sec
> INTN_MAX
? INTN_MAX
: sec
;
1416 config
->timeout_sec
= sec
;
1418 config
->timeout_sec_efivar
= -1;
1420 err
= efivar_get_int(L
"LoaderConfigTimeoutOneShot", &sec
);
1421 if (!EFI_ERROR(err
)) {
1422 /* Unset variable now, after all it's "one shot". */
1423 (void) efivar_set(L
"LoaderConfigTimeoutOneShot", NULL
, TRUE
);
1425 config
->timeout_sec
= sec
;
1426 config
->force_menu
= TRUE
; /* force the menu when this is set */
1430 static VOID
config_load_entries(
1434 CHAR16
*loaded_image_path
) {
1436 EFI_FILE_HANDLE entries_dir
;
1439 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &entries_dir
, L
"\\loader\\entries", EFI_FILE_MODE_READ
, 0ULL);
1440 if (!EFI_ERROR(err
)) {
1445 _cleanup_freepool_ CHAR8
*content
= NULL
;
1448 bufsize
= sizeof(buf
);
1449 err
= uefi_call_wrapper(entries_dir
->Read
, 3, entries_dir
, &bufsize
, buf
);
1450 if (bufsize
== 0 || EFI_ERROR(err
))
1453 f
= (EFI_FILE_INFO
*) buf
;
1454 if (f
->FileName
[0] == '.')
1456 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1459 len
= StrLen(f
->FileName
);
1462 if (StriCmp(f
->FileName
+ len
- 5, L
".conf") != 0)
1464 if (StrnCmp(f
->FileName
, L
"auto-", 5) == 0)
1467 err
= file_read(entries_dir
, f
->FileName
, 0, 0, &content
, NULL
);
1468 if (!EFI_ERROR(err
))
1469 config_entry_add_from_file(config
, device
, L
"\\loader\\entries", f
->FileName
, content
, loaded_image_path
);
1471 uefi_call_wrapper(entries_dir
->Close
, 1, entries_dir
);
1475 static INTN
config_entry_compare(ConfigEntry
*a
, ConfigEntry
*b
) {
1478 /* Order entries that have no tries left to the end of the list */
1479 if (a
->tries_left
!= 0 && b
->tries_left
== 0)
1481 if (a
->tries_left
== 0 && b
->tries_left
!= 0)
1484 r
= str_verscmp(a
->id
, b
->id
);
1488 if (a
->tries_left
== (UINTN
) -1 ||
1489 b
->tries_left
== (UINTN
) -1)
1492 /* If both items have boot counting, and otherwise are identical, put the entry with more tries left first */
1493 if (a
->tries_left
> b
->tries_left
)
1495 if (a
->tries_left
< b
->tries_left
)
1498 /* If they have the same number of tries left, then let the one win which was tried fewer times so far */
1499 if (a
->tries_done
< b
->tries_done
)
1501 if (a
->tries_done
> b
->tries_done
)
1507 static VOID
config_sort_entries(Config
*config
) {
1510 for (i
= 1; i
< config
->entry_count
; i
++) {
1515 for (k
= 0; k
< config
->entry_count
- i
; k
++) {
1518 if (config_entry_compare(config
->entries
[k
], config
->entries
[k
+1]) <= 0)
1521 entry
= config
->entries
[k
];
1522 config
->entries
[k
] = config
->entries
[k
+1];
1523 config
->entries
[k
+1] = entry
;
1531 static INTN
config_entry_find(Config
*config
, CHAR16
*id
) {
1534 for (i
= 0; i
< config
->entry_count
; i
++)
1535 if (StrCmp(config
->entries
[i
]->id
, id
) == 0)
1541 static VOID
config_default_entry_select(Config
*config
) {
1542 _cleanup_freepool_ CHAR16
*entry_oneshot
= NULL
, *entry_default
= NULL
;
1547 * The EFI variable to specify a boot entry for the next, and only the
1548 * next reboot. The variable is always cleared directly after it is read.
1550 err
= efivar_get(L
"LoaderEntryOneShot", &entry_oneshot
);
1551 if (!EFI_ERROR(err
)) {
1553 config
->entry_oneshot
= StrDuplicate(entry_oneshot
);
1554 efivar_set(L
"LoaderEntryOneShot", NULL
, TRUE
);
1556 i
= config_entry_find(config
, entry_oneshot
);
1558 config
->idx_default
= i
;
1564 * The EFI variable to select the default boot entry overrides the
1565 * configured pattern. The variable can be set and cleared by pressing
1566 * the 'd' key in the loader selection menu, the entry is marked with
1569 err
= efivar_get(L
"LoaderEntryDefault", &entry_default
);
1570 if (!EFI_ERROR(err
)) {
1572 i
= config_entry_find(config
, entry_default
);
1574 config
->idx_default
= i
;
1575 config
->idx_default_efivar
= i
;
1579 config
->idx_default_efivar
= -1;
1581 if (config
->entry_count
== 0)
1585 * Match the pattern from the end of the list to the start, find last
1586 * entry (largest number) matching the given pattern.
1588 if (config
->entry_default_pattern
) {
1589 i
= config
->entry_count
;
1591 if (config
->entries
[i
]->no_autoselect
)
1593 if (MetaiMatch(config
->entries
[i
]->id
, config
->entry_default_pattern
)) {
1594 config
->idx_default
= i
;
1600 /* select the last suitable entry */
1601 i
= config
->entry_count
;
1603 if (config
->entries
[i
]->no_autoselect
)
1605 config
->idx_default
= i
;
1609 /* no entry found */
1610 config
->idx_default
= -1;
1613 static BOOLEAN
find_nonunique(ConfigEntry
**entries
, UINTN entry_count
) {
1614 BOOLEAN non_unique
= FALSE
;
1617 for (i
= 0; i
< entry_count
; i
++)
1618 entries
[i
]->non_unique
= FALSE
;
1620 for (i
= 0; i
< entry_count
; i
++)
1621 for (k
= 0; k
< entry_count
; k
++) {
1624 if (StrCmp(entries
[i
]->title_show
, entries
[k
]->title_show
) != 0)
1627 non_unique
= entries
[i
]->non_unique
= entries
[k
]->non_unique
= TRUE
;
1633 /* generate a unique title, avoiding non-distinguishable menu entries */
1634 static VOID
config_title_generate(Config
*config
) {
1638 for (i
= 0; i
< config
->entry_count
; i
++) {
1641 FreePool(config
->entries
[i
]->title_show
);
1642 title
= config
->entries
[i
]->title
;
1644 title
= config
->entries
[i
]->id
;
1645 config
->entries
[i
]->title_show
= StrDuplicate(title
);
1648 if (!find_nonunique(config
->entries
, config
->entry_count
))
1651 /* add version to non-unique titles */
1652 for (i
= 0; i
< config
->entry_count
; i
++) {
1655 if (!config
->entries
[i
]->non_unique
)
1657 if (!config
->entries
[i
]->version
)
1660 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->version
);
1661 FreePool(config
->entries
[i
]->title_show
);
1662 config
->entries
[i
]->title_show
= s
;
1665 if (!find_nonunique(config
->entries
, config
->entry_count
))
1668 /* add machine-id to non-unique titles */
1669 for (i
= 0; i
< config
->entry_count
; i
++) {
1671 _cleanup_freepool_ CHAR16
*m
= NULL
;
1673 if (!config
->entries
[i
]->non_unique
)
1675 if (!config
->entries
[i
]->machine_id
)
1678 m
= StrDuplicate(config
->entries
[i
]->machine_id
);
1680 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, m
);
1681 FreePool(config
->entries
[i
]->title_show
);
1682 config
->entries
[i
]->title_show
= s
;
1685 if (!find_nonunique(config
->entries
, config
->entry_count
))
1688 /* add file name to non-unique titles */
1689 for (i
= 0; i
< config
->entry_count
; i
++) {
1692 if (!config
->entries
[i
]->non_unique
)
1694 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->id
);
1695 FreePool(config
->entries
[i
]->title_show
);
1696 config
->entries
[i
]->title_show
= s
;
1697 config
->entries
[i
]->non_unique
= FALSE
;
1701 static BOOLEAN
config_entry_add_call(
1705 EFI_STATUS (*call
)(VOID
)) {
1709 entry
= AllocatePool(sizeof(ConfigEntry
));
1710 *entry
= (ConfigEntry
) {
1711 .id
= StrDuplicate(id
),
1712 .title
= StrDuplicate(title
),
1714 .no_autoselect
= TRUE
,
1715 .tries_done
= (UINTN
) -1,
1716 .tries_left
= (UINTN
) -1,
1719 config_add_entry(config
, entry
);
1723 static ConfigEntry
*config_entry_add_loader(
1726 enum loader_type type
,
1734 entry
= AllocatePool(sizeof(ConfigEntry
));
1735 *entry
= (ConfigEntry
) {
1737 .title
= StrDuplicate(title
),
1739 .loader
= StrDuplicate(loader
),
1740 .id
= StrDuplicate(id
),
1742 .tries_done
= (UINTN
) -1,
1743 .tries_left
= (UINTN
) -1,
1748 config_add_entry(config
, entry
);
1752 static BOOLEAN
config_entry_add_loader_auto(
1756 CHAR16
*loaded_image_path
,
1762 EFI_FILE_HANDLE handle
;
1766 if (!config
->auto_entries
)
1769 /* do not add an entry for ourselves */
1770 if (loaded_image_path
) {
1772 _cleanup_freepool_ CHAR8
*content
= NULL
;
1774 if (StriCmp(loader
, loaded_image_path
) == 0)
1777 /* look for systemd-boot magic string */
1778 err
= file_read(root_dir
, loader
, 0, 100*1024, &content
, &len
);
1779 if (!EFI_ERROR(err
)) {
1780 CHAR8
*start
= content
;
1781 CHAR8
*last
= content
+ len
- sizeof(magic
) - 1;
1783 for (; start
<= last
; start
++)
1784 if (start
[0] == magic
[0] && CompareMem(start
, magic
, sizeof(magic
) - 1) == 0)
1789 /* check existence */
1790 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, loader
, EFI_FILE_MODE_READ
, 0ULL);
1793 uefi_call_wrapper(handle
->Close
, 1, handle
);
1795 entry
= config_entry_add_loader(config
, device
, LOADER_UNDEFINED
, id
, key
, title
, loader
);
1799 /* do not boot right away into auto-detected entries */
1800 entry
->no_autoselect
= TRUE
;
1805 static VOID
config_entry_add_osx(Config
*config
) {
1807 UINTN handle_count
= 0;
1808 _cleanup_freepool_ EFI_HANDLE
*handles
= NULL
;
1810 if (!config
->auto_entries
)
1813 err
= LibLocateHandle(ByProtocol
, &FileSystemProtocol
, NULL
, &handle_count
, &handles
);
1814 if (!EFI_ERROR(err
)) {
1817 for (i
= 0; i
< handle_count
; i
++) {
1821 root
= LibOpenRoot(handles
[i
]);
1824 found
= config_entry_add_loader_auto(config
, handles
[i
], root
, NULL
, L
"auto-osx", 'a', L
"macOS",
1825 L
"\\System\\Library\\CoreServices\\boot.efi");
1826 uefi_call_wrapper(root
->Close
, 1, root
);
1833 static VOID
config_entry_add_linux(
1836 EFI_FILE
*root_dir
) {
1838 EFI_FILE_HANDLE linux_dir
;
1842 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &linux_dir
, L
"\\EFI\\Linux", EFI_FILE_MODE_READ
, 0ULL);
1848 UINTN bufsize
= sizeof buf
;
1850 CHAR8
*sections
[] = {
1852 (UINT8
*)".cmdline",
1855 UINTN offs
[ELEMENTSOF(sections
)-1] = {};
1856 UINTN szs
[ELEMENTSOF(sections
)-1] = {};
1857 UINTN addrs
[ELEMENTSOF(sections
)-1] = {};
1858 CHAR8
*content
= NULL
;
1863 CHAR16
*os_name
= NULL
;
1864 CHAR16
*os_id
= NULL
;
1865 CHAR16
*os_version
= NULL
;
1866 CHAR16
*os_build
= NULL
;
1868 err
= uefi_call_wrapper(linux_dir
->Read
, 3, linux_dir
, &bufsize
, buf
);
1869 if (bufsize
== 0 || EFI_ERROR(err
))
1872 f
= (EFI_FILE_INFO
*) buf
;
1873 if (f
->FileName
[0] == '.')
1875 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1877 len
= StrLen(f
->FileName
);
1880 if (StriCmp(f
->FileName
+ len
- 4, L
".efi") != 0)
1883 /* look for .osrel and .cmdline sections in the .efi binary */
1884 err
= pe_file_locate_sections(linux_dir
, f
->FileName
, sections
, addrs
, offs
, szs
);
1888 err
= file_read(linux_dir
, f
->FileName
, offs
[0], szs
[0], &content
, NULL
);
1892 /* read properties from the embedded os-release file */
1893 while ((line
= line_get_key_value(content
, (CHAR8
*)"=", &pos
, &key
, &value
))) {
1894 if (strcmpa((CHAR8
*)"PRETTY_NAME", key
) == 0) {
1896 os_name
= stra_to_str(value
);
1900 if (strcmpa((CHAR8
*)"ID", key
) == 0) {
1902 os_id
= stra_to_str(value
);
1906 if (strcmpa((CHAR8
*)"VERSION_ID", key
) == 0) {
1907 FreePool(os_version
);
1908 os_version
= stra_to_str(value
);
1912 if (strcmpa((CHAR8
*)"BUILD_ID", key
) == 0) {
1914 os_build
= stra_to_str(value
);
1919 if (os_name
&& os_id
&& (os_version
|| os_build
)) {
1920 _cleanup_freepool_ CHAR16
*conf
= NULL
, *path
= NULL
;
1922 conf
= PoolPrint(L
"%s-%s", os_id
, os_version
? : os_build
);
1923 path
= PoolPrint(L
"\\EFI\\Linux\\%s", f
->FileName
);
1925 entry
= config_entry_add_loader(config
, device
, LOADER_LINUX
, conf
, 'l', os_name
, path
);
1930 /* read the embedded cmdline file */
1931 err
= file_read(linux_dir
, f
->FileName
, offs
[1], szs
[1], &content
, NULL
);
1932 if (!EFI_ERROR(err
)) {
1934 /* chomp the newline */
1935 if (content
[szs
[1]-1] == '\n')
1936 content
[szs
[1]-1] = '\0';
1938 entry
->options
= stra_to_str(content
);
1941 config_entry_parse_tries(entry
, L
"\\EFI\\Linux", f
->FileName
, L
".efi");
1946 FreePool(os_version
);
1951 uefi_call_wrapper(linux_dir
->Close
, 1, linux_dir
);
1954 /* Note that this is in GUID format, i.e. the first 32bit, and the following pair of 16bit are byteswapped. */
1955 static const UINT8 xbootldr_guid
[16] = {
1956 0xff, 0xc2, 0x13, 0xbc, 0xe6, 0x59, 0x62, 0x42, 0xa3, 0x52, 0xb2, 0x75, 0xfd, 0x6f, 0x71, 0x72
1959 EFI_DEVICE_PATH
*path_parent(EFI_DEVICE_PATH
*path
, EFI_DEVICE_PATH
*node
) {
1960 EFI_DEVICE_PATH
*parent
;
1963 len
= (UINT8
*) NextDevicePathNode(node
) - (UINT8
*) path
;
1964 parent
= (EFI_DEVICE_PATH
*) AllocatePool(len
+ sizeof(EFI_DEVICE_PATH
));
1965 CopyMem(parent
, path
, len
);
1966 CopyMem((UINT8
*) parent
+ len
, EndDevicePath
, sizeof(EFI_DEVICE_PATH
));
1971 static VOID
config_load_xbootldr(
1973 EFI_HANDLE
*device
) {
1975 EFI_DEVICE_PATH
*partition_path
, *node
, *disk_path
, *copy
;
1976 UINT32 found_partition_number
= (UINT32
) -1;
1977 UINT64 found_partition_start
= (UINT64
) -1;
1978 UINT64 found_partition_size
= (UINT64
) -1;
1979 UINT8 found_partition_signature
[16] = {};
1980 EFI_HANDLE new_device
;
1984 partition_path
= DevicePathFromHandle(device
);
1985 if (!partition_path
)
1988 for (node
= partition_path
; !IsDevicePathEnd(node
); node
= NextDevicePathNode(node
)) {
1989 EFI_HANDLE disk_handle
;
1990 EFI_BLOCK_IO
*block_io
;
1994 /* First, Let's look for the SCSI/SATA/USB/… device path node, i.e. one above the media
1996 if (DevicePathType(node
) != MESSAGING_DEVICE_PATH
)
1999 /* Determine the device path one level up */
2000 disk_path
= path_parent(partition_path
, node
);
2002 r
= uefi_call_wrapper(BS
->LocateDevicePath
, 3, &BlockIoProtocol
, &p
, &disk_handle
);
2006 r
= uefi_call_wrapper(BS
->HandleProtocol
, 3, disk_handle
, &BlockIoProtocol
, (VOID
**)&block_io
);
2010 /* Filter out some block devices early. (We only care about block devices that aren't
2011 * partitions themselves — we look for GPT partition tables to parse after all —, and only
2012 * those which contain a medium and have at least 2 blocks.) */
2013 if (block_io
->Media
->LogicalPartition
||
2014 !block_io
->Media
->MediaPresent
||
2015 block_io
->Media
->LastBlock
<= 1)
2018 /* Try both copies of the GPT header, in case one is corrupted */
2019 for (nr
= 0; nr
< 2; nr
++) {
2020 _cleanup_freepool_ EFI_PARTITION_ENTRY
* entries
= NULL
;
2022 EFI_PARTITION_TABLE_HEADER gpt_header
;
2023 uint8_t space
[((sizeof(EFI_PARTITION_TABLE_HEADER
) + 511) / 512) * 512];
2024 } gpt_header_buffer
;
2025 const EFI_PARTITION_TABLE_HEADER
*h
= &gpt_header_buffer
.gpt_header
;
2031 /* Read the first copy at LBA 1 */
2034 /* Read the second copy at the very last LBA of this block device */
2035 where
= block_io
->Media
->LastBlock
;
2037 /* Read the GPT header */
2038 r
= uefi_call_wrapper(block_io
->ReadBlocks
, 5,
2040 block_io
->Media
->MediaId
,
2042 sizeof(gpt_header_buffer
), &gpt_header_buffer
);
2046 /* Some superficial validation of the GPT header */
2047 c
= CompareMem(&h
->Header
.Signature
, "EFI PART", sizeof(h
->Header
.Signature
));
2051 if (h
->Header
.HeaderSize
< 92 ||
2052 h
->Header
.HeaderSize
> 512)
2055 if (h
->Header
.Revision
!= 0x00010000U
)
2058 /* Calculate CRC check */
2059 c
= ~crc32_exclude_offset((UINT32
) -1,
2060 (const UINT8
*) &gpt_header_buffer
,
2061 h
->Header
.HeaderSize
,
2062 OFFSETOF(EFI_PARTITION_TABLE_HEADER
, Header
.CRC32
),
2063 sizeof(h
->Header
.CRC32
));
2064 if (c
!= h
->Header
.CRC32
)
2067 if (h
->MyLBA
!= where
)
2070 if (h
->SizeOfPartitionEntry
< sizeof(EFI_PARTITION_ENTRY
))
2073 if (h
->NumberOfPartitionEntries
<= 0 ||
2074 h
->NumberOfPartitionEntries
> 1024)
2077 if (h
->SizeOfPartitionEntry
> UINTN_MAX
/ h
->NumberOfPartitionEntries
) /* overflow check */
2080 /* Now load the GPT entry table */
2081 sz
= ALIGN_TO((UINTN
) h
->SizeOfPartitionEntry
* (UINTN
) h
->NumberOfPartitionEntries
, 512);
2082 entries
= AllocatePool(sz
);
2084 r
= uefi_call_wrapper(block_io
->ReadBlocks
, 5,
2086 block_io
->Media
->MediaId
,
2087 h
->PartitionEntryLBA
,
2092 /* Calculate CRC of entries array, too */
2093 c
= ~crc32((UINT32
) -1, entries
, sz
);
2094 if (c
!= h
->PartitionEntryArrayCRC32
)
2097 for (i
= 0; i
< h
->NumberOfPartitionEntries
; i
++) {
2098 EFI_PARTITION_ENTRY
*entry
;
2100 entry
= (EFI_PARTITION_ENTRY
*) ((UINT8
*) entries
+ h
->SizeOfPartitionEntry
* i
);
2102 if (CompareMem(&entry
->PartitionTypeGUID
, xbootldr_guid
, 16) == 0) {
2105 /* Let's use memcpy(), in case the structs are not aligned (they really should be though) */
2106 CopyMem(&found_partition_start
, &entry
->StartingLBA
, sizeof(found_partition_start
));
2107 CopyMem(&end
, &entry
->EndingLBA
, sizeof(end
));
2109 if (end
< found_partition_start
) /* Bogus? */
2112 found_partition_size
= end
- found_partition_start
+ 1;
2113 CopyMem(found_partition_signature
, &entry
->UniquePartitionGUID
, sizeof(found_partition_signature
));
2115 found_partition_number
= i
+ 1;
2120 break; /* This GPT was fully valid, but we didn't find what we are looking for. This
2121 * means there's no reason to check the second copy of the GPT header */
2125 return; /* Not found */
2128 copy
= DuplicateDevicePath(partition_path
);
2130 /* Patch in the data we found */
2131 for (node
= copy
; !IsDevicePathEnd(node
); node
= NextDevicePathNode(node
)) {
2132 HARDDRIVE_DEVICE_PATH
*hd
;
2134 if (DevicePathType(node
) != MEDIA_DEVICE_PATH
)
2137 if (DevicePathSubType(node
) != MEDIA_HARDDRIVE_DP
)
2140 hd
= (HARDDRIVE_DEVICE_PATH
*) node
;
2141 hd
->PartitionNumber
= found_partition_number
;
2142 hd
->PartitionStart
= found_partition_start
;
2143 hd
->PartitionSize
= found_partition_size
;
2144 CopyMem(hd
->Signature
, found_partition_signature
, sizeof(hd
->Signature
));
2145 hd
->MBRType
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
2146 hd
->SignatureType
= SIGNATURE_TYPE_GUID
;
2149 r
= uefi_call_wrapper(BS
->LocateDevicePath
, 3, &BlockIoProtocol
, ©
, &new_device
);
2153 root_dir
= LibOpenRoot(new_device
);
2157 config_entry_add_linux(config
, new_device
, root_dir
);
2158 config_load_entries(config
, new_device
, root_dir
, NULL
);
2161 static EFI_STATUS
image_start(
2162 EFI_HANDLE parent_image
,
2163 const Config
*config
,
2164 const ConfigEntry
*entry
) {
2167 _cleanup_freepool_ EFI_DEVICE_PATH
*path
= NULL
;
2171 path
= FileDevicePath(entry
->device
, entry
->loader
);
2173 Print(L
"Error getting device path.");
2174 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2175 return EFI_INVALID_PARAMETER
;
2178 err
= uefi_call_wrapper(BS
->LoadImage
, 6, FALSE
, parent_image
, path
, NULL
, 0, &image
);
2179 if (EFI_ERROR(err
)) {
2180 Print(L
"Error loading %s: %r", entry
->loader
, err
);
2181 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2185 if (config
->options_edit
)
2186 options
= config
->options_edit
;
2187 else if (entry
->options
)
2188 options
= entry
->options
;
2192 EFI_LOADED_IMAGE
*loaded_image
;
2194 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
2195 parent_image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
2196 if (EFI_ERROR(err
)) {
2197 Print(L
"Error getting LoadedImageProtocol handle: %r", err
);
2198 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2201 loaded_image
->LoadOptions
= options
;
2202 loaded_image
->LoadOptionsSize
= (StrLen(loaded_image
->LoadOptions
)+1) * sizeof(CHAR16
);
2205 /* Try to log any options to the TPM, especially to catch manually edited options */
2206 err
= tpm_log_event(SD_TPM_PCR
,
2207 (EFI_PHYSICAL_ADDRESS
) (UINTN
) loaded_image
->LoadOptions
,
2208 loaded_image
->LoadOptionsSize
, loaded_image
->LoadOptions
);
2209 if (EFI_ERROR(err
)) {
2210 Print(L
"Unable to add image options measurement: %r", err
);
2211 uefi_call_wrapper(BS
->Stall
, 1, 200 * 1000);
2216 efivar_set_time_usec(L
"LoaderTimeExecUSec", 0);
2217 err
= uefi_call_wrapper(BS
->StartImage
, 3, image
, NULL
, NULL
);
2219 uefi_call_wrapper(BS
->UnloadImage
, 1, image
);
2223 static EFI_STATUS
reboot_into_firmware(VOID
) {
2224 _cleanup_freepool_ CHAR8
*b
= NULL
;
2229 osind
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
2231 err
= efivar_get_raw(&global_guid
, L
"OsIndications", &b
, &size
);
2232 if (!EFI_ERROR(err
))
2233 osind
|= (UINT64
)*b
;
2235 err
= efivar_set_raw(&global_guid
, L
"OsIndications", &osind
, sizeof(UINT64
), TRUE
);
2239 err
= uefi_call_wrapper(RT
->ResetSystem
, 4, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
2240 Print(L
"Error calling ResetSystem: %r", err
);
2241 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2245 static VOID
config_free(Config
*config
) {
2248 for (i
= 0; i
< config
->entry_count
; i
++)
2249 config_entry_free(config
->entries
[i
]);
2250 FreePool(config
->entries
);
2251 FreePool(config
->entry_default_pattern
);
2252 FreePool(config
->options_edit
);
2253 FreePool(config
->entry_oneshot
);
2256 static VOID
config_write_entries_to_variable(Config
*config
) {
2257 _cleanup_freepool_ CHAR16
*buffer
= NULL
;
2261 for (i
= 0; i
< config
->entry_count
; i
++)
2262 sz
+= StrLen(config
->entries
[i
]->id
) + 1;
2264 p
= buffer
= AllocatePool(sz
* sizeof(CHAR16
));
2266 for (i
= 0; i
< config
->entry_count
; i
++) {
2269 l
= StrLen(config
->entries
[i
]->id
) + 1;
2270 CopyMem(p
, config
->entries
[i
]->id
, l
* sizeof(CHAR16
));
2275 /* Store the full list of discovered entries. */
2276 (void) efivar_set_raw(&loader_guid
, L
"LoaderEntries", buffer
, (UINT8
*) p
- (UINT8
*) buffer
, FALSE
);
2279 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*sys_table
) {
2280 static const UINT64 loader_features
=
2281 EFI_LOADER_FEATURE_CONFIG_TIMEOUT
|
2282 EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
|
2283 EFI_LOADER_FEATURE_ENTRY_DEFAULT
|
2284 EFI_LOADER_FEATURE_ENTRY_ONESHOT
|
2285 EFI_LOADER_FEATURE_BOOT_COUNTING
|
2286 EFI_LOADER_FEATURE_XBOOTLDR
|
2289 _cleanup_freepool_ CHAR16
*infostr
= NULL
, *typestr
= NULL
;
2292 EFI_LOADED_IMAGE
*loaded_image
;
2294 CHAR16
*loaded_image_path
;
2298 BOOLEAN menu
= FALSE
;
2301 InitializeLib(image
, sys_table
);
2302 init_usec
= time_usec();
2303 efivar_set_time_usec(L
"LoaderTimeInitUSec", init_usec
);
2304 efivar_set(L
"LoaderInfo", L
"systemd-boot " GIT_VERSION
, FALSE
);
2306 infostr
= PoolPrint(L
"%s %d.%02d", ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
2307 efivar_set(L
"LoaderFirmwareInfo", infostr
, FALSE
);
2309 typestr
= PoolPrint(L
"UEFI %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
2310 efivar_set(L
"LoaderFirmwareType", typestr
, FALSE
);
2312 (void) efivar_set_raw(&loader_guid
, L
"LoaderFeatures", &loader_features
, sizeof(loader_features
), FALSE
);
2314 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
2315 image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
2316 if (EFI_ERROR(err
)) {
2317 Print(L
"Error getting a LoadedImageProtocol handle: %r ", err
);
2318 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2322 /* export the device path this image is started from */
2323 if (disk_get_part_uuid(loaded_image
->DeviceHandle
, uuid
) == EFI_SUCCESS
)
2324 efivar_set(L
"LoaderDevicePartUUID", uuid
, FALSE
);
2326 root_dir
= LibOpenRoot(loaded_image
->DeviceHandle
);
2328 Print(L
"Unable to open root directory.");
2329 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2330 return EFI_LOAD_ERROR
;
2333 if (secure_boot_enabled() && shim_loaded()) {
2334 err
= security_policy_install();
2335 if (EFI_ERROR(err
)) {
2336 Print(L
"Error installing security policy: %r ", err
);
2337 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2342 /* the filesystem path to this image, to prevent adding ourselves to the menu */
2343 loaded_image_path
= DevicePathToStr(loaded_image
->FilePath
);
2344 efivar_set(L
"LoaderImageIdentifier", loaded_image_path
, FALSE
);
2346 config_load_defaults(&config
, root_dir
);
2348 /* scan /EFI/Linux/ directory */
2349 config_entry_add_linux(&config
, loaded_image
->DeviceHandle
, root_dir
);
2351 /* scan /loader/entries/\*.conf files */
2352 config_load_entries(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
);
2354 /* Similar, but on any XBOOTLDR partition */
2355 config_load_xbootldr(&config
, loaded_image
->DeviceHandle
);
2357 /* sort entries after version number */
2358 config_sort_entries(&config
);
2360 /* if we find some well-known loaders, add them to the end of the list */
2361 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, NULL
,
2362 L
"auto-windows", 'w', L
"Windows Boot Manager", L
"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
2363 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, NULL
,
2364 L
"auto-efi-shell", 's', L
"EFI Shell", L
"\\shell" EFI_MACHINE_TYPE_NAME
".efi");
2365 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
2366 L
"auto-efi-default", '\0', L
"EFI Default Loader", L
"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME
".efi");
2367 config_entry_add_osx(&config
);
2369 if (config
.auto_firmware
&& efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
2370 UINT64 osind
= (UINT64
)*b
;
2372 if (osind
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
)
2373 config_entry_add_call(&config
,
2374 L
"auto-reboot-to-firmware-setup",
2375 L
"Reboot Into Firmware Interface",
2376 reboot_into_firmware
);
2380 if (config
.entry_count
== 0) {
2381 Print(L
"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
2382 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2386 config_write_entries_to_variable(&config
);
2388 config_title_generate(&config
);
2390 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */
2391 config_default_entry_select(&config
);
2393 /* if no configured entry to select from was found, enable the menu */
2394 if (config
.idx_default
== -1) {
2395 config
.idx_default
= 0;
2396 if (config
.timeout_sec
== 0)
2397 config
.timeout_sec
= 10;
2400 /* select entry or show menu when key is pressed or timeout is set */
2401 if (config
.force_menu
|| config
.timeout_sec
> 0)
2406 err
= console_key_read(&key
, FALSE
);
2407 if (!EFI_ERROR(err
)) {
2410 /* find matching key in config entries */
2411 idx
= entry_lookup_key(&config
, config
.idx_default
, KEYCHAR(key
));
2413 config
.idx_default
= idx
;
2422 entry
= config
.entries
[config
.idx_default
];
2424 efivar_set_time_usec(L
"LoaderTimeMenuUSec", 0);
2425 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 0, 0x10000, 0, NULL
);
2426 if (!menu_run(&config
, &entry
, loaded_image_path
))
2430 /* run special entry like "reboot" */
2436 config_entry_bump_counters(entry
, root_dir
);
2438 /* export the selected boot entry to the system */
2439 efivar_set(L
"LoaderEntrySelected", entry
->id
, FALSE
);
2441 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 5 * 60, 0x10000, 0, NULL
);
2442 err
= image_start(image
, &config
, entry
);
2443 if (EFI_ERROR(err
)) {
2444 graphics_mode(FALSE
);
2445 Print(L
"\nFailed to execute %s (%s): %r\n", entry
->title
, entry
->loader
, err
);
2446 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2451 config
.timeout_sec
= 0;
2455 FreePool(loaded_image_path
);
2456 config_free(&config
);
2457 uefi_call_wrapper(root_dir
->Close
, 1, root_dir
);
2458 uefi_call_wrapper(BS
->CloseProtocol
, 4, image
, &LoadedImageProtocol
, image
, NULL
);