From 86dc0a3f51da3440bc216d988c04b225ba169247 Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Thu, 16 Jun 2022 12:46:08 +0200
Subject: [PATCH] ipq40xx: add dual boot support for ecw5211

Signed-off-by: John Crispin <john@phrozen.org>
---
 .../ipq40xx/base-files/etc/init.d/bootcount   | 17 ++++++++++++++---
 .../base-files/lib/upgrade/platform.sh        | 19 ++++++++++++++++++-
 .../arm/boot/dts/qcom-ipq4018-ecw5211.dts     | 10 ++++++++++
 3 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/target/linux/ipq40xx/base-files/etc/init.d/bootcount b/target/linux/ipq40xx/base-files/etc/init.d/bootcount
index 36b5d56d0c..5cda1fc245 100755
--- a/target/linux/ipq40xx/base-files/etc/init.d/bootcount
+++ b/target/linux/ipq40xx/base-files/etc/init.d/bootcount
@@ -13,13 +13,24 @@ boot() {
 	linksys,mr8300)
 		mtd resetbc s_env || true
 		;;
-	edgecore,spw2ac1200|\
-	edgecore,spw2ac1200-lan-poe|\
 	edgecore,ecw5211)
+		part="$(awk -F 'ubi.mtd=' '{printf $2}' /proc/cmdline | cut -d " " -f1)"
+		case "$part" in
+		rootfs1|\
+		rootfs2)
+			avail=$(fw_printenv -n upgrade_available)
+			[ ${avail} -ne 1 ] && fw_setenv upgrade_available 1
+			fw_setenv bootcount 0
+			;;
+		esac
+		;;
+	edgecore,spw2ac1200|\
+	edgecore,spw2ac1200-lan-poe)
 		avail=$(fw_printenv -n upgrade_available)
 		[ ${avail} -eq 0 ] || {
-			fw_setenv bootcount 0
 			fw_setenv upgrade_available 0
+			fw_setenv bootcount 0
 		}
+		;;
 	esac
 }
diff --git a/target/linux/ipq40xx/base-files/lib/upgrade/platform.sh b/target/linux/ipq40xx/base-files/lib/upgrade/platform.sh
index d44a57c62a..6f2bff527c 100644
--- a/target/linux/ipq40xx/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ipq40xx/base-files/lib/upgrade/platform.sh
@@ -66,7 +66,6 @@ platform_do_upgrade() {
 	avm,fritzrepeater-3000 |\
 	buffalo,wtr-m2133hp |\
 	cilab,meshpoint-one |\
-	edgecore,ecw5211 |\
 	edgecore,oap100 |\
 	engenius,eap2200 |\
 	glinet,gl-ap1300 |\
@@ -78,6 +77,24 @@ platform_do_upgrade() {
 	tp-link,ec420-g1)
 		nand_do_upgrade "$1"
 		;;
+	edgecore,ecw5211)
+		mkdir -p /var/lock/
+		part="$(awk -F 'ubi.mtd=' '{printf $2}' /proc/cmdline | cut -d " " -f 1)"
+		case "$part" in
+		rootfs1)
+			fw_setenv active 2 || exit 1
+			CI_UBIPART="rootfs2"
+			;;
+		rootfs2)
+			fw_setenv active 1 || exit 1
+			CI_UBIPART="rootfs1"
+			;;
+		*)
+			# legacy bootloader
+			;;
+		esac
+		nand_do_upgrade "$1"
+		;;
 	alfa-network,ap120c-ac)
 		mkdir -p /var/lock/
 		part="$(awk -F 'ubi.mtd=' '{printf $2}' /proc/cmdline | sed -e 's/ .*$//')"
diff --git a/target/linux/ipq40xx/files/arch/arm/boot/dts/qcom-ipq4018-ecw5211.dts b/target/linux/ipq40xx/files/arch/arm/boot/dts/qcom-ipq4018-ecw5211.dts
index 0ee8d1a52e..d8c0853c58 100644
--- a/target/linux/ipq40xx/files/arch/arm/boot/dts/qcom-ipq4018-ecw5211.dts
+++ b/target/linux/ipq40xx/files/arch/arm/boot/dts/qcom-ipq4018-ecw5211.dts
@@ -258,6 +258,16 @@
 				label = "rootfs";
 				reg = <0x00000000 0x04000000>;
 			};
+
+			partition@1 {
+				label = "rootfs1";
+				reg = <0x00000000 0x04000000>;
+			};
+
+			partition@4000000 {
+				label = "rootfs2";
+				reg = <0x04000000 0x04000000>;
+			};
 		};
 	};
 };
-- 
2.25.1

