The function is copied from barebox and based on commit

| commit 09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb
| Author: Ahmad Fatoum <a.fat...@pengutronix.de>
| Date:   Wed Sep 30 14:53:01 2020 +0200
|
|     of: implement of_property_write_strings for multiple strings
|
|     The current way to set a property with multiple values (e.g. compatible
|     strings) is to have
|
|       char properties[] = "st,stm32mp157c-dk2\0st,stm32mp157";
|       of_set_property(np, "compatible", properties, sizeof(properties), 1);
|
|     Add a new helper to make this easier at the cost of one runtime
|     reallocation:
|
|       of_property_write_strings(np, "compatible,
|             "st,stm32mp157c-dk2", "st,stm32mp157", NULL);
|
|     Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de>
|     Signed-off-by: Sascha Hauer <s.ha...@pengutronix.de>

Signed-off-by: Marco Felsch <m.fel...@pengutronix.de>
---
 src/dt/dt.h         |  2 ++
 src/libdt-utils.sym |  1 +
 src/libdt.c         | 47 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/dt/dt.h b/src/dt/dt.h
index 4ae24ba..97dd855 100644
--- a/src/dt/dt.h
+++ b/src/dt/dt.h
@@ -195,6 +195,8 @@ extern int of_property_write_u64_array(struct device_node 
*np,
                                size_t sz);
 extern int of_property_write_string(struct device_node *np,
                                const char *propname, const char *value);
+extern int of_property_write_strings(struct device_node *np,
+               const char *propname, ...) __attribute__((__sentinel__));
 
 extern struct device_node *of_parse_phandle(const struct device_node *np,
                                            const char *phandle_name,
diff --git a/src/libdt-utils.sym b/src/libdt-utils.sym
index 2c63d55..a749317 100644
--- a/src/libdt-utils.sym
+++ b/src/libdt-utils.sym
@@ -64,6 +64,7 @@ global:
        of_property_read_u8_array;
        of_property_write_bool;
        of_property_write_string;
+       of_property_write_strings;
        of_property_write_u16_array;
        of_property_write_u32_array;
        of_property_write_u64_array;
diff --git a/src/libdt.c b/src/libdt.c
index 59e76d3..f18ea90 100644
--- a/src/libdt.c
+++ b/src/libdt.c
@@ -1208,6 +1208,53 @@ int of_property_write_u64_array(struct device_node *np,
        return 0;
 }
 
+/**
+ * of_property_write_strings - Write strings to a property. If
+ * the property does not exist, it will be created and appended to the given
+ * device node.
+ *
+ * @np:                device node to which the property value is to be 
written.
+ * @propname:  name of the property to be written.
+ * @...:       pointers to strings to write
+ *
+ * Search for a property in a device node and write a string to
+ * it. If the property does not exist, it will be created and appended to
+ * the device node. Returns 0 on success, -ENOMEM if the property or array
+ * of elements cannot be created, -EINVAL if no strings specified.
+ */
+int of_property_write_strings(struct device_node *np,
+                             const char *propname, ...)
+{
+       const char *val;
+       char *buf = NULL, *next;
+       size_t len = 0;
+       va_list ap;
+       int ret = 0;
+
+       va_start(ap, propname);
+       for (val = va_arg(ap, char *); val; val = va_arg(ap, char *))
+               len += strlen(val) + 1;
+       va_end(ap);
+
+       if (!len)
+               return -EINVAL;
+
+       buf = malloc(len);
+       if (!buf)
+               return -ENOMEM;
+
+       next = buf;
+
+       va_start(ap, propname);
+       for (val = va_arg(ap, char *); val; val = va_arg(ap, char *))
+               next = stpcpy(next, val) + 1;
+       va_end(ap);
+
+       ret = of_set_property(np, propname, buf, len, 1);
+       free(buf);
+       return ret;
+}
+
 /**
  * of_property_write_string - Write a string to a property. If
  * the property does not exist, it will be created and appended to the given
-- 
2.30.2


Reply via email to