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 # A set of fake board candidates per ec type. 20 FAKE_BOARD_DICT = {'npcx':['coral', 'reef'], 21 'stm32':['samus', 'nami'], 22 'it83':['dragonegg', 'waddledee']} 23 24 def initialize(self, host, cmdline_args, full_args): 25 """Initialize the test and pick a fake board to use for corruption. """ 26 super(firmware_ECRestoreFW, self).initialize(host, cmdline_args, 27 full_args) 28 29 # Don't bother if there is no Chrome EC. 30 if not self.check_ec_capability(): 31 raise error.TestNAError('Nothing needs to be tested on this device') 32 33 self.board_as = None 34 # find if "board_as" was given in the command line arguments. 35 for arg in cmdline_args: 36 match = re.search(r'^board_as=(.+)', arg) 37 if match: 38 self.board_as = match.group(1) 39 break 40 else: 41 # if "board_as" was not given, then pick one from FAKE_BOARD_DICT. 42 ec_chip = self.servo.get('ec_chip') 43 if 'stm32' in ec_chip: 44 ec_type = 'stm32' 45 elif 'it83' in ec_chip: 46 ec_type = 'it83' 47 else: 48 ec_type = 'npcx' 49 50 for board in self.FAKE_BOARD_DICT[ec_type]: 51 if board not in self.faft_config.platform: 52 self.board_as = board 53 break 54 55 if not self.board_as: 56 raise error.TestError('fake board is not selected.') 57 58 logging.info('A fake board to use for corruption: %s', self.board_as) 59 60 self.backup_firmware() 61 62 def cleanup(self): 63 """The method called by the control file to start the test. 64 65 Raises: 66 TestFail: if the firmware restoration fails. 67 """ 68 try: 69 if self.is_firmware_saved(): 70 self.restore_firmware() 71 except Exception as e: 72 raise error.TestFail('FW Restoration failed: %s' % str(e)) 73 finally: 74 super(firmware_ECRestoreFW, self).cleanup() 75 76 def run_once(self, host): 77 """The method called by the control file to start the test. 78 79 Args: 80 host: a CrosHost object of the machine to update. 81 """ 82 83 logging.info('Downloading a firmware of %s', self.board_as) 84 value = host.get_latest_release_version(self.board_as) 85 if not value: 86 raise error.TestError('Cannot locate the latest release for %s' % 87 self.board_as) 88 89 try: 90 host.firmware_install(build=value, dest=self.resultsdir, 91 install_ec=True, install_bios=False, 92 board_as=self.board_as) 93 except error.TestError as e: 94 # It failed before the test attempts to install firmware. 95 # It could be either devserver timeout or servo device error. 96 # Let this test fail in those cases. 97 raise e 98 except Exception as e: 99 # Nothing can be guaranteed with the firmware corruption with wrong 100 # firmware. Let's not this test fail for that. 101 logging.info('Caught an exception: %s', e) 102