diff --git a/local/recipes/system/redbear-hwutils/source/src/bin/redbear-usb-check.rs b/local/recipes/system/redbear-hwutils/source/src/bin/redbear-usb-check.rs index 5487d757..355c4504 100644 --- a/local/recipes/system/redbear-hwutils/source/src/bin/redbear-usb-check.rs +++ b/local/recipes/system/redbear-hwutils/source/src/bin/redbear-usb-check.rs @@ -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 { +#[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, 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, + } + 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 = 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 { + 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 { 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::(&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::(&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::(&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); } diff --git a/local/scripts/test-usb-runtime.sh b/local/scripts/test-usb-runtime.sh new file mode 100644 index 00000000..07f0fc74 --- /dev/null +++ b/local/scripts/test-usb-runtime.sh @@ -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 </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