raiden_debug_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  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. * This SPI flash programming interface is designed to talk to a Chromium OS
  39. * device over a Raiden USB connection. The USB connection is routed to a
  40. * microcontroller running an image compiled from:
  41. *
  42. * https://chromium.googlesource.com/chromiumos/platform/ec
  43. *
  44. * The protocol for the USB-SPI bridge is documented in the following file in
  45. * that respository:
  46. *
  47. * chip/stm32/usb_spi.c
  48. */
  49. #include "check.h"
  50. #include "programmer.h"
  51. #include "spi.h"
  52. #include "usb_device.h"
  53. #include <libusb.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #define GOOGLE_VID 0x18D1
  58. #define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51
  59. #define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01
  60. enum raiden_debug_spi_request {
  61. RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
  62. RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
  63. RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
  64. RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
  65. };
  66. #define PACKET_HEADER_SIZE 2
  67. #define MAX_PACKET_SIZE 64
  68. /*
  69. * This timeout is so large because the Raiden SPI timeout is 800ms.
  70. */
  71. #define TRANSFER_TIMEOUT_MS 1000
  72. struct usb_device *device = NULL;
  73. uint8_t in_endpoint = 0;
  74. uint8_t out_endpoint = 0;
  75. static int send_command(const struct flashctx *flash,
  76. unsigned int write_count,
  77. unsigned int read_count,
  78. const unsigned char *write_buffer,
  79. unsigned char *read_buffer)
  80. {
  81. uint8_t buffer[MAX_PACKET_SIZE];
  82. int transferred;
  83. if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
  84. msg_perr("Raiden: invalid write_count of %d\n", write_count);
  85. return SPI_INVALID_LENGTH;
  86. }
  87. if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
  88. msg_perr("Raiden: invalid read_count of %d\n", read_count);
  89. return SPI_INVALID_LENGTH;
  90. }
  91. buffer[0] = write_count;
  92. buffer[1] = read_count;
  93. memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
  94. CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
  95. out_endpoint,
  96. buffer,
  97. write_count + PACKET_HEADER_SIZE,
  98. &transferred,
  99. TRANSFER_TIMEOUT_MS)),
  100. "Raiden: OUT transfer failed\n"
  101. " write_count = %d\n"
  102. " read_count = %d\n",
  103. write_count,
  104. read_count);
  105. if (transferred != write_count + PACKET_HEADER_SIZE) {
  106. msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
  107. transferred, write_count + PACKET_HEADER_SIZE);
  108. return 0x10001;
  109. }
  110. CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
  111. in_endpoint,
  112. buffer,
  113. read_count + PACKET_HEADER_SIZE,
  114. &transferred,
  115. TRANSFER_TIMEOUT_MS)),
  116. "Raiden: IN transfer failed\n"
  117. " write_count = %d\n"
  118. " read_count = %d\n",
  119. write_count,
  120. read_count);
  121. if (transferred != read_count + PACKET_HEADER_SIZE) {
  122. msg_perr("Raiden: Read failure (read %d, expected %d)\n",
  123. transferred, read_count + PACKET_HEADER_SIZE);
  124. return 0x10002;
  125. }
  126. memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
  127. return buffer[0] | (buffer[1] << 8);
  128. }
  129. /*
  130. * Unfortunately there doesn't seem to be a way to specify the maximum number
  131. * of bytes that your SPI device can read/write, these values are the maximum
  132. * data chunk size that flashrom will package up with an additional four bytes
  133. * of command for the flash device, resulting in a 62 byte packet, that we then
  134. * add two bytes to in either direction, making our way up to the 64 byte
  135. * maximum USB packet size for the device.
  136. *
  137. * The largest command that flashrom generates is the byte program command, so
  138. * we use that command header maximum size here. The definition of
  139. * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data
  140. * to write, so we add one byte of space back.
  141. */
  142. #define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
  143. PACKET_HEADER_SIZE - \
  144. JEDEC_BYTE_PROGRAM_OUTSIZE + \
  145. 1)
  146. static const struct spi_programmer spi_programmer_raiden_debug = {
  147. .type = SPI_CONTROLLER_RAIDEN_DEBUG,
  148. .max_data_read = MAX_DATA_SIZE,
  149. .max_data_write = MAX_DATA_SIZE,
  150. .command = send_command,
  151. .multicommand = default_spi_send_multicommand,
  152. .read = default_spi_read,
  153. .write_256 = default_spi_write_256,
  154. };
  155. static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
  156. enum libusb_endpoint_direction direction)
  157. {
  158. return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
  159. direction) &&
  160. ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
  161. LIBUSB_TRANSFER_TYPE_BULK));
  162. }
  163. static int find_endpoints(void)
  164. {
  165. int i;
  166. int in_count = 0;
  167. int out_count = 0;
  168. for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) {
  169. struct libusb_endpoint_descriptor const *endpoint =
  170. &device->interface_descriptor->endpoint[i];
  171. if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
  172. in_count++;
  173. in_endpoint = endpoint->bEndpointAddress;
  174. } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
  175. out_count++;
  176. out_endpoint = endpoint->bEndpointAddress;
  177. }
  178. }
  179. if (in_count != 1 || out_count != 1) {
  180. msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
  181. " found %d IN and %d OUT endpoints\n",
  182. in_count,
  183. out_count);
  184. return 1;
  185. }
  186. msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint);
  187. msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint);
  188. return 0;
  189. }
  190. static int shutdown(void * data)
  191. {
  192. CHECK(LIBUSB(libusb_control_transfer(
  193. device->handle,
  194. LIBUSB_ENDPOINT_OUT |
  195. LIBUSB_REQUEST_TYPE_VENDOR |
  196. LIBUSB_RECIPIENT_INTERFACE,
  197. RAIDEN_DEBUG_SPI_REQ_DISABLE,
  198. 0,
  199. device->interface_descriptor->bInterfaceNumber,
  200. NULL,
  201. 0,
  202. TRANSFER_TIMEOUT_MS)),
  203. "Raiden: Failed to disable SPI bridge\n");
  204. usb_device_free(device);
  205. device = NULL;
  206. libusb_exit(NULL);
  207. return 0;
  208. }
  209. int raiden_debug_spi_init(void)
  210. {
  211. struct usb_match match;
  212. int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
  213. char *target_str = extract_programmer_param("target");
  214. char *serial = extract_programmer_param("serial");
  215. struct usb_device *current;
  216. int found = 0;
  217. if (target_str) {
  218. if (!strcasecmp(target_str, "ap"))
  219. request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
  220. else if (!strcasecmp(target_str, "ec"))
  221. request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
  222. else {
  223. msg_perr("Invalid target: %s\n", target_str);
  224. free(target_str);
  225. return 1;
  226. }
  227. }
  228. free(target_str);
  229. usb_match_init(&match);
  230. usb_match_value_default(&match.vid, GOOGLE_VID);
  231. usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
  232. usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
  233. usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
  234. CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
  235. CHECK(usb_device_find(&match, &current),
  236. "Raiden: Failed to find devices\n");
  237. while (current) {
  238. device = current;
  239. if (find_endpoints()) {
  240. msg_pdbg("Raiden: Failed to find valid endpoints on device");
  241. usb_device_show(" ", current);
  242. goto loop_end;
  243. }
  244. if (usb_device_claim(device)) {
  245. msg_pdbg("Raiden: Failed to claim USB device");
  246. usb_device_show(" ", current);
  247. goto loop_end;
  248. }
  249. if (!serial) {
  250. found = 1;
  251. goto loop_end;
  252. } else {
  253. unsigned char dev_serial[32];
  254. struct libusb_device_descriptor descriptor;
  255. int rc;
  256. memset(dev_serial, 0, sizeof(dev_serial));
  257. if (libusb_get_device_descriptor(device->device, &descriptor)) {
  258. msg_pdbg("USB: Failed to get device descriptor.\n");
  259. goto loop_end;
  260. }
  261. rc = libusb_get_string_descriptor_ascii(device->handle,
  262. descriptor.iSerialNumber,
  263. dev_serial,
  264. sizeof(dev_serial));
  265. if (rc < 0) {
  266. LIBUSB(rc);
  267. } else {
  268. if (strcmp(serial, (char *)dev_serial)) {
  269. msg_pdbg("Raiden: Serial number %s did not match device", serial);
  270. usb_device_show(" ", current);
  271. } else {
  272. msg_pinfo("Raiden: Serial number %s matched device", serial);
  273. usb_device_show(" ", current);
  274. found = 1;
  275. }
  276. }
  277. }
  278. loop_end:
  279. if (found)
  280. break;
  281. else
  282. current = usb_device_free(current);
  283. }
  284. if (!device || !found) {
  285. msg_perr("Raiden: No usable device found.\n");
  286. return 1;
  287. }
  288. /* free devices we don't care about */
  289. current = current->next;
  290. while (current)
  291. current = usb_device_free(current);
  292. CHECK(LIBUSB(libusb_control_transfer(
  293. device->handle,
  294. LIBUSB_ENDPOINT_OUT |
  295. LIBUSB_REQUEST_TYPE_VENDOR |
  296. LIBUSB_RECIPIENT_INTERFACE,
  297. request_enable,
  298. 0,
  299. device->interface_descriptor->bInterfaceNumber,
  300. NULL,
  301. 0,
  302. TRANSFER_TIMEOUT_MS)),
  303. "Raiden: Failed to enable SPI bridge\n");
  304. register_spi_programmer(&spi_programmer_raiden_debug);
  305. register_shutdown(shutdown, NULL);
  306. return 0;
  307. }