dtoc: Add support for reading fixed-length bytes properties

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

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2021-11-23 11:03:40 -07:00
parent d866e62917
commit 40b4d647c6
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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')