From d866e6291739de3da9550f2280574e1c44474dc3 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 23 Nov 2021 11:03:39 -0700 Subject: 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 --- tools/dtoc/fdt_util.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 37e96b98642..51d0eb52423 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -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 -- cgit v1.2.3 From 40b4d647c606b6df7394333c1a58c10996c96a78 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 23 Nov 2021 11:03:40 -0700 Subject: 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 --- tools/dtoc/fdt_util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tools/dtoc/fdt_util.py') 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 -- cgit v1.2.3 From 1b5a5331f3c7ad3ae5688841a7a6e710a2cb4dc7 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 23 Nov 2021 21:09:51 -0700 Subject: dtoc: Add support for reading string-list properties Add a function to read a list of strings from the devicetree. Signed-off-by: Simon Glass --- tools/dtoc/fdt_util.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 51bdbdcd3b2..19eb13aef33 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -163,6 +163,27 @@ def GetString(node, propname, default=None): "a single string" % (node.name, propname)) return value +def GetStringList(node, propname, default=None): + """Get a string list from a property + + Args: + node (Node): Node object to read from + propname (str): property name to read + default (list of str): Default value to use if the node/property do not + exist, or None + + Returns: + String value read, or default if none + """ + prop = node.props.get(propname) + if not prop: + return default + value = prop.value + if not isinstance(value, list): + strval = GetString(node, propname) + return [strval] + return value + def GetBool(node, propname, default=False): """Get an boolean from a property -- cgit v1.2.3