2022-04-20 13:49:59 +02:00
|
|
|
#![no_std]
|
2022-08-30 13:07:35 +02:00
|
|
|
#![warn(missing_docs)]
|
2022-11-23 14:48:51 +01:00
|
|
|
#![doc = include_str!("../README.md")]
|
2022-04-20 13:49:59 +02:00
|
|
|
mod fmt;
|
|
|
|
|
2023-08-03 20:56:04 +02:00
|
|
|
pub use embassy_boot::{
|
|
|
|
AlignedBuffer, BlockingFirmwareState, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareUpdaterConfig, State,
|
|
|
|
};
|
2023-05-30 13:57:19 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
2023-08-03 20:56:04 +02:00
|
|
|
pub use embassy_boot::{FirmwareState, FirmwareUpdater};
|
2023-05-30 13:57:19 +02:00
|
|
|
use embedded_storage::nor_flash::NorFlash;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
2022-08-30 13:07:35 +02:00
|
|
|
/// A bootloader for STM32 devices.
|
2023-05-30 13:57:19 +02:00
|
|
|
pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> {
|
|
|
|
boot: embassy_boot::BootLoader<ACTIVE, DFU, STATE>,
|
2023-04-04 22:22:25 +02:00
|
|
|
aligned_buf: AlignedBuffer<BUFFER_SIZE>,
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 13:57:19 +02:00
|
|
|
impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize>
|
|
|
|
BootLoader<ACTIVE, DFU, STATE, BUFFER_SIZE>
|
|
|
|
{
|
2022-11-01 07:54:43 +01:00
|
|
|
/// Create a new bootloader instance using the supplied partitions for active, dfu and state.
|
2023-05-30 13:57:19 +02:00
|
|
|
pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self {
|
2022-11-01 07:54:43 +01:00
|
|
|
Self {
|
2023-05-30 13:57:19 +02:00
|
|
|
boot: embassy_boot::BootLoader::new(config),
|
2023-04-04 22:22:25 +02:00
|
|
|
aligned_buf: AlignedBuffer([0; BUFFER_SIZE]),
|
2022-11-01 07:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inspect the bootloader state and perform actions required before booting, such as swapping
|
|
|
|
/// firmware.
|
2023-05-30 13:57:19 +02:00
|
|
|
pub fn prepare(&mut self) {
|
|
|
|
self.boot
|
|
|
|
.prepare_boot(self.aligned_buf.as_mut())
|
|
|
|
.expect("Boot prepare error");
|
2022-11-01 07:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Boots the application.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This modifies the stack pointer and reset vector and will run code placed in the active partition.
|
2023-05-30 14:03:31 +02:00
|
|
|
pub unsafe fn load(self, start: u32) -> ! {
|
|
|
|
core::mem::drop(self.boot);
|
|
|
|
|
2022-11-01 07:54:43 +01:00
|
|
|
trace!("Loading app at 0x{:x}", start);
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut p = cortex_m::Peripherals::steal();
|
|
|
|
#[cfg(not(armv6m))]
|
|
|
|
p.SCB.invalidate_icache();
|
2023-05-30 13:57:19 +02:00
|
|
|
p.SCB.vtor.write(start);
|
2022-11-01 07:54:43 +01:00
|
|
|
|
|
|
|
cortex_m::asm::bootload(start as *const u32)
|
|
|
|
}
|
|
|
|
}
|