Enable boot.img creation

In order to build a boot.img, the following build variables
need to be defined in the build.config file:

As we finalize where all the prebuilts will get delivered to,
some of the below may become unnecessary.

BUILD_BOOT_IMG=1
MKBOOTIMG_PREBUILT=<path to mkbootimg.py>
GKI_RAMDISK_PREBUILT_BINARY=<path to GKI ramdisk prebuilt>
VENDOR_RAMDISK_PREBUILT_BINARY=<path to vendor ramdisk prebuilt>
KERNEL_BINARY=<name of kernel file, eg. Image.lz4, Image.gz etc>
BOOT_IMAGE_HEADER_VERSION=<boot image header version>

Change-Id: I191c39911b53cef17c01bd4adb8dd4f5a2e17aee
diff --git a/build.sh b/build.sh
index 7b9bc6f..f1dfd75 100755
--- a/build.sh
+++ b/build.sh
@@ -83,6 +83,26 @@
 #   SKIP_CP_KERNEL_HDR
 #     if defined, skip installing kernel headers.
 #
+#   BUILD_BOOT_IMG
+#     if defined, build a boot.img binary that can be flashed into the 'boot'
+#     partition of an Android device. The boot image contains a header as per the
+#     format defined by https://source.android.com/devices/bootloader/boot-image-header
+#     followed by several components like kernel, ramdisk, DTB etc. The ramdisk
+#     component comprises of a GKI ramdisk cpio archive concatenated with a
+#     vendor ramdisk cpio archive which is then gzipped. It is expected that
+#     all components are present in ${DIST_DIR}.
+#
+#     When the BUILD_BOOT_IMG flag is defined, the following flags that point to the
+#     various components needed to build a boot.img also need to be defined.
+#     - MKBOOTIMG_PATH=<path to the mkbootimg.py script which builds boot.img>
+#     - GKI_RAMDISK_PREBUILT_BINARY=<Name of the GKI ramdisk prebuilt which includes
+#       the generic ramdisk components like init and the non-device-specific rc files>
+#     - VENDOR_RAMDISK_BINARY=<Name of the vendor ramdisk binary which includes the
+#       device-specific components of ramdisk like the fstab file and the
+#       device-specific rc files.>
+#     - KERNEL_BINARY=<name of kernel binary, eg. Image.lz4, Image.gz etc>
+#     - BOOT_IMAGE_HEADER_VERSION=<version of the boot image header>
+#
 # Note: For historic reasons, internally, OUT_DIR will be copied into
 # COMMON_OUT_DIR, and OUT_DIR will be then set to
 # ${COMMON_OUT_DIR}/${KERNEL_DIR}. This has been done to accommodate existing
@@ -331,6 +351,56 @@
 echo "========================================================"
 echo " Files copied to ${DIST_DIR}"
 
+if [ ! -z "${BUILD_BOOT_IMG}" ] ; then
+
+	DTB_FILE_LIST=$(find ${DIST_DIR} -name "*.dtb")
+	if [ -z "${DTB_FILE_LIST}" ]; then
+		echo "No *.dtb files found in ${DIST_DIR}"
+		exit 1
+	fi
+	cat $DTB_FILE_LIST > ${DIST_DIR}/dtb.img
+
+	if [ ! -f "${DIST_DIR}/$GKI_RAMDISK_PREBUILT_BINARY" ]; then
+		echo "GKI ramdisk prebuilt binary" \
+			"(GKI_RAMDISK_PREBUILT_BINARY = $GKI_RAMDISK_PREBUILT_BINARY)" \
+			"not present in ${DIST_DIR}"
+		exit 1
+	fi
+	if [ ! -f "${DIST_DIR}/$VENDOR_RAMDISK_BINARY" ]; then
+		echo "vendor ramdisk binary(VENDOR_RAMDISK_BINARY = $VENDOR_RAMDISK_BINARY)" \
+			"not present in ${DIST_DIR}"
+		exit 1
+	fi
+
+	cat ${DIST_DIR}/$GKI_RAMDISK_PREBUILT_BINARY ${DIST_DIR}/$VENDOR_RAMDISK_BINARY \
+		> ${DIST_DIR}/ramdisk.cpio
+	gzip -f ${DIST_DIR}/ramdisk.cpio > ${DIST_DIR}/ramdisk
+
+	if [ ! -x "$MKBOOTIMG_PATH" ]; then
+		echo "mkbootimg.py script not found or not executable. MKBOOTIMG_PATH = $MKBOOTIMG_PATH"
+		exit 1
+	fi
+
+	if [ ! -f "${DIST_DIR}/$KERNEL_BINARY" ]; then
+		echo "kernel binary(KERNEL_BINARY = $KERNEL_BINARY) not present in ${DIST_DIR}"
+		exit 1
+	fi
+
+	if [ -z "${BOOT_IMAGE_HEADER_VERSION}" ]; then
+		echo "BOOT_IMAGE_HEADER_VERSION must specify the boot image header version"
+		exit 1
+	fi
+
+	(set -x; $MKBOOTIMG_PATH --kernel ${DIST_DIR}/$KERNEL_BINARY \
+		--ramdisk ${DIST_DIR}/ramdisk \
+		--dtb ${DIST_DIR}/dtb.img --header_version $BOOT_IMAGE_HEADER_VERSION \
+		-o ${DIST_DIR}/boot.img
+	)
+	set +x
+	echo "boot image created at ${DIST_DIR}/boot.img"
+fi
+
+
 # No trace_printk use on build server build
 if readelf -a ${DIST_DIR}/vmlinux 2>&1 | grep -q trace_printk_fmt; then
   echo "========================================================"