blob: 37501ad94162a191d9b3bd86944a37fe439bfaf6 [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
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08006import logging
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08007import os
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08008import re
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +08009import subprocess
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080010import sys
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080011import time
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080012
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +080013from autotest_lib.client.bin import utils
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080014from autotest_lib.client.common_lib import error
Tom Wai-Hong Tam6ec46e32012-10-05 16:39:21 +080015from autotest_lib.server.cros import vboot_constants as vboot
Tom Wai-Hong Tam665281c2012-10-30 11:55:10 +080016from autotest_lib.server.cros import chrome_ec
Vic Yangf93f7022012-10-31 09:40:36 +080017from autotest_lib.server.cros.faft_checkers import FAFTCheckers
Vic Yangebd6de62012-06-26 14:25:57 +080018from autotest_lib.server.cros.faft_client_attribute import FAFTClientAttribute
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +080019from autotest_lib.server.cros.faft_delay_constants import FAFTDelayConstants
Tom Wai-Hong Tam22b77302011-11-03 13:03:48 +080020from autotest_lib.server.cros.servo_test import ServoTest
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080021from autotest_lib.site_utils import lab_test
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +080022from autotest_lib.site_utils.chromeos_test.common_util import ChromeOSTestError
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080023
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080024class FAFTSequence(ServoTest):
25 """
26 The base class of Fully Automated Firmware Test Sequence.
27
28 Many firmware tests require several reboot cycles and verify the resulted
29 system states. To do that, an Autotest test case should detailly handle
30 every action on each step. It makes the test case hard to read and many
31 duplicated code. The base class FAFTSequence is to solve this problem.
32
33 The actions of one reboot cycle is defined in a dict, namely FAFT_STEP.
34 There are four functions in the FAFT_STEP dict:
35 state_checker: a function to check the current is valid or not,
36 returning True if valid, otherwise, False to break the whole
37 test sequence.
38 userspace_action: a function to describe the action ran in userspace.
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +080039 reboot_action: a function to do reboot, default: sync_and_warm_reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080040 firmware_action: a function to describe the action ran after reboot.
41
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +080042 And configurations:
43 install_deps_after_boot: if True, install the Autotest dependency after
44 boot; otherwise, do nothing. It is for the cases of recovery mode
45 test. The test boots a USB/SD image instead of an internal image.
46 The previous installed Autotest dependency on the internal image
47 is lost. So need to install it again.
48
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080049 The default FAFT_STEP checks nothing in state_checker and does nothing in
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080050 userspace_action and firmware_action. Its reboot_action is a hardware
51 reboot. You can change the default FAFT_STEP by calling
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080052 self.register_faft_template(FAFT_STEP).
53
54 A FAFT test case consists of several FAFT_STEP's, namely FAFT_SEQUENCE.
55 FAFT_SEQUENCE is an array of FAFT_STEP's. Any missing fields on FAFT_STEP
56 fall back to default.
57
58 In the run_once(), it should register and run FAFT_SEQUENCE like:
59 def run_once(self):
60 self.register_faft_sequence(FAFT_SEQUENCE)
61 self.run_faft_sequnce()
62
63 Note that in the last step, we only run state_checker. The
64 userspace_action, reboot_action, and firmware_action are not executed.
65
66 Attributes:
67 _faft_template: The default FAFT_STEP of each step. The actions would
68 be over-written if the registered FAFT_SEQUENCE is valid.
69 _faft_sequence: The registered FAFT_SEQUENCE.
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +080070 _install_image_path: The URL or the path on the host to the Chrome OS
71 test image to be installed.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +080072 _firmware_update: Boolean. True if firmware update needed after
73 installing the image.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080074 """
75 version = 1
76
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080077 # Mapping of partition number of kernel and rootfs.
78 KERNEL_MAP = {'a':'2', 'b':'4', '2':'2', '4':'4', '3':'2', '5':'4'}
79 ROOTFS_MAP = {'a':'3', 'b':'5', '2':'3', '4':'5', '3':'3', '5':'5'}
80 OTHER_KERNEL_MAP = {'a':'4', 'b':'2', '2':'4', '4':'2', '3':'4', '5':'2'}
81 OTHER_ROOTFS_MAP = {'a':'5', 'b':'3', '2':'5', '4':'3', '3':'5', '5':'3'}
82
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +080083 CHROMEOS_MAGIC = "CHROMEOS"
84 CORRUPTED_MAGIC = "CORRUPTD"
85
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +080086 _HTTP_PREFIX = 'http://'
87 _DEVSERVER_PORT = '8090'
88
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +080089 _faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080090 _faft_sequence = ()
91
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080092 _install_image_path = None
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +080093 _firmware_update = False
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +080094
ctchang38ae4922012-09-03 17:01:16 +080095 _backup_firmware_sha = ()
96
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +080097 # Class level variable, keep track the states of one time setup.
98 # This variable is preserved across tests which inherit this class.
99 _global_setup_done = {
100 'gbb_flags': False,
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800101 'reimage': False,
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800102 'usb_check': False,
103 }
Vic Yang54f70572012-10-19 17:05:26 +0800104
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800105 @classmethod
106 def check_setup_done(cls, label):
107 """Check if the given setup is done.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800108
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800109 Args:
110 label: The label of the setup.
111 """
112 return cls._global_setup_done[label]
113
114
115 @classmethod
116 def mark_setup_done(cls, label):
117 """Mark the given setup done.
118
119 Args:
120 label: The label of the setup.
121 """
122 cls._global_setup_done[label] = True
123
124
125 @classmethod
126 def unmark_setup_done(cls, label):
127 """Mark the given setup not done.
128
129 Args:
130 label: The label of the setup.
131 """
132 cls._global_setup_done[label] = False
Vic Yang54f70572012-10-19 17:05:26 +0800133
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800134
135 def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False):
136 # Parse arguments from command line
137 args = {}
138 for arg in cmdline_args:
139 match = re.search("^(\w+)=(.+)", arg)
140 if match:
141 args[match.group(1)] = match.group(2)
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800142 if 'image' in args:
143 self._install_image_path = args['image']
Vic Yang772df8a2012-10-31 10:10:49 +0800144 logging.info('Install Chrome OS test image path: %s',
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800145 self._install_image_path)
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800146 if 'firmware_update' in args and args['firmware_update'].lower() \
147 not in ('0', 'false', 'no'):
148 if self._install_image_path:
149 self._firmware_update = True
150 logging.info('Also update firmware after installing.')
151 else:
152 logging.warning('Firmware update will not not performed '
153 'since no image is specified.')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800154
155 super(FAFTSequence, self).initialize(host, cmdline_args, use_pyauto,
156 use_faft)
Vic Yangebd6de62012-06-26 14:25:57 +0800157 if use_faft:
158 self.client_attr = FAFTClientAttribute(
159 self.faft_client.get_platform_name())
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800160 self.delay = FAFTDelayConstants(
161 self.faft_client.get_platform_name())
Vic Yangf93f7022012-10-31 09:40:36 +0800162 self.checkers = FAFTCheckers(self, self.faft_client)
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800163
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800164 if self.client_attr.chrome_ec:
Tom Wai-Hong Tam665281c2012-10-30 11:55:10 +0800165 self.ec = chrome_ec.ChromeEC(self.servo)
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800166
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800167 if not self.client_attr.has_keyboard:
168 # The environment variable USBKM232_UART_DEVICE should point
169 # to the USB-KM232 UART device.
170 if ('USBKM232_UART_DEVICE' not in os.environ or
171 not os.path.exists(os.environ['USBKM232_UART_DEVICE'])):
172 raise error.TestError('Must set a valid environment '
173 'variable USBKM232_UART_DEVICE.')
174
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700175 # Setting up key matrix mapping
176 self.servo.set_key_matrix(self.client_attr.key_matrix_layout)
177
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800178
179 def setup(self):
180 """Autotest setup function."""
181 super(FAFTSequence, self).setup()
182 if not self._remote_infos['faft']['used']:
183 raise error.TestError('The use_faft flag should be enabled.')
184 self.register_faft_template({
185 'state_checker': (None),
186 'userspace_action': (None),
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +0800187 'reboot_action': (self.sync_and_warm_reboot),
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800188 'firmware_action': (None)
189 })
Tom Wai-Hong Tam19ad9682012-10-24 09:33:42 +0800190 self.install_test_image(self._install_image_path, self._firmware_update)
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800191 self.setup_gbb_flags()
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800192
193
194 def cleanup(self):
195 """Autotest cleanup function."""
196 self._faft_sequence = ()
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800197 self._faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800198 super(FAFTSequence, self).cleanup()
199
200
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800201 def invalidate_firmware_setup(self):
202 """Invalidate all firmware related setup state.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800203
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800204 This method is called when the firmware is re-flashed. It resets all
205 firmware related setup states so that the next test setup properly
206 again.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800207 """
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800208 self.unmark_setup_done('gbb_flags')
Vic Yangdbaba8f2012-10-17 16:05:35 +0800209
210
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800211 def _retrieve_recovery_reason_from_trap(self):
212 """Try to retrieve the recovery reason from a trapped recovery screen.
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800213
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800214 Returns:
215 The recovery_reason, 0 if any error.
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800216 """
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800217 recovery_reason = 0
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +0800218 logging.info('Try to retrieve recovery reason...')
219 if self.servo.get('usb_mux_sel1') == 'dut_sees_usbkey':
220 self.wait_fw_screen_and_plug_usb()
221 else:
222 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
223
224 try:
225 self.wait_for_client(install_deps=True)
226 lines = self.faft_client.run_shell_command_get_output(
227 'crossystem recovery_reason')
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800228 recovery_reason = int(lines[0])
229 logging.info('Got the recovery reason %d.', recovery_reason)
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +0800230 except AssertionError:
231 logging.info('Failed to get the recovery reason.')
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800232 return recovery_reason
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +0800233
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800234
235 def _reset_client(self):
236 """Reset client to a workable state.
237
238 This method is called when the client is not responsive. It may be
239 caused by the following cases:
240 - halt on a firmware screen without timeout, e.g. REC_INSERT screen;
241 - corrupted firmware;
242 - corrutped OS image.
243 """
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800244 # DUT may halt on a firmware screen. Try cold reboot.
245 logging.info('Try cold reboot...')
246 self.cold_reboot()
247 try:
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800248 self.wait_for_client()
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800249 return
250 except AssertionError:
251 pass
252
253 # DUT may be broken by a corrupted firmware. Restore firmware.
254 # We assume the recovery boot still works fine. Since the recovery
255 # code is in RO region and all FAFT tests don't change the RO region
256 # except GBB.
257 if self.is_firmware_saved():
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800258 self._ensure_client_in_recovery()
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800259 logging.info('Try restore the original firmware...')
260 if self.is_firmware_changed():
261 try:
262 self.restore_firmware()
263 return
264 except AssertionError:
265 logging.info('Restoring firmware doesn\'t help.')
266
267 # DUT may be broken by a corrupted OS image. Restore OS image.
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800268 self._ensure_client_in_recovery()
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800269 logging.info('Try restore the OS image...')
270 self.faft_client.run_shell_command('chromeos-install --yes')
271 self.sync_and_warm_reboot()
272 self.wait_for_client_offline()
273 try:
274 self.wait_for_client(install_deps=True)
275 logging.info('Successfully restore OS image.')
276 return
277 except AssertionError:
278 logging.info('Restoring OS image doesn\'t help.')
279
280
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800281 def _ensure_client_in_recovery(self):
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800282 """Ensure client in recovery boot; reboot into it if necessary.
283
284 Raises:
285 error.TestError: if failed to boot the USB image.
286 """
287 # DUT works fine and is already in recovery boot, done.
288 if self._ping_test(self._client.ip, timeout=5):
Vic Yangf93f7022012-10-31 09:40:36 +0800289 if self.checkers.crossystem_checker({'mainfw_type': 'recovery'}):
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800290 return
291
292 logging.info('Try boot into USB image...')
293 self.servo.enable_usb_hub(host=True)
294 self.enable_rec_mode_and_reboot()
295 self.wait_fw_screen_and_plug_usb()
296 try:
297 self.wait_for_client(install_deps=True)
298 except AssertionError:
299 raise error.TestError('Failed to boot the USB image.')
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800300
301
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800302 def _restore_routine_from_timeout(self, next_step=None):
303 """A routine to try to restore the system from a timeout error.
304
305 This method is called when FAFT failed to connect DUT after reboot.
306
307 Args:
308 next_step: Optional, a FAFT_STEP dict of the next step, which is used
309 for diagnostic.
310
311 Raises:
312 error.TestFail: This exception is already raised, with a decription
313 why it failed.
314 """
315 next_checker_matched = False
316 if next_step is not None:
317 next_test = {}
318 next_test.update(self._faft_template)
319 next_test.update(next_step)
320
321 # TODO(waihong@chromium.org): Implement replugging the Ethernet to
322 # identify if it is a network flaky.
323
324 recovery_reason = self._retrieve_recovery_reason_from_trap()
325 if next_step is not None and recovery_reason:
326 if self._call_action(next_test['state_checker']):
327 # Repluging the USB can pass the state_checker of the next step,
328 # meaning that the firmware failed to boot into USB directly.
329 next_checker_matched = True
330
331 # Reset client to a workable state.
332 self._reset_client()
333
334 # Raise the proper TestFail exception.
335 if next_checker_matched:
336 raise error.TestFail('Firmware failed to auto-boot USB in the '
Tom Wai-Hong Tam66648432012-11-07 10:06:00 +0800337 'recovery boot (reason: %d)' % recovery_reason)
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800338 elif recovery_reason:
Tom Wai-Hong Tam66648432012-11-07 10:06:00 +0800339 raise error.TestFail('Trapped in the recovery screen (reason: %d) '
340 'and timed out' % recovery_reason)
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800341 else:
Tom Wai-Hong Tam66648432012-11-07 10:06:00 +0800342 raise error.TestFail('Timed out waiting for DUT reboot')
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +0800343
344
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800345 def assert_test_image_in_path(self, image_path):
346 """Assert the image of image_path be a Chrome OS test image.
347
348 Args:
349 image_path: A path on the host to the test image.
350
351 Raises:
352 error.TestError: if the image is not a test image.
353 """
354 try:
Tom Wai-Hong Tam5929b1e2012-11-07 09:40:42 +0800355 build_ver, build_hash = lab_test.VerifyImageAndGetId(
356 os.environ['CROS_WORKON_SRCROOT'],
357 image_path)
Vic Yang772df8a2012-10-31 10:10:49 +0800358 logging.info('Build of image: %s %s', build_ver, build_hash)
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800359 except ChromeOSTestError:
360 raise error.TestError(
361 'An USB disk containning a test image should be plugged '
362 'in the servo board.')
363
364
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800365 def assert_test_image_in_usb_disk(self, usb_dev=None):
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800366 """Assert an USB disk plugged-in on servo and a test image inside.
367
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800368 Args:
369 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
370 If None, it is detected automatically.
371
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800372 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800373 error.TestError: if USB disk not detected or not a test image.
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800374 """
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800375 if self.check_setup_done('usb_check'):
Vic Yang54f70572012-10-19 17:05:26 +0800376 return
377
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800378 # TODO(waihong@chromium.org): We skip the check when servod runs in
379 # a different host since no easy way to access the servo host so far.
380 # Should find a way to work-around it.
381 if not self.servo.is_localhost():
382 logging.info('Skip checking Chrome OS test image in USB as servod '
383 'runs in a different host.')
384 return
385
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800386 if usb_dev:
387 assert self.servo.get('usb_mux_sel1') == 'servo_sees_usbkey'
388 else:
Vadim Bendeburycacf29f2012-07-30 17:49:11 -0700389 self.servo.enable_usb_hub(host=True)
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800390 usb_dev = self.servo.probe_host_usb_dev()
391 if not usb_dev:
392 raise error.TestError(
393 'An USB disk should be plugged in the servo board.')
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800394 self.assert_test_image_in_path(usb_dev)
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800395 self.mark_setup_done('usb_check')
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800396
397
Tom Wai-Hong Tam893bcc02012-11-02 16:13:34 +0800398 def setup_usbkey(self, usbkey, host=None):
399 """Setup the USB disk for the test.
400
401 It checks the setup of USB disk and a valid ChromeOS test image inside.
402 It also muxes the USB disk to either the host or DUT by request.
403
404 Args:
405 usbkey: True if the USB disk is required for the test, False if not
406 required.
407 host: Optional, True to mux the USB disk to host, False to mux it
408 to DUT, default to do nothing.
409 """
410 if usbkey:
411 self.assert_test_image_in_usb_disk()
412 elif host is None:
413 # USB disk is not required for the test. Better to mux it to host.
414 host = True
415
416 if host is True:
417 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
418 elif host is False:
419 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
420
421
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800422 def get_server_address(self):
423 """Get the server address seen from the client.
424
425 Returns:
426 A string of the server address.
427 """
428 r = self.faft_client.run_shell_command_get_output("echo $SSH_CLIENT")
429 return r[0].split()[0]
430
431
Simran Basi741b5d42012-05-18 11:27:15 -0700432 def install_test_image(self, image_path=None, firmware_update=False):
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800433 """Install the test image specied by the path onto the USB and DUT disk.
434
435 The method first copies the image to USB disk and reboots into it via
Mike Truty49153d82012-08-21 22:27:30 -0500436 recovery mode. Then runs 'chromeos-install' (and possible
437 chromeos-firmwareupdate') to install it to DUT disk.
438
439 Sample command line:
440
441 run_remote_tests.sh --servo --board=daisy --remote=w.x.y.z \
442 --args="image=/tmp/chromiumos_test_image.bin firmware_update=True" \
443 server/site_tests/firmware_XXXX/control
444
445 This test requires an automated recovery to occur while simulating
446 inserting and removing the usb key from the servo. To allow this the
447 following hardware setup is required:
448 1. servo2 board connected via servoflex.
449 2. USB key inserted in the servo2.
450 3. servo2 connected to the dut via dut_hub_in in the usb 2.0 slot.
451 4. network connected via usb dongle in the dut in usb 3.0 slot.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800452
453 Args:
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800454 image_path: An URL or a path on the host to the test image.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800455 firmware_update: Also update the firmware after installing.
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800456
457 Raises:
458 error.TestError: If devserver failed to start.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800459 """
Tom Wai-Hong Tam19ad9682012-10-24 09:33:42 +0800460 if not image_path:
461 return
462
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800463 if self.check_setup_done('reimage'):
464 return
465
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800466 if image_path.startswith(self._HTTP_PREFIX):
467 # TODO(waihong@chromium.org): Add the check of the URL to ensure
468 # it is a test image.
469 devserver = None
470 image_url = image_path
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800471 elif self.servo.is_localhost():
472 self.assert_test_image_in_path(image_path)
473 # If servod is localhost, i.e. both servod and FAFT see the same
474 # file system, do nothing.
475 devserver = None
476 image_url = image_path
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800477 else:
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800478 self.assert_test_image_in_path(image_path)
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800479 image_dir, image_base = os.path.split(image_path)
480 logging.info('Starting devserver to serve the image...')
481 # The following stdout and stderr arguments should not be None,
482 # even we don't use them. Otherwise, the socket of devserve is
483 # created as fd 1 (as no stdout) but it still thinks stdout is fd
484 # 1 and dump the log to the socket. Wrong HTTP protocol happens.
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800485 devserver = subprocess.Popen(['/usr/lib/devserver/devserver.py',
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800486 '--archive_dir=%s' % image_dir,
487 '--port=%s' % self._DEVSERVER_PORT],
488 stdout=subprocess.PIPE,
489 stderr=subprocess.PIPE)
490 image_url = '%s%s:%s/static/archive/%s' % (
491 self._HTTP_PREFIX,
492 self.get_server_address(),
493 self._DEVSERVER_PORT,
494 image_base)
495
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800496 # Wait devserver startup completely
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800497 time.sleep(self.delay.devserver)
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800498 # devserver is a service running forever. If it is terminated,
499 # some error does happen.
500 if devserver.poll():
501 raise error.TestError('Starting devserver failed, '
502 'returning %d.' % devserver.returncode)
503
Vic Yang772df8a2012-10-31 10:10:49 +0800504 logging.info('Ask Servo to install the image from %s', image_url)
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800505 self.servo.image_to_servo_usb(image_url)
506
507 if devserver and devserver.poll() is None:
508 logging.info('Shutting down devserver...')
509 devserver.terminate()
Mike Truty49153d82012-08-21 22:27:30 -0500510
511 # DUT is powered off while imaging servo USB.
512 # Now turn it on.
513 self.servo.power_short_press()
514 self.wait_for_client()
515 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
516
517 install_cmd = 'chromeos-install --yes'
518 if firmware_update:
519 install_cmd += ' && chromeos-firmwareupdate --mode recovery'
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +0800520 self.backup_firmware()
Mike Truty49153d82012-08-21 22:27:30 -0500521
522 self.register_faft_sequence((
523 { # Step 1, request recovery boot
Vic Yangf93f7022012-10-31 09:40:36 +0800524 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500525 'mainfw_type': ('developer', 'normal'),
526 }),
527 'userspace_action': self.faft_client.request_recovery_boot,
528 'firmware_action': self.wait_fw_screen_and_plug_usb,
529 'install_deps_after_boot': True,
530 },
531 { # Step 2, expected recovery boot
Vic Yangf93f7022012-10-31 09:40:36 +0800532 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500533 'mainfw_type': 'recovery',
Tom Wai-Hong Tam6ec46e32012-10-05 16:39:21 +0800534 'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
Mike Truty49153d82012-08-21 22:27:30 -0500535 }),
536 'userspace_action': (self.faft_client.run_shell_command,
537 install_cmd),
538 'reboot_action': self.cold_reboot,
539 'install_deps_after_boot': True,
540 },
541 { # Step 3, expected normal or developer boot (not recovery)
Vic Yangf93f7022012-10-31 09:40:36 +0800542 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500543 'mainfw_type': ('developer', 'normal')
544 }),
545 },
546 ))
547 self.run_faft_sequence()
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +0800548
549 if firmware_update:
550 self.clear_saved_firmware()
551
Mike Truty49153d82012-08-21 22:27:30 -0500552 # 'Unplug' any USB keys in the servo from the dut.
Tom Wai-Hong Tam953c7742012-10-16 21:09:31 +0800553 self.servo.enable_usb_hub(host=True)
Tom Wai-Hong Tam6668b762012-10-23 11:45:36 +0800554 # Mark usb_check done so it won't check a test image in USB anymore.
555 self.mark_setup_done('usb_check')
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800556 self.mark_setup_done('reimage')
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800557
558
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800559 def clear_set_gbb_flags(self, clear_mask, set_mask):
560 """Clear and set the GBB flags in the current flashrom.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800561
562 Args:
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800563 clear_mask: A mask of flags to be cleared.
564 set_mask: A mask of flags to be set.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800565 """
566 gbb_flags = self.faft_client.get_gbb_flags()
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800567 new_flags = gbb_flags & ctypes.c_uint32(~clear_mask).value | set_mask
568
569 if (gbb_flags != new_flags):
Vic Yang772df8a2012-10-31 10:10:49 +0800570 logging.info('Change the GBB flags from 0x%x to 0x%x.',
571 gbb_flags, new_flags)
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800572 self.faft_client.run_shell_command(
Tom Wai-Hong Tamfda76e22012-08-08 17:19:10 +0800573 '/usr/share/vboot/bin/set_gbb_flags.sh 0x%x' % new_flags)
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +0800574 self.faft_client.reload_firmware()
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800575 # If changing FORCE_DEV_SWITCH_ON flag, reboot to get a clear state
Tom Wai-Hong Tam6ec46e32012-10-05 16:39:21 +0800576 if ((gbb_flags ^ new_flags) & vboot.GBB_FLAG_FORCE_DEV_SWITCH_ON):
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800577 self.run_faft_step({
578 'firmware_action': self.wait_fw_screen_and_ctrl_d,
579 })
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800580
581
Vic Yang772df8a2012-10-31 10:10:49 +0800582 def check_ec_capability(self, required_cap=None, suppress_warning=False):
Vic Yang4d72cb62012-07-24 11:51:09 +0800583 """Check if current platform has required EC capabilities.
584
585 Args:
586 required_cap: A list containing required EC capabilities. Pass in
587 None to only check for presence of Chrome EC.
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800588 suppress_warning: True to suppress any warning messages.
Vic Yang4d72cb62012-07-24 11:51:09 +0800589
590 Returns:
591 True if requirements are met. Otherwise, False.
592 """
593 if not self.client_attr.chrome_ec:
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800594 if not suppress_warning:
595 logging.warn('Requires Chrome EC to run this test.')
Vic Yang4d72cb62012-07-24 11:51:09 +0800596 return False
597
Vic Yang772df8a2012-10-31 10:10:49 +0800598 if not required_cap:
599 return True
600
Vic Yang4d72cb62012-07-24 11:51:09 +0800601 for cap in required_cap:
602 if cap not in self.client_attr.ec_capability:
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800603 if not suppress_warning:
604 logging.warn('Requires EC capability "%s" to run this '
Vic Yang772df8a2012-10-31 10:10:49 +0800605 'test.', cap)
Vic Yang4d72cb62012-07-24 11:51:09 +0800606 return False
607
608 return True
609
610
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800611 def check_root_part_on_non_recovery(self, part):
612 """Check the partition number of root device and on normal/dev boot.
613
614 Returns:
615 True if the root device matched and on normal/dev boot;
616 otherwise, False.
617 """
Vic Yangf93f7022012-10-31 09:40:36 +0800618 return self.checkers.root_part_checker(part) and \
619 self.checkers.crossystem_checker({
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800620 'mainfw_type': ('normal', 'developer'),
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800621 })
622
623
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800624 def _join_part(self, dev, part):
625 """Return a concatenated string of device and partition number.
626
627 Args:
628 dev: A string of device, e.g.'/dev/sda'.
629 part: A string of partition number, e.g.'3'.
630
631 Returns:
632 A concatenated string of device and partition number, e.g.'/dev/sda3'.
633
634 >>> seq = FAFTSequence()
635 >>> seq._join_part('/dev/sda', '3')
636 '/dev/sda3'
637 >>> seq._join_part('/dev/mmcblk0', '2')
638 '/dev/mmcblk0p2'
639 """
640 if 'mmcblk' in dev:
641 return dev + 'p' + part
642 else:
643 return dev + part
644
645
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800646 def copy_kernel_and_rootfs(self, from_part, to_part):
647 """Copy kernel and rootfs from from_part to to_part.
648
649 Args:
650 from_part: A string of partition number to be copied from.
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800651 to_part: A string of partition number to be copied to.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800652 """
653 root_dev = self.faft_client.get_root_dev()
Vic Yang772df8a2012-10-31 10:10:49 +0800654 logging.info('Copying kernel from %s to %s. Please wait...',
Vic Yang55f31252012-11-01 17:07:54 +0800655 from_part, to_part)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800656 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800657 (self._join_part(root_dev, self.KERNEL_MAP[from_part]),
658 self._join_part(root_dev, self.KERNEL_MAP[to_part])))
Vic Yang772df8a2012-10-31 10:10:49 +0800659 logging.info('Copying rootfs from %s to %s. Please wait...',
Vic Yang55f31252012-11-01 17:07:54 +0800660 from_part, to_part)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800661 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800662 (self._join_part(root_dev, self.ROOTFS_MAP[from_part]),
663 self._join_part(root_dev, self.ROOTFS_MAP[to_part])))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800664
665
666 def ensure_kernel_boot(self, part):
667 """Ensure the request kernel boot.
668
669 If not, it duplicates the current kernel to the requested kernel
670 and sets the requested higher priority to ensure it boot.
671
672 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800673 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800674 """
Vic Yangf93f7022012-10-31 09:40:36 +0800675 if not self.checkers.root_part_checker(part):
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800676 if self.faft_client.diff_kernel_a_b():
677 self.copy_kernel_and_rootfs(
678 from_part=self.OTHER_KERNEL_MAP[part],
679 to_part=part)
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800680 self.run_faft_step({
681 'userspace_action': (self.reset_and_prioritize_kernel, part),
682 })
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800683
684
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800685 def set_hardware_write_protect(self, enable):
Vic Yang2cabf812012-08-28 02:39:04 +0800686 """Set hardware write protect pin.
687
688 Args:
689 enable: True if asserting write protect pin. Otherwise, False.
690 """
691 self.servo.set('fw_wp_vref', self.client_attr.wp_voltage)
692 self.servo.set('fw_wp_en', 'on')
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800693 self.servo.set('fw_wp', 'on' if enable else 'off')
Vic Yang416f2032012-08-28 10:18:03 +0800694
695
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800696 def set_EC_write_protect_and_reboot(self, enable):
Vic Yang416f2032012-08-28 10:18:03 +0800697 """Set EC write protect status and reboot to take effect.
698
699 EC write protect is only activated if both hardware write protect pin
700 is asserted and software write protect flag is set. Also, a reboot is
701 required for write protect to take effect.
702
703 Since the software write protect flag cannot be unset if hardware write
704 protect pin is asserted, we need to deasserted the pin first if we are
705 deactivating write protect. Similarly, a reboot is required before we
706 can modify the software flag.
707
708 This method asserts/deasserts hardware write protect pin first, and
709 set corresponding EC software write protect flag.
710
711 Args:
712 enable: True if activating EC write protect. Otherwise, False.
713 """
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800714 self.set_hardware_write_protect(enable)
715 if enable:
Vic Yang416f2032012-08-28 10:18:03 +0800716 # Set write protect flag and reboot to take effect.
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800717 self.ec.set_flash_write_protect(enable)
Vic Yang416f2032012-08-28 10:18:03 +0800718 self.sync_and_ec_reboot()
719 else:
720 # Reboot after deasserting hardware write protect pin to deactivate
721 # write protect. And then remove software write protect flag.
722 self.sync_and_ec_reboot()
Tom Wai-Hong Tam05574ee2012-10-30 11:34:21 +0800723 self.ec.set_flash_write_protect(enable)
Vic Yang2cabf812012-08-28 02:39:04 +0800724
725
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800726 def press_ctrl_d(self):
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800727 """Send Ctrl-D key to DUT."""
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800728 if not self.client_attr.has_keyboard:
729 logging.info('Running usbkm232-ctrld...')
730 os.system('usbkm232-ctrld')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800731 else:
732 self.servo.ctrl_d()
733
734
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800735 def press_ctrl_u(self):
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800736 """Send Ctrl-U key to DUT.
737
738 Raises:
739 error.TestError: if a non-Chrome EC device or no Ctrl-U command given
740 on a no-build-in-keyboard device.
741 """
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800742 if not self.client_attr.has_keyboard:
743 logging.info('Running usbkm232-ctrlu...')
744 os.system('usbkm232-ctrlu')
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800745 elif self.check_ec_capability(['keyboard'], suppress_warning=True):
746 self.ec.key_down('<ctrl_l>')
747 self.ec.key_down('u')
748 self.ec.key_up('u')
749 self.ec.key_up('<ctrl_l>')
750 elif self.client_attr.has_keyboard:
751 raise error.TestError(
752 "Can't send Ctrl-U to DUT without using Chrome EC.")
753 else:
754 raise error.TestError(
755 "Should specify the ctrl_u_cmd argument.")
756
757
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800758 def press_enter(self):
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800759 """Send Enter key to DUT."""
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800760 if not self.client_attr.has_keyboard:
761 logging.info('Running usbkm232-enter...')
762 os.system('usbkm232-enter')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800763 else:
764 self.servo.enter_key()
765
766
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800767 def wait_fw_screen_and_ctrl_d(self):
768 """Wait for firmware warning screen and press Ctrl-D."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800769 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800770 self.press_ctrl_d()
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800771
772
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800773 def wait_fw_screen_and_ctrl_u(self):
774 """Wait for firmware warning screen and press Ctrl-U."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800775 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800776 self.press_ctrl_u()
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800777
778
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800779 def wait_fw_screen_and_trigger_recovery(self, need_dev_transition=False):
780 """Wait for firmware warning screen and trigger recovery boot."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800781 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800782 self.press_enter()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800783
784 # For Alex/ZGB, there is a dev warning screen in text mode.
785 # Skip it by pressing Ctrl-D.
786 if need_dev_transition:
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800787 time.sleep(self.delay.legacy_text_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800788 self.press_ctrl_d()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800789
790
Mike Truty49153d82012-08-21 22:27:30 -0500791 def wait_fw_screen_and_unplug_usb(self):
792 """Wait for firmware warning screen and then unplug the servo USB."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800793 time.sleep(self.delay.load_usb)
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800794 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800795 time.sleep(self.delay.between_usb_plug)
Mike Truty49153d82012-08-21 22:27:30 -0500796
797
798 def wait_fw_screen_and_plug_usb(self):
799 """Wait for firmware warning screen and then unplug and plug the USB."""
800 self.wait_fw_screen_and_unplug_usb()
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800801 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
802
803
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800804 def wait_fw_screen_and_press_power(self):
805 """Wait for firmware warning screen and press power button."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800806 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam7317c042012-08-14 11:59:06 +0800807 # While the firmware screen, the power button probing loop sleeps
808 # 0.25 second on every scan. Use the normal delay (1.2 second) for
809 # power press.
810 self.servo.power_normal_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800811
812
Tom Wai-Hong Tam4f5e5922012-07-27 16:23:15 +0800813 def wait_longer_fw_screen_and_press_power(self):
814 """Wait for firmware screen without timeout and press power button."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800815 time.sleep(self.delay.dev_screen_timeout)
Tom Wai-Hong Tam4f5e5922012-07-27 16:23:15 +0800816 self.wait_fw_screen_and_press_power()
817
818
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800819 def wait_fw_screen_and_close_lid(self):
820 """Wait for firmware warning screen and close lid."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800821 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800822 self.servo.lid_close()
823
824
Tom Wai-Hong Tam473cfa72012-07-27 17:16:57 +0800825 def wait_longer_fw_screen_and_close_lid(self):
826 """Wait for firmware screen without timeout and close lid."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800827 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam473cfa72012-07-27 17:16:57 +0800828 self.wait_fw_screen_and_close_lid()
829
830
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800831 def setup_gbb_flags(self):
832 """Setup the GBB flags for FAFT test."""
833 if self.check_setup_done('gbb_flags'):
834 return
835
836 logging.info('Set proper GBB flags for test.')
837 self.clear_set_gbb_flags(vboot.GBB_FLAG_DEV_SCREEN_SHORT_DELAY |
838 vboot.GBB_FLAG_FORCE_DEV_SWITCH_ON |
839 vboot.GBB_FLAG_FORCE_DEV_BOOT_USB |
840 vboot.GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK,
841 vboot.GBB_FLAG_ENTER_TRIGGERS_TONORM)
842 self.mark_setup_done('gbb_flags')
843
844
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800845 def setup_tried_fwb(self, tried_fwb):
846 """Setup for fw B tried state.
847
848 It makes sure the system in the requested fw B tried state. If not, it
849 tries to do so.
850
851 Args:
852 tried_fwb: True if requested in tried_fwb=1; False if tried_fwb=0.
853 """
854 if tried_fwb:
Vic Yangf93f7022012-10-31 09:40:36 +0800855 if not self.checkers.crossystem_checker({'tried_fwb': '1'}):
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800856 logging.info(
857 'Firmware is not booted with tried_fwb. Reboot into it.')
858 self.run_faft_step({
859 'userspace_action': self.faft_client.set_try_fw_b,
860 })
861 else:
Vic Yangf93f7022012-10-31 09:40:36 +0800862 if not self.checkers.crossystem_checker({'tried_fwb': '0'}):
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800863 logging.info(
864 'Firmware is booted with tried_fwb. Reboot to clear.')
865 self.run_faft_step({})
866
867
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800868 def enable_rec_mode_and_reboot(self):
869 """Switch to rec mode and reboot.
870
871 This method emulates the behavior of the old physical recovery switch,
872 i.e. switch ON + reboot + switch OFF, and the new keyboard controlled
873 recovery mode, i.e. just press Power + Esc + Refresh.
874 """
Tom Wai-Hong Tam80419a82012-10-30 09:10:00 +0800875 if self.client_attr.chrome_ec:
Vic Yang81273092012-08-21 15:57:09 +0800876 # Cold reset to clear EC_IN_RW signal
Vic Yanga7250662012-08-31 04:00:08 +0800877 self.servo.set('cold_reset', 'on')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800878 time.sleep(self.delay.hold_cold_reset)
Vic Yanga7250662012-08-31 04:00:08 +0800879 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800880 time.sleep(self.delay.ec_boot_to_console)
Tom Wai-Hong Tambb92e6c2012-10-30 11:06:09 +0800881 self.ec.reboot("ap-off")
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800882 time.sleep(self.delay.ec_boot_to_console)
Tom Wai-Hong Tam665281c2012-10-30 11:55:10 +0800883 self.ec.set_hostevent(chrome_ec.HOSTEVENT_KEYBOARD_RECOVERY)
Vic Yang611dd852012-08-02 15:36:31 +0800884 self.servo.power_short_press()
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800885 else:
886 self.servo.enable_recovery_mode()
887 self.cold_reboot()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800888 time.sleep(self.delay.ec_reboot_cmd)
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800889 self.servo.disable_recovery_mode()
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800890
891
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800892 def enable_dev_mode_and_reboot(self):
893 """Switch to developer mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800894 if self.client_attr.keyboard_dev:
895 self.enable_keyboard_dev_mode()
896 else:
897 self.servo.enable_development_mode()
898 self.faft_client.run_shell_command(
899 'chromeos-firmwareupdate --mode todev && reboot')
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800900
901
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800902 def enable_normal_mode_and_reboot(self):
903 """Switch to normal mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800904 if self.client_attr.keyboard_dev:
905 self.disable_keyboard_dev_mode()
906 else:
907 self.servo.disable_development_mode()
908 self.faft_client.run_shell_command(
909 'chromeos-firmwareupdate --mode tonormal && reboot')
910
911
912 def wait_fw_screen_and_switch_keyboard_dev_mode(self, dev):
913 """Wait for firmware screen and then switch into or out of dev mode.
914
915 Args:
916 dev: True if switching into dev mode. Otherwise, False.
917 """
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800918 time.sleep(self.delay.firmware_screen)
Vic Yange7553162012-06-20 16:20:47 +0800919 if dev:
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800920 self.press_ctrl_d()
Vic Yange7553162012-06-20 16:20:47 +0800921 else:
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800922 self.press_enter()
Vic Yang0dc84b82012-10-31 12:29:39 +0800923 time.sleep(self.delay.confirm_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800924 self.press_enter()
Vic Yange7553162012-06-20 16:20:47 +0800925
926
927 def enable_keyboard_dev_mode(self):
928 logging.info("Enabling keyboard controlled developer mode")
Tom Wai-Hong Tamf1a17d72012-07-26 11:39:52 +0800929 # Plug out USB disk for preventing recovery boot without warning
930 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Vic Yange7553162012-06-20 16:20:47 +0800931 # Rebooting EC with rec mode on. Should power on AP.
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800932 self.enable_rec_mode_and_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +0800933 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +0800934 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=True)
Vic Yange7553162012-06-20 16:20:47 +0800935
936
937 def disable_keyboard_dev_mode(self):
938 logging.info("Disabling keyboard controlled developer mode")
Tom Wai-Hong Tamb0b3f412012-08-13 17:17:06 +0800939 if not self.client_attr.chrome_ec:
Vic Yang611dd852012-08-02 15:36:31 +0800940 self.servo.disable_recovery_mode()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800941 self.cold_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +0800942 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +0800943 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=False)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800944
945
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800946 def setup_dev_mode(self, dev_mode):
947 """Setup for development mode.
948
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800949 It makes sure the system in the requested normal/dev mode. If not, it
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800950 tries to do so.
951
952 Args:
953 dev_mode: True if requested in dev mode; False if normal mode.
954 """
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800955 # Change the default firmware_action for dev mode passing the fw screen.
956 self.register_faft_template({
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800957 'firmware_action': (self.wait_fw_screen_and_ctrl_d if dev_mode
958 else None),
959 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800960 if dev_mode:
Vic Yange7553162012-06-20 16:20:47 +0800961 if (not self.client_attr.keyboard_dev and
Vic Yangf93f7022012-10-31 09:40:36 +0800962 not self.checkers.crossystem_checker({'devsw_cur': '1'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800963 logging.info('Dev switch is not on. Now switch it on.')
964 self.servo.enable_development_mode()
Vic Yangf93f7022012-10-31 09:40:36 +0800965 if not self.checkers.crossystem_checker({'devsw_boot': '1',
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800966 'mainfw_type': 'developer'}):
967 logging.info('System is not in dev mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800968 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +0800969 'userspace_action': None if self.client_attr.keyboard_dev
970 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800971 'chromeos-firmwareupdate --mode todev && reboot'),
Vic Yange7553162012-06-20 16:20:47 +0800972 'reboot_action': self.enable_keyboard_dev_mode if
973 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800974 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800975 else:
Vic Yange7553162012-06-20 16:20:47 +0800976 if (not self.client_attr.keyboard_dev and
Vic Yangf93f7022012-10-31 09:40:36 +0800977 not self.checkers.crossystem_checker({'devsw_cur': '0'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800978 logging.info('Dev switch is not off. Now switch it off.')
979 self.servo.disable_development_mode()
Vic Yangf93f7022012-10-31 09:40:36 +0800980 if not self.checkers.crossystem_checker({'devsw_boot': '0',
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800981 'mainfw_type': 'normal'}):
982 logging.info('System is not in normal mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800983 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +0800984 'userspace_action': None if self.client_attr.keyboard_dev
985 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800986 'chromeos-firmwareupdate --mode tonormal && reboot'),
Vic Yange7553162012-06-20 16:20:47 +0800987 'reboot_action': self.disable_keyboard_dev_mode if
988 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800989 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800990
991
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800992 def setup_kernel(self, part):
993 """Setup for kernel test.
994
995 It makes sure both kernel A and B bootable and the current boot is
996 the requested kernel part.
997
998 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800999 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001000 """
1001 self.ensure_kernel_boot(part)
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +08001002 if self.faft_client.diff_kernel_a_b():
1003 self.copy_kernel_and_rootfs(from_part=part,
1004 to_part=self.OTHER_KERNEL_MAP[part])
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001005 self.reset_and_prioritize_kernel(part)
1006
1007
1008 def reset_and_prioritize_kernel(self, part):
1009 """Make the requested partition highest priority.
1010
1011 This function also reset kerenl A and B to bootable.
1012
1013 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +08001014 part: A string of partition number to be prioritized.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001015 """
1016 root_dev = self.faft_client.get_root_dev()
1017 # Reset kernel A and B to bootable.
1018 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
1019 (self.KERNEL_MAP['a'], root_dev))
1020 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
1021 (self.KERNEL_MAP['b'], root_dev))
1022 # Set kernel part highest priority.
1023 self.faft_client.run_shell_command('cgpt prioritize -i%s %s' %
1024 (self.KERNEL_MAP[part], root_dev))
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +08001025 # Safer to sync and wait until the cgpt status written to the disk.
1026 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001027 time.sleep(self.delay.sync)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +08001028
1029
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001030 def warm_reboot(self):
1031 """Request a warm reboot.
1032
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001033 A wrapper for underlying servo warm reset.
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001034 """
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001035 # Use cold reset if the warm reset is broken.
1036 if self.client_attr.broken_warm_reset:
Gediminas Ramanauskase021e152012-09-04 19:10:59 -07001037 logging.info('broken_warm_reset is True. Cold rebooting instead.')
1038 self.cold_reboot()
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +08001039 else:
1040 self.servo.warm_reset()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001041
1042
1043 def cold_reboot(self):
1044 """Request a cold reboot.
1045
1046 A wrapper for underlying servo cold reset.
1047 """
Gediminas Ramanauskasc6025692012-10-23 14:33:40 -07001048 if self.client_attr.broken_warm_reset:
Tom Wai-Hong Tama276d0a2012-08-22 11:15:17 +08001049 self.servo.set('pwr_button', 'press')
1050 self.servo.set('cold_reset', 'on')
1051 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001052 time.sleep(self.delay.ec_boot_to_pwr_button)
Tom Wai-Hong Tama276d0a2012-08-22 11:15:17 +08001053 self.servo.set('pwr_button', 'release')
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +08001054 elif self.check_ec_capability(suppress_warning=True):
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001055 # We don't use servo.cold_reset() here because software sync is
1056 # not yet finished, and device may or may not come up after cold
1057 # reset. Pressing power button before firmware comes up solves this.
1058 #
1059 # The correct behavior should be (not work now):
1060 # - If rebooting EC with rec mode on, power on AP and it boots
1061 # into recovery mode.
1062 # - If rebooting EC with rec mode off, power on AP for software
1063 # sync. Then AP checks if lid open or not. If lid open, continue;
1064 # otherwise, shut AP down and need servo for a power button
1065 # press.
1066 self.servo.set('cold_reset', 'on')
1067 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001068 time.sleep(self.delay.ec_boot_to_pwr_button)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001069 self.servo.power_short_press()
1070 else:
1071 self.servo.cold_reset()
1072
1073
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001074 def sync_and_warm_reboot(self):
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001075 """Request the client sync and do a warm reboot.
1076
1077 This is the default reboot action on FAFT.
1078 """
1079 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001080 time.sleep(self.delay.sync)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001081 self.warm_reboot()
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001082
1083
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001084 def sync_and_cold_reboot(self):
1085 """Request the client sync and do a cold reboot.
1086
1087 This reboot action is used to reset EC for recovery mode.
1088 """
1089 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001090 time.sleep(self.delay.sync)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001091 self.cold_reboot()
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001092
1093
Tom Wai-Hong Tambb92e6c2012-10-30 11:06:09 +08001094 def sync_and_ec_reboot(self, flags=''):
Vic Yangaeb10392012-08-28 09:25:09 +08001095 """Request the client sync and do a EC triggered reboot.
1096
1097 Args:
Tom Wai-Hong Tambb92e6c2012-10-30 11:06:09 +08001098 flags: Optional, a space-separated string of flags passed to EC
1099 reboot command, including:
1100 default: EC soft reboot;
1101 'hard': EC cold/hard reboot.
Vic Yangaeb10392012-08-28 09:25:09 +08001102 """
Vic Yang59cac9c2012-05-21 15:28:42 +08001103 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001104 time.sleep(self.delay.sync)
Tom Wai-Hong Tambb92e6c2012-10-30 11:06:09 +08001105 self.ec.reboot(flags)
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001106 time.sleep(self.delay.ec_reboot_cmd)
Vic Yangf86728a2012-07-30 10:44:07 +08001107 self.check_lid_and_power_on()
1108
1109
Chun-ting Changa4f65532012-10-17 16:57:28 +08001110 def sync_and_reboot_with_factory_install_shim(self):
1111 """Request the client sync and do a warm reboot to recovery mode.
1112
1113 After reboot, the client will use factory install shim to reset TPM
1114 values. The client ignore TPM rollback, so here forces it to recovery
1115 mode.
1116 """
Vic Yangf93f7022012-10-31 09:40:36 +08001117 is_dev = self.checkers.crossystem_checker({'devsw_boot': '1'})
Chun-ting Changa4f65532012-10-17 16:57:28 +08001118 if not is_dev:
1119 self.enable_dev_mode_and_reboot()
Vic Yangf1fdf712012-10-31 12:09:22 +08001120 time.sleep(self.delay.sync)
Chun-ting Changa4f65532012-10-17 16:57:28 +08001121 self.enable_rec_mode_and_reboot()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001122 time.sleep(self.delay.install_shim_done)
Chun-ting Changa4f65532012-10-17 16:57:28 +08001123 self.warm_reboot()
1124
1125
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +08001126 def full_power_off_and_on(self):
1127 """Shutdown the device by pressing power button and power on again."""
1128 # Press power button to trigger Chrome OS normal shutdown process.
1129 self.servo.power_normal_press()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001130 time.sleep(self.delay.shutdown)
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +08001131 # Short press power button to boot DUT again.
1132 self.servo.power_short_press()
1133
1134
Vic Yangf86728a2012-07-30 10:44:07 +08001135 def check_lid_and_power_on(self):
1136 """
1137 On devices with EC software sync, system powers on after EC reboots if
1138 lid is open. Otherwise, the EC shuts down CPU after about 3 seconds.
1139 This method checks lid switch state and presses power button if
1140 necessary.
1141 """
1142 if self.servo.get("lid_open") == "no":
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001143 time.sleep(self.delay.software_sync)
Vic Yangf86728a2012-07-30 10:44:07 +08001144 self.servo.power_short_press()
Vic Yang59cac9c2012-05-21 15:28:42 +08001145
1146
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001147 def _modify_usb_kernel(self, usb_dev, from_magic, to_magic):
1148 """Modify the kernel header magic in USB stick.
1149
1150 The kernel header magic is the first 8-byte of kernel partition.
1151 We modify it to make it fail on kernel verification check.
1152
1153 Args:
1154 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1155 from_magic: A string of magic which we change it from.
1156 to_magic: A string of magic which we change it to.
1157
1158 Raises:
1159 error.TestError: if failed to change magic.
1160 """
1161 assert len(from_magic) == 8
1162 assert len(to_magic) == 8
Tom Wai-Hong Tama1d9a0f2011-12-23 09:13:33 +08001163 # USB image only contains one kernel.
1164 kernel_part = self._join_part(usb_dev, self.KERNEL_MAP['a'])
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001165 read_cmd = "sudo dd if=%s bs=8 count=1 2>/dev/null" % kernel_part
1166 current_magic = utils.system_output(read_cmd)
1167 if current_magic == to_magic:
Vic Yang772df8a2012-10-31 10:10:49 +08001168 logging.info("The kernel magic is already %s.", current_magic)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001169 return
1170 if current_magic != from_magic:
1171 raise error.TestError("Invalid kernel image on USB: wrong magic.")
1172
Vic Yang772df8a2012-10-31 10:10:49 +08001173 logging.info('Modify the kernel magic in USB, from %s to %s.',
1174 from_magic, to_magic)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001175 write_cmd = ("echo -n '%s' | sudo dd of=%s oflag=sync conv=notrunc "
1176 " 2>/dev/null" % (to_magic, kernel_part))
1177 utils.system(write_cmd)
1178
1179 if utils.system_output(read_cmd) != to_magic:
1180 raise error.TestError("Failed to write new magic.")
1181
1182
1183 def corrupt_usb_kernel(self, usb_dev):
1184 """Corrupt USB kernel by modifying its magic from CHROMEOS to CORRUPTD.
1185
1186 Args:
1187 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1188 """
1189 self._modify_usb_kernel(usb_dev, self.CHROMEOS_MAGIC,
1190 self.CORRUPTED_MAGIC)
1191
1192
1193 def restore_usb_kernel(self, usb_dev):
1194 """Restore USB kernel by modifying its magic from CORRUPTD to CHROMEOS.
1195
1196 Args:
1197 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1198 """
1199 self._modify_usb_kernel(usb_dev, self.CORRUPTED_MAGIC,
1200 self.CHROMEOS_MAGIC)
1201
1202
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001203 def _call_action(self, action_tuple, check_status=False):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001204 """Call the action function with/without arguments.
1205
1206 Args:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001207 action_tuple: A function, or a tuple (function, args, error_msg),
1208 in which, args and error_msg are optional. args is
1209 either a value or a tuple if multiple arguments.
Vic Yang52116d42012-11-05 16:22:34 +08001210 This can also be a list containing multiple function
1211 or tuple. In this case, these actions are called in
1212 sequence.
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001213 check_status: Check the return value of action function. If not
1214 succeed, raises a TestFail exception.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001215
1216 Returns:
1217 The result value of the action function.
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001218
1219 Raises:
1220 error.TestError: An error when the action function is not callable.
1221 error.TestFail: When check_status=True, action function not succeed.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001222 """
Vic Yang52116d42012-11-05 16:22:34 +08001223 if isinstance(action_tuple, list):
1224 return all([self._call_action(action, check_status=check_status)
1225 for action in action_tuple])
1226
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001227 action = action_tuple
1228 args = ()
1229 error_msg = 'Not succeed'
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001230 if isinstance(action_tuple, tuple):
1231 action = action_tuple[0]
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001232 if len(action_tuple) >= 2:
1233 args = action_tuple[1]
1234 if not isinstance(args, tuple):
1235 args = (args,)
1236 if len(action_tuple) >= 3:
Tom Wai-Hong Tamff560882012-10-15 16:50:06 +08001237 error_msg = action_tuple[2]
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001238
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001239 if action is None:
1240 return
1241
1242 if not callable(action):
1243 raise error.TestError('action is not callable!')
1244
1245 info_msg = 'calling %s' % str(action)
1246 if args:
1247 info_msg += ' with args %s' % str(args)
1248 logging.info(info_msg)
1249 ret = action(*args)
1250
1251 if check_status and not ret:
1252 raise error.TestFail('%s: %s returning %s' %
1253 (error_msg, info_msg, str(ret)))
1254 return ret
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001255
1256
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001257 def run_shutdown_process(self, shutdown_action, pre_power_action=None,
1258 post_power_action=None):
1259 """Run shutdown_action(), which makes DUT shutdown, and power it on.
1260
1261 Args:
1262 shutdown_action: a function which makes DUT shutdown, like pressing
1263 power key.
1264 pre_power_action: a function which is called before next power on.
1265 post_power_action: a function which is called after next power on.
1266
1267 Raises:
1268 error.TestFail: if the shutdown_action() failed to turn DUT off.
1269 """
1270 self._call_action(shutdown_action)
1271 logging.info('Wait to ensure DUT shut down...')
1272 try:
1273 self.wait_for_client()
1274 raise error.TestFail(
1275 'Should shut the device down after calling %s.' %
1276 str(shutdown_action))
1277 except AssertionError:
1278 logging.info(
1279 'DUT is surely shutdown. We are going to power it on again...')
1280
1281 if pre_power_action:
1282 self._call_action(pre_power_action)
Tom Wai-Hong Tam610262a2012-01-12 14:16:53 +08001283 self.servo.power_short_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001284 if post_power_action:
1285 self._call_action(post_power_action)
1286
1287
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001288 def register_faft_template(self, template):
1289 """Register FAFT template, the default FAFT_STEP of each step.
1290
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001291 Any missing field falls back to the original faft_template.
1292
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001293 Args:
1294 template: A FAFT_STEP dict.
1295 """
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001296 self._faft_template.update(template)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001297
1298
1299 def register_faft_sequence(self, sequence):
1300 """Register FAFT sequence.
1301
1302 Args:
1303 sequence: A FAFT_SEQUENCE array which consisted of FAFT_STEP dicts.
1304 """
1305 self._faft_sequence = sequence
1306
1307
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001308 def run_faft_step(self, step, no_reboot=False, next_step=None):
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001309 """Run a single FAFT step.
1310
1311 Any missing field falls back to faft_template. An empty step means
1312 running the default faft_template.
1313
1314 Args:
1315 step: A FAFT_STEP dict.
1316 no_reboot: True to prevent running reboot_action and firmware_action.
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001317 next_step: Optional, a FAFT_STEP dict of the next step, which is used
1318 for diagnostic.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001319
1320 Raises:
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001321 error.TestError: An error when the given step is not valid.
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +08001322 error.TestFail: Test failed in waiting DUT reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001323 """
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001324 FAFT_STEP_KEYS = ('state_checker', 'userspace_action', 'reboot_action',
1325 'firmware_action', 'install_deps_after_boot')
1326
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001327 test = {}
1328 test.update(self._faft_template)
1329 test.update(step)
1330
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001331 for key in test:
1332 if key not in FAFT_STEP_KEYS:
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001333 raise error.TestError('Invalid key in FAFT step: %s', key)
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001334
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001335 if test['state_checker']:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001336 self._call_action(test['state_checker'], check_status=True)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001337
1338 self._call_action(test['userspace_action'])
1339
1340 # Don't run reboot_action and firmware_action if no_reboot is True.
1341 if not no_reboot:
1342 self._call_action(test['reboot_action'])
1343 self.wait_for_client_offline()
1344 self._call_action(test['firmware_action'])
1345
Vic Yang8eaf5ad2012-09-13 14:05:37 +08001346 try:
1347 if 'install_deps_after_boot' in test:
1348 self.wait_for_client(
1349 install_deps=test['install_deps_after_boot'])
1350 else:
1351 self.wait_for_client()
1352 except AssertionError:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001353 logging.info('wait_for_client() timed out.')
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001354 self._restore_routine_from_timeout(next_step)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001355
1356
1357 def run_faft_sequence(self):
1358 """Run FAFT sequence which was previously registered."""
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001359 sequence = self._faft_sequence
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001360 for index, step in enumerate(sequence):
Vic Yang772df8a2012-10-31 10:10:49 +08001361 logging.info('======== Running FAFT sequence step %d ========',
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001362 index + 1)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001363 # Don't reboot in the last step.
Tom Wai-Hong Tamc285f4c2012-10-30 15:44:39 +08001364 if index == len(sequence) - 1:
1365 self.run_faft_step(step, no_reboot=True)
1366 else:
1367 self.run_faft_step(step, next_step=sequence[index + 1])
ctchang38ae4922012-09-03 17:01:16 +08001368
1369
ctchang38ae4922012-09-03 17:01:16 +08001370 def get_current_firmware_sha(self):
1371 """Get current firmware sha of body and vblock.
1372
1373 Returns:
1374 Current firmware sha follows the order (
1375 vblock_a_sha, body_a_sha, vblock_b_sha, body_b_sha)
1376 """
1377 current_firmware_sha = (self.faft_client.get_firmware_sig_sha('a'),
1378 self.faft_client.get_firmware_sha('a'),
1379 self.faft_client.get_firmware_sig_sha('b'),
1380 self.faft_client.get_firmware_sha('b'))
1381 return current_firmware_sha
1382
1383
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001384 def is_firmware_changed(self):
1385 """Check if the current firmware changed, by comparing its SHA.
ctchang38ae4922012-09-03 17:01:16 +08001386
1387 Returns:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001388 True if it is changed, otherwise Flase.
ctchang38ae4922012-09-03 17:01:16 +08001389 """
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001390 # Device may not be rebooted after test.
1391 self.faft_client.reload_firmware()
ctchang38ae4922012-09-03 17:01:16 +08001392
1393 current_sha = self.get_current_firmware_sha()
1394
1395 if current_sha == self._backup_firmware_sha:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001396 return False
ctchang38ae4922012-09-03 17:01:16 +08001397 else:
ctchang38ae4922012-09-03 17:01:16 +08001398 corrupt_VBOOTA = (current_sha[0] != self._backup_firmware_sha[0])
1399 corrupt_FVMAIN = (current_sha[1] != self._backup_firmware_sha[1])
1400 corrupt_VBOOTB = (current_sha[2] != self._backup_firmware_sha[2])
1401 corrupt_FVMAINB = (current_sha[3] != self._backup_firmware_sha[3])
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001402 logging.info("Firmware changed:")
Vic Yang772df8a2012-10-31 10:10:49 +08001403 logging.info('VBOOTA is changed: %s', corrupt_VBOOTA)
1404 logging.info('VBOOTB is changed: %s', corrupt_VBOOTB)
1405 logging.info('FVMAIN is changed: %s', corrupt_FVMAIN)
1406 logging.info('FVMAINB is changed: %s', corrupt_FVMAINB)
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001407 return True
ctchang38ae4922012-09-03 17:01:16 +08001408
1409
1410 def backup_firmware(self, suffix='.original'):
1411 """Backup firmware to file, and then send it to host.
1412
1413 Args:
1414 suffix: a string appended to backup file name
1415 """
1416 remote_temp_dir = self.faft_client.create_temp_dir()
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001417 self.faft_client.dump_firmware(os.path.join(remote_temp_dir, 'bios'))
1418 self._client.get_file(os.path.join(remote_temp_dir, 'bios'),
1419 os.path.join(self.resultsdir, 'bios' + suffix))
ctchang38ae4922012-09-03 17:01:16 +08001420
1421 self._backup_firmware_sha = self.get_current_firmware_sha()
Vic Yang772df8a2012-10-31 10:10:49 +08001422 logging.info('Backup firmware stored in %s with suffix %s',
1423 self.resultsdir, suffix)
ctchang38ae4922012-09-03 17:01:16 +08001424
1425
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001426 def is_firmware_saved(self):
1427 """Check if a firmware saved (called backup_firmware before).
1428
1429 Returns:
1430 True if the firmware is backuped; otherwise False.
1431 """
1432 return self._backup_firmware_sha != ()
1433
1434
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +08001435 def clear_saved_firmware(self):
1436 """Clear the firmware saved by the method backup_firmware."""
1437 self._backup_firmware_sha = ()
1438
1439
ctchang38ae4922012-09-03 17:01:16 +08001440 def restore_firmware(self, suffix='.original'):
1441 """Restore firmware from host in resultsdir.
1442
1443 Args:
1444 suffix: a string appended to backup file name
1445 """
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001446 if not self.is_firmware_changed():
ctchang38ae4922012-09-03 17:01:16 +08001447 return
1448
1449 # Backup current corrupted firmware.
1450 self.backup_firmware(suffix='.corrupt')
1451
1452 # Restore firmware.
1453 remote_temp_dir = self.faft_client.create_temp_dir()
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001454 self._client.send_file(os.path.join(self.resultsdir, 'bios' + suffix),
1455 os.path.join(remote_temp_dir, 'bios'))
ctchang38ae4922012-09-03 17:01:16 +08001456
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001457 self.faft_client.write_firmware(os.path.join(remote_temp_dir, 'bios'))
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001458 self.sync_and_warm_reboot()
1459 self.wait_for_client_offline()
1460 self.wait_for_client()
1461
ctchang38ae4922012-09-03 17:01:16 +08001462 logging.info('Successfully restore firmware.')
Chun-ting Changf91ee0f2012-09-17 18:31:54 +08001463
1464
1465 def setup_firmwareupdate_shellball(self, shellball=None):
1466 """Deside a shellball to use in firmware update test.
1467
1468 Check if there is a given shellball, and it is a shell script. Then,
1469 send it to the remote host. Otherwise, use
1470 /usr/sbin/chromeos-firmwareupdate.
1471
1472 Args:
1473 shellball: path of a shellball or default to None.
1474
1475 Returns:
1476 Path of shellball in remote host.
1477 If use default shellball, reutrn None.
1478 """
1479 updater_path = None
1480 if shellball:
1481 # Determine the firmware file is a shellball or a raw binary.
1482 is_shellball = (utils.system_output("file %s" % shellball).find(
1483 "shell script") != -1)
1484 if is_shellball:
Vic Yang772df8a2012-10-31 10:10:49 +08001485 logging.info('Device will update firmware with shellball %s',
1486 shellball)
Chun-ting Changf91ee0f2012-09-17 18:31:54 +08001487 temp_dir = self.faft_client.create_temp_dir('shellball_')
1488 temp_shellball = os.path.join(temp_dir, 'updater.sh')
1489 self._client.send_file(shellball, temp_shellball)
1490 updater_path = temp_shellball
1491 else:
1492 raise error.TestFail(
1493 'The given shellball is not a shell script.')
1494 return updater_path