Refresh the BeagleBone Black image

Barebox won't build (it complains about <linux/list.h> not found and I
cannot easily solve that), so let's go back to U-Boot.

The 2019.01 U-Boot release is buggy in its recognizing of partition
tables (`fatls` complains, for example), and the whole thing reverts to
a "legacy boot", so let's use the latest release, the 2019.07.

I'm also giving up on the g_cdc module because the board that I'm using
has a microUSB header for the debug console.

- FIXME: Properly distinguish between an SD-card boot and an eMMC boot
(right now it is hardcoded to boot from the SD card)
- FIXME: test Rauc updates once eMMC works
- FIXME: activate watchdog from U-Boot

Change-Id: I013f145cf2fe24e41ea0dafd225e3feb1854712c
diff --git a/board/czechlight/beaglebone/barebox.env/boot/sd b/board/czechlight/beaglebone/barebox.env/boot/sd
deleted file mode 100644
index 2aa7912..0000000
--- a/board/czechlight/beaglebone/barebox.env/boot/sd
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-
-global.bootm.image=/boot/zImage
-
-global.bootm.oftree=/boot/czechlight-bbb.dtb
-
-# No initrd
-# global.bootm.initrd=<path to initrd>
-
-global.linux.bootargs.dyn.root="root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
-global.linux.bootargs.base="omap_wdt.early_enable=1"
diff --git a/board/czechlight/beaglebone/barebox.env/config-board b/board/czechlight/beaglebone/barebox.env/config-board
deleted file mode 100644
index 042ed45..0000000
--- a/board/czechlight/beaglebone/barebox.env/config-board
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-global.boot.default=sd
-global.boot.watchdog_timeout=60
diff --git a/board/czechlight/beaglebone/boot.scr.txt b/board/czechlight/beaglebone/boot.scr.txt
new file mode 100644
index 0000000..9a8adc7
--- /dev/null
+++ b/board/czechlight/beaglebone/boot.scr.txt
@@ -0,0 +1,8 @@
+setenv bootargs root=/dev/mmcblk0p${rauc_part} console=ttyO0,115200n8 init=/sbin/init-czechlight.sh rauc.slot=${rauc_slot} czechlight=${czechlight} systemd.machine_id=${machineid} panic=10 oops=panic
+if test -n ${czechlight} && load mmc 0:${rauc_part} ${fdt_addr_r} /boot/${czechlight}-bbb.dtb; then
+  echo "DTS: ${czechlight}-bbb"
+else
+  load mmc 0:${rauc_part} ${fdt_addr_r} /boot/czechlight-bbb.dtb || reset
+fi
+load mmc 0:${rauc_part} ${kernel_addr_r} /boot/zImage || reset
+bootz ${kernel_addr_r} - ${fdt_addr_r} || reset
diff --git a/board/czechlight/beaglebone/create-uEnv.sh b/board/czechlight/beaglebone/create-uEnv.sh
new file mode 100755
index 0000000..9808fdd
--- /dev/null
+++ b/board/czechlight/beaglebone/create-uEnv.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+install -m 0644 -D $BINARIES_DIR/boot.scr $TARGET_DIR/boot/boot.scr
+
+# prepare the static script with RAUC logic
+${HOST_DIR}/bin/mkimage -C none -A arm -T script \
+        -d ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/beaglebone/rauc.scr.txt ${BINARIES_DIR}/rauc.scr
+
+# ...and a simplified one for SD-card booting (no image determination)
+${HOST_DIR}/bin/mkimage -C none -A arm -T script \
+        -d ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/beaglebone/sd.scr.txt ${BINARIES_DIR}/sd.scr
diff --git a/board/czechlight/beaglebone/genimage.cfg b/board/czechlight/beaglebone/genimage.cfg
index a1cb8c9..dc9f7c1 100644
--- a/board/czechlight/beaglebone/genimage.cfg
+++ b/board/czechlight/beaglebone/genimage.cfg
@@ -1,33 +1,82 @@
-image boot.vfat {
+image boot-sd.vfat {
 	vfat {
-		file MLO {
-			image = "barebox-am33xx-beaglebone-mlo.img"
-		}
-		file barebox.bin {
-			image = "barebox-am33xx-beaglebone.img"
-		}
 		files = {
-			"barebox.env",
-			"czechlight-bbb.dtb",
-			"zImage"
+			"MLO",
+			"u-boot.img"
+		}
+		file boot.scr {
+			image = "sd.scr"
 		}
 	}
-	size = 32M
+	size = 8M
 }
 
 image sdcard.img {
 	hdimage {
 	}
 
-	partition boot {
+	partition u-boot {
 		partition-type = 0xC
-		in-partition-table = "yes"
 		bootable = "true"
-		image = "boot.vfat"
+		image = "boot-sd.vfat"
 	}
 
 	partition rootfs {
 		partition-type = 0x83
 		image = "rootfs.ext4"
 	}
+
+	partition cfg {
+		partition-type = 0x83
+		image = "cfg.ext4"
+	}
 }
+
+# this is largely untested...
+#image boot-emmc.vfat {
+#	vfat {
+#		files = {
+#			"MLO",
+#			"u-boot.img"
+#		}
+#		file boot.scr {
+#			image = "rauc.scr"
+#		}
+#	}
+#	size = 8M
+#}
+#
+#image emmc.img {
+#	hdimage {
+#	}
+#
+#	partition u-boot {
+#		partition-type = 0xC
+#		bootable = "true"
+#		image = "boot-emmc.vfat"
+#	}
+#
+#	partition rootfs-A {
+#		partition-type = 0x83
+#		image = "rootfs.ext4"
+#		size = 1024M
+#	}
+#
+#	partition cfg-A {
+#		partition-type = 0x83
+#		image = "cfg.ext4"
+#		size = 512M
+#	}
+#
+#	partition rootfs-B {
+#		partition-type = 0x83
+#		image = "rootfs.ext4"
+#		size = 1024M
+#	}
+#
+#	partition cfg-B {
+#		partition-type = 0x83
+#		image = "cfg.ext4"
+#		size = 512M
+#	}
+#}
diff --git a/board/czechlight/beaglebone/linux.fragment b/board/czechlight/beaglebone/linux.fragment
index 5dcd313..2871883 100644
--- a/board/czechlight/beaglebone/linux.fragment
+++ b/board/czechlight/beaglebone/linux.fragment
@@ -1,5 +1 @@
 CONFIG_OMAP_WATCHDOG=y
-
-# CONFIG_USB_ZERO is not set
-CONFIG_USB_CDC_COMPOSITE=m
-# CONFIG_USB_G_NOKIA is not set
diff --git a/board/czechlight/beaglebone/overlay/etc/modules-load.d/usb-serial-gadget.conf b/board/czechlight/beaglebone/overlay/etc/modules-load.d/usb-serial-gadget.conf
deleted file mode 100644
index 41c481d..0000000
--- a/board/czechlight/beaglebone/overlay/etc/modules-load.d/usb-serial-gadget.conf
+++ /dev/null
@@ -1,2 +0,0 @@
-# composite module for serial console and Ethernet over the miniUSB port
-g_cdc
diff --git a/board/czechlight/beaglebone/overlay/etc/systemd/system/network/30-usbnet-apipa.network b/board/czechlight/beaglebone/overlay/etc/systemd/system/network/30-usbnet-apipa.network
deleted file mode 100644
index 807dcc4..0000000
--- a/board/czechlight/beaglebone/overlay/etc/systemd/system/network/30-usbnet-apipa.network
+++ /dev/null
@@ -1,6 +0,0 @@
-[Match]
-Name=usb0
-
-[Network]
-Description=miniUSB console
-LinkLocalAddressing=yes
diff --git a/board/czechlight/beaglebone/post-image.sh b/board/czechlight/beaglebone/post-image.sh
deleted file mode 100755
index 149ffb1..0000000
--- a/board/czechlight/beaglebone/post-image.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-
-BOARD_DIR="$(dirname $0)"
-GENIMAGE_CFG="${BOARD_DIR}/genimage.cfg"
-GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp"
-
-rm -rf "${GENIMAGE_TMP}"
-
-genimage \
-	--rootpath "${TARGET_DIR}" \
-	--tmppath "${GENIMAGE_TMP}" \
-	--inputpath "${BINARIES_DIR}" \
-	--outputpath "${BINARIES_DIR}" \
-	--config "${GENIMAGE_CFG}"
-
-exit $?
-
diff --git a/board/czechlight/beaglebone/rauc.scr.txt b/board/czechlight/beaglebone/rauc.scr.txt
new file mode 100644
index 0000000..8e5ba65
--- /dev/null
+++ b/board/czechlight/beaglebone/rauc.scr.txt
@@ -0,0 +1,35 @@
+echo CzechLight BeagleBone Black RAUC
+
+test -n "${BOOT_A_LEFT}" || setenv BOOT_A_LEFT 3;
+test -n "${BOOT_B_LEFT}" || setenv BOOT_B_LEFT 3;
+test -n "${BOOT_ORDER}" || setenv BOOT_ORDER "A B";
+for BOOT_SLOT in "${BOOT_ORDER}"; 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";
+      rauc_part=2;
+      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";
+      rauc_part=4;
+      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} ${scriptaddr} /boot/boot.scr || reset;
+source ${scriptaddr} || reset;
diff --git a/board/czechlight/beaglebone/sd.scr.txt b/board/czechlight/beaglebone/sd.scr.txt
new file mode 100644
index 0000000..9d02237
--- /dev/null
+++ b/board/czechlight/beaglebone/sd.scr.txt
@@ -0,0 +1,4 @@
+rauc_part=2
+rauc_slot=A
+load mmc 0:${rauc_part} ${scriptaddr} /boot/boot.scr || reset
+source ${scriptaddr} || reset
diff --git a/board/czechlight/beaglebone/uboot.fragment b/board/czechlight/beaglebone/uboot.fragment
new file mode 100644
index 0000000..d231b5d
--- /dev/null
+++ b/board/czechlight/beaglebone/uboot.fragment
@@ -0,0 +1 @@
+CONFIG_CMD_SETEXPR=y