• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2017 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.faft.firmware_test import FirmwareTest
9
10
11class firmware_RecoveryCacheBootKeys(FirmwareTest):
12    """
13    This test ensures that when booting to recovery mode the device will use the
14    cache instead training memory every boot.
15    """
16    version = 1
17    NEEDS_SERVO_USB = True
18
19    # Message in older mrc_cache driver code
20    USED_CACHE_MSG1 = ('MRC: Hash comparison successful. '
21                       'Using data from RECOVERY_MRC_CACHE')
22    # Message in newer mrc_cache driver code
23    USED_CACHE_MSG2 = ('MRC: Hash idx 0x100b comparison successful.')
24    REBUILD_CACHE_MSG = "MRC: cache data 'RECOVERY_MRC_CACHE' needs update."
25    RECOVERY_CACHE_SECTION = 'RECOVERY_MRC_CACHE'
26    FIRMWARE_LOG_CMD = 'cbmem -1' + ' | grep ' + REBUILD_CACHE_MSG[:3]
27    RECOVERY_REASON_REBUILD_CMD = 'crossystem recovery_request=0xC4'
28
29    def initialize(self, host, cmdline_args, dev_mode=False):
30        super(firmware_RecoveryCacheBootKeys, self).initialize(
31                host, cmdline_args)
32
33        if not self.faft_config.rec_force_mrc:
34            raise error.TestNAError('DUT cannot force memory training.')
35
36        self.client = host
37        self.dev_mode = dev_mode
38        self.switcher.setup_mode('dev' if dev_mode else 'normal')
39        self.setup_usbkey(usbkey=True, host=False)
40
41    def cleanup(self):
42        super(firmware_RecoveryCacheBootKeys, self).cleanup()
43        self.switcher.simple_reboot()
44
45    def boot_to_recovery(self, rebuild_mrc_cache=False):
46        """Boots the device into recovery mode."""
47        if rebuild_mrc_cache:
48            self.switcher.reboot_to_mode(to_mode='rec_force_mrc')
49        else:
50            self.switcher.reboot_to_mode(to_mode='rec')
51
52        self.check_state((self.checkers.crossystem_checker, {
53                'mainfw_type': 'recovery'
54        }))
55
56    def cache_exist(self):
57        """Checks the firmware log to ensure that the recovery cache exists.
58
59        @return True if cache exists
60        """
61        logging.info("Checking if device has RECOVERY_MRC_CACHE")
62
63        # If flashrom can read the section, this means it exists.
64        command = ('flashrom -p host -r -i %s:/dev/null'
65                   % self.RECOVERY_CACHE_SECTION)
66
67        return self.faft_client.system.run_shell_command_check_output(
68            command, 'SUCCESS')
69
70    def check_cache_used(self):
71        """Checks the firmware log to ensure that the recovery cache was used
72        during recovery boot.
73
74        @return True if cache used
75        """
76        logging.info('Checking if cache was used.')
77
78        return (self.faft_client.system.run_shell_command_check_output(
79                self.FIRMWARE_LOG_CMD, self.USED_CACHE_MSG1)
80                or self.faft_client.system.run_shell_command_check_output(
81                        self.FIRMWARE_LOG_CMD, self.USED_CACHE_MSG2))
82
83    def check_cache_rebuilt(self):
84        """Checks the firmware log to ensure that the recovery cache was rebuilt
85        during recovery boot.
86
87        @return True if cache rebuilt
88        """
89        logging.info('Checking if cache was rebuilt.')
90
91        return self.faft_client.system.run_shell_command_check_output(
92                self.FIRMWARE_LOG_CMD, self.REBUILD_CACHE_MSG)
93
94    def run_once(self):
95        """Runs a single iteration of the test."""
96        if not self.cache_exist():
97            raise error.TestNAError('No RECOVERY_MRC_CACHE was found on DUT.')
98
99        logging.info('Ensure we\'ve done memory training.')
100        self.boot_to_recovery()
101
102        # With EFS, when the EC is in RO and we do a recovery boot, it
103        # causes the EC to do a soft reboot, which will in turn cause
104        # a PMIC reset (double reboot) on SKL/KBL power architectures.
105        # This causes us to lose the request to do MRC training for
106        # this test.  The solution is to make sure that the EC is in
107        # RW before doing a recovery boot to ensure that the double
108        # reboot does not occur and information/requests are not lost.
109        self.switcher.simple_reboot('cold')
110
111        logging.info('Checking recovery boot.')
112        self.boot_to_recovery()
113
114        if not self.check_cache_used():
115            raise error.TestFail('Recovery Cache was not used.')
116
117        self.switcher.simple_reboot('cold')
118
119        logging.info('Checking recovery boot with forced MRC cache training.')
120        self.boot_to_recovery(rebuild_mrc_cache=True)
121        self.switcher.wait_for_client()
122
123        if not self.check_cache_rebuilt():
124            raise error.TestFail('Recovery Cache was not rebuilt.')
125
126        logging.info('Reboot out of Recovery')
127        self.switcher.simple_reboot()
128