6 class Emulator(object):
8 def __init__(self, builddir, downloaddir, logtofile):
10 self.downloaddir = downloaddir
11 self.logfile = infra.open_log_file(builddir, "run", logtofile)
13 # Start Qemu to boot the system
15 # arch: Qemu architecture to use
17 # kernel: path to the kernel image, or the special string
18 # 'builtin'. 'builtin' means a pre-built kernel image will be
19 # downloaded from ARTEFACTS_URL and suitable options are
20 # automatically passed to qemu and added to the kernel cmdline. So
21 # far only armv5, armv7 and i386 builtin kernels are available.
22 # If None, then no kernel is used, and we assume a bootable device
25 # kernel_cmdline: array of kernel arguments to pass to Qemu -append option
27 # options: array of command line options to pass to Qemu
29 def boot(self, arch, kernel=None, kernel_cmdline=None, options=None):
30 if arch in ["armv7", "armv5"]:
35 qemu_cmd = ["qemu-system-{}".format(qemu_arch),
42 if kernel_cmdline is None:
46 if kernel == "builtin":
47 if arch in ["armv7", "armv5"]:
48 kernel_cmdline.append("console=ttyAMA0")
51 kernel = infra.download(self.downloaddir,
53 dtb = infra.download(self.downloaddir,
54 "vexpress-v2p-ca9.dtb")
55 qemu_cmd += ["-dtb", dtb]
56 qemu_cmd += ["-M", "vexpress-a9"]
58 kernel = infra.download(self.downloaddir,
60 qemu_cmd += ["-M", "versatilepb"]
62 qemu_cmd += ["-kernel", kernel]
65 qemu_cmd += ["-append", " ".join(kernel_cmdline)]
67 self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
68 self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:], timeout=5)
69 # We want only stdout into the log to avoid double echo
70 self.qemu.logfile_read = self.logfile
72 # Wait for the login prompt to appear, and then login as root with
73 # the provided password, or no password if not specified.
74 def login(self, password=None):
75 index = self.qemu.expect(["buildroot login:", pexpect.TIMEOUT],
78 self.logfile.write("==> System does not boot")
79 raise SystemError("System does not boot")
81 self.qemu.sendline("root")
83 self.qemu.expect("Password:")
84 self.qemu.sendline(password)
85 index = self.qemu.expect(["# ", pexpect.TIMEOUT])
87 raise SystemError("Cannot login")
88 self.run("dmesg -n 1")
90 # Run the given 'cmd' on the target
91 # return a tuple (output, exit_code)
93 self.qemu.sendline(cmd)
94 self.qemu.expect("# ")
95 # Remove double carriage return from qemu stdout so str.splitlines()
97 output = self.qemu.before.replace("\r\r", "\r").splitlines()[1:]
99 self.qemu.sendline("echo $?")
100 self.qemu.expect("# ")
101 exit_code = self.qemu.before.splitlines()[2]
102 exit_code = int(exit_code)
104 return output, exit_code
107 if self.qemu is None:
109 self.qemu.terminate(force=True)