diff options
| author | Ulf Lilleengen <[email protected]> | 2023-08-03 20:56:04 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2023-08-06 19:46:53 +0200 |
| commit | a34331ae5fbf76a61bb2f65dbb13af4d34fcb176 (patch) | |
| tree | eddfa2b200b206923a91b9aae1474156c04e40fa /examples/boot | |
| parent | a40daa923ba031b543ce402f8bd83c2ec41329d8 (diff) | |
Refactor firmware updater
* Allow manipulating state without accessing DFU partition.
* Provide aligned buffer when creating updater to reduce potential wrong parameters passed.
Diffstat (limited to 'examples/boot')
| -rw-r--r-- | examples/boot/application/nrf/src/bin/a.rs | 8 | ||||
| -rw-r--r-- | examples/boot/application/rp/src/bin/a.rs | 7 | ||||
| -rw-r--r-- | examples/boot/application/stm32f3/src/bin/a.rs | 8 | ||||
| -rw-r--r-- | examples/boot/application/stm32f7/src/bin/a.rs | 6 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/src/bin/a.rs | 6 | ||||
| -rw-r--r-- | examples/boot/application/stm32l0/src/bin/a.rs | 8 | ||||
| -rw-r--r-- | examples/boot/application/stm32l1/src/bin/a.rs | 8 | ||||
| -rw-r--r-- | examples/boot/application/stm32l4/src/bin/a.rs | 8 | ||||
| -rw-r--r-- | examples/boot/application/stm32wl/src/bin/a.rs | 8 |
9 files changed, 34 insertions, 33 deletions
diff --git a/examples/boot/application/nrf/src/bin/a.rs b/examples/boot/application/nrf/src/bin/a.rs index 021d77f3b..8b510ed35 100644 --- a/examples/boot/application/nrf/src/bin/a.rs +++ b/examples/boot/application/nrf/src/bin/a.rs | |||
| @@ -52,20 +52,20 @@ async fn main(_spawner: Spawner) { | |||
| 52 | let nvmc = Mutex::new(BlockingAsync::new(nvmc)); | 52 | let nvmc = Mutex::new(BlockingAsync::new(nvmc)); |
| 53 | 53 | ||
| 54 | let config = FirmwareUpdaterConfig::from_linkerfile(&nvmc); | 54 | let config = FirmwareUpdaterConfig::from_linkerfile(&nvmc); |
| 55 | let mut updater = FirmwareUpdater::new(config); | 55 | let mut magic = [0; 4]; |
| 56 | let mut updater = FirmwareUpdater::new(config, &mut magic); | ||
| 56 | loop { | 57 | loop { |
| 57 | led.set_low(); | 58 | led.set_low(); |
| 58 | button.wait_for_any_edge().await; | 59 | button.wait_for_any_edge().await; |
| 59 | if button.is_low() { | 60 | if button.is_low() { |
| 60 | let mut offset = 0; | 61 | let mut offset = 0; |
| 61 | let mut magic = [0; 4]; | ||
| 62 | for chunk in APP_B.chunks(4096) { | 62 | for chunk in APP_B.chunks(4096) { |
| 63 | let mut buf: [u8; 4096] = [0; 4096]; | 63 | let mut buf: [u8; 4096] = [0; 4096]; |
| 64 | buf[..chunk.len()].copy_from_slice(chunk); | 64 | buf[..chunk.len()].copy_from_slice(chunk); |
| 65 | updater.write_firmware(&mut magic, offset, &buf).await.unwrap(); | 65 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 66 | offset += chunk.len(); | 66 | offset += chunk.len(); |
| 67 | } | 67 | } |
| 68 | updater.mark_updated(&mut magic).await.unwrap(); | 68 | updater.mark_updated().await.unwrap(); |
| 69 | led.set_high(); | 69 | led.set_high(); |
| 70 | cortex_m::peripheral::SCB::sys_reset(); | 70 | cortex_m::peripheral::SCB::sys_reset(); |
| 71 | } | 71 | } |
diff --git a/examples/boot/application/rp/src/bin/a.rs b/examples/boot/application/rp/src/bin/a.rs index b5e1950cc..f0dda39d0 100644 --- a/examples/boot/application/rp/src/bin/a.rs +++ b/examples/boot/application/rp/src/bin/a.rs | |||
| @@ -38,7 +38,8 @@ async fn main(_s: Spawner) { | |||
| 38 | let flash = Mutex::new(RefCell::new(flash)); | 38 | let flash = Mutex::new(RefCell::new(flash)); |
| 39 | 39 | ||
| 40 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | 40 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); |
| 41 | let mut updater = BlockingFirmwareUpdater::new(config); | 41 | let mut aligned = AlignedBuffer([0; 4]); |
| 42 | let mut updater = BlockingFirmwareUpdater::new(config, &mut aligned.0); | ||
| 42 | 43 | ||
| 43 | Timer::after(Duration::from_secs(5)).await; | 44 | Timer::after(Duration::from_secs(5)).await; |
| 44 | watchdog.feed(); | 45 | watchdog.feed(); |
| @@ -47,7 +48,7 @@ async fn main(_s: Spawner) { | |||
| 47 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); | 48 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); |
| 48 | defmt::info!("preparing update"); | 49 | defmt::info!("preparing update"); |
| 49 | let writer = updater | 50 | let writer = updater |
| 50 | .prepare_update(&mut buf.0[..1]) | 51 | .prepare_update() |
| 51 | .map_err(|e| defmt::warn!("E: {:?}", defmt::Debug2Format(&e))) | 52 | .map_err(|e| defmt::warn!("E: {:?}", defmt::Debug2Format(&e))) |
| 52 | .unwrap(); | 53 | .unwrap(); |
| 53 | defmt::info!("writer created, starting write"); | 54 | defmt::info!("writer created, starting write"); |
| @@ -59,7 +60,7 @@ async fn main(_s: Spawner) { | |||
| 59 | } | 60 | } |
| 60 | watchdog.feed(); | 61 | watchdog.feed(); |
| 61 | defmt::info!("firmware written, marking update"); | 62 | defmt::info!("firmware written, marking update"); |
| 62 | updater.mark_updated(&mut buf.0[..1]).unwrap(); | 63 | updater.mark_updated().unwrap(); |
| 63 | Timer::after(Duration::from_secs(2)).await; | 64 | Timer::after(Duration::from_secs(2)).await; |
| 64 | led.set_low(); | 65 | led.set_low(); |
| 65 | defmt::info!("update marked, resetting"); | 66 | defmt::info!("update marked, resetting"); |
diff --git a/examples/boot/application/stm32f3/src/bin/a.rs b/examples/boot/application/stm32f3/src/bin/a.rs index c0a11d699..8be39bfb7 100644 --- a/examples/boot/application/stm32f3/src/bin/a.rs +++ b/examples/boot/application/stm32f3/src/bin/a.rs | |||
| @@ -31,17 +31,17 @@ async fn main(_spawner: Spawner) { | |||
| 31 | led.set_high(); | 31 | led.set_high(); |
| 32 | 32 | ||
| 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 34 | let mut updater = FirmwareUpdater::new(config); | 34 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 35 | let mut updater = FirmwareUpdater::new(config, &mut magic.0); | ||
| 35 | button.wait_for_falling_edge().await; | 36 | button.wait_for_falling_edge().await; |
| 36 | let mut offset = 0; | 37 | let mut offset = 0; |
| 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 38 | for chunk in APP_B.chunks(2048) { | 38 | for chunk in APP_B.chunks(2048) { |
| 39 | let mut buf: [u8; 2048] = [0; 2048]; | 39 | let mut buf: [u8; 2048] = [0; 2048]; |
| 40 | buf[..chunk.len()].copy_from_slice(chunk); | 40 | buf[..chunk.len()].copy_from_slice(chunk); |
| 41 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); | 41 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 42 | offset += chunk.len(); | 42 | offset += chunk.len(); |
| 43 | } | 43 | } |
| 44 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 44 | updater.mark_updated().await.unwrap(); |
| 45 | led.set_low(); | 45 | led.set_low(); |
| 46 | cortex_m::peripheral::SCB::sys_reset(); | 46 | cortex_m::peripheral::SCB::sys_reset(); |
| 47 | } | 47 | } |
diff --git a/examples/boot/application/stm32f7/src/bin/a.rs b/examples/boot/application/stm32f7/src/bin/a.rs index dea682a96..0c3819bed 100644 --- a/examples/boot/application/stm32f7/src/bin/a.rs +++ b/examples/boot/application/stm32f7/src/bin/a.rs | |||
| @@ -33,9 +33,9 @@ async fn main(_spawner: Spawner) { | |||
| 33 | led.set_high(); | 33 | led.set_high(); |
| 34 | 34 | ||
| 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); |
| 36 | let mut updater = BlockingFirmwareUpdater::new(config); | ||
| 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 38 | let writer = updater.prepare_update(magic.as_mut()).unwrap(); | 37 | let mut updater = BlockingFirmwareUpdater::new(config, &mut magic.0); |
| 38 | let writer = updater.prepare_update().unwrap(); | ||
| 39 | button.wait_for_rising_edge().await; | 39 | button.wait_for_rising_edge().await; |
| 40 | let mut offset = 0; | 40 | let mut offset = 0; |
| 41 | let mut buf = AlignedBuffer([0; 4096]); | 41 | let mut buf = AlignedBuffer([0; 4096]); |
| @@ -44,7 +44,7 @@ async fn main(_spawner: Spawner) { | |||
| 44 | writer.write(offset, buf.as_ref()).unwrap(); | 44 | writer.write(offset, buf.as_ref()).unwrap(); |
| 45 | offset += chunk.len() as u32; | 45 | offset += chunk.len() as u32; |
| 46 | } | 46 | } |
| 47 | updater.mark_updated(magic.as_mut()).unwrap(); | 47 | updater.mark_updated().unwrap(); |
| 48 | led.set_low(); | 48 | led.set_low(); |
| 49 | cortex_m::peripheral::SCB::sys_reset(); | 49 | cortex_m::peripheral::SCB::sys_reset(); |
| 50 | } | 50 | } |
diff --git a/examples/boot/application/stm32h7/src/bin/a.rs b/examples/boot/application/stm32h7/src/bin/a.rs index 719176692..f239e3732 100644 --- a/examples/boot/application/stm32h7/src/bin/a.rs +++ b/examples/boot/application/stm32h7/src/bin/a.rs | |||
| @@ -34,8 +34,8 @@ async fn main(_spawner: Spawner) { | |||
| 34 | 34 | ||
| 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); |
| 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 37 | let mut updater = BlockingFirmwareUpdater::new(config); | 37 | let mut updater = BlockingFirmwareUpdater::new(config, &mut magic.0); |
| 38 | let writer = updater.prepare_update(magic.as_mut()).unwrap(); | 38 | let writer = updater.prepare_update().unwrap(); |
| 39 | button.wait_for_rising_edge().await; | 39 | button.wait_for_rising_edge().await; |
| 40 | let mut offset = 0; | 40 | let mut offset = 0; |
| 41 | let mut buf = AlignedBuffer([0; 4096]); | 41 | let mut buf = AlignedBuffer([0; 4096]); |
| @@ -44,7 +44,7 @@ async fn main(_spawner: Spawner) { | |||
| 44 | writer.write(offset, buf.as_ref()).unwrap(); | 44 | writer.write(offset, buf.as_ref()).unwrap(); |
| 45 | offset += chunk.len() as u32; | 45 | offset += chunk.len() as u32; |
| 46 | } | 46 | } |
| 47 | updater.mark_updated(magic.as_mut()).unwrap(); | 47 | updater.mark_updated().unwrap(); |
| 48 | led.set_low(); | 48 | led.set_low(); |
| 49 | cortex_m::peripheral::SCB::sys_reset(); | 49 | cortex_m::peripheral::SCB::sys_reset(); |
| 50 | } | 50 | } |
diff --git a/examples/boot/application/stm32l0/src/bin/a.rs b/examples/boot/application/stm32l0/src/bin/a.rs index ce80056e6..b4cdcd44d 100644 --- a/examples/boot/application/stm32l0/src/bin/a.rs +++ b/examples/boot/application/stm32l0/src/bin/a.rs | |||
| @@ -33,18 +33,18 @@ async fn main(_spawner: Spawner) { | |||
| 33 | led.set_high(); | 33 | led.set_high(); |
| 34 | 34 | ||
| 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 36 | let mut updater = FirmwareUpdater::new(config); | 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 37 | let mut updater = FirmwareUpdater::new(config, &mut magic.0); | ||
| 37 | button.wait_for_falling_edge().await; | 38 | button.wait_for_falling_edge().await; |
| 38 | let mut offset = 0; | 39 | let mut offset = 0; |
| 39 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 40 | for chunk in APP_B.chunks(128) { | 40 | for chunk in APP_B.chunks(128) { |
| 41 | let mut buf: [u8; 128] = [0; 128]; | 41 | let mut buf: [u8; 128] = [0; 128]; |
| 42 | buf[..chunk.len()].copy_from_slice(chunk); | 42 | buf[..chunk.len()].copy_from_slice(chunk); |
| 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); | 43 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 44 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 47 | updater.mark_updated().await.unwrap(); |
| 48 | led.set_low(); | 48 | led.set_low(); |
| 49 | Timer::after(Duration::from_secs(1)).await; | 49 | Timer::after(Duration::from_secs(1)).await; |
| 50 | cortex_m::peripheral::SCB::sys_reset(); | 50 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32l1/src/bin/a.rs b/examples/boot/application/stm32l1/src/bin/a.rs index 1e9bf3cb9..b4cdcd44d 100644 --- a/examples/boot/application/stm32l1/src/bin/a.rs +++ b/examples/boot/application/stm32l1/src/bin/a.rs | |||
| @@ -33,18 +33,18 @@ async fn main(_spawner: Spawner) { | |||
| 33 | led.set_high(); | 33 | led.set_high(); |
| 34 | 34 | ||
| 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 36 | let mut updater = FirmwareUpdater::new(config); | ||
| 37 | button.wait_for_falling_edge().await; | ||
| 38 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 37 | let mut updater = FirmwareUpdater::new(config, &mut magic.0); | ||
| 38 | button.wait_for_falling_edge().await; | ||
| 39 | let mut offset = 0; | 39 | let mut offset = 0; |
| 40 | for chunk in APP_B.chunks(128) { | 40 | for chunk in APP_B.chunks(128) { |
| 41 | let mut buf: [u8; 128] = [0; 128]; | 41 | let mut buf: [u8; 128] = [0; 128]; |
| 42 | buf[..chunk.len()].copy_from_slice(chunk); | 42 | buf[..chunk.len()].copy_from_slice(chunk); |
| 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); | 43 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 44 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 47 | updater.mark_updated().await.unwrap(); |
| 48 | led.set_low(); | 48 | led.set_low(); |
| 49 | Timer::after(Duration::from_secs(1)).await; | 49 | Timer::after(Duration::from_secs(1)).await; |
| 50 | cortex_m::peripheral::SCB::sys_reset(); | 50 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32l4/src/bin/a.rs b/examples/boot/application/stm32l4/src/bin/a.rs index a514ab5be..eefa25f75 100644 --- a/examples/boot/application/stm32l4/src/bin/a.rs +++ b/examples/boot/application/stm32l4/src/bin/a.rs | |||
| @@ -31,17 +31,17 @@ async fn main(_spawner: Spawner) { | |||
| 31 | led.set_high(); | 31 | led.set_high(); |
| 32 | 32 | ||
| 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 34 | let mut updater = FirmwareUpdater::new(config); | ||
| 35 | button.wait_for_falling_edge().await; | ||
| 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 34 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 35 | let mut updater = FirmwareUpdater::new(config, &mut magic.0); | ||
| 36 | button.wait_for_falling_edge().await; | ||
| 37 | let mut offset = 0; | 37 | let mut offset = 0; |
| 38 | for chunk in APP_B.chunks(2048) { | 38 | for chunk in APP_B.chunks(2048) { |
| 39 | let mut buf: [u8; 2048] = [0; 2048]; | 39 | let mut buf: [u8; 2048] = [0; 2048]; |
| 40 | buf[..chunk.len()].copy_from_slice(chunk); | 40 | buf[..chunk.len()].copy_from_slice(chunk); |
| 41 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); | 41 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 42 | offset += chunk.len(); | 42 | offset += chunk.len(); |
| 43 | } | 43 | } |
| 44 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 44 | updater.mark_updated().await.unwrap(); |
| 45 | led.set_low(); | 45 | led.set_low(); |
| 46 | cortex_m::peripheral::SCB::sys_reset(); | 46 | cortex_m::peripheral::SCB::sys_reset(); |
| 47 | } | 47 | } |
diff --git a/examples/boot/application/stm32wl/src/bin/a.rs b/examples/boot/application/stm32wl/src/bin/a.rs index 52a197a5c..c837e47b5 100644 --- a/examples/boot/application/stm32wl/src/bin/a.rs +++ b/examples/boot/application/stm32wl/src/bin/a.rs | |||
| @@ -31,19 +31,19 @@ async fn main(_spawner: Spawner) { | |||
| 31 | led.set_high(); | 31 | led.set_high(); |
| 32 | 32 | ||
| 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 34 | let mut updater = FirmwareUpdater::new(config); | 34 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 35 | let mut updater = FirmwareUpdater::new(config, &mut magic.0); | ||
| 35 | button.wait_for_falling_edge().await; | 36 | button.wait_for_falling_edge().await; |
| 36 | //defmt::info!("Starting update"); | 37 | //defmt::info!("Starting update"); |
| 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 38 | let mut offset = 0; | 38 | let mut offset = 0; |
| 39 | for chunk in APP_B.chunks(2048) { | 39 | for chunk in APP_B.chunks(2048) { |
| 40 | let mut buf: [u8; 2048] = [0; 2048]; | 40 | let mut buf: [u8; 2048] = [0; 2048]; |
| 41 | buf[..chunk.len()].copy_from_slice(chunk); | 41 | buf[..chunk.len()].copy_from_slice(chunk); |
| 42 | // defmt::info!("Writing chunk at 0x{:x}", offset); | 42 | // defmt::info!("Writing chunk at 0x{:x}", offset); |
| 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); | 43 | updater.write_firmware(offset, &buf).await.unwrap(); |
| 44 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 45 | } | 45 | } |
| 46 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 46 | updater.mark_updated().await.unwrap(); |
| 47 | //defmt::info!("Marked as updated"); | 47 | //defmt::info!("Marked as updated"); |
| 48 | led.set_low(); | 48 | led.set_low(); |
| 49 | cortex_m::peripheral::SCB::sys_reset(); | 49 | cortex_m::peripheral::SCB::sys_reset(); |
