feat(gpt): add ci support to test using backup-gpt

Current GPT test uses the primary GPT header, so corrupt primary GPT
header at second LBA after the MBR. This would force it to use the
backup-gpt header at last LBA.

Also additional changes done to handle backup_gpt header to TF-A
causes BL31 to run out of space so add FVP_TRUSTED_SRAM_SIZE=384
to gpt builds.

Change-Id: I6660a5e3c533f58738272302c8008c88dd82b8a6
Signed-off-by: Govindraj Raja <govindraj.raja@arm.com>
diff --git a/expect-lava/backup_gpt.exp b/expect-lava/backup_gpt.exp
new file mode 100644
index 0000000..3ead05d
--- /dev/null
+++ b/expect-lava/backup_gpt.exp
@@ -0,0 +1,11 @@
+#
+# Copyright (c) 2023 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Expect script for Trusted Firmware Test Framework
+#
+
+expect_string+=('i;Trying to retrieve back-up GPT header')
+
+source $ci_root/expect-lava/tftf.inc
diff --git a/expect-lava/tftf.exp b/expect-lava/tftf.exp
index a855030..307e8f1 100644
--- a/expect-lava/tftf.exp
+++ b/expect-lava/tftf.exp
@@ -8,17 +8,4 @@
 
 source $ci_root/expect-lava/trusted-firmware.inc
 
-prompt='Booting trusted firmware test framework'
-failures='Panic:'
-expect_string+=("i;${prompt};;${failures}")
-
-prompt='Running at NS-EL(1|2)'
-expect_string+=("i;${prompt}")
-
-prompt='Tests Failed  : 0'
-failures='Unhandled exception on |Tests Failed  : [^0]'
-expect_string+=("i;${prompt};;${failures}")
-
-prompt='Exiting tests.'
-failures='Tests Passed  : 0'
-expect_string+=("i;${prompt};;${failures}")
+source $ci_root/expect-lava/tftf.inc
diff --git a/expect-lava/tftf.inc b/expect-lava/tftf.inc
new file mode 100644
index 0000000..37a6e80
--- /dev/null
+++ b/expect-lava/tftf.inc
@@ -0,0 +1,22 @@
+#
+# Copyright (c) 2023 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Expect script for Trusted Firmware Test Framework
+#
+
+prompt='Booting trusted firmware test framework'
+failures='Panic:'
+expect_string+=("i;${prompt};;${failures}")
+
+prompt='Running at NS-EL(1|2)'
+expect_string+=("i;${prompt}")
+
+prompt='Tests Failed  : 0'
+failures='Unhandled exception on |Tests Failed  : [^0]'
+expect_string+=("i;${prompt};;${failures}")
+
+prompt='Exiting tests.'
+failures='Tests Passed  : 0'
+expect_string+=("i;${prompt};;${failures}")
diff --git a/expect/backup_gpt.exp b/expect/backup_gpt.exp
new file mode 100644
index 0000000..f455321
--- /dev/null
+++ b/expect/backup_gpt.exp
@@ -0,0 +1,15 @@
+#
+# Copyright (c) 2023 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Expect script for Trusted Firmware Test Framework
+#
+
+source [file join [file dirname [info script]] handle-arguments.inc]
+
+expect_string "Trying to retrieve back-up GPT header"
+
+source [file join [file dirname [info script]] tftf.inc]
+
+exit_uart -1
diff --git a/expect/tftf.exp b/expect/tftf.exp
index 161c171..f14b00a 100644
--- a/expect/tftf.exp
+++ b/expect/tftf.exp
@@ -10,22 +10,6 @@
 
 source [file join [file dirname [info script]] trusted-firmware.inc]
 
-expect_string "Booting trusted firmware test framework" "TFTF is booting"
-expect_re "Running at NS-EL(1|2)"
-
-expect {
-	"Tests Failed  : 0" {
-		expect_string "Exiting tests." "all TFTF tests passed"
-		exit_uart 0
-	}
-	"Tests Passed  : 0" {
-		expect_string "Exiting tests." "no TFTF tests passed"
-		exit_uart -1
-	}
-	-re "Tests Failed  : \[^0]" {
-		expect_string "Exiting tests." "one or more TFTF tests failed"
-		exit_uart -1
-	}
-}
+source [file join [file dirname [info script]] tftf.inc]
 
 exit_uart -1
diff --git a/expect/tftf.inc b/expect/tftf.inc
new file mode 100644
index 0000000..2b671fd
--- /dev/null
+++ b/expect/tftf.inc
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2023 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Expect script for Trusted Firmware Test Framework
+#
+
+expect_string "Booting trusted firmware test framework" "TFTF is booting"
+
+expect_re "Running at NS-EL(1|2)"
+
+expect {
+	"Tests Failed  : 0" {
+		expect_string "Exiting tests." "all TFTF tests passed"
+		exit_uart 0
+	}
+	"Tests Passed  : 0" {
+		expect_string "Exiting tests." "no TFTF tests passed"
+		exit_uart -1
+	}
+	-re "Tests Failed  : \[^0]" {
+		expect_string "Exiting tests." "one or more TFTF tests failed"
+		exit_uart -1
+	}
+}
diff --git a/fvp_utils.sh b/fvp_utils.sh
index 03921f5..888d9a2 100644
--- a/fvp_utils.sh
+++ b/fvp_utils.sh
@@ -506,4 +506,14 @@
     archive_file "fip_gpt.bin"
 }
 
+#corrupt GPT image header and archive it
+corrupt_gpt_bin() {
+    bin="${1:?}"
+
+    # Primary GPT header is present in LBA-1 second block after MBR
+    # empty the primary GPT header forcing to use backup GPT header
+    # and backup GPT entries.
+    dd if=/dev/zero of=$bin bs=512 seek=1 count=1 conv=notrunc
+}
+
 set +u
diff --git a/group/tftf-l2-fvp/fvp-gpt,fvp-default:fvp-tftf.backup_gpt-aemv8a.gpt-debug b/group/tftf-l2-fvp/fvp-gpt,fvp-default:fvp-tftf.backup_gpt-aemv8a.gpt-debug
new file mode 100644
index 0000000..8c2d647
--- /dev/null
+++ b/group/tftf-l2-fvp/fvp-gpt,fvp-default:fvp-tftf.backup_gpt-aemv8a.gpt-debug
@@ -0,0 +1,5 @@
+#
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
diff --git a/run_config/fvp-tftf.backup_gpt b/run_config/fvp-tftf.backup_gpt
new file mode 100644
index 0000000..a920668
--- /dev/null
+++ b/run_config/fvp-tftf.backup_gpt
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+post_tf_build() {
+	build_fip BL33="$archive/tftf.bin"
+}
+
+post_tf_archive() {
+	gen_gpt_bin "$archive/fip.bin"
+
+	# Corrupt primary GPT header to force TF-A to use the backup one
+	corrupt_gpt_bin "$archive/fip_gpt.bin"
+}
+
+generate_lava_job_template() {
+	uart="0" file="backup_gpt.exp" track_expect
+	uart="1" file="hold_uart.exp" track_expect
+
+	payload_type="tftf" gen_yaml_template
+}
diff --git a/tf_config/fvp-gpt b/tf_config/fvp-gpt
index 0572149..ee5c35e 100644
--- a/tf_config/fvp-gpt
+++ b/tf_config/fvp-gpt
@@ -1,3 +1,4 @@
 ARM_GPT_SUPPORT=1
 CROSS_COMPILE=aarch64-none-elf-
 PLAT=fvp
+FVP_TRUSTED_SRAM_SIZE=384