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
6
7from autotest_lib.server.cros import vboot_constants as vboot
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_UserRequestRecovery(FirmwareTest):
12    """
13    Servo based user request recovery boot test.
14
15    This test requires a USB disk plugged-in, which contains a ChromeOS test
16    image (built by "build_image --test"). On runtime, this test first requests
17    a recovery mode on next boot by setting the crossystem recovery_request
18    flag. It then triggers recovery mode by unplugging and plugging in the USB
19    disk and checks success of it.
20    """
21    version = 1
22    NEEDS_SERVO_USB = True
23
24    def ensure_normal_boot(self):
25        """Ensure normal mode boot this time.
26
27        If not, it may be a test failure during step 2, try to recover to
28        normal mode by simply rebooting the machine.
29        """
30        if not self.checkers.crossystem_checker(
31                {'mainfw_type': ('normal', 'developer')}):
32            self.switcher.mode_aware_reboot()
33
34    def initialize(self, host, cmdline_args, dev_mode=False, ec_wp=None):
35        super(firmware_UserRequestRecovery, self).initialize(host, cmdline_args,
36                                                             ec_wp=ec_wp)
37        self.switcher.setup_mode('dev' if dev_mode else 'normal')
38        self.setup_usbkey(usbkey=True, host=True)
39
40    def cleanup(self):
41        try:
42            self.ensure_normal_boot()
43        except Exception as e:
44            logging.error("Caught exception: %s", str(e))
45        super(firmware_UserRequestRecovery, self).cleanup()
46
47    def run_once(self, dev_mode=False):
48        """Runs a single iteration of the test."""
49        logging.info("Request recovery boot.")
50        self.check_state((self.checkers.crossystem_checker, {
51                           'mainfw_type': 'developer' if dev_mode else 'normal',
52                           }))
53        # Execute on DUT: crossystem recovery_request=193
54        self.faft_client.system.request_recovery_boot()
55        # Execute from desktop:
56        #   dut-control warm_reset:on sleep:0.5000 warm_reset:off
57        self.switcher.simple_reboot()
58
59        # DUT should be waiting for USB after reboot.
60        # Connect servo USB to DUT and DUT should boot from USB.
61        # dut-control usb_mux_sel1:dut_sees_usbkey
62        # bypass_rec_mode will issue power_state:rec if host did not response in
63        # delay_reboot_to_ping seconds, which may reset recovery_reason and
64        # fail this tests.
65        self.switcher.bypass_rec_mode()
66        self.switcher.wait_for_client()
67
68        logging.info("Expected recovery boot, request recovery again.")
69        self.check_state((self.checkers.crossystem_checker, {
70                           'mainfw_type': 'recovery',
71                           'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
72                           }))
73
74        self.faft_client.system.request_recovery_boot()
75        self.switcher.simple_reboot()
76        self.switcher.bypass_rec_mode()
77        self.switcher.wait_for_client()
78
79        logging.info("Expected recovery boot.")
80        self.check_state((self.checkers.crossystem_checker, {
81                           'mainfw_type': 'recovery',
82                           'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
83                           }))
84        self.switcher.mode_aware_reboot()
85
86        logging.info("Expected normal boot.")
87        self.check_state((self.checkers.crossystem_checker, {
88                           'mainfw_type': 'developer' if dev_mode else 'normal',
89                           }))
90