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