blob: 5bbd882f4446f51d314095af62a55e63023e3ff4 [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
Jon Salzc88e5b62011-11-30 14:38:54 +080013from autotest_lib.server import utils
Vadim Bendeburyb80ba592012-12-07 15:02:34 -080014from autotest_lib.server.cros import programmer
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070015
J. Richard Barnette384056b2012-04-16 11:04:46 -070016class Servo(object):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070017 """Manages control of a Servo board.
18
19 Servo is a board developed by hardware group to aide in the debug and
20 control of various partner devices. Servo's features include the simulation
21 of pressing the power button, closing the lid, and pressing Ctrl-d. This
22 class manages setting up and communicating with a servo demon (servod)
23 process. It provides both high-level functions for common servo tasks and
24 low-level functions for directly setting and reading gpios.
25 """
26
Chrome Bot9a1137d2011-07-19 14:35:00 -070027 # Power button press delays in seconds.
J. Richard Barnetted2e4cbd2012-06-29 12:18:40 -070028 #
J. Richard Barnette5383f072012-07-26 17:35:40 -070029 # The EC specification says that 8.0 seconds should be enough
30 # for the long power press. However, some platforms need a bit
31 # more time. Empirical testing has found these requirements:
32 # Alex: 8.2 seconds
33 # ZGB: 8.5 seconds
34 # The actual value is set to the largest known necessary value.
35 #
36 # TODO(jrbarnette) Being generous is the right thing to do for
37 # existing platforms, but if this code is to be used for
38 # qualification of new hardware, we should be less generous.
39 LONG_DELAY = 8.5
Chrome Bot9a1137d2011-07-19 14:35:00 -070040 SHORT_DELAY = 0.1
41 NORMAL_TRANSITION_DELAY = 1.2
J. Richard Barnette5383f072012-07-26 17:35:40 -070042
Todd Broch31c82502011-08-29 08:14:39 -070043 # Maximum number of times to re-read power button on release.
44 RELEASE_RETRY_MAX = 5
Todd Brochcf7c6652012-02-24 13:03:59 -080045 GET_RETRY_MAX = 10
Chrome Bot9a1137d2011-07-19 14:35:00 -070046
J. Richard Barnette5383f072012-07-26 17:35:40 -070047 # Delays to deal with DUT state transitions.
Chrome Bot9a1137d2011-07-19 14:35:00 -070048 SLEEP_DELAY = 6
49 BOOT_DELAY = 10
J. Richard Barnette8bd49842012-07-19 14:21:15 -070050 RECOVERY_BOOT_DELAY = 10
J. Richard Barnettec5a77ad2012-04-25 08:19:00 -070051 RECOVERY_INSTALL_DELAY = 540
Chrome Bot9a1137d2011-07-19 14:35:00 -070052
J. Richard Barnetteb6133972012-07-19 17:13:55 -070053 # Time required for the EC to be working after cold reset.
54 # Five seconds is at least twice as big as necessary for Alex,
55 # and is presumably good enough for all future systems.
56 _EC_RESET_DELAY = 5.0
57
Chrome Bot9a1137d2011-07-19 14:35:00 -070058 # Servo-specific delays.
59 MAX_SERVO_STARTUP_DELAY = 10
60 SERVO_SEND_SIGNAL_DELAY = 0.5
Vic Yang0aca1c22012-11-19 18:33:56 -080061 SERVO_KEY_PRESS_DELAY = 0.1
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070062
Jon Salzc88e5b62011-11-30 14:38:54 +080063 # Time between an usb disk plugged-in and detected in the system.
64 USB_DETECTION_DELAY = 10
65
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -070066 KEY_MATRIX_ALT_0 = {
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -070067 'ctrl_refresh': ['0', '0', '0', '1'],
68 'ctrl_d': ['0', '1', '0', '0'],
69 'd': ['0', '1', '1', '1'],
70 'ctrl_enter': ['1', '0', '0', '0'],
71 'enter': ['1', '0', '1', '1'],
72 'ctrl': ['1', '1', '0', '0'],
73 'refresh': ['1', '1', '0', '1'],
74 'unused': ['1', '1', '1', '0'],
75 'none': ['1', '1', '1', '1']}
Chris Masone6a0680f2012-03-02 08:40:00 -080076
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -070077 KEY_MATRIX_ALT_1 = {
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -070078 'ctrl_d': ['0', '0', '1', '0'],
79 'd': ['0', '0', '1', '1'],
80 'ctrl_enter': ['0', '1', '1', '0'],
81 'enter': ['0', '1', '1', '1'],
82 'ctrl_refresh': ['1', '0', '0', '1'],
83 'unused': ['1', '1', '0', '0'],
84 'refresh': ['1', '1', '0', '1'],
85 'ctrl': ['1', '1', '1', '0'],
86 'none': ['1', '1', '1', '1']}
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -070087
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -080088 KEY_MATRIX_ALT_2 = {
89 'ctrl_d': ['0', '0', '0', '1'],
90 'd': ['0', '0', '1', '1'],
91 'unused': ['0', '1', '1', '1'],
92 'rec_mode': ['1', '0', '0', '0'],
93 'ctrl_enter': ['1', '0', '0', '1'],
94 'enter': ['1', '0', '1', '1'],
95 'ctrl': ['1', '1', '0', '1'],
96 'refresh': ['1', '1', '1', '0'],
97 'ctrl_refresh': ['1', '1', '1', '1'],
98 'none': ['1', '1', '1', '1']}
99
100 KEY_MATRIX = [KEY_MATRIX_ALT_0, KEY_MATRIX_ALT_1, KEY_MATRIX_ALT_2]
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700101
Chris Masone6a0680f2012-03-02 08:40:00 -0800102 @staticmethod
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700103 def _make_servo_hostname(hostname):
104 host_parts = hostname.split('.')
105 host_parts[0] = host_parts[0] + '-servo'
106 return '.'.join(host_parts)
Chris Masone6a0680f2012-03-02 08:40:00 -0800107
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700108 @staticmethod
109 def get_lab_servo(target_hostname):
110 """Instantiate a Servo for |target_hostname| in the lab.
Chris Masone6a0680f2012-03-02 08:40:00 -0800111
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700112 Assuming that |target_hostname| is a device in the CrOS test
113 lab, create and return a Servo object pointed at the servo
114 attached to that DUT. The servo in the test lab is assumed
115 to already have servod up and running on it.
116
117 @param target_hostname: device whose servo we want to target.
Chris Masone6a0680f2012-03-02 08:40:00 -0800118 @return an appropriately configured Servo
119 """
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700120 servo_host = Servo._make_servo_hostname(target_hostname)
121 if utils.host_is_in_lab_zone(servo_host):
Vic Yang3a7cf602012-11-07 17:28:39 +0800122 try:
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800123 return Servo(servo_host=servo_host, target_host=target_hostname)
Vic Yang3a7cf602012-11-07 17:28:39 +0800124 except: # pylint: disable=W0702
125 # TODO(jrbarnette): Long-term, if we can't get to
126 # a servo in the lab, we want to fail, so we should
127 # pass any exceptions along. Short-term, we're not
128 # ready to rely on servo, so we ignore failures.
129 pass
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700130 return None
Chris Masone6a0680f2012-03-02 08:40:00 -0800131
132
Tom Wai-Hong Tamc5c14ef2012-11-20 16:33:37 +0800133 def __init__(self, servo_host='localhost', target_host=None,
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800134 servo_port=9999):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700135 """Sets up the servo communication infrastructure.
136
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800137 @param servo_host Name of the host where the servod process
138 is running.
139 @param target_host Name of the target which is connected to servo
140 @param servo_port Port the servod process is listening on.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700141 """
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700142 self._key_matrix = 0
J. Richard Barnette384056b2012-04-16 11:04:46 -0700143 self._server = None
Todd Brochf24d2782011-08-19 10:55:41 -0700144 self._connect_servod(servo_host, servo_port)
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800145 self._is_localhost = (servo_host == 'localhost')
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800146 self._target_host = target_host
147
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800148 # Commands on the servo host must be run by the superuser. Our account
149 # on Beaglebone is root, but locally we might be running as a
150 # different user. If so - `sudo ' will have to be added to the
151 # commands.
152 if self._is_localhost:
153 self._sudo_required = utils.system_output('id -u') != '0'
154 self._ssh_prefix = ''
155 else:
156 common_options = '-o PasswordAuthentication=no'
157 self._sudo_required = False
158 self._ssh_prefix = 'ssh %s root@%s ' % (common_options, servo_host)
159 self._scp_cmd_template = 'scp %s ' % common_options
160 self._scp_cmd_template += '%s ' + 'root@' + servo_host + ':%s'
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800161
162 def get_target_hostname(self):
163 """Retrieves target (DUT) hostname."""
164 return self._target_host
Chrome Bot9a1137d2011-07-19 14:35:00 -0700165
166
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700167 def initialize_dut(self, cold_reset=False):
168 """Initializes a dut for testing purposes.
169
170 This sets various servo signals back to default values
171 appropriate for the target board. By default, if the DUT
172 is already on, it stays on. If the DUT is powered off
173 before initialization, its state afterward is unspecified.
174
175 If cold reset is requested, the DUT is guaranteed to be off
176 at the end of initialization, regardless of its initial
177 state.
178
179 Rationale: Basic initialization of servo sets the lid open,
180 when there is a lid. This operation won't affect powered on
181 units; however, setting the lid open may power on a unit
182 that's off, depending on factors outside the scope of this
183 function.
184
185 @param cold_reset If True, cold reset the device after
186 initialization.
187 """
188 self._server.hwinit()
189 if cold_reset:
190 self.cold_reset()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700191
192
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800193 def is_localhost(self):
194 """Is the servod hosted locally?
195
196 Returns:
197 True if local hosted; otherwise, False.
198 """
199 return self._is_localhost
200
201
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700202 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700203 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700204 # After a long power press, the EC may ignore the next power
205 # button press (at least on Alex). To guarantee that this
206 # won't happen, we need to allow the EC one second to
207 # collect itself.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700208 self.power_key(Servo.LONG_DELAY)
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700209 time.sleep(1.0)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700210
211
212 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700213 """Simulate a normal power button press."""
214 self.power_key()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700215
216
217 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700218 """Simulate a short power button press."""
219 self.power_key(Servo.SHORT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700220
221
Chrome Bot9a1137d2011-07-19 14:35:00 -0700222 def power_key(self, secs=NORMAL_TRANSITION_DELAY):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700223 """Simulate a power button press.
224
225 Args:
226 secs: Time in seconds to simulate the keypress.
227 """
Craig Harrison6b36b122011-06-28 17:58:43 -0700228 self.set_nocheck('pwr_button', 'press')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700229 time.sleep(secs)
Todd Broch31c82502011-08-29 08:14:39 -0700230 self.set_nocheck('pwr_button', 'release')
231 # TODO(tbroch) Different systems have different release times on the
232 # power button that this loop addresses. Longer term we may want to
233 # make this delay platform specific.
234 retry = 1
235 while True:
236 value = self.get('pwr_button')
237 if value == 'release' or retry > Servo.RELEASE_RETRY_MAX:
238 break
Todd Broch9753bd42012-03-21 10:15:08 -0700239 logging.info('Waiting for pwr_button to release, retry %d.', retry)
Todd Broch31c82502011-08-29 08:14:39 -0700240 retry += 1
241 time.sleep(Servo.SHORT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700242
243
244 def lid_open(self):
245 """Simulate opening the lid."""
Craig Harrison48997262011-06-27 14:31:10 -0700246 self.set_nocheck('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700247
248
249 def lid_close(self):
Craig Harrison48997262011-06-27 14:31:10 -0700250 """Simulate closing the lid.
251
252 Waits 6 seconds to ensure the device is fully asleep before returning.
253 """
254 self.set_nocheck('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700255 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700256
257
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800258 def _press_keys(self, key):
259 """Simulate button presses.
260
261 Note, key presses will remain on indefinitely. See
262 _press_and_release_keys for release procedure.
263 """
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700264 (m1_a1, m1_a0, m2_a1, m2_a0) = self.KEY_MATRIX[self._key_matrix][key]
Todd Broch9753bd42012-03-21 10:15:08 -0700265 self.set_nocheck('kbd_m2_a0', m2_a0)
266 self.set_nocheck('kbd_m2_a1', m2_a1)
267 self.set_nocheck('kbd_m1_a0', m1_a0)
268 self.set_nocheck('kbd_m1_a1', m1_a1)
Vic Yange262a3e2012-11-02 18:48:37 +0800269 self.set_nocheck('kbd_en', 'on')
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800270
271
272 def _press_and_release_keys(self, key,
273 press_secs=SERVO_KEY_PRESS_DELAY):
274 """Simulate button presses and release."""
275 self._press_keys(key)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700276 time.sleep(press_secs)
277 self.set_nocheck('kbd_en', 'off')
278
279
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700280 def set_key_matrix(self, matrix=0):
281 """Set keyboard mapping"""
282 self._key_matrix = matrix
283
284
Chrome Bot9a1137d2011-07-19 14:35:00 -0700285 def ctrl_d(self):
286 """Simulate Ctrl-d simultaneous button presses."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700287 self._press_and_release_keys('ctrl_d')
Todd Broch9dfc3a82011-11-01 08:09:28 -0700288
289
Todd Broch9753bd42012-03-21 10:15:08 -0700290 def ctrl_enter(self):
291 """Simulate Ctrl-enter simultaneous button presses."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700292 self._press_and_release_keys('ctrl_enter')
Todd Broch9753bd42012-03-21 10:15:08 -0700293
294
Todd Broch9dfc3a82011-11-01 08:09:28 -0700295 def d_key(self):
296 """Simulate Enter key button press."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700297 self._press_and_release_keys('d')
Todd Broch9dfc3a82011-11-01 08:09:28 -0700298
299
300 def ctrl_key(self):
301 """Simulate Enter key button press."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700302 self._press_and_release_keys('ctrl')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700303
304
Chrome Bot9a1137d2011-07-19 14:35:00 -0700305 def enter_key(self):
306 """Simulate Enter key button press."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700307 self._press_and_release_keys('enter')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700308
309
Chrome Bot9a1137d2011-07-19 14:35:00 -0700310 def refresh_key(self):
311 """Simulate Refresh key (F3) button press."""
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700312 self._press_and_release_keys('refresh')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700313
314
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800315 def ctrl_refresh_key(self):
316 """Simulate Ctrl and Refresh (F3) simultaneous press.
317
318 This key combination is an alternative of Space key.
319 """
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700320 self._press_and_release_keys('ctrl_refresh')
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800321
322
Chrome Bot9a1137d2011-07-19 14:35:00 -0700323 def imaginary_key(self):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700324 """Simulate imaginary key button press.
325
326 Maps to a key that doesn't physically exist.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700327 """
Gediminas Ramanauskas515e7012012-09-18 17:01:10 -0700328 self._press_and_release_keys('unused')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700329
330
Craig Harrison6b36b122011-06-28 17:58:43 -0700331 def enable_recovery_mode(self):
332 """Enable recovery mode on device."""
333 self.set('rec_mode', 'on')
334
335
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800336 def custom_recovery_mode(self):
337 """Custom key combination to enter recovery mode."""
338 self._press_keys('rec_mode')
339 self.power_normal_press()
340 time.sleep(self.SERVO_KEY_PRESS_DELAY)
341 self.set_nocheck('kbd_en', 'off')
342
343
Craig Harrison6b36b122011-06-28 17:58:43 -0700344 def disable_recovery_mode(self):
345 """Disable recovery mode on device."""
346 self.set('rec_mode', 'off')
347
348
349 def enable_development_mode(self):
350 """Enable development mode on device."""
351 self.set('dev_mode', 'on')
352
353
354 def disable_development_mode(self):
355 """Disable development mode on device."""
356 self.set('dev_mode', 'off')
357
Chris Sosa8ee1d592011-08-14 16:50:31 -0700358 def enable_usb_hub(self, host=False):
Craig Harrison86b1a572011-08-12 11:26:52 -0700359 """Enable Servo's USB/ethernet hub.
360
Chris Sosa8ee1d592011-08-14 16:50:31 -0700361 This is equivalent to plugging in the USB devices attached to Servo to
362 the host (if |host| is True) or dut (if |host| is False).
363 For host=False, requires that the USB out on the servo board is
364 connected to a USB in port on the target device. Servo's USB ports are
365 labeled DUT_HUB_USB1 and DUT_HUB_USB2. Servo's ethernet port is also
366 connected to this hub. Servo's USB port DUT_HUB_IN is the output of the
367 hub.
Craig Harrison86b1a572011-08-12 11:26:52 -0700368 """
369 self.set('dut_hub_pwren', 'on')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700370 if host:
Todd Broch9753bd42012-03-21 10:15:08 -0700371 self.set('usb_mux_oe1', 'on')
372 self.set('usb_mux_sel1', 'servo_sees_usbkey')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700373 else:
Todd Broch9753bd42012-03-21 10:15:08 -0700374 self.set('dut_hub_sel', 'dut_sees_hub')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700375
Craig Harrison86b1a572011-08-12 11:26:52 -0700376 self.set('dut_hub_on', 'yes')
377
378
379 def disable_usb_hub(self):
380 """Disable Servo's USB/ethernet hub.
381
382 This is equivalent to unplugging the USB devices attached to Servo.
383 """
384 self.set('dut_hub_on', 'no')
385
386
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700387 def boot_devmode(self):
388 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800389 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700390 self.pass_devmode()
391
392
393 def pass_devmode(self):
394 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700395 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700396 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700397 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700398
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700399
Craig Harrison6b36b122011-06-28 17:58:43 -0700400 def cold_reset(self):
401 """Perform a cold reset of the EC.
402
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700403 This has the side effect of shutting off the device. The
404 device is guaranteed to be off at the end of this call.
Craig Harrison6b36b122011-06-28 17:58:43 -0700405 """
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700406 # After the reset, give the EC the time it needs to
407 # re-initialize.
Craig Harrison6b36b122011-06-28 17:58:43 -0700408 self.set('cold_reset', 'on')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700409 time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY)
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700410 self.set('cold_reset', 'off')
411 time.sleep(self._EC_RESET_DELAY)
Craig Harrison6b36b122011-06-28 17:58:43 -0700412
413
414 def warm_reset(self):
415 """Perform a warm reset of the device.
416
417 Has the side effect of restarting the device.
418 """
419 self.set('warm_reset', 'on')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700420 time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY)
Craig Harrison6b36b122011-06-28 17:58:43 -0700421 self.set('warm_reset', 'off')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700422
423
Todd Brochefe72cb2012-07-11 19:58:53 -0700424 def _get_xmlrpclib_exception(self, xmlexc):
425 """Get meaningful exception string from xmlrpc.
426
427 Args:
428 xmlexc: xmlrpclib.Fault object
429
430 xmlrpclib.Fault.faultString has the following format:
431
432 <type 'exception type'>:'actual error message'
433
434 Parse and return the real exception from the servod side instead of the
435 less meaningful one like,
436 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
437 attribute 'hw_driver'">
438
439 Returns:
440 string of underlying exception raised in servod.
441 """
442 return re.sub('^.*>:', '', xmlexc.faultString)
443
444
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700445 def get(self, gpio_name):
446 """Get the value of a gpio from Servod."""
447 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700448 try:
449 return self._server.get(gpio_name)
450 except xmlrpclib.Fault as e:
451 err_msg = "Getting '%s' :: %s" % \
452 (gpio_name, self._get_xmlrpclib_exception(e))
453 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700454
455
456 def set(self, gpio_name, gpio_value):
457 """Set and check the value of a gpio using Servod."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700458 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800459 retry_count = Servo.GET_RETRY_MAX
460 while gpio_value != self.get(gpio_name) and retry_count:
461 logging.warn("%s != %s, retry %d", gpio_name, gpio_value,
462 retry_count)
463 retry_count -= 1
464 time.sleep(Servo.SHORT_DELAY)
465 if not retry_count:
466 assert gpio_value == self.get(gpio_name), \
467 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700468
469
470 def set_nocheck(self, gpio_name, gpio_value):
471 """Set the value of a gpio using Servod."""
472 assert gpio_name and gpio_value
Todd Broch9753bd42012-03-21 10:15:08 -0700473 logging.info('Setting %s to %s', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700474 try:
475 self._server.set(gpio_name, gpio_value)
476 except xmlrpclib.Fault as e:
477 err_msg = "Setting '%s' to '%s' :: %s" % \
478 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
479 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700480
481
Jon Salzc88e5b62011-11-30 14:38:54 +0800482 # TODO(waihong) It may fail if multiple servo's are connected to the same
483 # host. Should look for a better way, like the USB serial name, to identify
484 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700485 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
486 # updated.
Jon Salzc88e5b62011-11-30 14:38:54 +0800487 def probe_host_usb_dev(self):
488 """Probe the USB disk device plugged-in the servo from the host side.
489
490 It tries to switch the USB mux to make the host unable to see the
491 USB disk and compares the result difference.
492
493 This only works if the servo is attached to the local host.
494
495 Returns:
496 A string of USB disk path, like '/dev/sdb', or None if not existed.
497 """
498 cmd = 'ls /dev/sd[a-z]'
499 original_value = self.get('usb_mux_sel1')
500
501 # Make the host unable to see the USB disk.
502 if original_value != 'dut_sees_usbkey':
503 self.set('usb_mux_sel1', 'dut_sees_usbkey')
504 time.sleep(self.USB_DETECTION_DELAY)
505 no_usb_set = set(utils.system_output(cmd, ignore_status=True).split())
506
507 # Make the host able to see the USB disk.
508 self.set('usb_mux_sel1', 'servo_sees_usbkey')
509 time.sleep(self.USB_DETECTION_DELAY)
510 has_usb_set = set(utils.system_output(cmd, ignore_status=True).split())
511
512 # Back to its original value.
513 if original_value != 'servo_sees_usbkey':
514 self.set('usb_mux_sel1', original_value)
515 time.sleep(self.USB_DETECTION_DELAY)
516
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 """
539 # Turn the device off. This should happen before USB key detection, to
540 # prevent a recovery destined DUT from sensing the USB key due to the
541 # autodetection procedure.
542 self.initialize_dut(cold_reset=True)
543
544 # Set up Servo's usb mux.
545 self.set('prtctl4_pwren', 'on')
546 self.enable_usb_hub(host=True)
547 if image_path:
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800548 logging.info('Searching for usb device and copying image to it. '
549 'Please wait a few minutes...')
Mike Truty49153d82012-08-21 22:27:30 -0500550 if not self._server.download_image_to_usb(image_path):
551 logging.error('Failed to transfer requested image to USB. '
552 'Please take a look at Servo Logs.')
553 raise error.AutotestError('Download image to usb failed.')
554 if make_image_noninteractive:
555 logging.info('Making image noninteractive')
556 if not self._server.make_image_noninteractive():
557 logging.error('Failed to make image noninteractive. '
558 'Please take a look at Servo Logs.')
559
560
Simran Basi741b5d42012-05-18 11:27:15 -0700561 def install_recovery_image(self, image_path=None,
562 wait_timeout=RECOVERY_INSTALL_DELAY,
563 make_image_noninteractive=False,
564 host=None):
Jon Salzc88e5b62011-11-30 14:38:54 +0800565 """Install the recovery image specied by the path onto the DUT.
566
567 This method uses google recovery mode to install a recovery image
568 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 +0800569 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800570 we use the recovery image already on the usb image.
571
572 Args:
573 image_path: Path on the host to the recovery image.
Gilad Arnold9df73de2012-03-14 09:35:08 -0700574 wait_timeout: How long to wait for completion; default is
575 determined by a constant.
Simran Basi741b5d42012-05-18 11:27:15 -0700576 make_image_noninteractive: Make the recovery image noninteractive,
577 therefore the DUT will reboot
578 automatically after installation.
579 host: Host object for the DUT that the installation process is
580 running on. If provided, will wait to see if the host is back
581 up after starting recovery mode.
Jon Salzc88e5b62011-11-30 14:38:54 +0800582 """
Mike Truty49153d82012-08-21 22:27:30 -0500583 self.image_to_servo_usb(image_path, make_image_noninteractive)
Jon Salzc88e5b62011-11-30 14:38:54 +0800584
585 # Boot in recovery mode.
586 try:
Jon Salzc88e5b62011-11-30 14:38:54 +0800587 self.enable_recovery_mode()
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800588 self.power_short_press()
Jon Salzc88e5b62011-11-30 14:38:54 +0800589 time.sleep(Servo.RECOVERY_BOOT_DELAY)
Tom Wai-Hong Tam922ed052012-01-09 12:27:52 +0800590 self.set('usb_mux_sel1', 'dut_sees_usbkey')
Jon Salzc88e5b62011-11-30 14:38:54 +0800591 self.disable_recovery_mode()
592
Simran Basi741b5d42012-05-18 11:27:15 -0700593 if host:
Jon Salzc88e5b62011-11-30 14:38:54 +0800594 logging.info('Running the recovery process on the DUT. '
Simran Basi741b5d42012-05-18 11:27:15 -0700595 'Will wait up to %d seconds for recovery to '
596 'complete.', wait_timeout)
597 start_time = time.time()
598 # Wait for the host to come up.
599 if host.wait_up(timeout=wait_timeout):
600 logging.info('Recovery process completed successfully in '
601 '%d seconds.', time.time() - start_time)
602 else:
Vic Yang3a7cf602012-11-07 17:28:39 +0800603 logging.error('Host failed to come back up in the allotted '
604 'time: %d seconds.', wait_timeout)
Jon Salzc88e5b62011-11-30 14:38:54 +0800605 logging.info('Removing the usb key from the DUT.')
606 self.disable_usb_hub()
Jon Salzc88e5b62011-11-30 14:38:54 +0800607 except:
608 # In case anything went wrong we want to make sure to do a clean
609 # reset.
610 self.disable_recovery_mode()
611 self.warm_reset()
612 raise
613
614
Todd Brochf24d2782011-08-19 10:55:41 -0700615 def _connect_servod(self, servo_host, servo_port):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700616 """Connect to the Servod process with XMLRPC.
617
618 Args:
619 servo_port: Port the Servod process is listening on.
620 """
Todd Brochf24d2782011-08-19 10:55:41 -0700621 remote = 'http://%s:%s' % (servo_host, servo_port)
Todd Broch96d83aa2011-08-29 14:37:38 -0700622 self._server = xmlrpclib.ServerProxy(remote)
Todd Brochf24d2782011-08-19 10:55:41 -0700623 try:
624 self._server.echo("ping-test")
Todd Broch96d83aa2011-08-29 14:37:38 -0700625 except:
626 logging.error('Connection to servod failed')
Todd Brochf24d2782011-08-19 10:55:41 -0700627 raise
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800628
629
630 def _scp_image(self, image_path):
631 """Copy image to the servo host.
632
633 When programming a firmware image on the DUT, the image must be
634 located on the host to which the servo device is connected. Sometimes
635 servo is controlled by a remote host, in this case the image needs to
636 be transferred to the remote host.
637
638 @param image_path: a string, name of the firmware image file to be
639 transferred.
640 @return: a string, full path name of the copied file on the remote.
641 """
642
643 dest_path = os.path.join('/tmp', os.path.basename(image_path))
644 scp_cmd = self._scp_cmd_template % (image_path, dest_path)
645 utils.system(scp_cmd)
646 return dest_path
647
648
649 def system_on_servo(self, command):
650 """Execute the passed in command on the servod host."""
651 if self._sudo_required:
652 command = 'sudo -n %s' % command
653 if self._ssh_prefix:
654 command = "%s '%s'" % (self._ssh_prefix, command)
655 logging.info('Will execute on servo host: %s' % command)
656 utils.system(command)
657
658
659 def program_ec(self, board, image):
660 """Program EC on a given board using given image."""
661 if not self.is_localhost():
662 image = self._scp_image(image)
663 programmer.program_ec(board, self, image)
664
665
666 def program_bootprom(self, board, image):
667 """Program bootprom on a given board using given image."""
668 if not self.is_localhost():
669 image = self._scp_image(image)
670 programmer.program_bootprom(board, self, image)