[PATCH v2 05/10] pylibfdt: Support device-tree creation/expansion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



Add support for fdt_open_into() and fdt_create_empty_tree() from the
Python library. The former is named resize() since it better fits with
what the Python binding actually does.

Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx>
---

Changes in v2:
- Rename open_into() to resize() since it describes the operation better

 pylibfdt/libfdt.i       | 33 +++++++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 18 +++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index b8732dc..4d87da9 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -452,6 +452,39 @@ class Fdt:
             return pdata
         return Property(pdata[0], pdata[1])
 
+    @staticmethod
+    def create_empty_tree(size, quiet=()):
+        """Create an empty device tree ready for use
+
+        Args:
+            size: Size of device tree in bytes
+
+        Returns:
+            Fdt object containing the device tree
+        """
+        data = bytearray(size)
+        err = check_err(fdt_create_empty_tree(data, size), quiet)
+        if err:
+            return err
+        return Fdt(data)
+
+    def resize(self, size, quiet=()):
+        """Move the device tree into a larger or smaller space
+
+        This creates a new device tree of size @size and moves the existing
+        device tree contents over to that. It can be used to create more space
+        in a device tree. Note that the Fdt object remains the same, but it
+        now has a new bytearray holding the contents.
+
+        Args:
+            size: Required new size of device tree in bytes
+        """
+        fdt = bytearray(size)
+        err = check_err(fdt_open_into(self._fdt, fdt, size), quiet)
+        if err:
+            return err
+        self._fdt = fdt
+
     def pack(self, quiet=()):
         """Pack the device tree to remove unused space
 
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index 458bc9a..96a46f3 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -55,7 +55,7 @@ import unittest
 
 sys.path.insert(0, '../pylibfdt')
 import libfdt
-from libfdt import FdtException, QUIET_NOTFOUND, QUIET_ALL
+from libfdt import Fdt, FdtException, QUIET_NOTFOUND, QUIET_ALL
 
 def get_err(err_code):
     """Convert an error code into an error message
@@ -377,6 +377,22 @@ class PyLibfdtTests(unittest.TestCase):
                           self.fdt.get_mem_rsv(0))
         self.assertEquals([123456789, 010000], self.fdt.get_mem_rsv(1))
 
+    def testEmpty(self):
+        """Test that we can create an empty tree"""
+        self.assertEquals(-libfdt.NOSPACE,
+                          Fdt.create_empty_tree(1, (libfdt.NOSPACE,)))
+        fdt = Fdt.create_empty_tree(128)
+        self.assertEquals(128, fdt.totalsize())
+
+    def testOpenInto(self):
+        """Test that we can resize a tree"""
+        fdt = Fdt.create_empty_tree(128)
+        self.assertEquals(128, fdt.totalsize())
+        fdt.resize(256)
+        self.assertEquals(256, fdt.totalsize())
+        fdt.pack()
+        self.assertTrue(fdt.totalsize() < 128)
+
 
 if __name__ == "__main__":
     unittest.main()
-- 
2.17.1.1185.g55be947832-goog

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux