blob: 761164a9c9ab41fb8c7229324072d8e1adfec687 [file] [log] [blame]
Simon Glassa542a702020-12-28 20:35:06 -07001#!/usr/bin/python
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Copyright (C) 2017 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
7
8"""Scanning of U-Boot source for drivers and structs
9
10This scans the source tree to find out things about all instances of
11U_BOOT_DRIVER(), UCLASS_DRIVER and all struct declarations in header files.
12
13See doc/driver-model/of-plat.rst for more informaiton
14"""
15
16import os
17import re
18import sys
19
20
21def conv_name_to_c(name):
22 """Convert a device-tree name to a C identifier
23
24 This uses multiple replace() calls instead of re.sub() since it is faster
25 (400ms for 1m calls versus 1000ms for the 're' version).
26
27 Args:
28 name (str): Name to convert
29 Return:
30 str: String containing the C version of this name
31 """
32 new = name.replace('@', '_at_')
33 new = new.replace('-', '_')
34 new = new.replace(',', '_')
35 new = new.replace('.', '_')
36 return new
37
38def get_compat_name(node):
39 """Get the node's list of compatible string as a C identifiers
40
41 Args:
42 node (fdt.Node): Node object to check
43 Return:
44 list of str: List of C identifiers for all the compatible strings
45 """
46 compat = node.props['compatible'].value
47 if not isinstance(compat, list):
48 compat = [compat]
49 return [conv_name_to_c(c) for c in compat]
50
51
52class Driver:
53 """Information about a driver in U-Boot
54
55 Attributes:
56 name: Name of driver. For U_BOOT_DRIVER(x) this is 'x'
Simon Glassc58662f2021-02-03 06:00:50 -070057 fname: Filename where the driver was found
58 uclass_id: Name of uclass, e.g. 'UCLASS_I2C'
59 compat: Driver data for each compatible string:
60 key: Compatible string, e.g. 'rockchip,rk3288-grf'
61 value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
62 fname: Filename where the driver was found
63 priv (str): struct name of the priv_auto member, e.g. 'serial_priv'
Simon Glassa542a702020-12-28 20:35:06 -070064 """
Simon Glassc58662f2021-02-03 06:00:50 -070065 def __init__(self, name, fname):
Simon Glassa542a702020-12-28 20:35:06 -070066 self.name = name
Simon Glassc58662f2021-02-03 06:00:50 -070067 self.fname = fname
68 self.uclass_id = None
69 self.compat = None
70 self.priv = ''
Simon Glassa542a702020-12-28 20:35:06 -070071
72 def __eq__(self, other):
Simon Glassc58662f2021-02-03 06:00:50 -070073 return (self.name == other.name and
74 self.uclass_id == other.uclass_id and
75 self.compat == other.compat and
76 self.priv == other.priv)
Simon Glassa542a702020-12-28 20:35:06 -070077
78 def __repr__(self):
Simon Glassc58662f2021-02-03 06:00:50 -070079 return ("Driver(name='%s', uclass_id='%s', compat=%s, priv=%s)" %
80 (self.name, self.uclass_id, self.compat, self.priv))
Simon Glassa542a702020-12-28 20:35:06 -070081
82
83class Scanner:
84 """Scanning of the U-Boot source tree
85
86 Properties:
87 _basedir (str): Base directory of U-Boot source code. Defaults to the
88 grandparent of this file's directory
89 _drivers: Dict of valid driver names found in drivers/
90 key: Driver name
91 value: Driver for that driver
92 _driver_aliases: Dict that holds aliases for driver names
93 key: Driver alias declared with
94 DM_DRIVER_ALIAS(driver_alias, driver_name)
95 value: Driver name declared with U_BOOT_DRIVER(driver_name)
Simon Glass10ea9c02020-12-28 20:35:07 -070096 _warning_disabled: true to disable warnings about driver names not found
Simon Glassa542a702020-12-28 20:35:06 -070097 _drivers_additional (list or str): List of additional drivers to use
98 during scanning
Simon Glassc58662f2021-02-03 06:00:50 -070099 _of_match: Dict holding information about compatible strings
100 key: Name of struct udevice_id variable
101 value: Dict of compatible info in that variable:
102 key: Compatible string, e.g. 'rockchip,rk3288-grf'
103 value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
104 _compat_to_driver: Maps compatible strings to Driver
Simon Glassa542a702020-12-28 20:35:06 -0700105 """
Simon Glass10ea9c02020-12-28 20:35:07 -0700106 def __init__(self, basedir, warning_disabled, drivers_additional):
Simon Glassa542a702020-12-28 20:35:06 -0700107 """Set up a new Scanner
108 """
109 if not basedir:
110 basedir = sys.argv[0].replace('tools/dtoc/dtoc', '')
111 if basedir == '':
112 basedir = './'
113 self._basedir = basedir
114 self._drivers = {}
115 self._driver_aliases = {}
116 self._drivers_additional = drivers_additional or []
117 self._warning_disabled = warning_disabled
Simon Glassc58662f2021-02-03 06:00:50 -0700118 self._of_match = {}
119 self._compat_to_driver = {}
Simon Glassa542a702020-12-28 20:35:06 -0700120
121 def get_normalized_compat_name(self, node):
122 """Get a node's normalized compat name
123
124 Returns a valid driver name by retrieving node's list of compatible
125 string as a C identifier and performing a check against _drivers
126 and a lookup in driver_aliases printing a warning in case of failure.
127
128 Args:
129 node (Node): Node object to check
130 Return:
131 Tuple:
132 Driver name associated with the first compatible string
133 List of C identifiers for all the other compatible strings
134 (possibly empty)
135 In case of no match found, the return will be the same as
136 get_compat_name()
137 """
138 compat_list_c = get_compat_name(node)
139
140 for compat_c in compat_list_c:
141 if not compat_c in self._drivers.keys():
142 compat_c = self._driver_aliases.get(compat_c)
143 if not compat_c:
144 continue
145
146 aliases_c = compat_list_c
147 if compat_c in aliases_c:
148 aliases_c.remove(compat_c)
149 return compat_c, aliases_c
150
151 if not self._warning_disabled:
152 print('WARNING: the driver %s was not found in the driver list'
153 % (compat_list_c[0]))
154
155 return compat_list_c[0], compat_list_c[1:]
156
Simon Glassc58662f2021-02-03 06:00:50 -0700157 @classmethod
158 def _get_re_for_member(cls, member):
159 """_get_re_for_member: Get a compiled regular expression
160
161 Args:
162 member (str): Struct member name, e.g. 'priv_auto'
163
164 Returns:
165 re.Pattern: Compiled regular expression that parses:
166
167 .member = sizeof(struct fred),
168
169 and returns "fred" as group 1
170 """
171 return re.compile(r'^\s*.%s\s*=\s*sizeof\(struct\s+(.*)\),$' % member)
172
173 def _parse_driver(self, fname, buff):
174 """Parse a C file to extract driver information contained within
175
176 This parses U_BOOT_DRIVER() structs to obtain various pieces of useful
177 information.
178
179 It updates the following members:
180 _drivers - updated with new Driver records for each driver found
181 in the file
182 _of_match - updated with each compatible string found in the file
183 _compat_to_driver - Maps compatible string to Driver
184
185 Args:
186 fname (str): Filename being parsed (used for warnings)
187 buff (str): Contents of file
188
189 Raises:
190 ValueError: Compatible variable is mentioned in .of_match in
191 U_BOOT_DRIVER() but not found in the file
192 """
193 # Dict holding information about compatible strings collected in this
194 # function so far
195 # key: Name of struct udevice_id variable
196 # value: Dict of compatible info in that variable:
197 # key: Compatible string, e.g. 'rockchip,rk3288-grf'
198 # value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
199 of_match = {}
200
201 # Dict holding driver information collected in this function so far
202 # key: Driver name (C name as in U_BOOT_DRIVER(xxx))
203 # value: Driver
204 drivers = {}
205
206 # Collect the driver info
207 driver = None
208 re_driver = re.compile(r'U_BOOT_DRIVER\((.*)\)')
209
210 # Collect the uclass ID, e.g. 'UCLASS_SPI'
211 re_id = re.compile(r'\s*\.id\s*=\s*(UCLASS_[A-Z0-9_]+)')
212
213 # Collect the compatible string, e.g. 'rockchip,rk3288-grf'
214 compat = None
215 re_compat = re.compile(r'{\s*.compatible\s*=\s*"(.*)"\s*'
216 r'(,\s*.data\s*=\s*(\S*))?\s*},')
217
218 # This is a dict of compatible strings that were found:
219 # key: Compatible string, e.g. 'rockchip,rk3288-grf'
220 # value: Driver data, e,g, 'ROCKCHIP_SYSCON_GRF', or None
221 compat_dict = {}
222
223 # Holds the var nane of the udevice_id list, e.g.
224 # 'rk3288_syscon_ids_noc' in
225 # static const struct udevice_id rk3288_syscon_ids_noc[] = {
226 ids_name = None
227 re_ids = re.compile(r'struct udevice_id (.*)\[\]\s*=')
228
229 # Matches the references to the udevice_id list
230 re_of_match = re.compile(
231 r'\.of_match\s*=\s*(of_match_ptr\()?([a-z0-9_]+)(\))?,')
232
233 # Matches the struct name for priv
234 re_priv = self._get_re_for_member('priv_auto')
235
236 prefix = ''
237 for line in buff.splitlines():
238 # Handle line continuation
239 if prefix:
240 line = prefix + line
241 prefix = ''
242 if line.endswith('\\'):
243 prefix = line[:-1]
244 continue
245
246 driver_match = re_driver.search(line)
247
248 # If this line contains U_BOOT_DRIVER()...
249 if driver:
250 m_id = re_id.search(line)
251 m_of_match = re_of_match.search(line)
252 m_priv = re_priv.match(line)
253 if m_priv:
254 driver.priv = m_priv.group(1)
255 elif m_id:
256 driver.uclass_id = m_id.group(1)
257 elif m_of_match:
258 compat = m_of_match.group(2)
259 elif '};' in line:
260 if driver.uclass_id and compat:
261 if compat not in of_match:
262 raise ValueError(
263 "%s: Unknown compatible var '%s' (found: %s)" %
264 (fname, compat, ','.join(of_match.keys())))
265 driver.compat = of_match[compat]
266
267 # This needs to be deterministic, since a driver may
268 # have multiple compatible strings pointing to it.
269 # We record the one earliest in the alphabet so it
270 # will produce the same result on all machines.
271 for compat_id in of_match[compat]:
272 old = self._compat_to_driver.get(compat_id)
273 if not old or driver.name < old.name:
274 self._compat_to_driver[compat_id] = driver
275 drivers[driver.name] = driver
276 else:
277 # The driver does not have a uclass or compat string.
278 # The first is required but the second is not, so just
279 # ignore this.
280 pass
281 driver = None
282 ids_name = None
283 compat = None
284 compat_dict = {}
285
286 elif ids_name:
287 compat_m = re_compat.search(line)
288 if compat_m:
289 compat_dict[compat_m.group(1)] = compat_m.group(3)
290 elif '};' in line:
291 of_match[ids_name] = compat_dict
292 ids_name = None
293 elif driver_match:
294 driver_name = driver_match.group(1)
295 driver = Driver(driver_name, fname)
296 else:
297 ids_m = re_ids.search(line)
298 if ids_m:
299 ids_name = ids_m.group(1)
300
301 # Make the updates based on what we found
302 self._drivers.update(drivers)
303 self._of_match.update(of_match)
304
Simon Glassa542a702020-12-28 20:35:06 -0700305 def scan_driver(self, fname):
306 """Scan a driver file to build a list of driver names and aliases
307
Simon Glassc58662f2021-02-03 06:00:50 -0700308 It updates the following members:
309 _drivers - updated with new Driver records for each driver found
310 in the file
311 _of_match - updated with each compatible string found in the file
312 _compat_to_driver - Maps compatible string to Driver
313 _driver_aliases - Maps alias names to driver name
Simon Glassa542a702020-12-28 20:35:06 -0700314
315 Args
316 fname: Driver filename to scan
317 """
318 with open(fname, encoding='utf-8') as inf:
319 try:
320 buff = inf.read()
321 except UnicodeDecodeError:
322 # This seems to happen on older Python versions
323 print("Skipping file '%s' due to unicode error" % fname)
324 return
325
Simon Glassc58662f2021-02-03 06:00:50 -0700326 # If this file has any U_BOOT_DRIVER() declarations, process it to
327 # obtain driver information
328 if 'U_BOOT_DRIVER' in buff:
329 self._parse_driver(fname, buff)
Simon Glassa542a702020-12-28 20:35:06 -0700330
331 # The following re will search for driver aliases declared as
332 # DM_DRIVER_ALIAS(alias, driver_name)
333 driver_aliases = re.findall(
334 r'DM_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)',
335 buff)
336
337 for alias in driver_aliases: # pragma: no cover
338 if len(alias) != 2:
339 continue
340 self._driver_aliases[alias[1]] = alias[0]
341
342 def scan_drivers(self):
343 """Scan the driver folders to build a list of driver names and aliases
344
345 This procedure will populate self._drivers and self._driver_aliases
346 """
347 for (dirpath, _, filenames) in os.walk(self._basedir):
Simon Glass36b22202021-02-03 06:00:52 -0700348 rel_path = dirpath[len(self._basedir):]
349 if rel_path.startswith('/'):
350 rel_path = rel_path[1:]
351 if rel_path.startswith('build') or rel_path.startswith('.git'):
352 continue
Simon Glassa542a702020-12-28 20:35:06 -0700353 for fname in filenames:
354 if not fname.endswith('.c'):
355 continue
356 self.scan_driver(dirpath + '/' + fname)
357
358 for fname in self._drivers_additional:
359 if not isinstance(fname, str) or len(fname) == 0:
360 continue
361 if fname[0] == '/':
362 self.scan_driver(fname)
363 else:
364 self.scan_driver(self._basedir + '/' + fname)