#!/usr/bin/env bash # Validate the Red Bear OS Phase 3 runtime substrate. # # Modes: # --guest Run inside a Red Bear OS guest # --qemu [CONFIG] Boot CONFIG in QEMU and run the same checks automatically set -euo pipefail find_uefi_firmware() { local candidates=( "/usr/share/ovmf/x64/OVMF.4m.fd" "/usr/share/OVMF/x64/OVMF.4m.fd" "/usr/share/ovmf/x64/OVMF_CODE.4m.fd" "/usr/share/OVMF/x64/OVMF_CODE.4m.fd" "/usr/share/ovmf/OVMF.fd" "/usr/share/OVMF/OVMF_CODE.fd" "/usr/share/qemu/edk2-x86_64-code.fd" ) local path for path in "${candidates[@]}"; do if [[ -f "$path" ]]; then printf '%s\n' "$path" return 0 fi done return 1 } run_guest_checks() { echo "=== Red Bear OS Phase 3 Runtime Substrate Test ===" echo require_path() { local path="$1" local message="$2" if [ -e "$path" ]; then echo "✅ $message" else echo "❌ $message" exit 1 fi } require_command() { local cmd="$1" local message="$2" if command -v "$cmd" >/dev/null 2>&1; then echo "✅ $message" else echo "❌ $message" exit 1 fi } echo "=== Runtime commands ===" require_command redbear-info "redbear-info is installed" require_command udev-shim "udev-shim command is installed" require_command evdevd "evdevd command is installed" require_command redbear-evtest "evdev consumer test tool is installed" require_command redbear-input-inject "input injector test tool is installed" require_command redbear-phase3-input-check "phase 3 guest input check script is installed" echo echo "=== Scheme surfaces ===" require_path /scheme/pci "PCI scheme is available" echo echo "=== redbear-info --json ===" local report report="$(redbear-info --json)" printf '%s\n' "$report" case "$report" in *'scheme firmware is registered in /scheme'*) echo "✅ firmware scheme reported" ;; *) echo "❌ firmware scheme not reported"; exit 1 ;; esac case "$report" in *'scheme udev is registered in /scheme'*) echo "✅ udev scheme reported" ;; *) echo "❌ udev scheme not reported"; exit 1 ;; esac case "$report" in *'"name": "evdevd"'*'"state": "active"'*) echo "✅ evdevd reported active" ;; *) echo "❌ evdevd not reported active"; exit 1 ;; esac echo echo "=== Phase 3 input validation ===" echo "Run 'redbear-phase3-input-check' to prove the evdev consumer path." echo echo "=== Test Complete ===" } run_qemu_checks() { local config="$1" local firmware firmware="$(find_uefi_firmware)" || { echo "ERROR: no usable x86_64 UEFI firmware found" >&2 exit 1 } local arch image iso extra arch="${ARCH:-$(uname -m)}" image="build/$arch/$config/harddrive.img" iso="build/$arch/$config.iso" extra="build/$arch/$config/extra.img" local boot_media boot_args boot_media="" if [[ -f "$iso" ]]; then boot_media="iso" elif [[ -f "$image" ]]; then boot_media="harddrive" else echo "ERROR: no bootable image found ($iso or $image)" >&2 echo "Build it first with: ./local/scripts/build-redbear.sh $config" >&2 exit 1 fi if [[ "$boot_media" == "iso" ]]; then boot_args=(-cdrom "$iso") else boot_args=(-drive file="$image",format=raw,if=none,id=drv0,snapshot=on -device nvme,drive=drv0,serial=NVME_SERIAL) fi if [[ ! -f "$extra" ]]; then truncate -s 1g "$extra" fi log="build/$arch/$config/phase3-check.log" attempt=1 while true; do rc=0 python3 local/scripts/qemu-login-expect.py \ --timeout 300 \ --log "$log" \ --step "expect:login:" \ --step "send:root" \ --step "expect:assword:" \ --step "send:password" \ --step "expect:Type 'help' for available commands." \ --step "send:echo __READY__" \ --step "expect:__READY__" \ --step "send:redbear-phase3-input-check; echo __EVTEST_DONE__" \ --step "expect:Injected synthetic key event: A" \ --step "send:redbear-info --json" \ --step "expect:\"virtio_net_present\": true" \ --step "expect:scheme firmware is registered" \ --step "expect:scheme udev is registered" \ --step "expect:\"name\": \"evdevd\"" \ --step "expect:\"state\": \"active\"" \ --step "expect:EV_KEY code=" \ --step "expect:__EVTEST_DONE__" \ --step "send:shutdown" \ --pass_marker "__EVTEST_DONE__" \ -- \ qemu-system-x86_64 -name "Red Bear OS x86_64" -device qemu-xhci -smp 4 -m 2048 -bios "$firmware" -chardev stdio,id=debug,signal=off,mux=on -serial chardev:debug -mon chardev:debug -machine q35 -device ich9-intel-hda -device hda-output -device virtio-net,netdev=net0 -netdev user,id=net0 -object filter-dump,id=f1,netdev=net0,file="build/$arch/$config/network.pcap" -display none -vga none "${boot_args[@]}" -drive file="$extra",format=raw,if=none,id=drv1 -device nvme,drive=drv1,serial=NVME_EXTRA -enable-kvm -cpu host || rc=$? if [[ $rc -eq 0 ]]; then break fi if [[ ( $rc -eq 3 || $rc -eq 137 ) && $attempt -lt 5 ]]; then attempt=$((attempt + 1)) echo "QEMU exited before completing the check; retrying (attempt $attempt/5)" sleep 2 continue fi exit "$rc" done } usage() { cat <<'USAGE' Usage: ./local/scripts/test-phase3-runtime-substrate.sh --guest ./local/scripts/test-phase3-runtime-substrate.sh --qemu [redbear-full] USAGE } case "${1:-}" in --guest) run_guest_checks ;; --qemu) run_qemu_checks "${2:-redbear-full}" ;; *) usage exit 1 ;; esac