xref: /aosp_15_r20/external/autotest/server/site_tests/firmware_WriteProtect/firmware_WriteProtect.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
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        """Initialize the test"""
19        super(firmware_WriteProtect, self).initialize(host, cmdline_args)
20        self.switcher.setup_mode('dev' if dev_mode else 'normal',
21                                 allow_gbb_force=True)
22        self._original_wp = 'on' in self.servo.get('fw_wp_state')
23
24    def cleanup(self):
25        """Cleanup the test"""
26        try:
27            if hasattr(self, '_original_wp'):
28                self.set_ap_write_protect_and_reboot(self._original_wp)
29        except Exception as e:
30            logging.error('Caught exception: %s', str(e))
31        super(firmware_WriteProtect, self).cleanup()
32
33    def run_once(self):
34        """Runs a single iteration of the test."""
35        logging.info('Enable write-protect.')
36        self.set_ap_write_protect_and_reboot(True)
37        self.check_state((self.checkers.crossystem_checker, {'wpsw_cur': '1'}))
38        logging.info('Now disable write-protect and check again.')
39        self.set_ap_write_protect_and_reboot(False)
40        self.check_state((self.checkers.crossystem_checker, {'wpsw_cur': '0'}))
41        logging.info('Enable write-protect again to check final state.')
42        self.set_ap_write_protect_and_reboot(True)
43        self.check_state((self.checkers.crossystem_checker, {'wpsw_cur': '1'}))
44