diskio/
lib.rs

1//! Diskio library provides trait for handling disk IO devices.
2
3#![no_std]
4
5use flagset::{flags, FlagSet};
6
7flags! {
8    /// Flags of [`Status`].
9    pub enum StatusFlag: u8 {
10        /// Drive not initialized.
11        NotInitialized = 0x01,
12        /// Drive is write protected.
13        WriteProtected = 0x02,
14        /// Error occured.
15        ErrorOccured = 0x04,
16    }
17}
18
19/// Flagset of [`DiskioDevice`] status.
20pub type Status = FlagSet<StatusFlag>;
21
22/// Memory sector address.
23pub type Lba = u64;
24
25/// Sector size of a drive.
26pub type SectorSize = usize;
27
28/// Block size of a drive.
29pub type BlockSize = usize;
30
31/// Data area.
32/// `0` - start of data area.
33/// `1` - end of data area.
34pub type DataArea = (Lba, Lba);
35
36/// [`DiskioDevice`] error type.
37///
38/// `T` - Device error type.
39#[derive(Debug, Clone, Copy)]
40pub enum Error<T> {
41    /// Device isn't initialized.
42    NotInitialized,
43    /// Device is already initialized.
44    AlreadyInitialized,
45    /// The feature isn't supported by this device.
46    NotSupported,
47    /// Can't write to write protected device.
48    WriteProtected,
49    /// Invalid argument passed to device methods.
50    InvalidArgument,
51    /// Hardware error occurred.
52    Hardware(T),
53}
54
55/// Ioctl commands.
56pub enum IoctlCmd<'a> {
57    /// Complete pending write process.
58    CtrlSync,
59    /// Get media size.
60    GetSectorCount(&'a mut Lba),
61    /// Get sector size.
62    GetSectorSize(&'a mut SectorSize),
63    /// Get erase block size.
64    GetBlockSize(&'a mut BlockSize),
65    /// Inform device that the data on the block of sectors is no longer used.
66    CtrlTrim(&'a DataArea),
67}
68
69/// Represents disk IO device.
70pub trait DiskioDevice {
71    /// Device error type.
72    type HardwareError;
73
74    /// Get status of device.
75    fn status(&self) -> Status;
76
77    /// Reset device (optional).
78    fn reset(&mut self) {}
79
80    /// Initialize device.
81    fn initialize(&mut self) -> Result<(), Error<Self::HardwareError>>;
82
83    /// Read data blocks from device by address.
84    fn read(&self, buf: &mut [u8], lba: Lba) -> Result<(), Error<Self::HardwareError>>;
85
86    /// Write data blocks to device by address.
87    fn write(&self, buf: &[u8], lba: Lba) -> Result<(), Error<Self::HardwareError>>;
88
89    /// Make ioctl query to device.
90    fn ioctl(&self, cmd: IoctlCmd) -> Result<(), Error<Self::HardwareError>>;
91}