blob: 25abd2cbe2751b0d9d85c4e86c959383c01cf3f6 [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('dut_hub_pwren', 'on')
216 self.set('usb_mux_oe1', 'on')
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700217 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700218 self.switch_usbkey('off')
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700219 if cold_reset:
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700220 self._power_state.reset()
J. Richard Barnette1777f5d2014-09-03 15:18:19 -0700221 logging.debug('Servo initialized, version is %s',
222 self._server.get_version())
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700223
224
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800225 def is_localhost(self):
226 """Is the servod hosted locally?
227
228 Returns:
229 True if local hosted; otherwise, False.
230 """
Fang Deng5d518f42013-08-02 14:04:32 -0700231 return self._servo_host.is_localhost()
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800232
233
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700234 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700235 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700236 # After a long power press, the EC may ignore the next power
237 # button press (at least on Alex). To guarantee that this
238 # won't happen, we need to allow the EC one second to
239 # collect itself.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800240 self._server.power_long_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700241
242
243 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700244 """Simulate a normal power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800245 self._server.power_normal_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700246
247
248 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700249 """Simulate a short power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800250 self._server.power_short_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700251
252
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800253 def power_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700254 """Simulate a power button press.
255
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800256 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700257 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800258 self._server.power_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700259
260
261 def lid_open(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800262 """Simulate opening the lid and raise exception if all attempts fail"""
263 self.set('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700264
265
266 def lid_close(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800267 """Simulate closing the lid and raise exception if all attempts fail
Craig Harrison48997262011-06-27 14:31:10 -0700268
269 Waits 6 seconds to ensure the device is fully asleep before returning.
270 """
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800271 self.set('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700272 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700273
274
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800275 def ctrl_d(self, press_secs=''):
276 """Simulate Ctrl-d simultaneous button presses.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800277
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800278 @param press_secs : Str. Time to press key.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800279 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800280 self._server.ctrl_d(press_secs)
Gediminas Ramanauskas9da80f22012-11-14 12:59:43 -0800281
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800282
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800283 def ctrl_u(self):
284 """Simulate Ctrl-u simultaneous button presses.
285
286 @param press_secs : Str. Time to press key.
287 """
288 self._server.ctrl_u()
Todd Broch9dfc3a82011-11-01 08:09:28 -0700289
290
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800291 def ctrl_enter(self, press_secs=''):
292 """Simulate Ctrl-enter simultaneous button presses.
293
294 @param press_secs : Str. Time to press key.
295 """
296 self._server.ctrl_enter(press_secs)
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700297
298
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800299 def d_key(self, press_secs=''):
300 """Simulate Enter key button press.
301
302 @param press_secs : Str. Time to press key.
303 """
304 self._server.d_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700305
306
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800307 def ctrl_key(self, press_secs=''):
308 """Simulate Enter key button press.
309
310 @param press_secs : Str. Time to press key.
311 """
312 self._server.ctrl_key(press_secs)
Todd Broch9753bd42012-03-21 10:15:08 -0700313
314
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800315 def enter_key(self, press_secs=''):
316 """Simulate Enter key button press.
317
318 @param press_secs : Str. Time to press key.
319 """
320 self._server.enter_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700321
322
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800323 def refresh_key(self, press_secs=''):
324 """Simulate Refresh key (F3) button press.
325
326 @param press_secs : Str. Time to press key.
327 """
328 self._server.refresh_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700329
330
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800331 def ctrl_refresh_key(self, press_secs=''):
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800332 """Simulate Ctrl and Refresh (F3) simultaneous press.
333
334 This key combination is an alternative of Space key.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800335
336 @param press_secs : Str. Time to press key.
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800337 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800338 self._server.ctrl_refresh_key(press_secs)
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800339
340
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800341 def imaginary_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700342 """Simulate imaginary key button press.
343
344 Maps to a key that doesn't physically exist.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800345
346 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700347 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800348 self._server.imaginary_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700349
350
Tom Wai-Hong Tam93b5c092015-05-14 02:50:43 +0800351 def toggle_recovery_switch(self):
352 """Toggle recovery switch on and off."""
353 self.enable_recovery_mode()
354 time.sleep(self.REC_TOGGLE_DELAY)
355 self.disable_recovery_mode()
356
357
Craig Harrison6b36b122011-06-28 17:58:43 -0700358 def enable_recovery_mode(self):
359 """Enable recovery mode on device."""
360 self.set('rec_mode', 'on')
361
362
363 def disable_recovery_mode(self):
364 """Disable recovery mode on device."""
365 self.set('rec_mode', 'off')
366
367
Tom Wai-Hong Tamf9ded092015-05-20 05:37:22 +0800368 def toggle_development_switch(self):
369 """Toggle development switch on and off."""
370 self.enable_development_mode()
371 time.sleep(self.DEV_TOGGLE_DELAY)
372 self.disable_development_mode()
373
374
Craig Harrison6b36b122011-06-28 17:58:43 -0700375 def enable_development_mode(self):
376 """Enable development mode on device."""
377 self.set('dev_mode', 'on')
378
379
380 def disable_development_mode(self):
381 """Disable development mode on device."""
382 self.set('dev_mode', 'off')
383
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700384 def boot_devmode(self):
385 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800386 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700387 self.pass_devmode()
388
389
390 def pass_devmode(self):
391 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700392 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700393 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700394 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700395
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700396
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800397 def get_board(self):
398 """Get the board connected to servod.
399
400 """
401 return self._server.get_board()
402
403
Todd Brochefe72cb2012-07-11 19:58:53 -0700404 def _get_xmlrpclib_exception(self, xmlexc):
405 """Get meaningful exception string from xmlrpc.
406
407 Args:
408 xmlexc: xmlrpclib.Fault object
409
410 xmlrpclib.Fault.faultString has the following format:
411
412 <type 'exception type'>:'actual error message'
413
414 Parse and return the real exception from the servod side instead of the
415 less meaningful one like,
416 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
417 attribute 'hw_driver'">
418
419 Returns:
420 string of underlying exception raised in servod.
421 """
422 return re.sub('^.*>:', '', xmlexc.faultString)
423
424
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700425 def get(self, gpio_name):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700426 """Get the value of a gpio from Servod.
427
428 @param gpio_name Name of the gpio.
429 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700430 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700431 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700432 return self._server.get(gpio_name)
Todd Brochefe72cb2012-07-11 19:58:53 -0700433 except xmlrpclib.Fault as e:
434 err_msg = "Getting '%s' :: %s" % \
435 (gpio_name, self._get_xmlrpclib_exception(e))
436 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700437
438
439 def set(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700440 """Set and check the value of a gpio using Servod.
441
442 @param gpio_name Name of the gpio.
443 @param gpio_value New setting for the gpio.
444 """
Chrome Bot9a1137d2011-07-19 14:35:00 -0700445 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800446 retry_count = Servo.GET_RETRY_MAX
447 while gpio_value != self.get(gpio_name) and retry_count:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700448 logging.warning("%s != %s, retry %d", gpio_name, gpio_value,
Todd Brochcf7c6652012-02-24 13:03:59 -0800449 retry_count)
450 retry_count -= 1
451 time.sleep(Servo.SHORT_DELAY)
452 if not retry_count:
453 assert gpio_value == self.get(gpio_name), \
454 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700455
456
457 def set_nocheck(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700458 """Set the value of a gpio using Servod.
459
460 @param gpio_name Name of the gpio.
461 @param gpio_value New setting for the gpio.
462 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700463 assert gpio_name and gpio_value
Todd Broch9753bd42012-03-21 10:15:08 -0700464 logging.info('Setting %s to %s', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700465 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700466 self._server.set(gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700467 except xmlrpclib.Fault as e:
468 err_msg = "Setting '%s' to '%s' :: %s" % \
469 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
470 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700471
472
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800473 def set_get_all(self, controls):
474 """Set &| get one or more control values.
475
476 @param controls: list of strings, controls to set &| get.
477
478 @raise: error.TestError in case error occurs setting/getting values.
479 """
480 rv = []
481 try:
Vic Yangcad9acb2013-05-21 14:02:05 +0800482 logging.info('Set/get all: %s', str(controls))
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800483 rv = self._server.set_get_all(controls)
484 except xmlrpclib.Fault as e:
485 # TODO(waihong): Remove the following backward compatibility when
486 # the new versions of hdctools are deployed.
487 if 'not supported' in str(e):
488 logging.warning('The servod is too old that set_get_all '
489 'not supported. Use set and get instead.')
490 for control in controls:
491 if ':' in control:
492 (name, value) = control.split(':')
493 if name == 'sleep':
494 time.sleep(float(value))
495 else:
496 self.set_nocheck(name, value)
497 rv.append(True)
498 else:
499 rv.append(self.get(name))
500 else:
501 err_msg = "Problem with '%s' :: %s" % \
502 (controls, self._get_xmlrpclib_exception(e))
503 raise error.TestFail(err_msg)
504 return rv
505
506
Jon Salzc88e5b62011-11-30 14:38:54 +0800507 # TODO(waihong) It may fail if multiple servo's are connected to the same
508 # host. Should look for a better way, like the USB serial name, to identify
509 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700510 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
511 # updated.
Jon Salzc88e5b62011-11-30 14:38:54 +0800512 def probe_host_usb_dev(self):
513 """Probe the USB disk device plugged-in the servo from the host side.
514
515 It tries to switch the USB mux to make the host unable to see the
516 USB disk and compares the result difference.
517
Jon Salzc88e5b62011-11-30 14:38:54 +0800518 Returns:
519 A string of USB disk path, like '/dev/sdb', or None if not existed.
520 """
521 cmd = 'ls /dev/sd[a-z]'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800522 original_value = self.get_usbkey_direction()
Jon Salzc88e5b62011-11-30 14:38:54 +0800523
524 # Make the host unable to see the USB disk.
Fang Dengafb88142013-05-30 17:44:31 -0700525 self.switch_usbkey('off')
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800526 no_usb_set = set(self.system_output(cmd, ignore_status=True).split())
Jon Salzc88e5b62011-11-30 14:38:54 +0800527
528 # Make the host able to see the USB disk.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800529 self.switch_usbkey('host')
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800530 has_usb_set = set(self.system_output(cmd, ignore_status=True).split())
Jon Salzc88e5b62011-11-30 14:38:54 +0800531
532 # Back to its original value.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800533 if original_value != self.get_usbkey_direction():
534 self.switch_usbkey(original_value)
Jon Salzc88e5b62011-11-30 14:38:54 +0800535
536 diff_set = has_usb_set - no_usb_set
537 if len(diff_set) == 1:
538 return diff_set.pop()
539 else:
540 return None
541
542
Mike Truty49153d82012-08-21 22:27:30 -0500543 def image_to_servo_usb(self, image_path=None,
544 make_image_noninteractive=False):
545 """Install an image to the USB key plugged into the servo.
546
547 This method may copy any image to the servo USB key including a
548 recovery image or a test image. These images are frequently used
549 for test purposes such as restoring a corrupted image or conducting
550 an upgrade of ec/fw/kernel as part of a test of a specific image part.
551
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700552 @param image_path Path on the host to the recovery image.
553 @param make_image_noninteractive Make the recovery image
554 noninteractive, therefore the DUT
555 will reboot automatically after
556 installation.
Mike Truty49153d82012-08-21 22:27:30 -0500557 """
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700558 # We're about to start plugging/unplugging the USB key. We
559 # don't know the state of the DUT, or what it might choose
560 # to do to the device after hotplug. To avoid surprises,
561 # force the DUT to be off.
562 self._server.hwinit()
563 self._power_state.power_off()
Mike Truty49153d82012-08-21 22:27:30 -0500564
565 # Set up Servo's usb mux.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800566 self.switch_usbkey('host')
Mike Truty49153d82012-08-21 22:27:30 -0500567 if image_path:
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800568 logging.info('Searching for usb device and copying image to it. '
569 'Please wait a few minutes...')
Mike Truty49153d82012-08-21 22:27:30 -0500570 if not self._server.download_image_to_usb(image_path):
571 logging.error('Failed to transfer requested image to USB. '
572 'Please take a look at Servo Logs.')
573 raise error.AutotestError('Download image to usb failed.')
574 if make_image_noninteractive:
575 logging.info('Making image noninteractive')
576 if not self._server.make_image_noninteractive():
577 logging.error('Failed to make image noninteractive. '
578 'Please take a look at Servo Logs.')
579
580
Simran Basi741b5d42012-05-18 11:27:15 -0700581 def install_recovery_image(self, image_path=None,
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800582 make_image_noninteractive=False):
Jon Salzc88e5b62011-11-30 14:38:54 +0800583 """Install the recovery image specied by the path onto the DUT.
584
585 This method uses google recovery mode to install a recovery image
586 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 +0800587 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800588 we use the recovery image already on the usb image.
589
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700590 @param image_path Path on the host to the recovery image.
591 @param make_image_noninteractive Make the recovery image
592 noninteractive, therefore
593 the DUT will reboot
594 automatically after
595 installation.
Jon Salzc88e5b62011-11-30 14:38:54 +0800596 """
Mike Truty49153d82012-08-21 22:27:30 -0500597 self.image_to_servo_usb(image_path, make_image_noninteractive)
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700598 self._power_state.power_on(rec_mode=self._power_state.REC_ON)
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700599 self.switch_usbkey('dut')
Jon Salzc88e5b62011-11-30 14:38:54 +0800600
601
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800602 def _scp_image(self, image_path):
603 """Copy image to the servo host.
604
605 When programming a firmware image on the DUT, the image must be
606 located on the host to which the servo device is connected. Sometimes
607 servo is controlled by a remote host, in this case the image needs to
608 be transferred to the remote host.
609
610 @param image_path: a string, name of the firmware image file to be
611 transferred.
612 @return: a string, full path name of the copied file on the remote.
613 """
614
615 dest_path = os.path.join('/tmp', os.path.basename(image_path))
Fang Deng5d518f42013-08-02 14:04:32 -0700616 self._servo_host.send_file(image_path, dest_path)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800617 return dest_path
618
619
Dan Shifecdaf42015-07-28 10:17:26 -0700620 def system(self, command, timeout=3600):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700621 """Execute the passed in command on the servod host.
622
623 @param command Command to be executed.
Dan Shifecdaf42015-07-28 10:17:26 -0700624 @param timeout Maximum number of seconds of runtime allowed. Default to
625 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700626 """
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800627 logging.info('Will execute on servo host: %s', command)
Fang Deng5d518f42013-08-02 14:04:32 -0700628 self._servo_host.run(command, timeout=timeout)
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800629
630
Dan Shifecdaf42015-07-28 10:17:26 -0700631 def system_output(self, command, timeout=3600,
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800632 ignore_status=False, args=()):
633 """Execute the passed in command on the servod host, return stdout.
634
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700635 @param command a string, the command to execute
636 @param timeout an int, max number of seconds to wait til command
Dan Shifecdaf42015-07-28 10:17:26 -0700637 execution completes. Default to 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700638 @param ignore_status a Boolean, if true - ignore command's nonzero exit
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800639 status, otherwise an exception will be thrown
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700640 @param args a tuple of strings, each becoming a separate command line
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800641 parameter for the command
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700642 @return command's stdout as a string.
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800643 """
Fang Deng5d518f42013-08-02 14:04:32 -0700644 return self._servo_host.run(command, timeout=timeout,
645 ignore_status=ignore_status,
646 args=args).stdout.strip()
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800647
648
Dan Shia5fef052015-05-18 23:28:47 -0700649 def get_servo_version(self):
650 """Get the version of the servo, e.g., servo_v2 or servo_v3.
651
652 @return: The version of the servo.
653
654 """
655 return self._server.get_version()
656
657
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700658 def _initialize_programmer(self):
659 if self._programmer:
660 return
661 # Initialize firmware programmer
Dan Shia5fef052015-05-18 23:28:47 -0700662 servo_version = self.get_servo_version()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700663 if servo_version.startswith('servo_v2'):
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700664 self._programmer = firmware_programmer.ProgrammerV2(self)
Dan Shia5fef052015-05-18 23:28:47 -0700665 elif servo_version.startswith('servo_v3'):
666 self._programmer = firmware_programmer.ProgrammerV3(self)
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700667 else:
668 raise error.TestError(
669 'No firmware programmer for servo version: %s' %
670 servo_version)
671
672
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800673 def program_bios(self, image):
674 """Program bios on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800675
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800676 @param image: a string, file name of the BIOS image to program
677 on the DUT.
678
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800679 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700680 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800681 if not self.is_localhost():
682 image = self._scp_image(image)
683 self._programmer.program_bios(image)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800684
685
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800686 def program_ec(self, image):
687 """Program ec on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800688
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800689 @param image: a string, file name of the EC image to program
690 on the DUT.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800691
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800692 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700693 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800694 if not self.is_localhost():
695 image = self._scp_image(image)
Tom Wai-Hong Tam7b7eeac2015-07-23 01:53:52 +0800696 self._programmer.program_ec(image)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800697
Fang Dengafb88142013-05-30 17:44:31 -0700698
699 def _switch_usbkey_power(self, power_state, detection_delay=False):
700 """Switch usbkey power.
701
702 This function switches usbkey power by setting the value of
703 'prtctl4_pwren'. If the power is already in the
704 requested state, this function simply returns.
705
706 @param power_state: A string, 'on' or 'off'.
707 @param detection_delay: A boolean value, if True, sleep
708 for |USB_DETECTION_DELAY| after switching
709 the power on.
710 """
711 self.set('prtctl4_pwren', power_state)
712 if power_state == 'off':
713 time.sleep(self.USB_POWEROFF_DELAY)
714 elif detection_delay:
715 time.sleep(self.USB_DETECTION_DELAY)
716
717
718 def switch_usbkey(self, usb_state):
719 """Connect USB flash stick to either host or DUT, or turn USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800720
721 This function switches the servo multiplexer to provide electrical
Fang Dengafb88142013-05-30 17:44:31 -0700722 connection between the USB port J3 and either host or DUT side. It
723 can also be used to turn the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800724
Fang Dengafb88142013-05-30 17:44:31 -0700725 Switching to 'dut' or 'host' is accompanied by powercycling
726 of the USB stick, because it sometimes gets wedged if the mux
727 is switched while the stick power is on.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800728
Fang Dengafb88142013-05-30 17:44:31 -0700729 @param usb_state: A string, one of 'dut', 'host', or 'off'.
730 'dut' and 'host' indicate which side the
731 USB flash device is required to be connected to.
732 'off' indicates turning the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800733
Fang Dengafb88142013-05-30 17:44:31 -0700734 @raise: error.TestError in case the parameter is not 'dut'
735 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800736 """
Fang Dengafb88142013-05-30 17:44:31 -0700737 if self.get_usbkey_direction() == usb_state:
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800738 return
739
Fang Dengafb88142013-05-30 17:44:31 -0700740 if usb_state == 'off':
Dan Shia5fef052015-05-18 23:28:47 -0700741 self._switch_usbkey_power('off')
742 self._usb_state = usb_state
743 return
Fang Dengafb88142013-05-30 17:44:31 -0700744 elif usb_state == 'host':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800745 mux_direction = 'servo_sees_usbkey'
Fang Dengafb88142013-05-30 17:44:31 -0700746 elif usb_state == 'dut':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800747 mux_direction = 'dut_sees_usbkey'
748 else:
Fang Dengafb88142013-05-30 17:44:31 -0700749 raise error.TestError('Unknown USB state request: %s' % usb_state)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800750
Fang Dengafb88142013-05-30 17:44:31 -0700751 self._switch_usbkey_power('off')
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800752 self.set('usb_mux_sel1', mux_direction)
753 time.sleep(self.USB_POWEROFF_DELAY)
Fang Dengafb88142013-05-30 17:44:31 -0700754 self._switch_usbkey_power('on', usb_state == 'host')
755 self._usb_state = usb_state
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800756
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800757
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800758 def get_usbkey_direction(self):
Fang Dengafb88142013-05-30 17:44:31 -0700759 """Get which side USB is connected to or 'off' if usb power is off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800760
Fang Dengafb88142013-05-30 17:44:31 -0700761 @return: A string, one of 'dut', 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800762 """
Fang Dengafb88142013-05-30 17:44:31 -0700763 if not self._usb_state:
764 if self.get('prtctl4_pwren') == 'off':
765 self._usb_state = 'off'
766 elif self.get('usb_mux_sel1').startswith('dut'):
767 self._usb_state = 'dut'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800768 else:
Fang Dengafb88142013-05-30 17:44:31 -0700769 self._usb_state = 'host'
770 return self._usb_state