emulator.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import pexpect
  2. import infra
  3. class Emulator(object):
  4. def __init__(self, builddir, downloaddir, logtofile):
  5. self.qemu = None
  6. self.downloaddir = downloaddir
  7. self.logfile = infra.open_log_file(builddir, "run", logtofile)
  8. # Start Qemu to boot the system
  9. #
  10. # arch: Qemu architecture to use
  11. #
  12. # kernel: path to the kernel image, or the special string
  13. # 'builtin'. 'builtin' means a pre-built kernel image will be
  14. # downloaded from ARTEFACTS_URL and suitable options are
  15. # automatically passed to qemu and added to the kernel cmdline. So
  16. # far only armv5, armv7 and i386 builtin kernels are available.
  17. # If None, then no kernel is used, and we assume a bootable device
  18. # will be specified.
  19. #
  20. # kernel_cmdline: array of kernel arguments to pass to Qemu -append option
  21. #
  22. # options: array of command line options to pass to Qemu
  23. #
  24. def boot(self, arch, kernel=None, kernel_cmdline=None, options=None):
  25. if arch in ["armv7", "armv5"]:
  26. qemu_arch = "arm"
  27. else:
  28. qemu_arch = arch
  29. qemu_cmd = ["qemu-system-{}".format(qemu_arch),
  30. "-serial", "stdio",
  31. "-display", "none"]
  32. if options:
  33. qemu_cmd += options
  34. if kernel_cmdline is None:
  35. kernel_cmdline = []
  36. if kernel:
  37. if kernel == "builtin":
  38. if arch in ["armv7", "armv5"]:
  39. kernel_cmdline.append("console=ttyAMA0")
  40. if arch == "armv7":
  41. kernel = infra.download(self.downloaddir,
  42. "kernel-vexpress")
  43. dtb = infra.download(self.downloaddir,
  44. "vexpress-v2p-ca9.dtb")
  45. qemu_cmd += ["-dtb", dtb]
  46. qemu_cmd += ["-M", "vexpress-a9"]
  47. elif arch == "armv5":
  48. kernel = infra.download(self.downloaddir,
  49. "kernel-versatile")
  50. qemu_cmd += ["-M", "versatilepb"]
  51. qemu_cmd += ["-kernel", kernel]
  52. if kernel_cmdline:
  53. qemu_cmd += ["-append", " ".join(kernel_cmdline)]
  54. self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
  55. self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:], timeout=5,
  56. env={"QEMU_AUDIO_DRV": "none"})
  57. # We want only stdout into the log to avoid double echo
  58. self.qemu.logfile_read = self.logfile
  59. # Wait for the login prompt to appear, and then login as root with
  60. # the provided password, or no password if not specified.
  61. def login(self, password=None):
  62. # The login prompt can take some time to appear when running multiple
  63. # instances in parallel, so set the timeout to a large value
  64. index = self.qemu.expect(["buildroot login:", pexpect.TIMEOUT],
  65. timeout=60)
  66. if index != 0:
  67. self.logfile.write("==> System does not boot")
  68. raise SystemError("System does not boot")
  69. self.qemu.sendline("root")
  70. if password:
  71. self.qemu.expect("Password:")
  72. self.qemu.sendline(password)
  73. index = self.qemu.expect(["# ", pexpect.TIMEOUT])
  74. if index != 0:
  75. raise SystemError("Cannot login")
  76. self.run("dmesg -n 1")
  77. # Run the given 'cmd' with a 'timeout' on the target
  78. # return a tuple (output, exit_code)
  79. def run(self, cmd, timeout=-1):
  80. self.qemu.sendline(cmd)
  81. self.qemu.expect("# ", timeout=timeout)
  82. # Remove double carriage return from qemu stdout so str.splitlines()
  83. # works as expected.
  84. output = self.qemu.before.replace("\r\r", "\r").splitlines()[1:]
  85. self.qemu.sendline("echo $?")
  86. self.qemu.expect("# ")
  87. exit_code = self.qemu.before.splitlines()[2]
  88. exit_code = int(exit_code)
  89. return output, exit_code
  90. def stop(self):
  91. if self.qemu is None:
  92. return
  93. self.qemu.terminate(force=True)