Ensure bootloader state is parsed correctly

This commit is contained in:
Ulf Lilleengen 2024-09-19 09:15:08 +02:00
parent b1897c58fa
commit ab0a227e4c
3 changed files with 20 additions and 14 deletions

View File

@ -304,12 +304,7 @@ impl<'d, STATE: NorFlash> FirmwareState<'d, STATE> {
/// `mark_booted`.
pub async fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
self.state.read(0, &mut self.aligned).await?;
if !self.aligned.iter().any(|&b| b != SWAP_MAGIC) {
Ok(State::Swap)
} else {
Ok(State::Boot)
}
Ok(State::from(&self.aligned))
}
/// Mark to trigger firmware swap on next boot.

View File

@ -339,14 +339,7 @@ impl<'d, STATE: NorFlash> BlockingFirmwareState<'d, STATE> {
/// `mark_booted`.
pub fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
self.state.read(0, &mut self.aligned)?;
if !self.aligned.iter().any(|&b| b != SWAP_MAGIC) {
Ok(State::Swap)
} else if !self.aligned.iter().any(|&b| b != DFU_DETACH_MAGIC) {
Ok(State::DfuDetach)
} else {
Ok(State::Boot)
}
Ok(State::from(&self.aligned))
}
/// Mark to trigger firmware swap on next boot.

View File

@ -44,6 +44,24 @@ pub enum State {
DfuDetach,
}
impl<T> From<T> for State
where
T: AsRef<[u8]>,
{
fn from(magic: T) -> State {
let magic = magic.as_ref();
if !magic.iter().any(|&b| b != SWAP_MAGIC) {
State::Swap
} else if !magic.iter().any(|&b| b != REVERT_MAGIC) {
State::Revert
} else if !magic.iter().any(|&b| b != DFU_DETACH_MAGIC) {
State::DfuDetach
} else {
State::Boot
}
}
}
/// Buffer aligned to 32 byte boundary, largest known alignment requirement for embassy-boot.
#[repr(align(32))]
pub struct AlignedBuffer<const N: usize>(pub [u8; N]);