clearfog: Implement USB booting

Here's how to load this from a possible bricked device:

1) Set the boot switches to 01001 (yes, it's different from what the doc
says)
2) Load the bootloader via UART, e.g.:
  `./tools/kwboot -b ./u-boot-spl.kwb -t -p /dev/ttyUSB0`
3) Interrupt U-Boot's autoboot to get a prompt
4) Plug a USB flash with images/usb-flash.img loaded
5) One on the U-Boot prompt, execute:
  `usb start; fatload usb 0:1 00800000 usb-boot.scr; source 00800000`
6) Get a nice emergency shell

Change-Id: Icef4badc9105bbed78ad949c060c0a46e9ff7f76
diff --git a/board/czechlight/clearfog/copy-boot-scr.sh b/board/czechlight/clearfog/copy-boot-scr.sh
index ff22ebd..88508a4 100755
--- a/board/czechlight/clearfog/copy-boot-scr.sh
+++ b/board/czechlight/clearfog/copy-boot-scr.sh
@@ -1,3 +1,7 @@
 #!/bin/sh
 
 install -m 0644 -D $BINARIES_DIR/boot.scr $TARGET_DIR/boot/boot.scr
+
+# Prepare a boot script for USB booting as well
+${HOST_DIR}/bin/mkimage -C none -A arm -T script \
+	-d ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/clearfog/usb-boot.scr.txt ${BINARIES_DIR}/usb-boot.scr
diff --git a/board/czechlight/clearfog/genimage.cfg b/board/czechlight/clearfog/genimage.cfg
index 7633c8f..01866bb 100644
--- a/board/czechlight/clearfog/genimage.cfg
+++ b/board/czechlight/clearfog/genimage.cfg
@@ -36,3 +36,25 @@
 		size = 2000M
 	}
 }
+
+image usb-flash.vfat {
+	vfat {
+		files = {
+			"usb-boot.scr",
+			"zImage",
+			"czechlight-clearfog.dtb",
+			"rootfs.cpio.uboot"
+		}
+	}
+	size = 64M
+}
+
+image usb-flash.img {
+	hdimage {
+	}
+
+	partition boot {
+		partition-type = 0xc
+		image = "usb-flash.vfat"
+	}
+}
diff --git a/board/czechlight/clearfog/patches/u-boot/boot.patch b/board/czechlight/clearfog/patches/u-boot/boot.patch
index 1c6032c..44c1c3e 100644
--- a/board/czechlight/clearfog/patches/u-boot/boot.patch
+++ b/board/czechlight/clearfog/patches/u-boot/boot.patch
@@ -88,7 +88,7 @@
 +CONFIG_WDT=y
 +CONFIG_WDT_ORION=y
 diff --git a/include/configs/clearfog.h b/include/configs/clearfog.h
-index bf87bac300..4c5ac5b075 100644
+index bf87bac300..75926b00ed 100644
 --- a/include/configs/clearfog.h
 +++ b/include/configs/clearfog.h
 @@ -32,6 +32,11 @@
@@ -113,7 +113,7 @@
  #define CONFIG_PHY_MARVELL		/* there is a marvell phy */
  #define PHY_ANEG_TIMEOUT	8000	/* PHY needs a longer aneg time */
  
-@@ -161,17 +169,47 @@
+@@ -161,17 +169,48 @@
  	"kernel_addr_r=" KERNEL_ADDR_R "\0" \
  	"fdt_addr_r=" FDT_ADDR_R "\0" \
  	"ramdisk_addr_r=" RAMDISK_ADDR_R "\0" \
@@ -129,6 +129,7 @@
 -	"fdtfile=" CONFIG_DEFAULT_DEVICE_TREE ".dtb\0" \
  	"console=ttyS0,115200\0" \
 -	BOOTENV
++	"usbboot=usb start; fatload usb 0:1 ${script_addr_r} usb-boot.scr; source ${script_addr_r}\0" \
 +	"bootcmd=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\";" \
diff --git a/board/czechlight/clearfog/usb-boot.scr.txt b/board/czechlight/clearfog/usb-boot.scr.txt
new file mode 100644
index 0000000..0227a74
--- /dev/null
+++ b/board/czechlight/clearfog/usb-boot.scr.txt
@@ -0,0 +1,5 @@
+setenv bootargs systemd.unit=emergency.target
+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
+bootz ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
diff --git a/configs/czechlight_clearfog_defconfig b/configs/czechlight_clearfog_defconfig
index e6a5a7e..a09bf97 100644
--- a/configs/czechlight_clearfog_defconfig
+++ b/configs/czechlight_clearfog_defconfig
@@ -40,6 +40,9 @@
 # BR2_PACKAGE_SYSTEMD_HWDB is not set
 BR2_PACKAGE_VIM=y
 # BR2_PACKAGE_VIM_RUNTIME is not set
+BR2_TARGET_ROOTFS_CPIO=y
+BR2_TARGET_ROOTFS_CPIO_GZIP=y
+BR2_TARGET_ROOTFS_CPIO_UIMAGE=y
 BR2_TARGET_ROOTFS_EXT2=y
 BR2_TARGET_ROOTFS_EXT2_4=y
 BR2_TARGET_ROOTFS_EXT2_SIZE="256M"
@@ -59,6 +62,7 @@
 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
 BR2_PACKAGE_HOST_SQUASHFS=y