Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 1 | # Lint as: python2, python3 |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | # |
| 6 | # Expects to be run in an environment with sudo and no interactive password |
| 7 | # prompt, such as within the Chromium OS development chroot. |
| 8 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 9 | import ast |
| 10 | import logging |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 11 | import os |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 12 | import re |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 13 | import six |
Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 14 | import six.moves.xmlrpc_client |
| 15 | import six.moves.http_client |
| 16 | import time |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 17 | |
Simran Basi | 741b5d4 | 2012-05-18 11:27:15 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib import error |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 19 | from autotest_lib.client.common_lib import lsbrelease_utils |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import seven |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 21 | from autotest_lib.server import utils as server_utils |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 22 | from autotest_lib.server.cros.servo import firmware_programmer |
Brent Peterson | 0781db7 | 2020-02-13 13:13:37 -0800 | [diff] [blame] | 23 | from autotest_lib.server.cros.faft.utils.config import Config as FAFTConfig |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 24 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 25 | |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 26 | # Regex to match XMLRPC errors due to a servod control not existing. |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 27 | # Servod uses both 'No control %s' and 'No control named %s' in exceptions. |
| 28 | NO_CONTROL_RE = re.compile(r'No control(?: named)? (?P<name>\w*\.?\w*)') |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 29 | |
Ruben Rodriguez Buchillon | 03fded9 | 2020-04-13 19:55:10 -0700 | [diff] [blame] | 30 | # Please see servo/drv/pty_driver.py for error messages to match. |
| 31 | |
| 32 | # This common prefix can apply to all subtypes of console errors. |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 33 | # The first portion is an optional qualifier of the type |
| 34 | # of error that occurred. Each error is or'd. |
Ruben Rodriguez Buchillon | 03fded9 | 2020-04-13 19:55:10 -0700 | [diff] [blame] | 35 | CONSOLE_COMMON_RE = (r'((Timeout waiting for response|' |
| 36 | r'Known error [\w\'\".\s]+). )?' |
| 37 | # The second portion is an optional name for the console |
| 38 | # source |
| 39 | r'(\w+\: )?') |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 40 | |
Ruben Rodriguez Buchillon | 2afbeea | 2020-02-19 15:24:20 -0800 | [diff] [blame] | 41 | # Regex to match XMLRPC errors due to a console being unresponsive. |
Ruben Rodriguez Buchillon | 03fded9 | 2020-04-13 19:55:10 -0700 | [diff] [blame] | 42 | NO_CONSOLE_OUTPUT_RE = re.compile(r'%sNo data was sent from the pty\.' % |
| 43 | CONSOLE_COMMON_RE) |
Ruben Rodriguez Buchillon | 2afbeea | 2020-02-19 15:24:20 -0800 | [diff] [blame] | 44 | |
| 45 | |
Ruben Rodriguez Buchillon | 348d93f | 2020-02-19 15:30:22 -0800 | [diff] [blame] | 46 | # Regex to match XMLRPC errors due to a console control failing, but the |
| 47 | # underlying Console being responsive. |
Ruben Rodriguez Buchillon | 03fded9 | 2020-04-13 19:55:10 -0700 | [diff] [blame] | 48 | CONSOLE_MISMATCH_RE = re.compile(r'%sThere was output:' % CONSOLE_COMMON_RE) |
Ruben Rodriguez Buchillon | 348d93f | 2020-02-19 15:30:22 -0800 | [diff] [blame] | 49 | |
| 50 | |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 51 | # The minimum voltage on the charger port on servo v4 that is expected. This is |
| 52 | # to query whether a charger is plugged into servo v4 and thus pd control |
| 53 | # capabilities can be used. |
| 54 | V4_CHG_ATTACHED_MIN_VOLTAGE_MV = 4400 |
| 55 | |
Ruben Rodriguez Buchillon | 2afbeea | 2020-02-19 15:24:20 -0800 | [diff] [blame] | 56 | |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 57 | class ControlUnavailableError(error.TestFail): |
| 58 | """Custom error class to indicate a control is unavailable on servod.""" |
| 59 | pass |
| 60 | |
| 61 | |
Ruben Rodriguez Buchillon | 348d93f | 2020-02-19 15:30:22 -0800 | [diff] [blame] | 62 | class ConsoleError(error.TestFail): |
| 63 | """Common error class for servod console-back control failures.""" |
| 64 | pass |
| 65 | |
| 66 | |
| 67 | class UnresponsiveConsoleError(ConsoleError): |
Ruben Rodriguez Buchillon | 2afbeea | 2020-02-19 15:24:20 -0800 | [diff] [blame] | 68 | """Error for: A console control fails for lack of console output.""" |
| 69 | pass |
| 70 | |
| 71 | |
Ruben Rodriguez Buchillon | 348d93f | 2020-02-19 15:30:22 -0800 | [diff] [blame] | 72 | class ResponsiveConsoleError(ConsoleError): |
| 73 | """Error for: A console control fails but console is responsive.""" |
| 74 | pass |
| 75 | |
| 76 | |
Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 77 | class ServodBadResponse(six.moves.http_client.BadStatusLine): |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 78 | """Indicates a bad HTTP response from servod""" |
| 79 | |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 80 | def __init__(self, when, line): |
| 81 | """ |
| 82 | |
| 83 | @param when: Description of the operation being performed (get/set) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 84 | @param line: The line that came from the server, often an empty string. |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 85 | """ |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 86 | super(ServodBadResponse, self).__init__(line) |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 87 | self.when = when |
| 88 | |
| 89 | def __str__(self): |
| 90 | """String representation of the exception""" |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 91 | return '%s -- StatusLine=%s' % (self.when, self.line) |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 92 | |
| 93 | |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 94 | class ServodEmptyResponse(ServodBadResponse): |
| 95 | """Indicates an empty response from servod, possibly because it exited.""" |
| 96 | pass |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 97 | |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 98 | |
| 99 | class ServodConnectionError(seven.SOCKET_ERRORS[0]): |
| 100 | """Indicates socket errors seen during communication with servod""" |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 101 | |
| 102 | def __init__(self, when, errno, strerror, filename): |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 103 | """Instance initializer |
| 104 | |
| 105 | The filename is used to add details to the exception message: |
| 106 | [Errno 104] Connection reset by peer: "<Servo 'ipaddr:9999'>" |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 107 | |
| 108 | @param when: Description of the operation being performed at the time |
| 109 | @param errno: errno value, such as ECONNRESET |
| 110 | @param strerror: OS-provided description ("connection reset by peer") |
| 111 | @param filename: Something to report as a path, such as a socket address |
| 112 | """ |
| 113 | # [Errno 104] [Setting ctrl:val] Connection reset by peer: <Servo... |
| 114 | self.when = when |
| 115 | super(ServodConnectionError, self).__init__(errno, strerror, filename) |
| 116 | |
| 117 | def __str__(self): |
| 118 | """String representation of the exception""" |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 119 | return '%s -- [Errno %d] %s: %r' % (self.when, self.errno, |
| 120 | self.strerror, self.filename) |
| 121 | |
| 122 | |
| 123 | # TODO: once in python 3, inherit from AbstractContextManager |
| 124 | class _WrapServoErrors(object): |
| 125 | """ |
| 126 | Wrap an operation, replacing BadStatusLine and socket.error with |
| 127 | servo-specific versions, and extracting exception info from xmlrplib.Fault. |
| 128 | |
| 129 | @param servo_name: The servo object, used to add the servo name to errors. |
| 130 | See the ServodConnectionError docstring. |
| 131 | @param description: String to use when describing what was being done |
| 132 | @raise ServodBadStatusLine: if exception is a httplib.BadStatusLine |
| 133 | @raise ServodSocketError: if exception is a socket.error |
| 134 | @raise ControlUnavailableError: if Fault matches NO_CONTROL_RE |
| 135 | @raise UnresponsiveConsoleError: if Fault matches NO_CONSOLE_OUTPUT_RE |
| 136 | @raise ResponsiveConsoleError: if Fault matches CONSOLE_MISMATCH_RE |
| 137 | """ |
| 138 | |
| 139 | def __init__(self, servo, description): |
| 140 | self.servo_name = str(servo) |
| 141 | self.description = description |
| 142 | |
| 143 | @staticmethod |
| 144 | def _get_xmlrpclib_exception(xmlexc): |
| 145 | """Get meaningful exception string from xmlrpc. |
| 146 | |
| 147 | Args: |
| 148 | xmlexc: xmlrpclib.Fault object |
| 149 | |
| 150 | xmlrpclib.Fault.faultString has the following format: |
| 151 | |
| 152 | <type 'exception type'>:'actual error message' |
| 153 | |
| 154 | Parse and return the real exception from the servod side instead of the |
| 155 | less meaningful one like, |
| 156 | <Fault 1: "<type 'exceptions.AttributeError'>:'tca6416' object has no |
| 157 | attribute 'hw_driver'"> |
| 158 | |
| 159 | Returns: |
| 160 | string of underlying exception raised in servod. |
| 161 | """ |
| 162 | return re.sub('^.*>:', '', xmlexc.faultString) |
| 163 | |
Brent Peterson | 8eda97f | 2020-11-12 13:10:40 -0500 | [diff] [blame] | 164 | @staticmethod |
| 165 | def _log_exception(exc_type, exc_val, exc_tb): |
| 166 | """Log exception information""" |
| 167 | if exc_val is not None: |
| 168 | logging.debug( |
| 169 | 'Wrapped exception:', exc_info=(exc_type, exc_val, exc_tb)) |
| 170 | |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 171 | def __enter__(self): |
| 172 | """Enter the context""" |
| 173 | return self |
| 174 | |
| 175 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 176 | """Exit the context, handling the exception if there was one""" |
| 177 | try: |
Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 178 | if isinstance(exc_val, six.moves.http_client.BadStatusLine): |
Brent Peterson | 8eda97f | 2020-11-12 13:10:40 -0500 | [diff] [blame] | 179 | self._log_exception(exc_type, exc_val, exc_tb) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 180 | if exc_val.line in ('', "''"): |
| 181 | err = ServodEmptyResponse(self.description, exc_val.line) |
| 182 | else: |
| 183 | err = ServodBadResponse(self.description, exc_val.line) |
| 184 | six.reraise(err.__class__, err, exc_tb) |
| 185 | |
| 186 | if isinstance(exc_val, seven.SOCKET_ERRORS): |
Brent Peterson | 8eda97f | 2020-11-12 13:10:40 -0500 | [diff] [blame] | 187 | self._log_exception(exc_type, exc_val, exc_tb) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 188 | err = ServodConnectionError(self.description, exc_val.args[0], |
| 189 | exc_val.args[1], self.servo_name) |
| 190 | six.reraise(err.__class__, err, exc_tb) |
| 191 | |
Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 192 | if isinstance(exc_val, six.moves.xmlrpc_client.Fault): |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 193 | err_str = self._get_xmlrpclib_exception(exc_val) |
| 194 | err_msg = '%s :: %s' % (self.description, err_str) |
| 195 | unknown_ctrl = re.search(NO_CONTROL_RE, err_str) |
| 196 | if not unknown_ctrl: |
| 197 | # Log the full text for errors, except unavailable controls. |
Brent Peterson | 8eda97f | 2020-11-12 13:10:40 -0500 | [diff] [blame] | 198 | self._log_exception(exc_type, exc_val, exc_tb) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 199 | logging.debug(err_msg) |
| 200 | if unknown_ctrl: |
| 201 | # The error message for unavailable controls is huge, since |
| 202 | # it reports all known controls. Don't log the full text. |
| 203 | unknown_ctrl_name = unknown_ctrl.group('name') |
Evan Benn | 36d6059 | 2020-10-16 11:06:20 +1100 | [diff] [blame] | 204 | logging.debug('%s :: No control named %r', |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 205 | self.description, unknown_ctrl_name) |
| 206 | err = ControlUnavailableError( |
| 207 | 'No control named %r' % unknown_ctrl_name) |
| 208 | elif re.search(NO_CONSOLE_OUTPUT_RE, err_str): |
| 209 | err = UnresponsiveConsoleError( |
| 210 | 'Console not printing output. %s.' % |
| 211 | self.description) |
| 212 | elif re.search(CONSOLE_MISMATCH_RE, err_str): |
| 213 | err = ResponsiveConsoleError( |
| 214 | 'Control failed but console alive. %s.' % |
| 215 | self.description) |
| 216 | else: |
| 217 | err = error.TestFail(err_msg) |
| 218 | six.reraise(err.__class__, err, exc_tb) |
| 219 | finally: |
| 220 | del exc_tb |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 221 | |
| 222 | |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 223 | def _extract_image_from_tarball(tarball, dest_dir, image_candidates, timeout): |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 224 | """Try extracting the image_candidates from the tarball. |
| 225 | |
| 226 | @param tarball: The path of the tarball. |
| 227 | @param dest_path: The path of the destination. |
| 228 | @param image_candidates: A tuple of the paths of image candidates. |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 229 | @param timeout: Time to wait in seconds before timing out. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 230 | |
| 231 | @return: The first path from the image candidates, which succeeds, or None |
| 232 | if all the image candidates fail. |
| 233 | """ |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 234 | |
| 235 | # Create the firmware_name subdirectory if it doesn't exist |
| 236 | if not os.path.exists(dest_dir): |
| 237 | os.mkdir(dest_dir) |
| 238 | |
Brent Peterson | cf7ac14 | 2020-02-12 14:38:35 -0800 | [diff] [blame] | 239 | # Generate a list of all tarball files |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 240 | stdout = server_utils.system_output('tar tf %s' % tarball, |
| 241 | timeout=timeout, |
| 242 | ignore_status=True) |
| 243 | tarball_files = stdout.splitlines() |
Brent Peterson | cf7ac14 | 2020-02-12 14:38:35 -0800 | [diff] [blame] | 244 | |
| 245 | # Check if image candidates are in the list of tarball files |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 246 | for image in image_candidates: |
Brent Peterson | cf7ac14 | 2020-02-12 14:38:35 -0800 | [diff] [blame] | 247 | if image in tarball_files: |
| 248 | # Extract and return the first image candidate found |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 249 | tar_cmd = 'tar xf %s -C %s %s' % (tarball, dest_dir, image) |
| 250 | status = server_utils.system(tar_cmd, |
| 251 | timeout=timeout, |
| 252 | ignore_status=True) |
Brent Peterson | cf7ac14 | 2020-02-12 14:38:35 -0800 | [diff] [blame] | 253 | if status == 0: |
| 254 | return image |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 255 | return None |
| 256 | |
| 257 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 258 | class _PowerStateController(object): |
| 259 | |
| 260 | """Class to provide board-specific power operations. |
| 261 | |
| 262 | This class is responsible for "power on" and "power off" |
| 263 | operations that can operate without making assumptions in |
| 264 | advance about board state. It offers an interface that |
| 265 | abstracts out the different sequences required for different |
| 266 | board types. |
| 267 | |
| 268 | """ |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 269 | # Constants acceptable to be passed for the `rec_mode` parameter |
| 270 | # to power_on(). |
| 271 | # |
| 272 | # REC_ON: Boot the DUT in recovery mode, i.e. boot from USB or |
| 273 | # SD card. |
| 274 | # REC_OFF: Boot in normal mode, i.e. boot from internal storage. |
| 275 | |
| 276 | REC_ON = 'rec' |
| 277 | REC_OFF = 'on' |
Shelley Chen | 6593862 | 2018-05-16 07:45:54 -0700 | [diff] [blame] | 278 | REC_ON_FORCE_MRC = 'rec_force_mrc' |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 279 | |
| 280 | # Delay in seconds needed between asserting and de-asserting |
| 281 | # warm reset. |
| 282 | _RESET_HOLD_TIME = 0.5 |
| 283 | |
Ruben Rodriguez Buchillon | aed7c89 | 2020-02-06 13:11:14 -0800 | [diff] [blame] | 284 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 285 | def __init__(self, servo): |
| 286 | """Initialize the power state control. |
| 287 | |
| 288 | @param servo Servo object providing the underlying `set` and `get` |
| 289 | methods for the target controls. |
| 290 | |
| 291 | """ |
| 292 | self._servo = servo |
Ruben Rodriguez Buchillon | aed7c89 | 2020-02-06 13:11:14 -0800 | [diff] [blame] | 293 | self.supported = self._servo.has_control('power_state') |
Patrick Georgi | b3bcb7f | 2020-08-05 17:34:03 +0200 | [diff] [blame] | 294 | self.last_rec_mode = self.REC_OFF |
Ruben Rodriguez Buchillon | aed7c89 | 2020-02-06 13:11:14 -0800 | [diff] [blame] | 295 | if not self.supported: |
| 296 | logging.info('Servo setup does not support power-state operations. ' |
| 297 | 'All power-state calls will lead to error.TestFail') |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 298 | |
Greg Edelston | b73cddd | 2020-02-11 11:06:29 -0700 | [diff] [blame] | 299 | def _check_supported(self): |
| 300 | """Throw an error if dts mode control is not supported.""" |
| 301 | if not self.supported: |
| 302 | raise error.TestFail('power_state controls not supported') |
| 303 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 304 | def reset(self): |
| 305 | """Force the DUT to reset. |
| 306 | |
| 307 | The DUT is guaranteed to be on at the end of this call, |
| 308 | regardless of its previous state, provided that there is |
| 309 | working OS software. This also guarantees that the EC has |
| 310 | been restarted. |
| 311 | |
| 312 | """ |
Greg Edelston | b73cddd | 2020-02-11 11:06:29 -0700 | [diff] [blame] | 313 | self._check_supported() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 314 | self._servo.set_nocheck('power_state', 'reset') |
| 315 | |
Otabek Kasimov | 8ff1ce7 | 2020-04-17 20:17:59 -0700 | [diff] [blame] | 316 | def cr50_reset(self): |
| 317 | """Force the DUT to reset. |
| 318 | |
| 319 | The DUT is guaranteed to be on at the end of this call, |
| 320 | regardless of its previous state, provided that there is |
| 321 | working OS software. This also guarantees that the EC has |
| 322 | been restarted. Works only for ccd connections. |
| 323 | |
| 324 | """ |
| 325 | self._check_supported() |
| 326 | self._servo.set_nocheck('power_state', 'cr50_reset') |
| 327 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 328 | def warm_reset(self): |
| 329 | """Apply warm reset to the DUT. |
| 330 | |
| 331 | This asserts, then de-asserts the 'warm_reset' signal. |
| 332 | Generally, this causes the board to restart. |
| 333 | |
| 334 | """ |
Ravutappa | 951ca43 | 2019-05-15 09:52:52 -0700 | [diff] [blame] | 335 | # TODO: warm_reset support has added to power_state.py. Once it |
| 336 | # available to labstation remove fallback method. |
Greg Edelston | b73cddd | 2020-02-11 11:06:29 -0700 | [diff] [blame] | 337 | self._check_supported() |
Ravutappa | 951ca43 | 2019-05-15 09:52:52 -0700 | [diff] [blame] | 338 | try: |
| 339 | self._servo.set_nocheck('power_state', 'warm_reset') |
| 340 | except error.TestFail as err: |
| 341 | logging.info("Fallback to warm_reset control method") |
| 342 | self._servo.set_get_all(['warm_reset:on', |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 343 | 'sleep:%.4f' % self._RESET_HOLD_TIME, |
| 344 | 'warm_reset:off']) |
Gregory Nisbet | 1f18f88 | 2020-07-07 10:31:59 -0700 | [diff] [blame] | 345 | |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 346 | def power_off(self): |
| 347 | """Force the DUT to power off. |
| 348 | |
| 349 | The DUT is guaranteed to be off at the end of this call, |
| 350 | regardless of its previous state, provided that there is |
| 351 | working EC and boot firmware. There is no requirement for |
| 352 | working OS software. |
| 353 | |
| 354 | """ |
Greg Edelston | b73cddd | 2020-02-11 11:06:29 -0700 | [diff] [blame] | 355 | self._check_supported() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 356 | self._servo.set_nocheck('power_state', 'off') |
| 357 | |
| 358 | def power_on(self, rec_mode=REC_OFF): |
| 359 | """Force the DUT to power on. |
| 360 | |
| 361 | Prior to calling this function, the DUT must be powered off, |
| 362 | e.g. with a call to `power_off()`. |
| 363 | |
| 364 | At power on, recovery mode is set as specified by the |
| 365 | corresponding argument. When booting with recovery mode on, it |
| 366 | is the caller's responsibility to unplug/plug in a bootable |
| 367 | external storage device. |
| 368 | |
| 369 | If the DUT requires a delay after powering on but before |
| 370 | processing inputs such as USB stick insertion, the delay is |
| 371 | handled by this method; the caller is not responsible for such |
| 372 | delays. |
| 373 | |
| 374 | @param rec_mode Setting of recovery mode to be applied at |
| 375 | power on. default: REC_OFF aka 'off' |
| 376 | |
| 377 | """ |
Greg Edelston | b73cddd | 2020-02-11 11:06:29 -0700 | [diff] [blame] | 378 | self._check_supported() |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 379 | self._servo.set_nocheck('power_state', rec_mode) |
Patrick Georgi | b3bcb7f | 2020-08-05 17:34:03 +0200 | [diff] [blame] | 380 | self.last_rec_mode = rec_mode |
| 381 | |
| 382 | def retry_power_on(self): |
| 383 | """Retry powering on the DUT. |
| 384 | |
| 385 | After power_on(...) the system might not come up reliably, although |
| 386 | the reasons aren't known yet. This function retries turning on the |
| 387 | system again, trying to bring it in the last state that power_on() |
| 388 | attempted to reach. |
| 389 | """ |
| 390 | self._check_supported() |
| 391 | self._servo.set_nocheck('power_state', self.last_rec_mode) |
J. Richard Barnette | 0c9c588 | 2014-06-11 15:27:05 -0700 | [diff] [blame] | 392 | |
| 393 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 394 | class _Uart(object): |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 395 | """Class to capture UART streams of CPU, EC, Cr50, etc.""" |
Mary Ruthven | ad0e527 | 2020-08-12 16:00:58 -0700 | [diff] [blame] | 396 | _UartToCapture = ( |
| 397 | 'cpu', |
| 398 | 'cr50', |
| 399 | 'ec', |
| 400 | 'servo_micro', |
| 401 | 'servo_v4', |
| 402 | 'usbpd', |
| 403 | 'ccd_cr50.ec', |
| 404 | 'ccd_cr50.cpu', |
| 405 | 'ccd_cr50.cr50' |
| 406 | ) |
| 407 | |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 408 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 409 | def __init__(self, servo): |
| 410 | self._servo = servo |
| 411 | self._streams = [] |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 412 | self.logs_dir = None |
Congbin Guo | fe4d4e8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 413 | |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 414 | def _start_stop_capture(self, uart, start): |
| 415 | """Helper function to start/stop capturing on specified UART. |
| 416 | |
| 417 | @param uart: The UART name to start/stop capturing. |
| 418 | @param start: True to start capturing, otherwise stop. |
| 419 | |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 420 | @returns True if the operation completes successfully. |
| 421 | False if the UART capturing is not supported or failed due to |
| 422 | an error. |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 423 | """ |
| 424 | logging.debug('%s capturing %s UART.', 'Start' if start else 'Stop', |
| 425 | uart) |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 426 | uart_cmd = '%s_uart_capture' % uart |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 427 | target_level = 'on' if start else 'off' |
| 428 | level = None |
Ruben Rodriguez Buchillon | b0e2a9f | 2019-08-12 12:40:44 -0700 | [diff] [blame] | 429 | if self._servo.has_control(uart_cmd): |
Ruben Rodriguez Buchillon | 9f48564 | 2019-08-21 14:32:22 -0700 | [diff] [blame] | 430 | # Do our own implementation of set() here as not_applicable |
| 431 | # should also count as a valid control. |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 432 | logging.debug('Trying to set %s to %s.', uart_cmd, target_level) |
| 433 | try: |
| 434 | self._servo.set_nocheck(uart_cmd, target_level) |
| 435 | level = self._servo.get(uart_cmd) |
| 436 | except error.TestFail as e: |
| 437 | # Any sort of test failure here should not stop the test. This |
| 438 | # is just to capture more output. Log and move on. |
| 439 | logging.warning('Failed to set %s to %s. %s. Ignoring.', |
| 440 | uart_cmd, target_level, str(e)) |
| 441 | if level == target_level: |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 442 | logging.debug('Managed to set %s to %s.', uart_cmd, level) |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 443 | else: |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 444 | logging.debug('Failed to set %s to %s. Got %s.', uart_cmd, |
| 445 | target_level, level) |
Ruben Rodriguez Buchillon | ecb525f | 2019-09-03 14:52:14 -0700 | [diff] [blame] | 446 | return level == target_level |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 447 | |
| 448 | def start_capture(self): |
| 449 | """Start capturing UART streams.""" |
| 450 | for uart in self._UartToCapture: |
Mary Ruthven | 20e84d1 | 2021-03-18 11:15:51 -0700 | [diff] [blame] | 451 | # Always try to start the uart. Only add it to _streams if it's not |
| 452 | # in the list. |
| 453 | if (self._start_stop_capture(uart, True) |
| 454 | and uart not in self._streams): |
Mary Ruthven | f2dd7a4 | 2020-08-12 16:18:24 -0700 | [diff] [blame] | 455 | self._streams.append(uart) |
| 456 | |
| 457 | def get_logfile(self, uart): |
| 458 | """Return the path to the uart logfile or none if logs_dir isn't set.""" |
| 459 | if not self.logs_dir: |
| 460 | return None |
| 461 | return os.path.join(self.logs_dir, '%s_uart.txt' % uart) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 462 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 463 | def dump(self): |
| 464 | """Dump UART streams to log files accordingly.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 465 | if not self.logs_dir: |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 466 | return |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 467 | |
Mary Ruthven | f2dd7a4 | 2020-08-12 16:18:24 -0700 | [diff] [blame] | 468 | for uart in self._streams: |
| 469 | logfile_fullname = self.get_logfile(uart) |
| 470 | stream = '%s_uart_stream' % uart |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 471 | try: |
| 472 | content = self._servo.get(stream) |
| 473 | except Exception as err: |
| 474 | logging.warn('Failed to get UART log for %s: %s', stream, err) |
| 475 | continue |
| 476 | |
Kuang-che Wu | 12192fc | 2019-08-31 09:55:00 +0800 | [diff] [blame] | 477 | if content == 'not_applicable': |
| 478 | logging.warn('%s is not applicable', stream) |
| 479 | continue |
| 480 | |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 481 | # The UART stream may contain non-printable characters, and servo |
| 482 | # returns it in string representation. We use `ast.leteral_eval` |
| 483 | # to revert it back. |
| 484 | with open(logfile_fullname, 'a') as fd: |
Kuang-che Wu | 12192fc | 2019-08-31 09:55:00 +0800 | [diff] [blame] | 485 | try: |
| 486 | fd.write(ast.literal_eval(content)) |
| 487 | except ValueError: |
| 488 | logging.exception('Invalid value for %s: %r', stream, |
| 489 | content) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 490 | |
| 491 | def stop_capture(self): |
| 492 | """Stop capturing UART streams.""" |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 493 | for uart in self._UartToCapture: |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 494 | try: |
Congbin Guo | 0f00be8 | 2019-04-18 17:51:14 -0700 | [diff] [blame] | 495 | self._start_stop_capture(uart, False) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 496 | except Exception as err: |
| 497 | logging.warn('Failed to stop UART logging for %s: %s', uart, |
| 498 | err) |
| 499 | |
| 500 | |
J. Richard Barnette | 384056b | 2012-04-16 11:04:46 -0700 | [diff] [blame] | 501 | class Servo(object): |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 502 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 503 | """Manages control of a Servo board. |
| 504 | |
| 505 | Servo is a board developed by hardware group to aide in the debug and |
| 506 | control of various partner devices. Servo's features include the simulation |
| 507 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 508 | class manages setting up and communicating with a servo demon (servod) |
| 509 | process. It provides both high-level functions for common servo tasks and |
| 510 | low-level functions for directly setting and reading gpios. |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 511 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 512 | """ |
| 513 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 514 | # Power button press delays in seconds. |
J. Richard Barnette | d2e4cbd | 2012-06-29 12:18:40 -0700 | [diff] [blame] | 515 | # |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 516 | # The EC specification says that 8.0 seconds should be enough |
| 517 | # for the long power press. However, some platforms need a bit |
| 518 | # more time. Empirical testing has found these requirements: |
| 519 | # Alex: 8.2 seconds |
| 520 | # ZGB: 8.5 seconds |
| 521 | # The actual value is set to the largest known necessary value. |
| 522 | # |
| 523 | # TODO(jrbarnette) Being generous is the right thing to do for |
| 524 | # existing platforms, but if this code is to be used for |
| 525 | # qualification of new hardware, we should be less generous. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 526 | SHORT_DELAY = 0.1 |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 527 | |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 528 | # Maximum number of times to re-read power button on release. |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 529 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 530 | |
J. Richard Barnette | 5383f07 | 2012-07-26 17:35:40 -0700 | [diff] [blame] | 531 | # Delays to deal with DUT state transitions. |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 532 | SLEEP_DELAY = 6 |
| 533 | BOOT_DELAY = 10 |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 534 | |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 535 | # Default minimum time interval between 'press' and 'release' |
| 536 | # keyboard events. |
Vic Yang | 0aca1c2 | 2012-11-19 18:33:56 -0800 | [diff] [blame] | 537 | SERVO_KEY_PRESS_DELAY = 0.1 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 538 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 539 | # Time to toggle recovery switch on and off. |
| 540 | REC_TOGGLE_DELAY = 0.1 |
| 541 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 542 | # Time to toggle development switch on and off. |
| 543 | DEV_TOGGLE_DELAY = 0.1 |
| 544 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 545 | # Time between an usb disk plugged-in and detected in the system. |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 546 | USB_DETECTION_DELAY = 5 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 547 | |
Simran Basi | b7850bb | 2013-07-24 12:33:42 -0700 | [diff] [blame] | 548 | # Time to wait before timing out on servo initialization. |
| 549 | INIT_TIMEOUT_SECS = 10 |
Simran Basi | 5933134 | 2013-07-12 12:06:29 -0700 | [diff] [blame] | 550 | |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 551 | # Time to wait before timing out when extracting firmware images. |
| 552 | # |
| 553 | # This was increased from 60 seconds to support boards with very |
| 554 | # large (>500MB) firmware archives taking longer than expected to |
| 555 | # extract firmware on the lab host machines (b/149419503). |
venkata srinivasa raju penmetcha | 38a01ba | 2021-01-14 18:25:30 -0800 | [diff] [blame] | 556 | EXTRACT_TIMEOUT_SECS = 300 |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 557 | |
Dawid Niedzwiecki | de89cd1 | 2020-10-16 10:10:02 +0200 | [diff] [blame] | 558 | # The VBUS voltage threshold used to detect if VBUS is supplied |
| 559 | VBUS_THRESHOLD = 3000.0 |
| 560 | |
Mary Ruthven | 86fc161 | 2021-01-28 13:45:32 -0800 | [diff] [blame] | 561 | # List of servos that connect to a debug header on the board. |
| 562 | FLEX_SERVOS = ['c2d2', 'servo_micro', 'servo_v3'] |
| 563 | |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 564 | def __init__(self, servo_host, servo_serial=None, delay_init=False): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 565 | """Sets up the servo communication infrastructure. |
| 566 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 567 | @param servo_host: A ServoHost object representing |
| 568 | the host running servod. |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame] | 569 | @type servo_host: autotest_lib.server.hosts.servo_host.ServoHost |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 570 | @param servo_serial: Serial number of the servo board. |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 571 | @param delay_init: Delay cache servo_type and power_state to prevent |
| 572 | attempt to connect to the servod. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 573 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 574 | # TODO(fdeng): crbug.com/298379 |
| 575 | # We should move servo_host object out of servo object |
| 576 | # to minimize the dependencies on the rest of Autotest. |
| 577 | self._servo_host = servo_host |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 578 | self._servo_serial = servo_serial |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 579 | self._servo_type = None |
| 580 | self._power_state = None |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 581 | self._programmer = None |
Dana Goyette | 4dc0adc | 2019-05-06 14:51:53 -0700 | [diff] [blame] | 582 | self._prev_log_inode = None |
| 583 | self._prev_log_size = 0 |
Mary Ruthven | 9003558 | 2020-06-18 12:17:15 -0700 | [diff] [blame] | 584 | self._ccd_watchdog_disabled = False |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 585 | if not delay_init: |
| 586 | self._servo_type = self.get_servo_version() |
| 587 | self._power_state = _PowerStateController(self) |
| 588 | self._uart = _Uart(self) |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 589 | |
Dana Goyette | afa62fd | 2020-03-16 13:45:27 -0700 | [diff] [blame] | 590 | def __str__(self): |
| 591 | """Description of this object and address, for use in errors""" |
| 592 | return "<%s '%s:%s'>" % ( |
| 593 | type(self).__name__, |
| 594 | self._servo_host.hostname, |
| 595 | self._servo_host.servo_port) |
| 596 | |
Andrew McRae | f067993 | 2020-08-13 09:15:23 +1000 | [diff] [blame] | 597 | @property |
| 598 | def _server(self): |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 599 | with _WrapServoErrors( |
| 600 | servo=self, description='get_servod_server_proxy()'): |
Andrew McRae | f067993 | 2020-08-13 09:15:23 +1000 | [diff] [blame] | 601 | return self._servo_host.get_servod_server_proxy() |
| 602 | |
Ricky Liang | 0dd379c | 2014-04-23 16:29:08 +0800 | [diff] [blame] | 603 | @property |
| 604 | def servo_serial(self): |
| 605 | """Returns the serial number of the servo board.""" |
| 606 | return self._servo_serial |
| 607 | |
Tom Wai-Hong Tam | 22ee0e5 | 2013-04-03 13:36:39 +0800 | [diff] [blame] | 608 | def get_power_state_controller(self): |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 609 | """Return the power state controller for this Servo. |
| 610 | |
| 611 | The power state controller provides board-independent |
| 612 | interfaces for reset, power-on, power-off operations. |
| 613 | |
| 614 | """ |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 615 | if self._power_state is None: |
| 616 | self._power_state = _PowerStateController(self) |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 617 | return self._power_state |
| 618 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 619 | |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 620 | def initialize_dut(self, cold_reset=False, enable_main=True): |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 621 | """Initializes a dut for testing purposes. |
| 622 | |
| 623 | This sets various servo signals back to default values |
| 624 | appropriate for the target board. By default, if the DUT |
| 625 | is already on, it stays on. If the DUT is powered off |
| 626 | before initialization, its state afterward is unspecified. |
| 627 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 628 | Rationale: Basic initialization of servo sets the lid open, |
| 629 | when there is a lid. This operation won't affect powered on |
| 630 | units; however, setting the lid open may power on a unit |
J. Richard Barnette | 75136b3 | 2013-03-26 13:38:44 -0700 | [diff] [blame] | 631 | that's off, depending on the board type and previous state |
| 632 | of the device. |
| 633 | |
J. Richard Barnette | 4b6af0d | 2014-06-05 09:57:20 -0700 | [diff] [blame] | 634 | If `cold_reset` is a true value, the DUT and its EC will be |
| 635 | reset, and the DUT rebooted in normal mode. |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 636 | |
| 637 | @param cold_reset If True, cold reset the device after |
| 638 | initialization. |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 639 | @param enable_main If True, make sure the main servo device has |
| 640 | control of the dut. |
| 641 | |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 642 | """ |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 643 | if enable_main: |
| 644 | self.enable_main_servo_device() |
| 645 | |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 646 | with _WrapServoErrors( |
| 647 | servo=self, description='initialize_dut()->hwinit()'): |
Dana Goyette | a2f00ea | 2019-06-26 16:14:12 -0700 | [diff] [blame] | 648 | self._server.hwinit() |
Mengqi Guo | 51d5bea | 2020-02-03 18:34:45 -0800 | [diff] [blame] | 649 | if self.has_control('usb_mux_oe1'): |
| 650 | self.set('usb_mux_oe1', 'on') |
| 651 | self.switch_usbkey('off') |
| 652 | else: |
| 653 | logging.warning('Servod command \'usb_mux_oe1\' is not available. ' |
| 654 | 'Any USB drive related servo routines will fail.') |
Ruben Rodriguez Buchillon | 78ad927 | 2020-09-11 12:48:57 -0700 | [diff] [blame] | 655 | # Create a record of SBU voltages if this is running support servo (v4, |
| 656 | # v4p1). |
| 657 | # TODO(coconutruben): eventually, replace this with a metric to track |
| 658 | # SBU voltages wrt servo-hw/dut-hw |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 659 | if self.has_control('servo_dut_sbu1_mv'): |
Gregory Nisbet | 8c2ee93 | 2020-11-24 10:30:34 -0800 | [diff] [blame] | 660 | # Attempt to take a reading of sbu1 and sbu2 multiple times to |
| 661 | # account for situations where the two lines exchange hi/lo roles |
| 662 | # frequently. |
| 663 | for i in range(10): |
Ruben Rodriguez Buchillon | 78ad927 | 2020-09-11 12:48:57 -0700 | [diff] [blame] | 664 | try: |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 665 | sbu1 = int(self.get('servo_dut_sbu1_mv')) |
| 666 | sbu2 = int(self.get('servo_dut_sbu2_mv')) |
Gregory Nisbet | 8c2ee93 | 2020-11-24 10:30:34 -0800 | [diff] [blame] | 667 | logging.info('attempt %d sbu1 %d sbu2 %d', i, sbu1, sbu2) |
Ruben Rodriguez Buchillon | 78ad927 | 2020-09-11 12:48:57 -0700 | [diff] [blame] | 668 | except error.TestFail as e: |
| 669 | # This is a nice to have but if reading this fails, it |
| 670 | # shouldn't interfere with the test. |
Gregory Nisbet | 8c2ee93 | 2020-11-24 10:30:34 -0800 | [diff] [blame] | 671 | logging.exception(e) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 672 | self._uart.start_capture() |
J. Richard Barnette | b613397 | 2012-07-19 17:13:55 -0700 | [diff] [blame] | 673 | if cold_reset: |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 674 | if not self.get_power_state_controller().supported: |
Ruben Rodriguez Buchillon | aed7c89 | 2020-02-06 13:11:14 -0800 | [diff] [blame] | 675 | logging.info('Cold-reset for DUT requested, but servo ' |
| 676 | 'setup does not support power_state. Skipping.') |
| 677 | else: |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 678 | self.get_power_state_controller().reset() |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 679 | with _WrapServoErrors( |
| 680 | servo=self, description='initialize_dut()->get_version()'): |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 681 | version = self._server.get_version() |
| 682 | logging.debug('Servo initialized, version is %s', version) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 683 | |
| 684 | |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 685 | def is_localhost(self): |
| 686 | """Is the servod hosted locally? |
| 687 | |
| 688 | Returns: |
| 689 | True if local hosted; otherwise, False. |
| 690 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 691 | return self._servo_host.is_localhost() |
Tom Wai-Hong Tam | 1c86c7a | 2012-10-22 10:08:24 +0800 | [diff] [blame] | 692 | |
| 693 | |
Mary Ruthven | ecf1271 | 2019-06-26 17:36:21 -0700 | [diff] [blame] | 694 | def get_os_version(self): |
| 695 | """Returns the chromeos release version.""" |
| 696 | lsb_release_content = self.system_output('cat /etc/lsb-release', |
| 697 | ignore_status=True) |
| 698 | return lsbrelease_utils.get_chromeos_release_builder_path( |
| 699 | lsb_release_content=lsb_release_content) |
| 700 | |
| 701 | |
Mary Ruthven | 83bb595 | 2019-06-27 12:34:05 -0700 | [diff] [blame] | 702 | def get_servod_version(self): |
| 703 | """Returns the servod version.""" |
Ruben Rodriguez Buchillon | a1ad37f | 2020-04-24 16:51:07 -0700 | [diff] [blame] | 704 | # TODO: use system_output once servod --sversion prints to stdout |
| 705 | try: |
Evan Benn | 30c79aa | 2020-10-28 15:15:27 +1100 | [diff] [blame] | 706 | result = self._servo_host.run('servod --sversion 2>&1') |
Ruben Rodriguez Buchillon | a1ad37f | 2020-04-24 16:51:07 -0700 | [diff] [blame] | 707 | except error.AutoservRunError as e: |
| 708 | if 'command execution error' in str(e): |
| 709 | # Fall back to version if sversion is not supported yet. |
Evan Benn | 30c79aa | 2020-10-28 15:15:27 +1100 | [diff] [blame] | 710 | result = self._servo_host.run('servod --version 2>&1') |
Ruben Rodriguez Buchillon | a1ad37f | 2020-04-24 16:51:07 -0700 | [diff] [blame] | 711 | return result.stdout.strip() or result.stderr.strip() |
| 712 | # An actually unexpected error occurred, just raise. |
| 713 | raise e |
| 714 | sversion = result.stdout or result.stderr |
| 715 | # The sversion output contains 3 lines: |
| 716 | # servod v1.0.816-ff8e966 // the extended version with git hash |
| 717 | # 2020-04-08 01:10:29 // the time of the latest commit |
| 718 | # chromeos-ci-legacy-us-central1-b-x32-55-u8zc // builder information |
| 719 | # For debugging purposes, we mainly care about the version, and the |
| 720 | # timestamp. |
| 721 | return ' '.join(sversion.split()[1:4]) |
Mary Ruthven | 83bb595 | 2019-06-27 12:34:05 -0700 | [diff] [blame] | 722 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 723 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 724 | """Simulate a long power button press.""" |
J. Richard Barnette | f12bff2 | 2012-07-18 14:41:06 -0700 | [diff] [blame] | 725 | # After a long power press, the EC may ignore the next power |
| 726 | # button press (at least on Alex). To guarantee that this |
| 727 | # won't happen, we need to allow the EC one second to |
| 728 | # collect itself. |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 729 | # long_press is defined as 8.5s in servod |
| 730 | self.set_nocheck('power_key', 'long_press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 731 | |
| 732 | |
| 733 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 734 | """Simulate a normal power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 735 | # press is defined as 1.2s in servod |
| 736 | self.set_nocheck('power_key', 'press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 737 | |
| 738 | |
| 739 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 740 | """Simulate a short power button press.""" |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 741 | # tab is defined as 0.2s in servod |
| 742 | self.set_nocheck('power_key', 'tab') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 743 | |
| 744 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 745 | def power_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 746 | """Simulate a power button press. |
| 747 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 748 | @param press_secs: int, float, str; time to press key in seconds or |
| 749 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 750 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 751 | self.set_nocheck('power_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 752 | |
| 753 | |
Ruben Zakarian | 47c1d4f | 2019-09-30 14:17:41 -0700 | [diff] [blame] | 754 | def pwr_button(self, action='press'): |
| 755 | """Simulate a power button press. |
| 756 | |
| 757 | @param action: str; could be press or could be release. |
| 758 | """ |
| 759 | self.set_nocheck('pwr_button', action) |
| 760 | |
| 761 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 762 | def lid_open(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 763 | """Simulate opening the lid and raise exception if all attempts fail""" |
| 764 | self.set('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 765 | |
| 766 | |
| 767 | def lid_close(self): |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 768 | """Simulate closing the lid and raise exception if all attempts fail |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 769 | |
| 770 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 771 | """ |
Yuli Huang | 7b82dcf | 2015-05-13 17:58:52 +0800 | [diff] [blame] | 772 | self.set('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 773 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 774 | |
Ruben Zakarian | 47c1d4f | 2019-09-30 14:17:41 -0700 | [diff] [blame] | 775 | |
| 776 | def vbus_power_get(self): |
| 777 | """Get current vbus_power.""" |
| 778 | return self.get('vbus_power') |
| 779 | |
| 780 | |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 781 | def volume_up(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 782 | """Simulate pushing the volume down button. |
| 783 | |
| 784 | @param timeout: Timeout for setting the volume. |
| 785 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 786 | self.set_get_all(['volume_up:yes', |
| 787 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 788 | 'volume_up:no']) |
| 789 | # we need to wait for commands to take effect before moving on |
| 790 | time_left = float(timeout) |
| 791 | while time_left > 0.0: |
| 792 | value = self.get('volume_up') |
| 793 | if value == 'no': |
| 794 | return |
| 795 | time.sleep(self.SHORT_DELAY) |
| 796 | time_left = time_left - self.SHORT_DELAY |
| 797 | raise error.TestFail("Failed setting volume_up to no") |
| 798 | |
| 799 | def volume_down(self, timeout=300): |
Kevin Cheng | e448a8b | 2016-09-09 10:18:11 -0700 | [diff] [blame] | 800 | """Simulate pushing the volume down button. |
| 801 | |
| 802 | @param timeout: Timeout for setting the volume. |
| 803 | """ |
Shelley Chen | c26575a | 2015-09-18 10:56:16 -0700 | [diff] [blame] | 804 | self.set_get_all(['volume_down:yes', |
| 805 | 'sleep:%.4f' % self.SERVO_KEY_PRESS_DELAY, |
| 806 | 'volume_down:no']) |
| 807 | # we need to wait for commands to take effect before moving on |
| 808 | time_left = float(timeout) |
| 809 | while time_left > 0.0: |
| 810 | value = self.get('volume_down') |
| 811 | if value == 'no': |
| 812 | return |
| 813 | time.sleep(self.SHORT_DELAY) |
| 814 | time_left = time_left - self.SHORT_DELAY |
| 815 | raise error.TestFail("Failed setting volume_down to no") |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 816 | |
Yu-Ping Wu | d52fac9 | 2019-11-13 11:12:08 +0800 | [diff] [blame] | 817 | def arrow_up(self, press_secs='tab'): |
| 818 | """Simulate arrow up key presses. |
| 819 | |
| 820 | @param press_secs: int, float, str; time to press key in seconds or |
| 821 | known shorthand: 'tab' 'press' 'long_press'. |
| 822 | """ |
| 823 | # TODO: Remove this check after a lab update to include CL:1913684 |
| 824 | if not self.has_control('arrow_up'): |
| 825 | logging.warning('Control arrow_up ignored. ' |
| 826 | 'Please update hdctools') |
| 827 | return |
| 828 | self.set_nocheck('arrow_up', press_secs) |
| 829 | |
| 830 | def arrow_down(self, press_secs='tab'): |
| 831 | """Simulate arrow down key presses. |
| 832 | |
| 833 | @param press_secs: int, float, str; time to press key in seconds or |
| 834 | known shorthand: 'tab' 'press' 'long_press'. |
| 835 | """ |
| 836 | # TODO: Remove this check after a lab update to include CL:1913684 |
| 837 | if not self.has_control('arrow_down'): |
| 838 | logging.warning('Control arrow_down ignored. ' |
| 839 | 'Please update hdctools') |
| 840 | return |
| 841 | self.set_nocheck('arrow_down', press_secs) |
| 842 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 843 | def ctrl_d(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 844 | """Simulate Ctrl-d simultaneous button presses. |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 845 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 846 | @param press_secs: int, float, str; time to press key in seconds or |
| 847 | known shorthand: 'tab' 'press' 'long_press' |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 848 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 849 | self.set_nocheck('ctrl_d', press_secs) |
Gediminas Ramanauskas | 9da80f2 | 2012-11-14 12:59:43 -0800 | [diff] [blame] | 850 | |
Gediminas Ramanauskas | ba352ad | 2012-11-09 09:43:32 -0800 | [diff] [blame] | 851 | |
Yu-Ping Wu | 7f9c018 | 2020-05-26 09:48:48 +0800 | [diff] [blame] | 852 | def ctrl_s(self, press_secs='tab'): |
| 853 | """Simulate Ctrl-s simultaneous button presses. |
| 854 | |
| 855 | @param press_secs: int, float, str; time to press key in seconds or |
| 856 | known shorthand: 'tab' 'press' 'long_press' |
| 857 | """ |
| 858 | self.set_nocheck('ctrl_s', press_secs) |
| 859 | |
| 860 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 861 | def ctrl_u(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 862 | """Simulate Ctrl-u simultaneous button presses. |
| 863 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 864 | @param press_secs: int, float, str; time to press key in seconds or |
| 865 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 866 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 867 | self.set_nocheck('ctrl_u', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 868 | |
| 869 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 870 | def ctrl_enter(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 871 | """Simulate Ctrl-enter simultaneous button presses. |
| 872 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 873 | @param press_secs: int, float, str; time to press key in seconds or |
| 874 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 875 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 876 | self.set_nocheck('ctrl_enter', press_secs) |
Gediminas Ramanauskas | 3297d4f | 2012-09-10 15:30:10 -0700 | [diff] [blame] | 877 | |
| 878 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 879 | def ctrl_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 880 | """Simulate Enter key button press. |
| 881 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 882 | @param press_secs: int, float, str; time to press key in seconds or |
| 883 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 884 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 885 | self.set_nocheck('ctrl_key', press_secs) |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 886 | |
| 887 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 888 | def enter_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 889 | """Simulate Enter key button press. |
| 890 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 891 | @param press_secs: int, float, str; time to press key in seconds or |
| 892 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 893 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 894 | self.set_nocheck('enter_key', press_secs) |
Todd Broch | 9753bd4 | 2012-03-21 10:15:08 -0700 | [diff] [blame] | 895 | |
| 896 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 897 | def refresh_key(self, press_secs='tab'): |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 898 | """Simulate Refresh key (F3) button press. |
| 899 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 900 | @param press_secs: int, float, str; time to press key in seconds or |
| 901 | known shorthand: 'tab' 'press' 'long_press' |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 902 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 903 | self.set_nocheck('refresh_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 904 | |
| 905 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 906 | def ctrl_refresh_key(self, press_secs='tab'): |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 907 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 908 | |
| 909 | This key combination is an alternative of Space key. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 910 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 911 | @param press_secs: int, float, str; time to press key in seconds or |
| 912 | known shorthand: 'tab' 'press' 'long_press' |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 913 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 914 | self.set_nocheck('ctrl_refresh_key', press_secs) |
Tom Wai-Hong Tam | 9e61e66 | 2012-08-01 15:10:07 +0800 | [diff] [blame] | 915 | |
| 916 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 917 | def imaginary_key(self, press_secs='tab'): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 918 | """Simulate imaginary key button press. |
| 919 | |
| 920 | Maps to a key that doesn't physically exist. |
Yusuf Mohsinally | 103441a | 2014-01-14 15:25:44 -0800 | [diff] [blame] | 921 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 922 | @param press_secs: int, float, str; time to press key in seconds or |
| 923 | known shorthand: 'tab' 'press' 'long_press' |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 924 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 925 | self.set_nocheck('imaginary_key', press_secs) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 926 | |
| 927 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 928 | def sysrq_x(self, press_secs='tab'): |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 929 | """Simulate Alt VolumeUp X simulataneous press. |
| 930 | |
| 931 | This key combination is the kernel system request (sysrq) X. |
| 932 | |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 933 | @param press_secs: int, float, str; time to press key in seconds or |
| 934 | known shorthand: 'tab' 'press' 'long_press' |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 935 | """ |
Ruben Rodriguez Buchillon | 84eba75 | 2018-07-27 17:59:48 +0800 | [diff] [blame] | 936 | self.set_nocheck('sysrq_x', press_secs) |
Vincent Palatin | e7dc928 | 2016-07-14 11:31:58 +0200 | [diff] [blame] | 937 | |
| 938 | |
Tom Wai-Hong Tam | 93b5c09 | 2015-05-14 02:50:43 +0800 | [diff] [blame] | 939 | def toggle_recovery_switch(self): |
| 940 | """Toggle recovery switch on and off.""" |
| 941 | self.enable_recovery_mode() |
| 942 | time.sleep(self.REC_TOGGLE_DELAY) |
| 943 | self.disable_recovery_mode() |
| 944 | |
| 945 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 946 | def enable_recovery_mode(self): |
| 947 | """Enable recovery mode on device.""" |
| 948 | self.set('rec_mode', 'on') |
| 949 | |
| 950 | |
| 951 | def disable_recovery_mode(self): |
| 952 | """Disable recovery mode on device.""" |
| 953 | self.set('rec_mode', 'off') |
| 954 | |
| 955 | |
Tom Wai-Hong Tam | f9ded09 | 2015-05-20 05:37:22 +0800 | [diff] [blame] | 956 | def toggle_development_switch(self): |
| 957 | """Toggle development switch on and off.""" |
| 958 | self.enable_development_mode() |
| 959 | time.sleep(self.DEV_TOGGLE_DELAY) |
| 960 | self.disable_development_mode() |
| 961 | |
| 962 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 963 | def enable_development_mode(self): |
| 964 | """Enable development mode on device.""" |
| 965 | self.set('dev_mode', 'on') |
| 966 | |
| 967 | |
| 968 | def disable_development_mode(self): |
| 969 | """Disable development mode on device.""" |
| 970 | self.set('dev_mode', 'off') |
| 971 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 972 | def boot_devmode(self): |
| 973 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 974 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 975 | self.pass_devmode() |
| 976 | |
| 977 | |
| 978 | def pass_devmode(self): |
| 979 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 980 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 981 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 982 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 983 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 984 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 985 | def get_board(self): |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 986 | """Get the board connected to servod.""" |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 987 | with _WrapServoErrors(servo=self, description='get_board()'): |
Dana Goyette | afa62fd | 2020-03-16 13:45:27 -0700 | [diff] [blame] | 988 | return self._server.get_board() |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 989 | |
| 990 | |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 991 | def get_base_board(self): |
| 992 | """Get the board of the base connected to servod.""" |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame] | 993 | try: |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 994 | with _WrapServoErrors(servo=self, description='get_base_board()'): |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 995 | return self._server.get_base_board() |
Derek Beckett | a7f8c4f | 2020-10-01 10:27:38 -0700 | [diff] [blame] | 996 | except six.moves.xmlrpc_client.Fault as e: |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame] | 997 | # TODO(waihong): Remove the following compatibility check when |
| 998 | # the new versions of hdctools are deployed. |
| 999 | if 'not supported' in str(e): |
| 1000 | logging.warning('The servod is too old that get_base_board ' |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1001 | 'not supported.') |
Wai-Hong Tam | 7f6169e | 2017-09-29 09:23:48 -0700 | [diff] [blame] | 1002 | return '' |
| 1003 | raise |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 1004 | |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1005 | def get_ec_board(self): |
| 1006 | """Get the board name from EC.""" |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1007 | if self.has_control('active_dut_controller'): |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1008 | # If servo v4 is allowing dual_v4 devices, then choose the |
| 1009 | # active device. |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1010 | active_device = self.get('active_dut_controller') |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1011 | if active_device == self.get_main_servo_device(): |
| 1012 | active_device = '' |
| 1013 | else: |
| 1014 | active_device = '' |
| 1015 | return self.get('ec_board', prefix=active_device) |
Wai-Hong Tam | 0f90400 | 2017-09-19 15:52:22 -0700 | [diff] [blame] | 1016 | |
Wai-Hong Tam | cc399ee | 2017-12-08 12:43:28 -0800 | [diff] [blame] | 1017 | def get_ec_active_copy(self): |
| 1018 | """Get the active copy of the EC image.""" |
| 1019 | return self.get('ec_active_copy') |
| 1020 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1021 | def has_control(self, ctrl_name, prefix=''): |
| 1022 | """Query servod server to determine if |ctrl_name| is a valid control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1023 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1024 | @param ctrl_name Name of the control. |
| 1025 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1026 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1027 | @returns: true if |ctrl_name| is a known control, false otherwise. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1028 | """ |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1029 | ctrl_name = self._build_ctrl_name(ctrl_name, prefix) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1030 | try: |
| 1031 | # If the control exists, doc() will work. |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1032 | with _WrapServoErrors( |
| 1033 | servo=self, |
| 1034 | description='has_control(%s)->doc()' % ctrl_name): |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 1035 | self._server.doc(ctrl_name) |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1036 | return True |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1037 | except ControlUnavailableError: |
| 1038 | return False |
Todd Broch | efe72cb | 2012-07-11 19:58:53 -0700 | [diff] [blame] | 1039 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1040 | def _build_ctrl_name(self, ctrl_name, prefix): |
| 1041 | """Helper to build the control name if a prefix is used. |
| 1042 | |
| 1043 | @param ctrl_name Name of the control. |
| 1044 | @param prefix: prefix to route control to correct servo device. |
| 1045 | |
| 1046 | @returns: [|prefix|.]ctrl_name depending on whether prefix is non-empty. |
| 1047 | """ |
| 1048 | assert ctrl_name |
| 1049 | if prefix: |
| 1050 | return '%s.%s' % (prefix, ctrl_name) |
| 1051 | return ctrl_name |
| 1052 | |
| 1053 | def get(self, ctrl_name, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1054 | """Get the value of a gpio from Servod. |
| 1055 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1056 | @param ctrl_name Name of the control. |
| 1057 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1058 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1059 | @returns: server response to |ctrl_name| request. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1060 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1061 | @raise ControlUnavailableError: if |ctrl_name| not a known control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1062 | @raise error.TestFail: for all other failures doing get(). |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1063 | """ |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1064 | ctrl_name = self._build_ctrl_name(ctrl_name, prefix) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1065 | with _WrapServoErrors( |
| 1066 | servo=self, description='Getting %s' % ctrl_name): |
| 1067 | return self._server.get(ctrl_name) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1068 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1069 | def set(self, ctrl_name, ctrl_value, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1070 | """Set and check the value of a gpio using Servod. |
| 1071 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1072 | @param ctrl_name: Name of the control. |
| 1073 | @param ctrl_value: New setting for the control. |
| 1074 | @param prefix: prefix to route control to correct servo device. |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 1075 | @raise error.TestFail: if the control value fails to change. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1076 | """ |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1077 | ctrl_name = self._build_ctrl_name(ctrl_name, prefix) |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1078 | self.set_nocheck(ctrl_name, ctrl_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 1079 | retry_count = Servo.GET_RETRY_MAX |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1080 | actual_value = self.get(ctrl_name) |
| 1081 | while ctrl_value != actual_value and retry_count: |
| 1082 | logging.warning("%s != %s, retry %d", ctrl_name, ctrl_value, |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 1083 | retry_count) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame] | 1084 | retry_count -= 1 |
| 1085 | time.sleep(Servo.SHORT_DELAY) |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1086 | actual_value = self.get(ctrl_name) |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 1087 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1088 | if ctrl_value != actual_value: |
Dana Goyette | 7ff06c9 | 2019-10-11 13:38:03 -0700 | [diff] [blame] | 1089 | raise error.TestFail( |
| 1090 | 'Servo failed to set %s to %s. Got %s.' |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1091 | % (ctrl_name, ctrl_value, actual_value)) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1092 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1093 | def set_nocheck(self, ctrl_name, ctrl_value, prefix=''): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1094 | """Set the value of a gpio using Servod. |
| 1095 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1096 | @param ctrl_name Name of the control. |
| 1097 | @param ctrl_value New setting for the control. |
| 1098 | @param prefix: prefix to route control to correct servo device. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1099 | |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1100 | @raise ControlUnavailableError: if |ctrl_name| not a known control. |
Ruben Rodriguez Buchillon | 2cba8e4 | 2019-08-12 11:31:53 -0700 | [diff] [blame] | 1101 | @raise error.TestFail: for all other failures doing set(). |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1102 | """ |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1103 | ctrl_name = self._build_ctrl_name(ctrl_name, prefix) |
Ruben Rodriguez Buchillon | 1c0219c | 2019-06-25 12:30:44 -0700 | [diff] [blame] | 1104 | # The real danger here is to pass a None value through the xmlrpc. |
Ruben Rodriguez Buchillon | 5d3ced0 | 2020-02-04 14:56:35 -0800 | [diff] [blame] | 1105 | assert ctrl_value is not None |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 1106 | description = 'Setting %s to %r' % (ctrl_name, ctrl_value) |
| 1107 | logging.debug('%s', description) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1108 | with _WrapServoErrors(servo=self, description=description): |
| 1109 | self._server.set(ctrl_name, ctrl_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1110 | |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 1111 | def set_get_all(self, controls): |
| 1112 | """Set &| get one or more control values. |
| 1113 | |
| 1114 | @param controls: list of strings, controls to set &| get. |
| 1115 | |
| 1116 | @raise: error.TestError in case error occurs setting/getting values. |
| 1117 | """ |
Dana Goyette | c9e7bef | 2020-07-21 13:32:30 -0700 | [diff] [blame] | 1118 | description = 'Set/get all: %s' % str(controls) |
| 1119 | logging.debug('%s', description) |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1120 | with _WrapServoErrors(servo=self, description=description): |
| 1121 | return self._server.set_get_all(controls) |
Tom Wai-Hong Tam | 92569bb | 2013-03-26 14:25:10 +0800 | [diff] [blame] | 1122 | |
| 1123 | |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1124 | def probe_host_usb_dev(self): |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1125 | """Probe the USB disk device plugged-in the servo from the host side. |
| 1126 | |
Kevin Cheng | eb06fe7 | 2016-08-22 15:26:32 -0700 | [diff] [blame] | 1127 | 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] | 1128 | |
Kevin Cheng | a22c4a8 | 2016-10-07 14:13:25 -0700 | [diff] [blame] | 1129 | @return: String of USB disk path (e.g. '/dev/sdb') or None. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1130 | """ |
Ruben Rodriguez Buchillon | 9a4bfc3 | 2018-10-16 20:46:25 +0800 | [diff] [blame] | 1131 | # Set up Servo's usb mux. |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1132 | return self.get('image_usbkey_dev') or None |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1133 | |
| 1134 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1135 | def image_to_servo_usb(self, image_path=None, |
Otabek Kasimov | 5069131 | 2020-06-04 20:29:35 -0700 | [diff] [blame] | 1136 | make_image_noninteractive=False, |
| 1137 | power_off_dut=True): |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1138 | """Install an image to the USB key plugged into the servo. |
| 1139 | |
| 1140 | This method may copy any image to the servo USB key including a |
| 1141 | recovery image or a test image. These images are frequently used |
| 1142 | for test purposes such as restoring a corrupted image or conducting |
| 1143 | an upgrade of ec/fw/kernel as part of a test of a specific image part. |
| 1144 | |
Otabek Kasimov | 5069131 | 2020-06-04 20:29:35 -0700 | [diff] [blame] | 1145 | @param image_path: Path on the host to the recovery image. |
| 1146 | @param make_image_noninteractive: Make the recovery image |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1147 | noninteractive, therefore the DUT |
| 1148 | will reboot automatically after |
| 1149 | installation. |
Otabek Kasimov | 5069131 | 2020-06-04 20:29:35 -0700 | [diff] [blame] | 1150 | @param power_off_dut: To put the DUT in power off mode. |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1151 | """ |
J. Richard Barnette | 41320ee | 2013-03-11 13:00:13 -0700 | [diff] [blame] | 1152 | # We're about to start plugging/unplugging the USB key. We |
| 1153 | # don't know the state of the DUT, or what it might choose |
| 1154 | # to do to the device after hotplug. To avoid surprises, |
| 1155 | # force the DUT to be off. |
Otabek Kasimov | 5069131 | 2020-06-04 20:29:35 -0700 | [diff] [blame] | 1156 | if power_off_dut: |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1157 | self.get_power_state_controller().power_off() |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1158 | |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1159 | if image_path: |
Tom Wai-Hong Tam | 42f136d | 2012-10-26 11:11:23 +0800 | [diff] [blame] | 1160 | logging.info('Searching for usb device and copying image to it. ' |
| 1161 | 'Please wait a few minutes...') |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1162 | # The servod control automatically sets up the host in the host |
| 1163 | # direction. |
| 1164 | try: |
| 1165 | self.set_nocheck('download_image_to_usb_dev', image_path) |
| 1166 | except error.TestFail as e: |
| 1167 | logging.error('Failed to transfer requested image to USB. %s.' |
| 1168 | 'Please take a look at Servo Logs.', str(e)) |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1169 | raise error.AutotestError('Download image to usb failed.') |
| 1170 | if make_image_noninteractive: |
| 1171 | logging.info('Making image noninteractive') |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1172 | try: |
| 1173 | dev = self.probe_host_usb_dev() |
| 1174 | if not dev: |
| 1175 | # This is fine but also should never happen: if we |
| 1176 | # successfully download an image but somehow cannot |
| 1177 | # find the stick again, it needs to be investigated. |
| 1178 | raise error.TestFail('No image usb key detected ' |
| 1179 | 'after successful download. ' |
| 1180 | 'Please investigate.') |
| 1181 | # The modification has to happen on partition 1. |
| 1182 | dev_partition = '%s1' % dev |
| 1183 | self.set_nocheck('make_usb_dev_image_noninteractive', |
| 1184 | dev_partition) |
| 1185 | except error.TestFail as e: |
| 1186 | logging.error('Failed to make image noninteractive. %s.' |
| 1187 | 'Please take a look at Servo Logs.', |
| 1188 | str(e)) |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1189 | |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1190 | def boot_in_recovery_mode(self, snk_mode=False): |
| 1191 | """Boot host DUT in recovery mode. |
| 1192 | |
| 1193 | @param snk_mode: If True, switch servo_v4 role to 'snk' mode before |
| 1194 | boot DUT into recovery mode. |
| 1195 | """ |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1196 | # This call has a built-in delay to ensure that we wait a timeout |
| 1197 | # for the stick to enumerate and settle on the DUT side. |
Kalin Stoyanov | b3c11f3 | 2018-05-11 09:02:00 -0700 | [diff] [blame] | 1198 | self.switch_usbkey('dut') |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1199 | # Switch servo_v4 mode to snk as the DUT won't able to see usb drive |
| 1200 | # in recovery mode if the servo is in src mode(see crbug.com/1129165). |
| 1201 | if snk_mode: |
| 1202 | logging.info('Setting servo_v4 role to snk mode in order to make' |
| 1203 | ' the DUT can see usb drive while in recovery mode.') |
| 1204 | self.set_servo_v4_role('snk') |
| 1205 | |
Otabek Kasimov | b0e1e5d | 2020-05-06 18:53:51 -0700 | [diff] [blame] | 1206 | try: |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1207 | power_state = self.get_power_state_controller() |
| 1208 | power_state.power_on(rec_mode=power_state.REC_ON) |
Otabek Kasimov | b0e1e5d | 2020-05-06 18:53:51 -0700 | [diff] [blame] | 1209 | except error.TestFail as e: |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1210 | self.set_servo_v4_role('src') |
Otabek Kasimov | b0e1e5d | 2020-05-06 18:53:51 -0700 | [diff] [blame] | 1211 | logging.error('Failed to boot DUT in recovery mode. %s.', str(e)) |
| 1212 | raise error.AutotestError('Failed to boot DUT in recovery mode.') |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1213 | |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1214 | def install_recovery_image(self, |
| 1215 | image_path=None, |
| 1216 | make_image_noninteractive=False, |
| 1217 | snk_mode=False): |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 1218 | """Install the recovery image specified by the path onto the DUT. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1219 | |
| 1220 | This method uses google recovery mode to install a recovery image |
| 1221 | 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] | 1222 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1223 | we use the recovery image already on the usb image. |
| 1224 | |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1225 | This method will switch servo_v4 role to 'snk' mode in order to make |
| 1226 | the DUT can see the usb drive plugged on servo, the caller should |
| 1227 | set servo_v4 role back to 'src' mode one the DUT exit recovery mode. |
| 1228 | |
Dan Shi | c67f133 | 2016-04-06 12:38:06 -0700 | [diff] [blame] | 1229 | @param image_path: Path on the host to the recovery image. |
| 1230 | @param make_image_noninteractive: Make the recovery image |
| 1231 | noninteractive, therefore the DUT will reboot automatically |
| 1232 | after installation. |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1233 | @param snk_mode: If True, switch servo_v4 role to 'snk' mode before |
| 1234 | boot DUT into recovery mode. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1235 | """ |
Mike Truty | 49153d8 | 2012-08-21 22:27:30 -0500 | [diff] [blame] | 1236 | self.image_to_servo_usb(image_path, make_image_noninteractive) |
Garry Wang | d265681 | 2019-08-07 17:29:16 -0700 | [diff] [blame] | 1237 | # Give the DUT some time to power_off if we skip |
| 1238 | # download image to usb. (crbug.com/982993) |
| 1239 | if not image_path: |
| 1240 | time.sleep(10) |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1241 | self.boot_in_recovery_mode(snk_mode=snk_mode) |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 1242 | |
| 1243 | |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1244 | def _scp_image(self, image_path): |
| 1245 | """Copy image to the servo host. |
| 1246 | |
| 1247 | When programming a firmware image on the DUT, the image must be |
| 1248 | located on the host to which the servo device is connected. Sometimes |
| 1249 | servo is controlled by a remote host, in this case the image needs to |
Dana Goyette | d5a9554 | 2019-12-30 11:14:14 -0800 | [diff] [blame] | 1250 | be transferred to the remote host. This adds the servod port number, to |
| 1251 | make sure tests for different DUTs don't trample on each other's files. |
Namyoon Woo | d18a446 | 2020-06-03 09:37:36 -0700 | [diff] [blame] | 1252 | Along with the firmware image, any subsidiary files in the same |
| 1253 | directory shall be copied to the host as well. |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1254 | |
| 1255 | @param image_path: a string, name of the firmware image file to be |
| 1256 | transferred. |
| 1257 | @return: a string, full path name of the copied file on the remote. |
| 1258 | """ |
Namyoon Woo | d18a446 | 2020-06-03 09:37:36 -0700 | [diff] [blame] | 1259 | src_path = os.path.dirname(image_path) |
| 1260 | dest_path = os.path.join('/tmp', 'dut_%d' % self._servo_host.servo_port) |
| 1261 | logging.info('Copying %s to %s', src_path, dest_path) |
Namyoon Woo | 0093aff | 2020-06-10 09:58:02 -0700 | [diff] [blame] | 1262 | # Copy a directory, src_path to dest_path. send_file() will create a |
| 1263 | # directory named basename(src_path) under dest_path, and copy all files |
| 1264 | # in src_path to the destination. |
Namyoon Woo | d18a446 | 2020-06-03 09:37:36 -0700 | [diff] [blame] | 1265 | self._servo_host.send_file(src_path, dest_path, delete_dest=True) |
Namyoon Woo | 0093aff | 2020-06-10 09:58:02 -0700 | [diff] [blame] | 1266 | |
| 1267 | # Make a image path of the destination. |
| 1268 | # e.g. /tmp/dut_9999/EC/ec.bin |
| 1269 | rv = os.path.join(dest_path, os.path.basename(src_path)) |
| 1270 | return os.path.join(rv, os.path.basename(image_path)) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1271 | |
| 1272 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1273 | def system(self, command, timeout=3600): |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1274 | """Execute the passed in command on the servod host. |
| 1275 | |
| 1276 | @param command Command to be executed. |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1277 | @param timeout Maximum number of seconds of runtime allowed. Default to |
| 1278 | 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1279 | """ |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1280 | logging.info('Will execute on servo host: %s', command) |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1281 | self._servo_host.run(command, timeout=timeout) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1282 | |
| 1283 | |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1284 | def system_output(self, command, timeout=3600, |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1285 | ignore_status=False, args=()): |
| 1286 | """Execute the passed in command on the servod host, return stdout. |
| 1287 | |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1288 | @param command a string, the command to execute |
| 1289 | @param timeout an int, max number of seconds to wait til command |
Dan Shi | fecdaf4 | 2015-07-28 10:17:26 -0700 | [diff] [blame] | 1290 | execution completes. Default to 1 hour. |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1291 | @param ignore_status a Boolean, if true - ignore command's nonzero exit |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1292 | status, otherwise an exception will be thrown |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1293 | @param args a tuple of strings, each becoming a separate command line |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1294 | parameter for the command |
J. Richard Barnette | cdd1edf | 2015-07-24 11:39:46 -0700 | [diff] [blame] | 1295 | @return command's stdout as a string. |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 1296 | """ |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1297 | return self._servo_host.run(command, timeout=timeout, |
| 1298 | ignore_status=ignore_status, |
| 1299 | args=args).stdout.strip() |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1300 | |
| 1301 | |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1302 | def get_servo_version(self, active=False): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1303 | """Get the version of the servo, e.g., servo_v2 or servo_v3. |
| 1304 | |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1305 | @param active: Only return the servo type with the active device. |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1306 | @return: The version of the servo. |
| 1307 | |
| 1308 | """ |
Dana Goyette | 0d92eaf | 2020-08-07 22:23:10 -0700 | [diff] [blame] | 1309 | with _WrapServoErrors( |
| 1310 | servo=self, description='get_servo_version()->get_version()'): |
Dana Goyette | afa62fd | 2020-03-16 13:45:27 -0700 | [diff] [blame] | 1311 | servo_type = self._server.get_version() |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1312 | if '_and_' not in servo_type or not active: |
| 1313 | return servo_type |
| 1314 | |
| 1315 | # If servo v4 is using ccd and servo micro, modify the servo type to |
| 1316 | # reflect the active device. |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1317 | active_device = self.get('active_dut_controller') |
Mary Ruthven | 38d90af | 2019-08-16 13:13:31 -0700 | [diff] [blame] | 1318 | if active_device in servo_type: |
| 1319 | logging.info('%s is active', active_device) |
| 1320 | return 'servo_v4_with_' + active_device |
| 1321 | |
| 1322 | logging.warn("%s is active even though it's not in servo type", |
| 1323 | active_device) |
| 1324 | return servo_type |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1325 | |
| 1326 | |
Otabek Kasimov | 41301a2 | 2020-05-10 15:28:21 -0700 | [diff] [blame] | 1327 | def get_servo_type(self): |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1328 | if self._servo_type is None: |
| 1329 | self._servo_type = self.get_servo_version() |
Otabek Kasimov | 41301a2 | 2020-05-10 15:28:21 -0700 | [diff] [blame] | 1330 | return self._servo_type |
| 1331 | |
Greg Edelston | 38035e8 | 2021-02-08 12:28:40 -0700 | [diff] [blame] | 1332 | def get_servo_v4_type(self): |
| 1333 | """Return the servo_v4_type (such as 'type-c'), or None if not v4.""" |
| 1334 | if not hasattr(self, '_servo_v4_type'): |
| 1335 | if 'servo_v4' in self.get_servo_type(): |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1336 | self._servo_v4_type = self.get('root.dut_connection_type') |
Greg Edelston | 38035e8 | 2021-02-08 12:28:40 -0700 | [diff] [blame] | 1337 | else: |
| 1338 | self._servo_v4_type = None |
| 1339 | return self._servo_v4_type |
| 1340 | |
| 1341 | def is_servo_v4_type_a(self): |
| 1342 | """True if the servo is v4 and type-a, else False.""" |
| 1343 | return self.get_servo_v4_type() == 'type-a' |
| 1344 | |
| 1345 | def is_servo_v4_type_c(self): |
| 1346 | """True if the servo is v4 and type-c, else False.""" |
| 1347 | return self.get_servo_v4_type() == 'type-c' |
| 1348 | |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1349 | def get_main_servo_device(self): |
| 1350 | """Return the main servo device""" |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1351 | return self.get_servo_type().split('_with_')[-1].split('_and_')[0] |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1352 | |
| 1353 | |
| 1354 | def enable_main_servo_device(self): |
| 1355 | """Make sure the main device has control of the dut.""" |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1356 | if not self.has_control('active_dut_controller'): |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1357 | return |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1358 | self.set('active_dut_controller', self.get_main_servo_device()) |
Mary Ruthven | a3c52b8 | 2019-10-09 14:09:08 -0700 | [diff] [blame] | 1359 | |
| 1360 | |
Ruben Rodriguez Buchillon | 1823bad | 2019-09-30 17:04:34 -0700 | [diff] [blame] | 1361 | def main_device_is_ccd(self): |
| 1362 | """Whether the main servo device (no prefixes) is a ccd device.""" |
Mary Ruthven | c844d87 | 2021-01-28 13:41:21 -0800 | [diff] [blame] | 1363 | servo = self.get_servo_type() |
Mary Ruthven | 86fc161 | 2021-01-28 13:45:32 -0800 | [diff] [blame] | 1364 | return 'ccd_cr50' in servo and not self.main_device_is_flex() |
Mary Ruthven | 2724ee6 | 2019-07-16 11:16:59 -0700 | [diff] [blame] | 1365 | |
| 1366 | |
Ruben Rodriguez Buchillon | 1823bad | 2019-09-30 17:04:34 -0700 | [diff] [blame] | 1367 | def main_device_is_flex(self): |
| 1368 | """Whether the main servo device (no prefixes) is a legacy device.""" |
Mary Ruthven | 86fc161 | 2021-01-28 13:45:32 -0800 | [diff] [blame] | 1369 | servo = self.get_servo_type() |
| 1370 | return any([flex in servo for flex in self.FLEX_SERVOS]) |
Ruben Rodriguez Buchillon | 1823bad | 2019-09-30 17:04:34 -0700 | [diff] [blame] | 1371 | |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1372 | |
| 1373 | def main_device_is_active(self): |
| 1374 | """Return whether the main device is the active device. |
| 1375 | |
| 1376 | This is only relevant for a dual setup with ccd and legacy on the same |
| 1377 | DUT. The main device is the servo that has no prefix on its controls. |
| 1378 | This helper answers the question whether that device is also the |
| 1379 | active device or not. |
| 1380 | """ |
| 1381 | # TODO(coconutruben): The current implementation of the dual setup only |
| 1382 | # ever has legacy as the main device. Therefore, it suffices to ask |
| 1383 | # whether the active device is ccd. |
| 1384 | if not self.dts_mode_is_valid(): |
| 1385 | # Use dts support as a proxy to whether the servo setup could |
| 1386 | # support a dual role. Only those setups now support legacy and ccd. |
| 1387 | return True |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1388 | active_device = self.get('active_dut_controller') |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1389 | return 'ccd_cr50' not in active_device |
| 1390 | |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1391 | def _initialize_programmer(self, rw_only=False): |
| 1392 | """Initialize the firmware programmer. |
| 1393 | |
| 1394 | @param rw_only: True to initialize a programmer which only |
| 1395 | programs the RW portions. |
| 1396 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1397 | if self._programmer: |
| 1398 | return |
| 1399 | # Initialize firmware programmer |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1400 | servo_type = self.get_servo_type() |
| 1401 | if servo_type.startswith('servo_v2'): |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1402 | self._programmer = firmware_programmer.ProgrammerV2(self) |
Wai-Hong Tam | c36c4d2 | 2016-09-09 10:39:45 -0700 | [diff] [blame] | 1403 | self._programmer_rw = firmware_programmer.ProgrammerV2RwOnly(self) |
Kevin Cheng | df2e29f | 2016-09-09 02:31:22 -0700 | [diff] [blame] | 1404 | # Both servo v3 and v4 use the same programming methods so just leverage |
| 1405 | # ProgrammerV3 for servo v4 as well. |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1406 | elif (servo_type.startswith('servo_v3') |
| 1407 | or servo_type.startswith('servo_v4')): |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1408 | self._programmer = firmware_programmer.ProgrammerV3(self) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1409 | self._programmer_rw = firmware_programmer.ProgrammerV3RwOnly(self) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1410 | else: |
| 1411 | raise error.TestError( |
| 1412 | 'No firmware programmer for servo version: %s' % |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1413 | self.get_servo_type()) |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1414 | |
| 1415 | |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1416 | def program_bios(self, image, rw_only=False, copy_image=True): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1417 | """Program bios on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1418 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1419 | @param image: a string, file name of the BIOS image to program |
| 1420 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1421 | @param rw_only: True to only program the RW portion of BIOS. |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1422 | @param copy_image: True indicates we need scp the image to servohost |
| 1423 | while False means the image file is already on |
| 1424 | servohost. |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1425 | |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1426 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1427 | self._initialize_programmer() |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1428 | # We don't need scp if test runs locally. |
| 1429 | if copy_image and not self.is_localhost(): |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 1430 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1431 | if rw_only: |
| 1432 | self._programmer_rw.program_bios(image) |
| 1433 | else: |
| 1434 | self._programmer.program_bios(image) |
Vadim Bendebury | b80ba59 | 2012-12-07 15:02:34 -0800 | [diff] [blame] | 1435 | |
| 1436 | |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1437 | def program_ec(self, image, rw_only=False, copy_image=True): |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1438 | """Program ec on DUT with given image. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1439 | |
Yusuf Mohsinally | 6c078ae | 2013-11-21 11:06:42 -0800 | [diff] [blame] | 1440 | @param image: a string, file name of the EC image to program |
| 1441 | on the DUT. |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1442 | @param rw_only: True to only program the RW portion of EC. |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1443 | @param copy_image: True indicates we need scp the image to servohost |
| 1444 | while False means the image file is already on |
| 1445 | servohost. |
Vadim Bendebury | 1a7ec63 | 2013-02-17 16:22:09 -0800 | [diff] [blame] | 1446 | """ |
J. Richard Barnette | 8f19b39 | 2014-08-11 14:05:39 -0700 | [diff] [blame] | 1447 | self._initialize_programmer() |
Garry Wang | 61cfe0b | 2020-08-21 16:26:00 -0700 | [diff] [blame] | 1448 | # We don't need scp if test runs locally. |
| 1449 | if copy_image and not self.is_localhost(): |
Ricky Liang | c31aab3 | 2014-07-03 16:23:29 +0800 | [diff] [blame] | 1450 | image = self._scp_image(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1451 | if rw_only: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1452 | self._programmer_rw.program_ec(image) |
Tom Wai-Hong Tam | 4ac7898 | 2016-01-08 02:34:37 +0800 | [diff] [blame] | 1453 | else: |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1454 | self._programmer.program_ec(image) |
| 1455 | |
| 1456 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1457 | def extract_ec_image(self, board, model, tarball_path): |
| 1458 | """Helper function to extract EC image from downloaded tarball. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1459 | |
Shelley Chen | ac61d5a | 2019-06-24 15:35:46 -0700 | [diff] [blame] | 1460 | @param board: The DUT board name. |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1461 | @param model: The DUT model name. |
| 1462 | @param tarball_path: The path of the downloaded build tarball. |
Justin TerAvest | 1d0c1bb | 2019-12-13 11:53:25 -0700 | [diff] [blame] | 1463 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1464 | @return: Path to extracted EC image. |
| 1465 | """ |
Brent Peterson | 0781db7 | 2020-02-13 13:13:37 -0800 | [diff] [blame] | 1466 | |
| 1467 | # Ignore extracting EC image and re-programming if not a Chrome EC |
| 1468 | chrome_ec = FAFTConfig(board).chrome_ec |
| 1469 | if not chrome_ec: |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1470 | logging.warn('Not a Chrome EC, ignore re-programming it') |
Brent Peterson | 0781db7 | 2020-02-13 13:13:37 -0800 | [diff] [blame] | 1471 | return None |
| 1472 | |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1473 | # Try to retrieve firmware build target from the version reported |
| 1474 | # by the EC. If this doesn't work, we assume the firmware build |
| 1475 | # target is the same as the model name. |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1476 | try: |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1477 | fw_target = self.get_ec_board() |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1478 | except Exception as err: |
| 1479 | logging.warn('Failed to get ec_board value; ignoring') |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1480 | fw_target = model |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1481 | pass |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1482 | |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1483 | # Array of candidates for EC image |
| 1484 | ec_image_candidates = [ |
| 1485 | 'ec.bin', |
| 1486 | '%s/ec.bin' % fw_target, |
| 1487 | '%s/ec.bin' % board |
| 1488 | ] |
| 1489 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1490 | # Extract EC image from tarball |
| 1491 | dest_dir = os.path.join(os.path.dirname(tarball_path), 'EC') |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 1492 | ec_image = _extract_image_from_tarball(tarball_path, |
| 1493 | dest_dir, |
| 1494 | ec_image_candidates, |
| 1495 | self.EXTRACT_TIMEOUT_SECS) |
Congbin Guo | 4242761 | 2019-02-12 10:22:06 -0800 | [diff] [blame] | 1496 | |
Brent Peterson | 0781db7 | 2020-02-13 13:13:37 -0800 | [diff] [blame] | 1497 | # Check if EC image was found and return path or raise error |
Brent Peterson | 669edf4 | 2020-02-07 15:07:54 -0800 | [diff] [blame] | 1498 | if ec_image: |
Namyoon Woo | 643f0f8 | 2020-04-24 15:28:52 -0700 | [diff] [blame] | 1499 | # Extract subsidiary binaries for EC |
| 1500 | # Find a monitor binary for NPCX_UUT chip type, if any. |
| 1501 | mon_candidates = [candidate.replace('ec.bin', 'npcx_monitor.bin') |
| 1502 | for candidate in ec_image_candidates] |
| 1503 | _extract_image_from_tarball(tarball_path, dest_dir, mon_candidates, |
| 1504 | self.EXTRACT_TIMEOUT_SECS) |
| 1505 | |
Brent Peterson | 669edf4 | 2020-02-07 15:07:54 -0800 | [diff] [blame] | 1506 | return os.path.join(dest_dir, ec_image) |
| 1507 | else: |
Namyoon Woo | 34e90e9 | 2020-05-20 17:05:03 -0700 | [diff] [blame] | 1508 | raise error.TestError('Failed to extract EC image from %s' % |
Brent Peterson | 0781db7 | 2020-02-13 13:13:37 -0800 | [diff] [blame] | 1509 | tarball_path) |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1510 | |
| 1511 | |
| 1512 | def extract_bios_image(self, board, model, tarball_path): |
| 1513 | """Helper function to extract BIOS image from downloaded tarball. |
| 1514 | |
| 1515 | @param board: The DUT board name. |
| 1516 | @param model: The DUT model name. |
| 1517 | @param tarball_path: The path of the downloaded build tarball. |
| 1518 | |
| 1519 | @return: Path to extracted BIOS image. |
| 1520 | """ |
| 1521 | |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1522 | # Try to retrieve firmware build target from the version reported |
| 1523 | # by the EC. If this doesn't work, we assume the firmware build |
| 1524 | # target is the same as the model name. |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1525 | try: |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1526 | fw_target = self.get_ec_board() |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1527 | except Exception as err: |
| 1528 | logging.warn('Failed to get ec_board value; ignoring') |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1529 | fw_target = model |
Namyoon Woo | 0fdf76e | 2020-02-26 09:14:50 -0800 | [diff] [blame] | 1530 | pass |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1531 | |
Philip Chen | 6427276 | 2020-10-16 15:45:52 -0700 | [diff] [blame] | 1532 | # Array of candidates for BIOS image |
| 1533 | bios_image_candidates = [ |
| 1534 | 'image.bin', |
| 1535 | 'image-%s.bin' % fw_target, |
| 1536 | 'image-%s.bin' % board |
| 1537 | ] |
| 1538 | |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1539 | # Extract BIOS image from tarball |
| 1540 | dest_dir = os.path.join(os.path.dirname(tarball_path), 'BIOS') |
Brent Peterson | 8df4a5b | 2020-03-06 11:50:49 -0800 | [diff] [blame] | 1541 | bios_image = _extract_image_from_tarball(tarball_path, |
| 1542 | dest_dir, |
| 1543 | bios_image_candidates, |
| 1544 | self.EXTRACT_TIMEOUT_SECS) |
Brent Peterson | 1cb623a | 2020-01-09 13:14:28 -0800 | [diff] [blame] | 1545 | |
Brent Peterson | 669edf4 | 2020-02-07 15:07:54 -0800 | [diff] [blame] | 1546 | # Check if BIOS image was found and return path or raise error |
| 1547 | if bios_image: |
| 1548 | return os.path.join(dest_dir, bios_image) |
| 1549 | else: |
Namyoon Woo | 34e90e9 | 2020-05-20 17:05:03 -0700 | [diff] [blame] | 1550 | raise error.TestError('Failed to extract BIOS image from %s' % |
Brent Peterson | 669edf4 | 2020-02-07 15:07:54 -0800 | [diff] [blame] | 1551 | tarball_path) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1552 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1553 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1554 | def switch_usbkey(self, usb_state): |
| 1555 | """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] | 1556 | |
| 1557 | This function switches the servo multiplexer to provide electrical |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1558 | connection between the USB port J3 and either host or DUT side. It |
| 1559 | can also be used to turn the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1560 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1561 | @param usb_state: A string, one of 'dut', 'host', or 'off'. |
| 1562 | 'dut' and 'host' indicate which side the |
| 1563 | USB flash device is required to be connected to. |
| 1564 | 'off' indicates turning the USB port off. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1565 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1566 | @raise: error.TestError in case the parameter is not 'dut' |
| 1567 | 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1568 | """ |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1569 | if self.get_usbkey_state() == usb_state: |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1570 | return |
| 1571 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1572 | if usb_state == 'off': |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1573 | self.set_nocheck('image_usbkey_pwr', 'off') |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 1574 | return |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1575 | elif usb_state == 'host': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1576 | mux_direction = 'servo_sees_usbkey' |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1577 | elif usb_state == 'dut': |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1578 | mux_direction = 'dut_sees_usbkey' |
| 1579 | else: |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1580 | raise error.TestError('Unknown USB state request: %s' % usb_state) |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1581 | # On the servod side, this control will ensure that |
| 1582 | # - the port is power cycled if it is changing directions |
| 1583 | # - the port ends up in a powered state after this call |
| 1584 | # - if facing the host side, the call only returns once a usb block |
| 1585 | # device is detected, or after a generous timeout (10s) |
| 1586 | self.set('image_usbkey_direction', mux_direction) |
| 1587 | # As servod makes no guarantees when switching to the dut side, |
| 1588 | # add a detection delay here when facing the dut. |
Greg Edelston | 5979b7a | 2020-05-26 12:18:12 -0600 | [diff] [blame] | 1589 | if mux_direction == 'dut_sees_usbkey': |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1590 | time.sleep(self.USB_DETECTION_DELAY) |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1591 | |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1592 | def get_usbkey_state(self): |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1593 | """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] | 1594 | |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1595 | @return: A string, one of 'dut', 'host', or 'off'. |
Vadim Bendebury | e7bd936 | 2012-12-19 14:35:20 -0800 | [diff] [blame] | 1596 | """ |
Ruben Rodriguez Buchillon | 11daf2c | 2020-02-25 12:31:40 -0800 | [diff] [blame] | 1597 | pwr = self.get('image_usbkey_pwr') |
| 1598 | if pwr == 'off': |
| 1599 | return pwr |
Ruben Rodriguez Buchillon | f2de477 | 2020-03-13 16:43:47 -0700 | [diff] [blame] | 1600 | direction = self.get('image_usbkey_direction') |
| 1601 | if direction == 'servo_sees_usbkey': |
| 1602 | return 'host' |
| 1603 | if direction == 'dut_sees_usbkey': |
| 1604 | return 'dut' |
| 1605 | raise error.TestFail('image_usbkey_direction set an unknown mux ' |
| 1606 | 'direction: %s' % direction) |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1607 | |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1608 | def set_servo_v4_role(self, role): |
| 1609 | """Set the power role of servo v4, either 'src' or 'snk'. |
| 1610 | |
| 1611 | It does nothing if not a servo v4. |
| 1612 | |
| 1613 | @param role: Power role for DUT port on servo v4, either 'src' or 'snk'. |
| 1614 | """ |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1615 | if not self.get_servo_type().startswith('servo_v4'): |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1616 | logging.debug('Not a servo v4, unable to set role to %s.', role) |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1617 | return |
| 1618 | |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1619 | if not self.has_control('servo_pd_role'): |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1620 | logging.debug( |
| 1621 | 'Servo does not has servo_v4_role control, unable' |
| 1622 | ' to set role to %s.', role) |
| 1623 | return |
| 1624 | |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1625 | value = self.get('servo_pd_role') |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1626 | if value != role: |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1627 | self.set_nocheck('servo_pd_role', role) |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1628 | else: |
| 1629 | logging.debug('Already in the role: %s.', role) |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1630 | |
Dawid Niedzwiecki | f6cbdd5 | 2020-11-17 09:15:09 +0100 | [diff] [blame] | 1631 | def get_servo_v4_role(self): |
| 1632 | """Get the power role of servo v4, either 'src' or 'snk'. |
| 1633 | |
| 1634 | It returns None if not a servo v4. |
| 1635 | """ |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1636 | if not self.get_servo_type().startswith('servo_v4'): |
Dawid Niedzwiecki | f6cbdd5 | 2020-11-17 09:15:09 +0100 | [diff] [blame] | 1637 | logging.debug('Not a servo v4, unable to get role') |
| 1638 | return None |
| 1639 | |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1640 | if not self.has_control('servo_pd_role'): |
Dawid Niedzwiecki | f6cbdd5 | 2020-11-17 09:15:09 +0100 | [diff] [blame] | 1641 | logging.debug( |
| 1642 | 'Servo does not has servo_v4_role control, unable' |
| 1643 | ' to get the role.') |
| 1644 | return None |
| 1645 | |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1646 | return self.get('servo_pd_role') |
Dawid Niedzwiecki | f6cbdd5 | 2020-11-17 09:15:09 +0100 | [diff] [blame] | 1647 | |
Eric Yilun Lin | ee8e6f5 | 2020-02-26 16:08:39 +0800 | [diff] [blame] | 1648 | def set_servo_v4_pd_comm(self, en): |
| 1649 | """Set the PD communication of servo v4, either 'on' or 'off'. |
| 1650 | |
| 1651 | It does nothing if not a servo v4. |
| 1652 | |
| 1653 | @param en: a string of 'on' or 'off' for PD communication. |
| 1654 | """ |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1655 | if self.get_servo_type().startswith('servo_v4'): |
Otabek Kasimov | 8855f4d | 2021-05-18 18:22:04 -0700 | [diff] [blame^] | 1656 | self.set_nocheck('servo_pd_comm', en) |
Eric Yilun Lin | ee8e6f5 | 2020-02-26 16:08:39 +0800 | [diff] [blame] | 1657 | else: |
| 1658 | logging.debug('Not a servo v4, unable to set PD comm to %s.', en) |
Wai-Hong Tam | 6037726 | 2018-03-01 10:55:39 -0800 | [diff] [blame] | 1659 | |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1660 | def supports_built_in_pd_control(self): |
| 1661 | """Return whether the servo type supports pd charging and control.""" |
Greg Edelston | 38035e8 | 2021-02-08 12:28:40 -0700 | [diff] [blame] | 1662 | # Only servo v4 type-c supports this feature. |
| 1663 | if not self.is_servo_v4_type_c(): |
| 1664 | logging.info('PD controls require a servo v4 type-c.') |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1665 | return False |
| 1666 | # Lastly, one cannot really do anything without a plugged in charger. |
| 1667 | chg_port_mv = self.get('ppchg5_mv') |
| 1668 | if chg_port_mv < V4_CHG_ATTACHED_MIN_VOLTAGE_MV: |
Otabek Kasimov | ed03171 | 2021-01-29 17:45:25 -0800 | [diff] [blame] | 1669 | logging.info( |
| 1670 | 'It appears that no charger is plugged into servo v4. ' |
| 1671 | 'Charger port voltage: %dmV', chg_port_mv) |
Ruben Rodriguez Buchillon | 5f7ee68 | 2019-09-30 12:50:09 -0700 | [diff] [blame] | 1672 | return False |
| 1673 | logging.info('Charger port voltage: %dmV', chg_port_mv) |
| 1674 | return True |
| 1675 | |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1676 | def dts_mode_is_valid(self): |
| 1677 | """Return whether servo setup supports dts mode control for cr50.""" |
Greg Edelston | 38035e8 | 2021-02-08 12:28:40 -0700 | [diff] [blame] | 1678 | # Only servo v4 type-c supports this feature. |
| 1679 | return self.is_servo_v4_type_c() |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1680 | |
| 1681 | def dts_mode_is_safe(self): |
| 1682 | """Return whether servo setup supports dts mode without losing access. |
| 1683 | |
| 1684 | DTS mode control exists but the main device might go through ccd. |
| 1685 | In that case, it's only safe to control dts mode if the main device |
| 1686 | is legacy as otherwise the connection to the main device cuts out. |
| 1687 | """ |
| 1688 | return self.dts_mode_is_valid() and self.main_device_is_flex() |
| 1689 | |
| 1690 | def get_dts_mode(self): |
| 1691 | """Return servo dts mode. |
| 1692 | |
| 1693 | @returns: on/off whether dts is on or off |
| 1694 | """ |
| 1695 | if not self.dts_mode_is_valid(): |
| 1696 | logging.info('Not a valid servo setup. Unable to get dts mode.') |
| 1697 | return |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1698 | return self.get('servo_dts_mode') |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1699 | |
Mary Ruthven | 9003558 | 2020-06-18 12:17:15 -0700 | [diff] [blame] | 1700 | def ccd_watchdog_enable(self, enable): |
| 1701 | """Control the ccd watchdog.""" |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1702 | if 'ccd' not in self.get_servo_type(): |
Mary Ruthven | 9003558 | 2020-06-18 12:17:15 -0700 | [diff] [blame] | 1703 | return |
| 1704 | if self._ccd_watchdog_disabled and enable: |
| 1705 | logging.info('CCD watchdog disabled for test') |
| 1706 | return |
| 1707 | control = 'watchdog_add' if enable else 'watchdog_remove' |
| 1708 | self.set_nocheck(control, 'ccd') |
| 1709 | |
| 1710 | def disable_ccd_watchdog_for_test(self): |
| 1711 | """Prevent servo from enabling the watchdog.""" |
| 1712 | self._ccd_watchdog_disabled = True |
| 1713 | self.ccd_watchdog_enable(False) |
| 1714 | |
| 1715 | def allow_ccd_watchdog_for_test(self): |
| 1716 | """Allow servo to enable the ccd watchdog.""" |
| 1717 | self._ccd_watchdog_disabled = False |
| 1718 | self.ccd_watchdog_enable(True) |
| 1719 | |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1720 | def set_dts_mode(self, state): |
| 1721 | """Set servo dts mode to off or on. |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1722 | |
| 1723 | It does nothing if not a servo v4. Disable the ccd watchdog if we're |
| 1724 | disabling dts mode. CCD will disconnect. The watchdog only allows CCD |
| 1725 | to disconnect for 10 seconds until it kills servod. Disable the |
| 1726 | watchdog, so CCD can stay disconnected indefinitely. |
| 1727 | |
| 1728 | @param state: Set servo v4 dts mode 'off' or 'on'. |
| 1729 | """ |
Ruben Rodriguez Buchillon | 82c94ad | 2019-09-30 14:49:25 -0700 | [diff] [blame] | 1730 | if not self.dts_mode_is_valid(): |
| 1731 | logging.info('Not a valid servo setup. Unable to set dts mode %s.', |
| 1732 | state) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1733 | return |
| 1734 | |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1735 | enable_watchdog = state == 'on' |
| 1736 | |
Mary Ruthven | 9003558 | 2020-06-18 12:17:15 -0700 | [diff] [blame] | 1737 | if not enable_watchdog: |
| 1738 | self.ccd_watchdog_enable(False) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1739 | |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1740 | self.set_nocheck('servo_dts_mode', state) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1741 | |
Mary Ruthven | 9003558 | 2020-06-18 12:17:15 -0700 | [diff] [blame] | 1742 | if enable_watchdog: |
| 1743 | self.ccd_watchdog_enable(True) |
Mary Ruthven | 739b292 | 2019-08-22 11:16:06 -0700 | [diff] [blame] | 1744 | |
| 1745 | |
Ruben Rodriguez Buchillon | 6f36a96 | 2019-10-02 14:55:59 -0700 | [diff] [blame] | 1746 | def _get_servo_type_fw_version(self, servo_type, prefix=''): |
| 1747 | """Helper to handle fw retrieval for micro/v4 vs ccd. |
| 1748 | |
Mary Ruthven | 5b0ed10 | 2021-01-28 13:46:43 -0800 | [diff] [blame] | 1749 | @param servo_type: one of 'servo_v4', 'servo_micro', 'ccd_cr50', 'c2d2' |
Ruben Rodriguez Buchillon | 6f36a96 | 2019-10-02 14:55:59 -0700 | [diff] [blame] | 1750 | @param prefix: whether the control has a prefix |
| 1751 | |
| 1752 | @returns: fw version for non-ccd devices, cr50 version for ccd device |
| 1753 | """ |
| 1754 | if servo_type == 'ccd_cr50': |
| 1755 | # ccd_cr50 runs on cr50, so need to query the cr50 fw. |
| 1756 | servo_type = 'cr50' |
| 1757 | cmd = '%s_version' % servo_type |
| 1758 | try: |
| 1759 | return self.get(cmd, prefix=prefix) |
| 1760 | except error.TestFail: |
| 1761 | # Do not fail here, simply report the version as unknown. |
| 1762 | logging.warn('Unable to query %r to get servo fw version.', cmd) |
| 1763 | return 'unknown' |
| 1764 | |
| 1765 | |
| 1766 | def get_servo_fw_versions(self): |
| 1767 | """Retrieve a summary of attached servos and their firmware. |
| 1768 | |
| 1769 | Note: that only the Google firmware owned servos supports this e.g. |
| 1770 | micro, v4, etc. For anything else, the dictionary will have no entry. |
| 1771 | If no device is has Google owned firmware (e.g. v3) then the result |
| 1772 | is an empty dictionary. |
| 1773 | |
| 1774 | @returns: dict, a collection of each attached servo & their firmware. |
| 1775 | """ |
| 1776 | def get_fw_version_tag(tag, dev): |
| 1777 | return '%s_version.%s' % (dev, tag) |
| 1778 | |
| 1779 | fw_versions = {} |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1780 | # Note, this works because v4p1 starts with v4 as well. |
| 1781 | # TODO(coconutruben): make this more robust so that it can work on |
| 1782 | # a future v-whatever as well. |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1783 | if 'servo_v4' not in self.get_servo_type(): |
Ruben Rodriguez Buchillon | 6f36a96 | 2019-10-02 14:55:59 -0700 | [diff] [blame] | 1784 | return {} |
Otabek Kasimov | 7d10e41 | 2021-04-30 15:19:50 -0700 | [diff] [blame] | 1785 | # v4 or v4p1 |
| 1786 | v4_flavor = self.get_servo_type().split('_with_')[0] |
| 1787 | v4_tag = get_fw_version_tag('root', v4_flavor) |
| 1788 | fw_versions[v4_tag] = self._get_servo_type_fw_version('servo_fw', |
| 1789 | prefix='root') |
Otabek Kasimov | b887a00 | 2020-12-29 02:54:38 -0800 | [diff] [blame] | 1790 | if 'with' in self.get_servo_type(): |
| 1791 | dut_devs = self.get_servo_type().split('_with_')[1].split('_and_') |
Ruben Rodriguez Buchillon | 6f36a96 | 2019-10-02 14:55:59 -0700 | [diff] [blame] | 1792 | main_tag = get_fw_version_tag('main', dut_devs[0]) |
| 1793 | fw_versions[main_tag] = self._get_servo_type_fw_version(dut_devs[0]) |
| 1794 | if len(dut_devs) == 2: |
| 1795 | # Right now, the only way for this to happen is for a dual setup |
| 1796 | # to exist where ccd is attached on top of servo micro. Thus, we |
| 1797 | # know that the prefix is ccd_cr50 and the type is ccd_cr50. |
| 1798 | # TODO(coconutruben): If the new servod is not deployed by |
| 1799 | # the time that there are more cases of '_and_' devices, |
| 1800 | # this needs to be reworked. |
| 1801 | dual_tag = get_fw_version_tag('ccd_flex_secondary', dut_devs[1]) |
| 1802 | fw = self._get_servo_type_fw_version(dut_devs[1], 'ccd_cr50') |
| 1803 | fw_versions[dual_tag] = fw |
| 1804 | return fw_versions |
| 1805 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1806 | @property |
| 1807 | def uart_logs_dir(self): |
| 1808 | """Return the directory to save UART logs.""" |
| 1809 | return self._uart.logs_dir if self._uart else "" |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1810 | |
Congbin Guo | fc3b896 | 2019-03-22 17:38:46 -0700 | [diff] [blame] | 1811 | |
| 1812 | @uart_logs_dir.setter |
| 1813 | def uart_logs_dir(self, logs_dir): |
| 1814 | """Set directory to save UART logs. |
| 1815 | |
| 1816 | @param logs_dir String of directory name.""" |
Mary Ruthven | f2dd7a4 | 2020-08-12 16:18:24 -0700 | [diff] [blame] | 1817 | self._uart.logs_dir = logs_dir |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1818 | |
Mary Ruthven | f2dd7a4 | 2020-08-12 16:18:24 -0700 | [diff] [blame] | 1819 | def get_uart_logfile(self, uart): |
| 1820 | """Return the path to the uart log file.""" |
| 1821 | return self._uart.get_logfile(uart) |
| 1822 | |
| 1823 | def record_uart_capture(self, outdir=None): |
| 1824 | """Save uart stream output.""" |
| 1825 | if outdir and not self.uart_logs_dir: |
| 1826 | self.uart_logs_dir = outdir |
| 1827 | self._uart.dump() |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1828 | |
Ruben Rodriguez Buchillon | 93084d0 | 2020-01-21 15:17:36 -0800 | [diff] [blame] | 1829 | def close(self, outdir=None): |
Congbin Guo | a1f9cba | 2018-07-03 11:36:59 -0700 | [diff] [blame] | 1830 | """Close the servo object.""" |
Garry Wang | 53fc8f3 | 2020-09-18 13:30:08 -0700 | [diff] [blame] | 1831 | # We want to ensure that servo_v4 is in src mode to avoid DUTs |
| 1832 | # left in discharge state after a task. |
| 1833 | try: |
| 1834 | self.set_servo_v4_role('src') |
| 1835 | except Exception as e: |
| 1836 | logging.info( |
| 1837 | 'Unexpected error while setting servo_v4 role' |
| 1838 | ' to src; %s', e) |
| 1839 | |
Mary Ruthven | f2dd7a4 | 2020-08-12 16:18:24 -0700 | [diff] [blame] | 1840 | self._uart.stop_capture() |
| 1841 | self.record_uart_capture(outdir) |
Gregory Nisbet | 1f18f88 | 2020-07-07 10:31:59 -0700 | [diff] [blame] | 1842 | |
| 1843 | def ec_reboot(self): |
| 1844 | """Reboot Just the embedded controller.""" |
| 1845 | self.set_nocheck('ec_uart_flush', 'off') |
| 1846 | self.set_nocheck('ec_uart_cmd', 'reboot') |
| 1847 | self.set_nocheck('ec_uart_flush', 'on') |
Dawid Niedzwiecki | de89cd1 | 2020-10-16 10:10:02 +0200 | [diff] [blame] | 1848 | |
| 1849 | def get_vbus_voltage(self): |
| 1850 | """Get the voltage of VBUS'. |
| 1851 | |
| 1852 | @returns The voltage of VBUS, if vbus_voltage is supported. |
| 1853 | None , if vbus_voltage is not supported. |
| 1854 | """ |
| 1855 | if not self.has_control('vbus_voltage'): |
| 1856 | logging.debug('Servo does not have vbus_voltage control,' |
| 1857 | 'unable to get vbus voltage') |
| 1858 | return None |
| 1859 | |
| 1860 | return self.get('vbus_voltage') |