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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user