+ def Flush(self):
+ """Flush device tree changes back to the file
+
+ If the device tree has changed in memory, write it back to the file.
+ """
+ with open(self._fname, 'wb') as fd:
+ fd.write(self._fdt_obj.as_bytearray())
+
+ def Pack(self):
+ """Pack the device tree down to its minimum size
+
+ When nodes and properties shrink or are deleted, wasted space can
+ build up in the device tree binary.
+ """
+ CheckErr(self._fdt_obj.pack(), 'pack')
+ self.Invalidate()
+
+ def GetContents(self):
+ """Get the contents of the FDT
+
+ Returns:
+ The FDT contents as a string of bytes
+ """
+ return self._fdt_obj.as_bytearray()
+
+ def GetFdtObj(self):
+ """Get the contents of the FDT
+
+ Returns:
+ The FDT contents as a libfdt.Fdt object
+ """
+ return self._fdt_obj
+
+ def CheckErr(self, errnum, msg):
+ if errnum:
+ raise ValueError('Error %d: %s: %s' %
+ (errnum, libfdt.fdt_strerror(errnum), msg))
+
+ def GetProps(self, node):
+ """Get all properties from a node.
+
+ Args:
+ node: Full path to node name to look in.
+
+ Returns:
+ A dictionary containing all the properties, indexed by node name.
+ The entries are Prop objects.
+
+ Raises:
+ ValueError: if the node does not exist.
+ """
+ props_dict = {}
+ poffset = self._fdt_obj.first_property_offset(node._offset,
+ QUIET_NOTFOUND)
+ while poffset >= 0:
+ p = self._fdt_obj.get_property_by_offset(poffset)
+ prop = Prop(node, poffset, p.name, p)
+ props_dict[prop.name] = prop
+
+ poffset = self._fdt_obj.next_property_offset(poffset,
+ QUIET_NOTFOUND)
+ return props_dict
+
+ def Invalidate(self):
+ """Mark our offset cache as invalid"""
+ self._cached_offsets = False
+
+ def CheckCache(self):
+ """Refresh the offset cache if needed"""
+ if self._cached_offsets:
+ return
+ self.Refresh()
+ self._cached_offsets = True
+
+ def Refresh(self):
+ """Refresh the offset cache"""
+ self._root.Refresh(0)
+
+ def GetStructOffset(self, offset):
+ """Get the file offset of a given struct offset
+
+ Args:
+ offset: Offset within the 'struct' region of the device tree
+ Returns:
+ Position of @offset within the device tree binary
+ """
+ return self._fdt_obj.off_dt_struct() + offset
+
+ @classmethod
+ def Node(self, fdt, parent, offset, name, path):
+ """Create a new node
+
+ This is used by Fdt.Scan() to create a new node using the correct
+ class.
+
+ Args:
+ fdt: Fdt object
+ parent: Parent node, or None if this is the root node
+ offset: Offset of node
+ name: Node name
+ path: Full path to node
+ """
+ node = Node(fdt, parent, offset, name, path)
+ return node
+
+def FdtScan(fname):
+ """Returns a new Fdt object from the implementation we are using"""
+ dtb = Fdt(fname)
+ dtb.Scan()
+ return dtb