blob: 53d2a068aa8825f7a2328e08c9556a9bdfc53a22 [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
Todd Brochefe72cb2012-07-11 19:58:53 -07008import logging, os, re, select, subprocess, sys, time, xmlrpclib
Jon Salzc88e5b62011-11-30 14:38:54 +08009from autotest_lib.client.bin import utils as client_utils
Simran Basi741b5d42012-05-18 11:27:15 -070010from autotest_lib.client.common_lib import error
Jon Salzc88e5b62011-11-30 14:38:54 +080011from autotest_lib.server import utils
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070012
J. Richard Barnette384056b2012-04-16 11:04:46 -070013class Servo(object):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070014 """Manages control of a Servo board.
15
16 Servo is a board developed by hardware group to aide in the debug and
17 control of various partner devices. Servo's features include the simulation
18 of pressing the power button, closing the lid, and pressing Ctrl-d. This
19 class manages setting up and communicating with a servo demon (servod)
20 process. It provides both high-level functions for common servo tasks and
21 low-level functions for directly setting and reading gpios.
22 """
23
Chrome Bot9a1137d2011-07-19 14:35:00 -070024 # Power button press delays in seconds.
J. Richard Barnetted2e4cbd2012-06-29 12:18:40 -070025 #
J. Richard Barnette5383f072012-07-26 17:35:40 -070026 # The EC specification says that 8.0 seconds should be enough
27 # for the long power press. However, some platforms need a bit
28 # more time. Empirical testing has found these requirements:
29 # Alex: 8.2 seconds
30 # ZGB: 8.5 seconds
31 # The actual value is set to the largest known necessary value.
32 #
33 # TODO(jrbarnette) Being generous is the right thing to do for
34 # existing platforms, but if this code is to be used for
35 # qualification of new hardware, we should be less generous.
36 LONG_DELAY = 8.5
Chrome Bot9a1137d2011-07-19 14:35:00 -070037 SHORT_DELAY = 0.1
38 NORMAL_TRANSITION_DELAY = 1.2
J. Richard Barnette5383f072012-07-26 17:35:40 -070039
Todd Broch31c82502011-08-29 08:14:39 -070040 # Maximum number of times to re-read power button on release.
41 RELEASE_RETRY_MAX = 5
Todd Brochcf7c6652012-02-24 13:03:59 -080042 GET_RETRY_MAX = 10
Chrome Bot9a1137d2011-07-19 14:35:00 -070043
J. Richard Barnette5383f072012-07-26 17:35:40 -070044 # Delays to deal with DUT state transitions.
Chrome Bot9a1137d2011-07-19 14:35:00 -070045 SLEEP_DELAY = 6
46 BOOT_DELAY = 10
J. Richard Barnette8bd49842012-07-19 14:21:15 -070047 RECOVERY_BOOT_DELAY = 10
J. Richard Barnettec5a77ad2012-04-25 08:19:00 -070048 RECOVERY_INSTALL_DELAY = 540
Chrome Bot9a1137d2011-07-19 14:35:00 -070049
J. Richard Barnetteb6133972012-07-19 17:13:55 -070050 # Time required for the EC to be working after cold reset.
51 # Five seconds is at least twice as big as necessary for Alex,
52 # and is presumably good enough for all future systems.
53 _EC_RESET_DELAY = 5.0
54
Chrome Bot9a1137d2011-07-19 14:35:00 -070055 # Servo-specific delays.
56 MAX_SERVO_STARTUP_DELAY = 10
57 SERVO_SEND_SIGNAL_DELAY = 0.5
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070058
Jon Salzc88e5b62011-11-30 14:38:54 +080059 # Time between an usb disk plugged-in and detected in the system.
60 USB_DETECTION_DELAY = 10
61
Todd Broch9753bd42012-03-21 10:15:08 -070062 KEY_MATRIX = {
63 'm1': {'ctrl_r': ['0', '0'], 'd': ['0', '1'],
64 'enter': ['1', '0'], 'none': ['1', '1']},
65 'm2': {'ctrl': ['0', '0'], 'refresh': ['0', '1'],
66 'unused': ['1', '0'], 'none': ['1', '1']}
67 }
Chris Masone6a0680f2012-03-02 08:40:00 -080068
J. Richard Barnette67ccb872012-04-19 16:34:56 -070069
Chris Masone6a0680f2012-03-02 08:40:00 -080070 @staticmethod
J. Richard Barnette67ccb872012-04-19 16:34:56 -070071 def _make_servo_hostname(hostname):
72 host_parts = hostname.split('.')
73 host_parts[0] = host_parts[0] + '-servo'
74 return '.'.join(host_parts)
Chris Masone6a0680f2012-03-02 08:40:00 -080075
J. Richard Barnette67ccb872012-04-19 16:34:56 -070076 @staticmethod
77 def get_lab_servo(target_hostname):
78 """Instantiate a Servo for |target_hostname| in the lab.
Chris Masone6a0680f2012-03-02 08:40:00 -080079
J. Richard Barnette67ccb872012-04-19 16:34:56 -070080 Assuming that |target_hostname| is a device in the CrOS test
81 lab, create and return a Servo object pointed at the servo
82 attached to that DUT. The servo in the test lab is assumed
83 to already have servod up and running on it.
84
85 @param target_hostname: device whose servo we want to target.
Chris Masone6a0680f2012-03-02 08:40:00 -080086 @return an appropriately configured Servo
87 """
J. Richard Barnette67ccb872012-04-19 16:34:56 -070088 servo_host = Servo._make_servo_hostname(target_hostname)
89 if utils.host_is_in_lab_zone(servo_host):
90 try:
91 return Servo(servo_host=servo_host)
92 except:
93 # TODO(jrbarnette): Long-term, if we can't get to
94 # a servo in the lab, we want to fail, so we should
95 # pass any exceptions along. Short-term, we're not
96 # ready to rely on servo, so we ignore failures.
97 pass
98 return None
Chris Masone6a0680f2012-03-02 08:40:00 -080099
100
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700101 def __init__(self, servo_host='localhost', servo_port=9999):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700102 """Sets up the servo communication infrastructure.
103
J. Richard Barnette55fb8062012-05-23 10:29:31 -0700104 @param servo_host Name of the host where the servod process
105 is running.
106 @param servo_port Port the servod process is listening on.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700107 """
J. Richard Barnette384056b2012-04-16 11:04:46 -0700108 self._server = None
Todd Brochf24d2782011-08-19 10:55:41 -0700109 self._connect_servod(servo_host, servo_port)
Chrome Bot9a1137d2011-07-19 14:35:00 -0700110
111
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700112 def initialize_dut(self, cold_reset=False):
113 """Initializes a dut for testing purposes.
114
115 This sets various servo signals back to default values
116 appropriate for the target board. By default, if the DUT
117 is already on, it stays on. If the DUT is powered off
118 before initialization, its state afterward is unspecified.
119
120 If cold reset is requested, the DUT is guaranteed to be off
121 at the end of initialization, regardless of its initial
122 state.
123
124 Rationale: Basic initialization of servo sets the lid open,
125 when there is a lid. This operation won't affect powered on
126 units; however, setting the lid open may power on a unit
127 that's off, depending on factors outside the scope of this
128 function.
129
130 @param cold_reset If True, cold reset the device after
131 initialization.
132 """
133 self._server.hwinit()
134 if cold_reset:
135 self.cold_reset()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700136
137
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700138 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700139 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700140 # After a long power press, the EC may ignore the next power
141 # button press (at least on Alex). To guarantee that this
142 # won't happen, we need to allow the EC one second to
143 # collect itself.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700144 self.power_key(Servo.LONG_DELAY)
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700145 time.sleep(1.0)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700146
147
148 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700149 """Simulate a normal power button press."""
150 self.power_key()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700151
152
153 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700154 """Simulate a short power button press."""
155 self.power_key(Servo.SHORT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700156
157
Chrome Bot9a1137d2011-07-19 14:35:00 -0700158 def power_key(self, secs=NORMAL_TRANSITION_DELAY):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700159 """Simulate a power button press.
160
161 Args:
162 secs: Time in seconds to simulate the keypress.
163 """
Craig Harrison6b36b122011-06-28 17:58:43 -0700164 self.set_nocheck('pwr_button', 'press')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700165 time.sleep(secs)
Todd Broch31c82502011-08-29 08:14:39 -0700166 self.set_nocheck('pwr_button', 'release')
167 # TODO(tbroch) Different systems have different release times on the
168 # power button that this loop addresses. Longer term we may want to
169 # make this delay platform specific.
170 retry = 1
171 while True:
172 value = self.get('pwr_button')
173 if value == 'release' or retry > Servo.RELEASE_RETRY_MAX:
174 break
Todd Broch9753bd42012-03-21 10:15:08 -0700175 logging.info('Waiting for pwr_button to release, retry %d.', retry)
Todd Broch31c82502011-08-29 08:14:39 -0700176 retry += 1
177 time.sleep(Servo.SHORT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700178
179
180 def lid_open(self):
181 """Simulate opening the lid."""
Craig Harrison48997262011-06-27 14:31:10 -0700182 self.set_nocheck('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700183
184
185 def lid_close(self):
Craig Harrison48997262011-06-27 14:31:10 -0700186 """Simulate closing the lid.
187
188 Waits 6 seconds to ensure the device is fully asleep before returning.
189 """
190 self.set_nocheck('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700191 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700192
193
Todd Broch9753bd42012-03-21 10:15:08 -0700194 def _press_and_release_keys(self, m1, m2,
195 press_secs=SERVO_SEND_SIGNAL_DELAY):
Todd Broch9dfc3a82011-11-01 08:09:28 -0700196 """Simulate button presses."""
Todd Broch9753bd42012-03-21 10:15:08 -0700197 # set keys to none
198 (m2_a1, m2_a0) = self.KEY_MATRIX['m2']['none']
199 (m1_a1, m1_a0) = self.KEY_MATRIX['m1']['none']
200 self.set_nocheck('kbd_m2_a0', m2_a0)
201 self.set_nocheck('kbd_m2_a1', m2_a1)
202 self.set_nocheck('kbd_m1_a0', m1_a0)
203 self.set_nocheck('kbd_m1_a1', m1_a1)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700204
Todd Broch9753bd42012-03-21 10:15:08 -0700205 (m2_a1, m2_a0) = self.KEY_MATRIX['m2'][m2]
206 (m1_a1, m1_a0) = self.KEY_MATRIX['m1'][m1]
Todd Broch9dfc3a82011-11-01 08:09:28 -0700207 self.set_nocheck('kbd_en', 'on')
Todd Broch9753bd42012-03-21 10:15:08 -0700208 self.set_nocheck('kbd_m2_a0', m2_a0)
209 self.set_nocheck('kbd_m2_a1', m2_a1)
210 self.set_nocheck('kbd_m1_a0', m1_a0)
211 self.set_nocheck('kbd_m1_a1', m1_a1)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700212 time.sleep(press_secs)
213 self.set_nocheck('kbd_en', 'off')
214
215
Chrome Bot9a1137d2011-07-19 14:35:00 -0700216 def ctrl_d(self):
217 """Simulate Ctrl-d simultaneous button presses."""
Todd Broch9dfc3a82011-11-01 08:09:28 -0700218 self._press_and_release_keys('d', 'ctrl')
219
220
Todd Broch9753bd42012-03-21 10:15:08 -0700221 def ctrl_enter(self):
222 """Simulate Ctrl-enter simultaneous button presses."""
223 self._press_and_release_keys('enter', 'ctrl')
224
225
Todd Broch9dfc3a82011-11-01 08:09:28 -0700226 def d_key(self):
227 """Simulate Enter key button press."""
228 self._press_and_release_keys('d', 'none')
229
230
231 def ctrl_key(self):
232 """Simulate Enter key button press."""
233 self._press_and_release_keys('none', 'ctrl')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700234
235
Chrome Bot9a1137d2011-07-19 14:35:00 -0700236 def enter_key(self):
237 """Simulate Enter key button press."""
Todd Broch9dfc3a82011-11-01 08:09:28 -0700238 self._press_and_release_keys('enter', 'none')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700239
240
Chrome Bot9a1137d2011-07-19 14:35:00 -0700241 def refresh_key(self):
242 """Simulate Refresh key (F3) button press."""
Todd Broch9dfc3a82011-11-01 08:09:28 -0700243 self._press_and_release_keys('none', 'refresh')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700244
245
Chrome Bot9a1137d2011-07-19 14:35:00 -0700246 def imaginary_key(self):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700247 """Simulate imaginary key button press.
248
249 Maps to a key that doesn't physically exist.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700250 """
Todd Broch9dfc3a82011-11-01 08:09:28 -0700251 self._press_and_release_keys('none', 'unused')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700252
253
Craig Harrison6b36b122011-06-28 17:58:43 -0700254 def enable_recovery_mode(self):
255 """Enable recovery mode on device."""
256 self.set('rec_mode', 'on')
257
258
259 def disable_recovery_mode(self):
260 """Disable recovery mode on device."""
261 self.set('rec_mode', 'off')
262
263
264 def enable_development_mode(self):
265 """Enable development mode on device."""
266 self.set('dev_mode', 'on')
267
268
269 def disable_development_mode(self):
270 """Disable development mode on device."""
271 self.set('dev_mode', 'off')
272
Chris Sosa8ee1d592011-08-14 16:50:31 -0700273 def enable_usb_hub(self, host=False):
Craig Harrison86b1a572011-08-12 11:26:52 -0700274 """Enable Servo's USB/ethernet hub.
275
Chris Sosa8ee1d592011-08-14 16:50:31 -0700276 This is equivalent to plugging in the USB devices attached to Servo to
277 the host (if |host| is True) or dut (if |host| is False).
278 For host=False, requires that the USB out on the servo board is
279 connected to a USB in port on the target device. Servo's USB ports are
280 labeled DUT_HUB_USB1 and DUT_HUB_USB2. Servo's ethernet port is also
281 connected to this hub. Servo's USB port DUT_HUB_IN is the output of the
282 hub.
Craig Harrison86b1a572011-08-12 11:26:52 -0700283 """
284 self.set('dut_hub_pwren', 'on')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700285 if host:
Todd Broch9753bd42012-03-21 10:15:08 -0700286 self.set('usb_mux_oe1', 'on')
287 self.set('usb_mux_sel1', 'servo_sees_usbkey')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700288 else:
Todd Broch9753bd42012-03-21 10:15:08 -0700289 self.set('dut_hub_sel', 'dut_sees_hub')
Chris Sosa8ee1d592011-08-14 16:50:31 -0700290
Craig Harrison86b1a572011-08-12 11:26:52 -0700291 self.set('dut_hub_on', 'yes')
292
293
294 def disable_usb_hub(self):
295 """Disable Servo's USB/ethernet hub.
296
297 This is equivalent to unplugging the USB devices attached to Servo.
298 """
299 self.set('dut_hub_on', 'no')
300
301
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700302 def boot_devmode(self):
303 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800304 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700305 self.pass_devmode()
306
307
308 def pass_devmode(self):
309 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700310 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700311 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700312 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700313
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700314
Craig Harrison6b36b122011-06-28 17:58:43 -0700315 def cold_reset(self):
316 """Perform a cold reset of the EC.
317
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700318 This has the side effect of shutting off the device. The
319 device is guaranteed to be off at the end of this call.
Craig Harrison6b36b122011-06-28 17:58:43 -0700320 """
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700321 # After the reset, give the EC the time it needs to
322 # re-initialize.
Craig Harrison6b36b122011-06-28 17:58:43 -0700323 self.set('cold_reset', 'on')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700324 time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY)
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700325 self.set('cold_reset', 'off')
326 time.sleep(self._EC_RESET_DELAY)
Craig Harrison6b36b122011-06-28 17:58:43 -0700327
328
329 def warm_reset(self):
330 """Perform a warm reset of the device.
331
332 Has the side effect of restarting the device.
333 """
334 self.set('warm_reset', 'on')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700335 time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY)
Craig Harrison6b36b122011-06-28 17:58:43 -0700336 self.set('warm_reset', 'off')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700337
338
Todd Brochefe72cb2012-07-11 19:58:53 -0700339 def _get_xmlrpclib_exception(self, xmlexc):
340 """Get meaningful exception string from xmlrpc.
341
342 Args:
343 xmlexc: xmlrpclib.Fault object
344
345 xmlrpclib.Fault.faultString has the following format:
346
347 <type 'exception type'>:'actual error message'
348
349 Parse and return the real exception from the servod side instead of the
350 less meaningful one like,
351 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
352 attribute 'hw_driver'">
353
354 Returns:
355 string of underlying exception raised in servod.
356 """
357 return re.sub('^.*>:', '', xmlexc.faultString)
358
359
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700360 def get(self, gpio_name):
361 """Get the value of a gpio from Servod."""
362 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700363 try:
364 return self._server.get(gpio_name)
365 except xmlrpclib.Fault as e:
366 err_msg = "Getting '%s' :: %s" % \
367 (gpio_name, self._get_xmlrpclib_exception(e))
368 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700369
370
371 def set(self, gpio_name, gpio_value):
372 """Set and check the value of a gpio using Servod."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700373 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800374 retry_count = Servo.GET_RETRY_MAX
375 while gpio_value != self.get(gpio_name) and retry_count:
376 logging.warn("%s != %s, retry %d", gpio_name, gpio_value,
377 retry_count)
378 retry_count -= 1
379 time.sleep(Servo.SHORT_DELAY)
380 if not retry_count:
381 assert gpio_value == self.get(gpio_name), \
382 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700383
384
385 def set_nocheck(self, gpio_name, gpio_value):
386 """Set the value of a gpio using Servod."""
387 assert gpio_name and gpio_value
Todd Broch9753bd42012-03-21 10:15:08 -0700388 logging.info('Setting %s to %s', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700389 try:
390 self._server.set(gpio_name, gpio_value)
391 except xmlrpclib.Fault as e:
392 err_msg = "Setting '%s' to '%s' :: %s" % \
393 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
394 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700395
396
Jon Salzc88e5b62011-11-30 14:38:54 +0800397 # TODO(waihong) It may fail if multiple servo's are connected to the same
398 # host. Should look for a better way, like the USB serial name, to identify
399 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700400 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
401 # updated.
Jon Salzc88e5b62011-11-30 14:38:54 +0800402 def probe_host_usb_dev(self):
403 """Probe the USB disk device plugged-in the servo from the host side.
404
405 It tries to switch the USB mux to make the host unable to see the
406 USB disk and compares the result difference.
407
408 This only works if the servo is attached to the local host.
409
410 Returns:
411 A string of USB disk path, like '/dev/sdb', or None if not existed.
412 """
413 cmd = 'ls /dev/sd[a-z]'
414 original_value = self.get('usb_mux_sel1')
415
416 # Make the host unable to see the USB disk.
417 if original_value != 'dut_sees_usbkey':
418 self.set('usb_mux_sel1', 'dut_sees_usbkey')
419 time.sleep(self.USB_DETECTION_DELAY)
420 no_usb_set = set(utils.system_output(cmd, ignore_status=True).split())
421
422 # Make the host able to see the USB disk.
423 self.set('usb_mux_sel1', 'servo_sees_usbkey')
424 time.sleep(self.USB_DETECTION_DELAY)
425 has_usb_set = set(utils.system_output(cmd, ignore_status=True).split())
426
427 # Back to its original value.
428 if original_value != 'servo_sees_usbkey':
429 self.set('usb_mux_sel1', original_value)
430 time.sleep(self.USB_DETECTION_DELAY)
431
432 diff_set = has_usb_set - no_usb_set
433 if len(diff_set) == 1:
434 return diff_set.pop()
435 else:
436 return None
437
438
Simran Basi741b5d42012-05-18 11:27:15 -0700439 def install_recovery_image(self, image_path=None,
440 wait_timeout=RECOVERY_INSTALL_DELAY,
441 make_image_noninteractive=False,
442 host=None):
Jon Salzc88e5b62011-11-30 14:38:54 +0800443 """Install the recovery image specied by the path onto the DUT.
444
445 This method uses google recovery mode to install a recovery image
446 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 +0800447 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800448 we use the recovery image already on the usb image.
449
450 Args:
451 image_path: Path on the host to the recovery image.
Gilad Arnold9df73de2012-03-14 09:35:08 -0700452 wait_timeout: How long to wait for completion; default is
453 determined by a constant.
Simran Basi741b5d42012-05-18 11:27:15 -0700454 make_image_noninteractive: Make the recovery image noninteractive,
455 therefore the DUT will reboot
456 automatically after installation.
457 host: Host object for the DUT that the installation process is
458 running on. If provided, will wait to see if the host is back
459 up after starting recovery mode.
Jon Salzc88e5b62011-11-30 14:38:54 +0800460 """
Gilad Arnold9df73de2012-03-14 09:35:08 -0700461 # Turn the device off. This should happen before USB key detection, to
462 # prevent a recovery destined DUT from sensing the USB key due to the
463 # autodetection procedure.
J. Richard Barnette524c7fb2012-07-18 16:30:10 -0700464 self.initialize_dut(cold_reset=True)
Gilad Arnold9df73de2012-03-14 09:35:08 -0700465
Jon Salzc88e5b62011-11-30 14:38:54 +0800466 # Set up Servo's usb mux.
467 self.set('prtctl4_pwren', 'on')
468 self.enable_usb_hub(host=True)
Tom Wai-Hong Tama07115e2012-01-09 12:27:01 +0800469 if image_path:
Simran Basi741b5d42012-05-18 11:27:15 -0700470 logging.info('Searching for usb device')
471 if not self._server.download_image_to_usb(image_path):
472 logging.error('Failed to transfer requested image to USB. '
473 'Please take a look at Servo Logs.')
474 raise error.AutotestError('Download image to usb failed.')
475 if make_image_noninteractive:
476 logging.info('Making image noninteractive')
477 if not self._server.make_image_noninteractive():
478 logging.error('Failed to make image noninteractive. '
479 'Please take a look at Servo Logs.')
Jon Salzc88e5b62011-11-30 14:38:54 +0800480
481 # Boot in recovery mode.
482 try:
Jon Salzc88e5b62011-11-30 14:38:54 +0800483 self.enable_recovery_mode()
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800484 self.power_short_press()
Jon Salzc88e5b62011-11-30 14:38:54 +0800485 time.sleep(Servo.RECOVERY_BOOT_DELAY)
Tom Wai-Hong Tam922ed052012-01-09 12:27:52 +0800486 self.set('usb_mux_sel1', 'dut_sees_usbkey')
Jon Salzc88e5b62011-11-30 14:38:54 +0800487 self.disable_recovery_mode()
488
Simran Basi741b5d42012-05-18 11:27:15 -0700489 if host:
Jon Salzc88e5b62011-11-30 14:38:54 +0800490 logging.info('Running the recovery process on the DUT. '
Simran Basi741b5d42012-05-18 11:27:15 -0700491 'Will wait up to %d seconds for recovery to '
492 'complete.', wait_timeout)
493 start_time = time.time()
494 # Wait for the host to come up.
495 if host.wait_up(timeout=wait_timeout):
496 logging.info('Recovery process completed successfully in '
497 '%d seconds.', time.time() - start_time)
498 else:
499 logger.error('Host failed to come back up in the allotted '
500 'time: %d seconds.', wait_timeout)
Jon Salzc88e5b62011-11-30 14:38:54 +0800501 logging.info('Removing the usb key from the DUT.')
502 self.disable_usb_hub()
Jon Salzc88e5b62011-11-30 14:38:54 +0800503 except:
504 # In case anything went wrong we want to make sure to do a clean
505 # reset.
506 self.disable_recovery_mode()
507 self.warm_reset()
508 raise
509
510
Todd Brochf24d2782011-08-19 10:55:41 -0700511 def _connect_servod(self, servo_host, servo_port):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700512 """Connect to the Servod process with XMLRPC.
513
514 Args:
515 servo_port: Port the Servod process is listening on.
516 """
Todd Brochf24d2782011-08-19 10:55:41 -0700517 remote = 'http://%s:%s' % (servo_host, servo_port)
Todd Broch96d83aa2011-08-29 14:37:38 -0700518 self._server = xmlrpclib.ServerProxy(remote)
Todd Brochf24d2782011-08-19 10:55:41 -0700519 try:
520 self._server.echo("ping-test")
Todd Broch96d83aa2011-08-29 14:37:38 -0700521 except:
522 logging.error('Connection to servod failed')
Todd Brochf24d2782011-08-19 10:55:41 -0700523 raise