2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (C) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
8 # Utility functions for reading from a device tree. Once the upstream pylibfdt
9 # implementation advances far enough, we should be able to drop these.
19 VERSION3 = sys.version_info > (3, 0)
21 def get_plain_bytes(val):
22 """Handle Python 3 strings"""
23 if isinstance(val, bytes):
24 val = val.decode('utf-8')
25 return val.encode('raw_unicode_escape')
27 def fdt32_to_cpu(val):
28 """Convert a device tree cell to an integer
31 Value to convert (4-character string representing the cell value)
34 A native-endian integer value
37 # This code is not reached in Python 2
38 val = get_plain_bytes(val) # pragma: no cover
39 return struct.unpack('>I', val)[0]
41 def fdt_cells_to_cpu(val, cells):
42 """Convert one or two cells to a long integer
45 Value to convert (array of one or more 4-character strings)
48 A native-endian long value
52 out = long(fdt32_to_cpu(val[0]))
54 out = out << 32 | fdt32_to_cpu(val[1])
57 def EnsureCompiled(fname, capture_stderr=False):
58 """Compile an fdt .dts source file into a .dtb binary blob if needed.
61 fname: Filename (if .dts it will be compiled). It not it will be
65 Filename of resulting .dtb file
67 _, ext = os.path.splitext(fname)
71 dts_input = tools.GetOutputFilename('source.dts')
72 dtb_output = tools.GetOutputFilename('source.dtb')
74 search_paths = [os.path.join(os.getcwd(), 'include')]
75 root, _ = os.path.splitext(fname)
76 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
78 for path in search_paths:
79 args.extend(['-I', path])
80 args += ['-o', dts_input, fname]
81 command.Run('cc', *args)
83 # If we don't have a directory, put it in the tools tempdir
85 for path in search_paths:
86 search_list.extend(['-i', path])
87 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
88 '-W', 'no-unit_address_vs_reg']
89 args.extend(search_list)
90 args.append(dts_input)
91 dtc = os.environ.get('DTC') or 'dtc'
92 command.Run(dtc, *args, capture_stderr=capture_stderr)
95 def GetInt(node, propname, default=None):
96 """Get an integer from a property
99 node: Node object to read from
100 propname: property name to read
101 default: Default value to use if the node/property do not exist
104 Integer value read, or default if none
106 prop = node.props.get(propname)
109 if isinstance(prop.value, list):
110 raise ValueError("Node '%s' property '%s' has list value: expecting "
111 "a single integer" % (node.name, propname))
112 value = fdt32_to_cpu(prop.value)
115 def GetString(node, propname, default=None):
116 """Get a string from a property
119 node: Node object to read from
120 propname: property name to read
121 default: Default value to use if the node/property do not exist
124 String value read, or default if none
126 prop = node.props.get(propname)
130 if isinstance(value, list):
131 raise ValueError("Node '%s' property '%s' has list value: expecting "
132 "a single string" % (node.name, propname))
135 def GetBool(node, propname, default=False):
136 """Get an boolean from a property
139 node: Node object to read from
140 propname: property name to read
141 default: Default value to use if the node/property do not exist
144 Boolean value read, or default if none (if you set this to True the
145 function will always return True)
147 if propname in node.props:
151 def GetByte(node, propname, default=None):
152 """Get an byte from a property
155 node: Node object to read from
156 propname: property name to read
157 default: Default value to use if the node/property do not exist
160 Byte value read, or default if none
162 prop = node.props.get(propname)
166 if isinstance(value, list):
167 raise ValueError("Node '%s' property '%s' has list value: expecting "
168 "a single byte" % (node.name, propname))
170 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
171 (node.name, propname, len(value), 1))
174 def GetDatatype(node, propname, datatype):
175 """Get a value of a given type from a property
178 node: Node object to read from
179 propname: property name to read
180 datatype: Type to read (str or int)
183 value read, or None if none
186 ValueError if datatype is not str or int
189 return GetString(node, propname)
190 elif datatype == int:
191 return GetInt(node, propname)
192 raise ValueError("fdt_util internal error: Unknown data type '%s'" %