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 Chrome OS 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
23    def ensure_normal_boot(self):
24        """Ensure normal mode boot this time.
25
26        If not, it may be a test failure during step 2, try to recover to
27        normal mode by simply rebooting the machine.
28        """
29        if not self.checkers.crossystem_checker(
30                {'mainfw_type': ('normal', 'developer')}):
31            self.switcher.mode_aware_reboot()
32
33    def initialize(self, host, cmdline_args, dev_mode=False, ec_wp=None):
34        super(firmware_UserRequestRecovery, self).initialize(host, cmdline_args,
35                                                             ec_wp=ec_wp)
36        self.switcher.setup_mode('dev' if dev_mode else 'normal')
37        self.setup_usbkey(usbkey=True, host=True)
38
39    def cleanup(self):
40        self.ensure_normal_boot()
41        super(firmware_UserRequestRecovery, self).cleanup()
42
43    def run_once(self, dev_mode=False):
44        logging.info("Request recovery boot.")
45        self.check_state((self.checkers.crossystem_checker, {
46                           'mainfw_type': 'developer' if dev_mode else 'normal',
47                           }))
48        self.faft_client.system.request_recovery_boot()
49        self.switcher.mode_aware_reboot(wait_for_dut_up=False)
50        self.switcher.bypass_rec_mode()
51        self.switcher.wait_for_client()
52
53        logging.info("Expected recovery boot, request recovery again.")
54        self.check_state((self.checkers.crossystem_checker, {
55                           'mainfw_type': 'recovery',
56                           'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
57                           }))
58        self.faft_client.system.request_recovery_boot()
59        self.switcher.mode_aware_reboot(wait_for_dut_up=False)
60        if not dev_mode:
61            self.switcher.bypass_rec_mode()
62        self.switcher.wait_for_client()
63
64        logging.info("Expected recovery boot.")
65        self.check_state((self.checkers.crossystem_checker, {
66                           'mainfw_type': 'recovery',
67                           'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
68                           }))
69        self.switcher.mode_aware_reboot()
70
71        logging.info("Expected normal boot.")
72        self.check_state((self.checkers.crossystem_checker, {
73                           'mainfw_type': 'developer' if dev_mode else 'normal',
74                           }))
75