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.server.cros import vboot_constants as vboot
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_RollbackKernel(FirmwareTest):
12    """
13    Servo based kernel rollback test.
14
15    This test requires a USB disk plugged-in, which contains a Chrome OS test
16    image (built by "build_image --test"). In normal mode, this test rollbacks
17    kernel A and results kernel B boot. It then rollbacks kernel B and
18    results recovery boot. In developer mode, the firmware ignores kernel
19    rollback check so it remains unchanged.
20    """
21    version = 1
22
23    def ensure_kernel_on_non_recovery(self, part):
24        """Ensure the requested kernel part on normal/dev boot path.
25
26        If not, it may be a test failure during step 2 or 3, try to recover to
27        the requested kernel on normal/dev mode by recovering the whole OS
28        and rebooting.
29
30        @param part: The expected kernel partition number.
31        """
32        if not self.check_root_part_on_non_recovery(part):
33            logging.info('Recover the disk OS by running chromeos-install...')
34            self.faft_client.system.run_shell_command('chromeos-install --yes')
35            self.switcher.mode_aware_reboot()
36
37    def initialize(self, host, cmdline_args, dev_mode=False):
38        super(firmware_RollbackKernel, self).initialize(host, cmdline_args)
39        self.backup_kernel()
40        self.backup_cgpt_attributes()
41        self.switcher.setup_mode('dev' if dev_mode else 'normal')
42        self.setup_usbkey(usbkey=True, host=False)
43        self.setup_kernel('a')
44
45    def cleanup(self):
46        self.ensure_kernel_on_non_recovery('a')
47        self.restore_cgpt_attributes()
48        self.restore_kernel()
49        super(firmware_RollbackKernel, self).cleanup()
50
51    def run_once(self, dev_mode=False):
52        # Historical reason that the old models use a different value.
53        if self.faft_client.system.get_platform_name() in (
54                'Mario', 'Alex', 'ZGB'):
55            recovery_reason = vboot.RECOVERY_REASON['RW_NO_OS']
56        else:
57            recovery_reason = (vboot.RECOVERY_REASON['DEP_RW_NO_DISK'],
58                               vboot.RECOVERY_REASON['RW_NO_KERNEL'])
59
60        if dev_mode:
61            logging.info("Rollbacks kernel A.")
62            self.check_state((self.check_root_part_on_non_recovery, 'a'))
63            self.faft_client.kernel.move_version_backward('a')
64            self.switcher.mode_aware_reboot()
65
66            logging.info("Still kernel A boot since dev_mode ignores "
67                         "kernel rollback check.")
68            self.check_state((self.check_root_part_on_non_recovery, 'a'))
69            self.faft_client.kernel.move_version_forward('a')
70        else:
71            logging.info("Rollbacks kernel A.")
72            self.check_state((self.check_root_part_on_non_recovery, 'a'))
73            self.faft_client.kernel.move_version_backward('a')
74            self.switcher.mode_aware_reboot()
75
76            logging.info("Expected kernel B boot and rollbacks kernel B.")
77            self.check_state((self.check_root_part_on_non_recovery, 'b'))
78            self.faft_client.kernel.move_version_backward('b')
79            self.switcher.mode_aware_reboot()
80
81            logging.info("Expected recovery boot and restores the OS image.")
82            self.check_state((self.checkers.crossystem_checker, {
83                                  'mainfw_type': 'recovery',
84                                  'recovery_reason': recovery_reason,
85                                  }))
86            self.faft_client.kernel.move_version_forward(('a', 'b'))
87            self.switcher.mode_aware_reboot()
88
89            logging.info("Expected kernel A boot and done.")
90            self.check_state((self.check_root_part_on_non_recovery, 'a'))
91