From 24dee870a862d16fa2dc9be13d3f10dc3941b54b Mon Sep 17 00:00:00 2001
From: Rasmus Melchior Jacobsen <rmja@laesoe.org>
Date: Tue, 30 May 2023 13:57:03 +0200
Subject: [PATCH] Align rp

---
 embassy-boot/rp/src/lib.rs | 69 +++++++++-----------------------------
 1 file changed, 16 insertions(+), 53 deletions(-)

diff --git a/embassy-boot/rp/src/lib.rs b/embassy-boot/rp/src/lib.rs
index fb9bc3242..e825a7d13 100644
--- a/embassy-boot/rp/src/lib.rs
+++ b/embassy-boot/rp/src/lib.rs
@@ -3,7 +3,9 @@
 #![doc = include_str!("../README.md")]
 mod fmt;
 
-pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State};
+#[cfg(feature = "nightly")]
+pub use embassy_boot::FirmwareUpdater;
+pub use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareUpdaterConfig, State};
 use embassy_rp::flash::{Flash, ERASE_SIZE};
 use embassy_rp::peripherals::{FLASH, WATCHDOG};
 use embassy_rp::watchdog::Watchdog;
@@ -11,27 +13,28 @@ use embassy_time::Duration;
 use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
 
 /// A bootloader for RP2040 devices.
-pub struct BootLoader<const BUFFER_SIZE: usize = ERASE_SIZE> {
-    boot: embassy_boot::BootLoader,
+pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize = ERASE_SIZE> {
+    boot: embassy_boot::BootLoader<ACTIVE, DFU, STATE>,
     aligned_buf: AlignedBuffer<BUFFER_SIZE>,
 }
 
-impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> {
+impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize>
+    BootLoader<ACTIVE, DFU, STATE, BUFFER_SIZE>
+{
     /// Create a new bootloader instance using the supplied partitions for active, dfu and state.
-    pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self {
+    pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self {
         Self {
-            boot: embassy_boot::BootLoader::new(active, dfu, state),
+            boot: embassy_boot::BootLoader::new(config),
             aligned_buf: AlignedBuffer([0; BUFFER_SIZE]),
         }
     }
 
     /// 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 {
-        match self.boot.prepare_boot(flash, self.aligned_buf.as_mut()) {
-            Ok(_) => embassy_rp::flash::FLASH_BASE + self.boot.boot_address(),
-            Err(_) => panic!("boot prepare error!"),
-        }
+    pub fn prepare(&mut self) {
+        self.boot
+            .prepare_boot(self.aligned_buf.as_mut())
+            .expect("Boot prepare error");
     }
 
     /// Boots the application.
@@ -39,58 +42,18 @@ impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> {
     /// # 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) -> ! {
+    pub unsafe fn load(&mut self, start: u32) -> ! {
         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);
+        p.SCB.vtor.write(start);
 
         cortex_m::asm::bootload(start as *const u32)
     }
 }
 
-#[cfg(target_os = "none")]
-impl Default for BootLoader<ERASE_SIZE> {
-    /// Create a new bootloader instance using parameters from linker script
-    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 u32,
-                &__bootloader_active_end as *const u32 as u32,
-            )
-        };
-        let dfu = unsafe {
-            Partition::new(
-                &__bootloader_dfu_start as *const u32 as u32,
-                &__bootloader_dfu_end as *const u32 as u32,
-            )
-        };
-        let state = unsafe {
-            Partition::new(
-                &__bootloader_state_start as *const u32 as u32,
-                &__bootloader_state_end as *const u32 as u32,
-            )
-        };
-
-        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)
-    }
-}
-
 /// A flash implementation that will feed a watchdog when touching flash.
 pub struct WatchdogFlash<'d, const SIZE: usize> {
     flash: Flash<'d, FLASH, SIZE>,