Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 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 | |
| 8 | import time |
| 9 | from u_boot_spawn import Spawn |
| 10 | from u_boot_console_base import ConsoleBase |
| 11 | |
| 12 | class ConsoleSandbox(ConsoleBase): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 13 | """Represents a connection to a sandbox U-Boot console, executed as a sub- |
| 14 | process.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 15 | |
| 16 | def __init__(self, log, config): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 17 | """Initialize a U-Boot console connection. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 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. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 25 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 26 | |
| 27 | super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024) |
| 28 | |
| 29 | def get_spawn(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 30 | """Connect to a fresh U-Boot instance. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 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. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 41 | |
Stephen Warren | b8936ae | 2016-01-27 23:57:52 -0700 | [diff] [blame] | 42 | cmd = [ |
| 43 | self.config.build_dir + '/u-boot', |
| 44 | '-d', |
| 45 | self.config.build_dir + '/arch/sandbox/dts/test.dtb' |
| 46 | ] |
Stephen Warren | a85fce9 | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 47 | return Spawn(cmd, cwd=self.config.source_dir) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 48 | |
| 49 | def kill(self, sig): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 50 | """Send a specific Unix signal to the sandbox process. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 51 | |
| 52 | Args: |
| 53 | sig: The Unix signal to send to the process. |
| 54 | |
| 55 | Returns: |
| 56 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 57 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 58 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 59 | self.log.action('kill %d' % sig) |
| 60 | self.p.kill(sig) |
| 61 | |
| 62 | def validate_exited(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """Determine whether the sandbox process has exited. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 64 | |
| 65 | If required, this function waits a reasonable time for the process to |
| 66 | exit. |
| 67 | |
| 68 | Args: |
| 69 | None. |
| 70 | |
| 71 | Returns: |
| 72 | Boolean indicating whether the process has exited. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 73 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 74 | |
| 75 | p = self.p |
| 76 | self.p = None |
| 77 | for i in xrange(100): |
| 78 | ret = not p.isalive() |
| 79 | if ret: |
| 80 | break |
| 81 | time.sleep(0.1) |
| 82 | p.close() |
| 83 | return ret |