|
| 1 | +//! Block Device related commands for Neotron OS |
| 2 | +
|
| 3 | +use crate::{bios, print, println, Ctx, API}; |
| 4 | + |
| 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 | + |
| 14 | +pub static READ_ITEM: menu::Item<Ctx> = menu::Item { |
| 15 | + item_type: menu::ItemType::Callback { |
| 16 | + function: read_block, |
| 17 | + parameters: &[ |
| 18 | + menu::Parameter::Mandatory { |
| 19 | + parameter_name: "device_idx", |
| 20 | + help: Some("The block device ID to fetch from"), |
| 21 | + }, |
| 22 | + menu::Parameter::Mandatory { |
| 23 | + parameter_name: "block_idx", |
| 24 | + help: Some("The block to fetch, 0..num_blocks"), |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + command: "readblk", |
| 29 | + help: Some("List all the Block Devices"), |
| 30 | +}; |
| 31 | + |
| 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 | + println!("Block Devices:"); |
| 38 | + for dev_idx in 0..=255u8 { |
| 39 | + if let bios::Option::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 | + println!("Device {}:", dev_idx); |
| 52 | + println!(" Name: {}", device_info.name); |
| 53 | + println!(" Type: {:?}", device_info.device_type); |
| 54 | + println!(" Block size: {}", device_info.block_size); |
| 55 | + println!(" Num Blocks: {}", device_info.num_blocks); |
| 56 | + println!( |
| 57 | + " Card Size: {}.{} {} ({}.{} {})", |
| 58 | + bsize / 10, |
| 59 | + bsize % 10, |
| 60 | + bunits, |
| 61 | + dsize / 10, |
| 62 | + dsize % 10, |
| 63 | + dunits |
| 64 | + ); |
| 65 | + println!(" Ejectable: {}", device_info.ejectable); |
| 66 | + println!(" Removable: {}", device_info.removable); |
| 67 | + println!(" Media Present: {}", device_info.media_present); |
| 68 | + println!(" Read Only: {}", device_info.read_only); |
| 69 | + found = true; |
| 70 | + } |
| 71 | + } |
| 72 | + if !found { |
| 73 | + println!(" None"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Called when the "read_block" command is executed. |
| 78 | +fn read_block(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], _ctx: &mut Ctx) { |
| 79 | + let api = API.get(); |
| 80 | + let Ok(dev_idx) = args[0].parse::<u8>() else { |
| 81 | + println!("Couldn't parse {:?}", args[0]); |
| 82 | + return; |
| 83 | + }; |
| 84 | + let Ok(block_idx) = args[1].parse::<u64>() else { |
| 85 | + println!("Couldn't parse {:?}", args[1]); |
| 86 | + return; |
| 87 | + }; |
| 88 | + println!("Reading block {}:", block_idx); |
| 89 | + let mut buffer = [0u8; 512]; |
| 90 | + match (api.block_read)( |
| 91 | + dev_idx, |
| 92 | + bios::block_dev::BlockIdx(block_idx), |
| 93 | + 1, |
| 94 | + bios::ApiBuffer::new(&mut buffer), |
| 95 | + ) { |
| 96 | + bios::Result::Ok(_) => { |
| 97 | + // Carry on |
| 98 | + let mut count = 0; |
| 99 | + for chunk in buffer.chunks(16) { |
| 100 | + print!("{:03x}: ", count); |
| 101 | + for b in chunk { |
| 102 | + print!("{:02x} ", *b); |
| 103 | + } |
| 104 | + print!(" "); |
| 105 | + for b in chunk { |
| 106 | + let c = char::from(*b); |
| 107 | + print!("{}", if c.is_ascii_graphic() { c } else { '.' }); |
| 108 | + } |
| 109 | + count += chunk.len(); |
| 110 | + println!(); |
| 111 | + } |
| 112 | + } |
| 113 | + bios::Result::Err(e) => { |
| 114 | + println!("Failed to read: {:?}", e); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments