Multiple architecture support in Enterprise BSP

- flash.sh now understands USERSPACE_ARCH
- Move the sdcard mounting/packing into a shell script, capturing
losetup output in Makefile was being flaky for unclear reasons, and
feels really hacky. Also remove the sdcard-allocate target in favor of
allocating in the sdcard.img rule

Change-Id: Ia320c6390e960dd07e377599774afc85ed709143
diff --git a/bootloader.mk b/bootloader.mk
index a1f1f27..10f4fe1 100644
--- a/bootloader.mk
+++ b/bootloader.mk
@@ -22,9 +22,9 @@
 
 $(PRODUCT_OUT)/u-boot.imx: uboot-imx | out-dirs
 	$(LOG) u-boot extract
-	find $(PRODUCT_OUT)/packages -name 'uboot-imx*.deb' | xargs \
+	find $(PRODUCT_OUT)/packages -name 'uboot-imx*$(USERSPACE_ARCH)*.deb' | xargs \
 	dpkg --fsys-tarfile | \
 	tar --strip-components 2 -C $(PRODUCT_OUT) -xf - ./boot/u-boot.imx
 	$(LOG) u-boot finished
 
-.PHONY:: bootloader
\ No newline at end of file
+.PHONY:: bootloader
diff --git a/flash.sh b/flash.sh
index 21b8dbd..139f916 100755
--- a/flash.sh
+++ b/flash.sh
@@ -19,9 +19,12 @@
 ROOTDIR=$(dirname $0)/..
 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 
+# Set USERSPACE_ARCH to arm64, if not set in the environment
+USERSPACE_ARCH=${USERSPACE_ARCH:=arm64}
+
 # If there's a boot.img in the same folder as flash.sh,
 # find artifacts in the directory containing the script.
-if [ -e ${SCRIPT_DIR}/boot.img ]; then
+if [ -e ${SCRIPT_DIR}/boot_${USERSPACE_ARCH}.img ]; then
     PRODUCT_OUT=${SCRIPT_DIR}
 else
     PRODUCT_OUT=${PRODUCT_OUT:=${ROOTDIR}/out/target/product/imx8m_phanbell}
@@ -56,6 +59,6 @@
 
 # Flash filesystems
 fastboot erase misc
-fastboot flash boot ${PRODUCT_OUT}/boot.img
-fastboot flash rootfs ${PRODUCT_OUT}/rootfs.img
+fastboot flash boot ${PRODUCT_OUT}/boot_${USERSPACE_ARCH}.img
+fastboot flash rootfs ${PRODUCT_OUT}/rootfs_${USERSPACE_ARCH}.img
 fastboot reboot
diff --git a/make_sdcard.sh b/make_sdcard.sh
new file mode 100755
index 0000000..111f305
--- /dev/null
+++ b/make_sdcard.sh
@@ -0,0 +1,20 @@
+#!/bin/bash -x
+
+ROOTFS_DIR=$1
+SDCARD_PATH=$2
+ROOTDIR=$3
+
+LOOP=$(sudo losetup --show -f $2)
+sudo partx -d ${LOOP} || true
+sudo partx -a ${LOOP}
+
+sudo mount ${LOOP}p2 $1
+sudo mount ${LOOP}p1 $1/boot
+
+sudo cp $3/board/fstab.sdcard $1/etc/fstab
+
+sudo umount $1/boot
+sudo umount $1
+
+sudo partx -d ${LOOP}
+sudo losetup -d ${LOOP}
\ No newline at end of file
diff --git a/sdcard.mk b/sdcard.mk
index 4ffb84b..63d80f8 100644
--- a/sdcard.mk
+++ b/sdcard.mk
@@ -24,53 +24,43 @@
 UBOOT_START := 66# Cribbed from Yocto.
 SDIMAGE_SIZE_MB := $(shell echo $(ROOTFS_START)+$(ROOTFS_SIZE_MB) | bc)
 
-sdcard: $(PRODUCT_OUT)/sdcard.img
-sdcard-xz: $(PRODUCT_OUT)/sdcard.img.xz
+SDCARD_WIP_PATH := $(PRODUCT_OUT)/sdcard_$(USERSPACE_ARCH).img.wip
+SDCARD_PATH := $(PRODUCT_OUT)/sdcard_$(USERSPACE_ARCH).img
 
-sdcard-allocate: | $(PRODUCT_OUT)
-	fallocate -l $(SDIMAGE_SIZE_MB)M $(PRODUCT_OUT)/sdcard.img
+sdcard: $(SDCARD_PATH)
+sdcard-xz: $(SDCARD_PATH).xz
 
-$(PRODUCT_OUT)/sdcard.img: sdcard-allocate \
-                           $(ROOTDIR)/build/rootfs.mk \
-                           $(ROOTDIR)/build/boot.mk \
-                           $(ROOTDIR)/board/fstab.sdcard \
-                           | $(PRODUCT_OUT)/u-boot.imx \
-                           $(PRODUCT_OUT)/boot.img \
-                           $(PRODUCT_OUT)/obj/ROOTFS/rootfs.patched.img
-	parted -s $(PRODUCT_OUT)/sdcard.img mklabel msdos
-	parted -s $(PRODUCT_OUT)/sdcard.img unit MiB mkpart primary ext2 $(BOOT_START) $(ROOTFS_START)
-	parted -s $(PRODUCT_OUT)/sdcard.img unit MiB mkpart primary ext4 $(ROOTFS_START) 100%
-	dd if=$(PRODUCT_OUT)/u-boot.imx of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(UBOOT_START) bs=512
-	dd if=$(PRODUCT_OUT)/boot.img of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(BOOT_START) bs=1M
-	dd if=$(PRODUCT_OUT)/obj/ROOTFS/rootfs.patched.img \
-		of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(ROOTFS_START) bs=1M
-
+$(SDCARD_PATH): $(ROOTDIR)/build/rootfs.mk \
+                $(ROOTDIR)/build/boot.mk \
+                $(ROOTDIR)/board/fstab.sdcard \
+                | $(PRODUCT_OUT)/u-boot.imx \
+                $(PRODUCT_OUT)/boot_$(USERSPACE_ARCH).img \
+                $(PRODUCT_OUT)/obj/ROOTFS/rootfs_$(USERSPACE_ARCH).patched.img \
+                out-dirs
+	fallocate -l $(SDIMAGE_SIZE_MB)M $(SDCARD_WIP_PATH)
+	parted -s $(SDCARD_WIP_PATH) mklabel msdos
+	parted -s $(SDCARD_WIP_PATH) unit MiB mkpart primary ext2 $(BOOT_START) $(ROOTFS_START)
+	parted -s $(SDCARD_WIP_PATH) unit MiB mkpart primary ext4 $(ROOTFS_START) 100%
+	dd if=$(PRODUCT_OUT)/u-boot.imx of=$(SDCARD_WIP_PATH) conv=notrunc seek=$(UBOOT_START) bs=512
+	dd if=$(PRODUCT_OUT)/boot_$(USERSPACE_ARCH).img of=$(SDCARD_WIP_PATH) conv=notrunc seek=$(BOOT_START) bs=1M
+	dd if=$(PRODUCT_OUT)/obj/ROOTFS/rootfs_$(USERSPACE_ARCH).patched.img \
+		of=$(SDCARD_WIP_PATH) conv=notrunc seek=$(ROOTFS_START) bs=1M
 
 	mkdir -p $(ROOTFS_DIR)
 	-sudo umount $(ROOTFS_DIR)/boot
 	-sudo umount $(ROOTFS_DIR)
-	$(eval LOOP=$(shell sudo losetup --show -f $(PRODUCT_OUT)/sdcard.img))
-	-sudo partx -d $(LOOP)
-	sudo partx -a $(LOOP)
-
-	sudo mount $(LOOP)p2 $(ROOTFS_DIR)
-	sudo mount $(LOOP)p1 $(ROOTFS_DIR)/boot
-
-	sudo cp $(ROOTDIR)/board/fstab.sdcard $(ROOTFS_DIR)/etc/fstab
-
-	sudo umount $(ROOTFS_DIR)/boot
-	sudo umount $(ROOTFS_DIR)
-	sudo partx -d $(LOOP)
-	sudo losetup -d $(LOOP)
+	$(shell $(ROOTDIR)/board/make_sdcard.sh $(ROOTFS_DIR) $(SDCARD_WIP_PATH) $(ROOTDIR))
 	rmdir $(ROOTFS_DIR)
 
-$(PRODUCT_OUT)/sdcard.img.xz: $(PRODUCT_OUT)/sdcard.img
-	xz -k -T0 -0 $(PRODUCT_OUT)/sdcard.img
+	mv $(SDCARD_WIP_PATH) $(SDCARD_PATH)
+
+$(SDCARD_PATH).xz: $(SDCARD_PATH)
+	xz -k -T0 -0 $(SDCARD_PATH)
 
 targets::
 	@echo "sdcard     - generate a flashable sdcard image"
 
 clean::
-	rm -f $(PRODUCT_OUT)/sdcard.img $(PRODUCT_OUT)/sdcard.img.xz
+	rm -f $(PRODUCT_OUT)/sdcard_*.img $(PRODUCT_OUT)/sdcard_*.img.xz
 
 .PHONY:: sdcard sdcard-xz