diff options
| author | Mathias <[email protected]> | 2022-11-01 07:54:43 +0100 |
|---|---|---|
| committer | Mathias <[email protected]> | 2022-11-01 07:54:43 +0100 |
| commit | 97d18c5ffb21cee4352c3ae799d762533c60e42b (patch) | |
| tree | 1ff49885d87dd7b2a20ef42e0d9d4c96d49e72de | |
| parent | eed34f945ccd5c4ef2af77230042dd4954e981ac (diff) | |
Move default initializer function to Default trait implementation
| -rw-r--r-- | embassy-boot/nrf/src/lib.rs | 6 | ||||
| -rw-r--r-- | embassy-boot/stm32/src/lib.rs | 72 |
2 files changed, 41 insertions, 37 deletions
diff --git a/embassy-boot/nrf/src/lib.rs b/embassy-boot/nrf/src/lib.rs index 385e089fe..82475d1e2 100644 --- a/embassy-boot/nrf/src/lib.rs +++ b/embassy-boot/nrf/src/lib.rs | |||
| @@ -17,9 +17,9 @@ pub struct BootLoader { | |||
| 17 | page: AlignedBuffer<PAGE_SIZE>, | 17 | page: AlignedBuffer<PAGE_SIZE>, |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | impl BootLoader { | 20 | impl Default for BootLoader { |
| 21 | /// Create a new bootloader instance using parameters from linker script | 21 | /// Create a new bootloader instance using parameters from linker script |
| 22 | pub fn default() -> Self { | 22 | fn default() -> Self { |
| 23 | extern "C" { | 23 | extern "C" { |
| 24 | static __bootloader_state_start: u32; | 24 | static __bootloader_state_start: u32; |
| 25 | static __bootloader_state_end: u32; | 25 | static __bootloader_state_end: u32; |
| @@ -54,7 +54,9 @@ impl BootLoader { | |||
| 54 | 54 | ||
| 55 | Self::new(active, dfu, state) | 55 | Self::new(active, dfu, state) |
| 56 | } | 56 | } |
| 57 | } | ||
| 57 | 58 | ||
| 59 | impl BootLoader { | ||
| 58 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | 60 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. |
| 59 | pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self { | 61 | pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self { |
| 60 | Self { | 62 | Self { |
diff --git a/embassy-boot/stm32/src/lib.rs b/embassy-boot/stm32/src/lib.rs index edba39cca..d549eccc6 100644 --- a/embassy-boot/stm32/src/lib.rs +++ b/embassy-boot/stm32/src/lib.rs | |||
| @@ -14,8 +14,44 @@ pub struct BootLoader<const PAGE_SIZE: usize, const WRITE_SIZE: usize> { | |||
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | impl<const PAGE_SIZE: usize, const WRITE_SIZE: usize> BootLoader<PAGE_SIZE, WRITE_SIZE> { | 16 | impl<const PAGE_SIZE: usize, const WRITE_SIZE: usize> BootLoader<PAGE_SIZE, WRITE_SIZE> { |
| 17 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | ||
| 18 | pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self { | ||
| 19 | Self { | ||
| 20 | boot: embassy_boot::BootLoader::new(active, dfu, state), | ||
| 21 | magic: AlignedBuffer([0; WRITE_SIZE]), | ||
| 22 | page: AlignedBuffer([0; PAGE_SIZE]), | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | /// Inspect the bootloader state and perform actions required before booting, such as swapping | ||
| 27 | /// firmware. | ||
| 28 | pub fn prepare<F: FlashConfig>(&mut self, flash: &mut F) -> usize { | ||
| 29 | match self.boot.prepare_boot(flash, self.magic.as_mut(), self.page.as_mut()) { | ||
| 30 | Ok(_) => embassy_stm32::flash::FLASH_BASE + self.boot.boot_address(), | ||
| 31 | Err(_) => panic!("boot prepare error!"), | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | /// Boots the application. | ||
| 36 | /// | ||
| 37 | /// # Safety | ||
| 38 | /// | ||
| 39 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | ||
| 40 | pub unsafe fn load(&mut self, start: usize) -> ! { | ||
| 41 | trace!("Loading app at 0x{:x}", start); | ||
| 42 | #[allow(unused_mut)] | ||
| 43 | let mut p = cortex_m::Peripherals::steal(); | ||
| 44 | #[cfg(not(armv6m))] | ||
| 45 | p.SCB.invalidate_icache(); | ||
| 46 | p.SCB.vtor.write(start as u32); | ||
| 47 | |||
| 48 | cortex_m::asm::bootload(start as *const u32) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<const PAGE_SIZE: usize, const WRITE_SIZE: usize> Default for BootLoader<PAGE_SIZE, WRITE_SIZE> { | ||
| 17 | /// Create a new bootloader instance using parameters from linker script | 53 | /// Create a new bootloader instance using parameters from linker script |
| 18 | pub fn default() -> Self { | 54 | fn default() -> Self { |
| 19 | extern "C" { | 55 | extern "C" { |
| 20 | static __bootloader_state_start: u32; | 56 | static __bootloader_state_start: u32; |
| 21 | static __bootloader_state_end: u32; | 57 | static __bootloader_state_end: u32; |
| @@ -50,38 +86,4 @@ impl<const PAGE_SIZE: usize, const WRITE_SIZE: usize> BootLoader<PAGE_SIZE, WRIT | |||
| 50 | 86 | ||
| 51 | Self::new(active, dfu, state) | 87 | Self::new(active, dfu, state) |
| 52 | } | 88 | } |
| 53 | |||
| 54 | /// Create a new bootloader instance using the supplied partitions for active, dfu and state. | ||
| 55 | pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self { | ||
| 56 | Self { | ||
| 57 | boot: embassy_boot::BootLoader::new(active, dfu, state), | ||
| 58 | magic: AlignedBuffer([0; WRITE_SIZE]), | ||
| 59 | page: AlignedBuffer([0; PAGE_SIZE]), | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | /// Inspect the bootloader state and perform actions required before booting, such as swapping | ||
| 64 | /// firmware. | ||
| 65 | pub fn prepare<F: FlashConfig>(&mut self, flash: &mut F) -> usize { | ||
| 66 | match self.boot.prepare_boot(flash, self.magic.as_mut(), self.page.as_mut()) { | ||
| 67 | Ok(_) => embassy_stm32::flash::FLASH_BASE + self.boot.boot_address(), | ||
| 68 | Err(_) => panic!("boot prepare error!"), | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | /// Boots the application. | ||
| 73 | /// | ||
| 74 | /// # Safety | ||
| 75 | /// | ||
| 76 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | ||
| 77 | pub unsafe fn load(&mut self, start: usize) -> ! { | ||
| 78 | trace!("Loading app at 0x{:x}", start); | ||
| 79 | #[allow(unused_mut)] | ||
| 80 | let mut p = cortex_m::Peripherals::steal(); | ||
| 81 | #[cfg(not(armv6m))] | ||
| 82 | p.SCB.invalidate_icache(); | ||
| 83 | p.SCB.vtor.write(start as u32); | ||
| 84 | |||
| 85 | cortex_m::asm::bootload(start as *const u32) | ||
| 86 | } | ||
| 87 | } | 89 | } |
