2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (C) 2017 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
8 """Scanning of U-Boot source for drivers and structs
10 This scans the source tree to find out things about all instances of
11 U_BOOT_DRIVER(), UCLASS_DRIVER and all struct declarations in header files.
13 See doc/driver-model/of-plat.rst for more informaiton
21 def conv_name_to_c(name):
22 """Convert a device-tree name to a C identifier
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).
28 name (str): Name to convert
30 str: String containing the C version of this name
32 new = name.replace('@', '_at_')
33 new = new.replace('-', '_')
34 new = new.replace(',', '_')
35 new = new.replace('.', '_')
38 def get_compat_name(node):
39 """Get the node's list of compatible string as a C identifiers
42 node (fdt.Node): Node object to check
44 list of str: List of C identifiers for all the compatible strings
46 compat = node.props['compatible'].value
47 if not isinstance(compat, list):
49 return [conv_name_to_c(c) for c in compat]
53 """Information about a driver in U-Boot
56 name: Name of driver. For U_BOOT_DRIVER(x) this is 'x'
58 def __init__(self, name):
61 def __eq__(self, other):
62 return self.name == other.name
65 return "Driver(name='%s')" % self.name
69 """Scanning of the U-Boot source tree
72 _basedir (str): Base directory of U-Boot source code. Defaults to the
73 grandparent of this file's directory
74 _drivers: Dict of valid driver names found in drivers/
76 value: Driver for that driver
77 _driver_aliases: Dict that holds aliases for driver names
78 key: Driver alias declared with
79 DM_DRIVER_ALIAS(driver_alias, driver_name)
80 value: Driver name declared with U_BOOT_DRIVER(driver_name)
81 _warning_disabled: true to disable warnings about driver names not found
82 _drivers_additional (list or str): List of additional drivers to use
85 def __init__(self, basedir, warning_disabled, drivers_additional):
86 """Set up a new Scanner
89 basedir = sys.argv[0].replace('tools/dtoc/dtoc', '')
92 self._basedir = basedir
94 self._driver_aliases = {}
95 self._drivers_additional = drivers_additional or []
96 self._warning_disabled = warning_disabled
98 def get_normalized_compat_name(self, node):
99 """Get a node's normalized compat name
101 Returns a valid driver name by retrieving node's list of compatible
102 string as a C identifier and performing a check against _drivers
103 and a lookup in driver_aliases printing a warning in case of failure.
106 node (Node): Node object to check
109 Driver name associated with the first compatible string
110 List of C identifiers for all the other compatible strings
112 In case of no match found, the return will be the same as
115 compat_list_c = get_compat_name(node)
117 for compat_c in compat_list_c:
118 if not compat_c in self._drivers.keys():
119 compat_c = self._driver_aliases.get(compat_c)
123 aliases_c = compat_list_c
124 if compat_c in aliases_c:
125 aliases_c.remove(compat_c)
126 return compat_c, aliases_c
128 if not self._warning_disabled:
129 print('WARNING: the driver %s was not found in the driver list'
130 % (compat_list_c[0]))
132 return compat_list_c[0], compat_list_c[1:]
134 def scan_driver(self, fname):
135 """Scan a driver file to build a list of driver names and aliases
137 This procedure will populate self._drivers and self._driver_aliases
140 fname: Driver filename to scan
142 with open(fname, encoding='utf-8') as inf:
145 except UnicodeDecodeError:
146 # This seems to happen on older Python versions
147 print("Skipping file '%s' due to unicode error" % fname)
150 # The following re will search for driver names declared as
151 # U_BOOT_DRIVER(driver_name)
152 drivers = re.findall(r'U_BOOT_DRIVER\((.*)\)', buff)
154 for driver in drivers:
155 self._drivers[driver] = Driver(driver)
157 # The following re will search for driver aliases declared as
158 # DM_DRIVER_ALIAS(alias, driver_name)
159 driver_aliases = re.findall(
160 r'DM_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)',
163 for alias in driver_aliases: # pragma: no cover
166 self._driver_aliases[alias[1]] = alias[0]
168 def scan_drivers(self):
169 """Scan the driver folders to build a list of driver names and aliases
171 This procedure will populate self._drivers and self._driver_aliases
173 for (dirpath, _, filenames) in os.walk(self._basedir):
174 for fname in filenames:
175 if not fname.endswith('.c'):
177 self.scan_driver(dirpath + '/' + fname)
179 for fname in self._drivers_additional:
180 if not isinstance(fname, str) or len(fname) == 0:
183 self.scan_driver(fname)
185 self.scan_driver(self._basedir + '/' + fname)