main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.rst
  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, '..'))
  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. from dtoc import dtb_platdata
  34. from patman 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. if result.errors or result.failures:
  60. print('dtoc tests FAILED')
  61. return 1
  62. return 0
  63. def RunTestCoverage():
  64. """Run the tests and check that we get 100% coverage"""
  65. sys.argv = [sys.argv[0]]
  66. test_util.RunTestCoverage('tools/dtoc/dtoc', '/main.py',
  67. ['tools/patman/*.py', '*/fdt*', '*test*'], options.build_dir)
  68. if __name__ != '__main__':
  69. sys.exit(1)
  70. parser = OptionParser()
  71. parser.add_option('-B', '--build-dir', type='string', default='b',
  72. help='Directory containing the build output')
  73. parser.add_option('-d', '--dtb-file', action='store',
  74. help='Specify the .dtb input file')
  75. parser.add_option('--include-disabled', action='store_true',
  76. help='Include disabled nodes')
  77. parser.add_option('-o', '--output', action='store', default='-',
  78. help='Select output filename')
  79. parser.add_option('-P', '--processes', type=int,
  80. help='set number of processes to use for running tests')
  81. parser.add_option('-t', '--test', action='store_true', dest='test',
  82. default=False, help='run tests')
  83. parser.add_option('-T', '--test-coverage', action='store_true',
  84. default=False, help='run tests and check for 100% coverage')
  85. (options, args) = parser.parse_args()
  86. # Run our meagre tests
  87. if options.test:
  88. ret_code = run_tests(args)
  89. sys.exit(ret_code)
  90. elif options.test_coverage:
  91. RunTestCoverage()
  92. else:
  93. dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
  94. options.output)