blob: cf502f762f3ccd0dc7fd15a18de52d57092e4005 [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 Staaf5614e252015-03-24 14:33:33 -070051#include "check.h"
Anton Staafb2647882014-09-17 15:13:43 -070052#include "programmer.h"
53#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -070054#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -070055
56#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -070057#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -070058#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -070059#include <string.h>
Anton Staafb2647882014-09-17 15:13:43 -070060
Anton Staaf5614e252015-03-24 14:33:33 -070061#define GOOGLE_VID 0x18D1
62#define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51
63#define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01
Anton Staafb2647882014-09-17 15:13:43 -070064
Anton Staaf4589cd12015-03-23 13:36:44 -070065enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -070066 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
67 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
68 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
69 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -070070};
71
Anton Staafd27536d2014-09-30 08:10:17 -070072#define PACKET_HEADER_SIZE 2
73#define MAX_PACKET_SIZE 64
Anton Staafb2647882014-09-17 15:13:43 -070074
75/*
76 * This timeout is so large because the Raiden SPI timeout is 800ms.
77 */
78#define TRANSFER_TIMEOUT_MS 1000
79
Anton Staaf5614e252015-03-24 14:33:33 -070080struct usb_device *device = NULL;
81uint8_t in_endpoint = 0;
82uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -070083
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084static int send_command(const struct flashctx *flash,
85 unsigned int write_count,
Anton Staafb2647882014-09-17 15:13:43 -070086 unsigned int read_count,
87 const unsigned char *write_buffer,
88 unsigned char *read_buffer)
89{
90 uint8_t buffer[MAX_PACKET_SIZE];
91 int transferred;
92
93 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
94 msg_perr("Raiden: invalid write_count of %d\n", write_count);
95 return SPI_INVALID_LENGTH;
96 }
97
98 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
99 msg_perr("Raiden: invalid read_count of %d\n", read_count);
100 return SPI_INVALID_LENGTH;
101 }
102
103 buffer[0] = write_count;
104 buffer[1] = read_count;
105
106 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
107
Anton Staaf5614e252015-03-24 14:33:33 -0700108 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
109 out_endpoint,
110 buffer,
111 write_count + PACKET_HEADER_SIZE,
112 &transferred,
113 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700114 "Raiden: OUT transfer failed\n"
115 " write_count = %d\n"
116 " read_count = %d\n",
117 write_count,
118 read_count);
119
120 if (transferred != write_count + PACKET_HEADER_SIZE) {
121 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
122 transferred, write_count + PACKET_HEADER_SIZE);
123 return 0x10001;
124 }
125
Anton Staaf5614e252015-03-24 14:33:33 -0700126 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
127 in_endpoint,
128 buffer,
129 read_count + PACKET_HEADER_SIZE,
130 &transferred,
131 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700132 "Raiden: IN transfer failed\n"
133 " write_count = %d\n"
134 " read_count = %d\n",
135 write_count,
136 read_count);
137
138 if (transferred != read_count + PACKET_HEADER_SIZE) {
139 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
140 transferred, read_count + PACKET_HEADER_SIZE);
141 return 0x10002;
142 }
143
144 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
145
146 return buffer[0] | (buffer[1] << 8);
147}
148
149/*
150 * Unfortunately there doesn't seem to be a way to specify the maximum number
151 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700152 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700153 * of command for the flash device, resulting in a 62 byte packet, that we then
154 * add two bytes to in either direction, making our way up to the 64 byte
155 * maximum USB packet size for the device.
156 *
157 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700158 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700159 */
160#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
161 PACKET_HEADER_SIZE - \
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700162 JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700163
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100164static const struct spi_master spi_master_raiden_debug = {
Anton Staafb2647882014-09-17 15:13:43 -0700165 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
Mathew Kingbd9670f2019-08-07 14:17:48 -0600166 .features = SPI_MASTER_4BA,
Anton Staafb2647882014-09-17 15:13:43 -0700167 .max_data_read = MAX_DATA_SIZE,
168 .max_data_write = MAX_DATA_SIZE,
169 .command = send_command,
170 .multicommand = default_spi_send_multicommand,
171 .read = default_spi_read,
172 .write_256 = default_spi_write_256,
173};
174
Anton Staaf5614e252015-03-24 14:33:33 -0700175static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
176 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700177{
Anton Staaf5614e252015-03-24 14:33:33 -0700178 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
179 direction) &&
180 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
181 LIBUSB_TRANSFER_TYPE_BULK));
182}
Anton Staafd27536d2014-09-30 08:10:17 -0700183
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100184static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700185{
186 int i;
187 int in_count = 0;
188 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700189
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100190 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700191 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100192 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700193
Anton Staaf5614e252015-03-24 14:33:33 -0700194 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
195 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100196 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700197 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
198 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100199 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700200 }
201 }
202
203 if (in_count != 1 || out_count != 1) {
204 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
205 " found %d IN and %d OUT endpoints\n",
206 in_count,
207 out_count);
208 return 1;
209 }
210
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100211 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
212 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700213
214 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700215}
216
David Hendricks93784b42016-08-09 17:00:38 -0700217static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700218{
Anton Staaf5614e252015-03-24 14:33:33 -0700219 CHECK(LIBUSB(libusb_control_transfer(
220 device->handle,
221 LIBUSB_ENDPOINT_OUT |
222 LIBUSB_REQUEST_TYPE_VENDOR |
223 LIBUSB_RECIPIENT_INTERFACE,
224 RAIDEN_DEBUG_SPI_REQ_DISABLE,
225 0,
226 device->interface_descriptor->bInterfaceNumber,
227 NULL,
228 0,
229 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700230 "Raiden: Failed to disable SPI bridge\n");
231
Anton Staaf5614e252015-03-24 14:33:33 -0700232 usb_device_free(device);
233
234 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700235 libusb_exit(NULL);
236
237 return 0;
238}
239
David Hendricksac1d25c2016-08-09 17:00:58 -0700240int raiden_debug_spi_init(void)
Anton Staafb2647882014-09-17 15:13:43 -0700241{
Anton Staaf5614e252015-03-24 14:33:33 -0700242 struct usb_match match;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700243 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
244 char *target_str = extract_programmer_param("target");
David Hendricks5c79a492016-06-14 20:56:36 -0700245 char *serial = extract_programmer_param("serial");
246 struct usb_device *current;
247 int found = 0;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700248
249 if (target_str) {
250 if (!strcasecmp(target_str, "ap"))
251 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
252 else if (!strcasecmp(target_str, "ec"))
253 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
254 else {
255 msg_perr("Invalid target: %s\n", target_str);
256 free(target_str);
David Hendricks98b3c572016-11-30 01:50:08 +0000257 return 1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700258 }
259 }
David Hendricks98b3c572016-11-30 01:50:08 +0000260 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700261
Anton Staaf5614e252015-03-24 14:33:33 -0700262 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700263
Anton Staaf5614e252015-03-24 14:33:33 -0700264 usb_match_value_default(&match.vid, GOOGLE_VID);
265 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
266 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
267 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700268
Anton Staaf5614e252015-03-24 14:33:33 -0700269 CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
270
David Hendricks5c79a492016-06-14 20:56:36 -0700271 CHECK(usb_device_find(&match, &current),
Anton Staaf5614e252015-03-24 14:33:33 -0700272 "Raiden: Failed to find devices\n");
273
David Hendricks5c79a492016-06-14 20:56:36 -0700274 while (current) {
275 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700276
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100277 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700278 msg_pdbg("Raiden: Failed to find valid endpoints on device");
279 usb_device_show(" ", current);
280 goto loop_end;
281 }
Anton Staaf5614e252015-03-24 14:33:33 -0700282
David Hendricks5c79a492016-06-14 20:56:36 -0700283 if (usb_device_claim(device)) {
284 msg_pdbg("Raiden: Failed to claim USB device");
285 usb_device_show(" ", current);
286 goto loop_end;
287 }
Anton Staaf5614e252015-03-24 14:33:33 -0700288
David Hendricks5c79a492016-06-14 20:56:36 -0700289 if (!serial) {
290 found = 1;
291 goto loop_end;
292 } else {
293 unsigned char dev_serial[32];
294 struct libusb_device_descriptor descriptor;
295 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700296
David Hendricks5c79a492016-06-14 20:56:36 -0700297 memset(dev_serial, 0, sizeof(dev_serial));
298
299 if (libusb_get_device_descriptor(device->device, &descriptor)) {
300 msg_pdbg("USB: Failed to get device descriptor.\n");
301 goto loop_end;
302 }
303
304 rc = libusb_get_string_descriptor_ascii(device->handle,
305 descriptor.iSerialNumber,
306 dev_serial,
307 sizeof(dev_serial));
308 if (rc < 0) {
309 LIBUSB(rc);
310 } else {
311 if (strcmp(serial, (char *)dev_serial)) {
312 msg_pdbg("Raiden: Serial number %s did not match device", serial);
313 usb_device_show(" ", current);
314 } else {
315 msg_pinfo("Raiden: Serial number %s matched device", serial);
316 usb_device_show(" ", current);
317 found = 1;
318 }
319 }
320 }
321
322loop_end:
323 if (found)
324 break;
325 else
326 current = usb_device_free(current);
327 }
328
329 if (!device || !found) {
330 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000331 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700332 }
333
David Hendricks5c79a492016-06-14 20:56:36 -0700334 /* free devices we don't care about */
335 current = current->next;
336 while (current)
337 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700338
Anton Staaf5614e252015-03-24 14:33:33 -0700339 CHECK(LIBUSB(libusb_control_transfer(
340 device->handle,
341 LIBUSB_ENDPOINT_OUT |
342 LIBUSB_REQUEST_TYPE_VENDOR |
343 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700344 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700345 0,
346 device->interface_descriptor->bInterfaceNumber,
347 NULL,
348 0,
349 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700350 "Raiden: Failed to enable SPI bridge\n");
351
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100352 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700353 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700354
David Hendricks98b3c572016-11-30 01:50:08 +0000355 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700356}