Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
| 5 | # Expects to be run in an environment with sudo and no interactive password |
| 6 | # prompt, such as within the Chromium OS development chroot. |
| 7 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 8 | import logging, os, subprocess, time, xmlrpclib |
| 9 | from autotest_lib.client.bin import utils as client_utils |
| 10 | from autotest_lib.server import utils |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 11 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 12 | class Servo: |
| 13 | """Manages control of a Servo board. |
| 14 | |
| 15 | Servo is a board developed by hardware group to aide in the debug and |
| 16 | control of various partner devices. Servo's features include the simulation |
| 17 | of pressing the power button, closing the lid, and pressing Ctrl-d. This |
| 18 | class manages setting up and communicating with a servo demon (servod) |
| 19 | process. It provides both high-level functions for common servo tasks and |
| 20 | low-level functions for directly setting and reading gpios. |
| 21 | """ |
| 22 | |
| 23 | _server = None |
| 24 | _servod = None |
| 25 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 26 | # Power button press delays in seconds. |
| 27 | LONG_DELAY = 8 |
| 28 | SHORT_DELAY = 0.1 |
| 29 | NORMAL_TRANSITION_DELAY = 1.2 |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 30 | # Maximum number of times to re-read power button on release. |
| 31 | RELEASE_RETRY_MAX = 5 |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame^] | 32 | GET_RETRY_MAX = 10 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 33 | |
| 34 | # Delays to deal with computer transitions. |
| 35 | SLEEP_DELAY = 6 |
| 36 | BOOT_DELAY = 10 |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 37 | RECOVERY_BOOT_DELAY = 30 |
| 38 | RECOVERY_INSTALL_DELAY = 180 |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 39 | |
| 40 | # Servo-specific delays. |
| 41 | MAX_SERVO_STARTUP_DELAY = 10 |
| 42 | SERVO_SEND_SIGNAL_DELAY = 0.5 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 43 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 44 | # Time between an usb disk plugged-in and detected in the system. |
| 45 | USB_DETECTION_DELAY = 10 |
| 46 | |
Tom Wai-Hong Tam | 5fc2c79 | 2011-11-03 13:05:39 +0800 | [diff] [blame] | 47 | def __init__(self, servo_host=None, servo_port=None, |
| 48 | xml_config=['servo.xml'], servo_vid=None, servo_pid=None, |
| 49 | servo_serial=None, cold_reset=False): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 50 | """Sets up the servo communication infrastructure. |
| 51 | |
| 52 | Args: |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 53 | servo_host: Host the servod process should listen on. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 54 | servo_port: Port the servod process should listen on. |
Tom Wai-Hong Tam | 5fc2c79 | 2011-11-03 13:05:39 +0800 | [diff] [blame] | 55 | xml_config: A list of configuration XML files for servod. |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 56 | servo_vid: USB vendor id of servo. |
| 57 | servo_pid: USB product id of servo. |
| 58 | servo_serial: USB serial id in device descriptor to host to |
| 59 | distinguish and control multiple servos. Note servo's EEPROM must |
| 60 | be programmed to use this feature. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 61 | cold_reset: If True, cold reset device and boot during init, |
| 62 | otherwise perform init with device running. |
| 63 | """ |
| 64 | # launch servod |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 65 | self._servod = None |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 66 | |
Todd Broch | 96d83aa | 2011-08-29 14:37:38 -0700 | [diff] [blame] | 67 | if not servo_port: |
| 68 | servo_port = 9999 |
| 69 | # TODO(tbroch) In case where servo h/w is not connected to the host |
| 70 | # running the autotest server, servod will need to be launched by |
| 71 | # another means (udev likely). For now we can assume servo_host == |
| 72 | # localhost as one hueristic way of determining this. |
| 73 | if not servo_host or servo_host == 'localhost': |
| 74 | servo_host = 'localhost' |
| 75 | self._launch_servod(servo_host, servo_port, xml_config, servo_vid, |
| 76 | servo_pid, servo_serial) |
| 77 | else: |
| 78 | logging.info('servod should already be running on host = %s', |
| 79 | servo_host) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 80 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 81 | self._do_cold_reset = cold_reset |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 82 | self._connect_servod(servo_host, servo_port) |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def initialize_dut(self): |
| 86 | """Initializes a dut for testing purposes.""" |
| 87 | if self._do_cold_reset: |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 88 | self._init_seq_cold_reset_devmode() |
| 89 | else: |
| 90 | self._init_seq() |
| 91 | |
| 92 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 93 | def power_long_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 94 | """Simulate a long power button press.""" |
| 95 | self.power_key(Servo.LONG_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def power_normal_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 99 | """Simulate a normal power button press.""" |
| 100 | self.power_key() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def power_short_press(self): |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 104 | """Simulate a short power button press.""" |
| 105 | self.power_key(Servo.SHORT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 106 | |
| 107 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 108 | def power_key(self, secs=NORMAL_TRANSITION_DELAY): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 109 | """Simulate a power button press. |
| 110 | |
| 111 | Args: |
| 112 | secs: Time in seconds to simulate the keypress. |
| 113 | """ |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 114 | self.set_nocheck('pwr_button', 'press') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 115 | time.sleep(secs) |
Todd Broch | 31c8250 | 2011-08-29 08:14:39 -0700 | [diff] [blame] | 116 | self.set_nocheck('pwr_button', 'release') |
| 117 | # TODO(tbroch) Different systems have different release times on the |
| 118 | # power button that this loop addresses. Longer term we may want to |
| 119 | # make this delay platform specific. |
| 120 | retry = 1 |
| 121 | while True: |
| 122 | value = self.get('pwr_button') |
| 123 | if value == 'release' or retry > Servo.RELEASE_RETRY_MAX: |
| 124 | break |
| 125 | logging.info('Waiting for pwr_button to release, retry %d.' % retry) |
| 126 | retry += 1 |
| 127 | time.sleep(Servo.SHORT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def lid_open(self): |
| 131 | """Simulate opening the lid.""" |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 132 | self.set_nocheck('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def lid_close(self): |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 136 | """Simulate closing the lid. |
| 137 | |
| 138 | Waits 6 seconds to ensure the device is fully asleep before returning. |
| 139 | """ |
| 140 | self.set_nocheck('lid_open', 'no') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 141 | time.sleep(Servo.SLEEP_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 142 | |
| 143 | |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 144 | def _press_and_release_keys(self, m1, m2, press_secs=None): |
| 145 | """Simulate button presses.""" |
| 146 | if press_secs is None: |
| 147 | press_secs = Servo.SERVO_SEND_SIGNAL_DELAY |
| 148 | |
| 149 | self.set_nocheck('kbd_en', 'on') |
| 150 | self.set_nocheck('kbd_m1', m1) |
| 151 | self.set_nocheck('kbd_m2', m2) |
| 152 | time.sleep(press_secs) |
| 153 | self.set_nocheck('kbd_en', 'off') |
| 154 | |
| 155 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 156 | def ctrl_d(self): |
| 157 | """Simulate Ctrl-d simultaneous button presses.""" |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 158 | self._press_and_release_keys('d', 'ctrl') |
| 159 | |
| 160 | |
| 161 | def d_key(self): |
| 162 | """Simulate Enter key button press.""" |
| 163 | self._press_and_release_keys('d', 'none') |
| 164 | |
| 165 | |
| 166 | def ctrl_key(self): |
| 167 | """Simulate Enter key button press.""" |
| 168 | self._press_and_release_keys('none', 'ctrl') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 169 | |
| 170 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 171 | def enter_key(self): |
| 172 | """Simulate Enter key button press.""" |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 173 | self._press_and_release_keys('enter', 'none') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 174 | |
| 175 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 176 | def refresh_key(self): |
| 177 | """Simulate Refresh key (F3) button press.""" |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 178 | self._press_and_release_keys('none', 'refresh') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 179 | |
| 180 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 181 | def imaginary_key(self): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 182 | """Simulate imaginary key button press. |
| 183 | |
| 184 | Maps to a key that doesn't physically exist. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 185 | """ |
Todd Broch | 9dfc3a8 | 2011-11-01 08:09:28 -0700 | [diff] [blame] | 186 | self._press_and_release_keys('none', 'unused') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 187 | |
| 188 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 189 | def enable_recovery_mode(self): |
| 190 | """Enable recovery mode on device.""" |
| 191 | self.set('rec_mode', 'on') |
| 192 | |
| 193 | |
| 194 | def disable_recovery_mode(self): |
| 195 | """Disable recovery mode on device.""" |
| 196 | self.set('rec_mode', 'off') |
| 197 | |
| 198 | |
| 199 | def enable_development_mode(self): |
| 200 | """Enable development mode on device.""" |
| 201 | self.set('dev_mode', 'on') |
| 202 | |
| 203 | |
| 204 | def disable_development_mode(self): |
| 205 | """Disable development mode on device.""" |
| 206 | self.set('dev_mode', 'off') |
| 207 | |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 208 | def enable_usb_hub(self, host=False): |
Craig Harrison | 86b1a57 | 2011-08-12 11:26:52 -0700 | [diff] [blame] | 209 | """Enable Servo's USB/ethernet hub. |
| 210 | |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 211 | This is equivalent to plugging in the USB devices attached to Servo to |
| 212 | the host (if |host| is True) or dut (if |host| is False). |
| 213 | For host=False, requires that the USB out on the servo board is |
| 214 | connected to a USB in port on the target device. Servo's USB ports are |
| 215 | labeled DUT_HUB_USB1 and DUT_HUB_USB2. Servo's ethernet port is also |
| 216 | connected to this hub. Servo's USB port DUT_HUB_IN is the output of the |
| 217 | hub. |
Craig Harrison | 86b1a57 | 2011-08-12 11:26:52 -0700 | [diff] [blame] | 218 | """ |
| 219 | self.set('dut_hub_pwren', 'on') |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 220 | if host: |
| 221 | self.set('usb_mux_oe1', 'on') |
| 222 | self.set('usb_mux_sel1', 'servo_sees_usbkey') |
| 223 | else: |
| 224 | self.set('dut_hub_sel', 'dut_sees_hub') |
| 225 | |
Craig Harrison | 86b1a57 | 2011-08-12 11:26:52 -0700 | [diff] [blame] | 226 | self.set('dut_hub_on', 'yes') |
| 227 | |
| 228 | |
| 229 | def disable_usb_hub(self): |
| 230 | """Disable Servo's USB/ethernet hub. |
| 231 | |
| 232 | This is equivalent to unplugging the USB devices attached to Servo. |
| 233 | """ |
| 234 | self.set('dut_hub_on', 'no') |
| 235 | |
| 236 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 237 | def boot_devmode(self): |
| 238 | """Boot a dev-mode device that is powered off.""" |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 239 | self.power_short_press() |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 240 | self.pass_devmode() |
| 241 | |
| 242 | |
| 243 | def pass_devmode(self): |
| 244 | """Pass through boot screens in dev-mode.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 245 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 246 | self.ctrl_d() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 247 | time.sleep(Servo.BOOT_DELAY) |
Craig Harrison | 4899726 | 2011-06-27 14:31:10 -0700 | [diff] [blame] | 248 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 249 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 250 | def cold_reset(self): |
| 251 | """Perform a cold reset of the EC. |
| 252 | |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 253 | Has the side effect of shutting off the device. Device is guaranteed |
| 254 | to be off at the end of this call. |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 255 | """ |
| 256 | self.set('cold_reset', 'on') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 257 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def warm_reset(self): |
| 261 | """Perform a warm reset of the device. |
| 262 | |
| 263 | Has the side effect of restarting the device. |
| 264 | """ |
| 265 | self.set('warm_reset', 'on') |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 266 | time.sleep(Servo.SERVO_SEND_SIGNAL_DELAY) |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 267 | self.set('warm_reset', 'off') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 268 | |
| 269 | |
| 270 | def get(self, gpio_name): |
| 271 | """Get the value of a gpio from Servod.""" |
| 272 | assert gpio_name |
| 273 | return self._server.get(gpio_name) |
| 274 | |
| 275 | |
| 276 | def set(self, gpio_name, gpio_value): |
| 277 | """Set and check the value of a gpio using Servod.""" |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 278 | self.set_nocheck(gpio_name, gpio_value) |
Todd Broch | cf7c665 | 2012-02-24 13:03:59 -0800 | [diff] [blame^] | 279 | retry_count = Servo.GET_RETRY_MAX |
| 280 | while gpio_value != self.get(gpio_name) and retry_count: |
| 281 | logging.warn("%s != %s, retry %d", gpio_name, gpio_value, |
| 282 | retry_count) |
| 283 | retry_count -= 1 |
| 284 | time.sleep(Servo.SHORT_DELAY) |
| 285 | if not retry_count: |
| 286 | assert gpio_value == self.get(gpio_name), \ |
| 287 | 'Servo failed to set %s to %s' % (gpio_name, gpio_value) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 288 | |
| 289 | |
| 290 | def set_nocheck(self, gpio_name, gpio_value): |
| 291 | """Set the value of a gpio using Servod.""" |
| 292 | assert gpio_name and gpio_value |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 293 | logging.info('Setting %s to %s' % (gpio_name, gpio_value)) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 294 | self._server.set(gpio_name, gpio_value) |
| 295 | |
| 296 | |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 297 | # TODO(waihong) It may fail if multiple servo's are connected to the same |
| 298 | # host. Should look for a better way, like the USB serial name, to identify |
| 299 | # the USB device. |
| 300 | def probe_host_usb_dev(self): |
| 301 | """Probe the USB disk device plugged-in the servo from the host side. |
| 302 | |
| 303 | It tries to switch the USB mux to make the host unable to see the |
| 304 | USB disk and compares the result difference. |
| 305 | |
| 306 | This only works if the servo is attached to the local host. |
| 307 | |
| 308 | Returns: |
| 309 | A string of USB disk path, like '/dev/sdb', or None if not existed. |
| 310 | """ |
| 311 | cmd = 'ls /dev/sd[a-z]' |
| 312 | original_value = self.get('usb_mux_sel1') |
| 313 | |
| 314 | # Make the host unable to see the USB disk. |
| 315 | if original_value != 'dut_sees_usbkey': |
| 316 | self.set('usb_mux_sel1', 'dut_sees_usbkey') |
| 317 | time.sleep(self.USB_DETECTION_DELAY) |
| 318 | no_usb_set = set(utils.system_output(cmd, ignore_status=True).split()) |
| 319 | |
| 320 | # Make the host able to see the USB disk. |
| 321 | self.set('usb_mux_sel1', 'servo_sees_usbkey') |
| 322 | time.sleep(self.USB_DETECTION_DELAY) |
| 323 | has_usb_set = set(utils.system_output(cmd, ignore_status=True).split()) |
| 324 | |
| 325 | # Back to its original value. |
| 326 | if original_value != 'servo_sees_usbkey': |
| 327 | self.set('usb_mux_sel1', original_value) |
| 328 | time.sleep(self.USB_DETECTION_DELAY) |
| 329 | |
| 330 | diff_set = has_usb_set - no_usb_set |
| 331 | if len(diff_set) == 1: |
| 332 | return diff_set.pop() |
| 333 | else: |
| 334 | return None |
| 335 | |
| 336 | |
Tom Wai-Hong Tam | a07115e | 2012-01-09 12:27:01 +0800 | [diff] [blame] | 337 | def install_recovery_image(self, image_path=None, usb_dev=None, |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 338 | wait_for_completion=True): |
| 339 | """Install the recovery image specied by the path onto the DUT. |
| 340 | |
| 341 | This method uses google recovery mode to install a recovery image |
| 342 | 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] | 343 | board specified by the usb_dev. If no image path is specified |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 344 | we use the recovery image already on the usb image. |
| 345 | |
| 346 | Args: |
| 347 | image_path: Path on the host to the recovery image. |
Tom Wai-Hong Tam | a07115e | 2012-01-09 12:27:01 +0800 | [diff] [blame] | 348 | usb_dev: When servo_sees_usbkey is enabled, which dev |
| 349 | e.g. /dev/sdb will the usb key show up as. |
| 350 | If None, detects it automatically. |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 351 | wait_for_completion: Whether to wait for completion of the |
| 352 | factory install and disable the USB hub |
| 353 | before returning. Currently this is just |
| 354 | a hardcoded wait of RECOVERY_INSTALL_DELAY |
| 355 | (for the recovery process to complete). |
| 356 | """ |
| 357 | # Set up Servo's usb mux. |
| 358 | self.set('prtctl4_pwren', 'on') |
| 359 | self.enable_usb_hub(host=True) |
Tom Wai-Hong Tam | a07115e | 2012-01-09 12:27:01 +0800 | [diff] [blame] | 360 | if image_path: |
| 361 | if not usb_dev: |
| 362 | usb_dev = self.probe_host_usb_dev() |
| 363 | logging.info('Installing image onto usb stick. ' |
| 364 | 'This takes a while...') |
| 365 | client_utils.poll_for_condition( |
| 366 | lambda: os.path.exists(usb_dev), |
| 367 | timeout=Servo.USB_DETECTION_DELAY, |
| 368 | desc="%s exists" % usb_dev) |
| 369 | utils.system(' '.join( |
| 370 | ['sudo', 'dd', 'if=%s' % image_path, |
| 371 | 'of=%s' % usb_dev, 'bs=4M'])) |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 372 | |
| 373 | # Turn the device off. |
| 374 | self.power_normal_press() |
| 375 | time.sleep(Servo.SLEEP_DELAY) |
| 376 | |
| 377 | # Boot in recovery mode. |
| 378 | try: |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 379 | self.enable_recovery_mode() |
Tom Wai-Hong Tam | 2386910 | 2012-01-17 15:41:30 +0800 | [diff] [blame] | 380 | self.power_short_press() |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 381 | time.sleep(Servo.RECOVERY_BOOT_DELAY) |
Tom Wai-Hong Tam | 922ed05 | 2012-01-09 12:27:52 +0800 | [diff] [blame] | 382 | self.set('usb_mux_sel1', 'dut_sees_usbkey') |
Jon Salz | c88e5b6 | 2011-11-30 14:38:54 +0800 | [diff] [blame] | 383 | self.disable_recovery_mode() |
| 384 | |
| 385 | if wait_for_completion: |
| 386 | # Enable recovery installation. |
| 387 | logging.info('Running the recovery process on the DUT. ' |
| 388 | 'Waiting %d seconds for recovery to complete ...', |
| 389 | Servo.RECOVERY_INSTALL_DELAY) |
| 390 | time.sleep(Servo.RECOVERY_INSTALL_DELAY) |
| 391 | |
| 392 | # Go back into normal mode and reboot. |
| 393 | # Machine automatically reboots after the usb key is removed. |
| 394 | logging.info('Removing the usb key from the DUT.') |
| 395 | self.disable_usb_hub() |
| 396 | time.sleep(Servo.BOOT_DELAY) |
| 397 | except: |
| 398 | # In case anything went wrong we want to make sure to do a clean |
| 399 | # reset. |
| 400 | self.disable_recovery_mode() |
| 401 | self.warm_reset() |
| 402 | raise |
| 403 | |
| 404 | |
Craig Harrison | 6b36b12 | 2011-06-28 17:58:43 -0700 | [diff] [blame] | 405 | def _init_seq_cold_reset_devmode(self): |
| 406 | """Cold reset, init device, and boot in dev-mode.""" |
| 407 | self.cold_reset() |
| 408 | self._init_seq() |
| 409 | self.set('dev_mode', 'on') |
| 410 | self.boot_devmode() |
| 411 | |
| 412 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 413 | def __del__(self): |
| 414 | """Kill the Servod process.""" |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 415 | if not self._servod: |
| 416 | return |
| 417 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 418 | # kill servod one way or another |
| 419 | try: |
| 420 | # won't work without superuser privileges |
| 421 | self._servod.terminate() |
| 422 | except: |
| 423 | # should work without superuser privileges |
| 424 | assert subprocess.call(['sudo', 'kill', str(self._servod.pid)]) |
| 425 | |
| 426 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 427 | def _launch_servod(self, servo_host, servo_port, xml_config, servo_vid, |
| 428 | servo_pid, servo_serial): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 429 | """Launch the servod process. |
| 430 | |
| 431 | Args: |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 432 | servo_host: Host to start servod listening on. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 433 | servo_port: Port to start servod listening on. |
Tom Wai-Hong Tam | 5fc2c79 | 2011-11-03 13:05:39 +0800 | [diff] [blame] | 434 | xml_config: A list of XML configuration files for servod. |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 435 | servo_vid: USB vendor id of servo. |
| 436 | servo_pid: USB product id of servo. |
| 437 | servo_serial: USB serial id in device descriptor to host to |
| 438 | distinguish and control multiple servos. Note servo's EEPROM must |
| 439 | be programmed to use this feature. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 440 | """ |
Tom Wai-Hong Tam | 5fc2c79 | 2011-11-03 13:05:39 +0800 | [diff] [blame] | 441 | cmdlist = ['sudo', 'servod'] |
| 442 | for config in xml_config: |
| 443 | cmdlist += ['-c', str(config)] |
Todd Broch | 96d83aa | 2011-08-29 14:37:38 -0700 | [diff] [blame] | 444 | if servo_host is not None: |
| 445 | cmdlist.append('--host=%s' % str(servo_host)) |
| 446 | if servo_port is not None: |
| 447 | cmdlist.append('--port=%s' % str(servo_port)) |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 448 | if servo_vid is not None: |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 449 | cmdlist.append('--vendor=%s' % str(servo_vid)) |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 450 | if servo_pid is not None: |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 451 | cmdlist.append('--product=%s' % str(servo_pid)) |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 452 | if servo_serial is not None: |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 453 | cmdlist.append('--serialname=%s' % str(servo_serial)) |
Todd Broch | 5fd6bc0 | 2011-07-20 15:53:37 -0700 | [diff] [blame] | 454 | logging.info('starting servod w/ cmd :: %s' % ' '.join(cmdlist)) |
| 455 | self._servod = subprocess.Popen(cmdlist, 0, None, None, None, |
| 456 | subprocess.PIPE) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 457 | # wait for servod to initialize |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 458 | timeout = Servo.MAX_SERVO_STARTUP_DELAY |
| 459 | while ('Listening' not in self._servod.stderr.readline() and |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 460 | self._servod.returncode is None and timeout > 0): |
| 461 | time.sleep(1) |
| 462 | timeout -= 1 |
| 463 | assert self._servod.returncode is None and timeout |
| 464 | |
| 465 | |
| 466 | def _init_seq(self): |
| 467 | """Initiate starting state for servo.""" |
| 468 | self.set('tx_dir', 'input') |
Todd Broch | 6ec2943 | 2011-12-19 14:32:02 -0800 | [diff] [blame] | 469 | # TODO(tbroch) Investigate method to determine DUT's type so we can |
| 470 | # conditionally set lid if applicable |
| 471 | self.set_nocheck('lid_open', 'yes') |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 472 | self.set('rec_mode', 'off') |
| 473 | |
| 474 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 475 | def _connect_servod(self, servo_host, servo_port): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 476 | """Connect to the Servod process with XMLRPC. |
| 477 | |
| 478 | Args: |
| 479 | servo_port: Port the Servod process is listening on. |
| 480 | """ |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 481 | remote = 'http://%s:%s' % (servo_host, servo_port) |
Todd Broch | 96d83aa | 2011-08-29 14:37:38 -0700 | [diff] [blame] | 482 | self._server = xmlrpclib.ServerProxy(remote) |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 483 | try: |
| 484 | self._server.echo("ping-test") |
Todd Broch | 96d83aa | 2011-08-29 14:37:38 -0700 | [diff] [blame] | 485 | except: |
| 486 | logging.error('Connection to servod failed') |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 487 | raise |