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

Alexander Aring alex.aring at gmail.com
Tue Feb 26 16:05:20 EST 2013


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.

Regards
Alex



More information about the barebox mailing list