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