fix(build): race condition in sp layout generation

To prevent concurrency errors make it so generate_json.sh is
only called once during make. To do this supply a list of
present partitions to the script and generate the sp_layout
file using this.

Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: If9987cf5cd88be4ca617ce304e37d95346fb481b
diff --git a/Makefile b/Makefile
index 738a926..ff8ac27 100644
--- a/Makefile
+++ b/Makefile
@@ -112,6 +112,9 @@
 include fwu/ns_bl1u/ns_bl1u.mk
 include fwu/ns_bl2u/ns_bl2u.mk
 
+# List of secure partitions present.
+SECURE_PARTITIONS	:=
+
 # Only platform fvp supports cactus_mm, quark
 ifeq (${ARCH}-${PLAT},aarch64-fvp)
 include spm/cactus_mm/cactus_mm.mk
@@ -531,6 +534,10 @@
   $(eval $(call MAKE_IMG,ivy))
 endif
 
+SP_LAYOUT:
+	${Q}tools/generate_json/generate_json.sh \
+		$(BUILD_PLAT) $(SECURE_PARTITIONS)
+
 # The EL3 test payload is only supported in AArch64. It has an independent build
 # system.
 .PHONY: el3_payload
diff --git a/spm/cactus/cactus.mk b/spm/cactus/cactus.mk
index ce65035..c3bbe59 100644
--- a/spm/cactus/cactus.mk
+++ b/spm/cactus/cactus.mk
@@ -13,7 +13,8 @@
 	include ${CACTUS_PLAT_PATH}/platform.mk
 endif
 
-CACTUS_DTB	:= $(BUILD_PLAT)/cactus.dtb
+CACTUS_DTB		:= $(BUILD_PLAT)/cactus.dtb
+SECURE_PARTITIONS	+= cactus
 
 CACTUS_INCLUDES :=					\
 	-Itftf/framework/include			\
@@ -95,13 +96,11 @@
 	@echo "  DTBGEN  $@"
 	${Q}tools/generate_dtb/generate_dtb.sh \
 		cactus ${CACTUS_DTS} $(BUILD_PLAT) $(CACTUS_DTB)
-	${Q}tools/generate_json/generate_json.sh \
-		cactus $(BUILD_PLAT)
 	@echo
 	@echo "Built $@ successfully"
 	@echo
 
-cactus: $(CACTUS_DTB)
+cactus: $(CACTUS_DTB) SP_LAYOUT
 
 # FDTS_CP copies flattened device tree sources
 #   $(1) = output directory
diff --git a/spm/ivy/ivy.mk b/spm/ivy/ivy.mk
index 0c0039d..2e6cbfa 100644
--- a/spm/ivy/ivy.mk
+++ b/spm/ivy/ivy.mk
@@ -16,9 +16,11 @@
 IVY_SHIM	:= 1
 
 ifeq (${IVY_SHIM},1)
-	IVY_DTB		:= $(BUILD_PLAT)/ivy-sel1.dtb
+	IVY_DTB			:= $(BUILD_PLAT)/ivy-sel1.dtb
+	SECURE_PARTITIONS	+= ivy_shim
 else
-	IVY_DTB		:= $(BUILD_PLAT)/ivy-sel0.dtb
+	IVY_DTB			:= $(BUILD_PLAT)/ivy-sel0.dtb
+	SECURE_PARTITIONS	+= ivy
 endif
 
 IVY_INCLUDES :=					\
@@ -98,13 +100,11 @@
 	@echo "  DTBGEN  $@"
 	${Q}tools/generate_dtb/generate_dtb.sh \
 		ivy ${IVY_DTS} $(BUILD_PLAT) $(IVY_DTB)
-	${Q}tools/generate_json/generate_json.sh \
-		ivy $(BUILD_PLAT) $(IVY_SHIM)
 	@echo
 	@echo "Built $@ successfully"
 	@echo
 
-ivy: $(IVY_DTB)
+ivy: $(IVY_DTB) SP_LAYOUT
 
 # FDTS_CP copies flattened device tree sources
 #   $(1) = output directory
diff --git a/tools/generate_json/generate_json.sh b/tools/generate_json/generate_json.sh
index 8a14517..81ab756 100755
--- a/tools/generate_json/generate_json.sh
+++ b/tools/generate_json/generate_json.sh
@@ -12,64 +12,82 @@
 # If you wish to only generate a layout file with this partition first run
 # "make realclean" to remove the existing file.
 
-# $1 = Secure Partition
-# $2 = Platform built path
-# $3 = Ivy Shim present
-# Output = $2/sp_layout.json
+# $1 = Platform built path
+# $2.. = List of Secure Partitions
+# Output = $1/sp_layout.json
 
-GENERATED_JSON=$2/sp_layout.json
-primary_dts="$1.dts"
+GENERATED_JSON=$1/sp_layout.json
+shift # Shift arguments 1
 
-if [ "$1" == "ivy" ]; then
-	if [ "$3" == "1" ]; then
-		primary_dts="ivy-sel1.dts"
-	else
-		primary_dts="ivy-sel0.dts"
-	fi
-fi
+PARTITION_ALREADY_PRESENT=false
 
-# Remove closing bracket and add comma if the dts is already present.
-if [ ! -f "$GENERATED_JSON" ]; then
-	echo -e "{\n" >> "$GENERATED_JSON"
-else
-	if [ $(grep "$primary_dts" "$GENERATED_JSON" | wc -l) -eq "0" ]; then
-		sed -i '$d' "$GENERATED_JSON"
-		sed -i '$ s/$/,/' "$GENERATED_JSON"
-		echo -e "\n" >> "$GENERATED_JSON"
-	else
-		exit 0
-	fi
-fi
+CACTUS_PRESENT=false
+IVY_PRESENT=false
+IVY_SHIM_PRESENT=false
+
+for target in "$@"
+do
+	case $target in
+		cactus)
+			CACTUS_PRESENT=true
+			;;
+		ivy)
+			IVY_PRESENT=true
+			;;
+		ivy_shim)
+			IVY_SHIM_PRESENT=true
+			;;
+		*)
+			echo "Invalid target $target"
+			exit 1
+			;;
+	esac
+done
+
+echo -e "{" > $GENERATED_JSON
 
 # To demonstrate communication between SP's, two cactus S-EL1 instances used.
 # To also test mapping of the RXTX region a third cactus S-EL1 instance is used.
 # cactus-primary, cactus-secondary and cactus-tertiary have same binary but
 # different partition manifests.
-if [ "$1" == "cactus" ]; then
-	echo -e "\t\"$1-primary\" : {\n \
+if [ $CACTUS_PRESENT == "true" ]; then
+	echo -ne "\t\"cactus-primary\" : {\n \
 	\t\"image\": {\n \
-	\t\t\"file\": \"$1.bin\",\n \
+	\t\t\"file\": \"cactus.bin\",\n \
 	\t\t\"offset\":\"0x2000\"\n\
 	\t},\n \
 	\t\"pm\": {\n \
-	\t\t\"file\": \"$1.dts\",\n \
+	\t\t\"file\": \"cactus.dts\",\n \
 	\t\t\"offset\":\"0x1000\"\n\
 	\t},\n
-	\t\"owner\": \"SiP\"\n\t},\n\n\t\"$1-secondary\" : {\n \
-	\t\"image\": \"$1.bin\",\n \
-	\t\"pm\": \"$1-secondary.dts\",\n \
-	\t\"owner\": \"Plat\"\n\t},\n\n\t\"$1-tertiary\" : {\n \
-	\t\"image\": \"$1.bin\",\n \
-	\t\"pm\": \"$1-tertiary.dts\",\n \
+	\t\"owner\": \"SiP\"\n\t},\n\n\t\"cactus-secondary\" : {\n \
+	\t\"image\": \"cactus.bin\",\n \
+	\t\"pm\": \"cactus-secondary.dts\",\n \
+	\t\"owner\": \"Plat\"\n\t},\n\n\t\"cactus-tertiary\" : {\n \
+	\t\"image\": \"cactus.bin\",\n \
+	\t\"pm\": \"cactus-tertiary.dts\",\n \
 	\t\"owner\": \"Plat\"\n\t}" \
 	>> "$GENERATED_JSON"
-elif [ "$1" == "ivy" ]; then
-	echo -e "\t\"ivy\" : {\n \
+	PARTITION_ALREADY_PRESENT=true
+fi
+if [ $IVY_PRESENT == "true" ]; then
+	if [ $PARTITION_ALREADY_PRESENT == "true" ]; then
+		echo -ne ",\n\n" >> $GENERATED_JSON
+	fi
+	echo -ne "\t\"ivy\" : {\n \
 	\t\"image\": \"ivy.bin\",\n \
-	\t\"pm\": \"$primary_dts\",\n \
+	\t\"pm\": \"ivy-sel0.dts\",\n \
 	\t\"owner\": \"Plat\"\n\t}" >> "$GENERATED_JSON"
-else
-	echo -e "\nWarning: Secure Partition not supported\n"
+	PARTITION_ALREADY_PRESENT=true
+elif [ $IVY_SHIM_PRESENT == "true" ]; then
+	if [ $PARTITION_ALREADY_PRESENT == "true" ]; then
+		echo -ne ",\n\n" >> $GENERATED_JSON
+	fi
+	echo -ne "\t\"ivy_shim\" : {\n \
+	\t\"image\": \"ivy.bin\",\n \
+	\t\"pm\": \"ivy-sel1.dts\",\n \
+	\t\"owner\": \"Plat\"\n\t}" >> "$GENERATED_JSON"
+	PARTITION_ALREADY_PRESENT=true
 fi
 
-echo -e "}" >> "$GENERATED_JSON"
+echo -e "\n}" >> "$GENERATED_JSON"