From 82e5594da4009f4dae6b00cfe7bd28a54a3dfc17 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 13 Jun 2024 00:37:07 +0800 Subject: [PATCH] add auto-trng feature to the USB stack for compact/quick testing of TRNG in a harness --- services/usb-device-xous/Cargo.toml | 1 + services/usb-device-xous/src/main_hw.rs | 52 ++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index e2c28c8b8..88909ee8c 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -71,6 +71,7 @@ cramium-soc = [ ] minimal = ["mass-storage"] mass-storage = ["usbd_scsi", "usbd_mass_storage"] +auto-trng = [] mjolnir = [ ] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care. default = [] diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 10058e5d9..0942f8545 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -10,6 +10,7 @@ use cram_hal_service::api::KeyMap; use cram_hal_service::keyboard; #[cfg(feature = "cramium-soc")] use cram_hal_service::trng; +use cram_hal_service::trng::api::TrngTestMode; #[cfg(feature = "cramium-soc")] use cramium_hal::usb::driver::CorigineUsb; #[cfg(feature = "cramium-soc")] @@ -401,7 +402,10 @@ pub(crate) fn main_hw() -> ! { let mut serial_listen_mode: SerialListenMode = SerialListenMode::NoListener; let mut serial_buf = Vec::::new(); let mut serial_rx_trigger = false; // when true, the condition was met to pass data to the listener (but the listener was not yet installed) + #[cfg(not(feature = "cramium-soc"))] let trng = trng::Trng::new(&xns).unwrap(); + #[cfg(feature = "cramium-soc")] + let mut trng = trng::Trng::new(&xns).unwrap(); let mut serial_trng_buf = Vec::::new(); let serial_trng_interval = Arc::new(AtomicU32::new(0)); let mut serial_trng_cid: Option = None; @@ -427,10 +431,54 @@ pub(crate) fn main_hw() -> ! { 100, // 100 * 64 bytes = 6.4kb, quite the backlog ); // track which view is visible on the device core - #[cfg(not(feature = "minimal"))] + #[cfg(all(not(feature = "minimal"), not(feature = "cramium-soc")))] let mut view = Views::FidoWithKbd; + #[cfg(feature = "cramium-soc")] - usb_dev.force_reset().ok(); // hard-coded to match the view specifier above + let mut view = Views::Serial; + #[cfg(feature = "cramium-soc")] + serial_device.force_reset().ok(); // hard-coded to match the view specifier above + + #[cfg(feature = "auto-trng")] + std::thread::spawn({ + let conn = cid; + move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + loop { + tt.sleep_ms(2_000).ok(); + let state = match xous::send_message( + conn, + xous::Message::new_blocking_scalar(Opcode::LinkStatus.to_usize().unwrap(), 0, 0, 0, 0), + ) { + Ok(xous::Result::Scalar1(code)) => match code { + 0 => UsbDeviceState::Default, + 1 => UsbDeviceState::Addressed, + 2 => UsbDeviceState::Configured, + 3 => UsbDeviceState::Suspend, + _ => panic!("Internal error: illegal status code"), + }, + _ => panic!("Internal error: illegal return type"), + }; + if state == UsbDeviceState::Configured { + log::info!("Core connected"); + break; + } + } + tt.sleep_ms(3_000).ok(); + log::info!("Starting serial sender"); + xous::send_message( + conn, + xous::Message::new_scalar( + Opcode::SerialHookTrngSender.to_usize().unwrap(), + TrngTestMode::Raw.to_usize().unwrap(), + 0, + 0, + 0, + ), + ) + .unwrap(); + } + }); let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default(); let mut fido_listener: Option = None;