[U-Boot] libfdt: make fdt_increase_size() available to everyone

Gerald Van Baren
2010-05-16T02:11:08Z

The function fdt_increase_size() increases the size of the device tree by
the given amount. This is useful for any code that wants to add a node
or large property, to avoid the possibility of running out of space.
It's much smarter to have U-Boot increase the size of device tree when
it knows it's going to add data, instead of hoping that the DTS was
compiled with the right -p value.

Improve the size increase algorithm by malloc()ing the necessary space.
This avoids potential problems with the FDT blob overwriting things that
follow it in memory.

Signed-off-by: Timur Tabi <timur@freescale.com>
Signed-off-by: Gerald Van Baren <vanbaren@cideas.com>
---

Hi Timur, Kumar,

This had me scratching my head briefly. I first thought you were
creating a new function, but you just exposed the existing function in
fdt_support.h and moved it to its logical position, given its new
status.

The code has one pre-existing weakness that bothers me: if there is
something following the FDT blob, it will get overwritten by the
increased blob. One way around this would be to malloc() a new memory
space and move and expand the blob to the new space. The first time
this was done, the original blob should not be free()ed since it wasn't
malloc()ed, but the second and subsequent times it should be free()ed.

I've added this to your patch, but have *NOT* execution tested it. Does
this addition (a) make sense and (b) work?

Thanks,
gvb

common/fdt_support.c | 35 +++++++++++++++++++++++++----------
include/fdt_support.h | 1 +
2 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/common/fdt_support.c b/common/fdt_support.c
index b6f252a..6bd03d6 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -645,6 +645,31 @@ int fdt_resize(void *blob)
return actualsize;
}

+/* Keep track of malloc()ed blobs for freeing */
+static void *fdt_malloc_blob = 0;
+
+int fdt_increase_size(void *fdt, int add_len)
+{
+ int newlen;
+ void *newblob;
+ int ret;
+
+ newlen = fdt_totalsize(fdt) + add_len;
+ newblob = malloc(newlen);
+
+ /* Copy the blob and expand it */
+ ret = fdt_open_into(fdt, newblob, newlen);
+ if (!ret) {
+ free(newblob);
+ return ret;
+ }
+
+ if (!fdt_malloc_blob)
+ free(fdt_malloc_blob);
+ fdt_malloc_blob = newblob;
+ return 0;
+}
+
#ifdef CONFIG_PCI
#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4

@@ -792,16 +817,6 @@ int fdt_del_subnodes(const void *blob, int parent_offset)
return 0;
}

-int fdt_increase_size(void *fdt, int add_len)
-{
- int newlen;
-
- newlen = fdt_totalsize(fdt) + add_len;
-
- /* Open in place with a new len */
- return fdt_open_into(fdt, fdt, newlen);
-}
-
int fdt_del_partitions(void *blob, int parent_offset)
{
const void *prop;
diff --git a/include/fdt_support.h b/include/fdt_support.h
index 9a453af..d70627d 100644
--- a/include/fdt_support.h
+++ b/include/fdt_support.h
@@ -78,6 +78,7 @@ void ft_pci_setup(void *blob, bd_t *bd);

void set_working_fdt_addr(void *addr);
int fdt_resize(void *blob);
+int fdt_increase_size(void *fdt, int add_len);

int fdt_fixup_nor_flash_size(void *blob, int cs, u32 size);

--
1.6.3.3