mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] of: implement of_append_property
@ 2022-08-09  5:57 Ahmad Fatoum
  2022-08-09  5:57 ` [PATCH 2/2] test: self: add tests for of_append_property Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2022-08-09  5:57 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

board code fix up the device tree for OS consumption. Sometimes it's
useful for the fixup to append onto an existing property.
Add a helper that simplifies this.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/base.c | 36 ++++++++++++++++++++++++++++++++++++
 include/of.h      |  8 ++++++++
 2 files changed, 44 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 101b2f74c74f..0893bdf3e04f 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2321,6 +2321,42 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
 	return 0;
 }
 
+int of_append_property(struct device_node *np, const char *name, const void *val, int len)
+{
+	struct property *pp;
+	void *buf = NULL;
+	int orig_len = 0;
+
+	if (!np)
+		return -ENOENT;
+
+	pp = of_find_property(np, name, NULL);
+	if (pp) {
+		buf = pp->value;
+		orig_len = pp->length;
+	}
+
+	buf = realloc(buf, orig_len + len);
+	if (!buf)
+		return -ENOMEM;
+
+	memcpy(buf + orig_len, val, len);
+
+	if (pp) {
+		pp->value = buf;
+		pp->length += len;
+
+		if (pp->value_const) {
+			memcpy(buf, pp->value_const, orig_len);
+			pp->value_const = NULL;
+		}
+	} else {
+		pp = __of_new_property(np, name, buf, len);
+	}
+
+	return 0;
+}
+
 int of_property_sprintf(struct device_node *np,
 			const char *propname, const char *fmt, ...)
 {
diff --git a/include/of.h b/include/of.h
index c65cef5a5a7c..97b4b3dd79bd 100644
--- a/include/of.h
+++ b/include/of.h
@@ -131,6 +131,8 @@ extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
 
 extern int of_set_property(struct device_node *node, const char *p,
 			const void *val, int len, int create);
+extern int of_append_property(struct device_node *np, const char *p,
+			      const void *val, int len);
 extern struct property *of_new_property(struct device_node *node,
 				const char *name, const void *data, int len);
 extern struct property *of_new_property_const(struct device_node *node,
@@ -515,6 +517,12 @@ static inline int of_set_property(struct device_node *node, const char *p,
 	return -ENOSYS;
 }
 
+static inline int of_append_property(struct device_node *np, const char *p,
+				      const void *val, int len)
+{
+	return -ENOSYS;
+}
+
 static inline struct property *of_new_property(struct device_node *node,
 				const char *name, const void *data, int len)
 {
-- 
2.30.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] test: self: add tests for of_append_property
  2022-08-09  5:57 [PATCH 1/2] of: implement of_append_property Ahmad Fatoum
@ 2022-08-09  5:57 ` Ahmad Fatoum
  2022-08-09  7:26 ` [PATCH 1/2] of: implement of_append_property Marco Felsch
  2022-08-09  9:24 ` Sascha Hauer
  2 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2022-08-09  5:57 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

of_append_property has been recently added, so extend the
of_manipulation tests to cover it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/of_manipulation.c   | 11 ++++++++++-
 test/self/of_manipulation.dts |  5 +++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/test/self/of_manipulation.c b/test/self/of_manipulation.c
index 1bcd593c8628..6eb6062e126b 100644
--- a/test/self/of_manipulation.c
+++ b/test/self/of_manipulation.c
@@ -61,12 +61,13 @@ static void test_of_basics(struct device_node *root)
 
 static void test_of_property_strings(struct device_node *root)
 {
-	struct device_node *np1, *np2, *np3;
+	struct device_node *np1, *np2, *np3, *np4;
 	char properties[] = "ayy\0bee\0sea";
 
 	np1 = of_new_node(root, "np1");
 	np2 = of_new_node(root, "np2");
 	np3 = of_new_node(root, "np3");
+	np4 = of_new_node(root, "np4");
 
 	of_property_sprintf(np1, "property-single", "%c%c%c", 'a', 'y', 'y');
 
@@ -89,6 +90,14 @@ static void test_of_property_strings(struct device_node *root)
 	of_set_property(np1, "property-multi", properties, sizeof(properties) - 1, 0);
 
 	assert_different(np1, np2, 1);
+
+	of_append_property(np4, "property-single", "ayy", 4);
+
+	of_append_property(np4, "property-multi", "ayy", 4);
+	of_append_property(np4, "property-multi", "bee", 4);
+	of_append_property(np4, "property-multi", "sea", 4);
+
+	assert_equal(np3, np4);
 }
 
 static void __init test_of_manipulation(void)
diff --git a/test/self/of_manipulation.dts b/test/self/of_manipulation.dts
index 3b690bb7f0fe..a69d944c1eb5 100644
--- a/test/self/of_manipulation.dts
+++ b/test/self/of_manipulation.dts
@@ -27,4 +27,9 @@
 		property-single = "ayy";
 		property-multi = "ayy", "bee", "sea";
 	};
+
+	np4 {
+		property-single = "ayy";
+		property-multi = "ayy", "bee", "sea";
+	};
 };
-- 
2.30.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] of: implement of_append_property
  2022-08-09  5:57 [PATCH 1/2] of: implement of_append_property Ahmad Fatoum
  2022-08-09  5:57 ` [PATCH 2/2] test: self: add tests for of_append_property Ahmad Fatoum
@ 2022-08-09  7:26 ` Marco Felsch
  2022-08-09  8:29   ` Ahmad Fatoum
  2022-08-09  9:24 ` Sascha Hauer
  2 siblings, 1 reply; 5+ messages in thread
From: Marco Felsch @ 2022-08-09  7:26 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

Hi Ahmad,

On 22-08-09, Ahmad Fatoum wrote:
> board code fix up the device tree for OS consumption. Sometimes it's
> useful for the fixup to append onto an existing property.
> Add a helper that simplifies this.

do you have a particular use-case for this?

Regards,
  Marco

> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/of/base.c | 36 ++++++++++++++++++++++++++++++++++++
>  include/of.h      |  8 ++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 101b2f74c74f..0893bdf3e04f 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -2321,6 +2321,42 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
>  	return 0;
>  }
>  
> +int of_append_property(struct device_node *np, const char *name, const void *val, int len)
> +{
> +	struct property *pp;
> +	void *buf = NULL;
> +	int orig_len = 0;
> +
> +	if (!np)
> +		return -ENOENT;
> +
> +	pp = of_find_property(np, name, NULL);
> +	if (pp) {
> +		buf = pp->value;
> +		orig_len = pp->length;
> +	}
> +
> +	buf = realloc(buf, orig_len + len);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	memcpy(buf + orig_len, val, len);
> +
> +	if (pp) {
> +		pp->value = buf;
> +		pp->length += len;
> +
> +		if (pp->value_const) {
> +			memcpy(buf, pp->value_const, orig_len);
> +			pp->value_const = NULL;
> +		}
> +	} else {
> +		pp = __of_new_property(np, name, buf, len);
> +	}
> +
> +	return 0;
> +}
> +
>  int of_property_sprintf(struct device_node *np,
>  			const char *propname, const char *fmt, ...)
>  {
> diff --git a/include/of.h b/include/of.h
> index c65cef5a5a7c..97b4b3dd79bd 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -131,6 +131,8 @@ extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
>  
>  extern int of_set_property(struct device_node *node, const char *p,
>  			const void *val, int len, int create);
> +extern int of_append_property(struct device_node *np, const char *p,
> +			      const void *val, int len);
>  extern struct property *of_new_property(struct device_node *node,
>  				const char *name, const void *data, int len);
>  extern struct property *of_new_property_const(struct device_node *node,
> @@ -515,6 +517,12 @@ static inline int of_set_property(struct device_node *node, const char *p,
>  	return -ENOSYS;
>  }
>  
> +static inline int of_append_property(struct device_node *np, const char *p,
> +				      const void *val, int len)
> +{
> +	return -ENOSYS;
> +}
> +
>  static inline struct property *of_new_property(struct device_node *node,
>  				const char *name, const void *data, int len)
>  {
> -- 
> 2.30.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] of: implement of_append_property
  2022-08-09  7:26 ` [PATCH 1/2] of: implement of_append_property Marco Felsch
@ 2022-08-09  8:29   ` Ahmad Fatoum
  0 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2022-08-09  8:29 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On 09.08.22 09:26, Marco Felsch wrote:
> Hi Ahmad,
> 
> On 22-08-09, Ahmad Fatoum wrote:
>> board code fix up the device tree for OS consumption. Sometimes it's
>> useful for the fixup to append onto an existing property.
>> Add a helper that simplifies this.
> 
> do you have a particular use-case for this?

I have two boards that aren't yet upstream with board support
code using this:

 - One uses it to append onto the model string information about the
   SoM revision

 - One parses a format from an EEPROM and fixes up the device tree
   accordingly. If it encounters the same tag twice, it appends
   onto the previous property instead of overwriting.

I figured if I needed it twice, it can be useful to others as well.
The tests should ensure it doesn't bit rot until hardware is
finalized and board support is upstreamed.

Cheers,
Ahmad 


> 
> Regards,
>   Marco
> 
>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>> ---
>>  drivers/of/base.c | 36 ++++++++++++++++++++++++++++++++++++
>>  include/of.h      |  8 ++++++++
>>  2 files changed, 44 insertions(+)
>>
>> diff --git a/drivers/of/base.c b/drivers/of/base.c
>> index 101b2f74c74f..0893bdf3e04f 100644
>> --- a/drivers/of/base.c
>> +++ b/drivers/of/base.c
>> @@ -2321,6 +2321,42 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
>>  	return 0;
>>  }
>>  
>> +int of_append_property(struct device_node *np, const char *name, const void *val, int len)
>> +{
>> +	struct property *pp;
>> +	void *buf = NULL;
>> +	int orig_len = 0;
>> +
>> +	if (!np)
>> +		return -ENOENT;
>> +
>> +	pp = of_find_property(np, name, NULL);
>> +	if (pp) {
>> +		buf = pp->value;
>> +		orig_len = pp->length;
>> +	}
>> +
>> +	buf = realloc(buf, orig_len + len);
>> +	if (!buf)
>> +		return -ENOMEM;
>> +
>> +	memcpy(buf + orig_len, val, len);
>> +
>> +	if (pp) {
>> +		pp->value = buf;
>> +		pp->length += len;
>> +
>> +		if (pp->value_const) {
>> +			memcpy(buf, pp->value_const, orig_len);
>> +			pp->value_const = NULL;
>> +		}
>> +	} else {
>> +		pp = __of_new_property(np, name, buf, len);
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>>  int of_property_sprintf(struct device_node *np,
>>  			const char *propname, const char *fmt, ...)
>>  {
>> diff --git a/include/of.h b/include/of.h
>> index c65cef5a5a7c..97b4b3dd79bd 100644
>> --- a/include/of.h
>> +++ b/include/of.h
>> @@ -131,6 +131,8 @@ extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
>>  
>>  extern int of_set_property(struct device_node *node, const char *p,
>>  			const void *val, int len, int create);
>> +extern int of_append_property(struct device_node *np, const char *p,
>> +			      const void *val, int len);
>>  extern struct property *of_new_property(struct device_node *node,
>>  				const char *name, const void *data, int len);
>>  extern struct property *of_new_property_const(struct device_node *node,
>> @@ -515,6 +517,12 @@ static inline int of_set_property(struct device_node *node, const char *p,
>>  	return -ENOSYS;
>>  }
>>  
>> +static inline int of_append_property(struct device_node *np, const char *p,
>> +				      const void *val, int len)
>> +{
>> +	return -ENOSYS;
>> +}
>> +
>>  static inline struct property *of_new_property(struct device_node *node,
>>  				const char *name, const void *data, int len)
>>  {
>> -- 
>> 2.30.2
>>
>>
>>
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] of: implement of_append_property
  2022-08-09  5:57 [PATCH 1/2] of: implement of_append_property Ahmad Fatoum
  2022-08-09  5:57 ` [PATCH 2/2] test: self: add tests for of_append_property Ahmad Fatoum
  2022-08-09  7:26 ` [PATCH 1/2] of: implement of_append_property Marco Felsch
@ 2022-08-09  9:24 ` Sascha Hauer
  2 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2022-08-09  9:24 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, Aug 09, 2022 at 07:57:32AM +0200, Ahmad Fatoum wrote:
> board code fix up the device tree for OS consumption. Sometimes it's
> useful for the fixup to append onto an existing property.
> Add a helper that simplifies this.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/of/base.c | 36 ++++++++++++++++++++++++++++++++++++
>  include/of.h      |  8 ++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 101b2f74c74f..0893bdf3e04f 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -2321,6 +2321,42 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
>  	return 0;
>  }
>  
> +int of_append_property(struct device_node *np, const char *name, const void *val, int len)
> +{
> +	struct property *pp;
> +	void *buf = NULL;
> +	int orig_len = 0;
> +
> +	if (!np)
> +		return -ENOENT;
> +
> +	pp = of_find_property(np, name, NULL);

	if (!pp) {
		of_new_property(np, name, val, len);
		return 0;
	}

With that you can make the rest of the function more straight forward.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-08-09  9:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-09  5:57 [PATCH 1/2] of: implement of_append_property Ahmad Fatoum
2022-08-09  5:57 ` [PATCH 2/2] test: self: add tests for of_append_property Ahmad Fatoum
2022-08-09  7:26 ` [PATCH 1/2] of: implement of_append_property Marco Felsch
2022-08-09  8:29   ` Ahmad Fatoum
2022-08-09  9:24 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox