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 24import argparse 25import os 26import re 27from serial_buffer import SerialBuffer 28import sys 29import threading 30 31class FastbootRun: 32 def __init__(self, args): 33 self.powerup = args.powerup 34 self.ser = SerialBuffer(args.dev, "results/serial-output.txt", "R SERIAL> ") 35 self.fastboot="fastboot boot -s {ser} artifacts/fastboot.img".format(ser=args.fbserial) 36 37 def logged_system(self, cmd): 38 print("Running '{}'".format(cmd)) 39 return os.system(cmd) 40 41 def run(self): 42 if self.logged_system(self.powerup) != 0: 43 return 1 44 45 fastboot_ready = False 46 for line in self.ser.lines(): 47 if re.search("fastboot: processing commands", line) or \ 48 re.search("Listening for fastboot command on", line): 49 fastboot_ready = True 50 break 51 52 if re.search("data abort", line): 53 return 1 54 55 if not fastboot_ready: 56 print("Failed to get to fastboot prompt") 57 return 1 58 59 if self.logged_system(self.fastboot) != 0: 60 return 1 61 62 for line in self.ser.lines(): 63 if re.search("---. end Kernel panic", line): 64 return 1 65 66 # The db820c boards intermittently reboot. Just restart the run 67 # when if we see a reboot after we got past fastboot. 68 if re.search("PON REASON", line): 69 print("Detected spontaneous reboot, restarting run...") 70 return 2 71 72 result = re.search("bare-metal result: (\S*)", line) 73 if result: 74 if result.group(1) == "pass": 75 return 0 76 else: 77 return 1 78 79 print("Reached the end of the CPU serial log without finding a result") 80 return 1 81 82def main(): 83 parser = argparse.ArgumentParser() 84 parser.add_argument('--dev', type=str, help='Serial device (otherwise reading from serial-output.txt)') 85 parser.add_argument('--powerup', type=str, help='shell command for rebooting', required=True) 86 parser.add_argument('--powerdown', type=str, help='shell command for powering off', required=True) 87 parser.add_argument('--fbserial', type=str, help='fastboot serial number of the board', required=True) 88 args = parser.parse_args() 89 90 fastboot = FastbootRun(args) 91 92 while True: 93 retval = fastboot.run() 94 if retval != 2: 95 break 96 97 fastboot.logged_system(args.powerdown) 98 99 sys.exit(retval) 100 101if __name__ == '__main__': 102 main() 103