Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright 2014, Google Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following disclaimer |
| 15 | * in the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * * Neither the name of Google Inc. nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * Alternatively, this software may be distributed under the terms of the |
| 34 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 35 | * Software Foundation. |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * This SPI flash programming interface is designed to talk to a Chromium OS |
| 40 | * device over a Raiden USB connection. The USB connection is routed to a |
| 41 | * microcontroller running an image compiled from: |
| 42 | * |
| 43 | * https://chromium.googlesource.com/chromiumos/platform/ec |
| 44 | * |
| 45 | * The protocol for the USB-SPI bridge is documented in the following file in |
| 46 | * that respository: |
| 47 | * |
| 48 | * chip/stm32/usb_spi.c |
| 49 | */ |
| 50 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 51 | #include "check.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 52 | #include "programmer.h" |
| 53 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 54 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 55 | |
| 56 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 57 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 58 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 59 | #include <string.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 60 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 61 | #define GOOGLE_VID 0x18D1 |
| 62 | #define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51 |
| 63 | #define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01 |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 64 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 65 | enum raiden_debug_spi_request { |
| 66 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 67 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 68 | }; |
| 69 | |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 70 | #define PACKET_HEADER_SIZE 2 |
| 71 | #define MAX_PACKET_SIZE 64 |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 75 | */ |
| 76 | #define TRANSFER_TIMEOUT_MS 1000 |
| 77 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 78 | struct usb_device *device = NULL; |
| 79 | uint8_t in_endpoint = 0; |
| 80 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 81 | |
| 82 | static int send_command(unsigned int write_count, |
| 83 | unsigned int read_count, |
| 84 | const unsigned char *write_buffer, |
| 85 | unsigned char *read_buffer) |
| 86 | { |
| 87 | uint8_t buffer[MAX_PACKET_SIZE]; |
| 88 | int transferred; |
| 89 | |
| 90 | if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 91 | msg_perr("Raiden: invalid write_count of %d\n", write_count); |
| 92 | return SPI_INVALID_LENGTH; |
| 93 | } |
| 94 | |
| 95 | if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 96 | msg_perr("Raiden: invalid read_count of %d\n", read_count); |
| 97 | return SPI_INVALID_LENGTH; |
| 98 | } |
| 99 | |
| 100 | buffer[0] = write_count; |
| 101 | buffer[1] = read_count; |
| 102 | |
| 103 | memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count); |
| 104 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 105 | CHECK(LIBUSB(libusb_bulk_transfer(device->handle, |
| 106 | out_endpoint, |
| 107 | buffer, |
| 108 | write_count + PACKET_HEADER_SIZE, |
| 109 | &transferred, |
| 110 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 111 | "Raiden: OUT transfer failed\n" |
| 112 | " write_count = %d\n" |
| 113 | " read_count = %d\n", |
| 114 | write_count, |
| 115 | read_count); |
| 116 | |
| 117 | if (transferred != write_count + PACKET_HEADER_SIZE) { |
| 118 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 119 | transferred, write_count + PACKET_HEADER_SIZE); |
| 120 | return 0x10001; |
| 121 | } |
| 122 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 123 | CHECK(LIBUSB(libusb_bulk_transfer(device->handle, |
| 124 | in_endpoint, |
| 125 | buffer, |
| 126 | read_count + PACKET_HEADER_SIZE, |
| 127 | &transferred, |
| 128 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 129 | "Raiden: IN transfer failed\n" |
| 130 | " write_count = %d\n" |
| 131 | " read_count = %d\n", |
| 132 | write_count, |
| 133 | read_count); |
| 134 | |
| 135 | if (transferred != read_count + PACKET_HEADER_SIZE) { |
| 136 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
| 137 | transferred, read_count + PACKET_HEADER_SIZE); |
| 138 | return 0x10002; |
| 139 | } |
| 140 | |
| 141 | memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count); |
| 142 | |
| 143 | return buffer[0] | (buffer[1] << 8); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 148 | * of bytes that your SPI device can read/write, these values are the maximum |
| 149 | * data chunk size that flashrom will package up with an additional four bytes |
| 150 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 151 | * add two bytes to in either direction, making our way up to the 64 byte |
| 152 | * maximum USB packet size for the device. |
| 153 | * |
| 154 | * The largest command that flashrom generates is the byte program command, so |
| 155 | * we use that command header maximum size here. The definition of |
| 156 | * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data |
| 157 | * to write, so we add one byte of space back. |
| 158 | */ |
| 159 | #define MAX_DATA_SIZE (MAX_PACKET_SIZE - \ |
| 160 | PACKET_HEADER_SIZE - \ |
| 161 | JEDEC_BYTE_PROGRAM_OUTSIZE + \ |
| 162 | 1) |
| 163 | |
| 164 | static const struct spi_programmer spi_programmer_raiden_debug = { |
| 165 | .type = SPI_CONTROLLER_RAIDEN_DEBUG, |
| 166 | .max_data_read = MAX_DATA_SIZE, |
| 167 | .max_data_write = MAX_DATA_SIZE, |
| 168 | .command = send_command, |
| 169 | .multicommand = default_spi_send_multicommand, |
| 170 | .read = default_spi_read, |
| 171 | .write_256 = default_spi_write_256, |
| 172 | }; |
| 173 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 174 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
| 175 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 176 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 177 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 178 | direction) && |
| 179 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 180 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 181 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 182 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 183 | static int find_endpoints(struct usb_device *device) |
| 184 | { |
| 185 | int i; |
| 186 | int in_count = 0; |
| 187 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 188 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 189 | for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) { |
| 190 | struct libusb_endpoint_descriptor const *endpoint = |
| 191 | &device->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 192 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 193 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 194 | in_count++; |
| 195 | in_endpoint = endpoint->bEndpointAddress; |
| 196 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 197 | out_count++; |
| 198 | out_endpoint = endpoint->bEndpointAddress; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (in_count != 1 || out_count != 1) { |
| 203 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 204 | " found %d IN and %d OUT endpoints\n", |
| 205 | in_count, |
| 206 | out_count); |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint); |
| 211 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint); |
| 212 | |
| 213 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 216 | static int shutdown(void * data) |
| 217 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 218 | CHECK(LIBUSB(libusb_control_transfer( |
| 219 | device->handle, |
| 220 | LIBUSB_ENDPOINT_OUT | |
| 221 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 222 | LIBUSB_RECIPIENT_INTERFACE, |
| 223 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 224 | 0, |
| 225 | device->interface_descriptor->bInterfaceNumber, |
| 226 | NULL, |
| 227 | 0, |
| 228 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 229 | "Raiden: Failed to disable SPI bridge\n"); |
| 230 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 231 | usb_device_free(device); |
| 232 | |
| 233 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 234 | libusb_exit(NULL); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 239 | int raiden_debug_spi_init(void) |
| 240 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 241 | struct usb_match match; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 242 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 243 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 244 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 245 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 246 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 247 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 248 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 249 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 250 | CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n"); |
| 251 | |
| 252 | CHECK(usb_device_find(&match, &device), |
| 253 | "Raiden: Failed to find devices\n"); |
| 254 | |
| 255 | if (device->next != NULL) { |
| 256 | struct usb_device *current; |
| 257 | |
| 258 | msg_perr("Raiden: Found too many compatible devices\n"); |
| 259 | msg_perr(" Use parameters to specify desired device\n"); |
| 260 | |
| 261 | for (current = device; |
| 262 | current != NULL; |
| 263 | current = usb_device_free(current)) |
| 264 | usb_device_show(" ", current); |
| 265 | |
| 266 | device = NULL; |
| 267 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 271 | CHECK(find_endpoints(device), |
| 272 | "Raiden: Failed to find valid endpoints\n"); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 273 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 274 | CHECK(usb_device_claim(device), |
| 275 | "Raiden: Failed to claim USB device\n"); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 276 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 277 | CHECK(LIBUSB(libusb_control_transfer( |
| 278 | device->handle, |
| 279 | LIBUSB_ENDPOINT_OUT | |
| 280 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 281 | LIBUSB_RECIPIENT_INTERFACE, |
| 282 | RAIDEN_DEBUG_SPI_REQ_ENABLE, |
| 283 | 0, |
| 284 | device->interface_descriptor->bInterfaceNumber, |
| 285 | NULL, |
| 286 | 0, |
| 287 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 288 | "Raiden: Failed to enable SPI bridge\n"); |
| 289 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 290 | register_spi_programmer(&spi_programmer_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 291 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 292 | |
| 293 | return 0; |
| 294 | } |