dtoc: Add support for reading 64-bit ints

Add functions to read a 64-bit integer property from the devicetree.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2021-11-23 11:03:39 -07:00
parent ff139b6c70
commit d866e62917
4 changed files with 56 additions and 3 deletions

View File

@@ -27,6 +27,18 @@ def fdt32_to_cpu(val):
"""
return struct.unpack('>I', val)[0]
def fdt64_to_cpu(val):
"""Convert a device tree cell to an integer
Args:
val (list): Value to convert (list of 2 4-character strings representing
the cell value)
Return:
int: A native-endian integer value
"""
return fdt32_to_cpu(val[0]) << 32 | fdt32_to_cpu(val[1])
def fdt_cells_to_cpu(val, cells):
"""Convert one or two cells to a long integer
@@ -108,6 +120,29 @@ def GetInt(node, propname, default=None):
value = fdt32_to_cpu(prop.value)
return value
def GetInt64(node, propname, default=None):
"""Get a 64-bit integer from a property
Args:
node (Node): Node object to read from
propname (str): property name to read
default (int): Default value to use if the node/property do not exist
Returns:
int: value read, or default if none
Raises:
ValueError: Property is not of the correct size
"""
prop = node.props.get(propname)
if not prop:
return default
if not isinstance(prop.value, list) or len(prop.value) != 2:
raise ValueError("Node '%s' property '%s' should be a list with 2 items for 64-bit values" %
(node.name, propname))
value = fdt64_to_cpu(prop.value)
return value
def GetString(node, propname, default=None):
"""Get a string from a property

View File

@@ -16,6 +16,7 @@
boolval;
maybe-empty-int = <>;
intval = <1>;
int64val = /bits/ 64 <0x123456789abcdef0>;
intarray = <2 3 4>;
byteval = [05];
bytearray = [06];

View File

@@ -296,6 +296,7 @@ struct dtd_sandbox_spl_test {
\tbool\t\tboolval;
\tunsigned char\tbytearray[3];
\tunsigned char\tbyteval;
\tfdt32_t\t\tint64val[2];
\tfdt32_t\t\tintarray[3];
\tfdt32_t\t\tintval;
\tunsigned char\tlongbytearray[9];
@@ -355,6 +356,7 @@ static struct dtd_sandbox_spl_test dtv_spl_test = {
\t.boolval\t\t= true,
\t.bytearray\t\t= {0x6, 0x0, 0x0},
\t.byteval\t\t= 0x5,
\t.int64val\t\t= {0x12345678, 0x9abcdef0},
\t.intarray\t\t= {0x2, 0x3, 0x4},
\t.intval\t\t\t= 0x1,
\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,

View File

@@ -24,7 +24,7 @@ sys.path.insert(2, os.path.join(our_path,
from dtoc import fdt
from dtoc import fdt_util
from dtoc.fdt_util import fdt32_to_cpu
from dtoc.fdt_util import fdt32_to_cpu, fdt64_to_cpu
from fdt import Type, BytesToValue
import libfdt
from patman import command
@@ -128,7 +128,7 @@ class TestFdt(unittest.TestCase):
node = self.dtb.GetNode('/spl-test')
props = self.dtb.GetProps(node)
self.assertEqual(['boolval', 'bytearray', 'byteval', 'compatible',
'intarray', 'intval', 'longbytearray',
'int64val', 'intarray', 'intval', 'longbytearray',
'maybe-empty-int', 'notstring', 'stringarray',
'stringval', 'u-boot,dm-pre-reloc'],
sorted(props.keys()))
@@ -335,6 +335,10 @@ class TestProp(unittest.TestCase):
self.assertEqual(Type.INT, prop.type)
self.assertEqual(1, fdt32_to_cpu(prop.value))
prop = self._ConvertProp('int64val')
self.assertEqual(Type.INT, prop.type)
self.assertEqual(0x123456789abcdef0, fdt64_to_cpu(prop.value))
prop = self._ConvertProp('intarray')
self.assertEqual(Type.INT, prop.type)
val = [fdt32_to_cpu(val) for val in prop.value]
@@ -586,10 +590,21 @@ class TestFdtUtil(unittest.TestCase):
self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3))
with self.assertRaises(ValueError) as e:
self.assertEqual(3, fdt_util.GetInt(self.node, 'intarray'))
fdt_util.GetInt(self.node, 'intarray')
self.assertIn("property 'intarray' has list value: expecting a single "
'integer', str(e.exception))
def testGetInt64(self):
self.assertEqual(0x123456789abcdef0,
fdt_util.GetInt64(self.node, 'int64val'))
self.assertEqual(3, fdt_util.GetInt64(self.node, 'missing', 3))
with self.assertRaises(ValueError) as e:
fdt_util.GetInt64(self.node, 'intarray')
self.assertIn(
"property 'intarray' should be a list with 2 items for 64-bit values",
str(e.exception))
def testGetString(self):
self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
self.assertEqual('test', fdt_util.GetString(self.node, 'missing',