Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/rt685s/bootloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ impl ec_slimloader_imxrt::ImxrtConfig for Config {
}
}

impl ec_slimloader::BootStatePolicy for Config {}

#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
ec_slimloader::start::<ec_slimloader_imxrt::Imxrt<Config>, JOURNAL_BUFFER_SIZE>(Config).await
Expand Down
4 changes: 2 additions & 2 deletions libs/ec-slimloader-imxrt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod mbi;
use core::ops::Range;

use defmt_or_log::{error, info, panic};
use ec_slimloader::{Board, BootError};
use ec_slimloader::{Board, BootError, BootStatePolicy};
use ec_slimloader_state::flash::FlashJournal;
use ec_slimloader_state::state::Slot;
use embassy_embedded_hal::adapter::BlockingAsync;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<C: ImxrtConfig> CheckImage for Imxrt<C> {
}
}

impl<C: ImxrtConfig> Board for Imxrt<C> {
impl<C: ImxrtConfig + BootStatePolicy> Board for Imxrt<C> {
type Config = C;

async fn init<const JOURNAL_BUFFER_SIZE: usize>(config: Self::Config) -> Self {
Expand Down
32 changes: 27 additions & 5 deletions libs/ec-slimloader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ use ec_slimloader_state::flash::FlashJournal;
use ec_slimloader_state::state::{Slot, State, Status};
use embedded_storage_async::nor_flash::NorFlash;

/// A trait for application specific configurations.
pub trait BootStatePolicy {
/// Get the application specific default boot state.
fn default_state() -> State {
State::new(Status::Initial, unwrap!(Slot::try_from(0)), unwrap!(Slot::try_from(0)))
}

/// Allows application specific validation of the boot state.
fn is_valid_state(_state: &State) -> bool {
true
}
}

/// A board that can boot an application image.
///
/// Typically a board needs to support the intrinsics for some microcontroller and
/// contain non volatile memory that stores the multiple images and bootloading state.
#[allow(async_fn_in_trait)]
pub trait Board {
/// Type used to instantiate a [Board] implementation.
type Config;
type Config: BootStatePolicy;

/// Initialize the [Board], can only be called once.
async fn init<const JOURNAL_BUFFER_SIZE: usize>(config: Self::Config) -> Self;
Expand Down Expand Up @@ -82,15 +95,24 @@ pub async fn start<B: Board, const JOURNAL_BUFFER_SIZE: usize>(config: B::Config
let mut state: State = match state {
Some(state) => {
info!("Latest state fetched from journal: {:?}", state);
*state
if B::Config::is_valid_state(state) {
*state
} else {
let default_state = B::Config::default_state();
warn!(
"State {:?} is invalid per application policy, using default state {:?}",
state, default_state
);
default_state
}
}
None => {
let slot = unwrap!(Slot::try_from(0));
let default_state = B::Config::default_state();
warn!(
"Initial bootup and no state was loaded into the journal, attempting {:?}",
slot
default_state
);
State::new(Status::Initial, slot, slot)
default_state
}
};

Expand Down