Angel Pons | 7ec15c8 | 2018-11-17 09:47:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Patrick Rudolph | 03d3142 | 2018-05-11 12:28:54 +0200 | [diff] [blame] | 2 | |
| 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 | |
| 17 | from pyfdt.pyfdt import FdtBlobParse |
| 18 | import argparse |
| 19 | |
| 20 | parser = argparse.ArgumentParser(description='Cavium DTB to C converter') |
| 21 | parser.add_argument('--indtb', help='Compiled devicetree blob to parse') |
| 22 | parser.add_argument('--out', help='The file to write') |
| 23 | parser.add_argument('--verbose', help='Be verbose', action='store_true', default=False) |
| 24 | args = parser.parse_args() |
| 25 | |
| 26 | outfile = None |
| 27 | if 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 | |
| 35 | with 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 | |
| 53 | if outfile is not None: |
| 54 | outfile.write("{0, 0},\n") |
| 55 | outfile.write("};\n") |