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        self.backup_firmware()
37        self.switcher.setup_mode('dev' if dev_mode else 'normal')
38        self.ec.send_command("chan 0")
39
40    def cleanup(self):
41        self.ec.send_command("chan 0xffffffff")
42        self.restore_firmware()
43        super(firmware_ECWriteProtect, self).cleanup()
44
45    def run_once(self):
46        flags = self.faft_client.bios.get_preamble_flags('a')
47        if flags & vboot.PREAMBLE_USE_RO_NORMAL == 0:
48            logging.info('The firmware USE_RO_NORMAL flag is disabled.')
49            return
50
51        logging.info("Expected EC RO boot, enable WP and reboot EC.")
52        self.check_state((self.checkers.ro_normal_checker, 'A'))
53        self.switcher.mode_aware_reboot(
54                'custom', lambda:self.set_ec_write_protect_and_reboot(True))
55
56        logging.info("Expected EC RO boot, write protected. Disable RO flag "
57                     "and reboot EC.")
58        self.check_state([(self.checkers.ro_normal_checker, 'A'),
59                          self.write_protect_checker])
60        self.faft_client.bios.set_preamble_flags('a', 0)
61        self.switcher.mode_aware_reboot(reboot_type='cold')
62
63        logging.info("Expected EC RW boot, write protected. Reboot EC by "
64                     "ectool.")
65        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
66        self.check_state(self.write_protect_checker)
67        self.switcher.mode_aware_reboot(
68                'custom', lambda:self.sync_and_ec_reboot('hard'))
69
70        logging.info("Expected EC RW boot, write protected. Restore RO "
71                     "normal flag and deactivate write protect.")
72        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
73        self.check_state(self.write_protect_checker)
74        self.faft_client.bios.set_preamble_flags(('a',
75                                                  vboot.PREAMBLE_USE_RO_NORMAL))
76        self.switcher.mode_aware_reboot(
77                'custom', lambda:self.set_ec_write_protect_and_reboot(False))
78
79        logging.info("Expected EC RO boot.")
80        self.check_state((self.checkers.ro_normal_checker, 'A'))
81