diff options
| author | Ulf Lilleengen <[email protected]> | 2023-08-11 19:49:33 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-08-11 19:49:33 +0000 |
| commit | 0727f8690c4684d0622547edee2cf9dc22215a9b (patch) | |
| tree | b73e5fee9027422cb121b892e6d467fd0f73cfe7 | |
| parent | c1da2c0219667085124c47d8059ffbf077adaf9d (diff) | |
| parent | 55ff397c0cde8a04c41cfc228645c3fd33383cd1 (diff) | |
Merge pull request #1773 from embassy-rs/boot-improvs
boot: release flash after prepare and refactor api
| -rw-r--r-- | embassy-boot/nrf/src/lib.rs | 37 | ||||
| -rw-r--r-- | embassy-boot/rp/src/lib.rs | 33 | ||||
| -rw-r--r-- | embassy-boot/stm32/src/lib.rs | 33 | ||||
| -rw-r--r-- | examples/boot/bootloader/nrf/src/main.rs | 4 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/src/main.rs | 4 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32/src/main.rs | 4 |
6 files changed, 35 insertions, 80 deletions
diff --git a/embassy-boot/nrf/src/lib.rs b/embassy-boot/nrf/src/lib.rs index df94819fc..b9d86eb17 100644 --- a/embassy-boot/nrf/src/lib.rs +++ b/embassy-boot/nrf/src/lib.rs | |||
| @@ -14,28 +14,17 @@ use embassy_nrf::wdt; | |||
| 14 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | 14 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; |
| 15 | 15 | ||
| 16 | /// A bootloader for nRF devices. | 16 | /// A bootloader for nRF devices. |
| 17 | pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize = PAGE_SIZE> { | 17 | pub struct BootLoader<const BUFFER_SIZE: usize = PAGE_SIZE>; |
| 18 | boot: embassy_boot::BootLoader<ACTIVE, DFU, STATE>, | 18 | |
| 19 | aligned_buf: AlignedBuffer<BUFFER_SIZE>, | 19 | impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> { |
| 20 | } | 20 | /// Inspect the bootloader state and perform actions required before booting, such as swapping firmware. |
| 21 | 21 | pub fn prepare<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash>( | |
| 22 | impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | 22 | config: BootLoaderConfig<ACTIVE, DFU, STATE>, |
| 23 | BootLoader<ACTIVE, DFU, STATE, BUFFER_SIZE> | 23 | ) -> Self { |
| 24 | { | 24 | let mut aligned_buf = AlignedBuffer([0; BUFFER_SIZE]); |
| 25 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | 25 | let mut boot = embassy_boot::BootLoader::new(config); |
| 26 | pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self { | 26 | boot.prepare_boot(&mut aligned_buf.0).expect("Boot prepare error"); |
| 27 | Self { | 27 | Self |
| 28 | boot: embassy_boot::BootLoader::new(config), | ||
| 29 | aligned_buf: AlignedBuffer([0; BUFFER_SIZE]), | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | /// Inspect the bootloader state and perform actions required before booting, such as swapping | ||
| 34 | /// firmware. | ||
| 35 | pub fn prepare(&mut self) { | ||
| 36 | self.boot | ||
| 37 | .prepare_boot(&mut self.aligned_buf.0) | ||
| 38 | .expect("Boot prepare error"); | ||
| 39 | } | 28 | } |
| 40 | 29 | ||
| 41 | /// Boots the application without softdevice mechanisms. | 30 | /// Boots the application without softdevice mechanisms. |
| @@ -45,8 +34,6 @@ impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | |||
| 45 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | 34 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. |
| 46 | #[cfg(not(feature = "softdevice"))] | 35 | #[cfg(not(feature = "softdevice"))] |
| 47 | pub unsafe fn load(self, start: u32) -> ! { | 36 | pub unsafe fn load(self, start: u32) -> ! { |
| 48 | core::mem::drop(self.boot); | ||
| 49 | |||
| 50 | let mut p = cortex_m::Peripherals::steal(); | 37 | let mut p = cortex_m::Peripherals::steal(); |
| 51 | p.SCB.invalidate_icache(); | 38 | p.SCB.invalidate_icache(); |
| 52 | p.SCB.vtor.write(start); | 39 | p.SCB.vtor.write(start); |
| @@ -59,7 +46,7 @@ impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | |||
| 59 | /// | 46 | /// |
| 60 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | 47 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. |
| 61 | #[cfg(feature = "softdevice")] | 48 | #[cfg(feature = "softdevice")] |
| 62 | pub unsafe fn load(&mut self, _app: u32) -> ! { | 49 | pub unsafe fn load(self, _app: u32) -> ! { |
| 63 | use nrf_softdevice_mbr as mbr; | 50 | use nrf_softdevice_mbr as mbr; |
| 64 | const NRF_SUCCESS: u32 = 0; | 51 | const NRF_SUCCESS: u32 = 0; |
| 65 | 52 | ||
diff --git a/embassy-boot/rp/src/lib.rs b/embassy-boot/rp/src/lib.rs index f5aefa416..96bcf3bf7 100644 --- a/embassy-boot/rp/src/lib.rs +++ b/embassy-boot/rp/src/lib.rs | |||
| @@ -15,28 +15,17 @@ use embassy_time::Duration; | |||
| 15 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | 15 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; |
| 16 | 16 | ||
| 17 | /// A bootloader for RP2040 devices. | 17 | /// A bootloader for RP2040 devices. |
| 18 | pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize = ERASE_SIZE> { | 18 | pub struct BootLoader<const BUFFER_SIZE: usize = ERASE_SIZE>; |
| 19 | boot: embassy_boot::BootLoader<ACTIVE, DFU, STATE>, | ||
| 20 | aligned_buf: AlignedBuffer<BUFFER_SIZE>, | ||
| 21 | } | ||
| 22 | |||
| 23 | impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | ||
| 24 | BootLoader<ACTIVE, DFU, STATE, BUFFER_SIZE> | ||
| 25 | { | ||
| 26 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | ||
| 27 | pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self { | ||
| 28 | Self { | ||
| 29 | boot: embassy_boot::BootLoader::new(config), | ||
| 30 | aligned_buf: AlignedBuffer([0; BUFFER_SIZE]), | ||
| 31 | } | ||
| 32 | } | ||
| 33 | 19 | ||
| 34 | /// Inspect the bootloader state and perform actions required before booting, such as swapping | 20 | impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> { |
| 35 | /// firmware. | 21 | /// Inspect the bootloader state and perform actions required before booting, such as swapping firmware |
| 36 | pub fn prepare(&mut self) { | 22 | pub fn prepare<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash>( |
| 37 | self.boot | 23 | config: BootLoaderConfig<ACTIVE, DFU, STATE>, |
| 38 | .prepare_boot(self.aligned_buf.as_mut()) | 24 | ) -> Self { |
| 39 | .expect("Boot prepare error"); | 25 | let mut aligned_buf = AlignedBuffer([0; BUFFER_SIZE]); |
| 26 | let mut boot = embassy_boot::BootLoader::new(config); | ||
| 27 | boot.prepare_boot(aligned_buf.as_mut()).expect("Boot prepare error"); | ||
| 28 | Self | ||
| 40 | } | 29 | } |
| 41 | 30 | ||
| 42 | /// Boots the application. | 31 | /// Boots the application. |
| @@ -45,8 +34,6 @@ impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | |||
| 45 | /// | 34 | /// |
| 46 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | 35 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. |
| 47 | pub unsafe fn load(self, start: u32) -> ! { | 36 | pub unsafe fn load(self, start: u32) -> ! { |
| 48 | core::mem::drop(self.boot); | ||
| 49 | |||
| 50 | trace!("Loading app at 0x{:x}", start); | 37 | trace!("Loading app at 0x{:x}", start); |
| 51 | #[allow(unused_mut)] | 38 | #[allow(unused_mut)] |
| 52 | let mut p = cortex_m::Peripherals::steal(); | 39 | let mut p = cortex_m::Peripherals::steal(); |
diff --git a/embassy-boot/stm32/src/lib.rs b/embassy-boot/stm32/src/lib.rs index 25f029423..c6350c495 100644 --- a/embassy-boot/stm32/src/lib.rs +++ b/embassy-boot/stm32/src/lib.rs | |||
| @@ -11,28 +11,17 @@ pub use embassy_boot::{FirmwareState, FirmwareUpdater}; | |||
| 11 | use embedded_storage::nor_flash::NorFlash; | 11 | use embedded_storage::nor_flash::NorFlash; |
| 12 | 12 | ||
| 13 | /// A bootloader for STM32 devices. | 13 | /// A bootloader for STM32 devices. |
| 14 | pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> { | 14 | pub struct BootLoader; |
| 15 | boot: embassy_boot::BootLoader<ACTIVE, DFU, STATE>, | ||
| 16 | aligned_buf: AlignedBuffer<BUFFER_SIZE>, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | ||
| 20 | BootLoader<ACTIVE, DFU, STATE, BUFFER_SIZE> | ||
| 21 | { | ||
| 22 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | ||
| 23 | pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self { | ||
| 24 | Self { | ||
| 25 | boot: embassy_boot::BootLoader::new(config), | ||
| 26 | aligned_buf: AlignedBuffer([0; BUFFER_SIZE]), | ||
| 27 | } | ||
| 28 | } | ||
| 29 | 15 | ||
| 30 | /// Inspect the bootloader state and perform actions required before booting, such as swapping | 16 | impl BootLoader { |
| 31 | /// firmware. | 17 | /// Inspect the bootloader state and perform actions required before booting, such as swapping firmware |
| 32 | pub fn prepare(&mut self) { | 18 | pub fn prepare<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize>( |
| 33 | self.boot | 19 | config: BootLoaderConfig<ACTIVE, DFU, STATE>, |
| 34 | .prepare_boot(self.aligned_buf.as_mut()) | 20 | ) -> Self { |
| 35 | .expect("Boot prepare error"); | 21 | let mut aligned_buf = AlignedBuffer([0; BUFFER_SIZE]); |
| 22 | let mut boot = embassy_boot::BootLoader::new(config); | ||
| 23 | boot.prepare_boot(aligned_buf.as_mut()).expect("Boot prepare error"); | ||
| 24 | Self | ||
| 36 | } | 25 | } |
| 37 | 26 | ||
| 38 | /// Boots the application. | 27 | /// Boots the application. |
| @@ -41,8 +30,6 @@ impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize> | |||
| 41 | /// | 30 | /// |
| 42 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | 31 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. |
| 43 | pub unsafe fn load(self, start: u32) -> ! { | 32 | pub unsafe fn load(self, start: u32) -> ! { |
| 44 | core::mem::drop(self.boot); | ||
| 45 | |||
| 46 | trace!("Loading app at 0x{:x}", start); | 33 | trace!("Loading app at 0x{:x}", start); |
| 47 | #[allow(unused_mut)] | 34 | #[allow(unused_mut)] |
| 48 | let mut p = cortex_m::Peripherals::steal(); | 35 | let mut p = cortex_m::Peripherals::steal(); |
diff --git a/examples/boot/bootloader/nrf/src/main.rs b/examples/boot/bootloader/nrf/src/main.rs index 72c95c02a..74e2e293f 100644 --- a/examples/boot/bootloader/nrf/src/main.rs +++ b/examples/boot/bootloader/nrf/src/main.rs | |||
| @@ -33,9 +33,7 @@ fn main() -> ! { | |||
| 33 | 33 | ||
| 34 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); | 34 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); |
| 35 | let active_offset = config.active.offset(); | 35 | let active_offset = config.active.offset(); |
| 36 | let mut bl: BootLoader<_, _, _> = BootLoader::new(config); | 36 | let bl: BootLoader = BootLoader::prepare(config); |
| 37 | |||
| 38 | bl.prepare(); | ||
| 39 | 37 | ||
| 40 | unsafe { bl.load(active_offset) } | 38 | unsafe { bl.load(active_offset) } |
| 41 | } | 39 | } |
diff --git a/examples/boot/bootloader/rp/src/main.rs b/examples/boot/bootloader/rp/src/main.rs index 6a81db804..c0e75d1ea 100644 --- a/examples/boot/bootloader/rp/src/main.rs +++ b/examples/boot/bootloader/rp/src/main.rs | |||
| @@ -29,9 +29,7 @@ fn main() -> ! { | |||
| 29 | 29 | ||
| 30 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); | 30 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); |
| 31 | let active_offset = config.active.offset(); | 31 | let active_offset = config.active.offset(); |
| 32 | let mut bl: BootLoader<_, _, _> = BootLoader::new(config); | 32 | let bl: BootLoader = BootLoader::prepare(config); |
| 33 | |||
| 34 | bl.prepare(); | ||
| 35 | 33 | ||
| 36 | unsafe { bl.load(embassy_rp::flash::FLASH_BASE as u32 + active_offset) } | 34 | unsafe { bl.load(embassy_rp::flash::FLASH_BASE as u32 + active_offset) } |
| 37 | } | 35 | } |
diff --git a/examples/boot/bootloader/stm32/src/main.rs b/examples/boot/bootloader/stm32/src/main.rs index 262eed200..5fd9ea588 100644 --- a/examples/boot/bootloader/stm32/src/main.rs +++ b/examples/boot/bootloader/stm32/src/main.rs | |||
| @@ -27,9 +27,7 @@ fn main() -> ! { | |||
| 27 | 27 | ||
| 28 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); | 28 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); |
| 29 | let active_offset = config.active.offset(); | 29 | let active_offset = config.active.offset(); |
| 30 | let mut bl: BootLoader<_, _, _, 2048> = BootLoader::new(config); | 30 | let bl = BootLoader::prepare::<_, _, _, 2048>(config); |
| 31 | |||
| 32 | bl.prepare(); | ||
| 33 | 31 | ||
| 34 | unsafe { bl.load(BANK1_REGION.base + active_offset) } | 32 | unsafe { bl.load(BANK1_REGION.base + active_offset) } |
| 35 | } | 33 | } |
