Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 3 | |
| 4 | # Utility code shared across multiple tests. |
| 5 | |
| 6 | import hashlib |
Stephen Warren | ac122ef | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 7 | import inspect |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 8 | import os |
| 9 | import os.path |
Heiko Schocher | b8218a9 | 2016-05-09 10:08:24 +0200 | [diff] [blame] | 10 | import pytest |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 11 | import sys |
| 12 | import time |
Liam Beguin | c3342cd | 2018-03-14 19:15:15 -0400 | [diff] [blame] | 13 | import re |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 14 | |
| 15 | def md5sum_data(data): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 16 | """Calculate the MD5 hash of some data. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 17 | |
| 18 | Args: |
| 19 | data: The data to hash. |
| 20 | |
| 21 | Returns: |
| 22 | The hash of the data, as a binary string. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 23 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 24 | |
| 25 | h = hashlib.md5() |
| 26 | h.update(data) |
| 27 | return h.digest() |
| 28 | |
| 29 | def md5sum_file(fn, max_length=None): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 30 | """Calculate the MD5 hash of the contents of a file. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 31 | |
| 32 | Args: |
| 33 | fn: The filename of the file to hash. |
| 34 | max_length: The number of bytes to hash. If the file has more |
| 35 | bytes than this, they will be ignored. If None or omitted, the |
| 36 | entire file will be hashed. |
| 37 | |
| 38 | Returns: |
| 39 | The hash of the file content, as a binary string. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 41 | |
| 42 | with open(fn, 'rb') as fh: |
| 43 | if max_length: |
| 44 | params = [max_length] |
| 45 | else: |
| 46 | params = [] |
| 47 | data = fh.read(*params) |
| 48 | return md5sum_data(data) |
| 49 | |
| 50 | class PersistentRandomFile(object): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 51 | """Generate and store information about a persistent file containing |
| 52 | random data.""" |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 53 | |
| 54 | def __init__(self, u_boot_console, fn, size): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 55 | """Create or process the persistent file. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 56 | |
| 57 | If the file does not exist, it is generated. |
| 58 | |
| 59 | If the file does exist, its content is hashed for later comparison. |
| 60 | |
| 61 | These files are always located in the "persistent data directory" of |
| 62 | the current test run. |
| 63 | |
| 64 | Args: |
| 65 | u_boot_console: A console connection to U-Boot. |
| 66 | fn: The filename (without path) to create. |
| 67 | size: The desired size of the file in bytes. |
| 68 | |
| 69 | Returns: |
| 70 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 71 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 72 | |
| 73 | self.fn = fn |
| 74 | |
| 75 | self.abs_fn = u_boot_console.config.persistent_data_dir + '/' + fn |
| 76 | |
| 77 | if os.path.exists(self.abs_fn): |
| 78 | u_boot_console.log.action('Persistent data file ' + self.abs_fn + |
| 79 | ' already exists') |
| 80 | self.content_hash = md5sum_file(self.abs_fn) |
| 81 | else: |
| 82 | u_boot_console.log.action('Generating ' + self.abs_fn + |
| 83 | ' (random, persistent, %d bytes)' % size) |
| 84 | data = os.urandom(size) |
| 85 | with open(self.abs_fn, 'wb') as fh: |
| 86 | fh.write(data) |
| 87 | self.content_hash = md5sum_data(data) |
| 88 | |
| 89 | def attempt_to_open_file(fn): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 90 | """Attempt to open a file, without throwing exceptions. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 91 | |
| 92 | Any errors (exceptions) that occur during the attempt to open the file |
| 93 | are ignored. This is useful in order to test whether a file (in |
| 94 | particular, a device node) exists and can be successfully opened, in order |
| 95 | to poll for e.g. USB enumeration completion. |
| 96 | |
| 97 | Args: |
| 98 | fn: The filename to attempt to open. |
| 99 | |
| 100 | Returns: |
| 101 | An open file handle to the file, or None if the file could not be |
| 102 | opened. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 103 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 104 | |
| 105 | try: |
| 106 | return open(fn, 'rb') |
| 107 | except: |
| 108 | return None |
| 109 | |
| 110 | def wait_until_open_succeeds(fn): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 111 | """Poll until a file can be opened, or a timeout occurs. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 112 | |
| 113 | Continually attempt to open a file, and return when this succeeds, or |
| 114 | raise an exception after a timeout. |
| 115 | |
| 116 | Args: |
| 117 | fn: The filename to attempt to open. |
| 118 | |
| 119 | Returns: |
| 120 | An open file handle to the file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 121 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 122 | |
Paul Burton | b8c4555 | 2017-09-14 14:34:44 -0700 | [diff] [blame] | 123 | for i in range(100): |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 124 | fh = attempt_to_open_file(fn) |
| 125 | if fh: |
| 126 | return fh |
| 127 | time.sleep(0.1) |
| 128 | raise Exception('File could not be opened') |
| 129 | |
| 130 | def wait_until_file_open_fails(fn, ignore_errors): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 131 | """Poll until a file cannot be opened, or a timeout occurs. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 132 | |
| 133 | Continually attempt to open a file, and return when this fails, or |
| 134 | raise an exception after a timeout. |
| 135 | |
| 136 | Args: |
| 137 | fn: The filename to attempt to open. |
| 138 | ignore_errors: Indicate whether to ignore timeout errors. If True, the |
| 139 | function will simply return if a timeout occurs, otherwise an |
| 140 | exception will be raised. |
| 141 | |
| 142 | Returns: |
| 143 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 144 | """ |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 145 | |
Paul Burton | b8c4555 | 2017-09-14 14:34:44 -0700 | [diff] [blame] | 146 | for i in range(100): |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 147 | fh = attempt_to_open_file(fn) |
| 148 | if not fh: |
| 149 | return |
| 150 | fh.close() |
| 151 | time.sleep(0.1) |
| 152 | if ignore_errors: |
| 153 | return |
| 154 | raise Exception('File can still be opened') |
| 155 | |
| 156 | def run_and_log(u_boot_console, cmd, ignore_errors=False): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 157 | """Run a command and log its output. |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 158 | |
| 159 | Args: |
| 160 | u_boot_console: A console connection to U-Boot. |
Simon Glass | ec70f8a | 2016-07-31 17:35:05 -0600 | [diff] [blame] | 161 | cmd: The command to run, as an array of argv[], or a string. |
| 162 | If a string, note that it is split up so that quoted spaces |
| 163 | will not be preserved. E.g. "fred and" becomes ['"fred', 'and"'] |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 164 | ignore_errors: Indicate whether to ignore errors. If True, the function |
| 165 | will simply return if the command cannot be executed or exits with |
| 166 | an error code, otherwise an exception will be raised if such |
| 167 | problems occur. |
| 168 | |
| 169 | Returns: |
Simon Glass | f3d3e95 | 2016-07-03 09:40:39 -0600 | [diff] [blame] | 170 | The output as a string. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 171 | """ |
Simon Glass | ec70f8a | 2016-07-31 17:35:05 -0600 | [diff] [blame] | 172 | if isinstance(cmd, str): |
| 173 | cmd = cmd.split() |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 174 | runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) |
Simon Glass | f3d3e95 | 2016-07-03 09:40:39 -0600 | [diff] [blame] | 175 | output = runner.run(cmd, ignore_errors=ignore_errors) |
Stephen Warren | 76b4693 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 176 | runner.close() |
Simon Glass | f3d3e95 | 2016-07-03 09:40:39 -0600 | [diff] [blame] | 177 | return output |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 178 | |
Simon Glass | 9e17b03 | 2016-07-03 09:40:41 -0600 | [diff] [blame] | 179 | def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg): |
Simon Glass | 72f5226 | 2016-07-31 17:35:04 -0600 | [diff] [blame] | 180 | """Run a command that is expected to fail. |
Simon Glass | 9e17b03 | 2016-07-03 09:40:41 -0600 | [diff] [blame] | 181 | |
| 182 | This runs a command and checks that it fails with the expected return code |
| 183 | and exception method. If not, an exception is raised. |
| 184 | |
| 185 | Args: |
| 186 | u_boot_console: A console connection to U-Boot. |
| 187 | cmd: The command to run, as an array of argv[]. |
| 188 | retcode: Expected non-zero return code from the command. |
Simon Glass | 72f5226 | 2016-07-31 17:35:04 -0600 | [diff] [blame] | 189 | msg: String that should be contained within the command's output. |
Simon Glass | 9e17b03 | 2016-07-03 09:40:41 -0600 | [diff] [blame] | 190 | """ |
| 191 | try: |
| 192 | runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) |
| 193 | runner.run(cmd) |
| 194 | except Exception as e: |
Simon Glass | 7f64b18 | 2016-07-31 17:35:03 -0600 | [diff] [blame] | 195 | assert(retcode == runner.exit_status) |
Simon Glass | 9e17b03 | 2016-07-03 09:40:41 -0600 | [diff] [blame] | 196 | assert(msg in runner.output) |
| 197 | else: |
Simon Glass | 7f64b18 | 2016-07-31 17:35:03 -0600 | [diff] [blame] | 198 | raise Exception("Expected an exception with retcode %d message '%s'," |
| 199 | "but it was not raised" % (retcode, msg)) |
Simon Glass | 9e17b03 | 2016-07-03 09:40:41 -0600 | [diff] [blame] | 200 | finally: |
| 201 | runner.close() |
| 202 | |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 203 | ram_base = None |
| 204 | def find_ram_base(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 205 | """Find the running U-Boot's RAM location. |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 206 | |
| 207 | Probe the running U-Boot to determine the address of the first bank |
| 208 | of RAM. This is useful for tests that test reading/writing RAM, or |
| 209 | load/save files that aren't associated with some standard address |
| 210 | typically represented in an environment variable such as |
| 211 | ${kernel_addr_r}. The value is cached so that it only needs to be |
| 212 | actively read once. |
| 213 | |
| 214 | Args: |
| 215 | u_boot_console: A console connection to U-Boot. |
| 216 | |
| 217 | Returns: |
| 218 | The address of U-Boot's first RAM bank, as an integer. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 219 | """ |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 220 | |
| 221 | global ram_base |
| 222 | if u_boot_console.config.buildconfig.get('config_cmd_bdi', 'n') != 'y': |
| 223 | pytest.skip('bdinfo command not supported') |
| 224 | if ram_base == -1: |
| 225 | pytest.skip('Previously failed to find RAM bank start') |
| 226 | if ram_base is not None: |
| 227 | return ram_base |
| 228 | |
| 229 | with u_boot_console.log.section('find_ram_base'): |
| 230 | response = u_boot_console.run_command('bdinfo') |
| 231 | for l in response.split('\n'): |
Daniel Schwierzeck | d56dd0b | 2016-07-06 12:44:22 +0200 | [diff] [blame] | 232 | if '-> start' in l or 'memstart =' in l: |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 233 | ram_base = int(l.split('=')[1].strip(), 16) |
| 234 | break |
| 235 | if ram_base is None: |
| 236 | ram_base = -1 |
| 237 | raise Exception('Failed to find RAM bank start in `bdinfo`') |
| 238 | |
Quentin Schulz | abba763 | 2018-07-09 19:16:26 +0200 | [diff] [blame] | 239 | # We don't want ram_base to be zero as some functions test if the given |
Bin Meng | b2c2608 | 2020-03-28 07:25:28 -0700 | [diff] [blame] | 240 | # address is NULL (0). Besides, on some RISC-V targets the low memory |
| 241 | # is protected that prevents S-mode U-Boot from access. |
| 242 | # Let's add 2MiB then (size of an ARM LPAE/v8 section). |
Quentin Schulz | abba763 | 2018-07-09 19:16:26 +0200 | [diff] [blame] | 243 | |
Bin Meng | b2c2608 | 2020-03-28 07:25:28 -0700 | [diff] [blame] | 244 | ram_base += 1024 * 1024 * 2 |
Quentin Schulz | abba763 | 2018-07-09 19:16:26 +0200 | [diff] [blame] | 245 | |
Stephen Warren | 0526610 | 2016-01-21 16:05:30 -0700 | [diff] [blame] | 246 | return ram_base |
Stephen Warren | ac122ef | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 247 | |
| 248 | class PersistentFileHelperCtxMgr(object): |
| 249 | """A context manager for Python's "with" statement, which ensures that any |
| 250 | generated file is deleted (and hence regenerated) if its mtime is older |
| 251 | than the mtime of the Python module which generated it, and gets an mtime |
| 252 | newer than the mtime of the Python module which generated after it is |
| 253 | generated. Objects of this type should be created by factory function |
| 254 | persistent_file_helper rather than directly.""" |
| 255 | |
| 256 | def __init__(self, log, filename): |
| 257 | """Initialize a new object. |
| 258 | |
| 259 | Args: |
| 260 | log: The Logfile object to log to. |
| 261 | filename: The filename of the generated file. |
| 262 | |
| 263 | Returns: |
| 264 | Nothing. |
| 265 | """ |
| 266 | |
| 267 | self.log = log |
| 268 | self.filename = filename |
| 269 | |
| 270 | def __enter__(self): |
| 271 | frame = inspect.stack()[1] |
| 272 | module = inspect.getmodule(frame[0]) |
| 273 | self.module_filename = module.__file__ |
| 274 | self.module_timestamp = os.path.getmtime(self.module_filename) |
| 275 | |
| 276 | if os.path.exists(self.filename): |
| 277 | filename_timestamp = os.path.getmtime(self.filename) |
| 278 | if filename_timestamp < self.module_timestamp: |
| 279 | self.log.action('Removing stale generated file ' + |
| 280 | self.filename) |
| 281 | os.unlink(self.filename) |
| 282 | |
| 283 | def __exit__(self, extype, value, traceback): |
| 284 | if extype: |
| 285 | try: |
| 286 | os.path.unlink(self.filename) |
| 287 | except: |
| 288 | pass |
| 289 | return |
| 290 | logged = False |
| 291 | for i in range(20): |
| 292 | filename_timestamp = os.path.getmtime(self.filename) |
| 293 | if filename_timestamp > self.module_timestamp: |
| 294 | break |
| 295 | if not logged: |
| 296 | self.log.action( |
| 297 | 'Waiting for generated file timestamp to increase') |
| 298 | logged = True |
| 299 | os.utime(self.filename) |
| 300 | time.sleep(0.1) |
| 301 | |
| 302 | def persistent_file_helper(u_boot_log, filename): |
| 303 | """Manage the timestamps and regeneration of a persistent generated |
| 304 | file. This function creates a context manager for Python's "with" |
| 305 | statement |
| 306 | |
| 307 | Usage: |
| 308 | with persistent_file_helper(u_boot_console.log, filename): |
| 309 | code to generate the file, if it's missing. |
| 310 | |
| 311 | Args: |
| 312 | u_boot_log: u_boot_console.log. |
| 313 | filename: The filename of the generated file. |
| 314 | |
| 315 | Returns: |
| 316 | A context manager object. |
| 317 | """ |
| 318 | |
| 319 | return PersistentFileHelperCtxMgr(u_boot_log, filename) |
Liam Beguin | c3342cd | 2018-03-14 19:15:15 -0400 | [diff] [blame] | 320 | |
| 321 | def crc32(u_boot_console, address, count): |
| 322 | """Helper function used to compute the CRC32 value of a section of RAM. |
| 323 | |
| 324 | Args: |
| 325 | u_boot_console: A U-Boot console connection. |
| 326 | address: Address where data starts. |
| 327 | count: Amount of data to use for calculation. |
| 328 | |
| 329 | Returns: |
| 330 | CRC32 value |
| 331 | """ |
| 332 | |
| 333 | bcfg = u_boot_console.config.buildconfig |
| 334 | has_cmd_crc32 = bcfg.get('config_cmd_crc32', 'n') == 'y' |
| 335 | assert has_cmd_crc32, 'Cannot compute crc32 without CONFIG_CMD_CRC32.' |
| 336 | output = u_boot_console.run_command('crc32 %08x %x' % (address, count)) |
| 337 | |
| 338 | m = re.search('==> ([0-9a-fA-F]{8})$', output) |
| 339 | assert m, 'CRC32 operation failed.' |
| 340 | |
| 341 | return m.group(1) |