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 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 69 | /* sp_prev_was_write used to detect writes with contiguous addresses |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 141 | /* 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] | 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; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 301 | unsigned char pgmname[17]; |
| 302 | unsigned char rbuf[3]; |
| 303 | unsigned char c; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 304 | char *device; |
| 305 | char *baudport; |
| 306 | int have_device = 0; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 307 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 308 | /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */ |
| 309 | device = extract_param(&programmer_param, "dev", ","); |
| 310 | if (device && strlen(device)) { |
| 311 | baudport = strstr(device, ":"); |
| 312 | if (baudport) { |
| 313 | /* Split device from baudrate. */ |
| 314 | *baudport = '\0'; |
| 315 | baudport++; |
| 316 | } |
| 317 | if (!baudport || !strlen(baudport)) { |
| 318 | msg_perr("Error: No baudrate specified.\n" |
| 319 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 320 | free(device); |
| 321 | return 1; |
| 322 | } |
| 323 | if (strlen(device)) { |
| 324 | sp_fd = sp_openserport(device, atoi(baudport)); |
| 325 | have_device++; |
| 326 | } |
| 327 | } |
| 328 | if (device && !strlen(device)) { |
| 329 | msg_perr("Error: No device specified.\n" |
| 330 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 331 | free(device); |
| 332 | return 1; |
| 333 | } |
| 334 | free(device); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 335 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 336 | device = extract_param(&programmer_param, "ip", ","); |
| 337 | if (have_device && device) { |
| 338 | msg_perr("Error: Both host and device specified.\n" |
| 339 | "Please use either dev= or ip= but not both.\n"); |
| 340 | free(device); |
| 341 | return 1; |
| 342 | } |
| 343 | if (device && strlen(device)) { |
| 344 | baudport = strstr(device, ":"); |
| 345 | if (baudport) { |
| 346 | /* Split host from port. */ |
| 347 | *baudport = '\0'; |
| 348 | baudport++; |
| 349 | } |
| 350 | if (!baudport || !strlen(baudport)) { |
| 351 | msg_perr("Error: No port specified.\n" |
| 352 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 353 | free(device); |
| 354 | return 1; |
| 355 | } |
| 356 | if (strlen(device)) { |
| 357 | sp_fd = sp_opensocket(device, atoi(baudport)); |
| 358 | have_device++; |
| 359 | } |
| 360 | } |
| 361 | if (device && !strlen(device)) { |
| 362 | msg_perr("Error: No host specified.\n" |
| 363 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 364 | free(device); |
| 365 | return 1; |
| 366 | } |
| 367 | free(device); |
| 368 | |
| 369 | if (!have_device) { |
| 370 | msg_perr("Error: Neither host nor device specified.\n" |
| 371 | "Use flashrom -p serprog:dev=/dev/device:baud or " |
| 372 | "flashrom -p serprog:ip=ipaddr:port\n"); |
| 373 | return 1; |
| 374 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 375 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 376 | msg_pdbg(MSGHEADER "connected - attempting to synchronize\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 377 | |
| 378 | sp_check_avail_automatic = 0; |
| 379 | |
| 380 | sp_synchronize(); |
| 381 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 382 | msg_pdbg(MSGHEADER "Synchronized\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 383 | |
| 384 | if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 385 | msg_perr("Error: NAK to Query Interface version\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 386 | exit(1); |
| 387 | } |
| 388 | |
| 389 | if (iface != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 390 | msg_perr("Error: Unknown interface version %d\n", iface); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 391 | exit(1); |
| 392 | } |
| 393 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 394 | msg_pdbg(MSGHEADER "Interface version ok.\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 395 | |
| 396 | if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 397 | msg_perr("Error: query command map not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 398 | exit(1); |
| 399 | } |
| 400 | |
| 401 | sp_check_avail_automatic = 1; |
| 402 | |
| 403 | /* Check for the minimum operational set of commands */ |
| 404 | if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 405 | msg_perr("Error: Single byte read not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 406 | exit(1); |
| 407 | } |
| 408 | /* This could be translated to single byte reads (if missing), * |
| 409 | * but now we dont support that. */ |
| 410 | if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 411 | msg_perr("Error: Read n bytes not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 412 | exit(1); |
| 413 | } |
| 414 | /* In the future one could switch to read-only mode if these * |
| 415 | * are not available. */ |
| 416 | if (sp_check_commandavail(S_CMD_O_INIT) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 417 | msg_perr("Error: Initialize operation buffer not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 418 | exit(1); |
| 419 | } |
| 420 | if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 421 | msg_perr("Error: Write to opbuf: write byte not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 422 | exit(1); |
| 423 | } |
| 424 | if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 425 | msg_perr("Error: Write to opbuf: delay not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 426 | exit(1); |
| 427 | } |
| 428 | if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 429 | msg_perr( |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 430 | "Error: Execute operation buffer not supported\n"); |
| 431 | exit(1); |
| 432 | } |
| 433 | |
| 434 | if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 435 | msg_perr("Warning: NAK to query programmer name\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 436 | strcpy((char *)pgmname, "(unknown)"); |
| 437 | } |
| 438 | pgmname[16] = 0; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 439 | msg_pinfo(MSGHEADER "Programmer name \"%s\"\n", pgmname); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 440 | |
| 441 | 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] | 442 | msg_perr("Warning: NAK to query serial buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 443 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 444 | msg_pdbg(MSGHEADER "serial buffer size %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 445 | sp_device_serbuf_size); |
| 446 | |
| 447 | 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] | 448 | msg_perr("Warning: NAK to query operation buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 449 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 450 | msg_pdbg(MSGHEADER "operation buffer size %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 451 | sp_device_opbuf_size); |
| 452 | |
| 453 | if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 454 | msg_perr("Warning: NAK to query supported buses\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 455 | c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */ |
| 456 | } |
| 457 | buses_supported = c; |
| 458 | |
| 459 | if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 460 | msg_perr("Error: NAK to initialize operation buffer\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 461 | exit(1); |
| 462 | } |
| 463 | |
| 464 | if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 465 | msg_pdbg(MSGHEADER "Write-n not supported"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 466 | sp_max_write_n = 0; |
| 467 | } else { |
| 468 | sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); |
| 469 | sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); |
| 470 | sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 471 | msg_pdbg(MSGHEADER "Maximum write-n length %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 472 | sp_max_write_n); |
| 473 | sp_write_n_buf = malloc(sp_max_write_n); |
| 474 | if (!sp_write_n_buf) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 475 | msg_perr("Error: cannot allocate memory for Write-n buffer\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 476 | exit(1); |
| 477 | } |
| 478 | sp_write_n_bytes = 0; |
| 479 | } |
| 480 | |
| 481 | if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN)) |
| 482 | &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) { |
| 483 | sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); |
| 484 | sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); |
| 485 | sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 486 | msg_pdbg(MSGHEADER "Maximum read-n length %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 487 | sp_max_read_n ? sp_max_read_n : (1<<24)); |
| 488 | } else { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 489 | msg_pdbg(MSGHEADER "Maximum read-n length not reported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 490 | sp_max_read_n = 0; |
| 491 | } |
| 492 | |
| 493 | sp_prev_was_write = 0; |
| 494 | sp_streamed_transmit_ops = 0; |
| 495 | sp_streamed_transmit_bytes = 0; |
| 496 | sp_opbuf_usage = 0; |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* Move an in flashrom buffer existing write-n operation to * |
| 501 | * the on-device operation buffer. */ |
| 502 | static void sp_pass_writen(void) |
| 503 | { |
| 504 | unsigned char header[7]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 505 | msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 506 | sp_write_n_bytes, sp_write_n_addr); |
| 507 | if (sp_streamed_transmit_bytes >= |
| 508 | (7 + sp_write_n_bytes + sp_device_serbuf_size)) |
| 509 | sp_flush_stream(); |
| 510 | /* In case it's just a single byte send it as a single write. */ |
| 511 | if (sp_write_n_bytes == 1) { |
| 512 | sp_write_n_bytes = 0; |
| 513 | header[0] = (sp_write_n_addr >> 0) & 0xFF; |
| 514 | header[1] = (sp_write_n_addr >> 8) & 0xFF; |
| 515 | header[2] = (sp_write_n_addr >> 16) & 0xFF; |
| 516 | header[3] = sp_write_n_buf[0]; |
| 517 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header); |
| 518 | sp_opbuf_usage += 5; |
| 519 | return; |
| 520 | } |
| 521 | header[0] = S_CMD_O_WRITEN; |
| 522 | header[1] = (sp_write_n_bytes >> 0) & 0xFF; |
| 523 | header[2] = (sp_write_n_bytes >> 8) & 0xFF; |
| 524 | header[3] = (sp_write_n_bytes >> 16) & 0xFF; |
| 525 | header[4] = (sp_write_n_addr >> 0) & 0xFF; |
| 526 | header[5] = (sp_write_n_addr >> 8) & 0xFF; |
| 527 | header[6] = (sp_write_n_addr >> 16) & 0xFF; |
| 528 | if (write(sp_fd, header, 7) != 7) |
| 529 | sp_die("Error: cannot write write-n command\n"); |
| 530 | if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) != |
| 531 | sp_write_n_bytes) |
| 532 | sp_die("Error: cannot write write-n data"); |
| 533 | sp_streamed_transmit_bytes += 7 + sp_write_n_bytes; |
| 534 | sp_streamed_transmit_ops += 1; |
| 535 | sp_opbuf_usage += 7 + sp_write_n_bytes; |
| 536 | sp_write_n_bytes = 0; |
| 537 | sp_prev_was_write = 0; |
| 538 | } |
| 539 | |
| 540 | static void sp_execute_opbuf_noflush(void) |
| 541 | { |
| 542 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 543 | sp_pass_writen(); |
| 544 | sp_stream_buffer_op(S_CMD_O_EXEC, 0, 0); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 545 | msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 546 | sp_opbuf_usage); |
| 547 | sp_opbuf_usage = 0; |
| 548 | sp_prev_was_write = 0; |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | static void sp_execute_opbuf(void) |
| 553 | { |
| 554 | sp_execute_opbuf_noflush(); |
| 555 | sp_flush_stream(); |
| 556 | } |
| 557 | |
| 558 | int serprog_shutdown(void) |
| 559 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 560 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 561 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 562 | sp_execute_opbuf(); |
| 563 | close(sp_fd); |
| 564 | if (sp_max_write_n) |
| 565 | free(sp_write_n_buf); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static void sp_check_opbuf_usage(int bytes_to_be_added) |
| 570 | { |
| 571 | if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) { |
| 572 | sp_execute_opbuf(); |
| 573 | /* 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] | 574 | * will probably fail. */ |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 575 | msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | void serprog_chip_writeb(uint8_t val, chipaddr addr) |
| 580 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 581 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 582 | if (sp_max_write_n) { |
| 583 | if ((sp_prev_was_write) |
| 584 | && (addr == (sp_write_n_addr + sp_write_n_bytes))) { |
| 585 | sp_write_n_buf[sp_write_n_bytes++] = val; |
| 586 | } else { |
| 587 | if ((sp_prev_was_write) && (sp_write_n_bytes)) |
| 588 | sp_pass_writen(); |
| 589 | sp_prev_was_write = 1; |
| 590 | sp_write_n_addr = addr; |
| 591 | sp_write_n_bytes = 1; |
| 592 | sp_write_n_buf[0] = val; |
| 593 | } |
| 594 | sp_check_opbuf_usage(7 + sp_write_n_bytes); |
| 595 | if (sp_write_n_bytes >= sp_max_write_n) |
| 596 | sp_pass_writen(); |
| 597 | } else { |
| 598 | /* We will have to do single writeb ops. */ |
| 599 | unsigned char writeb_parm[4]; |
| 600 | sp_check_opbuf_usage(6); |
| 601 | writeb_parm[0] = (addr >> 0) & 0xFF; |
| 602 | writeb_parm[1] = (addr >> 8) & 0xFF; |
| 603 | writeb_parm[2] = (addr >> 16) & 0xFF; |
| 604 | writeb_parm[3] = val; |
| 605 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); |
| 606 | sp_opbuf_usage += 5; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | uint8_t serprog_chip_readb(const chipaddr addr) |
| 611 | { |
| 612 | unsigned char c; |
| 613 | unsigned char buf[3]; |
| 614 | /* Will stream the read operation - eg. add it to the stream buffer, * |
| 615 | * then flush the buffer, then read the read answer. */ |
| 616 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 617 | sp_execute_opbuf_noflush(); |
| 618 | buf[0] = ((addr >> 0) & 0xFF); |
| 619 | buf[1] = ((addr >> 8) & 0xFF); |
| 620 | buf[2] = ((addr >> 16) & 0xFF); |
| 621 | sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); |
| 622 | sp_flush_stream(); |
| 623 | if (read(sp_fd, &c, 1) != 1) |
| 624 | sp_die("readb byteread"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 625 | 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] | 626 | return c; |
| 627 | } |
| 628 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 629 | /* 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] | 630 | static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len) |
| 631 | { |
| 632 | int rd_bytes = 0; |
| 633 | unsigned char sbuf[6]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 634 | 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] | 635 | /* Stream the read-n -- as above. */ |
| 636 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 637 | sp_execute_opbuf_noflush(); |
| 638 | sbuf[0] = ((addr >> 0) & 0xFF); |
| 639 | sbuf[1] = ((addr >> 8) & 0xFF); |
| 640 | sbuf[2] = ((addr >> 16) & 0xFF); |
| 641 | sbuf[3] = ((len >> 0) & 0xFF); |
| 642 | sbuf[4] = ((len >> 8) & 0xFF); |
| 643 | sbuf[5] = ((len >> 16) & 0xFF); |
| 644 | sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf); |
| 645 | sp_flush_stream(); |
| 646 | do { |
| 647 | int r = read(sp_fd, buf + rd_bytes, len - rd_bytes); |
| 648 | if (r <= 0) |
| 649 | sp_die("Error: cannot read read-n data"); |
| 650 | rd_bytes += r; |
| 651 | } while (rd_bytes != len); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | /* The externally called version that makes sure that max_read_n is obeyed. */ |
| 656 | void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len) |
| 657 | { |
| 658 | size_t lenm = len; |
| 659 | chipaddr addrm = addr; |
| 660 | while ((sp_max_read_n)&&(lenm > sp_max_read_n)) { |
| 661 | sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n); |
| 662 | addrm += sp_max_read_n; |
| 663 | lenm -= sp_max_read_n; |
| 664 | } |
| 665 | if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm); |
| 666 | } |
| 667 | |
| 668 | void serprog_delay(int delay) |
| 669 | { |
| 670 | unsigned char buf[4]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 671 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 672 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 673 | sp_pass_writen(); |
| 674 | sp_check_opbuf_usage(5); |
| 675 | buf[0] = ((delay >> 0) & 0xFF); |
| 676 | buf[1] = ((delay >> 8) & 0xFF); |
| 677 | buf[2] = ((delay >> 16) & 0xFF); |
| 678 | buf[3] = ((delay >> 24) & 0xFF); |
| 679 | sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf); |
| 680 | sp_opbuf_usage += 5; |
| 681 | sp_prev_was_write = 0; |
| 682 | } |