emulator.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. def __read_until(self, waitstr, timeout=5):
  59. index = self.qemu.expect([waitstr, pexpect.TIMEOUT], timeout=timeout)
  60. data = self.qemu.before
  61. if index == 0:
  62. data += self.qemu.after
  63. self.log += data
  64. self.logfile.write(data)
  65. # Remove double carriage return from qemu stdout so str.splitlines()
  66. # works as expected.
  67. return data.replace("\r\r", "\r")
  68. def __write(self, wstr):
  69. self.qemu.send(wstr)
  70. # Wait for the login prompt to appear, and then login as root with
  71. # the provided password, or no password if not specified.
  72. def login(self, password=None):
  73. self.__read_until("buildroot login:", 10)
  74. if "buildroot login:" not in self.log:
  75. self.logfile.write("==> System does not boot")
  76. raise SystemError("System does not boot")
  77. self.__write("root\n")
  78. if password:
  79. self.__read_until("Password:")
  80. self.__write(password + "\n")
  81. self.__read_until("# ")
  82. if "# " not in self.log:
  83. raise SystemError("Cannot login")
  84. self.run("dmesg -n 1")
  85. # Run the given 'cmd' on the target
  86. # return a tuple (output, exit_code)
  87. def run(self, cmd):
  88. self.__write(cmd + "\n")
  89. output = self.__read_until("# ")
  90. output = output.strip().splitlines()
  91. output = output[1:len(output)-1]
  92. self.__write("echo $?\n")
  93. exit_code = self.__read_until("# ")
  94. exit_code = exit_code.strip().splitlines()[1]
  95. exit_code = int(exit_code)
  96. return output, exit_code
  97. def stop(self):
  98. if self.qemu is None:
  99. return
  100. self.qemu.terminate(force=True)