dtoc.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # SPDX-License-Identifier: GPL-2.0+
  7. #
  8. """Device tree to C tool
  9. This tool converts a device tree binary file (.dtb) into two C files. The
  10. indent is to allow a C program to access data from the device tree without
  11. having to link against libfdt. By putting the data from the device tree into
  12. C structures, normal C code can be used. This helps to reduce the size of the
  13. compiled program.
  14. Dtoc produces two output files:
  15. dt-structs.h - contains struct definitions
  16. dt-platdata.c - contains data from the device tree using the struct
  17. definitions, as well as U-Boot driver definitions.
  18. This tool is used in U-Boot to provide device tree data to SPL without
  19. increasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
  20. options. For more information about the use of this options and tool please
  21. see doc/driver-model/of-plat.txt
  22. """
  23. from optparse import OptionParser
  24. import os
  25. import sys
  26. # Bring in the patman libraries
  27. our_path = os.path.dirname(os.path.realpath(__file__))
  28. sys.path.append(os.path.join(our_path, '../patman'))
  29. import dtb_platdata
  30. if __name__ != "__main__":
  31. pass
  32. parser = OptionParser()
  33. parser.add_option('-d', '--dtb-file', action='store',
  34. help='Specify the .dtb input file')
  35. parser.add_option('--include-disabled', action='store_true',
  36. help='Include disabled nodes')
  37. parser.add_option('-o', '--output', action='store', default='-',
  38. help='Select output filename')
  39. (options, args) = parser.parse_args()
  40. if not args:
  41. raise ValueError('Please specify a command: struct, platdata')
  42. plat = dtb_platdata.DtbPlatdata(options.dtb_file, options)
  43. plat.ScanDtb()
  44. plat.ScanTree()
  45. plat.SetupOutput(options.output)
  46. structs = plat.ScanStructs()
  47. plat.ScanPhandles()
  48. for cmd in args[0].split(','):
  49. if cmd == 'struct':
  50. plat.GenerateStructs(structs)
  51. elif cmd == 'platdata':
  52. plat.GenerateTables()
  53. else:
  54. raise ValueError("Unknown command '%s': (use: struct, platdata)" % cmd)