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 { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 66 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 67 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 68 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 69 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 72 | #define PACKET_HEADER_SIZE 2 |
| 73 | #define MAX_PACKET_SIZE 64 |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 77 | */ |
| 78 | #define TRANSFER_TIMEOUT_MS 1000 |
| 79 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 80 | struct usb_device *device = NULL; |
| 81 | uint8_t in_endpoint = 0; |
| 82 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 83 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 84 | static int send_command(const struct flashctx *flash, |
| 85 | unsigned int write_count, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 86 | unsigned int read_count, |
| 87 | const unsigned char *write_buffer, |
| 88 | unsigned char *read_buffer) |
| 89 | { |
| 90 | uint8_t buffer[MAX_PACKET_SIZE]; |
| 91 | int transferred; |
| 92 | |
| 93 | if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 94 | msg_perr("Raiden: invalid write_count of %d\n", write_count); |
| 95 | return SPI_INVALID_LENGTH; |
| 96 | } |
| 97 | |
| 98 | if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 99 | msg_perr("Raiden: invalid read_count of %d\n", read_count); |
| 100 | return SPI_INVALID_LENGTH; |
| 101 | } |
| 102 | |
| 103 | buffer[0] = write_count; |
| 104 | buffer[1] = read_count; |
| 105 | |
| 106 | memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count); |
| 107 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 108 | CHECK(LIBUSB(libusb_bulk_transfer(device->handle, |
| 109 | out_endpoint, |
| 110 | buffer, |
| 111 | write_count + PACKET_HEADER_SIZE, |
| 112 | &transferred, |
| 113 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 114 | "Raiden: OUT transfer failed\n" |
| 115 | " write_count = %d\n" |
| 116 | " read_count = %d\n", |
| 117 | write_count, |
| 118 | read_count); |
| 119 | |
| 120 | if (transferred != write_count + PACKET_HEADER_SIZE) { |
| 121 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 122 | transferred, write_count + PACKET_HEADER_SIZE); |
| 123 | return 0x10001; |
| 124 | } |
| 125 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 126 | CHECK(LIBUSB(libusb_bulk_transfer(device->handle, |
| 127 | in_endpoint, |
| 128 | buffer, |
| 129 | read_count + PACKET_HEADER_SIZE, |
| 130 | &transferred, |
| 131 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 132 | "Raiden: IN transfer failed\n" |
| 133 | " write_count = %d\n" |
| 134 | " read_count = %d\n", |
| 135 | write_count, |
| 136 | read_count); |
| 137 | |
| 138 | if (transferred != read_count + PACKET_HEADER_SIZE) { |
| 139 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
| 140 | transferred, read_count + PACKET_HEADER_SIZE); |
| 141 | return 0x10002; |
| 142 | } |
| 143 | |
| 144 | memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count); |
| 145 | |
| 146 | return buffer[0] | (buffer[1] << 8); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 151 | * of bytes that your SPI device can read/write, these values are the maximum |
| 152 | * data chunk size that flashrom will package up with an additional four bytes |
| 153 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 154 | * add two bytes to in either direction, making our way up to the 64 byte |
| 155 | * maximum USB packet size for the device. |
| 156 | * |
| 157 | * The largest command that flashrom generates is the byte program command, so |
| 158 | * we use that command header maximum size here. The definition of |
| 159 | * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data |
| 160 | * to write, so we add one byte of space back. |
| 161 | */ |
| 162 | #define MAX_DATA_SIZE (MAX_PACKET_SIZE - \ |
| 163 | PACKET_HEADER_SIZE - \ |
| 164 | JEDEC_BYTE_PROGRAM_OUTSIZE + \ |
| 165 | 1) |
| 166 | |
| 167 | static const struct spi_programmer spi_programmer_raiden_debug = { |
| 168 | .type = SPI_CONTROLLER_RAIDEN_DEBUG, |
| 169 | .max_data_read = MAX_DATA_SIZE, |
| 170 | .max_data_write = MAX_DATA_SIZE, |
| 171 | .command = send_command, |
| 172 | .multicommand = default_spi_send_multicommand, |
| 173 | .read = default_spi_read, |
| 174 | .write_256 = default_spi_write_256, |
| 175 | }; |
| 176 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 177 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
| 178 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 179 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 180 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 181 | direction) && |
| 182 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 183 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 184 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 185 | |
Souvik Ghosh | 5cdb7e5 | 2016-06-23 12:57:38 -0700 | [diff] [blame] | 186 | static int find_endpoints(void) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 187 | { |
| 188 | int i; |
| 189 | int in_count = 0; |
| 190 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 191 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 192 | for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) { |
| 193 | struct libusb_endpoint_descriptor const *endpoint = |
| 194 | &device->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 195 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 196 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 197 | in_count++; |
| 198 | in_endpoint = endpoint->bEndpointAddress; |
| 199 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 200 | out_count++; |
| 201 | out_endpoint = endpoint->bEndpointAddress; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (in_count != 1 || out_count != 1) { |
| 206 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 207 | " found %d IN and %d OUT endpoints\n", |
| 208 | in_count, |
| 209 | out_count); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint); |
| 214 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint); |
| 215 | |
| 216 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Souvik Ghosh | 052210a | 2016-07-11 12:29:05 -0700 | [diff] [blame] | 219 | static int shutdown(struct flashctx *flash, void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 220 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 221 | CHECK(LIBUSB(libusb_control_transfer( |
| 222 | device->handle, |
| 223 | LIBUSB_ENDPOINT_OUT | |
| 224 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 225 | LIBUSB_RECIPIENT_INTERFACE, |
| 226 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 227 | 0, |
| 228 | device->interface_descriptor->bInterfaceNumber, |
| 229 | NULL, |
| 230 | 0, |
| 231 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 232 | "Raiden: Failed to disable SPI bridge\n"); |
| 233 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 234 | usb_device_free(device); |
| 235 | |
| 236 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 237 | libusb_exit(NULL); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Souvik Ghosh | 63b92f9 | 2016-06-29 18:45:52 -0700 | [diff] [blame] | 242 | int raiden_debug_spi_init(struct flashctx *flash) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 243 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 244 | struct usb_match match; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 245 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
| 246 | char *target_str = extract_programmer_param("target"); |
| 247 | |
| 248 | if (target_str) { |
| 249 | if (!strcasecmp(target_str, "ap")) |
| 250 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 251 | else if (!strcasecmp(target_str, "ec")) |
| 252 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 253 | else { |
| 254 | msg_perr("Invalid target: %s\n", target_str); |
| 255 | free(target_str); |
| 256 | return 1; |
| 257 | } |
| 258 | } |
| 259 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 260 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 261 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 262 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 263 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 264 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 265 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 266 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 267 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 268 | CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n"); |
| 269 | |
| 270 | CHECK(usb_device_find(&match, &device), |
| 271 | "Raiden: Failed to find devices\n"); |
| 272 | |
| 273 | if (device->next != NULL) { |
| 274 | struct usb_device *current; |
| 275 | |
| 276 | msg_perr("Raiden: Found too many compatible devices\n"); |
| 277 | msg_perr(" Use parameters to specify desired device\n"); |
| 278 | |
| 279 | for (current = device; |
| 280 | current != NULL; |
| 281 | current = usb_device_free(current)) |
| 282 | usb_device_show(" ", current); |
| 283 | |
| 284 | device = NULL; |
| 285 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | |
Souvik Ghosh | 5cdb7e5 | 2016-06-23 12:57:38 -0700 | [diff] [blame] | 289 | CHECK(find_endpoints(), |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 290 | "Raiden: Failed to find valid endpoints\n"); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 291 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 292 | CHECK(usb_device_claim(device), |
| 293 | "Raiden: Failed to claim USB device\n"); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 294 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 295 | CHECK(LIBUSB(libusb_control_transfer( |
| 296 | device->handle, |
| 297 | LIBUSB_ENDPOINT_OUT | |
| 298 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 299 | LIBUSB_RECIPIENT_INTERFACE, |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 300 | request_enable, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 301 | 0, |
| 302 | device->interface_descriptor->bInterfaceNumber, |
| 303 | NULL, |
| 304 | 0, |
| 305 | TRANSFER_TIMEOUT_MS)), |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 306 | "Raiden: Failed to enable SPI bridge\n"); |
| 307 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 308 | register_spi_programmer(&spi_programmer_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 309 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |