1# Copyright 2017 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.client.common_lib import error 8from autotest_lib.server import autotest 9from autotest_lib.server.cros.faft.cr50_test import Cr50Test 10 11 12class firmware_FWMPDisableCCD(Cr50Test): 13 """A test that uses cryptohome to set the FWMP flags and verifies that 14 cr50 disables/enables console unlock.""" 15 version = 1 16 17 FWMP_DEV_DISABLE_CCD_UNLOCK = (1 << 6) 18 GSCTOOL_ERR = 'Error: rv 7, response 7' 19 PASSWORD = 'Password' 20 21 def initialize(self, host, cmdline_args, full_args): 22 """Initialize servo check if cr50 exists""" 23 super(firmware_FWMPDisableCCD, self).initialize(host, cmdline_args, 24 full_args) 25 self.fast_open(enable_testlab=True) 26 27 28 def try_set_ccd_level(self, level, fwmp_disabled_ccd): 29 """Try setting the ccd level with a password. 30 31 The normal Cr50 ccd path requires entering dev mode. Entering dev mode 32 may be disabled by the FWMP flags. Entering dev mode also erases the 33 FWMP. The test uses a password to get around the dev mode requirement. 34 Send CCD commands from the commandline with the password. Check the 35 output to make sure if it fails, it failed because of the FWMP. 36 37 @param level: the desired ccd level: 'open' or 'unlock'. 38 @param fwmp_disabled_ccd: True if 'ccd set $LEVEL' should fail 39 """ 40 # Make sure the console is locked 41 self.cr50.set_ccd_level('lock') 42 try: 43 self.cr50.set_ccd_level(level, self.PASSWORD) 44 if fwmp_disabled_ccd: 45 raise error.TestFail('FWMP failed to prevent %r' % level) 46 except error.TestFail, e: 47 logging.info(e) 48 if fwmp_disabled_ccd: 49 if ("FWMP disabled 'ccd open'" in str(e) or 50 'Console unlock not allowed' in str(e)): 51 logging.info('FWMP successfully disabled ccd %s', level) 52 return 53 else: 54 raise error.TestFail('FWMP disabled %s in unexpected ' 55 'manner %r' % (level, str(e))) 56 raise 57 58 59 def open_cr50_and_setup_ccd(self): 60 """Configure cr50 ccd for the test. 61 62 Open Cr50. Reset ccd, so the capabilities are reset and the password is 63 cleared. Set OpenNoTPMWipe to Always, so the FWMP won't be cleared 64 during open. 65 """ 66 # Clear the password and relock the console 67 self.cr50.send_command('ccd testlab open') 68 self.cr50.send_command('ccd reset') 69 # Set this so when we run the open test, it won't clear the FWMP 70 self.cr50.set_cap('OpenNoTPMWipe', 'Always') 71 72 73 def cr50_check_lock_control(self, flags): 74 """Verify cr50 lock enable/disable works as intended based on flags. 75 76 If flags & self.FWMP_DEV_DISABLE_CCD_UNLOCK is true, lock disable should 77 fail. 78 79 This will only run during a test with access to the cr50 console 80 81 @param flags: A string with the FWMP settings. 82 """ 83 fwmp_disabled_ccd = not not (self.FWMP_DEV_DISABLE_CCD_UNLOCK & 84 int(flags, 16)) 85 86 start_state = self.cr50.get_ccd_info()['TPM'] 87 if ('fwmp_lock' in start_state) != fwmp_disabled_ccd: 88 raise error.TestFail('Unexpected fwmp state with flags %s' % flags) 89 90 logging.info('Flags are set to %s ccd is%s permitted', flags, 91 ' not' if fwmp_disabled_ccd else '') 92 if not self.faft_config.has_powerbutton: 93 logging.info('Can not test ccd without power button') 94 return 95 96 self.open_cr50_and_setup_ccd() 97 # Try setting password after FWMP has been created. Setting password is 98 # always allowed. Open and unlock should still be blocked. Opening cr50 99 # requires the device is in dev mode unless there's a password set. FWMP 100 # flags may disable dev mode. Set a password so we can get around this. 101 self.set_ccd_password(self.PASSWORD) 102 103 # run ccd commands with the password. ccd open and unlock should fail 104 # when the FWMP has disabled ccd. 105 self.try_set_ccd_level('open', fwmp_disabled_ccd) 106 self.try_set_ccd_level('unlock', fwmp_disabled_ccd) 107 108 # Clear the password. 109 self.open_cr50_and_setup_ccd() 110 self.cr50.send_command('ccd lock') 111 112 113 def check_fwmp(self, flags, clear_fwmp, check_lock=True): 114 """Set the flags and verify ccd lock/unlock 115 116 Args: 117 flags: A string to used set the FWMP flags. If None, skip running 118 firmware_SetFWMP. 119 clear_fwmp: True if the flags should be reset. 120 check_lock: Check ccd open 121 """ 122 if clear_fwmp: 123 self.clear_fwmp() 124 125 logging.info('setting flags to %s', flags) 126 if flags: 127 autotest.Autotest(self.host).run_test('firmware_SetFWMP', 128 flags=flags, fwmp_cleared=clear_fwmp, 129 check_client_result=True) 130 131 # Verify ccd lock/unlock with the current flags works as intended. 132 if check_lock: 133 self.cr50_check_lock_control(flags if flags else '0') 134 135 136 def run_once(self): 137 """Verify FWMP disable with different flag values""" 138 # Skip checking ccd open, so the DUT doesn't reboot 139 self.check_fwmp('0xaa00', True, check_lock=False) 140 # Verify that the flags can be changed on the same boot 141 self.check_fwmp('0xbb00', False) 142 143 # Verify setting FWMP_DEV_DISABLE_CCD_UNLOCK disables ccd 144 self.check_fwmp(hex(self.FWMP_DEV_DISABLE_CCD_UNLOCK), True) 145 146 # 0x41 is the flag setting when dev boot is disabled. Make sure that 147 # nothing unexpected happens. 148 self.check_fwmp('0x41', True) 149 150 # Clear the TPM owner and verify lock can still be enabled/disabled when 151 # the FWMP has not been created 152 self.check_fwmp(None, True) 153