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
6from autotest_lib.server import utils
7from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
8from autotest_lib.client.common_lib import error
9
10
11class firmware_UpdateFirmwareVersion(FirmwareTest):
12    """
13    Servo based firmware update test which checks the firmware version.
14
15    This test requires a USB test image plugged in. The firmware id of
16    the current running firmware must matches the system shellball's, or user
17    can provide a shellball to do this test. In this way, the client will be
18    update with the given shellball first. On runtime, this test modifies the
19    firmware version of the shellball and runs autoupdate. Check firmware
20    version after boot with firmware B, and then recover firmware A and B to
21    original shellball.
22    """
23    version = 1
24
25    def check_firmware_version(self, expected_ver):
26        actual_ver = self.faft_client.bios.get_version(
27                'b' if self.fw_vboot2 else 'a')
28        actual_tpm_fwver = self.faft_client.tpm.get_firmware_version()
29        if actual_ver != expected_ver or actual_tpm_fwver != expected_ver:
30            raise error.TestFail(
31                'Firmware version should be %s,'
32                'but got (fwver, tpm_fwver) = (%s, %s).' %
33                (expected_ver, actual_ver, actual_tpm_fwver))
34        else:
35            logging.info(
36                'Update success, now version is %s',
37                actual_ver)
38
39    def initialize(self, host, cmdline_args):
40        dict_args = utils.args_to_dict(cmdline_args)
41        self.use_shellball = dict_args.get('shellball', None)
42        super(firmware_UpdateFirmwareVersion, self).initialize(
43            host, cmdline_args)
44        self.backup_firmware()
45        updater_path = self.setup_firmwareupdate_shellball(self.use_shellball)
46
47        # Update firmware if needed
48        if updater_path:
49            self.set_hardware_write_protect(enable=False)
50            self.faft_client.updater.run_factory_install()
51            self.switcher.mode_aware_reboot()
52
53        self.setup_usbkey(usbkey=True)
54        self.switcher.setup_mode('normal')
55        self._fwid = self.faft_client.updater.get_fwid()
56
57        actual_ver = self.faft_client.bios.get_version('a')
58        logging.info('Origin version is %s', actual_ver)
59        self._update_version = actual_ver + 1
60        logging.info('Firmware version will update to version %s',
61                     self._update_version)
62
63        self.faft_client.updater.resign_firmware(self._update_version)
64        self.faft_client.updater.repack_shellball('test')
65
66    def cleanup(self):
67        self.faft_client.updater.cleanup()
68        self.restore_firmware()
69        self.invalidate_firmware_setup()
70        super(firmware_UpdateFirmwareVersion, self).cleanup()
71
72    def run_once(self):
73        logging.info("Update firmware with new version.")
74        self.check_state((self.checkers.crossystem_checker, {
75                          'fwid': self._fwid
76                          }))
77        self.check_state((self.checkers.fw_tries_checker, 'A'))
78        self.faft_client.updater.run_autoupdate('test')
79        self.switcher.mode_aware_reboot()
80
81        logging.info("Copy firmware form B to A.")
82        self.faft_client.updater.run_bootok('test')
83        self.check_state((self.checkers.fw_tries_checker, 'B'))
84        self.switcher.mode_aware_reboot()
85
86        logging.info("Check firmware and TPM version, then recovery.")
87        self.check_state((self.checkers.fw_tries_checker,
88                          'B' if self.fw_vboot2 else 'A'))
89        self.check_firmware_version(self._update_version)
90        self.faft_client.updater.run_recovery()
91        self.reboot_and_reset_tpm()
92
93        logging.info("Check Rollback version.")
94        self.check_state((self.checkers.crossystem_checker, {
95                          'fwid': self._fwid
96                          }))
97        self.check_state((self.checkers.fw_tries_checker,
98                          'B' if self.fw_vboot2 else 'A'))
99        self.check_firmware_version(self._update_version - 1)
100