diff mbox series

[08/13] libpdbg: Read/write properties directly from/to device tree

Message ID 20200115051901.17514-9-amitay@ozlabs.org
State Accepted
Headers show
Series Use fdt properties directly | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch warning Failed to apply on branch master (8b4611b5d8e7e2279fe4aa80c892fcfe10aa398d)
snowpatch_ozlabs/apply_patch fail Failed to apply to any branch

Commit Message

Amitay Isaacs Jan. 15, 2020, 5:18 a.m. UTC
Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
---
 libpdbg/device.c  | 46 +++++++++++++++++++++++++++-------------------
 libpdbg/libpdbg.c |  8 ++++----
 libpdbg/libpdbg.h |  2 +-
 3 files changed, 32 insertions(+), 24 deletions(-)

Comments

Alistair Popple Jan. 16, 2020, 1:34 a.m. UTC | #1
Reviewed-by: Alistair Popple <alistair@popple.id.au>

On Wednesday, 15 January 2020 4:18:56 PM AEDT Amitay Isaacs wrote:
> Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> ---
>  libpdbg/device.c  | 46 +++++++++++++++++++++++++++-------------------
>  libpdbg/libpdbg.c |  8 ++++----
>  libpdbg/libpdbg.h |  2 +-
>  3 files changed, 32 insertions(+), 24 deletions(-)
> 
> diff --git a/libpdbg/device.c b/libpdbg/device.c
> index c5fdc4e..76d6b3e 100644
> --- a/libpdbg/device.c
> +++ b/libpdbg/device.c
> @@ -349,34 +349,42 @@ static void dt_add_phandle(struct pdbg_target *node,
> const char *name,
> 
>  bool pdbg_target_set_property(struct pdbg_target *target, const char *name,
> const void *val, size_t size) {
> -	struct dt_property *p;
> +	const void *p;
> +	size_t len;
> +	int ret;
> 
> -	if ((p = dt_find_property(target, name))) {
> -		if (size != p->len) {
> -			return false;
> -		}
> +	p = pdbg_target_property(target, name, &len);
> +	if (!p)
> +		return false;
> 
> -		memcpy(p->prop, val, size);
> -	} else {
> +	if (len != size)
> +		return false;
> +
> +	ret = fdt_setprop_inplace(target->fdt, target->fdt_offset, name, val,
> size); +	if (ret)
>  		return false;
> -	}
> 
>  	return true;
>  }
> 
> -void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size) +const void *pdbg_target_property(struct pdbg_target *target,
> const char *name, size_t *size) {
> -	struct dt_property *p;
> +	const void *buf;
> +	int buflen;
> 
> -	p = dt_find_property(target, name);
> -	if (p) {
> -		if (size)
> -			*size = p->len;
> -		return p->prop;
> -	} else if (size)
> +	if (target->fdt_offset == -1) {
>  		*size = 0;
> +		return NULL;
> +	}
> 
> -	return NULL;
> +	buf = fdt_getprop(target->fdt, target->fdt_offset, name, &buflen);
> +	if (!buf) {
> +		*size = 0;
> +		return NULL;
> +	}
> +
> +	*size = buflen;
> +	return buf;
>  }
> 
>  static u32 dt_property_get_cell(const struct dt_property *prop, u32 index)
> @@ -439,10 +447,10 @@ static const struct dt_property
> *dt_require_property(struct pdbg_target *node,
> 
>  bool pdbg_target_compatible(struct pdbg_target *target, const char
> *compatible) {
> -        char *c, *end;
> +	const char *c, *end;
>          size_t len;
> 
> -        c = pdbg_target_property(target, "compatible", &len);
> +        c = (const char *)pdbg_target_property(target, "compatible", &len);
> if (!c)
>                  return false;
> 
> diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> index 9f7b4cc..8af6a02 100644
> --- a/libpdbg/libpdbg.c
> +++ b/libpdbg/libpdbg.c
> @@ -270,7 +270,7 @@ const char *pdbg_target_dn_name(struct pdbg_target
> *target)
> 
>  int pdbg_target_u32_property(struct pdbg_target *target, const char *name,
> uint32_t *val) {
> -	uint32_t *p;
> +	const void *p;
>  	size_t size;
> 
>  	p = pdbg_get_target_property(target, name, &size);
> @@ -278,15 +278,15 @@ int pdbg_target_u32_property(struct pdbg_target
> *target, const char *name, uint3 return -1;
> 
>  	assert(size == 4);
> -	*val = be32toh(*p);
> +	*val = be32toh(*(uint32_t *)p);
> 
>  	return 0;
>  }
> 
>  int pdbg_target_u32_index(struct pdbg_target *target, const char *name, int
> index, uint32_t *val) {
> +	const void *p;
>          size_t len;
> -	uint32_t *p;
> 
>          p = pdbg_get_target_property(target, name, &len);
>          if (!p)
> @@ -298,7 +298,7 @@ int pdbg_target_u32_index(struct pdbg_target *target,
> const char *name, int inde assert(!((uintptr_t) p & 0x3));
> 
>          /* Always aligned, so this works. */
> -        *val = be32toh(p[index]);
> +        *val = be32toh(((uint32_t *)p)[index]);
>          return 0;
>  }
> 
> diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
> index d28fd90..f07d7d1 100644
> --- a/libpdbg/libpdbg.h
> +++ b/libpdbg/libpdbg.h
> @@ -87,7 +87,7 @@ struct pdbg_target *pdbg_target_require_parent(const char
> *klass, struct pdbg_ta bool pdbg_target_set_property(struct pdbg_target
> *target, const char *name, const void *val, size_t size);
> 
>  /* Get the given property and return the size */
> -void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size); +const void *pdbg_target_property(struct pdbg_target
> *target, const char *name, size_t *size); int
> pdbg_target_u32_property(struct pdbg_target *target, const char *name,
> uint32_t *val); int pdbg_target_u32_index(struct pdbg_target *target, const
> char *name, int index, uint32_t *val); uint64_t pdbg_target_address(struct
> pdbg_target *target, uint64_t *size);
Alistair Popple Jan. 16, 2020, 6:46 a.m. UTC | #2
On Wednesday, 15 January 2020 4:18:56 PM AEDT Amitay Isaacs wrote:
> Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> ---
>  libpdbg/device.c  | 46 +++++++++++++++++++++++++++-------------------
>  libpdbg/libpdbg.c |  8 ++++----
>  libpdbg/libpdbg.h |  2 +-
>  3 files changed, 32 insertions(+), 24 deletions(-)
> 
> diff --git a/libpdbg/device.c b/libpdbg/device.c
> index c5fdc4e..76d6b3e 100644
> --- a/libpdbg/device.c
> +++ b/libpdbg/device.c
> @@ -349,34 +349,42 @@ static void dt_add_phandle(struct pdbg_target *node,
> const char *name,
> 
>  bool pdbg_target_set_property(struct pdbg_target *target, const char *name,
> const void *val, size_t size) {
> -	struct dt_property *p;
> +	const void *p;
> +	size_t len;
> +	int ret;
> 
> -	if ((p = dt_find_property(target, name))) {
> -		if (size != p->len) {
> -			return false;
> -		}
> +	p = pdbg_target_property(target, name, &len);
> +	if (!p)
> +		return false;
> 
> -		memcpy(p->prop, val, size);
> -	} else {
> +	if (len != size)
> +		return false;
> +
> +	ret = fdt_setprop_inplace(target->fdt, target->fdt_offset, name, val,
> size); +	if (ret)
>  		return false;
> -	}
> 
>  	return true;
>  }
> 
> -void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size) +const void *pdbg_target_property(struct pdbg_target *target,
> const char *name, size_t *size) {
> -	struct dt_property *p;
> +	const void *buf;
> +	int buflen;
> 
> -	p = dt_find_property(target, name);
> -	if (p) {
> -		if (size)
> -			*size = p->len;
> -		return p->prop;
> -	} else if (size)

The existing API allows you to pass size == NULL which we should maintain as 
without it I am seeing segfaults.

- Alistair

> +	if (target->fdt_offset == -1) {
>  		*size = 0;
> +		return NULL;
> +	}
> 
> -	return NULL;
> +	buf = fdt_getprop(target->fdt, target->fdt_offset, name, &buflen);
> +	if (!buf) {
> +		*size = 0;
> +		return NULL;
> +	}
> +
> +	*size = buflen;
> +	return buf;
>  }
> 
>  static u32 dt_property_get_cell(const struct dt_property *prop, u32 index)
> @@ -439,10 +447,10 @@ static const struct dt_property
> *dt_require_property(struct pdbg_target *node,
> 
>  bool pdbg_target_compatible(struct pdbg_target *target, const char
> *compatible) {
> -        char *c, *end;
> +	const char *c, *end;
>          size_t len;
> 
> -        c = pdbg_target_property(target, "compatible", &len);
> +        c = (const char *)pdbg_target_property(target, "compatible", &len);
> if (!c)
>                  return false;
> 
> diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> index 9f7b4cc..8af6a02 100644
> --- a/libpdbg/libpdbg.c
> +++ b/libpdbg/libpdbg.c
> @@ -270,7 +270,7 @@ const char *pdbg_target_dn_name(struct pdbg_target
> *target)
> 
>  int pdbg_target_u32_property(struct pdbg_target *target, const char *name,
> uint32_t *val) {
> -	uint32_t *p;
> +	const void *p;
>  	size_t size;
> 
>  	p = pdbg_get_target_property(target, name, &size);
> @@ -278,15 +278,15 @@ int pdbg_target_u32_property(struct pdbg_target
> *target, const char *name, uint3 return -1;
> 
>  	assert(size == 4);
> -	*val = be32toh(*p);
> +	*val = be32toh(*(uint32_t *)p);
> 
>  	return 0;
>  }
> 
>  int pdbg_target_u32_index(struct pdbg_target *target, const char *name, int
> index, uint32_t *val) {
> +	const void *p;
>          size_t len;
> -	uint32_t *p;
> 
>          p = pdbg_get_target_property(target, name, &len);
>          if (!p)
> @@ -298,7 +298,7 @@ int pdbg_target_u32_index(struct pdbg_target *target,
> const char *name, int inde assert(!((uintptr_t) p & 0x3));
> 
>          /* Always aligned, so this works. */
> -        *val = be32toh(p[index]);
> +        *val = be32toh(((uint32_t *)p)[index]);
>          return 0;
>  }
> 
> diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
> index d28fd90..f07d7d1 100644
> --- a/libpdbg/libpdbg.h
> +++ b/libpdbg/libpdbg.h
> @@ -87,7 +87,7 @@ struct pdbg_target *pdbg_target_require_parent(const char
> *klass, struct pdbg_ta bool pdbg_target_set_property(struct pdbg_target
> *target, const char *name, const void *val, size_t size);
> 
>  /* Get the given property and return the size */
> -void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size); +const void *pdbg_target_property(struct pdbg_target
> *target, const char *name, size_t *size); int
> pdbg_target_u32_property(struct pdbg_target *target, const char *name,
> uint32_t *val); int pdbg_target_u32_index(struct pdbg_target *target, const
> char *name, int index, uint32_t *val); uint64_t pdbg_target_address(struct
> pdbg_target *target, uint64_t *size);
diff mbox series

Patch

diff --git a/libpdbg/device.c b/libpdbg/device.c
index c5fdc4e..76d6b3e 100644
--- a/libpdbg/device.c
+++ b/libpdbg/device.c
@@ -349,34 +349,42 @@  static void dt_add_phandle(struct pdbg_target *node, const char *name,
 
 bool pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size)
 {
-	struct dt_property *p;
+	const void *p;
+	size_t len;
+	int ret;
 
-	if ((p = dt_find_property(target, name))) {
-		if (size != p->len) {
-			return false;
-		}
+	p = pdbg_target_property(target, name, &len);
+	if (!p)
+		return false;
 
-		memcpy(p->prop, val, size);
-	} else {
+	if (len != size)
+		return false;
+
+	ret = fdt_setprop_inplace(target->fdt, target->fdt_offset, name, val, size);
+	if (ret)
 		return false;
-	}
 
 	return true;
 }
 
-void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size)
+const void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size)
 {
-	struct dt_property *p;
+	const void *buf;
+	int buflen;
 
-	p = dt_find_property(target, name);
-	if (p) {
-		if (size)
-			*size = p->len;
-		return p->prop;
-	} else if (size)
+	if (target->fdt_offset == -1) {
 		*size = 0;
+		return NULL;
+	}
 
-	return NULL;
+	buf = fdt_getprop(target->fdt, target->fdt_offset, name, &buflen);
+	if (!buf) {
+		*size = 0;
+		return NULL;
+	}
+
+	*size = buflen;
+	return buf;
 }
 
 static u32 dt_property_get_cell(const struct dt_property *prop, u32 index)
@@ -439,10 +447,10 @@  static const struct dt_property *dt_require_property(struct pdbg_target *node,
 
 bool pdbg_target_compatible(struct pdbg_target *target, const char *compatible)
 {
-        char *c, *end;
+	const char *c, *end;
         size_t len;
 
-        c = pdbg_target_property(target, "compatible", &len);
+        c = (const char *)pdbg_target_property(target, "compatible", &len);
         if (!c)
                 return false;
 
diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 9f7b4cc..8af6a02 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -270,7 +270,7 @@  const char *pdbg_target_dn_name(struct pdbg_target *target)
 
 int pdbg_target_u32_property(struct pdbg_target *target, const char *name, uint32_t *val)
 {
-	uint32_t *p;
+	const void *p;
 	size_t size;
 
 	p = pdbg_get_target_property(target, name, &size);
@@ -278,15 +278,15 @@  int pdbg_target_u32_property(struct pdbg_target *target, const char *name, uint3
 		return -1;
 
 	assert(size == 4);
-	*val = be32toh(*p);
+	*val = be32toh(*(uint32_t *)p);
 
 	return 0;
 }
 
 int pdbg_target_u32_index(struct pdbg_target *target, const char *name, int index, uint32_t *val)
 {
+	const void *p;
         size_t len;
-	uint32_t *p;
 
         p = pdbg_get_target_property(target, name, &len);
         if (!p)
@@ -298,7 +298,7 @@  int pdbg_target_u32_index(struct pdbg_target *target, const char *name, int inde
 	assert(!((uintptr_t) p & 0x3));
 
         /* Always aligned, so this works. */
-        *val = be32toh(p[index]);
+        *val = be32toh(((uint32_t *)p)[index]);
         return 0;
 }
 
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index d28fd90..f07d7d1 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -87,7 +87,7 @@  struct pdbg_target *pdbg_target_require_parent(const char *klass, struct pdbg_ta
 bool pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size);
 
 /* Get the given property and return the size */
-void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size);
+const void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size);
 int pdbg_target_u32_property(struct pdbg_target *target, const char *name, uint32_t *val);
 int pdbg_target_u32_index(struct pdbg_target *target, const char *name, int index, uint32_t *val);
 uint64_t pdbg_target_address(struct pdbg_target *target, uint64_t *size);