Provide a way to specify a phandle list of nodes which are to be inserted
into an existing node.

Signed-off-by: Simon Glass <[email protected]>
---

Changes in v3:
- Adjust to use the new example file

 tools/dtoc/fdt.py                  | 19 +++++++++++++++++++
 tools/dtoc/test/dtoc_test_copy.dts | 13 ++++++++++++-
 tools/dtoc/test_fdt.py             | 25 ++++++++++++++++++++++++-
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index dcd78b9e5fbf..4375d3923fbf 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -732,6 +732,25 @@ class Node:
         for node in reversed(src.subnodes):
             dst.copy_node(node)
 
+    def copy_subnodes_from_phandles(self, phandle_list):
+        """Copy subnodes of a list of nodes into another node
+
+        Args:
+            phandle_list (list of int): List of phandles of nodes to copy
+
+        For each node in the phandle list, its subnodes and their properties 
are
+        copied recursively. Note that it does not copy the node itself, nor its
+        properties.
+        """
+        # Process in reverse order, since new nodes are inserted at the start 
of
+        # the destination's node list. We want them to appear in order of the
+        # phandle list
+        for phandle in phandle_list.__reversed__():
+            parent = self.GetFdt().LookupPhandle(phandle)
+            tout.debug(f'adding template {parent.path} to node {self.path}')
+            for node in parent.subnodes.__reversed__():
+                self.copy_node(node)
+
 
 class Fdt:
     """Provides simple access to a flat device tree blob using libfdts.
diff --git a/tools/dtoc/test/dtoc_test_copy.dts 
b/tools/dtoc/test/dtoc_test_copy.dts
index bf2b50e92d71..1939256349fe 100644
--- a/tools/dtoc/test/dtoc_test_copy.dts
+++ b/tools/dtoc/test/dtoc_test_copy.dts
@@ -10,6 +10,7 @@
 / {
        #address-cells = <1>;
        #size-cells = <1>;
+       copy-list = <&another &base>;
 
        dest {
                bootph-all;
@@ -45,7 +46,7 @@
                };
        };
 
-       base {
+       base: base {
                compatible = "sandbox,i2c";
                bootph-all;
                #address-cells = <1>;
@@ -72,4 +73,14 @@
                        };
                };
        };
+
+       another: another {
+               earlier {
+                       wibble = <2>;
+               };
+
+               later {
+                       fibble = <3>;
+               };
+       };
 };
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index b3593cbee8b2..0ee4956591f9 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -316,7 +316,8 @@ class TestNode(unittest.TestCase):
             chk = dtb.GetNode('/dest/base')
             self.assertTrue(chk)
             self.assertEqual(
-                {'compatible', 'bootph-all', '#address-cells', '#size-cells'},
+                {'compatible', 'bootph-all', '#address-cells', '#size-cells',
+                 'phandle'},
                 chk.props.keys())
 
             # Check the first property
@@ -376,6 +377,28 @@ class TestNode(unittest.TestCase):
         dst = new_dtb.GetNode('/dest')
         do_copy_checks(new_dtb, dst, expect_none=False)
 
+    def test_copy_subnodes_from_phandles(self):
+        """Test copy_node() function"""
+        dtb = fdt.FdtScan(find_dtb_file('dtoc_test_copy.dts'))
+
+        orig = dtb.GetNode('/')
+        node_list = fdt_util.GetPhandleList(orig, 'copy-list')
+
+        dst = dtb.GetNode('/dest')
+        dst.copy_subnodes_from_phandles(node_list)
+
+        pmic = dtb.GetNode('/dest/over')
+        self.assertTrue(pmic)
+
+        subn = dtb.GetNode('/dest/first@0')
+        self.assertTrue(subn)
+        self.assertEqual({'a-prop', 'b-prop', 'reg'}, subn.props.keys())
+
+        self.assertEqual(
+            ['/dest/earlier', '/dest/later', '/dest/over', '/dest/first@0',
+             '/dest/second', '/dest/existing', '/dest/base'],
+            [n.path for n in dst.subnodes])
+
 
 class TestProp(unittest.TestCase):
     """Test operation of the Prop class"""
-- 
2.41.0.390.g38632f3daf-goog

Reply via email to