| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# When installing/updating systemd-boot, if _both_ signed shim and signed systemd-boot are available, and |
| 4 |
# signed grub is not available, then take over the ESP and the EFI boot entry, install systemd-boot as the |
| 5 |
# second stage loader, and install signed shim as the first stage loader and add a boot entry named after |
| 6 |
# the OS vendor pointing to it, and mark it as the default. |
| 7 |
# |
| 8 |
# When triggered, given dpkg does not tell us whether the trigger added or removed a file, if signed |
| 9 |
# grub is available then do nothing and assume it will take over the ESP. If it is not, and either |
| 10 |
# signed shim or signed systemd-boot or both are not available, then remove all of them from the ESP |
| 11 |
# and the EFI boot entry. If both signed shim and signed systemd-boot are available, then update them |
| 12 |
# in the ESP. |
| 13 |
|
| 14 |
set -e |
| 15 |
|
| 16 |
remove_shim() { |
| 17 |
case "$(dpkg --print-architecture)" in |
| 18 |
amd64) |
| 19 |
efi_arch_upper=X64 |
| 20 |
efi_arch=x64 |
| 21 |
grub_arch=x86_64 |
| 22 |
;; |
| 23 |
arm64) |
| 24 |
efi_arch_upper=AA64 |
| 25 |
efi_arch=aa64 |
| 26 |
grub_arch=arm64 |
| 27 |
;; |
| 28 |
*) |
| 29 |
return |
| 30 |
esac |
| 31 |
|
| 32 |
# shellcheck disable=SC1091 |
| 33 |
. /etc/os-release || . /usr/lib/os-release |
| 34 |
vendor="${ID:-debian}" |
| 35 |
vendor_upper="$(echo "$vendor" | cut -c1 | tr '[:lower:]' '[:upper:]')$(echo "$vendor" | cut -c2-)" |
| 36 |
|
| 37 |
esp_path="$(bootctl --quiet --print-esp-path 2>/dev/null)" |
| 38 |
if [ -z "$esp_path" ]; then |
| 39 |
return |
| 40 |
fi |
| 41 |
|
| 42 |
if [ -f "/usr/lib/grub/${grub_arch}-efi-signed/grub${efi_arch}.efi.signed" ]; then |
| 43 |
return |
| 44 |
fi |
| 45 |
|
| 46 |
if [ ! -f "${esp_path}/EFI/systemd/systemd-boot${efi_arch}.efi" ]; then |
| 47 |
return |
| 48 |
fi |
| 49 |
|
| 50 |
if [ -f "${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" ] && \ |
| 51 |
[ -f "${esp_path}/EFI/${vendor}/shim${efi_arch}.efi" ] && \ |
| 52 |
[ "$(<"${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" sha256sum)" = "$(<"${esp_path}/EFI/${vendor}/shim${efi_arch}.efi" sha256sum)" ]; then |
| 53 |
rm -f "${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" |
| 54 |
fi |
| 55 |
|
| 56 |
if [ -f "${esp_path}/EFI/BOOT/fb${efi_arch_upper}.efi" ] && \ |
| 57 |
[ -f "${esp_path}/EFI/${vendor}/fb${efi_arch}.efi" ] && \ |
| 58 |
[ "$(<"${esp_path}/EFI/BOOT/fb${efi_arch_upper}.efi" sha256sum)" = "$(<"${esp_path}/EFI/${vendor}/fb${efi_arch}.efi" sha256sum)" ]; then |
| 59 |
rm -f "${esp_path}/EFI/BOOT/fb${efi_arch_upper}.efi" |
| 60 |
fi |
| 61 |
|
| 62 |
for f in shim fb mm; do |
| 63 |
rm -f "${esp_path}/EFI/${vendor}/${f}${efi_arch}.efi" |
| 64 |
done |
| 65 |
|
| 66 |
rm -f "${esp_path}/EFI/${vendor}/BOOT${efi_arch_upper}.CSV" |
| 67 |
rmdir --ignore-fail-on-non-empty "${esp_path}/EFI/${vendor}" 2>/dev/null || true |
| 68 |
|
| 69 |
if efibootmgr | grep -i -q "Boot.*${vendor_upper}.*EFI\\\\${vendor}\\\\shim${efi_arch}.efi"; then |
| 70 |
bootentry="$(efibootmgr | grep -i "Boot.*${vendor_upper}.*EFI\\\\${vendor}\\\\shim${efi_arch}.efi" | cut -d' ' -f1 | sed -e 's/Boot//' -e 's/*//')" |
| 71 |
efibootmgr -q --delete-bootnum --bootnum "$bootentry" |
| 72 |
fi |
| 73 |
} |
| 74 |
|
| 75 |
install_shim() { |
| 76 |
case "$(dpkg --print-architecture)" in |
| 77 |
amd64) |
| 78 |
efi_arch_upper=X64 |
| 79 |
efi_arch=x64 |
| 80 |
grub_arch=x86_64 |
| 81 |
;; |
| 82 |
arm64) |
| 83 |
efi_arch_upper=AA64 |
| 84 |
efi_arch=aa64 |
| 85 |
grub_arch=arm64 |
| 86 |
;; |
| 87 |
*) |
| 88 |
return |
| 89 |
esac |
| 90 |
|
| 91 |
if [ ! -f "/usr/lib/shim/shim${efi_arch}.efi.signed" ] || [ ! -f "/usr/lib/systemd/boot/efi/systemd-boot${efi_arch}.efi.signed" ]; then |
| 92 |
if [ "$1" = trigger ]; then |
| 93 |
remove_shim |
| 94 |
fi |
| 95 |
return |
| 96 |
fi |
| 97 |
|
| 98 |
if [ -f "/usr/lib/grub/${grub_arch}-efi-signed/grub${efi_arch}.efi.signed" ]; then |
| 99 |
return |
| 100 |
fi |
| 101 |
|
| 102 |
esp_path="$(bootctl --quiet --print-esp-path 2>/dev/null)" |
| 103 |
if [ -z "$esp_path" ]; then |
| 104 |
return |
| 105 |
fi |
| 106 |
|
| 107 |
# shellcheck disable=SC1091 |
| 108 |
. /etc/os-release || . /usr/lib/os-release |
| 109 |
vendor="${ID:-debian}" |
| 110 |
vendor_upper="$(echo "$vendor" | cut -c1 | tr '[:lower:]' '[:upper:]')$(echo "$vendor" | cut -c2-)" |
| 111 |
|
| 112 |
for f in shim fb mm; do |
| 113 |
if [ ! -f "/usr/lib/shim/${f}${efi_arch}.efi.signed" ]; then |
| 114 |
continue |
| 115 |
fi |
| 116 |
if [ -f "${esp_path}/EFI/${vendor}/${f}${efi_arch}.efi" ] && [ "$(<"${esp_path}/EFI/${vendor}/${f}${efi_arch}.efi" sha256sum)" = "$(<"/usr/lib/shim/${f}${efi_arch}.efi.signed" sha256sum)" ]; then |
| 117 |
continue |
| 118 |
fi |
| 119 |
install -p -D "/usr/lib/shim/${f}${efi_arch}.efi.signed" "${esp_path}/EFI/${vendor}/${f}${efi_arch}.efi" |
| 120 |
done |
| 121 |
|
| 122 |
if [ ! -f "${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" ] || [ "$(<"${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" sha256sum)" != "$(<"/usr/lib/shim/shim${efi_arch}.efi.signed" sha256sum)" ]; then |
| 123 |
install -p -D "/usr/lib/shim/shim${efi_arch}.efi.signed" "${esp_path}/EFI/BOOT/BOOT${efi_arch_upper}.efi" |
| 124 |
fi |
| 125 |
|
| 126 |
if [ ! -f "${esp_path}/EFI/BOOT/fb${efi_arch}.efi" ] || [ "$(<"${esp_path}/EFI/BOOT/fb${efi_arch}.efi" sha256sum)" != "$(<"/usr/lib/shim/fb${efi_arch}.efi.signed" sha256sum)" ]; then |
| 127 |
install -p -D "/usr/lib/shim/fb${efi_arch}.efi.signed" "${esp_path}/EFI/BOOT/fb${efi_arch}.efi" |
| 128 |
fi |
| 129 |
|
| 130 |
if [ ! -f "${esp_path}/EFI/${vendor}/BOOT${efi_arch_upper}.CSV" ] && command -v iconv >/dev/null 2>&1; then |
| 131 |
printf "shim%s.efi,%s,\\\\EFI\\\\systemd\\\\systemd-boot%s.efi \\\\0,This is the boot entry for %s\n" "${efi_arch}" "${vendor_upper}" "${efi_arch}" "${vendor_upper}" | iconv -t UCS-2LE > "${esp_path}/EFI/${vendor}/BOOT${efi_arch_upper}.CSV" |
| 132 |
fi |
| 133 |
|
| 134 |
# 730079007300740065006d0064002d0062006f006f007400 is 'systemd-boot' encoded in UTF-16-LE" |
| 135 |
if ! efibootmgr | grep -i -q "Boot.*${vendor_upper}.*EFI\\\\${vendor}\\\\shim${efi_arch}.efi.*systemd-boot${efi_arch}.efi\|Boot.*${vendor_upper}.*EFI\\\\${vendor}\\\\shim${efi_arch}.efi.*730079007300740065006d0064002d0062006f006f007400"; then |
| 136 |
blkpart="$(findmnt -nvo SOURCE "$esp_path")" |
| 137 |
if [ ! -L "/sys/class/block/${blkpart##*/}" ]; then |
| 138 |
return |
| 139 |
fi |
| 140 |
drive="$(readlink -f "/sys/class/block/${blkpart##*/}")" |
| 141 |
drive="${drive%/*}" |
| 142 |
drive="/dev/${drive##*/}" |
| 143 |
partno="$(cat "/sys/class/block/${blkpart##*/}/partition")" |
| 144 |
efibootmgr -q --create --disk "$drive" --part "$partno" --loader "EFI/${vendor}/shim${efi_arch}.efi" --label "${vendor_upper}" --unicode "\EFI\systemd\systemd-boot${efi_arch}.efi \0" |
| 145 |
fi |
| 146 |
} |
| 147 |
|
| 148 |
if [ "$1" = configure ] && bootctl --print-esp-path > /dev/null 2>&1; then |
| 149 |
if bootctl is-installed > /dev/null 2>&1; then |
| 150 |
bootctl update --graceful |
| 151 |
else |
| 152 |
bootctl install --make-machine-id-directory=auto |
| 153 |
fi |
| 154 |
install_shim install |
| 155 |
|
| 156 |
if [ -z "$2" ]; then |
| 157 |
# register existing kernel(s) |
| 158 |
for k in /boot/vmlinu[xz]-*; do |
| 159 |
[ -f "$k" ] || continue |
| 160 |
ver=$(basename "$k" | sed s/^vmlinu[xz]-//) |
| 161 |
kernel-install add "$ver" "$k" |
| 162 |
done |
| 163 |
fi |
| 164 |
fi |
| 165 |
|
| 166 |
if [ "$1" = triggered ]; then |
| 167 |
shift |
| 168 |
for trigger in "$@"; do |
| 169 |
case $trigger in |
| 170 |
/usr/lib/shim|/usr/lib/grub|systemd-boot-signed) |
| 171 |
install_shim trigger |
| 172 |
;; |
| 173 |
esac |
| 174 |
done |
| 175 |
exit 0 |
| 176 |
fi |
| 177 |
|
| 178 |
#DEBHELPER# |