WIP: Integrate RAUC and U-Boot

Change-Id: I699e72e3229953af62393b9a1fa199e6767aba0b
diff --git a/board/czechlight/clearfog/boot.scr.txt b/board/czechlight/clearfog/boot.scr.txt
index a2b0580..99d980e 100644
--- a/board/czechlight/clearfog/boot.scr.txt
+++ b/board/czechlight/clearfog/boot.scr.txt
@@ -1,5 +1,39 @@
-setenv fdtfile armada-388-clearfog-base.dtb
-setenv bootargs root=/dev/mmcblk0p1 czechlight=${czechlight}
-load ${devtype} ${devnum}:${distro_bootpart} ${fdt_addr_r} ${prefix}/${fdtfile}
-load ${devtype} ${devnum}:${distro_bootpart} ${kernel_addr_r} ${prefix}/zImage
+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 8963619..9ec9991 100644
--- a/board/czechlight/clearfog/genimage.cfg
+++ b/board/czechlight/clearfog/genimage.cfg
@@ -1,3 +1,12 @@
+image boot.vfat {
+	vfat {
+		files = {
+			"boot.scr",
+		}
+	}
+	size = 1M
+}
+
 image sdcard.img {
 	hdimage {
 	}
@@ -8,9 +17,19 @@
 		offset = 512
 	}
 
-	partition rootfs {
+	partition boot {
+		partition-type = 0xc
+		image = "boot.vfat"
+		offset = 1M
+	}
+
+	partition rootfs-A {
 		partition-type = 0x83
 		image = "rootfs.ext4"
-		offset = 1M
+	}
+
+	partition rootfs-B {
+		partition-type = 0x83
+		image = "rootfs.ext4"
 	}
 }
diff --git a/board/czechlight/clearfog/overlay/etc/fw_env.config b/board/czechlight/clearfog/overlay/etc/fw_env.config
new file mode 100644
index 0000000..fbbc548
--- /dev/null
+++ b/board/czechlight/clearfog/overlay/etc/fw_env.config
@@ -0,0 +1,2 @@
+/dev/mmcblk0 0xf0000 0x10000 0x200
+/dev/mmcblk0 0xe0000 0x10000 0x200
diff --git a/board/czechlight/clearfog/overlay/etc/rauc/system.conf b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
new file mode 100644
index 0000000..40b839e
--- /dev/null
+++ b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
@@ -0,0 +1,11 @@
+[system]
+compatible=czechlight-clearfog
+bootloader=uboot
+
+[slot.rootfs.0]
+device=/dev/mmcblk0p2
+bootname=A
+
+[slot.rootfs.1]
+device=/dev/mmcblk0p3
+bootname=B
diff --git a/board/czechlight/clearfog/patches/u-boot/0002-Add-another-redundant-storage-for-the-environment.patch b/board/czechlight/clearfog/patches/u-boot/0002-Add-another-redundant-storage-for-the-environment.patch
new file mode 100644
index 0000000..da15be1
--- /dev/null
+++ b/board/czechlight/clearfog/patches/u-boot/0002-Add-another-redundant-storage-for-the-environment.patch
@@ -0,0 +1,26 @@
+From 3631fab797c4991e04fb8bf984ca53d00aec520f Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= <jan.kundrat@cesnet.cz>
+Date: Tue, 10 Oct 2017 20:49:41 +0200
+Subject: [PATCH 2/2] Add another, redundant storage for the environment
+
+---
+ include/configs/clearfog.h | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/include/configs/clearfog.h b/include/configs/clearfog.h
+index eac1e8bbce..8f87eca771 100644
+--- a/include/configs/clearfog.h
++++ b/include/configs/clearfog.h
+@@ -70,6 +70,9 @@
+ #define CONFIG_ENV_OFFSET		0xf0000
+ #define CONFIG_ENV_ADDR			CONFIG_ENV_OFFSET
+ 
++#define CONFIG_ENV_OFFSET_REDUND	0xe0000
++#define CONFIG_SYS_REDUNDAND_ENVIRONMENT
++
+ #define CONFIG_PHY_MARVELL		/* there is a marvell phy */
+ #define PHY_ANEG_TIMEOUT	8000	/* PHY needs a longer aneg time */
+ 
+-- 
+2.14.1
+
diff --git a/board/czechlight/clearfog/post-build.sh b/board/czechlight/clearfog/post-build.sh
deleted file mode 100755
index ff22ebd..0000000
--- a/board/czechlight/clearfog/post-build.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-
-install -m 0644 -D $BINARIES_DIR/boot.scr $TARGET_DIR/boot/boot.scr
diff --git a/board/czechlight/clearfog/uboot.fragment b/board/czechlight/clearfog/uboot.fragment
new file mode 100644
index 0000000..d231b5d
--- /dev/null
+++ b/board/czechlight/clearfog/uboot.fragment
@@ -0,0 +1 @@
+CONFIG_CMD_SETEXPR=y