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.
16 from patman import command
17 from patman import tools
19 def fdt32_to_cpu(val):
20 """Convert a device tree cell to an integer
23 Value to convert (4-character string representing the cell value)
26 A native-endian integer value
28 return struct.unpack('>I', val)[0]
30 def fdt_cells_to_cpu(val, cells):
31 """Convert one or two cells to a long integer
34 Value to convert (array of one or more 4-character strings)
37 A native-endian integer value
41 out = int(fdt32_to_cpu(val[0]))
43 out = out << 32 | fdt32_to_cpu(val[1])
46 def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
47 """Compile an fdt .dts source file into a .dtb binary blob if needed.
50 fname: Filename (if .dts it will be compiled). It not it will be
52 tmpdir: Temporary directory for output files, or None to use the
53 tools-module output directory
56 Filename of resulting .dtb file
58 _, ext = os.path.splitext(fname)
63 dts_input = os.path.join(tmpdir, 'source.dts')
64 dtb_output = os.path.join(tmpdir, 'source.dtb')
66 dts_input = tools.GetOutputFilename('source.dts')
67 dtb_output = tools.GetOutputFilename('source.dtb')
69 search_paths = [os.path.join(os.getcwd(), 'include')]
70 root, _ = os.path.splitext(fname)
71 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
73 for path in search_paths:
74 args.extend(['-I', path])
75 args += ['-o', dts_input, fname]
76 command.Run('cc', *args)
78 # If we don't have a directory, put it in the tools tempdir
80 for path in search_paths:
81 search_list.extend(['-i', path])
82 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
83 '-W', 'no-unit_address_vs_reg']
84 args.extend(search_list)
85 args.append(dts_input)
86 dtc = os.environ.get('DTC') or 'dtc'
87 command.Run(dtc, *args, capture_stderr=capture_stderr)
90 def GetInt(node, propname, default=None):
91 """Get an integer from a property
94 node: Node object to read from
95 propname: property name to read
96 default: Default value to use if the node/property do not exist
99 Integer value read, or default if none
101 prop = node.props.get(propname)
104 if isinstance(prop.value, list):
105 raise ValueError("Node '%s' property '%s' has list value: expecting "
106 "a single integer" % (node.name, propname))
107 value = fdt32_to_cpu(prop.value)
110 def GetString(node, propname, default=None):
111 """Get a string from a property
114 node: Node object to read from
115 propname: property name to read
116 default: Default value to use if the node/property do not exist
119 String value read, or default if none
121 prop = node.props.get(propname)
125 if isinstance(value, list):
126 raise ValueError("Node '%s' property '%s' has list value: expecting "
127 "a single string" % (node.name, propname))
130 def GetBool(node, propname, default=False):
131 """Get an boolean from a property
134 node: Node object to read from
135 propname: property name to read
136 default: Default value to use if the node/property do not exist
139 Boolean value read, or default if none (if you set this to True the
140 function will always return True)
142 if propname in node.props:
146 def GetByte(node, propname, default=None):
147 """Get an byte from a property
150 node: Node object to read from
151 propname: property name to read
152 default: Default value to use if the node/property do not exist
155 Byte value read, or default if none
157 prop = node.props.get(propname)
161 if isinstance(value, list):
162 raise ValueError("Node '%s' property '%s' has list value: expecting "
163 "a single byte" % (node.name, propname))
165 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
166 (node.name, propname, len(value), 1))
169 def GetPhandleList(node, propname):
170 """Get a list of phandles from a property
173 node: Node object to read from
174 propname: property name to read
177 List of phandles read, each an integer
179 prop = node.props.get(propname)
183 if not isinstance(value, list):
185 return [fdt32_to_cpu(v) for v in value]
187 def GetDatatype(node, propname, datatype):
188 """Get a value of a given type from a property
191 node: Node object to read from
192 propname: property name to read
193 datatype: Type to read (str or int)
196 value read, or None if none
199 ValueError if datatype is not str or int
202 return GetString(node, propname)
203 elif datatype == int:
204 return GetInt(node, propname)
205 raise ValueError("fdt_util internal error: Unknown data type '%s'" %