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 |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 49 | * |
| 50 | * Version 1: |
| 51 | * SPI transactions of up to 62B in each direction with every command having |
| 52 | * a response. The initial packet from host contains a 2B header indicating |
| 53 | * write and read counts with an optional payload length equal to the write |
| 54 | * count. The device will respond with a message that reports the 2B status |
| 55 | * code and an optional payload response length equal to read count. |
| 56 | * |
| 57 | * Message Format: |
| 58 | * |
| 59 | * Command Packet: |
| 60 | * +------------------+-----------------+------------------------+ |
| 61 | * | write count : 1B | read count : 1B | write payload : <= 62B | |
| 62 | * +------------------+-----------------+------------------------+ |
| 63 | * |
| 64 | * write count: 1 byte, zero based count of bytes to write |
| 65 | * |
| 66 | * read count: 1 byte, zero based count of bytes to read |
| 67 | * |
| 68 | * write payload: Up to 62 bytes of data to write to SPI, the total |
| 69 | * length of all TX packets must match write count. |
| 70 | * Due to data alignment constraints, this must be an |
| 71 | * even number of bytes unless this is the final packet. |
| 72 | * |
| 73 | * Response Packet: |
| 74 | * +-------------+-----------------------+ |
| 75 | * | status : 2B | read payload : <= 62B | |
| 76 | * +-------------+-----------------------+ |
| 77 | * |
| 78 | * status: 2 byte status |
| 79 | * 0x0000: Success |
| 80 | * 0x0001: SPI timeout |
| 81 | * 0x0002: Busy, try again |
| 82 | * This can happen if someone else has acquired the shared memory |
| 83 | * buffer that the SPI driver uses as /dev/null |
| 84 | * 0x0003: Write count invalid (V1 > 62B) |
| 85 | * 0x0004: Read count invalid (V1 > 62B) |
| 86 | * 0x0005: The SPI bridge is disabled. |
| 87 | * 0x8000: Unknown error mask |
| 88 | * The bottom 15 bits will contain the bottom 15 bits from the EC |
| 89 | * error code. |
| 90 | * |
| 91 | * read payload: Up to 62 bytes of data read from SPI, the total |
| 92 | * length of all RX packets must match read count |
| 93 | * unless an error status was returned. Due to data |
| 94 | * alignment constraints, this must be a even number |
| 95 | * of bytes unless this is the final packet. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 96 | * |
| 97 | * USB Error Codes: |
| 98 | * |
| 99 | * send_command return codes have the following format: |
| 100 | * |
| 101 | * 0x00000: Status code success. |
| 102 | * 0x00001-0x0FFFF: Error code returned by the USB SPI device. |
| 103 | * 0x10001-0x1FFFF: The host has determined an error has occurred. |
| 104 | * 0x20001-0x20063 Lower bits store the positive value representation |
| 105 | * of the libusb_error enum. See the libusb documentation: |
| 106 | * http://libusb.sourceforge.net/api-1.0/group__misc.html |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 107 | */ |
| 108 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 109 | #include "programmer.h" |
| 110 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 111 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 112 | |
| 113 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 114 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 115 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 116 | #include <string.h> |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 117 | #include <unistd.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 118 | |
Brian J. Nemec | b42d6c1 | 2020-07-23 03:07:38 -0700 | [diff] [blame] | 119 | /* FIXME: Add some programmer IDs here */ |
| 120 | const struct dev_entry devs_raiden[] = { |
| 121 | {0}, |
| 122 | }; |
| 123 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 124 | #define GOOGLE_VID (0x18D1) |
| 125 | #define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51) |
| 126 | #define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 127 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 128 | enum usb_spi_error { |
| 129 | USB_SPI_SUCCESS = 0x0000, |
| 130 | USB_SPI_TIMEOUT = 0x0001, |
| 131 | USB_SPI_BUSY = 0x0002, |
| 132 | USB_SPI_WRITE_COUNT_INVALID = 0x0003, |
| 133 | USB_SPI_READ_COUNT_INVALID = 0x0004, |
| 134 | USB_SPI_DISABLED = 0x0005, |
| 135 | USB_SPI_UNKNOWN_ERROR = 0x8000, |
| 136 | }; |
| 137 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 138 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 139 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 140 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 141 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 142 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 145 | #define PACKET_HEADER_SIZE (2) |
| 146 | #define MAX_PACKET_SIZE (64) |
| 147 | #define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 148 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 149 | /* |
| 150 | * Servo Micro has an error where it is capable of acknowledging USB packets |
| 151 | * without loading it into the USB endpoint buffers or triggering interrupts. |
| 152 | * See crbug.com/952494. Retry mechanisms have been implemented to recover |
| 153 | * from these rare failures allowing the process to continue. |
| 154 | */ |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 155 | #define WRITE_RETY_ATTEMPTS (3) |
| 156 | #define READ_RETY_ATTEMPTS (3) |
| 157 | #define RETY_INTERVAL_US (100 * 1000) |
| 158 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 159 | /* |
| 160 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 161 | */ |
Edward O'Callaghan | f1e6ef5 | 2020-03-03 13:57:15 +1100 | [diff] [blame] | 162 | #define TRANSFER_TIMEOUT_MS (200 + 800) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 163 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 164 | struct usb_device *device = NULL; |
| 165 | uint8_t in_endpoint = 0; |
| 166 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 167 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 168 | typedef struct { |
| 169 | int8_t write_count; |
| 170 | /* -1 Indicates readback all on halfduplex compliant devices. */ |
| 171 | int8_t read_count; |
| 172 | uint8_t data[PAYLOAD_SIZE]; |
| 173 | } __attribute__((packed)) usb_spi_command_t; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 174 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 175 | typedef struct { |
| 176 | uint16_t status_code; |
| 177 | uint8_t data[PAYLOAD_SIZE]; |
| 178 | } __attribute__((packed)) usb_spi_response_t; |
| 179 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 180 | /* |
| 181 | * This function will return true when an error code can potentially recover |
| 182 | * if we attempt to write SPI data to the device or read from it. We know |
| 183 | * that some conditions are not recoverable in the current state so allows us |
| 184 | * to bypass the retry logic and terminate early. |
| 185 | */ |
| 186 | static bool retry_recovery(int error_code) |
| 187 | { |
| 188 | if (error_code < 0x10000) { |
Brian J. Nemec | 1a61f72 | 2020-05-04 20:58:06 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Handle error codes returned from the device. USB_SPI_TIMEOUT, |
| 191 | * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed |
| 192 | * during transfer errors to the device and can be recovered. |
| 193 | */ |
| 194 | if (USB_SPI_READ_COUNT_INVALID <= error_code && |
| 195 | error_code <= USB_SPI_DISABLED) { |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | } else if (usb_device_is_libusb_error(error_code)) { |
| 199 | /* Handle error codes returned from libusb. */ |
| 200 | if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) { |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 207 | static int write_command(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 208 | unsigned int write_count, |
| 209 | unsigned int read_count, |
| 210 | const unsigned char *write_buffer, |
| 211 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 212 | { |
| 213 | |
| 214 | int transferred; |
| 215 | int ret; |
| 216 | usb_spi_command_t command_packet; |
| 217 | |
| 218 | if (write_count > PAYLOAD_SIZE) { |
Edward O'Callaghan | 8eb829a | 2020-03-03 13:55:08 +1100 | [diff] [blame] | 219 | msg_perr("Raiden: Invalid write_count of %d\n", write_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 220 | return SPI_INVALID_LENGTH; |
| 221 | } |
| 222 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 223 | if (read_count > PAYLOAD_SIZE) { |
Edward O'Callaghan | 8eb829a | 2020-03-03 13:55:08 +1100 | [diff] [blame] | 224 | msg_perr("Raiden: Invalid read_count of %d\n", read_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 225 | return SPI_INVALID_LENGTH; |
| 226 | } |
| 227 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 228 | command_packet.write_count = write_count; |
| 229 | command_packet.read_count = read_count; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 230 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 231 | memcpy(command_packet.data, write_buffer, write_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 232 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 233 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 234 | out_endpoint, |
| 235 | (void*)&command_packet, |
| 236 | write_count + PACKET_HEADER_SIZE, |
| 237 | &transferred, |
| 238 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 239 | if (ret != 0) { |
| 240 | msg_perr("Raiden: OUT transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 241 | " write_count = %d\n" |
| 242 | " read_count = %d\n", |
| 243 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 244 | return ret; |
| 245 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 246 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 247 | if ((unsigned) transferred != write_count + PACKET_HEADER_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 248 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 249 | transferred, write_count + PACKET_HEADER_SIZE); |
| 250 | return 0x10001; |
| 251 | } |
| 252 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int read_response(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 257 | unsigned int write_count, |
| 258 | unsigned int read_count, |
| 259 | const unsigned char *write_buffer, |
| 260 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 261 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 262 | int transferred; |
| 263 | int ret; |
| 264 | usb_spi_response_t response_packet; |
| 265 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 266 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 267 | in_endpoint, |
| 268 | (void*)&response_packet, |
| 269 | read_count + PACKET_HEADER_SIZE, |
| 270 | &transferred, |
| 271 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 272 | if (ret != 0) { |
| 273 | msg_perr("Raiden: IN transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 274 | " write_count = %d\n" |
| 275 | " read_count = %d\n", |
| 276 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 277 | return ret; |
| 278 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 279 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 280 | if ((unsigned) transferred != read_count + PACKET_HEADER_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 281 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 282 | transferred, read_count + PACKET_HEADER_SIZE); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 283 | return 0x10002; |
| 284 | } |
| 285 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 286 | memcpy(read_buffer, response_packet.data, read_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 287 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 288 | return response_packet.status_code; |
| 289 | } |
| 290 | |
| 291 | static int send_command(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 292 | unsigned int write_count, |
| 293 | unsigned int read_count, |
| 294 | const unsigned char *write_buffer, |
| 295 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 296 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 297 | int status = -1; |
| 298 | |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 299 | for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS; |
| 300 | write_attempt++) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 301 | |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 302 | status = write_command(flash, write_count, read_count, |
| 303 | write_buffer, read_buffer); |
| 304 | |
| 305 | if (status) { |
| 306 | /* Write operation failed. */ |
| 307 | msg_perr("Raiden: Write command failed\n" |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 308 | "Write attempt = %d\n" |
| 309 | "status = %d\n", |
| 310 | write_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 311 | if (!retry_recovery(status)) { |
| 312 | /* Reattempting will not result in a recovery. */ |
| 313 | return status; |
| 314 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 315 | programmer_delay(RETY_INTERVAL_US); |
| 316 | continue; |
| 317 | } |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 318 | for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS; read_attempt++) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 319 | |
| 320 | status = read_response(flash, write_count, read_count, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 321 | write_buffer, read_buffer); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 322 | |
| 323 | if (status != 0) { |
| 324 | /* Read operation failed. */ |
| 325 | msg_perr("Raiden: Read response failed\n" |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 326 | "Write attempt = %d\n" |
| 327 | "Read attempt = %d\n" |
| 328 | "status = %d\n", |
| 329 | write_attempt + 1, read_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 330 | if (!retry_recovery(status)) { |
| 331 | /* Reattempting will not result in a recovery. */ |
| 332 | return status; |
| 333 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 334 | programmer_delay(RETY_INTERVAL_US); |
| 335 | } else { |
| 336 | /* We were successful at performing the SPI transfer. */ |
| 337 | return status; |
| 338 | } |
| 339 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 340 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 341 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 346 | * of bytes that your SPI device can read/write, these values are the maximum |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 347 | * data chunk size that flashrom will package up with an additional five bytes |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 348 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 349 | * add two bytes to in either direction, making our way up to the 64 byte |
| 350 | * maximum USB packet size for the device. |
| 351 | * |
| 352 | * The largest command that flashrom generates is the byte program command, so |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 353 | * we use that command header maximum size here. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 354 | */ |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 355 | #define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 356 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 357 | static const struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 358 | .features = SPI_MASTER_4BA, |
| 359 | .max_data_read = MAX_DATA_SIZE, |
| 360 | .max_data_write = MAX_DATA_SIZE, |
| 361 | .command = send_command, |
| 362 | .multicommand = default_spi_send_multicommand, |
| 363 | .read = default_spi_read, |
| 364 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 367 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 368 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 369 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 370 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 371 | direction) && |
| 372 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 373 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 374 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 375 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 376 | static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 377 | { |
| 378 | int i; |
| 379 | int in_count = 0; |
| 380 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 381 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 382 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 383 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 384 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 385 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 386 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 387 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 388 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 389 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 390 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 391 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | if (in_count != 1 || out_count != 1) { |
| 396 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 397 | " found %d IN and %d OUT endpoints\n", |
| 398 | in_count, |
| 399 | out_count); |
| 400 | return 1; |
| 401 | } |
| 402 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 403 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 404 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 405 | |
| 406 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 407 | } |
| 408 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 409 | static int shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 410 | { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 411 | int ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 412 | device->handle, |
| 413 | LIBUSB_ENDPOINT_OUT | |
| 414 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 415 | LIBUSB_RECIPIENT_INTERFACE, |
| 416 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 417 | 0, |
| 418 | device->interface_descriptor->bInterfaceNumber, |
| 419 | NULL, |
| 420 | 0, |
| 421 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 422 | if (ret != 0) { |
| 423 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
| 424 | return ret; |
| 425 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 426 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 427 | usb_device_free(device); |
| 428 | |
| 429 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 430 | libusb_exit(NULL); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Edward O'Callaghan | dda1e54 | 2020-02-03 12:45:01 +1100 | [diff] [blame] | 435 | static int get_target(void) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 436 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 437 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 438 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 439 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 440 | if (target_str) { |
| 441 | if (!strcasecmp(target_str, "ap")) |
| 442 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 443 | else if (!strcasecmp(target_str, "ec")) |
| 444 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 445 | else { |
| 446 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 447 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 450 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 451 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 452 | return request_enable; |
| 453 | } |
| 454 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 455 | static void free_dev_list(struct usb_device **dev_lst) |
| 456 | { |
| 457 | struct usb_device *dev = *dev_lst; |
| 458 | /* free devices we don't care about */ |
| 459 | dev = dev->next; |
| 460 | while (dev) |
| 461 | dev = usb_device_free(dev); |
| 462 | } |
| 463 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 464 | int raiden_debug_spi_init(void) |
| 465 | { |
| 466 | struct usb_match match; |
| 467 | char *serial = extract_programmer_param("serial"); |
| 468 | struct usb_device *current; |
| 469 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 470 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 471 | |
| 472 | int request_enable = get_target(); |
| 473 | if (request_enable < 0) |
| 474 | return 1; |
| 475 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 476 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 477 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 478 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 479 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 480 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 481 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 482 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 483 | ret = LIBUSB(libusb_init(NULL)); |
| 484 | if (ret != 0) { |
| 485 | msg_perr("Raiden: libusb_init failed\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame^] | 486 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 487 | return ret; |
| 488 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 489 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 490 | ret = usb_device_find(&match, ¤t); |
| 491 | if (ret != 0) { |
| 492 | msg_perr("Raiden: Failed to find devices\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame^] | 493 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 494 | return ret; |
| 495 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 496 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 497 | while (current) { |
| 498 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 499 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 500 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 501 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 502 | usb_device_show(" ", current); |
| 503 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 504 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 505 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 506 | if (usb_device_claim(device)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 507 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 508 | usb_device_show(" ", current); |
| 509 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 510 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 511 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 512 | if (!serial) { |
| 513 | found = 1; |
| 514 | goto loop_end; |
| 515 | } else { |
| 516 | unsigned char dev_serial[32]; |
| 517 | struct libusb_device_descriptor descriptor; |
| 518 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 519 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 520 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 521 | |
| 522 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 523 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 524 | goto loop_end; |
| 525 | } |
| 526 | |
| 527 | rc = libusb_get_string_descriptor_ascii(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 528 | descriptor.iSerialNumber, |
| 529 | dev_serial, |
| 530 | sizeof(dev_serial)); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 531 | if (rc < 0) { |
| 532 | LIBUSB(rc); |
| 533 | } else { |
| 534 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 535 | msg_pdbg("Raiden: Serial number %s did not match device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 536 | usb_device_show(" ", current); |
| 537 | } else { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 538 | msg_pinfo("Raiden: Serial number %s matched device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 539 | usb_device_show(" ", current); |
| 540 | found = 1; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | loop_end: |
| 546 | if (found) |
| 547 | break; |
| 548 | else |
| 549 | current = usb_device_free(current); |
| 550 | } |
| 551 | |
| 552 | if (!device || !found) { |
| 553 | msg_perr("Raiden: No usable device found.\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame^] | 554 | free(serial); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 555 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 558 | free_dev_list(¤t); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 559 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 560 | ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 561 | device->handle, |
| 562 | LIBUSB_ENDPOINT_OUT | |
| 563 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 564 | LIBUSB_RECIPIENT_INTERFACE, |
| 565 | request_enable, |
| 566 | 0, |
| 567 | device->interface_descriptor->bInterfaceNumber, |
| 568 | NULL, |
| 569 | 0, |
| 570 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 571 | if (ret != 0) { |
| 572 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 573 | return ret; |
| 574 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 575 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 576 | /* |
| 577 | * Allow for power to settle on the AP and EC flash devices. |
| 578 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 579 | * can require up to 10 ms from power on to the first write. |
| 580 | */ |
| 581 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 582 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 583 | usleep(50 * 1000); |
| 584 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 585 | register_spi_master(&spi_master_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 586 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 587 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 588 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 589 | } |