• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        try:
42            self.ec.send_command("chan 0xffffffff")
43            self.restore_firmware()
44        except Exception as e:
45            logging.error("Caught exception: %s", str(e))
46        super(firmware_ECWriteProtect, self).cleanup()
47
48    def run_once(self):
49        flags = self.faft_client.bios.get_preamble_flags('a')
50        if flags & vboot.PREAMBLE_USE_RO_NORMAL == 0:
51            logging.info('The firmware USE_RO_NORMAL flag is disabled.')
52            return
53
54        logging.info("Expected EC RO boot, enable WP and reboot EC.")
55        self.check_state((self.checkers.ro_normal_checker, 'A'))
56        self.switcher.mode_aware_reboot(
57                'custom', lambda:self.set_ec_write_protect_and_reboot(True))
58
59        logging.info("Expected EC RO boot, write protected. Disable RO flag "
60                     "and reboot EC.")
61        self.check_state([(self.checkers.ro_normal_checker, 'A'),
62                          self.write_protect_checker])
63        self.faft_client.bios.set_preamble_flags('a', 0)
64        self.switcher.mode_aware_reboot(reboot_type='cold')
65
66        logging.info("Expected EC RW boot, write protected. Reboot EC by "
67                     "ectool.")
68        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
69        self.check_state(self.write_protect_checker)
70        self.switcher.mode_aware_reboot(
71                'custom', lambda:self.sync_and_ec_reboot('hard'))
72
73        logging.info("Expected EC RW boot, write protected. Restore RO "
74                     "normal flag and deactivate write protect.")
75        self.check_state((self.checkers.ro_normal_checker, ('A', True)))
76        self.check_state(self.write_protect_checker)
77        self.faft_client.bios.set_preamble_flags(('a',
78                                                  vboot.PREAMBLE_USE_RO_NORMAL))
79        self.switcher.mode_aware_reboot(
80                'custom', lambda:self.set_ec_write_protect_and_reboot(False))
81
82        logging.info("Expected EC RO boot.")
83        self.check_state((self.checkers.ro_normal_checker, 'A'))
84