1# Copyright (c) 2013 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 FirmwareTest
10
11
12class firmware_SelfSignedBoot(FirmwareTest):
13    """
14    Servo based developer mode boot only test to Self signed Kernels.
15
16    This test requires a USB disk plugged-in, which contains a Chrome OS test
17    image (built by 'build_image test'). On runtime, this test first switches
18    DUT to dev mode. When dev_boot_usb=1 and dev_boot_signed_only=1, pressing
19    Ctrl-U on developer screen should not boot the USB disk(recovery mode boot
20    should work), and when USB image is resigned with SSD keys, pressing Ctrl-U
21    should boot to the USB disk.
22    """
23    version = 1
24
25    def initialize(self, host, cmdline_args, ec_wp=None):
26        super(firmware_SelfSignedBoot, self).initialize(host, cmdline_args,
27                                                        ec_wp=ec_wp)
28        self.switcher.setup_mode('dev')
29        self.setup_usbkey(usbkey=True, host=False)
30
31        self.original_dev_boot_usb = self.faft_client.system.get_dev_boot_usb()
32        logging.info('Original dev_boot_usb value: %s',
33                     str(self.original_dev_boot_usb))
34
35        self.usb_dev = self.get_usbdisk_path_on_dut()
36        if not self.usb_dev:
37            raise error.TestError("Unable to find USB disk")
38
39    def cleanup(self):
40        self.faft_client.system.set_dev_boot_usb(self.original_dev_boot_usb)
41        self.disable_crossystem_selfsigned()
42        self.ensure_internal_device_boot()
43        self.resignimage_recoverykeys()
44        super(firmware_SelfSignedBoot, self).cleanup()
45
46    def ensure_internal_device_boot(self):
47        """Ensure internal device boot; if not, reboot into it.
48
49        If not, it may be a test failure during step 3 or 5, try to reboot
50        and press Ctrl-D to internal device boot.
51        """
52        if self.faft_client.system.is_removable_device_boot():
53            logging.info('Reboot into internal disk...')
54            self.switcher.mode_aware_reboot()
55
56    def resignimage_ssdkeys(self):
57        """Re-signing the USB image using the SSD keys."""
58        self.faft_client.system.run_shell_command(
59            '/usr/share/vboot/bin/make_dev_ssd.sh -i %s' % self.usb_dev)
60
61    def resignimage_recoverykeys(self):
62        """Re-signing the USB image using the Recovery keys."""
63        self.faft_client.system.run_shell_command(
64            '/usr/share/vboot/bin/make_dev_ssd.sh -i %s --recovery_key'
65            % self.usb_dev)
66
67    def enable_crossystem_selfsigned(self):
68        """Enable dev_boot_signed_only + dev_boot_usb."""
69        self.faft_client.system.run_shell_command(
70            'crossystem dev_boot_signed_only=1')
71        self.faft_client.system.run_shell_command('crossystem dev_boot_usb=1')
72
73    def disable_crossystem_selfsigned(self):
74        """Disable dev_boot_signed_only + dev_boot_usb."""
75        self.faft_client.system.run_shell_command(
76            'crossystem dev_boot_signed_only=0')
77        self.faft_client.system.run_shell_command('crossystem dev_boot_usb=0')
78
79    def run_once(self):
80        if (self.faft_config.has_keyboard and
81                not self.check_ec_capability(['keyboard'])):
82            raise error.TestNAError("TEST IT MANUALLY! This test can't be "
83                                    "automated on non-Chrome-EC devices.")
84
85        logging.info("Expected developer mode, set dev_boot_usb and "
86                     "dev_boot_signed_only to 1.")
87        self.check_state((self.checkers.dev_boot_usb_checker, False))
88        self.enable_crossystem_selfsigned()
89        self.switcher.mode_aware_reboot()
90
91        logging.info("Expected internal disk boot, switch to recovery mode.")
92        self.check_state((self.checkers.dev_boot_usb_checker, False,
93                          'Not internal disk boot, dev_boot_usb misbehaved'))
94        self.switcher.reboot_to_mode(to_mode='rec')
95
96        logging.info("Expected recovery boot and reboot.")
97        self.check_state((self.checkers.crossystem_checker, {
98                   'mainfw_type': 'recovery',
99                   'recovery_reason': vboot.RECOVERY_REASON['RO_MANUAL'],
100                   }))
101        self.switcher.mode_aware_reboot()
102
103        logging.info("Expected internal disk boot, resign with SSD keys.")
104        self.check_state((self.checkers.dev_boot_usb_checker, False,
105                          'Not internal disk boot, dev_boot_usb misbehaved'))
106        self.resignimage_ssdkeys()
107        self.switcher.mode_aware_reboot(wait_for_dut_up=False)
108        self.switcher.bypass_dev_boot_usb()
109        self.switcher.wait_for_client()
110
111        logging.info("Expected USB boot.")
112        self.check_state((self.checkers.dev_boot_usb_checker, True,
113                          'Not USB boot, Ctrl-U not work'))
114        self.switcher.mode_aware_reboot()
115
116        logging.info("Check and done.")
117        self.check_state((self.checkers.dev_boot_usb_checker, False))
118