Add build rules for crosvm.img prebuilt

Check in download script from Cuttlefish GitHub and add a build rule
which uses it to download Cuttlefish host tools (includes CrosVM) and
packages it into a ext4 image in prebuilts/.

Change-Id: Ia3b9bc59ea081a2b03af262204ae577096dc236e
diff --git a/Makefile b/Makefile
index ca0dd53..a243d98 100644
--- a/Makefile
+++ b/Makefile
@@ -238,17 +238,31 @@
 
 .PHONY:update-prebuilts
 update-prebuilts: \
-	prebuilts/linux-aarch64/images \
+	prebuilts/linux-aarch64/images/crosvm.ext4 \
+	prebuilts/linux-aarch64/images/efi-virtio.rom \
+	prebuilts/linux-aarch64/images/rootfs.ext4 \
 	prebuilts/linux-x86/qemu
 
-# The rootfs and rom images for qemu
-.PHONY:prebuilts/linux-aarch64/images
-prebuilts/linux-aarch64/images: buildroot
-	- rm -rf $@
-	mkdir -p $@
+prebuilts/linux-aarch64/images/rootfs.ext4: buildroot
+	mkdir -p $(shell dirname $@)
 	cp $(BR_OUT)/images/rootfs.ext4 $@
+
+prebuilts/linux-aarch64/images/efi-virtio.rom: buildroot
+	mkdir -p $(shell dirname $@)
 	cp $(BR_OUT)/per-package/host-qemu/host/share/qemu/efi-virtio.rom $@
 
+CF_DOWNLOAD_AOSP_SH := $(ROOT_DIR)/build/cuttlefish/download-aosp.sh
+CROSVM_IMG_SIZE_MB := 1024
+
+prebuilts/linux-aarch64/images/crosvm.ext4: TMP_DIR := $(shell mktemp -d)
+prebuilts/linux-aarch64/images/crosvm.ext4: $(CF_DOWNLOAD_AOSP_SH)
+	mkdir -p $(shell dirname $@)
+	cd $(TMP_DIR) && $(CF_DOWNLOAD_AOSP_SH) -a arm64 -C
+	dd if=/dev/zero of=$@.tmp bs=1M count=$(CROSVM_IMG_SIZE_MB)
+	mkfs.ext4 -d $(TMP_DIR) $@.tmp
+	rm -rf $(TMP_DIR)
+	mv $@.tmp $@
+
 # The qemu binary
 .PHONY:prebuilts/linux-x86/qemu
 prebuilts/linux-x86/qemu:
diff --git a/cuttlefish/download-aosp.sh b/cuttlefish/download-aosp.sh
new file mode 100755
index 0000000..9bbf271
--- /dev/null
+++ b/cuttlefish/download-aosp.sh
@@ -0,0 +1,111 @@
+#!/bin/bash
+##
+## Copied from:
+## https://github.com/google/android-cuttlefish/blob/main/download-aosp.sh
+##
+
+set -o errexit
+
+function print_help {
+	echo "   $0 <options>"
+	echo "     Options:"
+	echo "       -a | --arch                      : architecture to download for: x86, x86_64, arm64"
+	echo "       -A | --android                   : download the Android OS images (by default disabled)"
+	echo "       -C | --cuttlefish                : download the Cuttlefish host images (by default disabled)"
+	echo "       -h | --help                      : print this help message"
+}
+
+function parse_opts() {
+	local help=0
+	local params
+
+	if params=$(getopt -o 'a:ACh' -l 'arch:,android,cuttlefish,help' --name "$0" -- "$@"); then
+		eval set -- "${params}"
+		while true; do
+			case "$1" in
+				-a|--arch)
+					ARCH=$2
+					case "${ARCH}" in
+						x86)
+							THROTTLED=
+							;;
+						x86_64)
+							THROTTLED='-throttled'
+							;;
+						arm64|aarch64)
+							ARCH='arm64'
+							THROTTLED='-throttled'
+							;;
+						*)
+							echo "Uknown architecture ${ARCH}" 1>&2
+							help=1
+							;;
+					esac
+					shift 2
+					;;
+				-A|--android)
+					DOWNLOAD_ANDROID=1
+					shift
+					;;
+				-C|--cuttlefish)
+					DOWNLOAD_CF=1
+					shift
+					;;
+				-h|--help)
+					help=1
+					shift
+					;;
+				--)
+					shift
+					break
+					;;
+			esac
+		done
+	fi
+
+	if [ -z "${ARCH}" ]; then
+		echo "You must specify an architecture." 1>&2
+		help=1
+	fi
+
+	if [[ "${DOWNLOAD_ANDROID}" -eq 0 && "${DOWNLOAD_CF}" -eq 0 ]]; then
+		echo "You are not downloading anything.  Select -A and/or -C" 1>&2
+		help=1
+	fi
+
+	if [ "${help}" -eq 1 ]; then
+		print_help
+		exit 1
+	fi
+}
+
+DOWNLOAD_ANDROID=0
+DOWNLOAD_CF=0
+
+parse_opts $*
+
+URL=https://ci.android.com/builds/latest/branches/aosp-master${THROTTLED}/targets/aosp_cf_${ARCH}_phone-userdebug/view/BUILD_INFO
+RURL=$(curl -Ls -o /dev/null -w %{url_effective} ${URL})
+
+if [ "${DOWNLOAD_ANDROID}" -eq 1 ]; then
+	echo "#"
+	echo "# Downloading the latest Android images from AOSP"
+	echo "#"
+	set -x
+	IMG=aosp_cf_${ARCH}_phone-img-$(echo $RURL | awk -F\/ '{print $6}').zip
+	wget -nv ${RURL%/view/BUILD_INFO}/raw/${IMG}
+	unzip "${IMG}"
+	rm -v "${IMG}"
+	set +x
+fi
+
+if [ "${DOWNLOAD_CF}" -eq 1 ]; then
+	echo "#"
+	echo "# Downloading the latest Cuttlefish images from AOSP"
+	echo "#"
+	set -x
+	wget -nv ${RURL%/view/BUILD_INFO}/raw/cvd-host_package.tar.gz
+	tar xzvf cvd-host_package.tar.gz
+	rm -v cvd-host_package.tar.gz
+	set +x
+fi