Skip to main content

ax_driver_block/
lib.rs

1//! Common traits and types for block storage device drivers (i.e. disk).
2
3#![no_std]
4#![cfg_attr(doc, feature(doc_cfg))]
5
6extern crate alloc;
7
8#[cfg(test)]
9extern crate std;
10
11use alloc::boxed::Box;
12
13#[cfg(feature = "bcm2835-sdhci")]
14pub mod bcm2835sdhci;
15
16#[cfg(feature = "cvsd")]
17pub mod cvsd;
18
19#[cfg(feature = "ramdisk")]
20pub mod ramdisk;
21
22#[cfg(feature = "ramdisk-static")]
23pub mod ramdisk_static;
24
25#[cfg(feature = "ahci")]
26pub mod ahci;
27pub mod partition;
28#[cfg(feature = "sdmmc")]
29pub mod sdmmc;
30
31#[doc(no_inline)]
32pub use ax_driver_base::{BaseDriverOps, DevError, DevResult, DeviceType};
33
34/// Operations that require a block storage device driver to implement.
35pub trait BlockDriverOps: BaseDriverOps {
36    /// The number of blocks in this storage device.
37    ///
38    /// The total size of the device is `num_blocks() * block_size()`.
39    fn num_blocks(&self) -> u64;
40    /// The size of each block in bytes.
41    fn block_size(&self) -> usize;
42
43    /// Reads blocked data from the given block.
44    ///
45    /// The size of the buffer may exceed the block size, in which case multiple
46    /// contiguous blocks will be read.
47    fn read_block(&mut self, block_id: u64, buf: &mut [u8]) -> DevResult;
48
49    /// Writes blocked data to the given block.
50    ///
51    /// The size of the buffer may exceed the block size, in which case multiple
52    /// contiguous blocks will be written.
53    fn write_block(&mut self, block_id: u64, buf: &[u8]) -> DevResult;
54
55    /// Flushes the device to write all pending data to the storage.
56    fn flush(&mut self) -> DevResult;
57}
58
59impl<T: BlockDriverOps + ?Sized> BlockDriverOps for Box<T> {
60    fn num_blocks(&self) -> u64 {
61        (**self).num_blocks()
62    }
63
64    fn block_size(&self) -> usize {
65        (**self).block_size()
66    }
67
68    fn read_block(&mut self, block_id: u64, buf: &mut [u8]) -> DevResult {
69        (**self).read_block(block_id, buf)
70    }
71
72    fn write_block(&mut self, block_id: u64, buf: &[u8]) -> DevResult {
73        (**self).write_block(block_id, buf)
74    }
75
76    fn flush(&mut self) -> DevResult {
77        (**self).flush()
78    }
79}