1# Copyright (c) 2012 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
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_DevBootUSB(FirmwareTest):
12    """
13    Servo based Ctrl-U developer USB boot test.
14
15    This test requires a USB disk plugged-in, which contains a Chrome OS test
16    image (built by "build_image test"). On runtime, this test first switches
17    DUT to developer mode. When dev_boot_usb=0, pressing Ctrl-U on developer
18    screen should not boot the USB disk. When dev_boot_usb=1, pressing Ctrl-U
19    should boot the USB disk.
20    """
21    version = 1
22
23    def initialize(self, host, cmdline_args, ec_wp=None):
24        super(firmware_DevBootUSB, self).initialize(host, cmdline_args,
25                                                    ec_wp=ec_wp)
26        self.switcher.setup_mode('dev')
27        self.setup_usbkey(usbkey=True, host=False)
28
29        self.original_dev_boot_usb = self.faft_client.system.get_dev_boot_usb()
30        logging.info('Original dev_boot_usb value: %s',
31                     str(self.original_dev_boot_usb))
32
33    def cleanup(self):
34        self.ensure_internal_device_boot()
35        super(firmware_DevBootUSB, self).cleanup()
36
37    def ensure_internal_device_boot(self):
38        """Ensure internal device boot; if not, reboot into it.
39
40        If not, it may be a test failure during step 2 or 3, try to reboot
41        and press Ctrl-D to internal device boot.
42        """
43        if self.faft_client.system.is_removable_device_boot():
44            logging.info('Reboot into internal disk...')
45            self.faft_client.system.set_dev_boot_usb(self.original_dev_boot_usb)
46            self.switcher.mode_aware_reboot()
47
48    def run_once(self):
49        if (self.faft_config.has_keyboard and
50                not self.check_ec_capability(['keyboard'])):
51            raise error.TestNAError("TEST IT MANUALLY! This test can't be "
52                                    "automated on non-Chrome-EC devices.")
53
54        logging.info("Expected developer mode, set dev_boot_usb to 0.")
55        self.check_state((self.checkers.dev_boot_usb_checker, False))
56        self.faft_client.system.set_dev_boot_usb(0)
57        self.switcher.mode_aware_reboot()
58
59        logging.info("Expected internal disk boot, set dev_boot_usb to 1.")
60        self.check_state((self.checkers.dev_boot_usb_checker,
61                          False,
62                          "Not internal disk boot, dev_boot_usb misbehaved"))
63        self.faft_client.system.set_dev_boot_usb(1)
64        self.switcher.mode_aware_reboot(wait_for_dut_up=False)
65        self.switcher.bypass_dev_boot_usb()
66        self.switcher.wait_for_client()
67
68        logging.info("Expected USB boot, set dev_boot_usb to the original.")
69        self.check_state((self.checkers.dev_boot_usb_checker,
70                          True,
71                          "Not USB boot, Ctrl-U not work"))
72        self.faft_client.system.set_dev_boot_usb(self.original_dev_boot_usb)
73        self.switcher.mode_aware_reboot(wait_for_dut_up=False)
74        self.switcher.bypass_dev_mode()
75        self.switcher.wait_for_client()
76
77        logging.info("Check and done.")
78        self.check_state((self.checkers.dev_boot_usb_checker, False))
79