Support a board-specific manifest overlay for hftests

When an Android boards needs to break out of the hftest test-reboot
loop, it may need more permissions than granted to the primary VM
in the generic hftest manifests. For example, the primary VM may need to
perform SMC calls not included in the SMC whitelist.

To this end, support specifying a manifest overlay in toolchain_args and
use it when generating test initrds.

Change-Id: I5e0de4c25a6ae286b4040825a9d17e471b735134
diff --git a/build/image/image.gni b/build/image/image.gni
index 016546c..8004a8b 100644
--- a/build/image/image.gni
+++ b/build/image/image.gni
@@ -211,6 +211,31 @@
   }
 }
 
+template("fdt_overlay") {
+  action(target_name) {
+    forward_variables_from(invoker,
+                           [
+                             "testonly",
+                             "deps",
+                           ])
+    script = "//build/image/dtc.py"
+
+    sources = [
+      invoker.base,
+      invoker.overlay,
+    ]
+    outputs = [
+      invoker.output,
+    ]
+    args = [
+      "overlay",
+      rebase_path(outputs[0]),
+      rebase_path(sources[0]),
+      rebase_path(sources[1]),
+    ]
+  }
+}
+
 template("incbin") {
   source_set(target_name) {
     forward_variables_from(invoker,
@@ -255,10 +280,41 @@
   manifest_target = "${target_name}__manifest"
   manifest_file = "${base_out_dir}/manifest.dtb"
 
-  # Generate manifest.dtbo
-  device_tree(manifest_target) {
-    source = invoker.manifest
-    output = manifest_file
+  # Check if the invoker specified an overlay for the manifest.
+  if (defined(invoker.manifest_overlay) && invoker.manifest_overlay != "") {
+    manifest_base_target = "${target_name}__manifest_base"
+    manifest_base_file = "${base_out_dir}/manifest_base.dtb"
+    manifest_overlay_target = "${target_name}__manifest_overlay"
+    manifest_overlay_file = "${base_out_dir}/manifest_overlay.dtbo"
+
+    # Compile the base manifest.
+    device_tree(manifest_base_target) {
+      source = invoker.manifest
+      output = manifest_base_file
+    }
+
+    # Compile the manifest overlay.
+    device_tree(manifest_overlay_target) {
+      source = invoker.manifest_overlay
+      output = manifest_overlay_file
+    }
+
+    # Merge the base manifest and the overlay, producing the final manifest.
+    fdt_overlay(manifest_target) {
+      base = manifest_base_file
+      overlay = manifest_overlay_file
+      output = manifest_file
+      deps = [
+        ":$manifest_base_target",
+        ":$manifest_overlay_target",
+      ]
+    }
+  } else {
+    # No overlay specified. Compile the manifest to DTB.
+    device_tree(manifest_target) {
+      source = invoker.manifest
+      output = manifest_file
+    }
   }
 
   action(target_name) {
diff --git a/test/hftest/BUILD.gn b/test/hftest/BUILD.gn
index b11a50b..054a319 100644
--- a/test/hftest/BUILD.gn
+++ b/test/hftest/BUILD.gn
@@ -13,11 +13,7 @@
 # limitations under the License.
 
 import("//build/toolchain/platform.gni")
-
-declare_args() {
-  hftest_ctrl = ":ctrl_fdt"
-  hftest_device = ":device_psci"
-}
+import("//test/hftest/args.gni")
 
 config("hftest_config") {
   include_dirs = [ "//test/inc" ]
diff --git a/test/hftest/args.gni b/test/hftest/args.gni
new file mode 100644
index 0000000..89bf3a6
--- /dev/null
+++ b/test/hftest/args.gni
@@ -0,0 +1,19 @@
+# Copyright 2020 The Hafnium Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+declare_args() {
+  hftest_ctrl = ":ctrl_fdt"
+  hftest_device = ":device_psci"
+  hftest_manifest_overlay = ""
+}
diff --git a/test/linux/BUILD.gn b/test/linux/BUILD.gn
index 0be5630..0807b2f 100644
--- a/test/linux/BUILD.gn
+++ b/test/linux/BUILD.gn
@@ -14,6 +14,7 @@
 
 import("//build/image/image.gni")
 import("//build/toolchain/platform.gni")
+import("//test/hftest/args.gni")
 
 executable("test_binary") {
   include_dirs = [
@@ -58,6 +59,7 @@
   testonly = true
 
   manifest = "manifest.dts"
+  manifest_overlay = hftest_manifest_overlay
   primary_name = "vmlinuz"
   primary_vm = "//third_party/linux:linux__prebuilt"
   primary_initrd = ":linux_test_initrd"
diff --git a/test/vmapi/arch/aarch64/BUILD.gn b/test/vmapi/arch/aarch64/BUILD.gn
index 8441677..c4ebc61 100644
--- a/test/vmapi/arch/aarch64/BUILD.gn
+++ b/test/vmapi/arch/aarch64/BUILD.gn
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import("//build/image/image.gni")
+import("//test/hftest/args.gni")
 
 group("arch") {
   testonly = true
@@ -43,6 +44,7 @@
   testonly = true
 
   manifest = "manifest.dts"
+  manifest_overlay = hftest_manifest_overlay
   primary_name = "aarch64_test"
   primary_vm = ":aarch64_test_vm"
 }
diff --git a/test/vmapi/arch/aarch64/gicv3/BUILD.gn b/test/vmapi/arch/aarch64/gicv3/BUILD.gn
index 57ceb99..490f65b 100644
--- a/test/vmapi/arch/aarch64/gicv3/BUILD.gn
+++ b/test/vmapi/arch/aarch64/gicv3/BUILD.gn
@@ -14,6 +14,7 @@
 
 import("//build/image/image.gni")
 import("//build/toolchain/platform.gni")
+import("//test/hftest/args.gni")
 
 config("config") {
   include_dirs = [ "inc" ]
@@ -43,6 +44,7 @@
   testonly = true
 
   manifest = "manifest.dts"
+  manifest_overlay = hftest_manifest_overlay
   primary_name = "gicv3_test"
   primary_vm = ":gicv3_test_vm"
   secondary_vms = [ [
diff --git a/test/vmapi/primary_only/BUILD.gn b/test/vmapi/primary_only/BUILD.gn
index cdb35b9..bb084de 100644
--- a/test/vmapi/primary_only/BUILD.gn
+++ b/test/vmapi/primary_only/BUILD.gn
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import("//build/image/image.gni")
+import("//test/hftest/args.gni")
 
 # Tests with no secondary VMs.
 vm_kernel("primary_only_test_vm") {
@@ -31,6 +32,7 @@
 initrd("primary_only_test") {
   testonly = true
   manifest = "manifest.dts"
+  manifest_overlay = hftest_manifest_overlay
   primary_name = "primary_only_test"
   primary_vm = ":primary_only_test_vm"
 }
diff --git a/test/vmapi/primary_with_secondaries/BUILD.gn b/test/vmapi/primary_with_secondaries/BUILD.gn
index ee3ab92..b0c3885 100644
--- a/test/vmapi/primary_with_secondaries/BUILD.gn
+++ b/test/vmapi/primary_with_secondaries/BUILD.gn
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import("//build/image/image.gni")
+import("//test/hftest/args.gni")
 
 config("config") {
   include_dirs = [ "inc" ]
@@ -54,6 +55,7 @@
   testonly = true
 
   manifest = "manifest.dts"
+  manifest_overlay = hftest_manifest_overlay
 
   primary_name = "primary_with_secondaries_test"
   primary_vm = ":primary_with_secondaries_test_vm"