fdt_util.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. import os
  8. import struct
  9. import sys
  10. import tempfile
  11. import command
  12. import tools
  13. VERSION3 = sys.version_info > (3, 0)
  14. def get_plain_bytes(val):
  15. """Handle Python 3 strings"""
  16. if isinstance(val, bytes):
  17. val = val.decode('utf-8')
  18. return val.encode('raw_unicode_escape')
  19. def fdt32_to_cpu(val):
  20. """Convert a device tree cell to an integer
  21. Args:
  22. Value to convert (4-character string representing the cell value)
  23. Return:
  24. A native-endian integer value
  25. """
  26. if VERSION3:
  27. # This code is not reached in Python 2
  28. val = get_plain_bytes(val) # pragma: no cover
  29. return struct.unpack('>I', val)[0]
  30. def fdt_cells_to_cpu(val, cells):
  31. """Convert one or two cells to a long integer
  32. Args:
  33. Value to convert (array of one or more 4-character strings)
  34. Return:
  35. A native-endian long value
  36. """
  37. if not cells:
  38. return 0
  39. out = long(fdt32_to_cpu(val[0]))
  40. if cells == 2:
  41. out = out << 32 | fdt32_to_cpu(val[1])
  42. return out
  43. def EnsureCompiled(fname):
  44. """Compile an fdt .dts source file into a .dtb binary blob if needed.
  45. Args:
  46. fname: Filename (if .dts it will be compiled). It not it will be
  47. left alone
  48. Returns:
  49. Filename of resulting .dtb file
  50. """
  51. _, ext = os.path.splitext(fname)
  52. if ext != '.dts':
  53. return fname
  54. dts_input = tools.GetOutputFilename('source.dts')
  55. dtb_output = tools.GetOutputFilename('source.dtb')
  56. search_paths = [os.path.join(os.getcwd(), 'include')]
  57. root, _ = os.path.splitext(fname)
  58. args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
  59. args += ['-Ulinux']
  60. for path in search_paths:
  61. args.extend(['-I', path])
  62. args += ['-o', dts_input, fname]
  63. command.Run('cc', *args)
  64. # If we don't have a directory, put it in the tools tempdir
  65. search_list = []
  66. for path in search_paths:
  67. search_list.extend(['-i', path])
  68. args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
  69. '-W', 'no-unit_address_vs_reg']
  70. args.extend(search_list)
  71. args.append(dts_input)
  72. dtc = os.environ.get('DTC') or 'dtc'
  73. command.Run(dtc, *args)
  74. return dtb_output
  75. def GetInt(node, propname, default=None):
  76. prop = node.props.get(propname)
  77. if not prop:
  78. return default
  79. if isinstance(prop.value, list):
  80. raise ValueError("Node '%s' property '%s' has list value: expecting "
  81. "a single integer" % (node.name, propname))
  82. value = fdt32_to_cpu(prop.value)
  83. return value
  84. def GetString(node, propname, default=None):
  85. prop = node.props.get(propname)
  86. if not prop:
  87. return default
  88. value = prop.value
  89. if isinstance(value, list):
  90. raise ValueError("Node '%s' property '%s' has list value: expecting "
  91. "a single string" % (node.name, propname))
  92. return value
  93. def GetBool(node, propname, default=False):
  94. if propname in node.props:
  95. return True
  96. return default