blob: b730fe75b409208dfa905368434fc6274f615829 [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
5import logging
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08006import os
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08007import re
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08008import tempfile
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08009import time
10import xmlrpclib
11
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +080012from autotest_lib.client.bin import utils
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080013from autotest_lib.client.common_lib import error
Tom Wai-Hong Tam22b77302011-11-03 13:03:48 +080014from autotest_lib.server.cros.servo_test import ServoTest
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080015
16
17class FAFTSequence(ServoTest):
18 """
19 The base class of Fully Automated Firmware Test Sequence.
20
21 Many firmware tests require several reboot cycles and verify the resulted
22 system states. To do that, an Autotest test case should detailly handle
23 every action on each step. It makes the test case hard to read and many
24 duplicated code. The base class FAFTSequence is to solve this problem.
25
26 The actions of one reboot cycle is defined in a dict, namely FAFT_STEP.
27 There are four functions in the FAFT_STEP dict:
28 state_checker: a function to check the current is valid or not,
29 returning True if valid, otherwise, False to break the whole
30 test sequence.
31 userspace_action: a function to describe the action ran in userspace.
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080032 reboot_action: a function to do reboot, default: sync_and_hw_reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080033 firmware_action: a function to describe the action ran after reboot.
34
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +080035 And configurations:
36 install_deps_after_boot: if True, install the Autotest dependency after
37 boot; otherwise, do nothing. It is for the cases of recovery mode
38 test. The test boots a USB/SD image instead of an internal image.
39 The previous installed Autotest dependency on the internal image
40 is lost. So need to install it again.
41
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080042 The default FAFT_STEP checks nothing in state_checker and does nothing in
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080043 userspace_action and firmware_action. Its reboot_action is a hardware
44 reboot. You can change the default FAFT_STEP by calling
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080045 self.register_faft_template(FAFT_STEP).
46
47 A FAFT test case consists of several FAFT_STEP's, namely FAFT_SEQUENCE.
48 FAFT_SEQUENCE is an array of FAFT_STEP's. Any missing fields on FAFT_STEP
49 fall back to default.
50
51 In the run_once(), it should register and run FAFT_SEQUENCE like:
52 def run_once(self):
53 self.register_faft_sequence(FAFT_SEQUENCE)
54 self.run_faft_sequnce()
55
56 Note that in the last step, we only run state_checker. The
57 userspace_action, reboot_action, and firmware_action are not executed.
58
59 Attributes:
60 _faft_template: The default FAFT_STEP of each step. The actions would
61 be over-written if the registered FAFT_SEQUENCE is valid.
62 _faft_sequence: The registered FAFT_SEQUENCE.
63 """
64 version = 1
65
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080066
67 # Mapping of partition number of kernel and rootfs.
68 KERNEL_MAP = {'a':'2', 'b':'4', '2':'2', '4':'4', '3':'2', '5':'4'}
69 ROOTFS_MAP = {'a':'3', 'b':'5', '2':'3', '4':'5', '3':'3', '5':'5'}
70 OTHER_KERNEL_MAP = {'a':'4', 'b':'2', '2':'4', '4':'2', '3':'4', '5':'2'}
71 OTHER_ROOTFS_MAP = {'a':'5', 'b':'3', '2':'5', '4':'3', '3':'5', '5':'3'}
72
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +080073 # Delay timing
74 FIRMWARE_SCREEN_DELAY = 10
75 TEXT_SCREEN_DELAY = 20
Tom Wai-Hong Tam9ca742a2011-12-05 15:48:57 +080076 USB_PLUG_DELAY = 10
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +080077 SYNC_DELAY = 5
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080078
Tom Wai-Hong Tamf954d172011-12-08 17:14:15 +080079 # Recovery reason codes, copied from:
80 # vboot_reference/firmware/lib/vboot_nvstorage.h
81 # vboot_reference/firmware/lib/vboot_struct.h
82 RECOVERY_REASON = {
83 # Recovery not requested
84 'NOT_REQUESTED': '0', # 0x00
85 # Recovery requested from legacy utility
86 'LEGACY': '1', # 0x01
87 # User manually requested recovery via recovery button
88 'RO_MANUAL': '2', # 0x02
89 # RW firmware failed signature check
90 'RO_INVALID_RW': '3', # 0x03
91 # S3 resume failed
92 'RO_S3_RESUME': '4', # 0x04
93 # TPM error in read-only firmware
94 'RO_TPM_ERROR': '5', # 0x05
95 # Shared data error in read-only firmware
96 'RO_SHARED_DATA': '6', # 0x06
97 # Test error from S3Resume()
98 'RO_TEST_S3': '7', # 0x07
99 # Test error from LoadFirmwareSetup()
100 'RO_TEST_LFS': '8', # 0x08
101 # Test error from LoadFirmware()
102 'RO_TEST_LF': '9', # 0x09
103 # RW firmware failed signature check
104 'RW_NOT_DONE': '16', # 0x10
105 'RW_DEV_MISMATCH': '17', # 0x11
106 'RW_REC_MISMATCH': '18', # 0x12
107 'RW_VERIFY_KEYBLOCK': '19', # 0x13
108 'RW_KEY_ROLLBACK': '20', # 0x14
109 'RW_DATA_KEY_PARSE': '21', # 0x15
110 'RW_VERIFY_PREAMBLE': '22', # 0x16
111 'RW_FW_ROLLBACK': '23', # 0x17
112 'RW_HEADER_VALID': '24', # 0x18
113 'RW_GET_FW_BODY': '25', # 0x19
114 'RW_HASH_WRONG_SIZE': '26', # 0x1A
115 'RW_VERIFY_BODY': '27', # 0x1B
116 'RW_VALID': '28', # 0x1C
117 # Read-only normal path requested by firmware preamble, but
118 # unsupported by firmware.
119 'RW_NO_RO_NORMAL': '29', # 0x1D
120 # Firmware boot failure outside of verified boot
121 'RO_FIRMWARE': '32', # 0x20
122 # Recovery mode TPM initialization requires a system reboot.
123 # The system was already in recovery mode for some other reason
124 # when this happened.
125 'RO_TPM_REBOOT': '33', # 0x21
126 # Unspecified/unknown error in read-only firmware
127 'RO_UNSPECIFIED': '63', # 0x3F
128 # User manually requested recovery by pressing a key at developer
129 # warning screen.
130 'RW_DEV_SCREEN': '65', # 0x41
131 # No OS kernel detected
132 'RW_NO_OS': '66', # 0x42
133 # OS kernel failed signature check
134 'RW_INVALID_OS': '67', # 0x43
135 # TPM error in rewritable firmware
136 'RW_TPM_ERROR': '68', # 0x44
137 # RW firmware in dev mode, but dev switch is off.
138 'RW_DEV_MISMATCH': '69', # 0x45
139 # Shared data error in rewritable firmware
140 'RW_SHARED_DATA': '70', # 0x46
141 # Test error from LoadKernel()
142 'RW_TEST_LK': '71', # 0x47
143 # No bootable disk found
144 'RW_NO_DISK': '72', # 0x48
145 # Unspecified/unknown error in rewritable firmware
146 'RW_UNSPECIFIED': '127', # 0x7F
147 # DM-verity error
148 'KE_DM_VERITY': '129', # 0x81
149 # Unspecified/unknown error in kernel
150 'KE_UNSPECIFIED': '191', # 0xBF
151 # Recovery mode test from user-mode
152 'US_TEST': '193', # 0xC1
153 # Unspecified/unknown error in user-mode
154 'US_UNSPECIFIED': '255', # 0xFF
155 }
156
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800157 _faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800158 _faft_sequence = ()
159
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800160 _customized_ctrl_d_key_command = None
161 _customized_enter_key_command = None
162
163
164 def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False):
165 # Parse arguments from command line
166 args = {}
167 for arg in cmdline_args:
168 match = re.search("^(\w+)=(.+)", arg)
169 if match:
170 args[match.group(1)] = match.group(2)
171
172 # Keep the customized Ctrl-D and Enter key commands.
173 if 'ctrl_d_cmd' in args:
174 self._customized_ctrl_d_key_command = args['ctrl_d_cmd']
175 logging.info('Customized Ctrl-D key command: %s' %
176 self._customized_ctrl_d_key_command)
177 if 'enter_cmd' in args:
178 self._customized_enter_key_command = args['enter_cmd']
179 logging.info('Customized Enter key command: %s' %
180 self._customized_enter_key_command)
181
182 super(FAFTSequence, self).initialize(host, cmdline_args, use_pyauto,
183 use_faft)
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 Tamf1e34972011-11-02 17:07:04 +0800194 'reboot_action': (self.sync_and_hw_reboot),
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800195 'firmware_action': (None)
196 })
197
198
199 def cleanup(self):
200 """Autotest cleanup function."""
201 self._faft_sequence = ()
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800202 self._faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800203 super(FAFTSequence, self).cleanup()
204
205
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800206 def assert_test_image_in_usb_disk(self):
207 """Assert an USB disk plugged-in on servo and a test image inside.
208
209 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800210 error.TestError: if USB disk not detected or not a test image.
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800211 """
212 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Jon Salzc88e5b62011-11-30 14:38:54 +0800213 usb_dev = self.servo.probe_host_usb_dev()
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800214 if not usb_dev:
215 raise error.TestError(
216 'An USB disk should be plugged in the servo board.')
217
218 tmp_dir = tempfile.mkdtemp()
Tom Wai-Hong Tamb0e80852011-12-07 16:15:06 +0800219 utils.system('sudo mount -r -t ext2 %s3 %s' % (usb_dev, tmp_dir))
Tom Wai-Hong Tame77459e2011-11-03 17:19:46 +0800220 code = utils.system(
221 'grep -qE "(Test Build|testimage-channel)" %s/etc/lsb-release' %
222 tmp_dir, ignore_status=True)
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800223 utils.system('sudo umount %s' % tmp_dir)
224 os.removedirs(tmp_dir)
225 if code != 0:
226 raise error.TestError(
227 'The image in the USB disk should be a test image.')
228
229
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800230 def _parse_crossystem_output(self, lines):
231 """Parse the crossystem output into a dict.
232
233 Args:
234 lines: The list of crossystem output strings.
235
236 Returns:
237 A dict which contains the crossystem keys/values.
238
239 Raises:
240 error.TestError: If wrong format in crossystem output.
241
242 >>> seq = FAFTSequence()
243 >>> seq._parse_crossystem_output([ \
244 "arch = x86 # Platform architecture", \
245 "cros_debug = 1 # OS should allow debug", \
246 ])
247 {'cros_debug': '1', 'arch': 'x86'}
248 >>> seq._parse_crossystem_output([ \
249 "arch=x86", \
250 ])
251 Traceback (most recent call last):
252 ...
253 TestError: Failed to parse crossystem output: arch=x86
254 >>> seq._parse_crossystem_output([ \
255 "arch = x86 # Platform architecture", \
256 "arch = arm # Platform architecture", \
257 ])
258 Traceback (most recent call last):
259 ...
260 TestError: Duplicated crossystem key: arch
261 """
262 pattern = "^([^ =]*) *= *(.*[^ ]) *# [^#]*$"
263 parsed_list = {}
264 for line in lines:
265 matched = re.match(pattern, line.strip())
266 if not matched:
267 raise error.TestError("Failed to parse crossystem output: %s"
268 % line)
269 (name, value) = (matched.group(1), matched.group(2))
270 if name in parsed_list:
271 raise error.TestError("Duplicated crossystem key: %s" % name)
272 parsed_list[name] = value
273 return parsed_list
274
275
276 def crossystem_checker(self, expected_dict):
277 """Check the crossystem values matched.
278
279 Given an expect_dict which describes the expected crossystem values,
280 this function check the current crossystem values are matched or not.
281
282 Args:
283 expected_dict: A dict which contains the expected values.
284
285 Returns:
286 True if the crossystem value matched; otherwise, False.
287 """
288 lines = self.faft_client.run_shell_command_get_output('crossystem')
289 got_dict = self._parse_crossystem_output(lines)
290 for key in expected_dict:
291 if key not in got_dict:
292 logging.info('Expected key "%s" not in crossystem result' % key)
293 return False
294 if isinstance(expected_dict[key], str):
295 if got_dict[key] != expected_dict[key]:
296 logging.info("Expected '%s' value '%s' but got '%s'" %
297 (key, expected_dict[key], got_dict[key]))
298 return False
299 elif isinstance(expected_dict[key], tuple):
300 # Expected value is a tuple of possible actual values.
301 if got_dict[key] not in expected_dict[key]:
302 logging.info("Expected '%s' values %s but got '%s'" %
303 (key, str(expected_dict[key]), got_dict[key]))
304 return False
305 else:
306 logging.info("The expected_dict is neither a str nor a dict.")
307 return False
308 return True
309
310
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800311 def root_part_checker(self, expected_part):
312 """Check the partition number of the root device matched.
313
314 Args:
315 expected_part: A string containing the number of the expected root
316 partition.
317
318 Returns:
319 True if the currect root partition number matched; otherwise, False.
320 """
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800321 part = self.faft_client.get_root_part()[-1]
322 if self.ROOTFS_MAP[expected_part] != part:
323 logging.info("Expected root part %s but got %s" %
324 (self.ROOTFS_MAP[expected_part], part))
325 return False
326 return True
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800327
328
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800329 def _join_part(self, dev, part):
330 """Return a concatenated string of device and partition number.
331
332 Args:
333 dev: A string of device, e.g.'/dev/sda'.
334 part: A string of partition number, e.g.'3'.
335
336 Returns:
337 A concatenated string of device and partition number, e.g.'/dev/sda3'.
338
339 >>> seq = FAFTSequence()
340 >>> seq._join_part('/dev/sda', '3')
341 '/dev/sda3'
342 >>> seq._join_part('/dev/mmcblk0', '2')
343 '/dev/mmcblk0p2'
344 """
345 if 'mmcblk' in dev:
346 return dev + 'p' + part
347 else:
348 return dev + part
349
350
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800351 def copy_kernel_and_rootfs(self, from_part, to_part):
352 """Copy kernel and rootfs from from_part to to_part.
353
354 Args:
355 from_part: A string of partition number to be copied from.
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800356 to_part: A string of partition number to be copied to.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800357 """
358 root_dev = self.faft_client.get_root_dev()
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800359 logging.info('Copying kernel from %s to %s. Please wait...' %
360 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800361 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800362 (self._join_part(root_dev, self.KERNEL_MAP[from_part]),
363 self._join_part(root_dev, self.KERNEL_MAP[to_part])))
364 logging.info('Copying rootfs from %s to %s. Please wait...' %
365 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800366 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800367 (self._join_part(root_dev, self.ROOTFS_MAP[from_part]),
368 self._join_part(root_dev, self.ROOTFS_MAP[to_part])))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800369
370
371 def ensure_kernel_boot(self, part):
372 """Ensure the request kernel boot.
373
374 If not, it duplicates the current kernel to the requested kernel
375 and sets the requested higher priority to ensure it boot.
376
377 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800378 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800379 """
380 if not self.root_part_checker(part):
381 self.copy_kernel_and_rootfs(from_part=self.OTHER_KERNEL_MAP[part],
382 to_part=part)
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800383 self.run_faft_step({
384 'userspace_action': (self.reset_and_prioritize_kernel, part),
385 })
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800386
387
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800388 def send_ctrl_d_to_dut(self):
389 """Send Ctrl-D key to DUT."""
390 if self._customized_ctrl_d_key_command:
391 logging.info('running the customized Ctrl-D key command')
392 os.system(self._customized_ctrl_d_key_command)
393 else:
394 self.servo.ctrl_d()
395
396
397 def send_enter_to_dut(self):
398 """Send Enter key to DUT."""
399 if self._customized_enter_key_command:
400 logging.info('running the customized Enter key command')
401 os.system(self._customized_enter_key_command)
402 else:
403 self.servo.enter_key()
404
405
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800406 def wait_fw_screen_and_ctrl_d(self):
407 """Wait for firmware warning screen and press Ctrl-D."""
408 time.sleep(self.FIRMWARE_SCREEN_DELAY)
Tom Wai-Hong Tam1db43832011-12-09 10:50:56 +0800409 self.send_ctrl_d_to_dut()
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800410
411
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800412 def wait_fw_screen_and_plug_usb(self):
413 """Wait for firmware warning screen and then unplug and plug the USB."""
414 time.sleep(self.FIRMWARE_SCREEN_DELAY)
415 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
416 time.sleep(self.USB_PLUG_DELAY)
417 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
418
419
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800420 def setup_tried_fwb(self, tried_fwb):
421 """Setup for fw B tried state.
422
423 It makes sure the system in the requested fw B tried state. If not, it
424 tries to do so.
425
426 Args:
427 tried_fwb: True if requested in tried_fwb=1; False if tried_fwb=0.
428 """
429 if tried_fwb:
430 if not self.crossystem_checker({'tried_fwb': '1'}):
431 logging.info(
432 'Firmware is not booted with tried_fwb. Reboot into it.')
433 self.run_faft_step({
434 'userspace_action': self.faft_client.set_try_fw_b,
435 })
436 else:
437 if not self.crossystem_checker({'tried_fwb': '0'}):
438 logging.info(
439 'Firmware is booted with tried_fwb. Reboot to clear.')
440 self.run_faft_step({})
441
442
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800443 def setup_dev_mode(self, dev_mode):
444 """Setup for development mode.
445
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800446 It makes sure the system in the requested normal/dev mode. If not, it
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800447 tries to do so.
448
449 Args:
450 dev_mode: True if requested in dev mode; False if normal mode.
451 """
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800452 # Change the default firmware_action for dev mode passing the fw screen.
453 self.register_faft_template({
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800454 'firmware_action': (self.wait_fw_screen_and_ctrl_d if dev_mode
455 else None),
456 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800457 if dev_mode:
458 if not self.crossystem_checker({'devsw_cur': '1'}):
459 logging.info('Dev switch is not on. Now switch it on.')
460 self.servo.enable_development_mode()
461 if not self.crossystem_checker({'devsw_boot': '1',
462 'mainfw_type': 'developer'}):
463 logging.info('System is not in dev mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800464 self.run_faft_step({
465 'userspace_action': (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800466 'chromeos-firmwareupdate --mode todev && reboot'),
467 'reboot_action': None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800468 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800469 else:
470 if not self.crossystem_checker({'devsw_cur': '0'}):
471 logging.info('Dev switch is not off. Now switch it off.')
472 self.servo.disable_development_mode()
473 if not self.crossystem_checker({'devsw_boot': '0',
474 'mainfw_type': 'normal'}):
475 logging.info('System is not in normal mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800476 self.run_faft_step({
477 'userspace_action': (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800478 'chromeos-firmwareupdate --mode tonormal && reboot'),
479 'reboot_action': None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800480 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800481
482
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800483 def setup_kernel(self, part):
484 """Setup for kernel test.
485
486 It makes sure both kernel A and B bootable and the current boot is
487 the requested kernel part.
488
489 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800490 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800491 """
492 self.ensure_kernel_boot(part)
493 self.copy_kernel_and_rootfs(from_part=part,
494 to_part=self.OTHER_KERNEL_MAP[part])
495 self.reset_and_prioritize_kernel(part)
496
497
498 def reset_and_prioritize_kernel(self, part):
499 """Make the requested partition highest priority.
500
501 This function also reset kerenl A and B to bootable.
502
503 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800504 part: A string of partition number to be prioritized.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800505 """
506 root_dev = self.faft_client.get_root_dev()
507 # Reset kernel A and B to bootable.
508 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
509 (self.KERNEL_MAP['a'], root_dev))
510 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
511 (self.KERNEL_MAP['b'], root_dev))
512 # Set kernel part highest priority.
513 self.faft_client.run_shell_command('cgpt prioritize -i%s %s' %
514 (self.KERNEL_MAP[part], root_dev))
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800515 # Safer to sync and wait until the cgpt status written to the disk.
516 self.faft_client.run_shell_command('sync')
517 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800518
519
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +0800520 def sync_and_hw_reboot(self):
521 """Request the client sync and do a warm reboot.
522
523 This is the default reboot action on FAFT.
524 """
525 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800526 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +0800527 self.servo.warm_reset()
528
529
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800530 def _call_action(self, action_tuple):
531 """Call the action function with/without arguments.
532
533 Args:
534 action_tuple: A function, or a tuple which consisted of a function
535 and its arguments (if any).
536
537 Returns:
538 The result value of the action function.
539 """
540 if isinstance(action_tuple, tuple):
541 action = action_tuple[0]
542 args = action_tuple[1:]
543 if callable(action):
544 logging.info('calling %s with parameter %s' % (
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800545 str(action), str(action_tuple[1])))
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800546 return action(*args)
547 else:
548 logging.info('action is not callable!')
549 else:
550 action = action_tuple
551 if action is not None:
552 if callable(action):
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800553 logging.info('calling %s' % str(action))
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800554 return action()
555 else:
556 logging.info('action is not callable!')
557
558 return None
559
560
561 def register_faft_template(self, template):
562 """Register FAFT template, the default FAFT_STEP of each step.
563
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800564 Any missing field falls back to the original faft_template.
565
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800566 Args:
567 template: A FAFT_STEP dict.
568 """
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800569 self._faft_template.update(template)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800570
571
572 def register_faft_sequence(self, sequence):
573 """Register FAFT sequence.
574
575 Args:
576 sequence: A FAFT_SEQUENCE array which consisted of FAFT_STEP dicts.
577 """
578 self._faft_sequence = sequence
579
580
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800581 def run_faft_step(self, step, no_reboot=False):
582 """Run a single FAFT step.
583
584 Any missing field falls back to faft_template. An empty step means
585 running the default faft_template.
586
587 Args:
588 step: A FAFT_STEP dict.
589 no_reboot: True to prevent running reboot_action and firmware_action.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800590
591 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800592 error.TestFail: An error when the test failed.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800593 """
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800594 test = {}
595 test.update(self._faft_template)
596 test.update(step)
597
598 if test['state_checker']:
599 if not self._call_action(test['state_checker']):
600 raise error.TestFail('State checker failed!')
601
602 self._call_action(test['userspace_action'])
603
604 # Don't run reboot_action and firmware_action if no_reboot is True.
605 if not no_reboot:
606 self._call_action(test['reboot_action'])
607 self.wait_for_client_offline()
608 self._call_action(test['firmware_action'])
609
610 if 'install_deps_after_boot' in test:
611 self.wait_for_client(
612 install_deps=test['install_deps_after_boot'])
613 else:
614 self.wait_for_client()
615
616
617 def run_faft_sequence(self):
618 """Run FAFT sequence which was previously registered."""
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800619 sequence = self._faft_sequence
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800620 index = 1
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800621 for step in sequence:
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800622 logging.info('======== Running FAFT sequence step %d ========' %
623 index)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800624 # Don't reboot in the last step.
625 self.run_faft_step(step, no_reboot=(step is sequence[-1]))
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800626 index += 1