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>
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 2c4d521..39636a5 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -1388,15 +1388,27 @@
}
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 @@
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 @@
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)