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/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"