Angel Pons | 7ec15c8 | 2018-11-17 09:47:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Patrick Georgi | 55189c9 | 2020-05-10 20:09:31 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-3.0-or-later |
Patrick Rudolph | 03d3142 | 2018-05-11 12:28:54 +0200 | [diff] [blame] | 3 | # devicetree_convert Tool to convert a DTB to a static C file |
Patrick Rudolph | 03d3142 | 2018-05-11 12:28:54 +0200 | [diff] [blame] | 4 | |
| 5 | from pyfdt.pyfdt import FdtBlobParse |
| 6 | import argparse |
| 7 | |
| 8 | parser = argparse.ArgumentParser(description='Cavium DTB to C converter') |
| 9 | parser.add_argument('--indtb', help='Compiled devicetree blob to parse') |
| 10 | parser.add_argument('--out', help='The file to write') |
| 11 | parser.add_argument('--verbose', help='Be verbose', action='store_true', default=False) |
| 12 | args = parser.parse_args() |
| 13 | |
| 14 | outfile = None |
| 15 | if args.out is not None: |
| 16 | outfile = open(args.out, 'w') |
Patrick Rudolph | 03d3142 | 2018-05-11 12:28:54 +0200 | [diff] [blame] | 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 | |
| 22 | with 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 | |
| 40 | if outfile is not None: |
| 41 | outfile.write("{0, 0},\n") |
| 42 | outfile.write("};\n") |