# Copyright 2021 The Hafnium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/BSD-3-Clause.

import("//build/image/image.gni")
import("//src/arch/${plat_arch}/args.gni")
import("//test/hftest/args.gni")

config("config") {
  include_dirs = [ "inc" ]
}

source_set("ffa_secure_partitions") {
  testonly = true
  public_configs = [
    ":config",
    "//src/arch/${plat_arch}:config",
  ]

  defines = [ "SP2_EL=1" ]
  sources = [ "ffa_secure_partitions.c" ]

  deps = [ "//test/vmapi/common:ffa" ]
}

source_set("ffa_secure_partitions_vhe") {
  testonly = true
  public_configs = [
    ":config",
    "//src/arch/${plat_arch}:config",
  ]

  defines = [ "SP2_EL=0" ]
  sources = [ "ffa_secure_partitions.c" ]

  deps = [ "//test/vmapi/common:ffa" ]
}

# VM that will control execution of tests in a VM-to-SP set-up
vm_kernel("vm_primary") {
  testonly = true
  public_configs = [
    ":config",
    "services/arch/${plat_arch}/secure:config",
    "//src/arch/${plat_arch}:arch_config",
  ]

  sources = [
    "arch_timer.c",
    "dir_msg.c",
    "interrupts.c",
    "memory_sharing.c",
    "notifications.c",
    "power_mgt.c",
    "secure_interrupts.c",
    "setup_and_discovery.c",
    "smccc.c",
  ]

  deps = [
    ":ffa_secure_partitions",
    "//src/arch/${plat_arch}/hftest:interrupts_gicv3",
    "//src/arch/${plat_arch}/hftest:power_mgmt",
    "//test/hftest:hftest_primary_vm",
    "//test/vmapi/arch/${plat_arch}/gicv3:gicv3_setup",
    "//test/vmapi/common:ffa",
    "//test/vmapi/common/arch/aarch64:exception_handler",
    "//test/vmapi/common/arch/aarch64/sp805:wdog",
    "//vmlib",
  ]
}

manifest("vm_primary_manifest") {
  source = "partition_manifest_nwd_primary.dts"
  output = "partition_manifest_nwd_primary.dtb"
}

partition_package("vm_primary_test_package") {
  testonly = true
  pm_offset = "0x1000"
  img_offset = "0x2000"
  files = [ [
        "partition_manifest_nwd_primary.dtb",
        "vm_primary.bin",
        ":vm_primary_manifest",
        ":vm_primary",
      ] ]
  output = "vm_primary_test_package.img"
}

# VM that will control execution of tests in a VM-to-SP set-up with an S-EL0
# partition as Service2
vm_kernel("vm_primary_vhe") {
  testonly = true
  public_configs = [
    ":config",
    "services/arch/${plat_arch}/secure:config",
    "//src/arch/${plat_arch}:arch_config",
  ]

  sources = [
    "arch_timer.c",
    "dir_msg.c",
    "interrupts.c",
    "notifications.c",
    "pauth.c",
    "power_mgt.c",
    "secure_interrupts.c",
    "setup_and_discovery.c",
    "smccc.c",
  ]

  deps = [
    ":ffa_secure_partitions_vhe",
    "//src/arch/${plat_arch}/hftest:interrupts_gicv3",
    "//src/arch/${plat_arch}/hftest:power_mgmt",
    "//test/hftest:hftest_primary_vm",
    "//test/vmapi/arch/${plat_arch}/gicv3:gicv3_setup",
    "//test/vmapi/common:ffa",
    "//test/vmapi/common/arch/aarch64:exception_handler",
    "//test/vmapi/common/arch/aarch64/sp805:wdog",
  ]
}

partition_package("vm_primary_test_package_vhe") {
  testonly = true
  pm_offset = "0x1000"
  img_offset = "0x2000"
  files = [ [
        "partition_manifest_nwd_primary.dtb",
        "vm_primary_vhe.bin",
        ":vm_primary_manifest",
        ":vm_primary_vhe",
      ] ]
  output = "vm_primary_test_package_vhe.img"
}

# VM that will control execution of tests in a VM-to-SP set-up
# VM for running boot tests only
vm_kernel("vm_primary_boot_fail_test_sp1") {
  testonly = true
  public_configs = [
    ":config",
    "services/arch/${plat_arch}/secure:config",
    "//src/arch/${plat_arch}:arch_config",
  ]
  defines = [ "FAILING_SP=1" ]
  sources = [ "boot_fail.c" ]

  deps = [
    ":ffa_secure_partitions",
    "//test/hftest:hftest_primary_vm",
    "//test/vmapi/common:ffa",
  ]
}

# VM that will control execution of tests in a VM-to-SP set-up
# VM for running boot tests only
vm_kernel("vm_primary_boot_fail_test_sp2") {
  testonly = true
  public_configs = [
    ":config",
    "services/arch/${plat_arch}/secure:config",
    "//src/arch/${plat_arch}:arch_config",
  ]
  defines = [ "FAILING_SP=2" ]
  sources = [ "boot_fail.c" ]

  deps = [
    ":ffa_secure_partitions",
    "//test/hftest:hftest_primary_vm",
    "//test/vmapi/common:ffa",
  ]
}

# VM that will control execution of tests in a VM-to-SP set-up
# VM for running boot tests only
vm_kernel("vm_primary_boot_fail_test_sp3") {
  testonly = true
  public_configs = [
    ":config",
    "services/arch/${plat_arch}/secure:config",
    "//src/arch/${plat_arch}:arch_config",
  ]
  defines = [ "FAILING_SP=3" ]
  sources = [ "boot_fail.c" ]

  deps = [
    ":ffa_secure_partitions",
    "//test/hftest:hftest_primary_vm",
    "//test/vmapi/common:ffa",
  ]
}

partition_package("vm_primary_boot_fail_test_package_sp1") {
  testonly = true
  pm_offset = "0x1000"
  img_offset = "0x2000"
  files = [ [
        "partition_manifest_nwd_primary.dtb",
        "vm_primary_boot_fail_test_sp1.bin",
        ":vm_primary_manifest",
        ":vm_primary_boot_fail_test_sp1",
      ] ]
  output = "vm_primary_boot_fail_test_package_sp1.img"
}

partition_package("vm_primary_boot_fail_test_package_sp2") {
  testonly = true
  pm_offset = "0x1000"
  img_offset = "0x2000"
  files = [ [
        "partition_manifest_nwd_primary.dtb",
        "vm_primary_boot_fail_test_sp2.bin",
        ":vm_primary_manifest",
        ":vm_primary_boot_fail_test_sp2",
      ] ]
  output = "vm_primary_boot_fail_test_package_sp2.img"
}

partition_package("vm_primary_boot_fail_test_package_sp3") {
  testonly = true
  pm_offset = "0x1000"
  img_offset = "0x2000"
  files = [ [
        "partition_manifest_nwd_primary.dtb",
        "vm_primary_boot_fail_test_sp3.bin",
        ":vm_primary_manifest",
        ":vm_primary_boot_fail_test_sp3",
      ] ]
  output = "vm_primary_boot_fail_test_package_sp3.img"
}

partitions_json("ffa_both_world_partitions_vhe_test") {
  testonly = true

  sps = [
    [
      "service_sp_first_package.img",
      "services/arch/${plat_arch}/secure/manifest_service_sp_first.dts",
      "services/arch/${plat_arch}/secure:service_sp_first_package",
      "services/arch/${plat_arch}/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_el0_package.img",
      "services/arch/${plat_arch}/secure/el0/manifest_service_sp_second.dts",
      "services/arch/${plat_arch}/secure/el0:service_sp_second_package",
      "services//arch/${plat_arch}/secure/el0:partition_manifest_service_sp_second",
    ],
    [
      "service_sp_third_package.img",
      "services/arch/${plat_arch}/secure/manifest_service_sp_third.dts",
      "services/arch/${plat_arch}/secure:service_sp_third_package",
      "services/arch/${plat_arch}/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_test_package_vhe.img",
        "manifest.dts",
        ":vm_primary_test_package_vhe",
        ":vm_primary_test_package_vhe",
      ] ]

  json_file = "ffa_both_world_partitions_vhe_test.json"
}

partitions_json("ffa_both_world_partitions_test") {
  testonly = true

  sps = [
    [
      "service_sp_first_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_first.dts",
      "services/arch/aarch64/secure:service_sp_first_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_second.dts",
      "services/arch/aarch64/secure:service_sp_second_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_second",
    ],
    [
      "service_sp_third_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_third.dts",
      "services/arch/aarch64/secure:service_sp_third_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_test_package.img",
        "manifest.dts",
        ":vm_primary_test_package",
        ":vm_primary_test_package",
      ] ]

  json_file = "ffa_both_world_partitions_test.json"
}

partitions_json("ffa_both_world_partitions_boot_fail_test_sp1") {
  testonly = true

  sps = [
    [
      "service_sp_first_package_boot_fail_test.img",
      "services/arch/aarch64/secure/manifest_service_sp_first.dts",
      "services/arch/aarch64/secure:service_sp_first_package_boot_fail_test",
      "services/arch/aarch64/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_second.dts",
      "services/arch/aarch64/secure:service_sp_second_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_second",
    ],
    [
      "service_sp_third_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_third.dts",
      "services/arch/aarch64/secure:service_sp_third_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_boot_fail_test_package_sp1.img",
        "manifest.dts",
        ":vm_primary_boot_fail_test_package_sp1",
        ":vm_primary_boot_fail_test_package_sp1",
      ] ]

  json_file = "ffa_both_world_partitions_boot_fail_test_sp1.json"
}

partitions_json("ffa_both_world_partitions_boot_fail_test_sp2") {
  testonly = true

  sps = [
    [
      "service_sp_first_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_first.dts",
      "services/arch/aarch64/secure:service_sp_first_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_package_boot_fail_test.img",
      "services/arch/aarch64/secure/manifest_service_sp_second.dts",
      "services/arch/aarch64/secure:service_sp_second_package_boot_fail_test",
      "services/arch/aarch64/secure:partition_manifest_service_sp_second",
    ],
    [
      "service_sp_third_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_third.dts",
      "services/arch/aarch64/secure:service_sp_third_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_boot_fail_test_package_sp2.img",
        "manifest.dts",
        ":vm_primary_boot_fail_test_package_sp2",
        ":vm_primary_boot_fail_test_package_sp2",
      ] ]

  json_file = "ffa_both_world_partitions_boot_fail_test_sp2.json"
}

partitions_json("ffa_both_world_partitions_boot_fail_test_sp3") {
  testonly = true

  sps = [
    [
      "service_sp_first_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_first.dts",
      "services/arch/aarch64/secure:service_sp_first_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_second.dts",
      "services/arch/aarch64/secure:service_sp_second_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_second",
    ],
    [
      "service_sp_third_package_boot_fail_test.img",
      "services/arch/aarch64/secure/manifest_service_sp_third.dts",
      "services/arch/aarch64/secure:service_sp_third_package_boot_fail_test",
      "services/arch/aarch64/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_boot_fail_test_package_sp3.img",
        "manifest.dts",
        ":vm_primary_boot_fail_test_package_sp3",
        ":vm_primary_boot_fail_test_package_sp3",
      ] ]

  json_file = "ffa_both_world_partitions_boot_fail_test_sp3.json"
}

# This test setup differs from ffa_both_world_partitions_test with Second
# service being an UP S-EL1 partition.
partitions_json("ffa_both_world_partitions_sel1_up_test") {
  testonly = true

  sps = [
    [
      "service_sp_first_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_first.dts",
      "services/arch/aarch64/secure:service_sp_first_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_first",
    ],
    [
      "service_sp_second_package_up.img",
      "services/arch/aarch64/secure/manifest_service_sp_second_up.dts",
      "services/arch/aarch64/secure:service_sp_second_package_up",
      "services/arch/aarch64/secure:partition_manifest_service_sp_second_up",
    ],
    [
      "service_sp_third_package.img",
      "services/arch/aarch64/secure/manifest_service_sp_third.dts",
      "services/arch/aarch64/secure:service_sp_third_package",
      "services/arch/aarch64/secure:partition_manifest_service_sp_third",
    ],
  ]

  vms = [ [
        "vm_primary_test_package.img",
        "manifest.dts",
        ":vm_primary_test_package",
        ":vm_primary_test_package",
      ] ]

  json_file = "ffa_both_world_partitions_sel1_up_test.json"
}
