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        super(firmware_FwScreenCloseLid, self).initialize(host, cmdline_args)
49        if self.faft_config.has_lid:
50            self.assert_test_image_in_usb_disk()
51            self.switcher.setup_mode('dev')
52            self.servo.switch_usbkey('host')
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        try:
60            if self.faft_config.has_lid:
61                self.servo.switch_usbkey('host')
62                usb_dev = self.servo.probe_host_usb_dev()
63                # Restore kernel of USB stick which is corrupted on setup phase.
64                self.restore_usb_kernel(usb_dev)
65        except Exception as e:
66            logging.error("Caught exception: %s", str(e))
67        super(firmware_FwScreenCloseLid, self).cleanup()
68
69    def run_once(self):
70        if not self.faft_config.has_lid:
71            logging.info('This test does nothing on devices without lid.')
72            return
73
74        if (self.faft_config.fw_bypasser_type != 'ctrl_d_bypasser'
75          and self.faft_config.fw_bypasser_type != 'tablet_detachable_bypasser'):
76            raise error.TestNAError("This test is only valid on devices with "
77                                    "screens.")
78
79        if self.faft_config.chrome_ec and not self.check_ec_capability(['lid']):
80            raise error.TestNAError("TEST IT MANUALLY! ChromeEC can't control "
81                                    "lid on the device %s" %
82                                    self.faft_config.platform)
83
84        logging.info("Expected dev mode and reboot. "
85                     "When the next DEVELOPER SCREEN shown, close lid "
86                     "to make DUT shutdown.")
87        self.check_state((self.checkers.crossystem_checker, {
88                          'devsw_boot': '1',
89                          'mainfw_type': 'developer',
90                          }))
91        self.switcher.simple_reboot()
92        self.run_shutdown_process(self.wait_fw_screen_and_close_lid,
93                                  pre_power_action=self.servo.lid_open,
94                                  run_power_action=False,
95                                  post_power_action=self.switcher.bypass_dev_mode)
96        self.switcher.wait_for_client()
97
98        logging.info("Reboot. When the developer screen shown, press "
99                     "enter key to trigger either TO_NORM screen (new) or "
100                     "RECOVERY INSERT screen (old). Then close lid to "
101                     "make DUT shutdown.")
102        self.check_state((self.checkers.crossystem_checker, {
103                          'devsw_boot': '1',
104                          'mainfw_type': 'developer',
105                          }))
106        self.switcher.simple_reboot()
107        self.run_shutdown_process(self.wait_second_screen_and_close_lid,
108                                  pre_power_action=self.servo.lid_open,
109                                  run_power_action=False,
110                                  post_power_action=self.switcher.bypass_dev_mode,
111                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
112        self.switcher.wait_for_client()
113
114        logging.info("Request recovery boot. When the RECOVERY INSERT "
115                     "screen shows, close lid to make DUT shutdown.")
116        self.check_state((self.checkers.crossystem_checker, {
117                          'devsw_boot': '1',
118                          'mainfw_type': 'developer',
119                          }))
120        self.faft_client.system.request_recovery_boot()
121        self.switcher.simple_reboot()
122        self.run_shutdown_process(self.wait_longer_fw_screen_and_close_lid,
123                                  pre_power_action=self.servo.lid_open,
124                                  run_power_action=False,
125                                  post_power_action=self.switcher.bypass_dev_mode,
126                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
127        self.switcher.wait_for_client()
128
129        logging.info("Request recovery boot again. When the recovery "
130                     "insert screen shows, insert a corrupted USB and trigger "
131                     "a YUCK SCREEN. Then close lid to make DUT shutdown.")
132        self.check_state((self.checkers.crossystem_checker, {
133                          'devsw_boot': '1',
134                          'mainfw_type': 'developer',
135                          }))
136        self.faft_client.system.request_recovery_boot()
137        self.switcher.simple_reboot()
138        self.run_shutdown_process(self.wait_yuck_screen_and_close_lid,
139                                  pre_power_action=self.servo.lid_open,
140                                  run_power_action=False,
141                                  post_power_action=self.switcher.bypass_dev_mode,
142                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
143        self.switcher.wait_for_client()
144
145        logging.info("Switch back to normal mode.")
146        self.check_state((self.checkers.crossystem_checker, {
147                          'devsw_boot': '1',
148                          'mainfw_type': 'developer',
149                          }))
150        self.switcher.reboot_to_mode(to_mode='normal')
151
152        logging.info("Expected normal mode and request recovery boot. "
153                     "Because an USB stick is inserted, a RECOVERY REMOVE "
154                     "screen shows. Close lid to make DUT shutdown.")
155        self.check_state((self.checkers.crossystem_checker, {
156                          'devsw_boot': '0',
157                          'mainfw_type': 'normal',
158                          }))
159        self.faft_client.system.request_recovery_boot()
160        self.switcher.simple_reboot()
161        self.run_shutdown_process(self.wait_longer_fw_screen_and_close_lid,
162                                  pre_power_action=self.servo.lid_open,
163                                  run_power_action=False,
164                                  shutdown_timeout=self.SHORT_SHUTDOWN_CONFIRMATION_PERIOD)
165        self.switcher.wait_for_client()
166        self.check_state((self.checkers.crossystem_checker, {
167                          'devsw_boot': '0',
168                          'mainfw_type': 'normal',
169                          }))
170