1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
4 # Utility code shared across multiple tests.
15 def md5sum_data(data):
16 """Calculate the MD5 hash of some data.
19 data: The data to hash.
22 The hash of the data, as a binary string.
29 def md5sum_file(fn, max_length=None):
30 """Calculate the MD5 hash of the contents of a file.
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.
39 The hash of the file content, as a binary string.
42 with open(fn, 'rb') as fh:
47 data = fh.read(*params)
48 return md5sum_data(data)
50 class PersistentRandomFile(object):
51 """Generate and store information about a persistent file containing
54 def __init__(self, u_boot_console, fn, size):
55 """Create or process the persistent file.
57 If the file does not exist, it is generated.
59 If the file does exist, its content is hashed for later comparison.
61 These files are always located in the "persistent data directory" of
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.
75 self.abs_fn = u_boot_console.config.persistent_data_dir + '/' + fn
77 if os.path.exists(self.abs_fn):
78 u_boot_console.log.action('Persistent data file ' + self.abs_fn +
80 self.content_hash = md5sum_file(self.abs_fn)
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:
87 self.content_hash = md5sum_data(data)
89 def attempt_to_open_file(fn):
90 """Attempt to open a file, without throwing exceptions.
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.
98 fn: The filename to attempt to open.
101 An open file handle to the file, or None if the file could not be
106 return open(fn, 'rb')
110 def wait_until_open_succeeds(fn):
111 """Poll until a file can be opened, or a timeout occurs.
113 Continually attempt to open a file, and return when this succeeds, or
114 raise an exception after a timeout.
117 fn: The filename to attempt to open.
120 An open file handle to the file.
124 fh = attempt_to_open_file(fn)
128 raise Exception('File could not be opened')
130 def wait_until_file_open_fails(fn, ignore_errors):
131 """Poll until a file cannot be opened, or a timeout occurs.
133 Continually attempt to open a file, and return when this fails, or
134 raise an exception after a timeout.
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.
147 fh = attempt_to_open_file(fn)
154 raise Exception('File can still be opened')
156 def run_and_log(u_boot_console, cmd, ignore_errors=False):
157 """Run a command and log its output.
160 u_boot_console: A console connection to U-Boot.
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"']
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
170 The output as a string.
172 if isinstance(cmd, str):
174 runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
175 output = runner.run(cmd, ignore_errors=ignore_errors)
179 def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg):
180 """Run a command that is expected to fail.
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.
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.
189 msg: String that should be contained within the command's output.
192 runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
194 except Exception as e:
195 assert(retcode == runner.exit_status)
196 assert(msg in runner.output)
198 raise Exception("Expected an exception with retcode %d message '%s',"
199 "but it was not raised" % (retcode, msg))
204 def find_ram_base(u_boot_console):
205 """Find the running U-Boot's RAM location.
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
215 u_boot_console: A console connection to U-Boot.
218 The address of U-Boot's first RAM bank, as an integer.
222 if u_boot_console.config.buildconfig.get('config_cmd_bdi', 'n') != 'y':
223 pytest.skip('bdinfo command not supported')
225 pytest.skip('Previously failed to find RAM bank start')
226 if ram_base is not None:
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'):
232 if '-> start' in l or 'memstart =' in l:
233 ram_base = int(l.split('=')[1].strip(), 16)
237 raise Exception('Failed to find RAM bank start in `bdinfo`')
239 # We don't want ram_base to be zero as some functions test if the given
240 # address is NULL (0). Let's add 2MiB then (size of an ARM LPAE/v8 section).
243 ram_base += 1024 * 1024 * 2
247 class PersistentFileHelperCtxMgr(object):
248 """A context manager for Python's "with" statement, which ensures that any
249 generated file is deleted (and hence regenerated) if its mtime is older
250 than the mtime of the Python module which generated it, and gets an mtime
251 newer than the mtime of the Python module which generated after it is
252 generated. Objects of this type should be created by factory function
253 persistent_file_helper rather than directly."""
255 def __init__(self, log, filename):
256 """Initialize a new object.
259 log: The Logfile object to log to.
260 filename: The filename of the generated file.
267 self.filename = filename
270 frame = inspect.stack()[1]
271 module = inspect.getmodule(frame[0])
272 self.module_filename = module.__file__
273 self.module_timestamp = os.path.getmtime(self.module_filename)
275 if os.path.exists(self.filename):
276 filename_timestamp = os.path.getmtime(self.filename)
277 if filename_timestamp < self.module_timestamp:
278 self.log.action('Removing stale generated file ' +
280 os.unlink(self.filename)
282 def __exit__(self, extype, value, traceback):
285 os.path.unlink(self.filename)
291 filename_timestamp = os.path.getmtime(self.filename)
292 if filename_timestamp > self.module_timestamp:
296 'Waiting for generated file timestamp to increase')
298 os.utime(self.filename)
301 def persistent_file_helper(u_boot_log, filename):
302 """Manage the timestamps and regeneration of a persistent generated
303 file. This function creates a context manager for Python's "with"
307 with persistent_file_helper(u_boot_console.log, filename):
308 code to generate the file, if it's missing.
311 u_boot_log: u_boot_console.log.
312 filename: The filename of the generated file.
315 A context manager object.
318 return PersistentFileHelperCtxMgr(u_boot_log, filename)
320 def crc32(u_boot_console, address, count):
321 """Helper function used to compute the CRC32 value of a section of RAM.
324 u_boot_console: A U-Boot console connection.
325 address: Address where data starts.
326 count: Amount of data to use for calculation.
332 bcfg = u_boot_console.config.buildconfig
333 has_cmd_crc32 = bcfg.get('config_cmd_crc32', 'n') == 'y'
334 assert has_cmd_crc32, 'Cannot compute crc32 without CONFIG_CMD_CRC32.'
335 output = u_boot_console.run_command('crc32 %08x %x' % (address, count))
337 m = re.search('==> ([0-9a-fA-F]{8})$', output)
338 assert m, 'CRC32 operation failed.'