blob: 07dc798a69289ebc1b93b8a437fe2e2f484b307d [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 Tam6019a1a2012-10-12 14:03:34 +080016from autotest_lib.server.cros.chrome_ec import ChromeEC
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 Tam40fd9472012-01-09 17:11:02 +080024dirname = os.path.dirname(sys.modules[__name__].__file__)
25autotest_dir = os.path.abspath(os.path.join(dirname, "..", ".."))
26cros_dir = os.path.join(autotest_dir, "..", "..", "..", "..")
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080027
28class FAFTSequence(ServoTest):
29 """
30 The base class of Fully Automated Firmware Test Sequence.
31
32 Many firmware tests require several reboot cycles and verify the resulted
33 system states. To do that, an Autotest test case should detailly handle
34 every action on each step. It makes the test case hard to read and many
35 duplicated code. The base class FAFTSequence is to solve this problem.
36
37 The actions of one reboot cycle is defined in a dict, namely FAFT_STEP.
38 There are four functions in the FAFT_STEP dict:
39 state_checker: a function to check the current is valid or not,
40 returning True if valid, otherwise, False to break the whole
41 test sequence.
42 userspace_action: a function to describe the action ran in userspace.
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +080043 reboot_action: a function to do reboot, default: sync_and_warm_reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080044 firmware_action: a function to describe the action ran after reboot.
45
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +080046 And configurations:
47 install_deps_after_boot: if True, install the Autotest dependency after
48 boot; otherwise, do nothing. It is for the cases of recovery mode
49 test. The test boots a USB/SD image instead of an internal image.
50 The previous installed Autotest dependency on the internal image
51 is lost. So need to install it again.
52
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080053 The default FAFT_STEP checks nothing in state_checker and does nothing in
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080054 userspace_action and firmware_action. Its reboot_action is a hardware
55 reboot. You can change the default FAFT_STEP by calling
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080056 self.register_faft_template(FAFT_STEP).
57
58 A FAFT test case consists of several FAFT_STEP's, namely FAFT_SEQUENCE.
59 FAFT_SEQUENCE is an array of FAFT_STEP's. Any missing fields on FAFT_STEP
60 fall back to default.
61
62 In the run_once(), it should register and run FAFT_SEQUENCE like:
63 def run_once(self):
64 self.register_faft_sequence(FAFT_SEQUENCE)
65 self.run_faft_sequnce()
66
67 Note that in the last step, we only run state_checker. The
68 userspace_action, reboot_action, and firmware_action are not executed.
69
70 Attributes:
71 _faft_template: The default FAFT_STEP of each step. The actions would
72 be over-written if the registered FAFT_SEQUENCE is valid.
73 _faft_sequence: The registered FAFT_SEQUENCE.
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +080074 _install_image_path: The URL or the path on the host to the Chrome OS
75 test image to be installed.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +080076 _firmware_update: Boolean. True if firmware update needed after
77 installing the image.
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +080078 _trapped_in_recovery_reason: Keep the recovery reason when the test is
79 trapped in the recovery screen.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080080 """
81 version = 1
82
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080083 # Mapping of partition number of kernel and rootfs.
84 KERNEL_MAP = {'a':'2', 'b':'4', '2':'2', '4':'4', '3':'2', '5':'4'}
85 ROOTFS_MAP = {'a':'3', 'b':'5', '2':'3', '4':'5', '3':'3', '5':'5'}
86 OTHER_KERNEL_MAP = {'a':'4', 'b':'2', '2':'4', '4':'2', '3':'4', '5':'2'}
87 OTHER_ROOTFS_MAP = {'a':'5', 'b':'3', '2':'5', '4':'3', '3':'5', '5':'3'}
88
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +080089 CHROMEOS_MAGIC = "CHROMEOS"
90 CORRUPTED_MAGIC = "CORRUPTD"
91
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +080092 _HTTP_PREFIX = 'http://'
93 _DEVSERVER_PORT = '8090'
94
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +080095 _faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080096 _faft_sequence = ()
97
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +080098 _install_image_path = None
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +080099 _firmware_update = False
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +0800100 _trapped_in_recovery_reason = 0
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800101
ctchang38ae4922012-09-03 17:01:16 +0800102 _backup_firmware_sha = ()
103
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800104 # Class level variable, keep track the states of one time setup.
105 # This variable is preserved across tests which inherit this class.
106 _global_setup_done = {
107 'gbb_flags': False,
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800108 'reimage': False,
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800109 'usb_check': False,
110 }
Vic Yang54f70572012-10-19 17:05:26 +0800111
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800112 @classmethod
113 def check_setup_done(cls, label):
114 """Check if the given setup is done.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800115
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800116 Args:
117 label: The label of the setup.
118 """
119 return cls._global_setup_done[label]
120
121
122 @classmethod
123 def mark_setup_done(cls, label):
124 """Mark the given setup done.
125
126 Args:
127 label: The label of the setup.
128 """
129 cls._global_setup_done[label] = True
130
131
132 @classmethod
133 def unmark_setup_done(cls, label):
134 """Mark the given setup not done.
135
136 Args:
137 label: The label of the setup.
138 """
139 cls._global_setup_done[label] = False
Vic Yang54f70572012-10-19 17:05:26 +0800140
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800141
142 def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False):
143 # Parse arguments from command line
144 args = {}
145 for arg in cmdline_args:
146 match = re.search("^(\w+)=(.+)", arg)
147 if match:
148 args[match.group(1)] = match.group(2)
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800149 if 'image' in args:
150 self._install_image_path = args['image']
151 logging.info('Install Chrome OS test image path: %s' %
152 self._install_image_path)
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800153 if 'firmware_update' in args and args['firmware_update'].lower() \
154 not in ('0', 'false', 'no'):
155 if self._install_image_path:
156 self._firmware_update = True
157 logging.info('Also update firmware after installing.')
158 else:
159 logging.warning('Firmware update will not not performed '
160 'since no image is specified.')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800161
162 super(FAFTSequence, self).initialize(host, cmdline_args, use_pyauto,
163 use_faft)
Vic Yangebd6de62012-06-26 14:25:57 +0800164 if use_faft:
165 self.client_attr = FAFTClientAttribute(
166 self.faft_client.get_platform_name())
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800167 self.delay = FAFTDelayConstants(
168 self.faft_client.get_platform_name())
Vic Yangf93f7022012-10-31 09:40:36 +0800169 self.checkers = FAFTCheckers(self, self.faft_client)
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800170
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800171 if self.client_attr.chrome_ec:
172 self.ec = ChromeEC(self.servo)
173
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800174 if not self.client_attr.has_keyboard:
175 # The environment variable USBKM232_UART_DEVICE should point
176 # to the USB-KM232 UART device.
177 if ('USBKM232_UART_DEVICE' not in os.environ or
178 not os.path.exists(os.environ['USBKM232_UART_DEVICE'])):
179 raise error.TestError('Must set a valid environment '
180 'variable USBKM232_UART_DEVICE.')
181
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700182 # Setting up key matrix mapping
183 self.servo.set_key_matrix(self.client_attr.key_matrix_layout)
184
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800185
186 def setup(self):
187 """Autotest setup function."""
188 super(FAFTSequence, self).setup()
189 if not self._remote_infos['faft']['used']:
190 raise error.TestError('The use_faft flag should be enabled.')
191 self.register_faft_template({
192 'state_checker': (None),
193 'userspace_action': (None),
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +0800194 'reboot_action': (self.sync_and_warm_reboot),
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800195 'firmware_action': (None)
196 })
Tom Wai-Hong Tam19ad9682012-10-24 09:33:42 +0800197 self.install_test_image(self._install_image_path, self._firmware_update)
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800198 self.setup_gbb_flags()
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800199
200
201 def cleanup(self):
202 """Autotest cleanup function."""
203 self._faft_sequence = ()
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800204 self._faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800205 super(FAFTSequence, self).cleanup()
206
207
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800208 def invalidate_firmware_setup(self):
209 """Invalidate all firmware related setup state.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800210
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800211 This method is called when the firmware is re-flashed. It resets all
212 firmware related setup states so that the next test setup properly
213 again.
Vic Yangdbaba8f2012-10-17 16:05:35 +0800214 """
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800215 self.unmark_setup_done('gbb_flags')
Vic Yangdbaba8f2012-10-17 16:05:35 +0800216
217
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800218 def reset_client(self):
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800219 """Reset client, if necessary.
220
221 This method is called when the client is not responsive. It may be
222 caused by the following cases:
223 - network flaky (can be recovered by replugging the Ethernet);
224 - halt on a firmware screen without timeout, e.g. REC_INSERT screen;
225 - corrupted firmware;
226 - corrutped OS image.
227 """
228 # DUT works fine, done.
229 if self._ping_test(self._client.ip, timeout=5):
230 return
231
232 # TODO(waihong@chromium.org): Implement replugging the Ethernet in the
233 # first reset item.
234
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +0800235 # DUT may be trapped in the recovery screen. Try to boot into USB to
236 # retrieve the recovery reason.
237 logging.info('Try to retrieve recovery reason...')
238 if self.servo.get('usb_mux_sel1') == 'dut_sees_usbkey':
239 self.wait_fw_screen_and_plug_usb()
240 else:
241 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
242
243 try:
244 self.wait_for_client(install_deps=True)
245 lines = self.faft_client.run_shell_command_get_output(
246 'crossystem recovery_reason')
247 self._trapped_in_recovery_reason = int(lines[0])
248 logging.info('Got the recovery reason %d.' %
249 self._trapped_in_recovery_reason)
250 except AssertionError:
251 logging.info('Failed to get the recovery reason.')
252
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800253 # DUT may halt on a firmware screen. Try cold reboot.
254 logging.info('Try cold reboot...')
255 self.cold_reboot()
256 try:
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800257 self.wait_for_client()
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800258 return
259 except AssertionError:
260 pass
261
262 # DUT may be broken by a corrupted firmware. Restore firmware.
263 # We assume the recovery boot still works fine. Since the recovery
264 # code is in RO region and all FAFT tests don't change the RO region
265 # except GBB.
266 if self.is_firmware_saved():
267 self.ensure_client_in_recovery()
268 logging.info('Try restore the original firmware...')
269 if self.is_firmware_changed():
270 try:
271 self.restore_firmware()
272 return
273 except AssertionError:
274 logging.info('Restoring firmware doesn\'t help.')
275
276 # DUT may be broken by a corrupted OS image. Restore OS image.
277 self.ensure_client_in_recovery()
278 logging.info('Try restore the OS image...')
279 self.faft_client.run_shell_command('chromeos-install --yes')
280 self.sync_and_warm_reboot()
281 self.wait_for_client_offline()
282 try:
283 self.wait_for_client(install_deps=True)
284 logging.info('Successfully restore OS image.')
285 return
286 except AssertionError:
287 logging.info('Restoring OS image doesn\'t help.')
288
289
290 def ensure_client_in_recovery(self):
291 """Ensure client in recovery boot; reboot into it if necessary.
292
293 Raises:
294 error.TestError: if failed to boot the USB image.
295 """
296 # DUT works fine and is already in recovery boot, done.
297 if self._ping_test(self._client.ip, timeout=5):
Vic Yangf93f7022012-10-31 09:40:36 +0800298 if self.checkers.crossystem_checker({'mainfw_type': 'recovery'}):
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +0800299 return
300
301 logging.info('Try boot into USB image...')
302 self.servo.enable_usb_hub(host=True)
303 self.enable_rec_mode_and_reboot()
304 self.wait_fw_screen_and_plug_usb()
305 try:
306 self.wait_for_client(install_deps=True)
307 except AssertionError:
308 raise error.TestError('Failed to boot the USB image.')
Vic Yang8eaf5ad2012-09-13 14:05:37 +0800309
310
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800311 def assert_test_image_in_path(self, image_path):
312 """Assert the image of image_path be a Chrome OS test image.
313
314 Args:
315 image_path: A path on the host to the test image.
316
317 Raises:
318 error.TestError: if the image is not a test image.
319 """
320 try:
321 build_ver, build_hash = lab_test.VerifyImageAndGetId(cros_dir,
322 image_path)
323 logging.info('Build of image: %s %s' % (build_ver, build_hash))
324 except ChromeOSTestError:
325 raise error.TestError(
326 'An USB disk containning a test image should be plugged '
327 'in the servo board.')
328
329
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800330 def assert_test_image_in_usb_disk(self, usb_dev=None):
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800331 """Assert an USB disk plugged-in on servo and a test image inside.
332
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800333 Args:
334 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
335 If None, it is detected automatically.
336
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800337 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800338 error.TestError: if USB disk not detected or not a test image.
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800339 """
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800340 if self.check_setup_done('usb_check'):
Vic Yang54f70572012-10-19 17:05:26 +0800341 return
342
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800343 # TODO(waihong@chromium.org): We skip the check when servod runs in
344 # a different host since no easy way to access the servo host so far.
345 # Should find a way to work-around it.
346 if not self.servo.is_localhost():
347 logging.info('Skip checking Chrome OS test image in USB as servod '
348 'runs in a different host.')
349 return
350
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800351 if usb_dev:
352 assert self.servo.get('usb_mux_sel1') == 'servo_sees_usbkey'
353 else:
Vadim Bendeburycacf29f2012-07-30 17:49:11 -0700354 self.servo.enable_usb_hub(host=True)
Tom Wai-Hong Tam91f49822011-12-28 15:44:15 +0800355 usb_dev = self.servo.probe_host_usb_dev()
356 if not usb_dev:
357 raise error.TestError(
358 'An USB disk should be plugged in the servo board.')
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800359 self.assert_test_image_in_path(usb_dev)
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800360 self.mark_setup_done('usb_check')
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800361
362
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800363 def get_server_address(self):
364 """Get the server address seen from the client.
365
366 Returns:
367 A string of the server address.
368 """
369 r = self.faft_client.run_shell_command_get_output("echo $SSH_CLIENT")
370 return r[0].split()[0]
371
372
Simran Basi741b5d42012-05-18 11:27:15 -0700373 def install_test_image(self, image_path=None, firmware_update=False):
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800374 """Install the test image specied by the path onto the USB and DUT disk.
375
376 The method first copies the image to USB disk and reboots into it via
Mike Truty49153d82012-08-21 22:27:30 -0500377 recovery mode. Then runs 'chromeos-install' (and possible
378 chromeos-firmwareupdate') to install it to DUT disk.
379
380 Sample command line:
381
382 run_remote_tests.sh --servo --board=daisy --remote=w.x.y.z \
383 --args="image=/tmp/chromiumos_test_image.bin firmware_update=True" \
384 server/site_tests/firmware_XXXX/control
385
386 This test requires an automated recovery to occur while simulating
387 inserting and removing the usb key from the servo. To allow this the
388 following hardware setup is required:
389 1. servo2 board connected via servoflex.
390 2. USB key inserted in the servo2.
391 3. servo2 connected to the dut via dut_hub_in in the usb 2.0 slot.
392 4. network connected via usb dongle in the dut in usb 3.0 slot.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800393
394 Args:
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800395 image_path: An URL or a path on the host to the test image.
Tom Wai-Hong Tam1a3ff742012-01-11 16:36:46 +0800396 firmware_update: Also update the firmware after installing.
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800397
398 Raises:
399 error.TestError: If devserver failed to start.
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800400 """
Tom Wai-Hong Tam19ad9682012-10-24 09:33:42 +0800401 if not image_path:
402 return
403
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800404 if self.check_setup_done('reimage'):
405 return
406
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800407 if image_path.startswith(self._HTTP_PREFIX):
408 # TODO(waihong@chromium.org): Add the check of the URL to ensure
409 # it is a test image.
410 devserver = None
411 image_url = image_path
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800412 elif self.servo.is_localhost():
413 self.assert_test_image_in_path(image_path)
414 # If servod is localhost, i.e. both servod and FAFT see the same
415 # file system, do nothing.
416 devserver = None
417 image_url = image_path
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800418 else:
Tom Wai-Hong Tam08885ae2012-10-19 17:16:45 +0800419 self.assert_test_image_in_path(image_path)
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800420 image_dir, image_base = os.path.split(image_path)
421 logging.info('Starting devserver to serve the image...')
422 # The following stdout and stderr arguments should not be None,
423 # even we don't use them. Otherwise, the socket of devserve is
424 # created as fd 1 (as no stdout) but it still thinks stdout is fd
425 # 1 and dump the log to the socket. Wrong HTTP protocol happens.
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800426 devserver = subprocess.Popen(['/usr/lib/devserver/devserver.py',
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800427 '--archive_dir=%s' % image_dir,
428 '--port=%s' % self._DEVSERVER_PORT],
429 stdout=subprocess.PIPE,
430 stderr=subprocess.PIPE)
431 image_url = '%s%s:%s/static/archive/%s' % (
432 self._HTTP_PREFIX,
433 self.get_server_address(),
434 self._DEVSERVER_PORT,
435 image_base)
436
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800437 # Wait devserver startup completely
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800438 time.sleep(self.delay.devserver)
Tom Wai-Hong Tam71818d82012-10-24 14:57:43 +0800439 # devserver is a service running forever. If it is terminated,
440 # some error does happen.
441 if devserver.poll():
442 raise error.TestError('Starting devserver failed, '
443 'returning %d.' % devserver.returncode)
444
Tom Wai-Hong Tame796de42012-10-16 19:42:20 +0800445 logging.info('Ask Servo to install the image from %s' % image_url)
446 self.servo.image_to_servo_usb(image_url)
447
448 if devserver and devserver.poll() is None:
449 logging.info('Shutting down devserver...')
450 devserver.terminate()
Mike Truty49153d82012-08-21 22:27:30 -0500451
452 # DUT is powered off while imaging servo USB.
453 # Now turn it on.
454 self.servo.power_short_press()
455 self.wait_for_client()
456 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
457
458 install_cmd = 'chromeos-install --yes'
459 if firmware_update:
460 install_cmd += ' && chromeos-firmwareupdate --mode recovery'
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +0800461 self.backup_firmware()
Mike Truty49153d82012-08-21 22:27:30 -0500462
463 self.register_faft_sequence((
464 { # Step 1, request recovery boot
Vic Yangf93f7022012-10-31 09:40:36 +0800465 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500466 'mainfw_type': ('developer', 'normal'),
467 }),
468 'userspace_action': self.faft_client.request_recovery_boot,
469 'firmware_action': self.wait_fw_screen_and_plug_usb,
470 'install_deps_after_boot': True,
471 },
472 { # Step 2, expected recovery boot
Vic Yangf93f7022012-10-31 09:40:36 +0800473 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500474 'mainfw_type': 'recovery',
Tom Wai-Hong Tam6ec46e32012-10-05 16:39:21 +0800475 'recovery_reason' : vboot.RECOVERY_REASON['US_TEST'],
Mike Truty49153d82012-08-21 22:27:30 -0500476 }),
477 'userspace_action': (self.faft_client.run_shell_command,
478 install_cmd),
479 'reboot_action': self.cold_reboot,
480 'install_deps_after_boot': True,
481 },
482 { # Step 3, expected normal or developer boot (not recovery)
Vic Yangf93f7022012-10-31 09:40:36 +0800483 'state_checker': (self.checkers.crossystem_checker, {
Mike Truty49153d82012-08-21 22:27:30 -0500484 'mainfw_type': ('developer', 'normal')
485 }),
486 },
487 ))
488 self.run_faft_sequence()
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +0800489
490 if firmware_update:
491 self.clear_saved_firmware()
492
Mike Truty49153d82012-08-21 22:27:30 -0500493 # 'Unplug' any USB keys in the servo from the dut.
Tom Wai-Hong Tam953c7742012-10-16 21:09:31 +0800494 self.servo.enable_usb_hub(host=True)
Tom Wai-Hong Tam6668b762012-10-23 11:45:36 +0800495 # Mark usb_check done so it won't check a test image in USB anymore.
496 self.mark_setup_done('usb_check')
Tom Wai-Hong Tam73229372012-10-23 11:58:16 +0800497 self.mark_setup_done('reimage')
Tom Wai-Hong Tam40fd9472012-01-09 17:11:02 +0800498
499
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800500 def clear_set_gbb_flags(self, clear_mask, set_mask):
501 """Clear and set the GBB flags in the current flashrom.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800502
503 Args:
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800504 clear_mask: A mask of flags to be cleared.
505 set_mask: A mask of flags to be set.
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800506 """
507 gbb_flags = self.faft_client.get_gbb_flags()
Tom Wai-Hong Tamfa3142e2012-08-16 11:53:58 +0800508 new_flags = gbb_flags & ctypes.c_uint32(~clear_mask).value | set_mask
509
510 if (gbb_flags != new_flags):
511 logging.info('Change the GBB flags from 0x%x to 0x%x.' %
512 (gbb_flags, new_flags))
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800513 self.faft_client.run_shell_command(
Tom Wai-Hong Tamfda76e22012-08-08 17:19:10 +0800514 '/usr/share/vboot/bin/set_gbb_flags.sh 0x%x' % new_flags)
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +0800515 self.faft_client.reload_firmware()
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800516 # If changing FORCE_DEV_SWITCH_ON flag, reboot to get a clear state
Tom Wai-Hong Tam6ec46e32012-10-05 16:39:21 +0800517 if ((gbb_flags ^ new_flags) & vboot.GBB_FLAG_FORCE_DEV_SWITCH_ON):
Tom Wai-Hong Tama2481922012-08-08 17:24:42 +0800518 self.run_faft_step({
519 'firmware_action': self.wait_fw_screen_and_ctrl_d,
520 })
Tom Wai-Hong Tam15ce5812012-07-26 14:14:18 +0800521
522
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800523 def check_ec_capability(self, required_cap=[], suppress_warning=False):
Vic Yang4d72cb62012-07-24 11:51:09 +0800524 """Check if current platform has required EC capabilities.
525
526 Args:
527 required_cap: A list containing required EC capabilities. Pass in
528 None to only check for presence of Chrome EC.
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800529 suppress_warning: True to suppress any warning messages.
Vic Yang4d72cb62012-07-24 11:51:09 +0800530
531 Returns:
532 True if requirements are met. Otherwise, False.
533 """
534 if not self.client_attr.chrome_ec:
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800535 if not suppress_warning:
536 logging.warn('Requires Chrome EC to run this test.')
Vic Yang4d72cb62012-07-24 11:51:09 +0800537 return False
538
539 for cap in required_cap:
540 if cap not in self.client_attr.ec_capability:
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800541 if not suppress_warning:
542 logging.warn('Requires EC capability "%s" to run this '
543 'test.' % cap)
Vic Yang4d72cb62012-07-24 11:51:09 +0800544 return False
545
546 return True
547
548
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800549 def check_root_part_on_non_recovery(self, part):
550 """Check the partition number of root device and on normal/dev boot.
551
552 Returns:
553 True if the root device matched and on normal/dev boot;
554 otherwise, False.
555 """
Vic Yangf93f7022012-10-31 09:40:36 +0800556 return self.checkers.root_part_checker(part) and \
557 self.checkers.crossystem_checker({
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800558 'mainfw_type': ('normal', 'developer'),
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800559 })
560
561
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800562 def _join_part(self, dev, part):
563 """Return a concatenated string of device and partition number.
564
565 Args:
566 dev: A string of device, e.g.'/dev/sda'.
567 part: A string of partition number, e.g.'3'.
568
569 Returns:
570 A concatenated string of device and partition number, e.g.'/dev/sda3'.
571
572 >>> seq = FAFTSequence()
573 >>> seq._join_part('/dev/sda', '3')
574 '/dev/sda3'
575 >>> seq._join_part('/dev/mmcblk0', '2')
576 '/dev/mmcblk0p2'
577 """
578 if 'mmcblk' in dev:
579 return dev + 'p' + part
580 else:
581 return dev + part
582
583
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800584 def copy_kernel_and_rootfs(self, from_part, to_part):
585 """Copy kernel and rootfs from from_part to to_part.
586
587 Args:
588 from_part: A string of partition number to be copied from.
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800589 to_part: A string of partition number to be copied to.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800590 """
591 root_dev = self.faft_client.get_root_dev()
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800592 logging.info('Copying kernel from %s to %s. Please wait...' %
593 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800594 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800595 (self._join_part(root_dev, self.KERNEL_MAP[from_part]),
596 self._join_part(root_dev, self.KERNEL_MAP[to_part])))
597 logging.info('Copying rootfs from %s to %s. Please wait...' %
598 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800599 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800600 (self._join_part(root_dev, self.ROOTFS_MAP[from_part]),
601 self._join_part(root_dev, self.ROOTFS_MAP[to_part])))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800602
603
604 def ensure_kernel_boot(self, part):
605 """Ensure the request kernel boot.
606
607 If not, it duplicates the current kernel to the requested kernel
608 and sets the requested higher priority to ensure it boot.
609
610 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800611 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800612 """
Vic Yangf93f7022012-10-31 09:40:36 +0800613 if not self.checkers.root_part_checker(part):
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800614 if self.faft_client.diff_kernel_a_b():
615 self.copy_kernel_and_rootfs(
616 from_part=self.OTHER_KERNEL_MAP[part],
617 to_part=part)
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800618 self.run_faft_step({
619 'userspace_action': (self.reset_and_prioritize_kernel, part),
620 })
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800621
622
Vic Yang416f2032012-08-28 10:18:03 +0800623 def set_hardware_write_protect(self, enabled):
Vic Yang2cabf812012-08-28 02:39:04 +0800624 """Set hardware write protect pin.
625
626 Args:
627 enable: True if asserting write protect pin. Otherwise, False.
628 """
629 self.servo.set('fw_wp_vref', self.client_attr.wp_voltage)
630 self.servo.set('fw_wp_en', 'on')
Vic Yang416f2032012-08-28 10:18:03 +0800631 self.servo.set('fw_wp', 'on' if enabled else 'off')
632
633
634 def set_EC_write_protect_and_reboot(self, enabled):
635 """Set EC write protect status and reboot to take effect.
636
637 EC write protect is only activated if both hardware write protect pin
638 is asserted and software write protect flag is set. Also, a reboot is
639 required for write protect to take effect.
640
641 Since the software write protect flag cannot be unset if hardware write
642 protect pin is asserted, we need to deasserted the pin first if we are
643 deactivating write protect. Similarly, a reboot is required before we
644 can modify the software flag.
645
646 This method asserts/deasserts hardware write protect pin first, and
647 set corresponding EC software write protect flag.
648
649 Args:
650 enable: True if activating EC write protect. Otherwise, False.
651 """
652 self.set_hardware_write_protect(enabled)
653 if enabled:
654 # Set write protect flag and reboot to take effect.
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800655 self.ec.send_command("flashwp enable")
Vic Yang416f2032012-08-28 10:18:03 +0800656 self.sync_and_ec_reboot()
657 else:
658 # Reboot after deasserting hardware write protect pin to deactivate
659 # write protect. And then remove software write protect flag.
660 self.sync_and_ec_reboot()
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800661 self.ec.send_command("flashwp disable")
Vic Yang2cabf812012-08-28 02:39:04 +0800662
663
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800664 def press_ctrl_d(self):
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800665 """Send Ctrl-D key to DUT."""
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800666 if not self.client_attr.has_keyboard:
667 logging.info('Running usbkm232-ctrld...')
668 os.system('usbkm232-ctrld')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800669 else:
670 self.servo.ctrl_d()
671
672
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800673 def press_ctrl_u(self):
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800674 """Send Ctrl-U key to DUT.
675
676 Raises:
677 error.TestError: if a non-Chrome EC device or no Ctrl-U command given
678 on a no-build-in-keyboard device.
679 """
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800680 if not self.client_attr.has_keyboard:
681 logging.info('Running usbkm232-ctrlu...')
682 os.system('usbkm232-ctrlu')
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800683 elif self.check_ec_capability(['keyboard'], suppress_warning=True):
684 self.ec.key_down('<ctrl_l>')
685 self.ec.key_down('u')
686 self.ec.key_up('u')
687 self.ec.key_up('<ctrl_l>')
688 elif self.client_attr.has_keyboard:
689 raise error.TestError(
690 "Can't send Ctrl-U to DUT without using Chrome EC.")
691 else:
692 raise error.TestError(
693 "Should specify the ctrl_u_cmd argument.")
694
695
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800696 def press_enter(self):
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800697 """Send Enter key to DUT."""
Tom Wai-Hong Tam9c15b4b2012-10-29 17:59:26 +0800698 if not self.client_attr.has_keyboard:
699 logging.info('Running usbkm232-enter...')
700 os.system('usbkm232-enter')
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800701 else:
702 self.servo.enter_key()
703
704
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800705 def wait_fw_screen_and_ctrl_d(self):
706 """Wait for firmware warning screen and press Ctrl-D."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800707 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800708 self.press_ctrl_d()
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800709
710
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800711 def wait_fw_screen_and_ctrl_u(self):
712 """Wait for firmware warning screen and press Ctrl-U."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800713 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800714 self.press_ctrl_u()
Tom Wai-Hong Tamadbec3e2012-10-15 14:20:15 +0800715
716
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800717 def wait_fw_screen_and_trigger_recovery(self, need_dev_transition=False):
718 """Wait for firmware warning screen and trigger recovery boot."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800719 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800720 self.press_enter()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800721
722 # For Alex/ZGB, there is a dev warning screen in text mode.
723 # Skip it by pressing Ctrl-D.
724 if need_dev_transition:
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800725 time.sleep(self.delay.legacy_text_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800726 self.press_ctrl_d()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800727
728
Mike Truty49153d82012-08-21 22:27:30 -0500729 def wait_fw_screen_and_unplug_usb(self):
730 """Wait for firmware warning screen and then unplug the servo USB."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800731 time.sleep(self.delay.load_usb)
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800732 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800733 time.sleep(self.delay.between_usb_plug)
Mike Truty49153d82012-08-21 22:27:30 -0500734
735
736 def wait_fw_screen_and_plug_usb(self):
737 """Wait for firmware warning screen and then unplug and plug the USB."""
738 self.wait_fw_screen_and_unplug_usb()
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800739 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
740
741
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800742 def wait_fw_screen_and_press_power(self):
743 """Wait for firmware warning screen and press power button."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800744 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam7317c042012-08-14 11:59:06 +0800745 # While the firmware screen, the power button probing loop sleeps
746 # 0.25 second on every scan. Use the normal delay (1.2 second) for
747 # power press.
748 self.servo.power_normal_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800749
750
Tom Wai-Hong Tam4f5e5922012-07-27 16:23:15 +0800751 def wait_longer_fw_screen_and_press_power(self):
752 """Wait for firmware screen without timeout and press power button."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800753 time.sleep(self.delay.dev_screen_timeout)
Tom Wai-Hong Tam4f5e5922012-07-27 16:23:15 +0800754 self.wait_fw_screen_and_press_power()
755
756
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800757 def wait_fw_screen_and_close_lid(self):
758 """Wait for firmware warning screen and close lid."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800759 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800760 self.servo.lid_close()
761
762
Tom Wai-Hong Tam473cfa72012-07-27 17:16:57 +0800763 def wait_longer_fw_screen_and_close_lid(self):
764 """Wait for firmware screen without timeout and close lid."""
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800765 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam473cfa72012-07-27 17:16:57 +0800766 self.wait_fw_screen_and_close_lid()
767
768
Tom Wai-Hong Tam01d5e572012-10-23 10:07:11 +0800769 def setup_gbb_flags(self):
770 """Setup the GBB flags for FAFT test."""
771 if self.check_setup_done('gbb_flags'):
772 return
773
774 logging.info('Set proper GBB flags for test.')
775 self.clear_set_gbb_flags(vboot.GBB_FLAG_DEV_SCREEN_SHORT_DELAY |
776 vboot.GBB_FLAG_FORCE_DEV_SWITCH_ON |
777 vboot.GBB_FLAG_FORCE_DEV_BOOT_USB |
778 vboot.GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK,
779 vboot.GBB_FLAG_ENTER_TRIGGERS_TONORM)
780 self.mark_setup_done('gbb_flags')
781
782
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800783 def setup_tried_fwb(self, tried_fwb):
784 """Setup for fw B tried state.
785
786 It makes sure the system in the requested fw B tried state. If not, it
787 tries to do so.
788
789 Args:
790 tried_fwb: True if requested in tried_fwb=1; False if tried_fwb=0.
791 """
792 if tried_fwb:
Vic Yangf93f7022012-10-31 09:40:36 +0800793 if not self.checkers.crossystem_checker({'tried_fwb': '1'}):
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800794 logging.info(
795 'Firmware is not booted with tried_fwb. Reboot into it.')
796 self.run_faft_step({
797 'userspace_action': self.faft_client.set_try_fw_b,
798 })
799 else:
Vic Yangf93f7022012-10-31 09:40:36 +0800800 if not self.checkers.crossystem_checker({'tried_fwb': '0'}):
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800801 logging.info(
802 'Firmware is booted with tried_fwb. Reboot to clear.')
803 self.run_faft_step({})
804
805
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800806 def enable_rec_mode_and_reboot(self):
807 """Switch to rec mode and reboot.
808
809 This method emulates the behavior of the old physical recovery switch,
810 i.e. switch ON + reboot + switch OFF, and the new keyboard controlled
811 recovery mode, i.e. just press Power + Esc + Refresh.
812 """
Tom Wai-Hong Tam80419a82012-10-30 09:10:00 +0800813 if self.client_attr.chrome_ec:
Vic Yang81273092012-08-21 15:57:09 +0800814 # Cold reset to clear EC_IN_RW signal
Vic Yanga7250662012-08-31 04:00:08 +0800815 self.servo.set('cold_reset', 'on')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800816 time.sleep(self.delay.hold_cold_reset)
Vic Yanga7250662012-08-31 04:00:08 +0800817 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800818 time.sleep(self.delay.ec_boot_to_console)
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800819 self.ec.send_command("reboot ap-off")
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800820 time.sleep(self.delay.ec_boot_to_console)
Tom Wai-Hong Tam6019a1a2012-10-12 14:03:34 +0800821 self.ec.send_command("hostevent set 0x4000")
Vic Yang611dd852012-08-02 15:36:31 +0800822 self.servo.power_short_press()
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800823 else:
824 self.servo.enable_recovery_mode()
825 self.cold_reboot()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800826 time.sleep(self.delay.ec_reboot_cmd)
Tom Wai-Hong Tamac943172012-08-01 10:38:39 +0800827 self.servo.disable_recovery_mode()
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800828
829
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800830 def enable_dev_mode_and_reboot(self):
831 """Switch to developer mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800832 if self.client_attr.keyboard_dev:
833 self.enable_keyboard_dev_mode()
834 else:
835 self.servo.enable_development_mode()
836 self.faft_client.run_shell_command(
837 'chromeos-firmwareupdate --mode todev && reboot')
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800838
839
Tom Wai-Hong Tam0b9e6d72012-07-31 20:54:06 +0800840 def enable_normal_mode_and_reboot(self):
841 """Switch to normal mode and reboot."""
Vic Yange7553162012-06-20 16:20:47 +0800842 if self.client_attr.keyboard_dev:
843 self.disable_keyboard_dev_mode()
844 else:
845 self.servo.disable_development_mode()
846 self.faft_client.run_shell_command(
847 'chromeos-firmwareupdate --mode tonormal && reboot')
848
849
850 def wait_fw_screen_and_switch_keyboard_dev_mode(self, dev):
851 """Wait for firmware screen and then switch into or out of dev mode.
852
853 Args:
854 dev: True if switching into dev mode. Otherwise, False.
855 """
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800856 time.sleep(self.delay.firmware_screen)
Vic Yange7553162012-06-20 16:20:47 +0800857 if dev:
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800858 self.press_ctrl_d()
Vic Yange7553162012-06-20 16:20:47 +0800859 else:
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800860 self.press_enter()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800861 time.sleep(self.delay.firmware_screen)
Tom Wai-Hong Tam91612bc2012-10-29 16:04:21 +0800862 self.press_enter()
Vic Yange7553162012-06-20 16:20:47 +0800863
864
865 def enable_keyboard_dev_mode(self):
866 logging.info("Enabling keyboard controlled developer mode")
Tom Wai-Hong Tamf1a17d72012-07-26 11:39:52 +0800867 # Plug out USB disk for preventing recovery boot without warning
868 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Vic Yange7553162012-06-20 16:20:47 +0800869 # Rebooting EC with rec mode on. Should power on AP.
Tom Wai-Hong Tama373f802012-07-31 21:16:48 +0800870 self.enable_rec_mode_and_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +0800871 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +0800872 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=True)
Vic Yange7553162012-06-20 16:20:47 +0800873
874
875 def disable_keyboard_dev_mode(self):
876 logging.info("Disabling keyboard controlled developer mode")
Tom Wai-Hong Tamb0b3f412012-08-13 17:17:06 +0800877 if not self.client_attr.chrome_ec:
Vic Yang611dd852012-08-02 15:36:31 +0800878 self.servo.disable_recovery_mode()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800879 self.cold_reboot()
Tom Wai-Hong Tam8c54eb82012-08-01 10:31:07 +0800880 self.wait_for_client_offline()
Vic Yange7553162012-06-20 16:20:47 +0800881 self.wait_fw_screen_and_switch_keyboard_dev_mode(dev=False)
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +0800882
883
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800884 def setup_dev_mode(self, dev_mode):
885 """Setup for development mode.
886
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800887 It makes sure the system in the requested normal/dev mode. If not, it
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800888 tries to do so.
889
890 Args:
891 dev_mode: True if requested in dev mode; False if normal mode.
892 """
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800893 # Change the default firmware_action for dev mode passing the fw screen.
894 self.register_faft_template({
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800895 'firmware_action': (self.wait_fw_screen_and_ctrl_d if dev_mode
896 else None),
897 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800898 if dev_mode:
Vic Yange7553162012-06-20 16:20:47 +0800899 if (not self.client_attr.keyboard_dev and
Vic Yangf93f7022012-10-31 09:40:36 +0800900 not self.checkers.crossystem_checker({'devsw_cur': '1'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800901 logging.info('Dev switch is not on. Now switch it on.')
902 self.servo.enable_development_mode()
Vic Yangf93f7022012-10-31 09:40:36 +0800903 if not self.checkers.crossystem_checker({'devsw_boot': '1',
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800904 'mainfw_type': 'developer'}):
905 logging.info('System is not in dev mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800906 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +0800907 'userspace_action': None if self.client_attr.keyboard_dev
908 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800909 'chromeos-firmwareupdate --mode todev && reboot'),
Vic Yange7553162012-06-20 16:20:47 +0800910 'reboot_action': self.enable_keyboard_dev_mode if
911 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800912 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800913 else:
Vic Yange7553162012-06-20 16:20:47 +0800914 if (not self.client_attr.keyboard_dev and
Vic Yangf93f7022012-10-31 09:40:36 +0800915 not self.checkers.crossystem_checker({'devsw_cur': '0'})):
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800916 logging.info('Dev switch is not off. Now switch it off.')
917 self.servo.disable_development_mode()
Vic Yangf93f7022012-10-31 09:40:36 +0800918 if not self.checkers.crossystem_checker({'devsw_boot': '0',
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800919 'mainfw_type': 'normal'}):
920 logging.info('System is not in normal mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800921 self.run_faft_step({
Vic Yange7553162012-06-20 16:20:47 +0800922 'userspace_action': None if self.client_attr.keyboard_dev
923 else (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800924 'chromeos-firmwareupdate --mode tonormal && reboot'),
Vic Yange7553162012-06-20 16:20:47 +0800925 'reboot_action': self.disable_keyboard_dev_mode if
926 self.client_attr.keyboard_dev else None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800927 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800928
929
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800930 def setup_kernel(self, part):
931 """Setup for kernel test.
932
933 It makes sure both kernel A and B bootable and the current boot is
934 the requested kernel part.
935
936 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800937 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800938 """
939 self.ensure_kernel_boot(part)
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800940 if self.faft_client.diff_kernel_a_b():
941 self.copy_kernel_and_rootfs(from_part=part,
942 to_part=self.OTHER_KERNEL_MAP[part])
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800943 self.reset_and_prioritize_kernel(part)
944
945
946 def reset_and_prioritize_kernel(self, part):
947 """Make the requested partition highest priority.
948
949 This function also reset kerenl A and B to bootable.
950
951 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800952 part: A string of partition number to be prioritized.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800953 """
954 root_dev = self.faft_client.get_root_dev()
955 # Reset kernel A and B to bootable.
956 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
957 (self.KERNEL_MAP['a'], root_dev))
958 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
959 (self.KERNEL_MAP['b'], root_dev))
960 # Set kernel part highest priority.
961 self.faft_client.run_shell_command('cgpt prioritize -i%s %s' %
962 (self.KERNEL_MAP[part], root_dev))
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800963 # Safer to sync and wait until the cgpt status written to the disk.
964 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800965 time.sleep(self.delay.sync)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800966
967
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800968 def warm_reboot(self):
969 """Request a warm reboot.
970
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +0800971 A wrapper for underlying servo warm reset.
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800972 """
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +0800973 # Use cold reset if the warm reset is broken.
974 if self.client_attr.broken_warm_reset:
Gediminas Ramanauskase021e152012-09-04 19:10:59 -0700975 logging.info('broken_warm_reset is True. Cold rebooting instead.')
976 self.cold_reboot()
Tom Wai-Hong Tamb06f0802012-07-31 16:27:50 +0800977 else:
978 self.servo.warm_reset()
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800979
980
981 def cold_reboot(self):
982 """Request a cold reboot.
983
984 A wrapper for underlying servo cold reset.
985 """
Gediminas Ramanauskasc6025692012-10-23 14:33:40 -0700986 if self.client_attr.broken_warm_reset:
Tom Wai-Hong Tama276d0a2012-08-22 11:15:17 +0800987 self.servo.set('pwr_button', 'press')
988 self.servo.set('cold_reset', 'on')
989 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +0800990 time.sleep(self.delay.ec_boot_to_pwr_button)
Tom Wai-Hong Tama276d0a2012-08-22 11:15:17 +0800991 self.servo.set('pwr_button', 'release')
Tom Wai-Hong Tamb8a91392012-09-27 10:45:32 +0800992 elif self.check_ec_capability(suppress_warning=True):
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +0800993 # We don't use servo.cold_reset() here because software sync is
994 # not yet finished, and device may or may not come up after cold
995 # reset. Pressing power button before firmware comes up solves this.
996 #
997 # The correct behavior should be (not work now):
998 # - If rebooting EC with rec mode on, power on AP and it boots
999 # into recovery mode.
1000 # - If rebooting EC with rec mode off, power on AP for software
1001 # sync. Then AP checks if lid open or not. If lid open, continue;
1002 # otherwise, shut AP down and need servo for a power button
1003 # press.
1004 self.servo.set('cold_reset', 'on')
1005 self.servo.set('cold_reset', 'off')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001006 time.sleep(self.delay.ec_boot_to_pwr_button)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001007 self.servo.power_short_press()
1008 else:
1009 self.servo.cold_reset()
1010
1011
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001012 def sync_and_warm_reboot(self):
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001013 """Request the client sync and do a warm reboot.
1014
1015 This is the default reboot action on FAFT.
1016 """
1017 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001018 time.sleep(self.delay.sync)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001019 self.warm_reboot()
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +08001020
1021
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001022 def sync_and_cold_reboot(self):
1023 """Request the client sync and do a cold reboot.
1024
1025 This reboot action is used to reset EC for recovery mode.
1026 """
1027 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001028 time.sleep(self.delay.sync)
Tom Wai-Hong Tam7ad99ab2012-07-30 19:30:51 +08001029 self.cold_reboot()
Tom Wai-Hong Tamb21b6b42012-07-26 10:46:30 +08001030
1031
Vic Yangaeb10392012-08-28 09:25:09 +08001032 def sync_and_ec_reboot(self, args=''):
1033 """Request the client sync and do a EC triggered reboot.
1034
1035 Args:
1036 args: Arguments passed to "ectool reboot_ec". Including:
1037 RO: jump to EC RO firmware.
1038 RW: jump to EC RW firmware.
1039 cold: Cold/hard reboot.
1040 """
Vic Yang59cac9c2012-05-21 15:28:42 +08001041 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001042 time.sleep(self.delay.sync)
Vic Yangaeb10392012-08-28 09:25:09 +08001043 # Since EC reboot happens immediately, delay before actual reboot to
1044 # allow FAFT client returning.
1045 self.faft_client.run_shell_command('(sleep %d; ectool reboot_ec %s)&' %
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001046 (self.delay.ec_reboot_cmd, args))
1047 time.sleep(self.delay.ec_reboot_cmd)
Vic Yangf86728a2012-07-30 10:44:07 +08001048 self.check_lid_and_power_on()
1049
1050
Chun-ting Changa4f65532012-10-17 16:57:28 +08001051 def sync_and_reboot_with_factory_install_shim(self):
1052 """Request the client sync and do a warm reboot to recovery mode.
1053
1054 After reboot, the client will use factory install shim to reset TPM
1055 values. The client ignore TPM rollback, so here forces it to recovery
1056 mode.
1057 """
Vic Yangf93f7022012-10-31 09:40:36 +08001058 is_dev = self.checkers.crossystem_checker({'devsw_boot': '1'})
Chun-ting Changa4f65532012-10-17 16:57:28 +08001059 if not is_dev:
1060 self.enable_dev_mode_and_reboot()
1061 time.sleep(self.SYNC_DELAY)
1062 self.enable_rec_mode_and_reboot()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001063 time.sleep(self.delay.install_shim_done)
Chun-ting Changa4f65532012-10-17 16:57:28 +08001064 self.warm_reboot()
1065
1066
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +08001067 def full_power_off_and_on(self):
1068 """Shutdown the device by pressing power button and power on again."""
1069 # Press power button to trigger Chrome OS normal shutdown process.
1070 self.servo.power_normal_press()
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001071 time.sleep(self.delay.shutdown)
Tom Wai-Hong Tamc8f2ca02012-09-14 11:18:01 +08001072 # Short press power button to boot DUT again.
1073 self.servo.power_short_press()
1074
1075
Vic Yangf86728a2012-07-30 10:44:07 +08001076 def check_lid_and_power_on(self):
1077 """
1078 On devices with EC software sync, system powers on after EC reboots if
1079 lid is open. Otherwise, the EC shuts down CPU after about 3 seconds.
1080 This method checks lid switch state and presses power button if
1081 necessary.
1082 """
1083 if self.servo.get("lid_open") == "no":
Tom Wai-Hong Tam41738762012-10-29 14:32:39 +08001084 time.sleep(self.delay.software_sync)
Vic Yangf86728a2012-07-30 10:44:07 +08001085 self.servo.power_short_press()
Vic Yang59cac9c2012-05-21 15:28:42 +08001086
1087
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001088 def _modify_usb_kernel(self, usb_dev, from_magic, to_magic):
1089 """Modify the kernel header magic in USB stick.
1090
1091 The kernel header magic is the first 8-byte of kernel partition.
1092 We modify it to make it fail on kernel verification check.
1093
1094 Args:
1095 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1096 from_magic: A string of magic which we change it from.
1097 to_magic: A string of magic which we change it to.
1098
1099 Raises:
1100 error.TestError: if failed to change magic.
1101 """
1102 assert len(from_magic) == 8
1103 assert len(to_magic) == 8
Tom Wai-Hong Tama1d9a0f2011-12-23 09:13:33 +08001104 # USB image only contains one kernel.
1105 kernel_part = self._join_part(usb_dev, self.KERNEL_MAP['a'])
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001106 read_cmd = "sudo dd if=%s bs=8 count=1 2>/dev/null" % kernel_part
1107 current_magic = utils.system_output(read_cmd)
1108 if current_magic == to_magic:
1109 logging.info("The kernel magic is already %s." % current_magic)
1110 return
1111 if current_magic != from_magic:
1112 raise error.TestError("Invalid kernel image on USB: wrong magic.")
1113
1114 logging.info('Modify the kernel magic in USB, from %s to %s.' %
1115 (from_magic, to_magic))
1116 write_cmd = ("echo -n '%s' | sudo dd of=%s oflag=sync conv=notrunc "
1117 " 2>/dev/null" % (to_magic, kernel_part))
1118 utils.system(write_cmd)
1119
1120 if utils.system_output(read_cmd) != to_magic:
1121 raise error.TestError("Failed to write new magic.")
1122
1123
1124 def corrupt_usb_kernel(self, usb_dev):
1125 """Corrupt USB kernel by modifying its magic from CHROMEOS to CORRUPTD.
1126
1127 Args:
1128 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1129 """
1130 self._modify_usb_kernel(usb_dev, self.CHROMEOS_MAGIC,
1131 self.CORRUPTED_MAGIC)
1132
1133
1134 def restore_usb_kernel(self, usb_dev):
1135 """Restore USB kernel by modifying its magic from CORRUPTD to CHROMEOS.
1136
1137 Args:
1138 usb_dev: A string of USB stick path on the host, like '/dev/sdc'.
1139 """
1140 self._modify_usb_kernel(usb_dev, self.CORRUPTED_MAGIC,
1141 self.CHROMEOS_MAGIC)
1142
1143
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001144 def _call_action(self, action_tuple, check_status=False):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001145 """Call the action function with/without arguments.
1146
1147 Args:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001148 action_tuple: A function, or a tuple (function, args, error_msg),
1149 in which, args and error_msg are optional. args is
1150 either a value or a tuple if multiple arguments.
1151 check_status: Check the return value of action function. If not
1152 succeed, raises a TestFail exception.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001153
1154 Returns:
1155 The result value of the action function.
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001156
1157 Raises:
1158 error.TestError: An error when the action function is not callable.
1159 error.TestFail: When check_status=True, action function not succeed.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001160 """
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001161 action = action_tuple
1162 args = ()
1163 error_msg = 'Not succeed'
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001164 if isinstance(action_tuple, tuple):
1165 action = action_tuple[0]
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001166 if len(action_tuple) >= 2:
1167 args = action_tuple[1]
1168 if not isinstance(args, tuple):
1169 args = (args,)
1170 if len(action_tuple) >= 3:
Tom Wai-Hong Tamff560882012-10-15 16:50:06 +08001171 error_msg = action_tuple[2]
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001172
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001173 if action is None:
1174 return
1175
1176 if not callable(action):
1177 raise error.TestError('action is not callable!')
1178
1179 info_msg = 'calling %s' % str(action)
1180 if args:
1181 info_msg += ' with args %s' % str(args)
1182 logging.info(info_msg)
1183 ret = action(*args)
1184
1185 if check_status and not ret:
1186 raise error.TestFail('%s: %s returning %s' %
1187 (error_msg, info_msg, str(ret)))
1188 return ret
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001189
1190
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001191 def run_shutdown_process(self, shutdown_action, pre_power_action=None,
1192 post_power_action=None):
1193 """Run shutdown_action(), which makes DUT shutdown, and power it on.
1194
1195 Args:
1196 shutdown_action: a function which makes DUT shutdown, like pressing
1197 power key.
1198 pre_power_action: a function which is called before next power on.
1199 post_power_action: a function which is called after next power on.
1200
1201 Raises:
1202 error.TestFail: if the shutdown_action() failed to turn DUT off.
1203 """
1204 self._call_action(shutdown_action)
1205 logging.info('Wait to ensure DUT shut down...')
1206 try:
1207 self.wait_for_client()
1208 raise error.TestFail(
1209 'Should shut the device down after calling %s.' %
1210 str(shutdown_action))
1211 except AssertionError:
1212 logging.info(
1213 'DUT is surely shutdown. We are going to power it on again...')
1214
1215 if pre_power_action:
1216 self._call_action(pre_power_action)
Tom Wai-Hong Tam610262a2012-01-12 14:16:53 +08001217 self.servo.power_short_press()
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001218 if post_power_action:
1219 self._call_action(post_power_action)
1220
1221
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001222 def register_faft_template(self, template):
1223 """Register FAFT template, the default FAFT_STEP of each step.
1224
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001225 Any missing field falls back to the original faft_template.
1226
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001227 Args:
1228 template: A FAFT_STEP dict.
1229 """
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +08001230 self._faft_template.update(template)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001231
1232
1233 def register_faft_sequence(self, sequence):
1234 """Register FAFT sequence.
1235
1236 Args:
1237 sequence: A FAFT_SEQUENCE array which consisted of FAFT_STEP dicts.
1238 """
1239 self._faft_sequence = sequence
1240
1241
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001242 def run_faft_step(self, step, no_reboot=False):
1243 """Run a single FAFT step.
1244
1245 Any missing field falls back to faft_template. An empty step means
1246 running the default faft_template.
1247
1248 Args:
1249 step: A FAFT_STEP dict.
1250 no_reboot: True to prevent running reboot_action and firmware_action.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001251
1252 Raises:
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001253 error.TestError: An error when the given step is not valid.
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +08001254 error.TestFail: Test failed in waiting DUT reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001255 """
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001256 FAFT_STEP_KEYS = ('state_checker', 'userspace_action', 'reboot_action',
1257 'firmware_action', 'install_deps_after_boot')
1258
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001259 test = {}
1260 test.update(self._faft_template)
1261 test.update(step)
1262
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001263 for key in test:
1264 if key not in FAFT_STEP_KEYS:
Tom Wai-Hong Tam78709592011-12-19 11:16:50 +08001265 raise error.TestError('Invalid key in FAFT step: %s', key)
Tom Wai-Hong Tamd8445dc2011-12-15 09:00:04 +08001266
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001267 if test['state_checker']:
Tom Wai-Hong Tamfc700b52012-09-13 21:33:52 +08001268 self._call_action(test['state_checker'], check_status=True)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001269
1270 self._call_action(test['userspace_action'])
1271
1272 # Don't run reboot_action and firmware_action if no_reboot is True.
1273 if not no_reboot:
1274 self._call_action(test['reboot_action'])
1275 self.wait_for_client_offline()
1276 self._call_action(test['firmware_action'])
1277
Vic Yang8eaf5ad2012-09-13 14:05:37 +08001278 try:
1279 if 'install_deps_after_boot' in test:
1280 self.wait_for_client(
1281 install_deps=test['install_deps_after_boot'])
1282 else:
1283 self.wait_for_client()
1284 except AssertionError:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001285 logging.info('wait_for_client() timed out.')
Vic Yang8eaf5ad2012-09-13 14:05:37 +08001286 self.reset_client()
Tom Wai-Hong Tam4bb85e22012-10-25 14:35:24 +08001287 if self._trapped_in_recovery_reason:
1288 raise error.TestFail('Trapped in the recovery reason: %d' %
1289 self._trapped_in_recovery_reason)
1290 else:
1291 raise error.TestFail('Timed out waiting for DUT reboot.')
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001292
1293
1294 def run_faft_sequence(self):
1295 """Run FAFT sequence which was previously registered."""
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001296 sequence = self._faft_sequence
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001297 index = 1
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001298 for step in sequence:
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001299 logging.info('======== Running FAFT sequence step %d ========' %
1300 index)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +08001301 # Don't reboot in the last step.
1302 self.run_faft_step(step, no_reboot=(step is sequence[-1]))
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +08001303 index += 1
ctchang38ae4922012-09-03 17:01:16 +08001304
1305
ctchang38ae4922012-09-03 17:01:16 +08001306 def get_current_firmware_sha(self):
1307 """Get current firmware sha of body and vblock.
1308
1309 Returns:
1310 Current firmware sha follows the order (
1311 vblock_a_sha, body_a_sha, vblock_b_sha, body_b_sha)
1312 """
1313 current_firmware_sha = (self.faft_client.get_firmware_sig_sha('a'),
1314 self.faft_client.get_firmware_sha('a'),
1315 self.faft_client.get_firmware_sig_sha('b'),
1316 self.faft_client.get_firmware_sha('b'))
1317 return current_firmware_sha
1318
1319
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001320 def is_firmware_changed(self):
1321 """Check if the current firmware changed, by comparing its SHA.
ctchang38ae4922012-09-03 17:01:16 +08001322
1323 Returns:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001324 True if it is changed, otherwise Flase.
ctchang38ae4922012-09-03 17:01:16 +08001325 """
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001326 # Device may not be rebooted after test.
1327 self.faft_client.reload_firmware()
ctchang38ae4922012-09-03 17:01:16 +08001328
1329 current_sha = self.get_current_firmware_sha()
1330
1331 if current_sha == self._backup_firmware_sha:
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001332 return False
ctchang38ae4922012-09-03 17:01:16 +08001333 else:
ctchang38ae4922012-09-03 17:01:16 +08001334 corrupt_VBOOTA = (current_sha[0] != self._backup_firmware_sha[0])
1335 corrupt_FVMAIN = (current_sha[1] != self._backup_firmware_sha[1])
1336 corrupt_VBOOTB = (current_sha[2] != self._backup_firmware_sha[2])
1337 corrupt_FVMAINB = (current_sha[3] != self._backup_firmware_sha[3])
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001338 logging.info("Firmware changed:")
1339 logging.info('VBOOTA is changed: %s' % corrupt_VBOOTA)
1340 logging.info('VBOOTB is changed: %s' % corrupt_VBOOTB)
1341 logging.info('FVMAIN is changed: %s' % corrupt_FVMAIN)
1342 logging.info('FVMAINB is changed: %s' % corrupt_FVMAINB)
1343 return True
ctchang38ae4922012-09-03 17:01:16 +08001344
1345
1346 def backup_firmware(self, suffix='.original'):
1347 """Backup firmware to file, and then send it to host.
1348
1349 Args:
1350 suffix: a string appended to backup file name
1351 """
1352 remote_temp_dir = self.faft_client.create_temp_dir()
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001353 self.faft_client.dump_firmware(os.path.join(remote_temp_dir, 'bios'))
1354 self._client.get_file(os.path.join(remote_temp_dir, 'bios'),
1355 os.path.join(self.resultsdir, 'bios' + suffix))
ctchang38ae4922012-09-03 17:01:16 +08001356
1357 self._backup_firmware_sha = self.get_current_firmware_sha()
1358 logging.info('Backup firmware stored in %s with suffix %s' % (
1359 self.resultsdir, suffix))
1360
1361
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001362 def is_firmware_saved(self):
1363 """Check if a firmware saved (called backup_firmware before).
1364
1365 Returns:
1366 True if the firmware is backuped; otherwise False.
1367 """
1368 return self._backup_firmware_sha != ()
1369
1370
Tom Wai-Hong Tam1dd11592012-10-26 15:01:45 +08001371 def clear_saved_firmware(self):
1372 """Clear the firmware saved by the method backup_firmware."""
1373 self._backup_firmware_sha = ()
1374
1375
ctchang38ae4922012-09-03 17:01:16 +08001376 def restore_firmware(self, suffix='.original'):
1377 """Restore firmware from host in resultsdir.
1378
1379 Args:
1380 suffix: a string appended to backup file name
1381 """
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001382 if not self.is_firmware_changed():
ctchang38ae4922012-09-03 17:01:16 +08001383 return
1384
1385 # Backup current corrupted firmware.
1386 self.backup_firmware(suffix='.corrupt')
1387
1388 # Restore firmware.
1389 remote_temp_dir = self.faft_client.create_temp_dir()
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001390 self._client.send_file(os.path.join(self.resultsdir, 'bios' + suffix),
1391 os.path.join(remote_temp_dir, 'bios'))
ctchang38ae4922012-09-03 17:01:16 +08001392
Chun-ting Chang9380c2c2012-10-05 17:31:05 +08001393 self.faft_client.write_firmware(os.path.join(remote_temp_dir, 'bios'))
Tom Wai-Hong Tame6232342012-09-24 16:18:01 +08001394 self.sync_and_warm_reboot()
1395 self.wait_for_client_offline()
1396 self.wait_for_client()
1397
ctchang38ae4922012-09-03 17:01:16 +08001398 logging.info('Successfully restore firmware.')
Chun-ting Changf91ee0f2012-09-17 18:31:54 +08001399
1400
1401 def setup_firmwareupdate_shellball(self, shellball=None):
1402 """Deside a shellball to use in firmware update test.
1403
1404 Check if there is a given shellball, and it is a shell script. Then,
1405 send it to the remote host. Otherwise, use
1406 /usr/sbin/chromeos-firmwareupdate.
1407
1408 Args:
1409 shellball: path of a shellball or default to None.
1410
1411 Returns:
1412 Path of shellball in remote host.
1413 If use default shellball, reutrn None.
1414 """
1415 updater_path = None
1416 if shellball:
1417 # Determine the firmware file is a shellball or a raw binary.
1418 is_shellball = (utils.system_output("file %s" % shellball).find(
1419 "shell script") != -1)
1420 if is_shellball:
1421 logging.info('Device will update firmware with shellball %s'
1422 % shellball)
1423 temp_dir = self.faft_client.create_temp_dir('shellball_')
1424 temp_shellball = os.path.join(temp_dir, 'updater.sh')
1425 self._client.send_file(shellball, temp_shellball)
1426 updater_path = temp_shellball
1427 else:
1428 raise error.TestFail(
1429 'The given shellball is not a shell script.')
1430 return updater_path