Files
RedBear-OS/local/scripts/test-usb-qemu.sh
T
vasilito 6dab9b2953 tests: migrate remaining QEMU runtime proofs to qemu-login-expect.py
Convert the remaining expect-based test scripts (phase1-6 runtime, usb,
wifi, bluetooth, dbus, greeter, live-iso, posix) to the stdlib python
harness, completing the migration started in 9ec00d4c81. Each script now
prefers booting the live ISO when present (falling back to harddrive.img
with snapshot=on), and retries up to 5 times when QEMU exits early or is
killed (rc 3 / 137) — the host-contention failure mode recorded in the
2026-07-20 runtime-proof status.

qemu-login-expect.py: treat an incomplete step sequence as a failure even
when a pass marker was seen partway through — partial sequences must not
count as a pass.
2026-07-20 09:01:14 +09:00

251 lines
7.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Full USB stack validation harness for Red Bear OS in QEMU.
#
# Boots a Red Bear image with xHCI, USB keyboard, USB tablet, and USB mass storage
# attached, then checks boot logs for successful USB device enumeration and driver spawn.
set -euo pipefail
seed_usb_image() {
local image_path="$1"
python3 - "$image_path" <<'PY'
import base64
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
payload = (b"REDBEAR-USB-FULL-STACK-CHECK\0" * 32)[:512]
payload = payload.ljust(512, b'\0')
with path.open("r+b") as fh:
fh.seek(0)
fh.write(payload)
print(base64.b64encode(payload).decode("ascii"))
PY
}
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
}
usage() {
cat <<'USAGE'
Usage: test-usb-qemu.sh [--check] [config]
Boot or validate the full USB stack on a Red Bear image in QEMU.
Defaults to redbear-mini (mapped to the in-tree redbear-mini image).
Checks performed:
1. xHCI controller initializes and reports interrupt mode
2. USB HID driver spawns for keyboard/tablet
3. USB SCSI driver spawns for mass storage
4. USB storage sector-0 readback matches a seeded host pattern
5. BOS descriptor fetched (or gracefully skipped for USB 2)
6. No panics or crash-class errors in USB daemons
USAGE
}
check_mode=0
config="redbear-mini"
for arg in "$@"; do
case "$arg" in
--help|-h|help)
usage
exit 0
;;
--check)
check_mode=1
;;
redbear-*)
config="$arg"
;;
esac
done
if [[ "$config" == "redbear-mini" ]]; then
config="redbear-mini"
fi
firmware="$(find_uefi_firmware)" || {
echo "ERROR: no usable x86_64 UEFI firmware found" >&2
exit 1
}
arch="${ARCH:-$(uname -m)}"
iso="build/$arch/$config.iso"
image="build/$arch/$config/harddrive.img"
extra="build/$arch/$config/extra.img"
usb_img="build/$arch/$config/usb-test-storage.img"
log_file="build/$arch/$config/usb-stack-check.log"
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 [[ ! -f "$extra" ]]; then
truncate -s 1g "$extra"
fi
if [[ ! -f "$usb_img" ]]; then
truncate -s 64M "$usb_img"
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
expected_sector_b64="$(seed_usb_image "$usb_img")"
pkill -f "qemu-system-x86_64.*$image" 2>/dev/null || true
sleep 1
rm -f "$log_file"
attempt=1
while true; do
rc=0
python3 local/scripts/qemu-login-expect.py \
--timeout 300 \
--log "$log_file" \
--step "expect:interrupt delivery" \
--step "expect:USB HID driver spawned" \
--step "expect:USB SCSI driver spawned" \
--step "expect:DISK CONTENT: $expected_sector_b64" \
--step "expect:login:" \
--step "send:root" \
--step "expect:assword:" \
--step "send:password" \
--step "expect:Type 'help' for available commands." \
--step "send:shutdown" \
-- \
qemu-system-x86_64 -name "Red Bear OS USB Test" -device qemu-xhci,id=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 -display none -vga none "${boot_args[@]}" -drive file="$extra",format=raw,if=none,id=drv1,snapshot=on -device nvme,drive=drv1,serial=NVME_EXTRA -drive file="$usb_img",format=raw,if=none,id=usbdisk,snapshot=on -device usb-storage,bus=xhci.0,drive=usbdisk -device usb-kbd,bus=xhci.0 -device usb-tablet,bus=xhci.0 -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
break
done
expect_status="$rc"
pkill -f "qemu-system-x86_64.*$image" 2>/dev/null || true
# Milestone-derivation bridge: the helper logs real guest output. Reconstruct the
# MILESTONE:* markers the original TCL-side log injection emitted so the existing
# grep-based verdict checks below keep working unchanged.
grep -aq "interrupt delivery" "$log_file" && printf 'MILESTONE:XHCI_IRQ\n' >> "$log_file"
grep -aq "USB HID driver spawned" "$log_file" && printf 'MILESTONE:USB_HID\n' >> "$log_file"
grep -aq "USB SCSI driver spawned" "$log_file" && printf 'MILESTONE:USB_SCSI\n' >> "$log_file"
grep -aq "DISK CONTENT:" "$log_file" && printf 'MILESTONE:USB_READBACK\n' >> "$log_file"
grep -aq "Type 'help' for available commands." "$log_file" && printf 'MILESTONE:SHELL\n' >> "$log_file"
failures=0
echo "--- USB Stack Validation: $config ---"
# Check 1: xHCI interrupt mode
if grep -aq "MILESTONE:XHCI_IRQ" "$log_file"; then
echo " [PASS] xHCI interrupt-driven mode detected"
else
echo " [FAIL] xHCI did not report interrupt-driven mode" >&2
failures=$((failures + 1))
fi
# Check 2: USB HID driver spawn
if grep -aq "MILESTONE:USB_HID" "$log_file"; then
echo " [PASS] USB HID driver spawned"
else
echo " [FAIL] USB HID driver did not spawn" >&2
failures=$((failures + 1))
fi
# Check 3: USB SCSI driver spawn + bounded data readback
if grep -aq "MILESTONE:USB_SCSI" "$log_file"; then
echo " [PASS] USB SCSI driver spawned"
else
echo " [FAIL] USB SCSI driver did not spawn" >&2
failures=$((failures + 1))
fi
if grep -aq "MILESTONE:USB_READBACK" "$log_file"; then
echo " [PASS] USB storage sector readback matched seeded pattern"
else
echo " [FAIL] USB storage sector readback milestone was never reached" >&2
failures=$((failures + 1))
fi
# Check 4: BOS descriptor handling (info or debug log)
if grep -aq "BOS:" "$log_file"; then
echo " [PASS] BOS descriptor processing active"
elif grep -aq "BOS descriptor not available" "$log_file"; then
echo " [PASS] BOS descriptor gracefully skipped (USB 2 device)"
else
echo " [WARN] No BOS descriptor log output found"
fi
# Check 5: No panics or crash-class errors (stall recovery messages are expected)
if grep -aqi "panic\|usbscsid: .*IO ERROR\|usbscsid: startup failed\|usbhidd: .*IO ERROR" "$log_file"; then
echo " [FAIL] USB stack hit crash-class errors" >&2
failures=$((failures + 1))
else
echo " [PASS] No crash-class errors detected"
fi
# Check 6: Boot progressed far enough to reach the guest shell
if grep -aq "MILESTONE:SHELL" "$log_file"; then
echo " [PASS] Full USB stack boot reached guest shell"
else
echo " [FAIL] Full USB stack run never reached guest shell" >&2
failures=$((failures + 1))
fi
# Check 7: Hub driver (if hub detected)
if grep -aq "USB HUB driver spawned" "$log_file"; then
echo " [PASS] USB hub driver spawned"
else
echo " [INFO] No hub driver spawn (expected for direct-attached devices)"
fi
if [[ "$expect_status" -ne 0 ]]; then
echo " [INFO] expect exited with status $expect_status; milestone checks above determine pass/fail"
fi
echo "--- Results: $failures failure(s), log: $log_file ---"
if [[ "$failures" -gt 0 ]]; then
exit 1
fi
exit 0