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 6 7from autotest_lib.client.common_lib import error 8from autotest_lib.client.common_lib.cros import cr50_utils 9from autotest_lib.server.cros.faft.cr50_test import Cr50Test 10 11 12class firmware_Cr50PartialBoardId(Cr50Test): 13 """Verify cr50 partial board id. 14 15 16 Verify the board id flags can be set alone and the brand can be set later. 17 """ 18 version = 1 19 20 # Brand used for testing. It doesn't matter what this is. 21 DEFAULT_BRAND = 'ZZAF' 22 23 WHITELABEL_FLAGS = 0x3f80 24 OTHER_FLAGS = 0x7f7f 25 26 SUCCESS = '' 27 ERR_ALREADY_SET = 'Error 7 while setting board id' 28 ERR_BID_MISMATCH = 'Error 5 while setting board id' 29 30 31 def initialize(self, host, cmdline_args, full_args, bid=''): 32 """Generate the test flags and verify the device setup.""" 33 # Restore the original image, rlz code, and board id during cleanup. 34 super(firmware_Cr50PartialBoardId, self).initialize( 35 host, cmdline_args, full_args, restore_cr50_image=True, 36 restore_cr50_board_id=True) 37 if self.servo.main_device_is_ccd(): 38 raise error.TestNAError('Use a flex cable instead of CCD cable.') 39 40 running_ver = self.get_saved_cr50_original_version() 41 logging.info('Cr50 Version: %s', running_ver) 42 bid = running_ver[2] 43 brand = self.get_device_brand() 44 self.test_brand = brand if brand else self.DEFAULT_BRAND 45 logging.info('Test Brand: %r', self.test_brand) 46 self.image_flags = int(bid.rsplit(':', 1)[-1], 16) if bid else 0 47 # The image may have non-zero flags. Use test flags as close to the 48 # whitelabel flags as possible, but make sure they can be used with 49 # the running image. 50 self.test_flags = self.WHITELABEL_FLAGS | self.image_flags 51 self.other_flags = self.OTHER_FLAGS | self.image_flags 52 53 54 def eraseflashinfo(self): 55 """Eraseflashinfo if the board id is set.""" 56 if cr50_utils.GetChipBoardId(self.host) == cr50_utils.ERASED_CHIP_BID: 57 return 58 # Erase the board id so we can change it. 59 self.eraseflashinfo_and_restore_image() 60 61 62 def set_bid_with_dbg_image(self, bid): 63 """Use the console command on the DBG image to set the board id.""" 64 self.eraseflashinfo_and_restore_image(self.get_saved_dbg_image_path()) 65 self.cr50.set_board_id(int(bid[0], 16), bid[2]) 66 self.cr50_update(self.get_saved_cr50_original_path(), rollback=True) 67 68 69 @staticmethod 70 def get_bid_str(bid): 71 """Returns a string representation of the board id tuple.""" 72 bid_str_fields = [] 73 for field in bid: 74 if isinstance(field, str): 75 bid_str_fields.append(field) 76 elif isinstance(field, int): 77 bid_str_fields.append(hex(field)) 78 else: 79 bid_str_fields.append(str(field)) 80 return ':'.join(bid_str_fields) 81 82 83 def set_board_id_check_response(self, set_bid, expected_bid, expected_msg): 84 """Try to set the board id and verify the response. 85 86 @param set_bid: a tuple of the board id to set (brand, brand_inv, 87 flags). brand_inv is ignored, but it's included to be 88 consistent with expected_bid. The flags should be an 89 integer. 90 @param expected_bid: a tuple of the board id cr50 should have (brand, 91 brand_inv, flags). brand_inv is ignored if it's 92 None. The flags should be an integer. 93 @param expected_msg: The expected response from setting the board id 94 with gsctool. 95 @raises TestFail if the cr50 board id doesn't match expected_bid or 96 the gsctool doesn't contain expected_msg. 97 """ 98 logging.info('Set BID %s.', self.get_bid_str(set_bid)) 99 logging.info('Expect BID %s (%s)', self.get_bid_str(expected_bid), 100 expected_msg) 101 board_id_arg = '%s:0x%08x' % (set_bid[0], set_bid[2]) 102 103 result = cr50_utils.GSCTool(self.host, ['-a', '-i', board_id_arg], 104 ignore_status=True) 105 106 stderr = result.stderr.strip() 107 result_msg = stderr if stderr else result.stdout.strip() 108 logging.info('Response: BID %s %s', board_id_arg, 109 ('(%s)' % result_msg) if result_msg else '') 110 if expected_msg and expected_msg not in result_msg: 111 err = ('Unexpected response setting %r (%d): got %r expected %r' % 112 (board_id_arg, result.exit_status, result_msg, 113 expected_msg)) 114 raise error.TestFail(err) 115 cr50_utils.CheckChipBoardId(self.host, expected_bid[0], expected_bid[2], 116 board_id_inv=expected_bid[1]) 117 118 119 def run_once(self): 120 """Verify partial board id""" 121 self.eraseflashinfo() 122 # Basic check. Setting board id fails if it's already been fully set. 123 bid = (self.test_brand, None, self.test_flags) 124 self.set_board_id_check_response(bid, bid, self.SUCCESS) 125 self.set_board_id_check_response(bid, bid, self.ERR_ALREADY_SET) 126 127 self.eraseflashinfo() 128 # No special behavior for flags that are 0xffffffff. The flags cannot 129 # be changed if the board id is set even if the flags are 0xffffffff. 130 original_bid = (self.test_brand, None, cr50_utils.ERASED_BID_INT) 131 second_bid = (self.test_brand, None, self.test_flags) 132 self.set_board_id_check_response(original_bid, original_bid, 133 self.SUCCESS) 134 self.set_board_id_check_response(second_bid, original_bid, 135 self.ERR_ALREADY_SET) 136 137 self.eraseflashinfo() 138 # Flags can be set if board_id_type and board_id_type_inv are 0xffffffff 139 partial_bid = (cr50_utils.ERASED_BID_STR, None, self.test_flags) 140 stored_partial_bid = (cr50_utils.ERASED_BID_STR, 141 cr50_utils.ERASED_BID_STR, self.test_flags) 142 self.set_board_id_check_response(partial_bid, stored_partial_bid, 143 self.SUCCESS) 144 set_brand = (self.test_brand, None, self.other_flags) 145 updated_brand_bid = (self.test_brand, None, self.test_flags) 146 self.set_board_id_check_response(set_brand, updated_brand_bid, 147 self.SUCCESS) 148 149 # Setting the board id type to 0xffffffff on the console will set the 150 # type to 0xffffffff and type_inv to 0. This isn't considered a partial 151 # board id. Setting the board id a second time will fail. 152 bid = (cr50_utils.ERASED_BID_STR, '00000000', self.test_flags) 153 new_bid = (self.test_brand, None, self.other_flags) 154 self.set_bid_with_dbg_image(bid) 155 self.set_board_id_check_response(new_bid, bid, self.ERR_ALREADY_SET) 156 if not self.image_flags: 157 logging.info('Image is not board id locked. Done') 158 return 159 160 self.eraseflashinfo() 161 # Plain whitelabel flags will run on any board id locked image. 162 bid = (cr50_utils.ERASED_BID_STR, None, self.WHITELABEL_FLAGS) 163 self.set_board_id_check_response(bid, cr50_utils.ERASED_CHIP_BID, 164 self.ERR_BID_MISMATCH) 165 # Previous board id was rejected. The board id can still be set. 166 basic_bid = (self.test_brand, None, self.image_flags) 167 self.set_board_id_check_response(basic_bid, basic_bid, self.SUCCESS) 168