Skip to content

Commit 680ba43

Browse files
committed
Implement verify function
1 parent 0d68027 commit 680ba43

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ description = "A crate to write CMSIS-DAP flash algorithms for flashing embedded
1414
default = ["erase-chip", "panic-handler"]
1515
erase-chip = []
1616
panic-handler = []
17-
17+
verify = []

src/lib.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Implement a [CMSIS-Pack] flash algorithm in Rust
2-
//!
2+
//!
33
//! [CMSIS-Pack]: https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/flashAlgorithm.html
4-
//!
4+
//!
55
//! # Feature flags
6-
//!
6+
//!
77
//! - `panic-handler` this is enabled by default and includes a simple abort-on-panic
88
//! panic handler. Disable this feature flag if you would prefer to use a different
99
//! handler.
@@ -59,6 +59,16 @@ pub trait FlashAlgorithm: Sized + 'static {
5959
/// * `address` - The start address of the flash page to program.
6060
/// * `data` - The data to be written to the page.
6161
fn program_page(&mut self, address: u32, data: &[u8]) -> Result<(), ErrorCode>;
62+
63+
/// Verify the firmware that has been programmed. Will only be called after [`FlashAlgorithm::new()`] with [`Function::Verify`].
64+
///
65+
/// # Arguments
66+
///
67+
/// * `address` - The start address of the flash to verify.
68+
/// * `size` - The length of the data to verify.
69+
/// * `data` - The data to compare with.
70+
#[cfg(feature = "verify")]
71+
fn verify(&mut self, address: u32, size: u32, data: Option<&[u8]>) -> Result<(), ErrorCode>;
6272
}
6373

6474
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -145,6 +155,7 @@ macro_rules! algorithm {
145155
}
146156
}
147157
$crate::erase_chip!($type);
158+
$crate::verify!($type);
148159

149160
#[allow(non_upper_case_globals)]
150161
#[no_mangle]
@@ -214,7 +225,7 @@ macro_rules! algorithm {
214225
#[macro_export]
215226
#[cfg(not(feature = "erase-chip"))]
216227
macro_rules! erase_chip {
217-
($type:ty) => {}
228+
($type:ty) => {};
218229
}
219230
#[doc(hidden)]
220231
#[macro_export]
@@ -233,7 +244,42 @@ macro_rules! erase_chip {
233244
Err(e) => e.get(),
234245
}
235246
}
236-
}
247+
};
248+
}
249+
250+
#[doc(hidden)]
251+
#[macro_export]
252+
#[cfg(not(feature = "verify"))]
253+
macro_rules! verify {
254+
($type:ty) => {};
255+
}
256+
#[doc(hidden)]
257+
#[macro_export]
258+
#[cfg(feature = "verify")]
259+
macro_rules! verify {
260+
($type:ty) => {
261+
#[no_mangle]
262+
#[link_section = ".entry"]
263+
pub unsafe extern "C" fn Verify(addr: u32, size: u32, data: *const u8) -> u32 {
264+
if !_IS_INIT {
265+
return 1;
266+
}
267+
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
268+
269+
if data.is_null() {
270+
match <$type as FlashAlgorithm>::verify(this, addr, size, None) {
271+
Ok(()) => 0,
272+
Err(e) => e.get(),
273+
}
274+
} else {
275+
let data_slice: &[u8] = unsafe { core::slice::from_raw_parts(data, size as usize) };
276+
match <$type as FlashAlgorithm>::verify(this, addr, size, Some(data_slice)) {
277+
Ok(()) => 0,
278+
Err(e) => e.get(),
279+
}
280+
}
281+
}
282+
};
237283
}
238284

239285
#[doc(hidden)]

0 commit comments

Comments
 (0)