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, os
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_UpdateFirmwareDataKeyVersion(FirmwareTest):
12    """
13    This test requires a USB test image plugged in. The firmware id
14    should matches fwid of shellball chromeos-firmwareupdate, or user can
15    provide a shellball to do this test. In this way, the client will be update
16    with the given shellball first. On runtime, this test modifies shellball
17    and runs autoupdate. Check firmware datakey version after boot with
18    firmware B, and then recover firmware A and B to original shellball.
19    """
20    version = 1
21
22    def resign_datakey_version(self, host):
23        host.send_file(os.path.join(self.bindir,
24                                    'files/common.sh'),
25                       os.path.join(self.faft_client.updater.get_temp_path(),
26                                     'common.sh'))
27        host.send_file(os.path.join(self.bindir,
28                                    'files/make_keys.sh'),
29                       os.path.join(self.faft_client.updater.get_temp_path(),
30                                    'make_keys.sh'))
31
32        self.faft_client.system.run_shell_command('/bin/bash %s %s' % (
33             os.path.join(self.faft_client.updater.get_temp_path(),
34                          'make_keys.sh'),
35             self._update_version))
36
37
38    def check_firmware_datakey_version(self, expected_ver):
39        actual_ver = self.faft_client.bios.get_datakey_version(
40                'b' if self.fw_vboot2 else 'a')
41        actual_tpm_fwver = self.faft_client.tpm.get_firmware_datakey_version()
42        if actual_ver != expected_ver or actual_tpm_fwver != expected_ver:
43            raise error.TestFail(
44                'Firmware data key version should be %s,'
45                'but got (fwver, tpm_fwver) = (%s, %s).'
46                % (expected_ver, actual_ver, actual_tpm_fwver))
47        else:
48            logging.info(
49                'Update success, now datakey version is %s', actual_ver)
50
51
52    def initialize(self, host, cmdline_args):
53        dict_args = utils.args_to_dict(cmdline_args)
54        self.use_shellball = dict_args.get('shellball', None)
55        super(firmware_UpdateFirmwareDataKeyVersion, self).initialize(
56            host, cmdline_args)
57        self.backup_firmware()
58        updater_path = self.setup_firmwareupdate_shellball(self.use_shellball)
59
60        # Update firmware if needed
61        if updater_path:
62            self.set_hardware_write_protect(enable=False)
63            self.faft_client.updater.run_factory_install()
64            self.switcher.mode_aware_reboot()
65
66        self.setup_usbkey(usbkey=True)
67        self.switcher.setup_mode('normal')
68        self._fwid = self.faft_client.updater.get_fwid()
69
70        actual_ver = self.faft_client.bios.get_datakey_version('a')
71        logging.info('Origin version is %s', actual_ver)
72        self._update_version = actual_ver + 1
73        logging.info('Firmware version will update to version %s',
74            self._update_version)
75
76        self.resign_datakey_version(host)
77        self.faft_client.updater.resign_firmware(1)
78        self.faft_client.updater.repack_shellball('test')
79
80
81    def cleanup(self):
82        self.faft_client.updater.cleanup()
83        self.restore_firmware()
84        self.invalidate_firmware_setup()
85        super(firmware_UpdateFirmwareDataKeyVersion, self).cleanup()
86
87
88    def run_once(self):
89        logging.info("Update firmware with new datakey version.")
90        self.check_state((self.checkers.crossystem_checker, {
91                          'fwid': self._fwid
92                          }))
93        self.check_state((self.checkers.fw_tries_checker, 'A'))
94        self.faft_client.updater.run_autoupdate('test')
95        self.switcher.mode_aware_reboot()
96
97        logging.info("Check firmware data key version and Rollback.")
98        self.faft_client.updater.run_bootok('test')
99        self.check_state((self.checkers.fw_tries_checker, 'B'))
100        self.switcher.mode_aware_reboot()
101
102        logging.info("Check firmware and TPM version, then recovery.")
103        self.check_state((self.checkers.fw_tries_checker,
104                          'B' if self.fw_vboot2 else 'A'))
105        self.check_firmware_datakey_version(self._update_version)
106        self.faft_client.updater.run_recovery()
107        self.reboot_and_reset_tpm()
108
109        logging.info("Check Rollback version.")
110        self.check_state((self.checkers.crossystem_checker, {
111                          'fwid': self._fwid
112                          }))
113        self.check_state((self.checkers.fw_tries_checker,
114                          'B' if self.fw_vboot2 else 'A'))
115        self.check_firmware_datakey_version(self._update_version - 1)
116