dtoc.py 3.7 KB

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