blob: c3d48a4d9f02913ffef7fec7b6aa1098e2f8254a [file] [log] [blame]
Urja Rannikko22915352009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
Urja Rannikkoc93f5f12011-09-15 23:38:14 +00004 * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com>
Urja Rannikko22915352009-06-23 11:33:43 +00005 * Copyright (C) 2009 Carl-Daniel Hailfinger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Urja Rannikko22915352009-06-23 11:33:43 +000016 */
17
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000018#include "platform.h"
19
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000020#include <stdio.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000021#if ! IS_WINDOWS /* stuff (presumably) needed for sockets only */
Urja Rannikko22915352009-06-23 11:33:43 +000022#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000023#include <unistd.h>
Urja Rannikko22915352009-06-23 11:33:43 +000024#include <fcntl.h>
Urja Rannikko22915352009-06-23 11:33:43 +000025#include <sys/socket.h>
26#include <arpa/inet.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#include <netdb.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000030#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000031#if IS_WINDOWS
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000032#include <conio.h>
33#else
Urja Rannikko22915352009-06-23 11:33:43 +000034#include <termios.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000035#endif
36#include <string.h>
37#include <errno.h>
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000038#include "flash.h"
39#include "programmer.h"
Urja Rannikkoc93f5f12011-09-15 23:38:14 +000040#include "chipdrivers.h"
Stefan Tauner0d82e952012-10-27 00:34:18 +000041#include "serprog.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000042
Stefan Tauner31019d42011-10-22 21:45:27 +000043#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000044
David Hendricks8bb20212011-06-14 01:35:36 +000045/*
46 * FIXME: This prototype was added to help reduce diffs for the shutdown
47 * registration patch, which shifted many lines of code to place
48 * serprog_shutdown() before serprog_init(). It should be removed soon.
49 */
50static int serprog_shutdown(void *data);
51
Urja Rannikkof3196df2009-07-21 13:02:59 +000052static uint16_t sp_device_serbuf_size = 16;
53static uint16_t sp_device_opbuf_size = 300;
54/* Bitmap of supported commands */
55static uint8_t sp_cmdmap[32];
56
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000057/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000058 and combine them to write-n's */
59static int sp_prev_was_write = 0;
60/* sp_write_n_addr used as the starting addr of the currently
61 combined write-n operation */
62static uint32_t sp_write_n_addr;
63/* The maximum length of an write_n operation; 0 = write-n not supported */
64static uint32_t sp_max_write_n = 0;
65/* The maximum length of a read_n operation; 0 = 2^24 */
66static uint32_t sp_max_read_n = 0;
67
68/* A malloc'd buffer for combining the operation's data
69 and a counter that tells how much data is there. */
70static uint8_t *sp_write_n_buf;
71static uint32_t sp_write_n_bytes = 0;
72
73/* sp_streamed_* used for flow control checking */
74static int sp_streamed_transmit_ops = 0;
75static int sp_streamed_transmit_bytes = 0;
76
77/* sp_opbuf_usage used for counting the amount of
78 on-device operation buffer used */
79static int sp_opbuf_usage = 0;
80/* if true causes sp_docommand to automatically check
81 whether the command is supported before doing it */
82static int sp_check_avail_automatic = 0;
83
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000084#if ! IS_WINDOWS
Urja Rannikkof3196df2009-07-21 13:02:59 +000085static int sp_opensocket(char *ip, unsigned int port)
86{
87 int flag = 1;
88 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +000089 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +000090 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +000091 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +000092 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000093 if (sock < 0) {
94 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
95 return -1;
96 }
Urja Rannikkof3196df2009-07-21 13:02:59 +000097 hostPtr = gethostbyname(ip);
98 if (NULL == hostPtr) {
99 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000100 if (NULL == hostPtr) {
Stefan Reinauerb8792872014-04-26 16:12:03 +0000101 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000102 msg_perr("Error: cannot resolve %s\n", ip);
103 return -1;
104 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000105 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000106 sp.si.sin_family = AF_INET;
107 sp.si.sin_port = htons(port);
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000108 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000109 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000110 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000111 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
112 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000113 }
114 /* We are latency limited, and sometimes do write-write-read *
115 * (write-n) - so enable TCP_NODELAY. */
Stefan Reinauer907c3eb2014-04-26 16:12:31 +0000116 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int))) {
117 close(sock);
118 msg_perr("Error: serprog cannot set socket options: %s\n", strerror(errno));
119 return -1;
120 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000121 return sock;
122}
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000123#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000124
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000125/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000126 * always succeeded in) bring the serial protocol to known waiting-for- *
Stefan Taunerae3d8372013-04-01 00:45:45 +0000127 * command state - uses nonblocking I/O - rest of the driver uses *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000128 * blocking read - TODO: add an alarm() timer for the rest of the app on *
129 * serial operations, though not such a big issue as the first thing to *
130 * do is synchronize (eg. check that device is alive). */
Niklas Söderlund7145a502012-09-07 07:07:07 +0000131static int sp_synchronize(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000132{
133 int i;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000134 unsigned char buf[8];
Urja Rannikkof3196df2009-07-21 13:02:59 +0000135 /* First sends 8 NOPs, then flushes the return data - should cause *
136 * the device serial parser to get to a sane state, unless if it *
137 * is waiting for a real long write-n. */
138 memset(buf, S_CMD_NOP, 8);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000139 if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000140 goto err_out;
141 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000142 /* A second should be enough to get all the answers to the buffer */
Stefan Tauner79587f52013-04-01 00:45:51 +0000143 internal_delay(1000 * 1000);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000144 sp_flush_incoming();
145
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000146 /* Then try up to 8 times to send syncnop and get the correct special *
147 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
148 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
149 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000150 for (i = 0; i < 8; i++) {
151 int n;
152 unsigned char c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000153 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000154 goto err_out;
155 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000156 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000157 fflush(stdout);
158 for (n = 0; n < 10; n++) {
Stefan Tauner00e16082013-04-01 00:45:38 +0000159 int ret = serialport_read_nonblock(&c, 1, 50, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000160 if (ret < 0)
161 goto err_out;
162 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000163 continue;
Stefan Tauner00e16082013-04-01 00:45:38 +0000164 ret = serialport_read_nonblock(&c, 1, 20, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000165 if (ret < 0)
166 goto err_out;
167 if (ret > 0 || c != S_ACK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000168 continue;
169 c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000170 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
171 goto err_out;
Niklas Söderlund7145a502012-09-07 07:07:07 +0000172 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000173 ret = serialport_read_nonblock(&c, 1, 500, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000174 if (ret < 0)
175 goto err_out;
176 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000177 break; /* fail */
Stefan Tauner00e16082013-04-01 00:45:38 +0000178 ret = serialport_read_nonblock(&c, 1, 100, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000179 if (ret > 0 || ret < 0)
180 goto err_out;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000181 if (c != S_ACK)
182 break; /* fail */
Sean Nelson74e8af52010-01-10 01:06:23 +0000183 msg_pdbg("\n");
Niklas Söderlund7145a502012-09-07 07:07:07 +0000184 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000185 }
186 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000187err_out:
188 msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
189 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000190}
191
192static int sp_check_commandavail(uint8_t command)
193{
194 int byteoffs, bitoffs;
195 byteoffs = command / 8;
196 bitoffs = command % 8;
197 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
198}
199
200static int sp_automatic_cmdcheck(uint8_t cmd)
201{
202 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000203 msg_pdbg("Warning: Automatic command availability check failed "
Stefan Taunere34e3e82013-01-01 00:06:51 +0000204 "for cmd 0x%02x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000205 return 1;
206 }
207 return 0;
208}
209
210static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000211 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000212{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000213 unsigned char c;
214 if (sp_automatic_cmdcheck(command))
215 return 1;
Stefan Tauner79587f52013-04-01 00:45:51 +0000216 if (serialport_write(&command, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000217 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
218 return 1;
219 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000220 if (serialport_write(params, parmlen) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000221 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
222 return 1;
223 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000224 if (serialport_read(&c, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000225 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
226 return 1;
227 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000228 if (c == S_NAK)
229 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000230 if (c != S_ACK) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000231 msg_perr("Error: invalid response 0x%02X from device (to command 0x%02X)\n", c, command);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000232 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000233 }
234 if (retlen) {
Stefan Tauner79587f52013-04-01 00:45:51 +0000235 if (serialport_read(retparms, retlen) != 0) {
236 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
237 return 1;
238 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000239 }
240 return 0;
241}
242
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000243static int sp_flush_stream(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000244{
245 if (sp_streamed_transmit_ops)
246 do {
247 unsigned char c;
Stefan Tauner79587f52013-04-01 00:45:51 +0000248 if (serialport_read(&c, 1) != 0) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000249 msg_perr("Error: cannot read from device (flushing stream)");
250 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000251 }
252 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000253 msg_perr("Error: NAK to a stream buffer operation\n");
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000254 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000255 }
256 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000257 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000258 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000259 }
260 } while (--sp_streamed_transmit_ops);
261 sp_streamed_transmit_ops = 0;
262 sp_streamed_transmit_bytes = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000263 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000264}
265
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000266static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000267{
268 uint8_t *sp;
269 if (sp_automatic_cmdcheck(cmd))
270 return 1;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000271
Urja Rannikkof3196df2009-07-21 13:02:59 +0000272 sp = malloc(1 + parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000273 if (!sp) {
274 msg_perr("Error: cannot malloc command buffer\n");
275 return 1;
276 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000277 sp[0] = cmd;
278 memcpy(&(sp[1]), parms, parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000279
280 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) {
281 if (sp_flush_stream() != 0) {
282 free(sp);
283 return 1;
284 }
285 }
286 if (serialport_write(sp, 1 + parmlen) != 0) {
287 msg_perr("Error: cannot write command\n");
288 free(sp);
289 return 1;
290 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000291 sp_streamed_transmit_ops += 1;
292 sp_streamed_transmit_bytes += 1 + parmlen;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000293
294 free(sp);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000295 return 0;
296}
297
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000298static int serprog_spi_send_command(struct flashctx *flash,
299 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000300 const unsigned char *writearr,
301 unsigned char *readarr);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000302static struct spi_master spi_master_serprog = {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000303 .type = SPI_CONTROLLER_SERPROG,
Nico Huber1cf407b2017-11-10 20:18:23 +0100304 .features = SPI_MASTER_4BA,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000305 .max_data_read = MAX_DATA_READ_UNLIMITED,
306 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
307 .command = serprog_spi_send_command,
308 .multicommand = default_spi_send_multicommand,
Urja Rannikko731316a2017-06-15 13:32:01 +0300309 .read = default_spi_read,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000310 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000311 .write_aai = default_spi_write_aai,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000312};
313
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000314static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
315 chipaddr addr);
316static uint8_t serprog_chip_readb(const struct flashctx *flash,
317 const chipaddr addr);
318static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
319 const chipaddr addr, size_t len);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000320static const struct par_master par_master_serprog = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000321 .chip_readb = serprog_chip_readb,
322 .chip_readw = fallback_chip_readw,
323 .chip_readl = fallback_chip_readl,
324 .chip_readn = serprog_chip_readn,
325 .chip_writeb = serprog_chip_writeb,
326 .chip_writew = fallback_chip_writew,
327 .chip_writel = fallback_chip_writel,
328 .chip_writen = fallback_chip_writen,
329};
330
331static enum chipbustype serprog_buses_supported = BUS_NONE;
332
Urja Rannikkof3196df2009-07-21 13:02:59 +0000333int serprog_init(void)
334{
335 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000336 unsigned char pgmname[17];
337 unsigned char rbuf[3];
338 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000339 char *device;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000340 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000341
Stefan Tauner72587f82016-01-04 03:05:15 +0000342 /* the parameter is either of format "dev=/dev/device[:baud]" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000343 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000344 if (device && strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000345 char *baud_str = strstr(device, ":");
346 if (baud_str != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000347 /* Split device from baudrate. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000348 *baud_str = '\0';
349 baud_str++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000350 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000351 int baud;
352 /* Convert baud string to value.
353 * baud_str is either NULL (if strstr can't find the colon), points to the \0 after the colon
354 * if no characters where given after the colon, or a string to convert... */
355 if (baud_str == NULL || *baud_str == '\0') {
356 baud = -1;
357 msg_pdbg("No baudrate specified, using the hardware's defaults.\n");
358 } else
359 baud = atoi(baud_str); // FIXME: replace atoi with strtoul
360 if (strlen(device) > 0) {
361 sp_fd = sp_openserport(device, baud);
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000362 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000363 free(device);
364 return 1;
365 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000366 have_device++;
367 }
368 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000369
370#if !IS_WINDOWS
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000371 if (device && !strlen(device)) {
372 msg_perr("Error: No device specified.\n"
Stefan Tauner72587f82016-01-04 03:05:15 +0000373 "Use flashrom -p serprog:dev=/dev/device[:baud]\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000374 free(device);
375 return 1;
376 }
377 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000378
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000379 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000380 if (have_device && device) {
381 msg_perr("Error: Both host and device specified.\n"
382 "Please use either dev= or ip= but not both.\n");
383 free(device);
384 return 1;
385 }
386 if (device && strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000387 char *port = strstr(device, ":");
388 if (port != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000389 /* Split host from port. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000390 *port = '\0';
391 port++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000392 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000393 if (!port || !strlen(port)) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000394 msg_perr("Error: No port specified.\n"
395 "Use flashrom -p serprog:ip=ipaddr:port\n");
396 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000397 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000398 }
399 if (strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000400 sp_fd = sp_opensocket(device, atoi(port)); // FIXME: replace atoi with strtoul
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000401 if (sp_fd < 0) {
402 free(device);
403 return 1;
404 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000405 have_device++;
406 }
407 }
408 if (device && !strlen(device)) {
409 msg_perr("Error: No host specified.\n"
410 "Use flashrom -p serprog:ip=ipaddr:port\n");
411 free(device);
412 return 1;
413 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000414#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000415 free(device);
416
417 if (!have_device) {
Urja Rannikko27b431b2016-01-04 03:05:23 +0000418#if IS_WINDOWS
419 msg_perr("Error: No device specified.\n"
420 "Use flashrom -p serprog:dev=comN[:baud]\n");
421#else
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000422 msg_perr("Error: Neither host nor device specified.\n"
423 "Use flashrom -p serprog:dev=/dev/device:baud or "
424 "flashrom -p serprog:ip=ipaddr:port\n");
Urja Rannikko27b431b2016-01-04 03:05:23 +0000425#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000426 return 1;
427 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000428
David Hendricks8bb20212011-06-14 01:35:36 +0000429 if (register_shutdown(serprog_shutdown, NULL))
430 return 1;
431
Sean Nelson74e8af52010-01-10 01:06:23 +0000432 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000433
434 sp_check_avail_automatic = 0;
435
Niklas Söderlund7145a502012-09-07 07:07:07 +0000436 if (sp_synchronize())
437 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000438
Sean Nelson74e8af52010-01-10 01:06:23 +0000439 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000440
441 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000442 msg_perr("Error: NAK to query interface version\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000443 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000444 }
445
446 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000447 msg_perr("Error: Unknown interface version: %d\n", iface);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000448 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000449 }
450
Sean Nelson74e8af52010-01-10 01:06:23 +0000451 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000452
453 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000454 msg_perr("Error: query command map not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000455 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000456 }
457
458 sp_check_avail_automatic = 1;
459
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000460 /* FIXME: This assumes that serprog device bustypes are always
461 * identical with flashrom bustype enums and that they all fit
462 * in a single byte.
463 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000464 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000465 msg_pwarn("Warning: NAK to query supported buses\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000466 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000467 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000468 serprog_buses_supported = c;
469
Stefan Tauner31019d42011-10-22 21:45:27 +0000470 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
471 (c & BUS_PARALLEL) ? "on" : "off",
472 (c & BUS_LPC) ? "on" : "off",
473 (c & BUS_FWH) ? "on" : "off",
474 (c & BUS_SPI) ? "on" : "off");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000475 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000476 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000477 uint8_t bt = BUS_SPI;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000478 char *spispeed;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000479 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
480 msg_perr("Error: SPI operation not supported while the "
481 "bustype is SPI\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000482 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000483 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000484 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
485 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000486 /* Success of any of these commands is optional. We don't need
487 the programmer to tell us its limits, but if it doesn't, we
488 will assume stuff, so it's in the programmers best interest
489 to tell us. */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000490 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
491 uint32_t v;
492 v = ((unsigned int)(rbuf[0]) << 0);
493 v |= ((unsigned int)(rbuf[1]) << 8);
494 v |= ((unsigned int)(rbuf[2]) << 16);
495 if (v == 0)
496 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000497 spi_master_serprog.max_data_write = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000498 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
499 }
500 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
501 uint32_t v;
502 v = ((unsigned int)(rbuf[0]) << 0);
503 v |= ((unsigned int)(rbuf[1]) << 8);
504 v |= ((unsigned int)(rbuf[2]) << 16);
505 if (v == 0)
506 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000507 spi_master_serprog.max_data_read = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000508 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
509 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000510 spispeed = extract_programmer_param("spispeed");
511 if (spispeed && strlen(spispeed)) {
512 uint32_t f_spi_req, f_spi;
513 uint8_t buf[4];
514 char *f_spi_suffix;
515
516 errno = 0;
517 f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
518 if (errno != 0 || spispeed == f_spi_suffix) {
519 msg_perr("Error: Could not convert 'spispeed'.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000520 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000521 return 1;
522 }
523 if (strlen(f_spi_suffix) == 1) {
524 if (!strcasecmp(f_spi_suffix, "M"))
525 f_spi_req *= 1000000;
526 else if (!strcasecmp(f_spi_suffix, "k"))
527 f_spi_req *= 1000;
528 else {
529 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000530 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000531 return 1;
532 }
533 } else if (strlen(f_spi_suffix) > 1) {
534 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000535 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000536 return 1;
537 }
538
539 buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
540 buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
541 buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
542 buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
543
544 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000545 msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
546 else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000547 f_spi = buf[0];
548 f_spi |= buf[1] << (1 * 8);
549 f_spi |= buf[2] << (2 * 8);
550 f_spi |= buf[3] << (3 * 8);
551 msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
552 "It was actually set to %u Hz\n", f_spi_req, f_spi);
553 } else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000554 msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000555 }
556 free(spispeed);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000557 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000558 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
559 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000560 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000561
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000562 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000563 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
564 msg_perr("Error: Initialize operation buffer "
565 "not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000566 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000567 }
568
569 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
570 msg_perr("Error: Write to opbuf: "
571 "delay not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000572 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000573 }
574
575 /* S_CMD_O_EXEC availability checked later. */
576
577 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
578 msg_perr("Error: Single byte read not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000579 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000580 }
581 /* This could be translated to single byte reads (if missing),
582 * but now we don't support that. */
583 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
584 msg_perr("Error: Read n bytes not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000585 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000586 }
587 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
588 msg_perr("Error: Write to opbuf: "
589 "write byte not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000590 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000591 }
592
593 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
594 msg_pdbg(MSGHEADER "Write-n not supported");
595 sp_max_write_n = 0;
596 } else {
597 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
598 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
599 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
600 if (!sp_max_write_n) {
601 sp_max_write_n = (1 << 24);
602 }
603 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
604 sp_max_write_n);
605 sp_write_n_buf = malloc(sp_max_write_n);
606 if (!sp_write_n_buf) {
607 msg_perr("Error: cannot allocate memory for "
608 "Write-n buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000609 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000610 }
611 sp_write_n_bytes = 0;
612 }
613
614 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
615 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
616 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
617 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
618 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
619 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
620 sp_max_read_n ? sp_max_read_n : (1 << 24));
621 } else {
622 msg_pdbg(MSGHEADER "Maximum read-n length "
623 "not reported\n");
624 sp_max_read_n = 0;
625 }
626
Urja Rannikkof3196df2009-07-21 13:02:59 +0000627 }
628
629 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000630 msg_pwarn("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000631 strcpy((char *)pgmname, "(unknown)");
632 }
633 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000634 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000635
636 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000637 msg_pwarn("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000638 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000639 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000640 sp_device_serbuf_size);
641
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000642 if (sp_check_commandavail(S_CMD_O_INIT)) {
643 /* This would be inconsistent. */
644 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
645 msg_perr("Error: Execute operation buffer not "
646 "supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000647 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000648 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000649
650 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
651 msg_perr("Error: NAK to initialize operation buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000652 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000653 }
654
655 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
656 &sp_device_opbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000657 msg_pwarn("Warning: NAK to query operation buffer size\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000658 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000659 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000660 sp_device_opbuf_size);
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200661 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000662
Stefan Tauner92fefc92012-10-27 00:34:23 +0000663 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
664 uint8_t en = 1;
665 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
666 msg_perr("Error: could not enable output buffers\n");
667 return 1;
668 } else
669 msg_pdbg(MSGHEADER "Output drivers enabled\n");
670 } else
671 msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000672 sp_prev_was_write = 0;
673 sp_streamed_transmit_ops = 0;
674 sp_streamed_transmit_bytes = 0;
675 sp_opbuf_usage = 0;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000676 if (serprog_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000677 register_spi_master(&spi_master_serprog);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000678 if (serprog_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000679 register_par_master(&par_master_serprog, serprog_buses_supported & BUS_NONSPI);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000680 return 0;
681}
682
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000683/* Move an in flashrom buffer existing write-n operation to the on-device operation buffer. */
684static int sp_pass_writen(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000685{
686 unsigned char header[7];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000687 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
688 if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
689 if (sp_flush_stream() != 0) {
690 return 1;
691 }
692 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000693 /* In case it's just a single byte send it as a single write. */
694 if (sp_write_n_bytes == 1) {
695 sp_write_n_bytes = 0;
696 header[0] = (sp_write_n_addr >> 0) & 0xFF;
697 header[1] = (sp_write_n_addr >> 8) & 0xFF;
698 header[2] = (sp_write_n_addr >> 16) & 0xFF;
699 header[3] = sp_write_n_buf[0];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000700 if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
701 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000702 sp_opbuf_usage += 5;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000703 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000704 }
705 header[0] = S_CMD_O_WRITEN;
706 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
707 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
708 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
709 header[4] = (sp_write_n_addr >> 0) & 0xFF;
710 header[5] = (sp_write_n_addr >> 8) & 0xFF;
711 header[6] = (sp_write_n_addr >> 16) & 0xFF;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000712 if (serialport_write(header, 7) != 0) {
713 msg_perr(MSGHEADER "Error: cannot write write-n command\n");
714 return 1;
715 }
716 if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
717 msg_perr(MSGHEADER "Error: cannot write write-n data");
718 return 1;
719 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000720 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
721 sp_streamed_transmit_ops += 1;
722 sp_opbuf_usage += 7 + sp_write_n_bytes;
723 sp_write_n_bytes = 0;
724 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000725 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000726}
727
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000728static int sp_execute_opbuf_noflush(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000729{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000730 if ((sp_max_write_n) && (sp_write_n_bytes)) {
731 if (sp_pass_writen() != 0) {
732 msg_perr("Error: could not transfer write buffer\n");
733 return 1;
734 }
735 }
736 if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
737 msg_perr("Error: could not execute command buffer\n");
738 return 1;
739 }
740 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000741 sp_opbuf_usage = 0;
742 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000743 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000744}
745
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000746static int sp_execute_opbuf(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000747{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000748 if (sp_execute_opbuf_noflush() != 0)
749 return 1;
750 if (sp_flush_stream() != 0)
751 return 1;
752
753 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000754}
755
David Hendricks8bb20212011-06-14 01:35:36 +0000756static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000757{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000758 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000759 if (sp_execute_opbuf() != 0)
760 msg_pwarn("Could not flush command buffer.\n");
Stefan Tauner92fefc92012-10-27 00:34:23 +0000761 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
762 uint8_t dis = 0;
763 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
764 msg_pdbg(MSGHEADER "Output drivers disabled\n");
765 else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000766 msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
Stefan Tauner92fefc92012-10-27 00:34:23 +0000767 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000768 /* FIXME: fix sockets on windows(?), especially closing */
769 serialport_shutdown(&sp_fd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000770 if (sp_max_write_n)
771 free(sp_write_n_buf);
772 return 0;
773}
774
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000775static int sp_check_opbuf_usage(int bytes_to_be_added)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000776{
777 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000778 /* If this happens in the middle of a page load the page load will probably fail. */
779 msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
780 if (sp_execute_opbuf() != 0)
781 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000782 }
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000783 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000784}
785
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000786static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
787 chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000788{
Sean Nelson74e8af52010-01-10 01:06:23 +0000789 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000790 if (sp_max_write_n) {
791 if ((sp_prev_was_write)
792 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
793 sp_write_n_buf[sp_write_n_bytes++] = val;
794 } else {
795 if ((sp_prev_was_write) && (sp_write_n_bytes))
796 sp_pass_writen();
797 sp_prev_was_write = 1;
798 sp_write_n_addr = addr;
799 sp_write_n_bytes = 1;
800 sp_write_n_buf[0] = val;
801 }
802 sp_check_opbuf_usage(7 + sp_write_n_bytes);
803 if (sp_write_n_bytes >= sp_max_write_n)
804 sp_pass_writen();
805 } else {
806 /* We will have to do single writeb ops. */
807 unsigned char writeb_parm[4];
808 sp_check_opbuf_usage(6);
809 writeb_parm[0] = (addr >> 0) & 0xFF;
810 writeb_parm[1] = (addr >> 8) & 0xFF;
811 writeb_parm[2] = (addr >> 16) & 0xFF;
812 writeb_parm[3] = val;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000813 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000814 sp_opbuf_usage += 5;
815 }
816}
817
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000818static uint8_t serprog_chip_readb(const struct flashctx *flash,
819 const chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000820{
821 unsigned char c;
822 unsigned char buf[3];
823 /* Will stream the read operation - eg. add it to the stream buffer, *
824 * then flush the buffer, then read the read answer. */
825 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
826 sp_execute_opbuf_noflush();
827 buf[0] = ((addr >> 0) & 0xFF);
828 buf[1] = ((addr >> 8) & 0xFF);
829 buf[2] = ((addr >> 16) & 0xFF);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000830 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
831 sp_flush_stream(); // FIXME: return error
Stefan Tauner79587f52013-04-01 00:45:51 +0000832 if (serialport_read(&c, 1) != 0)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000833 msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
Stefan Taunerc2333752013-07-13 23:31:37 +0000834 msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000835 return c;
836}
837
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000838/* Local version that really does the job, doesn't care of max_read_n. */
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000839static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000840{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000841 unsigned char sbuf[6];
Stefan Tauner0554ca52013-07-25 22:54:25 +0000842 msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000843 /* Stream the read-n -- as above. */
844 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
845 sp_execute_opbuf_noflush();
846 sbuf[0] = ((addr >> 0) & 0xFF);
847 sbuf[1] = ((addr >> 8) & 0xFF);
848 sbuf[2] = ((addr >> 16) & 0xFF);
849 sbuf[3] = ((len >> 0) & 0xFF);
850 sbuf[4] = ((len >> 8) & 0xFF);
851 sbuf[5] = ((len >> 16) & 0xFF);
852 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000853 if (sp_flush_stream() != 0)
854 return 1;
855 if (serialport_read(buf, len) != 0) {
856 msg_perr(MSGHEADER "Error: cannot read read-n data");
857 return 1;
858 }
859 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000860}
861
862/* The externally called version that makes sure that max_read_n is obeyed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000863static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
864 const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000865{
866 size_t lenm = len;
867 chipaddr addrm = addr;
Stefan Tauner31019d42011-10-22 21:45:27 +0000868 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000869 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000870 addrm += sp_max_read_n;
871 lenm -= sp_max_read_n;
872 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000873 if (lenm)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000874 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000875}
876
Stefan Taunerf80419c2014-05-02 15:41:42 +0000877void serprog_delay(unsigned int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000878{
879 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000880 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000881 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000882 msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000883 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000884 return;
885 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000886 if ((sp_max_write_n) && (sp_write_n_bytes))
887 sp_pass_writen();
888 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000889 buf[0] = ((usecs >> 0) & 0xFF);
890 buf[1] = ((usecs >> 8) & 0xFF);
891 buf[2] = ((usecs >> 16) & 0xFF);
892 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000893 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
894 sp_opbuf_usage += 5;
895 sp_prev_was_write = 0;
896}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000897
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000898static int serprog_spi_send_command(struct flashctx *flash,
899 unsigned int writecnt, unsigned int readcnt,
900 const unsigned char *writearr,
901 unsigned char *readarr)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000902{
903 unsigned char *parmbuf;
904 int ret;
905 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000906 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
907 if (sp_execute_opbuf() != 0) {
908 msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
909 return 1;
910 }
911 }
912
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000913 parmbuf = malloc(writecnt + 6);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000914 if (!parmbuf) {
915 msg_perr("Error: could not allocate SPI send param buffer.\n");
916 return 1;
917 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000918 parmbuf[0] = (writecnt >> 0) & 0xFF;
919 parmbuf[1] = (writecnt >> 8) & 0xFF;
920 parmbuf[2] = (writecnt >> 16) & 0xFF;
921 parmbuf[3] = (readcnt >> 0) & 0xFF;
922 parmbuf[4] = (readcnt >> 8) & 0xFF;
923 parmbuf[5] = (readcnt >> 16) & 0xFF;
924 memcpy(parmbuf + 6, writearr, writecnt);
925 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
926 readarr);
927 free(parmbuf);
928 return ret;
929}
930
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000931void *serprog_map(const char *descr, uintptr_t phys_addr, size_t len)
932{
933 /* Serprog transmits 24 bits only and assumes the underlying implementation handles any remaining bits
934 * correctly (usually setting them to one either in software (for FWH/LPC) or relying on the fact that
935 * the hardware observes a subset of the address bits only). Combined with the standard mapping of
936 * flashrom this creates a 16 MB-wide window just below the 4 GB boundary where serprog can operate (as
937 * needed for non-SPI chips). Below we make sure that the requested range is within this window. */
938 if ((phys_addr & 0xFF000000) == 0xFF000000) {
939 return (void*)phys_addr;
940 } else {
941 msg_pwarn(MSGHEADER "requested mapping %s is incompatible: 0x%zx bytes at 0x%0*" PRIxPTR ".\n",
942 descr, len, PRIxPTR_WIDTH, phys_addr);
943 return NULL;
944 }
945}