clearfog: fancy flashing

There's no need to use emergency.target anymore, the `run usbboot` (or
its longer equivalent if we're in Marvell's factory U-Boot) will perform
the flashing automatically.

It's a bit complex because one has to wait for /dev/sda1 to appear
first. That needs udev to be running. Then one has to mount the image.
In the end I decided to go for a unit generator because it is not clear
whether the actual .device unit is just `dev-sda1.unit` or something
much more interesting like `sys-devices-platform-soc-soc:internal\x2dregs-f10f8000.usb3-usb2-2\x2d1-2\x2d1:1.0-host4-target4:0:0-4:0:0:0-block-sda-sda1.device`.

Change-Id: I1d1bc64e453c94c568cb84bc52f61dafa2a3b15d
diff --git a/README.md b/README.md
index cf9e878..7df94e9 100644
--- a/README.md
+++ b/README.md
@@ -90,10 +90,7 @@
 ```sh
 usb start; fatload usb 0:1 00800000 boot.scr; source 00800000
 ```
-
-A Linux session will start.
-Run the following from the shell prompt:
-
-```sh
-mount /dev/sda1 /mnt; sh /mnt/usb-reflash-factory.sh
-```
+The system will boot and flash the eMMC from the USB drive.
+Once the status LED starts blinking in yellow, data are being transferred to the eMMC.
+The light changes to solid yellow in later phases of the flashing process.
+Once everything is done, the status LED shows a solid white light and the system reboots automatically.
diff --git a/board/czechlight/clearfog/usb-boot.scr.txt b/board/czechlight/clearfog/usb-boot.scr.txt
index 0227a74..c3ca6e8 100644
--- a/board/czechlight/clearfog/usb-boot.scr.txt
+++ b/board/czechlight/clearfog/usb-boot.scr.txt
@@ -1,4 +1,4 @@
-setenv bootargs systemd.unit=emergency.target
+setenv bootargs systemd.unit=usb-flash.service
 fatload usb 0:1 ${kernel_addr_r} zImage
 fatload usb 0:1 ${fdt_addr_r} czechlight-clearfog.dtb
 fatload usb 0:1 ${ramdisk_addr_r} rootfs.cpio.uboot
diff --git a/package/czechlight-rauc/czechlight-rauc.mk b/package/czechlight-rauc/czechlight-rauc.mk
index 7c9c4cb..08a5b3b 100644
--- a/package/czechlight-rauc/czechlight-rauc.mk
+++ b/package/czechlight-rauc/czechlight-rauc.mk
@@ -77,6 +77,15 @@
 	$(INSTALL) -D -m 0644 $(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-rauc/enable-hw-watchdog.service \
 		$(TARGET_DIR)/usr/lib/systemd/system/
 	ln -sf ../enable-hw-watchdog.service $(TARGET_DIR)/usr/lib/systemd/system/multi-user.target.wants/
+
+	# Just for USB flashing
+	mkdir -p $(TARGET_DIR)/usr/lib/systemd/system-generators/
+	$(INSTALL) -D -m 0755 \
+		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-rauc/czechlight-usb-flash-mount-generator \
+		$(TARGET_DIR)/usr/lib/systemd/system-generators/
+	$(INSTALL) -D -m 0644 \
+		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-rauc/usb-flash.service \
+		$(TARGET_DIR)/usr/lib/systemd/system/
 endef
 
 $(eval $(generic-package))
diff --git a/package/czechlight-rauc/czechlight-usb-flash-mount-generator b/package/czechlight-rauc/czechlight-usb-flash-mount-generator
new file mode 100644
index 0000000..67cdf10
--- /dev/null
+++ b/package/czechlight-rauc/czechlight-usb-flash-mount-generator
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+grep -q systemd.unit=usb-flash.service /proc/cmdline || exit 0
+
+cat > $1/mnt.mount <<EOF
+[Unit]
+Description=Mount USB flash at /mnt
+DefaultDependencies=no
+Conflicts=umount.target
+Before=local-fs.target umount.target
+Requires=systemd-udevd.service
+
+[Mount]
+What=/dev/sda1
+Where=/mnt
+Type=auto
+Options=relatime,nosuid,nodev
+EOF
diff --git a/package/czechlight-rauc/usb-flash.service b/package/czechlight-rauc/usb-flash.service
new file mode 100644
index 0000000..59c1b79
--- /dev/null
+++ b/package/czechlight-rauc/usb-flash.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Reflash from USB
+DefaultDependencies=no
+Requires=mnt.mount
+After=mnt.mount
+
+[Service]
+Type=oneshot
+StandardInput=tty
+StandardOutput=tty
+StandardError=inherit
+ExecStart=/mnt/usb-reflash-factory.sh