@@ -59,6 +59,16 @@ pub trait FlashAlgorithm: Sized + 'static {
59
59
/// * `address` - The start address of the flash page to program.
60
60
/// * `data` - The data to be written to the page.
61
61
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 > ;
62
72
}
63
73
64
74
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
@@ -145,6 +155,7 @@ macro_rules! algorithm {
145
155
}
146
156
}
147
157
$crate:: erase_chip!( $type) ;
158
+ $crate:: verify!( $type) ;
148
159
149
160
#[ allow( non_upper_case_globals) ]
150
161
#[ no_mangle]
@@ -214,7 +225,7 @@ macro_rules! algorithm {
214
225
#[ macro_export]
215
226
#[ cfg( not( feature = "erase-chip" ) ) ]
216
227
macro_rules! erase_chip {
217
- ( $type: ty) => { }
228
+ ( $type: ty) => { } ;
218
229
}
219
230
#[ doc( hidden) ]
220
231
#[ macro_export]
@@ -233,7 +244,42 @@ macro_rules! erase_chip {
233
244
Err ( e) => e. get( ) ,
234
245
}
235
246
}
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
+ } ;
237
283
}
238
284
239
285
#[ doc( hidden) ]
0 commit comments