From b770f3d1c13f63a22c7015bd2438dbc1164b5362 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 13 Jun 2018 21:44:44 -0600 Subject: [PATCH] pylibfdt: Support setting the name of a node Add a method to call fdt_set_name(). Signed-off-by: Simon Glass Signed-off-by: David Gibson --- pylibfdt/libfdt.i | 17 +++++++++++++++++ tests/pylibfdt_tests.py | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i index 2896513..aed5390 100644 --- a/pylibfdt/libfdt.i +++ b/pylibfdt/libfdt.i @@ -535,6 +535,23 @@ class Fdt: """ return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet) + def set_name(self, nodeoffset, name, quiet=()): + """Set the name of a node + + Args: + nodeoffset: Node offset of node to update + name: New node name (string without \0) + + Returns: + Error code, or 0 if OK + + Raises: + FdtException if no parent found or other error occurs + """ + if chr(0) in name: + raise ValueError('Property contains embedded nul characters') + return check_err(fdt_set_name(self._fdt, nodeoffset, name), quiet) + def setprop(self, nodeoffset, prop_name, val, quiet=()): """Set the value of a property diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py index f3be0ac..9f3e55a 100644 --- a/tests/pylibfdt_tests.py +++ b/tests/pylibfdt_tests.py @@ -465,6 +465,21 @@ class PyLibfdtTests(unittest.TestCase): self.assertEquals(TEST_STRING_3, self.fdt.getprop(node, prop).as_str()) + def testSetName(self): + """Test that we can update a node name""" + node = self.fdt.path_offset('/subnode@1') + old_val = self.fdt.get_name(node) + self.fdt.set_name(node, 'test') + self.assertEquals('test', self.fdt.get_name(node)) + + with self.assertRaises(ValueError) as e: + self.fdt.set_name(node, 'some\0name') + self.assertIn('embedded nul', str(e.exception)) + + with self.assertRaises(ValueError) as e: + self.fdt.set_name(node, 'name\0') + self.assertIn('embedded nul', str(e.exception)) + if __name__ == "__main__": unittest.main()