1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "loader-features.h"
15 #include "random-seed.h"
19 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
20 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
23 /* magic string to find in the binary image */
24 static const char __attribute__((used
)) magic
[] = "#### LoaderInfo: systemd-boot " GIT_VERSION
" ####";
26 static const EFI_GUID global_guid
= EFI_GLOBAL_VARIABLE
;
35 CHAR16
*id
; /* The identifier for this entry (note that this id is not necessarily unique though!) */
41 enum loader_type type
;
45 EFI_STATUS (*call
)(VOID
);
46 BOOLEAN no_autoselect
;
56 ConfigEntry
**entries
;
59 INTN idx_default_efivar
;
61 UINTN timeout_sec_config
;
62 INTN timeout_sec_efivar
;
63 CHAR16
*entry_default_pattern
;
64 CHAR16
*entry_oneshot
;
68 BOOLEAN auto_firmware
;
71 enum console_mode_change_type console_mode_change
;
72 RandomSeedMode random_seed_mode
;
75 static VOID
cursor_left(UINTN
*cursor
, UINTN
*first
) {
78 else if ((*first
) > 0)
82 static VOID
cursor_right(
88 if ((*cursor
)+1 < x_max
)
90 else if ((*first
) + (*cursor
) < len
)
94 static BOOLEAN
line_edit(
100 _cleanup_freepool_ CHAR16
*line
= NULL
, *print
= NULL
;
101 UINTN size
, len
, first
, cursor
, clear
;
106 size
= StrLen(line_in
) + 1024;
107 line
= AllocatePool(size
* sizeof(CHAR16
));
108 StrCpy(line
, line_in
);
110 print
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
112 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, TRUE
);
127 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
128 while (clear
> 0 && i
< x_max
-1) {
134 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_pos
);
135 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, print
);
136 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
138 err
= console_key_read(&key
, TRUE
);
143 case KEYPRESS(0, SCAN_ESC
, 0):
144 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'c'):
145 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'g'):
146 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('c')):
147 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('g')):
151 case KEYPRESS(0, SCAN_HOME
, 0):
152 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'a'):
153 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('a')):
154 /* beginning-of-line */
159 case KEYPRESS(0, SCAN_END
, 0):
160 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'e'):
161 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('e')):
163 cursor
= len
- first
;
164 if (cursor
+1 >= x_max
) {
166 first
= len
- (x_max
-1);
170 case KEYPRESS(0, SCAN_DOWN
, 0):
171 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'f'):
172 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_RIGHT
, 0):
174 while (line
[first
+ cursor
] == ' ')
175 cursor_right(&cursor
, &first
, x_max
, len
);
176 while (line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
177 cursor_right(&cursor
, &first
, x_max
, len
);
178 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
181 case KEYPRESS(0, SCAN_UP
, 0):
182 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'b'):
183 case KEYPRESS(EFI_CONTROL_PRESSED
, SCAN_LEFT
, 0):
185 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
186 cursor_left(&cursor
, &first
);
187 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ')
188 cursor_left(&cursor
, &first
);
190 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ')
191 cursor_left(&cursor
, &first
);
192 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
195 case KEYPRESS(0, SCAN_RIGHT
, 0):
196 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'f'):
197 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('f')):
199 if (first
+ cursor
== len
)
201 cursor_right(&cursor
, &first
, x_max
, len
);
202 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
205 case KEYPRESS(0, SCAN_LEFT
, 0):
206 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'b'):
207 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('b')):
209 cursor_left(&cursor
, &first
);
210 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
213 case KEYPRESS(EFI_ALT_PRESSED
, 0, 'd'):
216 for (i
= first
+ cursor
; i
< len
&& line
[i
] == ' '; i
++)
218 for (; i
< len
&& line
[i
] != ' '; i
++)
221 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
222 line
[i
] = line
[i
+ clear
];
227 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'w'):
228 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('w')):
229 case KEYPRESS(EFI_ALT_PRESSED
, 0, CHAR_BACKSPACE
):
230 /* backward-kill-word */
232 if ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] == ' ') {
233 cursor_left(&cursor
, &first
);
235 while ((first
+ cursor
) > 0 && line
[first
+ cursor
] == ' ') {
236 cursor_left(&cursor
, &first
);
240 while ((first
+ cursor
) > 0 && line
[first
+ cursor
-1] != ' ') {
241 cursor_left(&cursor
, &first
);
244 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, cursor
, y_pos
);
246 for (i
= first
+ cursor
; i
+ clear
< len
; i
++)
247 line
[i
] = line
[i
+ clear
];
252 case KEYPRESS(0, SCAN_DELETE
, 0):
253 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'd'):
254 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('d')):
257 if (first
+ cursor
== len
)
259 for (i
= first
+ cursor
; i
< len
; i
++)
265 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'k'):
266 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('k')):
268 line
[first
+ cursor
] = '\0';
269 clear
= len
- (first
+ cursor
);
270 len
= first
+ cursor
;
273 case KEYPRESS(0, 0, CHAR_LINEFEED
):
274 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
275 if (StrCmp(line
, line_in
) != 0)
276 *line_out
= TAKE_PTR(line
);
281 case KEYPRESS(0, 0, CHAR_BACKSPACE
):
284 if (first
== 0 && cursor
== 0)
286 for (i
= first
+ cursor
-1; i
< len
; i
++)
292 if (cursor
> 0 || first
== 0)
294 /* show full line if it fits */
300 /* jump left to see what we delete */
310 case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
311 case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
314 for (i
= len
; i
> first
+ cursor
; i
--)
316 line
[first
+ cursor
] = KEYCHAR(key
);
319 if (cursor
+1 < x_max
)
321 else if (first
+ cursor
< len
)
327 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
331 static UINTN
entry_lookup_key(Config
*config
, UINTN start
, CHAR16 key
) {
337 /* select entry by number key */
338 if (key
>= '1' && key
<= '9') {
340 if (i
> config
->entry_count
)
341 i
= config
->entry_count
;
345 /* find matching key in config entries */
346 for (i
= start
; i
< config
->entry_count
; i
++)
347 if (config
->entries
[i
]->key
== key
)
350 for (i
= 0; i
< start
; i
++)
351 if (config
->entries
[i
]->key
== key
)
357 static VOID
print_status(Config
*config
, CHAR16
*loaded_image_path
) {
360 _cleanup_freepool_ CHAR8
*bootvar
= NULL
, *modevar
= NULL
, *indvar
= NULL
;
361 _cleanup_freepool_ CHAR16
*partstr
= NULL
, *defaultstr
= NULL
;
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: " GIT_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", &bootvar
, &size
) == EFI_SUCCESS
)
378 Print(L
"SecureBoot: %s\n", yes_no(*bootvar
> 0));
380 if (efivar_get_raw(&global_guid
, L
"SetupMode", &modevar
, &size
) == EFI_SUCCESS
)
381 Print(L
"SetupMode: %s\n", *modevar
> 0 ? L
"setup" : L
"user");
384 Print(L
"Shim: present\n");
386 if (efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &indvar
, &size
) == EFI_SUCCESS
)
387 Print(L
"OsIndicationsSupported: %d\n", (UINT64
)*indvar
);
389 Print(L
"\n--- press key ---\n\n");
390 console_key_read(&key
, TRUE
);
392 Print(L
"timeout: %u\n", config
->timeout_sec
);
393 if (config
->timeout_sec_efivar
>= 0)
394 Print(L
"timeout (EFI var): %d\n", config
->timeout_sec_efivar
);
395 Print(L
"timeout (config): %u\n", config
->timeout_sec_config
);
396 if (config
->entry_default_pattern
)
397 Print(L
"default pattern: '%s'\n", config
->entry_default_pattern
);
398 Print(L
"editor: %s\n", yes_no(config
->editor
));
399 Print(L
"auto-entries: %s\n", yes_no(config
->auto_entries
));
400 Print(L
"auto-firmware: %s\n", yes_no(config
->auto_firmware
));
402 switch (config
->random_seed_mode
) {
403 case RANDOM_SEED_OFF
:
404 Print(L
"random-seed-mode: off\n");
406 case RANDOM_SEED_WITH_SYSTEM_TOKEN
:
407 Print(L
"random-seed-node: with-system-token\n");
409 case RANDOM_SEED_ALWAYS
:
410 Print(L
"random-seed-node: always\n");
418 Print(L
"config entry count: %d\n", config
->entry_count
);
419 Print(L
"entry selected idx: %d\n", config
->idx_default
);
420 if (config
->idx_default_efivar
>= 0)
421 Print(L
"entry EFI var idx: %d\n", config
->idx_default_efivar
);
424 if (efivar_get_int(L
"LoaderConfigTimeout", &i
) == EFI_SUCCESS
)
425 Print(L
"LoaderConfigTimeout: %u\n", i
);
427 if (config
->entry_oneshot
)
428 Print(L
"LoaderEntryOneShot: %s\n", config
->entry_oneshot
);
429 if (efivar_get(L
"LoaderDevicePartUUID", &partstr
) == EFI_SUCCESS
)
430 Print(L
"LoaderDevicePartUUID: %s\n", partstr
);
431 if (efivar_get(L
"LoaderEntryDefault", &defaultstr
) == EFI_SUCCESS
)
432 Print(L
"LoaderEntryDefault: %s\n", defaultstr
);
434 Print(L
"\n--- press key ---\n\n");
435 console_key_read(&key
, TRUE
);
437 for (i
= 0; i
< config
->entry_count
; i
++) {
440 if (key
== KEYPRESS(0, SCAN_ESC
, 0) || key
== KEYPRESS(0, 0, 'q'))
443 entry
= config
->entries
[i
];
444 Print(L
"config entry: %d/%d\n", i
+1, config
->entry_count
);
446 Print(L
"id '%s'\n", entry
->id
);
447 Print(L
"title show '%s'\n", entry
->title_show
);
449 Print(L
"title '%s'\n", entry
->title
);
451 Print(L
"version '%s'\n", entry
->version
);
452 if (entry
->machine_id
)
453 Print(L
"machine-id '%s'\n", entry
->machine_id
);
455 EFI_DEVICE_PATH
*device_path
;
457 device_path
= DevicePathFromHandle(entry
->device
);
459 _cleanup_freepool_ CHAR16
*str
;
461 str
= DevicePathToStr(device_path
);
462 Print(L
"device handle '%s'\n", str
);
466 Print(L
"loader '%s'\n", entry
->loader
);
468 Print(L
"options '%s'\n", entry
->options
);
469 Print(L
"auto-select %s\n", yes_no(!entry
->no_autoselect
));
471 Print(L
"internal call yes\n");
473 if (entry
->tries_left
!= (UINTN
) -1)
474 Print(L
"counting boots yes\n"
477 "current path %s\\%s\n"
478 "next path %s\\%s\n",
481 entry
->path
, entry
->current_name
,
482 entry
->path
, entry
->next_name
);
484 Print(L
"\n--- press key ---\n\n");
485 console_key_read(&key
, TRUE
);
488 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
491 static BOOLEAN
menu_run(
493 ConfigEntry
**chosen_entry
,
494 CHAR16
*loaded_image_path
) {
499 UINTN idx_highlight_prev
;
515 BOOLEAN exit
= FALSE
;
517 BOOLEAN wait
= FALSE
;
518 BOOLEAN cleared_screen
= FALSE
;
520 graphics_mode(FALSE
);
521 uefi_call_wrapper(ST
->ConIn
->Reset
, 2, ST
->ConIn
, FALSE
);
522 uefi_call_wrapper(ST
->ConOut
->EnableCursor
, 2, ST
->ConOut
, FALSE
);
523 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
525 /* draw a single character to make ClearScreen work on some firmware */
526 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
" ");
528 if (config
->console_mode_change
!= CONSOLE_MODE_KEEP
) {
529 err
= console_set_mode(&config
->console_mode
, config
->console_mode_change
);
531 cleared_screen
= TRUE
;
535 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
537 if (config
->console_mode_change
!= CONSOLE_MODE_KEEP
&& EFI_ERROR(err
))
538 Print(L
"Error switching console mode to %ld: %r.\r", (UINT64
)config
->console_mode
, err
);
540 err
= uefi_call_wrapper(ST
->ConOut
->QueryMode
, 4, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
541 if (EFI_ERROR(err
)) {
546 /* we check 10 times per second for a keystroke */
547 if (config
->timeout_sec
> 0)
548 timeout_remain
= config
->timeout_sec
* 10;
552 idx_highlight
= config
->idx_default
;
553 idx_highlight_prev
= 0;
555 visible_max
= y_max
- 2;
557 if ((UINTN
)config
->idx_default
>= visible_max
)
558 idx_first
= config
->idx_default
-1;
562 idx_last
= idx_first
+ visible_max
-1;
567 /* length of the longest entry */
569 for (i
= 0; i
< config
->entry_count
; i
++) {
572 entry_len
= StrLen(config
->entries
[i
]->title_show
);
573 if (line_width
< entry_len
)
574 line_width
= entry_len
;
576 if (line_width
> x_max
-6)
577 line_width
= x_max
-6;
579 /* offsets to center the entries on the screen */
580 x_start
= (x_max
- (line_width
)) / 2;
581 if (config
->entry_count
< visible_max
)
582 y_start
= ((visible_max
- config
->entry_count
) / 2) + 1;
586 /* menu entries title lines */
587 lines
= AllocatePool(sizeof(CHAR16
*) * config
->entry_count
);
588 for (i
= 0; i
< config
->entry_count
; i
++) {
591 lines
[i
] = AllocatePool(((x_max
+1) * sizeof(CHAR16
)));
592 for (j
= 0; j
< x_start
; j
++)
595 for (k
= 0; config
->entries
[i
]->title_show
[k
] != '\0' && j
< x_max
; j
++, k
++)
596 lines
[i
][j
] = config
->entries
[i
]->title_show
[k
];
598 for (; j
< x_max
; j
++)
600 lines
[i
][x_max
] = '\0';
604 clearline
= AllocatePool((x_max
+1) * sizeof(CHAR16
));
605 for (i
= 0; i
< x_max
; i
++)
613 for (i
= 0; i
< config
->entry_count
; i
++) {
614 if (i
< idx_first
|| i
> idx_last
)
616 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ i
- idx_first
);
617 if (i
== idx_highlight
)
618 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
619 EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
621 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
,
622 EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
623 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[i
]);
624 if ((INTN
)i
== config
->idx_default_efivar
) {
625 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ i
- idx_first
);
626 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
630 } else if (highlight
) {
631 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight_prev
- idx_first
);
632 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
633 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight_prev
]);
634 if ((INTN
)idx_highlight_prev
== config
->idx_default_efivar
) {
635 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight_prev
- idx_first
);
636 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
639 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_start
+ idx_highlight
- idx_first
);
640 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_BLACK
|EFI_BACKGROUND_LIGHTGRAY
);
641 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, lines
[idx_highlight
]);
642 if ((INTN
)idx_highlight
== config
->idx_default_efivar
) {
643 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, x_start
-3, y_start
+ idx_highlight
- idx_first
);
644 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, L
"=>");
649 if (timeout_remain
> 0) {
651 status
= PoolPrint(L
"Boot in %d sec.", (timeout_remain
+ 5) / 10);
654 /* print status at last line of screen */
660 len
= StrLen(status
);
662 x
= (x_max
- len
) / 2;
665 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
666 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
667 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+ (x_max
- x
));
668 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, status
);
669 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1 + x
+ len
);
672 err
= console_key_read(&key
, wait
);
673 if (EFI_ERROR(err
)) {
674 /* timeout reached */
675 if (timeout_remain
== 0) {
680 /* sleep and update status */
681 if (timeout_remain
> 0) {
682 uefi_call_wrapper(BS
->Stall
, 1, 100 * 1000);
687 /* timeout disabled, wait for next key */
694 /* clear status after keystroke */
698 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
699 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
700 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
703 idx_highlight_prev
= idx_highlight
;
706 case KEYPRESS(0, SCAN_UP
, 0):
707 case KEYPRESS(0, 0, 'k'):
708 if (idx_highlight
> 0)
712 case KEYPRESS(0, SCAN_DOWN
, 0):
713 case KEYPRESS(0, 0, 'j'):
714 if (idx_highlight
< config
->entry_count
-1)
718 case KEYPRESS(0, SCAN_HOME
, 0):
719 case KEYPRESS(EFI_ALT_PRESSED
, 0, '<'):
720 if (idx_highlight
> 0) {
726 case KEYPRESS(0, SCAN_END
, 0):
727 case KEYPRESS(EFI_ALT_PRESSED
, 0, '>'):
728 if (idx_highlight
< config
->entry_count
-1) {
730 idx_highlight
= config
->entry_count
-1;
734 case KEYPRESS(0, SCAN_PAGE_UP
, 0):
735 if (idx_highlight
> visible_max
)
736 idx_highlight
-= visible_max
;
741 case KEYPRESS(0, SCAN_PAGE_DOWN
, 0):
742 idx_highlight
+= visible_max
;
743 if (idx_highlight
> config
->entry_count
-1)
744 idx_highlight
= config
->entry_count
-1;
747 case KEYPRESS(0, 0, CHAR_LINEFEED
):
748 case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN
):
752 case KEYPRESS(0, SCAN_F1
, 0):
753 case KEYPRESS(0, 0, 'h'):
754 case KEYPRESS(0, 0, '?'):
755 status
= StrDuplicate(L
"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
758 case KEYPRESS(0, 0, 'Q'):
763 case KEYPRESS(0, 0, 'd'):
764 if (config
->idx_default_efivar
!= (INTN
)idx_highlight
) {
765 /* store the selected entry in a persistent EFI variable */
766 efivar_set(L
"LoaderEntryDefault", config
->entries
[idx_highlight
]->id
, TRUE
);
767 config
->idx_default_efivar
= idx_highlight
;
768 status
= StrDuplicate(L
"Default boot entry selected.");
770 /* clear the default entry EFI variable */
771 efivar_set(L
"LoaderEntryDefault", NULL
, TRUE
);
772 config
->idx_default_efivar
= -1;
773 status
= StrDuplicate(L
"Default boot entry cleared.");
778 case KEYPRESS(0, 0, '-'):
779 case KEYPRESS(0, 0, 'T'):
780 if (config
->timeout_sec_efivar
> 0) {
781 config
->timeout_sec_efivar
--;
782 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
783 if (config
->timeout_sec_efivar
> 0)
784 status
= PoolPrint(L
"Menu timeout set to %d sec.", config
->timeout_sec_efivar
);
786 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
787 } else if (config
->timeout_sec_efivar
<= 0){
788 config
->timeout_sec_efivar
= -1;
789 efivar_set(L
"LoaderConfigTimeout", NULL
, TRUE
);
790 if (config
->timeout_sec_config
> 0)
791 status
= PoolPrint(L
"Menu timeout of %d sec is defined by configuration file.",
792 config
->timeout_sec_config
);
794 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
798 case KEYPRESS(0, 0, '+'):
799 case KEYPRESS(0, 0, 't'):
800 if (config
->timeout_sec_efivar
== -1 && config
->timeout_sec_config
== 0)
801 config
->timeout_sec_efivar
++;
802 config
->timeout_sec_efivar
++;
803 efivar_set_int(L
"LoaderConfigTimeout", config
->timeout_sec_efivar
, TRUE
);
804 if (config
->timeout_sec_efivar
> 0)
805 status
= PoolPrint(L
"Menu timeout set to %d sec.",
806 config
->timeout_sec_efivar
);
808 status
= StrDuplicate(L
"Menu disabled. Hold down key at bootup to show menu.");
811 case KEYPRESS(0, 0, 'e'):
812 /* only the options of configured entries can be edited */
813 if (!config
->editor
|| config
->entries
[idx_highlight
]->type
== LOADER_UNDEFINED
)
815 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_LIGHTGRAY
|EFI_BACKGROUND_BLACK
);
816 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
817 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
818 if (line_edit(config
->entries
[idx_highlight
]->options
, &config
->options_edit
, x_max
-1, y_max
-1))
820 uefi_call_wrapper(ST
->ConOut
->SetCursorPosition
, 3, ST
->ConOut
, 0, y_max
-1);
821 uefi_call_wrapper(ST
->ConOut
->OutputString
, 2, ST
->ConOut
, clearline
+1);
824 case KEYPRESS(0, 0, 'v'):
825 status
= PoolPrint(L
"systemd-boot " GIT_VERSION
" (" EFI_MACHINE_TYPE_NAME
"), UEFI Specification %d.%02d, Vendor %s %d.%02d",
826 ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff,
827 ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
830 case KEYPRESS(0, 0, 'P'):
831 print_status(config
, loaded_image_path
);
835 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, 'l'):
836 case KEYPRESS(EFI_CONTROL_PRESSED
, 0, CHAR_CTRL('l')):
841 /* jump with a hotkey directly to a matching entry */
842 idx
= entry_lookup_key(config
, idx_highlight
+1, KEYCHAR(key
));
849 if (idx_highlight
> idx_last
) {
850 idx_last
= idx_highlight
;
851 idx_first
= 1 + idx_highlight
- visible_max
;
853 } else if (idx_highlight
< idx_first
) {
854 idx_first
= idx_highlight
;
855 idx_last
= idx_highlight
+ visible_max
-1;
859 if (!refresh
&& idx_highlight
!= idx_highlight_prev
)
863 *chosen_entry
= config
->entries
[idx_highlight
];
865 for (i
= 0; i
< config
->entry_count
; i
++)
870 uefi_call_wrapper(ST
->ConOut
->SetAttribute
, 2, ST
->ConOut
, EFI_WHITE
|EFI_BACKGROUND_BLACK
);
871 uefi_call_wrapper(ST
->ConOut
->ClearScreen
, 1, ST
->ConOut
);
875 static VOID
config_add_entry(Config
*config
, ConfigEntry
*entry
) {
876 if ((config
->entry_count
& 15) == 0) {
879 i
= config
->entry_count
+ 16;
880 if (config
->entry_count
== 0)
881 config
->entries
= AllocatePool(sizeof(VOID
*) * i
);
883 config
->entries
= ReallocatePool(config
->entries
,
884 sizeof(VOID
*) * config
->entry_count
, sizeof(VOID
*) * i
);
886 config
->entries
[config
->entry_count
++] = entry
;
889 static VOID
config_entry_free(ConfigEntry
*entry
) {
894 FreePool(entry
->title_show
);
895 FreePool(entry
->title
);
896 FreePool(entry
->version
);
897 FreePool(entry
->machine_id
);
898 FreePool(entry
->loader
);
899 FreePool(entry
->options
);
900 FreePool(entry
->path
);
901 FreePool(entry
->current_name
);
902 FreePool(entry
->next_name
);
906 static BOOLEAN
is_digit(CHAR16 c
) {
907 return (c
>= '0') && (c
<= '9');
910 static UINTN
c_order(CHAR16 c
) {
915 else if ((c
>= 'a') && (c
<= 'z'))
921 static INTN
str_verscmp(CHAR16
*s1
, CHAR16
*s2
) {
928 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
931 order
= c_order(*s1
) - c_order(*s2
);
944 while (is_digit(*s1
) && is_digit(*s2
)) {
960 return StrCmp(os1
, os2
);
963 static CHAR8
*line_get_key_value(
975 line
= content
+ *pos
;
980 while (line
[linelen
] && !strchra((CHAR8
*)"\n\r", line
[linelen
]))
983 /* move pos to next line */
993 line
[linelen
] = '\0';
995 /* remove leading whitespace */
996 while (strchra((CHAR8
*)" \t", *line
)) {
1001 /* remove trailing whitespace */
1002 while (linelen
> 0 && strchra((CHAR8
*)" \t", line
[linelen
-1]))
1004 line
[linelen
] = '\0';
1009 /* split key/value */
1011 while (*value
&& !strchra(sep
, *value
))
1017 while (*value
&& strchra(sep
, *value
))
1021 if (value
[0] == '"' && line
[linelen
-1] == '"') {
1023 line
[linelen
-1] = '\0';
1031 static VOID
config_defaults_load_from_file(Config
*config
, CHAR8
*content
) {
1036 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1037 if (strcmpa((CHAR8
*)"timeout", key
) == 0) {
1038 _cleanup_freepool_ CHAR16
*s
= NULL
;
1040 s
= stra_to_str(value
);
1041 config
->timeout_sec_config
= Atoi(s
);
1042 config
->timeout_sec
= config
->timeout_sec_config
;
1046 if (strcmpa((CHAR8
*)"default", key
) == 0) {
1047 FreePool(config
->entry_default_pattern
);
1048 config
->entry_default_pattern
= stra_to_str(value
);
1049 StrLwr(config
->entry_default_pattern
);
1053 if (strcmpa((CHAR8
*)"editor", key
) == 0) {
1056 if (EFI_ERROR(parse_boolean(value
, &on
)))
1059 config
->editor
= on
;
1063 if (strcmpa((CHAR8
*)"auto-entries", key
) == 0) {
1066 if (EFI_ERROR(parse_boolean(value
, &on
)))
1069 config
->auto_entries
= on
;
1073 if (strcmpa((CHAR8
*)"auto-firmware", key
) == 0) {
1076 if (EFI_ERROR(parse_boolean(value
, &on
)))
1079 config
->auto_firmware
= on
;
1083 if (strcmpa((CHAR8
*)"console-mode", key
) == 0) {
1084 if (strcmpa((CHAR8
*)"auto", value
) == 0)
1085 config
->console_mode_change
= CONSOLE_MODE_AUTO
;
1086 else if (strcmpa((CHAR8
*)"max", value
) == 0)
1087 config
->console_mode_change
= CONSOLE_MODE_MAX
;
1088 else if (strcmpa((CHAR8
*)"keep", value
) == 0)
1089 config
->console_mode_change
= CONSOLE_MODE_KEEP
;
1091 _cleanup_freepool_ CHAR16
*s
= NULL
;
1093 s
= stra_to_str(value
);
1094 config
->console_mode
= Atoi(s
);
1095 config
->console_mode_change
= CONSOLE_MODE_SET
;
1101 if (strcmpa((CHAR8
*) "random-seed-mode", key
) == 0) {
1102 if (strcmpa((CHAR8
*) "off", value
) == 0)
1103 config
->random_seed_mode
= RANDOM_SEED_OFF
;
1104 else if (strcmpa((CHAR8
*) "with-system-token", value
) == 0)
1105 config
->random_seed_mode
= RANDOM_SEED_WITH_SYSTEM_TOKEN
;
1106 else if (strcmpa((CHAR8
*) "always", value
) == 0)
1107 config
->random_seed_mode
= RANDOM_SEED_ALWAYS
;
1111 if (EFI_ERROR(parse_boolean(value
, &on
)))
1114 config
->random_seed_mode
= on
? RANDOM_SEED_ALWAYS
: RANDOM_SEED_OFF
;
1120 static VOID
config_entry_parse_tries(
1126 UINTN left
= (UINTN
) -1, done
= (UINTN
) -1, factor
= 1, i
, next_left
, next_done
;
1127 _cleanup_freepool_ CHAR16
*prefix
= NULL
;
1130 * Parses a suffix of two counters (one going down, one going up) in the form "+LEFT-DONE" from the end of the
1131 * filename (but before the .efi/.conf suffix), where the "-DONE" part is optional and may be left out (in
1132 * which case that counter as assumed to be zero, i.e. the missing part is synonymous to "-0").
1134 * Names we grok, and the series they result in:
1136 * foobar+3.efi → foobar+2-1.efi → foobar+1-2.efi → foobar+0-3.efi → STOP!
1137 * foobar+4-0.efi → foobar+3-1.efi → foobar+2-2.efi → foobar+1-3.efi → foobar+0-4.efi → STOP!
1142 /* Chop off any suffix such as ".conf" or ".efi" */
1144 UINTN suffix_length
;
1146 suffix_length
= StrLen(suffix
);
1147 if (i
< suffix_length
)
1153 /* Go backwards through the string and parse everything we encounter */
1163 if (left
== (UINTN
) -1) /* didn't read at least one digit for 'left'? */
1166 if (done
== (UINTN
) -1) /* no 'done' counter? If so, it's equivalent to 0 */
1172 if (left
== (UINTN
) -1) /* didn't parse any digit yet? */
1175 if (done
!= (UINTN
) -1) /* already encountered a dash earlier? */
1178 /* So we encountered a dash. This means this counter is of the form +LEFT-DONE. Let's assign
1179 * what we already parsed to 'done', and start fresh for the 'left' part. */
1189 if (left
== (UINTN
) -1)
1190 left
= file
[i
] - '0';
1192 UINTN new_left
, digit
;
1194 digit
= file
[i
] - '0';
1195 if (digit
> (UINTN
) -1 / factor
) /* overflow check */
1198 new_left
= left
+ digit
* factor
;
1199 if (new_left
< left
) /* overflow check */
1202 if (new_left
== (UINTN
) -1) /* don't allow us to be confused */
1206 new_factor
= factor
* 10;
1207 if (new_factor
< factor
) /* overflow chck */
1210 factor
= new_factor
;
1220 entry
->tries_left
= left
;
1221 entry
->tries_done
= done
;
1223 entry
->path
= StrDuplicate(path
);
1224 entry
->current_name
= StrDuplicate(file
);
1226 next_left
= left
<= 0 ? 0 : left
- 1;
1227 next_done
= done
>= (UINTN
) -2 ? (UINTN
) -2 : done
+ 1;
1229 prefix
= StrDuplicate(file
);
1232 entry
->next_name
= PoolPrint(L
"%s+%u-%u%s", prefix
, next_left
, next_done
, suffix
?: L
"");
1235 static VOID
config_entry_bump_counters(
1237 EFI_FILE_HANDLE root_dir
) {
1239 _cleanup_freepool_ CHAR16
* old_path
= NULL
, *new_path
= NULL
;
1240 _cleanup_(FileHandleClosep
) EFI_FILE_HANDLE handle
= NULL
;
1241 static EFI_GUID EfiFileInfoGuid
= EFI_FILE_INFO_ID
;
1242 _cleanup_freepool_ EFI_FILE_INFO
*file_info
= NULL
;
1243 UINTN file_info_size
, a
, b
;
1246 if (entry
->tries_left
== (UINTN
) -1)
1249 if (!entry
->path
|| !entry
->current_name
|| !entry
->next_name
)
1252 old_path
= PoolPrint(L
"%s\\%s", entry
->path
, entry
->current_name
);
1254 r
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, old_path
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0ULL);
1258 a
= StrLen(entry
->current_name
);
1259 b
= StrLen(entry
->next_name
);
1261 file_info_size
= OFFSETOF(EFI_FILE_INFO
, FileName
) + (a
> b
? a
: b
) + 1;
1264 file_info
= AllocatePool(file_info_size
);
1266 r
= uefi_call_wrapper(handle
->GetInfo
, 4, handle
, &EfiFileInfoGuid
, &file_info_size
, file_info
);
1270 if (r
!= EFI_BUFFER_TOO_SMALL
|| file_info_size
* 2 < file_info_size
) {
1271 Print(L
"\nFailed to get file info for '%s': %r\n", old_path
, r
);
1272 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1276 file_info_size
*= 2;
1277 FreePool(file_info
);
1280 /* And rename the file */
1281 StrCpy(file_info
->FileName
, entry
->next_name
);
1282 r
= uefi_call_wrapper(handle
->SetInfo
, 4, handle
, &EfiFileInfoGuid
, file_info_size
, file_info
);
1284 Print(L
"\nFailed to rename '%s' to '%s', ignoring: %r\n", old_path
, entry
->next_name
, r
);
1285 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
1289 /* Flush everything to disk, just in case… */
1290 (void) uefi_call_wrapper(handle
->Flush
, 1, handle
);
1292 /* Let's tell the OS that we renamed this file, so that it knows what to rename to the counter-less name on
1294 new_path
= PoolPrint(L
"%s\\%s", entry
->path
, entry
->next_name
);
1295 efivar_set(L
"LoaderBootCountPath", new_path
, FALSE
);
1297 /* If the file we just renamed is the loader path, then let's update that. */
1298 if (StrCmp(entry
->loader
, old_path
) == 0) {
1299 FreePool(entry
->loader
);
1300 entry
->loader
= TAKE_PTR(new_path
);
1304 static VOID
config_entry_add_from_file(
1310 CHAR16
*loaded_image_path
) {
1317 _cleanup_freepool_ CHAR16
*initrd
= NULL
;
1319 entry
= AllocatePool(sizeof(ConfigEntry
));
1321 *entry
= (ConfigEntry
) {
1322 .tries_done
= (UINTN
) -1,
1323 .tries_left
= (UINTN
) -1,
1326 while ((line
= line_get_key_value(content
, (CHAR8
*)" \t", &pos
, &key
, &value
))) {
1327 if (strcmpa((CHAR8
*)"title", key
) == 0) {
1328 FreePool(entry
->title
);
1329 entry
->title
= stra_to_str(value
);
1333 if (strcmpa((CHAR8
*)"version", key
) == 0) {
1334 FreePool(entry
->version
);
1335 entry
->version
= stra_to_str(value
);
1339 if (strcmpa((CHAR8
*)"machine-id", key
) == 0) {
1340 FreePool(entry
->machine_id
);
1341 entry
->machine_id
= stra_to_str(value
);
1345 if (strcmpa((CHAR8
*)"linux", key
) == 0) {
1346 FreePool(entry
->loader
);
1347 entry
->type
= LOADER_LINUX
;
1348 entry
->loader
= stra_to_path(value
);
1353 if (strcmpa((CHAR8
*)"efi", key
) == 0) {
1354 entry
->type
= LOADER_EFI
;
1355 FreePool(entry
->loader
);
1356 entry
->loader
= stra_to_path(value
);
1358 /* do not add an entry for ourselves */
1359 if (loaded_image_path
&& StriCmp(entry
->loader
, loaded_image_path
) == 0) {
1360 entry
->type
= LOADER_UNDEFINED
;
1366 if (strcmpa((CHAR8
*)"architecture", key
) == 0) {
1367 /* do not add an entry for an EFI image of architecture not matching with that of the image */
1368 if (strcmpa((CHAR8
*)EFI_MACHINE_TYPE_NAME
, value
) != 0) {
1369 entry
->type
= LOADER_UNDEFINED
;
1375 if (strcmpa((CHAR8
*)"initrd", key
) == 0) {
1376 _cleanup_freepool_ CHAR16
*new = NULL
;
1378 new = stra_to_path(value
);
1382 s
= PoolPrint(L
"%s initrd=%s", initrd
, new);
1386 initrd
= PoolPrint(L
"initrd=%s", new);
1391 if (strcmpa((CHAR8
*)"options", key
) == 0) {
1392 _cleanup_freepool_ CHAR16
*new = NULL
;
1394 new = stra_to_str(value
);
1395 if (entry
->options
) {
1398 s
= PoolPrint(L
"%s %s", entry
->options
, new);
1399 FreePool(entry
->options
);
1402 entry
->options
= TAKE_PTR(new);
1408 if (entry
->type
== LOADER_UNDEFINED
) {
1409 config_entry_free(entry
);
1413 /* add initrd= to options */
1414 if (entry
->type
== LOADER_LINUX
&& initrd
) {
1415 if (entry
->options
) {
1418 s
= PoolPrint(L
"%s %s", initrd
, entry
->options
);
1419 FreePool(entry
->options
);
1422 entry
->options
= TAKE_PTR(initrd
);
1425 entry
->device
= device
;
1426 entry
->id
= StrDuplicate(file
);
1427 len
= StrLen(entry
->id
);
1428 /* remove ".conf" */
1430 entry
->id
[len
- 5] = '\0';
1433 config_add_entry(config
, entry
);
1435 config_entry_parse_tries(entry
, path
, file
, L
".conf");
1438 static VOID
config_load_defaults(Config
*config
, EFI_FILE
*root_dir
) {
1439 _cleanup_freepool_ CHAR8
*content
= NULL
;
1443 *config
= (Config
) {
1445 .auto_entries
= TRUE
,
1446 .auto_firmware
= TRUE
,
1447 .random_seed_mode
= RANDOM_SEED_WITH_SYSTEM_TOKEN
,
1450 err
= file_read(root_dir
, L
"\\loader\\loader.conf", 0, 0, &content
, NULL
);
1451 if (!EFI_ERROR(err
))
1452 config_defaults_load_from_file(config
, content
);
1454 err
= efivar_get_int(L
"LoaderConfigTimeout", &sec
);
1455 if (!EFI_ERROR(err
)) {
1456 config
->timeout_sec_efivar
= sec
> INTN_MAX
? INTN_MAX
: sec
;
1457 config
->timeout_sec
= sec
;
1459 config
->timeout_sec_efivar
= -1;
1461 err
= efivar_get_int(L
"LoaderConfigTimeoutOneShot", &sec
);
1462 if (!EFI_ERROR(err
)) {
1463 /* Unset variable now, after all it's "one shot". */
1464 (void) efivar_set(L
"LoaderConfigTimeoutOneShot", NULL
, TRUE
);
1466 config
->timeout_sec
= sec
;
1467 config
->force_menu
= TRUE
; /* force the menu when this is set */
1471 static VOID
config_load_entries(
1475 CHAR16
*loaded_image_path
) {
1477 EFI_FILE_HANDLE entries_dir
;
1480 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &entries_dir
, L
"\\loader\\entries", EFI_FILE_MODE_READ
, 0ULL);
1481 if (!EFI_ERROR(err
)) {
1486 _cleanup_freepool_ CHAR8
*content
= NULL
;
1489 bufsize
= sizeof(buf
);
1490 err
= uefi_call_wrapper(entries_dir
->Read
, 3, entries_dir
, &bufsize
, buf
);
1491 if (bufsize
== 0 || EFI_ERROR(err
))
1494 f
= (EFI_FILE_INFO
*) buf
;
1495 if (f
->FileName
[0] == '.')
1497 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1500 len
= StrLen(f
->FileName
);
1503 if (StriCmp(f
->FileName
+ len
- 5, L
".conf") != 0)
1505 if (StrnCmp(f
->FileName
, L
"auto-", 5) == 0)
1508 err
= file_read(entries_dir
, f
->FileName
, 0, 0, &content
, NULL
);
1509 if (!EFI_ERROR(err
))
1510 config_entry_add_from_file(config
, device
, L
"\\loader\\entries", f
->FileName
, content
, loaded_image_path
);
1512 uefi_call_wrapper(entries_dir
->Close
, 1, entries_dir
);
1516 static INTN
config_entry_compare(ConfigEntry
*a
, ConfigEntry
*b
) {
1519 /* Order entries that have no tries left to the end of the list */
1520 if (a
->tries_left
!= 0 && b
->tries_left
== 0)
1522 if (a
->tries_left
== 0 && b
->tries_left
!= 0)
1525 r
= str_verscmp(a
->id
, b
->id
);
1529 if (a
->tries_left
== (UINTN
) -1 ||
1530 b
->tries_left
== (UINTN
) -1)
1533 /* If both items have boot counting, and otherwise are identical, put the entry with more tries left first */
1534 if (a
->tries_left
> b
->tries_left
)
1536 if (a
->tries_left
< b
->tries_left
)
1539 /* If they have the same number of tries left, then let the one win which was tried fewer times so far */
1540 if (a
->tries_done
< b
->tries_done
)
1542 if (a
->tries_done
> b
->tries_done
)
1548 static VOID
config_sort_entries(Config
*config
) {
1551 for (i
= 1; i
< config
->entry_count
; i
++) {
1556 for (k
= 0; k
< config
->entry_count
- i
; k
++) {
1559 if (config_entry_compare(config
->entries
[k
], config
->entries
[k
+1]) <= 0)
1562 entry
= config
->entries
[k
];
1563 config
->entries
[k
] = config
->entries
[k
+1];
1564 config
->entries
[k
+1] = entry
;
1572 static INTN
config_entry_find(Config
*config
, CHAR16
*id
) {
1575 for (i
= 0; i
< config
->entry_count
; i
++)
1576 if (StrCmp(config
->entries
[i
]->id
, id
) == 0)
1582 static VOID
config_default_entry_select(Config
*config
) {
1583 _cleanup_freepool_ CHAR16
*entry_oneshot
= NULL
, *entry_default
= NULL
;
1588 * The EFI variable to specify a boot entry for the next, and only the
1589 * next reboot. The variable is always cleared directly after it is read.
1591 err
= efivar_get(L
"LoaderEntryOneShot", &entry_oneshot
);
1592 if (!EFI_ERROR(err
)) {
1594 config
->entry_oneshot
= StrDuplicate(entry_oneshot
);
1595 efivar_set(L
"LoaderEntryOneShot", NULL
, TRUE
);
1597 i
= config_entry_find(config
, entry_oneshot
);
1599 config
->idx_default
= i
;
1605 * The EFI variable to select the default boot entry overrides the
1606 * configured pattern. The variable can be set and cleared by pressing
1607 * the 'd' key in the loader selection menu, the entry is marked with
1610 err
= efivar_get(L
"LoaderEntryDefault", &entry_default
);
1611 if (!EFI_ERROR(err
)) {
1613 i
= config_entry_find(config
, entry_default
);
1615 config
->idx_default
= i
;
1616 config
->idx_default_efivar
= i
;
1620 config
->idx_default_efivar
= -1;
1622 if (config
->entry_count
== 0)
1626 * Match the pattern from the end of the list to the start, find last
1627 * entry (largest number) matching the given pattern.
1629 if (config
->entry_default_pattern
) {
1630 i
= config
->entry_count
;
1632 if (config
->entries
[i
]->no_autoselect
)
1634 if (MetaiMatch(config
->entries
[i
]->id
, config
->entry_default_pattern
)) {
1635 config
->idx_default
= i
;
1641 /* select the last suitable entry */
1642 i
= config
->entry_count
;
1644 if (config
->entries
[i
]->no_autoselect
)
1646 config
->idx_default
= i
;
1650 /* no entry found */
1651 config
->idx_default
= -1;
1654 static BOOLEAN
find_nonunique(ConfigEntry
**entries
, UINTN entry_count
) {
1655 BOOLEAN non_unique
= FALSE
;
1658 for (i
= 0; i
< entry_count
; i
++)
1659 entries
[i
]->non_unique
= FALSE
;
1661 for (i
= 0; i
< entry_count
; i
++)
1662 for (k
= 0; k
< entry_count
; k
++) {
1665 if (StrCmp(entries
[i
]->title_show
, entries
[k
]->title_show
) != 0)
1668 non_unique
= entries
[i
]->non_unique
= entries
[k
]->non_unique
= TRUE
;
1674 /* generate a unique title, avoiding non-distinguishable menu entries */
1675 static VOID
config_title_generate(Config
*config
) {
1679 for (i
= 0; i
< config
->entry_count
; i
++) {
1682 FreePool(config
->entries
[i
]->title_show
);
1683 title
= config
->entries
[i
]->title
;
1685 title
= config
->entries
[i
]->id
;
1686 config
->entries
[i
]->title_show
= StrDuplicate(title
);
1689 if (!find_nonunique(config
->entries
, config
->entry_count
))
1692 /* add version to non-unique titles */
1693 for (i
= 0; i
< config
->entry_count
; i
++) {
1696 if (!config
->entries
[i
]->non_unique
)
1698 if (!config
->entries
[i
]->version
)
1701 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->version
);
1702 FreePool(config
->entries
[i
]->title_show
);
1703 config
->entries
[i
]->title_show
= s
;
1706 if (!find_nonunique(config
->entries
, config
->entry_count
))
1709 /* add machine-id to non-unique titles */
1710 for (i
= 0; i
< config
->entry_count
; i
++) {
1712 _cleanup_freepool_ CHAR16
*m
= NULL
;
1714 if (!config
->entries
[i
]->non_unique
)
1716 if (!config
->entries
[i
]->machine_id
)
1719 m
= StrDuplicate(config
->entries
[i
]->machine_id
);
1721 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, m
);
1722 FreePool(config
->entries
[i
]->title_show
);
1723 config
->entries
[i
]->title_show
= s
;
1726 if (!find_nonunique(config
->entries
, config
->entry_count
))
1729 /* add file name to non-unique titles */
1730 for (i
= 0; i
< config
->entry_count
; i
++) {
1733 if (!config
->entries
[i
]->non_unique
)
1735 s
= PoolPrint(L
"%s (%s)", config
->entries
[i
]->title_show
, config
->entries
[i
]->id
);
1736 FreePool(config
->entries
[i
]->title_show
);
1737 config
->entries
[i
]->title_show
= s
;
1738 config
->entries
[i
]->non_unique
= FALSE
;
1742 static BOOLEAN
config_entry_add_call(
1746 EFI_STATUS (*call
)(VOID
)) {
1750 entry
= AllocatePool(sizeof(ConfigEntry
));
1751 *entry
= (ConfigEntry
) {
1752 .id
= StrDuplicate(id
),
1753 .title
= StrDuplicate(title
),
1755 .no_autoselect
= TRUE
,
1756 .tries_done
= (UINTN
) -1,
1757 .tries_left
= (UINTN
) -1,
1760 config_add_entry(config
, entry
);
1764 static ConfigEntry
*config_entry_add_loader(
1767 enum loader_type type
,
1775 entry
= AllocatePool(sizeof(ConfigEntry
));
1776 *entry
= (ConfigEntry
) {
1778 .title
= StrDuplicate(title
),
1780 .loader
= StrDuplicate(loader
),
1781 .id
= StrDuplicate(id
),
1783 .tries_done
= (UINTN
) -1,
1784 .tries_left
= (UINTN
) -1,
1789 config_add_entry(config
, entry
);
1793 static BOOLEAN
config_entry_add_loader_auto(
1797 CHAR16
*loaded_image_path
,
1803 EFI_FILE_HANDLE handle
;
1807 if (!config
->auto_entries
)
1810 /* do not add an entry for ourselves */
1811 if (loaded_image_path
) {
1813 _cleanup_freepool_ CHAR8
*content
= NULL
;
1815 if (StriCmp(loader
, loaded_image_path
) == 0)
1818 /* look for systemd-boot magic string */
1819 err
= file_read(root_dir
, loader
, 0, 100*1024, &content
, &len
);
1820 if (!EFI_ERROR(err
)) {
1821 CHAR8
*start
= content
;
1822 CHAR8
*last
= content
+ len
- sizeof(magic
) - 1;
1824 for (; start
<= last
; start
++)
1825 if (start
[0] == magic
[0] && CompareMem(start
, magic
, sizeof(magic
) - 1) == 0)
1830 /* check existence */
1831 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &handle
, loader
, EFI_FILE_MODE_READ
, 0ULL);
1834 uefi_call_wrapper(handle
->Close
, 1, handle
);
1836 entry
= config_entry_add_loader(config
, device
, LOADER_UNDEFINED
, id
, key
, title
, loader
);
1840 /* do not boot right away into auto-detected entries */
1841 entry
->no_autoselect
= TRUE
;
1846 static VOID
config_entry_add_osx(Config
*config
) {
1848 UINTN handle_count
= 0;
1849 _cleanup_freepool_ EFI_HANDLE
*handles
= NULL
;
1851 if (!config
->auto_entries
)
1854 err
= LibLocateHandle(ByProtocol
, &FileSystemProtocol
, NULL
, &handle_count
, &handles
);
1855 if (!EFI_ERROR(err
)) {
1858 for (i
= 0; i
< handle_count
; i
++) {
1862 root
= LibOpenRoot(handles
[i
]);
1865 found
= config_entry_add_loader_auto(config
, handles
[i
], root
, NULL
, L
"auto-osx", 'a', L
"macOS",
1866 L
"\\System\\Library\\CoreServices\\boot.efi");
1867 uefi_call_wrapper(root
->Close
, 1, root
);
1874 static VOID
config_entry_add_linux(
1877 EFI_FILE
*root_dir
) {
1879 EFI_FILE_HANDLE linux_dir
;
1883 err
= uefi_call_wrapper(root_dir
->Open
, 5, root_dir
, &linux_dir
, L
"\\EFI\\Linux", EFI_FILE_MODE_READ
, 0ULL);
1889 UINTN bufsize
= sizeof buf
;
1891 CHAR8
*sections
[] = {
1893 (UINT8
*)".cmdline",
1896 UINTN offs
[ELEMENTSOF(sections
)-1] = {};
1897 UINTN szs
[ELEMENTSOF(sections
)-1] = {};
1898 UINTN addrs
[ELEMENTSOF(sections
)-1] = {};
1899 CHAR8
*content
= NULL
;
1904 CHAR16
*os_name
= NULL
;
1905 CHAR16
*os_id
= NULL
;
1906 CHAR16
*os_version
= NULL
;
1907 CHAR16
*os_build
= NULL
;
1909 err
= uefi_call_wrapper(linux_dir
->Read
, 3, linux_dir
, &bufsize
, buf
);
1910 if (bufsize
== 0 || EFI_ERROR(err
))
1913 f
= (EFI_FILE_INFO
*) buf
;
1914 if (f
->FileName
[0] == '.')
1916 if (f
->Attribute
& EFI_FILE_DIRECTORY
)
1918 len
= StrLen(f
->FileName
);
1921 if (StriCmp(f
->FileName
+ len
- 4, L
".efi") != 0)
1924 /* look for .osrel and .cmdline sections in the .efi binary */
1925 err
= pe_file_locate_sections(linux_dir
, f
->FileName
, sections
, addrs
, offs
, szs
);
1929 err
= file_read(linux_dir
, f
->FileName
, offs
[0], szs
[0], &content
, NULL
);
1933 /* read properties from the embedded os-release file */
1934 while ((line
= line_get_key_value(content
, (CHAR8
*)"=", &pos
, &key
, &value
))) {
1935 if (strcmpa((CHAR8
*)"PRETTY_NAME", key
) == 0) {
1937 os_name
= stra_to_str(value
);
1941 if (strcmpa((CHAR8
*)"ID", key
) == 0) {
1943 os_id
= stra_to_str(value
);
1947 if (strcmpa((CHAR8
*)"VERSION_ID", key
) == 0) {
1948 FreePool(os_version
);
1949 os_version
= stra_to_str(value
);
1953 if (strcmpa((CHAR8
*)"BUILD_ID", key
) == 0) {
1955 os_build
= stra_to_str(value
);
1960 if (os_name
&& os_id
&& (os_version
|| os_build
)) {
1961 _cleanup_freepool_ CHAR16
*conf
= NULL
, *path
= NULL
;
1963 conf
= PoolPrint(L
"%s-%s", os_id
, os_version
? : os_build
);
1964 path
= PoolPrint(L
"\\EFI\\Linux\\%s", f
->FileName
);
1966 entry
= config_entry_add_loader(config
, device
, LOADER_LINUX
, conf
, 'l', os_name
, path
);
1971 /* read the embedded cmdline file */
1972 err
= file_read(linux_dir
, f
->FileName
, offs
[1], szs
[1], &content
, NULL
);
1973 if (!EFI_ERROR(err
)) {
1975 /* chomp the newline */
1976 if (content
[szs
[1]-1] == '\n')
1977 content
[szs
[1]-1] = '\0';
1979 entry
->options
= stra_to_str(content
);
1982 config_entry_parse_tries(entry
, L
"\\EFI\\Linux", f
->FileName
, L
".efi");
1987 FreePool(os_version
);
1992 uefi_call_wrapper(linux_dir
->Close
, 1, linux_dir
);
1995 /* Note that this is in GUID format, i.e. the first 32bit, and the following pair of 16bit are byteswapped. */
1996 static const UINT8 xbootldr_guid
[16] = {
1997 0xff, 0xc2, 0x13, 0xbc, 0xe6, 0x59, 0x62, 0x42, 0xa3, 0x52, 0xb2, 0x75, 0xfd, 0x6f, 0x71, 0x72
2000 EFI_DEVICE_PATH
*path_parent(EFI_DEVICE_PATH
*path
, EFI_DEVICE_PATH
*node
) {
2001 EFI_DEVICE_PATH
*parent
;
2004 len
= (UINT8
*) NextDevicePathNode(node
) - (UINT8
*) path
;
2005 parent
= (EFI_DEVICE_PATH
*) AllocatePool(len
+ sizeof(EFI_DEVICE_PATH
));
2006 CopyMem(parent
, path
, len
);
2007 CopyMem((UINT8
*) parent
+ len
, EndDevicePath
, sizeof(EFI_DEVICE_PATH
));
2012 static VOID
config_load_xbootldr(
2014 EFI_HANDLE
*device
) {
2016 EFI_DEVICE_PATH
*partition_path
, *node
, *disk_path
, *copy
;
2017 UINT32 found_partition_number
= (UINT32
) -1;
2018 UINT64 found_partition_start
= (UINT64
) -1;
2019 UINT64 found_partition_size
= (UINT64
) -1;
2020 UINT8 found_partition_signature
[16] = {};
2021 EFI_HANDLE new_device
;
2025 partition_path
= DevicePathFromHandle(device
);
2026 if (!partition_path
)
2029 for (node
= partition_path
; !IsDevicePathEnd(node
); node
= NextDevicePathNode(node
)) {
2030 EFI_HANDLE disk_handle
;
2031 EFI_BLOCK_IO
*block_io
;
2035 /* First, Let's look for the SCSI/SATA/USB/… device path node, i.e. one above the media
2037 if (DevicePathType(node
) != MESSAGING_DEVICE_PATH
)
2040 /* Determine the device path one level up */
2041 disk_path
= path_parent(partition_path
, node
);
2043 r
= uefi_call_wrapper(BS
->LocateDevicePath
, 3, &BlockIoProtocol
, &p
, &disk_handle
);
2047 r
= uefi_call_wrapper(BS
->HandleProtocol
, 3, disk_handle
, &BlockIoProtocol
, (VOID
**)&block_io
);
2051 /* Filter out some block devices early. (We only care about block devices that aren't
2052 * partitions themselves — we look for GPT partition tables to parse after all —, and only
2053 * those which contain a medium and have at least 2 blocks.) */
2054 if (block_io
->Media
->LogicalPartition
||
2055 !block_io
->Media
->MediaPresent
||
2056 block_io
->Media
->LastBlock
<= 1)
2059 /* Try both copies of the GPT header, in case one is corrupted */
2060 for (nr
= 0; nr
< 2; nr
++) {
2061 _cleanup_freepool_ EFI_PARTITION_ENTRY
* entries
= NULL
;
2063 EFI_PARTITION_TABLE_HEADER gpt_header
;
2064 uint8_t space
[((sizeof(EFI_PARTITION_TABLE_HEADER
) + 511) / 512) * 512];
2065 } gpt_header_buffer
;
2066 const EFI_PARTITION_TABLE_HEADER
*h
= &gpt_header_buffer
.gpt_header
;
2072 /* Read the first copy at LBA 1 */
2075 /* Read the second copy at the very last LBA of this block device */
2076 where
= block_io
->Media
->LastBlock
;
2078 /* Read the GPT header */
2079 r
= uefi_call_wrapper(block_io
->ReadBlocks
, 5,
2081 block_io
->Media
->MediaId
,
2083 sizeof(gpt_header_buffer
), &gpt_header_buffer
);
2087 /* Some superficial validation of the GPT header */
2088 c
= CompareMem(&h
->Header
.Signature
, "EFI PART", sizeof(h
->Header
.Signature
));
2092 if (h
->Header
.HeaderSize
< 92 ||
2093 h
->Header
.HeaderSize
> 512)
2096 if (h
->Header
.Revision
!= 0x00010000U
)
2099 /* Calculate CRC check */
2100 c
= ~crc32_exclude_offset((UINT32
) -1,
2101 (const UINT8
*) &gpt_header_buffer
,
2102 h
->Header
.HeaderSize
,
2103 OFFSETOF(EFI_PARTITION_TABLE_HEADER
, Header
.CRC32
),
2104 sizeof(h
->Header
.CRC32
));
2105 if (c
!= h
->Header
.CRC32
)
2108 if (h
->MyLBA
!= where
)
2111 if (h
->SizeOfPartitionEntry
< sizeof(EFI_PARTITION_ENTRY
))
2114 if (h
->NumberOfPartitionEntries
<= 0 ||
2115 h
->NumberOfPartitionEntries
> 1024)
2118 if (h
->SizeOfPartitionEntry
> UINTN_MAX
/ h
->NumberOfPartitionEntries
) /* overflow check */
2121 /* Now load the GPT entry table */
2122 sz
= ALIGN_TO((UINTN
) h
->SizeOfPartitionEntry
* (UINTN
) h
->NumberOfPartitionEntries
, 512);
2123 entries
= AllocatePool(sz
);
2125 r
= uefi_call_wrapper(block_io
->ReadBlocks
, 5,
2127 block_io
->Media
->MediaId
,
2128 h
->PartitionEntryLBA
,
2133 /* Calculate CRC of entries array, too */
2134 c
= ~crc32((UINT32
) -1, entries
, sz
);
2135 if (c
!= h
->PartitionEntryArrayCRC32
)
2138 for (i
= 0; i
< h
->NumberOfPartitionEntries
; i
++) {
2139 EFI_PARTITION_ENTRY
*entry
;
2141 entry
= (EFI_PARTITION_ENTRY
*) ((UINT8
*) entries
+ h
->SizeOfPartitionEntry
* i
);
2143 if (CompareMem(&entry
->PartitionTypeGUID
, xbootldr_guid
, 16) == 0) {
2146 /* Let's use memcpy(), in case the structs are not aligned (they really should be though) */
2147 CopyMem(&found_partition_start
, &entry
->StartingLBA
, sizeof(found_partition_start
));
2148 CopyMem(&end
, &entry
->EndingLBA
, sizeof(end
));
2150 if (end
< found_partition_start
) /* Bogus? */
2153 found_partition_size
= end
- found_partition_start
+ 1;
2154 CopyMem(found_partition_signature
, &entry
->UniquePartitionGUID
, sizeof(found_partition_signature
));
2156 found_partition_number
= i
+ 1;
2161 break; /* This GPT was fully valid, but we didn't find what we are looking for. This
2162 * means there's no reason to check the second copy of the GPT header */
2166 return; /* Not found */
2169 copy
= DuplicateDevicePath(partition_path
);
2171 /* Patch in the data we found */
2172 for (node
= copy
; !IsDevicePathEnd(node
); node
= NextDevicePathNode(node
)) {
2173 HARDDRIVE_DEVICE_PATH
*hd
;
2175 if (DevicePathType(node
) != MEDIA_DEVICE_PATH
)
2178 if (DevicePathSubType(node
) != MEDIA_HARDDRIVE_DP
)
2181 hd
= (HARDDRIVE_DEVICE_PATH
*) node
;
2182 hd
->PartitionNumber
= found_partition_number
;
2183 hd
->PartitionStart
= found_partition_start
;
2184 hd
->PartitionSize
= found_partition_size
;
2185 CopyMem(hd
->Signature
, found_partition_signature
, sizeof(hd
->Signature
));
2186 hd
->MBRType
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
2187 hd
->SignatureType
= SIGNATURE_TYPE_GUID
;
2190 r
= uefi_call_wrapper(BS
->LocateDevicePath
, 3, &BlockIoProtocol
, ©
, &new_device
);
2194 root_dir
= LibOpenRoot(new_device
);
2198 config_entry_add_linux(config
, new_device
, root_dir
);
2199 config_load_entries(config
, new_device
, root_dir
, NULL
);
2202 static EFI_STATUS
image_start(
2203 EFI_HANDLE parent_image
,
2204 const Config
*config
,
2205 const ConfigEntry
*entry
) {
2208 _cleanup_freepool_ EFI_DEVICE_PATH
*path
= NULL
;
2212 path
= FileDevicePath(entry
->device
, entry
->loader
);
2214 Print(L
"Error getting device path.");
2215 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2216 return EFI_INVALID_PARAMETER
;
2219 err
= uefi_call_wrapper(BS
->LoadImage
, 6, FALSE
, parent_image
, path
, NULL
, 0, &image
);
2220 if (EFI_ERROR(err
)) {
2221 Print(L
"Error loading %s: %r", entry
->loader
, err
);
2222 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2226 if (config
->options_edit
)
2227 options
= config
->options_edit
;
2228 else if (entry
->options
)
2229 options
= entry
->options
;
2233 EFI_LOADED_IMAGE
*loaded_image
;
2235 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
2236 parent_image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
2237 if (EFI_ERROR(err
)) {
2238 Print(L
"Error getting LoadedImageProtocol handle: %r", err
);
2239 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2242 loaded_image
->LoadOptions
= options
;
2243 loaded_image
->LoadOptionsSize
= (StrLen(loaded_image
->LoadOptions
)+1) * sizeof(CHAR16
);
2246 /* Try to log any options to the TPM, especially to catch manually edited options */
2247 err
= tpm_log_event(SD_TPM_PCR
,
2248 (EFI_PHYSICAL_ADDRESS
) (UINTN
) loaded_image
->LoadOptions
,
2249 loaded_image
->LoadOptionsSize
, loaded_image
->LoadOptions
);
2250 if (EFI_ERROR(err
)) {
2251 Print(L
"Unable to add image options measurement: %r", err
);
2252 uefi_call_wrapper(BS
->Stall
, 1, 200 * 1000);
2257 efivar_set_time_usec(L
"LoaderTimeExecUSec", 0);
2258 err
= uefi_call_wrapper(BS
->StartImage
, 3, image
, NULL
, NULL
);
2260 uefi_call_wrapper(BS
->UnloadImage
, 1, image
);
2264 static EFI_STATUS
reboot_into_firmware(VOID
) {
2265 _cleanup_freepool_ CHAR8
*b
= NULL
;
2270 osind
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
2272 err
= efivar_get_raw(&global_guid
, L
"OsIndications", &b
, &size
);
2273 if (!EFI_ERROR(err
))
2274 osind
|= (UINT64
)*b
;
2276 err
= efivar_set_raw(&global_guid
, L
"OsIndications", &osind
, sizeof(UINT64
), TRUE
);
2280 err
= uefi_call_wrapper(RT
->ResetSystem
, 4, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
2281 Print(L
"Error calling ResetSystem: %r", err
);
2282 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2286 static VOID
config_free(Config
*config
) {
2289 for (i
= 0; i
< config
->entry_count
; i
++)
2290 config_entry_free(config
->entries
[i
]);
2291 FreePool(config
->entries
);
2292 FreePool(config
->entry_default_pattern
);
2293 FreePool(config
->options_edit
);
2294 FreePool(config
->entry_oneshot
);
2297 static VOID
config_write_entries_to_variable(Config
*config
) {
2298 _cleanup_freepool_ CHAR16
*buffer
= NULL
;
2302 for (i
= 0; i
< config
->entry_count
; i
++)
2303 sz
+= StrLen(config
->entries
[i
]->id
) + 1;
2305 p
= buffer
= AllocatePool(sz
* sizeof(CHAR16
));
2307 for (i
= 0; i
< config
->entry_count
; i
++) {
2310 l
= StrLen(config
->entries
[i
]->id
) + 1;
2311 CopyMem(p
, config
->entries
[i
]->id
, l
* sizeof(CHAR16
));
2316 /* Store the full list of discovered entries. */
2317 (void) efivar_set_raw(&loader_guid
, L
"LoaderEntries", buffer
, (UINT8
*) p
- (UINT8
*) buffer
, FALSE
);
2320 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*sys_table
) {
2321 static const UINT64 loader_features
=
2322 EFI_LOADER_FEATURE_CONFIG_TIMEOUT
|
2323 EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
|
2324 EFI_LOADER_FEATURE_ENTRY_DEFAULT
|
2325 EFI_LOADER_FEATURE_ENTRY_ONESHOT
|
2326 EFI_LOADER_FEATURE_BOOT_COUNTING
|
2327 EFI_LOADER_FEATURE_XBOOTLDR
|
2328 EFI_LOADER_FEATURE_RANDOM_SEED
|
2331 _cleanup_freepool_ CHAR16
*infostr
= NULL
, *typestr
= NULL
;
2334 EFI_LOADED_IMAGE
*loaded_image
;
2336 CHAR16
*loaded_image_path
;
2340 BOOLEAN menu
= FALSE
;
2343 InitializeLib(image
, sys_table
);
2344 init_usec
= time_usec();
2345 efivar_set_time_usec(L
"LoaderTimeInitUSec", init_usec
);
2346 efivar_set(L
"LoaderInfo", L
"systemd-boot " GIT_VERSION
, FALSE
);
2348 infostr
= PoolPrint(L
"%s %d.%02d", ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
2349 efivar_set(L
"LoaderFirmwareInfo", infostr
, FALSE
);
2351 typestr
= PoolPrint(L
"UEFI %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
2352 efivar_set(L
"LoaderFirmwareType", typestr
, FALSE
);
2354 (void) efivar_set_raw(&loader_guid
, L
"LoaderFeatures", &loader_features
, sizeof(loader_features
), FALSE
);
2356 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
2357 image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
2358 if (EFI_ERROR(err
)) {
2359 Print(L
"Error getting a LoadedImageProtocol handle: %r", err
);
2360 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2364 /* export the device path this image is started from */
2365 if (disk_get_part_uuid(loaded_image
->DeviceHandle
, uuid
) == EFI_SUCCESS
)
2366 efivar_set(L
"LoaderDevicePartUUID", uuid
, FALSE
);
2368 root_dir
= LibOpenRoot(loaded_image
->DeviceHandle
);
2370 Print(L
"Unable to open root directory.");
2371 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2372 return EFI_LOAD_ERROR
;
2375 if (secure_boot_enabled() && shim_loaded()) {
2376 err
= security_policy_install();
2377 if (EFI_ERROR(err
)) {
2378 Print(L
"Error installing security policy: %r ", err
);
2379 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2384 /* the filesystem path to this image, to prevent adding ourselves to the menu */
2385 loaded_image_path
= DevicePathToStr(loaded_image
->FilePath
);
2386 efivar_set(L
"LoaderImageIdentifier", loaded_image_path
, FALSE
);
2388 config_load_defaults(&config
, root_dir
);
2390 /* scan /EFI/Linux/ directory */
2391 config_entry_add_linux(&config
, loaded_image
->DeviceHandle
, root_dir
);
2393 /* scan /loader/entries/\*.conf files */
2394 config_load_entries(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
);
2396 /* Similar, but on any XBOOTLDR partition */
2397 config_load_xbootldr(&config
, loaded_image
->DeviceHandle
);
2399 /* sort entries after version number */
2400 config_sort_entries(&config
);
2402 /* if we find some well-known loaders, add them to the end of the list */
2403 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, NULL
,
2404 L
"auto-windows", 'w', L
"Windows Boot Manager", L
"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
2405 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, NULL
,
2406 L
"auto-efi-shell", 's', L
"EFI Shell", L
"\\shell" EFI_MACHINE_TYPE_NAME
".efi");
2407 config_entry_add_loader_auto(&config
, loaded_image
->DeviceHandle
, root_dir
, loaded_image_path
,
2408 L
"auto-efi-default", '\0', L
"EFI Default Loader", L
"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME
".efi");
2409 config_entry_add_osx(&config
);
2411 if (config
.auto_firmware
&& efivar_get_raw(&global_guid
, L
"OsIndicationsSupported", &b
, &size
) == EFI_SUCCESS
) {
2412 UINT64 osind
= (UINT64
)*b
;
2414 if (osind
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
)
2415 config_entry_add_call(&config
,
2416 L
"auto-reboot-to-firmware-setup",
2417 L
"Reboot Into Firmware Interface",
2418 reboot_into_firmware
);
2422 if (config
.entry_count
== 0) {
2423 Print(L
"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
2424 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2428 config_write_entries_to_variable(&config
);
2430 config_title_generate(&config
);
2432 /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */
2433 config_default_entry_select(&config
);
2435 /* if no configured entry to select from was found, enable the menu */
2436 if (config
.idx_default
== -1) {
2437 config
.idx_default
= 0;
2438 if (config
.timeout_sec
== 0)
2439 config
.timeout_sec
= 10;
2442 /* select entry or show menu when key is pressed or timeout is set */
2443 if (config
.force_menu
|| config
.timeout_sec
> 0)
2448 err
= console_key_read(&key
, FALSE
);
2449 if (!EFI_ERROR(err
)) {
2452 /* find matching key in config entries */
2453 idx
= entry_lookup_key(&config
, config
.idx_default
, KEYCHAR(key
));
2455 config
.idx_default
= idx
;
2464 entry
= config
.entries
[config
.idx_default
];
2466 efivar_set_time_usec(L
"LoaderTimeMenuUSec", 0);
2467 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 0, 0x10000, 0, NULL
);
2468 if (!menu_run(&config
, &entry
, loaded_image_path
))
2472 /* run special entry like "reboot" */
2478 config_entry_bump_counters(entry
, root_dir
);
2480 /* Export the selected boot entry to the system */
2481 (VOID
) efivar_set(L
"LoaderEntrySelected", entry
->id
, FALSE
);
2483 /* Optionally, read a random seed off the ESP and pass it to the OS */
2484 (VOID
) process_random_seed(root_dir
, config
.random_seed_mode
);
2486 uefi_call_wrapper(BS
->SetWatchdogTimer
, 4, 5 * 60, 0x10000, 0, NULL
);
2487 err
= image_start(image
, &config
, entry
);
2488 if (EFI_ERROR(err
)) {
2489 graphics_mode(FALSE
);
2490 Print(L
"\nFailed to execute %s (%s): %r\n", entry
->title
, entry
->loader
, err
);
2491 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
2496 config
.timeout_sec
= 0;
2500 FreePool(loaded_image_path
);
2501 config_free(&config
);
2502 uefi_call_wrapper(root_dir
->Close
, 1, root_dir
);
2503 uefi_call_wrapper(BS
->CloseProtocol
, 4, image
, &LoadedImageProtocol
, image
, NULL
);