@@ -291,6 +291,27 @@ boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
291291}
292292#endif
293293
294+ bool bootutil_buffer_is_erased (const struct flash_area * area ,
295+ const void * buffer , size_t len )
296+ {
297+ size_t i ;
298+ uint8_t * u8b ;
299+ uint8_t erased_val ;
300+
301+ if (buffer == NULL || len == 0 ) {
302+ return false;
303+ }
304+
305+ erased_val = flash_area_erased_val (area );
306+ for (i = 0 , u8b = (uint8_t * )buffer ; i < len ; i ++ ) {
307+ if (u8b [i ] != erased_val ) {
308+ return false;
309+ }
310+ }
311+
312+ return true;
313+ }
314+
294315int
295316boot_read_swap_state (const struct flash_area * fap ,
296317 struct boot_swap_state * state )
@@ -301,18 +322,18 @@ boot_read_swap_state(const struct flash_area *fap,
301322 int rc ;
302323
303324 off = boot_magic_off (fap );
304- rc = flash_area_read_is_empty (fap , off , magic , BOOT_MAGIC_SZ );
325+ rc = flash_area_read (fap , off , magic , BOOT_MAGIC_SZ );
305326 if (rc < 0 ) {
306327 return BOOT_EFLASH ;
307328 }
308- if (rc == 1 ) {
329+ if (bootutil_buffer_is_erased ( fap , magic , BOOT_MAGIC_SZ ) ) {
309330 state -> magic = BOOT_MAGIC_UNSET ;
310331 } else {
311332 state -> magic = boot_magic_decode (magic );
312333 }
313334
314335 off = boot_swap_info_off (fap );
315- rc = flash_area_read_is_empty (fap , off , & swap_info , sizeof swap_info );
336+ rc = flash_area_read (fap , off , & swap_info , sizeof swap_info );
316337 if (rc < 0 ) {
317338 return BOOT_EFLASH ;
318339 }
@@ -321,30 +342,31 @@ boot_read_swap_state(const struct flash_area *fap,
321342 state -> swap_type = BOOT_GET_SWAP_TYPE (swap_info );
322343 state -> image_num = BOOT_GET_IMAGE_NUM (swap_info );
323344
324- if (rc == 1 || state -> swap_type > BOOT_SWAP_TYPE_REVERT ) {
345+ if (bootutil_buffer_is_erased (fap , & swap_info , sizeof swap_info ) ||
346+ state -> swap_type > BOOT_SWAP_TYPE_REVERT ) {
325347 state -> swap_type = BOOT_SWAP_TYPE_NONE ;
326348 state -> image_num = 0 ;
327349 }
328350
329351 off = boot_copy_done_off (fap );
330- rc = flash_area_read_is_empty (fap , off , & state -> copy_done ,
331- sizeof state -> copy_done );
352+ rc = flash_area_read (fap , off , & state -> copy_done , sizeof state -> copy_done );
332353 if (rc < 0 ) {
333354 return BOOT_EFLASH ;
334355 }
335- if (rc == 1 ) {
356+ if (bootutil_buffer_is_erased (fap , & state -> copy_done ,
357+ sizeof state -> copy_done )) {
336358 state -> copy_done = BOOT_FLAG_UNSET ;
337359 } else {
338360 state -> copy_done = boot_flag_decode (state -> copy_done );
339361 }
340362
341363 off = boot_image_ok_off (fap );
342- rc = flash_area_read_is_empty (fap , off , & state -> image_ok ,
343- sizeof state -> image_ok );
364+ rc = flash_area_read (fap , off , & state -> image_ok , sizeof state -> image_ok );
344365 if (rc < 0 ) {
345366 return BOOT_EFLASH ;
346367 }
347- if (rc == 1 ) {
368+ if (bootutil_buffer_is_erased (fap , & state -> image_ok ,
369+ sizeof state -> image_ok )) {
348370 state -> image_ok = BOOT_FLAG_UNSET ;
349371 } else {
350372 state -> image_ok = boot_flag_decode (state -> image_ok );
0 commit comments