diff options
Diffstat (limited to 'embassy-boot/boot/src/boot_loader.rs')
| -rw-r--r-- | embassy-boot/boot/src/boot_loader.rs | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/embassy-boot/boot/src/boot_loader.rs b/embassy-boot/boot/src/boot_loader.rs index db067da5b..37fff621a 100644 --- a/embassy-boot/boot/src/boot_loader.rs +++ b/embassy-boot/boot/src/boot_loader.rs | |||
| @@ -30,22 +30,18 @@ where | |||
| 30 | } | 30 | } |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | /// Extension of the embedded-storage flash type information with block size and erase value. | ||
| 34 | pub trait Flash: NorFlash { | ||
| 35 | /// The erase value of the flash. Typically the default of 0xFF is used, but some flashes use a different value. | ||
| 36 | const ERASE_VALUE: u8 = 0xFF; | ||
| 37 | } | ||
| 38 | |||
| 39 | /// Trait defining the flash handles used for active and DFU partition. | 33 | /// Trait defining the flash handles used for active and DFU partition. |
| 40 | /// The ACTIVE and DFU erase sizes must be equal. If this is not the case, then consider adding an adapter for the | 34 | /// The ACTIVE and DFU erase sizes must be equal. If this is not the case, then consider adding an adapter for the |
| 41 | /// smallest flash to increase its erase size such that they match. See e.g. [`crate::large_erase::LargeErase`]. | 35 | /// smallest flash to increase its erase size such that they match. See e.g. [`crate::large_erase::LargeErase`]. |
| 42 | pub trait FlashConfig { | 36 | pub trait FlashConfig { |
| 37 | /// The erase value of the state flash. Typically the default of 0xFF is used, but some flashes use a different value. | ||
| 38 | const STATE_ERASE_VALUE: u8 = 0xFF; | ||
| 43 | /// Flash type used for the state partition. | 39 | /// Flash type used for the state partition. |
| 44 | type STATE: Flash; | 40 | type STATE: NorFlash; |
| 45 | /// Flash type used for the active partition. | 41 | /// Flash type used for the active partition. |
| 46 | type ACTIVE: Flash; | 42 | type ACTIVE: NorFlash; |
| 47 | /// Flash type used for the dfu partition. | 43 | /// Flash type used for the dfu partition. |
| 48 | type DFU: Flash; | 44 | type DFU: NorFlash; |
| 49 | 45 | ||
| 50 | /// Return flash instance used to write/read to/from active partition. | 46 | /// Return flash instance used to write/read to/from active partition. |
| 51 | fn active(&mut self) -> &mut Self::ACTIVE; | 47 | fn active(&mut self) -> &mut Self::ACTIVE; |
| @@ -208,7 +204,7 @@ impl BootLoader { | |||
| 208 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; | 204 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; |
| 209 | 205 | ||
| 210 | // Invalidate progress | 206 | // Invalidate progress |
| 211 | state_word.fill(!P::STATE::ERASE_VALUE); | 207 | state_word.fill(!P::STATE_ERASE_VALUE); |
| 212 | self.state | 208 | self.state |
| 213 | .write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; | 209 | .write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; |
| 214 | 210 | ||
| @@ -237,7 +233,7 @@ impl BootLoader { | |||
| 237 | 233 | ||
| 238 | self.state | 234 | self.state |
| 239 | .read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; | 235 | .read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; |
| 240 | if state_word.iter().any(|&b| b != P::STATE::ERASE_VALUE) { | 236 | if state_word.iter().any(|&b| b != P::STATE_ERASE_VALUE) { |
| 241 | // Progress is invalid | 237 | // Progress is invalid |
| 242 | return Ok(max_index); | 238 | return Ok(max_index); |
| 243 | } | 239 | } |
| @@ -249,7 +245,7 @@ impl BootLoader { | |||
| 249 | state_word, | 245 | state_word, |
| 250 | )?; | 246 | )?; |
| 251 | 247 | ||
| 252 | if state_word.iter().any(|&b| b == P::STATE::ERASE_VALUE) { | 248 | if state_word.iter().any(|&b| b == P::STATE_ERASE_VALUE) { |
| 253 | return Ok(index); | 249 | return Ok(index); |
| 254 | } | 250 | } |
| 255 | } | 251 | } |
| @@ -263,7 +259,7 @@ impl BootLoader { | |||
| 263 | aligned_buf: &mut [u8], | 259 | aligned_buf: &mut [u8], |
| 264 | ) -> Result<(), BootError> { | 260 | ) -> Result<(), BootError> { |
| 265 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; | 261 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; |
| 266 | state_word.fill(!P::STATE::ERASE_VALUE); | 262 | state_word.fill(!P::STATE_ERASE_VALUE); |
| 267 | self.state | 263 | self.state |
| 268 | .write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, state_word)?; | 264 | .write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, state_word)?; |
| 269 | Ok(()) | 265 | Ok(()) |
| @@ -386,16 +382,16 @@ fn assert_partitions(active: Partition, dfu: Partition, state: Partition, page_s | |||
| 386 | } | 382 | } |
| 387 | 383 | ||
| 388 | /// A flash wrapper implementing the Flash and embedded_storage traits. | 384 | /// A flash wrapper implementing the Flash and embedded_storage traits. |
| 389 | pub struct BootFlash<F, const ERASE_VALUE: u8 = 0xFF> | 385 | pub struct BootFlash<F> |
| 390 | where | 386 | where |
| 391 | F: NorFlash + ReadNorFlash, | 387 | F: NorFlash, |
| 392 | { | 388 | { |
| 393 | flash: F, | 389 | flash: F, |
| 394 | } | 390 | } |
| 395 | 391 | ||
| 396 | impl<F, const ERASE_VALUE: u8> BootFlash<F, ERASE_VALUE> | 392 | impl<F> BootFlash<F> |
| 397 | where | 393 | where |
| 398 | F: NorFlash + ReadNorFlash, | 394 | F: NorFlash, |
| 399 | { | 395 | { |
| 400 | /// Create a new instance of a bootable flash | 396 | /// Create a new instance of a bootable flash |
| 401 | pub fn new(flash: F) -> Self { | 397 | pub fn new(flash: F) -> Self { |
| @@ -403,23 +399,16 @@ where | |||
| 403 | } | 399 | } |
| 404 | } | 400 | } |
| 405 | 401 | ||
| 406 | impl<F, const ERASE_VALUE: u8> Flash for BootFlash<F, ERASE_VALUE> | 402 | impl<F> ErrorType for BootFlash<F> |
| 407 | where | ||
| 408 | F: NorFlash + ReadNorFlash, | ||
| 409 | { | ||
| 410 | const ERASE_VALUE: u8 = ERASE_VALUE; | ||
| 411 | } | ||
| 412 | |||
| 413 | impl<F, const ERASE_VALUE: u8> ErrorType for BootFlash<F, ERASE_VALUE> | ||
| 414 | where | 403 | where |
| 415 | F: ReadNorFlash + NorFlash, | 404 | F: NorFlash, |
| 416 | { | 405 | { |
| 417 | type Error = F::Error; | 406 | type Error = F::Error; |
| 418 | } | 407 | } |
| 419 | 408 | ||
| 420 | impl<F, const ERASE_VALUE: u8> NorFlash for BootFlash<F, ERASE_VALUE> | 409 | impl<F> NorFlash for BootFlash<F> |
| 421 | where | 410 | where |
| 422 | F: ReadNorFlash + NorFlash, | 411 | F: NorFlash, |
| 423 | { | 412 | { |
| 424 | const WRITE_SIZE: usize = F::WRITE_SIZE; | 413 | const WRITE_SIZE: usize = F::WRITE_SIZE; |
| 425 | const ERASE_SIZE: usize = F::ERASE_SIZE; | 414 | const ERASE_SIZE: usize = F::ERASE_SIZE; |
| @@ -433,9 +422,9 @@ where | |||
| 433 | } | 422 | } |
| 434 | } | 423 | } |
| 435 | 424 | ||
| 436 | impl<F, const ERASE_VALUE: u8> ReadNorFlash for BootFlash<F, ERASE_VALUE> | 425 | impl<F> ReadNorFlash for BootFlash<F> |
| 437 | where | 426 | where |
| 438 | F: ReadNorFlash + NorFlash, | 427 | F: NorFlash, |
| 439 | { | 428 | { |
| 440 | const READ_SIZE: usize = F::READ_SIZE; | 429 | const READ_SIZE: usize = F::READ_SIZE; |
| 441 | 430 | ||
| @@ -451,14 +440,14 @@ where | |||
| 451 | /// Convenience provider that uses a single flash for all partitions. | 440 | /// Convenience provider that uses a single flash for all partitions. |
| 452 | pub struct SingleFlashConfig<'a, F> | 441 | pub struct SingleFlashConfig<'a, F> |
| 453 | where | 442 | where |
| 454 | F: Flash, | 443 | F: NorFlash, |
| 455 | { | 444 | { |
| 456 | flash: &'a mut F, | 445 | flash: &'a mut F, |
| 457 | } | 446 | } |
| 458 | 447 | ||
| 459 | impl<'a, F> SingleFlashConfig<'a, F> | 448 | impl<'a, F> SingleFlashConfig<'a, F> |
| 460 | where | 449 | where |
| 461 | F: Flash, | 450 | F: NorFlash, |
| 462 | { | 451 | { |
| 463 | /// Create a provider for a single flash. | 452 | /// Create a provider for a single flash. |
| 464 | pub fn new(flash: &'a mut F) -> Self { | 453 | pub fn new(flash: &'a mut F) -> Self { |
| @@ -468,7 +457,7 @@ where | |||
| 468 | 457 | ||
| 469 | impl<'a, F> FlashConfig for SingleFlashConfig<'a, F> | 458 | impl<'a, F> FlashConfig for SingleFlashConfig<'a, F> |
| 470 | where | 459 | where |
| 471 | F: Flash, | 460 | F: NorFlash, |
| 472 | { | 461 | { |
| 473 | type STATE = F; | 462 | type STATE = F; |
| 474 | type ACTIVE = F; | 463 | type ACTIVE = F; |
| @@ -488,9 +477,9 @@ where | |||
| 488 | /// Convenience flash provider that uses separate flash instances for each partition. | 477 | /// Convenience flash provider that uses separate flash instances for each partition. |
| 489 | pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU> | 478 | pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU> |
| 490 | where | 479 | where |
| 491 | ACTIVE: Flash, | 480 | ACTIVE: NorFlash, |
| 492 | STATE: Flash, | 481 | STATE: NorFlash, |
| 493 | DFU: Flash, | 482 | DFU: NorFlash, |
| 494 | { | 483 | { |
| 495 | active: &'a mut ACTIVE, | 484 | active: &'a mut ACTIVE, |
| 496 | state: &'a mut STATE, | 485 | state: &'a mut STATE, |
| @@ -499,9 +488,9 @@ where | |||
| 499 | 488 | ||
| 500 | impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU> | 489 | impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU> |
| 501 | where | 490 | where |
| 502 | ACTIVE: Flash, | 491 | ACTIVE: NorFlash, |
| 503 | STATE: Flash, | 492 | STATE: NorFlash, |
| 504 | DFU: Flash, | 493 | DFU: NorFlash, |
| 505 | { | 494 | { |
| 506 | /// Create a new flash provider with separate configuration for all three partitions. | 495 | /// Create a new flash provider with separate configuration for all three partitions. |
| 507 | pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self { | 496 | pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self { |
| @@ -511,9 +500,9 @@ where | |||
| 511 | 500 | ||
| 512 | impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU> | 501 | impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU> |
| 513 | where | 502 | where |
| 514 | ACTIVE: Flash, | 503 | ACTIVE: NorFlash, |
| 515 | STATE: Flash, | 504 | STATE: NorFlash, |
| 516 | DFU: Flash, | 505 | DFU: NorFlash, |
| 517 | { | 506 | { |
| 518 | type STATE = STATE; | 507 | type STATE = STATE; |
| 519 | type ACTIVE = ACTIVE; | 508 | type ACTIVE = ACTIVE; |
