esp-storage 0.9.0

Implementation of embedded-storage traits to access unencrypted ESP32 flash
Documentation
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use embedded_storage::nor_flash::{
    ErrorType,
    MultiwriteNorFlash,
    NorFlash,
    NorFlashError,
    NorFlashErrorKind,
    ReadNorFlash,
};

#[cfg(feature = "bytewise-read")]
use crate::buffer::FlashWordBuffer;
use crate::{
    FlashStorage,
    FlashStorageError,
    buffer::{FlashSectorBuffer, uninit_slice, uninit_slice_mut},
};

impl FlashStorage<'_> {
    #[inline(always)]
    fn is_word_aligned(bytes: &[u8]) -> bool {
        // TODO: Use is_aligned_to when stabilized (see `pointer_is_aligned`)
        (bytes.as_ptr() as usize) % (Self::WORD_SIZE as usize) == 0
    }
}

impl NorFlashError for FlashStorageError {
    fn kind(&self) -> NorFlashErrorKind {
        match self {
            Self::NotAligned => NorFlashErrorKind::NotAligned,
            Self::OutOfBounds => NorFlashErrorKind::OutOfBounds,
            _ => NorFlashErrorKind::Other,
        }
    }
}

impl ErrorType for FlashStorage<'_> {
    type Error = FlashStorageError;
}

impl ReadNorFlash for FlashStorage<'_> {
    #[cfg(not(feature = "bytewise-read"))]
    const READ_SIZE: usize = Self::WORD_SIZE as _;

    #[cfg(feature = "bytewise-read")]
    const READ_SIZE: usize = 1;

    fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
        const RS: u32 = FlashStorage::READ_SIZE as u32;
        self.check_alignment::<{ RS }>(offset, bytes.len())?;
        self.check_bounds(offset, bytes.len())?;

        #[cfg(feature = "bytewise-read")]
        let (offset, bytes) = {
            let byte_offset = (offset % Self::WORD_SIZE) as usize;
            if byte_offset > 0 {
                let mut word_buffer = FlashWordBuffer::uninit();

                let offset = offset - byte_offset as u32;
                let length = bytes.len().min(Self::WORD_SIZE as usize - byte_offset);

                self.internal_read(offset, word_buffer.as_bytes_mut())?;
                let word_buffer = unsafe { word_buffer.assume_init_bytes_mut() };
                bytes[..length].copy_from_slice(&word_buffer[byte_offset..][..length]);

                (offset + Self::WORD_SIZE, &mut bytes[length..])
            } else {
                (offset, bytes)
            }
        };

        if Self::is_word_aligned(bytes) {
            // Bytes buffer is word-aligned so we can read directly to it
            for (offset, chunk) in (offset..)
                .step_by(Self::SECTOR_SIZE as _)
                .zip(bytes.chunks_mut(Self::SECTOR_SIZE as _))
            {
                // Chunk already is word aligned so we can read directly to it
                #[cfg(not(feature = "bytewise-read"))]
                self.internal_read(offset, uninit_slice_mut(chunk))?;

                #[cfg(feature = "bytewise-read")]
                {
                    let length = chunk.len();
                    let byte_length = length % Self::WORD_SIZE as usize;
                    let length = length - byte_length;

                    self.internal_read(offset, &mut uninit_slice_mut(chunk)[..length])?;

                    // Read not aligned rest of data
                    if byte_length > 0 {
                        let mut word_buffer = FlashWordBuffer::uninit();

                        self.internal_read(offset + length as u32, word_buffer.as_bytes_mut())?;
                        let word_buffer = unsafe { word_buffer.assume_init_bytes_mut() };
                        chunk[length..].copy_from_slice(&word_buffer[..byte_length]);
                    }
                }
            }
        } else {
            // Bytes buffer isn't word-aligned so we might read only via aligned buffer
            let mut buffer = FlashSectorBuffer::uninit();

            for (offset, chunk) in (offset..)
                .step_by(Self::SECTOR_SIZE as _)
                .zip(bytes.chunks_mut(Self::SECTOR_SIZE as _))
            {
                // Read to temporary buffer first (chunk length is aligned)
                #[cfg(not(feature = "bytewise-read"))]
                self.internal_read(offset, &mut buffer.as_bytes_mut()[..chunk.len()])?;

                // Read to temporary buffer first (chunk length is not aligned)
                #[cfg(feature = "bytewise-read")]
                {
                    let length = chunk.len();
                    let byte_length = length % Self::WORD_SIZE as usize;
                    let length = if byte_length > 0 {
                        length - byte_length + Self::WORD_SIZE as usize
                    } else {
                        length
                    };

                    self.internal_read(offset, &mut buffer.as_bytes_mut()[..length])?;
                }
                let buffer = unsafe { buffer.assume_init_bytes() };

                // Copy to bytes buffer
                chunk.copy_from_slice(&buffer[..chunk.len()]);
            }
        }

        Ok(())
    }

    fn capacity(&self) -> usize {
        self.capacity
    }
}

impl NorFlash for FlashStorage<'_> {
    const WRITE_SIZE: usize = Self::WORD_SIZE as _;
    const ERASE_SIZE: usize = Self::SECTOR_SIZE as _;

    fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
        const WS: u32 = FlashStorage::WORD_SIZE;
        self.check_alignment::<{ WS }>(offset, bytes.len())?;
        self.check_bounds(offset, bytes.len())?;

        if Self::is_word_aligned(bytes) {
            // Bytes buffer is word-aligned so we can write directly from it
            for (offset, chunk) in (offset..)
                .step_by(Self::SECTOR_SIZE as _)
                .zip(bytes.chunks(Self::SECTOR_SIZE as _))
            {
                // Chunk already is word aligned so we can write directly from it
                self.internal_write(offset, chunk)?;
            }
        } else {
            // Bytes buffer isn't word-aligned so we might write only via aligned buffer
            let mut buffer = FlashSectorBuffer::uninit();

            for (offset, chunk) in (offset..)
                .step_by(Self::SECTOR_SIZE as _)
                .zip(bytes.chunks(Self::SECTOR_SIZE as _))
            {
                // Copy to temporary buffer first
                buffer.as_bytes_mut()[..chunk.len()].copy_from_slice(uninit_slice(chunk));
                // Write from temporary buffer
                self.internal_write(offset, unsafe {
                    &buffer.assume_init_bytes()[..chunk.len()]
                })?;
            }
        }

        Ok(())
    }

    fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
        let len = (to - from) as _;
        const SZ: u32 = FlashStorage::SECTOR_SIZE;
        self.check_alignment::<{ SZ }>(from, len)?;
        self.check_bounds(from, len)?;

        // First erase by sector up to the block boundary.
        let mut address = from;
        while address < to && (address % Self::BLOCK_SIZE) != 0 {
            self.internal_erase_sector(address / Self::SECTOR_SIZE)?;
            address += Self::SECTOR_SIZE;
        }

        // Then erase as much as possible by blocks.
        while (to - address) >= Self::BLOCK_SIZE {
            self.internal_erase_block(address / Self::BLOCK_SIZE)?;
            address += Self::BLOCK_SIZE;
        }

        // Finally, erase any remaining sectors.
        while address < to {
            self.internal_erase_sector(address / Self::SECTOR_SIZE)?;
            address += Self::SECTOR_SIZE;
        }

        Ok(())
    }
}

impl MultiwriteNorFlash for FlashStorage<'_> {}

// Run the tests with `--test-threads=1` - the emulation is not multithread safe
#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::Flash;

    const WORD_SIZE: u32 = 4;
    const SECTOR_SIZE: u32 = 4 << 10;
    const BLOCK_SIZE: u32 = 4 << 14;
    const NUM_BLOCKS: u32 = 3;
    const FLASH_SIZE: u32 = BLOCK_SIZE * NUM_BLOCKS;
    const MAX_OFFSET: u32 = SECTOR_SIZE * 1;
    const MAX_LENGTH: u32 = SECTOR_SIZE * 2;

    #[repr(C, align(4))]
    struct TestBuffer {
        data: [u8; FLASH_SIZE as _],
    }

    impl TestBuffer {
        const fn seq() -> Self {
            let mut data = [0u8; FLASH_SIZE as _];
            let mut index = 0;
            while index < FLASH_SIZE {
                data[index as usize] = (index & 0xff) as u8;
                index += 1;
            }
            Self { data }
        }
    }

    impl Default for TestBuffer {
        fn default() -> Self {
            Self {
                data: [0u8; FLASH_SIZE as usize],
            }
        }
    }

    #[cfg(not(miri))]
    fn range_gen<const ALIGN: u32, const MAX_OFF: u32, const MAX_LEN: u32>(
        aligned: Option<bool>,
    ) -> impl Iterator<Item = (u32, u32)> {
        (0..=MAX_OFF).flat_map(move |off| {
            (0..=MAX_LEN - off)
                .filter(move |len| {
                    aligned
                        .map(|aligned| aligned == (off % ALIGN == 0 && len % ALIGN == 0))
                        .unwrap_or(true)
                })
                .map(move |len| (off, len))
        })
    }

    #[cfg(miri)]
    fn range_gen<const ALIGN: u32, const MAX_OFF: u32, const MAX_LEN: u32>(
        aligned: Option<bool>,
    ) -> impl Iterator<Item = (u32, u32)> {
        // MIRI is very slow - just use a couple of combinations
        match aligned {
            Some(true) => vec![(0, 4), (0, 8), (0, 16), (0, 32), (0, 1024)],
            Some(false) => vec![(3, 7), (11, 11)],
            None => vec![
                (0, 4),
                (0, 8),
                (0, 16),
                (0, 32),
                (0, 1024),
                (3, 7),
                (11, 11),
                (0, 4098),
            ],
        }
        .into_iter()
    }

    #[test]
    #[cfg(not(feature = "bytewise-read"))]
    fn aligned_read() {
        let mut flash = FlashStorage::new(Flash::new());
        flash.capacity = FLASH_SIZE as usize;
        let src = TestBuffer::seq();
        let mut data = TestBuffer::default();

        flash.erase(0, FLASH_SIZE).unwrap();
        flash.write(0, &src.data).unwrap();

        for (off, len) in range_gen::<WORD_SIZE, MAX_OFFSET, MAX_LENGTH>(Some(true)) {
            flash.read(off, &mut data.data[..len as usize]).unwrap();
            assert_eq!(
                data.data[..len as usize],
                src.data[off as usize..][..len as usize]
            );
        }
    }

    #[test]
    #[cfg(not(feature = "bytewise-read"))]
    fn not_aligned_read_aligned_buffer() {
        let mut flash = FlashStorage::new(Flash::new());
        flash.capacity = FLASH_SIZE as usize;
        let mut data = TestBuffer::default();

        for (off, len) in range_gen::<WORD_SIZE, MAX_OFFSET, MAX_LENGTH>(Some(false)) {
            flash.read(off, &mut data.data[..len as usize]).unwrap_err();
        }
    }

    #[test]
    #[cfg(not(feature = "bytewise-read"))]
    fn aligned_read_not_aligned_buffer() {
        let mut flash = FlashStorage::new(Flash::new());
        flash.capacity = FLASH_SIZE as usize;
        let src = TestBuffer::seq();
        let mut data = TestBuffer::default();

        flash.erase(0, FLASH_SIZE).unwrap();
        flash.write(0, &src.data).unwrap();

        for (off, len) in range_gen::<WORD_SIZE, MAX_OFFSET, MAX_LENGTH>(Some(true)) {
            flash
                .read(off, &mut data.data[1..][..len as usize])
                .unwrap();
            assert_eq!(
                data.data[1..][..len as usize],
                src.data[off as usize..][..len as usize]
            );
        }
    }

    #[test]
    #[cfg(feature = "bytewise-read")]
    fn bytewise_read_aligned_buffer() {
        let mut flash = FlashStorage::new(Flash::new());

        flash.capacity = FLASH_SIZE as usize;
        let src = TestBuffer::seq();
        let mut data = TestBuffer::default();

        flash.erase(0, FLASH_SIZE).unwrap();
        flash.write(0, &src.data).unwrap();

        for (off, len) in range_gen::<WORD_SIZE, MAX_OFFSET, MAX_LENGTH>(None) {
            flash.read(off, &mut data.data[..len as usize]).unwrap();
            assert_eq!(
                data.data[..len as usize],
                src.data[off as usize..][..len as usize]
            );
        }
    }

    #[test]
    #[cfg(feature = "bytewise-read")]
    fn bytewise_read_not_aligned_buffer() {
        let mut flash = FlashStorage::new(Flash::new());

        flash.capacity = FLASH_SIZE as usize;
        let src = TestBuffer::seq();
        let mut data = TestBuffer::default();

        flash.erase(0, FLASH_SIZE).unwrap();
        flash.write(0, &src.data).unwrap();

        for (off, len) in range_gen::<WORD_SIZE, MAX_OFFSET, MAX_LENGTH>(None) {
            flash
                .read(off, &mut data.data[1..][..len as usize])
                .unwrap();
            assert_eq!(
                data.data[1..][..len as usize],
                src.data[off as usize..][..len as usize]
            );
        }
    }

    #[test]
    fn write_not_aligned_buffer() {
        let mut flash = FlashStorage::new(Flash::new());
        flash.capacity = FLASH_SIZE as usize;
        let mut read_data = TestBuffer::default();
        let write_data = TestBuffer::seq();

        flash.erase(0, FLASH_SIZE).unwrap();
        flash.write(0, &write_data.data[1..129]).unwrap();

        flash.read(0, &mut read_data.data[..128]).unwrap();

        assert_eq!(&read_data.data[..128], &write_data.data[1..129]);
    }

    #[test]
    fn erase_across_blocks() {
        let mut flash = FlashStorage::new(Flash::new());
        flash.capacity = FLASH_SIZE as usize;
        let mut read_data = TestBuffer::default();
        let write_data = [0u8; SECTOR_SIZE as usize];

        // An entire block and some sectors before and after
        let from = BLOCK_SIZE - (2 * SECTOR_SIZE);
        let to = (2 * BLOCK_SIZE) + (1 * SECTOR_SIZE);

        // Clear area before and after erase
        flash.erase(from - SECTOR_SIZE, from).unwrap();
        flash.write(from - SECTOR_SIZE, &write_data).unwrap();
        flash.erase(to, to + SECTOR_SIZE).unwrap();
        flash.write(to, &write_data).unwrap();

        // Erase and verify that only the desired parts were touched
        flash.erase(from, to).unwrap();
        flash.read(0, &mut read_data.data).unwrap();
        for i in from..to {
            assert_eq!(read_data.data[i as usize], 0xFF);
        }
        assert_eq!(read_data.data[(from - 1) as usize], 0x0);
        assert_eq!(read_data.data[to as usize], 0x0);
    }
}