1#!/usr/bin/env python3
2#
3# Copyright 2019 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
9"""Wrapper around Device Tree Compiler (dtc)"""
10
11import argparse
12import os
13import subprocess
14import sys
15
16DTC = "dtc"
17FDTOVERLAY = "fdtoverlay"
18
19def cmd_compile(args):
20    exec_args = [
21            DTC,
22            "-I", "dts", "-O", "dtb",
23            "--out-version", "17",
24        ]
25
26    if args.output_file:
27        exec_args += [ "-o", args.output_file ]
28    if args.input_file:
29        exec_args += [ args.input_file ]
30
31    return subprocess.call(exec_args)
32
33def cmd_overlay(args):
34    exec_args = [
35            FDTOVERLAY,
36            "-i", args.base_dtb,
37            "-o", args.output_dtb,
38        ] + args.overlay_dtb
39    return subprocess.call(exec_args)
40
41def main():
42    parser = argparse.ArgumentParser()
43    subparsers = parser.add_subparsers(dest="command")
44
45    parser_compile = subparsers.add_parser("compile", help="compile DTS to DTB")
46    parser_compile.add_argument("-i", "--input-file")
47    parser_compile.add_argument("-o", "--output-file")
48
49    parser_overlay = subparsers.add_parser("overlay", help="merge DTBs")
50    parser_overlay.add_argument("output_dtb")
51    parser_overlay.add_argument("base_dtb")
52    parser_overlay.add_argument("overlay_dtb", nargs='*')
53
54    args = parser.parse_args()
55
56    if args.command == "compile":
57        return cmd_compile(args)
58    elif args.command == "overlay":
59        return cmd_overlay(args)
60    else:
61        raise Error("Unknown command: {}".format(args.command))
62
63if __name__ == "__main__":
64    sys.exit(main())
65