Merge branch 'master' of git://git.denx.de/u-boot-spi
[platform/kernel/u-boot.git] / test / py / u_boot_console_sandbox.py
1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2015 Stephen Warren
3 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4
5 # Logic to interact with the sandbox port of U-Boot, running as a sub-process.
6
7 import time
8 from u_boot_spawn import Spawn
9 from u_boot_console_base import ConsoleBase
10
11 class ConsoleSandbox(ConsoleBase):
12     """Represents a connection to a sandbox U-Boot console, executed as a sub-
13     process."""
14
15     def __init__(self, log, config):
16         """Initialize a U-Boot console connection.
17
18         Args:
19             log: A multiplexed_log.Logfile instance.
20             config: A "configuration" object as defined in conftest.py.
21
22         Returns:
23             Nothing.
24         """
25
26         super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024)
27         self.sandbox_flags = []
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         bcfg = self.config.buildconfig
43         config_spl = bcfg.get('config_spl', 'n') == 'y'
44         fname = '/spl/u-boot-spl' if config_spl else '/u-boot'
45         print(fname)
46         cmd = []
47         if self.config.gdbserver:
48             cmd += ['gdbserver', self.config.gdbserver]
49         cmd += [
50             self.config.build_dir + fname,
51             '-v',
52             '-d',
53             self.config.dtb
54         ]
55         cmd += self.sandbox_flags
56         return Spawn(cmd, cwd=self.config.source_dir)
57
58     def restart_uboot_with_flags(self, flags):
59         """Run U-Boot with the given command-line flags
60
61         Args:
62             flags: List of flags to pass, each a string
63
64         Returns:
65             A u_boot_spawn.Spawn object that is attached to U-Boot.
66         """
67
68         try:
69             self.sandbox_flags = flags
70             return self.restart_uboot()
71         finally:
72             self.sandbox_flags = []
73
74     def kill(self, sig):
75         """Send a specific Unix signal to the sandbox process.
76
77         Args:
78             sig: The Unix signal to send to the process.
79
80         Returns:
81             Nothing.
82         """
83
84         self.log.action('kill %d' % sig)
85         self.p.kill(sig)
86
87     def validate_exited(self):
88         """Determine whether the sandbox process has exited.
89
90         If required, this function waits a reasonable time for the process to
91         exit.
92
93         Args:
94             None.
95
96         Returns:
97             Boolean indicating whether the process has exited.
98         """
99
100         p = self.p
101         self.p = None
102         for i in range(100):
103             ret = not p.isalive()
104             if ret:
105                 break
106             time.sleep(0.1)
107         p.close()
108         return ret