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 5 import logging 6 import time 7 8 from autotest_lib.client.common_lib import error 9 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 10 from autotest_lib.server.cros import vboot_constants as vboot 11 12 13 class firmware_SoftwareSync(FirmwareTest): 14 """ 15 Servo based EC software sync test. 16 """ 17 version = 1 18 19 def initialize(self, host, cmdline_args, dev_mode=False): 20 # This test tries to corrupt EC firmware. Should disable EC WP. 21 super(firmware_SoftwareSync, self).initialize(host, cmdline_args, 22 ec_wp=False) 23 # In order to test software sync, it must be enabled. 24 self.clear_set_gbb_flags(vboot.GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC, 0) 25 self.backup_firmware() 26 self.switcher.setup_mode('dev' if dev_mode else 'normal') 27 self.setup_usbkey(usbkey=False) 28 self.setup_rw_boot() 29 self.dev_mode = dev_mode 30 31 def cleanup(self): 32 try: 33 if self.is_firmware_saved(): 34 self.restore_firmware() 35 except Exception as e: 36 logging.error("Caught exception: %s", str(e)) 37 super(firmware_SoftwareSync, self).cleanup() 38 39 def record_hash(self): 40 """Record current EC hash.""" 41 self._ec_hash = self.faft_client.ec.get_active_hash() 42 logging.info("Stored EC hash: %s", self._ec_hash) 43 44 def corrupt_active_rw(self): 45 """Corrupt the active RW portion.""" 46 section = 'rw' 47 try: 48 if self.servo.get_ec_active_copy() == 'RW_B': 49 section = 'rw_b' 50 except error.TestFail: 51 # Skip the failure, as ec_active_copy is new. 52 # TODO(waihong): Remove this except clause. 53 pass 54 logging.info("Corrupt the EC section: %s", section) 55 self.faft_client.ec.corrupt_body(section) 56 57 def software_sync_checker(self): 58 """Check EC firmware is restored by software sync.""" 59 ec_hash = self.faft_client.ec.get_active_hash() 60 logging.info("Current EC hash: %s", ec_hash) 61 if self._ec_hash != ec_hash: 62 return False 63 return self.checkers.ec_act_copy_checker('RW') 64 65 def wait_software_sync_and_boot(self): 66 """Wait for software sync to update EC.""" 67 if self.dev_mode: 68 time.sleep(self.faft_config.software_sync_update + 69 self.faft_config.firmware_screen) 70 self.servo.ctrl_d() 71 else: 72 time.sleep(self.faft_config.software_sync_update) 73 74 def run_once(self): 75 logging.info("Corrupt EC firmware RW body.") 76 self.check_state((self.checkers.ec_act_copy_checker, 'RW')) 77 self.record_hash() 78 self.corrupt_active_rw() 79 logging.info("Reboot AP, check EC hash, and software sync it.") 80 self.switcher.simple_reboot(reboot_type='warm') 81 self.wait_software_sync_and_boot() 82 self.switcher.wait_for_client() 83 84 logging.info("Expect EC in RW and RW is restored.") 85 self.check_state(self.software_sync_checker) 86