fdt_util.py 2.8 KB

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