blob: d706841161e70ae1222ed8bb3371b55cc0f99423 [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;
91
92 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
93 msg_perr("Raiden: invalid write_count of %d\n", write_count);
94 return SPI_INVALID_LENGTH;
95 }
96
97 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
98 msg_perr("Raiden: invalid read_count of %d\n", read_count);
99 return SPI_INVALID_LENGTH;
100 }
101
102 buffer[0] = write_count;
103 buffer[1] = read_count;
104
105 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
106
Anton Staaf5614e252015-03-24 14:33:33 -0700107 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
108 out_endpoint,
109 buffer,
110 write_count + PACKET_HEADER_SIZE,
111 &transferred,
112 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700113 "Raiden: OUT transfer failed\n"
114 " write_count = %d\n"
115 " read_count = %d\n",
116 write_count,
117 read_count);
118
119 if (transferred != write_count + PACKET_HEADER_SIZE) {
120 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
121 transferred, write_count + PACKET_HEADER_SIZE);
122 return 0x10001;
123 }
124
Anton Staaf5614e252015-03-24 14:33:33 -0700125 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
126 in_endpoint,
127 buffer,
128 read_count + PACKET_HEADER_SIZE,
129 &transferred,
130 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700131 "Raiden: IN transfer failed\n"
132 " write_count = %d\n"
133 " read_count = %d\n",
134 write_count,
135 read_count);
136
137 if (transferred != read_count + PACKET_HEADER_SIZE) {
138 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
139 transferred, read_count + PACKET_HEADER_SIZE);
140 return 0x10002;
141 }
142
143 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
144
145 return buffer[0] | (buffer[1] << 8);
146}
147
148/*
149 * Unfortunately there doesn't seem to be a way to specify the maximum number
150 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700151 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700152 * of command for the flash device, resulting in a 62 byte packet, that we then
153 * add two bytes to in either direction, making our way up to the 64 byte
154 * maximum USB packet size for the device.
155 *
156 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700157 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700158 */
159#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
160 PACKET_HEADER_SIZE - \
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700161 JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700162
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100163static const struct spi_master spi_master_raiden_debug = {
Anton Staafb2647882014-09-17 15:13:43 -0700164 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
Mathew Kingbd9670f2019-08-07 14:17:48 -0600165 .features = SPI_MASTER_4BA,
Anton Staafb2647882014-09-17 15:13:43 -0700166 .max_data_read = MAX_DATA_SIZE,
167 .max_data_write = MAX_DATA_SIZE,
168 .command = send_command,
169 .multicommand = default_spi_send_multicommand,
170 .read = default_spi_read,
171 .write_256 = default_spi_write_256,
172};
173
Anton Staaf5614e252015-03-24 14:33:33 -0700174static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
175 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700176{
Anton Staaf5614e252015-03-24 14:33:33 -0700177 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
178 direction) &&
179 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
180 LIBUSB_TRANSFER_TYPE_BULK));
181}
Anton Staafd27536d2014-09-30 08:10:17 -0700182
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100183static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700184{
185 int i;
186 int in_count = 0;
187 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700188
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100189 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700190 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100191 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700192
Anton Staaf5614e252015-03-24 14:33:33 -0700193 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
194 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100195 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700196 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
197 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100198 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700199 }
200 }
201
202 if (in_count != 1 || out_count != 1) {
203 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
204 " found %d IN and %d OUT endpoints\n",
205 in_count,
206 out_count);
207 return 1;
208 }
209
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100210 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
211 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700212
213 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700214}
215
David Hendricks93784b42016-08-09 17:00:38 -0700216static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700217{
Anton Staaf5614e252015-03-24 14:33:33 -0700218 CHECK(LIBUSB(libusb_control_transfer(
219 device->handle,
220 LIBUSB_ENDPOINT_OUT |
221 LIBUSB_REQUEST_TYPE_VENDOR |
222 LIBUSB_RECIPIENT_INTERFACE,
223 RAIDEN_DEBUG_SPI_REQ_DISABLE,
224 0,
225 device->interface_descriptor->bInterfaceNumber,
226 NULL,
227 0,
228 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700229 "Raiden: Failed to disable SPI bridge\n");
230
Anton Staaf5614e252015-03-24 14:33:33 -0700231 usb_device_free(device);
232
233 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700234 libusb_exit(NULL);
235
236 return 0;
237}
238
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100239static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700240{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700241 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700242
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100243 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700244 if (target_str) {
245 if (!strcasecmp(target_str, "ap"))
246 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
247 else if (!strcasecmp(target_str, "ec"))
248 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
249 else {
250 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100251 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700252 }
253 }
David Hendricks98b3c572016-11-30 01:50:08 +0000254 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700255
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100256 return request_enable;
257}
258
259int raiden_debug_spi_init(void)
260{
261 struct usb_match match;
262 char *serial = extract_programmer_param("serial");
263 struct usb_device *current;
264 int found = 0;
265
266 int request_enable = get_target();
267 if (request_enable < 0)
268 return 1;
269
Anton Staaf5614e252015-03-24 14:33:33 -0700270 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700271
Anton Staaf5614e252015-03-24 14:33:33 -0700272 usb_match_value_default(&match.vid, GOOGLE_VID);
273 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
274 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
275 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700276
Anton Staaf5614e252015-03-24 14:33:33 -0700277 CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
278
David Hendricks5c79a492016-06-14 20:56:36 -0700279 CHECK(usb_device_find(&match, &current),
Anton Staaf5614e252015-03-24 14:33:33 -0700280 "Raiden: Failed to find devices\n");
281
David Hendricks5c79a492016-06-14 20:56:36 -0700282 while (current) {
283 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700284
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100285 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700286 msg_pdbg("Raiden: Failed to find valid endpoints on device");
287 usb_device_show(" ", current);
288 goto loop_end;
289 }
Anton Staaf5614e252015-03-24 14:33:33 -0700290
David Hendricks5c79a492016-06-14 20:56:36 -0700291 if (usb_device_claim(device)) {
292 msg_pdbg("Raiden: Failed to claim USB device");
293 usb_device_show(" ", current);
294 goto loop_end;
295 }
Anton Staaf5614e252015-03-24 14:33:33 -0700296
David Hendricks5c79a492016-06-14 20:56:36 -0700297 if (!serial) {
298 found = 1;
299 goto loop_end;
300 } else {
301 unsigned char dev_serial[32];
302 struct libusb_device_descriptor descriptor;
303 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700304
David Hendricks5c79a492016-06-14 20:56:36 -0700305 memset(dev_serial, 0, sizeof(dev_serial));
306
307 if (libusb_get_device_descriptor(device->device, &descriptor)) {
308 msg_pdbg("USB: Failed to get device descriptor.\n");
309 goto loop_end;
310 }
311
312 rc = libusb_get_string_descriptor_ascii(device->handle,
313 descriptor.iSerialNumber,
314 dev_serial,
315 sizeof(dev_serial));
316 if (rc < 0) {
317 LIBUSB(rc);
318 } else {
319 if (strcmp(serial, (char *)dev_serial)) {
320 msg_pdbg("Raiden: Serial number %s did not match device", serial);
321 usb_device_show(" ", current);
322 } else {
323 msg_pinfo("Raiden: Serial number %s matched device", serial);
324 usb_device_show(" ", current);
325 found = 1;
326 }
327 }
328 }
329
330loop_end:
331 if (found)
332 break;
333 else
334 current = usb_device_free(current);
335 }
336
337 if (!device || !found) {
338 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000339 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700340 }
341
David Hendricks5c79a492016-06-14 20:56:36 -0700342 /* free devices we don't care about */
343 current = current->next;
344 while (current)
345 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700346
Anton Staaf5614e252015-03-24 14:33:33 -0700347 CHECK(LIBUSB(libusb_control_transfer(
348 device->handle,
349 LIBUSB_ENDPOINT_OUT |
350 LIBUSB_REQUEST_TYPE_VENDOR |
351 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700352 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700353 0,
354 device->interface_descriptor->bInterfaceNumber,
355 NULL,
356 0,
357 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700358 "Raiden: Failed to enable SPI bridge\n");
359
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100360 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700361 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700362
David Hendricks98b3c572016-11-30 01:50:08 +0000363 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700364}