serprog.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 "platform.h"
  22. #include <stdio.h>
  23. #if ! IS_WINDOWS /* stuff (presumably) needed for sockets only */
  24. #include <stdlib.h>
  25. #include <unistd.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. #endif
  33. #if IS_WINDOWS
  34. #include <conio.h>
  35. #else
  36. #include <termios.h>
  37. #endif
  38. #include <string.h>
  39. #include <errno.h>
  40. #include "flash.h"
  41. #include "programmer.h"
  42. #include "chipdrivers.h"
  43. #include "serprog.h"
  44. #define MSGHEADER "serprog: "
  45. /*
  46. * FIXME: This prototype was added to help reduce diffs for the shutdown
  47. * registration patch, which shifted many lines of code to place
  48. * serprog_shutdown() before serprog_init(). It should be removed soon.
  49. */
  50. static int serprog_shutdown(void *data);
  51. static uint16_t sp_device_serbuf_size = 16;
  52. static uint16_t sp_device_opbuf_size = 300;
  53. /* Bitmap of supported commands */
  54. static uint8_t sp_cmdmap[32];
  55. /* sp_prev_was_write used to detect writes with contiguous addresses
  56. and combine them to write-n's */
  57. static int sp_prev_was_write = 0;
  58. /* sp_write_n_addr used as the starting addr of the currently
  59. combined write-n operation */
  60. static uint32_t sp_write_n_addr;
  61. /* The maximum length of an write_n operation; 0 = write-n not supported */
  62. static uint32_t sp_max_write_n = 0;
  63. /* The maximum length of a read_n operation; 0 = 2^24 */
  64. static uint32_t sp_max_read_n = 0;
  65. /* A malloc'd buffer for combining the operation's data
  66. and a counter that tells how much data is there. */
  67. static uint8_t *sp_write_n_buf;
  68. static uint32_t sp_write_n_bytes = 0;
  69. /* sp_streamed_* used for flow control checking */
  70. static int sp_streamed_transmit_ops = 0;
  71. static int sp_streamed_transmit_bytes = 0;
  72. /* sp_opbuf_usage used for counting the amount of
  73. on-device operation buffer used */
  74. static int sp_opbuf_usage = 0;
  75. /* if true causes sp_docommand to automatically check
  76. whether the command is supported before doing it */
  77. static int sp_check_avail_automatic = 0;
  78. #if ! IS_WINDOWS
  79. static int sp_opensocket(char *ip, unsigned int port)
  80. {
  81. int flag = 1;
  82. struct hostent *hostPtr = NULL;
  83. union { struct sockaddr_in si; struct sockaddr s; } sp = {};
  84. int sock;
  85. msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
  86. sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  87. if (sock < 0) {
  88. msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
  89. return -1;
  90. }
  91. hostPtr = gethostbyname(ip);
  92. if (NULL == hostPtr) {
  93. hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
  94. if (NULL == hostPtr) {
  95. close(sock);
  96. msg_perr("Error: cannot resolve %s\n", ip);
  97. return -1;
  98. }
  99. }
  100. sp.si.sin_family = AF_INET;
  101. sp.si.sin_port = htons(port);
  102. (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
  103. if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
  104. close(sock);
  105. msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
  106. return -1;
  107. }
  108. /* We are latency limited, and sometimes do write-write-read *
  109. * (write-n) - so enable TCP_NODELAY. */
  110. if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int))) {
  111. close(sock);
  112. msg_perr("Error: serprog cannot set socket options: %s\n", strerror(errno));
  113. return -1;
  114. }
  115. return sock;
  116. }
  117. #endif
  118. /* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
  119. * always succeeded in) bring the serial protocol to known waiting-for- *
  120. * command state - uses nonblocking I/O - rest of the driver uses *
  121. * blocking read - TODO: add an alarm() timer for the rest of the app on *
  122. * serial operations, though not such a big issue as the first thing to *
  123. * do is synchronize (eg. check that device is alive). */
  124. static int sp_synchronize(void)
  125. {
  126. int i;
  127. unsigned char buf[8];
  128. /* First sends 8 NOPs, then flushes the return data - should cause *
  129. * the device serial parser to get to a sane state, unless if it *
  130. * is waiting for a real long write-n. */
  131. memset(buf, S_CMD_NOP, 8);
  132. if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
  133. goto err_out;
  134. }
  135. /* A second should be enough to get all the answers to the buffer */
  136. internal_delay(1000 * 1000);
  137. sp_flush_incoming();
  138. /* Then try up to 8 times to send syncnop and get the correct special *
  139. * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
  140. * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
  141. * attempt, ~1s if immediate success. */
  142. for (i = 0; i < 8; i++) {
  143. int n;
  144. unsigned char c = S_CMD_SYNCNOP;
  145. if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
  146. goto err_out;
  147. }
  148. msg_pdbg(".");
  149. fflush(stdout);
  150. for (n = 0; n < 10; n++) {
  151. int ret = serialport_read_nonblock(&c, 1, 50, NULL);
  152. if (ret < 0)
  153. goto err_out;
  154. if (ret > 0 || c != S_NAK)
  155. continue;
  156. ret = serialport_read_nonblock(&c, 1, 20, NULL);
  157. if (ret < 0)
  158. goto err_out;
  159. if (ret > 0 || c != S_ACK)
  160. continue;
  161. c = S_CMD_SYNCNOP;
  162. if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
  163. goto err_out;
  164. }
  165. ret = serialport_read_nonblock(&c, 1, 500, NULL);
  166. if (ret < 0)
  167. goto err_out;
  168. if (ret > 0 || c != S_NAK)
  169. break; /* fail */
  170. ret = serialport_read_nonblock(&c, 1, 100, NULL);
  171. if (ret > 0 || ret < 0)
  172. goto err_out;
  173. if (c != S_ACK)
  174. break; /* fail */
  175. msg_pdbg("\n");
  176. return 0;
  177. }
  178. }
  179. err_out:
  180. msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
  181. return 1;
  182. }
  183. static int sp_check_commandavail(uint8_t command)
  184. {
  185. int byteoffs, bitoffs;
  186. byteoffs = command / 8;
  187. bitoffs = command % 8;
  188. return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
  189. }
  190. static int sp_automatic_cmdcheck(uint8_t cmd)
  191. {
  192. if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
  193. msg_pdbg("Warning: Automatic command availability check failed "
  194. "for cmd 0x%02x - won't execute cmd\n", cmd);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static int sp_docommand(uint8_t command, uint32_t parmlen,
  200. uint8_t *params, uint32_t retlen, void *retparms)
  201. {
  202. unsigned char c;
  203. if (sp_automatic_cmdcheck(command))
  204. return 1;
  205. if (serialport_write(&command, 1) != 0) {
  206. msg_perr("Error: cannot write op code: %s\n", strerror(errno));
  207. return 1;
  208. }
  209. if (serialport_write(params, parmlen) != 0) {
  210. msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
  211. return 1;
  212. }
  213. if (serialport_read(&c, 1) != 0) {
  214. msg_perr("Error: cannot read from device: %s\n", strerror(errno));
  215. return 1;
  216. }
  217. if (c == S_NAK)
  218. return 1;
  219. if (c != S_ACK) {
  220. msg_perr("Error: invalid response 0x%02X from device (to command 0x%02X)\n", c, command);
  221. return 1;
  222. }
  223. if (retlen) {
  224. if (serialport_read(retparms, retlen) != 0) {
  225. msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
  226. return 1;
  227. }
  228. }
  229. return 0;
  230. }
  231. static int sp_flush_stream(void)
  232. {
  233. if (sp_streamed_transmit_ops)
  234. do {
  235. unsigned char c;
  236. if (serialport_read(&c, 1) != 0) {
  237. msg_perr("Error: cannot read from device (flushing stream)");
  238. return 1;
  239. }
  240. if (c == S_NAK) {
  241. msg_perr("Error: NAK to a stream buffer operation\n");
  242. return 1;
  243. }
  244. if (c != S_ACK) {
  245. msg_perr("Error: Invalid reply 0x%02X from device\n", c);
  246. return 1;
  247. }
  248. } while (--sp_streamed_transmit_ops);
  249. sp_streamed_transmit_ops = 0;
  250. sp_streamed_transmit_bytes = 0;
  251. return 0;
  252. }
  253. static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
  254. {
  255. uint8_t *sp;
  256. if (sp_automatic_cmdcheck(cmd))
  257. return 1;
  258. sp = malloc(1 + parmlen);
  259. if (!sp) {
  260. msg_perr("Error: cannot malloc command buffer\n");
  261. return 1;
  262. }
  263. sp[0] = cmd;
  264. memcpy(&(sp[1]), parms, parmlen);
  265. if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) {
  266. if (sp_flush_stream() != 0) {
  267. free(sp);
  268. return 1;
  269. }
  270. }
  271. if (serialport_write(sp, 1 + parmlen) != 0) {
  272. msg_perr("Error: cannot write command\n");
  273. free(sp);
  274. return 1;
  275. }
  276. sp_streamed_transmit_ops += 1;
  277. sp_streamed_transmit_bytes += 1 + parmlen;
  278. free(sp);
  279. return 0;
  280. }
  281. static int serprog_spi_send_command(struct flashctx *flash,
  282. unsigned int writecnt, unsigned int readcnt,
  283. const unsigned char *writearr,
  284. unsigned char *readarr);
  285. static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
  286. unsigned int start, unsigned int len);
  287. static struct spi_master spi_master_serprog = {
  288. .type = SPI_CONTROLLER_SERPROG,
  289. .max_data_read = MAX_DATA_READ_UNLIMITED,
  290. .max_data_write = MAX_DATA_WRITE_UNLIMITED,
  291. .command = serprog_spi_send_command,
  292. .multicommand = default_spi_send_multicommand,
  293. .read = serprog_spi_read,
  294. .write_256 = default_spi_write_256,
  295. .write_aai = default_spi_write_aai,
  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_master par_master_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. int have_device = 0;
  322. /* the parameter is either of format "dev=/dev/device[:baud]" or "ip=ip:port" */
  323. device = extract_programmer_param("dev");
  324. if (device && strlen(device)) {
  325. char *baud_str = strstr(device, ":");
  326. if (baud_str != NULL) {
  327. /* Split device from baudrate. */
  328. *baud_str = '\0';
  329. baud_str++;
  330. }
  331. int baud;
  332. /* Convert baud string to value.
  333. * baud_str is either NULL (if strstr can't find the colon), points to the \0 after the colon
  334. * if no characters where given after the colon, or a string to convert... */
  335. if (baud_str == NULL || *baud_str == '\0') {
  336. baud = -1;
  337. msg_pdbg("No baudrate specified, using the hardware's defaults.\n");
  338. } else
  339. baud = atoi(baud_str); // FIXME: replace atoi with strtoul
  340. if (strlen(device) > 0) {
  341. sp_fd = sp_openserport(device, baud);
  342. if (sp_fd == SER_INV_FD) {
  343. free(device);
  344. return 1;
  345. }
  346. have_device++;
  347. }
  348. }
  349. #if !IS_WINDOWS
  350. if (device && !strlen(device)) {
  351. msg_perr("Error: No device specified.\n"
  352. "Use flashrom -p serprog:dev=/dev/device[:baud]\n");
  353. free(device);
  354. return 1;
  355. }
  356. free(device);
  357. device = extract_programmer_param("ip");
  358. if (have_device && device) {
  359. msg_perr("Error: Both host and device specified.\n"
  360. "Please use either dev= or ip= but not both.\n");
  361. free(device);
  362. return 1;
  363. }
  364. if (device && strlen(device)) {
  365. char *port = strstr(device, ":");
  366. if (port != NULL) {
  367. /* Split host from port. */
  368. *port = '\0';
  369. port++;
  370. }
  371. if (!port || !strlen(port)) {
  372. msg_perr("Error: No port specified.\n"
  373. "Use flashrom -p serprog:ip=ipaddr:port\n");
  374. free(device);
  375. return 1;
  376. }
  377. if (strlen(device)) {
  378. sp_fd = sp_opensocket(device, atoi(port)); // FIXME: replace atoi with strtoul
  379. if (sp_fd < 0) {
  380. free(device);
  381. return 1;
  382. }
  383. have_device++;
  384. }
  385. }
  386. if (device && !strlen(device)) {
  387. msg_perr("Error: No host specified.\n"
  388. "Use flashrom -p serprog:ip=ipaddr:port\n");
  389. free(device);
  390. return 1;
  391. }
  392. #endif
  393. free(device);
  394. if (!have_device) {
  395. #if IS_WINDOWS
  396. msg_perr("Error: No device specified.\n"
  397. "Use flashrom -p serprog:dev=comN[:baud]\n");
  398. #else
  399. msg_perr("Error: Neither host nor device specified.\n"
  400. "Use flashrom -p serprog:dev=/dev/device:baud or "
  401. "flashrom -p serprog:ip=ipaddr:port\n");
  402. #endif
  403. return 1;
  404. }
  405. if (register_shutdown(serprog_shutdown, NULL))
  406. return 1;
  407. msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
  408. sp_check_avail_automatic = 0;
  409. if (sp_synchronize())
  410. return 1;
  411. msg_pdbg(MSGHEADER "Synchronized\n");
  412. if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
  413. msg_perr("Error: NAK to query interface version\n");
  414. return 1;
  415. }
  416. if (iface != 1) {
  417. msg_perr("Error: Unknown interface version: %d\n", iface);
  418. return 1;
  419. }
  420. msg_pdbg(MSGHEADER "Interface version ok.\n");
  421. if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
  422. msg_perr("Error: query command map not supported\n");
  423. return 1;
  424. }
  425. sp_check_avail_automatic = 1;
  426. /* FIXME: This assumes that serprog device bustypes are always
  427. * identical with flashrom bustype enums and that they all fit
  428. * in a single byte.
  429. */
  430. if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
  431. msg_pwarn("Warning: NAK to query supported buses\n");
  432. c = BUS_NONSPI; /* A reasonable default for now. */
  433. }
  434. serprog_buses_supported = c;
  435. msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
  436. (c & BUS_PARALLEL) ? "on" : "off",
  437. (c & BUS_LPC) ? "on" : "off",
  438. (c & BUS_FWH) ? "on" : "off",
  439. (c & BUS_SPI) ? "on" : "off");
  440. /* Check for the minimum operational set of commands. */
  441. if (serprog_buses_supported & BUS_SPI) {
  442. uint8_t bt = BUS_SPI;
  443. char *spispeed;
  444. if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
  445. msg_perr("Error: SPI operation not supported while the "
  446. "bustype is SPI\n");
  447. return 1;
  448. }
  449. if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
  450. return 1;
  451. /* Success of any of these commands is optional. We don't need
  452. the programmer to tell us its limits, but if it doesn't, we
  453. will assume stuff, so it's in the programmers best interest
  454. to tell us. */
  455. if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
  456. uint32_t v;
  457. v = ((unsigned int)(rbuf[0]) << 0);
  458. v |= ((unsigned int)(rbuf[1]) << 8);
  459. v |= ((unsigned int)(rbuf[2]) << 16);
  460. if (v == 0)
  461. v = (1 << 24) - 1; /* SPI-op maximum. */
  462. spi_master_serprog.max_data_write = v;
  463. msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
  464. }
  465. if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
  466. uint32_t v;
  467. v = ((unsigned int)(rbuf[0]) << 0);
  468. v |= ((unsigned int)(rbuf[1]) << 8);
  469. v |= ((unsigned int)(rbuf[2]) << 16);
  470. if (v == 0)
  471. v = (1 << 24) - 1; /* SPI-op maximum. */
  472. spi_master_serprog.max_data_read = v;
  473. msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
  474. }
  475. spispeed = extract_programmer_param("spispeed");
  476. if (spispeed && strlen(spispeed)) {
  477. uint32_t f_spi_req, f_spi;
  478. uint8_t buf[4];
  479. char *f_spi_suffix;
  480. errno = 0;
  481. f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
  482. if (errno != 0 || spispeed == f_spi_suffix) {
  483. msg_perr("Error: Could not convert 'spispeed'.\n");
  484. free(spispeed);
  485. return 1;
  486. }
  487. if (strlen(f_spi_suffix) == 1) {
  488. if (!strcasecmp(f_spi_suffix, "M"))
  489. f_spi_req *= 1000000;
  490. else if (!strcasecmp(f_spi_suffix, "k"))
  491. f_spi_req *= 1000;
  492. else {
  493. msg_perr("Error: Garbage following 'spispeed' value.\n");
  494. free(spispeed);
  495. return 1;
  496. }
  497. } else if (strlen(f_spi_suffix) > 1) {
  498. msg_perr("Error: Garbage following 'spispeed' value.\n");
  499. free(spispeed);
  500. return 1;
  501. }
  502. buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
  503. buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
  504. buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
  505. buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
  506. if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0)
  507. msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
  508. else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
  509. f_spi = buf[0];
  510. f_spi |= buf[1] << (1 * 8);
  511. f_spi |= buf[2] << (2 * 8);
  512. f_spi |= buf[3] << (3 * 8);
  513. msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
  514. "It was actually set to %u Hz\n", f_spi_req, f_spi);
  515. } else
  516. msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
  517. }
  518. free(spispeed);
  519. bt = serprog_buses_supported;
  520. if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
  521. return 1;
  522. }
  523. if (serprog_buses_supported & BUS_NONSPI) {
  524. if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
  525. msg_perr("Error: Initialize operation buffer "
  526. "not supported\n");
  527. return 1;
  528. }
  529. if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
  530. msg_perr("Error: Write to opbuf: "
  531. "delay not supported\n");
  532. return 1;
  533. }
  534. /* S_CMD_O_EXEC availability checked later. */
  535. if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
  536. msg_perr("Error: Single byte read not supported\n");
  537. return 1;
  538. }
  539. /* This could be translated to single byte reads (if missing),
  540. * but now we don't support that. */
  541. if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
  542. msg_perr("Error: Read n bytes not supported\n");
  543. return 1;
  544. }
  545. if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
  546. msg_perr("Error: Write to opbuf: "
  547. "write byte not supported\n");
  548. return 1;
  549. }
  550. if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
  551. msg_pdbg(MSGHEADER "Write-n not supported");
  552. sp_max_write_n = 0;
  553. } else {
  554. sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
  555. sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
  556. sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
  557. if (!sp_max_write_n) {
  558. sp_max_write_n = (1 << 24);
  559. }
  560. msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
  561. sp_max_write_n);
  562. sp_write_n_buf = malloc(sp_max_write_n);
  563. if (!sp_write_n_buf) {
  564. msg_perr("Error: cannot allocate memory for "
  565. "Write-n buffer\n");
  566. return 1;
  567. }
  568. sp_write_n_bytes = 0;
  569. }
  570. if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
  571. (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
  572. sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
  573. sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
  574. sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
  575. msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
  576. sp_max_read_n ? sp_max_read_n : (1 << 24));
  577. } else {
  578. msg_pdbg(MSGHEADER "Maximum read-n length "
  579. "not reported\n");
  580. sp_max_read_n = 0;
  581. }
  582. }
  583. if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
  584. msg_pwarn("Warning: NAK to query programmer name\n");
  585. strcpy((char *)pgmname, "(unknown)");
  586. }
  587. pgmname[16] = 0;
  588. msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
  589. if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
  590. msg_pwarn("Warning: NAK to query serial buffer size\n");
  591. }
  592. msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
  593. sp_device_serbuf_size);
  594. if (sp_check_commandavail(S_CMD_O_INIT)) {
  595. /* This would be inconsistent. */
  596. if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
  597. msg_perr("Error: Execute operation buffer not "
  598. "supported\n");
  599. return 1;
  600. }
  601. if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
  602. msg_perr("Error: NAK to initialize operation buffer\n");
  603. return 1;
  604. }
  605. if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
  606. &sp_device_opbuf_size)) {
  607. msg_pwarn("Warning: NAK to query operation buffer size\n");
  608. }
  609. msg_pdbg(MSGHEADER "operation buffer size is %d\n",
  610. sp_device_opbuf_size);
  611. }
  612. if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
  613. uint8_t en = 1;
  614. if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
  615. msg_perr("Error: could not enable output buffers\n");
  616. return 1;
  617. } else
  618. msg_pdbg(MSGHEADER "Output drivers enabled\n");
  619. } else
  620. msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
  621. sp_prev_was_write = 0;
  622. sp_streamed_transmit_ops = 0;
  623. sp_streamed_transmit_bytes = 0;
  624. sp_opbuf_usage = 0;
  625. if (serprog_buses_supported & BUS_SPI)
  626. register_spi_master(&spi_master_serprog);
  627. if (serprog_buses_supported & BUS_NONSPI)
  628. register_par_master(&par_master_serprog, serprog_buses_supported & BUS_NONSPI);
  629. return 0;
  630. }
  631. /* Move an in flashrom buffer existing write-n operation to the on-device operation buffer. */
  632. static int sp_pass_writen(void)
  633. {
  634. unsigned char header[7];
  635. msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
  636. if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
  637. if (sp_flush_stream() != 0) {
  638. return 1;
  639. }
  640. }
  641. /* In case it's just a single byte send it as a single write. */
  642. if (sp_write_n_bytes == 1) {
  643. sp_write_n_bytes = 0;
  644. header[0] = (sp_write_n_addr >> 0) & 0xFF;
  645. header[1] = (sp_write_n_addr >> 8) & 0xFF;
  646. header[2] = (sp_write_n_addr >> 16) & 0xFF;
  647. header[3] = sp_write_n_buf[0];
  648. if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
  649. return 1;
  650. sp_opbuf_usage += 5;
  651. return 0;
  652. }
  653. header[0] = S_CMD_O_WRITEN;
  654. header[1] = (sp_write_n_bytes >> 0) & 0xFF;
  655. header[2] = (sp_write_n_bytes >> 8) & 0xFF;
  656. header[3] = (sp_write_n_bytes >> 16) & 0xFF;
  657. header[4] = (sp_write_n_addr >> 0) & 0xFF;
  658. header[5] = (sp_write_n_addr >> 8) & 0xFF;
  659. header[6] = (sp_write_n_addr >> 16) & 0xFF;
  660. if (serialport_write(header, 7) != 0) {
  661. msg_perr(MSGHEADER "Error: cannot write write-n command\n");
  662. return 1;
  663. }
  664. if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
  665. msg_perr(MSGHEADER "Error: cannot write write-n data");
  666. return 1;
  667. }
  668. sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
  669. sp_streamed_transmit_ops += 1;
  670. sp_opbuf_usage += 7 + sp_write_n_bytes;
  671. sp_write_n_bytes = 0;
  672. sp_prev_was_write = 0;
  673. return 0;
  674. }
  675. static int sp_execute_opbuf_noflush(void)
  676. {
  677. if ((sp_max_write_n) && (sp_write_n_bytes)) {
  678. if (sp_pass_writen() != 0) {
  679. msg_perr("Error: could not transfer write buffer\n");
  680. return 1;
  681. }
  682. }
  683. if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
  684. msg_perr("Error: could not execute command buffer\n");
  685. return 1;
  686. }
  687. msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
  688. sp_opbuf_usage = 0;
  689. sp_prev_was_write = 0;
  690. return 0;
  691. }
  692. static int sp_execute_opbuf(void)
  693. {
  694. if (sp_execute_opbuf_noflush() != 0)
  695. return 1;
  696. if (sp_flush_stream() != 0)
  697. return 1;
  698. return 0;
  699. }
  700. static int serprog_shutdown(void *data)
  701. {
  702. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  703. if (sp_execute_opbuf() != 0)
  704. msg_pwarn("Could not flush command buffer.\n");
  705. if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
  706. uint8_t dis = 0;
  707. if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
  708. msg_pdbg(MSGHEADER "Output drivers disabled\n");
  709. else
  710. msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
  711. }
  712. /* FIXME: fix sockets on windows(?), especially closing */
  713. serialport_shutdown(&sp_fd);
  714. if (sp_max_write_n)
  715. free(sp_write_n_buf);
  716. return 0;
  717. }
  718. static int sp_check_opbuf_usage(int bytes_to_be_added)
  719. {
  720. if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
  721. /* If this happens in the middle of a page load the page load will probably fail. */
  722. msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
  723. if (sp_execute_opbuf() != 0)
  724. return 1;
  725. }
  726. return 0;
  727. }
  728. static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
  729. chipaddr addr)
  730. {
  731. msg_pspew("%s\n", __func__);
  732. if (sp_max_write_n) {
  733. if ((sp_prev_was_write)
  734. && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
  735. sp_write_n_buf[sp_write_n_bytes++] = val;
  736. } else {
  737. if ((sp_prev_was_write) && (sp_write_n_bytes))
  738. sp_pass_writen();
  739. sp_prev_was_write = 1;
  740. sp_write_n_addr = addr;
  741. sp_write_n_bytes = 1;
  742. sp_write_n_buf[0] = val;
  743. }
  744. sp_check_opbuf_usage(7 + sp_write_n_bytes);
  745. if (sp_write_n_bytes >= sp_max_write_n)
  746. sp_pass_writen();
  747. } else {
  748. /* We will have to do single writeb ops. */
  749. unsigned char writeb_parm[4];
  750. sp_check_opbuf_usage(6);
  751. writeb_parm[0] = (addr >> 0) & 0xFF;
  752. writeb_parm[1] = (addr >> 8) & 0xFF;
  753. writeb_parm[2] = (addr >> 16) & 0xFF;
  754. writeb_parm[3] = val;
  755. sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
  756. sp_opbuf_usage += 5;
  757. }
  758. }
  759. static uint8_t serprog_chip_readb(const struct flashctx *flash,
  760. const chipaddr addr)
  761. {
  762. unsigned char c;
  763. unsigned char buf[3];
  764. /* Will stream the read operation - eg. add it to the stream buffer, *
  765. * then flush the buffer, then read the read answer. */
  766. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  767. sp_execute_opbuf_noflush();
  768. buf[0] = ((addr >> 0) & 0xFF);
  769. buf[1] = ((addr >> 8) & 0xFF);
  770. buf[2] = ((addr >> 16) & 0xFF);
  771. sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
  772. sp_flush_stream(); // FIXME: return error
  773. if (serialport_read(&c, 1) != 0)
  774. msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
  775. msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
  776. return c;
  777. }
  778. /* Local version that really does the job, doesn't care of max_read_n. */
  779. static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
  780. {
  781. unsigned char sbuf[6];
  782. msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
  783. /* Stream the read-n -- as above. */
  784. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
  785. sp_execute_opbuf_noflush();
  786. sbuf[0] = ((addr >> 0) & 0xFF);
  787. sbuf[1] = ((addr >> 8) & 0xFF);
  788. sbuf[2] = ((addr >> 16) & 0xFF);
  789. sbuf[3] = ((len >> 0) & 0xFF);
  790. sbuf[4] = ((len >> 8) & 0xFF);
  791. sbuf[5] = ((len >> 16) & 0xFF);
  792. sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
  793. if (sp_flush_stream() != 0)
  794. return 1;
  795. if (serialport_read(buf, len) != 0) {
  796. msg_perr(MSGHEADER "Error: cannot read read-n data");
  797. return 1;
  798. }
  799. return 0;
  800. }
  801. /* The externally called version that makes sure that max_read_n is obeyed. */
  802. static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
  803. const chipaddr addr, size_t len)
  804. {
  805. size_t lenm = len;
  806. chipaddr addrm = addr;
  807. while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
  808. sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
  809. addrm += sp_max_read_n;
  810. lenm -= sp_max_read_n;
  811. }
  812. if (lenm)
  813. sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
  814. }
  815. void serprog_delay(unsigned int usecs)
  816. {
  817. unsigned char buf[4];
  818. msg_pspew("%s usecs=%d\n", __func__, usecs);
  819. if (!sp_check_commandavail(S_CMD_O_DELAY)) {
  820. msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
  821. internal_delay(usecs);
  822. return;
  823. }
  824. if ((sp_max_write_n) && (sp_write_n_bytes))
  825. sp_pass_writen();
  826. sp_check_opbuf_usage(5);
  827. buf[0] = ((usecs >> 0) & 0xFF);
  828. buf[1] = ((usecs >> 8) & 0xFF);
  829. buf[2] = ((usecs >> 16) & 0xFF);
  830. buf[3] = ((usecs >> 24) & 0xFF);
  831. sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
  832. sp_opbuf_usage += 5;
  833. sp_prev_was_write = 0;
  834. }
  835. static int serprog_spi_send_command(struct flashctx *flash,
  836. unsigned int writecnt, unsigned int readcnt,
  837. const unsigned char *writearr,
  838. unsigned char *readarr)
  839. {
  840. unsigned char *parmbuf;
  841. int ret;
  842. msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
  843. if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
  844. if (sp_execute_opbuf() != 0) {
  845. msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
  846. return 1;
  847. }
  848. }
  849. parmbuf = malloc(writecnt + 6);
  850. if (!parmbuf) {
  851. msg_perr("Error: could not allocate SPI send param buffer.\n");
  852. return 1;
  853. }
  854. parmbuf[0] = (writecnt >> 0) & 0xFF;
  855. parmbuf[1] = (writecnt >> 8) & 0xFF;
  856. parmbuf[2] = (writecnt >> 16) & 0xFF;
  857. parmbuf[3] = (readcnt >> 0) & 0xFF;
  858. parmbuf[4] = (readcnt >> 8) & 0xFF;
  859. parmbuf[5] = (readcnt >> 16) & 0xFF;
  860. memcpy(parmbuf + 6, writearr, writecnt);
  861. ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
  862. readarr);
  863. free(parmbuf);
  864. return ret;
  865. }
  866. /* FIXME: This function is optimized so that it does not split each transaction
  867. * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
  868. * the advantage that it is much faster for most chips, but breaks those with
  869. * non-continuous reads. When spi_read_chunked is fixed this method can be removed. */
  870. static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
  871. unsigned int start, unsigned int len)
  872. {
  873. unsigned int i, cur_len;
  874. const unsigned int max_read = spi_master_serprog.max_data_read;
  875. for (i = 0; i < len; i += cur_len) {
  876. int ret;
  877. cur_len = min(max_read, (len - i));
  878. ret = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
  879. ? spi_nbyte_read(flash, start + i, buf + i, cur_len)
  880. : flash->chip->four_bytes_addr_funcs.read_nbyte(flash,
  881. start + i, buf + i, cur_len);
  882. if (ret)
  883. return ret;
  884. }
  885. return 0;
  886. }
  887. void *serprog_map(const char *descr, uintptr_t phys_addr, size_t len)
  888. {
  889. /* Serprog transmits 24 bits only and assumes the underlying implementation handles any remaining bits
  890. * correctly (usually setting them to one either in software (for FWH/LPC) or relying on the fact that
  891. * the hardware observes a subset of the address bits only). Combined with the standard mapping of
  892. * flashrom this creates a 16 MB-wide window just below the 4 GB boundary where serprog can operate (as
  893. * needed for non-SPI chips). Below we make sure that the requested range is within this window. */
  894. if ((phys_addr & 0xFF000000) == 0xFF000000) {
  895. return (void*)phys_addr;
  896. } else {
  897. msg_pwarn(MSGHEADER "requested mapping %s is incompatible: 0x%zx bytes at 0x%0*" PRIxPTR ".\n",
  898. descr, len, PRIxPTR_WIDTH, phys_addr);
  899. return NULL;
  900. }
  901. }