1# Copyright (c) 2011 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 datetime 6from autotest_lib.client.bin import test 7from autotest_lib.client.common_lib import error, smogcheck_tpm, smogcheck_util 8 9 10class hardware_TPMtspi(test.test): 11 """ 12 C libtspi functionality to Python for TPM testing. 13 """ 14 version = 1 15 16 def setup(self): 17 smogcheck_util.enableI2C() 18 19 def _prepareTpmController(self): 20 """Prepare a TpmController instance for use. 21 22 Returns: 23 an operational TpmControler instance, ready to use. 24 """ 25 try: 26 return smogcheck_tpm.TpmController() 27 except smogcheck_tpm.SmogcheckError as e: 28 raise error.TestFail('Error creating a TpmController: %s', e) 29 30 def run_once(self): 31 self.tpm_obj = self._prepareTpmController() 32 33 start_time = datetime.datetime.now() 34 try: 35 self.tpm_obj.setupContext() 36 self.tpm_obj.getTpmVersion() 37 self.tpm_obj.runTpmSelfTest() 38 39 # TODO(tgao): uncomment to enable. 40 #self.tpm_obj.takeTpmOwnership() 41 42 # TODO(tgao): uncomment to enable. 43 #self.tpm_obj.clearTpm() 44 45 # TODO(tgao): uncomment to enable. 46 #self.tpm_obj.setTpmActive('status') 47 48 # TODO(tgao): uncomment to enable. 49 #self.tpm_obj.setTpmActive('deactivate') 50 51 # TODO(tgao): uncomment to enable. 52 #self.tpm_obj.setTpmActive('activate') 53 54 # TODO(tgao): uncomment to enable. 55 #self.tpm_obj.setTpmActive('temp') 56 57 # TODO(tgao): uncomment to enable. 58 #self.tpm_obj.setTpmClearable('status') 59 60 # TODO(tgao): uncomment to enable. 61 #self.tpm_obj.setTpmClearable('owner') 62 63 # TODO(tgao): uncomment to enable. 64 #self.tpm_obj.setTpmClearable('force') 65 66 except smogcheck_tpm.SmogcheckError as e: 67 raise error.TestFail('Error: %r' % e) 68 finally: 69 # Close TPM context 70 if self.tpm_obj.closeContext(): 71 raise error.TestFail('Error closing tspi context') 72 73 end_time = datetime.datetime.now() 74 smogcheck_util.computeTimeElapsed(end_time, start_time) 75