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