test(ivy): S-EL0 partition using VHE

Starting from [1], Cactus remains the sample S-EL1 partition.
Ivy remains the sample "S-EL0 partition" either using the shim at S-EL1
(as of today), or leveraging Hafnium VHE (reason for this change).
The same code base is re-used by adding the IVY_SHIM toggle. IVY_SHIM=1
is default using the shim, or 0 to use VHE (and strip the shim out).
Using svc helper from spm/common/aarch64/sp_arch_helpers.S

We must modify generate_json.sh so it only adds the partition
information to the layout file for the sp given in arg1. This allows
the ability for sp's to pass flags to the script such as IVY_SHIM which
is used to vary the dts file included for ivy.

Typical build command for a VHE-enabled Ivy partition:
make CROSS_COMPILE=aarch64-none-elf- PLAT=fvp DEBUG=1 TESTS=spm
    ARM_ARCH_MINOR=5 BRANCH_PROTECTION=1 IVY_SHIM=0 ivy -j8

The intent is to create a dedicated tftf_config build config for the
VHE-enabled build in the CI.

[1] https://trustedfirmware-a-tests.readthedocs.io/en/latest/getting_started/build.html#cactus-and-ivy

Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: I34125b375b043c61c44ede558802d8ae757bd51f
diff --git a/tools/generate_json/generate_json.sh b/tools/generate_json/generate_json.sh
index d1b861f..0485862 100755
--- a/tools/generate_json/generate_json.sh
+++ b/tools/generate_json/generate_json.sh
@@ -1,26 +1,52 @@
 #!/bin/bash
 
 #
-# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 
 # Generate a JSON file which will be fed to TF-A as SPM_LAYOUT_FILE to package
 # Secure Partitions as part of FIP.
+# Note the script will append the partition to the existing layout file.
+# If you wish to only generate a layout file with this partition first run
+# "make realclean" to remove the existing file.
 
-# $1 = Secure Partition (cactus)
+# $1 = Secure Partition
 # $2 = Platform built path
+# $3 = Ivy Shim present
 # Output = $2/sp_layout.json
 
 GENERATED_JSON=$2/sp_layout.json
+primary_dts="$1.dts"
+
+# Remove closing bracket and add comma if the dts is already present.
+if [ ! -f "$GENERATED_JSON" ]; then
+	echo -e "{\n" >> "$GENERATED_JSON"
+else
+	if [ "$1" == "ivy" ]; then
+		if [ "$3" == "1" ]; then
+			primary_dts="ivy-sel1.dts"
+		else
+			primary_dts="ivy-sel0.dts"
+		fi
+	fi
+
+	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
 
 # 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 "{\n\t\"$1-primary\" : {\n \
+	echo -e "\t\"$1-primary\" : {\n \
 	\t\"image\": {\n \
 	\t\t\"file\": \"$1.bin\",\n \
 	\t\t\"offset\":\"0x2000\"\n\
@@ -35,12 +61,15 @@
 	\t\"owner\": \"Plat\"\n\t},\n\n\t\"$1-tertiary\" : {\n \
 	\t\"image\": \"$1.bin\",\n \
 	\t\"pm\": \"$1-tertiary.dts\",\n \
-	\t\"owner\": \"Plat\"\n\t},\n\n\t\"ivy\" : {\n \
+	\t\"owner\": \"Plat\"\n\t}" \
+	>> "$GENERATED_JSON"
+elif [ "$1" == "ivy" ]; then
+	echo -e "\t\"ivy\" : {\n \
 	\t\"image\": \"ivy.bin\",\n \
-	\t\"pm\": \"ivy.dts\", \n \
-	\t\"owner\": \"Plat\"\n \
-	}\n}" \
-	> "$GENERATED_JSON"
+	\t\"pm\": \"$primary_dts\",\n \
+	\t\"owner\": \"Plat\"\n\t}" >> "$GENERATED_JSON"
 else
-	echo -e "\nWarning: Only Cactus is supported as Secure Partition\n"
+	echo -e "\nWarning: Secure Partition not supported\n"
 fi
+
+echo -e "}" >> "$GENERATED_JSON"