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