blob: 2e55df506241cf84df01add6eda68d97b3d44294 [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
Jon Salzc88e5b62011-11-30 14:38:54 +0800145 # Time between an usb disk plugged-in and detected in the system.
146 USB_DETECTION_DELAY = 10
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800147 # Time to keep USB power off before and after USB mux direction is changed
148 USB_POWEROFF_DELAY = 2
Jon Salzc88e5b62011-11-30 14:38:54 +0800149
Simran Basib7850bb2013-07-24 12:33:42 -0700150 # Time to wait before timing out on servo initialization.
151 INIT_TIMEOUT_SECS = 10
Simran Basi59331342013-07-12 12:06:29 -0700152
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700153
Ricky Liang0dd379c2014-04-23 16:29:08 +0800154 def __init__(self, servo_host, servo_serial=None):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700155 """Sets up the servo communication infrastructure.
156
Fang Deng5d518f42013-08-02 14:04:32 -0700157 @param servo_host: A ServoHost object representing
158 the host running servod.
Ricky Liang0dd379c2014-04-23 16:29:08 +0800159 @param servo_serial: Serial number of the servo board.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700160 """
Fang Deng5d518f42013-08-02 14:04:32 -0700161 # TODO(fdeng): crbug.com/298379
162 # We should move servo_host object out of servo object
163 # to minimize the dependencies on the rest of Autotest.
164 self._servo_host = servo_host
Ricky Liang0dd379c2014-04-23 16:29:08 +0800165 self._servo_serial = servo_serial
Fang Deng5d518f42013-08-02 14:04:32 -0700166 self._server = servo_host.get_servod_server_proxy()
J. Richard Barnette0c9c5882014-06-11 15:27:05 -0700167 self._power_state = _PowerStateController(self)
Fang Dengafb88142013-05-30 17:44:31 -0700168 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700169 self._programmer = None
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800170
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800171
Ricky Liang0dd379c2014-04-23 16:29:08 +0800172 @property
173 def servo_serial(self):
174 """Returns the serial number of the servo board."""
175 return self._servo_serial
176
177
Tom Wai-Hong Tam22ee0e52013-04-03 13:36:39 +0800178 def get_power_state_controller(self):
J. Richard Barnette75136b32013-03-26 13:38:44 -0700179 """Return the power state controller for this Servo.
180
181 The power state controller provides board-independent
182 interfaces for reset, power-on, power-off operations.
183
184 """
185 return self._power_state
186
Fang Deng5d518f42013-08-02 14:04:32 -0700187
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700188 def initialize_dut(self, cold_reset=False):
189 """Initializes a dut for testing purposes.
190
191 This sets various servo signals back to default values
192 appropriate for the target board. By default, if the DUT
193 is already on, it stays on. If the DUT is powered off
194 before initialization, its state afterward is unspecified.
195
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700196 Rationale: Basic initialization of servo sets the lid open,
197 when there is a lid. This operation won't affect powered on
198 units; however, setting the lid open may power on a unit
J. Richard Barnette75136b32013-03-26 13:38:44 -0700199 that's off, depending on the board type and previous state
200 of the device.
201
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700202 If `cold_reset` is a true value, the DUT and its EC will be
203 reset, and the DUT rebooted in normal mode.
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700204
205 @param cold_reset If True, cold reset the device after
206 initialization.
207 """
208 self._server.hwinit()
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700209 self.set('dut_hub_pwren', 'on')
210 self.set('usb_mux_oe1', 'on')
211 self.switch_usbkey('off')
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700212 if cold_reset:
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700213 self._power_state.reset()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700214
215
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800216 def is_localhost(self):
217 """Is the servod hosted locally?
218
219 Returns:
220 True if local hosted; otherwise, False.
221 """
Fang Deng5d518f42013-08-02 14:04:32 -0700222 return self._servo_host.is_localhost()
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800223
224
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700225 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700226 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700227 # After a long power press, the EC may ignore the next power
228 # button press (at least on Alex). To guarantee that this
229 # won't happen, we need to allow the EC one second to
230 # collect itself.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800231 self._server.power_long_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700232
233
234 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700235 """Simulate a normal power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800236 self._server.power_normal_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700237
238
239 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700240 """Simulate a short power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800241 self._server.power_short_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700242
243
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800244 def power_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700245 """Simulate a power button press.
246
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800247 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700248 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800249 self._server.power_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700250
251
252 def lid_open(self):
253 """Simulate opening the lid."""
Craig Harrison48997262011-06-27 14:31:10 -0700254 self.set_nocheck('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700255
256
257 def lid_close(self):
Craig Harrison48997262011-06-27 14:31:10 -0700258 """Simulate closing the lid.
259
260 Waits 6 seconds to ensure the device is fully asleep before returning.
261 """
262 self.set_nocheck('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700263 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700264
265
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800266 def ctrl_d(self, press_secs=''):
267 """Simulate Ctrl-d simultaneous button presses.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800268
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800269 @param press_secs : Str. Time to press key.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800270 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800271 self._server.ctrl_d(press_secs)
Gediminas Ramanauskas9da80f22012-11-14 12:59:43 -0800272
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800273
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800274 def ctrl_u(self):
275 """Simulate Ctrl-u simultaneous button presses.
276
277 @param press_secs : Str. Time to press key.
278 """
279 self._server.ctrl_u()
Todd Broch9dfc3a82011-11-01 08:09:28 -0700280
281
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800282 def ctrl_enter(self, press_secs=''):
283 """Simulate Ctrl-enter simultaneous button presses.
284
285 @param press_secs : Str. Time to press key.
286 """
287 self._server.ctrl_enter(press_secs)
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700288
289
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800290 def d_key(self, press_secs=''):
291 """Simulate Enter key button press.
292
293 @param press_secs : Str. Time to press key.
294 """
295 self._server.d_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700296
297
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800298 def ctrl_key(self, press_secs=''):
299 """Simulate Enter key button press.
300
301 @param press_secs : Str. Time to press key.
302 """
303 self._server.ctrl_key(press_secs)
Todd Broch9753bd42012-03-21 10:15:08 -0700304
305
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800306 def enter_key(self, press_secs=''):
307 """Simulate Enter key button press.
308
309 @param press_secs : Str. Time to press key.
310 """
311 self._server.enter_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700312
313
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800314 def refresh_key(self, press_secs=''):
315 """Simulate Refresh key (F3) button press.
316
317 @param press_secs : Str. Time to press key.
318 """
319 self._server.refresh_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700320
321
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800322 def ctrl_refresh_key(self, press_secs=''):
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800323 """Simulate Ctrl and Refresh (F3) simultaneous press.
324
325 This key combination is an alternative of Space key.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800326
327 @param press_secs : Str. Time to press key.
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800328 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800329 self._server.ctrl_refresh_key(press_secs)
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800330
331
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800332 def imaginary_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700333 """Simulate imaginary key button press.
334
335 Maps to a key that doesn't physically exist.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800336
337 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700338 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800339 self._server.imaginary_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700340
341
Craig Harrison6b36b122011-06-28 17:58:43 -0700342 def enable_recovery_mode(self):
343 """Enable recovery mode on device."""
344 self.set('rec_mode', 'on')
345
346
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800347 def custom_recovery_mode(self):
348 """Custom key combination to enter recovery mode."""
349 self._press_keys('rec_mode')
350 self.power_normal_press()
351 time.sleep(self.SERVO_KEY_PRESS_DELAY)
352 self.set_nocheck('kbd_en', 'off')
353
354
Craig Harrison6b36b122011-06-28 17:58:43 -0700355 def disable_recovery_mode(self):
356 """Disable recovery mode on device."""
357 self.set('rec_mode', 'off')
358
359
360 def enable_development_mode(self):
361 """Enable development mode on device."""
362 self.set('dev_mode', 'on')
363
364
365 def disable_development_mode(self):
366 """Disable development mode on device."""
367 self.set('dev_mode', 'off')
368
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700369 def boot_devmode(self):
370 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800371 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700372 self.pass_devmode()
373
374
375 def pass_devmode(self):
376 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700377 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700378 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700379 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700380
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700381
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800382 def get_version(self):
383 """Get the version of servod board.
384
385 """
386 return self._server.get_version()
387
388
389 def get_board(self):
390 """Get the board connected to servod.
391
392 """
393 return self._server.get_board()
394
395
Todd Brochefe72cb2012-07-11 19:58:53 -0700396 def _get_xmlrpclib_exception(self, xmlexc):
397 """Get meaningful exception string from xmlrpc.
398
399 Args:
400 xmlexc: xmlrpclib.Fault object
401
402 xmlrpclib.Fault.faultString has the following format:
403
404 <type 'exception type'>:'actual error message'
405
406 Parse and return the real exception from the servod side instead of the
407 less meaningful one like,
408 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
409 attribute 'hw_driver'">
410
411 Returns:
412 string of underlying exception raised in servod.
413 """
414 return re.sub('^.*>:', '', xmlexc.faultString)
415
416
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700417 def get(self, gpio_name):
418 """Get the value of a gpio from Servod."""
419 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700420 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700421 return self._server.get(gpio_name)
Todd Brochefe72cb2012-07-11 19:58:53 -0700422 except xmlrpclib.Fault as e:
423 err_msg = "Getting '%s' :: %s" % \
424 (gpio_name, self._get_xmlrpclib_exception(e))
425 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700426
427
428 def set(self, gpio_name, gpio_value):
429 """Set and check the value of a gpio using Servod."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700430 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800431 retry_count = Servo.GET_RETRY_MAX
432 while gpio_value != self.get(gpio_name) and retry_count:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700433 logging.warning("%s != %s, retry %d", gpio_name, gpio_value,
Todd Brochcf7c6652012-02-24 13:03:59 -0800434 retry_count)
435 retry_count -= 1
436 time.sleep(Servo.SHORT_DELAY)
437 if not retry_count:
438 assert gpio_value == self.get(gpio_name), \
439 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700440
441
442 def set_nocheck(self, gpio_name, gpio_value):
443 """Set the value of a gpio using Servod."""
444 assert gpio_name and gpio_value
Todd Broch9753bd42012-03-21 10:15:08 -0700445 logging.info('Setting %s to %s', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700446 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700447 self._server.set(gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700448 except xmlrpclib.Fault as e:
449 err_msg = "Setting '%s' to '%s' :: %s" % \
450 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
451 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700452
453
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800454 def set_get_all(self, controls):
455 """Set &| get one or more control values.
456
457 @param controls: list of strings, controls to set &| get.
458
459 @raise: error.TestError in case error occurs setting/getting values.
460 """
461 rv = []
462 try:
Vic Yangcad9acb2013-05-21 14:02:05 +0800463 logging.info('Set/get all: %s', str(controls))
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800464 rv = self._server.set_get_all(controls)
465 except xmlrpclib.Fault as e:
466 # TODO(waihong): Remove the following backward compatibility when
467 # the new versions of hdctools are deployed.
468 if 'not supported' in str(e):
469 logging.warning('The servod is too old that set_get_all '
470 'not supported. Use set and get instead.')
471 for control in controls:
472 if ':' in control:
473 (name, value) = control.split(':')
474 if name == 'sleep':
475 time.sleep(float(value))
476 else:
477 self.set_nocheck(name, value)
478 rv.append(True)
479 else:
480 rv.append(self.get(name))
481 else:
482 err_msg = "Problem with '%s' :: %s" % \
483 (controls, self._get_xmlrpclib_exception(e))
484 raise error.TestFail(err_msg)
485 return rv
486
487
Jon Salzc88e5b62011-11-30 14:38:54 +0800488 # TODO(waihong) It may fail if multiple servo's are connected to the same
489 # host. Should look for a better way, like the USB serial name, to identify
490 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700491 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
492 # updated.
Jon Salzc88e5b62011-11-30 14:38:54 +0800493 def probe_host_usb_dev(self):
494 """Probe the USB disk device plugged-in the servo from the host side.
495
496 It tries to switch the USB mux to make the host unable to see the
497 USB disk and compares the result difference.
498
Jon Salzc88e5b62011-11-30 14:38:54 +0800499 Returns:
500 A string of USB disk path, like '/dev/sdb', or None if not existed.
501 """
502 cmd = 'ls /dev/sd[a-z]'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800503 original_value = self.get_usbkey_direction()
Jon Salzc88e5b62011-11-30 14:38:54 +0800504
505 # Make the host unable to see the USB disk.
Fang Dengafb88142013-05-30 17:44:31 -0700506 self.switch_usbkey('off')
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800507 no_usb_set = set(self.system_output(cmd, ignore_status=True).split())
Jon Salzc88e5b62011-11-30 14:38:54 +0800508
509 # Make the host able to see the USB disk.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800510 self.switch_usbkey('host')
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800511 has_usb_set = set(self.system_output(cmd, ignore_status=True).split())
Jon Salzc88e5b62011-11-30 14:38:54 +0800512
513 # Back to its original value.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800514 if original_value != self.get_usbkey_direction():
515 self.switch_usbkey(original_value)
Jon Salzc88e5b62011-11-30 14:38:54 +0800516
517 diff_set = has_usb_set - no_usb_set
518 if len(diff_set) == 1:
519 return diff_set.pop()
520 else:
521 return None
522
523
Mike Truty49153d82012-08-21 22:27:30 -0500524 def image_to_servo_usb(self, image_path=None,
525 make_image_noninteractive=False):
526 """Install an image to the USB key plugged into the servo.
527
528 This method may copy any image to the servo USB key including a
529 recovery image or a test image. These images are frequently used
530 for test purposes such as restoring a corrupted image or conducting
531 an upgrade of ec/fw/kernel as part of a test of a specific image part.
532
533 Args:
534 image_path: Path on the host to the recovery image.
535 make_image_noninteractive: Make the recovery image noninteractive,
536 therefore the DUT will reboot
537 automatically after installation.
538 """
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700539 # We're about to start plugging/unplugging the USB key. We
540 # don't know the state of the DUT, or what it might choose
541 # to do to the device after hotplug. To avoid surprises,
542 # force the DUT to be off.
543 self._server.hwinit()
544 self._power_state.power_off()
Mike Truty49153d82012-08-21 22:27:30 -0500545
546 # Set up Servo's usb mux.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800547 self.switch_usbkey('host')
Mike Truty49153d82012-08-21 22:27:30 -0500548 if image_path:
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800549 logging.info('Searching for usb device and copying image to it. '
550 'Please wait a few minutes...')
Mike Truty49153d82012-08-21 22:27:30 -0500551 if not self._server.download_image_to_usb(image_path):
552 logging.error('Failed to transfer requested image to USB. '
553 'Please take a look at Servo Logs.')
554 raise error.AutotestError('Download image to usb failed.')
555 if make_image_noninteractive:
556 logging.info('Making image noninteractive')
557 if not self._server.make_image_noninteractive():
558 logging.error('Failed to make image noninteractive. '
559 'Please take a look at Servo Logs.')
560
561
Simran Basi741b5d42012-05-18 11:27:15 -0700562 def install_recovery_image(self, image_path=None,
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800563 make_image_noninteractive=False):
Jon Salzc88e5b62011-11-30 14:38:54 +0800564 """Install the recovery image specied by the path onto the DUT.
565
566 This method uses google recovery mode to install a recovery image
567 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 +0800568 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800569 we use the recovery image already on the usb image.
570
571 Args:
572 image_path: Path on the host to the recovery image.
Simran Basi741b5d42012-05-18 11:27:15 -0700573 make_image_noninteractive: Make the recovery image noninteractive,
574 therefore the DUT will reboot
575 automatically after installation.
Jon Salzc88e5b62011-11-30 14:38:54 +0800576 """
Mike Truty49153d82012-08-21 22:27:30 -0500577 self.image_to_servo_usb(image_path, make_image_noninteractive)
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700578 self._power_state.power_on(rec_mode=self._power_state.REC_ON)
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700579 self.switch_usbkey('dut')
Jon Salzc88e5b62011-11-30 14:38:54 +0800580
581
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800582 def _scp_image(self, image_path):
583 """Copy image to the servo host.
584
585 When programming a firmware image on the DUT, the image must be
586 located on the host to which the servo device is connected. Sometimes
587 servo is controlled by a remote host, in this case the image needs to
588 be transferred to the remote host.
589
590 @param image_path: a string, name of the firmware image file to be
591 transferred.
592 @return: a string, full path name of the copied file on the remote.
593 """
594
595 dest_path = os.path.join('/tmp', os.path.basename(image_path))
Fang Deng5d518f42013-08-02 14:04:32 -0700596 self._servo_host.send_file(image_path, dest_path)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800597 return dest_path
598
599
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800600 def system(self, command, timeout=None):
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800601 """Execute the passed in command on the servod host."""
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800602 logging.info('Will execute on servo host: %s', command)
Fang Deng5d518f42013-08-02 14:04:32 -0700603 self._servo_host.run(command, timeout=timeout)
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800604
605
606 def system_output(self, command, timeout=None,
607 ignore_status=False, args=()):
608 """Execute the passed in command on the servod host, return stdout.
609
610 @param command, a string, the command to execute
611 @param timeout, an int, max number of seconds to wait til command
612 execution completes
613 @ignore_status, a Boolean, if true - ignore command's nonzero exit
614 status, otherwise an exception will be thrown
615 @param args, a tuple of strings, each becoming a separate command line
616 parameter for the command
617 @return: command's stdout as a string.
618 """
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800619 logging.info('Will execute and collect output on servo host: %s %s',
620 command, ' '.join("'%s'" % x for x in args))
Fang Deng5d518f42013-08-02 14:04:32 -0700621 return self._servo_host.run(command, timeout=timeout,
622 ignore_status=ignore_status,
623 args=args).stdout.strip()
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800624
625
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700626 def _initialize_programmer(self):
627 if self._programmer:
628 return
629 # Initialize firmware programmer
630 servo_version = self.get_version()
631 if servo_version == 'servo_v2':
632 self._programmer = firmware_programmer.ProgrammerV2(self)
633 else:
634 raise error.TestError(
635 'No firmware programmer for servo version: %s' %
636 servo_version)
637
638
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800639 def program_bios(self, image):
640 """Program bios on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800641
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800642 @param image: a string, file name of the BIOS image to program
643 on the DUT.
644
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800645 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700646 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800647 if not self.is_localhost():
648 image = self._scp_image(image)
649 self._programmer.program_bios(image)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800650
651
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800652 def program_ec(self, image):
653 """Program ec on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800654
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800655 @param image: a string, file name of the EC image to program
656 on the DUT.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800657
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800658 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700659 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800660 if not self.is_localhost():
661 image = self._scp_image(image)
662 self._programmer.program_ec(image)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800663
Fang Dengafb88142013-05-30 17:44:31 -0700664
665 def _switch_usbkey_power(self, power_state, detection_delay=False):
666 """Switch usbkey power.
667
668 This function switches usbkey power by setting the value of
669 'prtctl4_pwren'. If the power is already in the
670 requested state, this function simply returns.
671
672 @param power_state: A string, 'on' or 'off'.
673 @param detection_delay: A boolean value, if True, sleep
674 for |USB_DETECTION_DELAY| after switching
675 the power on.
676 """
677 self.set('prtctl4_pwren', power_state)
678 if power_state == 'off':
679 time.sleep(self.USB_POWEROFF_DELAY)
680 elif detection_delay:
681 time.sleep(self.USB_DETECTION_DELAY)
682
683
684 def switch_usbkey(self, usb_state):
685 """Connect USB flash stick to either host or DUT, or turn USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800686
687 This function switches the servo multiplexer to provide electrical
Fang Dengafb88142013-05-30 17:44:31 -0700688 connection between the USB port J3 and either host or DUT side. It
689 can also be used to turn the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800690
Fang Dengafb88142013-05-30 17:44:31 -0700691 Switching to 'dut' or 'host' is accompanied by powercycling
692 of the USB stick, because it sometimes gets wedged if the mux
693 is switched while the stick power is on.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800694
Fang Dengafb88142013-05-30 17:44:31 -0700695 @param usb_state: A string, one of 'dut', 'host', or 'off'.
696 'dut' and 'host' indicate which side the
697 USB flash device is required to be connected to.
698 'off' indicates turning the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800699
Fang Dengafb88142013-05-30 17:44:31 -0700700 @raise: error.TestError in case the parameter is not 'dut'
701 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800702 """
Fang Dengafb88142013-05-30 17:44:31 -0700703 if self.get_usbkey_direction() == usb_state:
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800704 return
705
Fang Dengafb88142013-05-30 17:44:31 -0700706 if usb_state == 'off':
707 self._switch_usbkey_power('off')
708 self._usb_state = usb_state
709 return
710 elif usb_state == 'host':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800711 mux_direction = 'servo_sees_usbkey'
Fang Dengafb88142013-05-30 17:44:31 -0700712 elif usb_state == 'dut':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800713 mux_direction = 'dut_sees_usbkey'
714 else:
Fang Dengafb88142013-05-30 17:44:31 -0700715 raise error.TestError('Unknown USB state request: %s' % usb_state)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800716
Fang Dengafb88142013-05-30 17:44:31 -0700717 self._switch_usbkey_power('off')
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800718 self.set('usb_mux_sel1', mux_direction)
719 time.sleep(self.USB_POWEROFF_DELAY)
Fang Dengafb88142013-05-30 17:44:31 -0700720 self._switch_usbkey_power('on', usb_state == 'host')
721 self._usb_state = usb_state
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800722
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800723
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800724 def get_usbkey_direction(self):
Fang Dengafb88142013-05-30 17:44:31 -0700725 """Get which side USB is connected to or 'off' if usb power is off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800726
Fang Dengafb88142013-05-30 17:44:31 -0700727 @return: A string, one of 'dut', 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800728 """
Fang Dengafb88142013-05-30 17:44:31 -0700729 if not self._usb_state:
730 if self.get('prtctl4_pwren') == 'off':
731 self._usb_state = 'off'
732 elif self.get('usb_mux_sel1').startswith('dut'):
733 self._usb_state = 'dut'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800734 else:
Fang Dengafb88142013-05-30 17:44:31 -0700735 self._usb_state = 'host'
736 return self._usb_state