xref: /rk3399_rockchip-uboot/test/py/u_boot_console_base.py (revision f6d34651d8a21514abbfa32f44a0ec2014ef002b)
1d201506cSStephen Warren# Copyright (c) 2015 Stephen Warren
2d201506cSStephen Warren# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
3d201506cSStephen Warren#
4d201506cSStephen Warren# SPDX-License-Identifier: GPL-2.0
5d201506cSStephen Warren
6d201506cSStephen Warren# Common logic to interact with U-Boot via the console. This class provides
7d201506cSStephen Warren# the interface that tests use to execute U-Boot shell commands and wait for
8d201506cSStephen Warren# their results. Sub-classes exist to perform board-type-specific setup
9d201506cSStephen Warren# operations, such as spawning a sub-process for Sandbox, or attaching to the
10d201506cSStephen Warren# serial console of real hardware.
11d201506cSStephen Warren
12d201506cSStephen Warrenimport multiplexed_log
13d201506cSStephen Warrenimport os
14d201506cSStephen Warrenimport pytest
15d201506cSStephen Warrenimport re
16d201506cSStephen Warrenimport sys
17c10eb9d3SStephen Warrenimport u_boot_spawn
18d201506cSStephen Warren
19d201506cSStephen Warren# Regexes for text we expect U-Boot to send to the console.
20c82ce04aSStephen Warrenpattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}[^\r\n]*\\))')
21c82ce04aSStephen Warrenpattern_u_boot_main_signon = re.compile('(U-Boot \\d{4}\\.\\d{2}[^\r\n]*\\))')
22d201506cSStephen Warrenpattern_stop_autoboot_prompt = re.compile('Hit any key to stop autoboot: ')
23d201506cSStephen Warrenpattern_unknown_command = re.compile('Unknown command \'.*\' - try \'help\'')
24d201506cSStephen Warrenpattern_error_notification = re.compile('## Error: ')
259129d9f5SStephen Warrenpattern_error_please_reset = re.compile('### ERROR ### Please RESET the board ###')
26d201506cSStephen Warren
27e4119ebbSStephen WarrenPAT_ID = 0
28e4119ebbSStephen WarrenPAT_RE = 1
29e4119ebbSStephen Warren
30e4119ebbSStephen Warrenbad_pattern_defs = (
31e4119ebbSStephen Warren    ('spl_signon', pattern_u_boot_spl_signon),
32e4119ebbSStephen Warren    ('main_signon', pattern_u_boot_main_signon),
33e4119ebbSStephen Warren    ('stop_autoboot_prompt', pattern_stop_autoboot_prompt),
34e4119ebbSStephen Warren    ('unknown_command', pattern_unknown_command),
35e4119ebbSStephen Warren    ('error_notification', pattern_error_notification),
369129d9f5SStephen Warren    ('error_please_reset', pattern_error_please_reset),
37e4119ebbSStephen Warren)
38e4119ebbSStephen Warren
39d201506cSStephen Warrenclass ConsoleDisableCheck(object):
40e8debf39SStephen Warren    """Context manager (for Python's with statement) that temporarily disables
41d201506cSStephen Warren    the specified console output error check. This is useful when deliberately
42d201506cSStephen Warren    executing a command that is known to trigger one of the error checks, in
43d201506cSStephen Warren    order to test that the error condition is actually raised. This class is
44d201506cSStephen Warren    used internally by ConsoleBase::disable_check(); it is not intended for
45e8debf39SStephen Warren    direct usage."""
46d201506cSStephen Warren
47d201506cSStephen Warren    def __init__(self, console, check_type):
48d201506cSStephen Warren        self.console = console
49d201506cSStephen Warren        self.check_type = check_type
50d201506cSStephen Warren
51d201506cSStephen Warren    def __enter__(self):
52d201506cSStephen Warren        self.console.disable_check_count[self.check_type] += 1
53e4119ebbSStephen Warren        self.console.eval_bad_patterns()
54d201506cSStephen Warren
55d201506cSStephen Warren    def __exit__(self, extype, value, traceback):
56d201506cSStephen Warren        self.console.disable_check_count[self.check_type] -= 1
57e4119ebbSStephen Warren        self.console.eval_bad_patterns()
58d201506cSStephen Warren
5987861c19SMichal Simekclass ConsoleSetupTimeout(object):
6087861c19SMichal Simek    """Context manager (for Python's with statement) that temporarily sets up
6187861c19SMichal Simek    timeout for specific command. This is useful when execution time is greater
6287861c19SMichal Simek    then default 30s."""
6387861c19SMichal Simek
6487861c19SMichal Simek    def __init__(self, console, timeout):
6587861c19SMichal Simek        self.p = console.p
6687861c19SMichal Simek        self.orig_timeout = self.p.timeout
6787861c19SMichal Simek        self.p.timeout = timeout
6887861c19SMichal Simek
6987861c19SMichal Simek    def __enter__(self):
7087861c19SMichal Simek        return self
7187861c19SMichal Simek
7287861c19SMichal Simek    def __exit__(self, extype, value, traceback):
7387861c19SMichal Simek        self.p.timeout = self.orig_timeout
7487861c19SMichal Simek
75d201506cSStephen Warrenclass ConsoleBase(object):
76e8debf39SStephen Warren    """The interface through which test functions interact with the U-Boot
77d201506cSStephen Warren    console. This primarily involves executing shell commands, capturing their
78d201506cSStephen Warren    results, and checking for common error conditions. Some common utilities
79e8debf39SStephen Warren    are also provided too."""
80d201506cSStephen Warren
81d201506cSStephen Warren    def __init__(self, log, config, max_fifo_fill):
82e8debf39SStephen Warren        """Initialize a U-Boot console connection.
83d201506cSStephen Warren
84d201506cSStephen Warren        Can only usefully be called by sub-classes.
85d201506cSStephen Warren
86d201506cSStephen Warren        Args:
87d201506cSStephen Warren            log: A mulptiplex_log.Logfile object, to which the U-Boot output
88d201506cSStephen Warren                will be logged.
89d201506cSStephen Warren            config: A configuration data structure, as built by conftest.py.
90d201506cSStephen Warren            max_fifo_fill: The maximum number of characters to send to U-Boot
91d201506cSStephen Warren                command-line before waiting for U-Boot to echo the characters
92d201506cSStephen Warren                back. For UART-based HW without HW flow control, this value
93d201506cSStephen Warren                should be set less than the UART RX FIFO size to avoid
94d201506cSStephen Warren                overflow, assuming that U-Boot can't keep up with full-rate
95d201506cSStephen Warren                traffic at the baud rate.
96d201506cSStephen Warren
97d201506cSStephen Warren        Returns:
98d201506cSStephen Warren            Nothing.
99e8debf39SStephen Warren        """
100d201506cSStephen Warren
101d201506cSStephen Warren        self.log = log
102d201506cSStephen Warren        self.config = config
103d201506cSStephen Warren        self.max_fifo_fill = max_fifo_fill
104d201506cSStephen Warren
105d201506cSStephen Warren        self.logstream = self.log.get_stream('console', sys.stdout)
106d201506cSStephen Warren
107d201506cSStephen Warren        # Array slice removes leading/trailing quotes
108d201506cSStephen Warren        self.prompt = self.config.buildconfig['config_sys_prompt'][1:-1]
109d201506cSStephen Warren        self.prompt_escaped = re.escape(self.prompt)
110d201506cSStephen Warren        self.p = None
111e4119ebbSStephen Warren        self.disable_check_count = {pat[PAT_ID]: 0 for pat in bad_pattern_defs}
112e4119ebbSStephen Warren        self.eval_bad_patterns()
113d201506cSStephen Warren
114d201506cSStephen Warren        self.at_prompt = False
115d201506cSStephen Warren        self.at_prompt_logevt = None
116d201506cSStephen Warren
117e4119ebbSStephen Warren    def eval_bad_patterns(self):
118e4119ebbSStephen Warren        self.bad_patterns = [pat[PAT_RE] for pat in bad_pattern_defs \
119e4119ebbSStephen Warren            if self.disable_check_count[pat[PAT_ID]] == 0]
120e4119ebbSStephen Warren        self.bad_pattern_ids = [pat[PAT_ID] for pat in bad_pattern_defs \
121e4119ebbSStephen Warren            if self.disable_check_count[pat[PAT_ID]] == 0]
122e4119ebbSStephen Warren
123d201506cSStephen Warren    def close(self):
124e8debf39SStephen Warren        """Terminate the connection to the U-Boot console.
125d201506cSStephen Warren
126d201506cSStephen Warren        This function is only useful once all interaction with U-Boot is
127d201506cSStephen Warren        complete. Once this function is called, data cannot be sent to or
128d201506cSStephen Warren        received from U-Boot.
129d201506cSStephen Warren
130d201506cSStephen Warren        Args:
131d201506cSStephen Warren            None.
132d201506cSStephen Warren
133d201506cSStephen Warren        Returns:
134d201506cSStephen Warren            Nothing.
135e8debf39SStephen Warren        """
136d201506cSStephen Warren
137d201506cSStephen Warren        if self.p:
138d201506cSStephen Warren            self.p.close()
139d201506cSStephen Warren        self.logstream.close()
140d201506cSStephen Warren
141d201506cSStephen Warren    def run_command(self, cmd, wait_for_echo=True, send_nl=True,
142d201506cSStephen Warren            wait_for_prompt=True):
143e8debf39SStephen Warren        """Execute a command via the U-Boot console.
144d201506cSStephen Warren
145d201506cSStephen Warren        The command is always sent to U-Boot.
146d201506cSStephen Warren
147d201506cSStephen Warren        U-Boot echoes any command back to its output, and this function
148d201506cSStephen Warren        typically waits for that to occur. The wait can be disabled by setting
149d201506cSStephen Warren        wait_for_echo=False, which is useful e.g. when sending CTRL-C to
150d201506cSStephen Warren        interrupt a long-running command such as "ums".
151d201506cSStephen Warren
152d201506cSStephen Warren        Command execution is typically triggered by sending a newline
153d201506cSStephen Warren        character. This can be disabled by setting send_nl=False, which is
154d201506cSStephen Warren        also useful when sending CTRL-C.
155d201506cSStephen Warren
156d201506cSStephen Warren        This function typically waits for the command to finish executing, and
157d201506cSStephen Warren        returns the console output that it generated. This can be disabled by
158d201506cSStephen Warren        setting wait_for_prompt=False, which is useful when invoking a long-
159d201506cSStephen Warren        running command such as "ums".
160d201506cSStephen Warren
161d201506cSStephen Warren        Args:
162d201506cSStephen Warren            cmd: The command to send.
163d201506cSStephen Warren            wait_for_each: Boolean indicating whether to wait for U-Boot to
164d201506cSStephen Warren                echo the command text back to its output.
165d201506cSStephen Warren            send_nl: Boolean indicating whether to send a newline character
166d201506cSStephen Warren                after the command string.
167d201506cSStephen Warren            wait_for_prompt: Boolean indicating whether to wait for the
168d201506cSStephen Warren                command prompt to be sent by U-Boot. This typically occurs
169d201506cSStephen Warren                immediately after the command has been executed.
170d201506cSStephen Warren
171d201506cSStephen Warren        Returns:
172d201506cSStephen Warren            If wait_for_prompt == False:
173d201506cSStephen Warren                Nothing.
174d201506cSStephen Warren            Else:
175d201506cSStephen Warren                The output from U-Boot during command execution. In other
176d201506cSStephen Warren                words, the text U-Boot emitted between the point it echod the
177d201506cSStephen Warren                command string and emitted the subsequent command prompts.
178e8debf39SStephen Warren        """
179d201506cSStephen Warren
180d201506cSStephen Warren        if self.at_prompt and \
181d201506cSStephen Warren                self.at_prompt_logevt != self.logstream.logfile.cur_evt:
182d201506cSStephen Warren            self.logstream.write(self.prompt, implicit=True)
183d201506cSStephen Warren
184d201506cSStephen Warren        try:
185d201506cSStephen Warren            self.at_prompt = False
186d201506cSStephen Warren            if send_nl:
187d201506cSStephen Warren                cmd += '\n'
188d201506cSStephen Warren            while cmd:
189d201506cSStephen Warren                # Limit max outstanding data, so UART FIFOs don't overflow
190d201506cSStephen Warren                chunk = cmd[:self.max_fifo_fill]
191d201506cSStephen Warren                cmd = cmd[self.max_fifo_fill:]
192d201506cSStephen Warren                self.p.send(chunk)
193d201506cSStephen Warren                if not wait_for_echo:
194d201506cSStephen Warren                    continue
195d201506cSStephen Warren                chunk = re.escape(chunk)
196d201506cSStephen Warren                chunk = chunk.replace('\\\n', '[\r\n]')
197e4119ebbSStephen Warren                m = self.p.expect([chunk] + self.bad_patterns)
198d201506cSStephen Warren                if m != 0:
199d201506cSStephen Warren                    self.at_prompt = False
200d201506cSStephen Warren                    raise Exception('Bad pattern found on console: ' +
201e4119ebbSStephen Warren                                    self.bad_pattern_ids[m - 1])
202d201506cSStephen Warren            if not wait_for_prompt:
203d201506cSStephen Warren                return
204e4119ebbSStephen Warren            m = self.p.expect([self.prompt_escaped] + self.bad_patterns)
205d201506cSStephen Warren            if m != 0:
206d201506cSStephen Warren                self.at_prompt = False
207d201506cSStephen Warren                raise Exception('Bad pattern found on console: ' +
208e4119ebbSStephen Warren                                self.bad_pattern_ids[m - 1])
209d201506cSStephen Warren            self.at_prompt = True
210d201506cSStephen Warren            self.at_prompt_logevt = self.logstream.logfile.cur_evt
211d201506cSStephen Warren            # Only strip \r\n; space/TAB might be significant if testing
212d201506cSStephen Warren            # indentation.
213d201506cSStephen Warren            return self.p.before.strip('\r\n')
214d201506cSStephen Warren        except Exception as ex:
215d201506cSStephen Warren            self.log.error(str(ex))
216d201506cSStephen Warren            self.cleanup_spawn()
217d201506cSStephen Warren            raise
218d201506cSStephen Warren
21973a9054dSSimon Glass    def run_command_list(self, cmds):
22073a9054dSSimon Glass        """Run a list of commands.
22173a9054dSSimon Glass
22273a9054dSSimon Glass        This is a helper function to call run_command() with default arguments
22373a9054dSSimon Glass        for each command in a list.
22473a9054dSSimon Glass
22573a9054dSSimon Glass        Args:
22672f52268SSimon Glass            cmd: List of commands (each a string).
22773a9054dSSimon Glass        Returns:
228*f6d34651SSimon Glass            A list of output strings from each command, one element for each
229*f6d34651SSimon Glass            command.
23073a9054dSSimon Glass        """
231*f6d34651SSimon Glass        output = []
23273a9054dSSimon Glass        for cmd in cmds:
233*f6d34651SSimon Glass            output.append(self.run_command(cmd))
23473a9054dSSimon Glass        return output
23573a9054dSSimon Glass
236d201506cSStephen Warren    def ctrlc(self):
237e8debf39SStephen Warren        """Send a CTRL-C character to U-Boot.
238d201506cSStephen Warren
239d201506cSStephen Warren        This is useful in order to stop execution of long-running synchronous
240d201506cSStephen Warren        commands such as "ums".
241d201506cSStephen Warren
242d201506cSStephen Warren        Args:
243d201506cSStephen Warren            None.
244d201506cSStephen Warren
245d201506cSStephen Warren        Returns:
246d201506cSStephen Warren            Nothing.
247e8debf39SStephen Warren        """
248d201506cSStephen Warren
249783cbcd3SStephen Warren        self.log.action('Sending Ctrl-C')
250d201506cSStephen Warren        self.run_command(chr(3), wait_for_echo=False, send_nl=False)
251d201506cSStephen Warren
25276b46939SStephen Warren    def wait_for(self, text):
253e8debf39SStephen Warren        """Wait for a pattern to be emitted by U-Boot.
25476b46939SStephen Warren
25576b46939SStephen Warren        This is useful when a long-running command such as "dfu" is executing,
25676b46939SStephen Warren        and it periodically emits some text that should show up at a specific
25776b46939SStephen Warren        location in the log file.
25876b46939SStephen Warren
25976b46939SStephen Warren        Args:
26076b46939SStephen Warren            text: The text to wait for; either a string (containing raw text,
26176b46939SStephen Warren                not a regular expression) or an re object.
26276b46939SStephen Warren
26376b46939SStephen Warren        Returns:
26476b46939SStephen Warren            Nothing.
265e8debf39SStephen Warren        """
26676b46939SStephen Warren
26776b46939SStephen Warren        if type(text) == type(''):
26876b46939SStephen Warren            text = re.escape(text)
2690c6189b5SStephen Warren        m = self.p.expect([text] + self.bad_patterns)
2700c6189b5SStephen Warren        if m != 0:
2710c6189b5SStephen Warren            raise Exception('Bad pattern found on console: ' +
2720c6189b5SStephen Warren                            self.bad_pattern_ids[m - 1])
27376b46939SStephen Warren
274c10eb9d3SStephen Warren    def drain_console(self):
275e8debf39SStephen Warren        """Read from and log the U-Boot console for a short time.
276c10eb9d3SStephen Warren
277c10eb9d3SStephen Warren        U-Boot's console output is only logged when the test code actively
278c10eb9d3SStephen Warren        waits for U-Boot to emit specific data. There are cases where tests
279c10eb9d3SStephen Warren        can fail without doing this. For example, if a test asks U-Boot to
280c10eb9d3SStephen Warren        enable USB device mode, then polls until a host-side device node
281c10eb9d3SStephen Warren        exists. In such a case, it is useful to log U-Boot's console output
282c10eb9d3SStephen Warren        in case U-Boot printed clues as to why the host-side even did not
283c10eb9d3SStephen Warren        occur. This function will do that.
284c10eb9d3SStephen Warren
285c10eb9d3SStephen Warren        Args:
286c10eb9d3SStephen Warren            None.
287c10eb9d3SStephen Warren
288c10eb9d3SStephen Warren        Returns:
289c10eb9d3SStephen Warren            Nothing.
290e8debf39SStephen Warren        """
291c10eb9d3SStephen Warren
292c10eb9d3SStephen Warren        # If we are already not connected to U-Boot, there's nothing to drain.
293c10eb9d3SStephen Warren        # This should only happen when a previous call to run_command() or
294c10eb9d3SStephen Warren        # wait_for() failed (and hence the output has already been logged), or
295c10eb9d3SStephen Warren        # the system is shutting down.
296c10eb9d3SStephen Warren        if not self.p:
297c10eb9d3SStephen Warren            return
298c10eb9d3SStephen Warren
299c10eb9d3SStephen Warren        orig_timeout = self.p.timeout
300c10eb9d3SStephen Warren        try:
301c10eb9d3SStephen Warren            # Drain the log for a relatively short time.
302c10eb9d3SStephen Warren            self.p.timeout = 1000
303c10eb9d3SStephen Warren            # Wait for something U-Boot will likely never send. This will
304c10eb9d3SStephen Warren            # cause the console output to be read and logged.
305c10eb9d3SStephen Warren            self.p.expect(['This should never match U-Boot output'])
306c10eb9d3SStephen Warren        except u_boot_spawn.Timeout:
307c10eb9d3SStephen Warren            pass
308c10eb9d3SStephen Warren        finally:
309c10eb9d3SStephen Warren            self.p.timeout = orig_timeout
310c10eb9d3SStephen Warren
311d201506cSStephen Warren    def ensure_spawned(self):
312e8debf39SStephen Warren        """Ensure a connection to a correctly running U-Boot instance.
313d201506cSStephen Warren
314d201506cSStephen Warren        This may require spawning a new Sandbox process or resetting target
315d201506cSStephen Warren        hardware, as defined by the implementation sub-class.
316d201506cSStephen Warren
317d201506cSStephen Warren        This is an internal function and should not be called directly.
318d201506cSStephen Warren
319d201506cSStephen Warren        Args:
320d201506cSStephen Warren            None.
321d201506cSStephen Warren
322d201506cSStephen Warren        Returns:
323d201506cSStephen Warren            Nothing.
324e8debf39SStephen Warren        """
325d201506cSStephen Warren
326d201506cSStephen Warren        if self.p:
327d201506cSStephen Warren            return
328d201506cSStephen Warren        try:
32997255438SStephen Warren            self.log.start_section('Starting U-Boot')
330d201506cSStephen Warren            self.at_prompt = False
331d201506cSStephen Warren            self.p = self.get_spawn()
332d201506cSStephen Warren            # Real targets can take a long time to scroll large amounts of
333d201506cSStephen Warren            # text if LCD is enabled. This value may need tweaking in the
334d201506cSStephen Warren            # future, possibly per-test to be optimal. This works for 'help'
335d201506cSStephen Warren            # on board 'seaboard'.
33689ab8410SStephen Warren            if not self.config.gdbserver:
337d201506cSStephen Warren                self.p.timeout = 30000
338d201506cSStephen Warren            self.p.logfile_read = self.logstream
339b1309a23SHeiko Schocher            bcfg = self.config.buildconfig
340b1309a23SHeiko Schocher            config_spl = bcfg.get('config_spl', 'n') == 'y'
341b1309a23SHeiko Schocher            config_spl_serial_support = bcfg.get('config_spl_serial_support',
342b1309a23SHeiko Schocher                                                 'n') == 'y'
343299e5bb7SMichal Simek            env_spl_skipped = self.config.env.get('env__spl_skipped',
344299e5bb7SMichal Simek                                                  False)
345299e5bb7SMichal Simek            if config_spl and config_spl_serial_support and not env_spl_skipped:
346b1309a23SHeiko Schocher                m = self.p.expect([pattern_u_boot_spl_signon] +
347b1309a23SHeiko Schocher                                  self.bad_patterns)
3480c6189b5SStephen Warren                if m != 0:
349c7f636f5SSimon Glass                    raise Exception('Bad pattern found on SPL console: ' +
3500c6189b5SStephen Warren                                    self.bad_pattern_ids[m - 1])
3510c6189b5SStephen Warren            m = self.p.expect([pattern_u_boot_main_signon] + self.bad_patterns)
3520c6189b5SStephen Warren            if m != 0:
3530c6189b5SStephen Warren                raise Exception('Bad pattern found on console: ' +
3540c6189b5SStephen Warren                                self.bad_pattern_ids[m - 1])
355c82ce04aSStephen Warren            self.u_boot_version_string = self.p.after
356d201506cSStephen Warren            while True:
3570c6189b5SStephen Warren                m = self.p.expect([self.prompt_escaped,
3580c6189b5SStephen Warren                    pattern_stop_autoboot_prompt] + self.bad_patterns)
3590c6189b5SStephen Warren                if m == 0:
3600c6189b5SStephen Warren                    break
3610c6189b5SStephen Warren                if m == 1:
36238831ca3SStephen Warren                    self.p.send(' ')
363d201506cSStephen Warren                    continue
3640c6189b5SStephen Warren                raise Exception('Bad pattern found on console: ' +
3650c6189b5SStephen Warren                                self.bad_pattern_ids[m - 2])
366d201506cSStephen Warren            self.at_prompt = True
367d201506cSStephen Warren            self.at_prompt_logevt = self.logstream.logfile.cur_evt
368d201506cSStephen Warren        except Exception as ex:
369d201506cSStephen Warren            self.log.error(str(ex))
370d201506cSStephen Warren            self.cleanup_spawn()
371d201506cSStephen Warren            raise
37297255438SStephen Warren        finally:
37397255438SStephen Warren            self.log.end_section('Starting U-Boot')
374d201506cSStephen Warren
375d201506cSStephen Warren    def cleanup_spawn(self):
376e8debf39SStephen Warren        """Shut down all interaction with the U-Boot instance.
377d201506cSStephen Warren
378d201506cSStephen Warren        This is used when an error is detected prior to re-establishing a
379d201506cSStephen Warren        connection with a fresh U-Boot instance.
380d201506cSStephen Warren
381d201506cSStephen Warren        This is an internal function and should not be called directly.
382d201506cSStephen Warren
383d201506cSStephen Warren        Args:
384d201506cSStephen Warren            None.
385d201506cSStephen Warren
386d201506cSStephen Warren        Returns:
387d201506cSStephen Warren            Nothing.
388e8debf39SStephen Warren        """
389d201506cSStephen Warren
390d201506cSStephen Warren        try:
391d201506cSStephen Warren            if self.p:
392d201506cSStephen Warren                self.p.close()
393d201506cSStephen Warren        except:
394d201506cSStephen Warren            pass
395d201506cSStephen Warren        self.p = None
396d201506cSStephen Warren
39727c087d5SSimon Glass    def restart_uboot(self):
39827c087d5SSimon Glass        """Shut down and restart U-Boot."""
39927c087d5SSimon Glass        self.cleanup_spawn()
40027c087d5SSimon Glass        self.ensure_spawned()
40127c087d5SSimon Glass
402ebec58fbSSimon Glass    def get_spawn_output(self):
403ebec58fbSSimon Glass        """Return the start-up output from U-Boot
404ebec58fbSSimon Glass
405ebec58fbSSimon Glass        Returns:
406ebec58fbSSimon Glass            The output produced by ensure_spawed(), as a string.
407ebec58fbSSimon Glass        """
408ebec58fbSSimon Glass        if self.p:
409ebec58fbSSimon Glass            return self.p.get_expect_output()
410ebec58fbSSimon Glass        return None
411ebec58fbSSimon Glass
412d201506cSStephen Warren    def validate_version_string_in_text(self, text):
413e8debf39SStephen Warren        """Assert that a command's output includes the U-Boot signon message.
414d201506cSStephen Warren
415d201506cSStephen Warren        This is primarily useful for validating the "version" command without
416d201506cSStephen Warren        duplicating the signon text regex in a test function.
417d201506cSStephen Warren
418d201506cSStephen Warren        Args:
419d201506cSStephen Warren            text: The command output text to check.
420d201506cSStephen Warren
421d201506cSStephen Warren        Returns:
422d201506cSStephen Warren            Nothing. An exception is raised if the validation fails.
423e8debf39SStephen Warren        """
424d201506cSStephen Warren
425d201506cSStephen Warren        assert(self.u_boot_version_string in text)
426d201506cSStephen Warren
427d201506cSStephen Warren    def disable_check(self, check_type):
428e8debf39SStephen Warren        """Temporarily disable an error check of U-Boot's output.
429d201506cSStephen Warren
430d201506cSStephen Warren        Create a new context manager (for use with the "with" statement) which
431d201506cSStephen Warren        temporarily disables a particular console output error check.
432d201506cSStephen Warren
433d201506cSStephen Warren        Args:
434d201506cSStephen Warren            check_type: The type of error-check to disable. Valid values may
435d201506cSStephen Warren            be found in self.disable_check_count above.
436d201506cSStephen Warren
437d201506cSStephen Warren        Returns:
438d201506cSStephen Warren            A context manager object.
439e8debf39SStephen Warren        """
440d201506cSStephen Warren
441d201506cSStephen Warren        return ConsoleDisableCheck(self, check_type)
44287861c19SMichal Simek
44387861c19SMichal Simek    def temporary_timeout(self, timeout):
44487861c19SMichal Simek        """Temporarily set up different timeout for commands.
44587861c19SMichal Simek
44687861c19SMichal Simek        Create a new context manager (for use with the "with" statement) which
44787861c19SMichal Simek        temporarily change timeout.
44887861c19SMichal Simek
44987861c19SMichal Simek        Args:
45087861c19SMichal Simek            timeout: Time in milliseconds.
45187861c19SMichal Simek
45287861c19SMichal Simek        Returns:
45387861c19SMichal Simek            A context manager object.
45487861c19SMichal Simek        """
45587861c19SMichal Simek
45687861c19SMichal Simek        return ConsoleSetupTimeout(self, timeout)
457