kwboot.c 14 KB

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