blob: fea97bc1c0b5e7f73e4ace3348edae9ba07f249a [file] [log] [blame]
Anton Staafb2647882014-09-17 15:13:43 -07001/*
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 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070045 * The protocol for the USB-SPI bridge is implemented in the following files
46 * in that repository:
Anton Staafb2647882014-09-17 15:13:43 -070047 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070048 * chip/stm32/usb_spi.h
Anton Staafb2647882014-09-17 15:13:43 -070049 * chip/stm32/usb_spi.c
Brian J. Nemecda496dc2020-02-04 11:13:05 -080050 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070051 * bInterfaceProtocol determines which protocol is used by the USB SPI device.
Brian J. Nemecda496dc2020-02-04 11:13:05 -080052 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080053 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070054 * USB SPI Version 1:
55 *
56 * SPI transactions of up to 62B in each direction with every command having
57 * a response. The initial packet from host contains a 2B header indicating
58 * write and read counts with an optional payload length equal to the write
59 * count. The device will respond with a message that reports the 2B status
60 * code and an optional payload response length equal to read count.
61 *
62 *
63 * Message Packets:
64 *
65 * Command First Packet (Host to Device):
66 *
67 * USB SPI command, containing the number of bytes to write and read
68 * and a payload of bytes to write.
69 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080070 * +------------------+-----------------+------------------------+
71 * | write count : 1B | read count : 1B | write payload : <= 62B |
72 * +------------------+-----------------+------------------------+
73 *
74 * write count: 1 byte, zero based count of bytes to write
75 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070076 * read count: 1 byte, zero based count of bytes to read. Full duplex
77 * mode is enabled with UINT8_MAX
Brian J. Nemecda496dc2020-02-04 11:13:05 -080078 *
79 * write payload: Up to 62 bytes of data to write to SPI, the total
80 * length of all TX packets must match write count.
81 * Due to data alignment constraints, this must be an
82 * even number of bytes unless this is the final packet.
83 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070084 * Response Packet (Device to Host):
85 *
86 * USB SPI response, containing the status code and any bytes of the
87 * read payload.
88 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080089 * +-------------+-----------------------+
90 * | status : 2B | read payload : <= 62B |
91 * +-------------+-----------------------+
92 *
93 * status: 2 byte status
94 * 0x0000: Success
95 * 0x0001: SPI timeout
96 * 0x0002: Busy, try again
97 * This can happen if someone else has acquired the shared memory
98 * buffer that the SPI driver uses as /dev/null
Brian J. Nemecc5d69462020-07-22 03:12:15 -070099 * 0x0003: Write count invalid (over 62 bytes)
100 * 0x0004: Read count invalid (over 62 bytes)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800101 * 0x0005: The SPI bridge is disabled.
102 * 0x8000: Unknown error mask
103 * The bottom 15 bits will contain the bottom 15 bits from the EC
104 * error code.
105 *
106 * read payload: Up to 62 bytes of data read from SPI, the total
107 * length of all RX packets must match read count
108 * unless an error status was returned. Due to data
109 * alignment constraints, this must be a even number
110 * of bytes unless this is the final packet.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800111 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700112 *
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800113 * USB Error Codes:
114 *
115 * send_command return codes have the following format:
116 *
117 * 0x00000: Status code success.
118 * 0x00001-0x0FFFF: Error code returned by the USB SPI device.
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700119 * 0x10001-0x1FFFF: Error code returned by the USB SPI host.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800120 * 0x20001-0x20063 Lower bits store the positive value representation
121 * of the libusb_error enum. See the libusb documentation:
122 * http://libusb.sourceforge.net/api-1.0/group__misc.html
Anton Staafb2647882014-09-17 15:13:43 -0700123 */
124
Anton Staafb2647882014-09-17 15:13:43 -0700125#include "programmer.h"
126#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700127#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700128
129#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700130#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700131#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700132#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700133#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700134
Brian J. Nemecb42d6c12020-07-23 03:07:38 -0700135/* FIXME: Add some programmer IDs here */
136const struct dev_entry devs_raiden[] = {
137 {0},
138};
139
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800140#define GOOGLE_VID (0x18D1)
141#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
Brian J. Nemecb746f822020-07-22 02:57:56 -0700142
143enum {
144 GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01,
145 GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02,
146};
Anton Staafb2647882014-09-17 15:13:43 -0700147
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700148enum {
149 /* The host failed to transfer the data with no libusb error. */
150 USB_SPI_HOST_TX_BAD_TRANSFER = 0x10001,
151 /* The number of bytes written did not match expected. */
152 USB_SPI_HOST_TX_WRITE_FAILURE = 0x10002,
153
154 /* We did not receive the expected USB packet. */
155 USB_SPI_HOST_RX_UNEXPECTED_PACKET = 0x11001,
156 /* We received a continue packet with an invalid data index. */
157 USB_SPI_HOST_RX_BAD_DATA_INDEX = 0x11002,
158 /* We received too much data. */
159 USB_SPI_HOST_RX_DATA_OVERFLOW = 0x11003,
160 /* The number of bytes read did not match expected. */
161 USB_SPI_HOST_RX_READ_FAILURE = 0x11004,
162
163 /* We were unable to configure the device. */
164 USB_SPI_HOST_INIT_FAILURE = 0x12001,
165};
166
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800167enum usb_spi_error {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700168 USB_SPI_SUCCESS = 0x0000,
169 USB_SPI_TIMEOUT = 0x0001,
170 USB_SPI_BUSY = 0x0002,
171 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
172 USB_SPI_READ_COUNT_INVALID = 0x0004,
173 USB_SPI_DISABLED = 0x0005,
174 USB_SPI_UNKNOWN_ERROR = 0x8000,
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800175};
176
Anton Staaf4589cd12015-03-23 13:36:44 -0700177enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700178 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
179 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
180 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
181 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700182};
183
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800184#define PACKET_HEADER_SIZE (2)
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700185#define USB_MAX_PACKET_SIZE (64)
186#define PAYLOAD_SIZE_V1 (USB_MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700187
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800188/*
189 * Servo Micro has an error where it is capable of acknowledging USB packets
190 * without loading it into the USB endpoint buffers or triggering interrupts.
191 * See crbug.com/952494. Retry mechanisms have been implemented to recover
192 * from these rare failures allowing the process to continue.
193 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700194#define WRITE_RETRY_ATTEMPTS (3)
195#define READ_RETRY_ATTEMPTS (3)
196#define RETRY_INTERVAL_US (100 * 1000)
Brian J. Nemec1118a582020-02-04 18:26:02 -0800197
Anton Staafb2647882014-09-17 15:13:43 -0700198/*
199 * This timeout is so large because the Raiden SPI timeout is 800ms.
200 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100201#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700202
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700203struct raiden_debug_spi_data {
204 struct usb_device *dev;
205 uint8_t in_ep;
206 uint8_t out_ep;
207};
Anton Staafb2647882014-09-17 15:13:43 -0700208
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700209/*
210 * Version 1 protocol specific attributes
211 */
Anton Staafb2647882014-09-17 15:13:43 -0700212
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700213struct usb_spi_command_v1 {
214 uint8_t write_count;
215 /* UINT8_MAX indicates full duplex mode on compliant devices. */
216 uint8_t read_count;
217 uint8_t data[PAYLOAD_SIZE_V1];
218} __attribute__((packed));
219
220struct usb_spi_response_v1 {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800221 uint16_t status_code;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700222 uint8_t data[PAYLOAD_SIZE_V1];
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700223} __attribute__((packed));
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800224
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700225union usb_spi_packet_v1 {
226 struct usb_spi_command_v1 command;
227 struct usb_spi_response_v1 response;
228} __attribute__((packed));
229
230struct usb_spi_packet_ctx {
231 union {
232 uint8_t bytes[USB_MAX_PACKET_SIZE];
233 union usb_spi_packet_v1 packet_v1;
234 };
235 /*
236 * By storing the number of bytes in the header and knowing that the
237 * USB data packets are all 64B long, we are able to use the header
238 * size to store the offset of the buffer and it's size without
239 * duplicating variables that can go out of sync.
240 */
241 size_t header_size;
242 /* Number of bytes in the packet */
243 size_t packet_size;
244};
245
Brian J. Nemec1889a032020-07-22 03:16:45 -0700246struct usb_spi_transmit_ctx {
247 /* Buffer we are reading data from. */
248 const uint8_t *buffer;
249 /* Number of bytes in the transfer. */
250 size_t transmit_size;
251 /* Number of bytes transferred. */
252 size_t transmit_index;
253};
254
255struct usb_spi_receive_ctx {
256 /* Buffer we are writing data into. */
257 uint8_t *buffer;
258 /* Number of bytes in the transfer. */
259 size_t receive_size;
260 /* Number of bytes transferred. */
261 size_t receive_index;
262};
263
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800264/*
265 * This function will return true when an error code can potentially recover
266 * if we attempt to write SPI data to the device or read from it. We know
267 * that some conditions are not recoverable in the current state so allows us
268 * to bypass the retry logic and terminate early.
269 */
270static bool retry_recovery(int error_code)
271{
272 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700273 /*
274 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
275 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
276 * during transfer errors to the device and can be recovered.
277 */
278 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
279 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800280 return false;
281 }
282 } else if (usb_device_is_libusb_error(error_code)) {
283 /* Handle error codes returned from libusb. */
284 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
285 return false;
286 }
287 }
288 return true;
289}
290
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700291static const struct raiden_debug_spi_data *
292 get_raiden_data_from_context(const struct flashctx *flash)
293{
294 return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
295}
296
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700297/*
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700298 * Read data into the receive buffer.
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700299 *
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700300 * @param dst Destination receive context we are writing data to.
301 * @param src Source packet context we are reading data from.
302 *
303 * @returns status code 0 on success.
304 * USB_SPI_HOST_RX_DATA_OVERFLOW if the source packet is too
305 * large to fit in read buffer.
306 */
307static int read_usb_packet(struct usb_spi_receive_ctx *dst,
308 const struct usb_spi_packet_ctx *src)
309{
310 size_t max_read_length = dst->receive_size - dst->receive_index;
311 size_t bytes_in_buffer = src->packet_size - src->header_size;
312 const uint8_t *packet_buffer = src->bytes + src->header_size;
313
314 if (bytes_in_buffer > max_read_length) {
315 /*
316 * An error occurred, we should not receive more data than
317 * the buffer can support.
318 */
319 msg_perr("Raiden: Receive packet overflowed\n"
320 " bytes_in_buffer = %zu\n"
321 " max_read_length = %zu\n"
322 " receive_index = %zu\n"
323 " receive_size = %zu\n",
324 bytes_in_buffer, max_read_length,
325 dst->receive_size, dst->receive_index);
326 return USB_SPI_HOST_RX_DATA_OVERFLOW;
327 }
328 memcpy(dst->buffer + dst->receive_index, packet_buffer,
329 bytes_in_buffer);
330
331 dst->receive_index += bytes_in_buffer;
332 return 0;
333}
334
335/*
336 * Fill the USB packet with data from the transmit buffer.
337 *
338 * @param dst Destination packet context we are writing data to.
339 * @param src Source transmit context we are reading data from.
340 */
341static void fill_usb_packet(struct usb_spi_packet_ctx *dst,
342 struct usb_spi_transmit_ctx *src)
343{
344 size_t transmit_size = src->transmit_size - src->transmit_index;
345 size_t max_buffer_size = USB_MAX_PACKET_SIZE - dst->header_size;
346 uint8_t *packet_buffer = dst->bytes + dst->header_size;
347
348 if (transmit_size > max_buffer_size)
349 transmit_size = max_buffer_size;
350
351 memcpy(packet_buffer, src->buffer + src->transmit_index, transmit_size);
352
353 dst->packet_size = dst->header_size + transmit_size;
354 src->transmit_index += transmit_size;
355}
356
357/*
358 * Receive the data from the device USB endpoint and store in the packet.
359 *
360 * @param ctx_data Raiden SPI config.
361 * @param packet Destination packet used to store the endpoint data.
362 *
363 * @returns Returns status code with 0 on success.
364 */
365static int receive_packet(const struct raiden_debug_spi_data *ctx_data,
366 struct usb_spi_packet_ctx *packet)
367{
368 int received;
369 int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
370 ctx_data->in_ep,
371 packet->bytes,
372 USB_MAX_PACKET_SIZE,
373 &received,
374 TRANSFER_TIMEOUT_MS));
375 packet->packet_size = received;
376 if (status) {
377 msg_perr("Raiden: IN transfer failed\n"
378 " received = %d\n"
379 " status = 0x%05x\n",
380 received, status);
381 }
382 return status;
383}
384
385/*
386 * Transmit data from the packet to the device's USB endpoint.
387 *
388 * @param ctx_data Raiden SPI config.
389 * @param packet Source packet we will write to the endpoint data.
390 *
391 * @returns Returns status code with 0 on success.
392 */
393static int transmit_packet(const struct raiden_debug_spi_data *ctx_data,
394 struct usb_spi_packet_ctx *packet)
395{
396 int transferred;
397 int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
398 ctx_data->out_ep,
399 packet->bytes,
400 packet->packet_size,
401 &transferred,
402 TRANSFER_TIMEOUT_MS));
403 if (status || (size_t)transferred != packet->packet_size) {
404 if (!status) {
405 /* No error was reported, but we didn't transmit the data expected. */
406 status = USB_SPI_HOST_TX_BAD_TRANSFER;
407 }
408 msg_perr("Raiden: OUT transfer failed\n"
409 " transferred = %d\n"
410 " packet_size = %zu\n"
411 " status = 0x%05x\n",
412 transferred, packet->packet_size, status);
413
414 }
415 return status;
416}
417
418/*
419 * Version 1 protocol command to start a USB SPI transfer and write the payload.
420 *
421 * @param ctx_data Raiden SPI config.
Brian J. Nemec1889a032020-07-22 03:16:45 -0700422 * @param write Write context of data to transmit and write payload.
423 * @param read Read context of data to receive and read buffer.
424 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700425 * @returns Returns status code with 0 on success.
426 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700427static int write_command_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700428 struct usb_spi_transmit_ctx *write,
429 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800430{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700431 struct usb_spi_packet_ctx command = {
432 .header_size = offsetof(struct usb_spi_command_v1, data),
433 .packet_v1.command.write_count = write->transmit_size,
434 .packet_v1.command.read_count = read->receive_size
435 };
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800436
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700437 /* Reset the write context to the start. */
438 write->transmit_index = 0;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800439
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700440 fill_usb_packet(&command, write);
441 return transmit_packet(ctx_data, &command);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800442}
443
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700444/*
445 * Version 1 Protocol: Responsible for reading the response of the USB SPI
446 * transfer. Status codes from the transfer and any read payload are copied
447 * to the read_buffer.
448 *
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700449 * @param ctx_data Raiden SPI config.
Brian J. Nemec1889a032020-07-22 03:16:45 -0700450 * @param write Write context of data to transmit and write payload.
451 * @param read Read context of data to receive and read buffer.
452 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700453 * @returns Returns status code with 0 on success.
454 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700455static int read_response_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700456 struct usb_spi_transmit_ctx *write,
457 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800458{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700459 int status;
460 struct usb_spi_packet_ctx response;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800461
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700462 /* Reset the read context to the start. */
463 read->receive_index = 0;
464
465 status = receive_packet(ctx_data, &response);
466 if (status) {
467 /* Return the transfer error since the status_code is unreliable */
468 return status;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100469 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700470 if (response.packet_v1.response.status_code) {
471 return response.packet_v1.response.status_code;
Anton Staafb2647882014-09-17 15:13:43 -0700472 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700473 response.header_size = offsetof(struct usb_spi_response_v1, data);
Anton Staafb2647882014-09-17 15:13:43 -0700474
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700475 status = read_usb_packet(read, &response);
476 return status;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800477}
478
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700479/*
480 * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device,
481 * reads the status code and any payload from the device. This will also handle
482 * recovery if an error has occurred.
483 *
484 * @param flash Flash context storing SPI capabilities and USB device
485 * information.
486 * @param write_count Number of bytes to write
487 * @param read_count Number of bytes to read
488 * @param write_buffer Address of write buffer
489 * @param read_buffer Address of buffer to store read data
490 *
491 * @returns Returns status code with 0 on success.
492 */
493static int send_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700494 unsigned int write_count,
495 unsigned int read_count,
496 const unsigned char *write_buffer,
497 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800498{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800499 int status = -1;
500
Brian J. Nemec1889a032020-07-22 03:16:45 -0700501 struct usb_spi_transmit_ctx write_ctx = {
502 .buffer = write_buffer,
503 .transmit_size = write_count
504 };
505 struct usb_spi_receive_ctx read_ctx = {
506 .buffer = read_buffer,
507 .receive_size = read_count
508 };
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700509 const struct raiden_debug_spi_data *ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemec1889a032020-07-22 03:16:45 -0700510
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700511 if (write_count > PAYLOAD_SIZE_V1) {
512 msg_perr("Raiden: Invalid write count\n"
513 " write count = %u\n"
514 " max write = %d\n",
515 write_count, PAYLOAD_SIZE_V1);
516 return SPI_INVALID_LENGTH;
517 }
518
519 if (read_count > PAYLOAD_SIZE_V1) {
520 msg_perr("Raiden: Invalid read count\n"
521 " read count = %d\n"
522 " max read = %d\n",
523 read_count, PAYLOAD_SIZE_V1);
524 return SPI_INVALID_LENGTH;
525 }
526
527 for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS;
Brian J. Nemec1118a582020-02-04 18:26:02 -0800528 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800529
Brian J. Nemec1889a032020-07-22 03:16:45 -0700530
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700531 status = write_command_v1(ctx_data, &write_ctx, &read_ctx);
532
533 if (!status &&
534 (write_ctx.transmit_index != write_ctx.transmit_size)) {
535 /* No errors were reported, but write is incomplete. */
536 status = USB_SPI_HOST_TX_WRITE_FAILURE;
537 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800538
539 if (status) {
540 /* Write operation failed. */
541 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700542 " write count = %u\n"
543 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700544 " transmitted bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700545 " write attempt = %u\n"
546 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700547
548 write_count, read_count, write_ctx.transmit_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700549 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800550 if (!retry_recovery(status)) {
551 /* Reattempting will not result in a recovery. */
552 return status;
553 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700554 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800555 continue;
556 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700557 for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS;
558 read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800559
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700560 status = read_response_v1(ctx_data, &write_ctx, &read_ctx);
561
562 if (!status) {
563 if (read_ctx.receive_size == read_ctx.receive_index) {
564 /* Successful transfer. */
565 return status;
566 } else {
567 /* Report the error from the failed read. */
568 status = USB_SPI_HOST_RX_READ_FAILURE;
569 }
570 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800571
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700572 if (status) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800573 /* Read operation failed. */
574 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700575 " write count = %u\n"
576 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700577 " received bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700578 " write attempt = %u\n"
579 " read attempt = %u\n"
580 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700581 write_count, read_count, read_ctx.receive_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700582 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800583 if (!retry_recovery(status)) {
584 /* Reattempting will not result in a recovery. */
585 return status;
586 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700587 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800588 }
589 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800590 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800591 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700592}
593
594/*
595 * Unfortunately there doesn't seem to be a way to specify the maximum number
596 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700597 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700598 * of command for the flash device, resulting in a 62 byte packet, that we then
599 * add two bytes to in either direction, making our way up to the 64 byte
600 * maximum USB packet size for the device.
601 *
602 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700603 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700604 */
Brian J. Nemecb746f822020-07-22 02:57:56 -0700605#define MAX_DATA_SIZE (PAYLOAD_SIZE_V1 - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700606
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700607static struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -0700608 .features = SPI_MASTER_4BA,
609 .max_data_read = MAX_DATA_SIZE,
610 .max_data_write = MAX_DATA_SIZE,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700611 .command = send_command_v1,
Brian J. Nemecb746f822020-07-22 02:57:56 -0700612 .multicommand = default_spi_send_multicommand,
613 .read = default_spi_read,
614 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700615};
616
Anton Staaf5614e252015-03-24 14:33:33 -0700617static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800618 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700619{
Anton Staaf5614e252015-03-24 14:33:33 -0700620 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
621 direction) &&
622 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
623 LIBUSB_TRANSFER_TYPE_BULK));
624}
Anton Staafd27536d2014-09-30 08:10:17 -0700625
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700626static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700627{
628 int i;
629 int in_count = 0;
630 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700631
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100632 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700633 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100634 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700635
Anton Staaf5614e252015-03-24 14:33:33 -0700636 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
637 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100638 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700639 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
640 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100641 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700642 }
643 }
644
645 if (in_count != 1 || out_count != 1) {
646 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
647 " found %d IN and %d OUT endpoints\n",
648 in_count,
649 out_count);
650 return 1;
651 }
652
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100653 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
654 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700655
656 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700657}
658
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700659static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700660{
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700661 struct raiden_debug_spi_data * ctx_data =
662 (struct raiden_debug_spi_data *)data;
663
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100664 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700665 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700666 LIBUSB_ENDPOINT_OUT |
667 LIBUSB_REQUEST_TYPE_VENDOR |
668 LIBUSB_RECIPIENT_INTERFACE,
669 RAIDEN_DEBUG_SPI_REQ_DISABLE,
670 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700671 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700672 NULL,
673 0,
674 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100675 if (ret != 0) {
676 msg_perr("Raiden: Failed to disable SPI bridge\n");
677 return ret;
678 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700679
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700680 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700681 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700682 free(ctx_data);
Anton Staaf4589cd12015-03-23 13:36:44 -0700683
684 return 0;
685}
686
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100687static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700688{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700689 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700690
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100691 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700692 if (target_str) {
693 if (!strcasecmp(target_str, "ap"))
694 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
695 else if (!strcasecmp(target_str, "ec"))
696 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
697 else {
698 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100699 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700700 }
701 }
David Hendricks98b3c572016-11-30 01:50:08 +0000702 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700703
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100704 return request_enable;
705}
706
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700707static void free_dev_list(struct usb_device **dev_lst)
708{
709 struct usb_device *dev = *dev_lst;
710 /* free devices we don't care about */
711 dev = dev->next;
712 while (dev)
713 dev = usb_device_free(dev);
714}
715
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100716int raiden_debug_spi_init(void)
717{
718 struct usb_match match;
719 char *serial = extract_programmer_param("serial");
720 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700721 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100722 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100723 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100724
725 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700726 if (request_enable < 0) {
727 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100728 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700729 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100730
Anton Staaf5614e252015-03-24 14:33:33 -0700731 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700732
Anton Staaf5614e252015-03-24 14:33:33 -0700733 usb_match_value_default(&match.vid, GOOGLE_VID);
734 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
735 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
Brian J. Nemecb746f822020-07-22 02:57:56 -0700736 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);
Anton Staafb2647882014-09-17 15:13:43 -0700737
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100738 ret = LIBUSB(libusb_init(NULL));
739 if (ret != 0) {
740 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700741 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100742 return ret;
743 }
Anton Staaf5614e252015-03-24 14:33:33 -0700744
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100745 ret = usb_device_find(&match, &current);
746 if (ret != 0) {
747 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700748 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100749 return ret;
750 }
Anton Staaf5614e252015-03-24 14:33:33 -0700751
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700752 uint8_t in_endpoint = 0;
753 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700754 while (current) {
755 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700756
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100757 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700758 msg_pdbg("Raiden: Failed to find valid endpoints on device");
759 usb_device_show(" ", current);
760 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700761 }
Anton Staaf5614e252015-03-24 14:33:33 -0700762
David Hendricks5c79a492016-06-14 20:56:36 -0700763 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700764 msg_pdbg("Raiden: Failed to claim USB device");
765 usb_device_show(" ", current);
766 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700767 }
Anton Staaf5614e252015-03-24 14:33:33 -0700768
David Hendricks5c79a492016-06-14 20:56:36 -0700769 if (!serial) {
770 found = 1;
771 goto loop_end;
772 } else {
773 unsigned char dev_serial[32];
774 struct libusb_device_descriptor descriptor;
775 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700776
David Hendricks5c79a492016-06-14 20:56:36 -0700777 memset(dev_serial, 0, sizeof(dev_serial));
778
779 if (libusb_get_device_descriptor(device->device, &descriptor)) {
780 msg_pdbg("USB: Failed to get device descriptor.\n");
781 goto loop_end;
782 }
783
784 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700785 descriptor.iSerialNumber,
786 dev_serial,
787 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700788 if (rc < 0) {
789 LIBUSB(rc);
790 } else {
791 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700792 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700793 usb_device_show(" ", current);
794 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700795 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700796 usb_device_show(" ", current);
797 found = 1;
798 }
799 }
800 }
801
802loop_end:
803 if (found)
804 break;
805 else
806 current = usb_device_free(current);
807 }
808
809 if (!device || !found) {
810 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700811 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000812 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700813 }
814
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700815 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700816
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100817 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700818 device->handle,
819 LIBUSB_ENDPOINT_OUT |
820 LIBUSB_REQUEST_TYPE_VENDOR |
821 LIBUSB_RECIPIENT_INTERFACE,
822 request_enable,
823 0,
824 device->interface_descriptor->bInterfaceNumber,
825 NULL,
826 0,
827 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100828 if (ret != 0) {
829 msg_perr("Raiden: Failed to enable SPI bridge\n");
830 return ret;
831 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700832
Keith Short8453b552020-02-03 18:10:14 -0700833 /*
834 * Allow for power to settle on the AP and EC flash devices.
835 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
836 * can require up to 10 ms from power on to the first write.
837 */
838 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
839 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
840 usleep(50 * 1000);
841
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700842 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
843 if (!data) {
844 msg_perr("Unable to allocate space for extra SPI master data.\n");
845 return SPI_GENERIC_ERROR;
846 }
847
848 data->dev = device;
849 data->in_ep = in_endpoint;
850 data->out_ep = out_endpoint;
851
852 spi_master_raiden_debug.data = data;
853
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100854 register_spi_master(&spi_master_raiden_debug);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700855 register_shutdown(raiden_debug_spi_shutdown, data);
Anton Staafb2647882014-09-17 15:13:43 -0700856
David Hendricks98b3c572016-11-30 01:50:08 +0000857 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700858}