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
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server.cros import vboot_constants as vboot
9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
10
11
12class firmware_ECWriteProtect(FirmwareTest):
13    """
14    Servo based EC write protect test.
15    """
16    version = 1
17
18    def write_protect_checker(self):
19        """Checker that ensure the following write protect flags are set:
20            - wp_gpio_asserted
21            - ro_at_boot
22            - ro_now
23            - all_now
24        """
25        try:
26            self.ec.send_command_get_output("flashinfo",
27                  ["Flags:\s+wp_gpio_asserted\s+ro_at_boot\s+ro_now\s+all_now"])
28            return True
29        except error.TestFail:
30            # Didn't get expected flags
31            return False
32
33    def initialize(self, host, cmdline_args, dev_mode=False):
34        super(firmware_ECWriteProtect, self).initialize(host, cmdline_args,
35                                                        ec_wp=False)
36        # Don't bother if there is no Chrome EC.
37        if not self.check_ec_capability():
38            raise error.TestNAError("Nothing needs to be tested on this device")
39        self.backup_firmware()
40        self.switcher.setup_mode('dev' if dev_mode else 'normal')
41        self.ec.send_command("chan 0")
42
43    def cleanup(self):
44        try:
45            self.ec.send_command("chan 0xffffffff")
46            self.restore_firmware()
47        except Exception as e:
48            logging.error("Caught exception: %s", str(e))
49        super(firmware_ECWriteProtect, self).cleanup()
50
51    def run_once(self):
52        """Execute the main body of the test.
53        """
54        flags = self.faft_client.bios.get_preamble_flags('a')
55        if flags & vboot.PREAMBLE_USE_RO_NORMAL == 0:
56            logging.info('The firmware USE_RO_NORMAL flag is disabled.')
57            return
58
59        logging.info("Expected EC RO boot, enable WP and reboot EC.")
60        self.check_state((self.checkers.ro_normal_checker, 'A'))
61        self.switcher.mode_aware_reboot(
62                'custom', lambda:self.set_ec_write_protect_and_reboot(True))
63
64        logging.info("Expected EC RO boot, write protected. Disable RO flag "
65                     "and reboot EC.")
66        self.check_state([(self.checkers.ro_normal_checker, 'A'),
67                          self.write_protect_checker])
68        self.faft_client.bios.set_preamble_flags('a', 0)
69        self.switcher.mode_aware_reboot(reboot_type='cold')
70
71        logging.info("Expected EC RW boot, write protected. Reboot EC by "
72                     "ectool.")
73        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
74        self.check_state(self.write_protect_checker)
75        self.switcher.mode_aware_reboot(
76                'custom', lambda:self.sync_and_ec_reboot('hard'))
77
78        logging.info("Expected EC RW boot, write protected. Restore RO "
79                     "normal flag and deactivate write protect.")
80        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
81        self.check_state(self.write_protect_checker)
82        self.faft_client.bios.set_preamble_flags(('a',
83                                                  vboot.PREAMBLE_USE_RO_NORMAL))
84        self.switcher.mode_aware_reboot(
85                'custom', lambda:self.set_ec_write_protect_and_reboot(False))
86
87        logging.info("Expected EC RO boot.")
88        self.check_state((self.checkers.ro_normal_checker, 'A'))
89