blob: 87a34087895190eead0c7b17b799c12f551f91c7 [file] [log] [blame]
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001# 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
Tom Wai-Hong Tamfda76e22012-08-08 17:19:10 +08005import ctypes
Vic Yangb4e3e742012-06-02 13:17:38 +08006import fdpexpect
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08007import logging
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08008import os
Vic Yangb4e3e742012-06-02 13:17:38 +08009import pexpect
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080010import re
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080011import sys
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +080012import tempfile
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080013import time
14import xmlrpclib
15
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +080016from autotest_lib.client.bin import utils
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080017from autotest_lib.client.common_lib import error
Vic Yangebd6de62012-06-26 14:25:57 +080018from autotest_lib.server.cros.faft_client_attribute import FAFTClientAttribute
Tom Wai-Hong Tam22b77302011-11-03 13:03:48 +080019from autotest_lib.server.cros.servo_test import ServoTest
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080020from autotest_lib.site_utils import lab_test
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080021
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080022dirname = os.path.dirname(sys.modules[__name__].__file__)
23autotest_dir = os.path.abspath(os.path.join(dirname, "..", ".."))
24cros_dir = os.path.join(autotest_dir, "..", "..", "..", "..")
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080025
26class FAFTSequence(ServoTest):
27 """
28 The base class of Fully Automated Firmware Test Sequence.
29
30 Many firmware tests require several reboot cycles and verify the resulted
31 system states. To do that, an Autotest test case should detailly handle
32 every action on each step. It makes the test case hard to read and many
33 duplicated code. The base class FAFTSequence is to solve this problem.
34
35 The actions of one reboot cycle is defined in a dict, namely FAFT_STEP.
36 There are four functions in the FAFT_STEP dict:
37 state_checker: a function to check the current is valid or not,
38 returning True if valid, otherwise, False to break the whole
39 test sequence.
40 userspace_action: a function to describe the action ran in userspace.
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +080041 reboot_action: a function to do reboot, default: sync_and_warm_reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080042 firmware_action: a function to describe the action ran after reboot.
43
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +080044 And configurations:
45 install_deps_after_boot: if True, install the Autotest dependency after
46 boot; otherwise, do nothing. It is for the cases of recovery mode
47 test. The test boots a USB/SD image instead of an internal image.
48 The previous installed Autotest dependency on the internal image
49 is lost. So need to install it again.
50
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080051 The default FAFT_STEP checks nothing in state_checker and does nothing in
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080052 userspace_action and firmware_action. Its reboot_action is a hardware
53 reboot. You can change the default FAFT_STEP by calling
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080054 self.register_faft_template(FAFT_STEP).
55
56 A FAFT test case consists of several FAFT_STEP's, namely FAFT_SEQUENCE.
57 FAFT_SEQUENCE is an array of FAFT_STEP's. Any missing fields on FAFT_STEP
58 fall back to default.
59
60 In the run_once(), it should register and run FAFT_SEQUENCE like:
61 def run_once(self):
62 self.register_faft_sequence(FAFT_SEQUENCE)
63 self.run_faft_sequnce()
64
65 Note that in the last step, we only run state_checker. The
66 userspace_action, reboot_action, and firmware_action are not executed.
67
68 Attributes:
69 _faft_template: The default FAFT_STEP of each step. The actions would
70 be over-written if the registered FAFT_SEQUENCE is valid.
71 _faft_sequence: The registered FAFT_SEQUENCE.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080072 _customized_ctrl_d_key_command: The customized Ctrl-D key command
73 instead of sending key via servo board.
74 _customized_enter_key_command: The customized Enter key command instead
75 of sending key via servo board.
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +080076 _customized_space_key_command: The customized Space key command instead
77 of sending key via servo board.
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +080078 _customized_rec_reboot_command: The customized recovery reboot command
79 instead of sending key combination of Power + Esc + F3 for
80 triggering recovery reboot.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080081 _install_image_path: The path of Chrome OS test image to be installed.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +080082 _firmware_update: Boolean. True if firmware update needed after
83 installing the image.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080084 """
85 version = 1
86
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080087
88 # Mapping of partition number of kernel and rootfs.
89 KERNEL_MAP = {'a':'2', 'b':'4', '2':'2', '4':'4', '3':'2', '5':'4'}
90 ROOTFS_MAP = {'a':'3', 'b':'5', '2':'3', '4':'5', '3':'3', '5':'5'}
91 OTHER_KERNEL_MAP = {'a':'4', 'b':'2', '2':'4', '4':'2', '3':'4', '5':'2'}
92 OTHER_ROOTFS_MAP = {'a':'5', 'b':'3', '2':'5', '4':'3', '3':'5', '5':'3'}
93
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +080094 # Delay between power-on and firmware screen.
Tom Wai-Hong Tam66af37b2012-08-01 10:48:42 +080095 FIRMWARE_SCREEN_DELAY = 10
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +080096 # Delay between passing firmware screen and text mode warning screen.
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +080097 TEXT_SCREEN_DELAY = 20
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +080098 # Delay of loading the USB kernel.
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +080099 USB_LOAD_DELAY = 10
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +0800100 # Delay between USB plug-out and plug-in.
Tom Wai-Hong Tam9ca742a2011-12-05 15:48:57 +0800101 USB_PLUG_DELAY = 10
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +0800102 # Delay after running the 'sync' command.
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800103 SYNC_DELAY = 5
Vic Yang59cac9c2012-05-21 15:28:42 +0800104 # Delay for waiting client to return before EC reboot
105 EC_REBOOT_DELAY = 1
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +0800106 # Delay for waiting client to full power off
107 FULL_POWER_OFF_DELAY = 30
Vic Yang59cac9c2012-05-21 15:28:42 +0800108 # Delay between EC reboot and pressing power button
109 POWER_BTN_DELAY = 0.5
Vic Yangf86728a2012-07-30 10:44:07 +0800110 # Delay of EC software sync hash calculating time
111 SOFTWARE_SYNC_DELAY = 6
Vic Yanga7250662012-08-31 04:00:08 +0800112 # Delay between EC boot and ChromeEC console functional
113 EC_BOOT_DELAY = 0.5
114 # Duration of holding cold_reset to reset device
115 COLD_RESET_DELAY = 0.1
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800116
Tom Wai-Hong Tam51ef2e12012-07-27 15:04:12 +0800117 # The developer screen timeouts fit our spec.
118 DEV_SCREEN_TIMEOUT = 30
119
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800120 CHROMEOS_MAGIC = "CHROMEOS"
121 CORRUPTED_MAGIC = "CORRUPTD"
122
Tom Wai-Hong Tamf954d172011-12-08 17:14:15 +0800123 # Recovery reason codes, copied from:
124 # vboot_reference/firmware/lib/vboot_nvstorage.h
125 # vboot_reference/firmware/lib/vboot_struct.h
126 RECOVERY_REASON = {
127 # Recovery not requested
128 'NOT_REQUESTED': '0', # 0x00
129 # Recovery requested from legacy utility
130 'LEGACY': '1', # 0x01
131 # User manually requested recovery via recovery button
132 'RO_MANUAL': '2', # 0x02
133 # RW firmware failed signature check
134 'RO_INVALID_RW': '3', # 0x03
135 # S3 resume failed
136 'RO_S3_RESUME': '4', # 0x04
137 # TPM error in read-only firmware
138 'RO_TPM_ERROR': '5', # 0x05
139 # Shared data error in read-only firmware
140 'RO_SHARED_DATA': '6', # 0x06
141 # Test error from S3Resume()
142 'RO_TEST_S3': '7', # 0x07
143 # Test error from LoadFirmwareSetup()
144 'RO_TEST_LFS': '8', # 0x08
145 # Test error from LoadFirmware()
146 'RO_TEST_LF': '9', # 0x09
147 # RW firmware failed signature check
148 'RW_NOT_DONE': '16', # 0x10
149 'RW_DEV_MISMATCH': '17', # 0x11
150 'RW_REC_MISMATCH': '18', # 0x12
151 'RW_VERIFY_KEYBLOCK': '19', # 0x13
152 'RW_KEY_ROLLBACK': '20', # 0x14
153 'RW_DATA_KEY_PARSE': '21', # 0x15
154 'RW_VERIFY_PREAMBLE': '22', # 0x16
155 'RW_FW_ROLLBACK': '23', # 0x17
156 'RW_HEADER_VALID': '24', # 0x18
157 'RW_GET_FW_BODY': '25', # 0x19
158 'RW_HASH_WRONG_SIZE': '26', # 0x1A
159 'RW_VERIFY_BODY': '27', # 0x1B
160 'RW_VALID': '28', # 0x1C
161 # Read-only normal path requested by firmware preamble, but
162 # unsupported by firmware.
163 'RW_NO_RO_NORMAL': '29', # 0x1D
164 # Firmware boot failure outside of verified boot
165 'RO_FIRMWARE': '32', # 0x20
166 # Recovery mode TPM initialization requires a system reboot.
167 # The system was already in recovery mode for some other reason
168 # when this happened.
169 'RO_TPM_REBOOT': '33', # 0x21
170 # Unspecified/unknown error in read-only firmware
171 'RO_UNSPECIFIED': '63', # 0x3F
172 # User manually requested recovery by pressing a key at developer
173 # warning screen.
174 'RW_DEV_SCREEN': '65', # 0x41
175 # No OS kernel detected
176 'RW_NO_OS': '66', # 0x42
177 # OS kernel failed signature check
178 'RW_INVALID_OS': '67', # 0x43
179 # TPM error in rewritable firmware
180 'RW_TPM_ERROR': '68', # 0x44
181 # RW firmware in dev mode, but dev switch is off.
182 'RW_DEV_MISMATCH': '69', # 0x45
183 # Shared data error in rewritable firmware
184 'RW_SHARED_DATA': '70', # 0x46
185 # Test error from LoadKernel()
186 'RW_TEST_LK': '71', # 0x47
187 # No bootable disk found
188 'RW_NO_DISK': '72', # 0x48
189 # Unspecified/unknown error in rewritable firmware
190 'RW_UNSPECIFIED': '127', # 0x7F
191 # DM-verity error
192 'KE_DM_VERITY': '129', # 0x81
193 # Unspecified/unknown error in kernel
194 'KE_UNSPECIFIED': '191', # 0xBF
195 # Recovery mode test from user-mode
196 'US_TEST': '193', # 0xC1
197 # Unspecified/unknown error in user-mode
198 'US_UNSPECIFIED': '255', # 0xFF
199 }
200
Tom Wai-Hong Tam1e40fa12012-07-25 16:22:24 +0800201 # GBB flags
202 GBB_FLAG_DEV_SCREEN_SHORT_DELAY = 0x00000001
203 GBB_FLAG_LOAD_OPTION_ROMS = 0x00000002
204 GBB_FLAG_ENABLE_ALTERNATE_OS = 0x00000004
205 GBB_FLAG_FORCE_DEV_SWITCH_ON = 0x00000008
206 GBB_FLAG_FORCE_DEV_BOOT_USB = 0x00000010
207 GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK = 0x00000020
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800208 GBB_FLAG_ENTER_TRIGGERS_TONORM = 0x00000040
Tom Wai-Hong Tam1e40fa12012-07-25 16:22:24 +0800209
Tom Wai-Hong Tam39b93b92012-09-04 16:56:05 +0800210 # VbSharedData flags
211 # Copied from vboot_reference/firmware/include/vboot_struct.h
212 VDAT_FLAG_FWB_TRIED = 0x00000001
213 VDAT_FLAG_KERNEL_KEY_VERIFIED = 0x00000002
214 VDAT_FLAG_LF_DEV_SWITCH_ON = 0x00000004
215 VDAT_FLAG_LF_USE_RO_NORMAL = 0x00000008
216 VDAT_FLAG_BOOT_DEV_SWITCH_ON = 0x00000010
217 VDAT_FLAG_BOOT_REC_SWITCH_ON = 0x00000020
218 VDAT_FLAG_BOOT_FIRMWARE_WP_ENABLED = 0x00000040
219 VDAT_FLAG_BOOT_S3_RESUME = 0x00000100
220 VDAT_FLAG_BOOT_RO_NORMAL_SUPPORT = 0x00000200
221 VDAT_FLAG_HONOR_VIRT_DEV_SWITCH = 0x00000400
222 VDAT_FLAG_EC_SOFTWARE_SYNC = 0x00000800
223 VDAT_FLAG_EC_SLOW_UPDATE = 0x00001000
224
Tom Wai-Hong Tame3c72432012-09-05 10:48:31 +0800225 # Firmware preamble flags
226 PREAMBLE_USE_RO_NORMAL = 0x00000001
227
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800228 _faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800229 _faft_sequence = ()
230
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800231 _customized_ctrl_d_key_command = None
232 _customized_enter_key_command = None
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800233 _customized_space_key_command = None
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800234 _customized_rec_reboot_command = None
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800235 _install_image_path = None
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800236 _firmware_update = False
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800237
238
239 def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False):
240 # Parse arguments from command line
241 args = {}
242 for arg in cmdline_args:
243 match = re.search("^(\w+)=(.+)", arg)
244 if match:
245 args[match.group(1)] = match.group(2)
246
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800247 # Keep the arguments which will be used later.
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800248 if 'ctrl_d_cmd' in args:
249 self._customized_ctrl_d_key_command = args['ctrl_d_cmd']
250 logging.info('Customized Ctrl-D key command: %s' %
251 self._customized_ctrl_d_key_command)
252 if 'enter_cmd' in args:
253 self._customized_enter_key_command = args['enter_cmd']
254 logging.info('Customized Enter key command: %s' %
255 self._customized_enter_key_command)
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800256 if 'space_cmd' in args:
257 self._customized_space_key_command = args['space_cmd']
258 logging.info('Customized Space key command: %s' %
259 self._customized_space_key_command)
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800260 if 'rec_reboot_cmd' in args:
261 self._customized_rec_reboot_command = args['rec_reboot_cmd']
262 logging.info('Customized recovery reboot command: %s' %
263 self._customized_rec_reboot_command)
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800264 if 'image' in args:
265 self._install_image_path = args['image']
266 logging.info('Install Chrome OS test image path: %s' %
267 self._install_image_path)
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800268 if 'firmware_update' in args and args['firmware_update'].lower() \
269 not in ('0', 'false', 'no'):
270 if self._install_image_path:
271 self._firmware_update = True
272 logging.info('Also update firmware after installing.')
273 else:
274 logging.warning('Firmware update will not not performed '
275 'since no image is specified.')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800276
277 super(FAFTSequence, self).initialize(host, cmdline_args, use_pyauto,
278 use_faft)
Vic Yangebd6de62012-06-26 14:25:57 +0800279 if use_faft:
280 self.client_attr = FAFTClientAttribute(
281 self.faft_client.get_platform_name())
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800282
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700283 # Setting up key matrix mapping
284 self.servo.set_key_matrix(self.client_attr.key_matrix_layout)
285
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800286
287 def setup(self):
288 """Autotest setup function."""
289 super(FAFTSequence, self).setup()
290 if not self._remote_infos['faft']['used']:
291 raise error.TestError('The use_faft flag should be enabled.')
292 self.register_faft_template({
293 'state_checker': (None),
294 'userspace_action': (None),
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +0800295 'reboot_action': (self.sync_and_warm_reboot),
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800296 'firmware_action': (None)
297 })
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800298 self.clear_set_gbb_flags(self.GBB_FLAG_FORCE_DEV_SWITCH_ON |
299 self.GBB_FLAG_DEV_SCREEN_SHORT_DELAY,
300 self.GBB_FLAG_ENTER_TRIGGERS_TONORM)
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800301 if self._install_image_path:
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800302 self.install_test_image(self._install_image_path,
303 self._firmware_update)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800304
305
306 def cleanup(self):
307 """Autotest cleanup function."""
308 self._faft_sequence = ()
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800309 self._faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800310 super(FAFTSequence, self).cleanup()
311
312
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800313 def reset_client(self):
314 """Reset client if necessary."""
315 # TODO(crosbug/23309): Implement complete reset.
316 if not self._ping_test(self._client.ip, timeout=1):
317 # Client is offline. Cold reset it.
318 self.cold_reboot()
319 self.wait_for_client()
320
321
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800322 def assert_test_image_in_usb_disk(self, usb_dev=None):
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800323 """Assert an USB disk plugged-in on servo and a test image inside.
324
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800325 Args:
326 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
327 If None, it is detected automatically.
328
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800329 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800330 error.TestError: if USB disk not detected or not a test image.
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800331 """
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800332 if usb_dev:
333 assert self.servo.get('usb_mux_sel1') == 'servo_sees_usbkey'
334 else:
Vadim Bendeburycacf29f2012-07-30 17:49:11 -0700335 self.servo.enable_usb_hub(host=True)
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800336 usb_dev = self.servo.probe_host_usb_dev()
337 if not usb_dev:
338 raise error.TestError(
339 'An USB disk should be plugged in the servo board.')
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800340
341 tmp_dir = tempfile.mkdtemp()
Tom Wai-Hong Tamb0e80852011-12-07 16:15:06 +0800342 utils.system('sudo mount -r -t ext2 %s3 %s' % (usb_dev, tmp_dir))
Tom Wai-Hong Tame77459e2011-11-03 17:19:46 +0800343 code = utils.system(
344 'grep -qE "(Test Build|testimage-channel)" %s/etc/lsb-release' %
345 tmp_dir, ignore_status=True)
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800346 utils.system('sudo umount %s' % tmp_dir)
347 os.removedirs(tmp_dir)
348 if code != 0:
349 raise error.TestError(
350 'The image in the USB disk should be a test image.')
351
352
Simran Basi741b5d42012-05-18 11:27:15 -0700353 def install_test_image(self, image_path=None, firmware_update=False):
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800354 """Install the test image specied by the path onto the USB and DUT disk.
355
356 The method first copies the image to USB disk and reboots into it via
Mike Truty49153d82012-08-21 22:27:30 -0500357 recovery mode. Then runs 'chromeos-install' (and possible
358 chromeos-firmwareupdate') to install it to DUT disk.
359
360 Sample command line:
361
362 run_remote_tests.sh --servo --board=daisy --remote=w.x.y.z \
363 --args="image=/tmp/chromiumos_test_image.bin firmware_update=True" \
364 server/site_tests/firmware_XXXX/control
365
366 This test requires an automated recovery to occur while simulating
367 inserting and removing the usb key from the servo. To allow this the
368 following hardware setup is required:
369 1. servo2 board connected via servoflex.
370 2. USB key inserted in the servo2.
371 3. servo2 connected to the dut via dut_hub_in in the usb 2.0 slot.
372 4. network connected via usb dongle in the dut in usb 3.0 slot.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800373
374 Args:
375 image_path: Path on the host to the test image.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800376 firmware_update: Also update the firmware after installing.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800377 """
378 build_ver, build_hash = lab_test.VerifyImageAndGetId(cros_dir,
379 image_path)
380 logging.info('Processing build: %s %s' % (build_ver, build_hash))
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800381
Mike Truty49153d82012-08-21 22:27:30 -0500382 # Reuse the servo method that uses the servo USB key to install
383 # the test image.
384 self.servo.image_to_servo_usb(image_path)
385
386 # DUT is powered off while imaging servo USB.
387 # Now turn it on.
388 self.servo.power_short_press()
389 self.wait_for_client()
390 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
391
392 install_cmd = 'chromeos-install --yes'
393 if firmware_update:
394 install_cmd += ' && chromeos-firmwareupdate --mode recovery'
395
396 self.register_faft_sequence((
397 { # Step 1, request recovery boot
398 'state_checker': (self.crossystem_checker, {
399 'mainfw_type': ('developer', 'normal'),
400 }),
401 'userspace_action': self.faft_client.request_recovery_boot,
402 'firmware_action': self.wait_fw_screen_and_plug_usb,
403 'install_deps_after_boot': True,
404 },
405 { # Step 2, expected recovery boot
406 'state_checker': (self.crossystem_checker, {
407 'mainfw_type': 'recovery',
408 'recovery_reason' : self.RECOVERY_REASON['US_TEST'],
409 }),
410 'userspace_action': (self.faft_client.run_shell_command,
411 install_cmd),
412 'reboot_action': self.cold_reboot,
413 'install_deps_after_boot': True,
414 },
415 { # Step 3, expected normal or developer boot (not recovery)
416 'state_checker': (self.crossystem_checker, {
417 'mainfw_type': ('developer', 'normal')
418 }),
419 },
420 ))
421 self.run_faft_sequence()
422 # 'Unplug' any USB keys in the servo from the dut.
423 self.servo.disable_usb_hub()
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800424
425
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800426 def clear_set_gbb_flags(self, clear_mask, set_mask):
427 """Clear and set the GBB flags in the current flashrom.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800428
429 Args:
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800430 clear_mask: A mask of flags to be cleared.
431 set_mask: A mask of flags to be set.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800432 """
433 gbb_flags = self.faft_client.get_gbb_flags()
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800434 new_flags = gbb_flags & ctypes.c_uint32(~clear_mask).value | set_mask
435
436 if (gbb_flags != new_flags):
437 logging.info('Change the GBB flags from 0x%x to 0x%x.' %
438 (gbb_flags, new_flags))
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800439 self.faft_client.run_shell_command(
Tom Wai-Hong Tamfda76e22012-08-08 17:19:10 +0800440 '/usr/share/vboot/bin/set_gbb_flags.sh 0x%x' % new_flags)
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +0800441 self.faft_client.reload_firmware()
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800442 # If changing FORCE_DEV_SWITCH_ON flag, reboot to get a clear state
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800443 if ((gbb_flags ^ new_flags) & self.GBB_FLAG_FORCE_DEV_SWITCH_ON):
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800444 self.run_faft_step({
445 'firmware_action': self.wait_fw_screen_and_ctrl_d,
446 })
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800447
448
Vic Yangb4e3e742012-06-02 13:17:38 +0800449 def _open_uart_pty(self):
450 """Open UART pty and spawn pexpect object.
451
452 Returns:
453 Tuple (fd, child): fd is the file descriptor of opened UART pty, and
454 child is a fdpexpect object tied to it.
455 """
456 fd = os.open(self.servo.get("uart1_pty"), os.O_RDWR | os.O_NONBLOCK)
457 child = fdpexpect.fdspawn(fd)
458 return (fd, child)
459
460
461 def _flush_uart_pty(self, child):
462 """Flush UART output to prevent previous pending message interferring.
463
464 Args:
465 child: The fdpexpect object tied to UART pty.
466 """
467 child.sendline("")
468 while True:
469 try:
470 child.expect(".", timeout=0.01)
471 except pexpect.TIMEOUT:
472 break
473
474
475 def _uart_send(self, child, line):
476 """Flush and send command through UART.
477
478 Args:
479 child: The pexpect object tied to UART pty.
480 line: String to send through UART.
481
482 Raises:
483 error.TestFail: Raised when writing to UART fails.
484 """
485 logging.info("Sending UART command: %s" % line)
486 self._flush_uart_pty(child)
487 if child.sendline(line) != len(line) + 1:
488 raise error.TestFail("Failed to send UART command.")
489
490
491 def send_uart_command(self, command):
492 """Send command through UART.
493
494 This function open UART pty when called, and then command is sent
495 through UART.
496
497 Args:
498 command: The command string to send.
499
500 Raises:
501 error.TestFail: Raised when writing to UART fails.
502 """
503 (fd, child) = self._open_uart_pty()
504 try:
505 self._uart_send(child, command)
506 finally:
507 os.close(fd)
508
509
510 def send_uart_command_get_output(self, command, regex_list, timeout=1):
511 """Send command through UART and wait for response.
512
513 This function waits for response message matching regular expressions.
514
515 Args:
516 command: The command sent.
517 regex_list: List of regular expressions used to match response message.
518 Note, list must be ordered.
519
520 Returns:
521 List of match objects of response message.
522
523 Raises:
524 error.TestFail: If timed out waiting for EC response.
525 """
526 if not isinstance(regex_list, list):
527 regex_list = [regex_list]
528 result_list = []
529 (fd, child) = self._open_uart_pty()
530 try:
531 self._uart_send(child, command)
532 for regex in regex_list:
533 child.expect(regex, timeout=timeout)
534 result_list.append(child.match)
535 except pexpect.TIMEOUT:
536 raise error.TestFail("Timeout waiting for UART response.")
537 finally:
538 os.close(fd)
539 return result_list
540
541
Vic Yang4d72cb62012-07-24 11:51:09 +0800542 def check_ec_capability(self, required_cap=[]):
543 """Check if current platform has required EC capabilities.
544
545 Args:
546 required_cap: A list containing required EC capabilities. Pass in
547 None to only check for presence of Chrome EC.
548
549 Returns:
550 True if requirements are met. Otherwise, False.
551 """
552 if not self.client_attr.chrome_ec:
553 logging.warn('Requires Chrome EC to run this test.')
554 return False
555
556 for cap in required_cap:
557 if cap not in self.client_attr.ec_capability:
558 logging.warn('Requires EC capability "%s" to run this test.' %
559 cap)
560 return False
561
562 return True
563
564
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800565 def _parse_crossystem_output(self, lines):
566 """Parse the crossystem output into a dict.
567
568 Args:
569 lines: The list of crossystem output strings.
570
571 Returns:
572 A dict which contains the crossystem keys/values.
573
574 Raises:
575 error.TestError: If wrong format in crossystem output.
576
577 >>> seq = FAFTSequence()
578 >>> seq._parse_crossystem_output([ \
579 "arch = x86 # Platform architecture", \
580 "cros_debug = 1 # OS should allow debug", \
581 ])
582 {'cros_debug': '1', 'arch': 'x86'}
583 >>> seq._parse_crossystem_output([ \
584 "arch=x86", \
585 ])
586 Traceback (most recent call last):
587 ...
588 TestError: Failed to parse crossystem output: arch=x86
589 >>> seq._parse_crossystem_output([ \
590 "arch = x86 # Platform architecture", \
591 "arch = arm # Platform architecture", \
592 ])
593 Traceback (most recent call last):
594 ...
595 TestError: Duplicated crossystem key: arch
596 """
597 pattern = "^([^ =]*) *= *(.*[^ ]) *# [^#]*$"
598 parsed_list = {}
599 for line in lines:
600 matched = re.match(pattern, line.strip())
601 if not matched:
602 raise error.TestError("Failed to parse crossystem output: %s"
603 % line)
604 (name, value) = (matched.group(1), matched.group(2))
605 if name in parsed_list:
606 raise error.TestError("Duplicated crossystem key: %s" % name)
607 parsed_list[name] = value
608 return parsed_list
609
610
611 def crossystem_checker(self, expected_dict):
612 """Check the crossystem values matched.
613
614 Given an expect_dict which describes the expected crossystem values,
615 this function check the current crossystem values are matched or not.
616
617 Args:
618 expected_dict: A dict which contains the expected values.
619
620 Returns:
621 True if the crossystem value matched; otherwise, False.
622 """
623 lines = self.faft_client.run_shell_command_get_output('crossystem')
624 got_dict = self._parse_crossystem_output(lines)
625 for key in expected_dict:
626 if key not in got_dict:
627 logging.info('Expected key "%s" not in crossystem result' % key)
628 return False
629 if isinstance(expected_dict[key], str):
630 if got_dict[key] != expected_dict[key]:
631 logging.info("Expected '%s' value '%s' but got '%s'" %
632 (key, expected_dict[key], got_dict[key]))
633 return False
634 elif isinstance(expected_dict[key], tuple):
635 # Expected value is a tuple of possible actual values.
636 if got_dict[key] not in expected_dict[key]:
637 logging.info("Expected '%s' values %s but got '%s'" %
638 (key, str(expected_dict[key]), got_dict[key]))
639 return False
640 else:
641 logging.info("The expected_dict is neither a str nor a dict.")
642 return False
643 return True
644
645
Tom Wai-Hong Tam39b93b92012-09-04 16:56:05 +0800646 def vdat_flags_checker(self, mask, value):
647 """Check the flags from VbSharedData matched.
648
649 This function checks the masked flags from VbSharedData using crossystem
650 are matched the given value.
651
652 Args:
653 mask: A bitmask of flags to be matched.
654 value: An expected value.
655
656 Returns:
657 True if the flags matched; otherwise, False.
658 """
659 lines = self.faft_client.run_shell_command_get_output(
660 'crossystem vdat_flags')
661 vdat_flags = int(lines[0], 16)
662 if vdat_flags & mask != value:
663 logging.info("Expected vdat_flags 0x%x mask 0x%x but got 0x%x" %
664 (value, mask, vdat_flags))
665 return False
666 return True
667
668
Tom Wai-Hong Tam3e82e362012-09-05 10:17:55 +0800669 def ro_normal_checker(self, expected_fw=None, twostop=False):
670 """Check the current boot uses RO boot.
671
672 Args:
673 expected_fw: A string of expected firmware, 'A', 'B', or
674 None if don't care.
675 twostop: True to expect a TwoStop boot; False to expect a RO boot.
676
677 Returns:
678 True if the currect boot firmware matched and used RO boot;
679 otherwise, False.
680 """
681 crossystem_dict = {'tried_fwb': '0'}
682 if expected_fw:
683 crossystem_dict['mainfw_act'] = expected_fw.upper()
684 if self.check_ec_capability():
685 crossystem_dict['ecfw_act'] = ('RW' if twostop else 'RO')
686
687 return (self.vdat_flags_checker(
688 self.VDAT_FLAG_LF_USE_RO_NORMAL,
689 0 if twostop else self.VDAT_FLAG_LF_USE_RO_NORMAL) and
690 self.crossystem_checker(crossystem_dict))
691
692
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800693 def root_part_checker(self, expected_part):
694 """Check the partition number of the root device matched.
695
696 Args:
697 expected_part: A string containing the number of the expected root
698 partition.
699
700 Returns:
701 True if the currect root partition number matched; otherwise, False.
702 """
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800703 part = self.faft_client.get_root_part()[-1]
704 if self.ROOTFS_MAP[expected_part] != part:
705 logging.info("Expected root part %s but got %s" %
706 (self.ROOTFS_MAP[expected_part], part))
707 return False
708 return True
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800709
710
Vic Yang59cac9c2012-05-21 15:28:42 +0800711 def ec_act_copy_checker(self, expected_copy):
712 """Check the EC running firmware copy matches.
713
714 Args:
715 expected_copy: A string containing 'RO', 'A', or 'B' indicating
716 the expected copy of EC running firmware.
717
718 Returns:
719 True if the current EC running copy matches; otherwise, False.
720 """
721 lines = self.faft_client.run_shell_command_get_output('ectool version')
722 pattern = re.compile("Firmware copy: (.*)")
723 for line in lines:
724 matched = pattern.match(line)
725 if matched and matched.group(1) == expected_copy:
726 return True
727 return False
728
729
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800730 def check_root_part_on_non_recovery(self, part):
731 """Check the partition number of root device and on normal/dev boot.
732
733 Returns:
734 True if the root device matched and on normal/dev boot;
735 otherwise, False.
736 """
737 return self.root_part_checker(part) and \
738 self.crossystem_checker({
739 'mainfw_type': ('normal', 'developer'),
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800740 })
741
742
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800743 def _join_part(self, dev, part):
744 """Return a concatenated string of device and partition number.
745
746 Args:
747 dev: A string of device, e.g.'/dev/sda'.
748 part: A string of partition number, e.g.'3'.
749
750 Returns:
751 A concatenated string of device and partition number, e.g.'/dev/sda3'.
752
753 >>> seq = FAFTSequence()
754 >>> seq._join_part('/dev/sda', '3')
755 '/dev/sda3'
756 >>> seq._join_part('/dev/mmcblk0', '2')
757 '/dev/mmcblk0p2'
758 """
759 if 'mmcblk' in dev:
760 return dev + 'p' + part
761 else:
762 return dev + part
763
764
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800765 def copy_kernel_and_rootfs(self, from_part, to_part):
766 """Copy kernel and rootfs from from_part to to_part.
767
768 Args:
769 from_part: A string of partition number to be copied from.
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800770 to_part: A string of partition number to be copied to.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800771 """
772 root_dev = self.faft_client.get_root_dev()
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800773 logging.info('Copying kernel from %s to %s. Please wait...' %
774 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800775 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800776 (self._join_part(root_dev, self.KERNEL_MAP[from_part]),
777 self._join_part(root_dev, self.KERNEL_MAP[to_part])))
778 logging.info('Copying rootfs from %s to %s. Please wait...' %
779 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800780 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800781 (self._join_part(root_dev, self.ROOTFS_MAP[from_part]),
782 self._join_part(root_dev, self.ROOTFS_MAP[to_part])))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800783
784
785 def ensure_kernel_boot(self, part):
786 """Ensure the request kernel boot.
787
788 If not, it duplicates the current kernel to the requested kernel
789 and sets the requested higher priority to ensure it boot.
790
791 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800792 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800793 """
794 if not self.root_part_checker(part):
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800795 if self.faft_client.diff_kernel_a_b():
796 self.copy_kernel_and_rootfs(
797 from_part=self.OTHER_KERNEL_MAP[part],
798 to_part=part)
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800799 self.run_faft_step({
800 'userspace_action': (self.reset_and_prioritize_kernel, part),
801 })
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800802
803
Vic Yang416f2032012-08-28 10:18:03 +0800804 def set_hardware_write_protect(self, enabled):
Vic Yang2cabf812012-08-28 02:39:04 +0800805 """Set hardware write protect pin.
806
807 Args:
808 enable: True if asserting write protect pin. Otherwise, False.
809 """
810 self.servo.set('fw_wp_vref', self.client_attr.wp_voltage)
811 self.servo.set('fw_wp_en', 'on')
Vic Yang416f2032012-08-28 10:18:03 +0800812 self.servo.set('fw_wp', 'on' if enabled else 'off')
813
814
815 def set_EC_write_protect_and_reboot(self, enabled):
816 """Set EC write protect status and reboot to take effect.
817
818 EC write protect is only activated if both hardware write protect pin
819 is asserted and software write protect flag is set. Also, a reboot is
820 required for write protect to take effect.
821
822 Since the software write protect flag cannot be unset if hardware write
823 protect pin is asserted, we need to deasserted the pin first if we are
824 deactivating write protect. Similarly, a reboot is required before we
825 can modify the software flag.
826
827 This method asserts/deasserts hardware write protect pin first, and
828 set corresponding EC software write protect flag.
829
830 Args:
831 enable: True if activating EC write protect. Otherwise, False.
832 """
833 self.set_hardware_write_protect(enabled)
834 if enabled:
835 # Set write protect flag and reboot to take effect.
836 self.send_uart_command("flashwp enable")
837 self.sync_and_ec_reboot()
838 else:
839 # Reboot after deasserting hardware write protect pin to deactivate
840 # write protect. And then remove software write protect flag.
841 self.sync_and_ec_reboot()
842 self.send_uart_command("flashwp disable")
Vic Yang2cabf812012-08-28 02:39:04 +0800843
844
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800845 def send_ctrl_d_to_dut(self):
846 """Send Ctrl-D key to DUT."""
847 if self._customized_ctrl_d_key_command:
848 logging.info('running the customized Ctrl-D key command')
849 os.system(self._customized_ctrl_d_key_command)
850 else:
851 self.servo.ctrl_d()
852
853
854 def send_enter_to_dut(self):
855 """Send Enter key to DUT."""
856 if self._customized_enter_key_command:
857 logging.info('running the customized Enter key command')
858 os.system(self._customized_enter_key_command)
859 else:
860 self.servo.enter_key()
861
862
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800863 def send_space_to_dut(self):
864 """Send Space key to DUT."""
865 if self._customized_space_key_command:
866 logging.info('running the customized Space key command')
867 os.system(self._customized_space_key_command)
868 else:
869 # Send the alternative key combinaton of space key to servo.
870 self.servo.ctrl_refresh_key()
871
872
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800873 def wait_fw_screen_and_ctrl_d(self):
874 """Wait for firmware warning screen and press Ctrl-D."""
875 time.sleep(self.FIRMWARE_SCREEN_DELAY)
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800876 self.send_ctrl_d_to_dut()
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800877
878
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800879 def wait_fw_screen_and_trigger_recovery(self, need_dev_transition=False):
880 """Wait for firmware warning screen and trigger recovery boot."""
881 time.sleep(self.FIRMWARE_SCREEN_DELAY)
882 self.send_enter_to_dut()
883
884 # For Alex/ZGB, there is a dev warning screen in text mode.
885 # Skip it by pressing Ctrl-D.
886 if need_dev_transition:
887 time.sleep(self.TEXT_SCREEN_DELAY)
888 self.send_ctrl_d_to_dut()
889
890
Mike Truty49153d82012-08-21 22:27:30 -0500891 def wait_fw_screen_and_unplug_usb(self):
892 """Wait for firmware warning screen and then unplug the servo USB."""
Tom Wai-Hong Tama79574c2012-02-07 09:29:03 +0800893 time.sleep(self.USB_LOAD_DELAY)
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800894 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
895 time.sleep(self.USB_PLUG_DELAY)
Mike Truty49153d82012-08-21 22:27:30 -0500896
897
898 def wait_fw_screen_and_plug_usb(self):
899 """Wait for firmware warning screen and then unplug and plug the USB."""
900 self.wait_fw_screen_and_unplug_usb()
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800901 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
902
903
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800904 def wait_fw_screen_and_press_power(self):
905 """Wait for firmware warning screen and press power button."""
906 time.sleep(self.FIRMWARE_SCREEN_DELAY)
Tom Wai-Hong Tam7317c042012-08-14 11:59:06 +0800907 # While the firmware screen, the power button probing loop sleeps
908 # 0.25 second on every scan. Use the normal delay (1.2 second) for
909 # power press.
910 self.servo.power_normal_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800911
912
Tom Wai-Hong Tam4f5e5922012-07-27 16:23:15 +0800913 def wait_longer_fw_screen_and_press_power(self):
914 """Wait for firmware screen without timeout and press power button."""
915 time.sleep(self.DEV_SCREEN_TIMEOUT)
916 self.wait_fw_screen_and_press_power()
917
918
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800919 def wait_fw_screen_and_close_lid(self):
920 """Wait for firmware warning screen and close lid."""
921 time.sleep(self.FIRMWARE_SCREEN_DELAY)
922 self.servo.lid_close()
923
924
Tom Wai-Hong Tam473cfa72012-07-27 17:16:57 +0800925 def wait_longer_fw_screen_and_close_lid(self):
926 """Wait for firmware screen without timeout and close lid."""
927 time.sleep(self.FIRMWARE_SCREEN_DELAY)
928 self.wait_fw_screen_and_close_lid()
929
930
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800931 def setup_tried_fwb(self, tried_fwb):
932 """Setup for fw B tried state.
933
934 It makes sure the system in the requested fw B tried state. If not, it
935 tries to do so.
936
937 Args:
938 tried_fwb: True if requested in tried_fwb=1; False if tried_fwb=0.
939 """
940 if tried_fwb:
941 if not self.crossystem_checker({'tried_fwb': '1'}):
942 logging.info(
943 'Firmware is not booted with tried_fwb. Reboot into it.')
944 self.run_faft_step({
945 'userspace_action': self.faft_client.set_try_fw_b,
946 })
947 else:
948 if not self.crossystem_checker({'tried_fwb': '0'}):
949 logging.info(
950 'Firmware is booted with tried_fwb. Reboot to clear.')
951 self.run_faft_step({})
952
953
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800954 def enable_rec_mode_and_reboot(self):
955 """Switch to rec mode and reboot.
956
957 This method emulates the behavior of the old physical recovery switch,
958 i.e. switch ON + reboot + switch OFF, and the new keyboard controlled
959 recovery mode, i.e. just press Power + Esc + Refresh.
960 """
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800961 if self._customized_rec_reboot_command:
962 logging.info('running the customized rec reboot command')
963 os.system(self._customized_rec_reboot_command)
Tom Wai-Hong Tamb0b3f412012-08-13 17:17:06 +0800964 elif self.client_attr.chrome_ec:
Vic Yang81273092012-08-21 15:57:09 +0800965 # Cold reset to clear EC_IN_RW signal
Vic Yanga7250662012-08-31 04:00:08 +0800966 self.servo.set('cold_reset', 'on')
967 time.sleep(self.COLD_RESET_DELAY)
968 self.servo.set('cold_reset', 'off')
969 time.sleep(self.EC_BOOT_DELAY)
Vic Yang81273092012-08-21 15:57:09 +0800970 self.send_uart_command("reboot ap-off")
Vic Yang611dd852012-08-02 15:36:31 +0800971 time.sleep(self.EC_BOOT_DELAY)
972 self.send_uart_command("hostevent set 0x4000")
973 self.servo.power_short_press()
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800974 else:
975 self.servo.enable_recovery_mode()
976 self.cold_reboot()
977 time.sleep(self.EC_REBOOT_DELAY)
978 self.servo.disable_recovery_mode()
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800979
980
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800981 def enable_dev_mode_and_reboot(self):
982 """Switch to developer mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800983 if self.client_attr.keyboard_dev:
984 self.enable_keyboard_dev_mode()
985 else:
986 self.servo.enable_development_mode()
987 self.faft_client.run_shell_command(
988 'chromeos-firmwareupdate --mode todev && reboot')
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800989
990
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800991 def enable_normal_mode_and_reboot(self):
992 """Switch to normal mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800993 if self.client_attr.keyboard_dev:
994 self.disable_keyboard_dev_mode()
995 else:
996 self.servo.disable_development_mode()
997 self.faft_client.run_shell_command(
998 'chromeos-firmwareupdate --mode tonormal && reboot')
999
1000
1001 def wait_fw_screen_and_switch_keyboard_dev_mode(self, dev):
1002 """Wait for firmware screen and then switch into or out of dev mode.
1003
1004 Args:
1005 dev: True if switching into dev mode. Otherwise, False.
1006 """
1007 time.sleep(self.FIRMWARE_SCREEN_DELAY)
1008 if dev:
Tom Wai-Hong Tamfe314ac2012-07-25 14:14:17 +08001009 self.send_ctrl_d_to_dut()
Vic Yange7553162012-06-20 16:20:47 +08001010 else:
Tom Wai-Hong Tamfe314ac2012-07-25 14:14:17 +08001011 self.send_enter_to_dut()
Tom Wai-Hong Tam1408f172012-07-31 15:06:21 +08001012 time.sleep(self.FIRMWARE_SCREEN_DELAY)
Tom Wai-Hong Tamfe314ac2012-07-25 14:14:17 +08001013 self.send_enter_to_dut()
Vic Yange7553162012-06-20 16:20:47 +08001014
1015
1016 def enable_keyboard_dev_mode(self):
1017 logging.info("Enabling keyboard controlled developer mode")
Tom Wai-Hong Tamf1a17d72012-07-26 11:39:52 +08001018 # Plug out USB disk for preventing recovery boot without warning
1019 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Vic Yange7553162012-06-20 16:20:47 +08001020 # Rebooting EC with rec mode on. Should power on AP.
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +08001021 self.enable_rec_mode_and_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +08001022 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +08001023 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=True)
Vic Yange7553162012-06-20 16:20:47 +08001024
1025
1026 def disable_keyboard_dev_mode(self):
1027 logging.info("Disabling keyboard controlled developer mode")
Tom Wai-Hong Tamb0b3f412012-08-13 17:17:06 +08001028 if not self.client_attr.chrome_ec:
Vic Yang611dd852012-08-02 15:36:31 +08001029 self.servo.disable_recovery_mode()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001030 self.cold_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +08001031 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +08001032 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=False)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001033
1034
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001035 def setup_dev_mode(self, dev_mode):
1036 """Setup for development mode.
1037
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001038 It makes sure the system in the requested normal/dev mode. If not, it
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001039 tries to do so.
1040
1041 Args:
1042 dev_mode: True if requested in dev mode; False if normal mode.
1043 """
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001044 # Change the default firmware_action for dev mode passing the fw screen.
1045 self.register_faft_template({
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001046 'firmware_action': (self.wait_fw_screen_and_ctrl_d if dev_mode
1047 else None),
1048 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001049 if dev_mode:
Vic Yange7553162012-06-20 16:20:47 +08001050 if (not self.client_attr.keyboard_dev and
1051 not self.crossystem_checker({'devsw_cur': '1'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001052 logging.info('Dev switch is not on. Now switch it on.')
1053 self.servo.enable_development_mode()
1054 if not self.crossystem_checker({'devsw_boot': '1',
1055 'mainfw_type': 'developer'}):
1056 logging.info('System is not in dev mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001057 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +08001058 'userspace_action': None if self.client_attr.keyboard_dev
1059 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +08001060 'chromeos-firmwareupdate --mode todev && reboot'),
Vic Yange7553162012-06-20 16:20:47 +08001061 'reboot_action': self.enable_keyboard_dev_mode if
1062 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001063 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001064 else:
Vic Yange7553162012-06-20 16:20:47 +08001065 if (not self.client_attr.keyboard_dev and
1066 not self.crossystem_checker({'devsw_cur': '0'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001067 logging.info('Dev switch is not off. Now switch it off.')
1068 self.servo.disable_development_mode()
1069 if not self.crossystem_checker({'devsw_boot': '0',
1070 'mainfw_type': 'normal'}):
1071 logging.info('System is not in normal mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001072 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +08001073 'userspace_action': None if self.client_attr.keyboard_dev
1074 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +08001075 'chromeos-firmwareupdate --mode tonormal && reboot'),
Vic Yange7553162012-06-20 16:20:47 +08001076 'reboot_action': self.disable_keyboard_dev_mode if
1077 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +08001078 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001079
1080
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001081 def setup_kernel(self, part):
1082 """Setup for kernel test.
1083
1084 It makes sure both kernel A and B bootable and the current boot is
1085 the requested kernel part.
1086
1087 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001088 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001089 """
1090 self.ensure_kernel_boot(part)
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +08001091 if self.faft_client.diff_kernel_a_b():
1092 self.copy_kernel_and_rootfs(from_part=part,
1093 to_part=self.OTHER_KERNEL_MAP[part])
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001094 self.reset_and_prioritize_kernel(part)
1095
1096
1097 def reset_and_prioritize_kernel(self, part):
1098 """Make the requested partition highest priority.
1099
1100 This function also reset kerenl A and B to bootable.
1101
1102 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001103 part: A string of partition number to be prioritized.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001104 """
1105 root_dev = self.faft_client.get_root_dev()
1106 # Reset kernel A and B to bootable.
1107 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
1108 (self.KERNEL_MAP['a'], root_dev))
1109 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
1110 (self.KERNEL_MAP['b'], root_dev))
1111 # Set kernel part highest priority.
1112 self.faft_client.run_shell_command('cgpt prioritize -i%s %s' %
1113 (self.KERNEL_MAP[part], root_dev))
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +08001114 # Safer to sync and wait until the cgpt status written to the disk.
1115 self.faft_client.run_shell_command('sync')
1116 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001117
1118
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001119 def warm_reboot(self):
1120 """Request a warm reboot.
1121
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001122 A wrapper for underlying servo warm reset.
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001123 """
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001124 # Use cold reset if the warm reset is broken.
1125 if self.client_attr.broken_warm_reset:
Gediminas Ramanauskase021e152012-09-04 19:10:59 -07001126 logging.info('broken_warm_reset is True. Cold rebooting instead.')
1127 self.cold_reboot()
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001128 else:
1129 self.servo.warm_reset()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001130
1131
1132 def cold_reboot(self):
1133 """Request a cold reboot.
1134
1135 A wrapper for underlying servo cold reset.
1136 """
Tom Wai-Hong Tama276d0a2012-08-22 11:15:17 +08001137 if self.client_attr.platform == 'Parrot':
1138 self.servo.set('pwr_button', 'press')
1139 self.servo.set('cold_reset', 'on')
1140 self.servo.set('cold_reset', 'off')
1141 time.sleep(self.POWER_BTN_DELAY)
1142 self.servo.set('pwr_button', 'release')
1143 elif self.check_ec_capability():
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001144 # We don't use servo.cold_reset() here because software sync is
1145 # not yet finished, and device may or may not come up after cold
1146 # reset. Pressing power button before firmware comes up solves this.
1147 #
1148 # The correct behavior should be (not work now):
1149 # - If rebooting EC with rec mode on, power on AP and it boots
1150 # into recovery mode.
1151 # - If rebooting EC with rec mode off, power on AP for software
1152 # sync. Then AP checks if lid open or not. If lid open, continue;
1153 # otherwise, shut AP down and need servo for a power button
1154 # press.
1155 self.servo.set('cold_reset', 'on')
1156 self.servo.set('cold_reset', 'off')
1157 time.sleep(self.POWER_BTN_DELAY)
1158 self.servo.power_short_press()
1159 else:
1160 self.servo.cold_reset()
1161
1162
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001163 def sync_and_warm_reboot(self):
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001164 """Request the client sync and do a warm reboot.
1165
1166 This is the default reboot action on FAFT.
1167 """
1168 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +08001169 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001170 self.warm_reboot()
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001171
1172
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001173 def sync_and_cold_reboot(self):
1174 """Request the client sync and do a cold reboot.
1175
1176 This reboot action is used to reset EC for recovery mode.
1177 """
1178 self.faft_client.run_shell_command('sync')
1179 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001180 self.cold_reboot()
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001181
1182
Vic Yangaeb10392012-08-28 09:25:09 +08001183 def sync_and_ec_reboot(self, args=''):
1184 """Request the client sync and do a EC triggered reboot.
1185
1186 Args:
1187 args: Arguments passed to "ectool reboot_ec". Including:
1188 RO: jump to EC RO firmware.
1189 RW: jump to EC RW firmware.
1190 cold: Cold/hard reboot.
1191 """
Vic Yang59cac9c2012-05-21 15:28:42 +08001192 self.faft_client.run_shell_command('sync')
1193 time.sleep(self.SYNC_DELAY)
Vic Yangaeb10392012-08-28 09:25:09 +08001194 # Since EC reboot happens immediately, delay before actual reboot to
1195 # allow FAFT client returning.
1196 self.faft_client.run_shell_command('(sleep %d; ectool reboot_ec %s)&' %
1197 (self.EC_REBOOT_DELAY, args))
Vic Yangf86728a2012-07-30 10:44:07 +08001198 time.sleep(self.EC_REBOOT_DELAY)
1199 self.check_lid_and_power_on()
1200
1201
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +08001202 def full_power_off_and_on(self):
1203 """Shutdown the device by pressing power button and power on again."""
1204 # Press power button to trigger Chrome OS normal shutdown process.
1205 self.servo.power_normal_press()
1206 time.sleep(self.FULL_POWER_OFF_DELAY)
1207 # Short press power button to boot DUT again.
1208 self.servo.power_short_press()
1209
1210
Vic Yangf86728a2012-07-30 10:44:07 +08001211 def check_lid_and_power_on(self):
1212 """
1213 On devices with EC software sync, system powers on after EC reboots if
1214 lid is open. Otherwise, the EC shuts down CPU after about 3 seconds.
1215 This method checks lid switch state and presses power button if
1216 necessary.
1217 """
1218 if self.servo.get("lid_open") == "no":
1219 time.sleep(self.SOFTWARE_SYNC_DELAY)
1220 self.servo.power_short_press()
Vic Yang59cac9c2012-05-21 15:28:42 +08001221
1222
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001223 def _modify_usb_kernel(self, usb_dev, from_magic, to_magic):
1224 """Modify the kernel header magic in USB stick.
1225
1226 The kernel header magic is the first 8-byte of kernel partition.
1227 We modify it to make it fail on kernel verification check.
1228
1229 Args:
1230 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1231 from_magic: A string of magic which we change it from.
1232 to_magic: A string of magic which we change it to.
1233
1234 Raises:
1235 error.TestError: if failed to change magic.
1236 """
1237 assert len(from_magic) == 8
1238 assert len(to_magic) == 8
Tom Wai-Hong Tama1d9a0f2011-12-23 09:13:33 +08001239 # USB image only contains one kernel.
1240 kernel_part = self._join_part(usb_dev, self.KERNEL_MAP['a'])
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001241 read_cmd = "sudo dd if=%s bs=8 count=1 2>/dev/null" % kernel_part
1242 current_magic = utils.system_output(read_cmd)
1243 if current_magic == to_magic:
1244 logging.info("The kernel magic is already %s." % current_magic)
1245 return
1246 if current_magic != from_magic:
1247 raise error.TestError("Invalid kernel image on USB: wrong magic.")
1248
1249 logging.info('Modify the kernel magic in USB, from %s to %s.' %
1250 (from_magic, to_magic))
1251 write_cmd = ("echo -n '%s' | sudo dd of=%s oflag=sync conv=notrunc "
1252 " 2>/dev/null" % (to_magic, kernel_part))
1253 utils.system(write_cmd)
1254
1255 if utils.system_output(read_cmd) != to_magic:
1256 raise error.TestError("Failed to write new magic.")
1257
1258
1259 def corrupt_usb_kernel(self, usb_dev):
1260 """Corrupt USB kernel by modifying its magic from CHROMEOS to CORRUPTD.
1261
1262 Args:
1263 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1264 """
1265 self._modify_usb_kernel(usb_dev, self.CHROMEOS_MAGIC,
1266 self.CORRUPTED_MAGIC)
1267
1268
1269 def restore_usb_kernel(self, usb_dev):
1270 """Restore USB kernel by modifying its magic from CORRUPTD to CHROMEOS.
1271
1272 Args:
1273 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1274 """
1275 self._modify_usb_kernel(usb_dev, self.CORRUPTED_MAGIC,
1276 self.CHROMEOS_MAGIC)
1277
1278
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001279 def _call_action(self, action_tuple, check_status=False):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001280 """Call the action function with/without arguments.
1281
1282 Args:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001283 action_tuple: A function, or a tuple (function, args, error_msg),
1284 in which, args and error_msg are optional. args is
1285 either a value or a tuple if multiple arguments.
1286 check_status: Check the return value of action function. If not
1287 succeed, raises a TestFail exception.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001288
1289 Returns:
1290 The result value of the action function.
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001291
1292 Raises:
1293 error.TestError: An error when the action function is not callable.
1294 error.TestFail: When check_status=True, action function not succeed.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001295 """
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001296 action = action_tuple
1297 args = ()
1298 error_msg = 'Not succeed'
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001299 if isinstance(action_tuple, tuple):
1300 action = action_tuple[0]
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001301 if len(action_tuple) >= 2:
1302 args = action_tuple[1]
1303 if not isinstance(args, tuple):
1304 args = (args,)
1305 if len(action_tuple) >= 3:
1306 error_msg = action
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001307
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001308 if action is None:
1309 return
1310
1311 if not callable(action):
1312 raise error.TestError('action is not callable!')
1313
1314 info_msg = 'calling %s' % str(action)
1315 if args:
1316 info_msg += ' with args %s' % str(args)
1317 logging.info(info_msg)
1318 ret = action(*args)
1319
1320 if check_status and not ret:
1321 raise error.TestFail('%s: %s returning %s' %
1322 (error_msg, info_msg, str(ret)))
1323 return ret
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001324
1325
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001326 def run_shutdown_process(self, shutdown_action, pre_power_action=None,
1327 post_power_action=None):
1328 """Run shutdown_action(), which makes DUT shutdown, and power it on.
1329
1330 Args:
1331 shutdown_action: a function which makes DUT shutdown, like pressing
1332 power key.
1333 pre_power_action: a function which is called before next power on.
1334 post_power_action: a function which is called after next power on.
1335
1336 Raises:
1337 error.TestFail: if the shutdown_action() failed to turn DUT off.
1338 """
1339 self._call_action(shutdown_action)
1340 logging.info('Wait to ensure DUT shut down...')
1341 try:
1342 self.wait_for_client()
1343 raise error.TestFail(
1344 'Should shut the device down after calling %s.' %
1345 str(shutdown_action))
1346 except AssertionError:
1347 logging.info(
1348 'DUT is surely shutdown. We are going to power it on again...')
1349
1350 if pre_power_action:
1351 self._call_action(pre_power_action)
Tom Wai-Hong Tam610262a2012-01-12 14:16:53 +08001352 self.servo.power_short_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001353 if post_power_action:
1354 self._call_action(post_power_action)
1355
1356
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001357 def register_faft_template(self, template):
1358 """Register FAFT template, the default FAFT_STEP of each step.
1359
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001360 Any missing field falls back to the original faft_template.
1361
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001362 Args:
1363 template: A FAFT_STEP dict.
1364 """
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001365 self._faft_template.update(template)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001366
1367
1368 def register_faft_sequence(self, sequence):
1369 """Register FAFT sequence.
1370
1371 Args:
1372 sequence: A FAFT_SEQUENCE array which consisted of FAFT_STEP dicts.
1373 """
1374 self._faft_sequence = sequence
1375
1376
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001377 def run_faft_step(self, step, no_reboot=False):
1378 """Run a single FAFT step.
1379
1380 Any missing field falls back to faft_template. An empty step means
1381 running the default faft_template.
1382
1383 Args:
1384 step: A FAFT_STEP dict.
1385 no_reboot: True to prevent running reboot_action and firmware_action.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001386
1387 Raises:
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001388 error.TestError: An error when the given step is not valid.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001389 """
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001390 FAFT_STEP_KEYS = ('state_checker', 'userspace_action', 'reboot_action',
1391 'firmware_action', 'install_deps_after_boot')
1392
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001393 test = {}
1394 test.update(self._faft_template)
1395 test.update(step)
1396
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001397 for key in test:
1398 if key not in FAFT_STEP_KEYS:
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001399 raise error.TestError('Invalid key in FAFT step: %s', key)
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001400
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001401 if test['state_checker']:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001402 self._call_action(test['state_checker'], check_status=True)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001403
1404 self._call_action(test['userspace_action'])
1405
1406 # Don't run reboot_action and firmware_action if no_reboot is True.
1407 if not no_reboot:
1408 self._call_action(test['reboot_action'])
1409 self.wait_for_client_offline()
1410 self._call_action(test['firmware_action'])
1411
Vic Yang8eaf5ad2012-09-13 14:05:37 +08001412 try:
1413 if 'install_deps_after_boot' in test:
1414 self.wait_for_client(
1415 install_deps=test['install_deps_after_boot'])
1416 else:
1417 self.wait_for_client()
1418 except AssertionError:
1419 self.reset_client()
1420 raise
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001421
1422
1423 def run_faft_sequence(self):
1424 """Run FAFT sequence which was previously registered."""
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001425 sequence = self._faft_sequence
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001426 index = 1
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001427 for step in sequence:
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001428 logging.info('======== Running FAFT sequence step %d ========' %
1429 index)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001430 # Don't reboot in the last step.
1431 self.run_faft_step(step, no_reboot=(step is sequence[-1]))
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001432 index += 1