@@ -312,3 +312,61 @@ bool target_flash_complete(target_s *target)
312312 target_exit_flash_mode (target );
313313 return result ;
314314}
315+
316+ static bool flash_blank_check (target_flash_s * flash , target_addr_t src , size_t len , target_addr_t * mismatch )
317+ {
318+ bool result = true; /* Catch false returns with &= */
319+ target_s * target = flash -> t ;
320+ platform_timeout_s timeout ;
321+ platform_timeout_set (& timeout , 500 );
322+
323+ while (len ) {
324+ const size_t offset = src % flash -> writebufsize ;
325+ const size_t local_len = MIN (flash -> writebufsize - offset , len );
326+ /* Fetch chunk into sector buffer */
327+ target_mem32_read (target , flash -> buf , src , local_len );
328+
329+ /* Compare bytewise with erased value */
330+ const uint8_t erased = flash -> erased ;
331+ for (size_t i = 0 ; i < local_len ; i ++ ) {
332+ if (flash -> buf [i ] != erased ) {
333+ * mismatch = src + i ;
334+ return false;
335+ }
336+ }
337+ src += local_len ;
338+ len -= local_len ;
339+ target_print_progress (& timeout );
340+ }
341+ return result ;
342+ }
343+
344+ bool target_flash_blank_check (target_s * target )
345+ {
346+ if (!target -> flash )
347+ return false;
348+
349+ bool result = true;
350+ target_addr_t mismatch = 0 ;
351+
352+ for (target_flash_s * flash = target -> flash ; flash ; flash = flash -> next ) {
353+ if (!flash -> buf && !flash_buffer_alloc (flash ))
354+ return false;
355+
356+ const target_addr_t local_end = flash -> start + flash -> length ;
357+ for (target_addr_t local_start = flash -> start ; local_start < local_end ; local_start += flash -> blocksize ) {
358+ result = flash_blank_check (flash , local_start , flash -> blocksize , & mismatch );
359+ if (!result )
360+ tc_printf (target , "Has data at 0x%08" PRIx32 "\n" , mismatch );
361+ else
362+ tc_printf (target , "Blank 0x%08" PRIx32 "+%" PRIu32 "\n" , local_start , (uint32_t )flash -> blocksize );
363+ }
364+ /* Free the operation buffer */
365+ if (flash -> buf ) {
366+ free (flash -> buf );
367+ flash -> buf = NULL ;
368+ }
369+ }
370+
371+ return result ;
372+ }
0 commit comments