1# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10from autotest_lib.server.cros import vboot_constants as vboot
11from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
12from autotest_lib.server.cros.faft.firmware_test import ConnectionError
13
14
15class firmware_ConsecutiveBoot(FirmwareTest):
16    """
17    Servo based consecutive boot test via power button to turn on DUT and
18    /sbin/shutdown command to turn off DUT.
19
20    This test is intended to be run with many iterations to ensure that the DUT
21    does boot into Chrome OS and then does power off later.
22
23    The iteration should be specified by the parameter -a "faft_iterations=10".
24    """
25    version = 1
26    # Number of power button press to boot before declare test fail.
27    POWER_ON_RETRY = 3
28
29
30    def initialize(self, host, cmdline_args, dev_mode=False):
31        # Parse arguments from command line
32        dict_args = utils.args_to_dict(cmdline_args)
33        self.faft_iterations = int(dict_args.get('faft_iterations', 1))
34        self.faft_waitup_time = int(dict_args.get('faft_waitup_time', 0))
35        self.faft_localrun = int(dict_args.get('faft_localrun', 0))
36        super(firmware_ConsecutiveBoot, self).initialize(host, cmdline_args)
37        self.switcher.setup_mode('dev' if dev_mode else 'normal')
38        if dev_mode:
39          self.clear_set_gbb_flags(0, vboot.GBB_FLAG_DEV_SCREEN_SHORT_DELAY)
40        self.setup_usbkey(usbkey=False)
41
42    def wait_for_client_aux(self):
43        """Use test specific timeout to wait for system to come up,
44           otherwise use default (180s).
45        """
46        logging.info('wait_for_client %d start.', self.faft_waitup_time)
47        if self.faft_waitup_time:
48            self.switcher.wait_for_client(self.faft_waitup_time)
49        else:
50            self.switcher.wait_for_client()
51
52    def shutdown_power_on(self):
53        """
54        Use /sbin/shutdown to turn off device follow by power button press to
55        turn on device.  Do not want to call full_power_off_and_on since we
56        are testing firmware and mainly want to focus on power on sequence.
57        """
58        boot_id = self.get_bootid()
59        # Call shutdown instead of long press the power key since we are
60        # testing the firmware and not the OS.
61        logging.info("Sending /sbin/shutdown -P now")
62        self.faft_client.system.run_shell_command('/sbin/shutdown -P now')
63        logging.info('Wait for client to go offline')
64        self.switcher.wait_for_client_offline(timeout=100, orig_boot_id=boot_id)
65        # Retry in case power_short_press was not registered.
66        for i in xrange(self.POWER_ON_RETRY):
67            logging.info("sleep %d, tap power key to boot.",
68                         self.faft_config.powerup_ready)
69            time.sleep(self.faft_config.powerup_ready)
70            self.servo.power_key(self.faft_config.hold_pwr_button_poweron)
71            try:
72                self.wait_for_client_aux()
73            except ConnectionError:
74                logging.error('wait_for_client exception %d.', i)
75            else:
76                logging.info('wait_for_client online done %d.', i)
77                return
78        raise ConnectionError()
79
80    def run_once(self, host, dev_mode=False):
81        for i in xrange(self.faft_iterations):
82            logging.info('======== Running FAFT ITERATION %d/%s ========',
83                         i+1, self.faft_iterations)
84            logging.info("Expected boot fine, full power off DUT and on.")
85            self.check_state((self.checkers.crossystem_checker, {
86                        'mainfw_type': 'developer' if dev_mode else 'normal',
87                        }))
88            self.shutdown_power_on()
89
90            logging.info("Expected boot fine.")
91            self.check_state((self.checkers.crossystem_checker, {
92                        'mainfw_type': 'developer' if dev_mode else 'normal',
93                        }))
94