ANDROID: Don't count boot-to-recovery as a boot attempt.

The slot rollback system is intended for normal boot failures after an
OTA, and therefore, we should not attempt to change slots based on a failure
to boot to recovery (or any other non-normal boot sequence).

Bug: 141272741
Test: cuttlefish boots
Change-Id: I3edc99b5f60635d30aba441dbddb8a29f2e34bfc

Link: https://android-review.googlesource.com/c/platform/external/u-boot/+/1133068/2
Bug: https://baylibre.atlassian.net/browse/RITA-97
Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
diff --git a/cmd/ab_select.c b/cmd/ab_select.c
index 7c8f2ee..936c662 100644
--- a/cmd/ab_select.c
+++ b/cmd/ab_select.c
@@ -23,7 +23,7 @@
 		return CMD_RET_FAILURE;
 	}
 
-	ret = ab_select_slot(dev_desc, &part_info);
+	ret = ab_select_slot(dev_desc, &part_info, true);
 	if (ret < 0) {
 		printf("Android boot failed, error %d.\n", ret);
 		return CMD_RET_FAILURE;
diff --git a/common/android_ab.c b/common/android_ab.c
index 1449bf4..1ed4f86 100644
--- a/common/android_ab.c
+++ b/common/android_ab.c
@@ -176,7 +176,8 @@
 	return 0;
 }
 
-int ab_select_slot(struct blk_desc *dev_desc, const disk_partition_t *part_info)
+int ab_select_slot(struct blk_desc *dev_desc, const disk_partition_t *part_info,
+				   bool normal_boot)
 {
 	struct bootloader_control *abc = NULL;
 	u32 crc32_le;
@@ -263,7 +264,11 @@
 		}
 	}
 
-	if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
+	/* Note that we only count the boot attempt as a valid try when performing
+	 * normal boots to Android. Booting to recovery or fastboot does not count
+	 * as a normal boot.
+	 */
+	if (slot >= 0 && !abc->slot_info[slot].successful_boot && normal_boot) {
 		log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
 			BOOT_SLOT_NAME(slot),
 			abc->slot_info[slot].tries_remaining);
diff --git a/include/android_ab.h b/include/android_ab.h
index 989d661..a21ddab 100644
--- a/include/android_ab.h
+++ b/include/android_ab.h
@@ -27,8 +27,10 @@
  *
  * @param[in] dev_desc Device where we should read/write the boot_control struct.
  * @param[in] part_info Partition on the 'dev_desc' to read/write.
+ * @param[in] normal_boot True if a normal boot, false if booting to recovery.
  * @return The slot number (>= 0) on success, or a negative on error
  */
-int ab_select_slot(struct blk_desc *dev_desc, const disk_partition_t *part_info);
+int ab_select_slot(struct blk_desc *dev_desc, const disk_partition_t *part_info,
+				   bool normal_boot);
 
 #endif /* __ANDROID_AB_H */