From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Fri, 14 Oct 2022 18:42:15 +0200 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ojNlI-008iEQ-Ef for lore@lore.pengutronix.de; Fri, 14 Oct 2022 18:42:15 +0200 Received: from localhost ([127.0.0.1] helo=metis.ext.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1ojNlE-0006Qm-HO; Fri, 14 Oct 2022 18:42:12 +0200 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ojNlB-0006Fv-Ej; Fri, 14 Oct 2022 18:42:09 +0200 Received: from [2a0a:edc0:0:1101:1d::28] (helo=dude02.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1ojNlA-001WTt-P8; Fri, 14 Oct 2022 18:42:08 +0200 Received: from mfe by dude02.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1ojNl8-00Fzpm-Qb; Fri, 14 Oct 2022 18:42:06 +0200 From: Marco Felsch To: oss-tools@pengutronix.de Date: Fri, 14 Oct 2022 18:42:02 +0200 Message-Id: <20221014164204.3812506-13-m.felsch@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221014164204.3812506-1-m.felsch@pengutronix.de> References: <20221014164204.3812506-1-m.felsch@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [OSS-Tools] [PATCH dt-utils 12/14] libdt: add of_property_write_strings support X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mfe@pengutronix.de Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false The function is copied from barebox and based on commit | commit 09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb | Author: Ahmad Fatoum | 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 | Signed-off-by: Sascha Hauer Signed-off-by: Marco Felsch --- 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