[v2,20/49] dtoc: Allow adding variable-sized data to a dtb

Message ID 20200613205717.v2.20.Ie0034d02b461e578e8c883e836b399c6994dd3b0@changeid
State Accepted
Commit 1f238bd5bdc35d996751ed4b8faf8b4c4e69f68d
Headers show
Series
  • rockchip: x86: Support building ROM files automatically with binman
Related show

Commit Message

Simon Glass June 14, 2020, 2:56 a.m.
Add a method for adding a property containing arbitrary bytes. Make sure
that the tree can expand as needed in this case.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 tools/dtoc/fdt.py      | 17 +++++++++++++++--
 tools/dtoc/test_fdt.py |  4 ++++
 2 files changed, 19 insertions(+), 2 deletions(-)

Patch

diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index 188490b728..d058c59e92 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -207,7 +207,8 @@  class Prop:
             if auto_resize:
                 while fdt_obj.setprop(node.Offset(), self.name, self.bytes,
                                     (libfdt.NOSPACE,)) == -libfdt.NOSPACE:
-                    fdt_obj.resize(fdt_obj.totalsize() + 1024)
+                    fdt_obj.resize(fdt_obj.totalsize() + 1024 +
+                                   len(self.bytes))
                     fdt_obj.setprop(node.Offset(), self.name, self.bytes)
             else:
                 fdt_obj.setprop(node.Offset(), self.name, self.bytes)
@@ -410,6 +411,18 @@  class Node:
             val = val.encode('utf-8')
         self._CheckProp(prop_name).props[prop_name].SetData(val + b'\0')
 
+    def AddData(self, prop_name, val):
+        """Add a new 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: Bytes value of property
+        """
+        self.props[prop_name] = Prop(self, None, prop_name, val)
+
     def AddString(self, prop_name, val):
         """Add a new string property to a node
 
@@ -422,7 +435,7 @@  class Node:
         """
         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')
+        self.AddData(prop_name, val + b'\0')
 
     def AddSubnode(self, name):
         """Add a new subnode to the node
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index 375e906424..b4f9b7f498 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -417,6 +417,10 @@  class TestProp(unittest.TestCase):
         self.node.SetData('empty', b'123')
         self.assertEqual(b'123', prop.bytes)
 
+        # Trying adding a lot of data at once
+        self.node.AddData('data', tools.GetBytes(65, 20000))
+        self.dtb.Sync(auto_resize=True)
+
     def testFromData(self):
         dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
         self.assertEqual(dtb2.GetContents(), self.dtb.GetContents())