1# Copyright (c) 2015 Stephen Warren
2# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
3#
4# SPDX-License-Identifier: GPL-2.0
5
6# Logic to interact with the sandbox port of U-Boot, running as a sub-process.
7
8import time
9from u_boot_spawn import Spawn
10from u_boot_console_base import ConsoleBase
11
12class ConsoleSandbox(ConsoleBase):
13    """Represents a connection to a sandbox U-Boot console, executed as a sub-
14    process."""
15
16    def __init__(self, log, config):
17        """Initialize a U-Boot console connection.
18
19        Args:
20            log: A multiplexed_log.Logfile instance.
21            config: A "configuration" object as defined in conftest.py.
22
23        Returns:
24            Nothing.
25        """
26
27        super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024)
28
29    def get_spawn(self):
30        """Connect to a fresh U-Boot instance.
31
32        A new sandbox process is created, so that U-Boot begins running from
33        scratch.
34
35        Args:
36            None.
37
38        Returns:
39            A u_boot_spawn.Spawn object that is attached to U-Boot.
40        """
41
42        cmd = []
43        if self.config.gdbserver:
44            cmd += ['gdbserver', self.config.gdbserver]
45        cmd += [
46            self.config.build_dir + '/u-boot',
47            '-v',
48            '-d',
49            self.config.build_dir + '/arch/sandbox/dts/test.dtb'
50        ]
51        return Spawn(cmd, cwd=self.config.source_dir)
52
53    def kill(self, sig):
54        """Send a specific Unix signal to the sandbox process.
55
56        Args:
57            sig: The Unix signal to send to the process.
58
59        Returns:
60            Nothing.
61        """
62
63        self.log.action('kill %d' % sig)
64        self.p.kill(sig)
65
66    def validate_exited(self):
67        """Determine whether the sandbox process has exited.
68
69        If required, this function waits a reasonable time for the process to
70        exit.
71
72        Args:
73            None.
74
75        Returns:
76            Boolean indicating whether the process has exited.
77        """
78
79        p = self.p
80        self.p = None
81        for i in xrange(100):
82            ret = not p.isalive()
83            if ret:
84                break
85            time.sleep(0.1)
86        p.close()
87        return ret
88