u_boot_console_base.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # Copyright (c) 2015 Stephen Warren
  2. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0
  5. # Common logic to interact with U-Boot via the console. This class provides
  6. # the interface that tests use to execute U-Boot shell commands and wait for
  7. # their results. Sub-classes exist to perform board-type-specific setup
  8. # operations, such as spawning a sub-process for Sandbox, or attaching to the
  9. # serial console of real hardware.
  10. import multiplexed_log
  11. import os
  12. import pytest
  13. import re
  14. import sys
  15. import u_boot_spawn
  16. # Regexes for text we expect U-Boot to send to the console.
  17. pattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}-[^\r\n]*)')
  18. pattern_u_boot_main_signon = re.compile('(U-Boot \\d{4}\\.\\d{2}-[^\r\n]*)')
  19. pattern_stop_autoboot_prompt = re.compile('Hit any key to stop autoboot: ')
  20. pattern_unknown_command = re.compile('Unknown command \'.*\' - try \'help\'')
  21. pattern_error_notification = re.compile('## Error: ')
  22. class ConsoleDisableCheck(object):
  23. '''Context manager (for Python's with statement) that temporarily disables
  24. the specified console output error check. This is useful when deliberately
  25. executing a command that is known to trigger one of the error checks, in
  26. order to test that the error condition is actually raised. This class is
  27. used internally by ConsoleBase::disable_check(); it is not intended for
  28. direct usage.'''
  29. def __init__(self, console, check_type):
  30. self.console = console
  31. self.check_type = check_type
  32. def __enter__(self):
  33. self.console.disable_check_count[self.check_type] += 1
  34. def __exit__(self, extype, value, traceback):
  35. self.console.disable_check_count[self.check_type] -= 1
  36. class ConsoleBase(object):
  37. '''The interface through which test functions interact with the U-Boot
  38. console. This primarily involves executing shell commands, capturing their
  39. results, and checking for common error conditions. Some common utilities
  40. are also provided too.'''
  41. def __init__(self, log, config, max_fifo_fill):
  42. '''Initialize a U-Boot console connection.
  43. Can only usefully be called by sub-classes.
  44. Args:
  45. log: A mulptiplex_log.Logfile object, to which the U-Boot output
  46. will be logged.
  47. config: A configuration data structure, as built by conftest.py.
  48. max_fifo_fill: The maximum number of characters to send to U-Boot
  49. command-line before waiting for U-Boot to echo the characters
  50. back. For UART-based HW without HW flow control, this value
  51. should be set less than the UART RX FIFO size to avoid
  52. overflow, assuming that U-Boot can't keep up with full-rate
  53. traffic at the baud rate.
  54. Returns:
  55. Nothing.
  56. '''
  57. self.log = log
  58. self.config = config
  59. self.max_fifo_fill = max_fifo_fill
  60. self.logstream = self.log.get_stream('console', sys.stdout)
  61. # Array slice removes leading/trailing quotes
  62. self.prompt = self.config.buildconfig['config_sys_prompt'][1:-1]
  63. self.prompt_escaped = re.escape(self.prompt)
  64. self.p = None
  65. self.disable_check_count = {
  66. 'spl_signon': 0,
  67. 'main_signon': 0,
  68. 'unknown_command': 0,
  69. 'error_notification': 0,
  70. }
  71. self.at_prompt = False
  72. self.at_prompt_logevt = None
  73. def close(self):
  74. '''Terminate the connection to the U-Boot console.
  75. This function is only useful once all interaction with U-Boot is
  76. complete. Once this function is called, data cannot be sent to or
  77. received from U-Boot.
  78. Args:
  79. None.
  80. Returns:
  81. Nothing.
  82. '''
  83. if self.p:
  84. self.p.close()
  85. self.logstream.close()
  86. def run_command(self, cmd, wait_for_echo=True, send_nl=True,
  87. wait_for_prompt=True):
  88. '''Execute a command via the U-Boot console.
  89. The command is always sent to U-Boot.
  90. U-Boot echoes any command back to its output, and this function
  91. typically waits for that to occur. The wait can be disabled by setting
  92. wait_for_echo=False, which is useful e.g. when sending CTRL-C to
  93. interrupt a long-running command such as "ums".
  94. Command execution is typically triggered by sending a newline
  95. character. This can be disabled by setting send_nl=False, which is
  96. also useful when sending CTRL-C.
  97. This function typically waits for the command to finish executing, and
  98. returns the console output that it generated. This can be disabled by
  99. setting wait_for_prompt=False, which is useful when invoking a long-
  100. running command such as "ums".
  101. Args:
  102. cmd: The command to send.
  103. wait_for_each: Boolean indicating whether to wait for U-Boot to
  104. echo the command text back to its output.
  105. send_nl: Boolean indicating whether to send a newline character
  106. after the command string.
  107. wait_for_prompt: Boolean indicating whether to wait for the
  108. command prompt to be sent by U-Boot. This typically occurs
  109. immediately after the command has been executed.
  110. Returns:
  111. If wait_for_prompt == False:
  112. Nothing.
  113. Else:
  114. The output from U-Boot during command execution. In other
  115. words, the text U-Boot emitted between the point it echod the
  116. command string and emitted the subsequent command prompts.
  117. '''
  118. if self.at_prompt and \
  119. self.at_prompt_logevt != self.logstream.logfile.cur_evt:
  120. self.logstream.write(self.prompt, implicit=True)
  121. bad_patterns = []
  122. bad_pattern_ids = []
  123. if (self.disable_check_count['spl_signon'] == 0 and
  124. self.u_boot_spl_signon):
  125. bad_patterns.append(self.u_boot_spl_signon_escaped)
  126. bad_pattern_ids.append('SPL signon')
  127. if self.disable_check_count['main_signon'] == 0:
  128. bad_patterns.append(self.u_boot_main_signon_escaped)
  129. bad_pattern_ids.append('U-Boot main signon')
  130. if self.disable_check_count['unknown_command'] == 0:
  131. bad_patterns.append(pattern_unknown_command)
  132. bad_pattern_ids.append('Unknown command')
  133. if self.disable_check_count['error_notification'] == 0:
  134. bad_patterns.append(pattern_error_notification)
  135. bad_pattern_ids.append('Error notification')
  136. try:
  137. self.at_prompt = False
  138. if send_nl:
  139. cmd += '\n'
  140. while cmd:
  141. # Limit max outstanding data, so UART FIFOs don't overflow
  142. chunk = cmd[:self.max_fifo_fill]
  143. cmd = cmd[self.max_fifo_fill:]
  144. self.p.send(chunk)
  145. if not wait_for_echo:
  146. continue
  147. chunk = re.escape(chunk)
  148. chunk = chunk.replace('\\\n', '[\r\n]')
  149. m = self.p.expect([chunk] + bad_patterns)
  150. if m != 0:
  151. self.at_prompt = False
  152. raise Exception('Bad pattern found on console: ' +
  153. bad_pattern_ids[m - 1])
  154. if not wait_for_prompt:
  155. return
  156. m = self.p.expect([self.prompt_escaped] + bad_patterns)
  157. if m != 0:
  158. self.at_prompt = False
  159. raise Exception('Bad pattern found on console: ' +
  160. bad_pattern_ids[m - 1])
  161. self.at_prompt = True
  162. self.at_prompt_logevt = self.logstream.logfile.cur_evt
  163. # Only strip \r\n; space/TAB might be significant if testing
  164. # indentation.
  165. return self.p.before.strip('\r\n')
  166. except Exception as ex:
  167. self.log.error(str(ex))
  168. self.cleanup_spawn()
  169. raise
  170. def ctrlc(self):
  171. '''Send a CTRL-C character to U-Boot.
  172. This is useful in order to stop execution of long-running synchronous
  173. commands such as "ums".
  174. Args:
  175. None.
  176. Returns:
  177. Nothing.
  178. '''
  179. self.log.action('Sending Ctrl-C')
  180. self.run_command(chr(3), wait_for_echo=False, send_nl=False)
  181. def wait_for(self, text):
  182. '''Wait for a pattern to be emitted by U-Boot.
  183. This is useful when a long-running command such as "dfu" is executing,
  184. and it periodically emits some text that should show up at a specific
  185. location in the log file.
  186. Args:
  187. text: The text to wait for; either a string (containing raw text,
  188. not a regular expression) or an re object.
  189. Returns:
  190. Nothing.
  191. '''
  192. if type(text) == type(''):
  193. text = re.escape(text)
  194. self.p.expect([text])
  195. def drain_console(self):
  196. '''Read from and log the U-Boot console for a short time.
  197. U-Boot's console output is only logged when the test code actively
  198. waits for U-Boot to emit specific data. There are cases where tests
  199. can fail without doing this. For example, if a test asks U-Boot to
  200. enable USB device mode, then polls until a host-side device node
  201. exists. In such a case, it is useful to log U-Boot's console output
  202. in case U-Boot printed clues as to why the host-side even did not
  203. occur. This function will do that.
  204. Args:
  205. None.
  206. Returns:
  207. Nothing.
  208. '''
  209. # If we are already not connected to U-Boot, there's nothing to drain.
  210. # This should only happen when a previous call to run_command() or
  211. # wait_for() failed (and hence the output has already been logged), or
  212. # the system is shutting down.
  213. if not self.p:
  214. return
  215. orig_timeout = self.p.timeout
  216. try:
  217. # Drain the log for a relatively short time.
  218. self.p.timeout = 1000
  219. # Wait for something U-Boot will likely never send. This will
  220. # cause the console output to be read and logged.
  221. self.p.expect(['This should never match U-Boot output'])
  222. except u_boot_spawn.Timeout:
  223. pass
  224. finally:
  225. self.p.timeout = orig_timeout
  226. def ensure_spawned(self):
  227. '''Ensure a connection to a correctly running U-Boot instance.
  228. This may require spawning a new Sandbox process or resetting target
  229. hardware, as defined by the implementation sub-class.
  230. This is an internal function and should not be called directly.
  231. Args:
  232. None.
  233. Returns:
  234. Nothing.
  235. '''
  236. if self.p:
  237. return
  238. try:
  239. self.at_prompt = False
  240. self.log.action('Starting U-Boot')
  241. self.p = self.get_spawn()
  242. # Real targets can take a long time to scroll large amounts of
  243. # text if LCD is enabled. This value may need tweaking in the
  244. # future, possibly per-test to be optimal. This works for 'help'
  245. # on board 'seaboard'.
  246. self.p.timeout = 30000
  247. self.p.logfile_read = self.logstream
  248. if self.config.buildconfig.get('CONFIG_SPL', False) == 'y':
  249. self.p.expect([pattern_u_boot_spl_signon])
  250. self.u_boot_spl_signon = self.p.after
  251. self.u_boot_spl_signon_escaped = re.escape(self.p.after)
  252. else:
  253. self.u_boot_spl_signon = None
  254. self.p.expect([pattern_u_boot_main_signon])
  255. self.u_boot_main_signon = self.p.after
  256. self.u_boot_main_signon_escaped = re.escape(self.p.after)
  257. build_idx = self.u_boot_main_signon.find(', Build:')
  258. if build_idx == -1:
  259. self.u_boot_version_string = self.u_boot_main_signon
  260. else:
  261. self.u_boot_version_string = self.u_boot_main_signon[:build_idx]
  262. while True:
  263. match = self.p.expect([self.prompt_escaped,
  264. pattern_stop_autoboot_prompt])
  265. if match == 1:
  266. self.p.send(chr(3)) # CTRL-C
  267. continue
  268. break
  269. self.at_prompt = True
  270. self.at_prompt_logevt = self.logstream.logfile.cur_evt
  271. except Exception as ex:
  272. self.log.error(str(ex))
  273. self.cleanup_spawn()
  274. raise
  275. def cleanup_spawn(self):
  276. '''Shut down all interaction with the U-Boot instance.
  277. This is used when an error is detected prior to re-establishing a
  278. connection with a fresh U-Boot instance.
  279. This is an internal function and should not be called directly.
  280. Args:
  281. None.
  282. Returns:
  283. Nothing.
  284. '''
  285. try:
  286. if self.p:
  287. self.p.close()
  288. except:
  289. pass
  290. self.p = None
  291. def validate_version_string_in_text(self, text):
  292. '''Assert that a command's output includes the U-Boot signon message.
  293. This is primarily useful for validating the "version" command without
  294. duplicating the signon text regex in a test function.
  295. Args:
  296. text: The command output text to check.
  297. Returns:
  298. Nothing. An exception is raised if the validation fails.
  299. '''
  300. assert(self.u_boot_version_string in text)
  301. def disable_check(self, check_type):
  302. '''Temporarily disable an error check of U-Boot's output.
  303. Create a new context manager (for use with the "with" statement) which
  304. temporarily disables a particular console output error check.
  305. Args:
  306. check_type: The type of error-check to disable. Valid values may
  307. be found in self.disable_check_count above.
  308. Returns:
  309. A context manager object.
  310. '''
  311. return ConsoleDisableCheck(self, check_type)