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>
This commit is contained in:
Simon Glass
2022-09-06 20:27:32 -06:00
committed by Tom Rini
parent 0d63213c1e
commit 0b58eaa89c
4 changed files with 92 additions and 13 deletions

View File

@@ -1350,19 +1350,23 @@ int ofnode_device_is_compatible(ofnode node, const char *compat);
/**
* ofnode_write_prop() - Set a property of a ofnode
*
* Note that the value passed to the function is *not* allocated by the
* function itself, but must be allocated by the caller if necessary. However
* it does allocate memory for the property struct and name.
* Note that if @copy is false, the value passed to the function is *not*
* allocated by the function itself, but must be allocated by the caller if
* necessary. However it does allocate memory for the property struct and name.
*
* @node: The node for whose property should be set
* @propname: The name of the property to set
* @value: The new value of the property (must be valid prior to calling
* the function)
* @len: The length of the new value of the property
* @copy: true to allocate memory for the value. This only has any effect with
* live tree, since flat tree handles this automatically. It allows a
* node's value to be written to the tree, without requiring that the
* caller allocate it
* Return: 0 if successful, -ve on error
*/
int ofnode_write_prop(ofnode node, const char *propname, const void *value,
int len);
int len, bool copy);
/**
* ofnode_write_string() - Set a string property of a ofnode