1#!/usr/bin/env python3
2#
3# Copyright 2020 The Hafnium Authors.
4#
5# Use of this source code is governed by a BSD-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/BSD-3-Clause.
8
9import argparse
10import sys
11import os
12import json
13
14"""
15This script aims at generating a json file that contains the artifacts of
16partitions that will execute alongside in the same test setup.
17A partition can be:
18 - Virtual Machine (VM) - to execute in EL1;
19 - Secure Partition (SP) - to execute in S-EL1.
20
21A setup can have multiple VMs and multiple SPs executing alongside.
22The json file shall list the VMs and SPs, such as:
23{
24    "SPs" : [ <SP information>, ... , <SPx Information>],
25    "VMs" : [ <VM information>, ... , <VMx Information>]
26}
27
28Where the information of each partition shall obey the following format:
29{
30     "img" : <path to partition package>.img,
31     "dts" : <path to manifest>.dts
32}
33
34In the arguments of this script provide the path to partition's artifacts
35separated by the character defined as 'ARG_SPLITTER'. Example:
36--sp <path to img>,<path to dts>
37--vm <path to img>,<path to dts>
38"""
39
40ARG_SPLITTER = ','
41ARG_FORMAT = f"<img>{ARG_SPLITTER}<dts>"
42
43def split_partition_arg(sp_arg : str):
44    ret = sp_arg.split(ARG_SPLITTER)
45    if len(ret) != 2:
46        raise Exception(f"Argument should follow format {ARG_FORMAT}")
47    return ret
48
49def partition_info(img, dts):
50    return {"img": img, "dts": dts}
51
52def list_of_partitions(partitions : list):
53    return [partition_info(*split_partition_arg(p)) for p in partitions]
54
55def Main():
56    parser = argparse.ArgumentParser()
57    parser.add_argument("--sp", action="append")
58    parser.add_argument("--vm", action="append")
59    parser.add_argument("--out", action="store", required=True)
60    args = parser.parse_args()
61
62    #Arguments sanity check:
63    if args.vm is None and args.sp is None:
64        raise Exception("Specify at least one VM (--vm) or one SP (--sp)")
65
66    partitions = dict()
67    if args.sp is not None:
68        partitions["SPs"] = list_of_partitions(args.sp)
69    if args.vm is not None:
70        partitions["VMs"] = list_of_partitions(args.vm)
71
72    json.dump(partitions, open(args.out, "w+"))
73    return 0
74
75if __name__ == "__main__":
76    sys.exit(Main())
77