A string array in device tree is simply a series of \0 terminated
strings next to each other. As libfdt doesn't support that directly
we need to build it ourselves.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20201105175153.30489-4-alex.bennee@linaro.org>
---
v2
- checkpatch long line fix
---
include/sysemu/device_tree.h | 17 +++++++++++++++++
softmmu/device_tree.c | 26 ++++++++++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
index 982c89345f..8a2fe55622 100644
--- a/include/sysemu/device_tree.h
+++ b/include/sysemu/device_tree.h
@@ -70,6 +70,23 @@ int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
const char *property, uint64_t val);
int qemu_fdt_setprop_string(void *fdt, const char *node_path,
const char *property, const char *string);
+
+/**
+ * qemu_fdt_setprop_string_array: set a string array property
+ *
+ * @fdt: pointer to the dt blob
+ * @name: node name
+ * @prop: property array
+ * @array: pointer to an array of string pointers
+ * @len: length of array
+ *
+ * assigns a string array to a property. This function converts and
+ * array of strings to a sequential string with \0 separators before
+ * setting the property.
+ */
+int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
+ const char *prop, char **array, int len);
+
int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
const char *property,
const char *target_node_path);
diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
index b9a3ddc518..2691c58cf6 100644
--- a/softmmu/device_tree.c
+++ b/softmmu/device_tree.c
@@ -21,6 +21,7 @@
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "qemu/bswap.h"
+#include "qemu/cutils.h"
#include "sysemu/device_tree.h"
#include "sysemu/sysemu.h"
#include "hw/loader.h"
@@ -397,6 +398,31 @@ int qemu_fdt_setprop_string(void *fdt, const char *node_path,
return r;
}
+/*
+ * libfdt doesn't allow us to add string arrays directly but they are
+ * test a series of null terminated strings with a length. We build
+ * the string up here so we can calculate the final length.
+ */
+int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
+ const char *prop, char **array, int len)
+{
+ int ret, i, total_len = 0;
+ char *str, *p;
+ for (i = 0; i < len; i++) {
+ total_len += strlen(array[i]) + 1;
+ }
+ p = str = g_malloc0(total_len);
+ for (i = 0; i < len; i++) {
+ int len = strlen(array[i]) + 1;
+ pstrcpy(p, len, array[i]);
+ p += len;
+ }
+
+ ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
+ g_free(str);
+ return ret;
+}
+
const void *qemu_fdt_getprop(void *fdt, const char *node_path,
const char *property, int *lenp, Error **errp)
{
--
2.20.1
On Thu, Feb 11, 2021 at 05:19:41PM +0000, Alex Bennée wrote: > A string array in device tree is simply a series of \0 terminated > strings next to each other. As libfdt doesn't support that directly > we need to build it ourselves. Hm, that might not make a bad extension to libfdt... > > Signed-off-by: Alex Bennée <alex.bennee@linaro.org> > Reviewed-by: Alistair Francis <alistair.francis@wdc.com> > Message-Id: <20201105175153.30489-4-alex.bennee@linaro.org> > > --- > v2 > - checkpatch long line fix > --- > include/sysemu/device_tree.h | 17 +++++++++++++++++ > softmmu/device_tree.c | 26 ++++++++++++++++++++++++++ > 2 files changed, 43 insertions(+) > > diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h > index 982c89345f..8a2fe55622 100644 > --- a/include/sysemu/device_tree.h > +++ b/include/sysemu/device_tree.h > @@ -70,6 +70,23 @@ int qemu_fdt_setprop_u64(void *fdt, const char *node_path, > const char *property, uint64_t val); > int qemu_fdt_setprop_string(void *fdt, const char *node_path, > const char *property, const char *string); > + > +/** > + * qemu_fdt_setprop_string_array: set a string array property > + * > + * @fdt: pointer to the dt blob > + * @name: node name > + * @prop: property array > + * @array: pointer to an array of string pointers > + * @len: length of array > + * > + * assigns a string array to a property. This function converts and > + * array of strings to a sequential string with \0 separators before > + * setting the property. > + */ > +int qemu_fdt_setprop_string_array(void *fdt, const char *node_path, > + const char *prop, char **array, int len); > + > int qemu_fdt_setprop_phandle(void *fdt, const char *node_path, > const char *property, > const char *target_node_path); > diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c > index b9a3ddc518..2691c58cf6 100644 > --- a/softmmu/device_tree.c > +++ b/softmmu/device_tree.c > @@ -21,6 +21,7 @@ > #include "qemu/error-report.h" > #include "qemu/option.h" > #include "qemu/bswap.h" > +#include "qemu/cutils.h" > #include "sysemu/device_tree.h" > #include "sysemu/sysemu.h" > #include "hw/loader.h" > @@ -397,6 +398,31 @@ int qemu_fdt_setprop_string(void *fdt, const char *node_path, > return r; > } > > +/* > + * libfdt doesn't allow us to add string arrays directly but they are > + * test a series of null terminated strings with a length. We build > + * the string up here so we can calculate the final length. > + */ > +int qemu_fdt_setprop_string_array(void *fdt, const char *node_path, > + const char *prop, char **array, int len) > +{ > + int ret, i, total_len = 0; > + char *str, *p; > + for (i = 0; i < len; i++) { > + total_len += strlen(array[i]) + 1; > + } > + p = str = g_malloc0(total_len); > + for (i = 0; i < len; i++) { > + int len = strlen(array[i]) + 1; > + pstrcpy(p, len, array[i]); > + p += len; > + } > + > + ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len); > + g_free(str); > + return ret; > +} > + > const void *qemu_fdt_getprop(void *fdt, const char *node_path, > const char *property, int *lenp, Error **errp) > { -- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
© 2016 - 2024 Red Hat, Inc.