Merge remote-tracking branch 'upstream/master' into incremental-hash
This commit is contained in:
@ -31,7 +31,7 @@ where
|
||||
}
|
||||
|
||||
/// Extension of the embedded-storage flash type information with block size and erase value.
|
||||
pub trait Flash: NorFlash + ReadNorFlash {
|
||||
pub trait Flash: NorFlash {
|
||||
/// The block size that should be used when writing to flash. For most builtin flashes, this is the same as the erase
|
||||
/// size of the flash, but for external QSPI flash modules, this can be lower.
|
||||
const BLOCK_SIZE: usize;
|
||||
@ -60,9 +60,11 @@ pub trait FlashConfig {
|
||||
/// different page sizes and flash write sizes.
|
||||
pub struct BootLoader {
|
||||
// Page with current state of bootloader. The state partition has the following format:
|
||||
// | Range | Description |
|
||||
// | 0 - WRITE_SIZE | Magic indicating bootloader state. BOOT_MAGIC means boot, SWAP_MAGIC means swap. |
|
||||
// | WRITE_SIZE - N | Progress index used while swapping or reverting |
|
||||
// All ranges are in multiples of WRITE_SIZE bytes.
|
||||
// | Range | Description |
|
||||
// | 0..1 | Magic indicating bootloader state. BOOT_MAGIC means boot, SWAP_MAGIC means swap. |
|
||||
// | 1..2 | Progress validity. ERASE_VALUE means valid, !ERASE_VALUE means invalid. |
|
||||
// | 2..2 + N | Progress index used while swapping or reverting |
|
||||
state: Partition,
|
||||
// Location of the partition which will be booted from
|
||||
active: Partition,
|
||||
@ -192,12 +194,17 @@ impl BootLoader {
|
||||
trace!("Reverting");
|
||||
self.revert(p, magic, page)?;
|
||||
|
||||
// Overwrite magic and reset progress
|
||||
let state_flash = p.state();
|
||||
|
||||
// Invalidate progress
|
||||
magic.fill(!P::STATE::ERASE_VALUE);
|
||||
self.state.write_blocking(state_flash, 0, magic)?;
|
||||
self.state
|
||||
.write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, magic)?;
|
||||
|
||||
// Clear magic and progress
|
||||
self.state.wipe_blocking(state_flash)?;
|
||||
|
||||
// Set magic
|
||||
magic.fill(BOOT_MAGIC);
|
||||
self.state.write_blocking(state_flash, 0, magic)?;
|
||||
}
|
||||
@ -215,28 +222,34 @@ impl BootLoader {
|
||||
|
||||
fn current_progress<P: FlashConfig>(&mut self, config: &mut P, aligned: &mut [u8]) -> Result<usize, BootError> {
|
||||
let write_size = aligned.len();
|
||||
let max_index = ((self.state.len() - write_size) / write_size) - 1;
|
||||
let max_index = ((self.state.len() - write_size) / write_size) - 2;
|
||||
aligned.fill(!P::STATE::ERASE_VALUE);
|
||||
|
||||
let state_flash = config.state();
|
||||
for i in 0..max_index {
|
||||
|
||||
self.state
|
||||
.read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, aligned)?;
|
||||
if aligned.iter().any(|&b| b != P::STATE::ERASE_VALUE) {
|
||||
// Progress is invalid
|
||||
return Ok(max_index);
|
||||
}
|
||||
|
||||
for index in 0..max_index {
|
||||
self.state
|
||||
.read_blocking(state_flash, (write_size + i * write_size) as u32, aligned)?;
|
||||
.read_blocking(state_flash, (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, aligned)?;
|
||||
|
||||
if aligned.iter().any(|&b| b == P::STATE::ERASE_VALUE) {
|
||||
return Ok(i);
|
||||
return Ok(index);
|
||||
}
|
||||
}
|
||||
Ok(max_index)
|
||||
}
|
||||
|
||||
fn update_progress<P: FlashConfig>(&mut self, idx: usize, p: &mut P, magic: &mut [u8]) -> Result<(), BootError> {
|
||||
let write_size = magic.len();
|
||||
|
||||
fn update_progress<P: FlashConfig>(&mut self, index: usize, p: &mut P, magic: &mut [u8]) -> Result<(), BootError> {
|
||||
let aligned = magic;
|
||||
aligned.fill(!P::STATE::ERASE_VALUE);
|
||||
self.state
|
||||
.write_blocking(p.state(), (write_size + idx * write_size) as u32, aligned)?;
|
||||
.write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, aligned)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -360,7 +373,7 @@ fn assert_partitions(active: Partition, dfu: Partition, state: Partition, page_s
|
||||
assert_eq!(active.len() % page_size, 0);
|
||||
assert_eq!(dfu.len() % page_size, 0);
|
||||
assert!(dfu.len() - active.len() >= page_size);
|
||||
assert!(2 * (active.len() / page_size) <= (state.len() - write_size) / write_size);
|
||||
assert!(2 + 2 * (active.len() / page_size) <= state.len() / write_size);
|
||||
}
|
||||
|
||||
/// A flash wrapper implementing the Flash and embedded_storage traits.
|
||||
|
@ -234,11 +234,24 @@ impl FirmwareUpdater {
|
||||
self.state.read(state_flash, 0, aligned).await?;
|
||||
|
||||
if aligned.iter().any(|&b| b != magic) {
|
||||
aligned.fill(0);
|
||||
// Read progress validity
|
||||
self.state.read(state_flash, F::WRITE_SIZE as u32, aligned).await?;
|
||||
|
||||
self.state.write(state_flash, 0, aligned).await?;
|
||||
// FIXME: Do not make this assumption.
|
||||
const STATE_ERASE_VALUE: u8 = 0xFF;
|
||||
|
||||
if aligned.iter().any(|&b| b != STATE_ERASE_VALUE) {
|
||||
// The current progress validity marker is invalid
|
||||
} else {
|
||||
// Invalidate progress
|
||||
aligned.fill(!STATE_ERASE_VALUE);
|
||||
self.state.write(state_flash, F::WRITE_SIZE as u32, aligned).await?;
|
||||
}
|
||||
|
||||
// Clear magic and progress
|
||||
self.state.wipe(state_flash).await?;
|
||||
|
||||
// Set magic
|
||||
aligned.fill(magic);
|
||||
self.state.write(state_flash, 0, aligned).await?;
|
||||
}
|
||||
@ -441,11 +454,24 @@ impl FirmwareUpdater {
|
||||
self.state.read_blocking(state_flash, 0, aligned)?;
|
||||
|
||||
if aligned.iter().any(|&b| b != magic) {
|
||||
aligned.fill(0);
|
||||
// Read progress validity
|
||||
self.state.read_blocking(state_flash, F::WRITE_SIZE as u32, aligned)?;
|
||||
|
||||
self.state.write_blocking(state_flash, 0, aligned)?;
|
||||
// FIXME: Do not make this assumption.
|
||||
const STATE_ERASE_VALUE: u8 = 0xFF;
|
||||
|
||||
if aligned.iter().any(|&b| b != STATE_ERASE_VALUE) {
|
||||
// The current progress validity marker is invalid
|
||||
} else {
|
||||
// Invalidate progress
|
||||
aligned.fill(!STATE_ERASE_VALUE);
|
||||
self.state.write_blocking(state_flash, F::WRITE_SIZE as u32, aligned)?;
|
||||
}
|
||||
|
||||
// Clear magic and progress
|
||||
self.state.wipe_blocking(state_flash)?;
|
||||
|
||||
// Set magic
|
||||
aligned.fill(magic);
|
||||
self.state.write_blocking(state_flash, 0, aligned)?;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ mod fmt;
|
||||
mod boot_loader;
|
||||
mod digest_adapters;
|
||||
mod firmware_updater;
|
||||
mod mem_flash;
|
||||
mod partition;
|
||||
|
||||
pub use boot_loader::{BootError, BootFlash, BootLoader, Flash, FlashConfig, MultiFlashConfig, SingleFlashConfig};
|
||||
@ -45,13 +46,10 @@ impl<const N: usize> AsMut<[u8]> for AlignedBuffer<N> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::convert::Infallible;
|
||||
|
||||
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
|
||||
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
|
||||
use futures::executor::block_on;
|
||||
|
||||
use super::*;
|
||||
use crate::mem_flash::MemFlash;
|
||||
|
||||
/*
|
||||
#[test]
|
||||
@ -74,8 +72,8 @@ mod tests {
|
||||
const ACTIVE: Partition = Partition::new(4096, 61440);
|
||||
const DFU: Partition = Partition::new(61440, 122880);
|
||||
|
||||
let mut flash = MemFlash::<131072, 4096, 4>([0xff; 131072]);
|
||||
flash.0[0..4].copy_from_slice(&[BOOT_MAGIC; 4]);
|
||||
let mut flash = MemFlash::<131072, 4096, 4>::default();
|
||||
flash.mem[0..4].copy_from_slice(&[BOOT_MAGIC; 4]);
|
||||
let mut flash = SingleFlashConfig::new(&mut flash);
|
||||
|
||||
let mut bootloader: BootLoader = BootLoader::new(ACTIVE, DFU, STATE);
|
||||
@ -94,14 +92,14 @@ mod tests {
|
||||
const STATE: Partition = Partition::new(0, 4096);
|
||||
const ACTIVE: Partition = Partition::new(4096, 61440);
|
||||
const DFU: Partition = Partition::new(61440, 122880);
|
||||
let mut flash = MemFlash::<131072, 4096, 4>([0xff; 131072]);
|
||||
let mut flash = MemFlash::<131072, 4096, 4>::random();
|
||||
|
||||
let original: [u8; ACTIVE.len()] = [rand::random::<u8>(); ACTIVE.len()];
|
||||
let update: [u8; DFU.len()] = [rand::random::<u8>(); DFU.len()];
|
||||
let mut aligned = [0; 4];
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
flash.0[i] = original[i - ACTIVE.from];
|
||||
flash.mem[i] = original[i - ACTIVE.from];
|
||||
}
|
||||
|
||||
let mut bootloader: BootLoader = BootLoader::new(ACTIVE, DFU, STATE);
|
||||
@ -123,12 +121,12 @@ mod tests {
|
||||
);
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
assert_eq!(flash.0[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
assert_eq!(flash.mem[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
}
|
||||
|
||||
// First DFU page is untouched
|
||||
for i in DFU.from + 4096..DFU.to {
|
||||
assert_eq!(flash.0[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
assert_eq!(flash.mem[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
}
|
||||
|
||||
// Running again should cause a revert
|
||||
@ -140,12 +138,12 @@ mod tests {
|
||||
);
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
assert_eq!(flash.0[i], original[i - ACTIVE.from], "Index {}", i);
|
||||
assert_eq!(flash.mem[i], original[i - ACTIVE.from], "Index {}", i);
|
||||
}
|
||||
|
||||
// Last page is untouched
|
||||
for i in DFU.from..DFU.to - 4096 {
|
||||
assert_eq!(flash.0[i], update[i - DFU.from], "Index {}", i);
|
||||
assert_eq!(flash.mem[i], update[i - DFU.from], "Index {}", i);
|
||||
}
|
||||
|
||||
// Mark as booted
|
||||
@ -165,16 +163,16 @@ mod tests {
|
||||
const ACTIVE: Partition = Partition::new(4096, 16384);
|
||||
const DFU: Partition = Partition::new(0, 16384);
|
||||
|
||||
let mut active = MemFlash::<16384, 4096, 8>([0xff; 16384]);
|
||||
let mut dfu = MemFlash::<16384, 2048, 8>([0xff; 16384]);
|
||||
let mut state = MemFlash::<4096, 128, 4>([0xff; 4096]);
|
||||
let mut active = MemFlash::<16384, 4096, 8>::random();
|
||||
let mut dfu = MemFlash::<16384, 2048, 8>::random();
|
||||
let mut state = MemFlash::<4096, 128, 4>::random();
|
||||
let mut aligned = [0; 4];
|
||||
|
||||
let original: [u8; ACTIVE.len()] = [rand::random::<u8>(); ACTIVE.len()];
|
||||
let update: [u8; DFU.len()] = [rand::random::<u8>(); DFU.len()];
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
active.0[i] = original[i - ACTIVE.from];
|
||||
active.mem[i] = original[i - ACTIVE.from];
|
||||
}
|
||||
|
||||
let mut updater = FirmwareUpdater::new(DFU, STATE);
|
||||
@ -202,12 +200,12 @@ mod tests {
|
||||
);
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
assert_eq!(active.0[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
assert_eq!(active.mem[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
}
|
||||
|
||||
// First DFU page is untouched
|
||||
for i in DFU.from + 4096..DFU.to {
|
||||
assert_eq!(dfu.0[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
assert_eq!(dfu.mem[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,15 +217,15 @@ mod tests {
|
||||
const DFU: Partition = Partition::new(0, 16384);
|
||||
|
||||
let mut aligned = [0; 4];
|
||||
let mut active = MemFlash::<16384, 2048, 4>([0xff; 16384]);
|
||||
let mut dfu = MemFlash::<16384, 4096, 8>([0xff; 16384]);
|
||||
let mut state = MemFlash::<4096, 128, 4>([0xff; 4096]);
|
||||
let mut active = MemFlash::<16384, 2048, 4>::random();
|
||||
let mut dfu = MemFlash::<16384, 4096, 8>::random();
|
||||
let mut state = MemFlash::<4096, 128, 4>::random();
|
||||
|
||||
let original: [u8; ACTIVE.len()] = [rand::random::<u8>(); ACTIVE.len()];
|
||||
let update: [u8; DFU.len()] = [rand::random::<u8>(); DFU.len()];
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
active.0[i] = original[i - ACTIVE.from];
|
||||
active.mem[i] = original[i - ACTIVE.from];
|
||||
}
|
||||
|
||||
let mut updater = FirmwareUpdater::new(DFU, STATE);
|
||||
@ -254,12 +252,12 @@ mod tests {
|
||||
);
|
||||
|
||||
for i in ACTIVE.from..ACTIVE.to {
|
||||
assert_eq!(active.0[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
assert_eq!(active.mem[i], update[i - ACTIVE.from], "Index {}", i);
|
||||
}
|
||||
|
||||
// First DFU page is untouched
|
||||
for i in DFU.from + 4096..DFU.to {
|
||||
assert_eq!(dfu.0[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
assert_eq!(dfu.mem[i], original[i - DFU.from - 4096], "Index {}", i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,13 +287,13 @@ mod tests {
|
||||
|
||||
const STATE: Partition = Partition::new(0, 4096);
|
||||
const DFU: Partition = Partition::new(4096, 8192);
|
||||
let mut flash = MemFlash::<8192, 4096, 4>([0xff; 8192]);
|
||||
let mut flash = MemFlash::<8192, 4096, 4>::default();
|
||||
|
||||
let firmware_len = firmware.len();
|
||||
|
||||
let mut write_buf = [0; 4096];
|
||||
write_buf[0..firmware_len].copy_from_slice(firmware);
|
||||
NorFlash::write(&mut flash, DFU.from as u32, &write_buf).unwrap();
|
||||
DFU.write_blocking(&mut flash, 0, &write_buf).unwrap();
|
||||
|
||||
// On with the test
|
||||
|
||||
@ -312,113 +310,4 @@ mod tests {
|
||||
))
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize>(pub [u8; SIZE]);
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = ERASE_SIZE;
|
||||
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
let from = from as usize;
|
||||
let to = to as usize;
|
||||
assert!(from % ERASE_SIZE == 0);
|
||||
assert!(to % ERASE_SIZE == 0, "To: {}, erase size: {}", to, ERASE_SIZE);
|
||||
for i in from..to {
|
||||
self.0[i] = 0xFF;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
|
||||
assert!(data.len() % WRITE_SIZE == 0);
|
||||
assert!(offset as usize % WRITE_SIZE == 0);
|
||||
assert!(offset as usize + data.len() <= SIZE);
|
||||
|
||||
self.0[offset as usize..offset as usize + data.len()].copy_from_slice(data);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
fn read(&mut self, offset: u32, buf: &mut [u8]) -> Result<(), Self::Error> {
|
||||
let len = buf.len();
|
||||
buf[..].copy_from_slice(&self.0[offset as usize..offset as usize + len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> super::Flash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const BLOCK_SIZE: usize = ERASE_SIZE;
|
||||
const ERASE_VALUE: u8 = 0xFF;
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncReadNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
async fn read(&mut self, offset: u32, buf: &mut [u8]) -> Result<(), Self::Error> {
|
||||
let len = buf.len();
|
||||
buf[..].copy_from_slice(&self.0[offset as usize..offset as usize + len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = ERASE_SIZE;
|
||||
|
||||
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
let from = from as usize;
|
||||
let to = to as usize;
|
||||
assert!(from % ERASE_SIZE == 0);
|
||||
assert!(to % ERASE_SIZE == 0);
|
||||
for i in from..to {
|
||||
self.0[i] = 0xFF;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
|
||||
info!("Writing {} bytes to 0x{:x}", data.len(), offset);
|
||||
assert!(data.len() % WRITE_SIZE == 0);
|
||||
assert!(offset as usize % WRITE_SIZE == 0);
|
||||
assert!(
|
||||
offset as usize + data.len() <= SIZE,
|
||||
"OFFSET: {}, LEN: {}, FLASH SIZE: {}",
|
||||
offset,
|
||||
data.len(),
|
||||
SIZE
|
||||
);
|
||||
|
||||
self.0[offset as usize..offset as usize + data.len()].copy_from_slice(data);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
156
embassy-boot/boot/src/mem_flash.rs
Normal file
156
embassy-boot/boot/src/mem_flash.rs
Normal file
@ -0,0 +1,156 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use core::ops::{Bound, Range, RangeBounds};
|
||||
|
||||
use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
|
||||
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
|
||||
|
||||
use crate::Flash;
|
||||
|
||||
pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
|
||||
pub mem: [u8; SIZE],
|
||||
pub pending_write_successes: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MemFlashError;
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
|
||||
pub const fn new(fill: u8) -> Self {
|
||||
Self {
|
||||
mem: [fill; SIZE],
|
||||
pending_write_successes: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn random() -> Self {
|
||||
let mut mem = [0; SIZE];
|
||||
for byte in mem.iter_mut() {
|
||||
*byte = rand::random::<u8>();
|
||||
}
|
||||
Self {
|
||||
mem,
|
||||
pending_write_successes: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Default
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new(0xFF)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Flash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const BLOCK_SIZE: usize = ERASE_SIZE;
|
||||
const ERASE_VALUE: u8 = 0xFF;
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
type Error = MemFlashError;
|
||||
}
|
||||
|
||||
impl NorFlashError for MemFlashError {
|
||||
fn kind(&self) -> NorFlashErrorKind {
|
||||
NorFlashErrorKind::Other
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
let len = bytes.len();
|
||||
bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = ERASE_SIZE;
|
||||
|
||||
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
let from = from as usize;
|
||||
let to = to as usize;
|
||||
assert!(from % ERASE_SIZE == 0);
|
||||
assert!(to % ERASE_SIZE == 0, "To: {}, erase size: {}", to, ERASE_SIZE);
|
||||
for i in from..to {
|
||||
self.mem[i] = 0xFF;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
let offset = offset as usize;
|
||||
assert!(bytes.len() % WRITE_SIZE == 0);
|
||||
assert!(offset % WRITE_SIZE == 0);
|
||||
assert!(offset + bytes.len() <= SIZE);
|
||||
|
||||
if let Some(pending_successes) = self.pending_write_successes {
|
||||
if pending_successes > 0 {
|
||||
self.pending_write_successes = Some(pending_successes - 1);
|
||||
} else {
|
||||
return Err(MemFlashError);
|
||||
}
|
||||
}
|
||||
|
||||
for ((offset, mem_byte), new_byte) in self
|
||||
.mem
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.skip(offset)
|
||||
.take(bytes.len())
|
||||
.zip(bytes)
|
||||
{
|
||||
assert_eq!(0xFF, *mem_byte, "Offset {} is not erased", offset);
|
||||
*mem_byte = *new_byte;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncReadNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
<Self as ReadNorFlash>::read(self, offset, bytes)
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
<Self as ReadNorFlash>::capacity(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = ERASE_SIZE;
|
||||
|
||||
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
<Self as NorFlash>::erase(self, from, to)
|
||||
}
|
||||
|
||||
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
<Self as NorFlash>::write(self, offset, bytes)
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ impl Partition {
|
||||
}
|
||||
|
||||
/// Read from the partition on the provided flash
|
||||
pub(crate) async fn read<F: AsyncReadNorFlash>(
|
||||
pub async fn read<F: AsyncReadNorFlash>(
|
||||
&self,
|
||||
flash: &mut F,
|
||||
offset: u32,
|
||||
@ -35,12 +35,7 @@ impl Partition {
|
||||
}
|
||||
|
||||
/// Write to the partition on the provided flash
|
||||
pub(crate) async fn write<F: AsyncNorFlash>(
|
||||
&self,
|
||||
flash: &mut F,
|
||||
offset: u32,
|
||||
bytes: &[u8],
|
||||
) -> Result<(), F::Error> {
|
||||
pub async fn write<F: AsyncNorFlash>(&self, flash: &mut F, offset: u32, bytes: &[u8]) -> Result<(), F::Error> {
|
||||
let offset = self.from as u32 + offset;
|
||||
flash.write(offset, bytes).await?;
|
||||
trace!("Wrote from 0x{:x} len {}", offset, bytes.len());
|
||||
@ -48,7 +43,7 @@ impl Partition {
|
||||
}
|
||||
|
||||
/// Erase part of the partition on the provided flash
|
||||
pub(crate) async fn erase<F: AsyncNorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
|
||||
pub async fn erase<F: AsyncNorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
|
||||
let from = self.from as u32 + from;
|
||||
let to = self.from as u32 + to;
|
||||
flash.erase(from, to).await?;
|
||||
@ -66,18 +61,13 @@ impl Partition {
|
||||
}
|
||||
|
||||
/// Read from the partition on the provided flash
|
||||
pub(crate) fn read_blocking<F: ReadNorFlash>(
|
||||
&self,
|
||||
flash: &mut F,
|
||||
offset: u32,
|
||||
bytes: &mut [u8],
|
||||
) -> Result<(), F::Error> {
|
||||
pub fn read_blocking<F: ReadNorFlash>(&self, flash: &mut F, offset: u32, bytes: &mut [u8]) -> Result<(), F::Error> {
|
||||
let offset = self.from as u32 + offset;
|
||||
flash.read(offset, bytes)
|
||||
}
|
||||
|
||||
/// Write to the partition on the provided flash
|
||||
pub(crate) fn write_blocking<F: NorFlash>(&self, flash: &mut F, offset: u32, bytes: &[u8]) -> Result<(), F::Error> {
|
||||
pub fn write_blocking<F: NorFlash>(&self, flash: &mut F, offset: u32, bytes: &[u8]) -> Result<(), F::Error> {
|
||||
let offset = self.from as u32 + offset;
|
||||
flash.write(offset, bytes)?;
|
||||
trace!("Wrote from 0x{:x} len {}", offset, bytes.len());
|
||||
@ -85,7 +75,7 @@ impl Partition {
|
||||
}
|
||||
|
||||
/// Erase part of the partition on the provided flash
|
||||
pub(crate) fn erase_blocking<F: NorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
|
||||
pub fn erase_blocking<F: NorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
|
||||
let from = self.from as u32 + from;
|
||||
let to = self.from as u32 + to;
|
||||
flash.erase(from, to)?;
|
||||
@ -105,45 +95,45 @@ impl Partition {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests::MemFlash;
|
||||
use crate::mem_flash::MemFlash;
|
||||
use crate::Partition;
|
||||
|
||||
#[test]
|
||||
fn can_erase() {
|
||||
let mut flash = MemFlash::<1024, 64, 4>([0x00; 1024]);
|
||||
let mut flash = MemFlash::<1024, 64, 4>::new(0x00);
|
||||
let partition = Partition::new(256, 512);
|
||||
|
||||
partition.erase_blocking(&mut flash, 64, 192).unwrap();
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().take(256 + 64) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().take(256 + 64) {
|
||||
assert_eq!(0x00, byte, "Index {}", index);
|
||||
}
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().skip(256 + 64).take(128) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().skip(256 + 64).take(128) {
|
||||
assert_eq!(0xFF, byte, "Index {}", index);
|
||||
}
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().skip(256 + 64 + 128) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().skip(256 + 64 + 128) {
|
||||
assert_eq!(0x00, byte, "Index {}", index);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_wipe() {
|
||||
let mut flash = MemFlash::<1024, 64, 4>([0x00; 1024]);
|
||||
let mut flash = MemFlash::<1024, 64, 4>::new(0x00);
|
||||
let partition = Partition::new(256, 512);
|
||||
|
||||
partition.wipe_blocking(&mut flash).unwrap();
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().take(256) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().take(256) {
|
||||
assert_eq!(0x00, byte, "Index {}", index);
|
||||
}
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().skip(256).take(256) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().skip(256).take(256) {
|
||||
assert_eq!(0xFF, byte, "Index {}", index);
|
||||
}
|
||||
|
||||
for (index, byte) in flash.0.iter().copied().enumerate().skip(512) {
|
||||
for (index, byte) in flash.mem.iter().copied().enumerate().skip(512) {
|
||||
assert_eq!(0x00, byte, "Index {}", index);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user