@@ -54,8 +54,12 @@ zephyr_img_mgmt_slot_to_image(int slot)
5454
5555/**
5656 * Determines if the specified area of flash is completely unwritten.
57+ *
58+ * @param fa pointer to flash area to scan
59+ *
60+ * @return 0 when not empty, 1 when empty, negative errno code on error.
5761 */
58- static int img_mgmt_flash_check_empty_inner (const struct flash_area * fa , bool * out_empty )
62+ static int img_mgmt_flash_check_empty_inner (const struct flash_area * fa )
5963{
6064 uint32_t data [16 ];
6165 off_t addr ;
@@ -80,37 +84,38 @@ static int img_mgmt_flash_check_empty_inner(const struct flash_area *fa, bool *o
8084 }
8185
8286 rc = flash_area_read (fa , addr , data , bytes_to_read );
83- if (rc != 0 ) {
84- return MGMT_ERR_EUNKNOWN ;
87+ if (rc < 0 ) {
88+ return rc ;
8589 }
8690
8791 for (i = 0 ; i < bytes_to_read / 4 ; i ++ ) {
8892 if (data [i ] != erased_val_32 ) {
89- * out_empty = false;
9093 return 0 ;
9194 }
9295 }
9396 }
9497
95- * out_empty = true;
96-
97- return 0 ;
98+ return 1 ;
9899}
99100
100101#ifndef CONFIG_IMG_ERASE_PROGRESSIVELY
101- static int img_mgmt_flash_check_empty (uint8_t fa_id , bool * out_empty )
102+ /* Check if area is empty
103+ *
104+ * @param fa_id ID of flash area to scan.
105+ *
106+ * @return 0 when not empty, 1 when empty, negative errno code on error.
107+ */
108+ static int img_mgmt_flash_check_empty (uint8_t fa_id )
102109{
103110 const struct flash_area * fa ;
104111 int rc ;
105112
106113 rc = flash_area_open (fa_id , & fa );
107- if (rc != 0 ) {
108- return MGMT_ERR_EUNKNOWN ;
109- }
114+ if (rc == 0 ) {
115+ rc = img_mgmt_flash_check_empty_inner (fa );
110116
111- rc = img_mgmt_flash_check_empty_inner (fa , out_empty );
112-
113- flash_area_close (fa );
117+ flash_area_close (fa );
118+ }
114119
115120 return rc ;
116121}
@@ -259,27 +264,26 @@ img_mgmt_impl_erase_slot(int slot)
259264 const struct flash_area * fa ;
260265 int rc ;
261266 int area_id = zephyr_img_mgmt_flash_area_id (slot );
262- bool empty ;
263267
264268 if (area_id < 0 ) {
265269 return MGMT_ERR_EUNKNOWN ;
266270 }
267271
268272 rc = flash_area_open (area_id , & fa );
269273
270- if (rc != 0 ) {
274+ if (rc < 0 ) {
271275 return MGMT_ERR_EUNKNOWN ;
272276 }
273277
274- rc = img_mgmt_flash_check_empty_inner (fa , & empty );
278+ rc = img_mgmt_flash_check_empty_inner (fa );
275279
276- if (! empty && rc == 0 ) {
280+ if (rc == 0 ) {
277281 rc = flash_area_erase (fa , 0 , fa -> fa_size );
278282 }
279283
280284 flash_area_close (fa );
281285
282- return (rc = = 0 ? MGMT_ERR_EOK : MGMT_ERR_EUNKNOWN );
286+ return (rc > = 0 ? MGMT_ERR_EOK : MGMT_ERR_EUNKNOWN );
283287}
284288
285289int
@@ -529,9 +533,6 @@ img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req,
529533 const struct image_header * hdr ;
530534 struct image_version cur_ver ;
531535 int rc ;
532- #ifndef CONFIG_IMG_ERASE_PROGRESSIVELY
533- bool empty ;
534- #endif
535536
536537 memset (action , 0 , sizeof (* action ));
537538
@@ -625,12 +626,12 @@ img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req,
625626 }
626627
627628#ifndef CONFIG_IMG_ERASE_PROGRESSIVELY
628- rc = img_mgmt_flash_check_empty (action -> area_id , & empty );
629- if (rc ) {
629+ rc = img_mgmt_flash_check_empty (action -> area_id );
630+ if (rc < 0 ) {
630631 return MGMT_ERR_EUNKNOWN ;
631632 }
632633
633- action -> erase = ! empty ;
634+ action -> erase = ( rc == 0 ) ;
634635#endif
635636 } else {
636637 /* Continuation of upload. */
0 commit comments