-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fs.rs
196 lines (183 loc) · 6.63 KB
/
fs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! File Systems related commands for Neotron OS
use crate::{bios, osprint, osprintln, Ctx};
pub static DIR_ITEM: menu::Item<Ctx> = menu::Item {
item_type: menu::ItemType::Callback {
function: dir,
parameters: &[],
},
command: "dir",
help: Some("Dir the root directory on block device 0"),
};
pub static LOAD_ITEM: menu::Item<Ctx> = 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"),
};
pub static EXEC_ITEM: menu::Item<Ctx> = menu::Item {
item_type: menu::ItemType::Callback {
function: exec,
parameters: &[menu::Parameter::Mandatory {
parameter_name: "file",
help: Some("The shell script to run"),
}],
},
command: "exec",
help: Some("Execute a shell script"),
};
pub static TYPE_ITEM: menu::Item<Ctx> = menu::Item {
item_type: menu::ItemType::Callback {
function: typefn,
parameters: &[menu::Parameter::Mandatory {
parameter_name: "file",
help: Some("The file to type"),
}],
},
command: "type",
help: Some("Type a file to the console"),
};
/// Called when the "dir" command is executed.
fn dir(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
fn work() -> Result<(), embedded_sdmmc::Error<bios::Error>> {
osprintln!("Listing files on Block Device 0, /");
let bios_block = crate::fs::BiosBlock();
let time = crate::fs::BiosTime();
let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time);
// Open the first partition
let volume = mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
let root_dir = mgr.open_root_dir(volume)?;
let mut total_bytes = 0u64;
let mut num_files = 0;
mgr.iterate_dir(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;
osprint!("{}", if ch.is_ascii_graphic() { ch } else { '?' });
}
for _ in 0..padding {
osprint!(" ");
}
osprint!(" ");
let padding = 3 - dir_entry.name.extension().len();
for b in dir_entry.name.extension() {
let ch = *b as char;
osprint!("{}", if ch.is_ascii_graphic() { ch } else { '?' });
}
for _ in 0..padding {
osprint!(" ");
}
if dir_entry.attributes.is_directory() {
osprint!(" <DIR> ");
} else {
osprint!(" {:-13}", dir_entry.size,);
}
osprint!(
" {: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
);
osprintln!(
" {:02}:{:02}",
dir_entry.mtime.hours,
dir_entry.mtime.minutes
);
total_bytes += dir_entry.size as u64;
num_files += 1;
})?;
osprintln!("{:-9} file(s) {:-13} bytes", num_files, total_bytes);
Ok(())
}
match work() {
Ok(_) => {}
Err(e) => {
osprintln!("Error: {:?}", e);
}
}
}
/// Called when the "load" command is executed.
fn load(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], ctx: &mut Ctx) {
let Some(filename) = args.first() else {
osprintln!("Need a filename");
return;
};
match ctx.tpa.load_program(filename) {
Ok(_) => {}
Err(e) => {
osprintln!("Error: {:?}", e);
}
}
}
/// Called when the "exec" command is executed.
fn exec(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], ctx: &mut Ctx) {
fn work(ctx: &mut Ctx, filename: &str) -> Result<(), embedded_sdmmc::Error<bios::Error>> {
let bios_block = crate::fs::BiosBlock();
let time = crate::fs::BiosTime();
let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time);
// Open the first partition
let volume = mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
let root_dir = mgr.open_root_dir(volume)?;
let file = mgr.open_file_in_dir(root_dir, filename, embedded_sdmmc::Mode::ReadOnly)?;
let buffer = ctx.tpa.as_slice_u8();
let count = mgr.read(file, buffer)?;
if count != mgr.file_length(file)? as usize {
osprintln!("File too large! Max {} bytes allowed.", buffer.len());
return Ok(());
}
let Ok(s) = core::str::from_utf8(&buffer[0..count]) else {
osprintln!("File is not valid UTF-8");
return Ok(());
};
// tell the main loop to run from these bytes next
ctx.exec_tpa = Some(s.len());
Ok(())
}
// index can't panic - we always have enough args
let r = work(ctx, args[0]);
match r {
Ok(_) => {}
Err(e) => {
osprintln!("Error: {:?}", e);
}
}
}
/// Called when the "type" command is executed.
fn typefn(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], ctx: &mut Ctx) {
fn work(ctx: &mut Ctx, filename: &str) -> Result<(), embedded_sdmmc::Error<bios::Error>> {
let bios_block = crate::fs::BiosBlock();
let time = crate::fs::BiosTime();
let mut mgr = embedded_sdmmc::VolumeManager::new(bios_block, time);
// Open the first partition
let volume = mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
let root_dir = mgr.open_root_dir(volume)?;
let file = mgr.open_file_in_dir(root_dir, filename, embedded_sdmmc::Mode::ReadOnly)?;
let buffer = ctx.tpa.as_slice_u8();
let count = mgr.read(file, buffer)?;
if count != mgr.file_length(file)? as usize {
osprintln!("File too large! Max {} bytes allowed.", buffer.len());
return Ok(());
}
let Ok(s) = core::str::from_utf8(&buffer[0..count]) else {
osprintln!("File is not valid UTF-8");
return Ok(());
};
osprintln!("{}", s);
Ok(())
}
// index can't panic - we always have enough args
let r = work(ctx, args[0]);
// reset SGR
osprint!("\u{001b}[0m");
match r {
Ok(_) => {}
Err(e) => {
osprintln!("Error: {:?}", e);
}
}
}
// End of file