kwboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Boot a Marvell SoC, with Xmodem over UART0.
  3. * supports Kirkwood, Dove, Armada 370, Armada XP
  4. *
  5. * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
  6. *
  7. * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
  8. * Integrated Controller: Functional Specifications" December 2,
  9. * 2008. Chapter 24.2 "BootROM Firmware".
  10. */
  11. #include "kwbimage.h"
  12. #include "mkimage.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <image.h>
  18. #include <libgen.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <stdint.h>
  23. #include <termios.h>
  24. #include <sys/mman.h>
  25. #include <sys/stat.h>
  26. /*
  27. * Marvell BootROM UART Sensing
  28. */
  29. static unsigned char kwboot_msg_boot[] = {
  30. 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  31. };
  32. static unsigned char kwboot_msg_debug[] = {
  33. 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  34. };
  35. /* Defines known to work on Kirkwood */
  36. #define KWBOOT_MSG_REQ_DELAY 10 /* ms */
  37. #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
  38. /* Defines known to work on Armada XP */
  39. #define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
  40. #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
  41. /*
  42. * Xmodem Transfers
  43. */
  44. #define SOH 1 /* sender start of block header */
  45. #define EOT 4 /* sender end of block transfer */
  46. #define ACK 6 /* target block ack */
  47. #define NAK 21 /* target block negative ack */
  48. #define CAN 24 /* target/sender transfer cancellation */
  49. struct kwboot_block {
  50. uint8_t soh;
  51. uint8_t pnum;
  52. uint8_t _pnum;
  53. uint8_t data[128];
  54. uint8_t csum;
  55. } __packed;
  56. #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
  57. static int kwboot_verbose;
  58. static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
  59. static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
  60. static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
  61. static void
  62. kwboot_printv(const char *fmt, ...)
  63. {
  64. va_list ap;
  65. if (kwboot_verbose) {
  66. va_start(ap, fmt);
  67. vprintf(fmt, ap);
  68. va_end(ap);
  69. fflush(stdout);
  70. }
  71. }
  72. static void
  73. __spinner(void)
  74. {
  75. const char seq[] = { '-', '\\', '|', '/' };
  76. const int div = 8;
  77. static int state, bs;
  78. if (state % div == 0) {
  79. fputc(bs, stdout);
  80. fputc(seq[state / div % sizeof(seq)], stdout);
  81. fflush(stdout);
  82. }
  83. bs = '\b';
  84. state++;
  85. }
  86. static void
  87. kwboot_spinner(void)
  88. {
  89. if (kwboot_verbose)
  90. __spinner();
  91. }
  92. static void
  93. __progress(int pct, char c)
  94. {
  95. const int width = 70;
  96. static const char *nl = "";
  97. static int pos;
  98. if (pos % width == 0)
  99. printf("%s%3d %% [", nl, pct);
  100. fputc(c, stdout);
  101. nl = "]\n";
  102. pos++;
  103. if (pct == 100) {
  104. while (pos++ < width)
  105. fputc(' ', stdout);
  106. fputs(nl, stdout);
  107. }
  108. fflush(stdout);
  109. }
  110. static void
  111. kwboot_progress(int _pct, char c)
  112. {
  113. static int pct;
  114. if (_pct != -1)
  115. pct = _pct;
  116. if (kwboot_verbose)
  117. __progress(pct, c);
  118. }
  119. static int
  120. kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
  121. {
  122. int rc, nfds;
  123. fd_set rfds;
  124. struct timeval tv;
  125. ssize_t n;
  126. rc = -1;
  127. FD_ZERO(&rfds);
  128. FD_SET(fd, &rfds);
  129. tv.tv_sec = 0;
  130. tv.tv_usec = timeo * 1000;
  131. if (tv.tv_usec > 1000000) {
  132. tv.tv_sec += tv.tv_usec / 1000000;
  133. tv.tv_usec %= 1000000;
  134. }
  135. do {
  136. nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
  137. if (nfds < 0)
  138. goto out;
  139. if (!nfds) {
  140. errno = ETIMEDOUT;
  141. goto out;
  142. }
  143. n = read(fd, buf, len);
  144. if (n <= 0)
  145. goto out;
  146. buf = (char *)buf + n;
  147. len -= n;
  148. } while (len > 0);
  149. rc = 0;
  150. out:
  151. return rc;
  152. }
  153. static int
  154. kwboot_tty_send(int fd, const void *buf, size_t len)
  155. {
  156. int rc;
  157. ssize_t n;
  158. if (!buf)
  159. return 0;
  160. rc = -1;
  161. do {
  162. n = write(fd, buf, len);
  163. if (n < 0)
  164. goto out;
  165. buf = (char *)buf + n;
  166. len -= n;
  167. } while (len > 0);
  168. rc = tcdrain(fd);
  169. out:
  170. return rc;
  171. }
  172. static int
  173. kwboot_tty_send_char(int fd, unsigned char c)
  174. {
  175. return kwboot_tty_send(fd, &c, 1);
  176. }
  177. static speed_t
  178. kwboot_tty_speed(int baudrate)
  179. {
  180. switch (baudrate) {
  181. case 115200:
  182. return B115200;
  183. case 57600:
  184. return B57600;
  185. case 38400:
  186. return B38400;
  187. case 19200:
  188. return B19200;
  189. case 9600:
  190. return B9600;
  191. }
  192. return -1;
  193. }
  194. static int
  195. kwboot_open_tty(const char *path, speed_t speed)
  196. {
  197. int rc, fd;
  198. struct termios tio;
  199. rc = -1;
  200. fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
  201. if (fd < 0)
  202. goto out;
  203. memset(&tio, 0, sizeof(tio));
  204. tio.c_iflag = 0;
  205. tio.c_cflag = CREAD|CLOCAL|CS8;
  206. tio.c_cc[VMIN] = 1;
  207. tio.c_cc[VTIME] = 10;
  208. cfsetospeed(&tio, speed);
  209. cfsetispeed(&tio, speed);
  210. rc = tcsetattr(fd, TCSANOW, &tio);
  211. if (rc)
  212. goto out;
  213. rc = fd;
  214. out:
  215. if (rc < 0) {
  216. if (fd >= 0)
  217. close(fd);
  218. }
  219. return rc;
  220. }
  221. static int
  222. kwboot_bootmsg(int tty, void *msg)
  223. {
  224. int rc;
  225. char c;
  226. int count;
  227. if (msg == NULL)
  228. kwboot_printv("Please reboot the target into UART boot mode...");
  229. else
  230. kwboot_printv("Sending boot message. Please reboot the target...");
  231. do {
  232. rc = tcflush(tty, TCIOFLUSH);
  233. if (rc)
  234. break;
  235. for (count = 0; count < 128; count++) {
  236. rc = kwboot_tty_send(tty, msg, 8);
  237. if (rc) {
  238. usleep(msg_req_delay * 1000);
  239. continue;
  240. }
  241. }
  242. rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
  243. kwboot_spinner();
  244. } while (rc || c != NAK);
  245. kwboot_printv("\n");
  246. return rc;
  247. }
  248. static int
  249. kwboot_debugmsg(int tty, void *msg)
  250. {
  251. int rc;
  252. kwboot_printv("Sending debug message. Please reboot the target...");
  253. do {
  254. char buf[16];
  255. rc = tcflush(tty, TCIOFLUSH);
  256. if (rc)
  257. break;
  258. rc = kwboot_tty_send(tty, msg, 8);
  259. if (rc) {
  260. usleep(msg_req_delay * 1000);
  261. continue;
  262. }
  263. rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
  264. kwboot_spinner();
  265. } while (rc);
  266. kwboot_printv("\n");
  267. return rc;
  268. }
  269. static int
  270. kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
  271. size_t size, int pnum)
  272. {
  273. const size_t blksz = sizeof(block->data);
  274. size_t n;
  275. int i;
  276. block->soh = SOH;
  277. block->pnum = pnum;
  278. block->_pnum = ~block->pnum;
  279. n = size < blksz ? size : blksz;
  280. memcpy(&block->data[0], data, n);
  281. memset(&block->data[n], 0, blksz - n);
  282. block->csum = 0;
  283. for (i = 0; i < n; i++)
  284. block->csum += block->data[i];
  285. return n;
  286. }
  287. static int
  288. kwboot_xm_sendblock(int fd, struct kwboot_block *block)
  289. {
  290. int rc, retries;
  291. char c;
  292. retries = 16;
  293. do {
  294. rc = kwboot_tty_send(fd, block, sizeof(*block));
  295. if (rc)
  296. break;
  297. do {
  298. rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
  299. if (rc)
  300. break;
  301. if (c != ACK && c != NAK && c != CAN)
  302. printf("%c", c);
  303. } while (c != ACK && c != NAK && c != CAN);
  304. if (c != ACK)
  305. kwboot_progress(-1, '+');
  306. } while (c == NAK && retries-- > 0);
  307. rc = -1;
  308. switch (c) {
  309. case ACK:
  310. rc = 0;
  311. break;
  312. case NAK:
  313. errno = EBADMSG;
  314. break;
  315. case CAN:
  316. errno = ECANCELED;
  317. break;
  318. default:
  319. errno = EPROTO;
  320. break;
  321. }
  322. return rc;
  323. }
  324. static int
  325. kwboot_xmodem(int tty, const void *_data, size_t size)
  326. {
  327. const uint8_t *data = _data;
  328. int rc, pnum, N, err;
  329. pnum = 1;
  330. N = 0;
  331. kwboot_printv("Sending boot image...\n");
  332. sleep(2); /* flush isn't effective without it */
  333. tcflush(tty, TCIOFLUSH);
  334. do {
  335. struct kwboot_block block;
  336. int n;
  337. n = kwboot_xm_makeblock(&block,
  338. data + N, size - N,
  339. pnum++);
  340. if (n < 0)
  341. goto can;
  342. if (!n)
  343. break;
  344. rc = kwboot_xm_sendblock(tty, &block);
  345. if (rc)
  346. goto out;
  347. N += n;
  348. kwboot_progress(N * 100 / size, '.');
  349. } while (1);
  350. rc = kwboot_tty_send_char(tty, EOT);
  351. out:
  352. return rc;
  353. can:
  354. err = errno;
  355. kwboot_tty_send_char(tty, CAN);
  356. errno = err;
  357. goto out;
  358. }
  359. static int
  360. kwboot_term_pipe(int in, int out, char *quit, int *s)
  361. {
  362. ssize_t nin, nout;
  363. char _buf[128], *buf = _buf;
  364. nin = read(in, buf, sizeof(_buf));
  365. if (nin <= 0)
  366. return -1;
  367. if (quit) {
  368. int i;
  369. for (i = 0; i < nin; i++) {
  370. if (*buf == quit[*s]) {
  371. (*s)++;
  372. if (!quit[*s])
  373. return 0;
  374. buf++;
  375. nin--;
  376. } else {
  377. while (*s > 0) {
  378. nout = write(out, quit, *s);
  379. if (nout <= 0)
  380. return -1;
  381. (*s) -= nout;
  382. }
  383. }
  384. }
  385. }
  386. while (nin > 0) {
  387. nout = write(out, buf, nin);
  388. if (nout <= 0)
  389. return -1;
  390. nin -= nout;
  391. }
  392. return 0;
  393. }
  394. static int
  395. kwboot_terminal(int tty)
  396. {
  397. int rc, in, s;
  398. char *quit = "\34c";
  399. struct termios otio, tio;
  400. rc = -1;
  401. in = STDIN_FILENO;
  402. if (isatty(in)) {
  403. rc = tcgetattr(in, &otio);
  404. if (!rc) {
  405. tio = otio;
  406. cfmakeraw(&tio);
  407. rc = tcsetattr(in, TCSANOW, &tio);
  408. }
  409. if (rc) {
  410. perror("tcsetattr");
  411. goto out;
  412. }
  413. kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
  414. quit[0]|0100, quit[1]);
  415. } else
  416. in = -1;
  417. rc = 0;
  418. s = 0;
  419. do {
  420. fd_set rfds;
  421. int nfds = 0;
  422. FD_SET(tty, &rfds);
  423. nfds = nfds < tty ? tty : nfds;
  424. if (in >= 0) {
  425. FD_SET(in, &rfds);
  426. nfds = nfds < in ? in : nfds;
  427. }
  428. nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
  429. if (nfds < 0)
  430. break;
  431. if (FD_ISSET(tty, &rfds)) {
  432. rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
  433. if (rc)
  434. break;
  435. }
  436. if (FD_ISSET(in, &rfds)) {
  437. rc = kwboot_term_pipe(in, tty, quit, &s);
  438. if (rc)
  439. break;
  440. }
  441. } while (quit[s] != 0);
  442. if (in >= 0)
  443. tcsetattr(in, TCSANOW, &otio);
  444. printf("\n");
  445. out:
  446. return rc;
  447. }
  448. static void *
  449. kwboot_mmap_image(const char *path, size_t *size, int prot)
  450. {
  451. int rc, fd, flags;
  452. struct stat st;
  453. void *img;
  454. rc = -1;
  455. img = NULL;
  456. fd = open(path, O_RDONLY);
  457. if (fd < 0)
  458. goto out;
  459. rc = fstat(fd, &st);
  460. if (rc)
  461. goto out;
  462. flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
  463. img = mmap(NULL, st.st_size, prot, flags, fd, 0);
  464. if (img == MAP_FAILED) {
  465. img = NULL;
  466. goto out;
  467. }
  468. rc = 0;
  469. *size = st.st_size;
  470. out:
  471. if (rc && img) {
  472. munmap(img, st.st_size);
  473. img = NULL;
  474. }
  475. if (fd >= 0)
  476. close(fd);
  477. return img;
  478. }
  479. static uint8_t
  480. kwboot_img_csum8(void *_data, size_t size)
  481. {
  482. uint8_t *data = _data, csum;
  483. for (csum = 0; size-- > 0; data++)
  484. csum += *data;
  485. return csum;
  486. }
  487. static int
  488. kwboot_img_patch_hdr(void *img, size_t size)
  489. {
  490. int rc;
  491. struct main_hdr_v1 *hdr;
  492. uint8_t csum;
  493. size_t hdrsz = sizeof(*hdr);
  494. int image_ver;
  495. rc = -1;
  496. hdr = img;
  497. if (size < hdrsz) {
  498. errno = EINVAL;
  499. goto out;
  500. }
  501. image_ver = image_version(img);
  502. if (image_ver != 0 && image_ver != 1) {
  503. fprintf(stderr, "Invalid image header version\n");
  504. errno = EINVAL;
  505. goto out;
  506. }
  507. if (image_ver == 0)
  508. hdrsz = sizeof(*hdr);
  509. else
  510. hdrsz = KWBHEADER_V1_SIZE(hdr);
  511. if (size < hdrsz) {
  512. errno = EINVAL;
  513. goto out;
  514. }
  515. csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
  516. if (csum != hdr->checksum) {
  517. errno = EINVAL;
  518. goto out;
  519. }
  520. if (hdr->blockid == IBR_HDR_UART_ID) {
  521. rc = 0;
  522. goto out;
  523. }
  524. hdr->blockid = IBR_HDR_UART_ID;
  525. if (image_ver == 0) {
  526. struct main_hdr_v0 *hdr_v0 = img;
  527. hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
  528. hdr_v0->nandpagesize = 0;
  529. hdr_v0->srcaddr = hdr_v0->ext
  530. ? sizeof(struct kwb_header)
  531. : sizeof(*hdr_v0);
  532. }
  533. hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
  534. rc = 0;
  535. out:
  536. return rc;
  537. }
  538. static void
  539. kwboot_usage(FILE *stream, char *progname)
  540. {
  541. fprintf(stream,
  542. "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
  543. progname);
  544. fprintf(stream, "\n");
  545. fprintf(stream,
  546. " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
  547. fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
  548. fprintf(stream,
  549. " -D <image>: boot <image> without preamble (Dove)\n");
  550. fprintf(stream, " -d: enter debug mode\n");
  551. fprintf(stream, " -a: use timings for Armada XP\n");
  552. fprintf(stream, " -q <req-delay>: use specific request-delay\n");
  553. fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
  554. fprintf(stream,
  555. " -o <block-timeo>: use specific xmodem block timeout\n");
  556. fprintf(stream, "\n");
  557. fprintf(stream, " -t: mini terminal\n");
  558. fprintf(stream, "\n");
  559. fprintf(stream, " -B <baud>: set baud rate\n");
  560. fprintf(stream, "\n");
  561. }
  562. int
  563. main(int argc, char **argv)
  564. {
  565. const char *ttypath, *imgpath;
  566. int rv, rc, tty, term, prot, patch;
  567. void *bootmsg;
  568. void *debugmsg;
  569. void *img;
  570. size_t size;
  571. speed_t speed;
  572. rv = 1;
  573. tty = -1;
  574. bootmsg = NULL;
  575. debugmsg = NULL;
  576. imgpath = NULL;
  577. img = NULL;
  578. term = 0;
  579. patch = 0;
  580. size = 0;
  581. speed = B115200;
  582. kwboot_verbose = isatty(STDOUT_FILENO);
  583. do {
  584. int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
  585. if (c < 0)
  586. break;
  587. switch (c) {
  588. case 'b':
  589. bootmsg = kwboot_msg_boot;
  590. imgpath = optarg;
  591. break;
  592. case 'D':
  593. bootmsg = NULL;
  594. imgpath = optarg;
  595. break;
  596. case 'd':
  597. debugmsg = kwboot_msg_debug;
  598. break;
  599. case 'p':
  600. patch = 1;
  601. break;
  602. case 't':
  603. term = 1;
  604. break;
  605. case 'a':
  606. msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
  607. msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
  608. break;
  609. case 'q':
  610. msg_req_delay = atoi(optarg);
  611. break;
  612. case 's':
  613. msg_rsp_timeo = atoi(optarg);
  614. break;
  615. case 'o':
  616. blk_rsp_timeo = atoi(optarg);
  617. break;
  618. case 'B':
  619. speed = kwboot_tty_speed(atoi(optarg));
  620. if (speed == -1)
  621. goto usage;
  622. break;
  623. case 'h':
  624. rv = 0;
  625. default:
  626. goto usage;
  627. }
  628. } while (1);
  629. if (!bootmsg && !term && !debugmsg)
  630. goto usage;
  631. if (patch && !imgpath)
  632. goto usage;
  633. if (argc - optind < 1)
  634. goto usage;
  635. ttypath = argv[optind++];
  636. tty = kwboot_open_tty(ttypath, speed);
  637. if (tty < 0) {
  638. perror(ttypath);
  639. goto out;
  640. }
  641. if (imgpath) {
  642. prot = PROT_READ | (patch ? PROT_WRITE : 0);
  643. img = kwboot_mmap_image(imgpath, &size, prot);
  644. if (!img) {
  645. perror(imgpath);
  646. goto out;
  647. }
  648. }
  649. if (patch) {
  650. rc = kwboot_img_patch_hdr(img, size);
  651. if (rc) {
  652. fprintf(stderr, "%s: Invalid image.\n", imgpath);
  653. goto out;
  654. }
  655. }
  656. if (debugmsg) {
  657. rc = kwboot_debugmsg(tty, debugmsg);
  658. if (rc) {
  659. perror("debugmsg");
  660. goto out;
  661. }
  662. } else if (bootmsg) {
  663. rc = kwboot_bootmsg(tty, bootmsg);
  664. if (rc) {
  665. perror("bootmsg");
  666. goto out;
  667. }
  668. }
  669. if (img) {
  670. rc = kwboot_xmodem(tty, img, size);
  671. if (rc) {
  672. perror("xmodem");
  673. goto out;
  674. }
  675. }
  676. if (term) {
  677. rc = kwboot_terminal(tty);
  678. if (rc && !(errno == EINTR)) {
  679. perror("terminal");
  680. goto out;
  681. }
  682. }
  683. rv = 0;
  684. out:
  685. if (tty >= 0)
  686. close(tty);
  687. if (img)
  688. munmap(img, size);
  689. return rv;
  690. usage:
  691. kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
  692. goto out;
  693. }