blob: 05f85aad4aa23013357f05d1c3f39a25c4758b0b [file] [log] [blame]
Angel Pons7ec15c82018-11-17 09:47:27 +01001#!/usr/bin/env python2
Patrick Georgi55189c92020-05-10 20:09:31 +02002# SPDX-License-Identifier: GPL-3.0-or-later
Patrick Rudolph03d31422018-05-11 12:28:54 +02003# devicetree_convert Tool to convert a DTB to a static C file
Patrick Rudolph03d31422018-05-11 12:28:54 +02004
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')
Patrick Rudolph03d31422018-05-11 12:28:54 +020017 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")