n1sdp: Add support for n1sdp in ci

Build scp and tf-a and test the artefacts on n1sdp in LAVA.

Change-Id: Iabe1bc65c5eb18f3204ec732fa259e7e6a4351bc
Signed-off-by: Khasim Syed Mohammed <khasim.mohammed@arm.com>
diff --git a/group/scp-boot-tests/n1sdp,n1sdp-default:n1sdp-linux-fip-firmware b/group/scp-boot-tests/n1sdp,n1sdp-default:n1sdp-linux-fip-firmware
new file mode 100644
index 0000000..1bbc737
--- /dev/null
+++ b/group/scp-boot-tests/n1sdp,n1sdp-default:n1sdp-linux-fip-firmware
@@ -0,0 +1,5 @@
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
diff --git a/job/tf-worker/is_n1sdp_config.sh b/job/tf-worker/is_n1sdp_config.sh
new file mode 100755
index 0000000..cc3a988
--- /dev/null
+++ b/job/tf-worker/is_n1sdp_config.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+set -e
+
+# For n1sdp runs, we need to let the board download build artefacts using a URL.
+# The only way to have a board-accessible URL at the moment is to have build
+# artefacts archived. Therefore, for n1sdp we spawn the build as a separate job
+if echo "$RUN_CONFIG" | grep -iqe '^n1sdp'; then
+        exit 0
+else
+        exit 1
+fi
diff --git a/job/tf-worker/run_lava.py b/job/tf-worker/run_lava.py
index d0c2f87..47e99eb 100644
--- a/job/tf-worker/run_lava.py
+++ b/job/tf-worker/run_lava.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2021 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -58,6 +58,20 @@
 
 
 if __name__ == "__main__":
+
+    # To deploy and boot the artefacts on a board in LAVA a platform specific
+    # yaml file should be dispatched to LAVA. The below logic will identify
+    # the name of the yaml file at run time for the platform defined in run_cfg.
+    platform_list = ['n1sdp', 'juno']
+
+    run_cfg = os.environ["RUN_CONFIG"]
+    res = [i for i in platform_list if i in run_cfg]
+    if res:
+        platform_yaml=''.join(res)+'.yaml'
+    else:
+        logging.critical("Exiting: Platform not found for LAVA in run-config %s", os.environ["RUN_CONFIG"])
+        sys.exit(-1)
+
     parser = argparse.ArgumentParser(
         description="Lava job runner with infrastructure error dectection and retry."
     )
@@ -70,7 +84,7 @@
     parser.add_argument(
         "job",
         nargs="?",
-        default=os.path.join("artefacts", os.environ["BIN_MODE"], "juno.yaml"),
+        default=os.path.join("artefacts", os.environ["BIN_MODE"], platform_yaml),
         help="the Lava job description file",
     )
     parser.add_argument(
diff --git a/n1sdp_utils.sh b/n1sdp_utils.sh
new file mode 100644
index 0000000..e9ea979
--- /dev/null
+++ b/n1sdp_utils.sh
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+source "$ci_root/fvp_utils.sh"
+
+n1sdp_prebuilts=${n1sdp_prebuilts:="$tfa_downloads/css/n1sdp"}
+
+get_n1sdp_firmware() {
+        url=$n1sdp_firmware_bin_url saveas="n1sdp-board-firmware.zip" fetch_file
+        archive_file "n1sdp-board-firmware.zip"
+}
+
+gen_recovery_image_n1sdp() {
+        local zip_dir="$workspace/$mode/n1sdp-board-firmware_primary"
+        local zip_file="${zip_dir}.zip"
+
+        mkdir -p "$zip_dir"
+
+        extract_tarball "$archive/n1sdp-board-firmware.zip" "$zip_dir"
+
+        cp -Rp --no-preserve=ownership "$archive/mcp_fw.bin" "$zip_dir/SOFTWARE"
+        cp -Rp --no-preserve=ownership "$archive/mcp_rom.bin" "$zip_dir/SOFTWARE"
+        cp -Rp --no-preserve=ownership "$archive/scp_fw.bin" "$zip_dir/SOFTWARE"
+        cp -Rp --no-preserve=ownership "$archive/scp_rom.bin" "$zip_dir/SOFTWARE"
+
+        (cd "$zip_dir" && zip -rq "$zip_file" -- *)
+
+        archive_file "$zip_file"
+}
+
+gen_n1sdp_yaml() {
+        local yaml_file="$workspace/n1sdp.yaml"
+        local job_file="$workspace/job.yaml"
+        local payload_type="${payload_type:?}"
+
+        bin_mode="$mode" \
+                "$ci_root/script/gen_n1sdp_${payload_type}_yaml.sh" > "$yaml_file"
+
+        cp "$yaml_file" "$job_file"
+        archive_file "$yaml_file"
+        archive_file "$job_file"
+}
diff --git a/run_config/n1sdp-fip b/run_config/n1sdp-fip
new file mode 100644
index 0000000..a609547
--- /dev/null
+++ b/run_config/n1sdp-fip
@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+post_tf_build() {
+        # Create FIP for SCP
+        "$fiptool" create --soc-fw "$tf_root/build/n1sdp/$bin_mode/bl31.bin" --scp-fw "$scp_root/build/product/n1sdp/scp_ramfw/$mode/bin/scp_ramfw.bin" "scp_fw.bin"
+        archive_file "scp_fw.bin"
+
+        # Create FIP for MCP, this needs fixed uuid for now
+        "$fiptool" create --blob uuid=54464222-a4cf-4bf8-b1b6-cee7dade539e,file="$scp_root/build/product/n1sdp/mcp_ramfw/$mode/bin/mcp_ramfw.bin" "mcp_fw.bin"
+        archive_file "mcp_fw.bin"
+}
diff --git a/run_config/n1sdp-firmware b/run_config/n1sdp-firmware
new file mode 100644
index 0000000..02bcf3a
--- /dev/null
+++ b/run_config/n1sdp-firmware
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+fetch_tf_resource() {
+        n1sdp_firmware_bin_url="$n1sdp_prebuilts/n1sdp-board-firmware.zip" get_n1sdp_firmware
+}
diff --git a/run_config/n1sdp-linux b/run_config/n1sdp-linux
new file mode 100644
index 0000000..b50417a
--- /dev/null
+++ b/run_config/n1sdp-linux
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+post_fetch_tf_resource() {
+        gen_recovery_image_n1sdp "$archive/n1sdp-board-firmware.zip"
+        payload_type="linux" gen_n1sdp_yaml
+}
diff --git a/scp_config/n1sdp b/scp_config/n1sdp
new file mode 100644
index 0000000..555dffd
--- /dev/null
+++ b/scp_config/n1sdp
@@ -0,0 +1,3 @@
+CC=/arm/pdsw/downloads/scp-models/tools/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc
+PRODUCT=n1sdp
+LOG_LEVEL=INFO
diff --git a/script/gen_n1sdp_linux_yaml.sh b/script/gen_n1sdp_linux_yaml.sh
new file mode 100755
index 0000000..ccbf2ec
--- /dev/null
+++ b/script/gen_n1sdp_linux_yaml.sh
@@ -0,0 +1,217 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# Generate a YAML file in order to dispatch N1SDP runs on LAVA. Note that this
+# script would produce a meaningful output when run via. Jenkins
+#
+# $bin_mode must be set. This script outputs to STDOUT
+
+ci_root="$(readlink -f "$(dirname "$0")/..")"
+source "$ci_root/utils.sh"
+source "$ci_root/n1sdp_utils.sh"
+
+get_recovery_image_url() {
+        local build_job="tf-build"
+        local bin_mode="${bin_mode:?}"
+
+        if upon "$jenkins_run"; then
+                echo "$jenkins_url/job/$JOB_NAME/$BUILD_NUMBER/artifact/artefacts/$bin_mode/n1sdp-board-firmware_primary.zip"
+        else
+                echo "file://$workspace/artefacts/$bin_mode/n1sdp-board-firmware_primary.zip"
+        fi
+}
+
+recovery_img_url="${recovery_img_url:-$(get_recovery_image_url)}"
+
+cat <<EOF
+device_type: n1sdp
+job_name: tf-n1sdp
+timeouts:
+  job:
+    minutes: 30
+
+priority: medium
+visibility: public
+context:
+  extra_nfsroot_args: ',vers=3'
+  extra_kernel_args: rootwait
+actions:
+#
+# Any firmware bundle deployed must be configured to boot automatically without
+# intervention. This means "PMIC_FORCE" must be set "TRUE" in the config file
+# to be deployed.
+#
+#
+# Deploy a firmware bundle with the customised "uefi.bin" installed. This
+# enables an EFI network driver, allowing us to force a TFTP boot from GRUB (assuming cobbler is setup)
+#
+- deploy:
+    namespace: recovery
+    to: flasher
+    images:
+      recovery_image:
+        url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/n1sdp-board-firmware-force-netboot.zip
+        compression: zip
+
+- deploy:
+    namespace: debian
+    to: tftp
+    os: debian
+    kernel:
+      url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/debian/linux
+      type: image
+    ramdisk:
+      url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/debian/ramdisk.img
+    nfsrootfs:
+      url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/debian/debian-buster-arm64-rootfs.tar.xz
+      compression: xz
+
+- boot:
+    namespace: recovery
+    timeout:
+      minutes: 3
+    method: minimal
+    parameters:
+      kernel-start-message: ''
+    prompts: ['Cmd>']
+
+- boot:
+    namespace: uart1
+    method: new_connection
+    connection: uart1
+
+- boot:
+    namespace: debian
+    connection-namespace: uart1
+    timeout:
+      minutes: 5
+    method: grub
+    commands: nfs
+    prompts:
+      - '/ # '
+
+- test:
+    namespace: debian
+    timeout:
+      minutes: 5
+    definitions:
+      - repository:
+          metadata:
+            format: Lava-Test Test Definition 1.0
+            name: device-network
+            description: '"Test device network connection"'
+            os:
+              - debian
+            scope:
+              - functional
+          run:
+            steps:
+              - apt -q update
+              - apt -q install -y iputils-ping
+              - ping -c 5 10.6.43.131 || lava-test-raise "Device failed to reach a remote host"
+              - hostname -I
+        from: inline
+        name: device-network
+        path: inline/device-network.yaml
+
+- test:
+    namespace: debian
+    timeout:
+      minutes: 5
+    definitions:
+      - repository:
+          metadata:
+            format: Lava-Test Test Definition 1.0
+            name: install-dependancies
+            description: '"Install dependancies for secondary media deployment"'
+            os:
+              - debian
+            scope:
+              - functional
+          run:
+            steps:
+              - apt-get update -q
+              - apt-get install -qy bmap-tools
+        from: inline
+        name: install-dependancies
+        path: inline/install-dependancies.yaml
+
+- deploy:
+    namespace: secondary_media
+    connection-namespace: uart1
+    timeout:
+      minutes: 10
+    to: usb
+    os: oe
+    images:
+      image:
+        url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/secondary/core-image-minimal-n1sdp.wic.gz
+        compression: gz
+      bmap:
+        url: http://files.oss.arm.com/downloads/lava/health-checks/n1sdp/4/secondary/core-image-minimal-n1sdp.wic.bmap
+    uniquify: false
+    device: usb_storage_device
+    writer:
+      tool: /usr/bin/bmaptool
+      options: copy {DOWNLOAD_URL} {DEVICE}
+      prompt: 'bmaptool: info'
+    tool:
+      prompts: ['copying time: [0-9ms\.\ ]+, copying speed [0-9\.]+ MiB\/sec']
+
+#
+# Deploy the primary board firmware bundle (this time without the additinal
+# network driver).
+#
+- deploy:
+    namespace: recovery
+    to: flasher
+    images:
+      recovery_image:
+        url: $recovery_img_url
+        compression: zip
+
+#
+# Do not verify the flash second time around as cached serial output on the
+# connection will immediately match the prompt.
+#
+- boot:
+    namespace: secondary_media
+    timeout:
+      minutes: 10
+    method: minimal
+    auto_login:
+      login_prompt: '(.*)login:'
+      username: root
+    prompts:
+      - 'root@(.*):~#'
+    transfer_overlay:
+      download_command: wget -S
+      unpack_command: tar -C / -xzf
+
+- test:
+    namespace: secondary_media
+    timeout:
+      minutes: 5
+    definitions:
+      - repository:
+          metadata:
+            format: Lava-Test Test Definition 1.0
+            name: linux-console-test-in-deployed-image
+            description: '"Run LAVA test steps inside the deployed image"'
+            os:
+              - oe
+            scope:
+              - functional
+          run:
+            steps:
+              - fdisk -l
+              - ip addr show
+              - cat /proc/cpuinfo
+        from: inline
+        name: linux-console-test
+        path: inline/linux-console-test.yaml
+EOF
diff --git a/tf_config/n1sdp-default b/tf_config/n1sdp-default
index 4125932..aa2ef2b 100644
--- a/tf_config/n1sdp-default
+++ b/tf_config/n1sdp-default
@@ -1,2 +1,3 @@
 CROSS_COMPILE=aarch64-none-elf-
 PLAT=n1sdp
+SCP_BL2=/dev/null