milestone: USB — enhanced checker + unified runtime harness

redbear-usb-check: rewritten from 99-line minimal checker to full
Phase-pattern validation (CheckResult/Report, JSON output, proper
cfg-gating). Checks xHCI controllers, USB device enumeration,
HID class detection, storage class detection.

test-usb-runtime.sh: guest + QEMU harness following Phase 1-5 pattern.

Zero warnings.
This commit is contained in:
2026-04-29 12:11:13 +01:00
parent 3623a28f54
commit 2c937ea006
2 changed files with 251 additions and 69 deletions
@@ -1,98 +1,206 @@
use std::fs;
// USB subsystem runtime validation check.
// Validates USB host controllers, device enumeration, topology, and class detection.
use std::process;
use redbear_hwutils::parse_args;
const PROGRAM: &str = "redbear-usb-check";
const USAGE: &str = "Usage: redbear-usb-check\n\nCheck the USB stack inside a Red Bear guest.\n\nWalks the usb scheme tree and reports controller and device status.";
const USAGE: &str = "Usage: redbear-usb-check [--json]\n\n\
USB subsystem runtime check. Validates xHCI controller registration,\n\
USB device enumeration, and class detection (HID, storage, hub).";
fn list_scheme_dir(path: &str) -> Vec<String> {
#[cfg(target_os = "redox")]
use std::fs;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CheckResult { Pass, Fail, Skip }
impl CheckResult {
fn label(self) -> &'static str {
match self { Self::Pass => "PASS", Self::Fail => "FAIL", Self::Skip => "SKIP" }
}
}
struct Check { name: String, result: CheckResult, detail: String }
impl Check {
fn pass(name: &str, detail: &str) -> Self {
Check { name: name.to_string(), result: CheckResult::Pass, detail: detail.to_string() }
}
fn fail(name: &str, detail: &str) -> Self {
Check { name: name.to_string(), result: CheckResult::Fail, detail: detail.to_string() }
}
fn skip(name: &str, detail: &str) -> Self {
Check { name: name.to_string(), result: CheckResult::Skip, detail: detail.to_string() }
}
}
struct Report { checks: Vec<Check>, json_mode: bool }
impl Report {
fn new(json_mode: bool) -> Self { Report { checks: Vec::new(), json_mode } }
fn add(&mut self, check: Check) { self.checks.push(check); }
fn any_failed(&self) -> bool { self.checks.iter().any(|c| c.result == CheckResult::Fail) }
fn print(&self) {
if self.json_mode { self.print_json(); } else { self.print_human(); }
}
fn print_human(&self) {
for check in &self.checks {
let icon = match check.result {
CheckResult::Pass => "[PASS]", CheckResult::Fail => "[FAIL]", CheckResult::Skip => "[SKIP]",
};
println!("{icon} {}: {}", check.name, check.detail);
}
}
fn print_json(&self) {
#[derive(serde::Serialize)]
struct JsonCheck { name: String, result: String, detail: String }
#[derive(serde::Serialize)]
struct JsonReport {
xhci_controllers: usize, usb_devices: usize,
hid_devices: usize, storage_devices: usize, checks: Vec<JsonCheck>,
}
let xhci = self.checks.iter().find(|c| c.name == "XHCI_CONTROLLER").map_or(0, |c| {
c.detail.split(' ').next().and_then(|s| s.parse().ok()).unwrap_or(0)
});
let devices = self.checks.iter().find(|c| c.name == "USB_DEVICES").map_or(0, |c| {
c.detail.strip_prefix("found ").and_then(|s| s.split(' ').next()).and_then(|s| s.parse().ok()).unwrap_or(0)
});
let hid = self.checks.iter().find(|c| c.name == "USB_HID").map_or(0, |c| {
c.detail.strip_prefix("found ").and_then(|s| s.split(' ').next()).and_then(|s| s.parse().ok()).unwrap_or(0)
});
let storage = self.checks.iter().find(|c| c.name == "USB_STORAGE").map_or(0, |c| {
c.detail.strip_prefix("found ").and_then(|s| s.split(' ').next()).and_then(|s| s.parse().ok()).unwrap_or(0)
});
let checks: Vec<JsonCheck> = self.checks.iter().map(|c| JsonCheck {
name: c.name.clone(), result: c.result.label().to_string(), detail: c.detail.clone(),
}).collect();
if let Err(err) = serde_json::to_writer(std::io::stdout(), &JsonReport { xhci_controllers: xhci, usb_devices: devices, hid_devices: hid, storage_devices: storage, checks }) {
eprintln!("{PROGRAM}: failed to serialize JSON: {err}");
}
}
}
#[cfg(target_os = "redox")]
fn parse_args() -> Result<bool, String> {
let mut json_mode = false;
for arg in std::env::args().skip(1) {
match arg.as_str() {
"--json" => json_mode = true,
"-h" | "--help" => { println!("{USAGE}"); return Err(String::new()); }
_ => return Err(format!("unsupported argument: {arg}")),
}
}
Ok(json_mode)
}
#[cfg(target_os = "redox")]
fn list_dir(path: &str) -> Vec<String> {
match fs::read_dir(path) {
Ok(entries) => entries
.filter_map(|e| e.ok())
.filter_map(|e| e.file_name().to_str().map(|s| s.to_string()))
.collect(),
Ok(entries) => entries.filter_map(|e| e.ok()).filter_map(|e| e.file_name().to_str().map(|s| s.to_string())).collect(),
Err(_) => Vec::new(),
}
}
fn run() -> Result<(), String> {
parse_args(PROGRAM, USAGE, std::env::args()).map_err(|err| {
if err.is_empty() {
process::exit(0);
}
err
})?;
let mut failures = 0;
let usb_entries = list_scheme_dir("/scheme/usb");
if usb_entries.is_empty() {
eprintln!("{PROGRAM}: FAIL: no usb scheme entries found");
failures += 1;
#[cfg(target_os = "redox")]
fn check_xhci_controller() -> Check {
let xhci = list_dir("/scheme/xhci");
if !xhci.is_empty() {
Check::pass("XHCI_CONTROLLER", &format!("{} xHCI controller(s): {}", xhci.len(), xhci.join(", ")))
} else {
println!(
"{PROGRAM}: found {} usb scheme entries: {:?}",
usb_entries.len(),
usb_entries
);
Check::fail("XHCI_CONTROLLER", "no xHCI controllers found under /scheme/xhci")
}
}
for entry in &usb_entries {
let scheme_path = format!("/scheme/usb/{}", entry);
let sub = list_scheme_dir(&scheme_path);
println!("{PROGRAM}: {} -> {:?} ports", entry, sub.len());
#[cfg(target_os = "redox")]
fn check_usb_devices() -> Check {
let entries = list_dir("/scheme/usb");
if entries.is_empty() {
return Check::fail("USB_DEVICES", "no USB devices found under /scheme/usb");
}
let mut total = 0usize;
for entry in &entries {
let port_path = format!("/scheme/usb/{}", entry);
let ports = list_dir(&port_path);
total += ports.len();
}
if total > 0 {
Check::pass("USB_DEVICES", &format!("found {} device(s) across {} hub(s)", total, entries.len()))
} else {
Check::skip("USB_DEVICES", "USB hubs present but no devices attached")
}
}
for port in &sub {
let port_path = format!("{}/{}/descriptors", scheme_path, port);
if let Ok(data) = fs::read_to_string(&port_path) {
if let Ok(dev_desc) = serde_json::from_str::<serde_json::Value>(&data) {
let vendor = dev_desc
.get("vendor")
.and_then(|v| v.as_u64())
.map(|v| format!("{:04x}", v))
.unwrap_or_else(|| "????".to_string());
let product = dev_desc
.get("product")
.and_then(|v| v.as_u64())
.map(|v| format!("{:04x}", v))
.unwrap_or_else(|| "????".to_string());
let ss = dev_desc
.get("supports_superspeed")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let product_str = dev_desc
.get("product_str")
.and_then(|v| v.as_str())
.unwrap_or("");
let speed_tag = if ss { " [SS]" } else { "" };
println!(
"{PROGRAM}: port {} -> {}:{} {}{}",
port, vendor, product, product_str, speed_tag
);
}
#[cfg(target_os = "redox")]
fn check_usb_hid() -> Check {
let mut hid_count = 0usize;
let entries = list_dir("/scheme/usb");
for entry in &entries {
let port_path = format!("/scheme/usb/{}", entry);
for port in list_dir(&port_path) {
let desc_path = format!("{}/{}/descriptors", port_path, port);
if let Ok(data) = fs::read_to_string(&desc_path) {
if let Ok(desc) = serde_json::from_str::<serde_json::Value>(&data) {
let class = desc.get("class").and_then(|v| v.as_u64()).unwrap_or(0);
if class == 3 { hid_count += 1; }
}
}
}
}
let xhci_entries = list_scheme_dir("/scheme/xhci");
if xhci_entries.is_empty() {
eprintln!("{PROGRAM}: FAIL: no xhci scheme entries found");
failures += 1;
if hid_count > 0 {
Check::pass("USB_HID", &format!("found {} HID device(s)", hid_count))
} else {
println!("{PROGRAM}: xhci controllers: {:?}", xhci_entries);
Check::skip("USB_HID", "no USB HID devices found (may need physical hardware)")
}
}
if failures > 0 {
Err(format!("{} check(s) failed", failures))
#[cfg(target_os = "redox")]
fn check_usb_storage() -> Check {
let mut storage_count = 0usize;
let entries = list_dir("/scheme/usb");
for entry in &entries {
let port_path = format!("/scheme/usb/{}", entry);
for port in list_dir(&port_path) {
let desc_path = format!("{}/{}/descriptors", port_path, port);
if let Ok(data) = fs::read_to_string(&desc_path) {
if let Ok(desc) = serde_json::from_str::<serde_json::Value>(&data) {
let class = desc.get("class").and_then(|v| v.as_u64()).unwrap_or(0);
if class == 8 { storage_count += 1; }
}
}
}
}
if storage_count > 0 {
Check::pass("USB_STORAGE", &format!("found {} storage device(s)", storage_count))
} else {
println!("{PROGRAM}: all checks passed");
Check::skip("USB_STORAGE", "no USB storage devices found (may need physical hardware)")
}
}
fn run() -> Result<(), String> {
#[cfg(not(target_os = "redox"))]
{
if std::env::args().any(|a| a == "-h" || a == "--help") { println!("{USAGE}"); return Err(String::new()); }
println!("{PROGRAM}: USB check requires Redox runtime");
return Ok(());
}
#[cfg(target_os = "redox")]
{
let json_mode = parse_args()?;
let mut report = Report::new(json_mode);
report.add(check_xhci_controller());
report.add(check_usb_devices());
report.add(check_usb_hid());
report.add(check_usb_storage());
report.print();
if report.any_failed() { return Err("one or more USB checks failed".to_string()); }
Ok(())
}
}
fn main() {
if let Err(err) = run() {
if err.is_empty() { process::exit(0); }
eprintln!("{PROGRAM}: {err}");
process::exit(1);
}
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
# USB maturity runtime validation harness.
# Follows Phase 1-5 pattern: guest + QEMU, exit-code-based.
set -euo pipefail
PROG="$(basename "$0")"
usage() {
cat <<'EOF'
Usage: test-usb-runtime.sh [--guest|--qemu CONFIG]
Exit: 0 if all pass, 1 otherwise.
EOF
exit 1
}
MODE=""; CONFIG=""
while [[ $# -gt 0 ]]; do
case "$1" in
--guest) MODE="guest"; shift ;;
--qemu) MODE="qemu"; CONFIG="$2"; shift 2 ;;
-h|--help) usage ;;
*) echo "$PROG: unknown: $1"; usage ;;
esac
done
[[ -z "$MODE" ]] && usage
run_guest_checks() {
local failures=0
run_check() {
local name="$1" cmd="$2" desc="$3"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo " FAIL $name: $cmd not found ($desc)"
failures=$((failures + 1)); return 0
fi
echo " Running $name..."
if "$cmd" --json >/dev/null 2>&1; then
echo " PASS $name: $desc"
else
echo " FAIL $name: $desc (exit non-zero)"
failures=$((failures + 1))
fi
}
echo "=== USB Maturity Validation ==="; echo
run_check "USB" "redbear-usb-check" "xHCI controller + device enumeration + class detection"
echo
echo "=== USB Summary ==="
if [[ $failures -eq 0 ]]; then echo "ALL USB CHECKS PASSED"; else echo "FAILURES: $failures"; exit 1; fi; exit 0
}
run_qemu_checks() {
local arch="${ARCH:-x86_64}"
local image="build/${arch}/${CONFIG}/harddrive.img"
local firmware="${FIRMWARE_PATH:-/usr/share/ovmf/x64/OVMF.fd}"
if [[ ! -f "$image" ]]; then echo "$PROG: image not found: $image"; exit 1; fi
if [[ ! -f "$firmware" ]]; then echo "$PROG: firmware not found: $firmware"; exit 1; fi
expect <<EXPECT_SCRIPT
log_user 1; set timeout 300
spawn qemu-system-x86_64 -name {Red Bear OS} -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 virtio-net,netdev=net0 -netdev user,id=net0 -nographic -vga none -drive file=$image,format=raw,if=none,id=drv0 -device nvme,drive=drv0,serial=NVME_SERIAL -enable-kvm -cpu host
expect "login:"; send "root\r"
expect "assword:"; send "password\r"
expect "Type 'help' for available commands."
send "echo __READY__\r"; expect "__READY__"
send "redbear-usb-check --json >/dev/null 2>&1 && echo __USB_OK__ || echo __USB_FAIL__\r"
expect { "__USB_OK__" { } "__USB_FAIL__" { puts "FAIL: usb"; exit 1 } timeout { puts "FAIL: timeout"; exit 1 } eof { puts "FAIL: eof"; exit 1 } }
puts "ALL USB CHECKS PASSED"
EXPECT_SCRIPT
exit $?
}
case "$MODE" in
guest) run_guest_checks ;;
qemu) export FIRMWARE_PATH="${FIRMWARE_PATH:-/usr/share/ovmf/x64/OVMF.fd}"; run_qemu_checks ;;
*) usage ;;
esac