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 5import logging 6import re 7import time 8 9from autotest_lib.client.common_lib import error, utils 10from autotest_lib.client.common_lib.cros import cr50_utils 11from autotest_lib.server.cros.faft.cr50_test import Cr50Test 12 13 14class firmware_Cr50GetName(Cr50Test): 15 """Verify cr50-get-name.sh 16 17 Verify cr50-get-name sets the correct board id and flags based on the 18 given stage. 19 """ 20 version = 1 21 22 GET_NAME_SCRIPT = '/usr/share/cros/cr50-get-name.sh' 23 # This translates to 'TEST' 24 TEST_BRAND = 0x54455354 25 MAX_VAL = 0xffffffff 26 27 28 def initialize(self, host, cmdline_args, full_args={}): 29 # Restore the original image, rlz code, and board id during cleanup. 30 super(firmware_Cr50GetName, self).initialize(host, cmdline_args, 31 full_args, restore_cr50_image=True, restore_cr50_board_id=True) 32 33 if not self.host.path_exists(self.GET_NAME_SCRIPT): 34 raise error.TestNAError('Device does not have "cr50-get-name"') 35 36 efi_path = self.get_saved_eraseflashinfo_image_path() 37 38 self.make_rootfs_writable() 39 cr50_utils.InstallImage(self.host, efi_path, cr50_utils.CR50_PROD) 40 cr50_utils.InstallImage(self.host, efi_path, cr50_utils.CR50_PREPVT) 41 42 # Update to the eraseflashinfo image so we can erase the board id after 43 # we set it. This test is verifying cr50-get-name, so it is ok if cr50 44 # is running a non-prod image. 45 self.cr50_update(self.get_saved_dbg_image_path()) 46 self.cr50_update(efi_path, rollback=True) 47 48 # Stop trunksd so it wont interfere with the update 49 cr50_utils.StopTrunksd(self.host) 50 51 # Get the current cr50 update messages. The test will keep track of the 52 # last message and separate the current output from actual test results. 53 self.get_result() 54 55 56 def cleanup(self): 57 """Reset the DUT to restore trunksd.""" 58 try: 59 self.host.reset_via_servo() 60 finally: 61 super(firmware_Cr50GetName, self).cleanup() 62 63 64 def get_result(self): 65 """Return the new cr50 update messages from /var/log/messages""" 66 # Get the cr50 messages 67 result = self.host.run('grep cr50 /var/log/messages').stdout.strip() 68 69 if hasattr(self, '_last_message'): 70 result = result.rsplit(self._last_message, 1)[-1] 71 72 # Save the last line. It will be used to separate the current results 73 # from later runs. 74 self._last_message = result.rsplit('\n', 1)[-1] 75 logging.debug('last cr50 update message: "%s"', self._last_message) 76 return result 77 78 79 def get_expected_result_re(self, brand, flags, erased): 80 """Return the expected update message re given the test flags 81 82 Args: 83 brand: The board id value to test. 84 flags: The flag value to test. 85 erased: True if the board id is erased 86 87 Returns: 88 A string with info that must be found in /var/log/messages for valid 89 update results. 90 """ 91 expected_result = [] 92 93 if erased: 94 board_id = 'ffffffff:ffffffff:ffffffff' 95 # If the board id is erased, the device should update to the prod 96 # image. 97 ext = 'prod' 98 expected_result.append('board ID is erased using prod image') 99 else: 100 board_id = '%08x:%08x:%08x' % (brand, brand ^ self.MAX_VAL, flags) 101 ext = 'prepvt' if flags & 0x10 else 'prod' 102 103 flag_str = board_id.rsplit(':', 1)[-1] 104 105 expected_result.append("board_id: '%s' board_flags: '0x%s', extension: " 106 "'%s'" % (board_id, flag_str, ext)) 107 expected_result.append('hashing /opt/google/cr50/firmware/cr50.bin.%s' % 108 ext) 109 return '(%s)' % '\n.*'.join(expected_result) 110 111 112 def check_result(self, brand, flags, erased): 113 """Verify the expected result string is found in the update messages 114 115 Args: 116 brand: The board id value to test. 117 flags: The flag value to test. 118 erased: True if the board id is erased 119 120 Raises: 121 TestFail if the expected result message did not match the update 122 output 123 """ 124 expected_result_re = self.get_expected_result_re(brand, flags, erased) 125 result = self.get_result() 126 match = re.search(expected_result_re, result) 127 128 logging.debug('EXPECT: %s', expected_result_re) 129 logging.debug('GOT: %s', result) 130 131 if not match: 132 raise error.TestFail('Unexpected result during update with %s' % 133 ('erased board id' if erased else '%x:%x' % (brand, flags))) 134 135 logging.info('FOUND UPDATE RESULT:\n%s', match.groups()[0]) 136 137 138 def cr50_update_is_running(self): 139 """Returns True if cr50-update is running on the host""" 140 time.sleep(1) 141 status = self.host.run('status cr50-update').stdout 142 logging.info('cr50-update status: %s', status) 143 return 'running' in status 144 145 146 def run_update(self, brand, flags, clear_bid=False): 147 """Set the board id then run cr50-update 148 149 Args: 150 brand: The board id int to test. 151 flags: The flag int to test. 152 clear_bid: True if the board id should be erased and not reset. 153 """ 154 if not self.cr50.eraseflashinfo(): 155 raise error.TestError('Unable to erase the board id') 156 157 if not clear_bid: 158 cr50_utils.SetChipBoardId(self.host, brand, flags) 159 160 # Get the current cr50 update messages. The test will keep track of the 161 # last message and separate the current output from actual test results. 162 self.get_result() 163 # Run the update script script 164 self.host.run('start cr50-update') 165 utils.wait_for_value(self.cr50_update_is_running, expected_value=False, 166 timeout_sec=30) 167 168 # Make sure cr50 used the right image. 169 self.check_result(brand, flags, clear_bid) 170 171 172 def run_once(self): 173 """Verify cr50-get-name.sh""" 174 # Test the MP flags 175 self.run_update(self.TEST_BRAND, 0x7f00) 176 177 # Test the pre-PVT flags 178 self.run_update(self.TEST_BRAND, 0x7f10) 179 180 # Erase the board id 181 self.run_update(0, 0, clear_bid=True) 182 183 # Make sure the script can tell the difference between an erased board 184 # id and one set to 0xffffffff:0xffffffff. 185 self.run_update(self.MAX_VAL, self.MAX_VAL) 186