1# Copyright 2014 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.faft.firmware_test import FirmwareTest
8
9
10class firmware_TPMKernelVersion(FirmwareTest):
11    """Dev mode will not corrupt kernel and firmware version stored in the TPM.
12
13    Automate 1.2.4 Check Kernel version in TPM is not corrupted.
14    """
15    version = 1
16
17    def initialize(self, host, cmdline_args):
18        """Initialize the test"""
19        super(firmware_TPMKernelVersion, self).initialize(host, cmdline_args)
20        self.switcher.setup_mode('dev')
21        # Use the USB key for Ctrl-U dev boot, not recovery.
22        self.setup_usbkey(usbkey=True, host=False, used_for_recovery=False)
23        self.original_dev_boot_usb = self.faft_client.system.get_dev_boot_usb()
24        logging.info('Original dev_boot_usb value: %s',
25                     str(self.original_dev_boot_usb))
26
27
28    def dut_run_cmd(self, command):
29        """Execute command on DUT.
30
31        @param command: shell command to be executed on DUT.
32        @returns command output.
33        """
34        logging.info('Execute %s', command)
35        output = self.faft_client.system.run_shell_command_get_output(command)
36        logging.info('Output %s', output)
37        return output
38
39    def cleanup(self):
40        """Reboot device from SSD."""
41        try:
42            self.ensure_dev_internal_boot(self.original_dev_boot_usb)
43        except Exception as e:
44            logging.error("Caught exception: %s", str(e))
45        super(firmware_TPMKernelVersion, self).cleanup()
46
47    def run_once(self):
48        """Main test logic"""
49        # Get the kernel version and its datakey version.
50        # TODO(twreid): Verify the results.
51        version = self.faft_client.tpm.get_kernel_version()
52        logging.info('Kernel version: %d', version)
53        version = self.faft_client.tpm.get_kernel_datakey_version()
54        logging.info('Kernel datakey version: %d', version)
55
56        self.dut_run_cmd('crossystem kernkey_vfy dev_boot_usb')
57        # Boot CrOS from USB
58        self.faft_client.system.set_dev_boot_usb(1)
59        self.switcher.simple_reboot()
60        self.switcher.bypass_dev_boot_usb()
61        self.switcher.wait_for_client()
62
63        # Check that DUT is booted from USB.
64        self.check_state((self.checkers.dev_boot_usb_checker, (True, True),
65                          'Device not booted from USB image properly.'))
66
67        out = self.dut_run_cmd('crossystem tpm_kernver tpm_fwver')
68        (kernver, fwver) = out[0].split(' ')
69        logging.info('tpm_kernver=%s tpm_fwver=%s', kernver, fwver)
70        assert kernver != '0xFFFFFFFF'
71        assert fwver != '0xFFFFFFFF'
72