bbb: use RAUC eMMC layout

This, unfortunately, means that the system will no longer boot from the
SD card:

- U-Boot is set to store its env in the p1 VFAT partition
- rootfs' /boot is something else (kernel, etc) than what gets mounted
on system's /boot
- rootfs' /boot/boot.scr contains device numbers which refer to the eMMC
block device, not to the SD card

I think it might be possible to add some workarounds, such as exporting
the number of an "active MMC device" to an U-Boot variable or something,
but hey, let's give up. The only thing that I need is *some* method of
flashing content to the device, and any distro is good enough for that.

Kill services which are no longer useful, such as Avahi (originally used
for MDNS), and the tty-over-USB which only holds up the build.

Also sync the config with clearfog.

Change-Id: Ia966448a05ed5ce52747f5e7169136232f5502c2
diff --git a/README.md b/README.md
index 81ac4eb..546059d 100644
--- a/README.md
+++ b/README.md
@@ -101,3 +101,12 @@
 # rauc install http://somewhere.example.org/update.raucb
 # reboot
 ```
+
+#### Beaglebone Black
+
+Obtain a reasonable Linux distro image for BBB and flash it to a µSD card.
+Unlock eMMC boot partitions (`echo 0 > /sys/class/block/mmcblk1boot0/force_ro; echo 0 > /sys/class/block/mmcblk1boot1/force_ro`).
+Clean the eMMC data (`blkdiscard /dev/mmcblk1`).
+Flash the content of `images/emmc.img` to device's `/dev/mmcblk1`.
+Flash what fits into `/dev/mmcblk1boot0` and `/dev/mmcblk1boot1`.
+Fetching the image over web (`python3 -m http.server` and `wget http://...:8000/emmc.img -O - | dd of=/dev/mmcblk1 conv=sparse`) works well.
diff --git a/board/czechlight/beaglebone/boot.scr.txt b/board/czechlight/beaglebone/boot.scr.txt
index 9a8adc7..4d21cf6 100644
--- a/board/czechlight/beaglebone/boot.scr.txt
+++ b/board/czechlight/beaglebone/boot.scr.txt
@@ -1,8 +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
+setenv bootargs root=/dev/mmcblk1p${rauc_part} console=ttyS0,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 1:${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
+  load mmc 1:${rauc_part} ${fdt_addr_r} /boot/czechlight-bbb.dtb || reset
 fi
-load mmc 0:${rauc_part} ${kernel_addr_r} /boot/zImage || reset
+load mmc 1:${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
index 3e0c513..83764f5 100755
--- a/board/czechlight/beaglebone/create-uEnv.sh
+++ b/board/czechlight/beaglebone/create-uEnv.sh
@@ -2,12 +2,6 @@
 
 install -m 0644 -D $BINARIES_DIR/boot.scr $TARGET_DIR/boot/boot.scr
 
-cp ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/beaglebone/sd-uEnv.txt ${BINARIES_DIR}/
-
 # 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 8f92ca1..bcbd27f 100644
--- a/board/czechlight/beaglebone/genimage.cfg
+++ b/board/czechlight/beaglebone/genimage.cfg
@@ -1,85 +1,45 @@
-image boot-sd.vfat {
+image boot-emmc.vfat {
 	vfat {
 		files = {
 			"MLO",
-			"u-boot.img"
-		}
-		file boot.scr {
-			image = "sd.scr"
-		}
-		file uEnv.txt {
-			image = "sd-uEnv.txt"
+			"u-boot.img",
+			"rauc.scr"
 		}
 	}
 	size = 8M
 }
 
-image sdcard.img {
+image emmc.img {
 	hdimage {
 	}
 
 	partition u-boot {
 		partition-type = 0xC
 		bootable = "true"
-		image = "boot-sd.vfat"
+		image = "boot-emmc.vfat"
 	}
 
-	partition rootfs {
+	partition rootfs-A {
 		partition-type = 0x83
 		image = "rootfs.ext4"
+		size = 1024M
 	}
 
-	partition cfg {
+	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
 	}
 }
-
-# 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/overlay/etc/fw_env.config b/board/czechlight/beaglebone/overlay/etc/fw_env.config
new file mode 100644
index 0000000..4521bf9
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/etc/fw_env.config
@@ -0,0 +1 @@
+/boot/uboot.env	0x0000          0x20000
diff --git a/board/czechlight/beaglebone/overlay/etc/systemd/system/getty.target.wants/serial-getty@ttyGS0.service b/board/czechlight/beaglebone/overlay/etc/systemd/system/getty.target.wants/serial-getty@ttyGS0.service
deleted file mode 120000
index 51e45a4..0000000
--- a/board/czechlight/beaglebone/overlay/etc/systemd/system/getty.target.wants/serial-getty@ttyGS0.service
+++ /dev/null
@@ -1 +0,0 @@
-../../../../lib/systemd/system/serial-getty@.service
\ No newline at end of file
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/boot.mount b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/boot.mount
new file mode 100644
index 0000000..210a26d
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/boot.mount
@@ -0,0 +1,11 @@
+[Unit]
+Description=U-Boot (/boot)
+DefaultDependencies=no
+Conflicts=umount.target
+Before=local-fs.target umount.target
+
+[Mount]
+What=/dev/mmcblk1p1
+Where=/boot
+Type=vfat
+Options=noauto,nodev
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/local-fs.target.wants/boot.mount b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/local-fs.target.wants/boot.mount
new file mode 120000
index 0000000..6308030
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/local-fs.target.wants/boot.mount
@@ -0,0 +1 @@
+../boot.mount
\ No newline at end of file
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/save-machineid-to-uboot.service b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/save-machineid-to-uboot.service
new file mode 120000
index 0000000..b60a4f9
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/save-machineid-to-uboot.service
@@ -0,0 +1 @@
+../save-machineid-to-uboot.service
\ No newline at end of file
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/systemd-journal-upload.service b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/systemd-journal-upload.service
new file mode 120000
index 0000000..f70515b
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/multi-user.target.wants/systemd-journal-upload.service
@@ -0,0 +1 @@
+../systemd-journal-upload.service
\ No newline at end of file
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/save-machineid-to-uboot.service b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/save-machineid-to-uboot.service
new file mode 100644
index 0000000..892411b
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/save-machineid-to-uboot.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Save the machine-id into U-Boot environment if not set
+
+[Service]
+Type=oneshot
+ExecStart=/bin/sh -c 'fw_printenv machineid &>/dev/null || fw_setenv machineid $(cat /etc/machine-id)'
diff --git a/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/systemd-journal-upload.service b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/systemd-journal-upload.service
new file mode 100644
index 0000000..f173525
--- /dev/null
+++ b/board/czechlight/beaglebone/overlay/usr/lib/systemd/system/systemd-journal-upload.service
@@ -0,0 +1,43 @@
+# CzechLight-specific configuration:
+#
+# - do not record a failed unit when that thing dies (likely due to a network issue)
+# - only start when a custom config file exists
+# - do not bring in a network-online.target because that might trigger
+#   extra failure reports (and a watchdog action)
+# - keep retrying upon failure(s)
+
+[Unit]
+Description=Journal Remote Upload Service
+
+[Service]
+DynamicUser=yes
+LockPersonality=yes
+MemoryDenyWriteExecute=yes
+PrivateDevices=yes
+ProtectControlGroups=yes
+ProtectHome=yes
+ProtectHostname=yes
+ProtectKernelModules=yes
+ProtectKernelTunables=yes
+RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
+RestrictNamespaces=yes
+RestrictRealtime=yes
+StateDirectory=systemd/journal-upload
+SupplementaryGroups=systemd-journal
+SystemCallArchitectures=native
+User=systemd-journal-upload
+WatchdogSec=3min
+LimitNOFILE=524288
+
+# - ignore failures
+# - read (one) location from the env file
+ExecStart=-/usr/lib/systemd/systemd-journal-upload --save-state --url=${DESTINATION}
+EnvironmentFile=-/cfg/journald-remote
+
+# run forever and ignore any network issues
+Restart=always
+RestartSec=5
+
+[Unit]
+# shared as an env file
+ConditionFileNotEmpty=/cfg/journald-remote
diff --git a/board/czechlight/beaglebone/rauc.scr.txt b/board/czechlight/beaglebone/rauc.scr.txt
index 8e5ba65..f2e1117 100644
--- a/board/czechlight/beaglebone/rauc.scr.txt
+++ b/board/czechlight/beaglebone/rauc.scr.txt
@@ -17,7 +17,7 @@
     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_part=5;
       rauc_slot=B;
     fi;
   fi;
@@ -31,5 +31,5 @@
   saveenv;
   reset;
 fi;
-load mmc 0:${rauc_part} ${scriptaddr} /boot/boot.scr || reset;
+load mmc 1:${rauc_part} ${scriptaddr} /boot/boot.scr || reset;
 source ${scriptaddr} || reset;
diff --git a/board/czechlight/beaglebone/sd-uEnv.txt b/board/czechlight/beaglebone/sd-uEnv.txt
deleted file mode 100644
index 8496b70..0000000
--- a/board/czechlight/beaglebone/sd-uEnv.txt
+++ /dev/null
@@ -1 +0,0 @@
-uenvcmd=setenv scriptaddr 0x80000000; load mmc 0:1 ${scriptaddr} boot.scr && source ${scriptaddr}
diff --git a/board/czechlight/beaglebone/sd.scr.txt b/board/czechlight/beaglebone/sd.scr.txt
deleted file mode 100644
index 20cb695..0000000
--- a/board/czechlight/beaglebone/sd.scr.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-# TODO: kill these
-czechlight=calibration
-machineid=567bc66a285241ea94f63d6ece9d00da
-
-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
index d231b5d..62c796c 100644
--- a/board/czechlight/beaglebone/uboot.fragment
+++ b/board/czechlight/beaglebone/uboot.fragment
@@ -1 +1,3 @@
 CONFIG_CMD_SETEXPR=y
+CONFIG_ENV_FAT_DEVICE_AND_PART="1:1"
+CONFIG_BOOTCOMMAND="fatload mmc 1:1 ${scriptaddr} /rauc.scr || reset; source ${scriptaddr} || reset"
diff --git a/configs/czechlight_beaglebone_defconfig b/configs/czechlight_beaglebone_defconfig
index d893b57..51549c0 100644
--- a/configs/czechlight_beaglebone_defconfig
+++ b/configs/czechlight_beaglebone_defconfig
@@ -13,7 +13,7 @@
 BR2_TARGET_GENERIC_HOSTNAME="czechlight"
 BR2_TARGET_GENERIC_ISSUE="Welcome to Czech Light"
 BR2_INIT_SYSTEMD=y
-BR2_TARGET_GENERIC_GETTY_PORT="ttyO0"
+BR2_TARGET_GENERIC_GETTY_PORT="ttyS0"
 # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
 BR2_ROOTFS_USERS_TABLES="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/users"
 BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/overlay/ $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/overlay/"
@@ -44,8 +44,6 @@
 BR2_PACKAGE_LIBSSH_SERVER=y
 BR2_PACKAGE_LIBGPIOD_TOOLS=y
 BR2_PACKAGE_MSTPD=y
-BR2_PACKAGE_AVAHI=y
-BR2_PACKAGE_AVAHI_DAEMON=y
 BR2_PACKAGE_ETHTOOL=y
 BR2_PACKAGE_IPROUTE2=y
 BR2_PACKAGE_NETCONF_CLI=y
@@ -56,6 +54,8 @@
 BR2_PACKAGE_DDRESCUE=y
 BR2_PACKAGE_RAUC=y
 BR2_PACKAGE_RAUC_NETWORK=y
+BR2_PACKAGE_SYSTEMD_JOURNAL_GATEWAY=y
+BR2_PACKAGE_SYSTEMD_JOURNAL_REMOTE=y
 # BR2_PACKAGE_SYSTEMD_HWDB is not set
 BR2_PACKAGE_SYSTEMD_RANDOMSEED=y
 # BR2_PACKAGE_SYSTEMD_VCONSOLE is not set
@@ -66,12 +66,12 @@
 BR2_TARGET_ROOTFS_CPIO_UIMAGE=y
 BR2_TARGET_ROOTFS_EXT2=y
 BR2_TARGET_ROOTFS_EXT2_4=y
-BR2_TARGET_ROOTFS_EXT2_SIZE="333M"
+BR2_TARGET_ROOTFS_EXT2_SIZE="366M"
 BR2_TARGET_ROOTFS_TAR_XZ=y
 BR2_TARGET_UBOOT=y
 BR2_TARGET_UBOOT_BUILD_SYSTEM_KCONFIG=y
 BR2_TARGET_UBOOT_CUSTOM_VERSION=y
-BR2_TARGET_UBOOT_CUSTOM_VERSION_VALUE="2019.07"
+BR2_TARGET_UBOOT_CUSTOM_VERSION_VALUE="2020.01"
 BR2_TARGET_UBOOT_BOARD_DEFCONFIG="am335x_evm"
 BR2_TARGET_UBOOT_CONFIG_FRAGMENT_FILES="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/uboot.fragment"
 BR2_TARGET_UBOOT_NEEDS_DTC=y
@@ -91,8 +91,8 @@
 CZECHLIGHT_CFG_FS_PERSIST_KEYS=y
 CZECHLIGHT_RAUC_ROOTFS=y
 CZECHLIGHT_RAUC_COMPATIBLE="beaglebone-black"
-CZECHLIGHT_RAUC_SLOT_A_ROOTFS_DEV="/dev/mmcblk0p2"
-CZECHLIGHT_RAUC_SLOT_A_CFG_DEV="/dev/mmcblk0p3"
-CZECHLIGHT_RAUC_SLOT_B_ROOTFS_DEV="/dev/mmcblk0p4"
-CZECHLIGHT_RAUC_SLOT_B_CFG_DEV="/dev/mmcblk0p5"
+CZECHLIGHT_RAUC_SLOT_A_ROOTFS_DEV="/dev/mmcblk1p2"
+CZECHLIGHT_RAUC_SLOT_A_CFG_DEV="/dev/mmcblk1p3"
+CZECHLIGHT_RAUC_SLOT_B_ROOTFS_DEV="/dev/mmcblk1p5"
+CZECHLIGHT_RAUC_SLOT_B_CFG_DEV="/dev/mmcblk1p6"
 CZECHLIGHT_NETCONF=y