Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 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 | |
| 6 | # Common logic to interact with U-Boot via the console. This class provides |
| 7 | # the interface that tests use to execute U-Boot shell commands and wait for |
| 8 | # their results. Sub-classes exist to perform board-type-specific setup |
| 9 | # operations, such as spawning a sub-process for Sandbox, or attaching to the |
| 10 | # serial console of real hardware. |
| 11 | |
| 12 | import multiplexed_log |
| 13 | import os |
| 14 | import pytest |
| 15 | import re |
| 16 | import sys |
Stephen Warren | 97a5466 | 2016-01-22 12:30:09 -0700 | [diff] [blame] | 17 | import u_boot_spawn |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 18 | |
| 19 | # Regexes for text we expect U-Boot to send to the console. |
Stephen Warren | 5af83c4 | 2016-02-05 18:04:43 -0700 | [diff] [blame] | 20 | pattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}[^\r\n]*\\))') |
| 21 | pattern_u_boot_main_signon = re.compile('(U-Boot \\d{4}\\.\\d{2}[^\r\n]*\\))') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 22 | pattern_stop_autoboot_prompt = re.compile('Hit any key to stop autoboot: ') |
| 23 | pattern_unknown_command = re.compile('Unknown command \'.*\' - try \'help\'') |
| 24 | pattern_error_notification = re.compile('## Error: ') |
Stephen Warren | 3bd79d3 | 2016-01-27 23:57:50 -0700 | [diff] [blame] | 25 | pattern_error_please_reset = re.compile('### ERROR ### Please RESET the board ###') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 26 | |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 27 | PAT_ID = 0 |
| 28 | PAT_RE = 1 |
| 29 | |
| 30 | bad_pattern_defs = ( |
| 31 | ('spl_signon', pattern_u_boot_spl_signon), |
| 32 | ('main_signon', pattern_u_boot_main_signon), |
| 33 | ('stop_autoboot_prompt', pattern_stop_autoboot_prompt), |
| 34 | ('unknown_command', pattern_unknown_command), |
| 35 | ('error_notification', pattern_error_notification), |
Stephen Warren | 3bd79d3 | 2016-01-27 23:57:50 -0700 | [diff] [blame] | 36 | ('error_please_reset', pattern_error_please_reset), |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 37 | ) |
| 38 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 39 | class ConsoleDisableCheck(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """Context manager (for Python's with statement) that temporarily disables |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 41 | the specified console output error check. This is useful when deliberately |
| 42 | executing a command that is known to trigger one of the error checks, in |
| 43 | order to test that the error condition is actually raised. This class is |
| 44 | used internally by ConsoleBase::disable_check(); it is not intended for |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 45 | direct usage.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 46 | |
| 47 | def __init__(self, console, check_type): |
| 48 | self.console = console |
| 49 | self.check_type = check_type |
| 50 | |
| 51 | def __enter__(self): |
| 52 | self.console.disable_check_count[self.check_type] += 1 |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 53 | self.console.eval_bad_patterns() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 54 | |
| 55 | def __exit__(self, extype, value, traceback): |
| 56 | self.console.disable_check_count[self.check_type] -= 1 |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 57 | self.console.eval_bad_patterns() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 58 | |
Michal Simek | 6b46318 | 2016-05-19 07:57:41 +0200 | [diff] [blame] | 59 | class ConsoleSetupTimeout(object): |
| 60 | """Context manager (for Python's with statement) that temporarily sets up |
| 61 | timeout for specific command. This is useful when execution time is greater |
| 62 | then default 30s.""" |
| 63 | |
| 64 | def __init__(self, console, timeout): |
| 65 | self.p = console.p |
| 66 | self.orig_timeout = self.p.timeout |
| 67 | self.p.timeout = timeout |
| 68 | |
| 69 | def __enter__(self): |
| 70 | return self |
| 71 | |
| 72 | def __exit__(self, extype, value, traceback): |
| 73 | self.p.timeout = self.orig_timeout |
| 74 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 75 | class ConsoleBase(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 76 | """The interface through which test functions interact with the U-Boot |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 77 | console. This primarily involves executing shell commands, capturing their |
| 78 | results, and checking for common error conditions. Some common utilities |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 79 | are also provided too.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 80 | |
| 81 | def __init__(self, log, config, max_fifo_fill): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 82 | """Initialize a U-Boot console connection. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 83 | |
| 84 | Can only usefully be called by sub-classes. |
| 85 | |
| 86 | Args: |
| 87 | log: A mulptiplex_log.Logfile object, to which the U-Boot output |
| 88 | will be logged. |
| 89 | config: A configuration data structure, as built by conftest.py. |
| 90 | max_fifo_fill: The maximum number of characters to send to U-Boot |
| 91 | command-line before waiting for U-Boot to echo the characters |
| 92 | back. For UART-based HW without HW flow control, this value |
| 93 | should be set less than the UART RX FIFO size to avoid |
| 94 | overflow, assuming that U-Boot can't keep up with full-rate |
| 95 | traffic at the baud rate. |
| 96 | |
| 97 | Returns: |
| 98 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 99 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 100 | |
| 101 | self.log = log |
| 102 | self.config = config |
| 103 | self.max_fifo_fill = max_fifo_fill |
| 104 | |
| 105 | self.logstream = self.log.get_stream('console', sys.stdout) |
| 106 | |
| 107 | # Array slice removes leading/trailing quotes |
| 108 | self.prompt = self.config.buildconfig['config_sys_prompt'][1:-1] |
Stephen Warren | 6d08340 | 2016-08-16 19:58:59 -0600 | [diff] [blame] | 109 | self.prompt_compiled = re.compile('^' + re.escape(self.prompt), re.MULTILINE) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 110 | self.p = None |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 111 | self.disable_check_count = {pat[PAT_ID]: 0 for pat in bad_pattern_defs} |
| 112 | self.eval_bad_patterns() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 113 | |
| 114 | self.at_prompt = False |
| 115 | self.at_prompt_logevt = None |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 116 | |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 117 | def eval_bad_patterns(self): |
| 118 | self.bad_patterns = [pat[PAT_RE] for pat in bad_pattern_defs \ |
| 119 | if self.disable_check_count[pat[PAT_ID]] == 0] |
| 120 | self.bad_pattern_ids = [pat[PAT_ID] for pat in bad_pattern_defs \ |
| 121 | if self.disable_check_count[pat[PAT_ID]] == 0] |
| 122 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 123 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 124 | """Terminate the connection to the U-Boot console. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 125 | |
| 126 | This function is only useful once all interaction with U-Boot is |
| 127 | complete. Once this function is called, data cannot be sent to or |
| 128 | received from U-Boot. |
| 129 | |
| 130 | Args: |
| 131 | None. |
| 132 | |
| 133 | Returns: |
| 134 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 135 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 136 | |
| 137 | if self.p: |
| 138 | self.p.close() |
| 139 | self.logstream.close() |
| 140 | |
| 141 | def run_command(self, cmd, wait_for_echo=True, send_nl=True, |
| 142 | wait_for_prompt=True): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 143 | """Execute a command via the U-Boot console. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 144 | |
| 145 | The command is always sent to U-Boot. |
| 146 | |
| 147 | U-Boot echoes any command back to its output, and this function |
| 148 | typically waits for that to occur. The wait can be disabled by setting |
| 149 | wait_for_echo=False, which is useful e.g. when sending CTRL-C to |
| 150 | interrupt a long-running command such as "ums". |
| 151 | |
| 152 | Command execution is typically triggered by sending a newline |
| 153 | character. This can be disabled by setting send_nl=False, which is |
| 154 | also useful when sending CTRL-C. |
| 155 | |
| 156 | This function typically waits for the command to finish executing, and |
| 157 | returns the console output that it generated. This can be disabled by |
| 158 | setting wait_for_prompt=False, which is useful when invoking a long- |
| 159 | running command such as "ums". |
| 160 | |
| 161 | Args: |
| 162 | cmd: The command to send. |
Heinrich Schuchardt | bec160a | 2017-09-14 12:27:07 +0200 | [diff] [blame] | 163 | wait_for_echo: Boolean indicating whether to wait for U-Boot to |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 164 | echo the command text back to its output. |
| 165 | send_nl: Boolean indicating whether to send a newline character |
| 166 | after the command string. |
| 167 | wait_for_prompt: Boolean indicating whether to wait for the |
| 168 | command prompt to be sent by U-Boot. This typically occurs |
| 169 | immediately after the command has been executed. |
| 170 | |
| 171 | Returns: |
| 172 | If wait_for_prompt == False: |
| 173 | Nothing. |
| 174 | Else: |
| 175 | The output from U-Boot during command execution. In other |
| 176 | words, the text U-Boot emitted between the point it echod the |
| 177 | command string and emitted the subsequent command prompts. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 178 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 179 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 180 | if self.at_prompt and \ |
| 181 | self.at_prompt_logevt != self.logstream.logfile.cur_evt: |
| 182 | self.logstream.write(self.prompt, implicit=True) |
| 183 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 184 | try: |
| 185 | self.at_prompt = False |
| 186 | if send_nl: |
| 187 | cmd += '\n' |
| 188 | while cmd: |
| 189 | # Limit max outstanding data, so UART FIFOs don't overflow |
| 190 | chunk = cmd[:self.max_fifo_fill] |
| 191 | cmd = cmd[self.max_fifo_fill:] |
| 192 | self.p.send(chunk) |
| 193 | if not wait_for_echo: |
| 194 | continue |
| 195 | chunk = re.escape(chunk) |
| 196 | chunk = chunk.replace('\\\n', '[\r\n]') |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 197 | m = self.p.expect([chunk] + self.bad_patterns) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 198 | if m != 0: |
| 199 | self.at_prompt = False |
| 200 | raise Exception('Bad pattern found on console: ' + |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 201 | self.bad_pattern_ids[m - 1]) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 202 | if not wait_for_prompt: |
| 203 | return |
Stephen Warren | 6d08340 | 2016-08-16 19:58:59 -0600 | [diff] [blame] | 204 | m = self.p.expect([self.prompt_compiled] + self.bad_patterns) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 205 | if m != 0: |
| 206 | self.at_prompt = False |
| 207 | raise Exception('Bad pattern found on console: ' + |
Stephen Warren | 1115a97 | 2016-01-27 23:57:48 -0700 | [diff] [blame] | 208 | self.bad_pattern_ids[m - 1]) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 209 | self.at_prompt = True |
| 210 | self.at_prompt_logevt = self.logstream.logfile.cur_evt |
| 211 | # Only strip \r\n; space/TAB might be significant if testing |
| 212 | # indentation. |
| 213 | return self.p.before.strip('\r\n') |
| 214 | except Exception as ex: |
| 215 | self.log.error(str(ex)) |
| 216 | self.cleanup_spawn() |
| 217 | raise |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 218 | finally: |
| 219 | self.log.timestamp() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 220 | |
Simon Glass | 2436bb0 | 2016-07-03 09:40:42 -0600 | [diff] [blame] | 221 | def run_command_list(self, cmds): |
| 222 | """Run a list of commands. |
| 223 | |
| 224 | This is a helper function to call run_command() with default arguments |
| 225 | for each command in a list. |
| 226 | |
| 227 | Args: |
Simon Glass | d5deca0 | 2016-07-31 17:35:04 -0600 | [diff] [blame] | 228 | cmd: List of commands (each a string). |
Simon Glass | 2436bb0 | 2016-07-03 09:40:42 -0600 | [diff] [blame] | 229 | Returns: |
Simon Glass | 2ca7311 | 2016-07-31 17:35:09 -0600 | [diff] [blame] | 230 | A list of output strings from each command, one element for each |
| 231 | command. |
Simon Glass | 2436bb0 | 2016-07-03 09:40:42 -0600 | [diff] [blame] | 232 | """ |
Simon Glass | 2ca7311 | 2016-07-31 17:35:09 -0600 | [diff] [blame] | 233 | output = [] |
Simon Glass | 2436bb0 | 2016-07-03 09:40:42 -0600 | [diff] [blame] | 234 | for cmd in cmds: |
Simon Glass | 2ca7311 | 2016-07-31 17:35:09 -0600 | [diff] [blame] | 235 | output.append(self.run_command(cmd)) |
Simon Glass | 2436bb0 | 2016-07-03 09:40:42 -0600 | [diff] [blame] | 236 | return output |
| 237 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 238 | def ctrlc(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 239 | """Send a CTRL-C character to U-Boot. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 240 | |
| 241 | This is useful in order to stop execution of long-running synchronous |
| 242 | commands such as "ums". |
| 243 | |
| 244 | Args: |
| 245 | None. |
| 246 | |
| 247 | Returns: |
| 248 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 249 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 250 | |
Stephen Warren | a88c417 | 2016-01-22 12:30:10 -0700 | [diff] [blame] | 251 | self.log.action('Sending Ctrl-C') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 252 | self.run_command(chr(3), wait_for_echo=False, send_nl=False) |
| 253 | |
Stephen Warren | ef824f5 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 254 | def wait_for(self, text): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 255 | """Wait for a pattern to be emitted by U-Boot. |
Stephen Warren | ef824f5 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 256 | |
| 257 | This is useful when a long-running command such as "dfu" is executing, |
| 258 | and it periodically emits some text that should show up at a specific |
| 259 | location in the log file. |
| 260 | |
| 261 | Args: |
| 262 | text: The text to wait for; either a string (containing raw text, |
| 263 | not a regular expression) or an re object. |
| 264 | |
| 265 | Returns: |
| 266 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 267 | """ |
Stephen Warren | ef824f5 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 268 | |
| 269 | if type(text) == type(''): |
| 270 | text = re.escape(text) |
Stephen Warren | 68a9bb6 | 2016-01-27 23:57:49 -0700 | [diff] [blame] | 271 | m = self.p.expect([text] + self.bad_patterns) |
| 272 | if m != 0: |
| 273 | raise Exception('Bad pattern found on console: ' + |
| 274 | self.bad_pattern_ids[m - 1]) |
Stephen Warren | ef824f5 | 2016-01-22 12:30:12 -0700 | [diff] [blame] | 275 | |
Stephen Warren | 97a5466 | 2016-01-22 12:30:09 -0700 | [diff] [blame] | 276 | def drain_console(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 277 | """Read from and log the U-Boot console for a short time. |
Stephen Warren | 97a5466 | 2016-01-22 12:30:09 -0700 | [diff] [blame] | 278 | |
| 279 | U-Boot's console output is only logged when the test code actively |
| 280 | waits for U-Boot to emit specific data. There are cases where tests |
| 281 | can fail without doing this. For example, if a test asks U-Boot to |
| 282 | enable USB device mode, then polls until a host-side device node |
| 283 | exists. In such a case, it is useful to log U-Boot's console output |
| 284 | in case U-Boot printed clues as to why the host-side even did not |
| 285 | occur. This function will do that. |
| 286 | |
| 287 | Args: |
| 288 | None. |
| 289 | |
| 290 | Returns: |
| 291 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 292 | """ |
Stephen Warren | 97a5466 | 2016-01-22 12:30:09 -0700 | [diff] [blame] | 293 | |
| 294 | # If we are already not connected to U-Boot, there's nothing to drain. |
| 295 | # This should only happen when a previous call to run_command() or |
| 296 | # wait_for() failed (and hence the output has already been logged), or |
| 297 | # the system is shutting down. |
| 298 | if not self.p: |
| 299 | return |
| 300 | |
| 301 | orig_timeout = self.p.timeout |
| 302 | try: |
| 303 | # Drain the log for a relatively short time. |
| 304 | self.p.timeout = 1000 |
| 305 | # Wait for something U-Boot will likely never send. This will |
| 306 | # cause the console output to be read and logged. |
| 307 | self.p.expect(['This should never match U-Boot output']) |
| 308 | except u_boot_spawn.Timeout: |
| 309 | pass |
| 310 | finally: |
| 311 | self.p.timeout = orig_timeout |
| 312 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 313 | def ensure_spawned(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 314 | """Ensure a connection to a correctly running U-Boot instance. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 315 | |
| 316 | This may require spawning a new Sandbox process or resetting target |
| 317 | hardware, as defined by the implementation sub-class. |
| 318 | |
| 319 | This is an internal function and should not be called directly. |
| 320 | |
| 321 | Args: |
| 322 | None. |
| 323 | |
| 324 | Returns: |
| 325 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 326 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 327 | |
| 328 | if self.p: |
| 329 | return |
| 330 | try: |
Stephen Warren | 80eea63 | 2016-02-11 11:46:12 -0700 | [diff] [blame] | 331 | self.log.start_section('Starting U-Boot') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 332 | self.at_prompt = False |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 333 | self.p = self.get_spawn() |
| 334 | # Real targets can take a long time to scroll large amounts of |
| 335 | # text if LCD is enabled. This value may need tweaking in the |
| 336 | # future, possibly per-test to be optimal. This works for 'help' |
| 337 | # on board 'seaboard'. |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 338 | if not self.config.gdbserver: |
| 339 | self.p.timeout = 30000 |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 340 | self.p.logfile_read = self.logstream |
Heiko Schocher | 48d5a7e | 2016-02-17 18:32:51 +0100 | [diff] [blame] | 341 | bcfg = self.config.buildconfig |
| 342 | config_spl = bcfg.get('config_spl', 'n') == 'y' |
| 343 | config_spl_serial_support = bcfg.get('config_spl_serial_support', |
| 344 | 'n') == 'y' |
Michal Simek | 777526a | 2016-02-25 14:58:24 +0100 | [diff] [blame] | 345 | env_spl_skipped = self.config.env.get('env__spl_skipped', |
| 346 | False) |
| 347 | if config_spl and config_spl_serial_support and not env_spl_skipped: |
Heiko Schocher | 48d5a7e | 2016-02-17 18:32:51 +0100 | [diff] [blame] | 348 | m = self.p.expect([pattern_u_boot_spl_signon] + |
| 349 | self.bad_patterns) |
Stephen Warren | 68a9bb6 | 2016-01-27 23:57:49 -0700 | [diff] [blame] | 350 | if m != 0: |
Simon Glass | ba83e94 | 2016-07-04 11:58:38 -0600 | [diff] [blame] | 351 | raise Exception('Bad pattern found on SPL console: ' + |
Stephen Warren | 68a9bb6 | 2016-01-27 23:57:49 -0700 | [diff] [blame] | 352 | self.bad_pattern_ids[m - 1]) |
| 353 | m = self.p.expect([pattern_u_boot_main_signon] + self.bad_patterns) |
| 354 | if m != 0: |
| 355 | raise Exception('Bad pattern found on console: ' + |
| 356 | self.bad_pattern_ids[m - 1]) |
Stephen Warren | 5af83c4 | 2016-02-05 18:04:43 -0700 | [diff] [blame] | 357 | self.u_boot_version_string = self.p.after |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 358 | while True: |
Stephen Warren | 6d08340 | 2016-08-16 19:58:59 -0600 | [diff] [blame] | 359 | m = self.p.expect([self.prompt_compiled, |
Stephen Warren | 68a9bb6 | 2016-01-27 23:57:49 -0700 | [diff] [blame] | 360 | pattern_stop_autoboot_prompt] + self.bad_patterns) |
| 361 | if m == 0: |
| 362 | break |
| 363 | if m == 1: |
Stephen Warren | 850e3f7 | 2016-02-15 17:39:38 -0700 | [diff] [blame] | 364 | self.p.send(' ') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 365 | continue |
Stephen Warren | 68a9bb6 | 2016-01-27 23:57:49 -0700 | [diff] [blame] | 366 | raise Exception('Bad pattern found on console: ' + |
| 367 | self.bad_pattern_ids[m - 2]) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 368 | self.at_prompt = True |
| 369 | self.at_prompt_logevt = self.logstream.logfile.cur_evt |
| 370 | except Exception as ex: |
| 371 | self.log.error(str(ex)) |
| 372 | self.cleanup_spawn() |
| 373 | raise |
Stephen Warren | 80eea63 | 2016-02-11 11:46:12 -0700 | [diff] [blame] | 374 | finally: |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 375 | self.log.timestamp() |
Stephen Warren | 80eea63 | 2016-02-11 11:46:12 -0700 | [diff] [blame] | 376 | self.log.end_section('Starting U-Boot') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 377 | |
| 378 | def cleanup_spawn(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 379 | """Shut down all interaction with the U-Boot instance. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 380 | |
| 381 | This is used when an error is detected prior to re-establishing a |
| 382 | connection with a fresh U-Boot instance. |
| 383 | |
| 384 | This is an internal function and should not be called directly. |
| 385 | |
| 386 | Args: |
| 387 | None. |
| 388 | |
| 389 | Returns: |
| 390 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 391 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 392 | |
| 393 | try: |
| 394 | if self.p: |
| 395 | self.p.close() |
| 396 | except: |
| 397 | pass |
| 398 | self.p = None |
| 399 | |
Simon Glass | 37c2ce1 | 2016-07-31 17:35:08 -0600 | [diff] [blame] | 400 | def restart_uboot(self): |
| 401 | """Shut down and restart U-Boot.""" |
| 402 | self.cleanup_spawn() |
| 403 | self.ensure_spawned() |
| 404 | |
Simon Glass | 9bc2083 | 2016-07-04 11:58:39 -0600 | [diff] [blame] | 405 | def get_spawn_output(self): |
| 406 | """Return the start-up output from U-Boot |
| 407 | |
| 408 | Returns: |
| 409 | The output produced by ensure_spawed(), as a string. |
| 410 | """ |
| 411 | if self.p: |
| 412 | return self.p.get_expect_output() |
| 413 | return None |
| 414 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 415 | def validate_version_string_in_text(self, text): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 416 | """Assert that a command's output includes the U-Boot signon message. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 417 | |
| 418 | This is primarily useful for validating the "version" command without |
| 419 | duplicating the signon text regex in a test function. |
| 420 | |
| 421 | Args: |
| 422 | text: The command output text to check. |
| 423 | |
| 424 | Returns: |
| 425 | Nothing. An exception is raised if the validation fails. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 426 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 427 | |
| 428 | assert(self.u_boot_version_string in text) |
| 429 | |
| 430 | def disable_check(self, check_type): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 431 | """Temporarily disable an error check of U-Boot's output. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 432 | |
| 433 | Create a new context manager (for use with the "with" statement) which |
| 434 | temporarily disables a particular console output error check. |
| 435 | |
| 436 | Args: |
| 437 | check_type: The type of error-check to disable. Valid values may |
| 438 | be found in self.disable_check_count above. |
| 439 | |
| 440 | Returns: |
| 441 | A context manager object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 442 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 443 | |
| 444 | return ConsoleDisableCheck(self, check_type) |
Michal Simek | 6b46318 | 2016-05-19 07:57:41 +0200 | [diff] [blame] | 445 | |
| 446 | def temporary_timeout(self, timeout): |
| 447 | """Temporarily set up different timeout for commands. |
| 448 | |
| 449 | Create a new context manager (for use with the "with" statement) which |
| 450 | temporarily change timeout. |
| 451 | |
| 452 | Args: |
| 453 | timeout: Time in milliseconds. |
| 454 | |
| 455 | Returns: |
| 456 | A context manager object. |
| 457 | """ |
| 458 | |
| 459 | return ConsoleSetupTimeout(self, timeout) |