diff options
author | Simon Glass <sjg@chromium.org> | 2018-09-11 03:28:39 -0600 |
---|---|---|
committer | David Gibson <david@gibson.dropbear.id.au> | 2018-09-13 10:16:34 +1000 |
commit | bfbfab047e45445dd59bcdba98b61843e086d0d2 (patch) | |
tree | 5af0971632451a1ca5d3491d2f34e3dc6c7285c6 | |
parent | 9005f4108e7c440764153d3ea0c85e927a75ae55 (diff) | |
download | dtc-bfbfab047e45445dd59bcdba98b61843e086d0d2.zip dtc-bfbfab047e45445dd59bcdba98b61843e086d0d2.tar.gz dtc-bfbfab047e45445dd59bcdba98b61843e086d0d2.tar.bz2 |
pylibfdt: Add a means to add and delete notes
These methods are needed to permit larger changes to the device tree blob.
Add two new methods and an associate test.
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r-- | pylibfdt/libfdt.i | 29 | ||||
-rw-r--r-- | tests/pylibfdt_tests.py | 31 |
2 files changed, 60 insertions, 0 deletions
diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i index 12d2812..842e7b6 100644 --- a/pylibfdt/libfdt.i +++ b/pylibfdt/libfdt.i @@ -689,6 +689,35 @@ class Fdt(FdtRo): """ return check_err(fdt_delprop(self._fdt, nodeoffset, prop_name), quiet) + def add_subnode(self, parentoffset, name, quiet=()): + """Add a new subnode to a node + + Args: + parentoffset: Parent offset to add the subnode to + name: Name of node to add + + Returns: + offset of the node created, or negative error code on failure + + Raises: + FdtError if there is not enough space, or another error occurs + """ + return check_err(fdt_add_subnode(self._fdt, parentoffset, name), quiet) + + def del_node(self, nodeoffset, quiet=()): + """Delete a node + + Args: + nodeoffset: Offset of node to delete + + Returns: + Error code, or 0 if OK + + Raises: + FdtError if an error occurs + """ + return check_err(fdt_del_node(self._fdt, nodeoffset), quiet) + class Property(bytearray): """Holds a device tree property name and value. diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py index e61fda9..fbb1ce2 100644 --- a/tests/pylibfdt_tests.py +++ b/tests/pylibfdt_tests.py @@ -139,6 +139,24 @@ class PyLibfdtBasicTests(unittest.TestCase): poffset = self.fdt.next_property_offset(poffset, QUIET_NOTFOUND) return prop_list + def GetSubnodes(self, node_path): + """Read a list of subnodes from a node + + Args: + node_path: Full path to node, e.g. '/subnode@1/subsubnode' + + Returns: + List of subnode names for that node, e.g. ['subsubnode', 'ss1'] + """ + subnode_list = [] + node = self.fdt.path_offset(node_path) + offset = self.fdt.first_subnode(node, QUIET_NOTFOUND) + while offset > 0: + name = self.fdt.get_name(offset) + subnode_list.append(name) + offset = self.fdt.next_subnode(offset, QUIET_NOTFOUND) + return subnode_list + def testImport(self): """Check that we can import the library correctly""" self.assertEquals(type(libfdt), types.ModuleType) @@ -495,6 +513,19 @@ class PyLibfdtBasicTests(unittest.TestCase): self.fdt.set_name(node, 'name\0') self.assertIn('embedded nul', str(e.exception)) + def testAddDeleteNodes(self): + """Test that we can add and delete nodes""" + node_name = '/subnode@1' + self.assertEquals(self.GetSubnodes(node_name), ['subsubnode', 'ss1']) + node = self.fdt.path_offset('%s/subsubnode' % node_name) + self.assertEquals(self.fdt.del_node(node, 'subsubnode'), 0) + self.assertEquals(self.GetSubnodes(node_name), ['ss1']) + + node = self.fdt.path_offset(node_name) + offset = self.fdt.add_subnode(node, 'more') + self.assertTrue(offset > 0) + self.assertEquals(self.GetSubnodes(node_name), ['more', 'ss1']) + class PyLibfdtSwTests(unittest.TestCase): """Test class for pylibfdt sequential-write DT creation |