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 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 64 | static uint16_t sp_device_serbuf_size = 16; |
| 65 | static uint16_t sp_device_opbuf_size = 300; |
| 66 | /* Bitmap of supported commands */ |
| 67 | static uint8_t sp_cmdmap[32]; |
| 68 | |
| 69 | /* sp_prev_was_write used to detect writes with continouous addresses |
| 70 | and combine them to write-n's */ |
| 71 | static int sp_prev_was_write = 0; |
| 72 | /* sp_write_n_addr used as the starting addr of the currently |
| 73 | combined write-n operation */ |
| 74 | static uint32_t sp_write_n_addr; |
| 75 | /* The maximum length of an write_n operation; 0 = write-n not supported */ |
| 76 | static uint32_t sp_max_write_n = 0; |
| 77 | /* The maximum length of a read_n operation; 0 = 2^24 */ |
| 78 | static uint32_t sp_max_read_n = 0; |
| 79 | |
| 80 | /* A malloc'd buffer for combining the operation's data |
| 81 | and a counter that tells how much data is there. */ |
| 82 | static uint8_t *sp_write_n_buf; |
| 83 | static uint32_t sp_write_n_bytes = 0; |
| 84 | |
| 85 | /* sp_streamed_* used for flow control checking */ |
| 86 | static int sp_streamed_transmit_ops = 0; |
| 87 | static int sp_streamed_transmit_bytes = 0; |
| 88 | |
| 89 | /* sp_opbuf_usage used for counting the amount of |
| 90 | on-device operation buffer used */ |
| 91 | static int sp_opbuf_usage = 0; |
| 92 | /* if true causes sp_docommand to automatically check |
| 93 | whether the command is supported before doing it */ |
| 94 | static int sp_check_avail_automatic = 0; |
| 95 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 96 | static int sp_opensocket(char *ip, unsigned int port) |
| 97 | { |
| 98 | int flag = 1; |
| 99 | struct hostent *hostPtr = NULL; |
Carl-Daniel Hailfinger | 6d12560 | 2009-09-05 01:10:23 +0000 | [diff] [blame] | 100 | union { struct sockaddr_in si; struct sockaddr s; } sp = {}; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 101 | int sock; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 102 | msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 103 | sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 104 | if (sock < 0) |
| 105 | sp_die("Error: serprog cannot open socket"); |
| 106 | hostPtr = gethostbyname(ip); |
| 107 | if (NULL == hostPtr) { |
| 108 | hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET); |
| 109 | if (NULL == hostPtr) |
| 110 | sp_die("Error: cannot resolve"); |
| 111 | } |
Carl-Daniel Hailfinger | 6d12560 | 2009-09-05 01:10:23 +0000 | [diff] [blame] | 112 | sp.si.sin_family = AF_INET; |
| 113 | sp.si.sin_port = htons(port); |
| 114 | (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length); |
| 115 | if (connect(sock, &sp.s, sizeof(sp.si)) < 0) { |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 116 | close(sock); |
| 117 | sp_die("Error: serprog cannot connect"); |
| 118 | } |
| 119 | /* We are latency limited, and sometimes do write-write-read * |
| 120 | * (write-n) - so enable TCP_NODELAY. */ |
| 121 | setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); |
| 122 | return sock; |
| 123 | } |
| 124 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 125 | static int sp_sync_read_timeout(int loops) |
| 126 | { |
| 127 | int i; |
| 128 | unsigned char c; |
| 129 | for (i = 0; i < loops; i++) { |
| 130 | ssize_t rv; |
| 131 | rv = read(sp_fd, &c, 1); |
| 132 | if (rv == 1) |
| 133 | return c; |
| 134 | if ((rv == -1) && (errno != EAGAIN)) |
| 135 | sp_die("read"); |
| 136 | usleep(10 * 1000); /* 10ms units */ |
| 137 | } |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* Synchronize: a bit tricky algorhytm that tries to (and in my tests has * |
| 142 | * always succeeded in) bring the serial protocol to known waiting-for- * |
| 143 | * command state - uses nonblocking read - rest of the driver uses * |
| 144 | * blocking read - TODO: add an alarm() timer for the rest of the app on * |
| 145 | * serial operations, though not such a big issue as the first thing to * |
| 146 | * do is synchronize (eg. check that device is alive). */ |
| 147 | static void sp_synchronize(void) |
| 148 | { |
| 149 | int i; |
| 150 | int flags = fcntl(sp_fd, F_GETFL); |
| 151 | unsigned char buf[8]; |
| 152 | flags |= O_NONBLOCK; |
| 153 | fcntl(sp_fd, F_SETFL, flags); |
| 154 | /* First sends 8 NOPs, then flushes the return data - should cause * |
| 155 | * the device serial parser to get to a sane state, unless if it * |
| 156 | * is waiting for a real long write-n. */ |
| 157 | memset(buf, S_CMD_NOP, 8); |
| 158 | if (write(sp_fd, buf, 8) != 8) |
| 159 | sp_die("flush write"); |
| 160 | /* A second should be enough to get all the answers to the buffer */ |
| 161 | usleep(1000 * 1000); |
| 162 | sp_flush_incoming(); |
| 163 | |
| 164 | /* Then try upto 8 times to send syncnop and get the correct special * |
| 165 | * return of NAK+ACK. Timing note: upto 10 characters, 10*50ms = * |
| 166 | * upto 500ms per try, 8*0.5s = 4s; +1s (above) = upto 5s sync * |
| 167 | * attempt, ~1s if immediate success. */ |
| 168 | for (i = 0; i < 8; i++) { |
| 169 | int n; |
| 170 | unsigned char c = S_CMD_SYNCNOP; |
| 171 | if (write(sp_fd, &c, 1) != 1) |
| 172 | sp_die("sync write"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 173 | msg_pdbg("."); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 174 | fflush(stdout); |
| 175 | for (n = 0; n < 10; n++) { |
| 176 | c = sp_sync_read_timeout(5); /* wait upto 50ms */ |
| 177 | if (c != S_NAK) |
| 178 | continue; |
| 179 | c = sp_sync_read_timeout(2); |
| 180 | if (c != S_ACK) |
| 181 | continue; |
| 182 | c = S_CMD_SYNCNOP; |
| 183 | if (write(sp_fd, &c, 1) != 1) |
| 184 | sp_die("sync write"); |
| 185 | c = sp_sync_read_timeout(50); |
| 186 | if (c != S_NAK) |
| 187 | break; /* fail */ |
| 188 | c = sp_sync_read_timeout(10); |
| 189 | if (c != S_ACK) |
| 190 | break; /* fail */ |
| 191 | /* Ok, synchronized; back to blocking reads and return. */ |
| 192 | flags &= ~O_NONBLOCK; |
| 193 | fcntl(sp_fd, F_SETFL, flags); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 194 | msg_pdbg("\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 198 | msg_perr("Error: cannot synchronize protocol\n" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 199 | "- check communications and reset device?\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
| 203 | static int sp_check_commandavail(uint8_t command) |
| 204 | { |
| 205 | int byteoffs, bitoffs; |
| 206 | byteoffs = command / 8; |
| 207 | bitoffs = command % 8; |
| 208 | return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0; |
| 209 | } |
| 210 | |
| 211 | static int sp_automatic_cmdcheck(uint8_t cmd) |
| 212 | { |
| 213 | if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 214 | msg_pdbg("Warning: Automatic command availability check" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 215 | " failed for cmd %d - wont execute cmd\n",cmd); |
| 216 | return 1; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int sp_docommand(uint8_t command, uint32_t parmlen, |
| 222 | uint8_t * params, uint32_t retlen, void *retparms) |
| 223 | { |
| 224 | unsigned char *sendpacket; |
| 225 | unsigned char c; |
| 226 | if (sp_automatic_cmdcheck(command)) |
| 227 | return 1; |
| 228 | sendpacket = malloc(1 + parmlen); |
| 229 | if (!sendpacket) |
| 230 | sp_die("Error: cannot malloc command buffer"); |
| 231 | sendpacket[0] = command; |
| 232 | memcpy(&(sendpacket[1]), params, parmlen); |
| 233 | if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) { |
| 234 | sp_die("Error: cannot write command"); |
| 235 | } |
| 236 | free(sendpacket); |
| 237 | if (read(sp_fd, &c, 1) != 1) |
| 238 | sp_die("Error: cannot read from device"); |
| 239 | if (c == S_NAK) return 1; |
| 240 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 241 | msg_perr("Error: invalid response 0x%02X from device\n",c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 242 | exit(1); |
| 243 | } |
| 244 | if (retlen) { |
| 245 | int rd_bytes = 0; |
| 246 | do { |
| 247 | int r; |
| 248 | r = read(sp_fd, retparms + rd_bytes, |
| 249 | retlen - rd_bytes); |
| 250 | if (r <= 0) sp_die |
| 251 | ("Error: cannot read return parameters"); |
| 252 | rd_bytes += r; |
| 253 | } while (rd_bytes != retlen); |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static void sp_flush_stream(void) |
| 259 | { |
| 260 | if (sp_streamed_transmit_ops) |
| 261 | do { |
| 262 | unsigned char c; |
| 263 | if (read(sp_fd, &c, 1) != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 264 | sp_die("Error: cannot read from device (flushing stream)"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 265 | } |
| 266 | if (c == S_NAK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 267 | msg_perr("Error: NAK to a stream buffer operation\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 268 | exit(1); |
| 269 | } |
| 270 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 271 | msg_perr("Error: Invalid reply 0x%02X from device\n", c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 272 | exit(1); |
| 273 | } |
| 274 | } while (--sp_streamed_transmit_ops); |
| 275 | sp_streamed_transmit_ops = 0; |
| 276 | sp_streamed_transmit_bytes = 0; |
| 277 | } |
| 278 | |
| 279 | static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms) |
| 280 | { |
| 281 | uint8_t *sp; |
| 282 | if (sp_automatic_cmdcheck(cmd)) |
| 283 | return 1; |
| 284 | sp = malloc(1 + parmlen); |
| 285 | if (!sp) sp_die("Error: cannot malloc command buffer"); |
| 286 | sp[0] = cmd; |
| 287 | memcpy(&(sp[1]), parms, parmlen); |
| 288 | if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) |
| 289 | sp_flush_stream(); |
| 290 | if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen)) |
| 291 | sp_die("Error: cannot write command"); |
| 292 | free(sp); |
| 293 | sp_streamed_transmit_ops += 1; |
| 294 | sp_streamed_transmit_bytes += 1 + parmlen; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int serprog_init(void) |
| 299 | { |
| 300 | uint16_t iface; |
| 301 | int len; |
| 302 | unsigned char pgmname[17]; |
| 303 | unsigned char rbuf[3]; |
| 304 | unsigned char c; |
| 305 | char *num; |
| 306 | char *dev; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 307 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 308 | /* 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] | 309 | if ((!programmer_param) || (!strlen(programmer_param))) { |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 310 | nodevice: |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 311 | msg_perr("Error: No device/host given for the serial programmer driver.\n" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 312 | "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n"); |
| 313 | exit(1); |
| 314 | } |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 315 | num = strstr(programmer_param, ":"); |
| 316 | len = num - programmer_param; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 317 | if (!len) goto nodevice; |
| 318 | if (!num) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 319 | msg_perr("Error: No port or baudrate specified to serial programmer driver.\n" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 320 | "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n"); |
| 321 | exit(1); |
| 322 | } |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 323 | len = num - programmer_param; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 324 | dev = malloc(len + 1); |
| 325 | if (!dev) sp_die("Error: memory allocation failure"); |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 326 | memcpy(dev, programmer_param, len); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 327 | dev[len] = 0; |
| 328 | num = strdup(num + 1); |
| 329 | if (!num) sp_die("Error: memory allocation failure"); |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 330 | free(programmer_param); |
| 331 | programmer_param = NULL; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 332 | |
| 333 | if (dev[0] == '/') sp_fd = sp_openserport(dev, atoi(num)); |
| 334 | else sp_fd = sp_opensocket(dev, atoi(num)); |
| 335 | |
| 336 | free(dev); dev = NULL; |
| 337 | free(num); num = NULL; |
| 338 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 339 | msg_pdbg(MSGHEADER "connected - attempting to synchronize\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 340 | |
| 341 | sp_check_avail_automatic = 0; |
| 342 | |
| 343 | sp_synchronize(); |
| 344 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 345 | msg_pdbg(MSGHEADER "Synchronized\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 346 | |
| 347 | if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 348 | msg_perr("Error: NAK to Query Interface version\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 349 | exit(1); |
| 350 | } |
| 351 | |
| 352 | if (iface != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 353 | msg_perr("Error: Unknown interface version %d\n", iface); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 354 | exit(1); |
| 355 | } |
| 356 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 357 | msg_pdbg(MSGHEADER "Interface version ok.\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 358 | |
| 359 | if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 360 | msg_perr("Error: query command map not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 361 | exit(1); |
| 362 | } |
| 363 | |
| 364 | sp_check_avail_automatic = 1; |
| 365 | |
| 366 | /* Check for the minimum operational set of commands */ |
| 367 | if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 368 | msg_perr("Error: Single byte read not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 369 | exit(1); |
| 370 | } |
| 371 | /* This could be translated to single byte reads (if missing), * |
| 372 | * but now we dont support that. */ |
| 373 | if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 374 | msg_perr("Error: Read n bytes not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 375 | exit(1); |
| 376 | } |
| 377 | /* In the future one could switch to read-only mode if these * |
| 378 | * are not available. */ |
| 379 | if (sp_check_commandavail(S_CMD_O_INIT) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 380 | msg_perr("Error: Initialize operation buffer not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 381 | exit(1); |
| 382 | } |
| 383 | if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 384 | msg_perr("Error: Write to opbuf: write byte not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 385 | exit(1); |
| 386 | } |
| 387 | if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 388 | msg_perr("Error: Write to opbuf: delay not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 389 | exit(1); |
| 390 | } |
| 391 | if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 392 | msg_perr( |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 393 | "Error: Execute operation buffer not supported\n"); |
| 394 | exit(1); |
| 395 | } |
| 396 | |
| 397 | if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 398 | msg_perr("Warning: NAK to query programmer name\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 399 | strcpy((char *)pgmname, "(unknown)"); |
| 400 | } |
| 401 | pgmname[16] = 0; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 402 | msg_pinfo(MSGHEADER "Programmer name \"%s\"\n", pgmname); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 403 | |
| 404 | if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 405 | msg_perr("Warning: NAK to query serial buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 406 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 407 | msg_pdbg(MSGHEADER "serial buffer size %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 408 | sp_device_serbuf_size); |
| 409 | |
| 410 | if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 411 | msg_perr("Warning: NAK to query operation buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 412 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 413 | msg_pdbg(MSGHEADER "operation buffer size %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 414 | sp_device_opbuf_size); |
| 415 | |
| 416 | if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 417 | msg_perr("Warning: NAK to query supported buses\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 418 | c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */ |
| 419 | } |
| 420 | buses_supported = c; |
| 421 | |
| 422 | if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 423 | msg_perr("Error: NAK to initialize operation buffer\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 424 | exit(1); |
| 425 | } |
| 426 | |
| 427 | if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 428 | msg_pdbg(MSGHEADER "Write-n not supported"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 429 | sp_max_write_n = 0; |
| 430 | } else { |
| 431 | sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); |
| 432 | sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); |
| 433 | sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 434 | msg_pdbg(MSGHEADER "Maximum write-n length %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 435 | sp_max_write_n); |
| 436 | sp_write_n_buf = malloc(sp_max_write_n); |
| 437 | if (!sp_write_n_buf) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 438 | msg_perr("Error: cannot allocate memory for Write-n buffer\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 439 | exit(1); |
| 440 | } |
| 441 | sp_write_n_bytes = 0; |
| 442 | } |
| 443 | |
| 444 | if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN)) |
| 445 | &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) { |
| 446 | sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); |
| 447 | sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); |
| 448 | sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 449 | msg_pdbg(MSGHEADER "Maximum read-n length %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 450 | sp_max_read_n ? sp_max_read_n : (1<<24)); |
| 451 | } else { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 452 | msg_pdbg(MSGHEADER "Maximum read-n length not reported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 453 | sp_max_read_n = 0; |
| 454 | } |
| 455 | |
| 456 | sp_prev_was_write = 0; |
| 457 | sp_streamed_transmit_ops = 0; |
| 458 | sp_streamed_transmit_bytes = 0; |
| 459 | sp_opbuf_usage = 0; |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* Move an in flashrom buffer existing write-n operation to * |
| 464 | * the on-device operation buffer. */ |
| 465 | static void sp_pass_writen(void) |
| 466 | { |
| 467 | unsigned char header[7]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 468 | msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 469 | sp_write_n_bytes, sp_write_n_addr); |
| 470 | if (sp_streamed_transmit_bytes >= |
| 471 | (7 + sp_write_n_bytes + sp_device_serbuf_size)) |
| 472 | sp_flush_stream(); |
| 473 | /* In case it's just a single byte send it as a single write. */ |
| 474 | if (sp_write_n_bytes == 1) { |
| 475 | sp_write_n_bytes = 0; |
| 476 | header[0] = (sp_write_n_addr >> 0) & 0xFF; |
| 477 | header[1] = (sp_write_n_addr >> 8) & 0xFF; |
| 478 | header[2] = (sp_write_n_addr >> 16) & 0xFF; |
| 479 | header[3] = sp_write_n_buf[0]; |
| 480 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header); |
| 481 | sp_opbuf_usage += 5; |
| 482 | return; |
| 483 | } |
| 484 | header[0] = S_CMD_O_WRITEN; |
| 485 | header[1] = (sp_write_n_bytes >> 0) & 0xFF; |
| 486 | header[2] = (sp_write_n_bytes >> 8) & 0xFF; |
| 487 | header[3] = (sp_write_n_bytes >> 16) & 0xFF; |
| 488 | header[4] = (sp_write_n_addr >> 0) & 0xFF; |
| 489 | header[5] = (sp_write_n_addr >> 8) & 0xFF; |
| 490 | header[6] = (sp_write_n_addr >> 16) & 0xFF; |
| 491 | if (write(sp_fd, header, 7) != 7) |
| 492 | sp_die("Error: cannot write write-n command\n"); |
| 493 | if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) != |
| 494 | sp_write_n_bytes) |
| 495 | sp_die("Error: cannot write write-n data"); |
| 496 | sp_streamed_transmit_bytes += 7 + sp_write_n_bytes; |
| 497 | sp_streamed_transmit_ops += 1; |
| 498 | sp_opbuf_usage += 7 + sp_write_n_bytes; |
| 499 | sp_write_n_bytes = 0; |
| 500 | sp_prev_was_write = 0; |
| 501 | } |
| 502 | |
| 503 | static void sp_execute_opbuf_noflush(void) |
| 504 | { |
| 505 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 506 | sp_pass_writen(); |
| 507 | sp_stream_buffer_op(S_CMD_O_EXEC, 0, 0); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 508 | msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 509 | sp_opbuf_usage); |
| 510 | sp_opbuf_usage = 0; |
| 511 | sp_prev_was_write = 0; |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | static void sp_execute_opbuf(void) |
| 516 | { |
| 517 | sp_execute_opbuf_noflush(); |
| 518 | sp_flush_stream(); |
| 519 | } |
| 520 | |
| 521 | int serprog_shutdown(void) |
| 522 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 523 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 524 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 525 | sp_execute_opbuf(); |
| 526 | close(sp_fd); |
| 527 | if (sp_max_write_n) |
| 528 | free(sp_write_n_buf); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static void sp_check_opbuf_usage(int bytes_to_be_added) |
| 533 | { |
| 534 | if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) { |
| 535 | sp_execute_opbuf(); |
| 536 | /* If this happens in the mid of an page load the page load * |
| 537 | * will propably fail. */ |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 538 | msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | void serprog_chip_writeb(uint8_t val, chipaddr addr) |
| 543 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 544 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 545 | if (sp_max_write_n) { |
| 546 | if ((sp_prev_was_write) |
| 547 | && (addr == (sp_write_n_addr + sp_write_n_bytes))) { |
| 548 | sp_write_n_buf[sp_write_n_bytes++] = val; |
| 549 | } else { |
| 550 | if ((sp_prev_was_write) && (sp_write_n_bytes)) |
| 551 | sp_pass_writen(); |
| 552 | sp_prev_was_write = 1; |
| 553 | sp_write_n_addr = addr; |
| 554 | sp_write_n_bytes = 1; |
| 555 | sp_write_n_buf[0] = val; |
| 556 | } |
| 557 | sp_check_opbuf_usage(7 + sp_write_n_bytes); |
| 558 | if (sp_write_n_bytes >= sp_max_write_n) |
| 559 | sp_pass_writen(); |
| 560 | } else { |
| 561 | /* We will have to do single writeb ops. */ |
| 562 | unsigned char writeb_parm[4]; |
| 563 | sp_check_opbuf_usage(6); |
| 564 | writeb_parm[0] = (addr >> 0) & 0xFF; |
| 565 | writeb_parm[1] = (addr >> 8) & 0xFF; |
| 566 | writeb_parm[2] = (addr >> 16) & 0xFF; |
| 567 | writeb_parm[3] = val; |
| 568 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); |
| 569 | sp_opbuf_usage += 5; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | uint8_t serprog_chip_readb(const chipaddr addr) |
| 574 | { |
| 575 | unsigned char c; |
| 576 | unsigned char buf[3]; |
| 577 | /* Will stream the read operation - eg. add it to the stream buffer, * |
| 578 | * then flush the buffer, then read the read answer. */ |
| 579 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 580 | sp_execute_opbuf_noflush(); |
| 581 | buf[0] = ((addr >> 0) & 0xFF); |
| 582 | buf[1] = ((addr >> 8) & 0xFF); |
| 583 | buf[2] = ((addr >> 16) & 0xFF); |
| 584 | sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); |
| 585 | sp_flush_stream(); |
| 586 | if (read(sp_fd, &c, 1) != 1) |
| 587 | sp_die("readb byteread"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 588 | msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 589 | return c; |
| 590 | } |
| 591 | |
| 592 | /* Local version that really does the job, doesnt care of max_read_n. */ |
| 593 | static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len) |
| 594 | { |
| 595 | int rd_bytes = 0; |
| 596 | unsigned char sbuf[6]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 597 | msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 598 | /* Stream the read-n -- as above. */ |
| 599 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 600 | sp_execute_opbuf_noflush(); |
| 601 | sbuf[0] = ((addr >> 0) & 0xFF); |
| 602 | sbuf[1] = ((addr >> 8) & 0xFF); |
| 603 | sbuf[2] = ((addr >> 16) & 0xFF); |
| 604 | sbuf[3] = ((len >> 0) & 0xFF); |
| 605 | sbuf[4] = ((len >> 8) & 0xFF); |
| 606 | sbuf[5] = ((len >> 16) & 0xFF); |
| 607 | sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf); |
| 608 | sp_flush_stream(); |
| 609 | do { |
| 610 | int r = read(sp_fd, buf + rd_bytes, len - rd_bytes); |
| 611 | if (r <= 0) |
| 612 | sp_die("Error: cannot read read-n data"); |
| 613 | rd_bytes += r; |
| 614 | } while (rd_bytes != len); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | /* The externally called version that makes sure that max_read_n is obeyed. */ |
| 619 | void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len) |
| 620 | { |
| 621 | size_t lenm = len; |
| 622 | chipaddr addrm = addr; |
| 623 | while ((sp_max_read_n)&&(lenm > sp_max_read_n)) { |
| 624 | sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n); |
| 625 | addrm += sp_max_read_n; |
| 626 | lenm -= sp_max_read_n; |
| 627 | } |
| 628 | if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm); |
| 629 | } |
| 630 | |
| 631 | void serprog_delay(int delay) |
| 632 | { |
| 633 | unsigned char buf[4]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 634 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 635 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 636 | sp_pass_writen(); |
| 637 | sp_check_opbuf_usage(5); |
| 638 | buf[0] = ((delay >> 0) & 0xFF); |
| 639 | buf[1] = ((delay >> 8) & 0xFF); |
| 640 | buf[2] = ((delay >> 16) & 0xFF); |
| 641 | buf[3] = ((delay >> 24) & 0xFF); |
| 642 | sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf); |
| 643 | sp_opbuf_usage += 5; |
| 644 | sp_prev_was_write = 0; |
| 645 | } |