xref: /rk3399_rockchip-uboot/test/py/u_boot_console_exec_attach.py (revision 93134e18e8772ad87a3c94d5d64970659835c944)
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# Logic to interact with U-Boot running on real hardware, typically via a
7d201506cSStephen Warren# physical serial port.
8d201506cSStephen Warren
9d201506cSStephen Warrenimport sys
10d201506cSStephen Warrenfrom u_boot_spawn import Spawn
11d201506cSStephen Warrenfrom u_boot_console_base import ConsoleBase
12d201506cSStephen Warren
13d201506cSStephen Warrenclass ConsoleExecAttach(ConsoleBase):
14e8debf39SStephen Warren    """Represents a physical connection to a U-Boot console, typically via a
15d201506cSStephen Warren    serial port. This implementation executes a sub-process to attach to the
16d201506cSStephen Warren    console, expecting that the stdin/out of the sub-process will be forwarded
17d201506cSStephen Warren    to/from the physical hardware. This approach isolates the test infra-
18d201506cSStephen Warren    structure from the user-/installation-specific details of how to
19e8debf39SStephen Warren    communicate with, and the identity of, serial ports etc."""
20d201506cSStephen Warren
21d201506cSStephen Warren    def __init__(self, log, config):
22e8debf39SStephen Warren        """Initialize a U-Boot console connection.
23d201506cSStephen Warren
24d201506cSStephen Warren        Args:
25d201506cSStephen Warren            log: A multiplexed_log.Logfile instance.
26d201506cSStephen Warren            config: A "configuration" object as defined in conftest.py.
27d201506cSStephen Warren
28d201506cSStephen Warren        Returns:
29d201506cSStephen Warren            Nothing.
30e8debf39SStephen Warren        """
31d201506cSStephen Warren
32d201506cSStephen Warren        # The max_fifo_fill value might need tweaking per-board/-SoC?
33d201506cSStephen Warren        # 1 would be safe anywhere, but is very slow (a pexpect issue?).
34d201506cSStephen Warren        # 16 is a common FIFO size.
35d201506cSStephen Warren        # HW flow control would mean this could be infinite.
36d201506cSStephen Warren        super(ConsoleExecAttach, self).__init__(log, config, max_fifo_fill=16)
37d201506cSStephen Warren
3883357fd5SStephen Warren        with self.log.section('flash'):
39d201506cSStephen Warren            self.log.action('Flashing U-Boot')
40d201506cSStephen Warren            cmd = ['u-boot-test-flash', config.board_type, config.board_identity]
41d201506cSStephen Warren            runner = self.log.get_runner(cmd[0], sys.stdout)
42d201506cSStephen Warren            runner.run(cmd)
43d201506cSStephen Warren            runner.close()
4483357fd5SStephen Warren            self.log.status_pass('OK')
45d201506cSStephen Warren
46d201506cSStephen Warren    def get_spawn(self):
47e8debf39SStephen Warren        """Connect to a fresh U-Boot instance.
48d201506cSStephen Warren
49d201506cSStephen Warren        The target board is reset, so that U-Boot begins running from scratch.
50d201506cSStephen Warren
51d201506cSStephen Warren        Args:
52d201506cSStephen Warren            None.
53d201506cSStephen Warren
54d201506cSStephen Warren        Returns:
55d201506cSStephen Warren            A u_boot_spawn.Spawn object that is attached to U-Boot.
56e8debf39SStephen Warren        """
57d201506cSStephen Warren
58d201506cSStephen Warren        args = [self.config.board_type, self.config.board_identity]
59d201506cSStephen Warren        s = Spawn(['u-boot-test-console'] + args)
60d201506cSStephen Warren
61*93134e18SStephen Warren        try:
62d201506cSStephen Warren            self.log.action('Resetting board')
63d201506cSStephen Warren            cmd = ['u-boot-test-reset'] + args
64d201506cSStephen Warren            runner = self.log.get_runner(cmd[0], sys.stdout)
65d201506cSStephen Warren            runner.run(cmd)
66d201506cSStephen Warren            runner.close()
67*93134e18SStephen Warren        except:
68*93134e18SStephen Warren            s.close()
69*93134e18SStephen Warren            raise
70d201506cSStephen Warren
71d201506cSStephen Warren        return s
72