@@ -237,6 +237,7 @@ public void FlashDfuFile(string filePath)
237237 Console . Write ( "Flashing device..." ) ;
238238 }
239239
240+ // write flash
240241 for ( int elementIndex = 0 ; elementIndex < dfuTarget . DfuElements . Length ; ++ elementIndex )
241242 {
242243 StDfu . DfuElement dfuElement = dfuTarget . DfuElements [ elementIndex ] ;
@@ -256,6 +257,44 @@ public void FlashDfuFile(string filePath)
256257 Console . WriteLine ( " OK" ) ;
257258 }
258259
260+ if ( Verbosity >= VerbosityLevel . Normal )
261+ {
262+ Console . Write ( "Verifying flash..." ) ;
263+ }
264+
265+ // read back for confirmation
266+ for ( int elementIndex = 0 ; elementIndex < dfuTarget . DfuElements . Length ; ++ elementIndex )
267+ {
268+ StDfu . DfuElement dfuElement = dfuTarget . DfuElements [ elementIndex ] ;
269+
270+ // read the data in MaxWriteBlockSize blocks
271+ for ( uint blockNumber = 0 ; blockNumber <= ( uint ) dfuElement . Data . Length / _maxWriteBlockSize ; blockNumber ++ )
272+ {
273+ byte [ ] readBuffer = new byte [ _maxWriteBlockSize ] ;
274+ StDfu . ReadBlock ( _hDevice , dfuElement . Address , readBuffer , blockNumber ) ;
275+
276+ // get data for the current block
277+ byte [ ] buffer = dfuElement . Data . Skip ( ( int ) ( _maxWriteBlockSize * blockNumber ) ) . Take ( _maxWriteBlockSize ) . ToArray ( ) ;
278+
279+ // exit condition has to be tied with the buffer length because the data for the last block
280+ // can be shorter than the block max size
281+ for ( int index = 0 ; index < buffer . Length ; index ++ )
282+ {
283+ if ( readBuffer [ index ] != buffer [ index ] )
284+ {
285+ Console . WriteLine ( "" ) ;
286+ Console . WriteLine ( $ "Error verifying flash write. Check failed for block address 0x{ dfuElement . Address + blockNumber * _maxWriteBlockSize : X8} .") ;
287+ throw new DfuVerificationFailedException ( ) ;
288+ }
289+ }
290+ }
291+ }
292+
293+ if ( Verbosity >= VerbosityLevel . Normal )
294+ {
295+ Console . WriteLine ( " OK" ) ;
296+ }
297+
259298 if ( Verbosity >= VerbosityLevel . Normal )
260299 {
261300 Console . Write ( "Launching nanoBooter..." ) ;
0 commit comments