diff options
| author | Badr Bouslikhin <[email protected]> | 2024-02-12 23:28:04 +0100 |
|---|---|---|
| committer | Badr Bouslikhin <[email protected]> | 2024-02-12 23:28:04 +0100 |
| commit | 7dd974aa0dd88ad319971b81083f63a190d278bf (patch) | |
| tree | dee620d6c74c20a6bd82d7086ccbbe2c3cf5fc12 /embassy-boot | |
| parent | 56e6b6bee6bd6087b35857e8aa1a011f6e83f703 (diff) | |
refactor(boot): use sector instead of page for consistency
Diffstat (limited to 'embassy-boot')
| -rw-r--r-- | embassy-boot/src/firmware_updater/asynch.rs | 16 | ||||
| -rw-r--r-- | embassy-boot/src/firmware_updater/blocking.rs | 16 |
2 files changed, 16 insertions, 16 deletions
diff --git a/embassy-boot/src/firmware_updater/asynch.rs b/embassy-boot/src/firmware_updater/asynch.rs index b76668136..3d211be65 100644 --- a/embassy-boot/src/firmware_updater/asynch.rs +++ b/embassy-boot/src/firmware_updater/asynch.rs | |||
| @@ -13,7 +13,7 @@ use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERA | |||
| 13 | pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { | 13 | pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { |
| 14 | dfu: DFU, | 14 | dfu: DFU, |
| 15 | state: FirmwareState<'d, STATE>, | 15 | state: FirmwareState<'d, STATE>, |
| 16 | last_erased_dfu_page_index: Option<usize>, | 16 | last_erased_dfu_sector_index: Option<usize>, |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | #[cfg(target_os = "none")] | 19 | #[cfg(target_os = "none")] |
| @@ -57,7 +57,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> { | |||
| 57 | Self { | 57 | Self { |
| 58 | dfu: config.dfu, | 58 | dfu: config.dfu, |
| 59 | state: FirmwareState::new(config.state, aligned), | 59 | state: FirmwareState::new(config.state, aligned), |
| 60 | last_erased_dfu_page_index: None, | 60 | last_erased_dfu_sector_index: None, |
| 61 | } | 61 | } |
| 62 | } | 62 | } |
| 63 | 63 | ||
| @@ -180,7 +180,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> { | |||
| 180 | /// It handles sector erasures and data writes while verifying the device is in a proper state | 180 | /// It handles sector erasures and data writes while verifying the device is in a proper state |
| 181 | /// for firmware updates. The function ensures that only unerased sectors are erased before | 181 | /// for firmware updates. The function ensures that only unerased sectors are erased before |
| 182 | /// writing and efficiently handles the writing process across sector boundaries and in | 182 | /// writing and efficiently handles the writing process across sector boundaries and in |
| 183 | /// various configurations (data size, page size, etc.). | 183 | /// various configurations (data size, sector size, etc.). |
| 184 | /// | 184 | /// |
| 185 | /// # Arguments | 185 | /// # Arguments |
| 186 | /// | 186 | /// |
| @@ -215,13 +215,13 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> { | |||
| 215 | let sector_end = sector_start + DFU::ERASE_SIZE; | 215 | let sector_end = sector_start + DFU::ERASE_SIZE; |
| 216 | // Determine if the current sector needs to be erased before writing. | 216 | // Determine if the current sector needs to be erased before writing. |
| 217 | let need_erase = self | 217 | let need_erase = self |
| 218 | .last_erased_dfu_page_index | 218 | .last_erased_dfu_sector_index |
| 219 | .map_or(true, |last_erased_sector| current_sector != last_erased_sector); | 219 | .map_or(true, |last_erased_sector| current_sector != last_erased_sector); |
| 220 | 220 | ||
| 221 | // If the sector needs to be erased, erase it and update the last erased sector index. | 221 | // If the sector needs to be erased, erase it and update the last erased sector index. |
| 222 | if need_erase { | 222 | if need_erase { |
| 223 | self.dfu.erase(sector_start as u32, sector_end as u32).await?; | 223 | self.dfu.erase(sector_start as u32, sector_end as u32).await?; |
| 224 | self.last_erased_dfu_page_index = Some(current_sector); | 224 | self.last_erased_dfu_sector_index = Some(current_sector); |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | // Calculate the size of the data chunk that can be written in the current iteration. | 227 | // Calculate the size of the data chunk that can be written in the current iteration. |
| @@ -380,7 +380,7 @@ mod tests { | |||
| 380 | } | 380 | } |
| 381 | 381 | ||
| 382 | #[test] | 382 | #[test] |
| 383 | fn can_verify_sha1_page_bigger_than_chunk() { | 383 | fn can_verify_sha1_sector_bigger_than_chunk() { |
| 384 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); | 384 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); |
| 385 | let state = Partition::new(&flash, 0, 4096); | 385 | let state = Partition::new(&flash, 0, 4096); |
| 386 | let dfu = Partition::new(&flash, 65536, 65536); | 386 | let dfu = Partition::new(&flash, 65536, 65536); |
| @@ -404,7 +404,7 @@ mod tests { | |||
| 404 | } | 404 | } |
| 405 | 405 | ||
| 406 | #[test] | 406 | #[test] |
| 407 | fn can_verify_sha1_page_smaller_than_chunk() { | 407 | fn can_verify_sha1_sector_smaller_than_chunk() { |
| 408 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); | 408 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); |
| 409 | let state = Partition::new(&flash, 0, 4096); | 409 | let state = Partition::new(&flash, 0, 4096); |
| 410 | let dfu = Partition::new(&flash, 65536, 65536); | 410 | let dfu = Partition::new(&flash, 65536, 65536); |
| @@ -428,7 +428,7 @@ mod tests { | |||
| 428 | } | 428 | } |
| 429 | 429 | ||
| 430 | #[test] | 430 | #[test] |
| 431 | fn can_verify_sha1_cross_page_boundary() { | 431 | fn can_verify_sha1_cross_sector_boundary() { |
| 432 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); | 432 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); |
| 433 | let state = Partition::new(&flash, 0, 4096); | 433 | let state = Partition::new(&flash, 0, 4096); |
| 434 | let dfu = Partition::new(&flash, 65536, 65536); | 434 | let dfu = Partition::new(&flash, 65536, 65536); |
diff --git a/embassy-boot/src/firmware_updater/blocking.rs b/embassy-boot/src/firmware_updater/blocking.rs index eb96a9523..35772a856 100644 --- a/embassy-boot/src/firmware_updater/blocking.rs +++ b/embassy-boot/src/firmware_updater/blocking.rs | |||
| @@ -13,7 +13,7 @@ use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERA | |||
| 13 | pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { | 13 | pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> { |
| 14 | dfu: DFU, | 14 | dfu: DFU, |
| 15 | state: BlockingFirmwareState<'d, STATE>, | 15 | state: BlockingFirmwareState<'d, STATE>, |
| 16 | last_erased_dfu_page_index: Option<usize>, | 16 | last_erased_dfu_sector_index: Option<usize>, |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | #[cfg(target_os = "none")] | 19 | #[cfg(target_os = "none")] |
| @@ -92,7 +92,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE> | |||
| 92 | Self { | 92 | Self { |
| 93 | dfu: config.dfu, | 93 | dfu: config.dfu, |
| 94 | state: BlockingFirmwareState::new(config.state, aligned), | 94 | state: BlockingFirmwareState::new(config.state, aligned), |
| 95 | last_erased_dfu_page_index: None, | 95 | last_erased_dfu_sector_index: None, |
| 96 | } | 96 | } |
| 97 | } | 97 | } |
| 98 | 98 | ||
| @@ -215,7 +215,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE> | |||
| 215 | /// It handles sector erasures and data writes while verifying the device is in a proper state | 215 | /// It handles sector erasures and data writes while verifying the device is in a proper state |
| 216 | /// for firmware updates. The function ensures that only unerased sectors are erased before | 216 | /// for firmware updates. The function ensures that only unerased sectors are erased before |
| 217 | /// writing and efficiently handles the writing process across sector boundaries and in | 217 | /// writing and efficiently handles the writing process across sector boundaries and in |
| 218 | /// various configurations (data size, page size, etc.). | 218 | /// various configurations (data size, sector size, etc.). |
| 219 | /// | 219 | /// |
| 220 | /// # Arguments | 220 | /// # Arguments |
| 221 | /// | 221 | /// |
| @@ -250,13 +250,13 @@ impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE> | |||
| 250 | let sector_end = sector_start + DFU::ERASE_SIZE; | 250 | let sector_end = sector_start + DFU::ERASE_SIZE; |
| 251 | // Determine if the current sector needs to be erased before writing. | 251 | // Determine if the current sector needs to be erased before writing. |
| 252 | let need_erase = self | 252 | let need_erase = self |
| 253 | .last_erased_dfu_page_index | 253 | .last_erased_dfu_sector_index |
| 254 | .map_or(true, |last_erased_sector| current_sector != last_erased_sector); | 254 | .map_or(true, |last_erased_sector| current_sector != last_erased_sector); |
| 255 | 255 | ||
| 256 | // If the sector needs to be erased, erase it and update the last erased sector index. | 256 | // If the sector needs to be erased, erase it and update the last erased sector index. |
| 257 | if need_erase { | 257 | if need_erase { |
| 258 | self.dfu.erase(sector_start as u32, sector_end as u32)?; | 258 | self.dfu.erase(sector_start as u32, sector_end as u32)?; |
| 259 | self.last_erased_dfu_page_index = Some(current_sector); | 259 | self.last_erased_dfu_sector_index = Some(current_sector); |
| 260 | } | 260 | } |
| 261 | 261 | ||
| 262 | // Calculate the size of the data chunk that can be written in the current iteration. | 262 | // Calculate the size of the data chunk that can be written in the current iteration. |
| @@ -420,7 +420,7 @@ mod tests { | |||
| 420 | } | 420 | } |
| 421 | 421 | ||
| 422 | #[test] | 422 | #[test] |
| 423 | fn can_verify_sha1_page_bigger_than_chunk() { | 423 | fn can_verify_sha1_sector_bigger_than_chunk() { |
| 424 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default())); | 424 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default())); |
| 425 | let state = BlockingPartition::new(&flash, 0, 4096); | 425 | let state = BlockingPartition::new(&flash, 0, 4096); |
| 426 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | 426 | let dfu = BlockingPartition::new(&flash, 65536, 65536); |
| @@ -446,7 +446,7 @@ mod tests { | |||
| 446 | } | 446 | } |
| 447 | 447 | ||
| 448 | #[test] | 448 | #[test] |
| 449 | fn can_verify_sha1_page_smaller_than_chunk() { | 449 | fn can_verify_sha1_sector_smaller_than_chunk() { |
| 450 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); | 450 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); |
| 451 | let state = BlockingPartition::new(&flash, 0, 4096); | 451 | let state = BlockingPartition::new(&flash, 0, 4096); |
| 452 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | 452 | let dfu = BlockingPartition::new(&flash, 65536, 65536); |
| @@ -472,7 +472,7 @@ mod tests { | |||
| 472 | } | 472 | } |
| 473 | 473 | ||
| 474 | #[test] | 474 | #[test] |
| 475 | fn can_verify_sha1_cross_page_boundary() { | 475 | fn can_verify_sha1_cross_sector_boundary() { |
| 476 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); | 476 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); |
| 477 | let state = BlockingPartition::new(&flash, 0, 4096); | 477 | let state = BlockingPartition::new(&flash, 0, 4096); |
| 478 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | 478 | let dfu = BlockingPartition::new(&flash, 65536, 65536); |
