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