2022-04-20 13:49:59 +02:00
|
|
|
#![no_std]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
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;
|
|
|
|
|
2022-08-30 13:07:35 +02:00
|
|
|
pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State};
|
2022-04-20 13:49:59 +02:00
|
|
|
|
2022-08-30 13:07:35 +02:00
|
|
|
/// A bootloader for STM32 devices.
|
2023-04-04 22:22:25 +02:00
|
|
|
pub struct BootLoader<const BUFFER_SIZE: usize> {
|
2022-08-30 13:07:35 +02:00
|
|
|
boot: embassy_boot::BootLoader,
|
2023-04-04 22:22:25 +02:00
|
|
|
aligned_buf: AlignedBuffer<BUFFER_SIZE>,
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 22:22:25 +02:00
|
|
|
impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> {
|
2022-11-01 07:54:43 +01:00
|
|
|
/// Create a new bootloader instance using the supplied partitions for active, dfu and state.
|
|
|
|
pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self {
|
|
|
|
Self {
|
|
|
|
boot: embassy_boot::BootLoader::new(active, dfu, state),
|
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.
|
|
|
|
pub fn prepare<F: FlashConfig>(&mut self, flash: &mut F) -> usize {
|
2023-04-04 22:22:25 +02:00
|
|
|
match self.boot.prepare_boot(flash, self.aligned_buf.as_mut()) {
|
2022-11-01 07:54:43 +01:00
|
|
|
Ok(_) => embassy_stm32::flash::FLASH_BASE + self.boot.boot_address(),
|
|
|
|
Err(_) => panic!("boot prepare error!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Boots the application.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This modifies the stack pointer and reset vector and will run code placed in the active partition.
|
|
|
|
pub unsafe fn load(&mut self, start: usize) -> ! {
|
|
|
|
trace!("Loading app at 0x{:x}", start);
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut p = cortex_m::Peripherals::steal();
|
|
|
|
#[cfg(not(armv6m))]
|
|
|
|
p.SCB.invalidate_icache();
|
|
|
|
p.SCB.vtor.write(start as u32);
|
|
|
|
|
|
|
|
cortex_m::asm::bootload(start as *const u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 22:22:25 +02:00
|
|
|
impl<const BUFFER_SIZE: usize> Default for BootLoader<BUFFER_SIZE> {
|
2022-04-20 13:49:59 +02:00
|
|
|
/// Create a new bootloader instance using parameters from linker script
|
2022-11-01 07:54:43 +01:00
|
|
|
fn default() -> Self {
|
2022-04-20 13:49:59 +02:00
|
|
|
extern "C" {
|
|
|
|
static __bootloader_state_start: u32;
|
|
|
|
static __bootloader_state_end: u32;
|
|
|
|
static __bootloader_active_start: u32;
|
|
|
|
static __bootloader_active_end: u32;
|
|
|
|
static __bootloader_dfu_start: u32;
|
|
|
|
static __bootloader_dfu_end: u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
let active = unsafe {
|
|
|
|
Partition::new(
|
|
|
|
&__bootloader_active_start as *const u32 as usize,
|
|
|
|
&__bootloader_active_end as *const u32 as usize,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let dfu = unsafe {
|
|
|
|
Partition::new(
|
|
|
|
&__bootloader_dfu_start as *const u32 as usize,
|
|
|
|
&__bootloader_dfu_end as *const u32 as usize,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let state = unsafe {
|
|
|
|
Partition::new(
|
|
|
|
&__bootloader_state_start as *const u32 as usize,
|
|
|
|
&__bootloader_state_end as *const u32 as usize,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("ACTIVE: 0x{:x} - 0x{:x}", active.from, active.to);
|
|
|
|
trace!("DFU: 0x{:x} - 0x{:x}", dfu.from, dfu.to);
|
|
|
|
trace!("STATE: 0x{:x} - 0x{:x}", state.from, state.to);
|
|
|
|
|
|
|
|
Self::new(active, dfu, state)
|
|
|
|
}
|
|
|
|
}
|