blob: 69d0c080b1ee5dfc4f0a6ab69fa108ba383e229d [file] [log] [blame]
Urja Rannikko22915352009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com>
5 * 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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000022#include <stdio.h>
Urja Rannikko22915352009-06-23 11:33:43 +000023#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000024#include <unistd.h>
25#include "flash.h"
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000026#include <string.h>
Urja Rannikko22915352009-06-23 11:33:43 +000027#include <ctype.h>
28#include <fcntl.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <arpa/inet.h>
32#include <netinet/in.h>
33#include <netinet/tcp.h>
34#include <netdb.h>
35#include <sys/stat.h>
36#include <errno.h>
Urja Rannikko22915352009-06-23 11:33:43 +000037#include <inttypes.h>
38#include <termios.h>
Urja Rannikkof3196df2009-07-21 13:02:59 +000039
40#define MSGHEADER "serprog:"
41
42#define S_ACK 0x06
43#define S_NAK 0x15
44#define S_CMD_NOP 0x00 /* No operation */
45#define S_CMD_Q_IFACE 0x01 /* Query interface version */
46#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
47#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
48#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
49#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
50#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
51#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
52#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum lenght */
53#define S_CMD_R_BYTE 0x09 /* Read a single byte */
54#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
55#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
56#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
57#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
58#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
59#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
60#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
61#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
62#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
63
64static int sp_fd;
65
66static uint16_t sp_device_serbuf_size = 16;
67static uint16_t sp_device_opbuf_size = 300;
68/* Bitmap of supported commands */
69static uint8_t sp_cmdmap[32];
70
71/* sp_prev_was_write used to detect writes with continouous addresses
72 and combine them to write-n's */
73static int sp_prev_was_write = 0;
74/* sp_write_n_addr used as the starting addr of the currently
75 combined write-n operation */
76static uint32_t sp_write_n_addr;
77/* The maximum length of an write_n operation; 0 = write-n not supported */
78static uint32_t sp_max_write_n = 0;
79/* The maximum length of a read_n operation; 0 = 2^24 */
80static uint32_t sp_max_read_n = 0;
81
82/* A malloc'd buffer for combining the operation's data
83 and a counter that tells how much data is there. */
84static uint8_t *sp_write_n_buf;
85static uint32_t sp_write_n_bytes = 0;
86
87/* sp_streamed_* used for flow control checking */
88static int sp_streamed_transmit_ops = 0;
89static int sp_streamed_transmit_bytes = 0;
90
91/* sp_opbuf_usage used for counting the amount of
92 on-device operation buffer used */
93static int sp_opbuf_usage = 0;
94/* if true causes sp_docommand to automatically check
95 whether the command is supported before doing it */
96static int sp_check_avail_automatic = 0;
97
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000098static void __attribute__((noreturn)) sp_die(char *msg)
Urja Rannikkof3196df2009-07-21 13:02:59 +000099{
100 perror(msg);
101 exit(1);
102}
103
104static int sp_opensocket(char *ip, unsigned int port)
105{
106 int flag = 1;
107 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000108 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +0000109 int sock;
110 printf_debug(MSGHEADER "IP %s port %d\n", ip, port);
111 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
112 if (sock < 0)
113 sp_die("Error: serprog cannot open socket");
114 hostPtr = gethostbyname(ip);
115 if (NULL == hostPtr) {
116 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
117 if (NULL == hostPtr)
118 sp_die("Error: cannot resolve");
119 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000120 sp.si.sin_family = AF_INET;
121 sp.si.sin_port = htons(port);
122 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
123 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000124 close(sock);
125 sp_die("Error: serprog cannot connect");
126 }
127 /* We are latency limited, and sometimes do write-write-read *
128 * (write-n) - so enable TCP_NODELAY. */
129 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
130 return sock;
131}
132
133struct baudentry {
134 int flag;
135 unsigned int baud;
136};
137
138/* I'd like if the C preprocessor could have directives in macros */
139#define BAUDENTRY(baud) { B##baud, baud },
140static const struct baudentry sp_baudtable[] = {
141 BAUDENTRY(9600)
142 BAUDENTRY(19200)
143 BAUDENTRY(38400)
144 BAUDENTRY(57600)
145 BAUDENTRY(115200)
146#ifdef B230400
147 BAUDENTRY(230400)
148#endif
149#ifdef B460800
150 BAUDENTRY(460800)
151#endif
152#ifdef B500000
153 BAUDENTRY(500000)
154#endif
155#ifdef B576000
156 BAUDENTRY(576000)
157#endif
158#ifdef B921600
159 BAUDENTRY(921600)
160#endif
161#ifdef B1000000
162 BAUDENTRY(1000000)
163#endif
164#ifdef B1152000
165 BAUDENTRY(1152000)
166#endif
167#ifdef B1500000
168 BAUDENTRY(1500000)
169#endif
170#ifdef B2000000
171 BAUDENTRY(2000000)
172#endif
173#ifdef B2500000
174 BAUDENTRY(2500000)
175#endif
176#ifdef B3000000
177 BAUDENTRY(3000000)
178#endif
179#ifdef B3500000
180 BAUDENTRY(3500000)
181#endif
182#ifdef B4000000
183 BAUDENTRY(4000000)
184#endif
185 {0, 0} /* Terminator */
186};
187
188static int sp_openserport(char *dev, unsigned int baud)
189{
190 struct termios options;
191 int fd, i;
192 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
193 if (fd < 0)
194 sp_die("Error: cannot open serial port");
195 fcntl(fd, F_SETFL, 0);
196 tcgetattr(fd, &options);
197 for (i = 0;; i++) {
198 if (sp_baudtable[i].baud == 0) {
199 close(fd);
200 fprintf(stderr,
201 "Error: cannot configure for baudrate %d\n",
202 baud);
203 exit(1);
204 }
205 if (sp_baudtable[i].baud == baud) {
206 cfsetispeed(&options, sp_baudtable[i].flag);
207 cfsetospeed(&options, sp_baudtable[i].flag);
208 break;
209 }
210 }
211 options.c_cflag &= ~PARENB;
212 options.c_cflag &= ~CSTOPB;
213 options.c_cflag &= ~CSIZE;
214 options.c_cflag |= CS8;
215 options.c_cflag &= ~CRTSCTS;
216 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
217 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
218 options.c_oflag &= ~OPOST;
219 options.c_cflag |= (CLOCAL | CREAD);
220 tcsetattr(fd, TCSANOW, &options);
221 return fd;
222}
223
224static void sp_flush_incoming(void)
225{
226 int i;
227 for (i=0;i<100;i++) { /* In case the device doesnt do EAGAIN, just read 0 */
228 unsigned char flush[16];
229 ssize_t rv;
230 rv = read(sp_fd, flush, sizeof(flush));
231 if ((rv == -1) && (errno == EAGAIN))
232 break;
233 if (rv == -1)
234 sp_die("flush read");
235 }
236 return;
237}
238
239static int sp_sync_read_timeout(int loops)
240{
241 int i;
242 unsigned char c;
243 for (i = 0; i < loops; i++) {
244 ssize_t rv;
245 rv = read(sp_fd, &c, 1);
246 if (rv == 1)
247 return c;
248 if ((rv == -1) && (errno != EAGAIN))
249 sp_die("read");
250 usleep(10 * 1000); /* 10ms units */
251 }
252 return -1;
253}
254
255/* Synchronize: a bit tricky algorhytm that tries to (and in my tests has *
256 * always succeeded in) bring the serial protocol to known waiting-for- *
257 * command state - uses nonblocking read - rest of the driver uses *
258 * blocking read - TODO: add an alarm() timer for the rest of the app on *
259 * serial operations, though not such a big issue as the first thing to *
260 * do is synchronize (eg. check that device is alive). */
261static void sp_synchronize(void)
262{
263 int i;
264 int flags = fcntl(sp_fd, F_GETFL);
265 unsigned char buf[8];
266 flags |= O_NONBLOCK;
267 fcntl(sp_fd, F_SETFL, flags);
268 /* First sends 8 NOPs, then flushes the return data - should cause *
269 * the device serial parser to get to a sane state, unless if it *
270 * is waiting for a real long write-n. */
271 memset(buf, S_CMD_NOP, 8);
272 if (write(sp_fd, buf, 8) != 8)
273 sp_die("flush write");
274 /* A second should be enough to get all the answers to the buffer */
275 usleep(1000 * 1000);
276 sp_flush_incoming();
277
278 /* Then try upto 8 times to send syncnop and get the correct special *
279 * return of NAK+ACK. Timing note: upto 10 characters, 10*50ms = *
280 * upto 500ms per try, 8*0.5s = 4s; +1s (above) = upto 5s sync *
281 * attempt, ~1s if immediate success. */
282 for (i = 0; i < 8; i++) {
283 int n;
284 unsigned char c = S_CMD_SYNCNOP;
285 if (write(sp_fd, &c, 1) != 1)
286 sp_die("sync write");
287 printf_debug(".");
288 fflush(stdout);
289 for (n = 0; n < 10; n++) {
290 c = sp_sync_read_timeout(5); /* wait upto 50ms */
291 if (c != S_NAK)
292 continue;
293 c = sp_sync_read_timeout(2);
294 if (c != S_ACK)
295 continue;
296 c = S_CMD_SYNCNOP;
297 if (write(sp_fd, &c, 1) != 1)
298 sp_die("sync write");
299 c = sp_sync_read_timeout(50);
300 if (c != S_NAK)
301 break; /* fail */
302 c = sp_sync_read_timeout(10);
303 if (c != S_ACK)
304 break; /* fail */
305 /* Ok, synchronized; back to blocking reads and return. */
306 flags &= ~O_NONBLOCK;
307 fcntl(sp_fd, F_SETFL, flags);
308 printf_debug("\n");
309 return;
310 }
311 }
312 fprintf(stderr,
313 "Error: cannot synchronize protocol\n"
314 "- check communications and reset device?\n");
315 exit(1);
316}
317
318static int sp_check_commandavail(uint8_t command)
319{
320 int byteoffs, bitoffs;
321 byteoffs = command / 8;
322 bitoffs = command % 8;
323 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
324}
325
326static int sp_automatic_cmdcheck(uint8_t cmd)
327{
328 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
329 printf_debug ("Warning: Automatic command availability check"
330 " failed for cmd %d - wont execute cmd\n",cmd);
331 return 1;
332 }
333 return 0;
334}
335
336static int sp_docommand(uint8_t command, uint32_t parmlen,
337 uint8_t * params, uint32_t retlen, void *retparms)
338{
339 unsigned char *sendpacket;
340 unsigned char c;
341 if (sp_automatic_cmdcheck(command))
342 return 1;
343 sendpacket = malloc(1 + parmlen);
344 if (!sendpacket)
345 sp_die("Error: cannot malloc command buffer");
346 sendpacket[0] = command;
347 memcpy(&(sendpacket[1]), params, parmlen);
348 if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) {
349 sp_die("Error: cannot write command");
350 }
351 free(sendpacket);
352 if (read(sp_fd, &c, 1) != 1)
353 sp_die("Error: cannot read from device");
354 if (c == S_NAK) return 1;
355 if (c != S_ACK) {
356 fprintf(stderr,
357 "Error: invalid response 0x%02X from device\n",c);
358 exit(1);
359 }
360 if (retlen) {
361 int rd_bytes = 0;
362 do {
363 int r;
364 r = read(sp_fd, retparms + rd_bytes,
365 retlen - rd_bytes);
366 if (r <= 0) sp_die
367 ("Error: cannot read return parameters");
368 rd_bytes += r;
369 } while (rd_bytes != retlen);
370 }
371 return 0;
372}
373
374static void sp_flush_stream(void)
375{
376 if (sp_streamed_transmit_ops)
377 do {
378 unsigned char c;
379 if (read(sp_fd, &c, 1) != 1) {
380 sp_die
381 ("Error: cannot read from device (flushing stream)");
382 }
383 if (c == S_NAK) {
384 fprintf(stderr,
385 "Error: NAK to a stream buffer operation\n");
386 exit(1);
387 }
388 if (c != S_ACK) {
389 fprintf(stderr,
390 "Error: Invalid reply 0x%02X from device\n",
391 c);
392 exit(1);
393 }
394 } while (--sp_streamed_transmit_ops);
395 sp_streamed_transmit_ops = 0;
396 sp_streamed_transmit_bytes = 0;
397}
398
399static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
400{
401 uint8_t *sp;
402 if (sp_automatic_cmdcheck(cmd))
403 return 1;
404 sp = malloc(1 + parmlen);
405 if (!sp) sp_die("Error: cannot malloc command buffer");
406 sp[0] = cmd;
407 memcpy(&(sp[1]), parms, parmlen);
408 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
409 sp_flush_stream();
410 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
411 sp_die("Error: cannot write command");
412 free(sp);
413 sp_streamed_transmit_ops += 1;
414 sp_streamed_transmit_bytes += 1 + parmlen;
415 return 0;
416}
417
418int serprog_init(void)
419{
420 uint16_t iface;
421 int len;
422 unsigned char pgmname[17];
423 unsigned char rbuf[3];
424 unsigned char c;
425 char *num;
426 char *dev;
427 printf_debug("%s\n", __func__);
428 /* the parameter is either of format "/dev/device:baud" or "ip:port" */
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000429 if ((!programmer_param) || (!strlen(programmer_param))) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000430 nodevice:
431 fprintf(stderr,
432 "Error: No device/host given for the serial programmer driver.\n"
433 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
434 exit(1);
435 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000436 num = strstr(programmer_param, ":");
437 len = num - programmer_param;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000438 if (!len) goto nodevice;
439 if (!num) {
440 fprintf(stderr,
441 "Error: No port or baudrate specified to serial programmer driver.\n"
442 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
443 exit(1);
444 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000445 len = num - programmer_param;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000446 dev = malloc(len + 1);
447 if (!dev) sp_die("Error: memory allocation failure");
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000448 memcpy(dev, programmer_param, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000449 dev[len] = 0;
450 num = strdup(num + 1);
451 if (!num) sp_die("Error: memory allocation failure");
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000452 free(programmer_param);
453 programmer_param = NULL;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000454
455 if (dev[0] == '/') sp_fd = sp_openserport(dev, atoi(num));
456 else sp_fd = sp_opensocket(dev, atoi(num));
457
458 free(dev); dev = NULL;
459 free(num); num = NULL;
460
461 printf_debug(MSGHEADER "connected - attempting to synchronize\n");
462
463 sp_check_avail_automatic = 0;
464
465 sp_synchronize();
466
467 printf_debug(MSGHEADER "Synchronized\n");
468
469 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
470 fprintf(stderr, "Error: NAK to Query Interface version\n");
471 exit(1);
472 }
473
474 if (iface != 1) {
475 fprintf(stderr, "Error: Unknown interface version %d\n", iface);
476 exit(1);
477 }
478
479 printf_debug(MSGHEADER "Interface version ok.\n");
480
481 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
482 fprintf(stderr, "Error: query command map not supported\n");
483 exit(1);
484 }
485
486 sp_check_avail_automatic = 1;
487
488 /* Check for the minimum operational set of commands */
489 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
490 fprintf(stderr, "Error: Single byte read not supported\n");
491 exit(1);
492 }
493 /* This could be translated to single byte reads (if missing), *
494 * but now we dont support that. */
495 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
496 fprintf(stderr, "Error: Read n bytes not supported\n");
497 exit(1);
498 }
499 /* In the future one could switch to read-only mode if these *
500 * are not available. */
501 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
502 fprintf(stderr,
503 "Error: Initialize operation buffer not supported\n");
504 exit(1);
505 }
506 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
507 fprintf(stderr,
508 "Error: Write to opbuf: write byte not supported\n");
509 exit(1);
510 }
511 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
512 fprintf(stderr, "Error: Write to opbuf: delay not supported\n");
513 exit(1);
514 }
515 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
516 fprintf(stderr,
517 "Error: Execute operation buffer not supported\n");
518 exit(1);
519 }
520
521 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
522 fprintf(stderr, "Warning: NAK to query programmer name\n");
523 strcpy((char *)pgmname, "(unknown)");
524 }
525 pgmname[16] = 0;
526 printf(MSGHEADER "Programmer name \"%s\"\n", pgmname);
527
528 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
529 fprintf(stderr, "Warning: NAK to query serial buffer size\n");
530 }
531 printf_debug(MSGHEADER "serial buffer size %d\n",
532 sp_device_serbuf_size);
533
534 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) {
535 fprintf(stderr,
536 "Warning: NAK to query operation buffer size\n");
537 }
538 printf_debug(MSGHEADER "operation buffer size %d\n",
539 sp_device_opbuf_size);
540
541 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
542 fprintf(stderr, "Warning: NAK to query supported buses\n");
543 c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */
544 }
545 buses_supported = c;
546
547 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
548 fprintf(stderr, "Error: NAK to initialize operation buffer\n");
549 exit(1);
550 }
551
552 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
553 printf_debug(MSGHEADER "Write-n not supported");
554 sp_max_write_n = 0;
555 } else {
556 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
557 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
558 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
559 printf_debug(MSGHEADER "Maximum write-n length %d\n",
560 sp_max_write_n);
561 sp_write_n_buf = malloc(sp_max_write_n);
562 if (!sp_write_n_buf) {
563 fprintf(stderr,
564 "Error: cannot allocate memory for Write-n buffer\n");
565 exit(1);
566 }
567 sp_write_n_bytes = 0;
568 }
569
570 if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN))
571 &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) {
572 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
573 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
574 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
575 printf_debug(MSGHEADER "Maximum read-n length %d\n",
576 sp_max_read_n ? sp_max_read_n : (1<<24));
577 } else {
578 printf_debug(MSGHEADER "Maximum read-n length not reported\n");
579 sp_max_read_n = 0;
580 }
581
582 sp_prev_was_write = 0;
583 sp_streamed_transmit_ops = 0;
584 sp_streamed_transmit_bytes = 0;
585 sp_opbuf_usage = 0;
586 return 0;
587}
588
589/* Move an in flashrom buffer existing write-n operation to *
590 * the on-device operation buffer. */
591static void sp_pass_writen(void)
592{
593 unsigned char header[7];
594 printf_debug(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
595 sp_write_n_bytes, sp_write_n_addr);
596 if (sp_streamed_transmit_bytes >=
597 (7 + sp_write_n_bytes + sp_device_serbuf_size))
598 sp_flush_stream();
599 /* In case it's just a single byte send it as a single write. */
600 if (sp_write_n_bytes == 1) {
601 sp_write_n_bytes = 0;
602 header[0] = (sp_write_n_addr >> 0) & 0xFF;
603 header[1] = (sp_write_n_addr >> 8) & 0xFF;
604 header[2] = (sp_write_n_addr >> 16) & 0xFF;
605 header[3] = sp_write_n_buf[0];
606 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
607 sp_opbuf_usage += 5;
608 return;
609 }
610 header[0] = S_CMD_O_WRITEN;
611 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
612 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
613 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
614 header[4] = (sp_write_n_addr >> 0) & 0xFF;
615 header[5] = (sp_write_n_addr >> 8) & 0xFF;
616 header[6] = (sp_write_n_addr >> 16) & 0xFF;
617 if (write(sp_fd, header, 7) != 7)
618 sp_die("Error: cannot write write-n command\n");
619 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
620 sp_write_n_bytes)
621 sp_die("Error: cannot write write-n data");
622 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
623 sp_streamed_transmit_ops += 1;
624 sp_opbuf_usage += 7 + sp_write_n_bytes;
625 sp_write_n_bytes = 0;
626 sp_prev_was_write = 0;
627}
628
629static void sp_execute_opbuf_noflush(void)
630{
631 if ((sp_max_write_n) && (sp_write_n_bytes))
632 sp_pass_writen();
633 sp_stream_buffer_op(S_CMD_O_EXEC, 0, 0);
634 printf_debug(MSGHEADER "Executed operation buffer of %d bytes\n",
635 sp_opbuf_usage);
636 sp_opbuf_usage = 0;
637 sp_prev_was_write = 0;
638 return;
639}
640
641static void sp_execute_opbuf(void)
642{
643 sp_execute_opbuf_noflush();
644 sp_flush_stream();
645}
646
647int serprog_shutdown(void)
648{
649 printf_debug("%s\n", __func__);
650 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
651 sp_execute_opbuf();
652 close(sp_fd);
653 if (sp_max_write_n)
654 free(sp_write_n_buf);
655 return 0;
656}
657
658static void sp_check_opbuf_usage(int bytes_to_be_added)
659{
660 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
661 sp_execute_opbuf();
662 /* If this happens in the mid of an page load the page load *
663 * will propably fail. */
664 printf_debug(MSGHEADER
665 "Warning: executed operation buffer due to size reasons\n");
666 }
667}
668
669void serprog_chip_writeb(uint8_t val, chipaddr addr)
670{
671 printf_debug("%s\n", __func__);
672 if (sp_max_write_n) {
673 if ((sp_prev_was_write)
674 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
675 sp_write_n_buf[sp_write_n_bytes++] = val;
676 } else {
677 if ((sp_prev_was_write) && (sp_write_n_bytes))
678 sp_pass_writen();
679 sp_prev_was_write = 1;
680 sp_write_n_addr = addr;
681 sp_write_n_bytes = 1;
682 sp_write_n_buf[0] = val;
683 }
684 sp_check_opbuf_usage(7 + sp_write_n_bytes);
685 if (sp_write_n_bytes >= sp_max_write_n)
686 sp_pass_writen();
687 } else {
688 /* We will have to do single writeb ops. */
689 unsigned char writeb_parm[4];
690 sp_check_opbuf_usage(6);
691 writeb_parm[0] = (addr >> 0) & 0xFF;
692 writeb_parm[1] = (addr >> 8) & 0xFF;
693 writeb_parm[2] = (addr >> 16) & 0xFF;
694 writeb_parm[3] = val;
695 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
696 sp_opbuf_usage += 5;
697 }
698}
699
700uint8_t serprog_chip_readb(const chipaddr addr)
701{
702 unsigned char c;
703 unsigned char buf[3];
704 /* Will stream the read operation - eg. add it to the stream buffer, *
705 * then flush the buffer, then read the read answer. */
706 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
707 sp_execute_opbuf_noflush();
708 buf[0] = ((addr >> 0) & 0xFF);
709 buf[1] = ((addr >> 8) & 0xFF);
710 buf[2] = ((addr >> 16) & 0xFF);
711 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
712 sp_flush_stream();
713 if (read(sp_fd, &c, 1) != 1)
714 sp_die("readb byteread");
715 printf_debug("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
716 return c;
717}
718
719/* Local version that really does the job, doesnt care of max_read_n. */
720static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
721{
722 int rd_bytes = 0;
723 unsigned char sbuf[6];
724 printf_debug("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
725 /* Stream the read-n -- as above. */
726 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
727 sp_execute_opbuf_noflush();
728 sbuf[0] = ((addr >> 0) & 0xFF);
729 sbuf[1] = ((addr >> 8) & 0xFF);
730 sbuf[2] = ((addr >> 16) & 0xFF);
731 sbuf[3] = ((len >> 0) & 0xFF);
732 sbuf[4] = ((len >> 8) & 0xFF);
733 sbuf[5] = ((len >> 16) & 0xFF);
734 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
735 sp_flush_stream();
736 do {
737 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
738 if (r <= 0)
739 sp_die("Error: cannot read read-n data");
740 rd_bytes += r;
741 } while (rd_bytes != len);
742 return;
743}
744
745/* The externally called version that makes sure that max_read_n is obeyed. */
746void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len)
747{
748 size_t lenm = len;
749 chipaddr addrm = addr;
750 while ((sp_max_read_n)&&(lenm > sp_max_read_n)) {
751 sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n);
752 addrm += sp_max_read_n;
753 lenm -= sp_max_read_n;
754 }
755 if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm);
756}
757
758void serprog_delay(int delay)
759{
760 unsigned char buf[4];
761 printf_debug("%s\n", __func__);
762 if ((sp_max_write_n) && (sp_write_n_bytes))
763 sp_pass_writen();
764 sp_check_opbuf_usage(5);
765 buf[0] = ((delay >> 0) & 0xFF);
766 buf[1] = ((delay >> 8) & 0xFF);
767 buf[2] = ((delay >> 16) & 0xFF);
768 buf[3] = ((delay >> 24) & 0xFF);
769 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
770 sp_opbuf_usage += 5;
771 sp_prev_was_write = 0;
772}