Files
Stefan Agner aaa55848de Implement A/B switch when moving back to Barebox (#1794)
* Implement manual boot switch when downgrading to Barebox

When installing OS 7 or older from a OS 8 installation, the bootloader
is changed back to Barebox. However, in this case Barebox does not know
which boot slot is correct (and rauc won't update Barebox' state as it
is configured to work with GRUB in OS 8+).

This implements a poor mans version to switch the boot partition. It
clears Barebox state and deploys one of the two pre-configured default
states, booting either boot slot A or B.
2022-03-23 16:00:49 +01:00

116 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
set -o errexit
##
# Hooks
case "$1" in
install-check)
if [ "$RAUC_MF_COMPATIBLE" = "$RAUC_SYSTEM_COMPATIBLE" ]; then
exit 0
fi
# Be compatible with hassos OS ID
# shellcheck disable=SC3060
rauc_os_compatible=${RAUC_MF_COMPATIBLE/haos-/hassos-}
if [ "$rauc_os_compatible" = "$RAUC_SYSTEM_COMPATIBLE" ]; then
exit 0
fi
# generic-x86-64: Be compatible with intel-nuc
# shellcheck disable=SC3060
rauc_board_compatible=${rauc_os_compatible/generic-x86-64/intel-nuc}
if [ "${rauc_board_compatible}" = "$RAUC_SYSTEM_COMPATIBLE" ]; then
exit 0
fi
echo "Compatible does not match!" 1>&2
exit 10
;;
slot-install)
# Use install handlers below
;;
slot-post-install)
# Handle main slot only
test "${RAUC_SLOT_CLASS}" = "kernel" || exit 0
BOOT_MNT=/mnt/boot
if ! systemctl -q is-active mnt-boot.mount; then
systemctl start mnt-boot.mount
fi
# OS 8+ to 7 and lower downgrade path
if ! command -v barebox-state > /dev/null; then
cp -f "${BOOT_MNT}/EFI/barebox/state-${RAUC_SLOT_BOOTNAME}.dtb" "${BOOT_MNT}"/EFI/barebox/state.dtb
blkdiscard /dev/disk/by-partuuid/33236519-7f32-4dff-8002-3390b62c309d
echo "Cleared state and copied default state-${RAUC_SLOT_BOOTNAME}.dtb to state.dtb."
fi
exit 0
;;
*)
exit 1
;;
esac
# Handle boot hocks
if [ "${RAUC_SLOT_CLASS}" = "boot" ]; then
BOOT_TMP=/tmp/boot-tmp
BOOT_NEW=/tmp/boot-new
BOOT_MNT=/mnt/boot
mkdir -p "${BOOT_TMP}"
mkdir -p "${BOOT_NEW}"
# Mount boot
if ! systemctl -q is-active mnt-boot.mount; then
systemctl start mnt-boot.mount
fi
mount "${RAUC_IMAGE_NAME}" "${BOOT_NEW}"
# Backup boot config
cp -f "${BOOT_MNT}"/*.txt "${BOOT_TMP}/"
# Update
cp -rf "${BOOT_NEW}"/* "${BOOT_MNT}/"
# Restore boot config
cp -f "${BOOT_TMP}"/*.txt "${BOOT_MNT}/"
umount "${BOOT_NEW}"
rm -rf "${BOOT_TMP}" "${BOOT_NEW}"
fi
# Handle spl install
if [ "${RAUC_SLOT_CLASS}" = "spl" ]; then
DEVICE_CHILD="$(findfs LABEL="hassos-boot")"
DEVICE_ROOT="/dev/$(lsblk -no pkname "${DEVICE_CHILD}")"
PART_TABLE="$(sfdisk -lqJ "${DEVICE_ROOT}")"
PART_LABEL="$(echo "${PART_TABLE}" | jq -r '.partitiontable.label')"
FLAGS=""
if dd oflag=direct if=/dev/null 2> /dev/null; then
FLAGS="oflag=direct"
fi
if [ "${PART_LABEL}" = "gpt" ]; then
dd if="${RAUC_IMAGE_NAME}" of="${DEVICE_ROOT}" conv=notrunc ${FLAGS} bs=512 seek=2 skip=2
else
dd if="${RAUC_IMAGE_NAME}" of="${DEVICE_ROOT}" conv=notrunc ${FLAGS} bs=1 count=440
dd if="${RAUC_IMAGE_NAME}" of="${DEVICE_ROOT}" conv=notrunc ${FLAGS} bs=512 seek=1 skip=1
fi
# Flash to eMMC boot partition if necessary
if [ "$RAUC_SYSTEM_COMPATIBLE" = "haos-odroid-xu4" ] && [ -b "${DEVICE_ROOT}boot0" ]; then
echo "Updating eMMC boot partition"
echo 0 > /sys/block/"$(basename "${DEVICE_ROOT}boot0")"/force_ro
dd if="${RAUC_IMAGE_NAME}" of="${DEVICE_ROOT}boot0" conv=notrunc ${FLAGS} bs=512 skip=1 count=2047
echo 1 > /sys/block/"$(basename "${DEVICE_ROOT}boot0")"/force_ro
fi
fi
##
# Fixups
exit 0