dtoc.py 2.3 KB

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