| 1 | #!/bin/ash |
| 2 | # vim: set ts=4: |
| 3 | set -euo pipefail |
| 4 | |
| 5 | readonly HOOK_NAME='secureboot' |
| 6 | |
| 7 | # Defaults |
| 8 | cmdline= |
| 9 | signing_cert='/etc/uefi-keys/db.crt' |
| 10 | signing_key='/etc/uefi-keys/db.key' |
| 11 | signing_disabled=no |
| 12 | openssl_engine= |
| 13 | microcode= |
| 14 | splash_image='/dev/null' |
| 15 | output_dir='/boot/efi/Alpine' |
| 16 | output_name='linux-{flavor}.efi' |
| 17 | backup_old=yes |
| 18 | skip_flavors= |
| 19 | efistub_file='/usr/lib/systemd/boot/efi/linux{march}.efi.stub' |
| 20 | |
| 21 | die() { |
| 22 | printf "$HOOK_NAME: %s\n" "$2" >&2 |
| 23 | exit $1 |
| 24 | } |
| 25 | |
| 26 | |
| 27 | if [ $# -lt 2 ]; then |
| 28 | echo "Usage: $0 <flavor> <new-version> <old-version>" >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | readonly FLAVOR=$1 |
| 33 | readonly NEW_VERSION=$2 |
| 34 | readonly OLD_VERSION=${3:-} |
| 35 | case "$(cat /etc/apk/arch)" in |
| 36 | aarch64) readonly MARCH="aa64";; |
| 37 | arm*) readonly MARCH="arm";; |
| 38 | riscv64) readonly MARCH="riscv64";; |
| 39 | x86) readonly MARCH="ia32";; |
| 40 | x86_64) readonly MARCH="x64";; |
| 41 | esac |
| 42 | |
| 43 | # Hook triggered for the kernel removal, nothing to do here. |
| 44 | [ "$NEW_VERSION" ] || exit 0 |
| 45 | |
| 46 | . /etc/kernel-hooks.d/secureboot.conf |
| 47 | |
| 48 | [ "$skip_flavors" ] && for flavor in $skip_flavors; do |
| 49 | [ "$flavor" = "$FLAVOR" ] \ |
| 50 | && die 0 "==> skipping UEFI image creation for $FLAVOR kernel" |
| 51 | done |
| 52 | |
| 53 | [ "$cmdline" ] \ |
| 54 | || die 0 "cmdline is not specified in /etc/kernel-hooks.d/$HOOK_NAME.conf, skipping hook!" |
| 55 | |
| 56 | if [ "$signing_disabled" != yes ]; then |
| 57 | [ -r "$signing_cert" ] \ |
| 58 | || die 2 "ERROR: signing cert '$signing_cert' does not exist or not readable!" |
| 59 | |
| 60 | [ -r "$signing_key" ] \ |
| 61 | || die 2 "ERROR: signing key '$signing_key' does not exist or not readable!" |
| 62 | fi |
| 63 | |
| 64 | vmlinuz="/boot/vmlinuz-$FLAVOR" |
| 65 | output_name=$(echo "$output_name" \ |
| 66 | | sed "s/{flavor}/$FLAVOR/; s/{version}/$NEW_VERSION/") |
| 67 | output="$output_dir/$output_name" |
| 68 | efistub_file=$(echo "$efistub_file" | sed "s/{march}/$MARCH/") |
| 69 | |
| 70 | [ "$microcode" ] || for path in /boot/intel-ucode.img /boot/amd-ucode.img; do |
| 71 | [ -f "$path" ] && microcode="$path" |
| 72 | done |
| 73 | |
| 74 | tmpdir=$(mktemp -dt "$HOOK_NAME.XXXXXX") |
| 75 | trap "rm -f '$tmpdir'/*; rmdir '$tmpdir'" EXIT HUP INT TERM |
| 76 | |
| 77 | if [ "$backup_old" = yes ] && [ -f "$output" ]; then |
| 78 | cp -a "$output" "$output.bak" |
| 79 | fi |
| 80 | |
| 81 | mkinitfs -o "$tmpdir"/initramfs "$NEW_VERSION-$FLAVOR" |
| 82 | |
| 83 | echo "==> $HOOK_NAME: creating UEFI Unified Kernel Image with $vmlinuz" |
| 84 | efi-mkuki \ |
| 85 | -c "$cmdline" \ |
| 86 | -k "$NEW_VERSION-$FLAVOR" \ |
| 87 | -s "$splash_image" \ |
| 88 | -S "$efistub_file" \ |
| 89 | -o "$tmpdir"/unsigned.efi \ |
| 90 | "$vmlinuz" $microcode "$tmpdir"/initramfs |
| 91 | |
| 92 | mkdir -p "$output_dir" |
| 93 | |
| 94 | if [ "$signing_disabled" = yes ]; then |
| 95 | echo "==> $HOOK_NAME: writing *unsigned* UEFI image to $output (signing is disabled!)" |
| 96 | mv "$tmpdir"/unsigned.efi "$output" |
| 97 | else |
| 98 | echo "==> $HOOK_NAME: signing UEFI image and writing to $output" |
| 99 | sbsign \ |
| 100 | --cert "$signing_cert" \ |
| 101 | --key "$signing_key" \ |
| 102 | --output "$output" \ |
| 103 | ${openssl_engine:+--engine $openssl_engine} \ |
| 104 | "$tmpdir"/unsigned.efi \ |
| 105 | 2>&1 | { grep -Fv -e ' gaps between PE/COFF ' -e 'Signing Unsigned ' ||:; } >&2 |
| 106 | # this is not an issue ^ |
| 107 | fi |
| 108 | |
| 109 | # Just a simple sanity check. |
| 110 | dir="$(readlink -f "$output_dir")" |
| 111 | while [ "$dir" ]; do |
| 112 | mount -t vfat | grep -Fq " on $dir type vfat " && exit 0 |
| 113 | dir=${dir%/*} |
| 114 | done |
| 115 | die 0 "WARNING: $output_dir is not on UEFI System Partition as it should be!" |