diff --git a/Cargo.toml b/Cargo.toml index 8ad29bb..a070592 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ bench = false [lib] crate-type = ["rlib", "cdylib"] +required-features = ["native-log"] [profile.release] lto = true @@ -44,3 +45,4 @@ postcard = "0.5" serde = { version = "1.0", default-features = false } menu = "0.3" chrono = { version = "0.4", default-features = false } +embedded-sdmmc = { version = "0.5", default-features = false } diff --git a/src/commands/block.rs b/src/commands/block.rs index 87fab98..df7585d 100644 --- a/src/commands/block.rs +++ b/src/commands/block.rs @@ -26,7 +26,7 @@ pub static READ_ITEM: menu::Item = menu::Item { ], }, command: "readblk", - help: Some("List all the Block Devices"), + help: Some("Display one disk block, as hex"), }; /// Called when the "lsblk" command is executed. @@ -96,15 +96,10 @@ fn read_block(_menu: &menu::Menu, _item: &menu::Item, args: &[&str], _ bios::Result::Ok(_) => { // Carry on let mut count = 0; - for chunk in buffer.chunks(16) { + for chunk in buffer.chunks(32) { print!("{:03x}: ", count); for b in chunk { - print!("{:02x} ", *b); - } - print!(" "); - for b in chunk { - let c = char::from(*b); - print!("{}", if c.is_ascii_graphic() { c } else { '.' }); + print!("{:02x}", *b); } count += chunk.len(); println!(); diff --git a/src/commands/fs.rs b/src/commands/fs.rs new file mode 100644 index 0000000..fae0138 --- /dev/null +++ b/src/commands/fs.rs @@ -0,0 +1,200 @@ +//! File Systems related commands for Neotron OS + +use chrono::{Datelike, Timelike}; +use embedded_sdmmc::VolumeIdx; + +use crate::{bios, print, println, Ctx, API}; + +pub static DIR_ITEM: menu::Item = menu::Item { + item_type: menu::ItemType::Callback { + function: dir, + parameters: &[], + }, + command: "dir", + help: Some("Dir the root directory on block device 0"), +}; + +#[cfg(target_os = "none")] +pub static LOAD_ITEM: menu::Item = menu::Item { + item_type: menu::ItemType::Callback { + function: load, + parameters: &[menu::Parameter::Mandatory { + parameter_name: "file", + help: Some("The file to load"), + }], + }, + command: "load", + help: Some("Load a file into the application area"), +}; + +struct BiosBlock(); + +impl embedded_sdmmc::BlockDevice for BiosBlock { + type Error = bios::Error; + + fn read( + &self, + blocks: &mut [embedded_sdmmc::Block], + start_block_idx: embedded_sdmmc::BlockIdx, + _reason: &str, + ) -> Result<(), Self::Error> { + let api = API.get(); + let byte_slice = unsafe { + core::slice::from_raw_parts_mut( + blocks.as_mut_ptr() as *mut u8, + blocks.len() * embedded_sdmmc::Block::LEN, + ) + }; + match (api.block_read)( + 0, + bios::block_dev::BlockIdx(u64::from(start_block_idx.0)), + blocks.len() as u8, + bios::ApiBuffer::new(byte_slice), + ) { + bios::Result::Ok(_) => Ok(()), + bios::Result::Err(e) => Err(e), + } + } + + fn write( + &self, + blocks: &[embedded_sdmmc::Block], + start_block_idx: embedded_sdmmc::BlockIdx, + ) -> Result<(), Self::Error> { + let api = API.get(); + let byte_slice = unsafe { + core::slice::from_raw_parts( + blocks.as_ptr() as *const u8, + blocks.len() * embedded_sdmmc::Block::LEN, + ) + }; + match (api.block_write)( + 0, + bios::block_dev::BlockIdx(u64::from(start_block_idx.0)), + blocks.len() as u8, + bios::ApiByteSlice::new(byte_slice), + ) { + bios::Result::Ok(_) => Ok(()), + bios::Result::Err(e) => Err(e), + } + } + + fn num_blocks(&self) -> Result { + let api = API.get(); + match (api.block_dev_get_info)(0) { + bios::Option::Some(info) => Ok(embedded_sdmmc::BlockCount(info.num_blocks as u32)), + bios::Option::None => Err(bios::Error::InvalidDevice), + } + } +} + +struct BiosTime(); + +impl embedded_sdmmc::TimeSource for BiosTime { + fn get_timestamp(&self) -> embedded_sdmmc::Timestamp { + let time = API.get_time(); + embedded_sdmmc::Timestamp { + year_since_1970: (time.year() - 1970) as u8, + zero_indexed_month: time.month0() as u8, + zero_indexed_day: time.day0() as u8, + hours: time.hour() as u8, + minutes: time.minute() as u8, + seconds: time.second() as u8, + } + } +} + +/// Called when the "dir" command is executed. +fn dir(_menu: &menu::Menu, _item: &menu::Item, _args: &[&str], _ctx: &mut Ctx) { + fn work() -> Result<(), embedded_sdmmc::Error> { + println!("Listing files on Block Device 0, /"); + let bios_block = BiosBlock(); + let time = BiosTime(); + let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time); + // Open the first partition + let volume = mgr.get_volume(VolumeIdx(0))?; + let root_dir = mgr.open_root_dir(&volume)?; + let mut total_bytes = 0u64; + let mut num_files = 0; + mgr.iterate_dir(&volume, &root_dir, |dir_entry| { + let padding = 8 - dir_entry.name.base_name().len(); + for b in dir_entry.name.base_name() { + let ch = *b as char; + print!("{}", if ch.is_ascii_graphic() { ch } else { '?' }); + } + for _ in 0..padding { + print!(" "); + } + print!(" "); + let padding = 3 - dir_entry.name.extension().len(); + for b in dir_entry.name.extension() { + let ch = *b as char; + print!("{}", if ch.is_ascii_graphic() { ch } else { '?' }); + } + for _ in 0..padding { + print!(" "); + } + if dir_entry.attributes.is_directory() { + print!(" "); + } else { + print!(" {:-13}", dir_entry.size,); + } + print!( + " {:02}/{:02}/{:04}", + dir_entry.mtime.zero_indexed_day + 1, + dir_entry.mtime.zero_indexed_month + 1, + u32::from(dir_entry.mtime.year_since_1970) + 1970 + ); + println!( + " {:02}:{:02}", + dir_entry.mtime.hours, dir_entry.mtime.minutes + ); + total_bytes += dir_entry.size as u64; + num_files += 1; + })?; + println!("{:-9} file(s) {:-13} bytes", num_files, total_bytes); + Ok(()) + } + + match work() { + Ok(_) => {} + Err(e) => { + println!("Error: {:?}", e); + } + } +} + +/// Called when the "load" command is executed. +#[cfg(target_os = "none")] +fn load(_menu: &menu::Menu, _item: &menu::Item, args: &[&str], _ctx: &mut Ctx) { + fn work(args: &[&str]) -> Result<(), embedded_sdmmc::Error> { + println!("Loading /{} from Block Device 0", args[0]); + let bios_block = BiosBlock(); + let time = BiosTime(); + let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time); + // Open the first partition + let mut volume = mgr.get_volume(VolumeIdx(0))?; + let root_dir = mgr.open_root_dir(&volume)?; + let mut file = mgr.open_file_in_dir( + &mut volume, + &root_dir, + args[0], + embedded_sdmmc::Mode::ReadOnly, + )?; + let file_length = file.length(); + // Application space starts 4K into Cortex-M SRAM + const APPLICATION_START_ADDR: usize = 0x2000_1000; + let application_ram: &'static mut [u8] = unsafe { + core::slice::from_raw_parts_mut(APPLICATION_START_ADDR as *mut u8, file_length as usize) + }; + mgr.read(&mut volume, &mut file, application_ram)?; + Ok(()) + } + + match work(args) { + Ok(_) => {} + Err(e) => { + println!("Error: {:?}", e); + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index fe0989f..c316eb3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,6 +6,7 @@ pub use super::Ctx; mod block; mod config; +mod fs; mod hardware; mod input; mod ram; @@ -19,11 +20,13 @@ pub static OS_MENU: menu::Menu = menu::Menu { &config::COMMAND_ITEM, &block::LSBLK_ITEM, &block::READ_ITEM, + &fs::DIR_ITEM, &hardware::LSHW_ITEM, &ram::HEXDUMP_ITEM, - &ram::LOAD_ITEM, #[cfg(target_os = "none")] &ram::RUN_ITEM, + #[cfg(target_os = "none")] + &fs::LOAD_ITEM, &screen::CLEAR_ITEM, &screen::BENCH_ITEM, &screen::FILL_ITEM, diff --git a/src/commands/ram.rs b/src/commands/ram.rs index 42580f8..5527bfd 100644 --- a/src/commands/ram.rs +++ b/src/commands/ram.rs @@ -20,24 +20,6 @@ pub static HEXDUMP_ITEM: menu::Item = menu::Item { help: Some("Dump the contents of RAM as hex"), }; -pub static LOAD_ITEM: menu::Item = menu::Item { - item_type: menu::ItemType::Callback { - function: load, - parameters: &[ - menu::Parameter::Mandatory { - parameter_name: "address", - help: Some("Start address"), - }, - menu::Parameter::Mandatory { - parameter_name: "hex", - help: Some("Bytes as hex string"), - }, - ], - }, - command: "load", - help: Some("Load hex bytes into RAM from stdin"), -}; - #[cfg(target_os = "none")] pub static RUN_ITEM: menu::Item = menu::Item { item_type: menu::ItemType::Callback { @@ -99,52 +81,16 @@ fn hexdump(_menu: &menu::Menu, _item: &menu::Item, args: &[&str], _ctx println!(); } -/// Called when the "load" command is executed. -fn load(_menu: &menu::Menu, _item: &menu::Item, args: &[&str], _ctx: &mut Ctx) { - let Some(address_str) = args.get(0) else { - println!("No address"); - return; - }; - let Ok(address) = parse_usize(address_str) else { - println!("Bad address"); - return; - }; - let Some(mut hex_str) = args.get(1).cloned() else { - println!("No hex"); - return; - }; - - let mut address = address as *mut u8; - let mut count = 0; - loop { - let Some(hex_byte) = hex_str.get(0..2) else { - println!("Bad hex from {:?}", hex_str); - return; - }; - hex_str = &hex_str[2..]; - let Ok(byte) = u8::from_str_radix(hex_byte, 16) else { - println!("Bad hex {:?}", hex_byte); - return; - }; - - unsafe { - address.write_volatile(byte); - address = address.offset(1); - } - count += 1; - - println!("Loaded {} bytes", count); - } -} - #[allow(unused)] #[repr(C)] pub struct Api { pub print: extern "C" fn(data: *const u8, len: usize), } +#[allow(unused)] static CALLBACK_TABLE: Api = Api { print: print_fn }; +#[allow(unused)] extern "C" fn print_fn(data: *const u8, len: usize) { let slice = unsafe { core::slice::from_raw_parts(data, len) }; if let Ok(s) = core::str::from_utf8(slice) { @@ -170,6 +116,7 @@ fn run(_menu: &menu::Menu, _item: &menu::Item, _args: &[&str], _ctx: & let code: extern "C" fn(*const Api) -> u32 = ::core::mem::transmute(start_ptr); code(&CALLBACK_TABLE) }; + println!(); if result != 0 { println!("Got error code {}", result); }