11//! File Systems related commands for Neotron OS
22
3- use chrono:: { Datelike , Timelike } ;
43use embedded_sdmmc:: VolumeIdx ;
54
6- use crate :: { bios, print, println, Ctx , API } ;
5+ use crate :: { bios, print, println, Ctx } ;
76
87pub static DIR_ITEM : menu:: Item < Ctx > = menu:: Item {
98 item_type : menu:: ItemType :: Callback {
@@ -14,7 +13,6 @@ pub static DIR_ITEM: menu::Item<Ctx> = menu::Item {
1413 help : Some ( "Dir the root directory on block device 0" ) ,
1514} ;
1615
17- #[ cfg( target_os = "none" ) ]
1816pub static LOAD_ITEM : menu:: Item < Ctx > = menu:: Item {
1917 item_type : menu:: ItemType :: Callback {
2018 function : load,
@@ -27,89 +25,12 @@ pub static LOAD_ITEM: menu::Item<Ctx> = menu::Item {
2725 help : Some ( "Load a file into the application area" ) ,
2826} ;
2927
30- struct BiosBlock ( ) ;
31-
32- impl embedded_sdmmc:: BlockDevice for BiosBlock {
33- type Error = bios:: Error ;
34-
35- fn read (
36- & self ,
37- blocks : & mut [ embedded_sdmmc:: Block ] ,
38- start_block_idx : embedded_sdmmc:: BlockIdx ,
39- _reason : & str ,
40- ) -> Result < ( ) , Self :: Error > {
41- let api = API . get ( ) ;
42- let byte_slice = unsafe {
43- core:: slice:: from_raw_parts_mut (
44- blocks. as_mut_ptr ( ) as * mut u8 ,
45- blocks. len ( ) * embedded_sdmmc:: Block :: LEN ,
46- )
47- } ;
48- match ( api. block_read ) (
49- 0 ,
50- bios:: block_dev:: BlockIdx ( u64:: from ( start_block_idx. 0 ) ) ,
51- blocks. len ( ) as u8 ,
52- bios:: ApiBuffer :: new ( byte_slice) ,
53- ) {
54- bios:: Result :: Ok ( _) => Ok ( ( ) ) ,
55- bios:: Result :: Err ( e) => Err ( e) ,
56- }
57- }
58-
59- fn write (
60- & self ,
61- blocks : & [ embedded_sdmmc:: Block ] ,
62- start_block_idx : embedded_sdmmc:: BlockIdx ,
63- ) -> Result < ( ) , Self :: Error > {
64- let api = API . get ( ) ;
65- let byte_slice = unsafe {
66- core:: slice:: from_raw_parts (
67- blocks. as_ptr ( ) as * const u8 ,
68- blocks. len ( ) * embedded_sdmmc:: Block :: LEN ,
69- )
70- } ;
71- match ( api. block_write ) (
72- 0 ,
73- bios:: block_dev:: BlockIdx ( u64:: from ( start_block_idx. 0 ) ) ,
74- blocks. len ( ) as u8 ,
75- bios:: ApiByteSlice :: new ( byte_slice) ,
76- ) {
77- bios:: Result :: Ok ( _) => Ok ( ( ) ) ,
78- bios:: Result :: Err ( e) => Err ( e) ,
79- }
80- }
81-
82- fn num_blocks ( & self ) -> Result < embedded_sdmmc:: BlockCount , Self :: Error > {
83- let api = API . get ( ) ;
84- match ( api. block_dev_get_info ) ( 0 ) {
85- bios:: Option :: Some ( info) => Ok ( embedded_sdmmc:: BlockCount ( info. num_blocks as u32 ) ) ,
86- bios:: Option :: None => Err ( bios:: Error :: InvalidDevice ) ,
87- }
88- }
89- }
90-
91- struct BiosTime ( ) ;
92-
93- impl embedded_sdmmc:: TimeSource for BiosTime {
94- fn get_timestamp ( & self ) -> embedded_sdmmc:: Timestamp {
95- let time = API . get_time ( ) ;
96- embedded_sdmmc:: Timestamp {
97- year_since_1970 : ( time. year ( ) - 1970 ) as u8 ,
98- zero_indexed_month : time. month0 ( ) as u8 ,
99- zero_indexed_day : time. day0 ( ) as u8 ,
100- hours : time. hour ( ) as u8 ,
101- minutes : time. minute ( ) as u8 ,
102- seconds : time. second ( ) as u8 ,
103- }
104- }
105- }
106-
10728/// Called when the "dir" command is executed.
10829fn dir ( _menu : & menu:: Menu < Ctx > , _item : & menu:: Item < Ctx > , _args : & [ & str ] , _ctx : & mut Ctx ) {
10930 fn work ( ) -> Result < ( ) , embedded_sdmmc:: Error < bios:: Error > > {
11031 println ! ( "Listing files on Block Device 0, /" ) ;
111- let bios_block = BiosBlock ( ) ;
112- let time = BiosTime ( ) ;
32+ let bios_block = crate :: fs :: BiosBlock ( ) ;
33+ let time = crate :: fs :: BiosTime ( ) ;
11334 let mut mgr = embedded_sdmmc:: VolumeManager :: new ( bios_block, time) ;
11435 // Open the first partition
11536 let volume = mgr. get_volume ( VolumeIdx ( 0 ) ) ?;
@@ -165,33 +86,12 @@ fn dir(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &
16586}
16687
16788/// Called when the "load" command is executed.
168- #[ cfg( target_os = "none" ) ]
169- fn load ( _menu : & menu:: Menu < Ctx > , _item : & menu:: Item < Ctx > , args : & [ & str ] , _ctx : & mut Ctx ) {
170- fn work ( args : & [ & str ] ) -> Result < ( ) , embedded_sdmmc:: Error < bios:: Error > > {
171- println ! ( "Loading /{} from Block Device 0" , args[ 0 ] ) ;
172- let bios_block = BiosBlock ( ) ;
173- let time = BiosTime ( ) ;
174- let mut mgr = embedded_sdmmc:: VolumeManager :: new ( bios_block, time) ;
175- // Open the first partition
176- let mut volume = mgr. get_volume ( VolumeIdx ( 0 ) ) ?;
177- let root_dir = mgr. open_root_dir ( & volume) ?;
178- let mut file = mgr. open_file_in_dir (
179- & mut volume,
180- & root_dir,
181- args[ 0 ] ,
182- embedded_sdmmc:: Mode :: ReadOnly ,
183- ) ?;
184- let file_length = file. length ( ) ;
185- // Application space starts 4K into Cortex-M SRAM
186- const APPLICATION_START_ADDR : usize = 0x2000_1000 ;
187- let application_ram: & ' static mut [ u8 ] = unsafe {
188- core:: slice:: from_raw_parts_mut ( APPLICATION_START_ADDR as * mut u8 , file_length as usize )
189- } ;
190- mgr. read ( & mut volume, & mut file, application_ram) ?;
191- Ok ( ( ) )
192- }
193-
194- match work ( args) {
89+ fn load ( _menu : & menu:: Menu < Ctx > , _item : & menu:: Item < Ctx > , args : & [ & str ] , ctx : & mut Ctx ) {
90+ let Some ( filename) = args. first ( ) else {
91+ println ! ( "Need a filename" ) ;
92+ return ;
93+ } ;
94+ match ctx. tpa . load_program ( filename) {
19595 Ok ( _) => { }
19696 Err ( e) => {
19797 println ! ( "Error: {:?}" , e) ;
0 commit comments