dtoc: Drop use of a local dtb buffer

At present the Fdt class has its own copy of the device tree. This is
confusing an unnecessary now that pylibfdt has its own. Drop it and
provide access functions to the buffer.

This allows us to move the rest of the implementation to use pylibfdt
methods instead of directly calling libfdt stubs.

Signed-off-by: Simon Glass <sjg@chromium.org>
lime2-spi
Simon Glass 6 years ago
parent 117f57b75a
commit 960662404f
  1. 16
      tools/dtoc/fdt.py
  2. 4
      tools/dtoc/test_fdt.py

@ -235,12 +235,13 @@ class Node:
Note: This does not take account of property offsets - these will not Note: This does not take account of property offsets - these will not
be updated. be updated.
""" """
fdt_obj = self._fdt._fdt_obj
if self._offset != my_offset: if self._offset != my_offset:
self._offset = my_offset self._offset = my_offset
offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND)
for subnode in self.subnodes: for subnode in self.subnodes:
subnode.Refresh(offset) subnode.Refresh(offset)
offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset) offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND)
def DeleteProp(self, prop_name): def DeleteProp(self, prop_name):
"""Delete a property of a node """Delete a property of a node
@ -252,7 +253,7 @@ class Node:
Raises: Raises:
ValueError if the property does not exist ValueError if the property does not exist
""" """
CheckErr(libfdt.fdt_delprop(self._fdt.GetFdt(), self.Offset(), prop_name), CheckErr(self._fdt._fdt_obj.delprop(self.Offset(), prop_name),
"Node '%s': delete property: '%s'" % (self.path, prop_name)) "Node '%s': delete property: '%s'" % (self.path, prop_name))
del self.props[prop_name] del self.props[prop_name]
self._fdt.Invalidate() self._fdt.Invalidate()
@ -272,8 +273,7 @@ class Fdt:
self._fname = fdt_util.EnsureCompiled(self._fname) self._fname = fdt_util.EnsureCompiled(self._fname)
with open(self._fname) as fd: with open(self._fname) as fd:
self._fdt = bytearray(fd.read()) self._fdt_obj = libfdt.Fdt(fd.read())
self._fdt_obj = libfdt.Fdt(self._fdt)
def Scan(self, root='/'): def Scan(self, root='/'):
"""Scan a device tree, building up a tree of Node objects """Scan a device tree, building up a tree of Node objects
@ -317,7 +317,7 @@ class Fdt:
If the device tree has changed in memory, write it 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: with open(self._fname, 'wb') as fd:
fd.write(self._fdt) fd.write(self._fdt_obj.as_bytearray())
def Pack(self): def Pack(self):
"""Pack the device tree down to its minimum size """Pack the device tree down to its minimum size
@ -328,13 +328,13 @@ class Fdt:
CheckErr(self._fdt_obj.pack(), 'pack') CheckErr(self._fdt_obj.pack(), 'pack')
self.Invalidate() self.Invalidate()
def GetFdt(self): def GetContents(self):
"""Get the contents of the FDT """Get the contents of the FDT
Returns: Returns:
The FDT contents as a string of bytes The FDT contents as a string of bytes
""" """
return self._fdt return self._fdt_obj.as_bytearray()
def GetFdtObj(self): def GetFdtObj(self):
"""Get the contents of the FDT """Get the contents of the FDT

@ -72,7 +72,7 @@ class TestFdt(unittest.TestCase):
def testGetFdt(self): def testGetFdt(self):
"""Tetst that we can access the raw device-tree data""" """Tetst that we can access the raw device-tree data"""
self.assertTrue(isinstance(self.dtb.GetFdt(), bytearray)) self.assertTrue(isinstance(self.dtb.GetContents(), bytearray))
def testGetProps(self): def testGetProps(self):
"""Tests obtaining a list of properties""" """Tests obtaining a list of properties"""
@ -157,7 +157,7 @@ class TestProp(unittest.TestCase):
# Add 12, which is sizeof(struct fdt_property), to get to start of data # Add 12, which is sizeof(struct fdt_property), to get to start of data
offset = prop.GetOffset() + 12 offset = prop.GetOffset() + 12
data = self.dtb._fdt[offset:offset + len(prop.value)] data = self.dtb.GetContents()[offset:offset + len(prop.value)]
bytes = [chr(x) for x in data] bytes = [chr(x) for x in data]
self.assertEqual(bytes, prop.value) self.assertEqual(bytes, prop.value)

Loading…
Cancel
Save