emulator.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import socket
  2. import subprocess
  3. import telnetlib
  4. import infra
  5. import infra.basetest
  6. # TODO: Most of the telnet stuff need to be replaced by stdio/pexpect to discuss
  7. # with the qemu machine.
  8. class Emulator(object):
  9. def __init__(self, builddir, downloaddir, logtofile):
  10. self.qemu = None
  11. self.__tn = None
  12. self.downloaddir = downloaddir
  13. self.log = ""
  14. self.logfile = infra.open_log_file(builddir, "run", logtofile)
  15. # Start Qemu to boot the system
  16. #
  17. # arch: Qemu architecture to use
  18. #
  19. # kernel: path to the kernel image, or the special string
  20. # 'builtin'. 'builtin' means a pre-built kernel image will be
  21. # downloaded from ARTEFACTS_URL and suitable options are
  22. # automatically passed to qemu and added to the kernel cmdline. So
  23. # far only armv5, armv7 and i386 builtin kernels are available.
  24. # If None, then no kernel is used, and we assume a bootable device
  25. # will be specified.
  26. #
  27. # kernel_cmdline: array of kernel arguments to pass to Qemu -append option
  28. #
  29. # options: array of command line options to pass to Qemu
  30. #
  31. def boot(self, arch, kernel=None, kernel_cmdline=None, options=None):
  32. if arch in ["armv7", "armv5"]:
  33. qemu_arch = "arm"
  34. else:
  35. qemu_arch = arch
  36. qemu_cmd = ["qemu-system-{}".format(qemu_arch),
  37. "-serial", "telnet::1234,server",
  38. "-display", "none"]
  39. if options:
  40. qemu_cmd += options
  41. if kernel_cmdline is None:
  42. kernel_cmdline = []
  43. if kernel:
  44. if kernel == "builtin":
  45. if arch in ["armv7", "armv5"]:
  46. kernel_cmdline.append("console=ttyAMA0")
  47. if arch == "armv7":
  48. kernel = infra.download(self.downloaddir,
  49. "kernel-vexpress")
  50. dtb = infra.download(self.downloaddir,
  51. "vexpress-v2p-ca9.dtb")
  52. qemu_cmd += ["-dtb", dtb]
  53. qemu_cmd += ["-M", "vexpress-a9"]
  54. elif arch == "armv5":
  55. kernel = infra.download(self.downloaddir,
  56. "kernel-versatile")
  57. qemu_cmd += ["-M", "versatilepb"]
  58. qemu_cmd += ["-kernel", kernel]
  59. if kernel_cmdline:
  60. qemu_cmd += ["-append", " ".join(kernel_cmdline)]
  61. self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
  62. self.qemu = subprocess.Popen(qemu_cmd, stdout=self.logfile, stderr=self.logfile)
  63. # Wait for the telnet port to appear and connect to it.
  64. while True:
  65. try:
  66. self.__tn = telnetlib.Telnet("localhost", 1234)
  67. if self.__tn:
  68. break
  69. except socket.error:
  70. continue
  71. def __read_until(self, waitstr, timeout=5):
  72. data = self.__tn.read_until(waitstr, timeout)
  73. self.log += data
  74. self.logfile.write(data)
  75. return data
  76. def __write(self, wstr):
  77. self.__tn.write(wstr)
  78. # Wait for the login prompt to appear, and then login as root with
  79. # the provided password, or no password if not specified.
  80. def login(self, password=None):
  81. self.__read_until("buildroot login:", 10)
  82. if "buildroot login:" not in self.log:
  83. self.logfile.write("==> System does not boot")
  84. raise SystemError("System does not boot")
  85. self.__write("root\n")
  86. if password:
  87. self.__read_until("Password:")
  88. self.__write(password + "\n")
  89. self.__read_until("# ")
  90. if "# " not in self.log:
  91. raise SystemError("Cannot login")
  92. self.run("dmesg -n 1")
  93. # Run the given 'cmd' on the target
  94. # return a tuple (output, exit_code)
  95. def run(self, cmd):
  96. self.__write(cmd + "\n")
  97. output = self.__read_until("# ")
  98. output = output.strip().splitlines()
  99. output = output[1:len(output)-1]
  100. self.__write("echo $?\n")
  101. exit_code = self.__read_until("# ")
  102. exit_code = exit_code.strip().splitlines()[1]
  103. exit_code = int(exit_code)
  104. return output, exit_code
  105. def stop(self):
  106. if self.qemu is None:
  107. return
  108. self.qemu.terminate()
  109. self.qemu.kill()