aboutsummaryrefslogtreecommitdiff
path: root/drivers/core/ofnode.c
diff options
context:
space:
mode:
authorSimon Glass2022-09-06 20:27:32 -0600
committerTom Rini2022-09-29 22:43:43 -0400
commit0b58eaa89c4d7a4aea1f7f63ff4aca2c2f1d90c4 (patch)
tree2cfa68b6cbd711ec4ec34e92aa1320930e6a8fd4 /drivers/core/ofnode.c
parent0d63213c1ed5246423c22cbe3c6798a09763a2b9 (diff)
dm: core: Allow copying ofnode property data when writing
At present ofnode_write_prop() is inconsistent between livetree and flattree, in that livetree requires the caller to ensure the property value is stable (e.g. in rodata or allocated) but flattree does not, since it makes a copy. This makes the API call a bit painful to use, since the caller must do different things depending on OF_LIVE. Add a new 'copy' argument which tells the function to make a copy if needed. Add some tests to cover this behaviour. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'drivers/core/ofnode.c')
-rw-r--r--drivers/core/ofnode.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 2c4d5210095..39636a5a18c 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -1388,15 +1388,27 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname,
}
int ofnode_write_prop(ofnode node, const char *propname, const void *value,
- int len)
+ int len, bool copy)
{
- if (of_live_active())
- return of_write_prop(ofnode_to_np(node), propname, len, value);
- else
+ if (of_live_active()) {
+ void *newval;
+ int ret;
+
+ if (copy) {
+ newval = malloc(len);
+ if (!newval)
+ return log_ret(-ENOMEM);
+ memcpy(newval, value, len);
+ value = newval;
+ }
+ ret = of_write_prop(ofnode_to_np(node), propname, len, value);
+ if (ret && copy)
+ free(newval);
+ return ret;
+ } else {
return fdt_setprop(ofnode_to_fdt(node), ofnode_to_offset(node),
propname, value, len);
-
- return 0;
+ }
}
int ofnode_write_string(ofnode node, const char *propname, const char *value)
@@ -1405,7 +1417,8 @@ int ofnode_write_string(ofnode node, const char *propname, const char *value)
debug("%s: %s = %s", __func__, propname, value);
- return ofnode_write_prop(node, propname, value, strlen(value) + 1);
+ return ofnode_write_prop(node, propname, value, strlen(value) + 1,
+ false);
}
int ofnode_write_u32(ofnode node, const char *propname, u32 value)
@@ -1420,7 +1433,7 @@ int ofnode_write_u32(ofnode node, const char *propname, u32 value)
return -ENOMEM;
*val = cpu_to_fdt32(value);
- return ofnode_write_prop(node, propname, val, sizeof(value));
+ return ofnode_write_prop(node, propname, val, sizeof(value), false);
}
int ofnode_set_enabled(ofnode node, bool value)