diff mbox series

[U-Boot,v3,1/4] core: Add functions to set properties in live-tree

Message ID 20180626064651.8551-1-mario.six@gdsys.cc
State Accepted
Delegated to: Simon Glass
Headers show
Series [U-Boot,v3,1/4] core: Add functions to set properties in live-tree | expand

Commit Message

Mario Six June 26, 2018, 6:46 a.m. UTC
Implement a set of functions to manipulate properties in a live device
tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

Signed-off-by: Mario Six <mario.six@gdsys.cc>

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for
  that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)

--
2.11.0

Comments

Simon Glass June 26, 2018, 11:18 p.m. UTC | #1
On 26 June 2018 at 00:46, Mario Six <mario.six@gdsys.cc> wrote:
> Implement a set of functions to manipulate properties in a live device
> tree:
>
> * ofnode_write_prop() to set generic properties of a node
> * ofnode_write_string() to set string properties of a node
> * ofnode_set_enabled() to either enable or disable a node
>
> Signed-off-by: Mario Six <mario.six@gdsys.cc>
>
> ---
>
> v2 -> v3:
> * Removed #ifdef, and used if (!of_live_active()) instead
> * Removed allocation of property value (caller is now responsible for
>   that); this also fixes a potential memory leak
> * Added error handling for malloc and strdup
> * Fixed style violations
>
> v1 -> v2:
> * Fix potential NULL pointer dereference in ofnode_write_property
> * Squashed the enable/disable functions into one
> * Renamed ofnode_set_property to ofnode_write_prop
>
> ---
>  drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
>  2 files changed, 116 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>
Mario Six Sept. 26, 2018, 1:34 p.m. UTC | #2
Hi Simon,
On Wed, Jun 27, 2018 at 1:18 AM Simon Glass <sjg@chromium.org> wrote:
>
> On 26 June 2018 at 00:46, Mario Six <mario.six@gdsys.cc> wrote:
> > Implement a set of functions to manipulate properties in a live device
> > tree:
> >
> > * ofnode_write_prop() to set generic properties of a node
> > * ofnode_write_string() to set string properties of a node
> > * ofnode_set_enabled() to either enable or disable a node
> >
> > Signed-off-by: Mario Six <mario.six@gdsys.cc>
> >
> > ---
> >
> > v2 -> v3:
> > * Removed #ifdef, and used if (!of_live_active()) instead
> > * Removed allocation of property value (caller is now responsible for
> >   that); this also fixes a potential memory leak
> > * Added error handling for malloc and strdup
> > * Fixed style violations
> >
> > v1 -> v2:
> > * Fix potential NULL pointer dereference in ofnode_write_property
> > * Squashed the enable/disable functions into one
> > * Renamed ofnode_set_property to ofnode_write_prop
> >
> > ---
> >  drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
> >  2 files changed, 116 insertions(+)
>
> Reviewed-by: Simon Glass <sjg@chromium.org>

This series seems to be a candidate for the u-boot-dm tree.

I can assign it to you in Patchwork if you want.

Best regards,
Mario
Simon Glass Sept. 27, 2018, 1:41 p.m. UTC | #3
Hi Mario,

On 26 September 2018 at 06:34, Mario Six <mario.six@gdsys.cc> wrote:
>
> Hi Simon,
> On Wed, Jun 27, 2018 at 1:18 AM Simon Glass <sjg@chromium.org> wrote:
> >
> > On 26 June 2018 at 00:46, Mario Six <mario.six@gdsys.cc> wrote:
> > > Implement a set of functions to manipulate properties in a live device
> > > tree:
> > >
> > > * ofnode_write_prop() to set generic properties of a node
> > > * ofnode_write_string() to set string properties of a node
> > > * ofnode_set_enabled() to either enable or disable a node
> > >
> > > Signed-off-by: Mario Six <mario.six@gdsys.cc>
> > >
> > > ---
> > >
> > > v2 -> v3:
> > > * Removed #ifdef, and used if (!of_live_active()) instead
> > > * Removed allocation of property value (caller is now responsible for
> > >   that); this also fixes a potential memory leak
> > > * Added error handling for malloc and strdup
> > > * Fixed style violations
> > >
> > > v1 -> v2:
> > > * Fix potential NULL pointer dereference in ofnode_write_property
> > > * Squashed the enable/disable functions into one
> > > * Renamed ofnode_set_property to ofnode_write_prop
> > >
> > > ---
> > >  drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
> > >  include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
> > >  2 files changed, 116 insertions(+)
> >
> > Reviewed-by: Simon Glass <sjg@chromium.org>
>
> This series seems to be a candidate for the u-boot-dm tree.
>
> I can assign it to you in Patchwork if you want.

OK that's fine with me.

Regards,
Simon
Mario Six Sept. 27, 2018, 1:52 p.m. UTC | #4
Hi Simon,
On Thu, Sep 27, 2018 at 3:42 PM Simon Glass <sjg@chromium.org> wrote:
>
> Hi Mario,
>
> On 26 September 2018 at 06:34, Mario Six <mario.six@gdsys.cc> wrote:
> >
> > Hi Simon,
> > On Wed, Jun 27, 2018 at 1:18 AM Simon Glass <sjg@chromium.org> wrote:
> > >
> > > On 26 June 2018 at 00:46, Mario Six <mario.six@gdsys.cc> wrote:
> > > > Implement a set of functions to manipulate properties in a live device
> > > > tree:
> > > >
> > > > * ofnode_write_prop() to set generic properties of a node
> > > > * ofnode_write_string() to set string properties of a node
> > > > * ofnode_set_enabled() to either enable or disable a node
> > > >
> > > > Signed-off-by: Mario Six <mario.six@gdsys.cc>
> > > >
> > > > ---
> > > >
> > > > v2 -> v3:
> > > > * Removed #ifdef, and used if (!of_live_active()) instead
> > > > * Removed allocation of property value (caller is now responsible for
> > > >   that); this also fixes a potential memory leak
> > > > * Added error handling for malloc and strdup
> > > > * Fixed style violations
> > > >
> > > > v1 -> v2:
> > > > * Fix potential NULL pointer dereference in ofnode_write_property
> > > > * Squashed the enable/disable functions into one
> > > > * Renamed ofnode_set_property to ofnode_write_prop
> > > >
> > > > ---
> > > >  drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
> > > >  include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
> > > >  2 files changed, 116 insertions(+)
> > >
> > > Reviewed-by: Simon Glass <sjg@chromium.org>
> >
> > This series seems to be a candidate for the u-boot-dm tree.
> >
> > I can assign it to you in Patchwork if you want.
>
> OK that's fine with me.
>
Thanks. I reassigned the series.

> Regards,
> Simon
>
Best regards,
Mario
Simon Glass Oct. 2, 2018, 11:20 a.m. UTC | #5
On 27 September 2018 at 06:52, Mario Six <mario.six@gdsys.cc> wrote:
> Hi Simon,
> On Thu, Sep 27, 2018 at 3:42 PM Simon Glass <sjg@chromium.org> wrote:
>>
>> Hi Mario,
>>
>> On 26 September 2018 at 06:34, Mario Six <mario.six@gdsys.cc> wrote:
>> >
>> > Hi Simon,
>> > On Wed, Jun 27, 2018 at 1:18 AM Simon Glass <sjg@chromium.org> wrote:
>> > >
>> > > On 26 June 2018 at 00:46, Mario Six <mario.six@gdsys.cc> wrote:
>> > > > Implement a set of functions to manipulate properties in a live device
>> > > > tree:
>> > > >
>> > > > * ofnode_write_prop() to set generic properties of a node
>> > > > * ofnode_write_string() to set string properties of a node
>> > > > * ofnode_set_enabled() to either enable or disable a node
>> > > >
>> > > > Signed-off-by: Mario Six <mario.six@gdsys.cc>
>> > > >
>> > > > ---
>> > > >
>> > > > v2 -> v3:
>> > > > * Removed #ifdef, and used if (!of_live_active()) instead
>> > > > * Removed allocation of property value (caller is now responsible for
>> > > >   that); this also fixes a potential memory leak
>> > > > * Added error handling for malloc and strdup
>> > > > * Fixed style violations
>> > > >
>> > > > v1 -> v2:
>> > > > * Fix potential NULL pointer dereference in ofnode_write_property
>> > > > * Squashed the enable/disable functions into one
>> > > > * Renamed ofnode_set_property to ofnode_write_prop
>> > > >
>> > > > ---
>> > > >  drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
>> > > >  include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
>> > > >  2 files changed, 116 insertions(+)
>> > >
>> > > Reviewed-by: Simon Glass <sjg@chromium.org>
>> >
>> > This series seems to be a candidate for the u-boot-dm tree.
>> >
>> > I can assign it to you in Patchwork if you want.
>>
>> OK that's fine with me.
>>
> Thanks. I reassigned the series.

Applied to u-boot-dm, and now in mainline, thanks!
diff mbox series

Patch

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 3cf3205a2f1..e8becf7f04d 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -697,3 +697,73 @@  int ofnode_device_is_compatible(ofnode node, const char *compat)
 						  ofnode_to_offset(node),
 						  compat);
 }
+
+int ofnode_write_prop(ofnode node, const char *propname, int len,
+		      const void *value)
+{
+	const struct device_node *np = ofnode_to_np(node);
+	struct property *pp;
+	struct property *pp_last = NULL;
+	struct property *new;
+
+	if (!of_live_active())
+		return -ENOSYS;
+
+	if (!np)
+		return -EINVAL;
+
+	for (pp = np->properties; pp; pp = pp->next) {
+		if (strcmp(pp->name, propname) == 0) {
+			/* Property exists -> change value */
+			pp->value = (void *)value;
+			pp->length = len;
+			return 0;
+		}
+		pp_last = pp;
+	}
+
+	if (!pp_last)
+		return -ENOENT;
+
+	/* Property does not exist -> append new property */
+	new = malloc(sizeof(struct property));
+	if (!new)
+		return -ENOMEM;
+
+	new->name = strdup(propname);
+	if (!new->name)
+		return -ENOMEM;
+
+	new->value = (void *)value;
+	new->length = len;
+	new->next = NULL;
+
+	pp_last->next = new;
+
+	return 0;
+}
+
+int ofnode_write_string(ofnode node, const char *propname, const char *value)
+{
+	if (!of_live_active())
+		return -ENOSYS;
+
+	assert(ofnode_valid(node));
+
+	debug("%s: %s = %s", __func__, propname, value);
+
+	return ofnode_write_prop(node, propname, strlen(value) + 1, value);
+}
+
+int ofnode_set_enabled(ofnode node, bool value)
+{
+	if (!of_live_active())
+		return -ENOSYS;
+
+	assert(ofnode_valid(node));
+
+	if (value)
+		return ofnode_write_string(node, "status", "okay");
+	else
+		return ofnode_write_string(node, "status", "disable");
+}
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 5af6b7e616a..1d60858c337 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -691,4 +691,50 @@  u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
  * @return true if OK, false if the compatible is not found
  */
 int ofnode_device_is_compatible(ofnode node, const char *compat);
+
+/**
+ * ofnode_write_prop() - Set a property of a ofnode
+ *
+ * Note that the value passed to the function is *not* allocated by the
+ * function itself, but must be allocated by the caller if necessary.
+ *
+ * @node:	The node for whose property should be set
+ * @propname:	The name of the property to set
+ * @len:	The length of the new value of the property
+ * @value:	The new value of the property (must be valid prior to calling
+ *		the function)
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_write_prop(ofnode node, const char *propname, int len,
+		      const void *value);
+
+/**
+ * ofnode_write_string() - Set a string property of a ofnode
+ *
+ * Note that the value passed to the function is *not* allocated by the
+ * function itself, but must be allocated by the caller if necessary.
+ *
+ * @node:	The node for whose string property should be set
+ * @propname:	The name of the string property to set
+ * @value:	The new value of the string property (must be valid prior to
+ *		calling the function)
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_write_string(ofnode node, const char *propname, const char *value);
+
+/**
+ * ofnode_set_enabled() - Enable or disable a device tree node given by its
+ *			  ofnode
+ *
+ * This function effectively sets the node's "status" property to either "okay"
+ * or "disable", hence making it available for driver model initialization or
+ * not.
+ *
+ * @node:	The node to enable
+ * @value:	Flag that tells the function to either disable or enable the
+ *		node
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_set_enabled(ofnode node, bool value);
+
 #endif