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