Skip to main content

hazard3_boot/firmware_updater/
mod.rs

1mod asynch;
2mod blocking;
3
4pub use asynch::{FirmwareState, FirmwareUpdater};
5pub use blocking::{BlockingFirmwareState, BlockingFirmwareUpdater};
6use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind};
7
8use crate::verification::VerificationError;
9
10/// Firmware updater flash configuration holding the two flashes used by the updater
11///
12/// If only a single flash is actually used, then that flash should be partitioned into two partitions before use.
13/// The easiest way to do this is to use [`FirmwareUpdaterConfig::from_linkerfile`] or [`FirmwareUpdaterConfig::from_linkerfile_blocking`] which will partition
14/// the provided flash according to symbols defined in the linkerfile.
15pub struct FirmwareUpdaterConfig<DFU, STATE> {
16    /// The dfu flash partition
17    pub dfu: DFU,
18    /// The state flash partition
19    pub state: STATE,
20}
21
22/// Errors returned by FirmwareUpdater
23#[derive(Debug)]
24pub enum FirmwareUpdaterError {
25    /// Error from flash.
26    Flash(NorFlashErrorKind),
27    /// Signature errors.
28    Signature(hazard3_crypto::Error),
29    /// Bad state.
30    BadState,
31}
32
33#[cfg(feature = "defmt")]
34impl defmt::Format for FirmwareUpdaterError {
35    fn format(&self, fmt: defmt::Formatter) {
36        match self {
37            FirmwareUpdaterError::Flash(_) => defmt::write!(fmt, "FirmwareUpdaterError::Flash(_)"),
38            FirmwareUpdaterError::Signature(_) => defmt::write!(fmt, "FirmwareUpdaterError::Signature(_)"),
39            FirmwareUpdaterError::BadState => defmt::write!(fmt, "FirmwareUpdaterError::BadState"),
40        }
41    }
42}
43
44impl<E> From<E> for FirmwareUpdaterError
45where
46    E: NorFlashError,
47{
48    fn from(error: E) -> Self {
49        FirmwareUpdaterError::Flash(error.kind())
50    }
51}
52
53impl From<VerificationError> for FirmwareUpdaterError {
54    fn from(error: VerificationError) -> Self {
55        match error {
56            VerificationError::Flash(e) => FirmwareUpdaterError::Flash(e),
57            VerificationError::Signature(e) => FirmwareUpdaterError::Signature(e),
58        }
59    }
60}