diff mbox

[U-Boot,19/30] dtoc: Support packing the device tree

Message ID 1469494766-26601-20-git-send-email-sjg@chromium.org
State Accepted
Commit da5f74998b9e5e6b706608a4ca625ef0ee195150
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass July 26, 2016, 12:59 a.m. UTC
After any node/property deletion the device tree can be packed to remove
spare space. Add a way to perform this operation.

Note that for fdt_fallback, fdtput automatically packs the device tree after
deletion, so no action is required here.

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

 lib/libfdt/libfdt.swig   |  1 +
 tools/dtoc/fdt.py        | 16 ++++++++++++++++
 tools/dtoc/fdt_normal.py | 11 +++++++++++
 3 files changed, 28 insertions(+)

Comments

Simon Glass Aug. 27, 2016, 4:06 p.m. UTC | #1
On 25 July 2016 at 18:59, Simon Glass <sjg@chromium.org> wrote:
> After any node/property deletion the device tree can be packed to remove
> spare space. Add a way to perform this operation.
>
> Note that for fdt_fallback, fdtput automatically packs the device tree after
> deletion, so no action is required here.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  lib/libfdt/libfdt.swig   |  1 +
>  tools/dtoc/fdt.py        | 16 ++++++++++++++++
>  tools/dtoc/fdt_normal.py | 11 +++++++++++
>  3 files changed, 28 insertions(+)

Applied to u-boot-dm/next.
diff mbox

Patch

diff --git a/lib/libfdt/libfdt.swig b/lib/libfdt/libfdt.swig
index ce516fd..0cb7977 100644
--- a/lib/libfdt/libfdt.swig
+++ b/lib/libfdt/libfdt.swig
@@ -107,3 +107,4 @@  int fdt_next_subnode(const void *fdt, int offset);
 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
 
 const char *fdt_strerror(int errval);
+int fdt_pack(void *fdt);
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index f01c7b1..403eb1f 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -240,3 +240,19 @@  class Fdt:
                 return None
         return node
 
+    def Flush(self):
+        """Flush device tree changes back to the file
+
+        If the device tree has changed in memory, write it back to the file.
+        Subclasses can implement this if needed.
+        """
+        pass
+
+    def Pack(self):
+        """Pack the device tree down to its minimum size
+
+        When nodes and properties shrink or are deleted, wasted space can
+        build up in the device tree binary. Subclasses can implement this
+        to remove that spare space.
+        """
+        pass
diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py
index 52d8055..f2cf608 100644
--- a/tools/dtoc/fdt_normal.py
+++ b/tools/dtoc/fdt_normal.py
@@ -140,6 +140,17 @@  class FdtNormal(Fdt):
         """
         return self._fdt
 
+    def Flush(self):
+        """Flush device tree changes back to the file"""
+        with open(self._fname, 'wb') as fd:
+            fd.write(self._fdt)
+
+    def Pack(self):
+        """Pack the device tree down to its minimum size"""
+        CheckErr(libfdt.fdt_pack(self._fdt), 'pack')
+        fdt_len = libfdt.fdt_totalsize(self._fdt)
+        del self._fdt[fdt_len:]
+
     def GetProps(self, node, path):
         """Get all properties from a node.