aboutsummaryrefslogtreecommitdiff
path: root/embassy-boot/src/firmware_updater
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-01-11 18:55:59 +0100
committerDario Nieuwenhuis <[email protected]>2024-01-11 18:55:59 +0100
commite0775fbc8ab1ecc83bce42fe6e11accf481bc9e1 (patch)
tree3d5119500fbb8627829e54e6bc999c3689ab4a0f /embassy-boot/src/firmware_updater
parentb452a6bcf6858893a85882614e2dcde5a3405748 (diff)
Flatten embassy-boot dir tree
Diffstat (limited to 'embassy-boot/src/firmware_updater')
-rw-r--r--embassy-boot/src/firmware_updater/asynch.rs329
-rw-r--r--embassy-boot/src/firmware_updater/blocking.rs340
-rw-r--r--embassy-boot/src/firmware_updater/mod.rs49
3 files changed, 718 insertions, 0 deletions
diff --git a/embassy-boot/src/firmware_updater/asynch.rs b/embassy-boot/src/firmware_updater/asynch.rs
new file mode 100644
index 000000000..2e43e1cc1
--- /dev/null
+++ b/embassy-boot/src/firmware_updater/asynch.rs
@@ -0,0 +1,329 @@
1use digest::Digest;
2#[cfg(target_os = "none")]
3use embassy_embedded_hal::flash::partition::Partition;
4#[cfg(target_os = "none")]
5use embassy_sync::blocking_mutex::raw::NoopRawMutex;
6use embedded_storage_async::nor_flash::NorFlash;
7
8use super::FirmwareUpdaterConfig;
9use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERASE_VALUE, SWAP_MAGIC};
10
11/// FirmwareUpdater is an application API for interacting with the BootLoader without the ability to
12/// 'mess up' the internal bootloader state
13pub struct FirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
14 dfu: DFU,
15 state: FirmwareState<'d, STATE>,
16}
17
18#[cfg(target_os = "none")]
19impl<'a, FLASH: NorFlash>
20 FirmwareUpdaterConfig<Partition<'a, NoopRawMutex, FLASH>, Partition<'a, NoopRawMutex, FLASH>>
21{
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 {
24 extern "C" {
25 static __bootloader_state_start: u32;
26 static __bootloader_state_end: u32;
27 static __bootloader_dfu_start: u32;
28 static __bootloader_dfu_end: u32;
29 }
30
31 let dfu = unsafe {
32 let start = &__bootloader_dfu_start as *const u32 as u32;
33 let end = &__bootloader_dfu_end as *const u32 as u32;
34 trace!("DFU: 0x{:x} - 0x{:x}", start, end);
35
36 Partition::new(flash, start, end - start)
37 };
38 let state = unsafe {
39 let start = &__bootloader_state_start as *const u32 as u32;
40 let end = &__bootloader_state_end as *const u32 as u32;
41 trace!("STATE: 0x{:x} - 0x{:x}", start, end);
42
43 Partition::new(flash, start, end - start)
44 };
45
46 Self { dfu, state }
47 }
48}
49
50impl<'d, DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<'d, DFU, STATE> {
51 /// Create a firmware updater instance with partition ranges for the update and state partitions.
52 pub fn new(config: FirmwareUpdaterConfig<DFU, STATE>, aligned: &'d mut [u8]) -> Self {
53 Self {
54 dfu: config.dfu,
55 state: FirmwareState::new(config.state, aligned),
56 }
57 }
58
59 /// Obtain the current state.
60 ///
61 /// This is useful to check if the bootloader has just done a swap, in order
62 /// to do verifications and self-tests of the new image before calling
63 /// `mark_booted`.
64 pub async fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
65 self.state.get_state().await
66 }
67
68 /// Verify the DFU given a public key. If there is an error then DO NOT
69 /// proceed with updating the firmware as it must be signed with a
70 /// corresponding private key (otherwise it could be malicious firmware).
71 ///
72 /// Mark to trigger firmware swap on next boot if verify suceeds.
73 ///
74 /// If the "ed25519-salty" feature is set (or another similar feature) then the signature is expected to have
75 /// been generated from a SHA-512 digest of the firmware bytes.
76 ///
77 /// If no signature feature is set then this method will always return a
78 /// signature error.
79 #[cfg(feature = "_verify")]
80 pub async fn verify_and_mark_updated(
81 &mut self,
82 _public_key: &[u8; 32],
83 _signature: &[u8; 64],
84 _update_len: u32,
85 ) -> Result<(), FirmwareUpdaterError> {
86 assert!(_update_len <= self.dfu.capacity() as u32);
87
88 self.state.verify_booted().await?;
89
90 #[cfg(feature = "ed25519-dalek")]
91 {
92 use ed25519_dalek::{Signature, SignatureError, Verifier, VerifyingKey};
93
94 use crate::digest_adapters::ed25519_dalek::Sha512;
95
96 let into_signature_error = |e: SignatureError| FirmwareUpdaterError::Signature(e.into());
97
98 let public_key = VerifyingKey::from_bytes(_public_key).map_err(into_signature_error)?;
99 let signature = Signature::from_bytes(_signature);
100
101 let mut chunk_buf = [0; 2];
102 let mut message = [0; 64];
103 self.hash::<Sha512>(_update_len, &mut chunk_buf, &mut message).await?;
104
105 public_key.verify(&message, &signature).map_err(into_signature_error)?
106 }
107 #[cfg(feature = "ed25519-salty")]
108 {
109 use salty::{PublicKey, Signature};
110
111 use crate::digest_adapters::salty::Sha512;
112
113 fn into_signature_error<E>(_: E) -> FirmwareUpdaterError {
114 FirmwareUpdaterError::Signature(signature::Error::default())
115 }
116
117 let public_key = PublicKey::try_from(_public_key).map_err(into_signature_error)?;
118 let signature = Signature::try_from(_signature).map_err(into_signature_error)?;
119
120 let mut message = [0; 64];
121 let mut chunk_buf = [0; 2];
122 self.hash::<Sha512>(_update_len, &mut chunk_buf, &mut message).await?;
123
124 let r = public_key.verify(&message, &signature);
125 trace!(
126 "Verifying with public key {}, signature {} and message {} yields ok: {}",
127 public_key.to_bytes(),
128 signature.to_bytes(),
129 message,
130 r.is_ok()
131 );
132 r.map_err(into_signature_error)?
133 }
134
135 self.state.mark_updated().await
136 }
137
138 /// Verify the update in DFU with any digest.
139 pub async fn hash<D: Digest>(
140 &mut self,
141 update_len: u32,
142 chunk_buf: &mut [u8],
143 output: &mut [u8],
144 ) -> Result<(), FirmwareUpdaterError> {
145 let mut digest = D::new();
146 for offset in (0..update_len).step_by(chunk_buf.len()) {
147 self.dfu.read(offset, chunk_buf).await?;
148 let len = core::cmp::min((update_len - offset) as usize, chunk_buf.len());
149 digest.update(&chunk_buf[..len]);
150 }
151 output.copy_from_slice(digest.finalize().as_slice());
152 Ok(())
153 }
154
155 /// Mark to trigger firmware swap on next boot.
156 #[cfg(not(feature = "_verify"))]
157 pub async fn mark_updated(&mut self) -> Result<(), FirmwareUpdaterError> {
158 self.state.mark_updated().await
159 }
160
161 /// Mark to trigger USB DFU on next boot.
162 pub async fn mark_dfu(&mut self) -> Result<(), FirmwareUpdaterError> {
163 self.state.verify_booted().await?;
164 self.state.mark_dfu().await
165 }
166
167 /// Mark firmware boot successful and stop rollback on reset.
168 pub async fn mark_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
169 self.state.mark_booted().await
170 }
171
172 /// Write data to a flash page.
173 ///
174 /// The buffer must follow alignment requirements of the target flash and a multiple of page size big.
175 ///
176 /// # Safety
177 ///
178 /// Failing to meet alignment and size requirements may result in a panic.
179 pub async fn write_firmware(&mut self, offset: usize, data: &[u8]) -> Result<(), FirmwareUpdaterError> {
180 assert!(data.len() >= DFU::ERASE_SIZE);
181
182 self.state.verify_booted().await?;
183
184 self.dfu.erase(offset as u32, (offset + data.len()) as u32).await?;
185
186 self.dfu.write(offset as u32, data).await?;
187
188 Ok(())
189 }
190
191 /// Prepare for an incoming DFU update by erasing the entire DFU area and
192 /// returning its `Partition`.
193 ///
194 /// Using this instead of `write_firmware` allows for an optimized API in
195 /// exchange for added complexity.
196 pub async fn prepare_update(&mut self) -> Result<&mut DFU, FirmwareUpdaterError> {
197 self.state.verify_booted().await?;
198 self.dfu.erase(0, self.dfu.capacity() as u32).await?;
199
200 Ok(&mut self.dfu)
201 }
202}
203
204/// Manages the state partition of the firmware update.
205///
206/// Can be used standalone for more fine grained control, or as part of the updater.
207pub struct FirmwareState<'d, STATE> {
208 state: STATE,
209 aligned: &'d mut [u8],
210}
211
212impl<'d, STATE: NorFlash> FirmwareState<'d, STATE> {
213 /// Create a firmware state instance from a FirmwareUpdaterConfig with a buffer for magic content and state partition.
214 ///
215 /// # Safety
216 ///
217 /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being read from
218 /// and written to.
219 pub fn from_config<DFU: NorFlash>(config: FirmwareUpdaterConfig<DFU, STATE>, aligned: &'d mut [u8]) -> Self {
220 Self::new(config.state, aligned)
221 }
222
223 /// Create a firmware state instance with a buffer for magic content and state partition.
224 ///
225 /// # Safety
226 ///
227 /// The `aligned` buffer must have a size of maximum of STATE::WRITE_SIZE and STATE::READ_SIZE,
228 /// and follow the alignment rules for the flash being read from and written to.
229 pub fn new(state: STATE, aligned: &'d mut [u8]) -> Self {
230 assert_eq!(aligned.len(), STATE::WRITE_SIZE.max(STATE::READ_SIZE));
231 Self { state, aligned }
232 }
233
234 // Make sure we are running a booted firmware to avoid reverting to a bad state.
235 async fn verify_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
236 if self.get_state().await? == State::Boot {
237 Ok(())
238 } else {
239 Err(FirmwareUpdaterError::BadState)
240 }
241 }
242
243 /// Obtain the current state.
244 ///
245 /// This is useful to check if the bootloader has just done a swap, in order
246 /// to do verifications and self-tests of the new image before calling
247 /// `mark_booted`.
248 pub async fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
249 self.state.read(0, &mut self.aligned).await?;
250
251 if !self.aligned.iter().any(|&b| b != SWAP_MAGIC) {
252 Ok(State::Swap)
253 } else {
254 Ok(State::Boot)
255 }
256 }
257
258 /// Mark to trigger firmware swap on next boot.
259 pub async fn mark_updated(&mut self) -> Result<(), FirmwareUpdaterError> {
260 self.set_magic(SWAP_MAGIC).await
261 }
262
263 /// Mark to trigger USB DFU on next boot.
264 pub async fn mark_dfu(&mut self) -> Result<(), FirmwareUpdaterError> {
265 self.set_magic(DFU_DETACH_MAGIC).await
266 }
267
268 /// Mark firmware boot successful and stop rollback on reset.
269 pub async fn mark_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
270 self.set_magic(BOOT_MAGIC).await
271 }
272
273 async fn set_magic(&mut self, magic: u8) -> Result<(), FirmwareUpdaterError> {
274 self.state.read(0, &mut self.aligned).await?;
275
276 if self.aligned.iter().any(|&b| b != magic) {
277 // Read progress validity
278 self.state.read(STATE::WRITE_SIZE as u32, &mut self.aligned).await?;
279
280 if self.aligned.iter().any(|&b| b != STATE_ERASE_VALUE) {
281 // The current progress validity marker is invalid
282 } else {
283 // Invalidate progress
284 self.aligned.fill(!STATE_ERASE_VALUE);
285 self.state.write(STATE::WRITE_SIZE as u32, &self.aligned).await?;
286 }
287
288 // Clear magic and progress
289 self.state.erase(0, self.state.capacity() as u32).await?;
290
291 // Set magic
292 self.aligned.fill(magic);
293 self.state.write(0, &self.aligned).await?;
294 }
295 Ok(())
296 }
297}
298
299#[cfg(test)]
300mod tests {
301 use embassy_embedded_hal::flash::partition::Partition;
302 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
303 use embassy_sync::mutex::Mutex;
304 use futures::executor::block_on;
305 use sha1::{Digest, Sha1};
306
307 use super::*;
308 use crate::mem_flash::MemFlash;
309
310 #[test]
311 fn can_verify_sha1() {
312 let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default());
313 let state = Partition::new(&flash, 0, 4096);
314 let dfu = Partition::new(&flash, 65536, 65536);
315 let mut aligned = [0; 8];
316
317 let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
318 let mut to_write = [0; 4096];
319 to_write[..7].copy_from_slice(update.as_slice());
320
321 let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned);
322 block_on(updater.write_firmware(0, to_write.as_slice())).unwrap();
323 let mut chunk_buf = [0; 2];
324 let mut hash = [0; 20];
325 block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap();
326
327 assert_eq!(Sha1::digest(update).as_slice(), hash);
328 }
329}
diff --git a/embassy-boot/src/firmware_updater/blocking.rs b/embassy-boot/src/firmware_updater/blocking.rs
new file mode 100644
index 000000000..f1368540d
--- /dev/null
+++ b/embassy-boot/src/firmware_updater/blocking.rs
@@ -0,0 +1,340 @@
1use digest::Digest;
2#[cfg(target_os = "none")]
3use embassy_embedded_hal::flash::partition::BlockingPartition;
4#[cfg(target_os = "none")]
5use embassy_sync::blocking_mutex::raw::NoopRawMutex;
6use embedded_storage::nor_flash::NorFlash;
7
8use super::FirmwareUpdaterConfig;
9use crate::{FirmwareUpdaterError, State, BOOT_MAGIC, DFU_DETACH_MAGIC, STATE_ERASE_VALUE, SWAP_MAGIC};
10
11/// Blocking FirmwareUpdater is an application API for interacting with the BootLoader without the ability to
12/// 'mess up' the internal bootloader state
13pub struct BlockingFirmwareUpdater<'d, DFU: NorFlash, STATE: NorFlash> {
14 dfu: DFU,
15 state: BlockingFirmwareState<'d, STATE>,
16}
17
18#[cfg(target_os = "none")]
19impl<'a, FLASH: NorFlash>
20 FirmwareUpdaterConfig<BlockingPartition<'a, NoopRawMutex, FLASH>, BlockingPartition<'a, NoopRawMutex, FLASH>>
21{
22 /// Create a firmware updater config from the flash and address symbols defined in the linkerfile
23 pub fn from_linkerfile_blocking(
24 flash: &'a embassy_sync::blocking_mutex::Mutex<NoopRawMutex, core::cell::RefCell<FLASH>>,
25 ) -> Self {
26 extern "C" {
27 static __bootloader_state_start: u32;
28 static __bootloader_state_end: u32;
29 static __bootloader_dfu_start: u32;
30 static __bootloader_dfu_end: u32;
31 }
32
33 let dfu = unsafe {
34 let start = &__bootloader_dfu_start as *const u32 as u32;
35 let end = &__bootloader_dfu_end as *const u32 as u32;
36 trace!("DFU: 0x{:x} - 0x{:x}", start, end);
37
38 BlockingPartition::new(flash, start, end - start)
39 };
40 let state = unsafe {
41 let start = &__bootloader_state_start as *const u32 as u32;
42 let end = &__bootloader_state_end as *const u32 as u32;
43 trace!("STATE: 0x{:x} - 0x{:x}", start, end);
44
45 BlockingPartition::new(flash, start, end - start)
46 };
47
48 Self { dfu, state }
49 }
50}
51
52impl<'d, DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<'d, DFU, STATE> {
53 /// Create a firmware updater instance with partition ranges for the update and state partitions.
54 ///
55 /// # Safety
56 ///
57 /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being read from
58 /// and written to.
59 pub fn new(config: FirmwareUpdaterConfig<DFU, STATE>, aligned: &'d mut [u8]) -> Self {
60 Self {
61 dfu: config.dfu,
62 state: BlockingFirmwareState::new(config.state, aligned),
63 }
64 }
65
66 /// Obtain the current state.
67 ///
68 /// This is useful to check if the bootloader has just done a swap, in order
69 /// to do verifications and self-tests of the new image before calling
70 /// `mark_booted`.
71 pub fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
72 self.state.get_state()
73 }
74
75 /// Verify the DFU given a public key. If there is an error then DO NOT
76 /// proceed with updating the firmware as it must be signed with a
77 /// corresponding private key (otherwise it could be malicious firmware).
78 ///
79 /// Mark to trigger firmware swap on next boot if verify suceeds.
80 ///
81 /// If the "ed25519-salty" feature is set (or another similar feature) then the signature is expected to have
82 /// been generated from a SHA-512 digest of the firmware bytes.
83 ///
84 /// If no signature feature is set then this method will always return a
85 /// signature error.
86 #[cfg(feature = "_verify")]
87 pub fn verify_and_mark_updated(
88 &mut self,
89 _public_key: &[u8; 32],
90 _signature: &[u8; 64],
91 _update_len: u32,
92 ) -> Result<(), FirmwareUpdaterError> {
93 assert!(_update_len <= self.dfu.capacity() as u32);
94
95 self.state.verify_booted()?;
96
97 #[cfg(feature = "ed25519-dalek")]
98 {
99 use ed25519_dalek::{Signature, SignatureError, Verifier, VerifyingKey};
100
101 use crate::digest_adapters::ed25519_dalek::Sha512;
102
103 let into_signature_error = |e: SignatureError| FirmwareUpdaterError::Signature(e.into());
104
105 let public_key = VerifyingKey::from_bytes(_public_key).map_err(into_signature_error)?;
106 let signature = Signature::from_bytes(_signature);
107
108 let mut message = [0; 64];
109 let mut chunk_buf = [0; 2];
110 self.hash::<Sha512>(_update_len, &mut chunk_buf, &mut message)?;
111
112 public_key.verify(&message, &signature).map_err(into_signature_error)?
113 }
114 #[cfg(feature = "ed25519-salty")]
115 {
116 use salty::{PublicKey, Signature};
117
118 use crate::digest_adapters::salty::Sha512;
119
120 fn into_signature_error<E>(_: E) -> FirmwareUpdaterError {
121 FirmwareUpdaterError::Signature(signature::Error::default())
122 }
123
124 let public_key = PublicKey::try_from(_public_key).map_err(into_signature_error)?;
125 let signature = Signature::try_from(_signature).map_err(into_signature_error)?;
126
127 let mut message = [0; 64];
128 let mut chunk_buf = [0; 2];
129 self.hash::<Sha512>(_update_len, &mut chunk_buf, &mut message)?;
130
131 let r = public_key.verify(&message, &signature);
132 trace!(
133 "Verifying with public key {}, signature {} and message {} yields ok: {}",
134 public_key.to_bytes(),
135 signature.to_bytes(),
136 message,
137 r.is_ok()
138 );
139 r.map_err(into_signature_error)?
140 }
141
142 self.state.mark_updated()
143 }
144
145 /// Verify the update in DFU with any digest.
146 pub fn hash<D: Digest>(
147 &mut self,
148 update_len: u32,
149 chunk_buf: &mut [u8],
150 output: &mut [u8],
151 ) -> Result<(), FirmwareUpdaterError> {
152 let mut digest = D::new();
153 for offset in (0..update_len).step_by(chunk_buf.len()) {
154 self.dfu.read(offset, chunk_buf)?;
155 let len = core::cmp::min((update_len - offset) as usize, chunk_buf.len());
156 digest.update(&chunk_buf[..len]);
157 }
158 output.copy_from_slice(digest.finalize().as_slice());
159 Ok(())
160 }
161
162 /// Mark to trigger firmware swap on next boot.
163 #[cfg(not(feature = "_verify"))]
164 pub fn mark_updated(&mut self) -> Result<(), FirmwareUpdaterError> {
165 self.state.mark_updated()
166 }
167
168 /// Mark to trigger USB DFU device on next boot.
169 pub fn mark_dfu(&mut self) -> Result<(), FirmwareUpdaterError> {
170 self.state.verify_booted()?;
171 self.state.mark_dfu()
172 }
173
174 /// Mark firmware boot successful and stop rollback on reset.
175 pub fn mark_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
176 self.state.mark_booted()
177 }
178
179 /// Write data to a flash page.
180 ///
181 /// The buffer must follow alignment requirements of the target flash and a multiple of page size big.
182 ///
183 /// # Safety
184 ///
185 /// Failing to meet alignment and size requirements may result in a panic.
186 pub fn write_firmware(&mut self, offset: usize, data: &[u8]) -> Result<(), FirmwareUpdaterError> {
187 assert!(data.len() >= DFU::ERASE_SIZE);
188 self.state.verify_booted()?;
189
190 self.dfu.erase(offset as u32, (offset + data.len()) as u32)?;
191
192 self.dfu.write(offset as u32, data)?;
193
194 Ok(())
195 }
196
197 /// Prepare for an incoming DFU update by erasing the entire DFU area and
198 /// returning its `Partition`.
199 ///
200 /// Using this instead of `write_firmware` allows for an optimized API in
201 /// exchange for added complexity.
202 pub fn prepare_update(&mut self) -> Result<&mut DFU, FirmwareUpdaterError> {
203 self.state.verify_booted()?;
204 self.dfu.erase(0, self.dfu.capacity() as u32)?;
205
206 Ok(&mut self.dfu)
207 }
208}
209
210/// Manages the state partition of the firmware update.
211///
212/// Can be used standalone for more fine grained control, or as part of the updater.
213pub struct BlockingFirmwareState<'d, STATE> {
214 state: STATE,
215 aligned: &'d mut [u8],
216}
217
218impl<'d, STATE: NorFlash> BlockingFirmwareState<'d, STATE> {
219 /// Creates a firmware state instance from a FirmwareUpdaterConfig, with a buffer for magic content and state partition.
220 ///
221 /// # Safety
222 ///
223 /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being read from
224 /// and written to.
225 pub fn from_config<DFU: NorFlash>(config: FirmwareUpdaterConfig<DFU, STATE>, aligned: &'d mut [u8]) -> Self {
226 Self::new(config.state, aligned)
227 }
228
229 /// Create a firmware state instance with a buffer for magic content and state partition.
230 ///
231 /// # Safety
232 ///
233 /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being read from
234 /// and written to.
235 pub fn new(state: STATE, aligned: &'d mut [u8]) -> Self {
236 assert_eq!(aligned.len(), STATE::WRITE_SIZE);
237 Self { state, aligned }
238 }
239
240 // Make sure we are running a booted firmware to avoid reverting to a bad state.
241 fn verify_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
242 if self.get_state()? == State::Boot || self.get_state()? == State::DfuDetach {
243 Ok(())
244 } else {
245 Err(FirmwareUpdaterError::BadState)
246 }
247 }
248
249 /// Obtain the current state.
250 ///
251 /// This is useful to check if the bootloader has just done a swap, in order
252 /// to do verifications and self-tests of the new image before calling
253 /// `mark_booted`.
254 pub fn get_state(&mut self) -> Result<State, FirmwareUpdaterError> {
255 self.state.read(0, &mut self.aligned)?;
256
257 if !self.aligned.iter().any(|&b| b != SWAP_MAGIC) {
258 Ok(State::Swap)
259 } else if !self.aligned.iter().any(|&b| b != DFU_DETACH_MAGIC) {
260 Ok(State::DfuDetach)
261 } else {
262 Ok(State::Boot)
263 }
264 }
265
266 /// Mark to trigger firmware swap on next boot.
267 pub fn mark_updated(&mut self) -> Result<(), FirmwareUpdaterError> {
268 self.set_magic(SWAP_MAGIC)
269 }
270
271 /// Mark to trigger USB DFU on next boot.
272 pub fn mark_dfu(&mut self) -> Result<(), FirmwareUpdaterError> {
273 self.set_magic(DFU_DETACH_MAGIC)
274 }
275
276 /// Mark firmware boot successful and stop rollback on reset.
277 pub fn mark_booted(&mut self) -> Result<(), FirmwareUpdaterError> {
278 self.set_magic(BOOT_MAGIC)
279 }
280
281 fn set_magic(&mut self, magic: u8) -> Result<(), FirmwareUpdaterError> {
282 self.state.read(0, &mut self.aligned)?;
283
284 if self.aligned.iter().any(|&b| b != magic) {
285 // Read progress validity
286 self.state.read(STATE::WRITE_SIZE as u32, &mut self.aligned)?;
287
288 if self.aligned.iter().any(|&b| b != STATE_ERASE_VALUE) {
289 // The current progress validity marker is invalid
290 } else {
291 // Invalidate progress
292 self.aligned.fill(!STATE_ERASE_VALUE);
293 self.state.write(STATE::WRITE_SIZE as u32, &self.aligned)?;
294 }
295
296 // Clear magic and progress
297 self.state.erase(0, self.state.capacity() as u32)?;
298
299 // Set magic
300 self.aligned.fill(magic);
301 self.state.write(0, &self.aligned)?;
302 }
303 Ok(())
304 }
305}
306
307#[cfg(test)]
308mod tests {
309 use core::cell::RefCell;
310
311 use embassy_embedded_hal::flash::partition::BlockingPartition;
312 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
313 use embassy_sync::blocking_mutex::Mutex;
314 use sha1::{Digest, Sha1};
315
316 use super::*;
317 use crate::mem_flash::MemFlash;
318
319 #[test]
320 fn can_verify_sha1() {
321 let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default()));
322 let state = BlockingPartition::new(&flash, 0, 4096);
323 let dfu = BlockingPartition::new(&flash, 65536, 65536);
324 let mut aligned = [0; 8];
325
326 let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
327 let mut to_write = [0; 4096];
328 to_write[..7].copy_from_slice(update.as_slice());
329
330 let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned);
331 updater.write_firmware(0, to_write.as_slice()).unwrap();
332 let mut chunk_buf = [0; 2];
333 let mut hash = [0; 20];
334 updater
335 .hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)
336 .unwrap();
337
338 assert_eq!(Sha1::digest(update).as_slice(), hash);
339 }
340}
diff --git a/embassy-boot/src/firmware_updater/mod.rs b/embassy-boot/src/firmware_updater/mod.rs
new file mode 100644
index 000000000..4814786bf
--- /dev/null
+++ b/embassy-boot/src/firmware_updater/mod.rs
@@ -0,0 +1,49 @@
1mod asynch;
2mod blocking;
3
4pub use asynch::{FirmwareState, FirmwareUpdater};
5pub use blocking::{BlockingFirmwareState, BlockingFirmwareUpdater};
6use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind};
7
8/// Firmware updater flash configuration holding the two flashes used by the updater
9///
10/// If only a single flash is actually used, then that flash should be partitioned into two partitions before use.
11/// The easiest way to do this is to use [`FirmwareUpdaterConfig::from_linkerfile`] or [`FirmwareUpdaterConfig::from_linkerfile_blocking`] which will partition
12/// the provided flash according to symbols defined in the linkerfile.
13pub struct FirmwareUpdaterConfig<DFU, STATE> {
14 /// The dfu flash partition
15 pub dfu: DFU,
16 /// The state flash partition
17 pub state: STATE,
18}
19
20/// Errors returned by FirmwareUpdater
21#[derive(Debug)]
22pub enum FirmwareUpdaterError {
23 /// Error from flash.
24 Flash(NorFlashErrorKind),
25 /// Signature errors.
26 Signature(signature::Error),
27 /// Bad state.
28 BadState,
29}
30
31#[cfg(feature = "defmt")]
32impl defmt::Format for FirmwareUpdaterError {
33 fn format(&self, fmt: defmt::Formatter) {
34 match self {
35 FirmwareUpdaterError::Flash(_) => defmt::write!(fmt, "FirmwareUpdaterError::Flash(_)"),
36 FirmwareUpdaterError::Signature(_) => defmt::write!(fmt, "FirmwareUpdaterError::Signature(_)"),
37 FirmwareUpdaterError::BadState => defmt::write!(fmt, "FirmwareUpdaterError::BadState"),
38 }
39 }
40}
41
42impl<E> From<E> for FirmwareUpdaterError
43where
44 E: NorFlashError,
45{
46 fn from(error: E) -> Self {
47 FirmwareUpdaterError::Flash(error.kind())
48 }
49}