blob: 9b279217db653c7e0b1a67baa85dc2d70bf82389 [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
Congbin Guoa1f9cba2018-07-03 11:36:59 -07008import ast
9import logging
Vadim Bendeburyb80ba592012-12-07 15:02:34 -080010import os
Congbin Guoa1f9cba2018-07-03 11:36:59 -070011import re
12import time
13import xmlrpclib
Vadim Bendeburyb80ba592012-12-07 15:02:34 -080014
Simran Basi741b5d42012-05-18 11:27:15 -070015from autotest_lib.client.common_lib import error
Congbin Guo42427612019-02-12 10:22:06 -080016from autotest_lib.server import utils as server_utils
Ricky Liangc31aab32014-07-03 16:23:29 +080017from autotest_lib.server.cros.servo import firmware_programmer
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070018
Kevin Chenga22c4a82016-10-07 14:13:25 -070019# Time to wait when probing for a usb device, it takes on avg 17 seconds
20# to do a full probe.
21_USB_PROBE_TIMEOUT = 40
22
J. Richard Barnette41320ee2013-03-11 13:00:13 -070023
Congbin Guo42427612019-02-12 10:22:06 -080024def _extract_image_from_tarball(tarball, dest_dir, image_candidates):
25 """Try extracting the image_candidates from the tarball.
26
27 @param tarball: The path of the tarball.
28 @param dest_path: The path of the destination.
29 @param image_candidates: A tuple of the paths of image candidates.
30
31 @return: The first path from the image candidates, which succeeds, or None
32 if all the image candidates fail.
33 """
34 for image in image_candidates:
35 status = server_utils.system(
36 ('tar xf %s -C %s %s' % (tarball, dest_dir, image)),
37 timeout=60, ignore_status=True)
38 if status == 0:
39 return image
40 return None
41
42
J. Richard Barnette0c9c5882014-06-11 15:27:05 -070043class _PowerStateController(object):
44
45 """Class to provide board-specific power operations.
46
47 This class is responsible for "power on" and "power off"
48 operations that can operate without making assumptions in
49 advance about board state. It offers an interface that
50 abstracts out the different sequences required for different
51 board types.
52
53 """
54
55 # Constants acceptable to be passed for the `rec_mode` parameter
56 # to power_on().
57 #
58 # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or
59 # SD card.
60 # REC_OFF: Boot in normal mode, i.e. boot from internal storage.
61
62 REC_ON = 'rec'
63 REC_OFF = 'on'
Shelley Chen65938622018-05-16 07:45:54 -070064 REC_ON_FORCE_MRC = 'rec_force_mrc'
J. Richard Barnette0c9c5882014-06-11 15:27:05 -070065
66 # Delay in seconds needed between asserting and de-asserting
67 # warm reset.
68 _RESET_HOLD_TIME = 0.5
69
70 def __init__(self, servo):
71 """Initialize the power state control.
72
73 @param servo Servo object providing the underlying `set` and `get`
74 methods for the target controls.
75
76 """
77 self._servo = servo
78
79 def reset(self):
80 """Force the DUT to reset.
81
82 The DUT is guaranteed to be on at the end of this call,
83 regardless of its previous state, provided that there is
84 working OS software. This also guarantees that the EC has
85 been restarted.
86
87 """
88 self._servo.set_nocheck('power_state', 'reset')
89
90 def warm_reset(self):
91 """Apply warm reset to the DUT.
92
93 This asserts, then de-asserts the 'warm_reset' signal.
94 Generally, this causes the board to restart.
95
96 """
97 self._servo.set_get_all(['warm_reset:on',
98 'sleep:%.4f' % self._RESET_HOLD_TIME,
99 'warm_reset:off'])
100
J. Richard Barnette0c9c5882014-06-11 15:27:05 -0700101 def power_off(self):
102 """Force the DUT to power off.
103
104 The DUT is guaranteed to be off at the end of this call,
105 regardless of its previous state, provided that there is
106 working EC and boot firmware. There is no requirement for
107 working OS software.
108
109 """
110 self._servo.set_nocheck('power_state', 'off')
111
112 def power_on(self, rec_mode=REC_OFF):
113 """Force the DUT to power on.
114
115 Prior to calling this function, the DUT must be powered off,
116 e.g. with a call to `power_off()`.
117
118 At power on, recovery mode is set as specified by the
119 corresponding argument. When booting with recovery mode on, it
120 is the caller's responsibility to unplug/plug in a bootable
121 external storage device.
122
123 If the DUT requires a delay after powering on but before
124 processing inputs such as USB stick insertion, the delay is
125 handled by this method; the caller is not responsible for such
126 delays.
127
128 @param rec_mode Setting of recovery mode to be applied at
129 power on. default: REC_OFF aka 'off'
130
131 """
132 self._servo.set_nocheck('power_state', rec_mode)
133
134
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700135class _Uart(object):
Congbin Guo0f00be82019-04-18 17:51:14 -0700136 """Class to capture UART streams of CPU, EC, Cr50, etc."""
137 _UartToCapture = ('cpu', 'ec', 'cr50', 'servo_v4', 'servo_micro', 'usbpd')
138
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700139 def __init__(self, servo):
140 self._servo = servo
141 self._streams = []
Congbin Guo0f00be82019-04-18 17:51:14 -0700142 self.logs_dir = None
Congbin Guofe4d4e82019-04-18 17:51:14 -0700143
Congbin Guo0f00be82019-04-18 17:51:14 -0700144 def _start_stop_capture(self, uart, start):
145 """Helper function to start/stop capturing on specified UART.
146
147 @param uart: The UART name to start/stop capturing.
148 @param start: True to start capturing, otherwise stop.
149
150 @returns True if the operation completes successfully. False if the UART
151 capturing is not supported or failed due to an error.
152 """
153 logging.debug('%s capturing %s UART.', 'Start' if start else 'Stop',
154 uart)
Congbin Guo39dc6f22019-04-23 23:01:21 +0000155 try:
Congbin Guo0f00be82019-04-18 17:51:14 -0700156 self._servo.set('%s_uart_capture' % uart,
157 'on' if start else 'off')
Congbin Guo39dc6f22019-04-23 23:01:21 +0000158 except error.TestFail as err:
159 if 'No control named' in str(err):
Congbin Guo0f00be82019-04-18 17:51:14 -0700160 logging.debug("The servod doesn't support %s_uart_capture.",
161 uart)
162 else:
163 logging.debug("Can't caputre UART for %s: %s", uart, err)
164 return False
165
166 return True
167
168 def start_capture(self):
169 """Start capturing UART streams."""
170 for uart in self._UartToCapture:
171 if self._start_stop_capture(uart, True):
172 self._streams.append(('%s_uart_stream' % uart, '%s_uart.log' %
173 uart))
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700174
Congbin Guofc3b8962019-03-22 17:38:46 -0700175 def dump(self):
176 """Dump UART streams to log files accordingly."""
Congbin Guo0f00be82019-04-18 17:51:14 -0700177 if not self.logs_dir:
Congbin Guofc3b8962019-03-22 17:38:46 -0700178 return
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700179
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700180 for stream, logfile in self._streams:
Congbin Guo0f00be82019-04-18 17:51:14 -0700181 logfile_fullname = os.path.join(self.logs_dir, logfile)
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700182 try:
183 content = self._servo.get(stream)
184 except Exception as err:
185 logging.warn('Failed to get UART log for %s: %s', stream, err)
186 continue
187
188 # The UART stream may contain non-printable characters, and servo
189 # returns it in string representation. We use `ast.leteral_eval`
190 # to revert it back.
191 with open(logfile_fullname, 'a') as fd:
192 fd.write(ast.literal_eval(content))
193
194 def stop_capture(self):
195 """Stop capturing UART streams."""
Congbin Guo0f00be82019-04-18 17:51:14 -0700196 for uart in self._UartToCapture:
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700197 try:
Congbin Guo0f00be82019-04-18 17:51:14 -0700198 self._start_stop_capture(uart, False)
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700199 except Exception as err:
200 logging.warn('Failed to stop UART logging for %s: %s', uart,
201 err)
202
203
J. Richard Barnette384056b2012-04-16 11:04:46 -0700204class Servo(object):
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700205
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700206 """Manages control of a Servo board.
207
208 Servo is a board developed by hardware group to aide in the debug and
209 control of various partner devices. Servo's features include the simulation
210 of pressing the power button, closing the lid, and pressing Ctrl-d. This
211 class manages setting up and communicating with a servo demon (servod)
212 process. It provides both high-level functions for common servo tasks and
213 low-level functions for directly setting and reading gpios.
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700214
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700215 """
216
Chrome Bot9a1137d2011-07-19 14:35:00 -0700217 # Power button press delays in seconds.
J. Richard Barnetted2e4cbd2012-06-29 12:18:40 -0700218 #
J. Richard Barnette5383f072012-07-26 17:35:40 -0700219 # The EC specification says that 8.0 seconds should be enough
220 # for the long power press. However, some platforms need a bit
221 # more time. Empirical testing has found these requirements:
222 # Alex: 8.2 seconds
223 # ZGB: 8.5 seconds
224 # The actual value is set to the largest known necessary value.
225 #
226 # TODO(jrbarnette) Being generous is the right thing to do for
227 # existing platforms, but if this code is to be used for
228 # qualification of new hardware, we should be less generous.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700229 SHORT_DELAY = 0.1
J. Richard Barnette5383f072012-07-26 17:35:40 -0700230
Todd Broch31c82502011-08-29 08:14:39 -0700231 # Maximum number of times to re-read power button on release.
Todd Brochcf7c6652012-02-24 13:03:59 -0800232 GET_RETRY_MAX = 10
Chrome Bot9a1137d2011-07-19 14:35:00 -0700233
J. Richard Barnette5383f072012-07-26 17:35:40 -0700234 # Delays to deal with DUT state transitions.
Chrome Bot9a1137d2011-07-19 14:35:00 -0700235 SLEEP_DELAY = 6
236 BOOT_DELAY = 10
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700237
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700238 # Default minimum time interval between 'press' and 'release'
239 # keyboard events.
Vic Yang0aca1c22012-11-19 18:33:56 -0800240 SERVO_KEY_PRESS_DELAY = 0.1
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700241
Tom Wai-Hong Tam93b5c092015-05-14 02:50:43 +0800242 # Time to toggle recovery switch on and off.
243 REC_TOGGLE_DELAY = 0.1
244
Tom Wai-Hong Tamf9ded092015-05-20 05:37:22 +0800245 # Time to toggle development switch on and off.
246 DEV_TOGGLE_DELAY = 0.1
247
Jon Salzc88e5b62011-11-30 14:38:54 +0800248 # Time between an usb disk plugged-in and detected in the system.
249 USB_DETECTION_DELAY = 10
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800250 # Time to keep USB power off before and after USB mux direction is changed
251 USB_POWEROFF_DELAY = 2
Jon Salzc88e5b62011-11-30 14:38:54 +0800252
Simran Basib7850bb2013-07-24 12:33:42 -0700253 # Time to wait before timing out on servo initialization.
254 INIT_TIMEOUT_SECS = 10
Simran Basi59331342013-07-12 12:06:29 -0700255
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700256
Ricky Liang0dd379c2014-04-23 16:29:08 +0800257 def __init__(self, servo_host, servo_serial=None):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700258 """Sets up the servo communication infrastructure.
259
Fang Deng5d518f42013-08-02 14:04:32 -0700260 @param servo_host: A ServoHost object representing
261 the host running servod.
Ricky Liang0dd379c2014-04-23 16:29:08 +0800262 @param servo_serial: Serial number of the servo board.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700263 """
Fang Deng5d518f42013-08-02 14:04:32 -0700264 # TODO(fdeng): crbug.com/298379
265 # We should move servo_host object out of servo object
266 # to minimize the dependencies on the rest of Autotest.
267 self._servo_host = servo_host
Ricky Liang0dd379c2014-04-23 16:29:08 +0800268 self._servo_serial = servo_serial
Richard Barnette180efe62016-12-02 23:20:44 +0000269 self._server = servo_host.get_servod_server_proxy()
J. Richard Barnette0c9c5882014-06-11 15:27:05 -0700270 self._power_state = _PowerStateController(self)
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700271 self._uart = _Uart(self)
Fang Dengafb88142013-05-30 17:44:31 -0700272 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700273 self._programmer = None
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800274
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800275
Ricky Liang0dd379c2014-04-23 16:29:08 +0800276 @property
277 def servo_serial(self):
278 """Returns the serial number of the servo board."""
279 return self._servo_serial
280
281
Tom Wai-Hong Tam22ee0e52013-04-03 13:36:39 +0800282 def get_power_state_controller(self):
J. Richard Barnette75136b32013-03-26 13:38:44 -0700283 """Return the power state controller for this Servo.
284
285 The power state controller provides board-independent
286 interfaces for reset, power-on, power-off operations.
287
288 """
289 return self._power_state
290
Fang Deng5d518f42013-08-02 14:04:32 -0700291
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700292 def initialize_dut(self, cold_reset=False):
293 """Initializes a dut for testing purposes.
294
295 This sets various servo signals back to default values
296 appropriate for the target board. By default, if the DUT
297 is already on, it stays on. If the DUT is powered off
298 before initialization, its state afterward is unspecified.
299
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700300 Rationale: Basic initialization of servo sets the lid open,
301 when there is a lid. This operation won't affect powered on
302 units; however, setting the lid open may power on a unit
J. Richard Barnette75136b32013-03-26 13:38:44 -0700303 that's off, depending on the board type and previous state
304 of the device.
305
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700306 If `cold_reset` is a true value, the DUT and its EC will be
307 reset, and the DUT rebooted in normal mode.
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700308
309 @param cold_reset If True, cold reset the device after
310 initialization.
311 """
312 self._server.hwinit()
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700313 self.set('usb_mux_oe1', 'on')
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700314 self._usb_state = None
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700315 self.switch_usbkey('off')
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700316 self._uart.start_capture()
J. Richard Barnetteb6133972012-07-19 17:13:55 -0700317 if cold_reset:
J. Richard Barnette4b6af0d2014-06-05 09:57:20 -0700318 self._power_state.reset()
J. Richard Barnette1777f5d2014-09-03 15:18:19 -0700319 logging.debug('Servo initialized, version is %s',
320 self._server.get_version())
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700321
322
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800323 def is_localhost(self):
324 """Is the servod hosted locally?
325
326 Returns:
327 True if local hosted; otherwise, False.
328 """
Fang Deng5d518f42013-08-02 14:04:32 -0700329 return self._servo_host.is_localhost()
Tom Wai-Hong Tam1c86c7a2012-10-22 10:08:24 +0800330
331
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700332 def power_long_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700333 """Simulate a long power button press."""
J. Richard Barnettef12bff22012-07-18 14:41:06 -0700334 # After a long power press, the EC may ignore the next power
335 # button press (at least on Alex). To guarantee that this
336 # won't happen, we need to allow the EC one second to
337 # collect itself.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800338 self._server.power_long_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700339
340
341 def power_normal_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700342 """Simulate a normal power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800343 self._server.power_normal_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700344
345
346 def power_short_press(self):
Chrome Bot9a1137d2011-07-19 14:35:00 -0700347 """Simulate a short power button press."""
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800348 self._server.power_short_press()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700349
350
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800351 def power_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700352 """Simulate a power button press.
353
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800354 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700355 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800356 self._server.power_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700357
358
359 def lid_open(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800360 """Simulate opening the lid and raise exception if all attempts fail"""
361 self.set('lid_open', 'yes')
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700362
363
364 def lid_close(self):
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800365 """Simulate closing the lid and raise exception if all attempts fail
Craig Harrison48997262011-06-27 14:31:10 -0700366
367 Waits 6 seconds to ensure the device is fully asleep before returning.
368 """
Yuli Huang7b82dcf2015-05-13 17:58:52 +0800369 self.set('lid_open', 'no')
Chrome Bot9a1137d2011-07-19 14:35:00 -0700370 time.sleep(Servo.SLEEP_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700371
Shelley Chenc26575a2015-09-18 10:56:16 -0700372 def volume_up(self, timeout=300):
Kevin Chenge448a8b2016-09-09 10:18:11 -0700373 """Simulate pushing the volume down button.
374
375 @param timeout: Timeout for setting the volume.
376 """
Shelley Chenc26575a2015-09-18 10:56:16 -0700377 self.set_get_all(['volume_up:yes',
378 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY,
379 'volume_up:no'])
380 # we need to wait for commands to take effect before moving on
381 time_left = float(timeout)
382 while time_left > 0.0:
383 value = self.get('volume_up')
384 if value == 'no':
385 return
386 time.sleep(self.SHORT_DELAY)
387 time_left = time_left - self.SHORT_DELAY
388 raise error.TestFail("Failed setting volume_up to no")
389
390 def volume_down(self, timeout=300):
Kevin Chenge448a8b2016-09-09 10:18:11 -0700391 """Simulate pushing the volume down button.
392
393 @param timeout: Timeout for setting the volume.
394 """
Shelley Chenc26575a2015-09-18 10:56:16 -0700395 self.set_get_all(['volume_down:yes',
396 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY,
397 'volume_down:no'])
398 # we need to wait for commands to take effect before moving on
399 time_left = float(timeout)
400 while time_left > 0.0:
401 value = self.get('volume_down')
402 if value == 'no':
403 return
404 time.sleep(self.SHORT_DELAY)
405 time_left = time_left - self.SHORT_DELAY
406 raise error.TestFail("Failed setting volume_down to no")
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700407
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800408 def ctrl_d(self, press_secs=''):
409 """Simulate Ctrl-d simultaneous button presses.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800410
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800411 @param press_secs : Str. Time to press key.
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800412 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800413 self._server.ctrl_d(press_secs)
Gediminas Ramanauskas9da80f22012-11-14 12:59:43 -0800414
Gediminas Ramanauskasba352ad2012-11-09 09:43:32 -0800415
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800416 def ctrl_u(self):
417 """Simulate Ctrl-u simultaneous button presses.
418
419 @param press_secs : Str. Time to press key.
420 """
421 self._server.ctrl_u()
Todd Broch9dfc3a82011-11-01 08:09:28 -0700422
423
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800424 def ctrl_enter(self, press_secs=''):
425 """Simulate Ctrl-enter simultaneous button presses.
426
427 @param press_secs : Str. Time to press key.
428 """
429 self._server.ctrl_enter(press_secs)
Gediminas Ramanauskas3297d4f2012-09-10 15:30:10 -0700430
431
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800432 def d_key(self, press_secs=''):
433 """Simulate Enter key button press.
434
435 @param press_secs : Str. Time to press key.
436 """
437 self._server.d_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700438
439
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800440 def ctrl_key(self, press_secs=''):
441 """Simulate Enter key button press.
442
443 @param press_secs : Str. Time to press key.
444 """
445 self._server.ctrl_key(press_secs)
Todd Broch9753bd42012-03-21 10:15:08 -0700446
447
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800448 def enter_key(self, press_secs=''):
449 """Simulate Enter key button press.
450
451 @param press_secs : Str. Time to press key.
452 """
453 self._server.enter_key(press_secs)
Todd Broch9dfc3a82011-11-01 08:09:28 -0700454
455
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800456 def refresh_key(self, press_secs=''):
457 """Simulate Refresh key (F3) button press.
458
459 @param press_secs : Str. Time to press key.
460 """
461 self._server.refresh_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700462
463
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800464 def ctrl_refresh_key(self, press_secs=''):
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800465 """Simulate Ctrl and Refresh (F3) simultaneous press.
466
467 This key combination is an alternative of Space key.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800468
469 @param press_secs : Str. Time to press key.
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800470 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800471 self._server.ctrl_refresh_key(press_secs)
Tom Wai-Hong Tam9e61e662012-08-01 15:10:07 +0800472
473
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800474 def imaginary_key(self, press_secs=''):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700475 """Simulate imaginary key button press.
476
477 Maps to a key that doesn't physically exist.
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800478
479 @param press_secs : Str. Time to press key.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700480 """
Yusuf Mohsinally103441a2014-01-14 15:25:44 -0800481 self._server.imaginary_key(press_secs)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700482
483
Vincent Palatine7dc9282016-07-14 11:31:58 +0200484 def sysrq_x(self, press_secs=''):
485 """Simulate Alt VolumeUp X simulataneous press.
486
487 This key combination is the kernel system request (sysrq) X.
488
489 @param press_secs : Str. Time to press key.
490 """
491 self._server.sysrq_x(press_secs)
492
493
Tom Wai-Hong Tam93b5c092015-05-14 02:50:43 +0800494 def toggle_recovery_switch(self):
495 """Toggle recovery switch on and off."""
496 self.enable_recovery_mode()
497 time.sleep(self.REC_TOGGLE_DELAY)
498 self.disable_recovery_mode()
499
500
Craig Harrison6b36b122011-06-28 17:58:43 -0700501 def enable_recovery_mode(self):
502 """Enable recovery mode on device."""
503 self.set('rec_mode', 'on')
504
505
506 def disable_recovery_mode(self):
507 """Disable recovery mode on device."""
508 self.set('rec_mode', 'off')
509
510
Tom Wai-Hong Tamf9ded092015-05-20 05:37:22 +0800511 def toggle_development_switch(self):
512 """Toggle development switch on and off."""
513 self.enable_development_mode()
514 time.sleep(self.DEV_TOGGLE_DELAY)
515 self.disable_development_mode()
516
517
Craig Harrison6b36b122011-06-28 17:58:43 -0700518 def enable_development_mode(self):
519 """Enable development mode on device."""
520 self.set('dev_mode', 'on')
521
522
523 def disable_development_mode(self):
524 """Disable development mode on device."""
525 self.set('dev_mode', 'off')
526
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700527 def boot_devmode(self):
528 """Boot a dev-mode device that is powered off."""
Tom Wai-Hong Tam23869102012-01-17 15:41:30 +0800529 self.power_short_press()
Craig Harrison48997262011-06-27 14:31:10 -0700530 self.pass_devmode()
531
532
533 def pass_devmode(self):
534 """Pass through boot screens in dev-mode."""
Chrome Bot9a1137d2011-07-19 14:35:00 -0700535 time.sleep(Servo.BOOT_DELAY)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700536 self.ctrl_d()
Chrome Bot9a1137d2011-07-19 14:35:00 -0700537 time.sleep(Servo.BOOT_DELAY)
Craig Harrison48997262011-06-27 14:31:10 -0700538
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700539
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800540 def get_board(self):
Wai-Hong Tam0f904002017-09-19 15:52:22 -0700541 """Get the board connected to servod."""
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800542 return self._server.get_board()
543
544
Wai-Hong Tam0f904002017-09-19 15:52:22 -0700545 def get_base_board(self):
546 """Get the board of the base connected to servod."""
Wai-Hong Tam7f6169e2017-09-29 09:23:48 -0700547 try:
548 return self._server.get_base_board()
549 except xmlrpclib.Fault as e:
550 # TODO(waihong): Remove the following compatibility check when
551 # the new versions of hdctools are deployed.
552 if 'not supported' in str(e):
553 logging.warning('The servod is too old that get_base_board '
554 'not supported.')
555 return ''
556 raise
Wai-Hong Tam0f904002017-09-19 15:52:22 -0700557
558
Wai-Hong Tamcc399ee2017-12-08 12:43:28 -0800559 def get_ec_active_copy(self):
560 """Get the active copy of the EC image."""
561 return self.get('ec_active_copy')
562
563
Todd Brochefe72cb2012-07-11 19:58:53 -0700564 def _get_xmlrpclib_exception(self, xmlexc):
565 """Get meaningful exception string from xmlrpc.
566
567 Args:
568 xmlexc: xmlrpclib.Fault object
569
570 xmlrpclib.Fault.faultString has the following format:
571
572 <type 'exception type'>:'actual error message'
573
574 Parse and return the real exception from the servod side instead of the
575 less meaningful one like,
576 <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no
577 attribute 'hw_driver'">
578
579 Returns:
580 string of underlying exception raised in servod.
581 """
582 return re.sub('^.*>:', '', xmlexc.faultString)
583
584
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700585 def get(self, gpio_name):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700586 """Get the value of a gpio from Servod.
587
588 @param gpio_name Name of the gpio.
589 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700590 assert gpio_name
Todd Brochefe72cb2012-07-11 19:58:53 -0700591 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700592 return self._server.get(gpio_name)
Todd Brochefe72cb2012-07-11 19:58:53 -0700593 except xmlrpclib.Fault as e:
594 err_msg = "Getting '%s' :: %s" % \
595 (gpio_name, self._get_xmlrpclib_exception(e))
596 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700597
598
599 def set(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700600 """Set and check the value of a gpio using Servod.
601
602 @param gpio_name Name of the gpio.
603 @param gpio_value New setting for the gpio.
604 """
Chrome Bot9a1137d2011-07-19 14:35:00 -0700605 self.set_nocheck(gpio_name, gpio_value)
Todd Brochcf7c6652012-02-24 13:03:59 -0800606 retry_count = Servo.GET_RETRY_MAX
607 while gpio_value != self.get(gpio_name) and retry_count:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700608 logging.warning("%s != %s, retry %d", gpio_name, gpio_value,
Todd Brochcf7c6652012-02-24 13:03:59 -0800609 retry_count)
610 retry_count -= 1
611 time.sleep(Servo.SHORT_DELAY)
612 if not retry_count:
613 assert gpio_value == self.get(gpio_name), \
614 'Servo failed to set %s to %s' % (gpio_name, gpio_value)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700615
616
617 def set_nocheck(self, gpio_name, gpio_value):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700618 """Set the value of a gpio using Servod.
619
620 @param gpio_name Name of the gpio.
621 @param gpio_value New setting for the gpio.
622 """
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700623 assert gpio_name and gpio_value
Mary Ruthven2f72e2a2018-05-01 17:12:58 -0700624 logging.info('Setting %s to %r', gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700625 try:
Simran Basi1cbc5f22013-07-17 14:23:58 -0700626 self._server.set(gpio_name, gpio_value)
Todd Brochefe72cb2012-07-11 19:58:53 -0700627 except xmlrpclib.Fault as e:
Mary Ruthven73d3a352018-05-10 15:35:20 -0700628 err_msg = "Setting '%s' to %r :: %s" % \
Todd Brochefe72cb2012-07-11 19:58:53 -0700629 (gpio_name, gpio_value, self._get_xmlrpclib_exception(e))
630 raise error.TestFail(err_msg)
Craig Harrison2b6c6fc2011-06-23 10:34:02 -0700631
632
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800633 def set_get_all(self, controls):
634 """Set &| get one or more control values.
635
636 @param controls: list of strings, controls to set &| get.
637
638 @raise: error.TestError in case error occurs setting/getting values.
639 """
640 rv = []
641 try:
Vic Yangcad9acb2013-05-21 14:02:05 +0800642 logging.info('Set/get all: %s', str(controls))
Tom Wai-Hong Tam92569bb2013-03-26 14:25:10 +0800643 rv = self._server.set_get_all(controls)
644 except xmlrpclib.Fault as e:
645 # TODO(waihong): Remove the following backward compatibility when
646 # the new versions of hdctools are deployed.
647 if 'not supported' in str(e):
648 logging.warning('The servod is too old that set_get_all '
649 'not supported. Use set and get instead.')
650 for control in controls:
651 if ':' in control:
652 (name, value) = control.split(':')
653 if name == 'sleep':
654 time.sleep(float(value))
655 else:
656 self.set_nocheck(name, value)
657 rv.append(True)
658 else:
659 rv.append(self.get(name))
660 else:
661 err_msg = "Problem with '%s' :: %s" % \
662 (controls, self._get_xmlrpclib_exception(e))
663 raise error.TestFail(err_msg)
664 return rv
665
666
Jon Salzc88e5b62011-11-30 14:38:54 +0800667 # TODO(waihong) It may fail if multiple servo's are connected to the same
668 # host. Should look for a better way, like the USB serial name, to identify
669 # the USB device.
Simran Basi741b5d42012-05-18 11:27:15 -0700670 # TODO(sbasi) Remove this code from autoserv once firmware tests have been
671 # updated.
Kevin Chenga22c4a82016-10-07 14:13:25 -0700672 def probe_host_usb_dev(self, timeout=_USB_PROBE_TIMEOUT):
Jon Salzc88e5b62011-11-30 14:38:54 +0800673 """Probe the USB disk device plugged-in the servo from the host side.
674
Kevin Chengeb06fe72016-08-22 15:26:32 -0700675 It uses servod to discover if there is a usb device attached to it.
Jon Salzc88e5b62011-11-30 14:38:54 +0800676
Kevin Chenga22c4a82016-10-07 14:13:25 -0700677 @param timeout The timeout period when probing for the usb host device.
678
679 @return: String of USB disk path (e.g. '/dev/sdb') or None.
Jon Salzc88e5b62011-11-30 14:38:54 +0800680 """
Ruben Rodriguez Buchillon9a4bfc32018-10-16 20:46:25 +0800681 # Set up Servo's usb mux.
682 self.switch_usbkey('host')
Kevin Chenga22c4a82016-10-07 14:13:25 -0700683 return self._server.probe_host_usb_dev(timeout) or None
Jon Salzc88e5b62011-11-30 14:38:54 +0800684
685
Mike Truty49153d82012-08-21 22:27:30 -0500686 def image_to_servo_usb(self, image_path=None,
687 make_image_noninteractive=False):
688 """Install an image to the USB key plugged into the servo.
689
690 This method may copy any image to the servo USB key including a
691 recovery image or a test image. These images are frequently used
692 for test purposes such as restoring a corrupted image or conducting
693 an upgrade of ec/fw/kernel as part of a test of a specific image part.
694
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700695 @param image_path Path on the host to the recovery image.
696 @param make_image_noninteractive Make the recovery image
697 noninteractive, therefore the DUT
698 will reboot automatically after
699 installation.
Mike Truty49153d82012-08-21 22:27:30 -0500700 """
J. Richard Barnette41320ee2013-03-11 13:00:13 -0700701 # We're about to start plugging/unplugging the USB key. We
702 # don't know the state of the DUT, or what it might choose
703 # to do to the device after hotplug. To avoid surprises,
704 # force the DUT to be off.
705 self._server.hwinit()
706 self._power_state.power_off()
Mike Truty49153d82012-08-21 22:27:30 -0500707
708 # Set up Servo's usb mux.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800709 self.switch_usbkey('host')
Mike Truty49153d82012-08-21 22:27:30 -0500710 if image_path:
Tom Wai-Hong Tam42f136d2012-10-26 11:11:23 +0800711 logging.info('Searching for usb device and copying image to it. '
712 'Please wait a few minutes...')
Mike Truty49153d82012-08-21 22:27:30 -0500713 if not self._server.download_image_to_usb(image_path):
714 logging.error('Failed to transfer requested image to USB. '
715 'Please take a look at Servo Logs.')
716 raise error.AutotestError('Download image to usb failed.')
717 if make_image_noninteractive:
718 logging.info('Making image noninteractive')
719 if not self._server.make_image_noninteractive():
720 logging.error('Failed to make image noninteractive. '
721 'Please take a look at Servo Logs.')
722
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700723 def boot_in_recovery_mode(self):
724 """Boot host DUT in recovery mode."""
725 self._power_state.power_on(rec_mode=self._power_state.REC_ON)
726 self.switch_usbkey('dut')
727
Mike Truty49153d82012-08-21 22:27:30 -0500728
Simran Basi741b5d42012-05-18 11:27:15 -0700729 def install_recovery_image(self, image_path=None,
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800730 make_image_noninteractive=False):
Dan Shic67f1332016-04-06 12:38:06 -0700731 """Install the recovery image specified by the path onto the DUT.
Jon Salzc88e5b62011-11-30 14:38:54 +0800732
733 This method uses google recovery mode to install a recovery image
734 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 +0800735 board specified by the usb_dev. If no image path is specified
Jon Salzc88e5b62011-11-30 14:38:54 +0800736 we use the recovery image already on the usb image.
737
Dan Shic67f1332016-04-06 12:38:06 -0700738 @param image_path: Path on the host to the recovery image.
739 @param make_image_noninteractive: Make the recovery image
740 noninteractive, therefore the DUT will reboot automatically
741 after installation.
Jon Salzc88e5b62011-11-30 14:38:54 +0800742 """
Mike Truty49153d82012-08-21 22:27:30 -0500743 self.image_to_servo_usb(image_path, make_image_noninteractive)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700744 self.boot_in_recovery_mode()
Jon Salzc88e5b62011-11-30 14:38:54 +0800745
746
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800747 def _scp_image(self, image_path):
748 """Copy image to the servo host.
749
750 When programming a firmware image on the DUT, the image must be
751 located on the host to which the servo device is connected. Sometimes
752 servo is controlled by a remote host, in this case the image needs to
753 be transferred to the remote host.
754
755 @param image_path: a string, name of the firmware image file to be
756 transferred.
757 @return: a string, full path name of the copied file on the remote.
758 """
759
760 dest_path = os.path.join('/tmp', os.path.basename(image_path))
Fang Deng5d518f42013-08-02 14:04:32 -0700761 self._servo_host.send_file(image_path, dest_path)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800762 return dest_path
763
764
Dan Shifecdaf42015-07-28 10:17:26 -0700765 def system(self, command, timeout=3600):
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700766 """Execute the passed in command on the servod host.
767
768 @param command Command to be executed.
Dan Shifecdaf42015-07-28 10:17:26 -0700769 @param timeout Maximum number of seconds of runtime allowed. Default to
770 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700771 """
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800772 logging.info('Will execute on servo host: %s', command)
Fang Deng5d518f42013-08-02 14:04:32 -0700773 self._servo_host.run(command, timeout=timeout)
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800774
775
Dan Shifecdaf42015-07-28 10:17:26 -0700776 def system_output(self, command, timeout=3600,
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800777 ignore_status=False, args=()):
778 """Execute the passed in command on the servod host, return stdout.
779
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700780 @param command a string, the command to execute
781 @param timeout an int, max number of seconds to wait til command
Dan Shifecdaf42015-07-28 10:17:26 -0700782 execution completes. Default to 1 hour.
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700783 @param ignore_status a Boolean, if true - ignore command's nonzero exit
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800784 status, otherwise an exception will be thrown
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700785 @param args a tuple of strings, each becoming a separate command line
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800786 parameter for the command
J. Richard Barnettecdd1edf2015-07-24 11:39:46 -0700787 @return command's stdout as a string.
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800788 """
Fang Deng5d518f42013-08-02 14:04:32 -0700789 return self._servo_host.run(command, timeout=timeout,
790 ignore_status=ignore_status,
791 args=args).stdout.strip()
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800792
793
Dan Shia5fef052015-05-18 23:28:47 -0700794 def get_servo_version(self):
795 """Get the version of the servo, e.g., servo_v2 or servo_v3.
796
797 @return: The version of the servo.
798
799 """
800 return self._server.get_version()
801
802
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800803 def _initialize_programmer(self, rw_only=False):
804 """Initialize the firmware programmer.
805
806 @param rw_only: True to initialize a programmer which only
807 programs the RW portions.
808 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700809 if self._programmer:
810 return
811 # Initialize firmware programmer
Dan Shia5fef052015-05-18 23:28:47 -0700812 servo_version = self.get_servo_version()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700813 if servo_version.startswith('servo_v2'):
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700814 self._programmer = firmware_programmer.ProgrammerV2(self)
Wai-Hong Tamc36c4d22016-09-09 10:39:45 -0700815 self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self)
Kevin Chengdf2e29f2016-09-09 02:31:22 -0700816 # Both servo v3 and v4 use the same programming methods so just leverage
817 # ProgrammerV3 for servo v4 as well.
818 elif (servo_version.startswith('servo_v3') or
819 servo_version.startswith('servo_v4')):
Dan Shia5fef052015-05-18 23:28:47 -0700820 self._programmer = firmware_programmer.ProgrammerV3(self)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800821 self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self)
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700822 else:
823 raise error.TestError(
824 'No firmware programmer for servo version: %s' %
Congbin Guo42427612019-02-12 10:22:06 -0800825 servo_version)
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700826
827
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800828 def program_bios(self, image, rw_only=False):
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800829 """Program bios on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800830
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800831 @param image: a string, file name of the BIOS image to program
832 on the DUT.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800833 @param rw_only: True to only program the RW portion of BIOS.
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800834
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800835 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700836 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800837 if not self.is_localhost():
838 image = self._scp_image(image)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800839 if rw_only:
840 self._programmer_rw.program_bios(image)
841 else:
842 self._programmer.program_bios(image)
Vadim Bendeburyb80ba592012-12-07 15:02:34 -0800843
844
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800845 def program_ec(self, image, rw_only=False):
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800846 """Program ec on DUT with given image.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800847
Yusuf Mohsinally6c078ae2013-11-21 11:06:42 -0800848 @param image: a string, file name of the EC image to program
849 on the DUT.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800850 @param rw_only: True to only program the RW portion of EC.
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800851
Vadim Bendebury1a7ec632013-02-17 16:22:09 -0800852 """
J. Richard Barnette8f19b392014-08-11 14:05:39 -0700853 self._initialize_programmer()
Ricky Liangc31aab32014-07-03 16:23:29 +0800854 if not self.is_localhost():
855 image = self._scp_image(image)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800856 if rw_only:
Congbin Guo42427612019-02-12 10:22:06 -0800857 self._programmer_rw.program_ec(image)
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800858 else:
Congbin Guo42427612019-02-12 10:22:06 -0800859 self._programmer.program_ec(image)
860
861
862 def _reprogram(self, tarball_path, firmware_name, image_candidates,
863 rw_only):
864 """Helper function to reprogram firmware for EC or BIOS.
865
866 @param tarball_path: The path of the downloaded build tarball.
867 @param: firmware_name: either 'EC' or 'BIOS'.
868 @param image_candidates: A tuple of the paths of image candidates.
869 @param rw_only: True to only install firmware to its RW portions. Keep
870 the RO portions unchanged.
871
872 @raise: TestError if cannot extract firmware from the tarball.
873 """
874 dest_dir = os.path.dirname(tarball_path)
875 image = _extract_image_from_tarball(tarball_path, dest_dir,
876 image_candidates)
877 if not image:
878 if firmware_name == 'EC':
879 logging.info('Not a Chrome EC, ignore re-programming it')
880 return
881 else:
882 raise error.TestError('Failed to extract the %s image from '
883 'tarball' % firmware_name)
884
885 logging.info('Will re-program %s %snow', firmware_name,
886 'RW ' if rw_only else '')
887
888 if firmware_name == 'EC':
889 self.program_ec(os.path.join(dest_dir, image), rw_only)
890 else:
891 self.program_bios(os.path.join(dest_dir, image), rw_only)
892
893
894 def program_firmware(self, model, tarball_path, rw_only=False):
895 """Program firmware (EC, if applied, and BIOS) of the DUT.
896
897 @param model: The DUT model name.
898 @param tarball_path: The path of the downloaded build tarball.
899 @param rw_only: True to only install firmware to its RW portions. Keep
900 the RO portions unchanged.
901 """
902 ap_image_candidates = ('image.bin', 'image-%s.bin' % model)
903 ec_image_candidates = ('ec.bin', '%s/ec.bin' % model)
904
905 self._reprogram(tarball_path, 'EC', ec_image_candidates, rw_only)
906 self._reprogram(tarball_path, 'BIOS', ap_image_candidates, rw_only)
907
908 self.get_power_state_controller().reset()
909 time.sleep(Servo.BOOT_DELAY)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800910
Fang Dengafb88142013-05-30 17:44:31 -0700911
912 def _switch_usbkey_power(self, power_state, detection_delay=False):
913 """Switch usbkey power.
914
915 This function switches usbkey power by setting the value of
916 'prtctl4_pwren'. If the power is already in the
917 requested state, this function simply returns.
918
919 @param power_state: A string, 'on' or 'off'.
920 @param detection_delay: A boolean value, if True, sleep
921 for |USB_DETECTION_DELAY| after switching
922 the power on.
923 """
Kevin Chenga22c4a82016-10-07 14:13:25 -0700924 # TODO(kevcheng): Forgive me for this terrible hack. This is just to
925 # handle beaglebones that haven't yet updated and have the
926 # safe_switch_usbkey_power RPC. I'll remove this once all beaglebones
927 # have been updated and also think about a better way to handle
928 # situations like this.
929 try:
930 self._server.safe_switch_usbkey_power(power_state)
931 except Exception:
932 self.set('prtctl4_pwren', power_state)
Fang Dengafb88142013-05-30 17:44:31 -0700933 if power_state == 'off':
934 time.sleep(self.USB_POWEROFF_DELAY)
935 elif detection_delay:
936 time.sleep(self.USB_DETECTION_DELAY)
937
938
939 def switch_usbkey(self, usb_state):
940 """Connect USB flash stick to either host or DUT, or turn USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800941
942 This function switches the servo multiplexer to provide electrical
Fang Dengafb88142013-05-30 17:44:31 -0700943 connection between the USB port J3 and either host or DUT side. It
944 can also be used to turn the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800945
Fang Dengafb88142013-05-30 17:44:31 -0700946 Switching to 'dut' or 'host' is accompanied by powercycling
947 of the USB stick, because it sometimes gets wedged if the mux
948 is switched while the stick power is on.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800949
Fang Dengafb88142013-05-30 17:44:31 -0700950 @param usb_state: A string, one of 'dut', 'host', or 'off'.
951 'dut' and 'host' indicate which side the
952 USB flash device is required to be connected to.
953 'off' indicates turning the USB port off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800954
Fang Dengafb88142013-05-30 17:44:31 -0700955 @raise: error.TestError in case the parameter is not 'dut'
956 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800957 """
Fang Dengafb88142013-05-30 17:44:31 -0700958 if self.get_usbkey_direction() == usb_state:
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800959 return
960
Fang Dengafb88142013-05-30 17:44:31 -0700961 if usb_state == 'off':
Dan Shia5fef052015-05-18 23:28:47 -0700962 self._switch_usbkey_power('off')
963 self._usb_state = usb_state
964 return
Fang Dengafb88142013-05-30 17:44:31 -0700965 elif usb_state == 'host':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800966 mux_direction = 'servo_sees_usbkey'
Fang Dengafb88142013-05-30 17:44:31 -0700967 elif usb_state == 'dut':
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800968 mux_direction = 'dut_sees_usbkey'
969 else:
Fang Dengafb88142013-05-30 17:44:31 -0700970 raise error.TestError('Unknown USB state request: %s' % usb_state)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800971
Fang Dengafb88142013-05-30 17:44:31 -0700972 self._switch_usbkey_power('off')
Kevin Chenga22c4a82016-10-07 14:13:25 -0700973 # TODO(kevcheng): Forgive me for this terrible hack. This is just to
974 # handle beaglebones that haven't yet updated and have the
975 # safe_switch_usbkey RPC. I'll remove this once all beaglebones have
976 # been updated and also think about a better way to handle situations
977 # like this.
978 try:
979 self._server.safe_switch_usbkey(mux_direction)
980 except Exception:
981 self.set('usb_mux_sel1', mux_direction)
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800982 time.sleep(self.USB_POWEROFF_DELAY)
Fang Dengafb88142013-05-30 17:44:31 -0700983 self._switch_usbkey_power('on', usb_state == 'host')
984 self._usb_state = usb_state
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800985
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800986
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800987 def get_usbkey_direction(self):
Fang Dengafb88142013-05-30 17:44:31 -0700988 """Get which side USB is connected to or 'off' if usb power is off.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800989
Fang Dengafb88142013-05-30 17:44:31 -0700990 @return: A string, one of 'dut', 'host', or 'off'.
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800991 """
Fang Dengafb88142013-05-30 17:44:31 -0700992 if not self._usb_state:
993 if self.get('prtctl4_pwren') == 'off':
994 self._usb_state = 'off'
995 elif self.get('usb_mux_sel1').startswith('dut'):
996 self._usb_state = 'dut'
Vadim Bendeburye7bd9362012-12-19 14:35:20 -0800997 else:
Fang Dengafb88142013-05-30 17:44:31 -0700998 self._usb_state = 'host'
999 return self._usb_state
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001000
1001
Wai-Hong Tam60377262018-03-01 10:55:39 -08001002 def set_servo_v4_role(self, role):
1003 """Set the power role of servo v4, either 'src' or 'snk'.
1004
1005 It does nothing if not a servo v4.
1006
1007 @param role: Power role for DUT port on servo v4, either 'src' or 'snk'.
1008 """
1009 servo_version = self.get_servo_version()
1010 if servo_version.startswith('servo_v4'):
1011 value = self.get('servo_v4_role')
1012 if value != role:
1013 self.set_nocheck('servo_v4_role', role)
1014 else:
1015 logging.debug('Already in the role: %s.', role)
1016 else:
1017 logging.debug('Not a servo v4, unable to set role to %s.', role)
1018
1019
Congbin Guofc3b8962019-03-22 17:38:46 -07001020 @property
1021 def uart_logs_dir(self):
1022 """Return the directory to save UART logs."""
1023 return self._uart.logs_dir if self._uart else ""
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001024
Congbin Guofc3b8962019-03-22 17:38:46 -07001025
1026 @uart_logs_dir.setter
1027 def uart_logs_dir(self, logs_dir):
1028 """Set directory to save UART logs.
1029
1030 @param logs_dir String of directory name."""
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001031 if self._uart:
Congbin Guofc3b8962019-03-22 17:38:46 -07001032 self._uart.logs_dir = logs_dir
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001033
1034
1035 def close(self):
1036 """Close the servo object."""
1037 if self._uart:
1038 self._uart.stop_capture()
Congbin Guofc3b8962019-03-22 17:38:46 -07001039 self._uart.dump()
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001040 self._uart = None