diff --git a/scripts/build_recovery_media.sh b/scripts/build_recovery_media.sh index 4439191e..7c916ef5 100755 --- a/scripts/build_recovery_media.sh +++ b/scripts/build_recovery_media.sh @@ -93,14 +93,16 @@ echo "Copying standalone recovery initrd to media..." sudo cp "${INITRD_TMP}" "${MOUNT_POINT}/aienos-recovery/initrd.img" rm -f "${INITRD_TMP}" -# Create standalone recovery GRUB config that boots directly to RAM maintenance shell +# Create standalone recovery GRUB config that boots directly to RAM maintenance shell. +# The last console= is /dev/console, where the shell runs: tty0 (the screen), +# because Machine 1 has no reachable serial console. cat << "GRUB_EOF" | sudo tee "${MOUNT_POINT}/EFI/BOOT/grub.cfg" > /dev/null set timeout=5 set default=0 menuentry "AIENOS Standalone Recovery System (RAM Maintenance Core)" { search --no-floppy --file --set=root /aienos-recovery/vmlinuz - linux /aienos-recovery/vmlinuz rdinit=/init console=tty0 console=ttyAMA0,115200n8 quiet + linux /aienos-recovery/vmlinuz rdinit=/init console=ttyAMA0,115200n8 console=tty0 quiet initrd /aienos-recovery/initrd.img } @@ -108,6 +110,10 @@ menuentry "Reboot to Incumbent Linux" { exit } GRUB_EOF +# Ubuntu's signed GRUB reads $prefix/grub.cfg (/EFI/ubuntu) before the copy +# next to itself, so both must carry the same menu. +sudo mkdir -p "${MOUNT_POINT}/EFI/ubuntu" +sudo cp "${MOUNT_POINT}/EFI/BOOT/grub.cfg" "${MOUNT_POINT}/EFI/ubuntu/grub.cfg" sync echo "AIENOS standalone recovery media build complete on ${TARGET_DEV}." diff --git a/scripts/build_standalone_recovery_initrd.sh b/scripts/build_standalone_recovery_initrd.sh index e4d4c402..c9535ae7 100755 --- a/scripts/build_standalone_recovery_initrd.sh +++ b/scripts/build_standalone_recovery_initrd.sh @@ -23,7 +23,7 @@ BINARIES=( /bin/sh /bin/bash /bin/lsblk - /bin/blkid + /sbin/blkid /bin/mount /bin/umount /bin/mkdir @@ -49,6 +49,9 @@ for bin in "${BINARIES[@]}"; do fi done fi + else + echo "Error: required recovery binary $bin not found on the build host" >&2 + exit 1 fi done @@ -66,6 +69,46 @@ if [[ -f "$GOCRYPTFS_BIN" ]]; then fi fi +# Kernel drivers the recovery shell needs on Machine 1 that the Ubuntu kernel +# builds as modules: the ACPI xHCI controllers (NVDA8000), USB and Logitech +# keyboards, USB sticks, the internal NVMe, dm-crypt and FAT text encodings. +# Modules must match the kernel the callers copy: the newest /boot/vmlinuz-*. +KVER="${AIENOS_RECOVERY_KVER:-$(basename "$(ls -1t /boot/vmlinuz-* | head -n 1)" | sed 's/^vmlinuz-//')}" +RECOVERY_MODULES=( + xhci-plat-hcd + usbhid hid-generic hid-logitech-dj hid-logitech-hidpp + usb-storage uas + nvme + dm-crypt + nls_iso8859-1 nls_utf8 +) + +mkdir -p "$WORK_DIR/lib/modules/aienos" "$WORK_DIR/etc" +: > "$WORK_DIR/etc/aienos-modules.order" +for mod in "${RECOVERY_MODULES[@]}"; do + deps=$(modprobe -S "$KVER" --show-depends "$mod") || { + echo "Error: module $mod not found for kernel $KVER" >&2 + exit 1 + } + # Dependencies come first; builtin lines need nothing copied. + while read -r kind path _; do + [[ "$kind" == "insmod" ]] || continue + name=$(basename "$path") + name="${name%.zst}" + name="${name%.xz}" + grep -qxF "$name" "$WORK_DIR/etc/aienos-modules.order" && continue + # Decompress so busybox insmod can load it; the signature appended + # before compression is kept, so Secure Boot lockdown still accepts it. + case "$path" in + *.zst) zstd -q -d -c "$path" > "$WORK_DIR/lib/modules/aienos/$name" ;; + *.xz) xz -d -c "$path" > "$WORK_DIR/lib/modules/aienos/$name" ;; + *) cp -p "$path" "$WORK_DIR/lib/modules/aienos/$name" ;; + esac + echo "$name" >> "$WORK_DIR/etc/aienos-modules.order" + done <<< "$deps" +done +echo "Packaged $(wc -l < "$WORK_DIR/etc/aienos-modules.order") kernel modules for $KVER" + # Create standalone init script cat << 'INIT_EOF' > "$WORK_DIR/init" #!/bin/busybox sh @@ -77,23 +120,116 @@ cat << 'INIT_EOF' > "$WORK_DIR/init" mount -t proc proc /proc mount -t sysfs sysfs /sys mount -t devtmpfs devtmpfs /dev +mount -t securityfs securityfs /sys/kernel/security 2>/dev/null +# Read-only: recovery must opt in explicitly before changing firmware variables. +mount -t efivarfs -o ro efivarfs /sys/firmware/efi/efivars 2>/dev/null + +secure_boot="unknown (no EFI variables)" +sb_var=/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c +if [ -r "$sb_var" ]; then + # 4 attribute bytes, then 1 data byte. busybox od ignores -j with -N, + # so read the last byte with tail. + case "$(tail -c1 "$sb_var" | od -An -tu1 | tr -d ' ')" in + 1) secure_boot="enabled" ;; + 0) secure_boot="disabled" ;; + *) secure_boot="unreadable" ;; + esac +fi +lockdown=$(cat /sys/kernel/security/lockdown 2>/dev/null || echo "unavailable") + +loaded=0 +failed="" +while read -r mod; do + if insmod "/lib/modules/aienos/$mod" 2>/dev/null; then + loaded=$((loaded + 1)) + else + failed="$failed $mod" + fi +done < /etc/aienos-modules.order +total=$(wc -l < /etc/aienos-modules.order) +# Give USB and NVMe time to enumerate before listing disks. +sleep 3 + +{ + echo "============================================================" + echo "AIENOS Standalone Hardware Recovery Core" + echo "Root filesystem: 100% RAM disk (Self-contained)" + echo "Internal NVMe status: UNMOUNTED" + echo "============================================================" + echo "" + echo "Secure Boot: $secure_boot" + echo "Kernel lockdown: $lockdown" + echo "Kernel modules loaded: $loaded/$total" + [ -n "$failed" ] && echo "Kernel modules failed:$failed" + echo "Input devices: $(ls /sys/class/input 2>/dev/null | grep -c '^input')" + cat /sys/class/input/input*/name 2>/dev/null | sed 's/^/ /' + echo "Kernel: $(uname -r) Clock (UTC): $(date -u +%Y-%m-%dT%H:%M:%SZ)" + for pcr in 0 7; do + echo "TPM PCR $pcr (sha256): $(cat /sys/class/tpm/tpm0/pcr-sha256/$pcr 2>/dev/null || echo unavailable)" + done + echo "" + echo "Block device topology:" + lsblk 2>/dev/null || true + for part in /dev/sd*[0-9] /dev/nvme*n*p*; do + [ -b "$part" ] && echo " $part: $(blkid -p -o export -s LABEL -s TYPE -s UUID "$part" 2>/dev/null | tr "\n" " ")" + done + echo "" + echo "Firmware boot entries:" + efibootmgr 2>/dev/null | sed 's/^/ /' || echo " unavailable" + echo "" +} > /tmp/recovery-report 2>&1 + +# One-shot unattended self-test: Linux leaves aienos-selftest-once on the +# stick. Only then is the stick mounted read-write, the flag removed and the +# report saved; an ordinary recovery boot never writes anywhere. +selftest=0 +# Probe partitions directly: without udev, blkid -L and lsblk see no labels. +stick="" +for part in /dev/sd*[0-9] /dev/nvme*n*p*; do + [ -b "$part" ] || continue + if [ "$(blkid -p -o value -s LABEL "$part" 2>/dev/null)" = AIENOSRECOV ]; then + stick="$part" + break + fi +done +if [ -n "$stick" ]; then + mkdir -p /mnt/stick + if mount -t vfat -o ro "$stick" /mnt/stick 2>/dev/null; then + if [ -f /mnt/stick/aienos-selftest-once ] && mount -o remount,rw /mnt/stick 2>/dev/null; then + selftest=1 + rm -f /mnt/stick/aienos-selftest-once + mkdir -p /mnt/stick/aienos-evidence + cp /tmp/recovery-report "/mnt/stick/aienos-evidence/selftest-$(date -u +%Y%m%dT%H%M%SZ).txt" + sync + fi + umount /mnt/stick + fi +fi clear -echo "============================================================" -echo "AIENOS Standalone Hardware Recovery Core" -echo "Root filesystem: 100% RAM disk (Self-contained)" -echo "Internal NVMe status: UNMOUNTED" -echo "============================================================" -echo "" -echo "Block device topology:" -lsblk -f 2>/dev/null || blkid 2>/dev/null || true -echo "" +cat /tmp/recovery-report +# The shell lives on /dev/console (the screen on Machine 1). Mirror the report +# to the serial port too, so emulator tests can read it; bounded so a serial +# port with no carrier can never delay the shell. +if [ -c /dev/ttyAMA0 ]; then + timeout 2 sh -c 'cat /tmp/recovery-report > /dev/ttyAMA0' 2>/dev/null & +fi if grep -q "aienos.test=1" /proc/cmdline 2>/dev/null; then echo "TEST MODE DETECTED: automated recovery verification complete." poweroff -f 2>/dev/null || reboot -f 2>/dev/null || exit 0 fi +if [ "$selftest" = 1 ]; then + echo "SELF-TEST: report saved to the recovery stick (aienos-evidence/)." + echo "Press Enter within 60 s to stay in the recovery shell; otherwise the machine restarts." + if ! read -t 60 _; then + echo "SELF-TEST: restarting." + sync + reboot -f + fi +fi + echo "Dropping into standalone maintenance shell." echo "Type 'reboot' or 'poweroff' when recovery verification completes." echo "" diff --git a/scripts/qemu_verify_recovery_media.sh b/scripts/qemu_verify_recovery_media.sh index 3ac54a07..d3189878 100755 --- a/scripts/qemu_verify_recovery_media.sh +++ b/scripts/qemu_verify_recovery_media.sh @@ -78,3 +78,61 @@ if [[ "${failed}" != 0 ]]; then fi echo "RECOVERY_ZERO_DISK: PASS (verified standalone RAM recovery with zero attached storage)" + +# Second boot: the packaged drivers must find a USB stick, an NVMe drive and a +# USB keyboard. The disks are blank scratch images, never host storage. +DEV_LOG="${WORK_DIR}/recovery_devices.log" +truncate -s 64M "${WORK_DIR}/usb.img" "${WORK_DIR}/nvme.img" +ACCEL=(-cpu max) +DEV_TIMEOUT=120 +if [[ -w /dev/kvm ]]; then + ACCEL=(-accel kvm -cpu host) + DEV_TIMEOUT=60 +fi + +echo "Booting recovery kernel with a scratch USB stick, NVMe drive and USB keyboard..." +set +e +timeout "${DEV_TIMEOUT}" qemu-system-aarch64 \ + -M virt "${ACCEL[@]}" -smp 2 -m 1024 \ + -kernel "${KERNEL_IMAGE}" \ + -initrd "${INITRD_TMP}" \ + -append "rdinit=/init aienos.test=1 console=ttyAMA0 panic=1" \ + -device qemu-xhci -device usb-kbd \ + -drive if=none,id=usbstick,format=raw,file="${WORK_DIR}/usb.img" -device usb-storage,drive=usbstick \ + -drive if=none,id=nvme0,format=raw,file="${WORK_DIR}/nvme.img" -device nvme,drive=nvme0,serial=aienos-test \ + -display none -nic none \ + -serial file:"${DEV_LOG}" -no-reboot +DEV_STATUS=$? +set -e + +tr -d '\r' <"${DEV_LOG}" >"${WORK_DIR}/recovery_devices.txt" +echo "qemu exit ${DEV_STATUS}" + +failed=0 +check_devices() { + if grep -qE -- "$2" "${WORK_DIR}/recovery_devices.txt"; then + echo "PASS $1" + else + echo "FAIL $1" + failed=1 + fi +} +modules_line=$(grep -m1 'Kernel modules loaded:' "${WORK_DIR}/recovery_devices.txt" || true) +if [[ "${modules_line}" =~ loaded:\ ([0-9]+)/([0-9]+) ]] && [[ "${BASH_REMATCH[1]}" == "${BASH_REMATCH[2]}" ]] && [[ "${BASH_REMATCH[2]}" -gt 0 ]]; then + echo "PASS All packaged kernel modules loaded (${BASH_REMATCH[1]}/${BASH_REMATCH[2]})" +else + echo "FAIL All packaged kernel modules loaded (${modules_line:-no report})" + failed=1 +fi +check_devices "USB keyboard detected" "Input devices: [1-9]" +check_devices "USB stick visible" "^sda" +check_devices "NVMe drive visible" "^nvme0n1" + +if [[ "${failed}" != 0 ]]; then + echo "---- recovery device console log ----" + cat "${WORK_DIR}/recovery_devices.txt" + echo "RECOVERY_DEVICES: FAIL" + exit 1 +fi + +echo "RECOVERY_DEVICES: PASS (packaged drivers found USB keyboard, USB stick and NVMe)"