Home | History | Annotate | Download | only in firmware_InvalidUSB
      1 # Copyright (c) 2011 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 import logging
      6 
      7 from autotest_lib.client.common_lib import error
      8 from autotest_lib.server.cros import vboot_constants as vboot
      9 from autotest_lib.server.cros.faft.firmware_test import ConnectionError
     10 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
     11 
     12 
     13 class firmware_InvalidUSB(FirmwareTest):
     14     """
     15     Servo based booting an invalid USB image test.
     16 
     17     This test requires a USB disk plugged-in, which contains a Chrome OS test
     18     image (built by "build_image --test"). On runtime, this test corrupts the
     19     USB image and tries to boot into it. A failure is expected. It then
     20     restores the USB image and boots into it again.
     21     """
     22     version = 1
     23 
     24     def restore_usb(self):
     25         """Restore the USB image. USB plugs/unplugs happen in this method."""
     26         self.servo.switch_usbkey('host')
     27         usb_dev = self.servo.probe_host_usb_dev()
     28         self.restore_usb_kernel(usb_dev)
     29 
     30     def initialize(self, host, cmdline_args):
     31         """Initialize the test"""
     32         super(firmware_InvalidUSB, self).initialize(host, cmdline_args)
     33         self.servo.switch_usbkey('host')
     34         usb_dev = self.servo.probe_host_usb_dev()
     35         self.assert_test_image_in_usb_disk(usb_dev)
     36         self.corrupt_usb_kernel(usb_dev)
     37         self.switcher.setup_mode('normal')
     38         self.servo.switch_usbkey('dut')
     39 
     40     def cleanup(self):
     41         """Cleanup the test"""
     42         try:
     43             self.restore_usb()
     44         except Exception as e:
     45             logging.error("Caught exception: %s", str(e))
     46         super(firmware_InvalidUSB, self).cleanup()
     47 
     48     def run_once(self):
     49         """Main test logic"""
     50         logging.info("Turn on the recovery boot. Remove and insert the"
     51                      "corrupted USB stick, a boot failure is expected."
     52                      "Restore the USB image and boot it again.")
     53         self.check_state((self.checkers.crossystem_checker, {
     54                           'devsw_boot': '0',
     55                           'mainfw_type': 'normal',
     56                           }))
     57 
     58         # Switch servo v4 (if present) as a SNK. Make sure USB key is bootable.
     59         self.set_servo_v4_role_to_snk()
     60 
     61         self.switcher.reboot_to_mode(to_mode='rec', wait_for_dut_up=False)
     62         logging.info('Wait to ensure the USB image is unable to boot...')
     63         try:
     64             self.switcher.wait_for_client(
     65                     timeout=self.faft_config.usb_image_boot_timeout)
     66             raise error.TestFail('Should not boot from the invalid USB image.')
     67         except ConnectionError:
     68             logging.info(
     69                 'The USB image is surely unable to boot. Restore it and try...')
     70 
     71         self.restore_usb()
     72         self.servo.switch_usbkey('dut')
     73         self.switcher.wait_for_client()
     74 
     75         logging.info("Expected to boot the restored USB image and reboot.")
     76         self.check_state((self.checkers.crossystem_checker, {
     77                           'mainfw_type': 'recovery',
     78                           'recovery_reason': vboot.RECOVERY_REASON['RO_MANUAL'],
     79                           }))
     80         self.switcher.mode_aware_reboot()
     81 
     82         logging.info("Expected to normal boot and done.")
     83         self.check_state((self.checkers.crossystem_checker, {
     84                           'devsw_boot': '0',
     85                           'mainfw_type': 'normal',
     86                           }))
     87