refactor(boot): use sector instead of page for consistency

This commit is contained in:
Badr Bouslikhin 2024-02-12 23:28:04 +01:00
parent 56e6b6bee6
commit 7dd974aa0d
No known key found for this signature in database
GPG Key ID: 3B081233DD4DE99B
2 changed files with 16 additions and 16 deletions

View File

@ -13,7 +13,7 @@ use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERA
pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
dfu: DFU, dfu: DFU,
state: FirmwareState<'d, STATE>, state: FirmwareState<'d, STATE>,
last_erased_dfu_page_index: Option<usize>, last_erased_dfu_sector_index: Option<usize>,
} }
#[cfg(target_os = "none")] #[cfg(target_os = "none")]
@ -57,7 +57,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> {
Self { Self {
dfu: config.dfu, dfu: config.dfu,
state: FirmwareState::new(config.state, aligned), state: FirmwareState::new(config.state, aligned),
last_erased_dfu_page_index: None, last_erased_dfu_sector_index: None,
} }
} }
@ -180,7 +180,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> {
/// It handles sector erasures and data writes while verifying the device is in a proper state /// It handles sector erasures and data writes while verifying the device is in a proper state
/// for firmware updates. The function ensures that only unerased sectors are erased before /// for firmware updates. The function ensures that only unerased sectors are erased before
/// writing and efficiently handles the writing process across sector boundaries and in /// writing and efficiently handles the writing process across sector boundaries and in
/// various configurations (data size, page size, etc.). /// various configurations (data size, sector size, etc.).
/// ///
/// # Arguments /// # Arguments
/// ///
@ -215,13 +215,13 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> {
let sector_end = sector_start + DFU::ERASE_SIZE; let sector_end = sector_start + DFU::ERASE_SIZE;
// Determine if the current sector needs to be erased before writing. // Determine if the current sector needs to be erased before writing.
let need_erase = self let need_erase = self
.last_erased_dfu_page_index .last_erased_dfu_sector_index
.map_or(true, |last_erased_sector| current_sector != last_erased_sector); .map_or(true, |last_erased_sector| current_sector != last_erased_sector);
// If the sector needs to be erased, erase it and update the last erased sector index. // If the sector needs to be erased, erase it and update the last erased sector index.
if need_erase { if need_erase {
self.dfu.erase(sector_start as u32, sector_end as u32).await?; self.dfu.erase(sector_start as u32, sector_end as u32).await?;
self.last_erased_dfu_page_index = Some(current_sector); self.last_erased_dfu_sector_index = Some(current_sector);
} }
// Calculate the size of the data chunk that can be written in the current iteration. // Calculate the size of the data chunk that can be written in the current iteration.
@ -380,7 +380,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_page_bigger_than_chunk() { fn can_verify_sha1_sector_bigger_than_chunk() {
let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default());
let state = Partition::new(&flash, 0, 4096); let state = Partition::new(&flash, 0, 4096);
let dfu = Partition::new(&flash, 65536, 65536); let dfu = Partition::new(&flash, 65536, 65536);
@ -404,7 +404,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_page_smaller_than_chunk() { fn can_verify_sha1_sector_smaller_than_chunk() {
let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default());
let state = Partition::new(&flash, 0, 4096); let state = Partition::new(&flash, 0, 4096);
let dfu = Partition::new(&flash, 65536, 65536); let dfu = Partition::new(&flash, 65536, 65536);
@ -428,7 +428,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_cross_page_boundary() { fn can_verify_sha1_cross_sector_boundary() {
let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default());
let state = Partition::new(&flash, 0, 4096); let state = Partition::new(&flash, 0, 4096);
let dfu = Partition::new(&flash, 65536, 65536); let dfu = Partition::new(&flash, 65536, 65536);

View File

@ -13,7 +13,7 @@ use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERA
pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
dfu: DFU, dfu: DFU,
state: BlockingFirmwareState<'d, STATE>, state: BlockingFirmwareState<'d, STATE>,
last_erased_dfu_page_index: Option<usize>, last_erased_dfu_sector_index: Option<usize>,
} }
#[cfg(target_os = "none")] #[cfg(target_os = "none")]
@ -92,7 +92,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE>
Self { Self {
dfu: config.dfu, dfu: config.dfu,
state: BlockingFirmwareState::new(config.state, aligned), state: BlockingFirmwareState::new(config.state, aligned),
last_erased_dfu_page_index: None, last_erased_dfu_sector_index: None,
} }
} }
@ -215,7 +215,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE>
/// It handles sector erasures and data writes while verifying the device is in a proper state /// It handles sector erasures and data writes while verifying the device is in a proper state
/// for firmware updates. The function ensures that only unerased sectors are erased before /// for firmware updates. The function ensures that only unerased sectors are erased before
/// writing and efficiently handles the writing process across sector boundaries and in /// writing and efficiently handles the writing process across sector boundaries and in
/// various configurations (data size, page size, etc.). /// various configurations (data size, sector size, etc.).
/// ///
/// # Arguments /// # Arguments
/// ///
@ -250,13 +250,13 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE>
let sector_end = sector_start + DFU::ERASE_SIZE; let sector_end = sector_start + DFU::ERASE_SIZE;
// Determine if the current sector needs to be erased before writing. // Determine if the current sector needs to be erased before writing.
let need_erase = self let need_erase = self
.last_erased_dfu_page_index .last_erased_dfu_sector_index
.map_or(true, |last_erased_sector| current_sector != last_erased_sector); .map_or(true, |last_erased_sector| current_sector != last_erased_sector);
// If the sector needs to be erased, erase it and update the last erased sector index. // If the sector needs to be erased, erase it and update the last erased sector index.
if need_erase { if need_erase {
self.dfu.erase(sector_start as u32, sector_end as u32)?; self.dfu.erase(sector_start as u32, sector_end as u32)?;
self.last_erased_dfu_page_index = Some(current_sector); self.last_erased_dfu_sector_index = Some(current_sector);
} }
// Calculate the size of the data chunk that can be written in the current iteration. // Calculate the size of the data chunk that can be written in the current iteration.
@ -420,7 +420,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_page_bigger_than_chunk() { fn can_verify_sha1_sector_bigger_than_chunk() {
let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default())); let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default()));
let state = BlockingPartition::new(&flash, 0, 4096); let state = BlockingPartition::new(&flash, 0, 4096);
let dfu = BlockingPartition::new(&flash, 65536, 65536); let dfu = BlockingPartition::new(&flash, 65536, 65536);
@ -446,7 +446,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_page_smaller_than_chunk() { fn can_verify_sha1_sector_smaller_than_chunk() {
let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default()));
let state = BlockingPartition::new(&flash, 0, 4096); let state = BlockingPartition::new(&flash, 0, 4096);
let dfu = BlockingPartition::new(&flash, 65536, 65536); let dfu = BlockingPartition::new(&flash, 65536, 65536);
@ -472,7 +472,7 @@ mod tests {
} }
#[test] #[test]
fn can_verify_sha1_cross_page_boundary() { fn can_verify_sha1_cross_sector_boundary() {
let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default()));
let state = BlockingPartition::new(&flash, 0, 4096); let state = BlockingPartition::new(&flash, 0, 4096);
let dfu = BlockingPartition::new(&flash, 65536, 65536); let dfu = BlockingPartition::new(&flash, 65536, 65536);