mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: oss-tools@pengutronix.de
Cc: mfe@pengutronix.de
Subject: [OSS-Tools] [PATCH dt-utils 12/14] libdt: add of_property_write_strings support
Date: Fri, 14 Oct 2022 18:42:02 +0200	[thread overview]
Message-ID: <20221014164204.3812506-13-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20221014164204.3812506-1-m.felsch@pengutronix.de>

The function is copied from barebox and based on commit

| commit 09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb
| Author: Ahmad Fatoum <a.fatoum@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.fatoum@pengutronix.de>
|     Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Signed-off-by: Marco Felsch <m.felsch@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




  parent reply	other threads:[~2022-10-14 16:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-14 16:41 [OSS-Tools] [PATCH dt-utils 00/14] Sync Barebox-State code base Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 01/14] state: Remove duplicate incudes Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 02/14] state: backend_raw: fix ignoring unpack failures Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 03/14] state: backend_storage: deal gracefully with runtime bucket corruption Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 04/14] state: treat state with all-invalid buckets as dirty Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 05/14] state: remove param member from struct state_string Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 06/14] state: remove param member from state_uint32, state_enum32, state_mac Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 07/14] state: remove unused function Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 08/14] state: propagate failure to fixup enum32 into DT Marco Felsch
2022-10-14 16:41 ` [OSS-Tools] [PATCH dt-utils 09/14] state: add SPDX-License-Identifier for files without explicit license Marco Felsch
2022-10-14 16:42 ` [OSS-Tools] [PATCH dt-utils 10/14] state: fix typos found with codespell Marco Felsch
2022-10-14 16:42 ` [OSS-Tools] [PATCH dt-utils 11/14] common: xstrdup: don't panic on xstrdup(NULL) Marco Felsch
2022-10-14 16:42 ` Marco Felsch [this message]
2022-10-14 16:42 ` [OSS-Tools] [PATCH dt-utils 13/14] libdt: add partition search function Marco Felsch
2022-10-14 16:42 ` [OSS-Tools] [PATCH dt-utils 14/14] state: sync with barebox to support new backend type Marco Felsch
2022-10-21  7:37 ` [OSS-Tools] [PATCH dt-utils 00/14] Sync Barebox-State code base Marco Felsch
2023-06-05 13:12 ` Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221014164204.3812506-13-m.felsch@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=mfe@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox