1# Copyright (c) 2011 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.server.cros.faft.firmware_test import FirmwareTest
10
11
12class firmware_FwScreenCloseLid(FirmwareTest):
13    """
14    Servo based lid close triggered shutdown test during firmware screens.
15
16    This test requires a USB disk plugged-in, which contains a Chrome OS test
17    image (built by "build_image --test"). On runtime, this test triggers
18    firmware screens (developer, remove, insert, yuck, to_norm screens),
19    and then closes the lid in order to power the machine down.
20    """
21    version = 1
22
23    SHORT_SHUTDOWN_CONFIRMATION_PERIOD = 0.1
24
25    def wait_fw_screen_and_close_lid(self):
26        """Wait for firmware warning screen and close lid."""
27        time.sleep(self.faft_config.firmware_screen)
28        self.servo.lid_close()
29
30    def wait_longer_fw_screen_and_close_lid(self):
31        """Wait for firmware screen without timeout and close lid."""
32        time.sleep(self.faft_config.firmware_screen)
33        self.wait_fw_screen_and_close_lid()
34
35    def wait_second_screen_and_close_lid(self):
36        """Wait and trigger TO_NORM or RECOVERY INSERT screen and close lid."""
37        self.switcher.trigger_dev_to_rec()
38        self.wait_longer_fw_screen_and_close_lid()
39
40    def wait_yuck_screen_and_close_lid(self):
41        """Wait and trigger yuck screen and clod lid."""
42        # Insert a corrupted USB stick. A yuck screen is expected.
43        self.servo.switch_usbkey('dut')
44        time.sleep(self.faft_config.usb_plug)
45        self.wait_longer_fw_screen_and_close_lid()
46
47    def initialize(self, host, cmdline_args):
48        """Initialize the test"""
49        super(firmware_FwScreenCloseLid, self).initialize(host, cmdline_args)
50        if self.faft_config.has_lid:
51            self.switcher.setup_mode('dev')
52            self.setup_usbkey(True, host=True)
53            usb_dev = self.servo.probe_host_usb_dev()
54            # Corrupt the kernel of USB stick. It is needed for triggering a
55            # yuck screen later.
56            self.corrupt_usb_kernel(usb_dev)
57
58    def cleanup(self):
59        """Cleanup the test"""
60        try:
61            if self.faft_config.has_lid:
62                self.servo.switch_usbkey('host')
63                usb_dev = self.servo.probe_host_usb_dev()
64                # Restore kernel of USB stick which is corrupted on setup phase.
65                self.restore_usb_kernel(usb_dev)
66        except Exception as e:
67            logging.error("Caught exception: %s", str(e))
68        super(firmware_FwScreenCloseLid, self).cleanup()
69
70    def run_once(self):
71        """Main test logic"""
72        if not self.faft_config.has_lid:
73            logging.info('This test does nothing on devices without lid.')
74            return
75
76        # Some platforms may not turn on with just lid open
77        power_action = False
78        if not self.faft_config.lid_wake_from_power_off:
79            power_action = True
80
81        if (self.faft_config.fw_bypasser_type != 'ctrl_d_bypasser'
82          and self.faft_config.fw_bypasser_type != 'tablet_detachable_bypasser'):
83            raise error.TestNAError("This test is only valid on devices with "
84                                    "screens.")
85
86        if self.faft_config.chrome_ec and not self.check_ec_capability(['lid']):
87            raise error.TestNAError("TEST IT MANUALLY! ChromeEC can't control "
88                                    "lid on the device %s" %
89                                    self.faft_config.platform)
90
91        logging.info("Expected dev mode and reboot. "
92                     "When the next DEVELOPER SCREEN shown, close lid "
93                     "to make DUT shutdown.")
94        self.check_state((self.checkers.crossystem_checker, {
95                          'devsw_boot': '1',
96                          'mainfw_type': 'developer',
97                          }))
98        self.switcher.simple_reboot()
99        self.run_shutdown_process(self.wait_fw_screen_and_close_lid,
100                                  pre_power_action=self.servo.lid_open,
101                                  run_power_action=power_action,
102                                  post_power_action=self.switcher.bypass_dev_mode)
103        self.switcher.wait_for_client()
104
105        logging.info("Reboot. When the developer screen shown, press "
106                     "enter key to trigger either TO_NORM screen (new) or "
107                     "RECOVERY INSERT screen (old). Then close lid to "
108                     "make DUT shutdown.")
109        self.check_state((self.checkers.crossystem_checker, {
110                          'devsw_boot': '1',
111                          'mainfw_type': 'developer',
112                          }))
113        self.switcher.simple_reboot()
114        self.run_shutdown_process(self.wait_second_screen_and_close_lid,
115                                  pre_power_action=self.servo.lid_open,
116                                  run_power_action=power_action,
117                                  post_power_action=self.switcher.bypass_dev_mode,
118                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
119        self.switcher.wait_for_client()
120
121        logging.info("Request recovery boot. When the RECOVERY INSERT "
122                     "screen shows, close lid to make DUT shutdown.")
123        self.check_state((self.checkers.crossystem_checker, {
124                          'devsw_boot': '1',
125                          'mainfw_type': 'developer',
126                          }))
127        self.faft_client.system.request_recovery_boot()
128        self.switcher.simple_reboot()
129        self.run_shutdown_process(self.wait_longer_fw_screen_and_close_lid,
130                                  pre_power_action=self.servo.lid_open,
131                                  run_power_action=power_action,
132                                  post_power_action=self.switcher.bypass_dev_mode,
133                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
134        self.switcher.wait_for_client()
135
136        logging.info("Request recovery boot again. When the recovery "
137                     "insert screen shows, insert a corrupted USB and trigger "
138                     "a YUCK SCREEN. Then close lid to make DUT shutdown.")
139        self.check_state((self.checkers.crossystem_checker, {
140                          'devsw_boot': '1',
141                          'mainfw_type': 'developer',
142                          }))
143        self.faft_client.system.request_recovery_boot()
144        self.switcher.simple_reboot()
145        self.run_shutdown_process(self.wait_yuck_screen_and_close_lid,
146                                  pre_power_action=self.servo.lid_open,
147                                  run_power_action=power_action,
148                                  post_power_action=self.switcher.bypass_dev_mode,
149                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
150        self.switcher.wait_for_client()
151
152        logging.info("Switch back to normal mode.")
153        self.check_state((self.checkers.crossystem_checker, {
154                          'devsw_boot': '1',
155                          'mainfw_type': 'developer',
156                          }))
157        self.switcher.reboot_to_mode(to_mode='normal')
158
159        logging.info("Expected normal mode and request recovery boot. "
160                     "Because an USB stick is inserted, a RECOVERY REMOVE "
161                     "screen shows. Close lid to make DUT shutdown.")
162        self.check_state((self.checkers.crossystem_checker, {
163                          'devsw_boot': '0',
164                          'mainfw_type': 'normal',
165                          }))
166        self.faft_client.system.request_recovery_boot()
167        self.switcher.simple_reboot()
168        self.run_shutdown_process(self.wait_longer_fw_screen_and_close_lid,
169                                  pre_power_action=self.servo.lid_open,
170                                  run_power_action=power_action,
171                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
172        self.switcher.wait_for_client()
173        self.check_state((self.checkers.crossystem_checker, {
174                          'devsw_boot': '0',
175                          'mainfw_type': 'normal',
176                          }))
177