+ def AddZeroProp(self, prop_name):
+ """Add a new property to the device tree with an integer value of 0.
+
+ Args:
+ prop_name: Name of property
+ """
+ self.props[prop_name] = Prop(self, None, prop_name,
+ tools.GetBytes(0, 4))
+
+ def AddEmptyProp(self, prop_name, len):
+ """Add a property with a fixed data size, for filling in later
+
+ The device tree is marked dirty so that the value will be written to
+ the blob on the next sync.
+
+ Args:
+ prop_name: Name of property
+ len: Length of data in property
+ """
+ value = tools.GetBytes(0, len)
+ self.props[prop_name] = Prop(self, None, prop_name, value)
+
+ def _CheckProp(self, prop_name):
+ """Check if a property is present
+
+ Args:
+ prop_name: Name of property
+
+ Returns:
+ self
+
+ Raises:
+ ValueError if the property is missing
+ """
+ if prop_name not in self.props:
+ raise ValueError("Fdt '%s', node '%s': Missing property '%s'" %
+ (self._fdt._fname, self.path, prop_name))
+ return self
+
+ def SetInt(self, prop_name, val):
+ """Update an integer property int the device tree.
+
+ This is not allowed to change the size of the FDT.
+
+ The device tree is marked dirty so that the value will be written to
+ the blob on the next sync.
+
+ Args:
+ prop_name: Name of property
+ val: Value to set
+ """
+ self._CheckProp(prop_name).props[prop_name].SetInt(val)
+
+ def SetData(self, prop_name, val):
+ """Set the data value of a property
+
+ The device tree is marked dirty so that the value will be written to
+ the blob on the next sync.
+
+ Args:
+ prop_name: Name of property to set
+ val: Data value to set
+ """
+ self._CheckProp(prop_name).props[prop_name].SetData(val)
+
+ def SetString(self, prop_name, val):
+ """Set the string value of a property
+
+ The device tree is marked dirty so that the value will be written to
+ the blob on the next sync.
+
+ Args:
+ prop_name: Name of property to set
+ val: String value to set (will be \0-terminated in DT)
+ """
+ if type(val) == str:
+ val = val.encode('utf-8')
+ self._CheckProp(prop_name).props[prop_name].SetData(val + b'\0')
+
+ def AddString(self, prop_name, val):
+ """Add a new string property to a node
+
+ The device tree is marked dirty so that the value will be written to
+ the blob on the next sync.
+
+ Args:
+ prop_name: Name of property to add
+ val: String value of property
+ """
+ if sys.version_info[0] >= 3: # pragma: no cover
+ val = bytes(val, 'utf-8')
+ self.props[prop_name] = Prop(self, None, prop_name, val + b'\0')
+
+ def AddSubnode(self, name):
+ """Add a new subnode to the node
+
+ Args:
+ name: name of node to add
+
+ Returns:
+ New subnode that was created
+ """
+ path = self.path + '/' + name
+ subnode = Node(self._fdt, self, None, name, path)
+ self.subnodes.append(subnode)
+ return subnode
+
+ def Sync(self, auto_resize=False):
+ """Sync node changes back to the device tree
+
+ This updates the device tree blob with any changes to this node and its
+ subnodes since the last sync.
+
+ Args:
+ auto_resize: Resize the device tree automatically if it does not
+ have enough space for the update
+
+ Raises:
+ FdtException if auto_resize is False and there is not enough space
+ """
+ if self._offset is None:
+ # The subnode doesn't exist yet, so add it
+ fdt_obj = self._fdt._fdt_obj
+ if auto_resize:
+ while True:
+ offset = fdt_obj.add_subnode(self.parent._offset, self.name,
+ (libfdt.NOSPACE,))
+ if offset != -libfdt.NOSPACE:
+ break
+ fdt_obj.resize(fdt_obj.totalsize() + 1024)
+ else:
+ offset = fdt_obj.add_subnode(self.parent._offset, self.name)
+ self._offset = offset
+
+ # Sync subnodes in reverse so that we don't disturb node offsets for
+ # nodes that are earlier in the DT. This avoids an O(n^2) rescan of
+ # node offsets.
+ for node in reversed(self.subnodes):
+ node.Sync(auto_resize)
+
+ # Sync properties now, whose offsets should not have been disturbed.
+ # We do this after subnodes, since this disturbs the offsets of these
+ # properties. Note that new properties will have an offset of None here,
+ # which Python 3 cannot sort against int. So use a large value instead
+ # to ensure that the new properties are added first.
+ prop_list = sorted(self.props.values(),
+ key=lambda prop: prop._offset or 1 << 31,
+ reverse=True)
+ for prop in prop_list:
+ prop.Sync(auto_resize)
+
+