Simon Glass | 9065bc9 | 2020-12-28 20:35:06 -0700 | [diff] [blame^] | 1 | #!/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 | |
| 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. |
| 12 | |
| 13 | See doc/driver-model/of-plat.rst for more informaiton |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | import re |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | def 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 | |
| 38 | def 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 | |
| 52 | class 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' |
| 57 | """ |
| 58 | def __init__(self, name): |
| 59 | self.name = name |
| 60 | |
| 61 | def __eq__(self, other): |
| 62 | return self.name == other.name |
| 63 | |
| 64 | def __repr__(self): |
| 65 | return "Driver(name='%s')" % self.name |
| 66 | |
| 67 | |
| 68 | class Scanner: |
| 69 | """Scanning of the U-Boot source tree |
| 70 | |
| 71 | Properties: |
| 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/ |
| 75 | key: Driver name |
| 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 | _drivers_additional (list or str): List of additional drivers to use |
| 82 | during scanning |
| 83 | _warning_disabled: true to disable warnings about driver names not found |
| 84 | """ |
| 85 | def __init__(self, basedir, drivers_additional, warning_disabled): |
| 86 | """Set up a new Scanner |
| 87 | """ |
| 88 | if not basedir: |
| 89 | basedir = sys.argv[0].replace('tools/dtoc/dtoc', '') |
| 90 | if basedir == '': |
| 91 | basedir = './' |
| 92 | self._basedir = basedir |
| 93 | self._drivers = {} |
| 94 | self._driver_aliases = {} |
| 95 | self._drivers_additional = drivers_additional or [] |
| 96 | self._warning_disabled = warning_disabled |
| 97 | |
| 98 | def get_normalized_compat_name(self, node): |
| 99 | """Get a node's normalized compat name |
| 100 | |
| 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. |
| 104 | |
| 105 | Args: |
| 106 | node (Node): Node object to check |
| 107 | Return: |
| 108 | Tuple: |
| 109 | Driver name associated with the first compatible string |
| 110 | List of C identifiers for all the other compatible strings |
| 111 | (possibly empty) |
| 112 | In case of no match found, the return will be the same as |
| 113 | get_compat_name() |
| 114 | """ |
| 115 | compat_list_c = get_compat_name(node) |
| 116 | |
| 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) |
| 120 | if not compat_c: |
| 121 | continue |
| 122 | |
| 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 |
| 127 | |
| 128 | if not self._warning_disabled: |
| 129 | print('WARNING: the driver %s was not found in the driver list' |
| 130 | % (compat_list_c[0])) |
| 131 | |
| 132 | return compat_list_c[0], compat_list_c[1:] |
| 133 | |
| 134 | def scan_driver(self, fname): |
| 135 | """Scan a driver file to build a list of driver names and aliases |
| 136 | |
| 137 | This procedure will populate self._drivers and self._driver_aliases |
| 138 | |
| 139 | Args |
| 140 | fname: Driver filename to scan |
| 141 | """ |
| 142 | with open(fname, encoding='utf-8') as inf: |
| 143 | try: |
| 144 | buff = inf.read() |
| 145 | except UnicodeDecodeError: |
| 146 | # This seems to happen on older Python versions |
| 147 | print("Skipping file '%s' due to unicode error" % fname) |
| 148 | return |
| 149 | |
| 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) |
| 153 | |
| 154 | for driver in drivers: |
| 155 | self._drivers[driver] = Driver(driver) |
| 156 | |
| 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*\)', |
| 161 | buff) |
| 162 | |
| 163 | for alias in driver_aliases: # pragma: no cover |
| 164 | if len(alias) != 2: |
| 165 | continue |
| 166 | self._driver_aliases[alias[1]] = alias[0] |
| 167 | |
| 168 | def scan_drivers(self): |
| 169 | """Scan the driver folders to build a list of driver names and aliases |
| 170 | |
| 171 | This procedure will populate self._drivers and self._driver_aliases |
| 172 | """ |
| 173 | for (dirpath, _, filenames) in os.walk(self._basedir): |
| 174 | for fname in filenames: |
| 175 | if not fname.endswith('.c'): |
| 176 | continue |
| 177 | self.scan_driver(dirpath + '/' + fname) |
| 178 | |
| 179 | for fname in self._drivers_additional: |
| 180 | if not isinstance(fname, str) or len(fname) == 0: |
| 181 | continue |
| 182 | if fname[0] == '/': |
| 183 | self.scan_driver(fname) |
| 184 | else: |
| 185 | self.scan_driver(self._basedir + '/' + fname) |