faft_checkers.py revision 69a7d658b794cdbd2ada1544ba80ad798155fab6
1# Copyright (c) 2012 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 re 6import logging 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.server.cros import vboot_constants as vboot 10 11 12class FAFTCheckers(object): 13 """Class that contains FAFT checkers.""" 14 version = 1 15 16 def __init__(self, faft_framework): 17 self.faft_framework = faft_framework 18 self.faft_client = faft_framework.faft_client 19 self.faft_config = faft_framework.faft_config 20 self.fw_vboot2 = self.faft_client.system.get_fw_vboot2() 21 22 def _parse_crossystem_output(self, lines): 23 """Parse the crossystem output into a dict. 24 25 @param lines: The list of crossystem output strings. 26 @return: A dict which contains the crossystem keys/values. 27 @raise TestError: If wrong format in crossystem output. 28 29 >>> seq = FAFTSequence() 30 >>> seq._parse_crossystem_output([ \ 31 "arch = x86 # Platform architecture", \ 32 "cros_debug = 1 # OS should allow debug", \ 33 ]) 34 {'cros_debug': '1', 'arch': 'x86'} 35 >>> seq._parse_crossystem_output([ \ 36 "arch=x86", \ 37 ]) 38 Traceback (most recent call last): 39 ... 40 TestError: Failed to parse crossystem output: arch=x86 41 >>> seq._parse_crossystem_output([ \ 42 "arch = x86 # Platform architecture", \ 43 "arch = arm # Platform architecture", \ 44 ]) 45 Traceback (most recent call last): 46 ... 47 TestError: Duplicated crossystem key: arch 48 """ 49 pattern = "^([^ =]*) *= *(.*[^ ]) *# [^#]*$" 50 parsed_list = {} 51 for line in lines: 52 matched = re.match(pattern, line.strip()) 53 if not matched: 54 raise error.TestError("Failed to parse crossystem output: %s" 55 % line) 56 (name, value) = (matched.group(1), matched.group(2)) 57 if name in parsed_list: 58 raise error.TestError("Duplicated crossystem key: %s" % name) 59 parsed_list[name] = value 60 return parsed_list 61 62 def crossystem_checker(self, expected_dict, suppress_logging=False): 63 """Check the crossystem values matched. 64 65 Given an expect_dict which describes the expected crossystem values, 66 this function check the current crossystem values are matched or not. 67 68 @param expected_dict: A dict which contains the expected values. 69 @param suppress_logging: True to suppress any logging messages. 70 @return: True if the crossystem value matched; otherwise, False. 71 """ 72 succeed = True 73 lines = self.faft_client.system.run_shell_command_get_output( 74 'crossystem') 75 got_dict = self._parse_crossystem_output(lines) 76 for key in expected_dict: 77 if key not in got_dict: 78 logging.warn('Expected key %r not in crossystem result', key) 79 succeed = False 80 continue 81 if isinstance(expected_dict[key], str): 82 if got_dict[key] != expected_dict[key]: 83 message = ('Expected %r value %r but got %r' % ( 84 key, expected_dict[key], got_dict[key])) 85 succeed = False 86 else: 87 message = ('Expected %r value %r == real value %r' % ( 88 key, expected_dict[key], got_dict[key])) 89 90 elif isinstance(expected_dict[key], tuple): 91 # Expected value is a tuple of possible actual values. 92 if got_dict[key] not in expected_dict[key]: 93 message = ('Expected %r values %r but got %r' % ( 94 key, expected_dict[key], got_dict[key])) 95 succeed = False 96 else: 97 message = ('Expected %r values %r == real value %r' % ( 98 key, expected_dict[key], got_dict[key])) 99 else: 100 logging.warn('The expected value of %r is neither a str nor a ' 101 'dict: %r', key, expected_dict[key]) 102 succeed = False 103 continue 104 if not suppress_logging: 105 logging.info(message) 106 return succeed 107 108 def mode_checker(self, mode): 109 """Check the current system in the given mode. 110 111 @param mode: A string of mode, one of 'normal', 'dev', or 'rec'. 112 @return: True if the system in the given mode; otherwise, False. 113 """ 114 if mode == 'normal': 115 if self.faft_config.keyboard_dev: 116 return self.crossystem_checker( 117 {'devsw_boot': '0', 118 'mainfw_type': 'normal'}, 119 suppress_logging=True) 120 else: 121 return self.crossystem_checker( 122 {'devsw_cur': '0'}, 123 suppress_logging=True) 124 elif mode == 'dev': 125 if self.faft_config.keyboard_dev: 126 return self.crossystem_checker( 127 {'devsw_boot': '1', 128 'mainfw_type': 'developer'}, 129 suppress_logging=True) 130 else: 131 return self.crossystem_checker( 132 {'devsw_cur': '1'}, 133 suppress_logging=True) 134 elif mode == 'rec': 135 return self.crossystem_checker( 136 {'mainfw_type': 'recovery'}, 137 suppress_logging=True) 138 else: 139 raise NotImplementedError('The given mode %s not supported' % mode) 140 141 def fw_tries_checker(self, 142 expected_mainfw_act, 143 expected_fw_tried=True, 144 expected_try_count=0): 145 """Check the current FW booted and try_count 146 147 Mainly for dealing with the vboot1-specific flags fwb_tries and 148 tried_fwb fields in crossystem. In vboot2, fwb_tries is meaningless and 149 is ignored while tried_fwb is translated into fw_try_count. 150 151 @param expected_mainfw_act: A string of expected firmware, 'A', 'B', or 152 None if don't care. 153 @param expected_fw_tried: True if tried expected FW at last boot. 154 This means that mainfw_act=A,tried_fwb=0 or 155 mainfw_act=B,tried_fwb=1. Set to False if want to 156 check the opposite case for the mainfw_act. This 157 check is only performed in vboot1 as tried_fwb is 158 never set in vboot2. 159 @param expected_try_count: Number of times to try a FW slot. 160 161 @return: True if the correct boot firmware fields matched. Otherwise, 162 False. 163 """ 164 crossystem_dict = {'mainfw_act': expected_mainfw_act.upper()} 165 166 if not self.fw_vboot2: 167 if expected_mainfw_act == 'B': 168 tried_fwb_val = True 169 else: 170 tried_fwb_val = False 171 if not expected_fw_tried: 172 tried_fwb_val = not tried_fwb_val 173 crossystem_dict['tried_fwb'] = '1' if tried_fwb_val else '0' 174 175 crossystem_dict['fwb_tries'] = str(expected_try_count) 176 else: 177 crossystem_dict['fw_try_count'] = str(expected_try_count) 178 return self.crossystem_checker(crossystem_dict) 179 180 def vdat_flags_checker(self, mask, value): 181 """Check the flags from VbSharedData matched. 182 183 This function checks the masked flags from VbSharedData using crossystem 184 are matched the given value. 185 186 @param mask: A bitmask of flags to be matched. 187 @param value: An expected value. 188 @return: True if the flags matched; otherwise, False. 189 """ 190 lines = self.faft_client.system.run_shell_command_get_output( 191 'crossystem vdat_flags') 192 vdat_flags = int(lines[0], 16) 193 if vdat_flags & mask != value: 194 logging.info("Expected vdat_flags 0x%x mask 0x%x but got 0x%x", 195 value, mask, vdat_flags) 196 return False 197 return True 198 199 def ro_normal_checker(self, expected_fw=None, twostop=False): 200 """Check the current boot uses RO boot. 201 202 @param expected_fw: A string of expected firmware, 'A', 'B', or 203 None if don't care. 204 @param twostop: True to expect a TwoStop boot; False to expect a RO 205 boot. 206 @return: True if the currect boot firmware matched and used RO boot; 207 otherwise, False. 208 """ 209 crossystem_dict = {'tried_fwb': '0'} 210 if expected_fw: 211 crossystem_dict['mainfw_act'] = expected_fw.upper() 212 succeed = True 213 if not self.vdat_flags_checker(vboot.VDAT_FLAG_LF_USE_RO_NORMAL, 214 0 if twostop else vboot.VDAT_FLAG_LF_USE_RO_NORMAL): 215 succeed = False 216 if not self.crossystem_checker(crossystem_dict): 217 succeed = False 218 if self.faft_framework.check_ec_capability(suppress_warning=True): 219 expected_ec = ('RW' if twostop else 'RO') 220 if not self.ec_act_copy_checker(expected_ec): 221 succeed = False 222 return succeed 223 224 def dev_boot_usb_checker(self, dev_boot_usb=True): 225 """Check the current boot is from a developer USB (Ctrl-U trigger). 226 227 @param dev_boot_usb: True to expect an USB boot; 228 False to expect an internal device boot. 229 @return: True if the currect boot device matched; otherwise, False. 230 """ 231 return (self.crossystem_checker({'mainfw_type': 'developer'}) and 232 self.faft_client.system.is_removable_device_boot() == dev_boot_usb) 233 234 def root_part_checker(self, expected_part): 235 """Check the partition number of the root device matched. 236 237 @param expected_part: A string containing the number of the expected 238 root partition. 239 @return: True if the currect root partition number matched; 240 otherwise, False. 241 """ 242 part = self.faft_client.system.get_root_part()[-1] 243 if self.faft_framework.ROOTFS_MAP[expected_part] != part: 244 logging.info("Expected root part %s but got %s", 245 self.faft_framework.ROOTFS_MAP[expected_part], part) 246 return False 247 return True 248 249 def ec_act_copy_checker(self, expected_copy): 250 """Check the EC running firmware copy matches. 251 252 @param expected_copy: A string containing 'RO', 'A', or 'B' indicating 253 the expected copy of EC running firmware. 254 @return: True if the current EC running copy matches; otherwise, False. 255 """ 256 lines = self.faft_client.system.run_shell_command_get_output( 257 'ectool version') 258 pattern = re.compile("Firmware copy: (.*)") 259 for line in lines: 260 matched = pattern.match(line) 261 if matched: 262 if matched.group(1) == expected_copy: 263 return True 264 else: 265 logging.info("Expected EC in %s but now in %s", 266 expected_copy, matched.group(1)) 267 return False 268 logging.info("Wrong output format of 'ectool version':\n%s", 269 '\n'.join(lines)) 270 return False 271