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 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 ConnectionError 10from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 11 12 13class 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 super(firmware_InvalidUSB, self).initialize(host, cmdline_args) 32 self.servo.switch_usbkey('host') 33 usb_dev = self.servo.probe_host_usb_dev() 34 self.assert_test_image_in_usb_disk(usb_dev) 35 self.corrupt_usb_kernel(usb_dev) 36 self.switcher.setup_mode('normal') 37 self.servo.switch_usbkey('dut') 38 39 def cleanup(self): 40 try: 41 self.restore_usb() 42 except Exception as e: 43 logging.error("Caught exception: %s", str(e)) 44 super(firmware_InvalidUSB, self).cleanup() 45 46 def run_once(self): 47 logging.info("Turn on the recovery boot. Remove and insert the" 48 "corrupted USB stick, a boot failure is expected." 49 "Restore the USB image and boot it again.") 50 self.check_state((self.checkers.crossystem_checker, { 51 'devsw_boot': '0', 52 'mainfw_type': 'normal', 53 })) 54 self.switcher.reboot_to_mode(to_mode='rec', wait_for_dut_up=False) 55 logging.info('Wait to ensure the USB image is unable to boot...') 56 try: 57 self.switcher.wait_for_client(timeout=self.faft_config.usb_image_boot_timeout) 58 raise error.TestFail('Should not boot from the invalid USB image.') 59 except ConnectionError: 60 logging.info( 61 'The USB image is surely unable to boot. Restore it and try...') 62 63 self.restore_usb() 64 self.servo.switch_usbkey('dut') 65 self.switcher.wait_for_client() 66 67 logging.info("Expected to boot the restored USB image and reboot.") 68 self.check_state((self.checkers.crossystem_checker, { 69 'mainfw_type': 'recovery', 70 'recovery_reason': vboot.RECOVERY_REASON['RO_MANUAL'], 71 })) 72 self.switcher.mode_aware_reboot() 73 74 logging.info("Expected to normal boot and done.") 75 self.check_state((self.checkers.crossystem_checker, { 76 'devsw_boot': '0', 77 'mainfw_type': 'normal', 78 })) 79