dtoc.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python2
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2016 Google, Inc
  5. # Written by Simon Glass <sjg@chromium.org>
  6. #
  7. """Device tree to C tool
  8. This tool converts a device tree binary file (.dtb) into two C files. The
  9. indent is to allow a C program to access data from the device tree without
  10. having to link against libfdt. By putting the data from the device tree into
  11. C structures, normal C code can be used. This helps to reduce the size of the
  12. compiled program.
  13. Dtoc produces two output files:
  14. dt-structs.h - contains struct definitions
  15. dt-platdata.c - contains data from the device tree using the struct
  16. definitions, as well as U-Boot driver definitions.
  17. This tool is used in U-Boot to provide device tree data to SPL without
  18. increasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
  19. options. For more information about the use of this options and tool please
  20. see doc/driver-model/of-plat.txt
  21. """
  22. from optparse import OptionParser
  23. import os
  24. import sys
  25. import unittest
  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. def run_tests(args):
  31. """Run all the test we have for dtoc
  32. Args:
  33. args: List of positional args provided to binman. This can hold a test
  34. name to execute (as in 'binman -t testSections', for example)
  35. """
  36. import test_dtoc
  37. result = unittest.TestResult()
  38. sys.argv = [sys.argv[0]]
  39. test_name = args and args[0] or None
  40. for module in (test_dtoc.TestDtoc,):
  41. if test_name:
  42. try:
  43. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  44. except AttributeError:
  45. continue
  46. else:
  47. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  48. suite.run(result)
  49. print result
  50. for _, err in result.errors:
  51. print err
  52. for _, err in result.failures:
  53. print err
  54. if __name__ != '__main__':
  55. sys.exit(1)
  56. parser = OptionParser()
  57. parser.add_option('-d', '--dtb-file', action='store',
  58. help='Specify the .dtb input file')
  59. parser.add_option('--include-disabled', action='store_true',
  60. help='Include disabled nodes')
  61. parser.add_option('-o', '--output', action='store', default='-',
  62. help='Select output filename')
  63. parser.add_option('-t', '--test', action='store_true', dest='test',
  64. default=False, help='run tests')
  65. (options, args) = parser.parse_args()
  66. # Run our meagre tests
  67. if options.test:
  68. run_tests(args)
  69. else:
  70. dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
  71. options.output)