• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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
5from __future__ import print_function
6
7import logging
8import time
9
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import tpm_utils
12from autotest_lib.server import autotest
13from autotest_lib.server.cros.faft.cr50_test import Cr50Test
14
15
16class firmware_Cr50FactoryResetVC(Cr50Test):
17    """A test verifying factory mode vendor command."""
18    version = 1
19
20    FWMP_DEV_DISABLE_CCD_UNLOCK = (1 << 6)
21    # Short wait to make sure cr50 has had enough time to update the ccd state
22    SLEEP = 2
23    BOOL_VALUES = (True, False)
24
25    def initialize(self, host, cmdline_args, full_args):
26        """Initialize servo check if cr50 exists."""
27        super(firmware_Cr50FactoryResetVC, self).initialize(host, cmdline_args,
28                full_args)
29        if not self.cr50.has_command('bpforce'):
30            raise error.TestNAError('Cannot run test without bpforce')
31        self.fast_ccd_open(enable_testlab=True)
32        # Reset ccd completely.
33        self.cr50.send_command('ccd reset')
34
35        # If we can fake battery connect/disconnect, then we can test the vendor
36        # command.
37        try:
38            self.bp_override(True)
39            self.bp_override(False)
40        except Exception as e:
41            logging.info(e)
42            raise error.TestNAError('Cannot fully test factory mode vendor '
43                    'command without the ability to fake battery presence')
44
45    def cleanup(self):
46        """Clear the FWMP and ccd state"""
47        try:
48            self.clear_state()
49        finally:
50            super(firmware_Cr50FactoryResetVC, self).cleanup()
51
52
53    def bp_override(self, connect):
54        """Deassert BATT_PRES signal, so cr50 will think wp is off."""
55        self.cr50.send_command('ccd testlab open')
56        self.cr50.set_batt_pres_state('connect' if connect else 'disconnect',
57                                      False)
58        if self.cr50.get_batt_pres_state()[1] != connect:
59            raise error.TestError('Could not fake battery %sconnect' %
60                    ('' if connect else 'dis'))
61        self.cr50.set_ccd_level('lock')
62
63
64    def fwmp_ccd_lockout(self):
65        """Returns True if FWMP is locking out CCD."""
66        return 'fwmp_lock' in self.cr50.get_ccd_info('TPM')
67
68
69    def set_fwmp_lockout(self, enable):
70        """Change the FWMP to enable or disable ccd.
71
72        Args:
73            enable: True if FWMP flags should lock out ccd.
74        """
75        logging.info('%sing FWMP ccd lockout', 'enabl' if enable else 'clear')
76        if enable:
77            flags = hex(self.FWMP_DEV_DISABLE_CCD_UNLOCK)
78            logging.info('Setting FWMP flags to %s', flags)
79            autotest.Autotest(self.host).run_test('firmware_SetFWMP',
80                    flags=flags, fwmp_cleared=True, check_client_result=True)
81
82        if (not self.fwmp_ccd_lockout()) != (not enable):
83            raise error.TestError('Could not %s fwmp lockout' %
84                    ('set' if enable else 'clear'))
85
86
87    def setup_ccd_password(self, set_password):
88        """Set the Cr50 CCD password.
89
90        Args:
91            set_password: if True set the password. The password is already
92                    cleared, so if False just check the password is cleared
93        """
94        if set_password:
95            self.cr50.send_command('ccd testlab open')
96            # Set the ccd password
97            self.set_ccd_password(self.CCD_PASSWORD)
98        if self.cr50.password_is_reset() == set_password:
99            raise error.TestError('Could not %s password' %
100                    ('set' if set_password else 'clear'))
101
102
103    def factory_mode_enabled(self):
104        """Returns True if factory mode is enabled."""
105        caps = self.cr50.get_cap_dict()
106        caps.pop('GscFullConsole')
107        return self.cr50.get_cap_overview(caps)[0]
108
109
110    def get_relevant_state(self):
111        """Returns cr50 state that can lock out factory mode.
112
113        FWMP, battery presence, or a password can all lock out enabling factory
114        mode using the vendor command. If any item in state is True, factory
115        mode should be locked out.
116        """
117        state = []
118        state.append(self.fwmp_ccd_lockout())
119        state.append(self.cr50.get_batt_pres_state()[1])
120        state.append(not self.cr50.password_is_reset())
121        return state
122
123
124    def get_state_message(self):
125        """Convert relevant state into a useful log message."""
126        fwmp, bp, password = self.get_relevant_state()
127        return ('fwmp %s bp %sconnected password %s' %
128                ('set' if fwmp else 'cleared',
129                 '' if bp else 'dis',
130                 'set' if password else 'cleared'))
131
132
133    def factory_locked_out(self):
134        """Returns True if any state preventing factory mode is True."""
135        return True in self.get_relevant_state()
136
137
138    def set_factory_mode(self, enable):
139        """Use the vendor command to control factory mode.
140
141        Args:
142            enable: Enable factory mode if True. Disable it if False.
143        """
144        enable_fail = self.factory_locked_out() and enable
145        time.sleep(self.SLEEP)
146        logging.info('%sABLING FACTORY MODE', 'EN' if enable else 'DIS')
147        if enable:
148            logging.info('EXPECT: %s', 'failure' if enable_fail else 'success')
149        cmd = 'enable' if enable else 'disable'
150
151        result = self.host.run('gsctool -a -F %s' % cmd,
152                ignore_status=(enable_fail or not enable))
153        logging.debug(result)
154        expect_enabled = enable and not enable_fail
155
156        if expect_enabled:
157            # Cr50 will reboot after it enables factory mode.
158            self.cr50.wait_for_reboot(timeout=10)
159        else:
160            # Wait long enoug for cr50 to udpate the ccd state.
161            time.sleep(self.SLEEP)
162        if self.factory_mode_enabled() != expect_enabled:
163            raise error.TestFail('Unexpected factory mode %s result' % cmd)
164
165
166    def clear_state(self):
167        """Clear the FWMP and reset CCD"""
168        # Clear the FWMP
169        self.clear_fwmp()
170        # make sure all of the ccd stuff is reset
171        self.cr50.send_command('ccd testlab open')
172        # Run ccd reset to make sure all ccd state is cleared
173        self.cr50.send_command('ccd reset')
174        # Clear the TPM owner, so we can set the ccd password and
175        # create the FWMP
176        tpm_utils.ClearTPMOwnerRequest(self.host, wait_for_ready=True)
177
178
179    def run_once(self):
180        """Verify FWMP disable with different flag values."""
181        errors = []
182        # Try enabling factory mode in each valid state. Cr50 checks write
183        # protect, password, and fwmp before allowing fwmp to be enabled.
184        for lockout_ccd_with_fwmp in self.BOOL_VALUES:
185            for set_password in self.BOOL_VALUES:
186                for connect in self.BOOL_VALUES:
187                    # Clear relevant state, so we can set the fwmp and password
188                    self.clear_state()
189
190                    # Setup the cr50 state
191                    self.setup_ccd_password(set_password)
192                    self.bp_override(connect)
193                    self.set_fwmp_lockout(lockout_ccd_with_fwmp)
194                    self.cr50.set_ccd_level('lock')
195
196                    logging.info('RUN: %s', self.get_state_message())
197
198                    try:
199                        self.set_factory_mode(True)
200                        self.set_factory_mode(False)
201                    except Exception as e:
202                        message = 'FAILURE %r %r' % (self.get_state_message(),
203                                e)
204                        logging.info(message)
205                        errors.append(message)
206        if errors:
207            raise error.TestFail(errors)
208