SPM: Makefile: Add targets to build resource description blobs

The targets cactus and ivy now also build the resource description blobs
used by Cactus and Ivy. This is done through a new shell script:
generate_dtb.sh

Example command line to build a package with Cactus and Ivy:

    CROSS_COMPILE=aarch64-linux-gnu- make PLAT=fvp   \
    DEBUG=1 LOG_LEVEL=50 TESTS=spm                   \
    tftf cactus ivy

    trusted_firmware/tools/sptool/sptool             \
        -o sp_package.bin                            \
        -i cactus.bin:cactus.dtb                     \
        -i ivy.bin:ivy.dtb

Also, add a disclaimer to explain that the current implementation of SPM
is a prototype that is going to undergo a lot of rework.

Change-Id: Iebc3ac28d20019b59f07f70f96fefc030f1a79ce
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
diff --git a/Makefile b/Makefile
index c0d1354..9c4ae7e 100644
--- a/Makefile
+++ b/Makefile
@@ -459,8 +459,8 @@
 	@echo "  tftf           Build the TFTF image"
 	@echo "  ns_bl1u        Build the NS_BL1U image"
 	@echo "  ns_bl2u        Build the NS_BL2U image"
-	@echo "  cactus         Build the Cactus image (Test S-EL0 payload)."
-	@echo "  ivy            Build the Ivy image (Test S-EL0 payload)."
+	@echo "  cactus         Build the Cactus image (Test S-EL0 payload) and resource description."
+	@echo "  ivy            Build the Ivy image (Test S-EL0 payload) and resource description."
 	@echo "  el3_payload    Build the EL3 test payload"
 	@echo "  checkcodebase  Check the coding style of the entire source tree"
 	@echo "  checkpatch     Check the coding style on changes in the current"
diff --git a/docs/user-guide.rst b/docs/user-guide.rst
index d070c6a..397b4c2 100644
--- a/docs/user-guide.rst
+++ b/docs/user-guide.rst
@@ -29,7 +29,7 @@
 
 ::
 
-    sudo apt-get install build-essential make git perl libxml-libxml-perl
+    sudo apt-get install device-tree-compiler build-essential make git perl libxml-libxml-perl
 
 Download and install the GNU cross-toolchain from Linaro. The TF-A Tests have
 been tested with version 6.2-2016.11 (gcc 6.2):
diff --git a/spm/README.txt b/spm/README.txt
new file mode 100644
index 0000000..bff23ef
--- /dev/null
+++ b/spm/README.txt
@@ -0,0 +1,8 @@
+This is a prototype loosely based on the SPCI Alpha and SPRT pre-alpha
+specifications. Any interface / platform API introduced for this is subject to
+change as it evolves.
+
+Cactus is meant to be the main test Secure Partition. It is the one meant to
+have most of the tests that a Secure Partition has to do. Ivy is meant to be
+more minimalistic. In the future, Cactus may be modified to be a S-EL1 partition
+while Ivy will remain as a S-EL0 partition.
diff --git a/spm/cactus/cactus.mk b/spm/cactus/cactus.mk
index 6ba0db0..1ebc692 100644
--- a/spm/cactus/cactus.mk
+++ b/spm/cactus/cactus.mk
@@ -6,6 +6,8 @@
 
 include lib/sprt/sprt_client.mk
 
+CACTUS_DTB	:= $(BUILD_PLAT)/cactus.dtb
+
 CACTUS_INCLUDES :=					\
 	-Iinclude					\
 	-Iinclude/common				\
@@ -73,4 +75,13 @@
         $(eval $(call add_define,CACTUS_DEFINES,AARCH64))
 endif
 
-cactus: ${AUTOGEN_DIR}/tests_list.h
+$(CACTUS_DTB) : $(BUILD_PLAT)/cactus $(BUILD_PLAT)/cactus/cactus.elf
+$(CACTUS_DTB) : spm/cactus/cactus.dts
+	@echo "  DTBGEN  spm/cactus/cactus.dts"
+	${Q}tools/generate_dtb/generate_dtb.sh \
+		cactus spm/cactus/cactus.dts $(BUILD_PLAT)
+	@echo
+	@echo "Built $@ successfully"
+	@echo
+
+cactus: $(CACTUS_DTB) $(AUTOGEN_DIR)/tests_list.h
diff --git a/spm/ivy/ivy.mk b/spm/ivy/ivy.mk
index 2f17433..89fd92d 100644
--- a/spm/ivy/ivy.mk
+++ b/spm/ivy/ivy.mk
@@ -6,6 +6,8 @@
 
 include lib/sprt/sprt_client.mk
 
+IVY_DTB		:= $(BUILD_PLAT)/ivy.dtb
+
 IVY_INCLUDES :=					\
 	-Iinclude					\
 	-Iinclude/common				\
@@ -70,4 +72,13 @@
         $(eval $(call add_define,IVY_DEFINES,AARCH64))
 endif
 
-ivy: ${AUTOGEN_DIR}/tests_list.h
+$(IVY_DTB) : $(BUILD_PLAT)/ivy $(BUILD_PLAT)/ivy/ivy.elf
+$(IVY_DTB) : spm/ivy/ivy.dts
+	@echo "  DTBGEN  spm/ivy/ivy.dts"
+	${Q}tools/generate_dtb/generate_dtb.sh \
+		ivy spm/ivy/ivy.dts $(BUILD_PLAT)
+	@echo
+	@echo "Built $@ successfully"
+	@echo
+
+ivy: $(IVY_DTB) $(AUTOGEN_DIR)/tests_list.h
diff --git a/tools/generate_dtb/generate_dtb.sh b/tools/generate_dtb/generate_dtb.sh
new file mode 100755
index 0000000..efcc67e
--- /dev/null
+++ b/tools/generate_dtb/generate_dtb.sh
@@ -0,0 +1,78 @@
+#!/bin/bash
+
+#
+# Copyright (c) 2018, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# Generate a DTB file from a base DTS file and the MAP file generated during the
+# compilation of a Secure Partition.
+
+# $1 = image_name (lowercase)
+# $2 = path/to/file.dts
+# $3 = build/$PLAT/$BUILD_TYPE/
+
+ORIGINAL_DTS=$2
+MAPFILE="$3/$1/$1.map"
+EXTRA_DTS="$3/$1/$1_extra.dts"
+COMBINED_DTS="$3/$1/$1_combined.dts"
+PREPROCESSED_DTS="$3/$1/$1_preprocessed.dts"
+GENERATED_DTB="$3/$1.dtb"
+
+# Look for the start and end of the sections that are only known in the elf file
+# after compiling the partition.
+
+TEXT_START=$(grep __TEXT_START__ $MAPFILE | awk {'print $1'})
+TEXT_END=$(grep __TEXT_END__ $MAPFILE | awk {'print $1'})
+
+RODATA_START=$(grep __RODATA_START__ $MAPFILE | awk {'print $1'})
+RODATA_END=$(grep __RODATA_END__ $MAPFILE | awk {'print $1'})
+
+DATA_START=$(grep __DATA_START__ $MAPFILE | awk {'print $1'})
+DATA_END=$(grep __DATA_END__ $MAPFILE | awk {'print $1'})
+
+BSS_START=$(grep __BSS_START__ $MAPFILE | awk {'print $1'})
+BSS_END=$(grep __BSS_END__ $MAPFILE | awk {'print $1'})
+
+# Inject new sections to the base DTS
+
+echo "\
+/ {
+	memory_regions {
+		text {
+			str = \"Text\";
+			base = <0x0 ${TEXT_START}ULL>;
+			size = <0x0 (${TEXT_END}ULL - ${TEXT_START}ULL)>;
+			attr = <RD_MEM_NORMAL_CODE>;
+		};
+		rodata {
+			str = \"RO Data\";
+			base = <0x0 (${RODATA_START}ULL)>;
+			size = <0x0 (${RODATA_END}ULL - ${RODATA_START}ULL)>;
+			attr = <RD_MEM_NORMAL_RODATA>;
+		};
+		rwdata {
+			str = \"Data\";
+			base = <0x0 ${DATA_START}ULL>;
+			size = <0x0 (${DATA_END}ULL - ${DATA_START}ULL)>;
+			attr = <RD_MEM_NORMAL_DATA>;
+		};
+		bss {
+			str = \"BSS\";
+			base = <0x0 ${BSS_START}ULL>;
+			size = <0x0 (${BSS_END}ULL - ${BSS_START}ULL)>;
+			attr = <RD_MEM_NORMAL_BSS>;
+		};
+	};
+};" > "$EXTRA_DTS"
+
+cat "$ORIGINAL_DTS" "$EXTRA_DTS" > "$COMBINED_DTS"
+
+INCLUDES="-I spm/cactus
+	  -I spm/ivy
+	  -I spm/include
+	  -I include/lib"
+
+cpp -x c -P -o "$PREPROCESSED_DTS" "$COMBINED_DTS" ${INCLUDES} -DAARCH64
+dtc -I dts -O dtb "$PREPROCESSED_DTS" > "$GENERATED_DTB"