xref: /aosp_15_r20/external/coreboot/util/cavium/devicetree_convert.py (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/usr/bin/env python2
2# SPDX-License-Identifier: GPL-3.0-or-later
3# devicetree_convert Tool to convert a DTB to a static C file
4
5from pyfdt.pyfdt import FdtBlobParse
6import argparse
7
8parser = argparse.ArgumentParser(description='Cavium DTB to C converter')
9parser.add_argument('--indtb', help='Compiled devicetree blob to parse')
10parser.add_argument('--out', help='The file to write')
11parser.add_argument('--verbose', help='Be verbose', action='store_true', default=False)
12args = parser.parse_args()
13
14outfile = None
15if args.out is not None:
16    outfile = open(args.out, 'w')
17    outfile.write("// This file is automatically generated.\n")
18    outfile.write("// DO NOT EDIT BY HAND.\n\n")
19    outfile.write("#include <bdk-devicetree.h>\n\n")
20    outfile.write("const struct bdk_devicetree_key_value devtree[] = {\n")
21
22with open(args.indtb) as infile:
23    dtb = FdtBlobParse(infile)
24    fdt = dtb.to_fdt()
25    for (path, node) in fdt.resolve_path('/cavium,bdk').walk():
26        if "/" in path:
27            path = path.replace("/", "")
28        if len(node) == 1:
29            for i in node:
30                if type(i) is not unicode:
31                    print "%s: Type is not string" % path
32                    continue
33                if args.verbose:
34                    print "%s = %s" % (path, i)
35                if outfile is not None:
36                    outfile.write("{\"%s\", \"%s\"},\n" % (path, i))
37        else:
38            print "%s: Arrays aren't supported" % path
39
40if outfile is not None:
41    outfile.write("{0, 0},\n")
42    outfile.write("};\n")
43