The phandles used inside a template are not copied to the node inserting
the template, leading to a missing phandle error.

The following example can be used to reproduce the issue:

&binman {
  some_template: template-0 {
    ti-secure-rom {
      content = <&some_data>;
      keyfile = "some_key";
    };
    some_data: blob-ext {
      optional;
    };
  };
  output-bin {
    insert-template = <&some_template>;
  };
};

With the error 'binman: Node '/binman/output-bin/ti-secure-rom': Cannot
find node for phandle 103' observed.

The test_copy_subnodes_from_phandles was also updated to verify the new
behavior of phandles being copied.

Reviewed-by: Moteen Shah <[email protected]>
Signed-off-by: Anshul Dalal <[email protected]>
---
Changes in v3:
- Add the missing 'R/by' tag, that was missed on v2
- Reword commit message
- Fix test_copy_subnodes_from_phandles
- Refactor lines within 80 column length
- Link to v2: 
https://patch.msgid.link/20260709-binman_template_phandle_copy_fix-v2-1-6c4017eec...@ti.com

Changes in v2:
- Add 'testTemplatePhandleCopy' binman test
- Change assignment in fdt.py from unused 'dst' to _
- Link to v1: 
https://patch.msgid.link/20260629-binman_template_phandle_copy_fix-v1-1-196f95a13...@ti.com
---
 tools/binman/ftest.py                           | 12 ++++++++++++
 tools/binman/test/fdt/template_phandle_copy.dts | 22 ++++++++++++++++++++++
 tools/dtoc/fdt.py                               |  2 +-
 tools/dtoc/test_fdt.py                          |  4 ++--
 4 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index bf98b268ac15..5e20b5c78056 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -8552,5 +8552,17 @@ fdt         fdtmap                Extract the devicetree 
blob from the fdtmap
         self.assertEqual(len(subnode4.props), 0,
                         "subnode shouldn't have any properties")
 
+    def testTemplatePhandleCopy(self):
+        """Test if phandles are copied properly when inserting template"""
+        _, _, _, fname = self._DoReadFileDtb('fdt/template_phandle_copy.dts')
+        dtb = fdt.Fdt(fname)
+        dtb.Scan()
+        parent = dtb.GetNode("/binman/section@0/section@1")
+        child = parent.FindNode("section@2")
+        parent_phandle = fdt_util.fdt32_to_cpu(child.props["parent"].value)
+        child_phandle = fdt_util.fdt32_to_cpu(parent.props["child"].value)
+        self.assertEqual(parent, dtb.LookupPhandle(parent_phandle))
+        self.assertEqual(child, dtb.LookupPhandle(child_phandle))
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/tools/binman/test/fdt/template_phandle_copy.dts 
b/tools/binman/test/fdt/template_phandle_copy.dts
new file mode 100644
index 000000000000..5c6782372a48
--- /dev/null
+++ b/tools/binman/test/fdt/template_phandle_copy.dts
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+/ {
+       #address-cells = <1>;
+       #size-cells = <1>;
+
+       binman: binman {
+               some_template: template {
+                       parent: section@1 {
+                               child = <&child>;
+                               child: section@2 {
+                                       parent = <&parent>;
+                               };
+                       };
+               };
+
+               section@0 {
+                       insert-template = <&some_template>;
+               };
+       };
+};
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index 991a36b98796..64123227d7bf 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -780,7 +780,7 @@ class Node:
             parent = self.GetFdt().LookupPhandle(phandle)
             tout.debug(f'adding template {parent.path} to node {self.path}')
             for node in parent.subnodes.__reversed__():
-                dst = self.copy_node(node)
+                self.copy_node(node, True)
 
             tout.debug(f'merge props from {parent.path} to {self.path}')
             self.merge_props(parent, False)
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index f141f931a949..a858da127bfb 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -426,10 +426,10 @@ class TestNode(unittest.TestCase):
              '/dest/second', '/dest/existing', '/dest/base'],
             [n.path for n in dst.subnodes])
 
-        # Make sure that the phandle for 'over' is not copied
+        # Make sure that the phandle for 'over' is copied
         over = dst.FindNode('over')
         tout.debug(f'keys: {over.props.keys()}')
-        self.assertNotIn('phandle', over.props.keys())
+        self.assertIn('phandle', over.props.keys())
 
         # Check the merged properties, first the base ones in '/dest'
         expect = {'bootph-all', 'compatible', 'stringarray', 'longbytearray',

---
base-commit: 63f6cc8ba618396cb9c0161bb5c6d217604ae1d0
change-id: 20260629-binman_template_phandle_copy_fix-956bc3ce48d6

Best regards,
--  
Anshul Dalal <[email protected]>

Reply via email to