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