[PATCH v8 1/7] of: add of_property_write_string_array()

Fabian Pflug
2026-04-21T10:30:07Z

Helper function to write an array of string into a property.

Signed-off-by: Fabian Pflug <f.pflug@pengutronix.de>
---
drivers/of/base.c | 43 +++++++++++++++++++++++++++++++++++++++++++
include/of.h | 3 +++
test/self/of_manipulation.c | 14 +++++++++++++-
test/self/of_manipulation.dts | 5 +++++
4 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 460b5e2f4b..376a2ac6a8 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1570,6 +1570,49 @@ int of_property_write_u64_array(struct device_node *np,
return 0;
}

+/**
+ * of_property_write_string_array - 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.
+ * @values: pointer to array elements to write.
+ * @sz: number of array elements 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_string_array(struct device_node *np,
+ const char *propname, const char **values,
+ size_t sz)
+{
+ char *buf = NULL, *next;
+ size_t len = 0;
+ int ret = 0, i;
+
+ for (i = 0; i < sz; i++)
+ len += strlen(values[i]) + 1;
+
+ if (!len)
+ return -EINVAL;
+
+ buf = malloc(len);
+ if (!buf)
+ return -ENOMEM;
+
+ next = buf;
+
+ for (i = 0; i < sz; i++)
+ next = stpcpy(next, values[i]) + 1;
+
+ ret = of_set_property(np, propname, buf, len, 1);
+ free(buf);
+ return ret;
+}
+
/**
* of_property_write_strings - Write strings to a property. If
* the property does not exist, it will be created and appended to the given
diff --git a/include/of.h b/include/of.h
index ba8d1e358d..34439ee763 100644
--- a/include/of.h
+++ b/include/of.h
@@ -310,6 +310,9 @@ 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__));
+int of_property_write_string_array(struct device_node *np,
+ const char *propname, const char **values,
+ size_t sz);
int of_property_sprintf(struct device_node *np, const char *propname, const char *fmt, ...)
__printf(3, 4);

diff --git a/test/self/of_manipulation.c b/test/self/of_manipulation.c
index 8d645b1137..faf948a17b 100644
--- a/test/self/of_manipulation.c
+++ b/test/self/of_manipulation.c
@@ -71,13 +71,19 @@ 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, *np4;
+ struct device_node *np1, *np2, *np3, *np4, *np5;
char properties[] = "ayy\0bee\0sea";
+ static const char * const prop_array[] = {
+ "ayy",
+ "bee\0\0\0",
+ "sea\0",
+ };

np1 = of_new_node(root, "np1");
np2 = of_new_node(root, "np2");
np3 = of_new_node(root, "np3");
np4 = of_new_node(root, "np4");
+ np5 = of_new_node(root, "np5");

of_property_sprintf(np1, "property-single", "%c%c%c", 'a', 'y', 'y');

@@ -108,6 +114,12 @@ static void test_of_property_strings(struct device_node *root)
of_prepend_property(np4, "property-multi", "ayy", 4);

assert_equal(np3, np4);
+
+ of_property_write_string_array(np5, "property-single", prop_array, 1);
+
+ of_property_write_string_array(np5, "property-multi", prop_array, 3);
+
+ assert_equal(np4, np5);
}

static void __init test_of_manipulation(void)
diff --git a/test/self/of_manipulation.dts b/test/self/of_manipulation.dts
index 2cc6773fa9..7a1fb1217d 100644
--- a/test/self/of_manipulation.dts
+++ b/test/self/of_manipulation.dts
@@ -34,4 +34,9 @@ np4 {
property-single = "ayy";
property-multi = "ayy", "bee", "sea";
};
+
+ np5 {
+ property-single = "ayy";
+ property-multi = "ayy", "bee", "sea";
+ };
};

--
2.47.3