2
1

emulator.py 4.5 KB

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