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        """Initialize the test"""
25        super(firmware_DevBootUSB, self).initialize(host, cmdline_args,
26                                                    ec_wp=ec_wp)
27        self.switcher.setup_mode('dev')
28        # Use the USB key for Ctrl-U dev boot, not recovery.
29        self.setup_usbkey(usbkey=True, host=False, used_for_recovery=False)
30
31        self.original_dev_boot_usb = self.faft_client.system.get_dev_boot_usb()
32        logging.info('Original dev_boot_usb value: %s',
33                     str(self.original_dev_boot_usb))
34
35    def cleanup(self):
36        """Cleanup the test"""
37        try:
38            self.ensure_dev_internal_boot(self.original_dev_boot_usb)
39        except Exception as e:
40            logging.error("Caught exception: %s", str(e))
41        super(firmware_DevBootUSB, self).cleanup()
42
43    def run_once(self):
44        """Main test logic"""
45        if (self.faft_config.has_keyboard and
46                not self.check_ec_capability(['keyboard'])):
47            raise error.TestNAError("TEST IT MANUALLY! This test can't be "
48                                    "automated on non-Chrome-EC devices.")
49
50        logging.info("Expected developer mode, set dev_boot_usb to 0.")
51        self.check_state((self.checkers.dev_boot_usb_checker, False))
52        self.faft_client.system.set_dev_boot_usb(0)
53        self.switcher.mode_aware_reboot()
54
55        logging.info("Expected internal disk boot, set dev_boot_usb to 1.")
56        self.check_state((self.checkers.dev_boot_usb_checker,
57                          False,
58                          "Not internal disk boot, dev_boot_usb misbehaved"))
59        self.faft_client.system.set_dev_boot_usb(1)
60        self.switcher.simple_reboot()
61        self.switcher.bypass_dev_boot_usb()
62        self.switcher.wait_for_client()
63
64        logging.info("Expected USB boot, set dev_boot_usb to the original.")
65        self.check_state((self.checkers.dev_boot_usb_checker, (True, True),
66                          'Device not booted from USB image properly.'))
67        self.faft_client.system.set_dev_boot_usb(self.original_dev_boot_usb)
68        self.switcher.mode_aware_reboot()
69
70        logging.info("Check and done.")
71        self.check_state((self.checkers.dev_boot_usb_checker, False))
72