dtoc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # Bring in the libfdt module
  30. sys.path.insert(0, 'scripts/dtc/pylibfdt')
  31. sys.path.insert(0, os.path.join(our_path,
  32. '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
  33. import dtb_platdata
  34. import test_util
  35. def run_tests(args):
  36. """Run all the test we have for dtoc
  37. Args:
  38. args: List of positional args provided to dtoc. This can hold a test
  39. name to execute (as in 'dtoc -t test_empty_file', for example)
  40. """
  41. import test_dtoc
  42. result = unittest.TestResult()
  43. sys.argv = [sys.argv[0]]
  44. test_name = args and args[0] or None
  45. for module in (test_dtoc.TestDtoc,):
  46. if test_name:
  47. try:
  48. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  49. except AttributeError:
  50. continue
  51. else:
  52. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  53. suite.run(result)
  54. print result
  55. for _, err in result.errors:
  56. print err
  57. for _, err in result.failures:
  58. print err
  59. def RunTestCoverage():
  60. """Run the tests and check that we get 100% coverage"""
  61. sys.argv = [sys.argv[0]]
  62. test_util.RunTestCoverage('tools/dtoc/dtoc.py', '/dtoc.py',
  63. ['tools/patman/*.py', '*/fdt*', '*test*'], options.build_dir)
  64. if __name__ != '__main__':
  65. sys.exit(1)
  66. parser = OptionParser()
  67. parser.add_option('-B', '--build-dir', type='string', default='b',
  68. help='Directory containing the build output')
  69. parser.add_option('-d', '--dtb-file', action='store',
  70. help='Specify the .dtb input file')
  71. parser.add_option('--include-disabled', action='store_true',
  72. help='Include disabled nodes')
  73. parser.add_option('-o', '--output', action='store', default='-',
  74. help='Select output filename')
  75. parser.add_option('-P', '--processes', type=int,
  76. help='set number of processes to use for running tests')
  77. parser.add_option('-t', '--test', action='store_true', dest='test',
  78. default=False, help='run tests')
  79. parser.add_option('-T', '--test-coverage', action='store_true',
  80. default=False, help='run tests and check for 100% coverage')
  81. (options, args) = parser.parse_args()
  82. # Run our meagre tests
  83. if options.test:
  84. run_tests(args)
  85. elif options.test_coverage:
  86. RunTestCoverage()
  87. else:
  88. dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
  89. options.output)