aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBadr Bouslikhin <[email protected]>2024-02-06 16:25:45 +0100
committerBadr Bouslikhin <[email protected]>2024-02-06 16:25:45 +0100
commitc267cb9ab764176bc8514535f4db8ac2331e30ce (patch)
treec997f07d290e0c30f1a583a15ee1557368569ce2
parentad7d4494fad12f98c7e8e2b776bc12453a66be9a (diff)
feat: enhance bootloader for multiple flash support
-rw-r--r--embassy-boot/src/boot_loader.rs20
-rw-r--r--embassy-boot/src/firmware_updater/asynch.rs13
-rw-r--r--embassy-boot/src/firmware_updater/blocking.rs14
-rw-r--r--examples/boot/bootloader/stm32-dual-bank/Cargo.toml57
-rw-r--r--examples/boot/bootloader/stm32-dual-bank/README.md44
-rw-r--r--examples/boot/bootloader/stm32-dual-bank/build.rs27
-rw-r--r--examples/boot/bootloader/stm32-dual-bank/memory.x18
-rw-r--r--examples/boot/bootloader/stm32-dual-bank/src/main.rs53
-rw-r--r--examples/boot/bootloader/stm32/src/main.rs2
-rw-r--r--examples/boot/bootloader/stm32wb-dfu/src/main.rs4
10 files changed, 231 insertions, 21 deletions
diff --git a/embassy-boot/src/boot_loader.rs b/embassy-boot/src/boot_loader.rs
index e568001bc..54ae8a34b 100644
--- a/embassy-boot/src/boot_loader.rs
+++ b/embassy-boot/src/boot_loader.rs
@@ -49,16 +49,20 @@ pub struct BootLoaderConfig<ACTIVE, DFU, STATE> {
49 pub state: STATE, 49 pub state: STATE,
50} 50}
51 51
52impl<'a, FLASH: NorFlash> 52impl<'a, ActiveFlash: NorFlash, DFUFlash: NorFlash, StateFlash: NorFlash>
53 BootLoaderConfig< 53 BootLoaderConfig<
54 BlockingPartition<'a, NoopRawMutex, FLASH>, 54 BlockingPartition<'a, NoopRawMutex, ActiveFlash>,
55 BlockingPartition<'a, NoopRawMutex, FLASH>, 55 BlockingPartition<'a, NoopRawMutex, DFUFlash>,
56 BlockingPartition<'a, NoopRawMutex, FLASH>, 56 BlockingPartition<'a, NoopRawMutex, StateFlash>,
57 > 57 >
58{ 58{
59 /// Create a bootloader config from the flash and address symbols defined in the linkerfile 59 /// Create a bootloader config from the flash and address symbols defined in the linkerfile
60 // #[cfg(target_os = "none")] 60 // #[cfg(target_os = "none")]
61 pub fn from_linkerfile_blocking(flash: &'a Mutex<NoopRawMutex, RefCell<FLASH>>) -> Self { 61 pub fn from_linkerfile_blocking(
62 active_flash: &'a Mutex<NoopRawMutex, RefCell<ActiveFlash>>,
63 dfu_flash: &'a Mutex<NoopRawMutex, RefCell<DFUFlash>>,
64 state_flash: &'a Mutex<NoopRawMutex, RefCell<StateFlash>>,
65 ) -> Self {
62 extern "C" { 66 extern "C" {
63 static __bootloader_state_start: u32; 67 static __bootloader_state_start: u32;
64 static __bootloader_state_end: u32; 68 static __bootloader_state_end: u32;
@@ -73,21 +77,21 @@ impl<'a, FLASH: NorFlash>
73 let end = &__bootloader_active_end as *const u32 as u32; 77 let end = &__bootloader_active_end as *const u32 as u32;
74 trace!("ACTIVE: 0x{:x} - 0x{:x}", start, end); 78 trace!("ACTIVE: 0x{:x} - 0x{:x}", start, end);
75 79
76 BlockingPartition::new(flash, start, end - start) 80 BlockingPartition::new(active_flash, start, end - start)
77 }; 81 };
78 let dfu = unsafe { 82 let dfu = unsafe {
79 let start = &__bootloader_dfu_start as *const u32 as u32; 83 let start = &__bootloader_dfu_start as *const u32 as u32;
80 let end = &__bootloader_dfu_end as *const u32 as u32; 84 let end = &__bootloader_dfu_end as *const u32 as u32;
81 trace!("DFU: 0x{:x} - 0x{:x}", start, end); 85 trace!("DFU: 0x{:x} - 0x{:x}", start, end);
82 86
83 BlockingPartition::new(flash, start, end - start) 87 BlockingPartition::new(dfu_flash, start, end - start)
84 }; 88 };
85 let state = unsafe { 89 let state = unsafe {
86 let start = &__bootloader_state_start as *const u32 as u32; 90 let start = &__bootloader_state_start as *const u32 as u32;
87 let end = &__bootloader_state_end as *const u32 as u32; 91 let end = &__bootloader_state_end as *const u32 as u32;
88 trace!("STATE: 0x{:x} - 0x{:x}", start, end); 92 trace!("STATE: 0x{:x} - 0x{:x}", start, end);
89 93
90 BlockingPartition::new(flash, start, end - start) 94 BlockingPartition::new(state_flash, start, end - start)
91 }; 95 };
92 96
93 Self { active, dfu, state } 97 Self { active, dfu, state }
diff --git a/embassy-boot/src/firmware_updater/asynch.rs b/embassy-boot/src/firmware_updater/asynch.rs
index 2e43e1cc1..5634b48d4 100644
--- a/embassy-boot/src/firmware_updater/asynch.rs
+++ b/embassy-boot/src/firmware_updater/asynch.rs
@@ -16,11 +16,14 @@ pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
16} 16}
17 17
18#[cfg(target_os = "none")] 18#[cfg(target_os = "none")]
19impl<'a, FLASH: NorFlash> 19impl<'a, DFUFlash: NorFlash, StateFlash: NorFlash>
20 FirmwareUpdaterConfig<Partition<'a, NoopRawMutex, FLASH>, Partition<'a, NoopRawMutex, FLASH>> 20 FirmwareUpdaterConfig<Partition<'a, NoopRawMutex, DFUFlash>, Partition<'a, NoopRawMutex, StateFlash>>
21{ 21{
22 /// Create a firmware updater config from the flash and address symbols defined in the linkerfile 22 /// Create a firmware updater config from the flash and address symbols defined in the linkerfile
23 pub fn from_linkerfile(flash: &'a embassy_sync::mutex::Mutex<NoopRawMutex, FLASH>) -> Self { 23 pub fn from_linkerfile(
24 dfu_flash: &'a embassy_sync::mutex::Mutex<NoopRawMutex, DFUFlash>,
25 state_flash: &'a embassy_sync::mutex::Mutex<NoopRawMutex, StateFlash>,
26 ) -> Self {
24 extern "C" { 27 extern "C" {
25 static __bootloader_state_start: u32; 28 static __bootloader_state_start: u32;
26 static __bootloader_state_end: u32; 29 static __bootloader_state_end: u32;
@@ -33,14 +36,14 @@ impl<'a, FLASH: NorFlash>
33 let end = &__bootloader_dfu_end as *const u32 as u32; 36 let end = &__bootloader_dfu_end as *const u32 as u32;
34 trace!("DFU: 0x{:x} - 0x{:x}", start, end); 37 trace!("DFU: 0x{:x} - 0x{:x}", start, end);
35 38
36 Partition::new(flash, start, end - start) 39 Partition::new(dfu_flash, start, end - start)
37 }; 40 };
38 let state = unsafe { 41 let state = unsafe {
39 let start = &__bootloader_state_start as *const u32 as u32; 42 let start = &__bootloader_state_start as *const u32 as u32;
40 let end = &__bootloader_state_end as *const u32 as u32; 43 let end = &__bootloader_state_end as *const u32 as u32;
41 trace!("STATE: 0x{:x} - 0x{:x}", start, end); 44 trace!("STATE: 0x{:x} - 0x{:x}", start, end);
42 45
43 Partition::new(flash, start, end - start) 46 Partition::new(state_flash, start, end - start)
44 }; 47 };
45 48
46 Self { dfu, state } 49 Self { dfu, state }
diff --git a/embassy-boot/src/firmware_updater/blocking.rs b/embassy-boot/src/firmware_updater/blocking.rs
index f1368540d..3814b6c31 100644
--- a/embassy-boot/src/firmware_updater/blocking.rs
+++ b/embassy-boot/src/firmware_updater/blocking.rs
@@ -16,12 +16,16 @@ pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
16} 16}
17 17
18#[cfg(target_os = "none")] 18#[cfg(target_os = "none")]
19impl<'a, FLASH: NorFlash> 19impl<'a, DFUFlash: NorFlash, StateFlash: NorFlash>
20 FirmwareUpdaterConfig<BlockingPartition<'a, NoopRawMutex, FLASH>, BlockingPartition<'a, NoopRawMutex, FLASH>> 20 FirmwareUpdaterConfig<
21 BlockingPartition<'a, NoopRawMutex, DFUFlash>,
22 BlockingPartition<'a, NoopRawMutex, StateFlash>,
23 >
21{ 24{
22 /// Create a firmware updater config from the flash and address symbols defined in the linkerfile 25 /// Create a firmware updater config from the flash and address symbols defined in the linkerfile
23 pub fn from_linkerfile_blocking( 26 pub fn from_linkerfile_blocking(
24 flash: &'a embassy_sync::blocking_mutex::Mutex<NoopRawMutex, core::cell::RefCell<FLASH>>, 27 dfu_flash: &'a embassy_sync::blocking_mutex::Mutex<NoopRawMutex, core::cell::RefCell<DFUFlash>>,
28 state_flash: &'a embassy_sync::blocking_mutex::Mutex<NoopRawMutex, core::cell::RefCell<StateFlash>>,
25 ) -> Self { 29 ) -> Self {
26 extern "C" { 30 extern "C" {
27 static __bootloader_state_start: u32; 31 static __bootloader_state_start: u32;
@@ -35,14 +39,14 @@ impl<'a, FLASH: NorFlash>
35 let end = &__bootloader_dfu_end as *const u32 as u32; 39 let end = &__bootloader_dfu_end as *const u32 as u32;
36 trace!("DFU: 0x{:x} - 0x{:x}", start, end); 40 trace!("DFU: 0x{:x} - 0x{:x}", start, end);
37 41
38 BlockingPartition::new(flash, start, end - start) 42 BlockingPartition::new(dfu_flash, start, end - start)
39 }; 43 };
40 let state = unsafe { 44 let state = unsafe {
41 let start = &__bootloader_state_start as *const u32 as u32; 45 let start = &__bootloader_state_start as *const u32 as u32;
42 let end = &__bootloader_state_end as *const u32 as u32; 46 let end = &__bootloader_state_end as *const u32 as u32;
43 trace!("STATE: 0x{:x} - 0x{:x}", start, end); 47 trace!("STATE: 0x{:x} - 0x{:x}", start, end);
44 48
45 BlockingPartition::new(flash, start, end - start) 49 BlockingPartition::new(state_flash, start, end - start)
46 }; 50 };
47 51
48 Self { dfu, state } 52 Self { dfu, state }
diff --git a/examples/boot/bootloader/stm32-dual-bank/Cargo.toml b/examples/boot/bootloader/stm32-dual-bank/Cargo.toml
new file mode 100644
index 000000000..313187adc
--- /dev/null
+++ b/examples/boot/bootloader/stm32-dual-bank/Cargo.toml
@@ -0,0 +1,57 @@
1[package]
2edition = "2021"
3name = "stm32-bootloader-dual-bank-flash-example"
4version = "0.1.0"
5description = "Example bootloader for dual-bank flash STM32 chips"
6license = "MIT OR Apache-2.0"
7
8[dependencies]
9defmt = { version = "0.3", optional = true }
10defmt-rtt = { version = "0.4", optional = true }
11
12embassy-stm32 = { path = "../../../../embassy-stm32", features = [] }
13embassy-boot-stm32 = { path = "../../../../embassy-boot-stm32" }
14cortex-m = { version = "0.7.6", features = [
15 "inline-asm",
16 "critical-section-single-core",
17] }
18embassy-sync = { version = "0.5.0", path = "../../../../embassy-sync" }
19cortex-m-rt = { version = "0.7" }
20embedded-storage = "0.3.1"
21embedded-storage-async = "0.4.0"
22cfg-if = "1.0.0"
23
24[features]
25defmt = ["dep:defmt", "embassy-boot-stm32/defmt", "embassy-stm32/defmt"]
26debug = ["defmt-rtt", "defmt"]
27
28[profile.dev]
29debug = 2
30debug-assertions = true
31incremental = false
32opt-level = 'z'
33overflow-checks = true
34
35[profile.release]
36codegen-units = 1
37debug = 2
38debug-assertions = false
39incremental = false
40lto = 'fat'
41opt-level = 'z'
42overflow-checks = false
43
44# do not optimize proc-macro crates = faster builds from scratch
45[profile.dev.build-override]
46codegen-units = 8
47debug = false
48debug-assertions = false
49opt-level = 0
50overflow-checks = false
51
52[profile.release.build-override]
53codegen-units = 8
54debug = false
55debug-assertions = false
56opt-level = 0
57overflow-checks = false
diff --git a/examples/boot/bootloader/stm32-dual-bank/README.md b/examples/boot/bootloader/stm32-dual-bank/README.md
new file mode 100644
index 000000000..3de3171cd
--- /dev/null
+++ b/examples/boot/bootloader/stm32-dual-bank/README.md
@@ -0,0 +1,44 @@
1# STM32 dual-bank flash Bootloader
2
3## Overview
4
5This bootloader leverages `embassy-boot` to interact with the flash.
6This example targets STM32 devices with dual-bank flash memory, with a primary focus on the STM32H747XI series.
7Users must modify the `memory.x` configuration file to match with the memory layout of their specific STM32 device.
8
9Additionally, this example can be extended to utilize external flash memory, such as QSPI, for storing partitions.
10
11## Memory Configuration
12
13In this example's `memory.x` file, various symbols are defined to assist in effective memory management within the bootloader environment.
14For dual-bank STM32 devices, it's crucial to assign these symbols correctly to their respective memory banks.
15
16### Symbol Definitions
17
18The bootloader's state and active symbols are anchored to the flash origin of **bank 1**:
19
20- `__bootloader_state_start` and `__bootloader_state_end`
21- `__bootloader_active_start` and `__bootloader_active_end`
22
23In contrast, the Device Firmware Upgrade (DFU) symbols are aligned with the DFU flash origin in **bank 2**:
24
25- `__bootloader_dfu_start` and `__bootloader_dfu_end`
26
27```rust
28__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(**FLASH**);
29__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(**FLASH**);
30
31__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(**FLASH**);
32__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(**FLASH**);
33
34__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(**DFU**);
35__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(**DFU**);
36```
37
38## Flashing the Bootloader
39
40To flash the bootloader onto your STM32H747XI device, use the following command:
41
42```bash
43cargo flash --features embassy-stm32/stm32h747xi-cm7 --release --chip STM32H747XIHx
44```
diff --git a/examples/boot/bootloader/stm32-dual-bank/build.rs b/examples/boot/bootloader/stm32-dual-bank/build.rs
new file mode 100644
index 000000000..fd605991f
--- /dev/null
+++ b/examples/boot/bootloader/stm32-dual-bank/build.rs
@@ -0,0 +1,27 @@
1use std::env;
2use std::fs::File;
3use std::io::Write;
4use std::path::PathBuf;
5
6fn main() {
7 // Put `memory.x` in our output directory and ensure it's
8 // on the linker search path.
9 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
10 File::create(out.join("memory.x"))
11 .unwrap()
12 .write_all(include_bytes!("memory.x"))
13 .unwrap();
14 println!("cargo:rustc-link-search={}", out.display());
15
16 // By default, Cargo will re-run a build script whenever
17 // any file in the project changes. By specifying `memory.x`
18 // here, we ensure the build script is only re-run when
19 // `memory.x` is changed.
20 println!("cargo:rerun-if-changed=memory.x");
21
22 println!("cargo:rustc-link-arg-bins=--nmagic");
23 println!("cargo:rustc-link-arg-bins=-Tlink.x");
24 if env::var("CARGO_FEATURE_DEFMT").is_ok() {
25 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
26 }
27}
diff --git a/examples/boot/bootloader/stm32-dual-bank/memory.x b/examples/boot/bootloader/stm32-dual-bank/memory.x
new file mode 100644
index 000000000..665da7139
--- /dev/null
+++ b/examples/boot/bootloader/stm32-dual-bank/memory.x
@@ -0,0 +1,18 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 FLASH : ORIGIN = 0x08000000, LENGTH = 128K
5 BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K
6 ACTIVE : ORIGIN = 0x08040000, LENGTH = 512K
7 DFU : ORIGIN = 0x08100000, LENGTH = 640K
8 RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 512K
9}
10
11__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH);
12__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH);
13
14__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH);
15__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH);
16
17__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(DFU);
18__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(DFU);
diff --git a/examples/boot/bootloader/stm32-dual-bank/src/main.rs b/examples/boot/bootloader/stm32-dual-bank/src/main.rs
new file mode 100644
index 000000000..4d2e82d26
--- /dev/null
+++ b/examples/boot/bootloader/stm32-dual-bank/src/main.rs
@@ -0,0 +1,53 @@
1#![no_std]
2#![no_main]
3
4use core::cell::RefCell;
5
6use cortex_m_rt::{entry, exception};
7#[cfg(feature = "defmt")]
8use defmt_rtt as _;
9use embassy_boot_stm32::*;
10use embassy_stm32::flash::{Flash, BANK1_REGION};
11use embassy_sync::blocking_mutex::Mutex;
12
13#[entry]
14fn main() -> ! {
15 let p = embassy_stm32::init(Default::default());
16
17 // Uncomment this if you are debugging the bootloader with debugger/RTT attached,
18 // as it prevents a hard fault when accessing flash 'too early' after boot.
19 /*
20 for i in 0..10000000 {
21 cortex_m::asm::nop();
22 }
23 */
24
25 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
26 let flash_bank1 = Mutex::new(RefCell::new(layout.bank1_region));
27 let flash_bank2 = Mutex::new(RefCell::new(layout.bank2_region));
28
29 let config = BootLoaderConfig::from_linkerfile_blocking(&flash_bank1, &flash_bank2, &flash_bank1);
30 let active_offset = config.active.offset();
31 let bl = BootLoader::prepare::<_, _, _, 2048>(config);
32
33 unsafe { bl.load(BANK1_REGION.base + active_offset) }
34}
35
36#[no_mangle]
37#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
38unsafe extern "C" fn HardFault() {
39 cortex_m::peripheral::SCB::sys_reset();
40}
41
42#[exception]
43unsafe fn DefaultHandler(_: i16) -> ! {
44 const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
45 let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
46
47 panic!("DefaultHandler #{:?}", irqn);
48}
49
50#[panic_handler]
51fn panic(_info: &core::panic::PanicInfo) -> ! {
52 cortex_m::asm::udf();
53}
diff --git a/examples/boot/bootloader/stm32/src/main.rs b/examples/boot/bootloader/stm32/src/main.rs
index 5fd9ea588..99a7a6a6b 100644
--- a/examples/boot/bootloader/stm32/src/main.rs
+++ b/examples/boot/bootloader/stm32/src/main.rs
@@ -25,7 +25,7 @@ fn main() -> ! {
25 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions(); 25 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
26 let flash = Mutex::new(RefCell::new(layout.bank1_region)); 26 let flash = Mutex::new(RefCell::new(layout.bank1_region));
27 27
28 let config = BootLoaderConfig::from_linkerfile_blocking(&flash); 28 let config = BootLoaderConfig::from_linkerfile_blocking(&flash, &flash, &flash);
29 let active_offset = config.active.offset(); 29 let active_offset = config.active.offset();
30 let bl = BootLoader::prepare::<_, _, _, 2048>(config); 30 let bl = BootLoader::prepare::<_, _, _, 2048>(config);
31 31
diff --git a/examples/boot/bootloader/stm32wb-dfu/src/main.rs b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
index a7ab813b6..d989fbfdf 100644
--- a/examples/boot/bootloader/stm32wb-dfu/src/main.rs
+++ b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
@@ -35,7 +35,7 @@ fn main() -> ! {
35 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions(); 35 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
36 let flash = Mutex::new(RefCell::new(layout.bank1_region)); 36 let flash = Mutex::new(RefCell::new(layout.bank1_region));
37 37
38 let config = BootLoaderConfig::from_linkerfile_blocking(&flash); 38 let config = BootLoaderConfig::from_linkerfile_blocking(&flash, &flash, &flash);
39 let active_offset = config.active.offset(); 39 let active_offset = config.active.offset();
40 let bl = BootLoader::prepare::<_, _, _, 2048>(config); 40 let bl = BootLoader::prepare::<_, _, _, 2048>(config);
41 if bl.state == State::DfuDetach { 41 if bl.state == State::DfuDetach {
@@ -45,7 +45,7 @@ fn main() -> ! {
45 config.product = Some("USB-DFU Bootloader example"); 45 config.product = Some("USB-DFU Bootloader example");
46 config.serial_number = Some("1235678"); 46 config.serial_number = Some("1235678");
47 47
48 let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); 48 let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash, &flash);
49 let mut buffer = AlignedBuffer([0; WRITE_SIZE]); 49 let mut buffer = AlignedBuffer([0; WRITE_SIZE]);
50 let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]); 50 let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]);
51 51