fdt_util.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # SPDX-License-Identifier: GPL-2.0+
  7. #
  8. import os
  9. import struct
  10. import tempfile
  11. import command
  12. import tools
  13. def fdt32_to_cpu(val):
  14. """Convert a device tree cell to an integer
  15. Args:
  16. Value to convert (4-character string representing the cell value)
  17. Return:
  18. A native-endian integer value
  19. """
  20. return struct.unpack('>I', val)[0]
  21. def EnsureCompiled(fname):
  22. """Compile an fdt .dts source file into a .dtb binary blob if needed.
  23. Args:
  24. fname: Filename (if .dts it will be compiled). It not it will be
  25. left alone
  26. Returns:
  27. Filename of resulting .dtb file
  28. """
  29. _, ext = os.path.splitext(fname)
  30. if ext != '.dts':
  31. return fname
  32. dts_input = tools.GetOutputFilename('source.dts')
  33. dtb_output = tools.GetOutputFilename('source.dtb')
  34. search_paths = [os.path.join(os.getcwd(), 'include')]
  35. root, _ = os.path.splitext(fname)
  36. args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
  37. args += ['-Ulinux']
  38. for path in search_paths:
  39. args.extend(['-I', path])
  40. args += ['-o', dts_input, fname]
  41. command.Run('cc', *args)
  42. # If we don't have a directory, put it in the tools tempdir
  43. search_list = []
  44. for path in search_paths:
  45. search_list.extend(['-i', path])
  46. args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb']
  47. args.extend(search_list)
  48. args.append(dts_input)
  49. command.Run('dtc', *args)
  50. return dtb_output