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
6
7from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
8
9
10class firmware_WriteProtect(FirmwareTest):
11    """
12    This test checks whether the hardware write-protect state reported by
13    crossystem matches the real write-protect state driven by Servo.
14    """
15    version = 1
16
17    def initialize(self, host, cmdline_args, dev_mode=False):
18        super(firmware_WriteProtect, self).initialize(host, cmdline_args)
19        self.switcher.setup_mode('dev' if dev_mode else 'normal')
20        self._original_wp = self.servo.get('fw_wp') == 'on'
21
22    def cleanup(self):
23        try:
24            if hasattr(self, '_original_wp'):
25              self.set_hardware_write_protect(self._original_wp)
26        except Exception as e:
27            logging.error('Caught exception: %s', str(e))
28        super(firmware_WriteProtect, self).cleanup()
29
30    def run_once(self):
31        logging.info('Force write-protect on and reboot for a clean slate.')
32        self.set_hardware_write_protect(True)
33        self.switcher.mode_aware_reboot()
34        self.check_state((self.checkers.crossystem_checker, {
35                              'wpsw_boot': '1',
36                              'wpsw_cur': '1',
37                          }))
38        logging.info('Now disable write-protect and check again.')
39        self.set_hardware_write_protect(False)
40        self.check_state((self.checkers.crossystem_checker, {
41                              'wpsw_boot': '1',
42                              'wpsw_cur': '0',
43                          }))
44        logging.info('Reboot so WP change takes effect for wpsw_boot.')
45        self.switcher.mode_aware_reboot()
46        self.check_state((self.checkers.crossystem_checker, {
47                              'wpsw_boot': '0',
48                              'wpsw_cur': '0',
49                          }))
50        logging.info('Enable write-protect again to observe final transition.')
51        self.set_hardware_write_protect(True)
52        self.check_state((self.checkers.crossystem_checker, {
53                              'wpsw_boot': '0',
54                              'wpsw_cur': '1',
55                          }))
56