blob: ce4ca7e55ef781caffc290ec8961ca5f6c17d708 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0
Stephen Warren10e50632016-01-15 11:15:24 -07002# Copyright (c) 2015 Stephen Warren
3# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warren10e50632016-01-15 11:15:24 -07004
Heinrich Schuchardtd209bab2021-11-23 00:01:48 +01005"""
6Logic to interact with the sandbox port of U-Boot, running as a sub-process.
7"""
Stephen Warren10e50632016-01-15 11:15:24 -07008
9import time
10from u_boot_spawn import Spawn
11from u_boot_console_base import ConsoleBase
12
13class ConsoleSandbox(ConsoleBase):
Stephen Warren75e731e2016-01-26 13:41:30 -070014 """Represents a connection to a sandbox U-Boot console, executed as a sub-
15 process."""
Stephen Warren10e50632016-01-15 11:15:24 -070016
17 def __init__(self, log, config):
Stephen Warren75e731e2016-01-26 13:41:30 -070018 """Initialize a U-Boot console connection.
Stephen Warren10e50632016-01-15 11:15:24 -070019
20 Args:
21 log: A multiplexed_log.Logfile instance.
22 config: A "configuration" object as defined in conftest.py.
23
24 Returns:
25 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070026 """
Stephen Warren10e50632016-01-15 11:15:24 -070027
28 super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024)
Simon Glass8125f2c2018-11-15 18:44:00 -070029 self.sandbox_flags = []
Stephen Warren10e50632016-01-15 11:15:24 -070030
31 def get_spawn(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070032 """Connect to a fresh U-Boot instance.
Stephen Warren10e50632016-01-15 11:15:24 -070033
34 A new sandbox process is created, so that U-Boot begins running from
35 scratch.
36
37 Args:
38 None.
39
40 Returns:
41 A u_boot_spawn.Spawn object that is attached to U-Boot.
Stephen Warren75e731e2016-01-26 13:41:30 -070042 """
Stephen Warren10e50632016-01-15 11:15:24 -070043
Simon Glass8cfa74e2016-07-04 11:58:40 -060044 bcfg = self.config.buildconfig
45 config_spl = bcfg.get('config_spl', 'n') == 'y'
46 fname = '/spl/u-boot-spl' if config_spl else '/u-boot'
Paul Burton00f2d202017-09-14 14:34:43 -070047 print(fname)
Stephen Warren33db1ee2016-02-04 16:11:50 -070048 cmd = []
49 if self.config.gdbserver:
50 cmd += ['gdbserver', self.config.gdbserver]
51 cmd += [
Simon Glass8cfa74e2016-07-04 11:58:40 -060052 self.config.build_dir + fname,
Stephen Warren84227032016-04-04 11:04:50 -060053 '-v',
Stephen Warrenb8936ae2016-01-27 23:57:52 -070054 '-d',
Simon Glass3b097872016-07-03 09:40:36 -060055 self.config.dtb
Stephen Warrenb8936ae2016-01-27 23:57:52 -070056 ]
Simon Glass8125f2c2018-11-15 18:44:00 -070057 cmd += self.sandbox_flags
Stephen Warrena85fce92016-01-27 23:57:53 -070058 return Spawn(cmd, cwd=self.config.source_dir)
Stephen Warren10e50632016-01-15 11:15:24 -070059
Masami Hiramatsu4d3b9962022-02-16 15:16:02 +090060 def restart_uboot_with_flags(self, flags, expect_reset=False):
Simon Glass8125f2c2018-11-15 18:44:00 -070061 """Run U-Boot with the given command-line flags
62
63 Args:
64 flags: List of flags to pass, each a string
Masami Hiramatsu4d3b9962022-02-16 15:16:02 +090065 expect_reset: Boolean indication whether this boot is expected
66 to be reset while the 1st boot process after main boot before
67 prompt. False by default.
Simon Glass8125f2c2018-11-15 18:44:00 -070068
69 Returns:
70 A u_boot_spawn.Spawn object that is attached to U-Boot.
71 """
72
73 try:
74 self.sandbox_flags = flags
Masami Hiramatsu4d3b9962022-02-16 15:16:02 +090075 return self.restart_uboot(expect_reset)
Simon Glass8125f2c2018-11-15 18:44:00 -070076 finally:
77 self.sandbox_flags = []
78
Stephen Warren10e50632016-01-15 11:15:24 -070079 def kill(self, sig):
Stephen Warren75e731e2016-01-26 13:41:30 -070080 """Send a specific Unix signal to the sandbox process.
Stephen Warren10e50632016-01-15 11:15:24 -070081
82 Args:
83 sig: The Unix signal to send to the process.
84
85 Returns:
86 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070087 """
Stephen Warren10e50632016-01-15 11:15:24 -070088
Stephen Warren10e50632016-01-15 11:15:24 -070089 self.log.action('kill %d' % sig)
90 self.p.kill(sig)
91
92 def validate_exited(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070093 """Determine whether the sandbox process has exited.
Stephen Warren10e50632016-01-15 11:15:24 -070094
95 If required, this function waits a reasonable time for the process to
96 exit.
97
98 Args:
99 None.
100
101 Returns:
102 Boolean indicating whether the process has exited.
Stephen Warren75e731e2016-01-26 13:41:30 -0700103 """
Stephen Warren10e50632016-01-15 11:15:24 -0700104
105 p = self.p
106 self.p = None
Paul Burtond2849ed2017-09-14 14:34:44 -0700107 for i in range(100):
Stephen Warren10e50632016-01-15 11:15:24 -0700108 ret = not p.isalive()
109 if ret:
110 break
111 time.sleep(0.1)
112 p.close()
113 return ret