[PATCH 7/8] dtoc: Add a way to read a phandle with params

Simon Glass sjg at chromium.org
Thu Jan 12 00:10:18 CET 2023


Add a function to read a phandle and associated name and offset. This is
useful for binman.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/dtoc/fdt_util.py                | 28 +++++++++++++++++++++++++++
 tools/dtoc/test/dtoc_test_phandle.dts |  1 +
 tools/dtoc/test_fdt.py                | 11 +++++++++++
 3 files changed, 40 insertions(+)

diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
index d7c38ad1e03..f34316632a7 100644
--- a/tools/dtoc/fdt_util.py
+++ b/tools/dtoc/fdt_util.py
@@ -281,6 +281,34 @@ def GetPhandleList(node, propname):
         value = [value]
     return [fdt32_to_cpu(v) for v in value]
 
+def GetPhandleNameOffset(node, propname):
+    """Get a <&phandle>, "string", <offset> value from a property
+
+    Args:
+        node: Node object to read from
+        propname: property name to read
+
+    Returns:
+        tuple:
+            Node object
+            str
+            int
+        or None if the property does not exist
+    """
+    prop = node.props.get(propname)
+    if not prop:
+        return None
+    value = prop.bytes
+    phandle = fdt32_to_cpu(value[:4])
+    node = node.GetFdt().LookupPhandle(phandle)
+    name = ''
+    for byte in value[4:]:
+        if not byte:
+            break
+        name += chr(byte)
+    val = fdt32_to_cpu(value[4 + len(name) + 1:])
+    return node, name, val
+
 def GetDatatype(node, propname, datatype):
     """Get a value of a given type from a property
 
diff --git a/tools/dtoc/test/dtoc_test_phandle.dts b/tools/dtoc/test/dtoc_test_phandle.dts
index a71acffc698..d9aa433503d 100644
--- a/tools/dtoc/test/dtoc_test_phandle.dts
+++ b/tools/dtoc/test/dtoc_test_phandle.dts
@@ -32,6 +32,7 @@
 		u-boot,dm-pre-reloc;
 		compatible = "source";
 		clocks = <&phandle &phandle_1 11 &phandle_2 12 13 &phandle>;
+		phandle-name-offset = <&phandle_2>, "fred", <123>;
 	};
 
 	phandle-source2 {
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index a3e36ea363f..3b8ee00d4e0 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -795,6 +795,17 @@ class TestFdtUtil(unittest.TestCase):
         finally:
             tools.outdir= old_outdir
 
+    def test_get_phandle_name_offset(self):
+        val = fdt_util.GetPhandleNameOffset(self.node, 'missing')
+        self.assertIsNone(val)
+
+        dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
+        node = dtb.GetNode('/phandle-source')
+        node, name, offset = fdt_util.GetPhandleNameOffset(node,
+                                                           'phandle-name-offset')
+        self.assertEqual('phandle3-target', node.name)
+        self.assertEqual('fred', name)
+        self.assertEqual(123, offset)
 
 def run_test_coverage(build_dir):
     """Run the tests and check that we get 100% coverage
-- 
2.39.0.314.g84b9a713c41-goog



More information about the U-Boot mailing list