Skip to content

Commit 4892620

Browse files
Can load files from disk.
1 parent 0a5aa28 commit 4892620

3 files changed

Lines changed: 51 additions & 58 deletions

File tree

src/commands/fs.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ pub static DIR_ITEM: menu::Item<Ctx> = menu::Item {
1414
help: Some("Dir the root directory on block device 0"),
1515
};
1616

17+
pub static LOAD_ITEM: menu::Item<Ctx> = menu::Item {
18+
item_type: menu::ItemType::Callback {
19+
function: load,
20+
parameters: &[menu::Parameter::Mandatory {
21+
parameter_name: "file",
22+
help: Some("The file to load"),
23+
}],
24+
},
25+
command: "load",
26+
help: Some("Load a file into the application area"),
27+
};
28+
1729
struct BiosBlock();
1830

1931
impl embedded_sdmmc::BlockDevice for BiosBlock {
@@ -91,7 +103,7 @@ impl embedded_sdmmc::TimeSource for BiosTime {
91103
}
92104
}
93105

94-
/// Called when the "lsblk" command is executed.
106+
/// Called when the "dir" command is executed.
95107
fn dir(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
96108
fn work() -> Result<(), embedded_sdmmc::Error<bios::Error>> {
97109
println!("Listing files on Block Device 0, /");
@@ -150,3 +162,38 @@ fn dir(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &
150162
}
151163
}
152164
}
165+
166+
/// Called when the "load" command is executed.
167+
#[cfg(target_os = "none")]
168+
fn load(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _ctx: &mut Ctx) {
169+
fn work(args: &[&str]) -> Result<(), embedded_sdmmc::Error<bios::Error>> {
170+
println!("Loading /{} from Block Device 0", args[0]);
171+
let bios_block = BiosBlock();
172+
let time = BiosTime();
173+
let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time);
174+
// Open the first partition
175+
let mut volume = mgr.get_volume(VolumeIdx(0))?;
176+
let root_dir = mgr.open_root_dir(&volume)?;
177+
let mut file = mgr.open_file_in_dir(
178+
&mut volume,
179+
&root_dir,
180+
args[0],
181+
embedded_sdmmc::Mode::ReadOnly,
182+
)?;
183+
let file_length = file.length();
184+
// Application space starts 4K into Cortex-M SRAM
185+
const APPLICATION_START_ADDR: usize = 0x2000_1000;
186+
let application_ram: &'static mut [u8] = unsafe {
187+
core::slice::from_raw_parts_mut(APPLICATION_START_ADDR as *mut u8, file_length as usize)
188+
};
189+
mgr.read(&mut volume, &mut file, application_ram)?;
190+
Ok(())
191+
}
192+
193+
match work(args) {
194+
Ok(_) => {}
195+
Err(e) => {
196+
println!("Error: {:?}", e);
197+
}
198+
}
199+
}

src/commands/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ pub static OS_MENU: menu::Menu<Ctx> = menu::Menu {
2323
&fs::DIR_ITEM,
2424
&hardware::LSHW_ITEM,
2525
&ram::HEXDUMP_ITEM,
26-
&ram::LOAD_ITEM,
2726
#[cfg(target_os = "none")]
2827
&ram::RUN_ITEM,
28+
#[cfg(target_os = "none")]
29+
&fs::LOAD_ITEM,
2930
&screen::CLEAR_ITEM,
3031
&screen::BENCH_ITEM,
3132
&screen::FILL_ITEM,

src/commands/ram.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ pub static HEXDUMP_ITEM: menu::Item<Ctx> = menu::Item {
2020
help: Some("Dump the contents of RAM as hex"),
2121
};
2222

23-
pub static LOAD_ITEM: menu::Item<Ctx> = menu::Item {
24-
item_type: menu::ItemType::Callback {
25-
function: load,
26-
parameters: &[
27-
menu::Parameter::Mandatory {
28-
parameter_name: "address",
29-
help: Some("Start address"),
30-
},
31-
menu::Parameter::Mandatory {
32-
parameter_name: "hex",
33-
help: Some("Bytes as hex string"),
34-
},
35-
],
36-
},
37-
command: "load",
38-
help: Some("Load hex bytes into RAM from stdin"),
39-
};
40-
4123
#[cfg(target_os = "none")]
4224
pub static RUN_ITEM: menu::Item<Ctx> = menu::Item {
4325
item_type: menu::ItemType::Callback {
@@ -99,44 +81,6 @@ fn hexdump(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _ctx
9981
println!();
10082
}
10183

102-
/// Called when the "load" command is executed.
103-
fn load(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _ctx: &mut Ctx) {
104-
let Some(address_str) = args.get(0) else {
105-
println!("No address");
106-
return;
107-
};
108-
let Ok(address) = parse_usize(address_str) else {
109-
println!("Bad address");
110-
return;
111-
};
112-
let Some(mut hex_str) = args.get(1).cloned() else {
113-
println!("No hex");
114-
return;
115-
};
116-
117-
let mut address = address as *mut u8;
118-
let mut count = 0;
119-
loop {
120-
let Some(hex_byte) = hex_str.get(0..2) else {
121-
println!("Bad hex from {:?}", hex_str);
122-
return;
123-
};
124-
hex_str = &hex_str[2..];
125-
let Ok(byte) = u8::from_str_radix(hex_byte, 16) else {
126-
println!("Bad hex {:?}", hex_byte);
127-
return;
128-
};
129-
130-
unsafe {
131-
address.write_volatile(byte);
132-
address = address.offset(1);
133-
}
134-
count += 1;
135-
136-
println!("Loaded {} bytes", count);
137-
}
138-
}
139-
14084
#[allow(unused)]
14185
#[repr(C)]
14286
pub struct Api {
@@ -170,6 +114,7 @@ fn run(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &
170114
let code: extern "C" fn(*const Api) -> u32 = ::core::mem::transmute(start_ptr);
171115
code(&CALLBACK_TABLE)
172116
};
117+
println!();
173118
if result != 0 {
174119
println!("Got error code {}", result);
175120
}

0 commit comments

Comments
 (0)