aboutsummaryrefslogtreecommitdiff
path: root/pylibfdt
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2018-06-13 21:44:43 -0600
committerDavid Gibson <david@gibson.dropbear.id.au>2018-06-14 14:07:40 +1000
commit2f0d07e678e05f260b575342867a26d466bdec85 (patch)
tree6c4987ff6139b4a1ad7a45f8f9c9aa19df95057c /pylibfdt
parent354d3dc55939499954b5e1f4948517da24591cd2 (diff)
downloaddtc-2f0d07e678e05f260b575342867a26d466bdec85.zip
dtc-2f0d07e678e05f260b575342867a26d466bdec85.tar.gz
dtc-2f0d07e678e05f260b575342867a26d466bdec85.tar.bz2
pylibfdt: Add functions to set and get properties as strings
It is common to want to set a property to a nul-terminated string in a device tree. Add python methods to handle this. Signed-off-by: Simon Glass <sjg@chromium.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'pylibfdt')
-rw-r--r--pylibfdt/libfdt.i30
1 files changed, 30 insertions, 0 deletions
diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index c0a2191..2896513 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -589,6 +589,28 @@ class Fdt:
return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
quiet)
+ def setprop_str(self, nodeoffset, prop_name, val, quiet=()):
+ """Set the string value of a property
+
+ The property is set to the string, with a nul terminator added
+
+ Args:
+ nodeoffset: Node offset containing the property to create/update
+ prop_name: Name of property
+ val: Value to write (string without nul terminator). Unicode is
+ supposed by encoding to UTF-8
+ quiet: Errors to ignore (empty to raise on all errors)
+
+ Returns:
+ Error code, or 0 if OK
+
+ Raises:
+ FdtException if no parent found or other error occurs
+ """
+ val = val.encode('utf-8') + '\0'
+ return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
+ val, len(val)), quiet)
+
def delprop(self, nodeoffset, prop_name):
"""Delete a property from a node
@@ -646,6 +668,14 @@ class Property(bytearray):
def as_int64(self):
return self.as_cell('q')
+
+ def as_str(self):
+ """Unicode is supported by decoding from UTF-8"""
+ if self[-1] != 0:
+ raise ValueError('Property lacks nul termination')
+ if 0 in self[:-1]:
+ raise ValueError('Property contains embedded nul characters')
+ return self[:-1].decode('utf-8')
%}
%rename(fdt_property) fdt_property_func;