Skip to content

Commit d0e632e

Browse files
authored
Merge pull request #4 from huming2207/master
Implement verify function
2 parents c734d17 + 680ba43 commit d0e632e

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
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: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)