Introduce a DT-based manifest

These are first steps towards a new manifest format. A new "device_tree"
build target is introduced to compile DTS files to DTB, and
`generate_initrd.py` now does not produce a "vms.txt" file. Instead
"initrd" targets are expected to provide a path to a DTS manifest in the
format:

    /dts-v1/;

    / {
      hypervisor {
        vm1 {
	  debug_name = "primary";
	};

	vm2 {
	  debug_name = "secondary1";
	  kernel_filename = "filename";
	  vcpu_count = <N>;
          mem_size = <M>;
	};

	...
      };
    };

The information provided in the manifest matches "vms.txt".

Bug: 117551352
Test: manifest_test.cc
Test: used by hftest
Change-Id: I6b70bd44d2b110c4f7a6b971018c834084b6d8c4
diff --git a/build/image/dtc.py b/build/image/dtc.py
new file mode 100644
index 0000000..49eeeb7
--- /dev/null
+++ b/build/image/dtc.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+#
+# Copyright 2019 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.
+
+"""Wrapper around Device Tree Compiler (dtc)"""
+
+import argparse
+import os
+import subprocess
+import sys
+
+DTC = "dtc"
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("input_file")
+    parser.add_argument("output_file")
+    args = parser.parse_args()
+
+    return subprocess.call([
+        DTC,
+        "-I", "dts", "-O", "dtb",
+        "-o", args.output_file,
+        "--out-version", "17",
+        args.input_file
+        ])
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/build/image/generate_initrd.py b/build/image/generate_initrd.py
index 6fb76de..8342cfb 100644
--- a/build/image/generate_initrd.py
+++ b/build/image/generate_initrd.py
@@ -29,17 +29,27 @@
 
 def Main():
     parser = argparse.ArgumentParser()
+    parser.add_argument("--manifest", required=True)
     parser.add_argument("--primary_vm", required=True)
     parser.add_argument("--primary_vm_initrd")
     parser.add_argument(
         "--secondary_vm",
         action="append",
-        nargs=4,
-        metavar=("MEMORY", "CORES", "NAME", "IMAGE"))
+        nargs=2,
+        metavar=("NAME", "IMAGE"))
     parser.add_argument("--staging", required=True)
     parser.add_argument("--output", required=True)
     args = parser.parse_args()
     staged_files = ["vmlinuz", "initrd.img"]
+
+    # Create staging folder if needed.
+    if not os.path.isdir(args.staging):
+        os.makedirs(args.staging)
+
+    # Prepare the manifest.
+    if args.manifest:
+        shutil.copyfile(args.manifest, os.path.join(args.staging, "manifest.dtb"))
+        staged_files += ["manifest.dtb"]
     # Prepare the primary VM image.
     shutil.copyfile(args.primary_vm, os.path.join(args.staging, "vmlinuz"))
     # Prepare the primary VM's initrd.
@@ -48,14 +58,11 @@
     else:
         open(os.path.join(args.staging, "initrd.img"), "w").close()
     # Prepare the secondary VMs.
-    with open(os.path.join(args.staging, "vms.txt"), "w") as vms_txt:
-        staged_files.append("vms.txt")
-        if args.secondary_vm:
-            for vm in args.secondary_vm:
-                (vm_memory, vm_cores, vm_name, vm_image) = vm
-                staged_files.append(vm_name)
-                shutil.copy(vm_image, os.path.join(args.staging, vm_name))
-                vms_txt.write("{} {} {}\n".format(vm_memory, vm_cores, vm_name))
+    if args.secondary_vm:
+        for vm in args.secondary_vm:
+            (vm_name, vm_image) = vm
+            staged_files.append(vm_name)
+            shutil.copy(vm_image, os.path.join(args.staging, vm_name))
     # Package files into an initial RAM disk.
     with open(args.output, "w") as initrd:
         # Move into the staging directory so the file names taken by cpio don't
diff --git a/build/image/image.gni b/build/image/image.gni
index fbaf14b..bed73df 100644
--- a/build/image/image.gni
+++ b/build/image/image.gni
@@ -148,11 +148,41 @@
   }
 }
 
+template("device_tree") {
+  action_foreach(target_name) {
+    forward_variables_from(invoker,
+                           [
+                             "testonly",
+                             "sources",
+                             "deps",
+                           ])
+    script = "//build/image/dtc.py"
+
+    dtb_file = "${target_out_dir}/{{source_name_part}}.dtb"
+
+    outputs = [
+      dtb_file,
+    ]
+    args = [
+      "{{source}}",
+      rebase_path(dtb_file),
+    ]
+  }
+}
+
 # Build the initial RAM disk for the hypervisor.
 template("initrd") {
   assert(defined(invoker.primary_vm),
          "initrd() must specify a \"primary_vm\" value")
 
+  manifest_target = "${target_name}__manifest"
+
+  device_tree(manifest_target) {
+    sources = [
+      invoker.manifest,
+    ]
+  }
+
   action(target_name) {
     forward_variables_from(invoker, [ "testonly" ])
     script = "//build/image/generate_initrd.py"
@@ -189,27 +219,29 @@
     # Add the info about the secondary VMs. The information about the VMs is
     # encoded in lists with the following elements:
     #
-    #    1. Memory in bytes.
-    #    2. Number of cores.
-    #    3. File name for the VM image.
-    #    4. Build target for the VM.
+    #    1. File name for the VM image.
+    #    2. Build target for the VM.
     if (defined(invoker.secondary_vms)) {
       foreach(vm, invoker.secondary_vms) {
-        deps += [ vm[3] ]
+        deps += [ vm[1] ]
         args += [
           "--secondary_vm",
           vm[0],
-          vm[1],
-          vm[2],
-          rebase_path(get_label_info(vm[3], "target_out_dir") + "/" +
-                      get_label_info(vm[3], "name") + ".bin"),
+          rebase_path(get_label_info(vm[1], "target_out_dir") + "/" +
+                      get_label_info(vm[1], "name") + ".bin"),
         ]
       }
     }
 
+    manifest_target_outputs = get_target_outputs(":${manifest_target}")
+    deps += [ ":${manifest_target}" ]
+    args += [
+      "--manifest",
+      rebase_path(manifest_target_outputs[0]),
+    ]
+
     outputs = [
       initrd_file,
-      "${initrd_staging}/vms.txt",
     ]
   }
 }