Skip to content

Commit aa582da

Browse files
committed
Clean up "ls" commands
1 parent 59dc473 commit aa582da

File tree

5 files changed

+182
-137
lines changed

5 files changed

+182
-137
lines changed

src/commands/block.rs

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
//! Block Device related commands for Neotron OS
22
3+
use super::{parse_u64, parse_u8};
34
use crate::{bios, osprint, osprintln, Ctx, API};
45

5-
pub static LSBLK_ITEM: menu::Item<Ctx> = menu::Item {
6-
item_type: menu::ItemType::Callback {
7-
function: lsblk,
8-
parameters: &[],
9-
},
10-
command: "lsblk",
11-
help: Some("List all the Block Devices"),
12-
};
13-
146
pub static READ_ITEM: menu::Item<Ctx> = menu::Item {
157
item_type: menu::ItemType::Callback {
168
function: read_block,
@@ -29,66 +21,21 @@ pub static READ_ITEM: menu::Item<Ctx> = menu::Item {
2921
help: Some("Display one disk block, as hex"),
3022
};
3123

32-
/// Called when the "lsblk" command is executed.
33-
fn lsblk(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
34-
let api = API.get();
35-
let mut found = false;
36-
37-
osprintln!("Block Devices:");
38-
for dev_idx in 0..=255u8 {
39-
if let bios::FfiOption::Some(device_info) = (api.block_dev_get_info)(dev_idx) {
40-
let (bsize, bunits, dsize, dunits) =
41-
match device_info.num_blocks * u64::from(device_info.block_size) {
42-
x if x < (1024 * 1024 * 1024) => {
43-
// Under 1 GiB, give it in 10s of MiB
44-
(10 * x / (1024 * 1024), "MiB", x / 100_000, "MB")
45-
}
46-
x => {
47-
// Anything else in GiB
48-
(10 * x / (1024 * 1024 * 1024), "GiB", x / 100_000_000, "GB")
49-
}
50-
};
51-
osprintln!("Device {}:", dev_idx);
52-
osprintln!(" Name: {}", device_info.name);
53-
osprintln!(" Type: {:?}", device_info.device_type);
54-
osprintln!(" Block size: {}", device_info.block_size);
55-
osprintln!(" Num Blocks: {}", device_info.num_blocks);
56-
osprintln!(
57-
" Card Size: {}.{} {} ({}.{} {})",
58-
bsize / 10,
59-
bsize % 10,
60-
bunits,
61-
dsize / 10,
62-
dsize % 10,
63-
dunits
64-
);
65-
osprintln!(" Ejectable: {}", device_info.ejectable);
66-
osprintln!(" Removable: {}", device_info.removable);
67-
osprintln!(" Media Present: {}", device_info.media_present);
68-
osprintln!(" Read Only: {}", device_info.read_only);
69-
found = true;
70-
}
71-
}
72-
if !found {
73-
osprintln!(" None");
74-
}
75-
}
76-
7724
/// Called when the "read_block" command is executed.
7825
fn read_block(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _ctx: &mut Ctx) {
7926
let api = API.get();
80-
let Ok(dev_idx) = args[0].parse::<u8>() else {
27+
let Ok(device_idx) = parse_u8(args[0]) else {
8128
osprintln!("Couldn't parse {:?}", args[0]);
8229
return;
8330
};
84-
let Ok(block_idx) = args[1].parse::<u64>() else {
31+
let Ok(block_idx) = parse_u64(args[1]) else {
8532
osprintln!("Couldn't parse {:?}", args[1]);
8633
return;
8734
};
8835
osprintln!("Reading block {}:", block_idx);
8936
let mut buffer = [0u8; 512];
9037
match (api.block_read)(
91-
dev_idx,
38+
device_idx,
9239
bios::block_dev::BlockIdx(block_idx),
9340
1,
9441
bios::FfiBuffer::new(&mut buffer),
@@ -110,3 +57,5 @@ fn read_block(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _
11057
}
11158
}
11259
}
60+
61+
// End of file

src/commands/hardware.rs

Lines changed: 126 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,49 @@
22
33
use crate::{bios, osprintln, Ctx, API};
44

5-
pub static LSHW_ITEM: menu::Item<Ctx> = menu::Item {
5+
pub static LSBLK_ITEM: menu::Item<Ctx> = menu::Item {
66
item_type: menu::ItemType::Callback {
7-
function: lshw,
7+
function: lsblk,
88
parameters: &[],
99
},
10-
command: "lshw",
11-
help: Some("List all the BIOS hardware"),
10+
command: "lsblk",
11+
help: Some("List all the Block Devices"),
12+
};
13+
14+
pub static LSBUS_ITEM: menu::Item<Ctx> = menu::Item {
15+
item_type: menu::ItemType::Callback {
16+
function: lsbus,
17+
parameters: &[],
18+
},
19+
command: "lsbus",
20+
help: Some("List all the Neotron Bus devices"),
21+
};
22+
23+
pub static LSI2C_ITEM: menu::Item<Ctx> = menu::Item {
24+
item_type: menu::ItemType::Callback {
25+
function: lsi2c,
26+
parameters: &[],
27+
},
28+
command: "lsi2c",
29+
help: Some("List all the BIOS I2C devices"),
30+
};
31+
32+
pub static LSMEM_ITEM: menu::Item<Ctx> = menu::Item {
33+
item_type: menu::ItemType::Callback {
34+
function: lsmem,
35+
parameters: &[],
36+
},
37+
command: "lsmem",
38+
help: Some("List all the BIOS Memory regions"),
39+
};
40+
41+
pub static LSUART_ITEM: menu::Item<Ctx> = menu::Item {
42+
item_type: menu::ItemType::Callback {
43+
function: lsuart,
44+
parameters: &[],
45+
},
46+
command: "lsuart",
47+
help: Some("List all the BIOS UARTs"),
1248
};
1349

1450
pub static SHUTDOWN_ITEM: menu::Item<Ctx> = menu::Item {
@@ -29,109 +65,130 @@ pub static SHUTDOWN_ITEM: menu::Item<Ctx> = menu::Item {
2965
help: Some("Shutdown the system"),
3066
};
3167

32-
/// Called when the "lshw" command is executed.
33-
fn lshw(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
68+
/// Called when the "lsblk" command is executed.
69+
fn lsblk(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
3470
let api = API.get();
3571
let mut found = false;
3672

37-
osprintln!("Memory regions:");
38-
for region_idx in 0..=255u8 {
39-
if let bios::FfiOption::Some(region) = (api.memory_get_region)(region_idx) {
40-
osprintln!(" {}: {}", region_idx, region);
41-
found = true;
42-
}
43-
}
44-
if !found {
45-
osprintln!(" None");
46-
}
47-
48-
found = false;
49-
50-
osprintln!("Serial Devices:");
73+
osprintln!("Block Devices:");
5174
for dev_idx in 0..=255u8 {
52-
if let bios::FfiOption::Some(device_info) = (api.serial_get_info)(dev_idx) {
75+
if let bios::FfiOption::Some(device_info) = (api.block_dev_get_info)(dev_idx) {
76+
let (bsize, bunits, dsize, dunits) =
77+
match device_info.num_blocks * u64::from(device_info.block_size) {
78+
x if x < (1024 * 1024 * 1024) => {
79+
// Under 1 GiB, give it in 10s of MiB
80+
(10 * x / (1024 * 1024), "MiB", x / 100_000, "MB")
81+
}
82+
x => {
83+
// Anything else in GiB
84+
(10 * x / (1024 * 1024 * 1024), "GiB", x / 100_000_000, "GB")
85+
}
86+
};
87+
osprintln!("Device {}:", dev_idx);
88+
osprintln!("\t Name: {}", device_info.name);
89+
osprintln!("\t Type: {:?}", device_info.device_type);
90+
osprintln!("\tBlock size: {}", device_info.block_size);
91+
osprintln!("\tNum Blocks: {}", device_info.num_blocks);
92+
osprintln!(
93+
"\t Card Size: {}.{} {} ({}.{} {})",
94+
bsize / 10,
95+
bsize % 10,
96+
bunits,
97+
dsize / 10,
98+
dsize % 10,
99+
dunits
100+
);
101+
osprintln!("\t Ejectable: {}", device_info.ejectable);
102+
osprintln!("\t Removable: {}", device_info.removable);
103+
osprintln!("\t Read Only: {}", device_info.read_only);
53104
osprintln!(
54-
" {}: {} {:?}",
55-
dev_idx,
56-
device_info.name,
57-
device_info.device_type
105+
"\t Media: {}",
106+
if device_info.media_present {
107+
"Present"
108+
} else {
109+
"Missing"
110+
}
58111
);
59112
found = true;
60113
}
61114
}
62115
if !found {
63-
osprintln!(" None");
116+
osprintln!("\tNone");
64117
}
118+
}
65119

66-
found = false;
67-
68-
osprintln!("Block Devices:");
120+
/// Called when the "lsbus" command is executed.
121+
fn lsbus(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
122+
let api = API.get();
123+
let mut found = false;
124+
osprintln!("Neotron Bus Devices:");
69125
for dev_idx in 0..=255u8 {
70-
if let bios::FfiOption::Some(device_info) = (api.block_dev_get_info)(dev_idx) {
71-
osprintln!(
72-
" {}: {} {:?} bs={} size={} MiB",
73-
dev_idx,
74-
device_info.name,
75-
device_info.device_type,
76-
device_info.block_size,
77-
(device_info.num_blocks * u64::from(device_info.block_size)) / (1024 * 1024)
78-
);
126+
if let bios::FfiOption::Some(device_info) = (api.bus_get_info)(dev_idx) {
127+
let kind = match device_info.kind {
128+
bios::bus::PeripheralKind::Slot => "Slot",
129+
bios::bus::PeripheralKind::SdCard => "SdCard",
130+
bios::bus::PeripheralKind::Reserved => "Reserved",
131+
};
132+
osprintln!("\t{}: {} ({})", dev_idx, device_info.name, kind);
79133
found = true;
80134
}
81135
}
82136
if !found {
83-
osprintln!(" None");
137+
osprintln!("\tNone");
84138
}
139+
}
85140

86-
found = false;
87-
141+
/// Called when the "lsi2c" command is executed.
142+
fn lsi2c(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
143+
let api = API.get();
144+
let mut found = false;
88145
osprintln!("I2C Buses:");
89146
for dev_idx in 0..=255u8 {
90147
if let bios::FfiOption::Some(device_info) = (api.i2c_bus_get_info)(dev_idx) {
91-
osprintln!(" {}: {:?}", dev_idx, device_info);
148+
osprintln!("\t{}: {}", dev_idx, device_info.name);
92149
found = true;
93150
}
94151
}
95152
if !found {
96-
osprintln!(" None");
153+
osprintln!("\tNone");
97154
}
155+
}
98156

99-
found = false;
100-
101-
osprintln!("Neotron Bus Devices:");
102-
for dev_idx in 0..=255u8 {
103-
if let bios::FfiOption::Some(device_info) = (api.bus_get_info)(dev_idx) {
104-
osprintln!(" {}: {:?}", dev_idx, device_info);
157+
/// Called when the "lsmem" command is executed.
158+
fn lsmem(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
159+
let api = API.get();
160+
let mut found = false;
161+
osprintln!("Memory regions:");
162+
for region_idx in 0..=255u8 {
163+
if let bios::FfiOption::Some(region) = (api.memory_get_region)(region_idx) {
164+
osprintln!("\t{}: {}", region_idx, region);
105165
found = true;
106166
}
107167
}
108168
if !found {
109-
osprintln!(" None");
169+
osprintln!("\tNone");
110170
}
171+
}
111172

112-
found = false;
113-
114-
osprintln!("Audio Mixers:");
173+
/// Called when the "lsuart" command is executed.
174+
fn lsuart(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, _args: &[&str], _ctx: &mut Ctx) {
175+
let api = API.get();
176+
let mut found = false;
177+
osprintln!("UART Devices:");
115178
for dev_idx in 0..=255u8 {
116-
if let bios::FfiOption::Some(device_info) = (api.audio_mixer_channel_get_info)(dev_idx) {
117-
let dir = match device_info.direction {
118-
bios::audio::Direction::Input => "In",
119-
bios::audio::Direction::Output => "Out",
120-
bios::audio::Direction::Loopback => "Loop",
179+
if let bios::FfiOption::Some(device_info) = (api.serial_get_info)(dev_idx) {
180+
let device_type = match device_info.device_type {
181+
bios::serial::DeviceType::Rs232 => "RS232",
182+
bios::serial::DeviceType::TtlUart => "TTL",
183+
bios::serial::DeviceType::UsbCdc => "USB",
184+
bios::serial::DeviceType::Midi => "MIDI",
121185
};
122-
osprintln!(
123-
" {}: {:08} ({}) {}/{}",
124-
dev_idx,
125-
device_info.name,
126-
dir,
127-
device_info.current_level,
128-
device_info.max_level
129-
);
186+
osprintln!("\t{}: {} ({})", dev_idx, device_info.name, device_type);
130187
found = true;
131188
}
132189
}
133190
if !found {
134-
osprintln!(" None");
191+
osprintln!("\tNone");
135192
}
136193
}
137194

@@ -150,3 +207,5 @@ fn shutdown(_menu: &menu::Menu<Ctx>, item: &menu::Item<Ctx>, args: &[&str], _ctx
150207
(api.power_control)(bios::PowerMode::Off);
151208
}
152209
}
210+
211+
// End of file

0 commit comments

Comments
 (0)