From bc1dea3656e55d91f7a3c1339d53fc9def3bbf31 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 25 Jul 2016 18:59:05 -0600 Subject: dtoc: Move BytesToValue() and GetEmpty() into PropBase These functions are currently in a separate fdt_util file. Since they are only used from PropBase and subclasses, it makes sense for them to be in the PropBase class. Move these functions into fdt.py along with the list of types. Signed-off-by: Simon Glass --- tools/dtoc/fdt_util.py | 66 -------------------------------------------------- 1 file changed, 66 deletions(-) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 929b524fcfe..6b572483e7f 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -8,72 +8,6 @@ import struct -# A list of types we support -(TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL) = range(4) - -def BytesToValue(bytes): - """Converts a string of bytes into a type and value - - Args: - A string containing bytes - - Return: - A tuple: - Type of data - Data, either a single element or a list of elements. Each element - is one of: - TYPE_STRING: string value from the property - TYPE_INT: a byte-swapped integer stored as a 4-byte string - TYPE_BYTE: a byte stored as a single-byte string - """ - size = len(bytes) - strings = bytes.split('\0') - is_string = True - count = len(strings) - 1 - if count > 0 and not strings[-1]: - for string in strings[:-1]: - if not string: - is_string = False - break - for ch in string: - if ch < ' ' or ch > '~': - is_string = False - break - else: - is_string = False - if is_string: - if count == 1: - return TYPE_STRING, strings[0] - else: - return TYPE_STRING, strings[:-1] - if size % 4: - if size == 1: - return TYPE_BYTE, bytes[0] - else: - return TYPE_BYTE, list(bytes) - val = [] - for i in range(0, size, 4): - val.append(bytes[i:i + 4]) - if size == 4: - return TYPE_INT, val[0] - else: - return TYPE_INT, val - -def GetEmpty(type): - """Get an empty / zero value of the given type - - Returns: - A single value of the given type - """ - if type == TYPE_BYTE: - return chr(0) - elif type == TYPE_INT: - return struct.pack(' Date: Mon, 25 Jul 2016 18:59:10 -0600 Subject: dtoc: Allow the device tree to be compiled from source If a source device tree is provide to the Fdt() constructors, compile it automatically. This will be used in tests, where we want to build a particular test .dts file and check that it works correctly in binman. Signed-off-by: Simon Glass --- tools/dtoc/fdt_util.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 6b572483e7f..3e25a8b980e 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -6,7 +6,12 @@ # SPDX-License-Identifier: GPL-2.0+ # +import os import struct +import tempfile + +import command +import tools def fdt32_to_cpu(val): """Convert a device tree cell to an integer @@ -18,3 +23,39 @@ def fdt32_to_cpu(val): A native-endian integer value """ return struct.unpack(">I", val)[0] + +def EnsureCompiled(fname): + """Compile an fdt .dts source file into a .dtb binary blob if needed. + + Args: + fname: Filename (if .dts it will be compiled). It not it will be + left alone + + Returns: + Filename of resulting .dtb file + """ + _, ext = os.path.splitext(fname) + if ext != '.dts': + return fname + + dts_input = tools.GetOutputFilename('source.dts') + dtb_output = tools.GetOutputFilename('source.dtb') + + search_paths = [os.path.join(os.getcwd(), 'include')] + root, _ = os.path.splitext(fname) + args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] + args += ['-Ulinux'] + for path in search_paths: + args.extend(['-I', path]) + args += ['-o', dts_input, fname] + command.Run('cc', *args) + + # If we don't have a directory, put it in the tools tempdir + search_list = [] + for path in search_paths: + search_list.extend(['-i', path]) + args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb'] + args.extend(search_list) + args.append(dts_input) + command.Run('dtc', *args) + return dtb_output -- cgit v1.2.3 From 20024daee58906712f71b927bd86951d1ddb469d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 25 Jul 2016 18:59:17 -0600 Subject: dtoc: Correct quotes in fdt_util The style is to use single quotes for strings where possible. Adjust this function. Signed-off-by: Simon Glass --- tools/dtoc/fdt_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 3e25a8b980e..32f41d72d79 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -22,7 +22,7 @@ def fdt32_to_cpu(val): Return: A native-endian integer value """ - return struct.unpack(">I", val)[0] + return struct.unpack('>I', val)[0] def EnsureCompiled(fname): """Compile an fdt .dts source file into a .dtb binary blob if needed. -- cgit v1.2.3 From 8f224b3734d042884a8981a14db64c48e87b87a2 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 25 Jul 2016 18:59:18 -0600 Subject: dtoc: Add methods for reading data from properties Provide easy helpers for reading integer, string and boolean values from device-tree properties. Signed-off-by: Simon Glass --- tools/dtoc/fdt_util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tools/dtoc/fdt_util.py') diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 32f41d72d79..3a108381099 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -59,3 +59,28 @@ def EnsureCompiled(fname): args.append(dts_input) command.Run('dtc', *args) return dtb_output + +def GetInt(node, propname, default=None): + prop = node.props.get(propname) + if not prop: + return default + value = fdt32_to_cpu(prop.value) + if type(value) == type(list): + raise ValueError("Node '%s' property '%' has list value: expecting" + "a single integer" % (node.name, propname)) + return value + +def GetString(node, propname, default=None): + prop = node.props.get(propname) + if not prop: + return default + value = prop.value + if type(value) == type(list): + raise ValueError("Node '%s' property '%' has list value: expecting" + "a single string" % (node.name, propname)) + return value + +def GetBool(node, propname, default=False): + if propname in node.props: + return True + return default -- cgit v1.2.3