emulator.py 4.1 KB

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