mirror of https://gitlab.com/qemu-project/dtc.git
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>
This commit is contained in:
parent
354d3dc559
commit
2f0d07e678
|
@ -589,6 +589,28 @@ class Fdt:
|
||||||
return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
|
return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
|
||||||
quiet)
|
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):
|
def delprop(self, nodeoffset, prop_name):
|
||||||
"""Delete a property from a node
|
"""Delete a property from a node
|
||||||
|
|
||||||
|
@ -646,6 +668,14 @@ class Property(bytearray):
|
||||||
|
|
||||||
def as_int64(self):
|
def as_int64(self):
|
||||||
return self.as_cell('q')
|
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;
|
%rename(fdt_property) fdt_property_func;
|
||||||
|
|
|
@ -68,6 +68,8 @@ TEST_VALUE64_1L = 0x01abcdef
|
||||||
TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
|
TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
|
||||||
|
|
||||||
TEST_STRING_1 = 'hello world'
|
TEST_STRING_1 = 'hello world'
|
||||||
|
TEST_STRING_2 = 'hi world'
|
||||||
|
TEST_STRING_3 = u'unicode ' + unichr(467)
|
||||||
|
|
||||||
|
|
||||||
def get_err(err_code):
|
def get_err(err_code):
|
||||||
|
@ -440,6 +442,29 @@ class PyLibfdtTests(unittest.TestCase):
|
||||||
self.assertEquals(struct.pack('>Q', TEST_VALUE64_1),
|
self.assertEquals(struct.pack('>Q', TEST_VALUE64_1),
|
||||||
self.fdt.getprop(node, prop))
|
self.fdt.getprop(node, prop))
|
||||||
|
|
||||||
|
def testSetPropStr(self):
|
||||||
|
"""Test that we can set a property to a particular string"""
|
||||||
|
node = 0
|
||||||
|
prop = 'prop-str'
|
||||||
|
self.assertEquals(TEST_STRING_1, self.fdt.getprop(node, prop).as_str())
|
||||||
|
self.fdt.setprop_str(node, prop, TEST_STRING_2)
|
||||||
|
self.assertEquals(TEST_STRING_2, self.fdt.getprop(node, prop).as_str())
|
||||||
|
with self.assertRaises(ValueError) as e:
|
||||||
|
self.fdt.getprop(node, 'prop-int').as_str()
|
||||||
|
self.assertIn('lacks nul termination', str(e.exception))
|
||||||
|
|
||||||
|
node2 = self.fdt.path_offset('/subnode@1/subsubnode')
|
||||||
|
with self.assertRaises(ValueError) as e:
|
||||||
|
self.fdt.getprop(node2, 'compatible').as_str()
|
||||||
|
self.assertIn('embedded nul', str(e.exception))
|
||||||
|
|
||||||
|
# Expand the device tree so we now have room
|
||||||
|
self.fdt.resize(self.fdt.totalsize() + 50)
|
||||||
|
prop = 'prop-unicode'
|
||||||
|
self.fdt.setprop_str(node, prop, TEST_STRING_3)
|
||||||
|
self.assertEquals(TEST_STRING_3,
|
||||||
|
self.fdt.getprop(node, prop).as_str())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue