]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
fdt: Add a function to remove unused strings from a device tree
authorSimon Glass <sjg@chromium.org>
Tue, 23 Jun 2015 21:38:26 +0000 (15:38 -0600)
committerLothar Waßmann <LW@KARO-electronics.de>
Wed, 9 Sep 2015 11:34:25 +0000 (13:34 +0200)
Property names are stored in a string table. When a node property is
removed, the string table is not updated since other nodes may have a
property with the same name.

Thus it is possible for the string table to build up a number of unused
strings. Add a function to remove these. This works by building a new device
tree from the old one, adding strings one by one as needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
include/libfdt.h
lib/libfdt/fdt_rw.c

index 421d64fd8b6328f0241bf32762febd2b958b049e..d0017d886549c975eb1769811e51b2448ca4e494 100644 (file)
@@ -1646,6 +1646,23 @@ int fdt_del_node(void *fdt, int nodeoffset);
 
 const char *fdt_strerror(int errval);
 
+/**
+ * fdt_remove_unused_strings() - Remove any unused strings from an FDT
+ *
+ * This creates a new device tree in @new with unused strings removed. The
+ * called can then use fdt_pack() to minimise the space consumed.
+ *
+ * @old:       Old device tree blog
+ * @new:       Place to put new device tree blob, which must be as large as
+ *             @old
+ * @return
+ *     0, on success
+ *     -FDT_ERR_BADOFFSET, corrupt device tree
+ *     -FDT_ERR_NOSPACE, out of space, which should not happen unless there
+ *             is something very wrong with the device tree input
+ */
+int fdt_remove_unused_strings(const void *old, void *new);
+
 struct fdt_region {
        int offset;
        int size;
index bec8b8ad891846e5240c476cf3b15e34eb3f66e8..1a358a8ca0a6f59e8652a8d92fd712d53bf96408 100644 (file)
@@ -449,3 +449,35 @@ int fdt_pack(void *fdt)
 
        return 0;
 }
+
+int fdt_remove_unused_strings(const void *old, void *new)
+{
+       const struct fdt_property *old_prop;
+       struct fdt_property *new_prop;
+       int size = fdt_totalsize(old);
+       int next_offset, offset;
+       const char *str;
+       int ret;
+       int tag = FDT_PROP;
+
+       /* Make a copy and remove the strings */
+       memcpy(new, old, size);
+       fdt_set_size_dt_strings(new, 0);
+
+       /* Add every property name back into the new string table */
+       for (offset = 0; tag != FDT_END; offset = next_offset) {
+               tag = fdt_next_tag(old, offset, &next_offset);
+               if (tag != FDT_PROP)
+                       continue;
+               old_prop = fdt_get_property_by_offset(old, offset, NULL);
+               new_prop = (struct fdt_property *)(unsigned long)
+                       fdt_get_property_by_offset(new, offset, NULL);
+               str = fdt_string(old, fdt32_to_cpu(old_prop->nameoff));
+               ret = _fdt_find_add_string(new, str);
+               if (ret < 0)
+                       return ret;
+               new_prop->nameoff = cpu_to_fdt32(ret);
+       }
+
+       return 0;
+}