fdt_util.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2016 Google, Inc
  5. # Written by Simon Glass <sjg@chromium.org>
  6. #
  7. # Utility functions for reading from a device tree. Once the upstream pylibfdt
  8. # implementation advances far enough, we should be able to drop these.
  9. import os
  10. import struct
  11. import sys
  12. import tempfile
  13. import command
  14. import tools
  15. def fdt32_to_cpu(val):
  16. """Convert a device tree cell to an integer
  17. Args:
  18. Value to convert (4-character string representing the cell value)
  19. Return:
  20. A native-endian integer value
  21. """
  22. return struct.unpack('>I', val)[0]
  23. def fdt_cells_to_cpu(val, cells):
  24. """Convert one or two cells to a long integer
  25. Args:
  26. Value to convert (array of one or more 4-character strings)
  27. Return:
  28. A native-endian integer value
  29. """
  30. if not cells:
  31. return 0
  32. out = int(fdt32_to_cpu(val[0]))
  33. if cells == 2:
  34. out = out << 32 | fdt32_to_cpu(val[1])
  35. return out
  36. def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
  37. """Compile an fdt .dts source file into a .dtb binary blob if needed.
  38. Args:
  39. fname: Filename (if .dts it will be compiled). It not it will be
  40. left alone
  41. tmpdir: Temporary directory for output files, or None to use the
  42. tools-module output directory
  43. Returns:
  44. Filename of resulting .dtb file
  45. """
  46. _, ext = os.path.splitext(fname)
  47. if ext != '.dts':
  48. return fname
  49. if tmpdir:
  50. dts_input = os.path.join(tmpdir, 'source.dts')
  51. dtb_output = os.path.join(tmpdir, 'source.dtb')
  52. else:
  53. dts_input = tools.GetOutputFilename('source.dts')
  54. dtb_output = tools.GetOutputFilename('source.dtb')
  55. search_paths = [os.path.join(os.getcwd(), 'include')]
  56. root, _ = os.path.splitext(fname)
  57. args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
  58. args += ['-Ulinux']
  59. for path in search_paths:
  60. args.extend(['-I', path])
  61. args += ['-o', dts_input, fname]
  62. command.Run('cc', *args)
  63. # If we don't have a directory, put it in the tools tempdir
  64. search_list = []
  65. for path in search_paths:
  66. search_list.extend(['-i', path])
  67. args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
  68. '-W', 'no-unit_address_vs_reg']
  69. args.extend(search_list)
  70. args.append(dts_input)
  71. dtc = os.environ.get('DTC') or 'dtc'
  72. command.Run(dtc, *args, capture_stderr=capture_stderr)
  73. return dtb_output
  74. def GetInt(node, propname, default=None):
  75. """Get an integer from a property
  76. Args:
  77. node: Node object to read from
  78. propname: property name to read
  79. default: Default value to use if the node/property do not exist
  80. Returns:
  81. Integer value read, or default if none
  82. """
  83. prop = node.props.get(propname)
  84. if not prop:
  85. return default
  86. if isinstance(prop.value, list):
  87. raise ValueError("Node '%s' property '%s' has list value: expecting "
  88. "a single integer" % (node.name, propname))
  89. value = fdt32_to_cpu(prop.value)
  90. return value
  91. def GetString(node, propname, default=None):
  92. """Get a string from a property
  93. Args:
  94. node: Node object to read from
  95. propname: property name to read
  96. default: Default value to use if the node/property do not exist
  97. Returns:
  98. String value read, or default if none
  99. """
  100. prop = node.props.get(propname)
  101. if not prop:
  102. return default
  103. value = prop.value
  104. if isinstance(value, list):
  105. raise ValueError("Node '%s' property '%s' has list value: expecting "
  106. "a single string" % (node.name, propname))
  107. return value
  108. def GetBool(node, propname, default=False):
  109. """Get an boolean from a property
  110. Args:
  111. node: Node object to read from
  112. propname: property name to read
  113. default: Default value to use if the node/property do not exist
  114. Returns:
  115. Boolean value read, or default if none (if you set this to True the
  116. function will always return True)
  117. """
  118. if propname in node.props:
  119. return True
  120. return default
  121. def GetByte(node, propname, default=None):
  122. """Get an byte from a property
  123. Args:
  124. node: Node object to read from
  125. propname: property name to read
  126. default: Default value to use if the node/property do not exist
  127. Returns:
  128. Byte value read, or default if none
  129. """
  130. prop = node.props.get(propname)
  131. if not prop:
  132. return default
  133. value = prop.value
  134. if isinstance(value, list):
  135. raise ValueError("Node '%s' property '%s' has list value: expecting "
  136. "a single byte" % (node.name, propname))
  137. if len(value) != 1:
  138. raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
  139. (node.name, propname, len(value), 1))
  140. return ord(value[0])
  141. def GetPhandleList(node, propname):
  142. """Get a list of phandles from a property
  143. Args:
  144. node: Node object to read from
  145. propname: property name to read
  146. Returns:
  147. List of phandles read, each an integer
  148. """
  149. prop = node.props.get(propname)
  150. if not prop:
  151. return None
  152. value = prop.value
  153. if not isinstance(value, list):
  154. value = [value]
  155. return [fdt32_to_cpu(v) for v in value]
  156. def GetDatatype(node, propname, datatype):
  157. """Get a value of a given type from a property
  158. Args:
  159. node: Node object to read from
  160. propname: property name to read
  161. datatype: Type to read (str or int)
  162. Returns:
  163. value read, or None if none
  164. Raises:
  165. ValueError if datatype is not str or int
  166. """
  167. if datatype == str:
  168. return GetString(node, propname)
  169. elif datatype == int:
  170. return GetInt(node, propname)
  171. raise ValueError("fdt_util internal error: Unknown data type '%s'" %
  172. datatype)