HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/bootspec.c

83,855 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 4615a8755ea1a36c83f44848960af185f13bd0ae

Lines 1–1000 of 2,354

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fnmatch.h>
#include <unistd.h>
 
#include "sd-json.h"
 
#include "alloc-util.h"
#include "bootspec.h"
#include "chase.h"
#include "devnum-util.h"
#include "dirent-util.h"
#include "efi-loader.h"
#include "efivars.h"
#include "env-file.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "find-esp.h"
#include "json-util.h"
#include "log.h"
#include "parse-util.h"
#include "path-util.h"
#include "pe-binary.h"
#include "pretty-print.h"
#include "recurse-dir.h"
#include "set.h"
#include "sort-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "uki.h"
#include "utf8.h"
 
static const char* const boot_entry_type_description_table[_BOOT_ENTRY_TYPE_MAX] = {
        [BOOT_ENTRY_TYPE1]  = "Boot Loader Specification Type #1 (.conf)",
        [BOOT_ENTRY_TYPE2]  = "Boot Loader Specification Type #2 (UKI, .efi)",
        [BOOT_ENTRY_LOADER] = "Reported by Boot Loader",
        [BOOT_ENTRY_AUTO]   = "Automatic",
};
 
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type_description, BootEntryType);
 
static const char* const boot_entry_type_table[_BOOT_ENTRY_TYPE_MAX] = {
        [BOOT_ENTRY_TYPE1]  = "type1",
        [BOOT_ENTRY_TYPE2]  = "type2",
        [BOOT_ENTRY_LOADER] = "loader",
        [BOOT_ENTRY_AUTO]   = "auto",
};
 
DEFINE_STRING_TABLE_LOOKUP(boot_entry_type, BootEntryType);
 
static const char* const boot_entry_source_description_table[_BOOT_ENTRY_SOURCE_MAX] = {
        [BOOT_ENTRY_ESP]      = "EFI System Partition",
        [BOOT_ENTRY_XBOOTLDR] = "Extended Boot Loader Partition",
};
 
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source_description, BootEntrySource);
 
static const char* const boot_entry_source_table[_BOOT_ENTRY_SOURCE_MAX] = {
        [BOOT_ENTRY_ESP]      = "esp",
        [BOOT_ENTRY_XBOOTLDR] = "xbootldr",
};
 
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source, BootEntrySource);
 
static BootEntryExtraType boot_entry_extra_type_from_filename(const char *path) {
        if (!path)
                return _BOOT_ENTRY_EXTRA_TYPE_INVALID;
 
        if (endswith_no_case(path, ".addon.efi"))
                return BOOT_ENTRY_ADDON;
        if (endswith_no_case(path, ".confext.raw"))
                return BOOT_ENTRY_CONFEXT;
        if (endswith_no_case(path, ".sysext.raw"))
                return BOOT_ENTRY_SYSEXT;
        if (endswith_no_case(path, ".cred"))
                return BOOT_ENTRY_CREDENTIAL;
 
        return _BOOT_ENTRY_EXTRA_TYPE_INVALID;
}
 
static void boot_entry_extras_done(BootEntryExtras *extras) {
        assert(extras);
 
        FOREACH_ARRAY(extra, extras->items, extras->n_items) {
                free(extra->location);
                free(extra->cmdline);
        }
        extras->items = mfree(extras->items);
        extras->n_items = 0;
}
 
static int boot_entry_extras_add(
                BootEntryExtras *extras,
                BootEntryExtraType type,
                const char *path,
                const char *cmdline) {
 
        assert(extras);
        assert(type >= 0);
        assert(type < _BOOT_ENTRY_EXTRA_TYPE_MAX);
        assert(path);
 
        _cleanup_free_ char *p = strdup(path);
        if (!p)
                return -ENOMEM;
 
        _cleanup_free_ char *c = NULL;
        if (cmdline) {
                c = strdup(cmdline);
                if (!c)
                        return -ENOMEM;
        }
 
        if (!GREEDY_REALLOC(extras->items, extras->n_items + 1))
                return -ENOMEM;
 
        extras->items[extras->n_items++] = (BootEntryExtra) {
                .type = type,
                .location = TAKE_PTR(p),
                .cmdline = TAKE_PTR(c),
        };
 
        return 0;
}
 
static void boot_entry_free(BootEntry *entry) {
        assert(entry);
 
        free(entry->id);
        free(entry->id_old);
        free(entry->id_without_profile);
        free(entry->path);
        free(entry->root);
        free(entry->title);
        free(entry->show_title);
        free(entry->sort_key);
        free(entry->version);
        free(entry->uname);
        free(entry->machine_id);
        free(entry->architecture);
        strv_free(entry->options);
        boot_entry_extras_done(&entry->local_extras);
        free(entry->kernel);
        free(entry->efi);
        free(entry->uki);
        free(entry->uki_url);
        strv_free(entry->initrd);
        free(entry->device_tree);
        strv_free(entry->device_tree_overlay);
}
 
static int mangle_path(
                const char *fname,
                unsigned line,
                const char *field,
                const char *p,
                char **ret) {
 
        _cleanup_free_ char *c = NULL;
 
        assert(field);
        assert(p);
        assert(ret);
 
        /* Spec leaves open if prefixed with "/" or not, let's normalize that */
        c = path_make_absolute(p, "/");
        if (!c)
                return -ENOMEM;
 
        /* We only reference files, never directories */
        if (endswith(c, "/")) {
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' has trailing slash, ignoring: %s", field, c);
                *ret = NULL;
                return 0;
        }
 
        /* Remove duplicate "/" */
        path_simplify(c);
 
        /* No ".." or "." or so */
        if (!path_is_normalized(c)) {
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' is not normalized, ignoring: %s", field, c);
                *ret = NULL;
                return 0;
        }
 
        *ret = TAKE_PTR(c);
        return 1;
}
 
static int parse_path_one(
                const char *fname,
                unsigned line,
                const char *field,
                char **s,
                const char *p) {
 
        _cleanup_free_ char *c = NULL;
        int r;
 
        assert(field);
        assert(s);
        assert(p);
 
        r = mangle_path(fname, line, field, p, &c);
        if (r <= 0)
                return r;
 
        return free_and_replace(*s, c);
}
 
static int parse_path_strv(
                const char *fname,
                unsigned line,
                const char *field,
                char ***s,
                const char *p) {
 
        char *c;
        int r;
 
        assert(field);
        assert(s);
        assert(p);
 
        r = mangle_path(fname, line, field, p, &c);
        if (r <= 0)
                return r;
 
        return strv_consume(s, c);
}
 
static int parse_path_many(
                const char *fname,
                unsigned line,
                const char *field,
                char ***s,
                const char *p) {
 
        _cleanup_strv_free_ char **l = NULL, **f = NULL;
        int r;
 
        l = strv_split(p, NULL);
        if (!l)
                return -ENOMEM;
 
        STRV_FOREACH(i, l) {
                char *c;
 
                r = mangle_path(fname, line, field, *i, &c);
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                r = strv_consume(&f, c);
                if (r < 0)
                        return r;
        }
 
        return strv_extend_strv_consume(s, TAKE_PTR(f), /* filter_duplicates= */ false);
}
 
static int parse_extra(
                const char *fname,
                unsigned line,
                const char *field,
                BootEntryExtras *extras,
                const char *p) {
 
        int r;
 
        assert(extras);
 
        _cleanup_strv_free_ char **l = strv_split(p, NULL);
        if (!l)
                return -ENOMEM;
 
        STRV_FOREACH(i, l) {
                _cleanup_free_ char *c = NULL;
                r = mangle_path(fname, line, field, *i, &c);
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                BootEntryExtraType type = boot_entry_extra_type_from_filename(c);
                if (type < 0) {
                        log_debug_errno(type, "Failed to determine boot entry extra type of '%s', skipping: %m", c);
                        continue;
                }
 
                /* Let's filter out EFI addons for now. We have no protocol for passing them from sd-boot to
                 * sd-stub, hence supporting them would require major plumbing first. */
                if (type == BOOT_ENTRY_ADDON) {
                        log_debug("EFI addons are currently not supported for Type #1 entries, skipping '%s'.", c);
                        continue;
                }
 
                r = boot_entry_extras_add(extras, type, c, /* cmdline= */ NULL);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int parse_tries(const char *fname, const char **p, unsigned *ret) {
        _cleanup_free_ char *d = NULL;
        unsigned tries;
        size_t n;
        int r;
 
        assert(fname);
        assert(p);
        assert(*p);
        assert(ret);
 
        n = strspn(*p, DIGITS);
        if (n == 0) {
                *ret = UINT_MAX;
                return 0;
        }
 
        d = strndup(*p, n);
        if (!d)
                return -ENOMEM;
 
        r = safe_atou_full(d, 10, &tries);
        if (r < 0)
                return r;
        if (tries > INT_MAX) /* sd-boot allows INT_MAX, let's use the same limit */
                return -ERANGE;
 
        *p = *p + n;
        *ret = tries;
        return 1;
}
 
int boot_filename_extract_tries(
                const char *fname,
                char **ret_stripped,
                unsigned *ret_tries_left,
                unsigned *ret_tries_done) {
 
        unsigned tries_left = UINT_MAX, tries_done = UINT_MAX;
        _cleanup_free_ char *stripped = NULL;
        const char *p, *suffix, *m;
        int r;
 
        assert(fname);
        assert(ret_stripped);
 
        /* Be liberal with suffix, only insist on a dot. After all we want to cover any capitalization here
         * (vfat is case insensitive after all), and at least .efi and .conf as suffix. */
        suffix = strrchr(fname, '.');
        if (!suffix)
                goto nothing;
 
        p = m = memrchr(fname, '+', suffix - fname);
        if (!p)
                goto nothing;
        p++;
 
        r = parse_tries(fname, &p, &tries_left);
        if (r < 0)
                return r;
        if (r == 0)
                goto nothing;
 
        if (*p == '-') {
                p++;
 
                r = parse_tries(fname, &p, &tries_done);
                if (r < 0)
                        return r;
                if (r == 0)
                        goto nothing;
        }
 
        if (p != suffix)
                goto nothing;
 
        stripped = strndup(fname, m - fname);
        if (!stripped)
                return -ENOMEM;
 
        if (!strextend(&stripped, suffix))
                return -ENOMEM;
 
        *ret_stripped = TAKE_PTR(stripped);
        if (ret_tries_left)
                *ret_tries_left = tries_left;
        if (ret_tries_done)
                *ret_tries_done = tries_done;
 
        return 0;
 
nothing:
        stripped = strdup(fname);
        if (!stripped)
                return -ENOMEM;
 
        *ret_stripped = TAKE_PTR(stripped);
        if (ret_tries_left)
                *ret_tries_left = UINT_MAX;
        if (ret_tries_done)
                *ret_tries_done = UINT_MAX;
        return 0;
}
 
static int boot_entry_load_type1(
                FILE *f,
                const char *root,
                const BootEntrySource source,
                const char *dir,
                const char *fname,
                BootEntry *ret) {
 
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_TYPE1, source);
        char *c;
        int r;
 
        assert(f);
        assert(root);
        assert(dir);
        assert(fname);
        assert(ret);
 
        /* Loads a Type #1 boot menu entry from the specified FILE* object */
 
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
        if (r < 0)
                return log_error_errno(r, "Failed to extract tries counters from '%s': %m", fname);
 
        if (!efi_loader_entry_name_valid(tmp.id))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", fname);
 
        c = endswith_no_case(tmp.id, ".conf");
        if (!c)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", fname);
 
        tmp.id_old = strndup(tmp.id, c - tmp.id); /* Without .conf suffix */
        if (!tmp.id_old)
                return log_oom();
 
        tmp.path = path_join(dir, fname);
        if (!tmp.path)
                return log_oom();
 
        tmp.root = strdup(root);
        if (!tmp.root)
                return log_oom();
 
        for (unsigned line = 1;; line++) {
                _cleanup_free_ char *buf = NULL, *field = NULL;
 
                r = read_stripped_line(f, LONG_LINE_MAX, &buf);
                if (r == -ENOBUFS)
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long.");
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m");
                if (r == 0)
                        break;
 
                if (IN_SET(buf[0], '#', '\0'))
                        continue;
 
                const char *p = buf;
                r = extract_first_word(&p, &field, NULL, 0);
                if (r < 0) {
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m");
                        continue;
                }
                if (r == 0) {
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Bad syntax, ignoring line.");
                        continue;
                }
 
                if (isempty(p)) {
                        /* Some fields can reasonably have an empty value. In other cases warn. */
                        if (!STR_IN_SET(field, "options", "devicetree-overlay"))
                                log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Field '%s' without value, ignoring line.", field);
 
                        continue;
                }
 
                if (streq(field, "title"))
                        r = free_and_strdup(&tmp.title, p);
                else if (streq(field, "sort-key"))
                        r = free_and_strdup(&tmp.sort_key, p);
                else if (streq(field, "version")) {
                        if (!version_is_valid(p, VERSION_ALLOW_UNDERSCORE|VERSION_ALLOW_PLUS))
                                log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Version string '%s' is not a valid version, accepting anyway.", p);
 
                        r = free_and_strdup(&tmp.version, p);
                } else if (streq(field, "machine-id"))
                        r = free_and_strdup(&tmp.machine_id, p);
                else if (streq(field, "architecture"))
                        r = free_and_strdup(&tmp.architecture, p);
                else if (streq(field, "options"))
                        r = strv_extend(&tmp.options, p);
                else if (streq(field, "linux"))
                        r = parse_path_one(tmp.path, line, field, &tmp.kernel, p);
                else if (streq(field, "efi"))
                        r = parse_path_one(tmp.path, line, field, &tmp.efi, p);
                else if (streq(field, "uki"))
                        r = parse_path_one(tmp.path, line, field, &tmp.uki, p);
                else if (streq(field, "uki-url"))
                        r = free_and_strdup(&tmp.uki_url, p);
                else if (streq(field, "profile"))
                        r = safe_atou_full(p, 10, &tmp.profile);
                else if (streq(field, "initrd"))
                        r = parse_path_strv(tmp.path, line, field, &tmp.initrd, p);
                else if (streq(field, "devicetree"))
                        r = parse_path_one(tmp.path, line, field, &tmp.device_tree, p);
                else if (streq(field, "devicetree-overlay"))
                        r = parse_path_many(tmp.path, line, field, &tmp.device_tree_overlay, p);
                else if (streq(field, "extra"))
                        r = parse_extra(tmp.path, line, field, &tmp.local_extras, p);
                else {
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Unknown line '%s', ignoring.", field);
                        continue;
                }
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while parsing: %m");
        }
 
        *ret = TAKE_STRUCT(tmp);
        return 0;
}
 
int boot_config_load_type1(
                BootConfig *config,
                FILE *f,
                const char *root,
                const BootEntrySource source,
                const char *dir,
                const char *filename) {
        int r;
 
        assert(config);
        assert(f);
        assert(root);
        assert(dir);
        assert(filename);
 
        if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
                return log_oom();
 
        BootEntry *entry = config->entries + config->n_entries;
 
        r = boot_entry_load_type1(f, root, source, dir, filename, entry);
        if (r < 0)
                return r;
        config->n_entries++;
 
        entry->global_extras = &config->global_extras[source];
 
        return 0;
}
 
void boot_config_free(BootConfig *config) {
        assert(config);
 
        free(config->preferred_pattern);
        free(config->default_pattern);
 
        free(config->entry_oneshot);
        free(config->entry_preferred);
        free(config->entry_default);
        free(config->entry_selected);
        free(config->entry_sysfail);
 
        FOREACH_ARRAY(i, config->entries, config->n_entries)
                boot_entry_free(i);
        free(config->entries);
 
        FOREACH_ELEMENT(i, config->global_extras)
                boot_entry_extras_done(i);
 
        set_free(config->inodes_seen);
}
 
int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
        int r;
 
        assert(config);
        assert(file);
        assert(path);
 
        for (unsigned line = 1;; line++) {
                _cleanup_free_ char *buf = NULL, *field = NULL;
 
                r = read_stripped_line(file, LONG_LINE_MAX, &buf);
                if (r == -ENOBUFS)
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long.");
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m");
                if (r == 0)
                        break;
 
                if (IN_SET(buf[0], '#', '\0'))
                        continue;
 
                const char *p = buf;
                r = extract_first_word(&p, &field, NULL, 0);
                if (r < 0) {
                        log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m");
                        continue;
                }
                if (r == 0) {
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Bad syntax, ignoring line.");
                        continue;
                }
                if (isempty(p)) {
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Field '%s' without value, ignoring line.", field);
                        continue;
                }
 
                if (streq(field, "preferred"))
                        r = free_and_strdup(&config->preferred_pattern, p);
                else if (streq(field, "default"))
                        r = free_and_strdup(&config->default_pattern, p);
                else if (STR_IN_SET(field, "timeout", "editor", "auto-entries", "auto-firmware",
                                    "auto-poweroff", "auto-reboot", "beep", "reboot-for-bitlocker",
                                    "reboot-on-error", "secure-boot-enroll", "secure-boot-enroll-action",
                                    "secure-boot-enroll-timeout-sec", "console-mode", "log-level"))
                        r = 0; /* we don't parse these in userspace, but they are OK */
                else {
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Unknown line '%s', ignoring.", field);
                        continue;
                }
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while parsing: %m");
        }
 
        return 1;
}
 
static int boot_loader_read_conf_path(BootConfig *config, const char *root, const char *path) {
        _cleanup_free_ char *full = NULL;
        _cleanup_fclose_ FILE *f = NULL;
        int r;
 
        assert(config);
        assert(path);
 
        r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, "re", &full, &f);
        config->loader_conf_status = r < 0 ? r : true;
        if (r == -ENOENT)
                return 0;
        if (r < 0)
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(path));
 
        return boot_loader_read_conf(config, f, full);
}
 
static unsigned boot_entry_profile(const BootEntry *a) {
        assert(a);
 
        return a->profile == UINT_MAX ? 0 : a->profile;
}
 
static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
        int r;
 
        assert(a);
        assert(b);
 
        /* This mimics a function of the same name in src/boot/efi/sd-boot.c */
 
        r = CMP(a->tries_left == 0, b->tries_left == 0);
        if (r != 0)
                return r;
 
        r = CMP(!a->sort_key, !b->sort_key);
        if (r != 0)
                return r;
 
        if (a->sort_key && b->sort_key) {
                r = strcmp(a->sort_key, b->sort_key);
                if (r != 0)
                        return r;
 
                r = strcmp_ptr(a->machine_id, b->machine_id);
                if (r != 0)
                        return r;
 
                r = -strverscmp_improved(a->version, b->version);
                if (r != 0)
                        return r;
 
                r = CMP(boot_entry_profile(a), boot_entry_profile(b));
                if (r != 0)
                        return r;
        }
 
        r = -strverscmp_improved(a->id_without_profile ?: a->id, b->id_without_profile ?: b->id);
        if (r != 0)
                return r;
 
        if (a->id_without_profile && b->id_without_profile) {
                /* The strverscmp_improved() call above already established that we are talking about the
                 * same image here, hence order by profile, if there is one */
                r = CMP(boot_entry_profile(a), boot_entry_profile(b));
                if (r != 0)
                        return r;
        }
 
        if (a->tries_left != UINT_MAX || b->tries_left != UINT_MAX)
                return 0;
 
        r = -CMP(a->tries_left, b->tries_left);
        if (r != 0)
                return r;
 
        return CMP(a->tries_done, b->tries_done);
}
 
static int config_check_inode_relevant_and_unseen(BootConfig *config, int fd, const char *fname) {
        _cleanup_free_ char *d = NULL;
        struct stat st;
 
        assert(config);
        assert(fd >= 0);
        assert(fname);
 
        /* So, here's the thing: because of the mess around /efi/ vs. /boot/ vs. /boot/efi/ it might be that
         * people have these dirs, or subdirs of them symlinked or bind mounted, and we might end up
         * iterating though some dirs multiple times. Let's thus rather be safe than sorry, and track the
         * inodes we already processed: let's ignore inodes we have seen already. This should be robust
         * against any form of symlinking or bind mounting, and effectively suppress any such duplicates. */
 
        if (fstat(fd, &st) < 0)
                return log_error_errno(errno, "Failed to stat('%s'): %m", fname);
        if (!S_ISREG(st.st_mode)) {
                log_debug("File '%s' is not a regular file, ignoring.", fname);
                return false;
        }
 
        if (set_contains(config->inodes_seen, &st)) {
                log_debug("Inode '%s' already seen before, ignoring.", fname);
                return false;
        }
 
        d = memdup(&st, sizeof(st));
        if (!d)
                return log_oom();
 
        if (set_ensure_consume(&config->inodes_seen, &inode_hash_ops, TAKE_PTR(d)) < 0)
                return log_oom();
 
        return true;
}
 
static int boot_entries_find_type1(
                BootConfig *config,
                const char *root,
                const BootEntrySource source,
                const char *dir) {
 
        _cleanup_free_ DirectoryEntries *dentries = NULL;
        _cleanup_free_ char *full = NULL;
        _cleanup_close_ int dir_fd = -EBADF;
        int r;
 
        assert(config);
        assert(root);
        assert(dir);
 
        dir_fd = chase_and_open(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, O_DIRECTORY|O_CLOEXEC, &full);
        if (dir_fd == -ENOENT)
                return 0;
        if (dir_fd < 0)
                return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, skip_leading_slash(dir));
 
        r = readdir_all(dir_fd, RECURSE_DIR_IGNORE_DOT, &dentries);
        if (r < 0)
                return log_error_errno(r, "Failed to read directory '%s': %m", full);
 
        FOREACH_ARRAY(i, dentries->entries, dentries->n_entries) {
                const struct dirent *de = *i;
                _cleanup_fclose_ FILE *f = NULL;
 
                if (!dirent_is_file(de))
                        continue;
 
                if (!endswith_no_case(de->d_name, ".conf"))
                        continue;
 
                r = xfopenat(dir_fd, de->d_name, "re", O_NOFOLLOW|O_NOCTTY, &f);
                if (r < 0) {
                        log_warning_errno(r, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
                        continue;
                }
 
                r = config_check_inode_relevant_and_unseen(config, fileno(f), de->d_name);
                if (r < 0)
                        return r;
                if (r == 0) /* inode already seen or otherwise not relevant */
                        continue;
 
                r = boot_config_load_type1(config, f, root, source, full, de->d_name);
                if (r == -ENOMEM) /* ignore all other errors */
                        return log_oom();
        }
 
        return 0;
}
 
static void mangle_osrelease_string(char **s, const char *field) {
        assert(s);
        assert(field);
 
        if (!isempty(*s) && !string_has_cc(*s, /* ok= */ NULL) && utf8_is_valid(*s))
                return;
 
        if (*s) {
                log_debug("OS release field '%s' is not clean, suppressing.", field);
                *s = mfree(*s);
        }
}
 
int bootspec_extract_osrelease(
                const char *text,
                char **ret_good_name,
                char **ret_good_version,
                char **ret_good_sort_key,
                char **ret_os_id,
                char **ret_os_version_id,
                char **ret_image_id,
                char **ret_image_version) {
 
        int r;
 
        assert(text);
 
        _cleanup_free_ char *os_pretty_name = NULL, *image_id = NULL, *os_name = NULL, *os_id = NULL,
                *image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
        r = parse_env_data(text, /* size= */ SIZE_MAX,
                           "os-release",
                           "PRETTY_NAME", &os_pretty_name,
                           "IMAGE_ID", &image_id,
                           "NAME", &os_name,
                           "ID", &os_id,
                           "IMAGE_VERSION", &image_version,
                           "VERSION", &os_version,
                           "VERSION_ID", &os_version_id,
                           "BUILD_ID", &os_build_id);
        if (r < 0)
                return r;
 
        mangle_osrelease_string(&os_pretty_name, "PRETTY_NAME");
        mangle_osrelease_string(&image_id, "IMAGE_ID");
        mangle_osrelease_string(&os_name, "NAME");
        mangle_osrelease_string(&os_id, "ID");
        mangle_osrelease_string(&image_version, "IMAGE_VERSION");
        mangle_osrelease_string(&os_version, "VERSION");
        mangle_osrelease_string(&os_version_id, "VERSION_ID");
        mangle_osrelease_string(&os_build_id, "BUILD_ID");
 
        const char *good_name, *good_version, *good_sort_key;
        if (!bootspec_pick_name_version_sort_key(
                            os_pretty_name,
                            image_id,
                            os_name,
                            os_id,
                            image_version,
                            os_version,
                            os_version_id,
                            os_build_id,
                            &good_name,
                            &good_version,
                            &good_sort_key))
                return -EBADMSG;
 
        _cleanup_free_ char *copy_good_name = NULL, *copy_good_version = NULL, *copy_good_sort_key = NULL;
        if (ret_good_name) {
                copy_good_name = strdup(good_name);
                if (!copy_good_name)
                        return -ENOMEM;
        }
 
        if (ret_good_version && good_version) {
                copy_good_version = strdup(good_version);
                if (!copy_good_version)
                        return -ENOMEM;
        }
 
        if (ret_good_sort_key && good_sort_key) {
                copy_good_sort_key = strdup(good_sort_key);
                if (!copy_good_sort_key)
                        return -ENOMEM;
        }
 
        if (ret_good_name)
                *ret_good_name = TAKE_PTR(copy_good_name);
        if (ret_good_version)
                *ret_good_version = TAKE_PTR(copy_good_version);
        if (ret_good_sort_key)
                *ret_good_sort_key = TAKE_PTR(copy_good_sort_key);
 
        if (ret_os_id)
                *ret_os_id = TAKE_PTR(os_id);
        if (ret_os_version_id)
                *ret_os_version_id = TAKE_PTR(os_version_id);
        if (ret_image_id)
                *ret_image_id = TAKE_PTR(image_id);
        if (ret_image_version)
                *ret_image_version = TAKE_PTR(image_version);
 
        return 0;
}
 
static int boot_entry_load_unified(
                const char *root,
                const BootEntrySource source,
                const char *path,
                unsigned profile,
                const char *osrelease_text,
                const char *profile_text,
                const char *cmdline_text,
                const char *uname_text,
                BootEntry *ret) {
 
        int r;
 
        assert(root);
        assert(path);
        assert(osrelease_text);
        assert(ret);
 
        const char *k = path_startswith(path, root);
        if (!k)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
 
        _cleanup_free_ char *good_name = NULL, *good_version = NULL, *good_sort_key = NULL, *os_id = NULL, *os_version_id = NULL;
        r = bootspec_extract_osrelease(
                        osrelease_text,
                        &good_name,
                        &good_version,
                        &good_sort_key,
                        &os_id,
                        &os_version_id,
                        /* ret_image_id= */ NULL,
                        /* ret_image_version= */ NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to extract name/version/sort-key from os-release data from unified kernel image %s, refusing: %m", path);
 
        _cleanup_free_ char *profile_id = NULL, *profile_title = NULL;
        if (profile_text) {
                r = parse_env_data(
                                profile_text, /* size= */ SIZE_MAX,
                                ".profile",
                                "ID", &profile_id,
                                "TITLE", &profile_title);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse profile data from unified kernel image '%s': %m", path);
        }
 
        _cleanup_free_ char *fname = NULL;
        r = path_extract_filename(path, &fname);
        if (r < 0)
                return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
 
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_TYPE2, source);
 
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
        if (r < 0)
                return log_error_errno(r, "Failed to extract tries counters from '%s': %m", fname);
 
        if (!efi_loader_entry_name_valid(tmp.id))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id);
 
        tmp.profile = profile;
 
        if (profile_id || profile > 0) {
                tmp.id_without_profile = TAKE_PTR(tmp.id);
 
                if (profile_id)
                        tmp.id = strjoin(tmp.id_without_profile, "@", profile_id);
                else
                        (void) asprintf(&tmp.id, "%s@%u", tmp.id_without_profile, profile);
                if (!tmp.id)
                        return log_oom();
        }
 
        if (os_id && os_version_id) {
                tmp.id_old = strjoin(os_id, "-", os_version_id);
                if (!tmp.id_old)
                        return log_oom();
        }
 
        tmp.path = strdup(path);
        if (!tmp.path)