serprog.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009, 2011 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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <fcntl.h>
  27. #include <sys/socket.h>
  28. #include <arpa/inet.h>
  29. #include <netinet/in.h>
  30. #include <netinet/tcp.h>
  31. #include <netdb.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. #include <inttypes.h>
  35. #include <termios.h>
  36. #include "flash.h"
  37. #include "programmer.h"
  38. #include "chipdrivers.h"
  39. #define MSGHEADER "serprog: "
  40. /*
  41. * FIXME: This prototype was added to help reduce diffs for the shutdown
  42. * registration patch, which shifted many lines of code to place
  43. * serprog_shutdown() before serprog_init(). It should be removed soon.
  44. */
  45. static int serprog_shutdown(void *data);
  46. #define S_ACK 0x06
  47. #define S_NAK 0x15
  48. #define S_CMD_NOP 0x00 /* No operation */
  49. #define S_CMD_Q_IFACE 0x01 /* Query interface version */
  50. #define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
  51. #define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
  52. #define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
  53. #define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
  54. #define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
  55. #define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
  56. #define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum length */
  57. #define S_CMD_R_BYTE 0x09 /* Read a single byte */
  58. #define S_CMD_R_NBYTES 0x0A /* Read n bytes */
  59. #define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
  60. #define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
  61. #define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
  62. #define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
  63. #define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
  64. #define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
  65. #define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
  66. #define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
  67. #define S_CMD_O_SPIOP 0x13 /* Perform SPI operation. */
  68. static uint16_t sp_device_serbuf_size = 16;
  69. static uint16_t sp_device_opbuf_size = 300;
  70. /* Bitmap of supported commands */
  71. static uint8_t sp_cmdmap[32];
  72. /* sp_prev_was_write used to detect writes with contiguous addresses
  73. and combine them to write-n's */
  74. static int sp_prev_was_write = 0;
  75. /* sp_write_n_addr used as the starting addr of the currently
  76. combined write-n operation */
  77. static uint32_t sp_write_n_addr;
  78. /* The maximum length of an write_n operation; 0 = write-n not supported */
  79. static uint32_t sp_max_write_n = 0;
  80. /* The maximum length of a read_n operation; 0 = 2^24 */
  81. static uint32_t sp_max_read_n = 0;
  82. /* A malloc'd buffer for combining the operation's data
  83. and a counter that tells how much data is there. */
  84. static uint8_t *sp_write_n_buf;
  85. static uint32_t sp_write_n_bytes = 0;
  86. /* sp_streamed_* used for flow control checking */
  87. static int sp_streamed_transmit_ops = 0;
  88. static int sp_streamed_transmit_bytes = 0;
  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. static int sp_opensocket(char *ip, unsigned int port)
  96. {
  97. int flag = 1;
  98. struct hostent *hostPtr = NULL;
  99. union { struct sockaddr_in si; struct sockaddr s; } sp = {};
  100. int sock;
  101. msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
  102. sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  103. if (sock < 0)
  104. sp_die("Error: serprog cannot open socket");
  105. hostPtr = gethostbyname(ip);
  106. if (NULL == hostPtr) {
  107. hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
  108. if (NULL == hostPtr)
  109. sp_die("Error: cannot resolve");
  110. }
  111. sp.si.sin_family = AF_INET;
  112. sp.si.sin_port = htons(port);
  113. (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
  114. if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
  115. close(sock);
  116. sp_die("Error: serprog cannot connect");
  117. }
  118. /* We are latency limited, and sometimes do write-write-read *
  119. * (write-n) - so enable TCP_NODELAY. */
  120. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  121. return sock;
  122. }
  123. static int sp_sync_read_timeout(int loops)
  124. {
  125. int i;
  126. unsigned char c;
  127. for (i = 0; i < loops; i++) {
  128. ssize_t rv;
  129. rv = read(sp_fd, &c, 1);
  130. if (rv == 1)
  131. return c;
  132. if ((rv == -1) && (errno != EAGAIN))
  133. sp_die("read");
  134. usleep(10 * 1000); /* 10ms units */
  135. }
  136. return -1;
  137. }
  138. /* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
  139. * always succeeded in) bring the serial protocol to known waiting-for- *
  140. * command state - uses nonblocking read - rest of the driver uses *
  141. * blocking read - TODO: add an alarm() timer for the rest of the app on *
  142. * serial operations, though not such a big issue as the first thing to *
  143. * do is synchronize (eg. check that device is alive). */
  144. static void sp_synchronize(void)
  145. {
  146. int i;
  147. int flags = fcntl(sp_fd, F_GETFL);
  148. unsigned char buf[8];
  149. flags |= O_NONBLOCK;
  150. fcntl(sp_fd, F_SETFL, flags);
  151. /* First sends 8 NOPs, then flushes the return data - should cause *
  152. * the device serial parser to get to a sane state, unless if it *
  153. * is waiting for a real long write-n. */
  154. memset(buf, S_CMD_NOP, 8);
  155. if (write(sp_fd, buf, 8) != 8)
  156. sp_die("flush write");
  157. /* A second should be enough to get all the answers to the buffer */
  158. usleep(1000 * 1000);
  159. sp_flush_incoming();
  160. /* Then try up to 8 times to send syncnop and get the correct special *
  161. * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
  162. * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
  163. * attempt, ~1s if immediate success. */
  164. for (i = 0; i < 8; i++) {
  165. int n;
  166. unsigned char c = S_CMD_SYNCNOP;
  167. if (write(sp_fd, &c, 1) != 1)
  168. sp_die("sync write");
  169. msg_pdbg(".");
  170. fflush(stdout);
  171. for (n = 0; n < 10; n++) {
  172. c = sp_sync_read_timeout(5); /* wait up to 50ms */
  173. if (c != S_NAK)
  174. continue;
  175. c = sp_sync_read_timeout(2);
  176. if (c != S_ACK)
  177. continue;
  178. c = S_CMD_SYNCNOP;
  179. if (write(sp_fd, &c, 1) != 1)
  180. sp_die("sync write");
  181. c = sp_sync_read_timeout(50);
  182. if (c != S_NAK)
  183. break; /* fail */
  184. c = sp_sync_read_timeout(10);
  185. if (c != S_ACK)
  186. break; /* fail */
  187. /* Ok, synchronized; back to blocking reads and return. */
  188. flags &= ~O_NONBLOCK;
  189. fcntl(sp_fd, F_SETFL, flags);
  190. msg_pdbg("\n");
  191. return;
  192. }
  193. }
  194. msg_perr("Error: cannot synchronize protocol "
  195. "- check communications and reset device?\n");
  196. exit(1);
  197. }
  198. static int sp_check_commandavail(uint8_t command)
  199. {
  200. int byteoffs, bitoffs;
  201. byteoffs = command / 8;
  202. bitoffs = command % 8;
  203. return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
  204. }
  205. static int sp_automatic_cmdcheck(uint8_t cmd)
  206. {
  207. if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
  208. msg_pdbg("Warning: Automatic command availability check failed "
  209. "for cmd 0x%x - won't execute cmd\n", cmd);
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int sp_docommand(uint8_t command, uint32_t parmlen,
  215. uint8_t *params, uint32_t retlen, void *retparms)
  216. {
  217. unsigned char c;
  218. if (sp_automatic_cmdcheck(command))
  219. return 1;
  220. if (write(sp_fd, &command, 1) != 1)
  221. sp_die("Error: cannot write op code");
  222. if (write(sp_fd, params, parmlen) != (parmlen))
  223. sp_die("Error: cannot write parameters");
  224. if (read(sp_fd, &c, 1) != 1)
  225. sp_die("Error: cannot read from device");
  226. if (c == S_NAK)
  227. return 1;
  228. if (c != S_ACK) {
  229. msg_perr("Error: invalid response 0x%02X from device\n",c);
  230. exit(1);
  231. }
  232. if (retlen) {
  233. int rd_bytes = 0;
  234. do {
  235. int r;
  236. r = read(sp_fd, retparms + rd_bytes,
  237. retlen - rd_bytes);
  238. if (r <= 0)
  239. sp_die("Error: cannot read return parameters");
  240. rd_bytes += r;
  241. } while (rd_bytes != retlen);
  242. }
  243. return 0;
  244. }
  245. static void sp_flush_stream(void)
  246. {
  247. if (sp_streamed_transmit_ops)
  248. do {
  249. unsigned char c;
  250. if (read(sp_fd, &c, 1) != 1) {
  251. sp_die("Error: cannot read from device (flushing stream)");
  252. }
  253. if (c == S_NAK) {
  254. msg_perr("Error: NAK to a stream buffer operation\n");
  255. exit(1);
  256. }
  257. if (c != S_ACK) {
  258. msg_perr("Error: Invalid reply 0x%02X from device\n", c);
  259. exit(1);
  260. }
  261. } while (--sp_streamed_transmit_ops);
  262. sp_streamed_transmit_ops = 0;
  263. sp_streamed_transmit_bytes = 0;
  264. }
  265. static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
  266. {
  267. uint8_t *sp;
  268. if (sp_automatic_cmdcheck(cmd))
  269. return 1;
  270. sp = malloc(1 + parmlen);
  271. if (!sp) sp_die("Error: cannot malloc command buffer");
  272. sp[0] = cmd;
  273. memcpy(&(sp[1]), parms, parmlen);
  274. if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
  275. sp_flush_stream();
  276. if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
  277. sp_die("Error: cannot write command");
  278. free(sp);
  279. sp_streamed_transmit_ops += 1;
  280. sp_streamed_transmit_bytes += 1 + parmlen;
  281. return 0;
  282. }
  283. static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  284. const unsigned char *writearr,
  285. unsigned char *readarr);
  286. static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
  287. unsigned int start, unsigned int len);
  288. static struct spi_programmer spi_programmer_serprog = {
  289. .type = SPI_CONTROLLER_SERPROG,
  290. .max_data_read = MAX_DATA_READ_UNLIMITED,
  291. .max_data_write = MAX_DATA_WRITE_UNLIMITED,
  292. .command = serprog_spi_send_command,
  293. .multicommand = default_spi_send_multicommand,
  294. .read = serprog_spi_read,
  295. .write_256 = default_spi_write_256,
  296. };
  297. static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
  298. chipaddr addr);
  299. static uint8_t serprog_chip_readb(const struct flashctx *flash,
  300. const chipaddr addr);
  301. static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
  302. const chipaddr addr, size_t len);
  303. static const struct par_programmer par_programmer_serprog = {
  304. .chip_readb = serprog_chip_readb,
  305. .chip_readw = fallback_chip_readw,
  306. .chip_readl = fallback_chip_readl,
  307. .chip_readn = serprog_chip_readn,
  308. .chip_writeb = serprog_chip_writeb,
  309. .chip_writew = fallback_chip_writew,
  310. .chip_writel = fallback_chip_writel,
  311. .chip_writen = fallback_chip_writen,
  312. };
  313. static enum chipbustype serprog_buses_supported = BUS_NONE;
  314. int serprog_init(void)
  315. {
  316. uint16_t iface;
  317. unsigned char pgmname[17];
  318. unsigned char rbuf[3];
  319. unsigned char c;
  320. char *device;
  321. char *baudport;
  322. int have_device = 0;
  323. /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
  324. device = extract_programmer_param("dev");
  325. if (device && strlen(device)) {
  326. baudport = strstr(device, ":");
  327. if (baudport) {
  328. /* Split device from baudrate. */
  329. *baudport = '\0';
  330. baudport++;
  331. }
  332. if (!baudport || !strlen(baudport)) {
  333. msg_perr("Error: No baudrate specified.\n"
  334. "Use flashrom -p serprog:dev=/dev/device:baud\n");
  335. free(device);
  336. return 1;
  337. }
  338. if (strlen(device)) {
  339. sp_fd = sp_openserport(device, atoi(baudport));
  340. have_device++;
  341. }
  342. }
  343. if (device && !strlen(device)) {
  344. msg_perr("Error: No device specified.\n"
  345. "Use flashrom -p serprog:dev=/dev/device:baud\n");
  346. free(device);
  347. return 1;
  348. }
  349. free(device);
  350. device = extract_programmer_param("ip");
  351. if (have_device && device) {
  352. msg_perr("Error: Both host and device specified.\n"
  353. "Please use either dev= or ip= but not both.\n");
  354. free(device);
  355. return 1;
  356. }
  357. if (device && strlen(device)) {
  358. baudport = strstr(device, ":");
  359. if (baudport) {
  360. /* Split host from port. */
  361. *baudport = '\0';
  362. baudport++;
  363. }
  364. if (!baudport || !strlen(baudport)) {
  365. msg_perr("Error: No port specified.\n"
  366. "Use flashrom -p serprog:ip=ipaddr:port\n");
  367. free(device);
  368. return 1;
  369. }
  370. if (strlen(device)) {
  371. sp_fd = sp_opensocket(device, atoi(baudport));
  372. have_device++;
  373. }
  374. }
  375. if (device && !strlen(device)) {
  376. msg_perr("Error: No host specified.\n"
  377. "Use flashrom -p serprog:ip=ipaddr:port\n");
  378. free(device);
  379. return 1;
  380. }
  381. free(device);
  382. if (!have_device) {
  383. msg_perr("Error: Neither host nor device specified.\n"
  384. "Use flashrom -p serprog:dev=/dev/device:baud or "
  385. "flashrom -p serprog:ip=ipaddr:port\n");
  386. return 1;
  387. }
  388. if (register_shutdown(serprog_shutdown, NULL))
  389. return 1;
  390. msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
  391. sp_check_avail_automatic = 0;
  392. sp_synchronize();
  393. msg_pdbg(MSGHEADER "Synchronized\n");
  394. if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
  395. msg_perr("Error: NAK to query interface version\n");
  396. return 1;
  397. }
  398. if (iface != 1) {
  399. msg_perr("Error: Unknown interface version: %d\n", iface);
  400. return 1;
  401. }
  402. msg_pdbg(MSGHEADER "Interface version ok.\n");
  403. if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
  404. msg_perr("Error: query command map not supported\n");
  405. return 1;
  406. }
  407. sp_check_avail_automatic = 1;
  408. /* FIXME: This assumes that serprog device bustypes are always
  409. * identical with flashrom bustype enums and that they all fit
  410. * in a single byte.
  411. */
  412. if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
  413. msg_perr("Warning: NAK to query supported buses\n");
  414. c = BUS_NONSPI; /* A reasonable default for now. */
  415. }
  416. serprog_buses_supported = c;
  417. msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
  418. (c & BUS_PARALLEL) ? "on" : "off",
  419. (c & BUS_LPC) ? "on" : "off",
  420. (c & BUS_FWH) ? "on" : "off",
  421. (c & BUS_SPI) ? "on" : "off");
  422. /* Check for the minimum operational set of commands. */
  423. if (serprog_buses_supported & BUS_SPI) {
  424. uint8_t bt = BUS_SPI;
  425. if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
  426. msg_perr("Error: SPI operation not supported while the "
  427. "bustype is SPI\n");
  428. return 1;
  429. }
  430. /* Success of any of these commands is optional. We don't need
  431. the programmer to tell us its limits, but if it doesn't, we
  432. will assume stuff, so it's in the programmers best interest
  433. to tell us. */
  434. sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
  435. if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
  436. uint32_t v;
  437. v = ((unsigned int)(rbuf[0]) << 0);
  438. v |= ((unsigned int)(rbuf[1]) << 8);
  439. v |= ((unsigned int)(rbuf[2]) << 16);
  440. if (v == 0)
  441. v = (1 << 24) - 1; /* SPI-op maximum. */
  442. spi_programmer_serprog.max_data_write = v;
  443. msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
  444. }
  445. if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
  446. uint32_t v;
  447. v = ((unsigned int)(rbuf[0]) << 0);
  448. v |= ((unsigned int)(rbuf[1]) << 8);
  449. v |= ((unsigned int)(rbuf[2]) << 16);
  450. if (v == 0)
  451. v = (1 << 24) - 1; /* SPI-op maximum. */
  452. spi_programmer_serprog.max_data_read = v;
  453. msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
  454. }
  455. bt = serprog_buses_supported;
  456. sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
  457. }
  458. if (serprog_buses_supported & BUS_NONSPI) {
  459. if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
  460. msg_perr("Error: Initialize operation buffer "
  461. "not supported\n");
  462. return 1;
  463. }
  464. if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
  465. msg_perr("Error: Write to opbuf: "
  466. "delay not supported\n");
  467. return 1;
  468. }
  469. /* S_CMD_O_EXEC availability checked later. */
  470. if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
  471. msg_perr("Error: Single byte read not supported\n");
  472. return 1;
  473. }
  474. /* This could be translated to single byte reads (if missing),
  475. * but now we don't support that. */
  476. if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
  477. msg_perr("Error: Read n bytes not supported\n");
  478. return 1;
  479. }
  480. if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
  481. msg_perr("Error: Write to opbuf: "
  482. "write byte not supported\n");
  483. return 1;
  484. }
  485. if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
  486. msg_pdbg(MSGHEADER "Write-n not supported");
  487. sp_max_write_n = 0;
  488. } else {
  489. sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
  490. sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
  491. sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
  492. if (!sp_max_write_n) {
  493. sp_max_write_n = (1 << 24);
  494. }
  495. msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
  496. sp_max_write_n);
  497. sp_write_n_buf = malloc(sp_max_write_n);
  498. if (!sp_write_n_buf) {
  499. msg_perr("Error: cannot allocate memory for "
  500. "Write-n buffer\n");
  501. return 1;
  502. }
  503. sp_write_n_bytes = 0;
  504. }
  505. if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
  506. (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
  507. sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
  508. sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
  509. sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
  510. msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
  511. sp_max_read_n ? sp_max_read_n : (1 << 24));
  512. } else {
  513. msg_pdbg(MSGHEADER "Maximum read-n length "
  514. "not reported\n");
  515. sp_max_read_n = 0;
  516. }
  517. }
  518. if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
  519. msg_perr("Warning: NAK to query programmer name\n");
  520. strcpy((char *)pgmname, "(unknown)");
  521. }
  522. pgmname[16] = 0;
  523. msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
  524. if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
  525. msg_perr("Warning: NAK to query serial buffer size\n");
  526. }
  527. msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
  528. sp_device_serbuf_size);
  529. if (sp_check_commandavail(S_CMD_O_INIT)) {
  530. /* This would be inconsistent. */
  531. if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
  532. msg_perr("Error: Execute operation buffer not "
  533. "supported\n");
  534. return 1;
  535. }
  536. if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
  537. msg_perr("Error: NAK to initialize operation buffer\n");
  538. return 1;
  539. }
  540. if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
  541. &sp_device_opbuf_size)) {
  542. msg_perr("Warning: NAK to query operation buffer "
  543. "size\n");
  544. }
  545. msg_pdbg(MSGHEADER "operation buffer size is %d\n",
  546. sp_device_opbuf_size);
  547. }
  548. sp_prev_was_write = 0;
  549. sp_streamed_transmit_ops = 0;
  550. sp_streamed_transmit_bytes = 0;
  551. sp_opbuf_usage = 0;
  552. if (serprog_buses_supported & BUS_SPI)
  553. register_spi_programmer(&spi_programmer_serprog);
  554. if (serprog_buses_supported & BUS_NONSPI)
  555. register_par_programmer(&par_programmer_serprog,
  556. serprog_buses_supported & BUS_NONSPI);
  557. return 0;
  558. }
  559. /* Move an in flashrom buffer existing write-n operation to *
  560. * the on-device operation buffer. */
  561. static void sp_pass_writen(void)
  562. {
  563. unsigned char header[7];
  564. msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
  565. sp_write_n_bytes, sp_write_n_addr);
  566. if (sp_streamed_transmit_bytes >=
  567. (7 + sp_write_n_bytes + sp_device_serbuf_size))
  568. sp_flush_stream();
  569. /* In case it's just a single byte send it as a single write. */
  570. if (sp_write_n_bytes == 1) {
  571. sp_write_n_bytes = 0;
  572. header[0] = (sp_write_n_addr >> 0) & 0xFF;
  573. header[1] = (sp_write_n_addr >> 8) & 0xFF;
  574. header[2] = (sp_write_n_addr >> 16) & 0xFF;
  575. header[3] = sp_write_n_buf[0];
  576. sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
  577. sp_opbuf_usage += 5;
  578. return;
  579. }
  580. header[0] = S_CMD_O_WRITEN;
  581. header[1] = (sp_write_n_bytes >> 0) & 0xFF;
  582. header[2] = (sp_write_n_bytes >> 8) & 0xFF;
  583. header[3] = (sp_write_n_bytes >> 16) & 0xFF;
  584. header[4] = (sp_write_n_addr >> 0) & 0xFF;
  585. header[5] = (sp_write_n_addr >> 8) & 0xFF;
  586. header[6] = (sp_write_n_addr >> 16) & 0xFF;
  587. if (write(sp_fd, header, 7) != 7)
  588. sp_die("Error: cannot write write-n command\n");
  589. if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
  590. sp_write_n_bytes)
  591. sp_die("Error: cannot write write-n data");
  592. sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
  593. sp_streamed_transmit_ops += 1;
  594. sp_opbuf_usage += 7 + sp_write_n_bytes;
  595. sp_write_n_bytes = 0;
  596. sp_prev_was_write = 0;
  597. }
  598. static void sp_execute_opbuf_noflush(void)
  599. {
  600. if ((sp_max_write_n) && (sp_write_n_bytes))
  601. sp_pass_writen();
  602. sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
  603. msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
  604. sp_opbuf_usage);
  605. sp_opbuf_usage = 0;
  606. sp_prev_was_write = 0;
  607. return;
  608. }
  609. static void sp_execute_opbuf(void)
  610. {
  611. sp_execute_opbuf_noflush();
  612. sp_flush_stream();
  613. }
  614. static int serprog_shutdown(void *data)
  615. {
  616. msg_pspew("%s\n", __func__);
  617. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  618. sp_execute_opbuf();
  619. close(sp_fd);
  620. if (sp_max_write_n)
  621. free(sp_write_n_buf);
  622. return 0;
  623. }
  624. static void sp_check_opbuf_usage(int bytes_to_be_added)
  625. {
  626. if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
  627. sp_execute_opbuf();
  628. /* If this happens in the mid of an page load the page load *
  629. * will probably fail. */
  630. msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
  631. }
  632. }
  633. void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
  634. {
  635. msg_pspew("%s\n", __func__);
  636. if (sp_max_write_n) {
  637. if ((sp_prev_was_write)
  638. && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
  639. sp_write_n_buf[sp_write_n_bytes++] = val;
  640. } else {
  641. if ((sp_prev_was_write) && (sp_write_n_bytes))
  642. sp_pass_writen();
  643. sp_prev_was_write = 1;
  644. sp_write_n_addr = addr;
  645. sp_write_n_bytes = 1;
  646. sp_write_n_buf[0] = val;
  647. }
  648. sp_check_opbuf_usage(7 + sp_write_n_bytes);
  649. if (sp_write_n_bytes >= sp_max_write_n)
  650. sp_pass_writen();
  651. } else {
  652. /* We will have to do single writeb ops. */
  653. unsigned char writeb_parm[4];
  654. sp_check_opbuf_usage(6);
  655. writeb_parm[0] = (addr >> 0) & 0xFF;
  656. writeb_parm[1] = (addr >> 8) & 0xFF;
  657. writeb_parm[2] = (addr >> 16) & 0xFF;
  658. writeb_parm[3] = val;
  659. sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
  660. sp_opbuf_usage += 5;
  661. }
  662. }
  663. uint8_t serprog_chip_readb(const struct flashctx *flash, const chipaddr addr)
  664. {
  665. unsigned char c;
  666. unsigned char buf[3];
  667. /* Will stream the read operation - eg. add it to the stream buffer, *
  668. * then flush the buffer, then read the read answer. */
  669. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  670. sp_execute_opbuf_noflush();
  671. buf[0] = ((addr >> 0) & 0xFF);
  672. buf[1] = ((addr >> 8) & 0xFF);
  673. buf[2] = ((addr >> 16) & 0xFF);
  674. sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
  675. sp_flush_stream();
  676. if (read(sp_fd, &c, 1) != 1)
  677. sp_die("readb byteread");
  678. msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
  679. return c;
  680. }
  681. /* Local version that really does the job, doesn't care of max_read_n. */
  682. static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
  683. {
  684. int rd_bytes = 0;
  685. unsigned char sbuf[6];
  686. msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
  687. /* Stream the read-n -- as above. */
  688. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  689. sp_execute_opbuf_noflush();
  690. sbuf[0] = ((addr >> 0) & 0xFF);
  691. sbuf[1] = ((addr >> 8) & 0xFF);
  692. sbuf[2] = ((addr >> 16) & 0xFF);
  693. sbuf[3] = ((len >> 0) & 0xFF);
  694. sbuf[4] = ((len >> 8) & 0xFF);
  695. sbuf[5] = ((len >> 16) & 0xFF);
  696. sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
  697. sp_flush_stream();
  698. do {
  699. int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
  700. if (r <= 0)
  701. sp_die("Error: cannot read read-n data");
  702. rd_bytes += r;
  703. } while (rd_bytes != len);
  704. return;
  705. }
  706. /* The externally called version that makes sure that max_read_n is obeyed. */
  707. void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf, const chipaddr addr, size_t len)
  708. {
  709. size_t lenm = len;
  710. chipaddr addrm = addr;
  711. while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
  712. sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
  713. addrm += sp_max_read_n;
  714. lenm -= sp_max_read_n;
  715. }
  716. if (lenm)
  717. sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
  718. }
  719. void serprog_delay(int usecs)
  720. {
  721. unsigned char buf[4];
  722. msg_pspew("%s usecs=%d\n", __func__, usecs);
  723. if (!sp_check_commandavail(S_CMD_O_DELAY)) {
  724. msg_pdbg("Note: serprog_delay used, but the programmer doesn't "
  725. "support delay\n");
  726. internal_delay(usecs);
  727. return;
  728. }
  729. if ((sp_max_write_n) && (sp_write_n_bytes))
  730. sp_pass_writen();
  731. sp_check_opbuf_usage(5);
  732. buf[0] = ((usecs >> 0) & 0xFF);
  733. buf[1] = ((usecs >> 8) & 0xFF);
  734. buf[2] = ((usecs >> 16) & 0xFF);
  735. buf[3] = ((usecs >> 24) & 0xFF);
  736. sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
  737. sp_opbuf_usage += 5;
  738. sp_prev_was_write = 0;
  739. }
  740. static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  741. const unsigned char *writearr,
  742. unsigned char *readarr)
  743. {
  744. unsigned char *parmbuf;
  745. int ret;
  746. msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
  747. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  748. sp_execute_opbuf();
  749. parmbuf = malloc(writecnt + 6);
  750. if (!parmbuf)
  751. sp_die("Error: cannot malloc SPI send param buffer");
  752. parmbuf[0] = (writecnt >> 0) & 0xFF;
  753. parmbuf[1] = (writecnt >> 8) & 0xFF;
  754. parmbuf[2] = (writecnt >> 16) & 0xFF;
  755. parmbuf[3] = (readcnt >> 0) & 0xFF;
  756. parmbuf[4] = (readcnt >> 8) & 0xFF;
  757. parmbuf[5] = (readcnt >> 16) & 0xFF;
  758. memcpy(parmbuf + 6, writearr, writecnt);
  759. ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
  760. readarr);
  761. free(parmbuf);
  762. return ret;
  763. }
  764. /* FIXME: This function is optimized so that it does not split each transaction
  765. * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
  766. * the advantage that it is much faster for most chips, but breaks those with
  767. * non-contiguous address space (like AT45DB161D). When spi_read_chunked is
  768. * fixed this method can be removed. */
  769. static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  770. {
  771. unsigned int i, cur_len;
  772. const unsigned int max_read = spi_programmer_serprog.max_data_read;
  773. for (i = 0; i < len; i += cur_len) {
  774. int ret;
  775. cur_len = min(max_read, (len - i));
  776. ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
  777. if (ret)
  778. return ret;
  779. }
  780. return 0;
  781. }