dtoc.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python2
  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. import unittest
  27. # Bring in the patman libraries
  28. our_path = os.path.dirname(os.path.realpath(__file__))
  29. sys.path.append(os.path.join(our_path, '../patman'))
  30. import dtb_platdata
  31. def run_tests():
  32. """Run all the test we have for dtoc"""
  33. import test_dtoc
  34. result = unittest.TestResult()
  35. sys.argv = [sys.argv[0]]
  36. for module in (test_dtoc.TestDtoc,):
  37. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  38. suite.run(result)
  39. print result
  40. for _, err in result.errors:
  41. print err
  42. for _, err in result.failures:
  43. print err
  44. if __name__ != '__main__':
  45. sys.exit(1)
  46. parser = OptionParser()
  47. parser.add_option('-d', '--dtb-file', action='store',
  48. help='Specify the .dtb input file')
  49. parser.add_option('--include-disabled', action='store_true',
  50. help='Include disabled nodes')
  51. parser.add_option('-o', '--output', action='store', default='-',
  52. help='Select output filename')
  53. parser.add_option('-t', '--test', action='store_true', dest='test',
  54. default=False, help='run tests')
  55. (options, args) = parser.parse_args()
  56. # Run our meagre tests
  57. if options.test:
  58. run_tests()
  59. else:
  60. dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
  61. options.output)