blob: 827e5a7a6ae32bda4794ab1024a05aae548551a8 [file] [log] [blame]
Angel Pons7ec15c82018-11-17 09:47:27 +01001#!/usr/bin/env python2
Patrick Rudolph03d31422018-05-11 12:28:54 +02002
3# devicetree_convert Tool to convert a DTB to a static C file
4# Copyright (C) 2018 Facebook Inc.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16
17from pyfdt.pyfdt import FdtBlobParse
18import argparse
19
20parser = argparse.ArgumentParser(description='Cavium DTB to C converter')
21parser.add_argument('--indtb', help='Compiled devicetree blob to parse')
22parser.add_argument('--out', help='The file to write')
23parser.add_argument('--verbose', help='Be verbose', action='store_true', default=False)
24args = parser.parse_args()
25
26outfile = None
27if args.out is not None:
28 outfile = open(args.out, 'w')
29 outfile.write("// This file is part of the coreboot project.\n")
30 outfile.write("// This file is automatically generated.\n")
31 outfile.write("// DO NOT EDIT BY HAND.\n\n")
32 outfile.write("#include <bdk-devicetree.h>\n\n")
33 outfile.write("const struct bdk_devicetree_key_value devtree[] = {\n")
34
35with open(args.indtb) as infile:
36 dtb = FdtBlobParse(infile)
37 fdt = dtb.to_fdt()
38 for (path, node) in fdt.resolve_path('/cavium,bdk').walk():
39 if "/" in path:
40 path = path.replace("/", "")
41 if len(node) == 1:
42 for i in node:
43 if type(i) is not unicode:
44 print "%s: Type is not string" % path
45 continue
46 if args.verbose:
47 print "%s = %s" % (path, i)
48 if outfile is not None:
49 outfile.write("{\"%s\", \"%s\"},\n" % (path, i))
50 else:
51 print "%s: Arrays aren't supported" % path
52
53if outfile is not None:
54 outfile.write("{0, 0},\n")
55 outfile.write("};\n")