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            self.set_hardware_write_protect(self._original_wp)
25        except Exception as e:
26            logging.error('Caught exception: %s', str(e))
27        super(firmware_WriteProtect, self).cleanup()
28
29    def run_once(self):
30        logging.info('Force write-protect on and reboot for a clean slate.')
31        self.set_hardware_write_protect(True)
32        self.switcher.mode_aware_reboot()
33        self.check_state((self.checkers.crossystem_checker, {
34                              'wpsw_boot': '1',
35                              'wpsw_cur': '1',
36                          }))
37        logging.info('Now disable write-protect and check again.')
38        self.set_hardware_write_protect(False)
39        self.check_state((self.checkers.crossystem_checker, {
40                              'wpsw_boot': '1',
41                              'wpsw_cur': '0',
42                          }))
43        logging.info('Reboot so WP change takes effect for wpsw_boot.')
44        self.switcher.mode_aware_reboot()
45        self.check_state((self.checkers.crossystem_checker, {
46                              'wpsw_boot': '0',
47                              'wpsw_cur': '0',
48                          }))
49        logging.info('Enable write-protect again to observe final transition.')
50        self.set_hardware_write_protect(True)
51        self.check_state((self.checkers.crossystem_checker, {
52                              'wpsw_boot': '0',
53                              'wpsw_cur': '1',
54                          }))
55