blob: a45efbe1914269fd3072a726ba1c36a82c614776 [file] [log] [blame]
J. Richard Barnette384056b2012-04-16 11:04:46 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4#
5# Expects to be run in an environment with sudo and no interactive password
6# prompt, such as within the Chromium OS development chroot.
7
Vadim Bendeburyb80ba592012-12-07 15:02:34 -08008import os
9
Vic Yang3a7cf602012-11-07 17:28:39 +080010import logging, re, time, xmlrpclib
Vadim Bendeburyb80ba592012-12-07 15:02:34 -080011
Simran Basi741b5d42012-05-18 11:27:15 -070012from autotest_lib.client.common_lib import error
Ricky Liangc31aab32014-07-03 16:23:29 +080013from autotest_lib.server.cros.servo import firmware_programmer
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070014
J. Richard Barnette41320ee2013-03-11 13:00:13 -070015
J. Richard Barnette0c9c5882014-06-11 15:27:05 -070016class _PowerStateController(object):
17
18 """Class to provide board-specific power operations.
19
20 This class is responsible for "power on" and "power off"
21 operations that can operate without making assumptions in
22 advance about board state. It offers an interface that
23 abstracts out the different sequences required for different
24 board types.
25
26 """
27
28 # Constants acceptable to be passed for the `rec_mode` parameter
29 # to power_on().
30 #
31 # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or
32 # SD card.
33 # REC_OFF: Boot in normal mode, i.e. boot from internal storage.
34
35 REC_ON = 'rec'
36 REC_OFF = 'on'
37
38 # Delay in seconds needed between asserting and de-asserting
39 # warm reset.
40 _RESET_HOLD_TIME = 0.5
41
42 def __init__(self, servo):
43 """Initialize the power state control.
44
45 @param servo Servo object providing the underlying `set` and `get`
46 methods for the target controls.
47
48 """
49 self._servo = servo
50
51 def reset(self):
52 """Force the DUT to reset.
53
54 The DUT is guaranteed to be on at the end of this call,
55 regardless of its previous state, provided that there is
56 working OS software. This also guarantees that the EC has
57 been restarted.
58
59 """
60 self._servo.set_nocheck('power_state', 'reset')
61
62 def warm_reset(self):
63 """Apply warm reset to the DUT.
64
65 This asserts, then de-asserts the 'warm_reset' signal.
66 Generally, this causes the board to restart.
67
68 """
69 self._servo.set_get_all(['warm_reset:on',
70 'sleep:%.4f' % self._RESET_HOLD_TIME,
71 'warm_reset:off'])
72
J. Richard Barnette0c9c5882014-06-11 15:27:05 -070073 def power_off(self):
74 """Force the DUT to power off.
75
76 The DUT is guaranteed to be off at the end of this call,
77 regardless of its previous state, provided that there is
78 working EC and boot firmware. There is no requirement for
79 working OS software.
80
81 """
82 self._servo.set_nocheck('power_state', 'off')
83
84 def power_on(self, rec_mode=REC_OFF):
85 """Force the DUT to power on.
86
87 Prior to calling this function, the DUT must be powered off,
88 e.g. with a call to `power_off()`.
89
90 At power on, recovery mode is set as specified by the
91 corresponding argument. When booting with recovery mode on, it
92 is the caller's responsibility to unplug/plug in a bootable
93 external storage device.
94
95 If the DUT requires a delay after powering on but before
96 processing inputs such as USB stick insertion, the delay is
97 handled by this method; the caller is not responsible for such
98 delays.
99
100 @param rec_mode Setting of recovery mode to be applied at
101 power on. default: REC_OFF aka 'off'
102
103 """
104 self._servo.set_nocheck('power_state', rec_mode)
105
106
J. Richard Barnette384056b2012-04-16 11:04:46 -0700107class Servo(object):
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700108
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700109 """Manages control of a Servo board.
110
111 Servo is a board developed by hardware group to aide in the debug and
112 control of various partner devices. Servo's features include the simulation
113 of pressing the power button, closing the lid, and pressing Ctrl-d. This
114 class manages setting up and communicating with a servo demon (servod)
115 process. It provides both high-level functions for common servo tasks and
116 low-level functions for directly setting and reading gpios.
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700117
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700118 """
119
Chrome Bot9a1137d2011-07-19 14:35:00 -0700120 # Power button press delays in seconds.
J. Richard Barnetted2e4cbd2012-06-29 12:18:40 -0700121 #
J. Richard Barnette5383f072012-07-26 17:35:40 -0700122 # The EC specification says that 8.0 seconds should be enough
123 # for the long power press. However, some platforms need a bit
124 # more time. Empirical testing has found these requirements:
125 # Alex: 8.2 seconds
126 # ZGB: 8.5 seconds
127 # The actual value is set to the largest known necessary value.
128 #
129 # TODO(jrbarnette) Being generous is the right thing to do for
130 # existing platforms, but if this code is to be used for
131 # qualification of new hardware, we should be less generous.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700132 SHORT_DELAY = 0.1
J. Richard Barnette5383f072012-07-26 17:35:40 -0700133
Todd Broch31c82502011-08-29 08:14:39 -0700134 # Maximum number of times to re-read power button on release.
Todd Brochcf7c6652012-02-24 13:03:59 -0800135 GET_RETRY_MAX = 10
Chrome Bot9a1137d2011-07-19 14:35:00 -0700136
J. Richard Barnette5383f072012-07-26 17:35:40 -0700137 # Delays to deal with DUT state transitions.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700138 SLEEP_DELAY = 6
139 BOOT_DELAY = 10
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700140
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700141 # Default minimum time interval between 'press' and 'release'
142 # keyboard events.
Vic Yang0aca1c22012-11-19 18:33:56 -0800143 SERVO_KEY_PRESS_DELAY = 0.1
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700144
Tom Wai-Hong Tam93b5c092015-05-14 02:50:43 +0800145 # Time to toggle recovery switch on and off.
146 REC_TOGGLE_DELAY = 0.1
147
Tom Wai-Hong Tamf9ded092015-05-20 05:37:22 +0800148 # Time to toggle development switch on and off.
149 DEV_TOGGLE_DELAY = 0.1
150
Jon Salzc88e5b62011-11-30 14:38:54 +0800151 # Time between an usb disk plugged-in and detected in the system.
152 USB_DETECTION_DELAY = 10
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800153 # Time to keep USB power off before and after USB mux direction is changed
154 USB_POWEROFF_DELAY = 2
Jon Salzc88e5b62011-11-30 14:38:54 +0800155
Simran Basib7850bb2013-07-24 12:33:42 -0700156 # Time to wait before timing out on servo initialization.
157 INIT_TIMEOUT_SECS = 10
Simran Basi59331342013-07-12 12:06:29 -0700158
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700159
Ricky Liang0dd379c2014-04-23 16:29:08 +0800160 def __init__(self, servo_host, servo_serial=None):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700161 """Sets up the servo communication infrastructure.
162
Fang Deng5d518f42013-08-02 14:04:32 -0700163 @param servo_host: A ServoHost object representing
164 the host running servod.
Ricky Liang0dd379c2014-04-23 16:29:08 +0800165 @param servo_serial: Serial number of the servo board.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700166 """
Fang Deng5d518f42013-08-02 14:04:32 -0700167 # TODO(fdeng): crbug.com/298379
168 # We should move servo_host object out of servo object
169 # to minimize the dependencies on the rest of Autotest.
170 self._servo_host = servo_host
Ricky Liang0dd379c2014-04-23 16:29:08 +0800171 self._servo_serial = servo_serial
Fang Deng5d518f42013-08-02 14:04:32 -0700172 self._server = servo_host.get_servod_server_proxy()
J. Richard Barnette0c9c5882014-06-11 15:27:05 -0700173 self._power_state = _PowerStateController(self)
Fang Dengafb88142013-05-30 17:44:31 -0700174 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700175 self._programmer = None
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800176
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800177
Ricky Liang0dd379c2014-04-23 16:29:08 +0800178 @property
179 def servo_serial(self):
180 """Returns the serial number of the servo board."""
181 return self._servo_serial
182
183
Tom Wai-Hong Tam22ee0e52013-04-03 13:36:39 +0800184 def get_power_state_controller(self):
J. Richard Barnette75136b32013-03-26 13:38:44 -0700185 """Return the power state controller for this Servo.
186
187 The power state controller provides board-independent
188 interfaces for reset, power-on, power-off operations.
189
190 """
191 return self._power_state
192
Fang Deng5d518f42013-08-02 14:04:32 -0700193
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700194 def initialize_dut(self, cold_reset=False):
195 """Initializes a dut for testing purposes.
196
197 This sets various servo signals back to default values
198 appropriate for the target board. By default, if the DUT
199 is already on, it stays on. If the DUT is powered off
200 before initialization, its state afterward is unspecified.
201
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700202 Rationale: Basic initialization of servo sets the lid open,
203 when there is a lid. This operation won't affect powered on
204 units; however, setting the lid open may power on a unit
J. Richard Barnette75136b32013-03-26 13:38:44 -0700205 that's off, depending on the board type and previous state
206 of the device.
207
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700208 If `cold_reset` is a true value, the DUT and its EC will be
209 reset, and the DUT rebooted in normal mode.
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700210
211 @param cold_reset If True, cold reset the device after
212 initialization.
213 """
214 self._server.hwinit()
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700215 self.set('usb_mux_oe1', 'on')
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700216 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700217 self.switch_usbkey('off')
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700218 if cold_reset:
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700219 self._power_state.reset()
J. Richard Barnette1777f5d2014-09-03 15:18:19 -0700220 logging.debug('Servo initialized, version is %s',
221 self._server.get_version())
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700222
223
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800224 def is_localhost(self):
225 """Is the servod hosted locally?
226
227 Returns:
228 True if local hosted; otherwise, False.
229 """
Fang Deng5d518f42013-08-02 14:04:32 -0700230 return self._servo_host.is_localhost()
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800231
232
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700233 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700234 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700235 # After a long power press, the EC may ignore the next power
236 # button press (at least on Alex). To guarantee that this
237 # won't happen, we need to allow the EC one second to
238 # collect itself.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800239 self._server.power_long_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700240
241
242 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700243 """Simulate a normal power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800244 self._server.power_normal_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700245
246
247 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700248 """Simulate a short power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800249 self._server.power_short_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700250
251
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800252 def power_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700253 """Simulate a power button press.
254
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800255 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700256 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800257 self._server.power_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700258
259
260 def lid_open(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800261 """Simulate opening the lid and raise exception if all attempts fail"""
262 self.set('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700263
264
265 def lid_close(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800266 """Simulate closing the lid and raise exception if all attempts fail
Craig Harrison48997262011-06-27 14:31:10 -0700267
268 Waits 6 seconds to ensure the device is fully asleep before returning.
269 """
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800270 self.set('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700271 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700272
Shelley Chenc26575a2015-09-18 10:56:16 -0700273 def volume_up(self, timeout=300):
Kevin Chenge448a8b2016-09-09 10:18:11 -0700274 """Simulate pushing the volume down button.
275
276 @param timeout: Timeout for setting the volume.
277 """
Shelley Chenc26575a2015-09-18 10:56:16 -0700278 self.set_get_all(['volume_up:yes',
279 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY,
280 'volume_up:no'])
281 # we need to wait for commands to take effect before moving on
282 time_left = float(timeout)
283 while time_left > 0.0:
284 value = self.get('volume_up')
285 if value == 'no':
286 return
287 time.sleep(self.SHORT_DELAY)
288 time_left = time_left - self.SHORT_DELAY
289 raise error.TestFail("Failed setting volume_up to no")
290
291 def volume_down(self, timeout=300):
Kevin Chenge448a8b2016-09-09 10:18:11 -0700292 """Simulate pushing the volume down button.
293
294 @param timeout: Timeout for setting the volume.
295 """
Shelley Chenc26575a2015-09-18 10:56:16 -0700296 self.set_get_all(['volume_down:yes',
297 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY,
298 'volume_down:no'])
299 # we need to wait for commands to take effect before moving on
300 time_left = float(timeout)
301 while time_left > 0.0:
302 value = self.get('volume_down')
303 if value == 'no':
304 return
305 time.sleep(self.SHORT_DELAY)
306 time_left = time_left - self.SHORT_DELAY
307 raise error.TestFail("Failed setting volume_down to no")
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700308
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800309 def ctrl_d(self, press_secs=''):
310 """Simulate Ctrl-d simultaneous button presses.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800311
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800312 @param press_secs : Str. Time to press key.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800313 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800314 self._server.ctrl_d(press_secs)
Gediminas Ramanauskas9da80f22012-11-14 12:59:43 -0800315
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800316
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800317 def ctrl_u(self):
318 """Simulate Ctrl-u simultaneous button presses.
319
320 @param press_secs : Str. Time to press key.
321 """
322 self._server.ctrl_u()
Todd Broch9dfc3a82011-11-01 08:09:28 -0700323
324
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800325 def ctrl_enter(self, press_secs=''):
326 """Simulate Ctrl-enter simultaneous button presses.
327
328 @param press_secs : Str. Time to press key.
329 """
330 self._server.ctrl_enter(press_secs)
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700331
332
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800333 def d_key(self, press_secs=''):
334 """Simulate Enter key button press.
335
336 @param press_secs : Str. Time to press key.
337 """
338 self._server.d_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700339
340
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800341 def ctrl_key(self, press_secs=''):
342 """Simulate Enter key button press.
343
344 @param press_secs : Str. Time to press key.
345 """
346 self._server.ctrl_key(press_secs)
Todd Broch9753bd42012-03-21 10:15:08 -0700347
348
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800349 def enter_key(self, press_secs=''):
350 """Simulate Enter key button press.
351
352 @param press_secs : Str. Time to press key.
353 """
354 self._server.enter_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700355
356
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800357 def refresh_key(self, press_secs=''):
358 """Simulate Refresh key (F3) button press.
359
360 @param press_secs : Str. Time to press key.
361 """
362 self._server.refresh_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700363
364
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800365 def ctrl_refresh_key(self, press_secs=''):
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800366 """Simulate Ctrl and Refresh (F3) simultaneous press.
367
368 This key combination is an alternative of Space key.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800369
370 @param press_secs : Str. Time to press key.
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800371 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800372 self._server.ctrl_refresh_key(press_secs)
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800373
374
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800375 def imaginary_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700376 """Simulate imaginary key button press.
377
378 Maps to a key that doesn't physically exist.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800379
380 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700381 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800382 self._server.imaginary_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700383
384
Vincent Palatine7dc9282016-07-14 11:31:58 +0200385 def sysrq_x(self, press_secs=''):
386 """Simulate Alt VolumeUp X simulataneous press.
387
388 This key combination is the kernel system request (sysrq) X.
389
390 @param press_secs : Str. Time to press key.
391 """
392 self._server.sysrq_x(press_secs)
393
394
Tom Wai-Hong Tam93b5c092015-05-14 02:50:43 +0800395 def toggle_recovery_switch(self):
396 """Toggle recovery switch on and off."""
397 self.enable_recovery_mode()
398 time.sleep(self.REC_TOGGLE_DELAY)
399 self.disable_recovery_mode()
400
401
Craig Harrison6b36b122011-06-28 17:58:43 -0700402 def enable_recovery_mode(self):
403 """Enable recovery mode on device."""
404 self.set('rec_mode', 'on')
405
406
407 def disable_recovery_mode(self):
408 """Disable recovery mode on device."""
409 self.set('rec_mode', 'off')
410
411
Tom Wai-Hong Tamf9ded092015-05-20 05:37:22 +0800412 def toggle_development_switch(self):
413 """Toggle development switch on and off."""
414 self.enable_development_mode()
415 time.sleep(self.DEV_TOGGLE_DELAY)
416 self.disable_development_mode()
417
418
Craig Harrison6b36b122011-06-28 17:58:43 -0700419 def enable_development_mode(self):
420 """Enable development mode on device."""
421 self.set('dev_mode', 'on')
422
423
424 def disable_development_mode(self):
425 """Disable development mode on device."""
426 self.set('dev_mode', 'off')
427
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700428 def boot_devmode(self):
429 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800430 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700431 self.pass_devmode()
432
433
434 def pass_devmode(self):
435 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700436 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700437 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700438 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700439
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700440
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800441 def get_board(self):
442 """Get the board connected to servod.
443
444 """
445 return self._server.get_board()
446
447
Todd Brochefe72cb2012-07-11 19:58:53 -0700448 def _get_xmlrpclib_exception(self, xmlexc):
449 """Get meaningful exception string from xmlrpc.
450
451 Args:
452 xmlexc: xmlrpclib.Fault object
453
454 xmlrpclib.Fault.faultString has the following format:
455
456 <type 'exception type'>:'actual error message'
457
458 Parse and return the real exception from the servod side instead of the
459 less meaningful one like,
460 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
461 attribute 'hw_driver'">
462
463 Returns:
464 string of underlying exception raised in servod.
465 """
466 return re.sub('^.*>:', '', xmlexc.faultString)
467
468
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700469 def get(self, gpio_name):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700470 """Get the value of a gpio from Servod.
471
472 @param gpio_name Name of the gpio.
473 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700474 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700475 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700476 return self._server.get(gpio_name)
Todd Brochefe72cb2012-07-11 19:58:53 -0700477 except xmlrpclib.Fault as e:
478 err_msg = "Getting '%s' :: %s" % \
479 (gpio_name, self._get_xmlrpclib_exception(e))
480 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700481
482
483 def set(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700484 """Set and check the value of a gpio using Servod.
485
486 @param gpio_name Name of the gpio.
487 @param gpio_value New setting for the gpio.
488 """
Chrome Bot9a1137d2011-07-19 14:35:00 -0700489 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800490 retry_count = Servo.GET_RETRY_MAX
491 while gpio_value != self.get(gpio_name) and retry_count:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700492 logging.warning("%s != %s, retry %d", gpio_name, gpio_value,
Todd Brochcf7c6652012-02-24 13:03:59 -0800493 retry_count)
494 retry_count -= 1
495 time.sleep(Servo.SHORT_DELAY)
496 if not retry_count:
497 assert gpio_value == self.get(gpio_name), \
498 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700499
500
501 def set_nocheck(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700502 """Set the value of a gpio using Servod.
503
504 @param gpio_name Name of the gpio.
505 @param gpio_value New setting for the gpio.
506 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700507 assert gpio_name and gpio_value
Todd Broch9753bd42012-03-21 10:15:08 -0700508 logging.info('Setting %s to %s', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700509 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700510 self._server.set(gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700511 except xmlrpclib.Fault as e:
512 err_msg = "Setting '%s' to '%s' :: %s" % \
513 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
514 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700515
516
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800517 def set_get_all(self, controls):
518 """Set &| get one or more control values.
519
520 @param controls: list of strings, controls to set &| get.
521
522 @raise: error.TestError in case error occurs setting/getting values.
523 """
524 rv = []
525 try:
Vic Yangcad9acb2013-05-21 14:02:05 +0800526 logging.info('Set/get all: %s', str(controls))
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800527 rv = self._server.set_get_all(controls)
528 except xmlrpclib.Fault as e:
529 # TODO(waihong): Remove the following backward compatibility when
530 # the new versions of hdctools are deployed.
531 if 'not supported' in str(e):
532 logging.warning('The servod is too old that set_get_all '
533 'not supported. Use set and get instead.')
534 for control in controls:
535 if ':' in control:
536 (name, value) = control.split(':')
537 if name == 'sleep':
538 time.sleep(float(value))
539 else:
540 self.set_nocheck(name, value)
541 rv.append(True)
542 else:
543 rv.append(self.get(name))
544 else:
545 err_msg = "Problem with '%s' :: %s" % \
546 (controls, self._get_xmlrpclib_exception(e))
547 raise error.TestFail(err_msg)
548 return rv
549
550
Jon Salzc88e5b62011-11-30 14:38:54 +0800551 # TODO(waihong) It may fail if multiple servo's are connected to the same
552 # host. Should look for a better way, like the USB serial name, to identify
553 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700554 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
555 # updated.
Jon Salzc88e5b62011-11-30 14:38:54 +0800556 def probe_host_usb_dev(self):
557 """Probe the USB disk device plugged-in the servo from the host side.
558
Kevin Chengeb06fe72016-08-22 15:26:32 -0700559 It uses servod to discover if there is a usb device attached to it.
Jon Salzc88e5b62011-11-30 14:38:54 +0800560
Jon Salzc88e5b62011-11-30 14:38:54 +0800561 Returns:
562 A string of USB disk path, like '/dev/sdb', or None if not existed.
563 """
Kevin Chengeb06fe72016-08-22 15:26:32 -0700564 return self._server.probe_host_usb_dev() or None
Jon Salzc88e5b62011-11-30 14:38:54 +0800565
566
Mike Truty49153d82012-08-21 22:27:30 -0500567 def image_to_servo_usb(self, image_path=None,
568 make_image_noninteractive=False):
569 """Install an image to the USB key plugged into the servo.
570
571 This method may copy any image to the servo USB key including a
572 recovery image or a test image. These images are frequently used
573 for test purposes such as restoring a corrupted image or conducting
574 an upgrade of ec/fw/kernel as part of a test of a specific image part.
575
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700576 @param image_path Path on the host to the recovery image.
577 @param make_image_noninteractive Make the recovery image
578 noninteractive, therefore the DUT
579 will reboot automatically after
580 installation.
Mike Truty49153d82012-08-21 22:27:30 -0500581 """
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700582 # We're about to start plugging/unplugging the USB key. We
583 # don't know the state of the DUT, or what it might choose
584 # to do to the device after hotplug. To avoid surprises,
585 # force the DUT to be off.
586 self._server.hwinit()
587 self._power_state.power_off()
Mike Truty49153d82012-08-21 22:27:30 -0500588
589 # Set up Servo's usb mux.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800590 self.switch_usbkey('host')
Mike Truty49153d82012-08-21 22:27:30 -0500591 if image_path:
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800592 logging.info('Searching for usb device and copying image to it. '
593 'Please wait a few minutes...')
Mike Truty49153d82012-08-21 22:27:30 -0500594 if not self._server.download_image_to_usb(image_path):
595 logging.error('Failed to transfer requested image to USB. '
596 'Please take a look at Servo Logs.')
597 raise error.AutotestError('Download image to usb failed.')
598 if make_image_noninteractive:
599 logging.info('Making image noninteractive')
600 if not self._server.make_image_noninteractive():
601 logging.error('Failed to make image noninteractive. '
602 'Please take a look at Servo Logs.')
603
604
Simran Basi741b5d42012-05-18 11:27:15 -0700605 def install_recovery_image(self, image_path=None,
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800606 make_image_noninteractive=False):
Dan Shic67f1332016-04-06 12:38:06 -0700607 """Install the recovery image specified by the path onto the DUT.
Jon Salzc88e5b62011-11-30 14:38:54 +0800608
609 This method uses google recovery mode to install a recovery image
610 onto a DUT through the use of a USB stick that is mounted on a servo
Tom Wai-Hong Tama07115e2012-01-09 12:27:01 +0800611 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800612 we use the recovery image already on the usb image.
613
Dan Shic67f1332016-04-06 12:38:06 -0700614 @param image_path: Path on the host to the recovery image.
615 @param make_image_noninteractive: Make the recovery image
616 noninteractive, therefore the DUT will reboot automatically
617 after installation.
Jon Salzc88e5b62011-11-30 14:38:54 +0800618 """
Mike Truty49153d82012-08-21 22:27:30 -0500619 self.image_to_servo_usb(image_path, make_image_noninteractive)
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700620 self._power_state.power_on(rec_mode=self._power_state.REC_ON)
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700621 self.switch_usbkey('dut')
Jon Salzc88e5b62011-11-30 14:38:54 +0800622
623
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800624 def _scp_image(self, image_path):
625 """Copy image to the servo host.
626
627 When programming a firmware image on the DUT, the image must be
628 located on the host to which the servo device is connected. Sometimes
629 servo is controlled by a remote host, in this case the image needs to
630 be transferred to the remote host.
631
632 @param image_path: a string, name of the firmware image file to be
633 transferred.
634 @return: a string, full path name of the copied file on the remote.
635 """
636
637 dest_path = os.path.join('/tmp', os.path.basename(image_path))
Fang Deng5d518f42013-08-02 14:04:32 -0700638 self._servo_host.send_file(image_path, dest_path)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800639 return dest_path
640
641
Dan Shifecdaf42015-07-28 10:17:26 -0700642 def system(self, command, timeout=3600):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700643 """Execute the passed in command on the servod host.
644
645 @param command Command to be executed.
Dan Shifecdaf42015-07-28 10:17:26 -0700646 @param timeout Maximum number of seconds of runtime allowed. Default to
647 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700648 """
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800649 logging.info('Will execute on servo host: %s', command)
Fang Deng5d518f42013-08-02 14:04:32 -0700650 self._servo_host.run(command, timeout=timeout)
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800651
652
Dan Shifecdaf42015-07-28 10:17:26 -0700653 def system_output(self, command, timeout=3600,
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800654 ignore_status=False, args=()):
655 """Execute the passed in command on the servod host, return stdout.
656
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700657 @param command a string, the command to execute
658 @param timeout an int, max number of seconds to wait til command
Dan Shifecdaf42015-07-28 10:17:26 -0700659 execution completes. Default to 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700660 @param ignore_status a Boolean, if true - ignore command's nonzero exit
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800661 status, otherwise an exception will be thrown
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700662 @param args a tuple of strings, each becoming a separate command line
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800663 parameter for the command
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700664 @return command's stdout as a string.
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800665 """
Fang Deng5d518f42013-08-02 14:04:32 -0700666 return self._servo_host.run(command, timeout=timeout,
667 ignore_status=ignore_status,
668 args=args).stdout.strip()
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800669
670
Dan Shia5fef052015-05-18 23:28:47 -0700671 def get_servo_version(self):
672 """Get the version of the servo, e.g., servo_v2 or servo_v3.
673
674 @return: The version of the servo.
675
676 """
677 return self._server.get_version()
678
679
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800680 def _initialize_programmer(self, rw_only=False):
681 """Initialize the firmware programmer.
682
683 @param rw_only: True to initialize a programmer which only
684 programs the RW portions.
685 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700686 if self._programmer:
687 return
688 # Initialize firmware programmer
Dan Shia5fef052015-05-18 23:28:47 -0700689 servo_version = self.get_servo_version()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700690 if servo_version.startswith('servo_v2'):
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700691 self._programmer = firmware_programmer.ProgrammerV2(self)
Wai-Hong Tamc36c4d22016-09-09 10:39:45 -0700692 self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self)
Dan Shia5fef052015-05-18 23:28:47 -0700693 elif servo_version.startswith('servo_v3'):
694 self._programmer = firmware_programmer.ProgrammerV3(self)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800695 self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self)
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700696 else:
697 raise error.TestError(
698 'No firmware programmer for servo version: %s' %
699 servo_version)
700
701
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800702 def program_bios(self, image, rw_only=False):
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800703 """Program bios on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800704
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800705 @param image: a string, file name of the BIOS image to program
706 on the DUT.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800707 @param rw_only: True to only program the RW portion of BIOS.
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800708
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800709 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700710 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800711 if not self.is_localhost():
712 image = self._scp_image(image)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800713 if rw_only:
714 self._programmer_rw.program_bios(image)
715 else:
716 self._programmer.program_bios(image)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800717
718
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800719 def program_ec(self, image, rw_only=False):
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800720 """Program ec on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800721
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800722 @param image: a string, file name of the EC image to program
723 on the DUT.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800724 @param rw_only: True to only program the RW portion of EC.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800725
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800726 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700727 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800728 if not self.is_localhost():
729 image = self._scp_image(image)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800730 if rw_only:
731 self._programmer_rw.program_ec(image)
732 else:
733 self._programmer.program_ec(image)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800734
Fang Dengafb88142013-05-30 17:44:31 -0700735
736 def _switch_usbkey_power(self, power_state, detection_delay=False):
737 """Switch usbkey power.
738
739 This function switches usbkey power by setting the value of
740 'prtctl4_pwren'. If the power is already in the
741 requested state, this function simply returns.
742
743 @param power_state: A string, 'on' or 'off'.
744 @param detection_delay: A boolean value, if True, sleep
745 for |USB_DETECTION_DELAY| after switching
746 the power on.
747 """
748 self.set('prtctl4_pwren', power_state)
749 if power_state == 'off':
750 time.sleep(self.USB_POWEROFF_DELAY)
751 elif detection_delay:
752 time.sleep(self.USB_DETECTION_DELAY)
753
754
755 def switch_usbkey(self, usb_state):
756 """Connect USB flash stick to either host or DUT, or turn USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800757
758 This function switches the servo multiplexer to provide electrical
Fang Dengafb88142013-05-30 17:44:31 -0700759 connection between the USB port J3 and either host or DUT side. It
760 can also be used to turn the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800761
Fang Dengafb88142013-05-30 17:44:31 -0700762 Switching to 'dut' or 'host' is accompanied by powercycling
763 of the USB stick, because it sometimes gets wedged if the mux
764 is switched while the stick power is on.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800765
Fang Dengafb88142013-05-30 17:44:31 -0700766 @param usb_state: A string, one of 'dut', 'host', or 'off'.
767 'dut' and 'host' indicate which side the
768 USB flash device is required to be connected to.
769 'off' indicates turning the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800770
Fang Dengafb88142013-05-30 17:44:31 -0700771 @raise: error.TestError in case the parameter is not 'dut'
772 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800773 """
Fang Dengafb88142013-05-30 17:44:31 -0700774 if self.get_usbkey_direction() == usb_state:
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800775 return
776
Fang Dengafb88142013-05-30 17:44:31 -0700777 if usb_state == 'off':
Dan Shia5fef052015-05-18 23:28:47 -0700778 self._switch_usbkey_power('off')
779 self._usb_state = usb_state
780 return
Fang Dengafb88142013-05-30 17:44:31 -0700781 elif usb_state == 'host':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800782 mux_direction = 'servo_sees_usbkey'
Fang Dengafb88142013-05-30 17:44:31 -0700783 elif usb_state == 'dut':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800784 mux_direction = 'dut_sees_usbkey'
785 else:
Fang Dengafb88142013-05-30 17:44:31 -0700786 raise error.TestError('Unknown USB state request: %s' % usb_state)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800787
Fang Dengafb88142013-05-30 17:44:31 -0700788 self._switch_usbkey_power('off')
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800789 self.set('usb_mux_sel1', mux_direction)
790 time.sleep(self.USB_POWEROFF_DELAY)
Fang Dengafb88142013-05-30 17:44:31 -0700791 self._switch_usbkey_power('on', usb_state == 'host')
792 self._usb_state = usb_state
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800793
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800794
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800795 def get_usbkey_direction(self):
Fang Dengafb88142013-05-30 17:44:31 -0700796 """Get which side USB is connected to or 'off' if usb power is off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800797
Fang Dengafb88142013-05-30 17:44:31 -0700798 @return: A string, one of 'dut', 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800799 """
Fang Dengafb88142013-05-30 17:44:31 -0700800 if not self._usb_state:
801 if self.get('prtctl4_pwren') == 'off':
802 self._usb_state = 'off'
803 elif self.get('usb_mux_sel1').startswith('dut'):
804 self._usb_state = 'dut'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800805 else:
Fang Dengafb88142013-05-30 17:44:31 -0700806 self._usb_state = 'host'
807 return self._usb_state