• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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
5
6"""The autotest performing FW update, both EC and AP in CCD mode."""
7import logging
8import re
9
10from autotest_lib.client.common_lib import error
11from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
12
13
14class firmware_ECRestoreFW(FirmwareTest):
15    """A test that restores a machine from an incrrect FW to backup."""
16
17    version = 1
18
19    def initialize(self, host, cmdline_args, full_args):
20        """Initialize the test and pick a fake board to use for corruption. """
21        super(firmware_ECRestoreFW, self).initialize(host, cmdline_args,
22                                                   full_args)
23
24        # Don't bother if there is no Chrome EC.
25        if not self.check_ec_capability():
26            raise error.TestNAError('Nothing needs to be tested on this device')
27
28        self.local_tarball = None
29        self.build = None
30        # find if "local_tarball" was given in the command line arguments.
31        for arg in cmdline_args:
32            match = re.search(r'^local_tarball=(.+)', arg)
33            if match:
34                self.local_tarball = match.group(1)
35                logging.info('Use local tarball %s', self.local_tarball)
36                break
37        else:
38            # Get the latest firmware release from the server.
39            # Even this test uses a fake EC image, it needs to download
40            # the release to get some subsidiary binary (like npcx_monitor.bin).
41            platform = self.faft_config.platform
42
43            # Get the parent (a.k.a. reference board or baseboard), and hand it
44            # to get_latest_release_version so that it can use it in search as
45            # secondary candidate. For example, bob doesn't have its own release
46            # directory, but its parent, gru does.
47            parent = getattr(self.faft_config, 'parent', None)
48
49            self.build = host.get_latest_release_version(platform, parent)
50
51            if not self.build:
52                raise error.TestError(
53                        'Cannot locate the latest release for %s' % platform)
54            logging.info('Will use the build %s', self.build)
55        self.backup_firmware()
56
57    def cleanup(self):
58        """The method called by the control file to start the test.
59
60        Raises:
61          TestFail: if the firmware restoration fails.
62        """
63        try:
64            if self.is_firmware_saved():
65                self.restore_firmware()
66        except Exception as e:
67            raise error.TestFail('FW Restoration failed: %s' % str(e))
68        finally:
69            super(firmware_ECRestoreFW, self).cleanup()
70
71    def run_once(self, host):
72        """The method called by the control file to start the test.
73
74        Args:
75          host:  a CrosHost object of the machine to update.
76        """
77
78        try:
79            host.firmware_install(build=self.build,
80                                  dest=self.resultsdir,
81                                  local_tarball=self.local_tarball,
82                                  install_ec=True,
83                                  install_bios=False,
84                                  corrupt_ec=True)
85        except error.TestError as e:
86            # It failed before the test attempts to install firmware.
87            # It could be either devserver timeout or servo device error.
88            # Let this test fail in those cases.
89            raise e
90        except Exception as e:
91            # Nothing can be guaranteed with the firmware corruption with wrong
92            # firmware. Let's not this test fail for that.
93            logging.info('Caught an exception: %s', e)
94