diff --git a/apps/transientdisk/src/flash_drive.rs b/apps/transientdisk/src/flash_drive.rs new file mode 100644 index 000000000..0dc3beed2 --- /dev/null +++ b/apps/transientdisk/src/flash_drive.rs @@ -0,0 +1,86 @@ +use xous::MessageEnvelope; + +pub struct FlashDrive { + capacity: usize, + block_size: usize, + memory: xous::MemoryRange, +} + +#[derive(Debug)] +pub enum FlashDriveError { + CapacityNotPageAligned, +} + +impl FlashDrive { + pub fn new(capacity: usize, block_size: usize) -> Result { + if (capacity % 1024) != 0 { + return Err(FlashDriveError::CapacityNotPageAligned); + } + + let mut backing = xous::syscall::map_memory( + None, + None, + capacity, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .unwrap(); + + // initialize backing slice with bogus data + let backing_slice: &mut [u32] = backing.as_slice_mut(); + for (index, d) in backing_slice.iter_mut().enumerate() { + *d = index as u32; + } + + Ok(Self { + capacity, + block_size, + memory: backing, + }) + } + + pub fn read(&mut self, msg: &mut MessageEnvelope) { + let body = msg + .body + .memory_message_mut() + .expect("incorrect message type received"); + let lba = body.offset.map(|v| v.get()).unwrap_or_default(); + let data = body.buf.as_slice_mut::(); + + self.read_inner(lba, data); + } + + pub fn write(&mut self, msg: &mut MessageEnvelope) { + let body = msg + .body + .memory_message_mut() + .expect("incorrect message type received"); + let lba = body.offset.map(|v| v.get()).unwrap_or_default(); + let data = body.buf.as_slice_mut::(); + + self.write_inner(lba, data); + } + + pub fn max_lba(&self, msg: &mut MessageEnvelope) { + xous::return_scalar(msg.sender, self.max_lba_inner() as usize).unwrap(); + } +} + +impl FlashDrive { + fn read_inner(&mut self, lba: usize, data: &mut [u8]) { + let backing_slice: &[u8] = self.memory.as_slice(); + + let rawdata = &backing_slice[lba * self.block_size..(lba + 1) * self.block_size]; + + data[..self.block_size].copy_from_slice(rawdata); + } + + fn write_inner(&mut self, lba: usize, data: &mut [u8]) { + let backing_slice: &mut [u8] = self.memory.as_slice_mut(); + backing_slice[lba * self.block_size..(lba + 1) * self.block_size] + .copy_from_slice(&data[..self.block_size]); + } + + fn max_lba_inner(&self) -> u32 { + (self.capacity as u32 / 512) - 1 + } +} diff --git a/apps/transientdisk/src/main.rs b/apps/transientdisk/src/main.rs index 047d3a5df..2b6dac2cd 100644 --- a/apps/transientdisk/src/main.rs +++ b/apps/transientdisk/src/main.rs @@ -1,10 +1,10 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] - use core::fmt::Write; use graphics_server::api::GlyphStyle; use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView}; use num_traits::*; +mod flash_drive; pub(crate) const SERVER_NAME_TRANSIENTDISK: &str = "_Transient Disk_"; @@ -115,63 +115,6 @@ impl UI { } } -struct FlashDrive { - capacity: usize, - block_size: usize, - memory: xous::MemoryRange, -} - -#[derive(Debug)] -enum FlashDriveError { - CapacityNotPageAligned, -} - -impl FlashDrive { - fn new(capacity: usize, block_size: usize) -> Result { - if (capacity % 1024) != 0 { - return Err(FlashDriveError::CapacityNotPageAligned); - } - - let mut backing = xous::syscall::map_memory( - None, - None, - capacity, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .unwrap(); - - // initialize backing slice with bogus data - let backing_slice: &mut [u32] = backing.as_slice_mut(); - for (index, d) in backing_slice.iter_mut().enumerate() { - *d = index as u32; - } - - Ok(Self { - capacity, - block_size, - memory: backing, - }) - } - - fn read(&mut self, lba: usize, data: &mut [u8]) { - let backing_slice: &[u8] = self.memory.as_slice(); - - let rawdata = &backing_slice[lba * self.block_size..(lba + 1) * self.block_size]; - - data[..self.block_size].copy_from_slice(rawdata); - } - - fn write(&mut self, lba: usize, data: &mut [u8]) { - let backing_slice: &mut [u8] = self.memory.as_slice_mut(); - backing_slice[lba * self.block_size..(lba + 1) * self.block_size] - .copy_from_slice(&data[..self.block_size]); - } - - fn max_lba(&self) -> u32 { - (self.capacity as u32 / 512) - 1 - } -} - fn main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -189,7 +132,8 @@ fn main() -> ! { let mut ui = UI::new(&xns, sid); - let mut fd = FlashDrive::new(1445888, 512).expect("cannot create flash drive instance"); + let mut fd = + flash_drive::FlashDrive::new(1445888, 512).expect("cannot create flash drive instance"); #[cfg(feature = "mass-storage")] let usb = usb_device_xous::UsbHid::new(); @@ -245,28 +189,12 @@ fn main() -> ! { } }), Some(TradOp::Read) => { - let body = msg - .body - .memory_message_mut() - .expect("incorrect message type received"); - let lba = body.offset.map(|v| v.get()).unwrap_or_default(); - let data = body.buf.as_slice_mut::(); - - fd.read(lba, data); + fd.read(&mut msg); } Some(TradOp::Write) => { - let body = msg - .body - .memory_message_mut() - .expect("incorrect message type received"); - let lba = body.offset.map(|v| v.get()).unwrap_or_default(); - let data = body.buf.as_slice_mut::(); - - fd.write(lba, data); - } - Some(TradOp::MaxLba) => { - xous::return_scalar(msg.sender, fd.max_lba() as usize).unwrap(); + fd.write(&mut msg); } + Some(TradOp::MaxLba) => fd.max_lba(&mut msg), _ => { log::error!("Got unknown message"); }