HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/bootctl/bootctl-install.c
Git blob: f04157a70f5e2faf2d187583688156373f6e2a3e
Lines 1–1000 of 2,206
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdlib.h>
#include <unistd.h>
#include "sd-device.h"
#include "sd-varlink.h"
#include "alloc-util.h"
#include "ask-password-api.h"
#include "blockdev-util.h"
#include "boot-entry.h"
#include "bootctl.h"
#include "bootctl-install.h"
#include "bootctl-random-seed.h"
#include "bootctl-util.h"
#include "chase.h"
#include "copy.h"
#include "crypto-util.h"
#include "dirent-util.h"
#include "efi-api.h"
#include "efi.h"
#include "efivars.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "find-esp.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "id128-util.h"
#include "install-file.h"
#include "io-util.h"
#include "json-util.h"
#include "kernel-config.h"
#include "log.h"
#include "parse-argument.h"
#include "path-util.h"
#include "pe-binary.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "umask-util.h"
#include "utf8.h"
#include "varlink-util.h"
typedef enum InstallOperation {
INSTALL_NEW,
INSTALL_UPDATE,
INSTALL_REMOVE,
INSTALL_TEST,
_INSTALL_OPERATION_MAX,
_INSTALL_OPERATION_INVALID = -1,
} InstallOperation;
typedef struct InstallContext {
InstallOperation operation;
bool graceful;
char *root;
int root_fd;
sd_id128_t machine_id;
char *install_layout;
BootEntryTokenType entry_token_type;
char *entry_token;
int make_entry_directory; /* tri-state */
InstallSource install_source;
char *esp_path;
int esp_fd;
uint32_t esp_part;
uint64_t esp_pstart;
uint64_t esp_psize;
sd_id128_t esp_uuid;
char *xbootldr_path;
int xbootldr_fd;
#if HAVE_OPENSSL
X509 *secure_boot_certificate;
EVP_PKEY *secure_boot_private_key;
#endif
int touch_variables; /* tri-state */
} InstallContext;
#define INSTALL_CONTEXT_NULL \
(InstallContext) { \
.operation = _INSTALL_OPERATION_INVALID, \
.root_fd = -EBADF, \
.entry_token_type = _BOOT_ENTRY_TOKEN_TYPE_INVALID, \
.make_entry_directory = -1, \
.install_source = _INSTALL_SOURCE_INVALID, \
.esp_part = UINT32_MAX, \
.esp_pstart = UINT64_MAX, \
.esp_psize = UINT64_MAX, \
.esp_fd = -EBADF, \
.xbootldr_fd = -EBADF, \
.touch_variables = -1, \
}
static const char* install_operation_table[_INSTALL_OPERATION_MAX] = {
[INSTALL_NEW] = "new",
[INSTALL_UPDATE] = "update",
[INSTALL_REMOVE] = "remove",
[INSTALL_TEST] = "test",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_operation, InstallOperation);
static void install_context_done(InstallContext *c) {
assert(c);
c->root = mfree(c->root);
c->root_fd = safe_close(c->root_fd);
c->install_layout = mfree(c->install_layout);
c->entry_token = mfree(c->entry_token);
c->esp_path = mfree(c->esp_path);
c->esp_fd = safe_close(c->esp_fd);
c->xbootldr_path = mfree(c->xbootldr_path);
c->xbootldr_fd = safe_close(c->xbootldr_fd);
#if HAVE_OPENSSL
if (c->secure_boot_private_key) {
sym_EVP_PKEY_free(c->secure_boot_private_key);
c->secure_boot_private_key = NULL;
}
if (c->secure_boot_certificate) {
sym_X509_free(c->secure_boot_certificate);
c->secure_boot_certificate = NULL;
}
#endif
}
static int install_context_from_cmdline(
InstallContext *ret,
InstallOperation operation) {
int r;
assert(ret);
assert(operation >= 0);
assert(operation < _INSTALL_OPERATION_MAX);
_cleanup_(install_context_done) InstallContext b = INSTALL_CONTEXT_NULL;
b.operation = operation;
b.graceful = arg_graceful() == ARG_GRACEFUL_FORCE ||
(operation == INSTALL_UPDATE && arg_graceful() != ARG_GRACEFUL_NO);
b.machine_id = arg_machine_id;
b.entry_token_type = arg_entry_token_type;
b.make_entry_directory = arg_make_entry_directory;
b.install_source = arg_install_source;
if (strdup_to(&b.entry_token, arg_entry_token) < 0 ||
strdup_to(&b.install_layout, arg_install_layout) < 0)
return log_oom();
if (arg_root) {
b.root_fd = open(arg_root, O_CLOEXEC|O_DIRECTORY|O_PATH);
if (b.root_fd < 0)
return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
r = strdup_to(&b.root, arg_root);
if (r < 0)
return log_oom();
} else
b.root_fd = XAT_FDROOT;
r = acquire_esp(/* unprivileged_mode= */ false,
b.graceful,
&b.esp_fd,
&b.esp_part,
&b.esp_pstart,
&b.esp_psize,
&b.esp_uuid,
/* ret_devid= */ NULL);
/* If --graceful is specified and we can't find an ESP, handle this cleanly */
if (r < 0 && (!b.graceful || r != -ENOKEY))
return r;
if (r >= 0) { /* An ESP has been found */
assert(arg_esp_path);
if (arg_root) {
const char *e = path_startswith(arg_esp_path, arg_root);
if (!e)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ESP path '%s' not below specified root '%s', refusing.", arg_esp_path, arg_root);
r = strdup_to(&b.esp_path, e);
} else
r = strdup_to(&b.esp_path, arg_esp_path);
if (r < 0)
return log_oom();
}
r = acquire_xbootldr(
/* unprivileged_mode= */ false,
&b.xbootldr_fd,
/* ret_uuid= */ NULL,
/* ret_devid= */ NULL);
if (r < 0)
return r;
if (r > 0) { /* XBOOTLDR has been found */
assert(arg_xbootldr_path);
if (arg_root) {
const char *e = path_startswith(arg_xbootldr_path, arg_root);
if (!e)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "XBOOTLDR path '%s' not below specified root '%s', refusing.", arg_xbootldr_path, arg_root);
r = strdup_to(&b.xbootldr_path, e);
} else
r = strdup_to(&b.xbootldr_path, arg_xbootldr_path);
if (r < 0)
return log_oom();
}
b.touch_variables = arg_touch_variables;
*ret = TAKE_GENERIC(b, InstallContext, INSTALL_CONTEXT_NULL);
return !!ret->esp_path; /* return positive if we found an ESP */
}
static int acquire_dollar_boot_fd(InstallContext *c) {
assert(c);
if (c->xbootldr_fd >= 0)
return c->xbootldr_fd;
if (c->esp_fd >= 0)
return c->esp_fd;
return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot access $BOOT, as neither ESP nor XBOOTLDR have been found.");
}
static const char* dollar_boot_path(InstallContext *c) {
assert(c);
return c->xbootldr_path ?: c->esp_path;
}
static bool should_touch_install_variables(InstallContext *c) {
assert(c);
if (c->touch_variables >= 0)
return c->touch_variables;
if (!is_efi_boot()) /* NB: this internally checks if we run in a container */
return false;
return empty_or_root(c->root);
}
static int load_etc_machine_id(InstallContext *c) {
int r;
assert(c);
r = id128_get_machine_at(c->root_fd, &c->machine_id);
if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Not set or empty */
return 0;
if (r < 0)
return log_error_errno(r, "Failed to get machine-id: %m");
log_debug("Loaded machine ID %s from '%s/etc/machine-id'.", strempty(c->root), SD_ID128_TO_STRING(c->machine_id));
return 0;
}
static int load_etc_machine_info(InstallContext *c) {
/* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
* for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
* the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
* has been deprecated and is only returned for compatibility. */
_cleanup_free_ char *s = NULL, *layout = NULL;
int r;
assert(c);
_cleanup_free_ char *j = path_join(c->root, "/etc/machine-info");
if (!j)
return log_oom();
_cleanup_close_ int fd =
chase_and_openat(
c->root_fd,
c->root_fd,
"/etc/machine-info",
CHASE_MUST_BE_REGULAR,
O_RDONLY|O_CLOEXEC,
/* ret_path= */ NULL);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return log_error_errno(fd, "Failed to open '%s': %m", j);
r = parse_env_file_fd(
fd, "/etc/machine-info",
"KERNEL_INSTALL_LAYOUT", &layout,
"KERNEL_INSTALL_MACHINE_ID", &s);
if (r < 0)
return log_error_errno(r, "Failed to parse '%s': %m", j);
if (!isempty(s)) {
if (!arg_quiet)
log_notice("Read $KERNEL_INSTALL_MACHINE_ID from '%s'. "
"Please move it to '%s/etc/kernel/entry-token'.", j, strempty(c->root));
r = sd_id128_from_string(s, &c->machine_id);
if (r < 0)
return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=\"%s\" in '%s': %m", s, j);
log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=\"%s\" from '%s'.",
SD_ID128_TO_STRING(c->machine_id), j);
}
if (!isempty(layout)) {
if (!arg_quiet)
log_notice("Read $KERNEL_INSTALL_LAYOUT from '%s'. "
"Please move it to the layout= setting of '%s/etc/kernel/install.conf'.", j, strempty(c->root));
log_debug("KERNEL_INSTALL_LAYOUT=\"%s\" is specified in '%s'.", layout, j);
free_and_replace(c->install_layout, layout);
}
return 0;
}
static int load_kernel_install_layout(InstallContext *c) {
_cleanup_free_ char *layout = NULL;
int r;
assert(c);
const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
r = load_kernel_install_conf_at(
e ? NULL : c->root,
e ? XAT_FDROOT : c->root_fd,
e,
/* ret_machine_id= */ NULL,
/* ret_boot_root= */ NULL,
&layout,
/* ret_initrd_generator= */ NULL,
/* ret_uki_generator= */ NULL,
/* ret_entry_name_format= */ NULL);
if (r <= 0)
return r;
if (!isempty(layout)) {
log_debug("layout=\"%s\" is specified in config.", layout);
free_and_replace(c->install_layout, layout);
}
return 0;
}
static bool use_boot_loader_spec_type1(InstallContext *c) {
assert(c);
/* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
* Specification Type #1 is the chosen format for our boot loader entries */
return !c->install_layout || streq(c->install_layout, "bls");
}
static int settle_make_entry_directory(InstallContext *c) {
int r;
assert(c);
r = load_etc_machine_id(c);
if (r < 0)
return r;
r = load_etc_machine_info(c);
if (r < 0)
return r;
r = load_kernel_install_layout(c);
if (r < 0)
return r;
const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
r = boot_entry_token_ensure_at(
e ? XAT_FDROOT : c->root_fd,
e,
c->machine_id,
/* machine_id_is_random= */ false,
&c->entry_token_type,
&c->entry_token);
if (r < 0)
return r;
log_debug("Using entry token: %s", c->entry_token);
bool layout_type1 = use_boot_loader_spec_type1(c);
if (c->make_entry_directory < 0) { /* Automatic mode */
if (layout_type1) {
if (c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID) {
_cleanup_free_ char *j = path_join(c->root, "/etc/machine-id");
if (!j)
return log_oom();
_cleanup_close_ int fd = -EBADF;
r = chaseat(c->root_fd,
c->root_fd,
"/etc/machine-id",
CHASE_MUST_BE_REGULAR,
/* ret_path= */ NULL,
&fd);
if (r < 0)
return log_debug_errno(r, "Unable to open '%s': %m", j);
r = fd_is_temporary_fs(fd);
if (r < 0)
return log_debug_errno(r, "Couldn't determine whether '%s' is on a temporary file system: %m", j);
c->make_entry_directory = r == 0;
} else
c->make_entry_directory = true;
} else
c->make_entry_directory = false;
}
if (c->make_entry_directory > 0 && !layout_type1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"KERNEL_INSTALL_LAYOUT=\"%s\" is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
c->install_layout);
return 0;
}
static int compare_product(const char *a, const char *b) {
size_t x, y;
assert(a);
assert(b);
x = strcspn(a, " ");
y = strcspn(b, " ");
if (x != y)
return x < y ? -1 : x > y ? 1 : 0;
return strncmp(a, b, x);
}
static int compare_version(const char *a, const char *b) {
assert(a);
assert(b);
a += strcspn(a, " ");
a += strspn(a, " ");
b += strcspn(b, " ");
b += strspn(b, " ");
return strverscmp_improved(a, b);
}
static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
_cleanup_free_ char *a = NULL, *b = NULL;
int r;
assert(fd_from >= 0);
assert(from);
assert(fd_to >= 0);
assert(to);
/* Does not reposition file offset */
r = get_file_version(fd_from, &a);
if (r == -ESRCH)
return log_notice_errno(r, "Source file \"%s\" does not carry version information!", from);
if (r < 0)
return log_error_errno(r, "Failed to get file version of '%s': %m", from);
r = get_file_version(fd_to, &b);
if (r == -ESRCH)
return log_info_errno(r, "Skipping \"%s\", it's owned by another boot loader (no version info found).", to);
if (r < 0)
return log_error_errno(r, "Failed to get file version of '%s': %m", to);
if (compare_product(a, b) != 0)
return log_info_errno(SYNTHETIC_ERRNO(ESRCH),
"Skipping \"%s\", it's owned by another boot loader.", to);
r = compare_version(a, b);
log_debug("Comparing versions: \"%s\" %s \"%s\"", a, comparison_operator(r), b);
if (r < 0)
return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
"Skipping \"%s\", newer boot loader version in place already.", to);
if (r == 0)
return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
"Skipping \"%s\", same boot loader version in place already.", to);
return 0;
}
static int copy_file_with_version_check(
const char *source_path,
int source_fd,
const char *dest_path,
int dest_parent_fd,
const char *dest_filename,
int dest_fd,
bool force) {
int r;
assert(source_path);
assert(source_fd >= 0);
assert(dest_path);
assert(dest_parent_fd >= 0);
assert(dest_filename);
if (!force && dest_fd >= 0) {
r = version_check(source_fd, source_path, dest_fd, dest_path);
if (r < 0)
return r;
}
_cleanup_free_ char *t = NULL;
_cleanup_close_ int write_fd = -EBADF;
write_fd = open_tmpfile_linkable_at(dest_parent_fd, dest_filename, O_WRONLY|O_CLOEXEC, &t);
if (write_fd < 0)
return log_error_errno(write_fd, "Failed to open \"%s\" for writing: %m", dest_path);
CLEANUP_TMPFILE_AT(dest_parent_fd, t);
/* Reset file offset before we start copying, since we copy this file multiple times, and the offset
* might be left at the end of the file. (Resetting before rather than after a copy attempt is safer
* because a previous attempt might have failed half-way, leaving the file offset at some undefined
* place.) */
r = copy_bytes(source_fd, write_fd, UINT64_MAX, COPY_SEEK0_SOURCE);
if (r < 0)
return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", source_path, dest_path);
(void) copy_times(source_fd, write_fd, /* flags= */ 0);
(void) fchmod(write_fd, 0644);
r = link_tmpfile_at(write_fd, dest_parent_fd, t, dest_filename, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
if (r < 0)
return log_error_errno(r, "Failed to move data from \"%s\" to \"%s\": %m", source_path, dest_path);
t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
log_info("Copied \"%s\" to \"%s\".", source_path, dest_path);
return 0;
}
static int mkdir_one(const char *root, int root_fd, const char *path) {
int r;
assert(root);
assert(root_fd >= 0);
assert(path);
_cleanup_free_ char *p = path_join(empty_to_root(root), path);
if (!p)
return log_oom();
r = chaseat(root_fd,
root_fd,
path,
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
/* ret_fd= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to create \"%s\": %m", p);
log_info("Created directory \"%s\".", p);
return 0;
}
static const char *const esp_subdirs[] = {
/* The directories to place in the ESP */
"EFI",
"EFI/systemd",
"EFI/BOOT",
"loader",
"loader/keys",
NULL
};
static const char *const dollar_boot_subdirs[] = {
/* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
"loader",
"loader/entries", /* Type #1 entries */
"EFI",
"EFI/Linux", /* Type #2 entries */
NULL
};
static int create_subdirs(const char *root, int root_fd, const char * const *subdirs) {
int r;
assert(root);
assert(root_fd >= 0);
STRV_FOREACH(i, subdirs) {
r = mkdir_one(root, root_fd, *i);
if (r < 0)
return r;
}
return 0;
}
static int update_efi_boot_binaries(
InstallContext *c,
const char *source_path,
int source_fd,
const char *ignore_filename) {
int r, ret = 0;
assert(c);
assert(source_path);
if (c->esp_fd < 0)
return c->esp_fd;
_cleanup_free_ char *j = path_join(c->root, c->esp_path);
if (!j)
return log_oom();
_cleanup_closedir_ DIR *d = NULL;
r = chase_and_opendirat(
c->esp_fd,
c->esp_fd,
"/EFI/BOOT",
CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&d);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", j);
FOREACH_DIRENT(de, d, break) {
_cleanup_close_ int fd = -EBADF;
if (!endswith_no_case(de->d_name, ".efi"))
continue;
if (strcaseeq_ptr(ignore_filename, de->d_name))
continue;
fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
if (fd < 0)
return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", j, de->d_name);
r = pe_is_native_fd(fd);
if (r < 0) {
log_warning_errno(r, "Failed to detect if \"%s/%s\" is for native architecture, ignoring: %m", j, de->d_name);
continue;
}
if (r == 0)
continue;
_cleanup_free_ char *dest_path = path_join(j, "/EFI/BOOT", de->d_name);
if (!dest_path)
return log_oom();
r = copy_file_with_version_check(source_path, source_fd, dest_path, dirfd(d), de->d_name, fd, /* force= */ false);
if (IN_SET(r, -ESTALE, -ESRCH))
continue;
RET_GATHER(ret, r);
}
return ret;
}
static int copy_one_file(
InstallContext *c,
const char *name,
bool force) {
int r, ret = 0;
assert(c);
_cleanup_free_ char *dest_name = strdup(name);
if (!dest_name)
return log_oom();
char *s = endswith_no_case(dest_name, ".signed");
if (s)
*s = 0;
_cleanup_free_ char *sp = path_join(BOOTLIBDIR, name);
if (!sp)
return log_oom();
_cleanup_free_ char *source_path = NULL;
_cleanup_close_ int source_fd = -EBADF;
if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
source_fd = chase_and_openat(
c->root_fd,
c->root_fd,
sp,
CHASE_MUST_BE_REGULAR,
O_RDONLY|O_CLOEXEC,
&source_path);
if (source_fd < 0 && (source_fd != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
return log_error_errno(source_fd, "Failed to resolve path '%s' under directory '%s': %m", sp, c->root);
/* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
}
if (source_fd < 0) {
source_fd = chase_and_open(
sp,
/* root= */ NULL,
CHASE_MUST_BE_REGULAR,
O_RDONLY|O_CLOEXEC,
&source_path);
if (source_fd < 0)
return log_error_errno(source_fd, "Failed to resolve path '%s': %m", sp);
}
if (c->esp_fd < 0)
return c->esp_fd;
_cleanup_free_ char *j = path_join(c->root, c->esp_path);
if (!j)
return log_oom();
_cleanup_close_ int dest_parent_fd = -EBADF;
r = chaseat(c->esp_fd,
c->esp_fd,
"/EFI/systemd",
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&dest_parent_fd);
if (r < 0)
return log_error_errno(r, "Failed to resolve path '/EFI/systemd' under directory '%s': %m", j);
_cleanup_free_ char *dest_path = path_join(j, "/EFI/systemd", dest_name);
if (!dest_path)
return log_oom();
_cleanup_close_ int dest_fd = xopenat_full(dest_parent_fd, dest_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
if (dest_fd < 0 && dest_fd != -ENOENT)
return log_error_errno(dest_fd, "Failed to open '%s' under '%s/EFI/systemd' directory: %m", dest_name, j);
const char *e = startswith(dest_name, "systemd-boot");
/* If a primary sd-boot binary already exists and the source is a newer version, copy
* the existing primary to systemd-boot-fallback{arch}.efi before installing the new
* one, so firmware has a fallback to the previous binary. The fallback is left alone
* when its product and version match the currently booted bootloader (from LoaderInfo),
* so a known good binary stays as the fallback. In all other cases, like no fallback yet,
* LoaderInfo is unavailable, or product/version differs from what booted, it is
* overwritten with the current primary. */
if (e && dest_fd >= 0 && !force) {
r = version_check(source_fd, source_path, dest_fd, dest_path);
if (r < 0)
/* Stash the error and fall through; the BOOT{arch}.EFI updates below still run. */
ret = r;
else {
_cleanup_free_ char *fallback_name = strjoin("systemd-boot-fallback", e);
if (!fallback_name)
return log_oom();
_cleanup_free_ char *fallback_path = path_join(j, "/EFI/systemd", fallback_name);
if (!fallback_path)
return log_oom();
/* Leave the fallback alone if it already holds the currently booted product
* and version, so a known good binary stays as the fallback. If there is no
* fallback yet, LoaderInfo is unavailable, or there is a mismatch, then
* overwrite it with the current primary. */
bool should_rotate = true;
_cleanup_close_ int fallback_fd = xopenat_full(dest_parent_fd, fallback_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
if (fallback_fd >= 0) {
_cleanup_free_ char *loader_info = NULL, *fallback_version = NULL;
if (efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderInfo"), &loader_info) >= 0 &&
get_file_version(fallback_fd, &fallback_version) >= 0)
should_rotate = compare_product(loader_info, fallback_version) != 0 ||
compare_version(loader_info, fallback_version) != 0;
}
if (should_rotate) {
r = copy_file_with_version_check(dest_path, dest_fd, fallback_path, dest_parent_fd, fallback_name, /* dest_fd= */ -EBADF, /* force= */ true);
if (r < 0)
log_warning_errno(r, "Failed to back up sd-boot binary to fallback path, continuing: %m");
}
ret = copy_file_with_version_check(source_path, source_fd, dest_path, dest_parent_fd, dest_name, dest_fd, /* force= */ true);
}
} else
/* Note that if this fails we do the second copy anyway, but return this error code,
* so we stash it away in a separate variable. */
ret = copy_file_with_version_check(source_path, source_fd, dest_path, dest_parent_fd, dest_name, dest_fd, force);
if (e) {
/* Create the EFI default boot loader name (specified for removable devices) */
_cleanup_free_ char *boot_dot_efi = strjoin("BOOT", e);
if (!boot_dot_efi)
return log_oom();
ascii_strupper(boot_dot_efi);
_cleanup_close_ int default_dest_parent_fd = -EBADF;
r = chaseat(c->esp_fd,
c->esp_fd,
"/EFI/BOOT",
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&default_dest_parent_fd);
if (r < 0)
return log_error_errno(r, "Failed to resolve path '/EFI/BOOT/' under directory '%s': %m", j);
_cleanup_free_ char *default_dest_path = path_join(j, "/EFI/BOOT", boot_dot_efi);
if (!default_dest_path)
return log_oom();
_cleanup_close_ int default_dest_fd = xopenat_full(default_dest_parent_fd, boot_dot_efi, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
if (default_dest_fd < 0 && default_dest_fd != -ENOENT)
return log_error_errno(default_dest_fd, "Failed to open '%s' under '%s/EFI/BOOT' directory: %m", boot_dot_efi, j);
RET_GATHER(ret, copy_file_with_version_check(source_path, source_fd, default_dest_path, default_dest_parent_fd, boot_dot_efi, default_dest_fd, force));
/* If we were installed under any other name in /EFI/BOOT/, make sure we update those
* binaries as well. */
if (!force)
RET_GATHER(ret, update_efi_boot_binaries(c, source_path, source_fd, boot_dot_efi));
}
return ret;
}
static int install_binaries(
InstallContext *c,
const char *arch) {
int r;
assert(c);
_cleanup_free_ char *source_path = NULL;
_cleanup_closedir_ DIR *d = NULL;
if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
r = chase_and_opendirat(
c->root_fd,
c->root_fd,
BOOTLIBDIR,
CHASE_MUST_BE_DIRECTORY,
&source_path,
&d);
if (r < 0 && (r != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
return log_error_errno(r, "Failed to resolve path '%s' under directory '%s': %m", BOOTLIBDIR, c->root);
/* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
}
if (!d) {
r = chase_and_opendir(
BOOTLIBDIR,
/* root= */ NULL,
CHASE_MUST_BE_DIRECTORY,
&source_path,
&d);
if (r == -ENOENT && c->graceful) {
log_debug("Source directory '%s' does not exist, ignoring.", BOOTLIBDIR);
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to resolve path '%s': %m", BOOTLIBDIR);
}
const char *suffix = strjoina(arch, ".efi");
const char *suffix_signed = strjoina(arch, ".efi.signed");
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", source_path)) {
int k;
if (endswith_no_case(de->d_name, suffix)) {
/* skip the .efi file, if there's a .signed version of it */
_cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
if (!s)
return log_oom();
if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
continue;
} else if (!endswith_no_case(de->d_name, suffix_signed))
continue;
k = copy_one_file(c, de->d_name, c->operation == INSTALL_NEW);
/* Don't propagate an error code if no update necessary, installed version already equal or
* newer version, or other boot loader in place. */
if (c->graceful && IN_SET(k, -ESTALE, -ESRCH))
continue;
RET_GATHER(r, k);
}
return r;
}
static int install_loader_config(InstallContext *c) {
int r;
assert(c);
assert(c->make_entry_directory >= 0);
if (c->esp_fd < 0)
return c->esp_fd;
_cleanup_free_ char *j = path_join(c->root, c->esp_path);
if (!j)
return log_oom();
_cleanup_close_ int loader_dir_fd = -EBADF;
r = chaseat(c->esp_fd,
c->esp_fd,
"loader",
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&loader_dir_fd);
if (r < 0)
return log_error_errno(r, "Failed to open '/loader/' directory below '%s': %m", j);
if (faccessat(loader_dir_fd, "loader.conf", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
if (errno != ENOENT)
return log_error_errno(errno, "Failed to check if '/loader/loader.conf' exists below '%s': %m", j);
} else /* Silently skip creation if the file already exists (early check) */
return 0;
_cleanup_free_ char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
r = fopen_tmpfile_linkable_at(loader_dir_fd, "loader.conf", O_WRONLY|O_CLOEXEC, &t, &f);
if (r < 0)
return log_error_errno(r, "Failed to open '%s/loader/loader.conf' for writing: %m", j);
CLEANUP_TMPFILE_AT(loader_dir_fd, t);
fprintf(f, "#timeout 3\n"
"#console-mode keep\n");
if (c->make_entry_directory) {
assert(c->entry_token);
fprintf(f, "default %s-*\n", c->entry_token);
}
r = flink_tmpfile_at(f, loader_dir_fd, t, "loader.conf", LINK_TMPFILE_SYNC);
if (r == -EEXIST)
return 0; /* Silently skip creation if the file exists now (recheck) */
if (r < 0)
return log_error_errno(r, "Failed to move '%s/loader/loader.conf' into place: %m", j);
t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
return 1;
}
static int install_loader_specification(InstallContext *c) {
int r;
assert(c);
int dollar_boot_fd = acquire_dollar_boot_fd(c);
if (dollar_boot_fd < 0)
return dollar_boot_fd;
_cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
if (!j)
return log_oom();
_cleanup_close_ int loader_dir_fd = -EBADF;
r = chaseat(dollar_boot_fd,
dollar_boot_fd,
"loader",
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&loader_dir_fd);
if (r < 0)
return log_error_errno(r, "Failed to pin '/loader' directory below '%s': %m", j);
if (faccessat(loader_dir_fd, "entries.srel", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
if (errno != ENOENT)
return log_error_errno(errno, "Failed to check if '/loader/entries.srel' exists below '%s': %m", j);
} else /* Silently skip creation if the file already exists (early check) */
return 0;
_cleanup_free_ char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
r = fopen_tmpfile_linkable_at(loader_dir_fd, "entries.srel", O_WRONLY|O_CLOEXEC, &t, &f);
if (r < 0)
return log_error_errno(r, "Failed to open '%s/loader/entries.srel' for writing: %m", j);
CLEANUP_TMPFILE_AT(loader_dir_fd, t);
fprintf(f, "type1\n");
r = flink_tmpfile_at(f, loader_dir_fd, t, "entries.srel", LINK_TMPFILE_SYNC);
if (r == -EEXIST)
return 0; /* Silently skip creation if the file exists now (recheck) */
if (r < 0)
return log_error_errno(r, "Failed to move '%s/loader/entries.srel' into place: %m", j);
t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
return 1;
}
static int install_entry_directory(InstallContext *c) {
assert(c);
assert(c->make_entry_directory >= 0);
if (!c->make_entry_directory)