HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/boot/boot.c
Git blob: 91c82b3536d2e6cb8dcb6498d9825975a409d28a
Lines 3001–3525 of 3,525
if (err != EFI_SUCCESS)
return log_error_status(err, "Error opening root path: %m");
err = make_file_device_path(entry->device, entry->loader, &path);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error making file device path: %m");
boot_policy = false;
}
/* Authenticate the image before we continue with initrd or DT stuff */
err = shim_load_image(parent_image, path, boot_policy, &image);
if (err != EFI_SUCCESS) {
if (entry->url) {
/* EFI_NOT_FOUND typically indicates that no network stack or NIC was available, let's give the user a hint. */
if (err == EFI_NOT_FOUND) {
log_info("Unable to boot remote UKI %ls, is networking available?", entry->url);
return err;
}
return log_error_status(err, "Error loading loading remote UKI %ls: %m", entry->url);
}
return log_error_status(err, "Error loading EFI binary %ls: %m", entry->loader);
}
_cleanup_free_ char16_t *options_initrd = NULL;
_cleanup_pages_ Pages initrd_pages = {}; /* Note: please keep order intact: these pages should be released after the initrd handle is released */
_cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL;
size_t initrd_size = 0;
if (image_root) {
/* DTBs are loaded by the kernel before ExitBootServices(), and they can be used to map and
* assign arbitrary memory ranges, so skip them when secure boot is enabled as the DTB here
* is unverified. */
if (entry->devicetree && !secure_boot_enabled()) {
err = devicetree_install(&dtstate, image_root, entry->devicetree);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error loading %ls: %m", entry->devicetree);
}
switch (entry->type) {
case LOADER_LINUX:
/* For traditional Linux we follow 'initrd' links, because that's how things worked in the good old days */
err = initrd_prepare(image_root, entry, &options_initrd, &initrd_pages, &initrd_size);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error preparing initrd: %m");
break;
case LOADER_UKI:
case LOADER_UKI_URL:
/* For modern UKIs we'll not bother with 'initrd', but we'll instead support 'extra'
* for loading credentials, sysexts, and confexts. */
err = load_extras(image_root, entry, &initrd_pages, &initrd_size);
if (err != EFI_SUCCESS)
return err; /* load_extras() logs on its own */
break;
default:
;
}
err = initrd_register(&IOVEC_MAKE(PHYSICAL_ADDRESS_TO_POINTER(initrd_pages.addr), initrd_size), &initrd_handle);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error registering initrd: %m");
/* NB: the initrd pages remain in our possession, we will free them if executing the image fails below */
}
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error getting LoadedImageProtocol handle: %m");
/* If we had to append an initrd= entry to the command line, we have to pass it, and measure it.
* Otherwise, only pass/measure it if it is not implicit anyway (i.e. embedded into the UKI or
* so). */
_cleanup_free_ char16_t *options = xstrdup16(options_initrd ?: entry->options_implied ? NULL : entry->options);
if (entry->type == LOADER_LINUX && !is_confidential_vm()) {
const char *extra = smbios_find_oem_string("io.systemd.boot.kernel-cmdline-extra=", /* after= */ NULL);
if (extra) {
_cleanup_free_ char16_t *tmp = TAKE_PTR(options), *extra16 = xstr8_to_16(extra);
if (isempty(tmp))
options = TAKE_PTR(extra16);
else
options = xasprintf("%ls %ls", tmp, extra16);
}
}
/* Prefix profile if it's non-zero */
if (entry->profile > 0) {
_cleanup_free_ char16_t *tmp = TAKE_PTR(options);
if (isempty(tmp))
options = xasprintf("@%u", entry->profile);
else
options = xasprintf("@%u %ls", entry->profile, tmp);
}
if (options) {
loaded_image->LoadOptions = options;
loaded_image->LoadOptionsSize = strsize16(options);
/* Try to log any options to the TPM, especially to catch manually edited options */
(void) tpm_log_load_options(options, NULL);
}
efivar_set_time_usec(MAKE_GUID_PTR(LOADER), u"LoaderTimeExecUSec", 0);
err = BS->StartImage(image, NULL, NULL);
graphics_mode(false);
if (err == EFI_SUCCESS)
return EFI_SUCCESS;
/* Try calling the kernel compat entry point if one exists. */
if (err == EFI_UNSUPPORTED && entry->type == LOADER_LINUX) {
uint32_t compat_address;
err = pe_kernel_info(loaded_image->ImageBase, loaded_image->ImageSize, /* ret_entry_point= */ NULL, &compat_address,
/* ret_size_in_memory= */ NULL,
/* ret_section_alignment= */ NULL);
if (err != EFI_SUCCESS) {
if (err != EFI_UNSUPPORTED)
return log_error_status(err, "Error finding kernel compat entry address: %m");
} else if (compat_address > 0) {
EFI_IMAGE_ENTRY_POINT kernel_entry =
(EFI_IMAGE_ENTRY_POINT) ((uint8_t *) loaded_image->ImageBase + compat_address);
err = kernel_entry(image, ST);
graphics_mode(false);
if (err == EFI_SUCCESS)
return EFI_SUCCESS;
} else
err = EFI_UNSUPPORTED;
}
return log_error_status(err, "Failed to execute %ls (%ls): %m", entry->title_show, entry->loader ?: entry->url);
}
static void config_free(Config *config) {
assert(config);
for (size_t i = 0; i < config->n_entries; i++)
boot_entry_free(config->entries[i]);
free(config->entries);
free(config->entry_default_config);
free(config->entry_default_efivar);
free(config->entry_preferred_config);
free(config->entry_preferred_efivar);
free(config->entry_oneshot);
free(config->entry_saved);
free(config->entry_sysfail);
}
static void config_write_entries_to_variable(Config *config) {
_cleanup_free_ char *buffer = NULL;
size_t sz = 0;
char *p;
assert(config);
for (size_t i = 0; i < config->n_entries; i++)
sz += strsize16(config->entries[i]->id);
p = buffer = xmalloc(sz);
for (size_t i = 0; i < config->n_entries; i++)
p = mempcpy(p, config->entries[i]->id, strsize16(config->entries[i]->id));
assert(p == buffer + sz);
/* Store the full list of discovered entries. */
(void) efivar_set_raw(MAKE_GUID_PTR(LOADER), u"LoaderEntries", buffer, sz, 0);
}
static void save_selected_entry(const Config *config, const BootEntry *entry) {
assert(config);
assert(entry);
if (!LOADER_TYPE_SAVE_ENTRY(entry->type))
return;
/* Always export the selected boot entry to the system in a volatile var. */
(void) efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderEntrySelected", entry->id, 0);
/* Do not save or delete if this was a oneshot boot. */
if (streq16(config->entry_oneshot, entry->id))
return;
if (config->use_saved_entry_efivar || (!config->entry_default_efivar && config->use_saved_entry)) {
/* Avoid unnecessary NVRAM writes. */
if (streq16(config->entry_saved, entry->id))
return;
(void) efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderEntryLastBooted", entry->id, EFI_VARIABLE_NON_VOLATILE);
} else
/* Delete the non-volatile var if not needed. */
(void) efivar_unset(MAKE_GUID_PTR(LOADER), u"LoaderEntryLastBooted", EFI_VARIABLE_NON_VOLATILE);
}
static EFI_STATUS call_secure_boot_enroll(const BootEntry *entry, EFI_FILE *root_dir, EFI_HANDLE parent_image) {
assert(entry);
return secure_boot_enroll_at(root_dir, entry->directory, /* force= */ true, /* action= */ ENROLL_ACTION_REBOOT,
ENROLL_TIMEOUT_DEFAULT);
}
static EFI_STATUS secure_boot_discover_keys(Config *config, EFI_FILE *root_dir) {
EFI_STATUS err;
_cleanup_file_close_ EFI_FILE *keys_basedir = NULL;
if (config->secure_boot_enroll == ENROLL_OFF)
return EFI_SUCCESS;
if (!IN_SET(secure_boot_mode(), SECURE_BOOT_SETUP, SECURE_BOOT_AUDIT))
return EFI_SUCCESS;
/* the lack of a 'keys' directory is not fatal and is silently ignored */
err = open_directory(root_dir, u"\\loader\\keys", &keys_basedir);
if (err == EFI_NOT_FOUND)
return EFI_SUCCESS;
if (err != EFI_SUCCESS)
return err;
for (;;) {
_cleanup_free_ EFI_FILE_INFO *dirent = NULL;
size_t dirent_size = 0;
BootEntry *entry = NULL;
err = readdir(keys_basedir, &dirent, &dirent_size);
if (err != EFI_SUCCESS || !dirent)
return err;
if (dirent->FileName[0] == '.')
continue;
if (!FLAGS_SET(dirent->Attribute, EFI_FILE_DIRECTORY))
continue;
entry = xnew(BootEntry, 1);
*entry = (BootEntry) {
.id = xasprintf("secure-boot-keys-%ls", dirent->FileName),
.title = xasprintf("Enroll Secure Boot keys: %ls", dirent->FileName),
.directory = xasprintf("\\loader\\keys\\%ls", dirent->FileName),
.type = LOADER_SECURE_BOOT_KEYS,
.tries_done = -1,
.tries_left = -1,
.call = call_secure_boot_enroll,
};
config_add_entry(config, entry);
if (IN_SET(config->secure_boot_enroll, ENROLL_IF_SAFE, ENROLL_FORCE) &&
strcaseeq16(dirent->FileName, u"auto"))
/* If we auto enroll successfully this call does not return.
* If it fails we still want to add other potential entries to the menu. */
secure_boot_enroll_at(root_dir, entry->directory, config->secure_boot_enroll == ENROLL_FORCE,
config->secure_boot_enroll_action, config->secure_boot_enroll_timeout_sec);
}
return EFI_SUCCESS;
}
static void export_loader_variables(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
uint64_t init_usec) {
static const uint64_t loader_features =
EFI_LOADER_FEATURE_CONFIG_TIMEOUT |
EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT |
EFI_LOADER_FEATURE_ENTRY_DEFAULT |
EFI_LOADER_FEATURE_ENTRY_PREFERRED |
EFI_LOADER_FEATURE_ENTRY_ONESHOT |
EFI_LOADER_FEATURE_BOOT_COUNTING |
EFI_LOADER_FEATURE_XBOOTLDR |
EFI_LOADER_FEATURE_RANDOM_SEED |
EFI_LOADER_FEATURE_LOAD_DRIVER |
EFI_LOADER_FEATURE_SORT_KEY |
EFI_LOADER_FEATURE_SAVED_ENTRY |
EFI_LOADER_FEATURE_DEVICETREE |
EFI_LOADER_FEATURE_SECUREBOOT_ENROLL |
EFI_LOADER_FEATURE_RETAIN_SHIM |
EFI_LOADER_FEATURE_MENU_DISABLE |
EFI_LOADER_FEATURE_MULTI_PROFILE_UKI |
EFI_LOADER_FEATURE_REPORT_URL |
EFI_LOADER_FEATURE_TYPE1_UKI |
EFI_LOADER_FEATURE_TYPE1_UKI_URL |
EFI_LOADER_FEATURE_TPM2_ACTIVE_PCR_BANKS |
EFI_LOADER_FEATURE_KEYBOARD_LAYOUT |
EFI_LOADER_FEATURE_SMBIOS_MEASURED |
0;
assert(loaded_image);
efivar_set_time_usec(MAKE_GUID_PTR(LOADER), u"LoaderTimeInitUSec", init_usec);
(void) efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderInfo", u"systemd-boot " GIT_VERSION, 0);
(void) efivar_set_uint64_le(MAKE_GUID_PTR(LOADER), u"LoaderFeatures", loader_features, 0);
}
static void config_add_system_entries(Config *config) {
assert(config);
if (config->auto_firmware && FLAGS_SET(get_os_indications_supported(), EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) {
BootEntry *entry = xnew(BootEntry, 1);
*entry = (BootEntry) {
.type = LOADER_FWSETUP,
.id = xstrdup16(u"auto-reboot-to-firmware-setup"),
.title = xstrdup16(u"Reboot Into Firmware Interface"),
.call = call_reboot_into_firmware,
.tries_done = -1,
.tries_left = -1,
};
config_add_entry(config, entry);
}
if (config->auto_poweroff) {
BootEntry *entry = xnew(BootEntry, 1);
*entry = (BootEntry) {
.type = LOADER_POWEROFF,
.id = xstrdup16(u"auto-poweroff"),
.title = xstrdup16(u"Power Off The System"),
.call = call_poweroff_system,
.tries_done = -1,
.tries_left = -1,
};
config_add_entry(config, entry);
}
if (config->auto_reboot) {
BootEntry *entry = xnew(BootEntry, 1);
*entry = (BootEntry) {
.type = LOADER_REBOOT,
.id = xstrdup16(u"auto-reboot"),
.title = xstrdup16(u"Reboot The System"),
.call = call_reboot_system,
.tries_done = -1,
.tries_left = -1,
};
config_add_entry(config, entry);
}
}
static void config_load_all_entries(
Config *config,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *loaded_image_path,
EFI_FILE *root_dir) {
assert(config);
assert(loaded_image);
assert(root_dir);
config_load_defaults(config, root_dir);
/* Scan /EFI/Linux/ directory */
config_load_type2_entries(config, loaded_image->DeviceHandle, root_dir);
/* Scan /loader/entries/\*.conf files */
config_load_type1_entries(config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
/* Similar, but on any XBOOTLDR partition */
config_load_xbootldr(config, loaded_image->DeviceHandle);
/* Pick up entries defined via SMBIOS Type #11 */
config_load_smbios_entries(config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
/* Sort entries after version number */
sort_pointer_array((void **) config->entries, config->n_entries, (compare_pointer_func_t) boot_entry_compare);
/* If we find some well-known loaders, add them to the end of the list */
config_add_entry_osx(config);
config_add_entry_windows(config, loaded_image->DeviceHandle, root_dir);
config_add_entry_loader_auto(
config,
loaded_image->DeviceHandle,
root_dir,
/* loaded_image_path= */ NULL,
u"auto-efi-shell",
's',
u"EFI Shell",
u"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
config_add_entry_loader_auto(
config,
loaded_image->DeviceHandle,
root_dir,
loaded_image_path,
u"auto-efi-default",
'\0',
u"EFI Default Loader",
/* loader= */ NULL);
config_add_system_entries(config);
/* Using the rules defined by the `secure-boot-enroll` variable, find secure boot signing keys
* and perform operations like autoloading them or create menu entries if configured. */
(void) secure_boot_discover_keys(config, root_dir);
if (config->n_entries == 0)
return;
config_write_entries_to_variable(config);
generate_boot_entry_titles(config);
/* Select entry by configured pattern or EFI LoaderDefaultEntry= variable */
config_select_default_entry(config);
}
static EFI_STATUS discover_root_dir(EFI_LOADED_IMAGE_PROTOCOL *loaded_image, EFI_FILE **ret_dir) {
if (is_direct_boot(loaded_image->DeviceHandle))
return vmm_open(&loaded_image->DeviceHandle, ret_dir);
else
return open_volume(loaded_image->DeviceHandle, ret_dir);
}
static EFI_STATUS run(EFI_HANDLE image) {
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
_cleanup_file_close_ EFI_FILE *root_dir = NULL;
_cleanup_(config_free) Config config = {};
EFI_STATUS err;
uint64_t init_usec;
bool menu = false;
/* set loglevel early to simplify debugging before loader.conf is loaded */
log_set_max_level_from_smbios();
init_usec = time_usec();
err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error getting a LoadedImageProtocol handle: %m");
err = discover_root_dir(loaded_image, &root_dir);
if (err != EFI_SUCCESS) {
log_error_status(err, "Unable to open root directory: %m");
/* If opening the root directory fails this typically means someone is trying to boot our
* systemd-boot EFI PE binary as network boot NBP. That cannot work however, since we
* wouldn't find any menu entries. Provide a helpful message what to try instead. */
if (err == EFI_UNSUPPORTED)
log_info("| Note that invoking systemd-boot directly as UEFI network boot NBP is not\n"
"| supported. Instead of booting the systemd-boot PE binary (i.e. an .efi file)\n"
"| via the network, use an EFI GPT disk image (i.e. a file with .img suffix)\n"
"| containing systemd-boot instead.");
return err;
}
/* Ask Shim to leave its protocol around, so that the stub can use it to validate PEs.
* By default, Shim uninstalls its protocol when calling StartImage(). */
shim_retain_protocol();
export_common_variables(loaded_image);
export_loader_variables(loaded_image, init_usec);
/* Measure SMBIOS data into PCR 1. This is done early, and suppressed if sd-stub later runs in
* the same boot (and vice versa), via the LoaderPcrSMBIOS EFI variable. */
measure_smbios();
(void) load_drivers(image, loaded_image, root_dir);
_cleanup_free_ char16_t *loaded_image_path = NULL;
(void) device_path_to_str(loaded_image->FilePath, &loaded_image_path);
config_load_all_entries(&config, loaded_image, loaded_image_path, root_dir);
(void) sysfail_process(&config);
if (config.n_entries == 0)
return log_error_status(
EFI_NOT_FOUND,
"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
/* select entry or show menu when key is pressed or timeout is set */
if (config.force_menu || !IN_SET(config.timeout_sec, TIMEOUT_MENU_HIDDEN, TIMEOUT_MENU_DISABLED))
menu = true;
else if (config.timeout_sec != TIMEOUT_MENU_DISABLED) {
uint64_t key;
/* Block up to 100ms to give firmware time to get input working. */
err = console_key_read(&key, 100 * 1000);
if (err == EFI_SUCCESS) {
/* find matching key in boot entries */
size_t idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
if (idx != IDX_INVALID)
config.idx_default = idx;
else
menu = true;
}
}
for (;;) {
BootEntry *entry;
entry = config.entries[config.idx_default];
if (menu) {
efivar_set_time_usec(MAKE_GUID_PTR(LOADER), u"LoaderTimeMenuUSec", 0);
if (!menu_run(&config, &entry, loaded_image_path))
return EFI_SUCCESS;
}
(void) boot_entry_bump_counters(entry);
save_selected_entry(&config, entry);
/* Optionally, read a random seed off the ESP and pass it to the OS */
if (LOADER_TYPE_PROCESS_RANDOM_SEED(entry->type))
(void) process_random_seed(root_dir);
err = ASSERT_PTR(entry->call)(entry, root_dir, image);
if (err != EFI_SUCCESS) {
if (config.reboot_on_error == REBOOT_YES || (config.reboot_on_error == REBOOT_AUTO && entry->tries_left > 0)) {
printf("Failed to start boot entry. Rebooting in 5s.\n");
BS->Stall(5 * 1000 * 1000);
(void) call_reboot_system(/* entry= */ NULL, /* root_dir= */ NULL, /* parent_image= */ NULL);
}
return err;
}
menu = true;
config.timeout_sec = 0;
}
}
// NOLINTNEXTLINE(misc-use-internal-linkage)
DEFINE_EFI_MAIN_FUNCTION(run, "systemd-boot", /* wait_for_debugger= */ false);