From 54467a75fc3ab7d39fa9c4f9d5ff1b52a85ab7ef Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 12 Jan 2023 00:58:48 +0800 Subject: [PATCH] gate mass-storage behind a feature that is currently default off --- services/shellchat/Cargo.toml | 1 + services/shellchat/src/cmds/usb.rs | 2 ++ services/usb-device-xous/Cargo.toml | 7 +++--- services/usb-device-xous/src/api.rs | 2 ++ services/usb-device-xous/src/main.rs | 2 +- services/usb-device-xous/src/main_hw.rs | 30 +++++++++++++++++-------- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 5b32549e8..69ba6b542 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -123,6 +123,7 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX) test-rekey = [] no-codec = [] nettest = [] # batch network tests +mass-storage = [] tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"] shellperf = ["ring", "perflib", "xous/v2p", "random-pick"] # this also needs the project-wide "perfcounter" feature to be selected. shellchat is the exclusive manager of the performance counter, do not use with e.g. "vaultperf" feature default = [] # "debugprint" diff --git a/services/shellchat/src/cmds/usb.rs b/services/shellchat/src/cmds/usb.rs index 0fa73a83a..2015f44b0 100644 --- a/services/shellchat/src/cmds/usb.rs +++ b/services/shellchat/src/cmds/usb.rs @@ -29,6 +29,7 @@ impl<'a> ShellCmdApi<'a> for Usb { self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::FidoKbd).unwrap(); write!(ret, "USB connected to HID (FIDO + keyboard) core").unwrap(); } + #[cfg(feature="mass-storage")] "ms" => { self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::MassStorage).unwrap(); write!(ret, "USB connected to mass storage core").unwrap(); } @@ -87,6 +88,7 @@ impl<'a> ShellCmdApi<'a> for Usb { _ => write!(ret, "HID not connected to USB host").unwrap(), } } + #[cfg(feature="mass-storage")] Ok(UsbDeviceType::MassStorage) => write!(ret, "USB mass storage connected").unwrap(), _ => write!(ret, "Invalid response checking status").unwrap(), } diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index f0f9efc70..12a9de480 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -29,8 +29,8 @@ usbd-human-interface-device = "0.2.1" embedded-time = "0.12.1" # required by the keyboard interface packed_struct = { version = "0.10", default-features = false } # used by the usbd-human-interface-device crate num_enum = { version = "0.5", default-features = false } # used by the usbd-human-interface-device crate -usbd_scsi = { path = "./stm32-usb.rs/firmware/usbd_scsi" } -usbd_mass_storage = { path = "./stm32-usb.rs/firmware/usbd_mass_storage" } +usbd_scsi = { path = "./stm32-usb.rs/firmware/usbd_scsi", optional = true } +usbd_mass_storage = { path = "./stm32-usb.rs/firmware/usbd_mass_storage", optional = true } ghostfat = "0.5.0" @@ -48,6 +48,7 @@ precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] renode = ["utralib/renode"] chain = [] -minimal = [] +minimal = ["mass-storage"] +mass-storage = ["usbd_scsi", "usbd_mass_storage"] mjolnir = [] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care. default = ["chain"] diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 70f3b849a..534bcdf60 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -69,6 +69,7 @@ pub enum UsbDeviceType { Debug = 0, FidoKbd = 1, Fido = 2, + #[cfg(feature="mass-storage")] MassStorage = 3, } use std::convert::TryFrom; @@ -80,6 +81,7 @@ impl TryFrom for UsbDeviceType { 0 => Ok(UsbDeviceType::Debug), 1 => Ok(UsbDeviceType::FidoKbd), 2 => Ok(UsbDeviceType::Fido), + #[cfg(feature="mass-storage")] 3 => Ok(UsbDeviceType::MassStorage), _ => Err("Invalid UsbDeviceType specifier"), } diff --git a/services/usb-device-xous/src/main.rs b/services/usb-device-xous/src/main.rs index d37dfc436..f15d0c23f 100644 --- a/services/usb-device-xous/src/main.rs +++ b/services/usb-device-xous/src/main.rs @@ -20,7 +20,7 @@ mod spinal_udc; use packed_struct::PackedStructSlice; #[cfg(any(feature="precursor", feature="renode"))] use spinal_udc::*; -#[cfg(any(feature="precursor", feature="renode"))] +#[cfg(all(any(feature="precursor", feature="renode"),feature="mass-storage"))] mod block_device; #[cfg(not(target_os = "xous"))] diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 278be67eb..7181c6e75 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -50,6 +50,7 @@ const EXTENDED_CORE_RESET_MS: usize = 4000; enum Views { FidoWithKbd = 0, FidoOnly = 1, + #[cfg(feature="mass-storage")] MassStorage = 2, } @@ -123,7 +124,7 @@ pub(crate) fn main_hw() -> ! { } } #[cfg(feature="minimal")] - let serial_number = "minimal_build"; + let serial_number = "minimalbuild"; #[cfg(feature="minimal")] { use utralib::generated::*; @@ -146,6 +147,7 @@ pub(crate) fn main_hw() -> ! { let usb_fido_dev = usb_fidokbd_dev.clone_unalloc(); // do the same thing for mass storage + #[cfg(feature="mass-storage")] let ums_dev = usb_fidokbd_dev.clone_unalloc(); // track which view is visible on the device core @@ -195,10 +197,11 @@ pub(crate) fn main_hw() -> ! { .serial_number(&serial_number) .build(); + #[cfg(feature="mass-storage")] let ums_alloc = UsbBusAllocator::new(ums_dev); - + #[cfg(feature="mass-storage")] let bd = block_device::BlockDevice::new(); - + #[cfg(feature="mass-storage")] let mut ums = usbd_scsi::Scsi::new( &ums_alloc, 64, @@ -207,7 +210,7 @@ pub(crate) fn main_hw() -> ! { "Kosagi Precursor".as_bytes(), "1".as_bytes() ); - + #[cfg(feature="mass-storage")] let mut ums_device = UsbDeviceBuilder::new(&ums_alloc, UsbVidPid(0x1209, 0x3613)) .manufacturer("Kosagi") .product("Precursor") @@ -271,8 +274,9 @@ pub(crate) fn main_hw() -> ! { _ => () }; } + #[cfg(feature="mass-storage")] Views::MassStorage => { - + // TODO: what to do on a resume for USB mass storage?? } } // resume2 brings us to our last application state @@ -328,6 +332,7 @@ pub(crate) fn main_hw() -> ! { let u2f = match view { Views::FidoWithKbd => composite.interface::, _>(), Views::FidoOnly => fido_class.interface::, _>(), + #[cfg(feature="mass-storage")] Views::MassStorage => panic!("expected u2f tx when in mass storage mode!"), }; u2f.write_report(&u2f_msg).ok(); @@ -339,6 +344,7 @@ pub(crate) fn main_hw() -> ! { buffer.replace(u2f_ipc).unwrap(); } Some(Opcode::UsbIrqHandler) => { + #[cfg(feature="mass-storage")] if matches!(view, Views::MassStorage) { if ums_device.poll(&mut [&mut ums]) { log::debug!("ums device had something to do!") @@ -369,6 +375,7 @@ pub(crate) fn main_hw() -> ! { None } }, + #[cfg(feature="mass-storage")] _ => { unreachable!("should not be able to arrive here if view is mass storage!") } @@ -398,6 +405,7 @@ pub(crate) fn main_hw() -> ! { let is_suspend = match view { Views::FidoWithKbd => usb_dev.state() == UsbDeviceState::Suspend, Views::FidoOnly => fido_dev.state() == UsbDeviceState::Suspend, + #[cfg(feature="mass-storage")] Views::MassStorage => ums_device.state() == UsbDeviceState::Suspend, }; if is_suspend { @@ -432,7 +440,7 @@ pub(crate) fn main_hw() -> ! { log::info!("Connecting USB device core; disconnecting debug USB core"); match view { Views::FidoWithKbd => usbmgmt.connect_device_core(true), - Views::FidoOnly | Views::MassStorage => { + _ => { view = Views::FidoWithKbd; usbmgmt.ll_reset(true); tt.sleep_ms(1000).ok(); @@ -445,7 +453,7 @@ pub(crate) fn main_hw() -> ! { UsbDeviceType::Fido => { match view { Views::FidoOnly => usbmgmt.connect_device_core(true), - Views::FidoWithKbd | Views::MassStorage => { + _ => { view = Views::FidoOnly; usbmgmt.ll_reset(true); tt.sleep_ms(1000).ok(); @@ -455,10 +463,11 @@ pub(crate) fn main_hw() -> ! { } } } + #[cfg(feature="mass-storage")] UsbDeviceType::MassStorage => { match view { Views::MassStorage => usbmgmt.connect_device_core(true), - Views::FidoWithKbd | Views::FidoOnly => { + _ => { view = Views::MassStorage; usbmgmt.ll_reset(true); tt.sleep_ms(1000).ok(); @@ -517,6 +526,7 @@ pub(crate) fn main_hw() -> ! { } } }, + #[cfg(feature="mass-storage")] UsbDeviceType::MassStorage => { if !usbmgmt.is_device_connected() { log::info!("Connecting USB device core; disconnecting debug USB core"); @@ -543,6 +553,7 @@ pub(crate) fn main_hw() -> ! { match view { Views::FidoWithKbd => xous::return_scalar(msg.sender, UsbDeviceType::FidoKbd as usize).unwrap(), Views::FidoOnly => xous::return_scalar(msg.sender, UsbDeviceType::Fido as usize).unwrap(), + #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, UsbDeviceType::MassStorage as usize).unwrap(), } } else { @@ -597,6 +608,7 @@ pub(crate) fn main_hw() -> ! { match view { Views::FidoWithKbd => xous::return_scalar(msg.sender, usb_dev.state() as usize).unwrap(), Views::FidoOnly => xous::return_scalar(msg.sender, fido_dev.state() as usize).unwrap(), + #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, ums_device.state() as usize).unwrap(), } }), @@ -629,7 +641,7 @@ pub(crate) fn main_hw() -> ! { xous::return_scalar(msg.sender, 1).unwrap(); } } - Views::FidoOnly | Views::MassStorage => { + _ => { xous::return_scalar(msg.sender, 1).unwrap(); } }