HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/boot/boot.c
Git blob: 91c82b3536d2e6cb8dcb6498d9825975a409d28a
Lines 1–1000 of 3,525
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "bcd.h"
#include "bootspec.h"
#include "console.h"
#include "cpio.h"
#include "device-path-util.h"
#include "devicetree.h"
#include "drivers.h"
#include "efi-efivars.h"
#include "efi-log.h"
#include "efi-string-table.h"
#include "efivars.h"
#include "export-vars.h"
#include "graphics.h"
#include "initrd.h"
#include "iovec-util.h"
#include "line-edit.h"
#include "measure.h"
#include "measure-smbios.h"
#include "memory-util.h"
#include "part-discovery.h"
#include "pe.h"
#include "proto/block-io.h"
#include "proto/disk-io.h"
#include "proto/load-file.h"
#include "proto/simple-text-io.h"
#include "random-seed.h"
#include "sbat.h"
#include "secure-boot.h"
#include "shim.h"
#include "smbios.h"
#include "strv.h"
#include "sysfail.h"
#include "ticks.h"
#include "tpm2-pcr.h"
#include "uki.h"
#include "url-discovery.h"
#include "util.h"
#include "version.h"
#include "vmm.h"
/* Safety margin, refuse larger extra files (this is not load bearing, only a safety net for robustness reasons). */
#define EXTRA_SIZE_MAX (1024U * 1024U * 1536U)
/* Magic string for recognizing our own binaries */
#define SD_MAGIC "#### LoaderInfo: systemd-boot " GIT_VERSION " ####"
DECLARE_NOALLOC_SECTION(".sdmagic", SD_MAGIC);
/* Makes systemd-boot available from \EFI\Linux\ for testing purposes. */
DECLARE_NOALLOC_SECTION(
".osrel",
"ID=systemd-boot\n"
"VERSION=\"" GIT_VERSION "\"\n"
"NAME=\"systemd-boot " GIT_VERSION "\"\n");
DECLARE_SBAT(SBAT_BOOT_SECTION_TEXT);
typedef enum LoaderType {
LOADER_UNDEFINED,
LOADER_AUTO,
LOADER_EFI, /* Boot loader spec type #1 entries with "efi" line */
LOADER_LINUX, /* Boot loader spec type #1 entries with "linux" line */
LOADER_UKI, /* Boot loader spec type #1 entries with "uki" line */
LOADER_UKI_URL, /* Boot loader spec type #1 entries with "uki-url" line */
LOADER_TYPE2_UKI, /* Boot loader spec type #2 entries */
LOADER_SECURE_BOOT_KEYS,
LOADER_BAD, /* Marker: this boot loader spec type #1 entry is invalid */
LOADER_IGNORE, /* Marker: this boot loader spec type #1 entry does not match local host */
LOADER_REBOOT,
LOADER_POWEROFF,
LOADER_FWSETUP,
_LOADER_TYPE_MAX,
} LoaderType;
/* Which loader types permit command line editing */
#define LOADER_TYPE_ALLOW_EDITOR(t) IN_SET(t, LOADER_EFI, LOADER_LINUX, LOADER_UKI, LOADER_UKI_URL, LOADER_TYPE2_UKI)
/* Which loader types allow command line editing in SecureBoot mode */
#define LOADER_TYPE_ALLOW_EDITOR_IN_SB(t) IN_SET(t, LOADER_EFI, LOADER_LINUX)
/* Which loader types shall be considered for automatic selection */
#define LOADER_TYPE_MAY_AUTO_SELECT(t) IN_SET(t, LOADER_EFI, LOADER_LINUX, LOADER_UKI, LOADER_UKI_URL, LOADER_TYPE2_UKI)
/* Whether to do boot attempt counting logic (only works if userspace can actually find the selected option later) */
#define LOADER_TYPE_BUMP_COUNTERS(t) IN_SET(t, LOADER_LINUX, LOADER_UKI, LOADER_TYPE2_UKI)
/* Whether to do random seed management (only we invoke Linux) */
#define LOADER_TYPE_PROCESS_RANDOM_SEED(t) IN_SET(t, LOADER_LINUX, LOADER_UKI, LOADER_TYPE2_UKI)
/* Whether to persistently save the selected entry in an EFI variable, if that's requested. */
#define LOADER_TYPE_SAVE_ENTRY(t) IN_SET(t, LOADER_AUTO, LOADER_EFI, LOADER_LINUX, LOADER_UKI, LOADER_UKI_URL, LOADER_TYPE2_UKI)
/* Whether this item is implemented fully inside of systemd-boot */
#define LOADER_TYPE_IS_INTERNAL(t) IN_SET(t, LOADER_SECURE_BOOT_KEYS, LOADER_REBOOT, LOADER_POWEROFF, LOADER_FWSETUP)
typedef enum {
REBOOT_NO,
REBOOT_YES,
REBOOT_AUTO,
_REBOOT_ON_ERROR_MAX,
} RebootOnError;
static const char *reboot_on_error_table[_REBOOT_ON_ERROR_MAX] = {
[REBOOT_NO] = "no",
[REBOOT_YES] = "yes",
[REBOOT_AUTO] = "auto",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(reboot_on_error, RebootOnError);
typedef struct BootEntry {
char16_t *id; /* The unique identifier for this entry (typically the filename of the file defining the entry, possibly suffixed with a profile id) */
char16_t *id_without_profile; /* same, but without any profile id suffixed */
char16_t *title_show; /* The string to actually display (this is made unique before showing) */
char16_t *title; /* The raw (human-readable) title string of the entry (not necessarily unique) */
char16_t *sort_key; /* The string to use as primary sort key, usually ID= from os-release, possibly suffixed */
char16_t *version; /* The raw (human-readable) version string of the entry */
char16_t *machine_id;
EFI_HANDLE *device;
LoaderType type;
char16_t *loader;
char16_t *url;
char16_t *devicetree;
char16_t *options;
bool options_implied; /* If true, these options are implied if we invoke the PE binary without any parameters (as in: UKI). If false we must specify these options explicitly. */
char16_t **initrd;
char16_t **extras;
char16_t key;
EFI_STATUS (*call)(const struct BootEntry *entry, EFI_FILE *root_dir, EFI_HANDLE parent_image);
int tries_done;
int tries_left;
char16_t *directory;
char16_t *current_name;
char16_t *next_name;
unsigned profile;
} BootEntry;
typedef struct {
BootEntry **entries;
size_t n_entries;
size_t idx_default;
size_t idx_default_efivar;
uint64_t timeout_sec; /* Actual timeout used (efi_main() override > smbios > efivar > config). */
uint64_t timeout_sec_smbios;
uint64_t timeout_sec_config;
uint64_t timeout_sec_efivar;
char16_t *entry_default_config;
char16_t *entry_preferred_config;
char16_t *entry_default_efivar;
char16_t *entry_preferred_efivar;
char16_t *entry_oneshot;
char16_t *entry_saved;
char16_t *entry_sysfail;
bool editor;
bool auto_entries;
bool auto_firmware;
bool auto_poweroff;
bool auto_reboot;
bool reboot_for_bitlocker;
RebootOnError reboot_on_error;
secure_boot_enroll secure_boot_enroll;
secure_boot_enroll_action secure_boot_enroll_action;
uint64_t secure_boot_enroll_timeout_sec;
bool force_menu;
bool use_saved_entry;
bool use_saved_entry_efivar;
bool use_saved_entry_preferred;
bool use_saved_entry_preferred_efivar;
bool beep;
bool sysfail_occurred;
int64_t console_mode;
int64_t console_mode_efivar;
} Config;
/* These values have been chosen so that the transitions the user sees could employ unsigned over-/underflow
* like this:
* efivar unset ↔ force menu ↔ no timeout/skip menu ↔ 1 s ↔ 2 s ↔ …
*
* Note: all the values below are ABI, so they are not allowed to change. The bootctl tool sets the numerical
* value of TIMEOUT_MENU_FORCE and TIMEOUT_MENU_HIDDEN, instead of the string for compatibility reasons.
*
* The other values may be set by systemd-boot itself and changing those will lead to functional regression
* when new version of systemd-boot is installed.
*
* All the 64bit values are not ABI and will never be written to an efi variable.
*/
enum {
TIMEOUT_MIN = 1,
TIMEOUT_MAX = UINT32_MAX - 2U,
TIMEOUT_UNSET = UINT32_MAX - 1U,
TIMEOUT_MENU_FORCE = UINT32_MAX,
TIMEOUT_MENU_HIDDEN = 0,
TIMEOUT_TYPE_MAX = UINT32_MAX,
TIMEOUT_MENU_DISABLED = (uint64_t)UINT32_MAX + 1U,
TIMEOUT_TYPE_MAX64 = UINT64_MAX,
};
enum {
IDX_MAX = INT16_MAX,
IDX_INVALID,
};
static size_t entry_lookup_key(Config *config, size_t start, char16_t key) {
assert(config);
if (key == 0)
return IDX_INVALID;
/* select entry by number key */
if (key >= '1' && key <= '9') {
size_t i = key - '0';
if (i > config->n_entries)
i = config->n_entries;
return i-1;
}
/* find matching key in boot entries */
for (size_t i = start; i < config->n_entries; i++)
if (config->entries[i]->key == key)
return i;
for (size_t i = 0; i < start; i++)
if (config->entries[i]->key == key)
return i;
return IDX_INVALID;
}
static char16_t* update_timeout_efivar(Config *config, bool inc) {
assert(config);
switch (config->timeout_sec) {
case TIMEOUT_MAX:
config->timeout_sec = inc ? TIMEOUT_MAX : config->timeout_sec - 1;
break;
case TIMEOUT_UNSET:
config->timeout_sec = inc ? TIMEOUT_MENU_FORCE : TIMEOUT_UNSET;
break;
case TIMEOUT_MENU_DISABLED:
config->timeout_sec = inc ? TIMEOUT_MIN : TIMEOUT_MENU_FORCE;
break;
case TIMEOUT_MENU_FORCE:
config->timeout_sec = inc ? TIMEOUT_MENU_HIDDEN : TIMEOUT_MENU_FORCE;
break;
case TIMEOUT_MENU_HIDDEN:
config->timeout_sec = inc ? TIMEOUT_MIN : TIMEOUT_MENU_FORCE;
break;
default:
config->timeout_sec = config->timeout_sec + (inc ? 1 : -1);
}
config->timeout_sec_efivar = config->timeout_sec;
switch (config->timeout_sec) {
case TIMEOUT_UNSET:
return xstrdup16(u"Menu timeout defined by configuration file.");
case TIMEOUT_MENU_DISABLED:
assert_not_reached();
case TIMEOUT_MENU_FORCE:
return xstrdup16(u"Timeout disabled, menu will always be shown.");
case TIMEOUT_MENU_HIDDEN:
return xstrdup16(u"Menu hidden. Hold down key at bootup to show menu.");
default:
return xasprintf("Menu timeout set to %"PRIu64"s.", config->timeout_sec_efivar);
}
}
static bool unicode_supported(void) {
static int cache = -1;
if (cache < 0)
/* Basic unicode box drawing support is mandated by the spec, but it does
* not hurt to make sure it works. */
cache = ST->ConOut->TestString(ST->ConOut, (char16_t *) u"─") == EFI_SUCCESS;
return cache;
}
static bool ps_continue(void) {
const char16_t *sep = unicode_supported() ? u"───" : u"---";
printf("\n%ls Press any key to continue, ESC or q to quit. %ls\n\n", sep, sep);
uint64_t key;
return console_key_read(&key, UINT64_MAX) == EFI_SUCCESS &&
!IN_SET(key, KEYPRESS(0, SCAN_ESC, 0), KEYPRESS(0, 0, 'q'), KEYPRESS(0, 0, 'Q'));
}
static void print_timeout_status(const char *label, uint64_t t) {
switch (t) {
case TIMEOUT_UNSET:
return;
case TIMEOUT_MENU_DISABLED:
return (void) printf("%s: menu-disabled\n", label);
case TIMEOUT_MENU_FORCE:
return (void) printf("%s: menu-force\n", label);
case TIMEOUT_MENU_HIDDEN:
return (void) printf("%s: menu-hidden\n", label);
default:
return (void) printf("%s: %"PRIu64"s\n", label, t);
}
}
static void print_status(Config *config, char16_t *loaded_image_path) {
size_t x_max, y_max;
uint32_t screen_width = 0, screen_height = 0;
SecureBootMode secure;
_cleanup_free_ char16_t *device_part_uuid = NULL;
assert(config);
clear_screen(COLOR_NORMAL);
console_query_mode(&x_max, &y_max);
query_screen_resolution(&screen_width, &screen_height);
secure = secure_boot_mode();
(void) efivar_get_str16(MAKE_GUID_PTR(LOADER), u"LoaderDevicePartUUID", &device_part_uuid);
printf(" systemd-boot version: " GIT_VERSION "\n");
if (loaded_image_path)
printf(" loaded image: %ls\n", loaded_image_path);
if (device_part_uuid)
printf(" loader partition UUID: %ls\n", device_part_uuid);
printf(" architecture: " EFI_MACHINE_TYPE_NAME "\n");
printf(" UEFI specification: %u.%02u\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
printf(" firmware vendor: %ls\n", ST->FirmwareVendor);
printf(" firmware version: %u.%02u\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
printf(" OS indications: %#" PRIx64 "\n", get_os_indications_supported());
printf(" secure boot: %ls (%ls)\n",
yes_no(IN_SET(secure, SECURE_BOOT_USER, SECURE_BOOT_DEPLOYED)),
secure_boot_mode_to_string(secure));
printf(" shim: %ls\n", yes_no(shim_loaded()));
printf(" TPM: %ls\n", yes_no(tpm_present()));
printf(" console mode: %i/%" PRIi64 " (%zux%zu",
ST->ConOut->Mode->Mode, ST->ConOut->Mode->MaxMode - INT64_C(1),
x_max, y_max);
if (screen_width > 0 && screen_height > 0)
printf(" @ %ux%u",
screen_width, screen_height);
printf(")\n");
if (!ps_continue())
return;
print_timeout_status(" timeout (config)", config->timeout_sec_config);
print_timeout_status(" timeout (EFI var)", config->timeout_sec_efivar);
print_timeout_status(" timeout (smbios)", config->timeout_sec_smbios);
if (config->entry_default_config)
printf(" default (config): %ls\n", config->entry_default_config);
if (config->entry_preferred_config)
printf(" preferred (config): %ls\n", config->entry_preferred_config);
if (config->entry_default_efivar)
printf(" default (EFI var): %ls\n", config->entry_default_efivar);
if (config->entry_preferred_efivar)
printf(" preferred (EFI var): %ls\n", config->entry_preferred_efivar);
if (config->entry_oneshot)
printf(" default (one-shot): %ls\n", config->entry_oneshot);
if (config->entry_sysfail)
printf(" sysfail: %ls\n", config->entry_sysfail);
if (config->entry_saved)
printf(" saved entry: %ls\n", config->entry_saved);
printf(" editor: %ls\n", yes_no(config->editor));
printf(" auto-entries: %ls\n", yes_no(config->auto_entries));
printf(" auto-firmware: %ls\n", yes_no(config->auto_firmware));
printf(" auto-poweroff: %ls\n", yes_no(config->auto_poweroff));
printf(" auto-reboot: %ls\n", yes_no(config->auto_reboot));
printf(" beep: %ls\n", yes_no(config->beep));
printf(" reboot-for-bitlocker: %ls\n", yes_no(config->reboot_for_bitlocker));
printf(" reboot-on-error: %s\n", reboot_on_error_to_string(config->reboot_on_error));
printf(" secure-boot-enroll: %s\n", secure_boot_enroll_to_string(config->secure_boot_enroll));
printf(" secure-boot-enroll-action: %s\n", secure_boot_enroll_action_to_string(config->secure_boot_enroll_action));
printf("secure-boot-enroll-timeout-sec: %"PRIu64"s\n", config->secure_boot_enroll_timeout_sec);
switch (config->console_mode) {
case CONSOLE_MODE_AUTO:
printf(" console-mode (config): auto\n");
break;
case CONSOLE_MODE_KEEP:
printf(" console-mode (config): keep\n");
break;
case CONSOLE_MODE_FIRMWARE_MAX:
printf(" console-mode (config): max\n");
break;
default:
printf(" console-mode (config): %" PRIi64 "\n", config->console_mode);
}
/* EFI var console mode is always a concrete value or unset. */
if (config->console_mode_efivar != CONSOLE_MODE_KEEP)
printf(" console-mode (EFI var): %" PRIi64 "\n", config->console_mode_efivar);
printf(" log-level: %s\n", log_level_to_string(log_get_max_level()));
if (!ps_continue())
return;
for (size_t i = 0; i < config->n_entries; i++) {
BootEntry *entry = config->entries[i];
EFI_DEVICE_PATH *dp = NULL;
_cleanup_free_ char16_t *dp_str = NULL;
if (entry->device &&
BS->HandleProtocol(entry->device, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp) ==
EFI_SUCCESS)
(void) device_path_to_str(dp, &dp_str);
printf(" boot entry: %zu/%zu\n", i + 1, config->n_entries);
printf(" id: %ls", entry->id);
if (entry->id_without_profile && !streq(entry->id_without_profile, entry->id))
printf(" (without profile: %ls)\n", entry->id_without_profile);
else
printf("\n");
if (entry->title)
printf(" title: %ls\n", entry->title);
if (entry->title_show && !streq16(entry->title, entry->title_show))
printf(" title show: %ls\n", entry->title_show);
if (entry->sort_key)
printf(" sort key: %ls\n", entry->sort_key);
if (entry->version)
printf(" version: %ls\n", entry->version);
if (entry->machine_id)
printf(" machine-id: %ls\n", entry->machine_id);
if (dp_str)
printf(" device: %ls\n", dp_str);
if (entry->loader)
printf(" loader: %ls\n", entry->loader);
if (entry->url)
printf(" url: %ls\n", entry->url);
STRV_FOREACH(initrd, entry->initrd)
printf(" initrd: %ls\n", *initrd);
STRV_FOREACH(extra, entry->extras)
printf(" extra: %ls\n", *extra);
if (entry->devicetree)
printf(" devicetree: %ls\n", entry->devicetree);
if (entry->options)
printf(" options: %ls\n", entry->options);
if (entry->profile > 0)
printf(" profile: %u\n", entry->profile);
printf(" internal call: %ls\n", yes_no(LOADER_TYPE_IS_INTERNAL(entry->type)));
printf("counting boots: %ls\n", yes_no(entry->tries_left >= 0));
if (entry->tries_left >= 0) {
printf(" tries: %i left, %i done\n", entry->tries_left, entry->tries_done);
printf(" current path: %ls\\%ls\n", entry->directory, entry->current_name);
printf(" next path: %ls\\%ls\n", entry->directory, entry->next_name);
}
if (!ps_continue())
return;
}
}
static EFI_STATUS set_reboot_into_firmware(void) {
EFI_STATUS err;
uint64_t osind = 0;
(void) efivar_get_uint64_le(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"OsIndications", &osind);
if (FLAGS_SET(osind, EFI_OS_INDICATIONS_BOOT_TO_FW_UI))
return EFI_SUCCESS;
osind |= EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
err = efivar_set_uint64_le(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"OsIndications", osind, EFI_VARIABLE_NON_VOLATILE);
if (err != EFI_SUCCESS)
return log_warning_status(err, "Error setting OsIndications, ignoring: %m");
return EFI_SUCCESS;
}
_noreturn_ static EFI_STATUS call_poweroff_system(const BootEntry *entry, EFI_FILE *root_dir, EFI_HANDLE parent_image) {
RT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
assert_not_reached();
}
_noreturn_ static EFI_STATUS call_reboot_system(const BootEntry *entry, EFI_FILE *root_dir, EFI_HANDLE parent_image) {
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
assert_not_reached();
}
static EFI_STATUS call_reboot_into_firmware(const BootEntry *entry, EFI_FILE *root_dir, EFI_HANDLE parent_image) {
EFI_STATUS err;
err = set_reboot_into_firmware();
if (err != EFI_SUCCESS)
return err;
return call_reboot_system(entry, root_dir, parent_image);
}
static bool menu_run(
Config *config,
BootEntry **chosen_entry,
char16_t *loaded_image_path) {
assert(config);
assert(chosen_entry);
EFI_STATUS err;
size_t visible_max = 0;
size_t idx_highlight = config->idx_default, idx_highlight_prev = 0;
size_t idx, idx_first = 0, idx_last = 0;
bool new_mode = true, clear = true;
bool refresh = true, highlight = false;
size_t x_start = 0, y_start = 0, y_status = 0, x_max, y_max;
_cleanup_strv_free_ char16_t **lines = NULL;
_cleanup_free_ char16_t *clearline = NULL, *separator = NULL, *status = NULL;
uint64_t timeout_efivar_saved = config->timeout_sec_efivar,
timeout_remain = config->timeout_sec == TIMEOUT_MENU_FORCE ? 0 : config->timeout_sec;
int64_t console_mode_initial = ST->ConOut->Mode->Mode, console_mode_efivar_saved = config->console_mode_efivar;
size_t default_efivar_saved = config->idx_default_efivar;
enum {
ACTION_CONTINUE, /* Continue with loop over user input */
ACTION_FIRMWARE_SETUP, /* Ask for confirmation and reboot into firmware setup */
ACTION_POWEROFF, /* Power off the machine */
ACTION_REBOOT, /* Reboot the machine */
ACTION_RUN, /* Execute a boot entry */
ACTION_QUIT, /* Return to the firmware */
} action = ACTION_CONTINUE;
graphics_mode(false);
ST->ConIn->Reset(ST->ConIn, false);
ST->ConOut->EnableCursor(ST->ConOut, false);
/* Draw a single character to the beginning of a line, in order to make ClearScreen() work on certain
* broken firmware. And let's immediately move back to the beginning of the line. */
printf("\r \r");
err = console_set_mode(config->console_mode_efivar != CONSOLE_MODE_KEEP ?
config->console_mode_efivar : config->console_mode);
if (err != EFI_SUCCESS) {
clear_screen(COLOR_NORMAL);
log_error_status(err, "Error switching console mode: %m");
}
size_t line_width = 0, entry_padding = 3;
while (IN_SET(action, ACTION_CONTINUE, ACTION_FIRMWARE_SETUP)) {
uint64_t key;
if (new_mode) {
console_query_mode(&x_max, &y_max);
/* account for padding+status */
visible_max = y_max - 2;
/* Drawing entries starts at idx_first until idx_last. We want to make
* sure that idx_highlight is centered, but not if we are close to the
* beginning/end of the entry list. Otherwise we would have a half-empty
* screen. */
if (config->n_entries <= visible_max || idx_highlight <= visible_max / 2)
idx_first = 0;
else if (idx_highlight >= config->n_entries - (visible_max / 2))
idx_first = config->n_entries - visible_max;
else
idx_first = idx_highlight - (visible_max / 2);
idx_last = idx_first + visible_max - 1;
/* length of the longest entry */
line_width = 0;
for (size_t i = 0; i < config->n_entries; i++)
line_width = MAX(line_width, strlen16(config->entries[i]->title_show));
line_width = MIN(line_width + 2 * entry_padding, x_max);
/* offsets to center the entries on the screen */
x_start = (x_max - (line_width)) / 2;
if (config->n_entries < visible_max)
y_start = ((visible_max - config->n_entries) / 2) + 1;
else
y_start = 0;
/* Put status line after the entry list, but give it some breathing room. */
y_status = MIN(y_start + MIN(visible_max, config->n_entries) + 1, y_max - 1);
lines = strv_free(lines);
clearline = mfree(clearline);
separator = mfree(separator);
/* menu entries title lines */
lines = xnew(char16_t *, config->n_entries + 1);
for (size_t i = 0; i < config->n_entries; i++) {
size_t width = line_width - MIN(strlen16(config->entries[i]->title_show), line_width);
size_t padding = width / 2;
bool odd = width % 2;
/* Make sure there is space for => */
padding = MAX((size_t) 2, padding);
size_t print_width = MIN(
strlen16(config->entries[i]->title_show),
line_width - padding * 2);
assert((padding + 1) <= INT_MAX);
assert(print_width <= INT_MAX);
lines[i] = xasprintf(
"%*ls%.*ls%*ls",
(int) padding, u"",
(int) print_width, config->entries[i]->title_show,
odd ? (int) (padding + 1) : (int) padding, u"");
}
lines[config->n_entries] = NULL;
clearline = xnew(char16_t, x_max + 1);
separator = xnew(char16_t, x_max + 1);
for (size_t i = 0; i < x_max; i++) {
clearline[i] = ' ';
separator[i] = unicode_supported() ? L'─' : L'-';
}
clearline[x_max] = 0;
separator[x_max] = 0;
new_mode = false;
clear = true;
}
if (clear) {
clear_screen(COLOR_NORMAL);
clear = false;
refresh = true;
}
if (refresh) {
for (size_t i = idx_first; i <= idx_last && i < config->n_entries; i++) {
print_at(x_start, y_start + i - idx_first,
i == idx_highlight ? COLOR_HIGHLIGHT : COLOR_ENTRY,
lines[i]);
if (i == config->idx_default_efivar)
print_at(x_start,
y_start + i - idx_first,
i == idx_highlight ? COLOR_HIGHLIGHT : COLOR_ENTRY,
unicode_supported() ? u" ►" : u"=>");
}
refresh = false;
} else if (highlight) {
print_at(x_start, y_start + idx_highlight_prev - idx_first, COLOR_ENTRY, lines[idx_highlight_prev]);
print_at(x_start, y_start + idx_highlight - idx_first, COLOR_HIGHLIGHT, lines[idx_highlight]);
if (idx_highlight_prev == config->idx_default_efivar)
print_at(x_start,
y_start + idx_highlight_prev - idx_first,
COLOR_ENTRY,
unicode_supported() ? u" ►" : u"=>");
if (idx_highlight == config->idx_default_efivar)
print_at(x_start,
y_start + idx_highlight - idx_first,
COLOR_HIGHLIGHT,
unicode_supported() ? u" ►" : u"=>");
highlight = false;
}
if (timeout_remain > 0) {
free(status);
status = xasprintf("Boot in %"PRIu64"s.", timeout_remain);
}
if (status) {
/* If we draw the last char of the last line, the screen will scroll and break our
* input. Therefore, draw one less character then we could for the status message.
* Note that the same does not apply for the separator line as it will never be drawn
* on the last line. */
size_t len = strnlen16(status, x_max - 1);
size_t x = (x_max - len) / 2;
status[len] = '\0';
print_at(0, y_status, COLOR_NORMAL, clearline + x_max - x);
ST->ConOut->OutputString(ST->ConOut, status);
ST->ConOut->OutputString(ST->ConOut, clearline + 1 + x + len);
len = MIN(MAX(len, line_width) + 2 * entry_padding, x_max);
x = (x_max - len) / 2;
print_at(x, y_status - 1, COLOR_NORMAL, separator + x_max - len);
} else {
print_at(0, y_status - 1, COLOR_NORMAL, clearline);
print_at(0, y_status, COLOR_NORMAL, clearline + 1); /* See comment above. */
}
/* Beep several times so that the selected entry can be distinguished. */
if (config->beep)
beep(idx_highlight + 1);
err = console_key_read(&key, timeout_remain > 0 ? 1000 * 1000 : UINT64_MAX);
if (err == EFI_NOT_READY)
/* No input device returned a key, try again. This
* normally should not happen. */
continue;
if (err == EFI_TIMEOUT) {
assert(timeout_remain > 0);
timeout_remain--;
if (timeout_remain == 0) {
action = ACTION_RUN;
break;
}
/* update status */
continue;
}
if (err != EFI_SUCCESS) {
action = ACTION_RUN;
break;
}
timeout_remain = 0;
/* clear status after keystroke */
status = mfree(status);
idx_highlight_prev = idx_highlight;
if (action == ACTION_FIRMWARE_SETUP) {
if (IN_SET(key, KEYPRESS(0, 0, '\r'), KEYPRESS(0, 0, '\n')) &&
set_reboot_into_firmware() == EFI_SUCCESS)
break;
/* Any key other than newline or a failed attempt cancel the request. */
action = ACTION_CONTINUE;
continue;
}
switch (key) {
case KEYPRESS(0, SCAN_UP, 0):
case KEYPRESS(0, SCAN_VOLUME_UP, 0): /* Handle phones/tablets that only have a volume up/down rocker + power key (and otherwise just touchscreen input) */
case KEYPRESS(0, 0, 'k'):
case KEYPRESS(0, 0, 'K'):
if (idx_highlight > 0)
idx_highlight--;
break;
case KEYPRESS(0, SCAN_DOWN, 0):
case KEYPRESS(0, SCAN_VOLUME_DOWN, 0):
case KEYPRESS(0, 0, 'j'):
case KEYPRESS(0, 0, 'J'):
if (idx_highlight < config->n_entries-1)
idx_highlight++;
break;
case KEYPRESS(0, SCAN_HOME, 0):
case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
if (idx_highlight > 0) {
refresh = true;
idx_highlight = 0;
}
break;
case KEYPRESS(0, SCAN_END, 0):
case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
if (idx_highlight < config->n_entries-1) {
refresh = true;
idx_highlight = config->n_entries-1;
}
break;
case KEYPRESS(0, SCAN_PAGE_UP, 0):
if (idx_highlight > visible_max)
idx_highlight -= visible_max;
else
idx_highlight = 0;
break;
case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
idx_highlight += visible_max;
if (idx_highlight > config->n_entries-1)
idx_highlight = config->n_entries-1;
break;
case KEYPRESS(0, 0, '\n'):
case KEYPRESS(0, 0, '\r'):
case KEYPRESS(0, SCAN_F3, 0): /* EZpad Mini 4s firmware sends malformed events */
case KEYPRESS(0, SCAN_F3, '\r'): /* Teclast X98+ II firmware sends malformed events */
case KEYPRESS(0, SCAN_RIGHT, 0):
case KEYPRESS(0, SCAN_SUSPEND, 0): /* Handle phones/tablets with only a power key + volume up/down rocker (and otherwise just touchscreen input) */
action = ACTION_RUN;
break;
case KEYPRESS(0, SCAN_F1, 0):
case KEYPRESS(0, 0, 'h'):
case KEYPRESS(0, 0, 'H'):
case KEYPRESS(0, 0, '?'):
/* This must stay below 80 characters! Q/v/Ctrl+l/f deliberately not advertised. */
status = xasprintf("(d)efault (t/T)imeout (e)dit (r/R)esolution (p)rint %s%s(h)elp",
config->auto_poweroff ? "" : "(O)ff ",
config->auto_reboot ? "" : "re(B)oot ");
break;
case KEYPRESS(0, 0, 'Q'):
action = ACTION_QUIT;
break;
/* Set/unset the preferred entry */
case KEYPRESS(0, 0, 'd'):
if (config->idx_default_efivar != idx_highlight) {
free(config->entry_preferred_efivar);
config->entry_preferred_efivar = xstrdup16(config->entries[idx_highlight]->id);
config->idx_default_efivar = idx_highlight;
status = xstrdup16(u"Preferred boot entry selected.");
} else {
config->entry_preferred_efivar = mfree(config->entry_preferred_efivar);
config->idx_default_efivar = IDX_INVALID;
status = xstrdup16(u"Preferred boot entry cleared.");
}
config->entry_default_efivar = mfree(config->entry_default_efivar);
config->use_saved_entry_efivar = false;
config->use_saved_entry_preferred_efivar = false;
refresh = true;
break;
/* Set/unset the default entry */
case KEYPRESS(0, 0, 'D'):
if (config->idx_default_efivar != idx_highlight) {
free(config->entry_default_efivar);
config->entry_default_efivar = xstrdup16(config->entries[idx_highlight]->id);
config->idx_default_efivar = idx_highlight;
status = xstrdup16(u"Default boot entry selected.");
} else {
config->entry_default_efivar = mfree(config->entry_default_efivar);
config->idx_default_efivar = IDX_INVALID;
status = xstrdup16(u"Default boot entry cleared.");
}
config->entry_preferred_efivar = mfree(config->entry_preferred_efivar);
config->use_saved_entry_efivar = false;
config->use_saved_entry_preferred_efivar = false;
refresh = true;
break;
case KEYPRESS(0, 0, '-'):
case KEYPRESS(0, 0, 'T'):
status = update_timeout_efivar(config, false);
break;
case KEYPRESS(0, 0, '+'):
case KEYPRESS(0, 0, 't'):
status = update_timeout_efivar(config, true);
break;
case KEYPRESS(0, 0, 'e'):
case KEYPRESS(0, 0, 'E'):
/* only the options of configured entries can be edited */
if (!config->editor ||
!LOADER_TYPE_ALLOW_EDITOR(config->entries[idx_highlight]->type)) {
status = xstrdup16(u"Entry does not support editing the command line.");
break;
}
/* Unified kernels that are signed as a whole will not accept command line options
* when secure boot is enabled unless there is none embedded in the image. Do not try
* to pretend we can edit it to only have it be ignored. */
if (!LOADER_TYPE_ALLOW_EDITOR_IN_SB(config->entries[idx_highlight]->type) &&
secure_boot_enabled() &&
config->entries[idx_highlight]->options) {
status = xstrdup16(u"Entry not editable in SecureBoot mode.");
break;
}
/* The edit line may end up on the last line of the screen. And even though we're
* not telling the firmware to advance the line, it still does in this one case,
* causing a scroll to happen that screws with our beautiful boot loader output.
* Since we cannot paint the last character of the edit line, we simply start
* at x-offset 1 for symmetry. */
print_at(1, y_status, COLOR_EDIT, clearline + 2);
if (line_edit(&config->entries[idx_highlight]->options, x_max - 2, y_status))
action = ACTION_RUN;
print_at(1, y_status, COLOR_NORMAL, clearline + 2);
/* The options string was now edited, hence we have to pass it to the invoked
* binary. */
config->entries[idx_highlight]->options_implied = false;
break;
case KEYPRESS(0, 0, 'v'):
status = xasprintf(
"systemd-boot " GIT_VERSION " (" EFI_MACHINE_TYPE_NAME "), "
"UEFI Specification %u.%02u, Vendor %ls %u.%02u",
ST->Hdr.Revision >> 16,
ST->Hdr.Revision & 0xffff,
ST->FirmwareVendor,
ST->FirmwareRevision >> 16,
ST->FirmwareRevision & 0xffff);
break;
case KEYPRESS(0, 0, 'p'):
case KEYPRESS(0, 0, 'P'):
print_status(config, loaded_image_path);
clear = true;
break;
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
case 'L': /* only uppercase, do not conflict with lower-case 'l' which picks first Linux entry */
clear = true;
break;
case KEYPRESS(0, 0, 'r'):
err = console_set_mode(CONSOLE_MODE_NEXT);
if (err != EFI_SUCCESS)
status = xasprintf_status(err, "Error changing console mode: %m");
else {
config->console_mode_efivar = ST->ConOut->Mode->Mode;
status = xasprintf(
"Console mode changed to %" PRIi64 ".",
config->console_mode_efivar);
}
new_mode = true;
break;
case KEYPRESS(0, 0, 'R'):
config->console_mode_efivar = CONSOLE_MODE_KEEP;
err = console_set_mode(config->console_mode == CONSOLE_MODE_KEEP ?
console_mode_initial : config->console_mode);
if (err != EFI_SUCCESS)
status = xasprintf_status(err, "Error resetting console mode: %m");
else
status = xasprintf(
"Console mode reset to %s default.",
config->console_mode == CONSOLE_MODE_KEEP ?
"firmware" :
"configuration file");
new_mode = true;
break;
case KEYPRESS(0, 0, 'f'):
case KEYPRESS(0, 0, 'F'):
case KEYPRESS(0, SCAN_F2, 0): /* Most vendors. */
case KEYPRESS(0, SCAN_F10, 0): /* HP and Lenovo. */
case KEYPRESS(0, SCAN_DELETE, 0): /* Same as F2. */
case KEYPRESS(0, SCAN_ESC, 0): /* HP. */
if (FLAGS_SET(get_os_indications_supported(), EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) {
action = ACTION_FIRMWARE_SETUP;
/* Let's make sure the user really wants to do this. */
status = xstrdup16(u"Press Enter to reboot into firmware interface.");
} else
status = xstrdup16(u"Reboot into firmware interface not supported.");
break;
case KEYPRESS(0, 0, 'O'): /* Only uppercase, so that it can't be hit so easily fat-fingered,
* but still works safely over serial. */
action = ACTION_POWEROFF;
break;
case KEYPRESS(0, 0, 'B'): /* ditto */
action = ACTION_REBOOT;
break;
default:
/* jump with a hotkey directly to a matching entry */
idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
if (idx == IDX_INVALID)
break;
idx_highlight = idx;
refresh = true;
}
if (idx_highlight > idx_last) {
idx_last = idx_highlight;
idx_first = 1 + idx_highlight - visible_max;
refresh = true;
} else if (idx_highlight < idx_first) {
idx_first = idx_highlight;
idx_last = idx_highlight + visible_max-1;
refresh = true;
}
if (!refresh && idx_highlight != idx_highlight_prev)
highlight = true;
}
/* Update EFI vars after we left the menu to reduce NVRAM writes. */
if (default_efivar_saved != config->idx_default_efivar) {
if (config->entry_preferred_efivar)
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderEntryPreferred", config->entry_preferred_efivar, EFI_VARIABLE_NON_VOLATILE);
else
efivar_unset(MAKE_GUID_PTR(LOADER), u"LoaderEntryPreferred", EFI_VARIABLE_NON_VOLATILE);
if (config->entry_default_efivar)
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderEntryDefault", config->entry_default_efivar, EFI_VARIABLE_NON_VOLATILE);
else
efivar_unset(MAKE_GUID_PTR(LOADER), u"LoaderEntryDefault", EFI_VARIABLE_NON_VOLATILE);
}
if (console_mode_efivar_saved != config->console_mode_efivar) {
if (config->console_mode_efivar == CONSOLE_MODE_KEEP)
efivar_unset(MAKE_GUID_PTR(LOADER), u"LoaderConfigConsoleMode", EFI_VARIABLE_NON_VOLATILE);
else
efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"LoaderConfigConsoleMode",
config->console_mode_efivar, EFI_VARIABLE_NON_VOLATILE);
}
if (timeout_efivar_saved != config->timeout_sec_efivar) {
switch (config->timeout_sec_efivar) {
case TIMEOUT_UNSET:
efivar_unset(MAKE_GUID_PTR(LOADER), u"LoaderConfigTimeout", EFI_VARIABLE_NON_VOLATILE);
break;
case TIMEOUT_MENU_DISABLED:
assert_not_reached();
case TIMEOUT_MENU_FORCE:
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderConfigTimeout", u"menu-force", EFI_VARIABLE_NON_VOLATILE);
break;
case TIMEOUT_MENU_HIDDEN:
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderConfigTimeout", u"menu-hidden", EFI_VARIABLE_NON_VOLATILE);