From 9df731a176aab8e03b984b681b1bea01ccff6644 Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Mon, 06 Nov 2023 07:23:06 +0000
Subject: [PATCH] rk3568 rt uboot init

---
 u-boot/common/spl/spl_ab.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/u-boot/common/spl/spl_ab.c b/u-boot/common/spl/spl_ab.c
index 31b63f7..842c36c 100644
--- a/u-boot/common/spl/spl_ab.c
+++ b/u-boot/common/spl/spl_ab.c
@@ -319,3 +319,38 @@
 out:
 	return ret;
 }
+
+/*
+ * If boot A/B system fail, tries-remaining decrease 1
+ * and do reset automatically if still bootable.
+ */
+int spl_ab_decrease_reset(struct blk_desc *dev_desc)
+{
+	AvbABData ab_data;
+	int ret;
+
+	ret = spl_ab_data_read(dev_desc, &ab_data, "misc");
+	if (ret)
+		return ret;
+
+	/* If current device cannot boot, return and try other devices. */
+	if (!spl_slot_is_bootable(&ab_data.slots[0]) &&
+	    !spl_slot_is_bootable(&ab_data.slots[1])) {
+		printf("A/B: no bootable slot\n");
+		return -ENODEV;
+	}
+
+	/* If current device still can boot, decrease and do reset. */
+	ret = spl_ab_decrease_tries(dev_desc);
+	if (ret)
+		return ret;
+
+	printf("A/B: slot boot fail, do reset\n");
+	do_reset(NULL, 0, 0, NULL);
+
+	/*
+	 * Only do_reset() fail will arrive here, return a
+	 * negative number, then enter maskrom in the caller.
+	 */
+	return -EINVAL;
+}

--
Gitblit v1.6.2