emulator.py 3.8 KB

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