From 78088b8f43717a43661cd2c1627a9860904c4794 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 24 Jan 2023 10:28:25 +0100 Subject: [PATCH 1/4] bootctl: tweak DOS header magic check Read the magic first, try reading the full DOS exe header only in case the magic check succeeds. This avoids throwing an header read error on small dummy files as used by test-kernel-install. --- src/boot/bootctl-uki.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/boot/bootctl-uki.c b/src/boot/bootctl-uki.c index e304cad06a..261e687e3f 100644 --- a/src/boot/bootctl-uki.c +++ b/src/boot/bootctl-uki.c @@ -31,11 +31,16 @@ static int pe_sections(FILE *uki, struct PeSectionHeader **ret, size_t *ret_n) { assert(ret_n); items = fread(&dos, 1, sizeof(dos), uki); - if (items != sizeof(dos)) - return log_error_errno(SYNTHETIC_ERRNO(EIO), "DOS header read error"); + if (items < sizeof(dos.Magic)) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "File is smaller than DOS magic (got %"PRIu64" of %zu bytes)", + items, sizeof(dos.Magic)); if (memcmp(dos.Magic, dos_file_magic, sizeof(dos_file_magic)) != 0) goto no_sections; + if (items != sizeof(dos)) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "File is smaller than DOS header (got %"PRIu64" of %zu bytes)", + items, sizeof(dos)); + if (fseek(uki, le32toh(dos.ExeHeader), SEEK_SET) < 0) return log_error_errno(errno, "seek to PE header"); From c7314ee7e290b3978e2f2d7726d07656eda071f9 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Jan 2023 12:59:33 +0100 Subject: [PATCH 2/4] 90-uki-copy.install: create $BOOT/EFI/Linux directory if needed Do not consider a missing 'Linux' subdirectory an error. Just create it instead. --- src/kernel-install/90-uki-copy.install | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kernel-install/90-uki-copy.install b/src/kernel-install/90-uki-copy.install index d6e3deb723..6c71b211d7 100755 --- a/src/kernel-install/90-uki-copy.install +++ b/src/kernel-install/90-uki-copy.install @@ -49,8 +49,8 @@ case "$COMMAND" in esac if ! [ -d "$UKI_DIR" ]; then - echo "Error: entry directory '$UKI_DIR' does not exist" >&2 - exit 1 + [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "creating $UKI_DIR" + mkdir -p "$UKI_DIR" fi TRIES_FILE="${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries" From 642617f431457382ec2140a52ee08bfbb3e5e1db Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 25 Jan 2023 13:57:09 +0100 Subject: [PATCH 3/4] kernel-install: remove math slang from man page --- man/kernel-install.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/kernel-install.xml b/man/kernel-install.xml index e50aeee949..f27036ea39 100644 --- a/man/kernel-install.xml +++ b/man/kernel-install.xml @@ -111,7 +111,7 @@ 90-uki-copy.install copies a file uki.efi from $KERNEL_INSTALL_STAGING_AREA or if it does - not exist the KERNEL-IMAGE argument, iff it has a + not exist the KERNEL-IMAGE argument, only if it has a .efi extension, to $BOOT/EFI/Linux/ENTRY-TOKEN-KERNEL-VERSION.efi. From 3d5f0bfe4e72fdc4d8f8d65f96dc5501dfed8a64 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Jan 2023 09:05:18 +0100 Subject: [PATCH 4/4] kernel-install: handle uki installs automatically Detect image type using "bootctl kernel-identify $kernel", store result in KERNEL_INSTALL_IMAGE_TYPE. Extend layout autodetection to check the kernel image type and pick layout=uki for UKIs. Resolves: #25822 --- man/kernel-install.xml | 17 +++++++++++++---- src/kernel-install/kernel-install.in | 12 ++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/man/kernel-install.xml b/man/kernel-install.xml index f27036ea39..a57e3e3759 100644 --- a/man/kernel-install.xml +++ b/man/kernel-install.xml @@ -224,10 +224,8 @@ (EFI System Partition) are mounted, and also conceptually referred to as $BOOT. Can be overridden by setting $BOOT_ROOT (see below). - $KERNEL_INSTALL_LAYOUT=bls|uki|other|... is set for the plugins to specify the - installation layout. Defaults to if - $BOOT/ENTRY-TOKEN exists, or - otherwise. Additional layout names may be defined by convention. If a plugin uses a special layout, + $KERNEL_INSTALL_LAYOUT=auto|bls|uki|other|... is set for the plugins to specify the + installation layout. Additional layout names may be defined by convention. If a plugin uses a special layout, it's encouraged to declare its own layout name and configure layout= in install.conf upon initial installation. The following values are currently understood: @@ -264,6 +262,17 @@ Some other layout not understood natively by kernel-install. + + auto + + Pick the layout automatically. If the kernel is a UKI set layout to + . If not default to if + $BOOT/loader/entries.srel with content type1 or + $BOOT/ENTRY-TOKEN exists, or + otherwise. + Leaving layout blank has the same effect. This is the default. + + $KERNEL_INSTALL_INITRD_GENERATOR is set for plugins to select the initrd diff --git a/src/kernel-install/kernel-install.in b/src/kernel-install/kernel-install.in index fa2c0d5276..25884fc0e2 100755 --- a/src/kernel-install/kernel-install.in +++ b/src/kernel-install/kernel-install.in @@ -250,10 +250,18 @@ if [ -z "$ENTRY_TOKEN" ]; then echo "No entry-token candidate matched, using \"$ENTRY_TOKEN\" from machine-id" fi -if [ -z "$layout" ]; then +export KERNEL_INSTALL_IMAGE_TYPE="" +if [ -f "$1" ]; then + KERNEL_INSTALL_IMAGE_TYPE="$(bootctl kernel-identify "$1" 2>/dev/null || echo "unknown")" +fi + +if [ "$layout" = "auto" ] || [ -z "$layout" ]; then # No layout configured by the administrator. Let's try to figure it out # automatically from metadata already contained in $BOOT_ROOT. - if [ -e "$BOOT_ROOT/loader/entries.srel" ]; then + if [ "$KERNEL_INSTALL_IMAGE_TYPE" = "uki" ]; then + layout="uki" + log_verbose "Kernel image is UKI, using layout=$layout" + elif [ -e "$BOOT_ROOT/loader/entries.srel" ]; then read -r ENTRIES_SREL <"$BOOT_ROOT/loader/entries.srel" if [ "$ENTRIES_SREL" = "type1" ]; then # The loader/entries.srel file clearly indicates that the installed