blob: 6c9ee4a3708da74bf9901d3a70ad54cd1b8916e0 [file] [log] [blame]
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +00004 * Copyright (C) 2009, 2010, 2011, 2012 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000014 */
15
16#include <stdio.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000017#include <strings.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000018#include <string.h>
Felix Singercf6b7082022-08-18 23:57:31 +020019#include <stdbool.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000020#include <stdlib.h>
21#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000022#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000023#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000025#include "spi.h"
26
27/* Change this to #define if you want to test without a serial implementation */
28#undef FAKE_COMMUNICATION
29
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060030struct buspirate_speeds {
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000031 const char *name;
32 const int speed;
33};
34
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060035#define BP_DEFAULTBAUD 115200
36
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000037#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000038static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000039{
40 /* 115200bps, 8 databits, no parity, 1 stopbit */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060041 sp_fd = sp_openserport(dev, BP_DEFAULTBAUD);
Elyes HAOUAS124ef382018-03-27 12:15:09 +020042 if (sp_fd == SER_INV_FD)
Niklas Söderlund2a95e872012-07-30 19:42:33 +000043 return 1;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000044 return 0;
45}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000046#else
47#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000048#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000049#define serialport_write(...) 0
50#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000051#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000052#endif
53
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000054static unsigned char *bp_commbuf = NULL;
55static int bp_commbufsize = 0;
56
57static int buspirate_commbuf_grow(int bufsize)
58{
59 unsigned char *tmpbuf;
60
61 /* Never shrink. realloc() calls are expensive. */
62 if (bufsize <= bp_commbufsize)
63 return 0;
64
65 tmpbuf = realloc(bp_commbuf, bufsize);
66 if (!tmpbuf) {
67 /* Keep the existing buffer because memory is already tight. */
68 msg_perr("Out of memory!\n");
69 return ERROR_OOM;
70 }
71
72 bp_commbuf = tmpbuf;
73 bp_commbufsize = bufsize;
74 return 0;
75}
76
Uwe Hermann91f4afa2011-07-28 08:13:25 +000077static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
78 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000079{
Nico Huber519be662018-12-23 20:03:35 +010080 unsigned int i;
81 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000082
Sean Nelson84f7bce2010-01-09 23:56:41 +000083 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000085 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000086 return 1;
87 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +000088 if (writecnt)
89 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000090 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000091 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000092#ifdef FAKE_COMMUNICATION
93 /* Placate the caller for now. */
94 if (readcnt) {
95 buf[0] = 0x01;
96 memset(buf + 1, 0xff, readcnt - 1);
97 }
98 ret = 0;
99#else
100 if (writecnt)
101 ret = serialport_write(buf, writecnt);
102 if (ret)
103 return ret;
104 if (readcnt)
105 ret = serialport_read(buf, readcnt);
106 if (ret)
107 return ret;
108#endif
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000109 if (readcnt)
110 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000111 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000112 msg_pspew(" 0x%02x", buf[i]);
113 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000114 return 0;
115}
116
Jacob Garber4a84ec22019-07-25 19:12:31 -0600117static int buspirate_wait_for_string(unsigned char *buf, const char *key)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000118{
119 unsigned int keylen = strlen(key);
120 int ret;
121
122 ret = buspirate_sendrecv(buf, 0, keylen);
123 while (!ret) {
124 if (!memcmp(buf, key, keylen))
125 return 0;
126 memmove(buf, buf + 1, keylen - 1);
127 ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
128 }
129 return ret;
130}
131
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000132static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000133 const unsigned char *writearr, unsigned char *readarr);
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000134static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000135 const unsigned char *writearr, unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000136
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000137static struct spi_master spi_master_buspirate = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100138 .features = SPI_MASTER_4BA,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000139 .max_data_read = MAX_DATA_UNSPECIFIED,
140 .max_data_write = MAX_DATA_UNSPECIFIED,
141 .command = NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000142 .multicommand = default_spi_send_multicommand,
143 .read = default_spi_read,
144 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000145 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000146};
147
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600148static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000149 {"30k", 0x0},
150 {"125k", 0x1},
151 {"250k", 0x2},
152 {"1M", 0x3},
153 {"2M", 0x4},
154 {"2.6M", 0x5},
155 {"4M", 0x6},
156 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600157 {NULL, 0x0}
158};
159
160static const struct buspirate_speeds serialspeeds[] = {
161 {"115200", 115200},
162 {"230400", 230400},
163 {"250000", 250000},
164 {"2000000", 2000000},
165 {"2M", 2000000},
166 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000167};
168
David Hendricks8bb20212011-06-14 01:35:36 +0000169static int buspirate_spi_shutdown(void *data)
170{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000171 int ret = 0, ret2 = 0;
172 /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */
David Hendricks8bb20212011-06-14 01:35:36 +0000173
174 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000175 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000176 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000177 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000178 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000179 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000180 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
181 goto out_shutdown;
182 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
183 if (bp_commbuf[0] != '1') {
184 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000185 ret = 1;
186 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000187 }
188 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000189 bp_commbuf[0] = 0x0f;
190 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000191
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000192out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000193 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000194 ret2 = serialport_shutdown(NULL);
195 /* Keep the oldest error, it is probably the best indicator. */
196 if (ret2 && !ret)
197 ret = ret2;
198 bp_commbufsize = 0;
199 free(bp_commbuf);
200 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000201 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000202 msg_pdbg("Bus Pirate shutdown failed.\n");
203 else
204 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000205
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000206 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000207}
208
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000209#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600210#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
211
212/**
213 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
214 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
215 */
216#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000217
Thomas Heijligencc853d82021-05-04 15:32:17 +0200218static int buspirate_spi_init(void)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000219{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000220 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000221 char *dev;
222 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600223 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000224 unsigned int fw_version_major = 0;
225 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600226 unsigned int hw_version_major = 0;
227 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000228 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600229 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000230 int ret = 0;
Felix Singercf6b7082022-08-18 23:57:31 +0200231 bool pullup = false;
232 bool psu = false;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000233
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000234 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000235 if (dev && !strlen(dev)) {
236 free(dev);
237 dev = NULL;
238 }
239 if (!dev) {
240 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000241 return 1;
242 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000243
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000244 tmp = extract_programmer_param("spispeed");
245 if (tmp) {
246 for (i = 0; spispeeds[i].name; i++) {
247 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000248 spispeed = spispeeds[i].speed;
249 break;
250 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000251 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000252 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000253 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000254 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000255 free(tmp);
256
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200257 /* Extract serialspeed parameter */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600258 tmp = extract_programmer_param("serialspeed");
259 if (tmp) {
260 for (i = 0; serialspeeds[i].name; i++) {
261 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
262 serialspeed_index = i;
263 break;
264 }
265 }
266 if (!serialspeeds[i].name)
267 msg_perr("Invalid serial speed %s, using default.\n", tmp);
268 }
269 free(tmp);
270
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000271 tmp = extract_programmer_param("pullups");
272 if (tmp) {
273 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200274 pullup = true;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000275 else if (strcasecmp("off", tmp) == 0)
276 ; // ignore
277 else
278 msg_perr("Invalid pullups state, not using them.\n");
279 }
280 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000281
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800282 tmp = extract_programmer_param("psus");
283 if (tmp) {
284 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200285 psu = true;
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800286 else if (strcasecmp("off", tmp) == 0)
287 ; // ignore
288 else
289 msg_perr("Invalid psus state, not enabling.\n");
290 }
291 free(tmp);
292
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000293 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
294#define DEFAULT_BUFSIZE (16 + 3)
295 bp_commbuf = malloc(DEFAULT_BUFSIZE);
296 if (!bp_commbuf) {
297 bp_commbufsize = 0;
298 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000299 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000300 return ERROR_OOM;
301 }
302 bp_commbufsize = DEFAULT_BUFSIZE;
303
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000304 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000305 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000306 if (ret) {
307 bp_commbufsize = 0;
308 free(bp_commbuf);
309 bp_commbuf = NULL;
310 return ret;
311 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000312
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000313 /* This is the brute force version, but it should work.
314 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
315 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
316 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
317 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000318 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000319 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000320 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000321 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000322 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000323 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000324 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000325 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
326 * response which came in over serial. Unfortunately that does not work reliably on Linux
327 * with FTDI USB-serial.
328 */
329 //sp_flush_incoming();
330 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
331 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
332 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000333 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000334 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000335 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
336 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000337 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000338
339 /* Reset the Bus Pirate. */
340 bp_commbuf[0] = 0x0f;
341 /* Send the command, don't read the response. */
342 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000343 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000344 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000345 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000346 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
347 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
348 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000349 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000350 if (strchr("\r\n\t ", bp_commbuf[i]))
351 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000352 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000353 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600354 msg_pdbg("Detected Bus Pirate hardware ");
355 if (bp_commbuf[0] != 'v')
356 msg_pdbg("(unknown version number format)");
357 else if (!strchr("0123456789", bp_commbuf[1]))
358 msg_pdbg("(unknown version number format)");
359 else {
360 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
361 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
362 tmp++;
363 hw_version_minor = strtoul(tmp, NULL, 10);
364 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
365 }
366 msg_pdbg2(" (\"%s\")", bp_commbuf);
367 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000368
369 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000370 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000371 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
372 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
373 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000374 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000375 if (strchr("\r\n\t ", bp_commbuf[i]))
376 break;
377 }
378 bp_commbuf[i] = '\0';
379 msg_pdbg("Detected Bus Pirate firmware ");
380 if (bp_commbuf[0] != 'v')
381 msg_pdbg("(unknown version number format)");
382 else if (!strchr("0123456789", bp_commbuf[1]))
383 msg_pdbg("(unknown version number format)");
384 else {
385 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
386 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
387 tmp++;
388 fw_version_minor = strtoul(tmp, NULL, 10);
389 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
390 }
391 msg_pdbg2(" (\"%s\")", bp_commbuf);
392 msg_pdbg("\n");
393
394 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000395 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600396
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000397 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
398 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
399 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
400 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000401 ret = SPI_PROGRAMMER_ERROR;
402 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000403 }
404
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000405 /* Use fast SPI mode in firmware 5.5 and newer. */
406 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600407 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000408 /* Sensible default buffer size. */
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000409 if (buspirate_commbuf_grow(260 + 5)) {
410 ret = ERROR_OOM;
411 goto init_err_cleanup_exit;
412 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000413 spi_master_buspirate.max_data_read = 2048;
414 spi_master_buspirate.max_data_write = 256;
415 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000416 } else {
417 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
418 msg_pinfo("Reading/writing a flash chip may take hours.\n");
419 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
420 /* Sensible default buffer size. */
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000421 if (buspirate_commbuf_grow(16 + 3)) {
422 ret = ERROR_OOM;
423 goto init_err_cleanup_exit;
424 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000425 spi_master_buspirate.max_data_read = 12;
426 spi_master_buspirate.max_data_write = 12;
427 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000428 }
429
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000430 /* Workaround for broken speed settings in firmware 6.1 and older. */
431 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
432 if (spispeed > 0x4) {
433 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
434 "Limiting speed to 2 MHz.\n");
435 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
436 spispeed = 0x4;
437 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600438
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000439 /* This works because speeds numbering starts at 0 and is contiguous. */
440 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
441
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600442 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
443 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
444 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
445 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
446 }
447
448 /* Set custom serial speed if specified */
449 if (serialspeed_index != -1) {
450 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
451 /* This feature requires firmware 5.5 or newer */
452 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
453 "Using default speed of 115200 baud.\n");
454 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
455 /* Set the serial speed to match the user's choice if it doesn't already */
456
457 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
458 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
459 " Continue at your own risk.\n");
460
461 /* Enter baud rate configuration mode */
462 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
463 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000464 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600465 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000466 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600467
468 /* Enter manual clock divisor entry mode */
469 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
470 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000471 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600472 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000473 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600474
475 /* Set the clock divisor to the value calculated from the user's input */
476 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
477 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
478
479 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000480 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600481 sleep(1);
482
483 /* Reconfigure the host's serial baud rate to the new value */
484 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
485 msg_perr("Unable to configure system baud rate to specified value.");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000486 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600487 }
488
489 /* Return to the main prompt */
490 bp_commbuf[0] = ' ';
491 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000492 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600493 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000494 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600495
496 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
497 }
498
499 }
500
501
502
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000503 /* Enter raw bitbang mode */
504 for (i = 0; i < 20; i++) {
505 bp_commbuf[0] = 0x00;
506 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000507 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000508 }
509 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000510 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000511 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000512 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000513 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
514 if (bp_commbuf[0] != '1') {
515 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000516 ret = 1;
517 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000518 }
519 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000520 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000521 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000522 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000523 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000524 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000525 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000526 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000527 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000528 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
529 if (bp_commbuf[0] != '1') {
530 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000531 ret = 1;
532 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000533 }
534
535 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000536 bp_commbuf[0] = 0x40 | 0x0b;
Felix Singercf6b7082022-08-18 23:57:31 +0200537 if (pullup) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000538 bp_commbuf[0] |= (1 << 2);
539 msg_pdbg("Enabling pull-up resistors.\n");
540 }
Felix Singercf6b7082022-08-18 23:57:31 +0200541 if (psu) {
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800542 bp_commbuf[0] |= (1 << 3);
543 msg_pdbg("Enabling PSUs.\n");
544 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000545 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000546 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000547 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000548 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000549 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000550 ret = 1;
551 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000552 }
553
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000554 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000555 bp_commbuf[0] = 0x60 | spispeed;
556 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000557 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000558 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000559 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000560 msg_perr("Protocol error while setting SPI speed!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000561 ret = 1;
562 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000563 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600564
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000565 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000566 bp_commbuf[0] = 0x80 | 0xa;
Felix Singercf6b7082022-08-18 23:57:31 +0200567 if (pullup) {
Maxime Vincent2099c642017-11-24 13:04:08 +0100568 bp_commbuf[0] &= ~(1 << 3);
569 msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n");
570 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000571 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000572 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000573 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000574 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000575 msg_perr("Protocol error while setting SPI config!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000576 ret = 1;
577 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000578 }
579
580 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000581 bp_commbuf[0] = 0x03;
582 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000583 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000584 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000585 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000586 msg_perr("Protocol error while raising CS#!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000587 ret = 1;
588 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000589 }
590
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000591 if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
592 ret = 1;
593 goto init_err_cleanup_exit;
594 }
Nico Huber5e08e3e2021-05-11 17:38:14 +0200595 register_spi_master(&spi_master_buspirate, NULL);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000596
597 return 0;
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000598
599init_err_cleanup_exit:
600 buspirate_spi_shutdown(NULL);
601 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000602}
603
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000604static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000605 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000606{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000607 unsigned int i = 0;
608 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000609
610 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
611 return SPI_INVALID_LENGTH;
612
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000613 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000614 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
615 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000616
617 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000618 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000619
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000620 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
621 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000622 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000623 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000624
625 i += readcnt;
626 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000627 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000628
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000629 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000630
631 if (ret) {
632 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000633 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000634 }
635
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000636 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000637 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000638 return SPI_GENERIC_ERROR;
639 }
640
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000641 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000642 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000643 return SPI_GENERIC_ERROR;
644 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000645
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000646 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000647 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000648 return SPI_GENERIC_ERROR;
649 }
650
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000651 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000652 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000653
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000654 return ret;
655}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000656
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000657static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000658 const unsigned char *writearr, unsigned char *readarr)
659{
660 int i = 0, ret = 0;
661
662 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
663 return SPI_INVALID_LENGTH;
664
665 /* 5 bytes extra for command, writelen, readlen.
666 * 1 byte extra for Ack/Nack.
667 */
668 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
669 return ERROR_OOM;
670
671 /* Combined SPI write/read. */
672 bp_commbuf[i++] = 0x04;
673 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
674 bp_commbuf[i++] = writecnt & 0xff;
675 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
676 bp_commbuf[i++] = readcnt & 0xff;
677 memcpy(bp_commbuf + i, writearr, writecnt);
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600678
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000679 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
680
681 if (ret) {
682 msg_perr("Bus Pirate communication error!\n");
683 return SPI_GENERIC_ERROR;
684 }
685
686 if (bp_commbuf[0] != 0x01) {
687 msg_perr("Protocol error while sending SPI write/read!\n");
688 return SPI_GENERIC_ERROR;
689 }
690
691 /* Skip Ack. */
692 memcpy(readarr, bp_commbuf + 1, readcnt);
693
694 return ret;
Nico Huberdeeac7e2017-04-22 00:09:42 +0200695}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200696
697const struct programmer_entry programmer_buspirate_spi = {
698 .name = "buspirate_spi",
699 .type = OTHER,
700 /* FIXME */
701 .devs.note = "Dangerous Prototypes Bus Pirate\n",
702 .init = buspirate_spi_init,
703 .map_flash_region = fallback_map,
704 .unmap_flash_region = fallback_unmap,
705 .delay = internal_delay,
706};