2022-04-20 13:49:59 +02:00
|
|
|
#![no_std]
|
|
|
|
#![feature(generic_associated_types)]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
mod fmt;
|
|
|
|
|
|
|
|
pub use embassy_boot::{FirmwareUpdater, FlashProvider, Partition, SingleFlashProvider, State};
|
2022-04-26 18:33:09 +02:00
|
|
|
use embassy_stm32::flash::{ERASE_SIZE, ERASE_VALUE, WRITE_SIZE};
|
2022-04-20 13:49:59 +02:00
|
|
|
|
2022-04-26 18:33:09 +02:00
|
|
|
pub struct BootLoader {
|
|
|
|
boot: embassy_boot::BootLoader<ERASE_SIZE, WRITE_SIZE, ERASE_VALUE>,
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 18:33:09 +02:00
|
|
|
impl BootLoader {
|
2022-04-20 13:49:59 +02:00
|
|
|
/// Create a new bootloader instance using parameters from linker script
|
|
|
|
pub fn default() -> Self {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Boots the application
|
|
|
|
pub fn prepare<F: FlashProvider>(&mut self, flash: &mut F) -> usize {
|
|
|
|
match self.boot.prepare_boot(flash) {
|
|
|
|
Ok(_) => self.boot.boot_address(),
|
|
|
|
Err(_) => panic!("boot prepare error!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn load(&mut self, start: usize) -> ! {
|
|
|
|
trace!("Loading app at 0x{:x}", start);
|
2022-04-26 18:33:09 +02:00
|
|
|
#[allow(unused_mut)]
|
2022-04-20 13:49:59 +02:00
|
|
|
let mut p = cortex_m::Peripherals::steal();
|
|
|
|
#[cfg(not(feature = "thumbv6"))]
|
|
|
|
p.SCB.invalidate_icache();
|
|
|
|
p.SCB.vtor.write(start as u32);
|
|
|
|
|
|
|
|
cortex_m::asm::bootload(start as *const u32)
|
|
|
|
}
|
|
|
|
}
|