blob: 11ef046da4517e67e328dcf72dfc8c9a6a90754c [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 *
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 Staafb2647882014-09-17 15:13:43 -070051#include "programmer.h"
52#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -070053#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -070054
55#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -070056#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -070057#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -070058#include <string.h>
Anton Staafb2647882014-09-17 15:13:43 -070059
Anton Staaf5614e252015-03-24 14:33:33 -070060#define GOOGLE_VID 0x18D1
61#define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51
62#define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01
Anton Staafb2647882014-09-17 15:13:43 -070063
Anton Staaf4589cd12015-03-23 13:36:44 -070064enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -070065 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
66 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
67 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
68 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -070069};
70
Anton Staafd27536d2014-09-30 08:10:17 -070071#define PACKET_HEADER_SIZE 2
72#define MAX_PACKET_SIZE 64
Anton Staafb2647882014-09-17 15:13:43 -070073
74/*
75 * This timeout is so large because the Raiden SPI timeout is 800ms.
76 */
77#define TRANSFER_TIMEOUT_MS 1000
78
Anton Staaf5614e252015-03-24 14:33:33 -070079struct usb_device *device = NULL;
80uint8_t in_endpoint = 0;
81uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -070082
Souvik Ghoshd75cd672016-06-17 14:21:39 -070083static int send_command(const struct flashctx *flash,
84 unsigned int write_count,
Anton Staafb2647882014-09-17 15:13:43 -070085 unsigned int read_count,
86 const unsigned char *write_buffer,
87 unsigned char *read_buffer)
88{
89 uint8_t buffer[MAX_PACKET_SIZE];
90 int transferred;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +110091 int ret;
Anton Staafb2647882014-09-17 15:13:43 -070092
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
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100108 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700109 out_endpoint,
110 buffer,
111 write_count + PACKET_HEADER_SIZE,
112 &transferred,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100113 TRANSFER_TIMEOUT_MS));
114 if (ret != 0) {
115 msg_perr("Raiden: OUT transfer failed\n"
116 " write_count = %d\n"
117 " read_count = %d\n",
118 write_count, read_count);
119 return ret;
120 }
Anton Staafb2647882014-09-17 15:13:43 -0700121
122 if (transferred != write_count + PACKET_HEADER_SIZE) {
123 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
124 transferred, write_count + PACKET_HEADER_SIZE);
125 return 0x10001;
126 }
127
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100128 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700129 in_endpoint,
130 buffer,
131 read_count + PACKET_HEADER_SIZE,
132 &transferred,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100133 TRANSFER_TIMEOUT_MS));
134 if (ret != 0) {
135 msg_perr("Raiden: IN transfer failed\n"
136 " write_count = %d\n"
137 " read_count = %d\n",
138 write_count, read_count);
139 return ret;
140 }
Anton Staafb2647882014-09-17 15:13:43 -0700141
142 if (transferred != read_count + PACKET_HEADER_SIZE) {
143 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
144 transferred, read_count + PACKET_HEADER_SIZE);
145 return 0x10002;
146 }
147
148 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
149
150 return buffer[0] | (buffer[1] << 8);
151}
152
153/*
154 * Unfortunately there doesn't seem to be a way to specify the maximum number
155 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700156 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700157 * of command for the flash device, resulting in a 62 byte packet, that we then
158 * add two bytes to in either direction, making our way up to the 64 byte
159 * maximum USB packet size for the device.
160 *
161 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700162 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700163 */
164#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
165 PACKET_HEADER_SIZE - \
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700166 JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700167
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100168static const struct spi_master spi_master_raiden_debug = {
Anton Staafb2647882014-09-17 15:13:43 -0700169 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
Mathew Kingbd9670f2019-08-07 14:17:48 -0600170 .features = SPI_MASTER_4BA,
Anton Staafb2647882014-09-17 15:13:43 -0700171 .max_data_read = MAX_DATA_SIZE,
172 .max_data_write = MAX_DATA_SIZE,
173 .command = send_command,
174 .multicommand = default_spi_send_multicommand,
175 .read = default_spi_read,
176 .write_256 = default_spi_write_256,
177};
178
Anton Staaf5614e252015-03-24 14:33:33 -0700179static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
180 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700181{
Anton Staaf5614e252015-03-24 14:33:33 -0700182 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
183 direction) &&
184 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
185 LIBUSB_TRANSFER_TYPE_BULK));
186}
Anton Staafd27536d2014-09-30 08:10:17 -0700187
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100188static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700189{
190 int i;
191 int in_count = 0;
192 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700193
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100194 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700195 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100196 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700197
Anton Staaf5614e252015-03-24 14:33:33 -0700198 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
199 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100200 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700201 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
202 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100203 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700204 }
205 }
206
207 if (in_count != 1 || out_count != 1) {
208 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
209 " found %d IN and %d OUT endpoints\n",
210 in_count,
211 out_count);
212 return 1;
213 }
214
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100215 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
216 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700217
218 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700219}
220
David Hendricks93784b42016-08-09 17:00:38 -0700221static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700222{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100223 int ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700224 device->handle,
225 LIBUSB_ENDPOINT_OUT |
226 LIBUSB_REQUEST_TYPE_VENDOR |
227 LIBUSB_RECIPIENT_INTERFACE,
228 RAIDEN_DEBUG_SPI_REQ_DISABLE,
229 0,
230 device->interface_descriptor->bInterfaceNumber,
231 NULL,
232 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100233 TRANSFER_TIMEOUT_MS));
234 if (ret != 0) {
235 msg_perr("Raiden: Failed to disable SPI bridge\n");
236 return ret;
237 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700238
Anton Staaf5614e252015-03-24 14:33:33 -0700239 usb_device_free(device);
240
241 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700242 libusb_exit(NULL);
243
244 return 0;
245}
246
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100247static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700248{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700249 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700250
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100251 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700252 if (target_str) {
253 if (!strcasecmp(target_str, "ap"))
254 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
255 else if (!strcasecmp(target_str, "ec"))
256 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
257 else {
258 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100259 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700260 }
261 }
David Hendricks98b3c572016-11-30 01:50:08 +0000262 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700263
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100264 return request_enable;
265}
266
267int raiden_debug_spi_init(void)
268{
269 struct usb_match match;
270 char *serial = extract_programmer_param("serial");
271 struct usb_device *current;
272 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100273 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100274
275 int request_enable = get_target();
276 if (request_enable < 0)
277 return 1;
278
Anton Staaf5614e252015-03-24 14:33:33 -0700279 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700280
Anton Staaf5614e252015-03-24 14:33:33 -0700281 usb_match_value_default(&match.vid, GOOGLE_VID);
282 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
283 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
284 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700285
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100286 ret = LIBUSB(libusb_init(NULL));
287 if (ret != 0) {
288 msg_perr("Raiden: libusb_init failed\n");
289 return ret;
290 }
Anton Staaf5614e252015-03-24 14:33:33 -0700291
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100292 ret = usb_device_find(&match, &current);
293 if (ret != 0) {
294 msg_perr("Raiden: Failed to find devices\n");
295 return ret;
296 }
Anton Staaf5614e252015-03-24 14:33:33 -0700297
David Hendricks5c79a492016-06-14 20:56:36 -0700298 while (current) {
299 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700300
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100301 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700302 msg_pdbg("Raiden: Failed to find valid endpoints on device");
303 usb_device_show(" ", current);
304 goto loop_end;
305 }
Anton Staaf5614e252015-03-24 14:33:33 -0700306
David Hendricks5c79a492016-06-14 20:56:36 -0700307 if (usb_device_claim(device)) {
308 msg_pdbg("Raiden: Failed to claim USB device");
309 usb_device_show(" ", current);
310 goto loop_end;
311 }
Anton Staaf5614e252015-03-24 14:33:33 -0700312
David Hendricks5c79a492016-06-14 20:56:36 -0700313 if (!serial) {
314 found = 1;
315 goto loop_end;
316 } else {
317 unsigned char dev_serial[32];
318 struct libusb_device_descriptor descriptor;
319 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700320
David Hendricks5c79a492016-06-14 20:56:36 -0700321 memset(dev_serial, 0, sizeof(dev_serial));
322
323 if (libusb_get_device_descriptor(device->device, &descriptor)) {
324 msg_pdbg("USB: Failed to get device descriptor.\n");
325 goto loop_end;
326 }
327
328 rc = libusb_get_string_descriptor_ascii(device->handle,
329 descriptor.iSerialNumber,
330 dev_serial,
331 sizeof(dev_serial));
332 if (rc < 0) {
333 LIBUSB(rc);
334 } else {
335 if (strcmp(serial, (char *)dev_serial)) {
336 msg_pdbg("Raiden: Serial number %s did not match device", serial);
337 usb_device_show(" ", current);
338 } else {
339 msg_pinfo("Raiden: Serial number %s matched device", serial);
340 usb_device_show(" ", current);
341 found = 1;
342 }
343 }
344 }
345
346loop_end:
347 if (found)
348 break;
349 else
350 current = usb_device_free(current);
351 }
352
353 if (!device || !found) {
354 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000355 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700356 }
357
David Hendricks5c79a492016-06-14 20:56:36 -0700358 /* free devices we don't care about */
359 current = current->next;
360 while (current)
361 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700362
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100363 ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700364 device->handle,
365 LIBUSB_ENDPOINT_OUT |
366 LIBUSB_REQUEST_TYPE_VENDOR |
367 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700368 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700369 0,
370 device->interface_descriptor->bInterfaceNumber,
371 NULL,
372 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100373 TRANSFER_TIMEOUT_MS));
374 if (ret != 0) {
375 msg_perr("Raiden: Failed to enable SPI bridge\n");
376 return ret;
377 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700378
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100379 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700380 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700381
David Hendricks98b3c572016-11-30 01:50:08 +0000382 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700383}