fdt.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 fdt_util
  9. import libfdt
  10. import sys
  11. # This deals with a device tree, presenting it as a list of Node and Prop
  12. # objects, representing nodes and properties, respectively.
  13. #
  14. # This implementation uses a libfdt Python library to access the device tree,
  15. # so it is fairly efficient.
  16. class Prop:
  17. """A device tree property
  18. Properties:
  19. name: Property name (as per the device tree)
  20. value: Property value as a string of bytes, or a list of strings of
  21. bytes
  22. type: Value type
  23. """
  24. def __init__(self, name, bytes):
  25. self.name = name
  26. self.value = None
  27. if not bytes:
  28. self.type = fdt_util.TYPE_BOOL
  29. self.value = True
  30. return
  31. self.type, self.value = fdt_util.BytesToValue(bytes)
  32. def GetPhandle(self):
  33. """Get a (single) phandle value from a property
  34. Gets the phandle valuie from a property and returns it as an integer
  35. """
  36. return fdt_util.fdt32_to_cpu(self.value[:4])
  37. def Widen(self, newprop):
  38. """Figure out which property type is more general
  39. Given a current property and a new property, this function returns the
  40. one that is less specific as to type. The less specific property will
  41. be ble to represent the data in the more specific property. This is
  42. used for things like:
  43. node1 {
  44. compatible = "fred";
  45. value = <1>;
  46. };
  47. node1 {
  48. compatible = "fred";
  49. value = <1 2>;
  50. };
  51. He we want to use an int array for 'value'. The first property
  52. suggests that a single int is enough, but the second one shows that
  53. it is not. Calling this function with these two propertes would
  54. update the current property to be like the second, since it is less
  55. specific.
  56. """
  57. if newprop.type < self.type:
  58. self.type = newprop.type
  59. if type(newprop.value) == list and type(self.value) != list:
  60. self.value = [self.value]
  61. if type(self.value) == list and len(newprop.value) > len(self.value):
  62. val = fdt_util.GetEmpty(self.type)
  63. while len(self.value) < len(newprop.value):
  64. self.value.append(val)
  65. class Node:
  66. """A device tree node
  67. Properties:
  68. offset: Integer offset in the device tree
  69. name: Device tree node tname
  70. path: Full path to node, along with the node name itself
  71. _fdt: Device tree object
  72. subnodes: A list of subnodes for this node, each a Node object
  73. props: A dict of properties for this node, each a Prop object.
  74. Keyed by property name
  75. """
  76. def __init__(self, fdt, offset, name, path):
  77. self.offset = offset
  78. self.name = name
  79. self.path = path
  80. self._fdt = fdt
  81. self.subnodes = []
  82. self.props = {}
  83. def Scan(self):
  84. """Scan a node's properties and subnodes
  85. This fills in the props and subnodes properties, recursively
  86. searching into subnodes so that the entire tree is built.
  87. """
  88. self.props = self._fdt.GetProps(self.path)
  89. offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self.offset)
  90. while offset >= 0:
  91. sep = '' if self.path[-1] == '/' else '/'
  92. name = libfdt.Name(self._fdt.GetFdt(), offset)
  93. path = self.path + sep + name
  94. node = Node(self._fdt, offset, name, path)
  95. self.subnodes.append(node)
  96. node.Scan()
  97. offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
  98. class Fdt:
  99. """Provides simple access to a flat device tree blob.
  100. Properties:
  101. fname: Filename of fdt
  102. _root: Root of device tree (a Node object)
  103. """
  104. def __init__(self, fname):
  105. self.fname = fname
  106. with open(fname) as fd:
  107. self._fdt = fd.read()
  108. def GetFdt(self):
  109. """Get the contents of the FDT
  110. Returns:
  111. The FDT contents as a string of bytes
  112. """
  113. return self._fdt
  114. def Scan(self):
  115. """Scan a device tree, building up a tree of Node objects
  116. This fills in the self._root property
  117. """
  118. self._root = Node(self, 0, '/', '/')
  119. self._root.Scan()
  120. def GetRoot(self):
  121. """Get the root Node of the device tree
  122. Returns:
  123. The root Node object
  124. """
  125. return self._root
  126. def GetProps(self, node):
  127. """Get all properties from a node.
  128. Args:
  129. node: Full path to node name to look in.
  130. Returns:
  131. A dictionary containing all the properties, indexed by node name.
  132. The entries are Prop objects.
  133. Raises:
  134. ValueError: if the node does not exist.
  135. """
  136. offset = libfdt.fdt_path_offset(self._fdt, node)
  137. if offset < 0:
  138. libfdt.Raise(offset)
  139. props_dict = {}
  140. poffset = libfdt.fdt_first_property_offset(self._fdt, offset)
  141. while poffset >= 0:
  142. dprop, plen = libfdt.fdt_get_property_by_offset(self._fdt, poffset)
  143. prop = Prop(libfdt.String(self._fdt, dprop.nameoff), libfdt.Data(dprop))
  144. props_dict[prop.name] = prop
  145. poffset = libfdt.fdt_next_property_offset(self._fdt, poffset)
  146. return props_dict