blob: 6ca47d4ad40d6f22f64595e76fbf2c6eec9263a5 [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>
Keith Short8453b552020-02-03 18:10:14 -070059#include <unistd.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;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +110092 int ret;
Anton Staafb2647882014-09-17 15:13:43 -070093
94 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
95 msg_perr("Raiden: invalid write_count of %d\n", write_count);
96 return SPI_INVALID_LENGTH;
97 }
98
99 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
100 msg_perr("Raiden: invalid read_count of %d\n", read_count);
101 return SPI_INVALID_LENGTH;
102 }
103
104 buffer[0] = write_count;
105 buffer[1] = read_count;
106
107 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
108
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100109 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700110 out_endpoint,
111 buffer,
112 write_count + PACKET_HEADER_SIZE,
113 &transferred,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100114 TRANSFER_TIMEOUT_MS));
115 if (ret != 0) {
116 msg_perr("Raiden: OUT transfer failed\n"
117 " write_count = %d\n"
118 " read_count = %d\n",
119 write_count, read_count);
120 return ret;
121 }
Anton Staafb2647882014-09-17 15:13:43 -0700122
123 if (transferred != write_count + PACKET_HEADER_SIZE) {
124 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
125 transferred, write_count + PACKET_HEADER_SIZE);
126 return 0x10001;
127 }
128
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100129 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700130 in_endpoint,
131 buffer,
132 read_count + PACKET_HEADER_SIZE,
133 &transferred,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100134 TRANSFER_TIMEOUT_MS));
135 if (ret != 0) {
136 msg_perr("Raiden: IN transfer failed\n"
137 " write_count = %d\n"
138 " read_count = %d\n",
139 write_count, read_count);
140 return ret;
141 }
Anton Staafb2647882014-09-17 15:13:43 -0700142
143 if (transferred != read_count + PACKET_HEADER_SIZE) {
144 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
145 transferred, read_count + PACKET_HEADER_SIZE);
146 return 0x10002;
147 }
148
149 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
150
151 return buffer[0] | (buffer[1] << 8);
152}
153
154/*
155 * Unfortunately there doesn't seem to be a way to specify the maximum number
156 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700157 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700158 * of command for the flash device, resulting in a 62 byte packet, that we then
159 * add two bytes to in either direction, making our way up to the 64 byte
160 * maximum USB packet size for the device.
161 *
162 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700163 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700164 */
165#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
166 PACKET_HEADER_SIZE - \
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700167 JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700168
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100169static const struct spi_master spi_master_raiden_debug = {
Anton Staafb2647882014-09-17 15:13:43 -0700170 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
Mathew Kingbd9670f2019-08-07 14:17:48 -0600171 .features = SPI_MASTER_4BA,
Anton Staafb2647882014-09-17 15:13:43 -0700172 .max_data_read = MAX_DATA_SIZE,
173 .max_data_write = MAX_DATA_SIZE,
174 .command = send_command,
175 .multicommand = default_spi_send_multicommand,
176 .read = default_spi_read,
177 .write_256 = default_spi_write_256,
178};
179
Anton Staaf5614e252015-03-24 14:33:33 -0700180static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
181 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700182{
Anton Staaf5614e252015-03-24 14:33:33 -0700183 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
184 direction) &&
185 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
186 LIBUSB_TRANSFER_TYPE_BULK));
187}
Anton Staafd27536d2014-09-30 08:10:17 -0700188
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100189static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700190{
191 int i;
192 int in_count = 0;
193 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700194
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100195 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700196 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100197 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700198
Anton Staaf5614e252015-03-24 14:33:33 -0700199 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
200 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100201 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700202 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
203 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100204 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700205 }
206 }
207
208 if (in_count != 1 || out_count != 1) {
209 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
210 " found %d IN and %d OUT endpoints\n",
211 in_count,
212 out_count);
213 return 1;
214 }
215
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100216 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
217 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700218
219 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700220}
221
David Hendricks93784b42016-08-09 17:00:38 -0700222static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700223{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100224 int ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700225 device->handle,
226 LIBUSB_ENDPOINT_OUT |
227 LIBUSB_REQUEST_TYPE_VENDOR |
228 LIBUSB_RECIPIENT_INTERFACE,
229 RAIDEN_DEBUG_SPI_REQ_DISABLE,
230 0,
231 device->interface_descriptor->bInterfaceNumber,
232 NULL,
233 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100234 TRANSFER_TIMEOUT_MS));
235 if (ret != 0) {
236 msg_perr("Raiden: Failed to disable SPI bridge\n");
237 return ret;
238 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700239
Anton Staaf5614e252015-03-24 14:33:33 -0700240 usb_device_free(device);
241
242 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700243 libusb_exit(NULL);
244
245 return 0;
246}
247
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100248static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700249{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700250 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700251
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100252 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700253 if (target_str) {
254 if (!strcasecmp(target_str, "ap"))
255 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
256 else if (!strcasecmp(target_str, "ec"))
257 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
258 else {
259 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100260 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700261 }
262 }
David Hendricks98b3c572016-11-30 01:50:08 +0000263 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700264
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100265 return request_enable;
266}
267
268int raiden_debug_spi_init(void)
269{
270 struct usb_match match;
271 char *serial = extract_programmer_param("serial");
272 struct usb_device *current;
273 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100274 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100275
276 int request_enable = get_target();
277 if (request_enable < 0)
278 return 1;
279
Anton Staaf5614e252015-03-24 14:33:33 -0700280 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700281
Anton Staaf5614e252015-03-24 14:33:33 -0700282 usb_match_value_default(&match.vid, GOOGLE_VID);
283 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
284 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
285 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700286
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100287 ret = LIBUSB(libusb_init(NULL));
288 if (ret != 0) {
289 msg_perr("Raiden: libusb_init failed\n");
290 return ret;
291 }
Anton Staaf5614e252015-03-24 14:33:33 -0700292
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100293 ret = usb_device_find(&match, &current);
294 if (ret != 0) {
295 msg_perr("Raiden: Failed to find devices\n");
296 return ret;
297 }
Anton Staaf5614e252015-03-24 14:33:33 -0700298
David Hendricks5c79a492016-06-14 20:56:36 -0700299 while (current) {
300 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700301
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100302 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700303 msg_pdbg("Raiden: Failed to find valid endpoints on device");
304 usb_device_show(" ", current);
305 goto loop_end;
306 }
Anton Staaf5614e252015-03-24 14:33:33 -0700307
David Hendricks5c79a492016-06-14 20:56:36 -0700308 if (usb_device_claim(device)) {
309 msg_pdbg("Raiden: Failed to claim USB device");
310 usb_device_show(" ", current);
311 goto loop_end;
312 }
Anton Staaf5614e252015-03-24 14:33:33 -0700313
David Hendricks5c79a492016-06-14 20:56:36 -0700314 if (!serial) {
315 found = 1;
316 goto loop_end;
317 } else {
318 unsigned char dev_serial[32];
319 struct libusb_device_descriptor descriptor;
320 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700321
David Hendricks5c79a492016-06-14 20:56:36 -0700322 memset(dev_serial, 0, sizeof(dev_serial));
323
324 if (libusb_get_device_descriptor(device->device, &descriptor)) {
325 msg_pdbg("USB: Failed to get device descriptor.\n");
326 goto loop_end;
327 }
328
329 rc = libusb_get_string_descriptor_ascii(device->handle,
330 descriptor.iSerialNumber,
331 dev_serial,
332 sizeof(dev_serial));
333 if (rc < 0) {
334 LIBUSB(rc);
335 } else {
336 if (strcmp(serial, (char *)dev_serial)) {
337 msg_pdbg("Raiden: Serial number %s did not match device", serial);
338 usb_device_show(" ", current);
339 } else {
340 msg_pinfo("Raiden: Serial number %s matched device", serial);
341 usb_device_show(" ", current);
342 found = 1;
343 }
344 }
345 }
346
347loop_end:
348 if (found)
349 break;
350 else
351 current = usb_device_free(current);
352 }
353
354 if (!device || !found) {
355 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000356 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700357 }
358
David Hendricks5c79a492016-06-14 20:56:36 -0700359 /* free devices we don't care about */
360 current = current->next;
361 while (current)
362 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700363
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100364 ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700365 device->handle,
366 LIBUSB_ENDPOINT_OUT |
367 LIBUSB_REQUEST_TYPE_VENDOR |
368 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700369 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700370 0,
371 device->interface_descriptor->bInterfaceNumber,
372 NULL,
373 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100374 TRANSFER_TIMEOUT_MS));
375 if (ret != 0) {
376 msg_perr("Raiden: Failed to enable SPI bridge\n");
377 return ret;
378 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700379
Keith Short8453b552020-02-03 18:10:14 -0700380 /*
381 * Allow for power to settle on the AP and EC flash devices.
382 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
383 * can require up to 10 ms from power on to the first write.
384 */
385 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
386 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
387 usleep(50 * 1000);
388
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100389 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700390 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700391
David Hendricks98b3c572016-11-30 01:50:08 +0000392 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700393}