HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/bootspec.c
Git blob: 4615a8755ea1a36c83f44848960af185f13bd0ae
Lines 1001–2000 of 2,354
return log_oom();
tmp.root = strdup(root);
if (!tmp.root)
return log_oom();
tmp.kernel = path_make_absolute(k, "/");
if (!tmp.kernel)
return log_oom();
tmp.options = strv_new(cmdline_text);
if (!tmp.options)
return log_oom();
if (uname_text) {
tmp.uname = strdup(uname_text);
if (!tmp.uname)
return log_oom();
}
if (profile_title)
tmp.title = strjoin(good_name, " (", profile_title, ")");
else if (profile_id)
tmp.title = strjoin(good_name, " (", profile_id, ")");
else if (profile > 0)
(void) asprintf(&tmp.title, "%s (@%u)", good_name, profile);
else
tmp.title = strdup(good_name);
if (!tmp.title)
return log_oom();
if (good_sort_key) {
tmp.sort_key = strdup(good_sort_key);
if (!tmp.sort_key)
return log_oom();
}
if (good_version) {
tmp.version = strdup(good_version);
if (!tmp.version)
return log_oom();
}
*ret = TAKE_STRUCT(tmp);
return 0;
}
static const IMAGE_SECTION_HEADER* pe_find_profile_section_table(
const PeHeader *pe_header,
const IMAGE_SECTION_HEADER *sections,
unsigned profile,
size_t *ret_n_sections) {
assert(pe_header);
/* Looks for the part of the section table that defines the specified profile. If 'profile' is
* specified as UINT_MAX this will look for the base profile. */
if (le16toh(pe_header->pe.NumberOfSections) == 0)
return NULL;
assert(sections);
const IMAGE_SECTION_HEADER
*p = sections,
*e = sections + le16toh(pe_header->pe.NumberOfSections),
*start = profile == UINT_MAX ? sections : NULL,
*end;
unsigned current_profile = UINT_MAX;
for (;;) {
p = pe_section_table_find(p, e - p, ".profile");
if (!p) {
end = e;
break;
}
if (current_profile == profile) {
end = p;
break;
}
if (current_profile == UINT_MAX)
current_profile = 0;
else
current_profile++;
if (current_profile == profile)
start = p;
p++; /* Continue scanning after the .profile entry we just found */
}
if (!start)
return NULL;
if (ret_n_sections)
*ret_n_sections = end - start;
return start;
}
static int trim_cmdline(char **cmdline) {
assert(cmdline);
/* Strips leading and trailing whitespace from command line */
if (!*cmdline)
return 0;
const char *skipped = skip_leading_chars(*cmdline, WHITESPACE);
if (isempty(skipped)) {
*cmdline = mfree(*cmdline);
return 0;
}
if (skipped != *cmdline) {
_cleanup_free_ char *c = strdup(skipped);
if (!c)
return -ENOMEM;
free_and_replace(*cmdline, c);
}
delete_trailing_chars(*cmdline, WHITESPACE);
return 1;
}
/* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
* the ones we do care about and we are willing to load into memory have this size limit.) */
#define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
int pe_find_uki_sections(
int fd,
const char *path,
unsigned profile,
char **ret_osrelease,
char **ret_profile,
char **ret_cmdline,
char **ret_uname) {
_cleanup_free_ char *osrelease_text = NULL, *profile_text = NULL, *cmdline_text = NULL, *uname_text = NULL;
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
_cleanup_free_ PeHeader *pe_header = NULL;
int r;
assert(fd >= 0);
assert(path);
assert(profile != UINT_MAX);
r = pe_load_headers_and_sections(fd, &pe_header, §ions);
if (r < 0)
return log_error_errno(r, "Failed to parse PE file '%s': %m", path);
if (!pe_is_uki(pe_header, sections))
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parsed PE file '%s' is not a UKI.", path);
if (!pe_is_native(pe_header)) /* Don't process non-native UKIs */
goto nothing;
/* Find part of the section table for this profile */
size_t n_psections = 0;
const IMAGE_SECTION_HEADER *psections = pe_find_profile_section_table(pe_header, sections, profile, &n_psections);
if (!psections && profile != 0) /* Profile not found? (Profile @0 needs no explicit .profile!) */
goto nothing;
/* Find base profile part of section table */
size_t n_bsections;
const IMAGE_SECTION_HEADER *bsections = ASSERT_PTR(pe_find_profile_section_table(pe_header, sections, UINT_MAX, &n_bsections));
struct {
const char *name;
char **data;
} table[] = {
{ ".osrel", &osrelease_text },
{ ".profile", &profile_text },
{ ".cmdline", &cmdline_text },
{ ".uname", &uname_text },
};
FOREACH_ELEMENT(t, table) {
const IMAGE_SECTION_HEADER *found;
/* First look in the profile part of the section table, and if we don't find anything there, look into the base part */
found = pe_section_table_find(psections, n_psections, t->name);
if (!found) {
found = pe_section_table_find(bsections, n_bsections, t->name);
if (!found)
continue;
}
/* Permit "masking" of sections in the base profile */
if (le32toh(found->VirtualSize) == 0)
continue;
r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) t->data, /* ret_size= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to load contents of section '%s': %m", t->name);
}
if (!osrelease_text)
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unified kernel image lacks .osrel data for profile @%u, refusing.", profile);
if (trim_cmdline(&cmdline_text) < 0)
return log_oom();
if (ret_osrelease)
*ret_osrelease = TAKE_PTR(osrelease_text);
if (ret_profile)
*ret_profile = TAKE_PTR(profile_text);
if (ret_cmdline)
*ret_cmdline = TAKE_PTR(cmdline_text);
if (ret_uname)
*ret_uname = TAKE_PTR(uname_text);
return 1;
nothing:
if (ret_osrelease)
*ret_osrelease = NULL;
if (ret_profile)
*ret_profile = NULL;
if (ret_cmdline)
*ret_cmdline = NULL;
if (ret_uname)
*ret_uname = NULL;
return 0;
}
static int pe_find_addon_sections(
int fd,
const char *path,
char **ret_cmdline) {
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
_cleanup_free_ PeHeader *pe_header = NULL;
int r;
assert(fd >= 0);
assert(path);
assert(ret_cmdline);
r = pe_load_headers_and_sections(fd, &pe_header, §ions);
if (r < 0)
return log_error_errno(r, "Failed to parse PE file '%s': %m", path);
if (!pe_is_addon(pe_header, sections))
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parse PE file '%s' is not an add-on.", path);
/* Define early, before the gotos below */
_cleanup_free_ char *cmdline_text = NULL;
if (!pe_is_native(pe_header))
goto nothing;
const IMAGE_SECTION_HEADER *found = pe_section_table_find(sections, le16toh(pe_header->pe.NumberOfSections), ".cmdline");
if (!found)
goto nothing;
r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) &cmdline_text, /* ret_size= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to load contents of section '.cmdline': %m");
if (trim_cmdline(&cmdline_text) < 0)
return log_oom();
*ret_cmdline = TAKE_PTR(cmdline_text);
return 1;
nothing:
*ret_cmdline = NULL;
return 0;
}
static int boot_entries_find_unified_extras(
BootConfig *config,
int d_fd,
const char *extra_dir,
BootEntryExtraType only_type,
const char *where,
bool suppress_seen,
BootEntryExtras *extras) {
int r;
assert(config);
assert(extras);
_cleanup_closedir_ DIR *d = NULL;
r = chase_and_opendirat(
/* root_fd= */ d_fd,
/* dir_fd= */ d_fd,
extra_dir,
/* chase_flags= */ 0,
/* ret_path= */ NULL,
&d);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to open '%s/%s': %m", where, skip_leading_slash(extra_dir));
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read '%s': %m", extra_dir)) {
if (!dirent_is_file(de))
continue;
BootEntryExtraType type = boot_entry_extra_type_from_filename(de->d_name);
if (type < 0) {
log_debug_errno(type, "Unrecognized extra file '%s', skipping.", de->d_name);
continue;
}
if (only_type >= 0 && type != only_type) {
log_debug("Extra file '%s' type not permitted in '%s', skipping.", de->d_name, extra_dir);
continue;
}
_cleanup_free_ char *location = path_join(extra_dir, de->d_name);
if (!location)
return log_oom();
_cleanup_close_ int pin_fd = openat(dirfd(d), de->d_name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
if (pin_fd < 0) {
log_debug_errno(errno, "Failed to pin '%s', ignoring: %m", location);
continue;
}
r = fd_verify_regular(pin_fd);
if (r < 0) {
log_debug_errno(r, "Unrecognized inode type of '%s', skipping.", location);
continue;
}
if (suppress_seen) {
r = config_check_inode_relevant_and_unseen(config, pin_fd, location);
if (r < 0)
return r;
if (r == 0) /* inode already seen or otherwise not relevant */
continue;
}
_cleanup_free_ char *cmdline = NULL;
if (type == BOOT_ENTRY_ADDON) {
_cleanup_close_ int fd = fd_reopen(pin_fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0) {
log_debug_errno(fd, "Failed to open '%s', ignoring: %m", location);
continue;
}
/* Try to extract the command line, but let's handle any failures gracefully, but
* still mention the extra file exists. */
(void) pe_find_addon_sections(fd, location, &cmdline);
}
r = boot_entry_extras_add(extras, type, location, cmdline);
if (r < 0)
return r;
}
return 0;
}
static int boot_entries_find_unified_global_extras(
BootConfig *config,
const char *where,
const char *extra_dir,
BootEntryExtraType only_type,
BootEntryExtras *extras) {
assert(extras);
_cleanup_close_ int where_fd = RET_NERRNO(open(where, O_DIRECTORY|O_CLOEXEC));
if (where_fd == -ENOENT)
return 0;
if (where_fd < 0)
return log_error_errno(where_fd, "Failed to open '%s': %m", where);
return boot_entries_find_unified_extras(
config,
where_fd,
extra_dir,
only_type,
where,
/* suppress_seen= */ true,
extras);
}
static int boot_entries_find_unified_local_extras(
BootConfig *config,
int d_fd,
const char *uki,
const char *where,
BootEntry *ret) {
_cleanup_free_ char *extra_dir = NULL;
assert(ret);
extra_dir = strjoin(uki, ".extra.d");
if (!extra_dir)
return log_oom();
return boot_entries_find_unified_extras(
config,
d_fd,
extra_dir,
/* only_type= */ _BOOT_ENTRY_EXTRA_TYPE_INVALID,
where,
/* suppress_seen= */ false,
&ret->local_extras);
}
static int boot_entries_find_unified(
BootConfig *config,
const char *root,
BootEntrySource source,
const char *dir) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *full = NULL;
int r;
assert(config);
assert(dir);
r = chase_and_opendir(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &full, &d);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(dir));
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) {
if (!dirent_is_file(de))
continue;
if (!endswith_no_case(de->d_name, ".efi"))
continue;
_cleanup_close_ int fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY);
if (fd < 0) {
log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
continue;
}
r = config_check_inode_relevant_and_unseen(config, fd, de->d_name);
if (r < 0)
return r;
if (r == 0) /* inode already seen or otherwise not relevant */
continue;
_cleanup_free_ char *j = path_join(full, de->d_name);
if (!j)
return log_oom();
for (unsigned p = 0; p < UNIFIED_PROFILES_MAX; p++) {
_cleanup_free_ char *osrelease = NULL, *profile = NULL, *cmdline = NULL, *uname = NULL;
r = pe_find_uki_sections(fd, j, p, &osrelease, &profile, &cmdline, &uname);
if (r == 0) /* this profile does not exist, we are done */
break;
if (r < 0)
continue;
if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
return log_oom();
BootEntry *entry = config->entries + config->n_entries;
if (boot_entry_load_unified(root, source, j, p, osrelease, profile, cmdline, uname, entry) < 0)
continue;
/* Look for .efi.extra.d/ */
(void) boot_entries_find_unified_local_extras(config, dirfd(d), de->d_name, full, entry);
/* Set up the backpointer, so that we can find the global extras */
entry->global_extras = &config->global_extras[source];
config->n_entries++;
}
}
return 0;
}
static bool find_nonunique(const BootEntry *entries, size_t n_entries, bool arr[]) {
bool non_unique = false;
assert(entries || n_entries == 0);
assert(arr || n_entries == 0);
for (size_t i = 0; i < n_entries; i++)
arr[i] = false;
for (size_t i = 0; i < n_entries; i++)
for (size_t j = 0; j < n_entries; j++)
if (i != j && streq(boot_entry_title(entries + i),
boot_entry_title(entries + j)))
non_unique = arr[i] = arr[j] = true;
return non_unique;
}
static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
_cleanup_free_ bool *arr = NULL;
char *s;
assert(entries || n_entries == 0);
if (n_entries == 0)
return 0;
arr = new(bool, n_entries);
if (!arr)
return -ENOMEM;
/* Find _all_ non-unique titles */
if (!find_nonunique(entries, n_entries, arr))
return 0;
/* Add version to non-unique titles */
for (size_t i = 0; i < n_entries; i++)
if (arr[i] && entries[i].version) {
if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version) < 0)
return -ENOMEM;
free_and_replace(entries[i].show_title, s);
}
if (!find_nonunique(entries, n_entries, arr))
return 0;
/* Add machine-id to non-unique titles */
for (size_t i = 0; i < n_entries; i++)
if (arr[i] && entries[i].machine_id) {
if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id) < 0)
return -ENOMEM;
free_and_replace(entries[i].show_title, s);
}
if (!find_nonunique(entries, n_entries, arr))
return 0;
/* Add file name to non-unique titles */
for (size_t i = 0; i < n_entries; i++)
if (arr[i]) {
if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id) < 0)
return -ENOMEM;
free_and_replace(entries[i].show_title, s);
}
return 0;
}
static int boot_config_find(const BootConfig *config, const char *id) {
assert(config);
if (!id)
return -1;
if (id[0] == '@') {
if (!strcaseeq(id, "@saved"))
return -1;
if (!config->entry_selected)
return -1;
id = config->entry_selected;
}
for (size_t i = 0; i < config->n_entries; i++)
if (fnmatch(id, config->entries[i].id, FNM_CASEFOLD) == 0)
return i;
return -1;
}
static int boot_entries_select_default(const BootConfig *config) {
int i;
assert(config);
assert(config->entries || config->n_entries == 0);
if (config->n_entries == 0) {
log_debug("Found no default boot entry :(");
return -1; /* -1 means "no default" */
}
if (config->entry_oneshot) {
i = boot_config_find(config, config->entry_oneshot);
if (i >= 0) {
log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
config->entries[i].id);
return i;
}
}
if (config->entry_preferred) {
i = boot_config_find(config, config->entry_preferred);
if (i >= 0) {
log_debug("Found default: id \"%s\" is matched by LoaderEntryPreferred",
config->entries[i].id);
return i;
}
}
if (config->entry_default) {
i = boot_config_find(config, config->entry_default);
if (i >= 0) {
log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
config->entries[i].id);
return i;
}
}
if (config->preferred_pattern) {
i = boot_config_find(config, config->preferred_pattern);
if (i >= 0) {
log_debug("Found preferred: id \"%s\" is matched by pattern \"%s\"",
config->entries[i].id, config->preferred_pattern);
return i;
}
}
if (config->default_pattern) {
i = boot_config_find(config, config->default_pattern);
if (i >= 0) {
log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
config->entries[i].id, config->default_pattern);
return i;
}
}
log_debug("Found default: first entry \"%s\"", config->entries[0].id);
return 0;
}
static int boot_entries_select_selected(const BootConfig *config) {
assert(config);
assert(config->entries || config->n_entries == 0);
if (!config->entry_selected || config->n_entries == 0)
return -1;
return boot_config_find(config, config->entry_selected);
}
static int boot_load_efi_entry_pointers(BootConfig *config, bool skip_efivars) {
int r;
assert(config);
if (skip_efivars || !is_efi_boot())
return 0;
/* Loads the three "pointers" to boot loader entries from their EFI variables */
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), &config->entry_oneshot);
if (r == -ENOMEM)
return log_oom();
if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\", ignoring: %m");
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"), &config->entry_preferred);
if (r == -ENOMEM)
return log_oom();
if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryPreferred\", ignoring: %m");
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"), &config->entry_default);
if (r == -ENOMEM)
return log_oom();
if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\", ignoring: %m");
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntrySelected"), &config->entry_selected);
if (r == -ENOMEM)
return log_oom();
if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySelected\", ignoring: %m");
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"), &config->entry_sysfail);
if (r == -ENOMEM)
return log_oom();
if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySysFail\", ignoring: %m");
return 1;
}
int boot_config_select_special_entries(BootConfig *config, bool skip_efivars) {
int r;
assert(config);
r = boot_load_efi_entry_pointers(config, skip_efivars);
if (r < 0)
return r;
config->default_entry = boot_entries_select_default(config);
config->selected_entry = boot_entries_select_selected(config);
return 0;
}
int boot_config_finalize(BootConfig *config) {
int r;
typesafe_qsort(config->entries, config->n_entries, boot_entry_compare);
r = boot_entries_uniquify(config->entries, config->n_entries);
if (r < 0)
return log_error_errno(r, "Failed to uniquify boot entries: %m");
return 0;
}
static int boot_entries_load(
BootConfig *config,
BootEntrySource source,
const char *where) { /* Mount point of ESP/XBOOTLDR */
int r;
assert(config);
assert(source >= 0);
assert(source < _BOOT_ENTRY_SOURCE_MAX);
if (!where)
return 0;
r = boot_entries_find_type1(config, where, source, "/loader/entries");
if (r < 0)
return r;
r = boot_entries_find_unified(config, where, source, "/EFI/Linux/");
if (r < 0)
return r;
static const struct {
BootEntryExtraType extra_type;
const char *directory;
} table[] = {
{ BOOT_ENTRY_ADDON, "/loader/addons/" },
{ BOOT_ENTRY_CONFEXT, "/loader/extensions/" },
{ BOOT_ENTRY_SYSEXT, "/loader/extensions/" },
{ BOOT_ENTRY_CREDENTIAL, "/loader/credentials/" },
};
FOREACH_ELEMENT(i, table) {
r = boot_entries_find_unified_global_extras(
config,
where,
i->directory,
i->extra_type,
&config->global_extras[source]);
if (r < 0)
return r;
}
return 0;
}
int boot_config_load(
BootConfig *config,
const char *esp_path,
const char *xbootldr_path) {
int r;
assert(config);
if (esp_path) {
r = boot_loader_read_conf_path(config, esp_path, "/loader/loader.conf");
if (r < 0)
return r;
r = boot_entries_load(config, BOOT_ENTRY_ESP, esp_path);
if (r < 0)
return r;
}
if (xbootldr_path) {
r = boot_entries_load(config, BOOT_ENTRY_XBOOTLDR, xbootldr_path);
if (r < 0)
return r;
}
return boot_config_finalize(config);
}
int boot_config_load_auto(
BootConfig *config,
const char *override_esp_path,
const char *override_xbootldr_path) {
_cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL;
dev_t esp_devid = 0, xbootldr_devid = 0;
int r;
assert(config);
/* This function is similar to boot_entries_load_config(), however we automatically search for the
* ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
* an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
* read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
* it). This allows other boot loaders to pass boot loader entry information to our tools if they
* want to. */
if (!override_esp_path && !override_xbootldr_path) {
if (access("/run/boot-loader-entries/", F_OK) >= 0)
return boot_config_load(config, "/run/boot-loader-entries/", NULL);
if (errno != ENOENT)
return log_error_errno(errno,
"Failed to determine whether /run/boot-loader-entries/ exists: %m");
}
r = find_esp_and_warn_full(
/* root= */ NULL,
override_esp_path,
/* unprivileged_mode= */ false,
&esp_where,
/* ret_fd= */ NULL,
/* ret_part= */ NULL,
/* ret_pstart= */ NULL,
/* ret_psize= */ NULL,
/* ret_uuid= */ NULL,
&esp_devid);
if (r < 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */
return r;
r = find_xbootldr_and_warn_full(
/* root= */ NULL,
override_xbootldr_path,
/* unprivileged_mode= */ false,
&xbootldr_where,
/* ret_fd= */ NULL,
/* ret_uuid= */ NULL,
&xbootldr_devid);
if (r < 0 && r != -ENOKEY)
return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
/* If both paths actually refer to the same inode, suppress the xbootldr path */
if (esp_where && xbootldr_where && devnum_set_and_equal(esp_devid, xbootldr_devid))
xbootldr_where = mfree(xbootldr_where);
return boot_config_load(config, esp_where, xbootldr_where);
}
int boot_config_augment_from_loader(
BootConfig *config,
char **found_by_loader,
bool auto_only) {
static const BootEntryExtras no_extras = (BootEntryExtras) {};
static const char *const title_table[] = {
/* Pretty names for a few well-known automatically discovered entries. */
"auto-osx", "macOS",
"auto-windows", "Windows Boot Manager",
"auto-efi-shell", "EFI Shell",
"auto-efi-default", "EFI Default Loader",
"auto-poweroff", "Power Off The System",
"auto-reboot", "Reboot The System",
"auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
NULL,
};
assert(config);
/* Let's add the entries discovered by the boot loader to the end of our list, unless they are
* already included there. */
STRV_FOREACH(i, found_by_loader) {
BootEntry *existing;
_cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
existing = boot_config_find_entry(config, *i);
if (existing) {
existing->reported_by_loader = true;
continue;
}
if (auto_only && !startswith(*i, "auto-"))
continue;
c = strdup(*i);
if (!c)
return log_oom();
STRV_FOREACH_PAIR(a, b, title_table)
if (streq(*a, *i)) {
t = strdup(*b);
if (!t)
return log_oom();
break;
}
p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderEntries")));
if (!p)
return log_oom();
if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
return log_oom();
config->entries[config->n_entries++] = (BootEntry) {
.type = startswith(*i, "auto-") ? BOOT_ENTRY_AUTO : BOOT_ENTRY_LOADER,
.id = TAKE_PTR(c),
.title = TAKE_PTR(t),
.path = TAKE_PTR(p),
.reported_by_loader = true,
.tries_left = UINT_MAX,
.tries_done = UINT_MAX,
.profile = UINT_MAX,
.global_extras = &no_extras,
};
}
return 0;
}
BootEntry* boot_config_find_entry(BootConfig *config, const char *id) {
assert(config);
assert(id);
for (size_t j = 0; j < config->n_entries; j++)
if (strcaseeq_ptr(config->entries[j].id, id) ||
strcaseeq_ptr(config->entries[j].id_old, id))
return config->entries + j;
return NULL;
}
static void boot_entry_file_list(
const char *field,
const char *root,
const char *p,
int *pstatus) {
assert(p);
assert(pstatus);
int status = chase_and_access(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, NULL);
/* Note that this shows two '/' between the root and the file. This is intentional to highlight (in
* the absence of color support) to the user that the boot loader is only interested in the second
* part of the file. */
printf("%13s%s %s%s/%s", strempty(field), field ? ":" : " ", ansi_grey(), root, ansi_normal());
if (status < 0) {
errno = -status;
printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal());
} else
printf("%s\n", p);
if (*pstatus == 0 && status < 0)
*pstatus = status;
}
static void print_extra(
const BootEntry *e,
const BootEntryExtra *extra,
const char *field,
int *status) {
assert(e);
assert(extra);
boot_entry_file_list(field, e->root, extra->location, status);
if (extra->cmdline)
printf(" options: %s%s\n", glyph(GLYPH_TREE_RIGHT), extra->cmdline);
}
static int indent_embedded_newlines(char *cmdline, char **ret_cmdline) {
_cleanup_free_ char *t = NULL;
_cleanup_strv_free_ char **ts = NULL;
assert(ret_cmdline);
ts = strv_split_newlines(cmdline);
if (!ts)
return -ENOMEM;
t = strv_join(ts, "\n ");
if (!t)
return -ENOMEM;
*ret_cmdline = TAKE_PTR(t);
return 0;
}
static int print_cmdline(const BootEntry *e, int *status) {
_cleanup_free_ char *options = NULL, *combined_cmdline = NULL, *t2 = NULL;
assert(e);
if (!strv_isempty(e->options)) {
_cleanup_free_ char *t = NULL;
options = strv_join(e->options, " ");
if (!options)
return log_oom();