J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 2 | # 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 Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 8 | import ast |
| 9 | import logging |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 10 | import os |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 11 | import re |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame^] | 12 | import socket |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 13 | import time |
| 14 | import xmlrpclib |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 15 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import error |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import lsbrelease_utils |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 18 | from autotest_lib.server import utils as server_utils |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 19 | from autotest_lib.server.cros.servo import firmware_programmer |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 20 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 21 | # Time to wait when probing for a usb device, it takes on avg 17 seconds |
| 22 | # to do a full probe. |
| 23 | _USB_PROBE_TIMEOUT = 40 |
| 24 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 25 | |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 26 | def _extract_image_from_tarball(tarball, dest_dir, image_candidates): |
| 27 | """Try extracting the image_candidates from the tarball. |
| 28 | |
| 29 | @param tarball: The path of the tarball. |
| 30 | @param dest_path: The path of the destination. |
| 31 | @param image_candidates: A tuple of the paths of image candidates. |
| 32 | |
| 33 | @return: The first path from the image candidates, which succeeds, or None |
| 34 | if all the image candidates fail. |
| 35 | """ |
| 36 | for image in image_candidates: |
| 37 | status = server_utils.system( |
| 38 | ('tar xf %s -C %s %s' % (tarball, dest_dir, image)), |
| 39 | timeout=60, ignore_status=True) |
| 40 | if status == 0: |
| 41 | return image |
| 42 | return None |
| 43 | |
| 44 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 45 | class _PowerStateController(object): |
| 46 | |
| 47 | """Class to provide board-specific power operations. |
| 48 | |
| 49 | This class is responsible for "power on" and "power off" |
| 50 | operations that can operate without making assumptions in |
| 51 | advance about board state. It offers an interface that |
| 52 | abstracts out the different sequences required for different |
| 53 | board types. |
| 54 | |
| 55 | """ |
| 56 | |
| 57 | # Constants acceptable to be passed for the `rec_mode` parameter |
| 58 | # to power_on(). |
| 59 | # |
| 60 | # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or |
| 61 | # SD card. |
| 62 | # REC_OFF: Boot in normal mode, i.e. boot from internal storage. |
| 63 | |
| 64 | REC_ON = 'rec' |
| 65 | REC_OFF = 'on' |
Shelley Chen | 6593862 | 2018-05-16 07:45:54 -0700 | [diff] [blame] | 66 | REC_ON_FORCE_MRC = 'rec_force_mrc' |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 67 | |
| 68 | # Delay in seconds needed between asserting and de-asserting |
| 69 | # warm reset. |
| 70 | _RESET_HOLD_TIME = 0.5 |
| 71 | |
| 72 | def __init__(self, servo): |
| 73 | """Initialize the power state control. |
| 74 | |
| 75 | @param servo Servo object providing the underlying `set` and `get` |
| 76 | methods for the target controls. |
| 77 | |
| 78 | """ |
| 79 | self._servo = servo |
| 80 | |
| 81 | def reset(self): |
| 82 | """Force the DUT to reset. |
| 83 | |
| 84 | The DUT is guaranteed to be on at the end of this call, |
| 85 | regardless of its previous state, provided that there is |
| 86 | working OS software. This also guarantees that the EC has |
| 87 | been restarted. |
| 88 | |
| 89 | """ |
| 90 | self._servo.set_nocheck('power_state', 'reset') |
| 91 | |
| 92 | def warm_reset(self): |
| 93 | """Apply warm reset to the DUT. |
| 94 | |
| 95 | This asserts, then de-asserts the 'warm_reset' signal. |
| 96 | Generally, this causes the board to restart. |
| 97 | |
| 98 | """ |
| 99 | self._servo.set_get_all(['warm_reset:on', |
| 100 | 'sleep:%.4f' % self._RESET_HOLD_TIME, |
| 101 | 'warm_reset:off']) |
| 102 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 103 | def power_off(self): |
| 104 | """Force the DUT to power off. |
| 105 | |
| 106 | The DUT is guaranteed to be off at the end of this call, |
| 107 | regardless of its previous state, provided that there is |
| 108 | working EC and boot firmware. There is no requirement for |
| 109 | working OS software. |
| 110 | |
| 111 | """ |
| 112 | self._servo.set_nocheck('power_state', 'off') |
| 113 | |
| 114 | def power_on(self, rec_mode=REC_OFF): |
| 115 | """Force the DUT to power on. |
| 116 | |
| 117 | Prior to calling this function, the DUT must be powered off, |
| 118 | e.g. with a call to `power_off()`. |
| 119 | |
| 120 | At power on, recovery mode is set as specified by the |
| 121 | corresponding argument. When booting with recovery mode on, it |
| 122 | is the caller's responsibility to unplug/plug in a bootable |
| 123 | external storage device. |
| 124 | |
| 125 | If the DUT requires a delay after powering on but before |
| 126 | processing inputs such as USB stick insertion, the delay is |
| 127 | handled by this method; the caller is not responsible for such |
| 128 | delays. |
| 129 | |
| 130 | @param rec_mode Setting of recovery mode to be applied at |
| 131 | power on. default: REC_OFF aka 'off' |
| 132 | |
| 133 | """ |
| 134 | self._servo.set_nocheck('power_state', rec_mode) |
| 135 | |
| 136 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 137 | class _Uart(object): |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 138 | """Class to capture UART streams of CPU, EC, Cr50, etc.""" |
| 139 | _UartToCapture = ('cpu', 'ec', 'cr50', 'servo_v4', 'servo_micro', 'usbpd') |
| 140 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 141 | def __init__(self, servo): |
| 142 | self._servo = servo |
| 143 | self._streams = [] |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 144 | self.logs_dir = None |
Congbin Guo | fe4d4e8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 145 | |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 146 | def _start_stop_capture(self, uart, start): |
| 147 | """Helper function to start/stop capturing on specified UART. |
| 148 | |
| 149 | @param uart: The UART name to start/stop capturing. |
| 150 | @param start: True to start capturing, otherwise stop. |
| 151 | |
| 152 | @returns True if the operation completes successfully. False if the UART |
| 153 | capturing is not supported or failed due to an error. |
| 154 | """ |
| 155 | logging.debug('%s capturing %s UART.', 'Start' if start else 'Stop', |
| 156 | uart) |
Congbin Guo | 39dc6f2 | 2019-04-23 23:01:21 +0000 | [diff] [blame] | 157 | try: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 158 | self._servo.set('%s_uart_capture' % uart, |
| 159 | 'on' if start else 'off') |
Congbin Guo | 39dc6f2 | 2019-04-23 23:01:21 +0000 | [diff] [blame] | 160 | except error.TestFail as err: |
| 161 | if 'No control named' in str(err): |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 162 | logging.debug("The servod doesn't support %s_uart_capture.", |
| 163 | uart) |
| 164 | else: |
| 165 | logging.debug("Can't caputre UART for %s: %s", uart, err) |
| 166 | return False |
| 167 | |
| 168 | return True |
| 169 | |
| 170 | def start_capture(self): |
| 171 | """Start capturing UART streams.""" |
| 172 | for uart in self._UartToCapture: |
| 173 | if self._start_stop_capture(uart, True): |
| 174 | self._streams.append(('%s_uart_stream' % uart, '%s_uart.log' % |
| 175 | uart)) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 176 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 177 | def dump(self): |
| 178 | """Dump UART streams to log files accordingly.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 179 | if not self.logs_dir: |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 180 | return |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 181 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 182 | for stream, logfile in self._streams: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 183 | logfile_fullname = os.path.join(self.logs_dir, logfile) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 184 | try: |
| 185 | content = self._servo.get(stream) |
| 186 | except Exception as err: |
| 187 | logging.warn('Failed to get UART log for %s: %s', stream, err) |
| 188 | continue |
| 189 | |
| 190 | # The UART stream may contain non-printable characters, and servo |
| 191 | # returns it in string representation. We use `ast.leteral_eval` |
| 192 | # to revert it back. |
| 193 | with open(logfile_fullname, 'a') as fd: |
| 194 | fd.write(ast.literal_eval(content)) |
| 195 | |
| 196 | def stop_capture(self): |
| 197 | """Stop capturing UART streams.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 198 | for uart in self._UartToCapture: |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 199 | try: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 200 | self._start_stop_capture(uart, False) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 201 | except Exception as err: |
| 202 | logging.warn('Failed to stop UART logging for %s: %s', uart, |
| 203 | err) |
| 204 | |
| 205 | |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 206 | class Servo(object): |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 207 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 208 | """Manages control of a Servo board. |
| 209 | |
| 210 | Servo is a board developed by hardware group to aide in the debug and |
| 211 | control of various partner devices. Servo's features include the simulation |
| 212 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 213 | class manages setting up and communicating with a servo demon (servod) |
| 214 | process. It provides both high-level functions for common servo tasks and |
| 215 | low-level functions for directly setting and reading gpios. |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 216 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 217 | """ |
| 218 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 219 | # Power button press delays in seconds. |
J. Richard Barnette | d2e4cbd | 2012-06-29 12:18:40 -0700 | [diff] [blame] | 220 | # |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 221 | # The EC specification says that 8.0 seconds should be enough |
| 222 | # for the long power press. However, some platforms need a bit |
| 223 | # more time. Empirical testing has found these requirements: |
| 224 | # Alex: 8.2 seconds |
| 225 | # ZGB: 8.5 seconds |
| 226 | # The actual value is set to the largest known necessary value. |
| 227 | # |
| 228 | # TODO(jrbarnette) Being generous is the right thing to do for |
| 229 | # existing platforms, but if this code is to be used for |
| 230 | # qualification of new hardware, we should be less generous. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 231 | SHORT_DELAY = 0.1 |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 232 | |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 233 | # Maximum number of times to re-read power button on release. |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 234 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 235 | |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 236 | # Delays to deal with DUT state transitions. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 237 | SLEEP_DELAY = 6 |
| 238 | BOOT_DELAY = 10 |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 239 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 240 | # Default minimum time interval between 'press' and 'release' |
| 241 | # keyboard events. |
Vic Yang | 0aca1c2 | 2012-11-19 18:33:56 -0800 | [diff] [blame] | 242 | SERVO_KEY_PRESS_DELAY = 0.1 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 243 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 244 | # Time to toggle recovery switch on and off. |
| 245 | REC_TOGGLE_DELAY = 0.1 |
| 246 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 247 | # Time to toggle development switch on and off. |
| 248 | DEV_TOGGLE_DELAY = 0.1 |
| 249 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 250 | # Time between an usb disk plugged-in and detected in the system. |
| 251 | USB_DETECTION_DELAY = 10 |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 252 | # Time to keep USB power off before and after USB mux direction is changed |
| 253 | USB_POWEROFF_DELAY = 2 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 254 | |
Simran Basi | b7850bb | 2013-07-24 12:33:42 -0700 | [diff] [blame] | 255 | # Time to wait before timing out on servo initialization. |
| 256 | INIT_TIMEOUT_SECS = 10 |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame] | 257 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 258 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 259 | def __init__(self, servo_host, servo_serial=None): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 260 | """Sets up the servo communication infrastructure. |
| 261 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 262 | @param servo_host: A ServoHost object representing |
| 263 | the host running servod. |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame^] | 264 | @type servo_host: autotest_lib.server.hosts.servo_host.ServoHost |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 265 | @param servo_serial: Serial number of the servo board. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 266 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 267 | # TODO(fdeng): crbug.com/298379 |
| 268 | # We should move servo_host object out of servo object |
| 269 | # to minimize the dependencies on the rest of Autotest. |
| 270 | self._servo_host = servo_host |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 271 | self._servo_serial = servo_serial |
Richard Barnette | 180efe6 | 2016-12-02 23:20:44 +0000 | [diff] [blame] | 272 | self._server = servo_host.get_servod_server_proxy() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 273 | self._power_state = _PowerStateController(self) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 274 | self._uart = _Uart(self) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 275 | self._usb_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 276 | self._programmer = None |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 277 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 278 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 279 | @property |
| 280 | def servo_serial(self): |
| 281 | """Returns the serial number of the servo board.""" |
| 282 | return self._servo_serial |
| 283 | |
| 284 | |
Tom Wai-Hong Tam | 22ee0e5 | 2013-04-03 13:36:39 +0800 | [diff] [blame] | 285 | def get_power_state_controller(self): |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 286 | """Return the power state controller for this Servo. |
| 287 | |
| 288 | The power state controller provides board-independent |
| 289 | interfaces for reset, power-on, power-off operations. |
| 290 | |
| 291 | """ |
| 292 | return self._power_state |
| 293 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 294 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 295 | def initialize_dut(self, cold_reset=False): |
| 296 | """Initializes a dut for testing purposes. |
| 297 | |
| 298 | This sets various servo signals back to default values |
| 299 | appropriate for the target board. By default, if the DUT |
| 300 | is already on, it stays on. If the DUT is powered off |
| 301 | before initialization, its state afterward is unspecified. |
| 302 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 303 | Rationale: Basic initialization of servo sets the lid open, |
| 304 | when there is a lid. This operation won't affect powered on |
| 305 | units; however, setting the lid open may power on a unit |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 306 | that's off, depending on the board type and previous state |
| 307 | of the device. |
| 308 | |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 309 | If `cold_reset` is a true value, the DUT and its EC will be |
| 310 | reset, and the DUT rebooted in normal mode. |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 311 | |
| 312 | @param cold_reset If True, cold reset the device after |
| 313 | initialization. |
| 314 | """ |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame^] | 315 | try: |
| 316 | self._server.hwinit() |
| 317 | except socket.error as e: |
| 318 | e.filename = '%s:%s' % (self._servo_host.hostname, |
| 319 | self._servo_host.servo_port) |
| 320 | raise |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 321 | self.set('usb_mux_oe1', 'on') |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 322 | self._usb_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 323 | self.switch_usbkey('off') |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 324 | self._uart.start_capture() |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 325 | if cold_reset: |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 326 | self._power_state.reset() |
J. Richard Barnette | 1777f5d | 2014-09-03 15:18:19 -0700 | [diff] [blame] | 327 | logging.debug('Servo initialized, version is %s', |
| 328 | self._server.get_version()) |
Ruben Rodriguez Buchillon | 7121b09 | 2019-04-29 12:39:34 -0700 | [diff] [blame] | 329 | try: |
Ruben Rodriguez Buchillon | 0529824 | 2019-05-20 11:29:06 -0700 | [diff] [blame] | 330 | # TODO(coconutruben): change this back to set() about a month |
| 331 | # after crrev.com/c/1586239 has been merged (or whenever that |
| 332 | # logic is in the labstation images). |
| 333 | self.set_nocheck('init_keyboard','on') |
Ruben Rodriguez Buchillon | 7121b09 | 2019-04-29 12:39:34 -0700 | [diff] [blame] | 334 | except error.TestFail as err: |
| 335 | if 'No control named' in str(err): |
| 336 | # This indicates the servod version does not |
| 337 | # have explicit keyboard initialization yet. |
| 338 | # Ignore this. |
| 339 | pass |
| 340 | else: |
| 341 | raise err |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 342 | |
| 343 | |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 344 | def is_localhost(self): |
| 345 | """Is the servod hosted locally? |
| 346 | |
| 347 | Returns: |
| 348 | True if local hosted; otherwise, False. |
| 349 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 350 | return self._servo_host.is_localhost() |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 351 | |
| 352 | |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 353 | def get_os_version(self): |
| 354 | """Returns the chromeos release version.""" |
| 355 | lsb_release_content = self.system_output('cat /etc/lsb-release', |
| 356 | ignore_status=True) |
| 357 | return lsbrelease_utils.get_chromeos_release_builder_path( |
| 358 | lsb_release_content=lsb_release_content) |
| 359 | |
| 360 | |
Mary Ruthven | 83bb595 | 2019-06-27 12:34:05 -0700 | [diff] [blame] | 361 | def get_servod_version(self): |
| 362 | """Returns the servod version.""" |
| 363 | result = self._servo_host.run('servod --version') |
| 364 | # TODO: use system_output once servod --version prints to stdout |
| 365 | stdout = result.stdout.strip() |
| 366 | return stdout if stdout else result.stderr.strip() |
| 367 | |
| 368 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 369 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 370 | """Simulate a long power button press.""" |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 371 | # After a long power press, the EC may ignore the next power |
| 372 | # button press (at least on Alex). To guarantee that this |
| 373 | # won't happen, we need to allow the EC one second to |
| 374 | # collect itself. |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 375 | # long_press is defined as 8.5s in servod |
| 376 | self.set_nocheck('power_key', 'long_press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 377 | |
| 378 | |
| 379 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 380 | """Simulate a normal power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 381 | # press is defined as 1.2s in servod |
| 382 | self.set_nocheck('power_key', 'press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 383 | |
| 384 | |
| 385 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 386 | """Simulate a short power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 387 | # tab is defined as 0.2s in servod |
| 388 | self.set_nocheck('power_key', 'tab') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 389 | |
| 390 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 391 | def power_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 392 | """Simulate a power button press. |
| 393 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 394 | @param press_secs: int, float, str; time to press key in seconds or |
| 395 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 396 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 397 | self.set_nocheck('power_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 398 | |
| 399 | |
| 400 | def lid_open(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 401 | """Simulate opening the lid and raise exception if all attempts fail""" |
| 402 | self.set('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 403 | |
| 404 | |
| 405 | def lid_close(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 406 | """Simulate closing the lid and raise exception if all attempts fail |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 407 | |
| 408 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 409 | """ |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 410 | self.set('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 411 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 412 | |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 413 | def volume_up(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 414 | """Simulate pushing the volume down button. |
| 415 | |
| 416 | @param timeout: Timeout for setting the volume. |
| 417 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 418 | self.set_get_all(['volume_up:yes', |
| 419 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 420 | 'volume_up:no']) |
| 421 | # we need to wait for commands to take effect before moving on |
| 422 | time_left = float(timeout) |
| 423 | while time_left > 0.0: |
| 424 | value = self.get('volume_up') |
| 425 | if value == 'no': |
| 426 | return |
| 427 | time.sleep(self.SHORT_DELAY) |
| 428 | time_left = time_left - self.SHORT_DELAY |
| 429 | raise error.TestFail("Failed setting volume_up to no") |
| 430 | |
| 431 | def volume_down(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 432 | """Simulate pushing the volume down button. |
| 433 | |
| 434 | @param timeout: Timeout for setting the volume. |
| 435 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 436 | self.set_get_all(['volume_down:yes', |
| 437 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 438 | 'volume_down:no']) |
| 439 | # we need to wait for commands to take effect before moving on |
| 440 | time_left = float(timeout) |
| 441 | while time_left > 0.0: |
| 442 | value = self.get('volume_down') |
| 443 | if value == 'no': |
| 444 | return |
| 445 | time.sleep(self.SHORT_DELAY) |
| 446 | time_left = time_left - self.SHORT_DELAY |
| 447 | raise error.TestFail("Failed setting volume_down to no") |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 448 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 449 | def ctrl_d(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 450 | """Simulate Ctrl-d simultaneous button presses. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 451 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 452 | @param press_secs: int, float, str; time to press key in seconds or |
| 453 | known shorthand: 'tab' 'press' 'long_press' |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 454 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 455 | self.set_nocheck('ctrl_d', press_secs) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 456 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 457 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 458 | def ctrl_u(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 459 | """Simulate Ctrl-u simultaneous button presses. |
| 460 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 461 | @param press_secs: int, float, str; time to press key in seconds or |
| 462 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 463 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 464 | self.set_nocheck('ctrl_u', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 465 | |
| 466 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 467 | def ctrl_enter(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 468 | """Simulate Ctrl-enter simultaneous button presses. |
| 469 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 470 | @param press_secs: int, float, str; time to press key in seconds or |
| 471 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 472 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 473 | self.set_nocheck('ctrl_enter', press_secs) |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 474 | |
| 475 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 476 | def ctrl_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 477 | """Simulate Enter key button press. |
| 478 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 479 | @param press_secs: int, float, str; time to press key in seconds or |
| 480 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 481 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 482 | self.set_nocheck('ctrl_key', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 483 | |
| 484 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 485 | def enter_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 486 | """Simulate Enter key button press. |
| 487 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 488 | @param press_secs: int, float, str; time to press key in seconds or |
| 489 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 490 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 491 | self.set_nocheck('enter_key', press_secs) |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 492 | |
| 493 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 494 | def refresh_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 495 | """Simulate Refresh key (F3) button press. |
| 496 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 497 | @param press_secs: int, float, str; time to press key in seconds or |
| 498 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 499 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 500 | self.set_nocheck('refresh_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 501 | |
| 502 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 503 | def ctrl_refresh_key(self, press_secs='tab'): |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 504 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 505 | |
| 506 | This key combination is an alternative of Space key. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 507 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 508 | @param press_secs: int, float, str; time to press key in seconds or |
| 509 | known shorthand: 'tab' 'press' 'long_press' |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 510 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 511 | self.set_nocheck('ctrl_refresh_key', press_secs) |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 512 | |
| 513 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 514 | def imaginary_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 515 | """Simulate imaginary key button press. |
| 516 | |
| 517 | Maps to a key that doesn't physically exist. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 518 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 519 | @param press_secs: int, float, str; time to press key in seconds or |
| 520 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 521 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 522 | self.set_nocheck('imaginary_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 523 | |
| 524 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 525 | def sysrq_x(self, press_secs='tab'): |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 526 | """Simulate Alt VolumeUp X simulataneous press. |
| 527 | |
| 528 | This key combination is the kernel system request (sysrq) X. |
| 529 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 530 | @param press_secs: int, float, str; time to press key in seconds or |
| 531 | known shorthand: 'tab' 'press' 'long_press' |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 532 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 533 | self.set_nocheck('sysrq_x', press_secs) |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 534 | |
| 535 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 536 | def toggle_recovery_switch(self): |
| 537 | """Toggle recovery switch on and off.""" |
| 538 | self.enable_recovery_mode() |
| 539 | time.sleep(self.REC_TOGGLE_DELAY) |
| 540 | self.disable_recovery_mode() |
| 541 | |
| 542 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 543 | def enable_recovery_mode(self): |
| 544 | """Enable recovery mode on device.""" |
| 545 | self.set('rec_mode', 'on') |
| 546 | |
| 547 | |
| 548 | def disable_recovery_mode(self): |
| 549 | """Disable recovery mode on device.""" |
| 550 | self.set('rec_mode', 'off') |
| 551 | |
| 552 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 553 | def toggle_development_switch(self): |
| 554 | """Toggle development switch on and off.""" |
| 555 | self.enable_development_mode() |
| 556 | time.sleep(self.DEV_TOGGLE_DELAY) |
| 557 | self.disable_development_mode() |
| 558 | |
| 559 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 560 | def enable_development_mode(self): |
| 561 | """Enable development mode on device.""" |
| 562 | self.set('dev_mode', 'on') |
| 563 | |
| 564 | |
| 565 | def disable_development_mode(self): |
| 566 | """Disable development mode on device.""" |
| 567 | self.set('dev_mode', 'off') |
| 568 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 569 | def boot_devmode(self): |
| 570 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 571 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 572 | self.pass_devmode() |
| 573 | |
| 574 | |
| 575 | def pass_devmode(self): |
| 576 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 577 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 578 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 579 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 580 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 581 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 582 | def get_board(self): |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 583 | """Get the board connected to servod.""" |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 584 | return self._server.get_board() |
| 585 | |
| 586 | |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 587 | def get_base_board(self): |
| 588 | """Get the board of the base connected to servod.""" |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame] | 589 | try: |
| 590 | return self._server.get_base_board() |
| 591 | except xmlrpclib.Fault as e: |
| 592 | # TODO(waihong): Remove the following compatibility check when |
| 593 | # the new versions of hdctools are deployed. |
| 594 | if 'not supported' in str(e): |
| 595 | logging.warning('The servod is too old that get_base_board ' |
| 596 | 'not supported.') |
| 597 | return '' |
| 598 | raise |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 599 | |
| 600 | |
Wai-Hong Tam | cc399ee | 2017-12-08 12:43:28 -0800 | [diff] [blame] | 601 | def get_ec_active_copy(self): |
| 602 | """Get the active copy of the EC image.""" |
| 603 | return self.get('ec_active_copy') |
| 604 | |
| 605 | |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 606 | def _get_xmlrpclib_exception(self, xmlexc): |
| 607 | """Get meaningful exception string from xmlrpc. |
| 608 | |
| 609 | Args: |
| 610 | xmlexc: xmlrpclib.Fault object |
| 611 | |
| 612 | xmlrpclib.Fault.faultString has the following format: |
| 613 | |
| 614 | <type 'exception type'>:'actual error message' |
| 615 | |
| 616 | Parse and return the real exception from the servod side instead of the |
| 617 | less meaningful one like, |
| 618 | <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no |
| 619 | attribute 'hw_driver'"> |
| 620 | |
| 621 | Returns: |
| 622 | string of underlying exception raised in servod. |
| 623 | """ |
| 624 | return re.sub('^.*>:', '', xmlexc.faultString) |
| 625 | |
| 626 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 627 | def get(self, gpio_name): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 628 | """Get the value of a gpio from Servod. |
| 629 | |
| 630 | @param gpio_name Name of the gpio. |
| 631 | """ |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 632 | assert gpio_name |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 633 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 634 | return self._server.get(gpio_name) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 635 | except xmlrpclib.Fault as e: |
| 636 | err_msg = "Getting '%s' :: %s" % \ |
| 637 | (gpio_name, self._get_xmlrpclib_exception(e)) |
| 638 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 639 | |
| 640 | |
| 641 | def set(self, gpio_name, gpio_value): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 642 | """Set and check the value of a gpio using Servod. |
| 643 | |
| 644 | @param gpio_name Name of the gpio. |
| 645 | @param gpio_value New setting for the gpio. |
| 646 | """ |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 647 | self.set_nocheck(gpio_name, gpio_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 648 | retry_count = Servo.GET_RETRY_MAX |
| 649 | while gpio_value != self.get(gpio_name) and retry_count: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 650 | logging.warning("%s != %s, retry %d", gpio_name, gpio_value, |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 651 | retry_count) |
| 652 | retry_count -= 1 |
| 653 | time.sleep(Servo.SHORT_DELAY) |
| 654 | if not retry_count: |
| 655 | assert gpio_value == self.get(gpio_name), \ |
| 656 | 'Servo failed to set %s to %s' % (gpio_name, gpio_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 657 | |
| 658 | |
| 659 | def set_nocheck(self, gpio_name, gpio_value): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 660 | """Set the value of a gpio using Servod. |
| 661 | |
| 662 | @param gpio_name Name of the gpio. |
| 663 | @param gpio_value New setting for the gpio. |
| 664 | """ |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 665 | assert gpio_name and gpio_value |
Mary Ruthven | dfa4534 | 2019-06-25 10:10:17 -0700 | [diff] [blame] | 666 | logging.debug('Setting %s to %r', gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 667 | try: |
Simran Basi | 1cbc5f2 | 2013-07-17 14:23:58 -0700 | [diff] [blame] | 668 | self._server.set(gpio_name, gpio_value) |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 669 | except xmlrpclib.Fault as e: |
Mary Ruthven | 73d3a35 | 2018-05-10 15:35:20 -0700 | [diff] [blame] | 670 | err_msg = "Setting '%s' to %r :: %s" % \ |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 671 | (gpio_name, gpio_value, self._get_xmlrpclib_exception(e)) |
| 672 | raise error.TestFail(err_msg) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 673 | |
| 674 | |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 675 | def set_get_all(self, controls): |
| 676 | """Set &| get one or more control values. |
| 677 | |
| 678 | @param controls: list of strings, controls to set &| get. |
| 679 | |
| 680 | @raise: error.TestError in case error occurs setting/getting values. |
| 681 | """ |
| 682 | rv = [] |
| 683 | try: |
Mary Ruthven | dfa4534 | 2019-06-25 10:10:17 -0700 | [diff] [blame] | 684 | logging.debug('Set/get all: %s', str(controls)) |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 685 | rv = self._server.set_get_all(controls) |
| 686 | except xmlrpclib.Fault as e: |
| 687 | # TODO(waihong): Remove the following backward compatibility when |
| 688 | # the new versions of hdctools are deployed. |
| 689 | if 'not supported' in str(e): |
| 690 | logging.warning('The servod is too old that set_get_all ' |
| 691 | 'not supported. Use set and get instead.') |
| 692 | for control in controls: |
| 693 | if ':' in control: |
| 694 | (name, value) = control.split(':') |
| 695 | if name == 'sleep': |
| 696 | time.sleep(float(value)) |
| 697 | else: |
| 698 | self.set_nocheck(name, value) |
| 699 | rv.append(True) |
| 700 | else: |
| 701 | rv.append(self.get(name)) |
| 702 | else: |
| 703 | err_msg = "Problem with '%s' :: %s" % \ |
| 704 | (controls, self._get_xmlrpclib_exception(e)) |
| 705 | raise error.TestFail(err_msg) |
| 706 | return rv |
| 707 | |
| 708 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 709 | # TODO(waihong) It may fail if multiple servo's are connected to the same |
| 710 | # host. Should look for a better way, like the USB serial name, to identify |
| 711 | # the USB device. |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 712 | # TODO(sbasi) Remove this code from autoserv once firmware tests have been |
| 713 | # updated. |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 714 | def probe_host_usb_dev(self, timeout=_USB_PROBE_TIMEOUT): |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 715 | """Probe the USB disk device plugged-in the servo from the host side. |
| 716 | |
Kevin Cheng | eb06fe7 | 2016-08-22 15:26:32 -0700 | [diff] [blame] | 717 | It uses servod to discover if there is a usb device attached to it. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 718 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 719 | @param timeout The timeout period when probing for the usb host device. |
| 720 | |
| 721 | @return: String of USB disk path (e.g. '/dev/sdb') or None. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 722 | """ |
Ruben Rodriguez Buchillon | 9a4bfc3 | 2018-10-16 20:46:25 +0800 | [diff] [blame] | 723 | # Set up Servo's usb mux. |
| 724 | self.switch_usbkey('host') |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 725 | return self._server.probe_host_usb_dev(timeout) or None |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 726 | |
| 727 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 728 | def image_to_servo_usb(self, image_path=None, |
| 729 | make_image_noninteractive=False): |
| 730 | """Install an image to the USB key plugged into the servo. |
| 731 | |
| 732 | This method may copy any image to the servo USB key including a |
| 733 | recovery image or a test image. These images are frequently used |
| 734 | for test purposes such as restoring a corrupted image or conducting |
| 735 | an upgrade of ec/fw/kernel as part of a test of a specific image part. |
| 736 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 737 | @param image_path Path on the host to the recovery image. |
| 738 | @param make_image_noninteractive Make the recovery image |
| 739 | noninteractive, therefore the DUT |
| 740 | will reboot automatically after |
| 741 | installation. |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 742 | """ |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 743 | # We're about to start plugging/unplugging the USB key. We |
| 744 | # don't know the state of the DUT, or what it might choose |
| 745 | # to do to the device after hotplug. To avoid surprises, |
| 746 | # force the DUT to be off. |
| 747 | self._server.hwinit() |
| 748 | self._power_state.power_off() |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 749 | |
| 750 | # Set up Servo's usb mux. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 751 | self.switch_usbkey('host') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 752 | if image_path: |
Tom Wai-Hong Tam | 42f136d | 2012-10-26 11:11:23 +0800 | [diff] [blame] | 753 | logging.info('Searching for usb device and copying image to it. ' |
| 754 | 'Please wait a few minutes...') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 755 | if not self._server.download_image_to_usb(image_path): |
| 756 | logging.error('Failed to transfer requested image to USB. ' |
| 757 | 'Please take a look at Servo Logs.') |
| 758 | raise error.AutotestError('Download image to usb failed.') |
| 759 | if make_image_noninteractive: |
| 760 | logging.info('Making image noninteractive') |
| 761 | if not self._server.make_image_noninteractive(): |
| 762 | logging.error('Failed to make image noninteractive. ' |
| 763 | 'Please take a look at Servo Logs.') |
| 764 | |
Kalin Stoyanov | b3c11f3 | 2018-05-11 09:02:00 -0700 | [diff] [blame] | 765 | def boot_in_recovery_mode(self): |
| 766 | """Boot host DUT in recovery mode.""" |
| 767 | self._power_state.power_on(rec_mode=self._power_state.REC_ON) |
| 768 | self.switch_usbkey('dut') |
| 769 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 770 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 771 | def install_recovery_image(self, image_path=None, |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 772 | make_image_noninteractive=False): |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 773 | """Install the recovery image specified by the path onto the DUT. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 774 | |
| 775 | This method uses google recovery mode to install a recovery image |
| 776 | onto a DUT through the use of a USB stick that is mounted on a servo |
Tom Wai-Hong Tam | a07115e | 2012-01-09 12:27:01 +0800 | [diff] [blame] | 777 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 778 | we use the recovery image already on the usb image. |
| 779 | |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 780 | @param image_path: Path on the host to the recovery image. |
| 781 | @param make_image_noninteractive: Make the recovery image |
| 782 | noninteractive, therefore the DUT will reboot automatically |
| 783 | after installation. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 784 | """ |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 785 | self.image_to_servo_usb(image_path, make_image_noninteractive) |
Kalin Stoyanov | b3c11f3 | 2018-05-11 09:02:00 -0700 | [diff] [blame] | 786 | self.boot_in_recovery_mode() |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 787 | |
| 788 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 789 | def _scp_image(self, image_path): |
| 790 | """Copy image to the servo host. |
| 791 | |
| 792 | When programming a firmware image on the DUT, the image must be |
| 793 | located on the host to which the servo device is connected. Sometimes |
| 794 | servo is controlled by a remote host, in this case the image needs to |
| 795 | be transferred to the remote host. |
| 796 | |
| 797 | @param image_path: a string, name of the firmware image file to be |
| 798 | transferred. |
| 799 | @return: a string, full path name of the copied file on the remote. |
| 800 | """ |
| 801 | |
| 802 | dest_path = os.path.join('/tmp', os.path.basename(image_path)) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 803 | self._servo_host.send_file(image_path, dest_path) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 804 | return dest_path |
| 805 | |
| 806 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 807 | def system(self, command, timeout=3600): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 808 | """Execute the passed in command on the servod host. |
| 809 | |
| 810 | @param command Command to be executed. |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 811 | @param timeout Maximum number of seconds of runtime allowed. Default to |
| 812 | 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 813 | """ |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 814 | logging.info('Will execute on servo host: %s', command) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 815 | self._servo_host.run(command, timeout=timeout) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 816 | |
| 817 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 818 | def system_output(self, command, timeout=3600, |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 819 | ignore_status=False, args=()): |
| 820 | """Execute the passed in command on the servod host, return stdout. |
| 821 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 822 | @param command a string, the command to execute |
| 823 | @param timeout an int, max number of seconds to wait til command |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 824 | execution completes. Default to 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 825 | @param ignore_status a Boolean, if true - ignore command's nonzero exit |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 826 | status, otherwise an exception will be thrown |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 827 | @param args a tuple of strings, each becoming a separate command line |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 828 | parameter for the command |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 829 | @return command's stdout as a string. |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 830 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 831 | return self._servo_host.run(command, timeout=timeout, |
| 832 | ignore_status=ignore_status, |
| 833 | args=args).stdout.strip() |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 834 | |
| 835 | |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 836 | def get_servo_version(self): |
| 837 | """Get the version of the servo, e.g., servo_v2 or servo_v3. |
| 838 | |
| 839 | @return: The version of the servo. |
| 840 | |
| 841 | """ |
| 842 | return self._server.get_version() |
| 843 | |
| 844 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 845 | def _initialize_programmer(self, rw_only=False): |
| 846 | """Initialize the firmware programmer. |
| 847 | |
| 848 | @param rw_only: True to initialize a programmer which only |
| 849 | programs the RW portions. |
| 850 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 851 | if self._programmer: |
| 852 | return |
| 853 | # Initialize firmware programmer |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 854 | servo_version = self.get_servo_version() |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 855 | if servo_version.startswith('servo_v2'): |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 856 | self._programmer = firmware_programmer.ProgrammerV2(self) |
Wai-Hong Tam | c36c4d2 | 2016-09-09 10:39:45 -0700 | [diff] [blame] | 857 | self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self) |
Kevin Cheng | df2e29f | 2016-09-09 02:31:22 -0700 | [diff] [blame] | 858 | # Both servo v3 and v4 use the same programming methods so just leverage |
| 859 | # ProgrammerV3 for servo v4 as well. |
| 860 | elif (servo_version.startswith('servo_v3') or |
| 861 | servo_version.startswith('servo_v4')): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 862 | self._programmer = firmware_programmer.ProgrammerV3(self) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 863 | self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 864 | else: |
| 865 | raise error.TestError( |
| 866 | 'No firmware programmer for servo version: %s' % |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 867 | servo_version) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 868 | |
| 869 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 870 | def program_bios(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 871 | """Program bios on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 872 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 873 | @param image: a string, file name of the BIOS image to program |
| 874 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 875 | @param rw_only: True to only program the RW portion of BIOS. |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 876 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 877 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 878 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 879 | if not self.is_localhost(): |
| 880 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 881 | if rw_only: |
| 882 | self._programmer_rw.program_bios(image) |
| 883 | else: |
| 884 | self._programmer.program_bios(image) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 885 | |
| 886 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 887 | def program_ec(self, image, rw_only=False): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 888 | """Program ec on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 889 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 890 | @param image: a string, file name of the EC image to program |
| 891 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 892 | @param rw_only: True to only program the RW portion of EC. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 893 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 894 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 895 | self._initialize_programmer() |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 896 | if not self.is_localhost(): |
| 897 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 898 | if rw_only: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 899 | self._programmer_rw.program_ec(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 900 | else: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 901 | self._programmer.program_ec(image) |
| 902 | |
| 903 | |
| 904 | def _reprogram(self, tarball_path, firmware_name, image_candidates, |
| 905 | rw_only): |
| 906 | """Helper function to reprogram firmware for EC or BIOS. |
| 907 | |
| 908 | @param tarball_path: The path of the downloaded build tarball. |
| 909 | @param: firmware_name: either 'EC' or 'BIOS'. |
| 910 | @param image_candidates: A tuple of the paths of image candidates. |
| 911 | @param rw_only: True to only install firmware to its RW portions. Keep |
| 912 | the RO portions unchanged. |
| 913 | |
| 914 | @raise: TestError if cannot extract firmware from the tarball. |
| 915 | """ |
| 916 | dest_dir = os.path.dirname(tarball_path) |
| 917 | image = _extract_image_from_tarball(tarball_path, dest_dir, |
| 918 | image_candidates) |
| 919 | if not image: |
| 920 | if firmware_name == 'EC': |
| 921 | logging.info('Not a Chrome EC, ignore re-programming it') |
| 922 | return |
| 923 | else: |
| 924 | raise error.TestError('Failed to extract the %s image from ' |
| 925 | 'tarball' % firmware_name) |
| 926 | |
| 927 | logging.info('Will re-program %s %snow', firmware_name, |
| 928 | 'RW ' if rw_only else '') |
| 929 | |
| 930 | if firmware_name == 'EC': |
| 931 | self.program_ec(os.path.join(dest_dir, image), rw_only) |
| 932 | else: |
| 933 | self.program_bios(os.path.join(dest_dir, image), rw_only) |
| 934 | |
| 935 | |
Shelley Chen | ac61d5a | 2019-06-24 15:35:46 -0700 | [diff] [blame] | 936 | def program_firmware(self, board, model, tarball_path, rw_only=False): |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 937 | """Program firmware (EC, if applied, and BIOS) of the DUT. |
| 938 | |
Shelley Chen | ac61d5a | 2019-06-24 15:35:46 -0700 | [diff] [blame] | 939 | @param board: The DUT board name. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 940 | @param model: The DUT model name. |
| 941 | @param tarball_path: The path of the downloaded build tarball. |
| 942 | @param rw_only: True to only install firmware to its RW portions. Keep |
| 943 | the RO portions unchanged. |
| 944 | """ |
Shelley Chen | ac61d5a | 2019-06-24 15:35:46 -0700 | [diff] [blame] | 945 | ap_image_candidates = ('image.bin', 'image-%s.bin' % model, |
| 946 | 'image-%s.bin' % board) |
| 947 | ec_image_candidates = ('ec.bin', '%s/ec.bin' % model, |
| 948 | '%s/ec.bin' % board) |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 949 | |
| 950 | self._reprogram(tarball_path, 'EC', ec_image_candidates, rw_only) |
| 951 | self._reprogram(tarball_path, 'BIOS', ap_image_candidates, rw_only) |
| 952 | |
| 953 | self.get_power_state_controller().reset() |
| 954 | time.sleep(Servo.BOOT_DELAY) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 955 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 956 | |
| 957 | def _switch_usbkey_power(self, power_state, detection_delay=False): |
| 958 | """Switch usbkey power. |
| 959 | |
| 960 | This function switches usbkey power by setting the value of |
| 961 | 'prtctl4_pwren'. If the power is already in the |
| 962 | requested state, this function simply returns. |
| 963 | |
| 964 | @param power_state: A string, 'on' or 'off'. |
| 965 | @param detection_delay: A boolean value, if True, sleep |
| 966 | for |USB_DETECTION_DELAY| after switching |
| 967 | the power on. |
| 968 | """ |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 969 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 970 | # handle beaglebones that haven't yet updated and have the |
| 971 | # safe_switch_usbkey_power RPC. I'll remove this once all beaglebones |
| 972 | # have been updated and also think about a better way to handle |
| 973 | # situations like this. |
| 974 | try: |
| 975 | self._server.safe_switch_usbkey_power(power_state) |
| 976 | except Exception: |
| 977 | self.set('prtctl4_pwren', power_state) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 978 | if power_state == 'off': |
| 979 | time.sleep(self.USB_POWEROFF_DELAY) |
| 980 | elif detection_delay: |
| 981 | time.sleep(self.USB_DETECTION_DELAY) |
| 982 | |
| 983 | |
| 984 | def switch_usbkey(self, usb_state): |
| 985 | """Connect USB flash stick to either host or DUT, or turn USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 986 | |
| 987 | This function switches the servo multiplexer to provide electrical |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 988 | connection between the USB port J3 and either host or DUT side. It |
| 989 | can also be used to turn the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 990 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 991 | Switching to 'dut' or 'host' is accompanied by powercycling |
| 992 | of the USB stick, because it sometimes gets wedged if the mux |
| 993 | is switched while the stick power is on. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 994 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 995 | @param usb_state: A string, one of 'dut', 'host', or 'off'. |
| 996 | 'dut' and 'host' indicate which side the |
| 997 | USB flash device is required to be connected to. |
| 998 | 'off' indicates turning the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 999 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1000 | @raise: error.TestError in case the parameter is not 'dut' |
| 1001 | 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1002 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1003 | if self.get_usbkey_direction() == usb_state: |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1004 | return |
| 1005 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1006 | if usb_state == 'off': |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1007 | self._switch_usbkey_power('off') |
| 1008 | self._usb_state = usb_state |
| 1009 | return |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1010 | elif usb_state == 'host': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1011 | mux_direction = 'servo_sees_usbkey' |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1012 | elif usb_state == 'dut': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1013 | mux_direction = 'dut_sees_usbkey' |
| 1014 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1015 | raise error.TestError('Unknown USB state request: %s' % usb_state) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1016 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1017 | self._switch_usbkey_power('off') |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 1018 | # TODO(kevcheng): Forgive me for this terrible hack. This is just to |
| 1019 | # handle beaglebones that haven't yet updated and have the |
| 1020 | # safe_switch_usbkey RPC. I'll remove this once all beaglebones have |
| 1021 | # been updated and also think about a better way to handle situations |
| 1022 | # like this. |
| 1023 | try: |
| 1024 | self._server.safe_switch_usbkey(mux_direction) |
| 1025 | except Exception: |
| 1026 | self.set('usb_mux_sel1', mux_direction) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1027 | time.sleep(self.USB_POWEROFF_DELAY) |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1028 | self._switch_usbkey_power('on', usb_state == 'host') |
| 1029 | self._usb_state = usb_state |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1030 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1031 | |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1032 | def get_usbkey_direction(self): |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1033 | """Get which side USB is connected to or 'off' if usb power is off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1034 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1035 | @return: A string, one of 'dut', 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1036 | """ |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1037 | if not self._usb_state: |
| 1038 | if self.get('prtctl4_pwren') == 'off': |
| 1039 | self._usb_state = 'off' |
| 1040 | elif self.get('usb_mux_sel1').startswith('dut'): |
| 1041 | self._usb_state = 'dut' |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1042 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1043 | self._usb_state = 'host' |
| 1044 | return self._usb_state |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1045 | |
| 1046 | |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1047 | def set_servo_v4_role(self, role): |
| 1048 | """Set the power role of servo v4, either 'src' or 'snk'. |
| 1049 | |
| 1050 | It does nothing if not a servo v4. |
| 1051 | |
| 1052 | @param role: Power role for DUT port on servo v4, either 'src' or 'snk'. |
| 1053 | """ |
| 1054 | servo_version = self.get_servo_version() |
| 1055 | if servo_version.startswith('servo_v4'): |
| 1056 | value = self.get('servo_v4_role') |
| 1057 | if value != role: |
| 1058 | self.set_nocheck('servo_v4_role', role) |
| 1059 | else: |
| 1060 | logging.debug('Already in the role: %s.', role) |
| 1061 | else: |
| 1062 | logging.debug('Not a servo v4, unable to set role to %s.', role) |
| 1063 | |
| 1064 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1065 | @property |
| 1066 | def uart_logs_dir(self): |
| 1067 | """Return the directory to save UART logs.""" |
| 1068 | return self._uart.logs_dir if self._uart else "" |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1069 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1070 | |
| 1071 | @uart_logs_dir.setter |
| 1072 | def uart_logs_dir(self, logs_dir): |
| 1073 | """Set directory to save UART logs. |
| 1074 | |
| 1075 | @param logs_dir String of directory name.""" |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1076 | if self._uart: |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1077 | self._uart.logs_dir = logs_dir |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1078 | |
| 1079 | |
| 1080 | def close(self): |
| 1081 | """Close the servo object.""" |
| 1082 | if self._uart: |
| 1083 | self._uart.stop_capture() |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1084 | self._uart.dump() |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1085 | self._uart = None |