blob: d762f93a9a8c0264d4ce93920dc4be7d23813dd9 [file] [log] [blame]
Simon Glassfa8974d2016-07-04 11:58:08 -06001#!/usr/bin/python
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassfa8974d2016-07-04 11:58:08 -06003#
4# Copyright (C) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glassfa8974d2016-07-04 11:58:08 -06007
Simon Glassfe599c72018-07-17 13:25:31 -06008# Utility functions for reading from a device tree. Once the upstream pylibfdt
9# implementation advances far enough, we should be able to drop these.
10
Simon Glassefc9bf62016-07-25 18:59:10 -060011import os
Simon Glassfa8974d2016-07-04 11:58:08 -060012import struct
Paul Burton307f1fa2016-09-27 16:03:57 +010013import sys
Simon Glassefc9bf62016-07-25 18:59:10 -060014import tempfile
15
16import command
17import tools
Simon Glassfa8974d2016-07-04 11:58:08 -060018
Simon Glass9c526332018-07-06 10:27:28 -060019VERSION3 = sys.version_info > (3, 0)
20
21def 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')
26
Simon Glassfa8974d2016-07-04 11:58:08 -060027def fdt32_to_cpu(val):
28 """Convert a device tree cell to an integer
29
30 Args:
31 Value to convert (4-character string representing the cell value)
32
33 Return:
34 A native-endian integer value
35 """
Simon Glass9c526332018-07-06 10:27:28 -060036 if VERSION3:
37 # This code is not reached in Python 2
38 val = get_plain_bytes(val) # pragma: no cover
Simon Glass29462c32016-07-25 18:59:17 -060039 return struct.unpack('>I', val)[0]
Simon Glassefc9bf62016-07-25 18:59:10 -060040
Simon Glassfc3ae9c2017-08-29 14:15:48 -060041def fdt_cells_to_cpu(val, cells):
42 """Convert one or two cells to a long integer
43
44 Args:
45 Value to convert (array of one or more 4-character strings)
46
47 Return:
48 A native-endian long value
49 """
Simon Glass1b1fe412017-08-29 14:15:50 -060050 if not cells:
51 return 0
Simon Glassfc3ae9c2017-08-29 14:15:48 -060052 out = long(fdt32_to_cpu(val[0]))
53 if cells == 2:
54 out = out << 32 | fdt32_to_cpu(val[1])
55 return out
56
Simon Glass3bce93d2018-07-06 10:27:37 -060057def EnsureCompiled(fname, capture_stderr=False):
Simon Glassefc9bf62016-07-25 18:59:10 -060058 """Compile an fdt .dts source file into a .dtb binary blob if needed.
59
60 Args:
61 fname: Filename (if .dts it will be compiled). It not it will be
62 left alone
63
64 Returns:
65 Filename of resulting .dtb file
66 """
67 _, ext = os.path.splitext(fname)
68 if ext != '.dts':
69 return fname
70
71 dts_input = tools.GetOutputFilename('source.dts')
72 dtb_output = tools.GetOutputFilename('source.dtb')
73
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__']
77 args += ['-Ulinux']
78 for path in search_paths:
79 args.extend(['-I', path])
80 args += ['-o', dts_input, fname]
81 command.Run('cc', *args)
82
83 # If we don't have a directory, put it in the tools tempdir
84 search_list = []
85 for path in search_paths:
86 search_list.extend(['-i', path])
Simon Glassc5c7b1a2017-11-12 21:52:09 -070087 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
88 '-W', 'no-unit_address_vs_reg']
Simon Glassefc9bf62016-07-25 18:59:10 -060089 args.extend(search_list)
90 args.append(dts_input)
Simon Glassade2ef62017-12-24 12:12:07 -070091 dtc = os.environ.get('DTC') or 'dtc'
Simon Glass3bce93d2018-07-06 10:27:37 -060092 command.Run(dtc, *args, capture_stderr=capture_stderr)
Simon Glassefc9bf62016-07-25 18:59:10 -060093 return dtb_output
Simon Glass4066b312016-07-25 18:59:18 -060094
95def GetInt(node, propname, default=None):
Simon Glassfe599c72018-07-17 13:25:31 -060096 """Get an integer from a property
97
98 Args:
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
102
103 Returns:
104 Integer value read, or default if none
105 """
Simon Glass4066b312016-07-25 18:59:18 -0600106 prop = node.props.get(propname)
107 if not prop:
108 return default
Simon Glass9c526332018-07-06 10:27:28 -0600109 if isinstance(prop.value, list):
110 raise ValueError("Node '%s' property '%s' has list value: expecting "
Simon Glass4066b312016-07-25 18:59:18 -0600111 "a single integer" % (node.name, propname))
Simon Glass9c526332018-07-06 10:27:28 -0600112 value = fdt32_to_cpu(prop.value)
Simon Glass4066b312016-07-25 18:59:18 -0600113 return value
114
115def GetString(node, propname, default=None):
Simon Glassfe599c72018-07-17 13:25:31 -0600116 """Get a string from a property
117
118 Args:
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
122
123 Returns:
124 String value read, or default if none
125 """
Simon Glass4066b312016-07-25 18:59:18 -0600126 prop = node.props.get(propname)
127 if not prop:
128 return default
129 value = prop.value
Simon Glass9c526332018-07-06 10:27:28 -0600130 if isinstance(value, list):
131 raise ValueError("Node '%s' property '%s' has list value: expecting "
Simon Glass4066b312016-07-25 18:59:18 -0600132 "a single string" % (node.name, propname))
133 return value
134
135def GetBool(node, propname, default=False):
Simon Glassfe599c72018-07-17 13:25:31 -0600136 """Get an boolean from a property
137
138 Args:
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
142
143 Returns:
144 Boolean value read, or default if none (if you set this to True the
145 function will always return True)
146 """
Simon Glass4066b312016-07-25 18:59:18 -0600147 if propname in node.props:
148 return True
149 return default
Simon Glass91710b32018-07-17 13:25:32 -0600150
Simon Glass53f53992018-07-17 13:25:40 -0600151def GetByte(node, propname, default=None):
152 """Get an byte from a property
153
154 Args:
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
158
159 Returns:
160 Byte value read, or default if none
161 """
162 prop = node.props.get(propname)
163 if not prop:
164 return default
165 value = prop.value
166 if isinstance(value, list):
167 raise ValueError("Node '%s' property '%s' has list value: expecting "
168 "a single byte" % (node.name, propname))
169 if len(value) != 1:
170 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
171 (node.name, propname, len(value), 1))
172 return ord(value[0])
173
Simon Glass91710b32018-07-17 13:25:32 -0600174def GetDatatype(node, propname, datatype):
175 """Get a value of a given type from a property
176
177 Args:
178 node: Node object to read from
179 propname: property name to read
180 datatype: Type to read (str or int)
181
182 Returns:
183 value read, or None if none
184
185 Raises:
186 ValueError if datatype is not str or int
187 """
188 if datatype == str:
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'" %
193 datatype)