#!/bin/sh
# local-premount: unlock the ENCRYPTED ROOT LV with the DEK from the board.
#
# Runs after local-top/lvm2 has activated the volume group, and before
# mountroot. Sourced by init — never call exit outside the prereqs branch.
# Gated by "dekroot=1" so the default boot is unaffected.
#
# Diagnostics are appended to /boot/dekroot.log on the (unencrypted) boot
# partition, so a failed boot can be investigated after recovering.

case "$1" in
prereqs)
    exit 0
    ;;
esac

case " $(cat /proc/cmdline) " in
  *" dekroot=1 "*)
    ;;
  *)
    return 0
    ;;
esac

DEK_KEY=/run/dek.key
MAPPER=dek-root
DEK_TIMEOUT="${DEK_TIMEOUT:-60s}"
BOOTDEV="${BOOTDEV:-/dev/sda2}"
ROOT_LV="${ROOT_LV:-/dev/ubuntu-vg/root-crypt}"
for arg in $(cat /proc/cmdline); do
    case "$arg" in
    dekrootlv=*)
        ROOT_LV="${arg#dekrootlv=}"
        ;;
    esac
done

# Log to the kernel console and to /boot (survives a failed boot).
dlog() {
    echo "dekroot: $*" > /dev/kmsg
    mkdir -p /tmp/dlog
    if mount -o rw "$BOOTDEV" /tmp/dlog 2>/dev/null; then
        printf '%8s %s\n' "$(cut -d' ' -f1 /proc/uptime)" "$*" >> /tmp/dlog/dekroot.log
        umount /tmp/dlog 2>/dev/null
    fi
}

# panic() is not available in this initramfs; the loop is the real stop.
fail_closed() {
    dlog "FAIL-CLOSED: $*"
    panic "DEK root fail-closed: $*"
    while :; do
        sleep 3600
    done
}

dlog "start; cmdline: $(cat /proc/cmdline)"
dlog "target LV: $ROOT_LV"

# Be defensive about LVM: activate the LV ourselves if lvm is present.
if [ -x /sbin/lvm ]; then
    /sbin/lvm lvchange -ay "$ROOT_LV" >/dev/null 2>&1 || true
fi
i=0
while [ ! -e "$ROOT_LV" ] && [ "$i" -lt 10 ]; do
    sleep 1
    i=$((i + 1))
done

if [ ! -e "$ROOT_LV" ]; then
    dlog "devices: $(ls /dev/mapper 2>/dev/null | tr '\n' ' ')"
    fail_closed "root LV $ROOT_LV does not exist (LVM not active?)"
fi
if ! /sbin/cryptsetup isLuks "$ROOT_LV" 2>/dev/null; then
    fail_closed "$ROOT_LV is not a LUKS device"
fi
dlog "target is a LUKS device"

dlog "fetching DEK over PCIe (timeout $DEK_TIMEOUT)"
if ! /bin/dek-fetch -key-out "$DEK_KEY" -timeout "$DEK_TIMEOUT"; then
    fail_closed "DEK unavailable over PCIe"
fi
dlog "DEK fetched"

if ! /sbin/cryptsetup luksOpen "$ROOT_LV" "$MAPPER" --key-file "$DEK_KEY"; then
    rm -f "$DEK_KEY"
    fail_closed "luksOpen $ROOT_LV failed"
fi

rm -f "$DEK_KEY"
dlog "$ROOT_LV unlocked as /dev/mapper/$MAPPER, key removed"

# Sanity-check the unlocked filesystem here, so a later mountroot failure can
# be told apart from an unlock failure.
dlog "mapper list: $(ls /dev/mapper 2>/dev/null | tr '\n' ' ')"
if [ -x /sbin/blkid ]; then
    dlog "blkid: $(/sbin/blkid -o value -s TYPE "/dev/mapper/$MAPPER" 2>&1)"
elif [ -x /usr/sbin/blkid ]; then
    dlog "blkid: $(/usr/sbin/blkid -o value -s TYPE "/dev/mapper/$MAPPER" 2>&1)"
fi
mkdir -p /tmp/rs
if mount -o ro "/dev/mapper/$MAPPER" /tmp/rs 2>/tmp/rs.err; then
    dlog "sanity mount OK; top entries: $(ls /tmp/rs 2>/dev/null | tr '\n' ' ')"
    umount /tmp/rs 2>/dev/null
else
    dlog "sanity mount FAILED: $(cat /tmp/rs.err 2>/dev/null)"
fi
