u_boot_console_base.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. self.ram_base = None
  74. def close(self):
  75. '''Terminate the connection to the U-Boot console.
  76. This function is only useful once all interaction with U-Boot is
  77. complete. Once this function is called, data cannot be sent to or
  78. received from U-Boot.
  79. Args:
  80. None.
  81. Returns:
  82. Nothing.
  83. '''
  84. if self.p:
  85. self.p.close()
  86. self.logstream.close()
  87. def run_command(self, cmd, wait_for_echo=True, send_nl=True,
  88. wait_for_prompt=True):
  89. '''Execute a command via the U-Boot console.
  90. The command is always sent to U-Boot.
  91. U-Boot echoes any command back to its output, and this function
  92. typically waits for that to occur. The wait can be disabled by setting
  93. wait_for_echo=False, which is useful e.g. when sending CTRL-C to
  94. interrupt a long-running command such as "ums".
  95. Command execution is typically triggered by sending a newline
  96. character. This can be disabled by setting send_nl=False, which is
  97. also useful when sending CTRL-C.
  98. This function typically waits for the command to finish executing, and
  99. returns the console output that it generated. This can be disabled by
  100. setting wait_for_prompt=False, which is useful when invoking a long-
  101. running command such as "ums".
  102. Args:
  103. cmd: The command to send.
  104. wait_for_each: Boolean indicating whether to wait for U-Boot to
  105. echo the command text back to its output.
  106. send_nl: Boolean indicating whether to send a newline character
  107. after the command string.
  108. wait_for_prompt: Boolean indicating whether to wait for the
  109. command prompt to be sent by U-Boot. This typically occurs
  110. immediately after the command has been executed.
  111. Returns:
  112. If wait_for_prompt == False:
  113. Nothing.
  114. Else:
  115. The output from U-Boot during command execution. In other
  116. words, the text U-Boot emitted between the point it echod the
  117. command string and emitted the subsequent command prompts.
  118. '''
  119. if self.at_prompt and \
  120. self.at_prompt_logevt != self.logstream.logfile.cur_evt:
  121. self.logstream.write(self.prompt, implicit=True)
  122. bad_patterns = []
  123. bad_pattern_ids = []
  124. if (self.disable_check_count['spl_signon'] == 0 and
  125. self.u_boot_spl_signon):
  126. bad_patterns.append(self.u_boot_spl_signon_escaped)
  127. bad_pattern_ids.append('SPL signon')
  128. if self.disable_check_count['main_signon'] == 0:
  129. bad_patterns.append(self.u_boot_main_signon_escaped)
  130. bad_pattern_ids.append('U-Boot main signon')
  131. if self.disable_check_count['unknown_command'] == 0:
  132. bad_patterns.append(pattern_unknown_command)
  133. bad_pattern_ids.append('Unknown command')
  134. if self.disable_check_count['error_notification'] == 0:
  135. bad_patterns.append(pattern_error_notification)
  136. bad_pattern_ids.append('Error notification')
  137. try:
  138. self.at_prompt = False
  139. if send_nl:
  140. cmd += '\n'
  141. while cmd:
  142. # Limit max outstanding data, so UART FIFOs don't overflow
  143. chunk = cmd[:self.max_fifo_fill]
  144. cmd = cmd[self.max_fifo_fill:]
  145. self.p.send(chunk)
  146. if not wait_for_echo:
  147. continue
  148. chunk = re.escape(chunk)
  149. chunk = chunk.replace('\\\n', '[\r\n]')
  150. m = self.p.expect([chunk] + bad_patterns)
  151. if m != 0:
  152. self.at_prompt = False
  153. raise Exception('Bad pattern found on console: ' +
  154. bad_pattern_ids[m - 1])
  155. if not wait_for_prompt:
  156. return
  157. m = self.p.expect([self.prompt_escaped] + bad_patterns)
  158. if m != 0:
  159. self.at_prompt = False
  160. raise Exception('Bad pattern found on console: ' +
  161. bad_pattern_ids[m - 1])
  162. self.at_prompt = True
  163. self.at_prompt_logevt = self.logstream.logfile.cur_evt
  164. # Only strip \r\n; space/TAB might be significant if testing
  165. # indentation.
  166. return self.p.before.strip('\r\n')
  167. except Exception as ex:
  168. self.log.error(str(ex))
  169. self.cleanup_spawn()
  170. raise
  171. def ctrlc(self):
  172. '''Send a CTRL-C character to U-Boot.
  173. This is useful in order to stop execution of long-running synchronous
  174. commands such as "ums".
  175. Args:
  176. None.
  177. Returns:
  178. Nothing.
  179. '''
  180. self.log.action('Sending Ctrl-C')
  181. self.run_command(chr(3), wait_for_echo=False, send_nl=False)
  182. def drain_console(self):
  183. '''Read from and log the U-Boot console for a short time.
  184. U-Boot's console output is only logged when the test code actively
  185. waits for U-Boot to emit specific data. There are cases where tests
  186. can fail without doing this. For example, if a test asks U-Boot to
  187. enable USB device mode, then polls until a host-side device node
  188. exists. In such a case, it is useful to log U-Boot's console output
  189. in case U-Boot printed clues as to why the host-side even did not
  190. occur. This function will do that.
  191. Args:
  192. None.
  193. Returns:
  194. Nothing.
  195. '''
  196. # If we are already not connected to U-Boot, there's nothing to drain.
  197. # This should only happen when a previous call to run_command() or
  198. # wait_for() failed (and hence the output has already been logged), or
  199. # the system is shutting down.
  200. if not self.p:
  201. return
  202. orig_timeout = self.p.timeout
  203. try:
  204. # Drain the log for a relatively short time.
  205. self.p.timeout = 1000
  206. # Wait for something U-Boot will likely never send. This will
  207. # cause the console output to be read and logged.
  208. self.p.expect(['This should never match U-Boot output'])
  209. except u_boot_spawn.Timeout:
  210. pass
  211. finally:
  212. self.p.timeout = orig_timeout
  213. def ensure_spawned(self):
  214. '''Ensure a connection to a correctly running U-Boot instance.
  215. This may require spawning a new Sandbox process or resetting target
  216. hardware, as defined by the implementation sub-class.
  217. This is an internal function and should not be called directly.
  218. Args:
  219. None.
  220. Returns:
  221. Nothing.
  222. '''
  223. if self.p:
  224. return
  225. try:
  226. self.at_prompt = False
  227. self.log.action('Starting U-Boot')
  228. self.p = self.get_spawn()
  229. # Real targets can take a long time to scroll large amounts of
  230. # text if LCD is enabled. This value may need tweaking in the
  231. # future, possibly per-test to be optimal. This works for 'help'
  232. # on board 'seaboard'.
  233. self.p.timeout = 30000
  234. self.p.logfile_read = self.logstream
  235. if self.config.buildconfig.get('CONFIG_SPL', False) == 'y':
  236. self.p.expect([pattern_u_boot_spl_signon])
  237. self.u_boot_spl_signon = self.p.after
  238. self.u_boot_spl_signon_escaped = re.escape(self.p.after)
  239. else:
  240. self.u_boot_spl_signon = None
  241. self.p.expect([pattern_u_boot_main_signon])
  242. self.u_boot_main_signon = self.p.after
  243. self.u_boot_main_signon_escaped = re.escape(self.p.after)
  244. build_idx = self.u_boot_main_signon.find(', Build:')
  245. if build_idx == -1:
  246. self.u_boot_version_string = self.u_boot_main_signon
  247. else:
  248. self.u_boot_version_string = self.u_boot_main_signon[:build_idx]
  249. while True:
  250. match = self.p.expect([self.prompt_escaped,
  251. pattern_stop_autoboot_prompt])
  252. if match == 1:
  253. self.p.send(chr(3)) # CTRL-C
  254. continue
  255. break
  256. self.at_prompt = True
  257. self.at_prompt_logevt = self.logstream.logfile.cur_evt
  258. except Exception as ex:
  259. self.log.error(str(ex))
  260. self.cleanup_spawn()
  261. raise
  262. def cleanup_spawn(self):
  263. '''Shut down all interaction with the U-Boot instance.
  264. This is used when an error is detected prior to re-establishing a
  265. connection with a fresh U-Boot instance.
  266. This is an internal function and should not be called directly.
  267. Args:
  268. None.
  269. Returns:
  270. Nothing.
  271. '''
  272. try:
  273. if self.p:
  274. self.p.close()
  275. except:
  276. pass
  277. self.p = None
  278. def validate_version_string_in_text(self, text):
  279. '''Assert that a command's output includes the U-Boot signon message.
  280. This is primarily useful for validating the "version" command without
  281. duplicating the signon text regex in a test function.
  282. Args:
  283. text: The command output text to check.
  284. Returns:
  285. Nothing. An exception is raised if the validation fails.
  286. '''
  287. assert(self.u_boot_version_string in text)
  288. def disable_check(self, check_type):
  289. '''Temporarily disable an error check of U-Boot's output.
  290. Create a new context manager (for use with the "with" statement) which
  291. temporarily disables a particular console output error check.
  292. Args:
  293. check_type: The type of error-check to disable. Valid values may
  294. be found in self.disable_check_count above.
  295. Returns:
  296. A context manager object.
  297. '''
  298. return ConsoleDisableCheck(self, check_type)
  299. def find_ram_base(self):
  300. '''Find the running U-Boot's RAM location.
  301. Probe the running U-Boot to determine the address of the first bank
  302. of RAM. This is useful for tests that test reading/writing RAM, or
  303. load/save files that aren't associated with some standard address
  304. typically represented in an environment variable such as
  305. ${kernel_addr_r}. The value is cached so that it only needs to be
  306. actively read once.
  307. Args:
  308. None.
  309. Returns:
  310. The address of U-Boot's first RAM bank, as an integer.
  311. '''
  312. if self.config.buildconfig.get('config_cmd_bdi', 'n') != 'y':
  313. pytest.skip('bdinfo command not supported')
  314. if self.ram_base == -1:
  315. pytest.skip('Previously failed to find RAM bank start')
  316. if self.ram_base is not None:
  317. return self.ram_base
  318. with self.log.section('find_ram_base'):
  319. response = self.run_command('bdinfo')
  320. for l in response.split('\n'):
  321. if '-> start' in l:
  322. self.ram_base = int(l.split('=')[1].strip(), 16)
  323. break
  324. if self.ram_base is None:
  325. self.ram_base = -1
  326. raise Exception('Failed to find RAM bank start in `bdinfo`')
  327. return self.ram_base