[PATCH 03/17] dtoc: Add support for reading fixed-length bytes properties

Simon Glass sjg at chromium.org
Tue Nov 23 19:03:40 CET 2021


Add functions to read a sequence of bytes from the devicetree.

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

 scripts/pylint.base    |  4 ++--
 tools/dtoc/fdt_util.py | 20 ++++++++++++++++++++
 tools/dtoc/test_fdt.py | 17 +++++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/scripts/pylint.base b/scripts/pylint.base
index d848ebe9058..f4f226799c1 100644
--- a/scripts/pylint.base
+++ b/scripts/pylint.base
@@ -44,12 +44,12 @@ cros_ec_rw -6.00
 defs 6.67
 dtoc.dtb_platdata 7.82
 dtoc.fdt 3.47
-dtoc.fdt_util 4.53
+dtoc.fdt_util 4.95
 dtoc.main 7.33
 dtoc.setup 5.00
 dtoc.src_scan 8.75
 dtoc.test_dtoc 8.54
-dtoc.test_fdt 6.92
+dtoc.test_fdt 6.93
 dtoc.test_src_scan 9.43
 efivar 6.71
 endian-swap 8.93
diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
index 51d0eb52423..51bdbdcd3b2 100644
--- a/tools/dtoc/fdt_util.py
+++ b/tools/dtoc/fdt_util.py
@@ -202,6 +202,26 @@ def GetByte(node, propname, default=None):
                          (node.name, propname, len(value), 1))
     return ord(value[0])
 
+def GetBytes(node, propname, size, default=None):
+    """Get a set of bytes from a property
+
+    Args:
+        node (Node): Node object to read from
+        propname (str): property name to read
+        size (int): Number of bytes to expect
+        default (bytes): Default value or None
+
+    Returns:
+        bytes: Bytes value read, or default if none
+    """
+    prop = node.props.get(propname)
+    if not prop:
+        return default
+    if len(prop.bytes) != size:
+        raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
+                         (node.name, propname, len(prop.bytes), size))
+    return prop.bytes
+
 def GetPhandleList(node, propname):
     """Get a list of phandles from a property
 
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index 21a9a7ca063..7a4c7efaa4a 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -635,6 +635,23 @@ class TestFdtUtil(unittest.TestCase):
         self.assertIn("property 'intval' has length 4, expecting 1",
                       str(e.exception))
 
+    def testGetBytes(self):
+        self.assertEqual(bytes([5]), fdt_util.GetBytes(self.node, 'byteval', 1))
+        self.assertEqual(None, fdt_util.GetBytes(self.node, 'missing', 3))
+        self.assertEqual(
+            bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3,  bytes([3])))
+
+        with self.assertRaises(ValueError) as e:
+            fdt_util.GetBytes(self.node, 'longbytearray', 7)
+        self.assertIn(
+            "Node 'spl-test' property 'longbytearray' has length 9, expecting 7",
+             str(e.exception))
+
+        self.assertEqual(
+            bytes([0, 0, 0, 1]), fdt_util.GetBytes(self.node, 'intval', 4))
+        self.assertEqual(
+            bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3,  bytes([3])))
+
     def testGetPhandleList(self):
         dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
         node = dtb.GetNode('/phandle-source2')
-- 
2.34.0.rc2.393.gf8c9666880-goog



More information about the U-Boot mailing list