rauc: Do not require an extra partition

...because we can do better by patching the built-in U-Boot's
configuration. Yup, it's a patch that is going to be quite painful to
maintain, but on the other hand the whole idea here is to *not* touch
the bootloader anyway because that is a risky operation. For that, it's
arguably safer to not rely on yet another piece of configuration (the
boot script) for reliable operation.

Change-Id: Ie559cf07d1dc6e50bc6ff80320509e3933b0af93
diff --git a/board/czechlight/clearfog/boot.scr.txt b/board/czechlight/clearfog/boot.scr.txt
deleted file mode 100644
index 99d980e..0000000
--- a/board/czechlight/clearfog/boot.scr.txt
+++ /dev/null
@@ -1,39 +0,0 @@
-test -n "${BOOT_A_LEFT}" || setenv BOOT_A_LEFT 3
-test -n "${BOOT_B_LEFT}" || setenv BOOT_B_LEFT 3
-
-setenv rauc_part
-
-for BOOT_SLOT in "A B"; do
-  if test "x${rauc_part}" != "x"; then
-    # skip remaining slots
-  elif test "x${BOOT_SLOT}" = "xA"; then
-    if test ${BOOT_A_LEFT} -gt 0; then
-      setexpr BOOT_A_LEFT ${BOOT_A_LEFT} - 1
-      echo "Found valid slot A, ${BOOT_A_LEFT} attempts remaining"
-      setenv rauc_part 2
-      setenv rauc_slot A
-    fi
-  elif test "x${BOOT_SLOT}" = "xB"; then
-    if test ${BOOT_B_LEFT} -gt 0; then
-      setexpr BOOT_B_LEFT ${BOOT_B_LEFT} - 1
-      echo "Found valid slot B, ${BOOT_B_LEFT} attempts remaining"
-      setenv rauc_part 3
-      setenv rauc_slot B
-    fi
-  fi
-done
-
-if test -n "${rauc_part}"; then
-  saveenv
-else
-  echo "No valid slot found, resetting tries to 3"
-  setenv BOOT_A_LEFT 3
-  setenv BOOT_B_LEFT 3
-  saveenv
-  reset
-fi
-
-load mmc 0:${rauc_part} ${fdt_addr_r} /boot/armada-388-clearfog-base.dtb
-load mmc 0:${rauc_part} ${kernel_addr_r} /boot/zImage
-setenv bootargs root=/dev/mmcblk0p${rauc_part} rauc.slot=${rauc_slot} czechlight=${czechlight}
-bootz ${kernel_addr_r} - ${fdt_addr_r}
diff --git a/board/czechlight/clearfog/genimage.cfg b/board/czechlight/clearfog/genimage.cfg
index 9ec9991..a0e0471 100644
--- a/board/czechlight/clearfog/genimage.cfg
+++ b/board/czechlight/clearfog/genimage.cfg
@@ -1,12 +1,3 @@
-image boot.vfat {
-	vfat {
-		files = {
-			"boot.scr",
-		}
-	}
-	size = 1M
-}
-
 image sdcard.img {
 	hdimage {
 	}
@@ -17,15 +8,10 @@
 		offset = 512
 	}
 
-	partition boot {
-		partition-type = 0xc
-		image = "boot.vfat"
-		offset = 1M
-	}
-
 	partition rootfs-A {
 		partition-type = 0x83
 		image = "rootfs.ext4"
+		offset = 1M
 	}
 
 	partition rootfs-B {
diff --git a/board/czechlight/clearfog/overlay/etc/rauc/system.conf b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
index 40b839e..8de0da8 100644
--- a/board/czechlight/clearfog/overlay/etc/rauc/system.conf
+++ b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
@@ -3,9 +3,9 @@
 bootloader=uboot
 
 [slot.rootfs.0]
-device=/dev/mmcblk0p2
+device=/dev/mmcblk0p1
 bootname=A
 
 [slot.rootfs.1]
-device=/dev/mmcblk0p3
+device=/dev/mmcblk0p2
 bootname=B
diff --git a/board/czechlight/clearfog/patches/u-boot/0003-clearfog-hard-code-the-RAUC-redundant-boot-to-uboot-.patch b/board/czechlight/clearfog/patches/u-boot/0003-clearfog-hard-code-the-RAUC-redundant-boot-to-uboot-.patch
new file mode 100644
index 0000000..8c151eb
--- /dev/null
+++ b/board/czechlight/clearfog/patches/u-boot/0003-clearfog-hard-code-the-RAUC-redundant-boot-to-uboot-.patch
@@ -0,0 +1,70 @@
+From a0bfcfc02bbcb3bdaedbe1328ae056a31b7e3d34 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= <jan.kundrat@cesnet.cz>
+Date: Wed, 11 Oct 2017 11:51:14 +0200
+Subject: [PATCH 3/3] clearfog: hard-code the RAUC redundant boot to uboot's
+ config
+
+...so that we can save one partition and an extra detour via a boot.scr
+in there.
+---
+ include/configs/clearfog.h | 40 ++++++++++++++++++++++++++++++++++++----
+ 1 file changed, 36 insertions(+), 4 deletions(-)
+
+diff --git a/include/configs/clearfog.h b/include/configs/clearfog.h
+index 8f87eca771..aaf896eb59 100644
+--- a/include/configs/clearfog.h
++++ b/include/configs/clearfog.h
+@@ -174,14 +174,46 @@
+ 	"scriptaddr=" SCRIPT_ADDR_R "\0" \
+ 	"pxefile_addr_r=" PXEFILE_ADDR_R "\0"
+ 
+-#include <config_distro_bootcmd.h>
+-
+ #define CONFIG_EXTRA_ENV_SETTINGS \
+ 	RELOCATION_LIMITS_ENV_SETTINGS \
+ 	LOAD_ADDRESS_ENV_SETTINGS \
+-	"fdtfile=" CONFIG_DEFAULT_DEVICE_TREE ".dtb\0" \
+ 	"console=ttyS0,115200\0" \
+-	BOOTENV
++	"bootcmd=test -n \"${BOOT_A_LEFT}\" || setenv BOOT_A_LEFT 3;" \
++	"test -n \"${BOOT_B_LEFT}\" || setenv BOOT_B_LEFT 3;" \
++	"setenv rauc_part;" \
++	"for BOOT_SLOT in \"A B\"; do" \
++	"  if test \"x${rauc_part}\" != \"x\"; then" \
++	"    ;" \
++	"  elif test \"x${BOOT_SLOT}\" = \"xA\"; then" \
++	"    if test ${BOOT_A_LEFT} -gt 0; then" \
++	"      setexpr BOOT_A_LEFT ${BOOT_A_LEFT} - 1;" \
++	"      echo \"Found valid slot A, ${BOOT_A_LEFT} attempts remaining\";" \
++	"      setenv rauc_part 1;" \
++	"      setenv rauc_slot A;" \
++	"    fi;" \
++	"  elif test \"x${BOOT_SLOT}\" = \"xB\"; then" \
++	"    if test ${BOOT_B_LEFT} -gt 0; then" \
++	"      setexpr BOOT_B_LEFT ${BOOT_B_LEFT} - 1;" \
++	"      echo \"Found valid slot B, ${BOOT_B_LEFT} attempts remaining\";" \
++	"      setenv rauc_part 2;" \
++	"      setenv rauc_slot B;" \
++	"    fi;" \
++	"  fi;" \
++	"done;" \
++	"if test -n \"${rauc_part}\"; then" \
++	"  saveenv ;" \
++	"else" \
++	"  echo \"No valid slot found, resetting tries to 3\";" \
++	"  setenv BOOT_A_LEFT 3;" \
++	"  setenv BOOT_B_LEFT 3;" \
++	"  saveenv;" \
++	"  reset;" \
++	"fi;" \
++	"setenv BOOT_SLOT;" \
++	"load mmc 0:${rauc_part} ${fdt_addr_r} /boot/armada-388-clearfog-base.dtb;" \
++	"load mmc 0:${rauc_part} ${kernel_addr_r} /boot/zImage;" \
++	"setenv bootargs root=/dev/mmcblk0p${rauc_part} rauc.slot=${rauc_slot} czechlight=${czechlight};" \
++	"bootz ${kernel_addr_r} - ${fdt_addr_r}\0"
+ 
+ #endif /* CONFIG_SPL_BUILD */
+ 
+-- 
+2.14.1
+
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/cla-CL-ROADMv2.service b/board/czechlight/common/overlay/usr/lib/systemd/system/cla-CL-ROADMv2.service
index c41b229..ac74aa3 100644
--- a/board/czechlight/common/overlay/usr/lib/systemd/system/cla-CL-ROADMv2.service
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/cla-CL-ROADMv2.service
@@ -1,6 +1,7 @@
 [Unit]
 Description=CzechLight CL-ROADMv2 driver
 After=syslog.target network.target sysrepod.service
+Before=rauc-mark-good.service
 Requires=sysrepod.service
 PartOf=sysrepod.service
 StartLimitIntervalSec=0
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/cla-STATIC-CL-ROADMv2.service b/board/czechlight/common/overlay/usr/lib/systemd/system/cla-STATIC-CL-ROADMv2.service
index a4eb9b0..40174cb 100644
--- a/board/czechlight/common/overlay/usr/lib/systemd/system/cla-STATIC-CL-ROADMv2.service
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/cla-STATIC-CL-ROADMv2.service
@@ -1,6 +1,7 @@
 [Unit]
 Description=CzechLight dummy static data for the CL-ROADMv2
 After=syslog.target network.target sysrepod.service
+Before=rauc-mark-good.service
 Requires=sysrepod.service
 PartOf=sysrepod.service
 StartLimitIntervalSec=0
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/multi-user.target.wants/rauc-mark-good.service b/board/czechlight/common/overlay/usr/lib/systemd/system/multi-user.target.wants/rauc-mark-good.service
new file mode 120000
index 0000000..fb70526
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/multi-user.target.wants/rauc-mark-good.service
@@ -0,0 +1 @@
+../rauc-mark-good.service
\ No newline at end of file
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/rauc-mark-good.service b/board/czechlight/common/overlay/usr/lib/systemd/system/rauc-mark-good.service
new file mode 100644
index 0000000..ae0ab6d
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/rauc-mark-good.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=RAUC Good-marking Service
+ConditionKernelCommandLine=|rauc.slot
+After=sysrepod.service netopeer2-server.service
+Requires=sysrepod.service netopeer2-server.service
+
+[Service]
+ExecStart=/usr/bin/rauc status mark-good
+
+[Install]
+WantedBy=multi-user.target
diff --git a/configs/czechlight_clearfog_defconfig b/configs/czechlight_clearfog_defconfig
index af64d3b..24c0ddb 100644
--- a/configs/czechlight_clearfog_defconfig
+++ b/configs/czechlight_clearfog_defconfig
@@ -40,15 +40,12 @@
 BR2_TARGET_UBOOT_BUILD_SYSTEM_KCONFIG=y
 BR2_TARGET_UBOOT_CUSTOM_VERSION=y
 BR2_TARGET_UBOOT_CUSTOM_VERSION_VALUE="2017.09"
-BR2_TARGET_UBOOT_PATCH="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/patches/u-boot/0001-Unconditionally-enable-watchdog-timer-on-Clearfog.patch $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/patches/u-boot/0002-Add-another-redundant-storage-for-the-environment.patch"
+BR2_TARGET_UBOOT_PATCH="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/patches/u-boot/0001-Unconditionally-enable-watchdog-timer-on-Clearfog.patch $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/patches/u-boot/0002-Add-another-redundant-storage-for-the-environment.patch $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/patches/u-boot/0003-clearfog-hard-code-the-RAUC-redundant-boot-to-uboot-.patch"
 BR2_TARGET_UBOOT_BOARD_DEFCONFIG="clearfog"
 BR2_TARGET_UBOOT_CONFIG_FRAGMENT_FILES="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/uboot.fragment"
 # BR2_TARGET_UBOOT_FORMAT_BIN is not set
 BR2_TARGET_UBOOT_FORMAT_CUSTOM=y
 BR2_TARGET_UBOOT_FORMAT_CUSTOM_NAME="u-boot-spl.kwb"
-BR2_TARGET_UBOOT_BOOT_SCRIPT=y
-BR2_TARGET_UBOOT_BOOT_SCRIPT_SOURCE="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/boot.scr.txt"
-BR2_PACKAGE_HOST_DOSFSTOOLS=y
 BR2_PACKAGE_HOST_GENIMAGE=y
 BR2_PACKAGE_HOST_RAUC=y
 CZECHLIGHT_NETCONF=y