[Pdbg] [PATCH v2 09/16] libpdbg: Move property code into libpdbg/device.c

Amitay Isaacs amitay at ozlabs.org
Wed Nov 7 17:24:34 AEDT 2018


Ah, this adds the size check for u32_property(). 

Reviewed-by: Amitay Isaacs <amitay at ozlabs.org>

On Wed, 2018-11-07 at 16:39 +1100, Alistair Popple wrote:
> Signed-off-by: Alistair Popple <alistair at popple.id.au>
> ---
>  libpdbg/device.c  | 72
> +++++++++++++++++++++++++++++++++++++++++++++----------
>  libpdbg/device.h  | 30 -----------------------
>  libpdbg/libpdbg.c | 51 +++++++--------------------------------
>  3 files changed, 68 insertions(+), 85 deletions(-)
> 
> diff --git a/libpdbg/device.c b/libpdbg/device.c
> index 2f1a97b..af2973b 100644
> --- a/libpdbg/device.c
> +++ b/libpdbg/device.c
> @@ -38,6 +38,21 @@ static u32 last_phandle = 0;
>  
>  struct pdbg_target *dt_root;
>  
> +/*
> + * An in-memory representation of a node in the device tree.
> + *
> + * This is trivially flattened into an fdt.
> + *
> + * Note that the add_* routines will make a copy of the name if it's
> not
> + * a read-only string (ie. usually a string literal).
> + */
> +struct dt_property {
> +	struct list_node list;
> +	const char *name;
> +	size_t len;
> +	char prop[/* len */];
> +};
> +
>  static const char *take_name(const char *name)
>  {
>  	if (!is_rodata(name) && !(name = strdup(name))) {
> @@ -285,6 +300,17 @@ static struct pdbg_target
> *dt_find_by_path(struct pdbg_target *root, const char
>  	return root;
>  }
>  
> +static struct dt_property *dt_find_property(const struct pdbg_target
> *node,
> +					   const char *name)
> +{
> +	struct dt_property *i;
> +
> +	list_for_each(&node->properties, i, list)
> +		if (strcmp(i->name, name) == 0)
> +			return i;
> +	return NULL;
> +}
> +
>  static struct dt_property *new_property(struct pdbg_target *node,
>  					const char *name, size_t size)
>  {
> @@ -313,7 +339,7 @@ static struct dt_property *new_property(struct
> pdbg_target *node,
>  	return p;
>  }
>  
> -struct dt_property *dt_add_property(struct pdbg_target *node,
> +static struct dt_property *dt_add_property(struct pdbg_target *node,
>  				    const char *name,
>  				    const void *val, size_t size)
>  {
> @@ -338,7 +364,7 @@ struct dt_property *dt_add_property(struct
> pdbg_target *node,
>  	return p;
>  }
>  
> -void dt_resize_property(struct dt_property **prop, size_t len)
> +static void dt_resize_property(struct dt_property **prop, size_t
> len)
>  {
>  	size_t new_len = sizeof(**prop) + len;
>  
> @@ -349,6 +375,37 @@ void dt_resize_property(struct dt_property
> **prop, size_t len)
>  	(*prop)->list.prev->next = &(*prop)->list;
>  }
>  
> +void pdbg_set_target_property(struct pdbg_target *target, const char
> *name, const void *val, size_t size)
> +{
> +	struct dt_property *p;
> +
> +	if ((p = dt_find_property(target, name))) {
> +		if (size > p->len) {
> +			dt_resize_property(&p, size);
> +			p->len = size;
> +		}
> +
> +		memcpy(p->prop, val, size);
> +	} else {
> +		dt_add_property(target, name, val, size);
> +	}
> +}
> +
> +void *pdbg_get_target_property(struct pdbg_target *target, const
> char *name, size_t *size)
> +{
> +	struct dt_property *p;
> +
> +	p = dt_find_property(target, name);
> +	if (p) {
> +		if (size)
> +			*size = p->len;
> +		return p->prop;
> +	} else if (size)
> +		*size = 0;
> +
> +	return NULL;
> +}
> +
>  static u32 dt_property_get_cell(const struct dt_property *prop, u32
> index)
>  {
>  	assert(prop->len >= (index+1)*sizeof(u32));
> @@ -382,17 +439,6 @@ static struct pdbg_target *dt_next(const struct
> pdbg_target *root,
>  	return NULL;
>  }
>  
> -struct dt_property *dt_find_property(const struct pdbg_target *node,
> -					   const char *name)
> -{
> -	struct dt_property *i;
> -
> -	list_for_each(&node->properties, i, list)
> -		if (strcmp(i->name, name) == 0)
> -			return i;
> -	return NULL;
> -}
> -
>  static const struct dt_property *dt_require_property(const struct
> pdbg_target *node,
>  						     const char *name,
> int wanted_len)
>  {
> diff --git a/libpdbg/device.h b/libpdbg/device.h
> index 098f7f4..f487443 100644
> --- a/libpdbg/device.h
> +++ b/libpdbg/device.h
> @@ -23,31 +23,8 @@
>  /* Any property or node with this prefix will not be passed to the
> kernel. */
>  #define DT_PRIVATE	"skiboot,"
>  
> -/*
> - * An in-memory representation of a node in the device tree.
> - *
> - * This is trivially flattened into an fdt.
> - *
> - * Note that the add_* routines will make a copy of the name if it's
> not
> - * a read-only string (ie. usually a string literal).
> - */
> -struct dt_property {
> -	struct list_node list;
> -	const char *name;
> -	size_t len;
> -	char prop[/* len */];
> -};
> -
>  extern struct pdbg_target *dt_root;
>  
> -/* Add a property node, various forms. */
> -struct dt_property *dt_add_property(struct pdbg_target *node,
> -				    const char *name,
> -				    const void *val, size_t size);
> -
> -/* Warning: moves *prop! */
> -void dt_resize_property(struct dt_property **prop, size_t len);
> -
>  /* Check a compatible property */
>  bool dt_node_is_compatible(const struct pdbg_target *node, const
> char *compat);
>  
> @@ -60,10 +37,6 @@ struct pdbg_target *dt_find_compatible_node(struct
> pdbg_target *root,
>  	for (node = NULL; 			        \
>  	     (node = dt_find_compatible_node(root, node, compat)) !=
> NULL;)
>  
> -/* Find a property by name. */
> -struct dt_property *dt_find_property(const struct pdbg_target
> *node,\
> -				     const char *name);
> -
>  /* Simplified accessors */
>  u32 dt_prop_get_u32(const struct pdbg_target *node, const char
> *prop);
>  u32 dt_prop_get_u32_index(const struct pdbg_target *node, const char
> *prop, u32 index);
> @@ -71,9 +44,6 @@ const void *dt_prop_get(const struct pdbg_target
> *node, const char *prop);
>  const void *dt_prop_get_def(const struct pdbg_target *node, const
> char *prop,
>  			    void *def);
>  
> -/* Parsing helpers */
> -u64 dt_get_number(const void *pdata, unsigned int cells);
> -
>  /* Find an chip-id property in this node; if not found, walk up the
> parent
>   * nodes. Returns -1 if no chip-id property exists. */
>  u32 dt_get_chip_id(const struct pdbg_target *node);
> diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> index 8aa22e1..8af216c 100644
> --- a/libpdbg/libpdbg.c
> +++ b/libpdbg/libpdbg.c
> @@ -135,52 +135,19 @@ const char *pdbg_target_dn_name(struct
> pdbg_target *target)
>  	return target->dn_name;
>  }
>  
> -void pdbg_set_target_property(struct pdbg_target *target, const char
> *name, const void *val, size_t size)
> -{
> -	struct dt_property *p;
> -
> -	if ((p = dt_find_property(target, name))) {
> -		if (size > p->len) {
> -			dt_resize_property(&p, size);
> -			p->len = size;
> -		}
> -
> -		memcpy(p->prop, val, size);
> -	} else {
> -		dt_add_property(target, name, val, size);
> -	}
> -}
> -
> -void *pdbg_get_target_property(struct pdbg_target *target, const
> char *name, size_t *size)
> -{
> -	struct dt_property *p;
> -
> -	p = dt_find_property(target, name);
> -	if (p) {
> -		if (size)
> -			*size = p->len;
> -		return p->prop;
> -	} else if (size)
> -		*size = 0;
> -
> -	return NULL;
> -}
> -
> -uint64_t pdbg_get_address(struct pdbg_target *target, uint64_t
> *size)
> -{
> -	return dt_get_address(target, 0, size);
> -}
> -
>  int pdbg_get_target_u32_property(struct pdbg_target *target, const
> char *name, uint32_t *val)
>  {
> -	struct dt_property *p;
> +        uint32_t *p;
> +        size_t size;
>  
> -	p = dt_find_property(target, name);
> -	if (!p)
> -		return -1;
> +        p = pdbg_get_target_property(target, name, &size);
> +        if (!p)
> +                return -1;
> +
> +        assert(size == 4);
> +        *val = be32toh(*p);
>  
> -	*val = dt_get_number(p->prop, 1);
> -	return 0;
> +        return 0;
>  }
>  
>  void pdbg_progress_tick(uint64_t cur, uint64_t end)
> -- 
> 2.11.0
> 

Amitay.
-- 

I'd like to be rich enough so I could throw soap away after the letters are
worn off. - Andy Rooney



More information about the Pdbg mailing list