b416f133adca382a59378640021c515cddcf9460
[mesa.git] / .gitlab-ci / bare-metal / cros_servo_run.py
1 #!/usr/bin/env python3
2 #
3 # Copyright © 2020 Google LLC
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23
24 import argparse
25 import re
26 from serial_buffer import SerialBuffer
27 import sys
28
29 class CrosServoRun:
30 def __init__(self, cpu, ec):
31 self.ec_ser = SerialBuffer(ec, "artifacts/serial-ec.txt", "R SERIAL-EC> ")
32 self.cpu_ser = SerialBuffer(cpu, "artifacts/serial.txt", "R SERIAL-CPU> ")
33
34 def ec_write(self, s):
35 print("W SERIAL-EC> %s" % s)
36 self.ec_ser.serial.write(s.encode())
37
38 def cpu_write(self, s):
39 print("W SERIAL-CPU> %s" % s)
40 self.cpu_ser.serial.write(s.encode())
41
42 def run(self):
43 # Flush any partial commands in the EC's prompt, then ask for a reboot.
44 self.ec_write("\n")
45 self.ec_write("reboot\n")
46
47 # This is emitted right when the bootloader pauses to check for input.
48 # Emit a ^N character to request network boot, because we don't have a
49 # direct-to-netboot firmware on cheza.
50 for line in self.cpu_ser.lines():
51 if re.search("load_archive: loading locale_en.bin", line):
52 self.cpu_write("\016")
53 break
54
55 tftp_failures = 0
56 for line in self.cpu_ser.lines():
57 if re.search("---. end Kernel panic", line):
58 return 1
59
60 # The Cheza boards have issues with failing to bring up power to
61 # the system sometimes, possibly dependent on ambient temperature
62 # in the farm.
63 if re.search("POWER_GOOD not seen in time", line):
64 return 2
65
66 # The Cheza firmware seems to occasionally get stuck looping in
67 # this error state during TFTP booting, possibly based on amount of
68 # network traffic around it, but it'll usually recover after a
69 # reboot.
70 if re.search("R8152: Bulk read error 0xffffffbf", line):
71 tftp_failures += 1
72 if tftp_failures >= 100:
73 return 2
74
75 result = re.search("bare-metal result: (\S*)", line)
76 if result:
77 if result.group(1) == "pass":
78 return 0
79 else:
80 return 1
81
82 print("Reached the end of the CPU serial log without finding a result")
83 return 1
84
85 def main():
86 parser = argparse.ArgumentParser()
87 parser.add_argument('--cpu', type=str, help='CPU Serial device', required=True)
88 parser.add_argument('--ec', type=str, help='EC Serial device', required=True)
89 args = parser.parse_args()
90
91 servo = CrosServoRun(args.cpu, args.ec)
92
93 while True:
94 retval = servo.run()
95 if retval != 2:
96 break
97
98 # power down the CPU on the device
99 servo.ec_write("power off\n")
100
101 sys.exit(retval)
102
103 if __name__ == '__main__':
104 main()