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