Build dtb.img and dtbo.img manually

We want to keep the order of DT blobs inside images fixed, in order to
get those DT blobs by index in bootloader. So let's build those images
manually. Remove BOARD_PREBUILT_DTBIMAGE_DIR, as we don't want Android
build system to generate dtb.img for us anymore (we do it locally).

Also add DTBO partition size, so that AVB can sign our dtbo.img.

Change-Id: Idd4653092c150dea99d37f29306733711d4fbb2b
Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
diff --git a/BoardConfig.mk b/BoardConfig.mk
index 2f03a28..ba5bd05 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -44,6 +44,7 @@
 BOARD_BOOTIMAGE_PARTITION_SIZE := 20971520 # 20 MiB
 BOARD_RECOVERYIMAGE_PARTITION_SIZE := 67108864 # 64 MiB
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 961658368 # ~917 MiB
+BOARD_DTBOIMG_PARTITION_SIZE := 8388608 # 8 MiB
 
 BOARD_SUPER_PARTITION_SIZE := 2684354560 # 2560 MiB
 BOARD_SUPER_PARTITION_GROUPS := group_oem
@@ -101,9 +102,15 @@
 
 # Include *.dtb to boot.img and use Android Boot Image v2
 BOARD_INCLUDE_DTB_IN_BOOTIMG := true
-BOARD_PREBUILT_DTBIMAGE_DIR := $(DTB_DIR)
 BOARD_MKBOOTIMG_ARGS := --header_version 2
 
+# Pass unsigned dtbo image (generated by build/tasks/dtimages.mk) to Android
+# build system for AVB signing
+DTBO_UNSIGNED := dtbo-unsigned.img
+# $(PRODUCT_OUT) hasn't been defined yet, so use "=" instead of ":="
+# so that it is resolved later
+BOARD_PREBUILT_DTBOIMAGE = $(PRODUCT_OUT)/$(DTBO_UNSIGNED)
+
 # Board uses A/B OTA.
 AB_OTA_UPDATER := true
 # A/B updater updatable partitions list. Keep in sync with the partition list
diff --git a/build/tasks/dtimages.mk b/build/tasks/dtimages.mk
new file mode 100644
index 0000000..19ebede
--- /dev/null
+++ b/build/tasks/dtimages.mk
@@ -0,0 +1,41 @@
+# Use this file to generate dtb.img and dtbo.img instead of using
+# BOARD_PREBUILT_DTBIMAGE_DIR. We need to keep dtb and dtbo files at the fixed
+# positions in images, so that bootloader can rely on their indexes in the
+# image. As dtbo.img must be signed with AVB tool, we generate intermediate
+# dtbo.img, and the resulting $(PRODUCT_OUT)/dtbo.img will be created with
+# Android build system, by exploiting BOARD_PREBUILT_DTBOIMAGE variable.
+
+ifneq ($(filter beagle_x15%, $(TARGET_DEVICE)),)
+
+MKDTIMG := $(realpath prebuilts/misc/$(HOST_PREBUILT_TAG)/libufdt/mkdtimg)
+DTBIMAGE := $(PRODUCT_OUT)/dtb.img
+DTBOIMAGE := $(PRODUCT_OUT)/$(DTBO_UNSIGNED)
+
+# Please keep this list fixed: add new files in the end of the list
+DTB_FILES := \
+	$(DTB_DIR)/am57xx-beagle-x15-revc.dtb \
+
+# Please keep this list fixed: add new files in the end of the list
+DTBO_FILES := \
+	$(DTBO_DIR)/am57xx-evm-common.dtbo \
+	$(DTBO_DIR)/am57xx-evm-reva3.dtbo
+
+$(DTBIMAGE): $(DTB_FILES)
+	cat $^ > $@
+
+$(DTBOIMAGE): $(DTBO_FILES)
+	$(MKDTIMG) create $@ $^
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := dtbimage
+LOCAL_ADDITIONAL_DEPENDENCIES := $(DTBIMAGE)
+include $(BUILD_PHONY_PACKAGE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := dtboimage
+LOCAL_ADDITIONAL_DEPENDENCIES := $(DTBOIMAGE)
+include $(BUILD_PHONY_PACKAGE)
+
+droidcore: dtbimage dtboimage
+
+endif