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