From bf2df680286956b3187b1820ae54c5a0fb594857 Mon Sep 17 00:00:00 2001
From: Alexey Romanov <avromanov@salutedevices.com>
Date: Mon, 25 Dec 2023 13:22:45 +0300
Subject: [PATCH] android_ab: don't ignore ab_control_store return code

ab_control_store() can return an error if writing to disk fails.
In this case, we have to pass the error code to the caller.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
 boot/android_ab.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/boot/android_ab.c b/boot/android_ab.c
index 0f20a34e51..c9df6d2b4b 100644
--- a/boot/android_ab.c
+++ b/boot/android_ab.c
@@ -336,7 +336,14 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
 
 	if (store_needed) {
 		abc->crc32_le = ab_control_compute_crc(abc);
-		ab_control_store(dev_desc, part_info, abc, 0);
+		ret = ab_control_store(dev_desc, part_info, abc, 0);
+		if (ret < 0) {
+#if ANDROID_AB_BACKUP_OFFSET
+			free(backup_abc);
+#endif
+			free(abc);
+			return ret;
+		}
 	}
 
 #if ANDROID_AB_BACKUP_OFFSET
@@ -345,8 +352,13 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
 	 * to the backup offset
 	 */
 	if (memcmp(backup_abc, abc, sizeof(*abc)) != 0) {
-		ab_control_store(dev_desc, part_info, abc,
-				 ANDROID_AB_BACKUP_OFFSET);
+		ret = ab_control_store(dev_desc, part_info, abc,
+				       ANDROID_AB_BACKUP_OFFSET);
+		if (ret < 0) {
+			free(backup_abc);
+			free(abc);
+			return ret;
+		}
 	}
 	free(backup_abc);
 #endif
-- 
2.39.5