1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::convert::Infallible;
use core::marker::PhantomData;

use futures::future;

use sel4_async_block_io::{
    access::{Access, Witness},
    constant_block_sizes, BlockIO, Operation,
};

pub use embedded_fat as fat;

pub struct BlockIOWrapper<T, A> {
    inner: T,
    _phantom: PhantomData<A>,
}

impl<T, A> BlockIOWrapper<T, A> {
    pub fn new(inner: T) -> Self {
        Self {
            inner,
            _phantom: PhantomData,
        }
    }
}

impl<T: BlockIO<A, BlockSize = constant_block_sizes::BlockSize512>, A: Access> fat::BlockDevice
    for BlockIOWrapper<T, A>
{
    type Error = Infallible;

    async fn read(
        &self,
        blocks: &mut [fat::Block],
        start_block_idx: fat::BlockIdx,
        _reason: &str,
    ) -> Result<(), Self::Error> {
        future::join_all(blocks.iter_mut().enumerate().map(|(i, block)| async move {
            let block_idx = u64::from(start_block_idx.0)
                .checked_add(i.try_into().unwrap())
                .unwrap();
            self.inner
                .read_or_write_blocks(
                    block_idx,
                    Operation::Read {
                        buf: &mut block.contents,
                        witness: A::ReadWitness::TRY_WITNESS.unwrap(),
                    },
                )
                .await
        }))
        .await;
        Ok(())
    }

    async fn write(
        &self,
        blocks: &[fat::Block],
        start_block_idx: fat::BlockIdx,
    ) -> Result<(), Self::Error> {
        future::join_all(blocks.iter().enumerate().map(|(i, block)| async move {
            let block_idx = u64::from(start_block_idx.0)
                .checked_add(i.try_into().unwrap())
                .unwrap();
            self.inner
                .read_or_write_blocks(
                    block_idx,
                    Operation::Write {
                        buf: &block.contents,
                        witness: A::WriteWitness::TRY_WITNESS.unwrap(),
                    },
                )
                .await
        }))
        .await;
        Ok(())
    }

    async fn num_blocks(&self) -> Result<fat::BlockCount, Self::Error> {
        Ok(fat::BlockCount(self.inner.num_blocks().try_into().unwrap()))
    }
}