u_boot_utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. # Utility code shared across multiple tests.
  5. import hashlib
  6. import os
  7. import os.path
  8. import sys
  9. import time
  10. def md5sum_data(data):
  11. '''Calculate the MD5 hash of some data.
  12. Args:
  13. data: The data to hash.
  14. Returns:
  15. The hash of the data, as a binary string.
  16. '''
  17. h = hashlib.md5()
  18. h.update(data)
  19. return h.digest()
  20. def md5sum_file(fn, max_length=None):
  21. '''Calculate the MD5 hash of the contents of a file.
  22. Args:
  23. fn: The filename of the file to hash.
  24. max_length: The number of bytes to hash. If the file has more
  25. bytes than this, they will be ignored. If None or omitted, the
  26. entire file will be hashed.
  27. Returns:
  28. The hash of the file content, as a binary string.
  29. '''
  30. with open(fn, 'rb') as fh:
  31. if max_length:
  32. params = [max_length]
  33. else:
  34. params = []
  35. data = fh.read(*params)
  36. return md5sum_data(data)
  37. class PersistentRandomFile(object):
  38. '''Generate and store information about a persistent file containing
  39. random data.'''
  40. def __init__(self, u_boot_console, fn, size):
  41. '''Create or process the persistent file.
  42. If the file does not exist, it is generated.
  43. If the file does exist, its content is hashed for later comparison.
  44. These files are always located in the "persistent data directory" of
  45. the current test run.
  46. Args:
  47. u_boot_console: A console connection to U-Boot.
  48. fn: The filename (without path) to create.
  49. size: The desired size of the file in bytes.
  50. Returns:
  51. Nothing.
  52. '''
  53. self.fn = fn
  54. self.abs_fn = u_boot_console.config.persistent_data_dir + '/' + fn
  55. if os.path.exists(self.abs_fn):
  56. u_boot_console.log.action('Persistent data file ' + self.abs_fn +
  57. ' already exists')
  58. self.content_hash = md5sum_file(self.abs_fn)
  59. else:
  60. u_boot_console.log.action('Generating ' + self.abs_fn +
  61. ' (random, persistent, %d bytes)' % size)
  62. data = os.urandom(size)
  63. with open(self.abs_fn, 'wb') as fh:
  64. fh.write(data)
  65. self.content_hash = md5sum_data(data)
  66. def attempt_to_open_file(fn):
  67. '''Attempt to open a file, without throwing exceptions.
  68. Any errors (exceptions) that occur during the attempt to open the file
  69. are ignored. This is useful in order to test whether a file (in
  70. particular, a device node) exists and can be successfully opened, in order
  71. to poll for e.g. USB enumeration completion.
  72. Args:
  73. fn: The filename to attempt to open.
  74. Returns:
  75. An open file handle to the file, or None if the file could not be
  76. opened.
  77. '''
  78. try:
  79. return open(fn, 'rb')
  80. except:
  81. return None
  82. def wait_until_open_succeeds(fn):
  83. '''Poll until a file can be opened, or a timeout occurs.
  84. Continually attempt to open a file, and return when this succeeds, or
  85. raise an exception after a timeout.
  86. Args:
  87. fn: The filename to attempt to open.
  88. Returns:
  89. An open file handle to the file.
  90. '''
  91. for i in xrange(100):
  92. fh = attempt_to_open_file(fn)
  93. if fh:
  94. return fh
  95. time.sleep(0.1)
  96. raise Exception('File could not be opened')
  97. def wait_until_file_open_fails(fn, ignore_errors):
  98. '''Poll until a file cannot be opened, or a timeout occurs.
  99. Continually attempt to open a file, and return when this fails, or
  100. raise an exception after a timeout.
  101. Args:
  102. fn: The filename to attempt to open.
  103. ignore_errors: Indicate whether to ignore timeout errors. If True, the
  104. function will simply return if a timeout occurs, otherwise an
  105. exception will be raised.
  106. Returns:
  107. Nothing.
  108. '''
  109. for i in xrange(100):
  110. fh = attempt_to_open_file(fn)
  111. if not fh:
  112. return
  113. fh.close()
  114. time.sleep(0.1)
  115. if ignore_errors:
  116. return
  117. raise Exception('File can still be opened')
  118. def run_and_log(u_boot_console, cmd, ignore_errors=False):
  119. '''Run a command and log its output.
  120. Args:
  121. u_boot_console: A console connection to U-Boot.
  122. cmd: The command to run, as an array of argv[].
  123. ignore_errors: Indicate whether to ignore errors. If True, the function
  124. will simply return if the command cannot be executed or exits with
  125. an error code, otherwise an exception will be raised if such
  126. problems occur.
  127. Returns:
  128. Nothing.
  129. '''
  130. runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
  131. runner.run(cmd, ignore_errors=ignore_errors)
  132. runner.close()