u_boot_console_sandbox.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. class ConsoleSandbox(ConsoleBase):
  11. """Represents a connection to a sandbox U-Boot console, executed as a sub-
  12. process."""
  13. def __init__(self, log, config):
  14. """Initialize a U-Boot console connection.
  15. Args:
  16. log: A multiplexed_log.Logfile instance.
  17. config: A "configuration" object as defined in conftest.py.
  18. Returns:
  19. Nothing.
  20. """
  21. super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024)
  22. self.sandbox_flags = []
  23. self.use_dtb = True
  24. def get_spawn(self):
  25. """Connect to a fresh U-Boot instance.
  26. A new sandbox process is created, so that U-Boot begins running from
  27. scratch.
  28. Args:
  29. None.
  30. Returns:
  31. A u_boot_spawn.Spawn object that is attached to U-Boot.
  32. """
  33. bcfg = self.config.buildconfig
  34. config_spl = bcfg.get('config_spl', 'n') == 'y'
  35. config_vpl = bcfg.get('config_vpl', 'n') == 'y'
  36. if config_vpl:
  37. # Run TPL first, which runs VPL
  38. fname = '/tpl/u-boot-tpl'
  39. else:
  40. fname = '/spl/u-boot-spl' if config_spl else '/u-boot'
  41. print(fname)
  42. cmd = []
  43. if self.config.gdbserver:
  44. cmd += ['gdbserver', self.config.gdbserver]
  45. cmd += [self.config.build_dir + fname, '-v']
  46. if self.use_dtb:
  47. cmd += ['-d', self.config.dtb]
  48. cmd += self.sandbox_flags
  49. return Spawn(cmd, cwd=self.config.source_dir)
  50. def restart_uboot_with_flags(self, flags, expect_reset=False, use_dtb=True):
  51. """Run U-Boot with the given command-line flags
  52. Args:
  53. flags: List of flags to pass, each a string
  54. expect_reset: Boolean indication whether this boot is expected
  55. to be reset while the 1st boot process after main boot before
  56. prompt. False by default.
  57. use_dtb: True to use a device tree file, False to run without one
  58. Returns:
  59. A u_boot_spawn.Spawn object that is attached to U-Boot.
  60. """
  61. try:
  62. self.sandbox_flags = flags
  63. self.use_dtb = use_dtb
  64. return self.restart_uboot(expect_reset)
  65. finally:
  66. self.sandbox_flags = []
  67. self.use_dtb = True
  68. def kill(self, sig):
  69. """Send a specific Unix signal to the sandbox process.
  70. Args:
  71. sig: The Unix signal to send to the process.
  72. Returns:
  73. Nothing.
  74. """
  75. self.log.action('kill %d' % sig)
  76. self.p.kill(sig)
  77. def validate_exited(self):
  78. """Determine whether the sandbox process has exited.
  79. If required, this function waits a reasonable time for the process to
  80. exit.
  81. Args:
  82. None.
  83. Returns:
  84. Boolean indicating whether the process has exited.
  85. """
  86. p = self.p
  87. self.p = None
  88. for i in range(100):
  89. ret = not p.isalive()
  90. if ret:
  91. break
  92. time.sleep(0.1)
  93. p.close()
  94. return ret