[PATCH 13/29] of: make flatten independent of libfdt

Sascha Hauer s.hauer at pengutronix.de
Wed Feb 27 03:40:16 EST 2013


On Tue, Feb 26, 2013 at 10:05:20PM +0100, Alexander Aring wrote:
> Hi Sascha,
> 
> On Tue, Feb 26, 2013 at 09:18:40PM +0100, Sascha Hauer wrote:
> > Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
> > ---
> >  common/oftree.c   |    8 ++-
> >  drivers/of/base.c |  180 ++++++++++++++++++++++++++++++++++++++++++++---------
> >  include/of.h      |    4 +-
> >  3 files changed, 160 insertions(+), 32 deletions(-)
> > 
> > diff --git a/common/oftree.c b/common/oftree.c
> > index 0df5209..841d2c4 100644
> > --- a/common/oftree.c
> > +++ b/common/oftree.c
> > @@ -329,7 +329,13 @@ struct fdt_header *of_get_fixed_tree(struct fdt_header *fdt)
> >  	int size, align;
> >  
> >  	if (!fdt) {
> > -		fdt = internalfdt = of_flatten_dtb();
> > +		struct device_node *root_node;
> > +
> > +		root_node = of_get_root_node();
> > +		if (!root_node)
> > +			return NULL;
> > +
> > +		fdt = internalfdt = of_flatten_dtb(root_node);
> >  		if (!fdt)
> >  			return NULL;
> >  	}
> > diff --git a/drivers/of/base.c b/drivers/of/base.c
> > index d6ca949..cd463e9 100644
> > --- a/drivers/of/base.c
> > +++ b/drivers/of/base.c
> > @@ -1154,20 +1154,108 @@ err:
> >  	return ERR_PTR(ret);
> >  }
> >  
> > -static int __of_flatten_dtb(void *fdt, struct device_node *node)
> > +struct fdt {
> > +	void *dt;
> > +	uint32_t dt_nextofs;
> > +	uint32_t dt_size;
> > +	char *strings;
> > +	uint32_t str_nextofs;
> > +	uint32_t str_size;
> > +};
> > +
> > +static inline uint32_t dt_next_ofs(uint32_t curofs, uint32_t len)
> > +{
> > +	return ALIGN(curofs + len, 4);
> > +}
> > +
> > +static int lstrcpy(char *dest, const char *src)
> > +{
> > +	int len = 0;
> > +	int maxlen = 1023;
> > +
> > +	while (*src) {
> > +		*dest++ = *src++;
> > +		len++;
> > +		if (!maxlen)
> > +			return -ENOSPC;
> > +		maxlen--;
> > +	}
> > +
> > +	return len;
> > +}
> > +
> > +static int fdt_ensure_space(struct fdt *fdt, int dtsize)
> > +{
> > +	/*
> > +	 * We assume strings and names have a maximum length of 1024
> > +	 * whereas properties can be longer. We allocate new memory
> > +	 * if we have less than 1024 bytes (+ the property size left.
> > +	 */
> > +	if (fdt->str_size - fdt->str_nextofs < 1024) {
> > +		fdt->strings = realloc(fdt->strings, fdt->str_size * 2);
> > +		if (!fdt->strings)
> > +			return -ENOMEM;
> > +		fdt->str_size *= 2;
> > +	}
> > +
> > +	if (fdt->dt_size - fdt->dt_nextofs < 1024 + dtsize) {
> > +		fdt->dt = realloc(fdt->dt, fdt->dt_size * 2);
> > +		if (!fdt->dt)
> > +			return -ENOMEM;
> 
> Leaking memory here. We need to clean fdt->strings.

Nope. When fdt_ensure_space fails we will free both fdt->strings and
fdt->dt in the out_free: path in of_flatten_dtb().

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list