dtoc: Add a way to create an Fdt object from a data block
authorSimon Glass <sjg@chromium.org>
Fri, 14 Sep 2018 10:57:17 +0000 (04:57 -0600)
committerSimon Glass <sjg@chromium.org>
Fri, 28 Sep 2018 17:09:01 +0000 (11:09 -0600)
Support creating an Fdt object without having to write the data to a file
first.

Signed-off-by: Simon Glass <sjg@chromium.org>
tools/dtoc/fdt.py
tools/dtoc/test_fdt.py

index c5054e8cc915e2ce1135764485d081c473f6e9fe..2df2d4b0cc7a67abcfbc60f1624b5e0db4b85467 100644 (file)
@@ -472,6 +472,20 @@ class Fdt:
             with open(self._fname) as fd:
                 self._fdt_obj = libfdt.Fdt(fd.read())
 
+    @staticmethod
+    def FromData(data):
+        """Create a new Fdt object from the given data
+
+        Args:
+            data: Device-tree data blob
+
+        Returns:
+            Fdt object containing the data
+        """
+        fdt = Fdt(None)
+        fdt._fdt_obj = libfdt.Fdt(bytearray(data))
+        return fdt
+
     def LookupPhandle(self, phandle):
         """Look up a phandle
 
index 22a075d6c5710c753141b66c244f20a40608a15a..d2597020500c7667bdab888efb890b31dd4f39fc 100755 (executable)
@@ -407,6 +407,14 @@ class TestProp(unittest.TestCase):
         self.node.SetData('empty', '123')
         self.assertEqual('123', prop.bytes)
 
+    def testFromData(self):
+        dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
+        self.assertEqual(dtb2.GetContents(), self.dtb.GetContents())
+
+        self.node.AddEmptyProp('empty', 5)
+        self.dtb.Sync(auto_resize=True)
+        self.assertTrue(dtb2.GetContents() != self.dtb.GetContents())
+
 
 class TestFdtUtil(unittest.TestCase):
     """Tests for the fdt_util module