Skip to main content

kernel/hil/
nonvolatile_storage.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Generic interface for nonvolatile memory.
6
7use crate::errorcode::ErrorCode;
8
9/// Simple interface for reading and writing nonvolatile memory. It is expected
10/// that drivers for nonvolatile memory would implement this trait.
11pub trait NonvolatileStorage<'a> {
12    fn set_client(&self, client: &'a dyn NonvolatileStorageClient);
13
14    /// Read `length` bytes starting at address `address` in to the provided
15    /// buffer. The buffer must be at least `length` bytes long. The address
16    /// must be in the address space of the physical storage.
17    ///
18    /// On error, the buffer is returned alongside the `ErrorCode` so the
19    /// caller does not lose ownership of it.
20    fn read(
21        &self,
22        buffer: &'static mut [u8],
23        address: usize,
24        length: usize,
25    ) -> Result<(), (ErrorCode, &'static mut [u8])>;
26
27    /// Write `length` bytes starting at address `address` from the provided
28    /// buffer. The buffer must be at least `length` bytes long. This address
29    /// must be in the address space of the physical storage.
30    ///
31    /// On error, the buffer is returned alongside the `ErrorCode` so the
32    /// caller does not lose ownership of it.
33    fn write(
34        &self,
35        buffer: &'static mut [u8],
36        address: usize,
37        length: usize,
38    ) -> Result<(), (ErrorCode, &'static mut [u8])>;
39}
40
41/// Client interface for nonvolatile storage.
42pub trait NonvolatileStorageClient {
43    /// `read_done` is called when the implementor is finished reading in to the
44    /// buffer. The callback returns the buffer and the number of bytes that
45    /// were actually read.
46    fn read_done(&self, buffer: &'static mut [u8], length: usize);
47
48    /// `write_done` is called when the implementor is finished writing from the
49    /// buffer. The callback returns the buffer and the number of bytes that
50    /// were actually written.
51    fn write_done(&self, buffer: &'static mut [u8], length: usize);
52}