1# Copyright 2019 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 6import re 7import time 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 11 12 13class firmware_Cr50TpmManufactured(FirmwareTest): 14 """Check if the TPM is manufactured.""" 15 version = 1 16 17 NUM_RESETS = 5 18 MANUFACTURED_RE = 'tpm_manufactured:( NOT)? manufactured' 19 20 def run_once(self): 21 """Check if the TPM is manufactured.""" 22 if not hasattr(self, 'cr50'): 23 raise error.TestNAError('Test can only be run on devices with ' 24 'access to the Cr50 console') 25 26 # Signing is different in dev signed images. This test doesn't really 27 # apply. 28 if not self.cr50.using_prod_rw_keys(): 29 raise error.TestNAError('Running dev signed image') 30 31 # Reset the AP a couple of times in case the console drops any 32 # characters. 33 for i in range(self.NUM_RESETS): 34 self.servo.get_power_state_controller().warm_reset() 35 time.sleep(10) 36 # Use cr50 uart capture to check the cr50 console messages. We could 37 # send sysrst pulse and check the cr50 response, but that would require 38 # opening cr50 which may not be possible if the TPM isn't manufactured. 39 # We want to make this as accessible as possible and checking the 40 # captured uart is the best way to do that. 41 self._record_uart_capture() 42 cr50_uart_file = self.servo.get_uart_logfile('cr50') 43 if not cr50_uart_file: 44 raise error.TestNAError('No saved uart file') 45 with open(cr50_uart_file, 'r') as f: 46 contents = f.read() 47 logging.debug('Cr50 reset logs:\n%s', contents) 48 49 found = re.findall(self.MANUFACTURED_RE, contents) 50 # The uart won't be captured if someone has the cr50 console open. 51 if not found: 52 raise error.TestNAError('Could not find %r. Close cr50 console' % 53 self.MANUFACTURED_RE) 54 logging.debug('Matches for tpm_manufactured: %r', found) 55 res = set(found) 56 if len(res) > 1: 57 raise error.TestError('Found more than one manufacture setting') 58 manufactured_state = res.pop() 59 logging.info('tpm%s manufactured', manufactured_state) 60 if ' NOT' == manufactured_state: 61 raise error.TestFail('TPM not manufactured') 62