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