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 logging
6import time
7
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_SoftwareSync(FirmwareTest):
12    """
13    Servo based EC software sync test.
14    """
15    version = 1
16
17    def initialize(self, host, cmdline_args, dev_mode=False):
18        # This test tries to corrupt EC firmware. Should disable EC WP.
19        super(firmware_SoftwareSync, self).initialize(host, cmdline_args,
20                                                      ec_wp=False)
21        self.backup_firmware()
22        self.switcher.setup_mode('dev' if dev_mode else 'normal')
23        self.setup_usbkey(usbkey=False)
24        self.setup_rw_boot()
25        self.dev_mode = dev_mode
26
27    def cleanup(self):
28        self.restore_firmware()
29        super(firmware_SoftwareSync, self).cleanup()
30
31    def record_hash_and_corrupt(self):
32        """Record current EC hash and corrupt EC firmware."""
33        self._ec_hash = self.faft_client.ec.get_firmware_sha()
34        logging.info("Stored EC hash: %s", self._ec_hash)
35        self.faft_client.ec.corrupt_body('rw')
36
37    def software_sync_checker(self):
38        """Check EC firmware is restored by software sync."""
39        ec_hash = self.faft_client.ec.get_firmware_sha()
40        logging.info("Current EC hash: %s", self._ec_hash)
41        if self._ec_hash != ec_hash:
42            return False
43        return self.checkers.ec_act_copy_checker('RW')
44
45    def wait_software_sync_and_boot(self):
46        """Wait for software sync to update EC."""
47        if self.dev_mode:
48            time.sleep(self.faft_config.software_sync_update +
49                       self.faft_config.firmware_screen)
50            self.servo.ctrl_d()
51        else:
52            time.sleep(self.faft_config.software_sync_update)
53
54    def run_once(self):
55        logging.info("Corrupt EC firmware RW body.")
56        self.check_state((self.checkers.ec_act_copy_checker, 'RW'))
57        self.record_hash_and_corrupt()
58        self.sync_and_ec_reboot()
59        self.wait_software_sync_and_boot()
60        self.switcher.wait_for_client()
61
62        logging.info("Expect EC in RW and RW is restored.")
63        self.check_state(self.software_sync_checker)
64