kwboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. while (nin > 0) {
  386. nout = write(out, buf, nin);
  387. if (nout <= 0)
  388. return -1;
  389. nin -= nout;
  390. }
  391. return 0;
  392. }
  393. static int
  394. kwboot_terminal(int tty)
  395. {
  396. int rc, in, s;
  397. char *quit = "\34c";
  398. struct termios otio, tio;
  399. rc = -1;
  400. in = STDIN_FILENO;
  401. if (isatty(in)) {
  402. rc = tcgetattr(in, &otio);
  403. if (!rc) {
  404. tio = otio;
  405. cfmakeraw(&tio);
  406. rc = tcsetattr(in, TCSANOW, &tio);
  407. }
  408. if (rc) {
  409. perror("tcsetattr");
  410. goto out;
  411. }
  412. kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
  413. quit[0]|0100, quit[1]);
  414. } else
  415. in = -1;
  416. rc = 0;
  417. s = 0;
  418. do {
  419. fd_set rfds;
  420. int nfds = 0;
  421. FD_SET(tty, &rfds);
  422. nfds = nfds < tty ? tty : nfds;
  423. if (in >= 0) {
  424. FD_SET(in, &rfds);
  425. nfds = nfds < in ? in : nfds;
  426. }
  427. nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
  428. if (nfds < 0)
  429. break;
  430. if (FD_ISSET(tty, &rfds)) {
  431. rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
  432. if (rc)
  433. break;
  434. }
  435. if (FD_ISSET(in, &rfds)) {
  436. rc = kwboot_term_pipe(in, tty, quit, &s);
  437. if (rc)
  438. break;
  439. }
  440. } while (quit[s] != 0);
  441. tcsetattr(in, TCSANOW, &otio);
  442. out:
  443. return rc;
  444. }
  445. static void *
  446. kwboot_mmap_image(const char *path, size_t *size, int prot)
  447. {
  448. int rc, fd, flags;
  449. struct stat st;
  450. void *img;
  451. rc = -1;
  452. img = NULL;
  453. fd = open(path, O_RDONLY);
  454. if (fd < 0)
  455. goto out;
  456. rc = fstat(fd, &st);
  457. if (rc)
  458. goto out;
  459. flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
  460. img = mmap(NULL, st.st_size, prot, flags, fd, 0);
  461. if (img == MAP_FAILED) {
  462. img = NULL;
  463. goto out;
  464. }
  465. rc = 0;
  466. *size = st.st_size;
  467. out:
  468. if (rc && img) {
  469. munmap(img, st.st_size);
  470. img = NULL;
  471. }
  472. if (fd >= 0)
  473. close(fd);
  474. return img;
  475. }
  476. static uint8_t
  477. kwboot_img_csum8(void *_data, size_t size)
  478. {
  479. uint8_t *data = _data, csum;
  480. for (csum = 0; size-- > 0; data++)
  481. csum += *data;
  482. return csum;
  483. }
  484. static int
  485. kwboot_img_patch_hdr(void *img, size_t size)
  486. {
  487. int rc;
  488. struct main_hdr_v1 *hdr;
  489. uint8_t csum;
  490. size_t hdrsz = sizeof(*hdr);
  491. int image_ver;
  492. rc = -1;
  493. hdr = img;
  494. if (size < hdrsz) {
  495. errno = EINVAL;
  496. goto out;
  497. }
  498. image_ver = image_version(img);
  499. if (image_ver < 0) {
  500. fprintf(stderr, "Invalid image header version\n");
  501. errno = EINVAL;
  502. goto out;
  503. }
  504. if (image_ver == 0)
  505. hdrsz = sizeof(*hdr);
  506. else
  507. hdrsz = KWBHEADER_V1_SIZE(hdr);
  508. csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
  509. if (csum != hdr->checksum) {
  510. errno = EINVAL;
  511. goto out;
  512. }
  513. if (hdr->blockid == IBR_HDR_UART_ID) {
  514. rc = 0;
  515. goto out;
  516. }
  517. hdr->blockid = IBR_HDR_UART_ID;
  518. if (image_ver == 0) {
  519. struct main_hdr_v0 *hdr_v0 = img;
  520. hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
  521. hdr_v0->nandpagesize = 0;
  522. hdr_v0->srcaddr = hdr_v0->ext
  523. ? sizeof(struct kwb_header)
  524. : sizeof(*hdr_v0);
  525. }
  526. hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
  527. rc = 0;
  528. out:
  529. return rc;
  530. }
  531. static void
  532. kwboot_usage(FILE *stream, char *progname)
  533. {
  534. fprintf(stream,
  535. "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
  536. progname);
  537. fprintf(stream, "\n");
  538. fprintf(stream,
  539. " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
  540. fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
  541. fprintf(stream,
  542. " -D <image>: boot <image> without preamble (Dove)\n");
  543. fprintf(stream, " -d: enter debug mode\n");
  544. fprintf(stream, " -a: use timings for Armada XP\n");
  545. fprintf(stream, " -q <req-delay>: use specific request-delay\n");
  546. fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
  547. fprintf(stream,
  548. " -o <block-timeo>: use specific xmodem block timeout\n");
  549. fprintf(stream, "\n");
  550. fprintf(stream, " -t: mini terminal\n");
  551. fprintf(stream, "\n");
  552. fprintf(stream, " -B <baud>: set baud rate\n");
  553. fprintf(stream, "\n");
  554. }
  555. int
  556. main(int argc, char **argv)
  557. {
  558. const char *ttypath, *imgpath;
  559. int rv, rc, tty, term, prot, patch;
  560. void *bootmsg;
  561. void *debugmsg;
  562. void *img;
  563. size_t size;
  564. speed_t speed;
  565. rv = 1;
  566. tty = -1;
  567. bootmsg = NULL;
  568. debugmsg = NULL;
  569. imgpath = NULL;
  570. img = NULL;
  571. term = 0;
  572. patch = 0;
  573. size = 0;
  574. speed = B115200;
  575. kwboot_verbose = isatty(STDOUT_FILENO);
  576. do {
  577. int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
  578. if (c < 0)
  579. break;
  580. switch (c) {
  581. case 'b':
  582. bootmsg = kwboot_msg_boot;
  583. imgpath = optarg;
  584. break;
  585. case 'D':
  586. bootmsg = NULL;
  587. imgpath = optarg;
  588. break;
  589. case 'd':
  590. debugmsg = kwboot_msg_debug;
  591. break;
  592. case 'p':
  593. patch = 1;
  594. break;
  595. case 't':
  596. term = 1;
  597. break;
  598. case 'a':
  599. msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
  600. msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
  601. break;
  602. case 'q':
  603. msg_req_delay = atoi(optarg);
  604. break;
  605. case 's':
  606. msg_rsp_timeo = atoi(optarg);
  607. break;
  608. case 'o':
  609. blk_rsp_timeo = atoi(optarg);
  610. break;
  611. case 'B':
  612. speed = kwboot_tty_speed(atoi(optarg));
  613. if (speed == -1)
  614. goto usage;
  615. break;
  616. case 'h':
  617. rv = 0;
  618. default:
  619. goto usage;
  620. }
  621. } while (1);
  622. if (!bootmsg && !term && !debugmsg)
  623. goto usage;
  624. if (patch && !imgpath)
  625. goto usage;
  626. if (argc - optind < 1)
  627. goto usage;
  628. ttypath = argv[optind++];
  629. tty = kwboot_open_tty(ttypath, speed);
  630. if (tty < 0) {
  631. perror(ttypath);
  632. goto out;
  633. }
  634. if (imgpath) {
  635. prot = PROT_READ | (patch ? PROT_WRITE : 0);
  636. img = kwboot_mmap_image(imgpath, &size, prot);
  637. if (!img) {
  638. perror(imgpath);
  639. goto out;
  640. }
  641. }
  642. if (patch) {
  643. rc = kwboot_img_patch_hdr(img, size);
  644. if (rc) {
  645. fprintf(stderr, "%s: Invalid image.\n", imgpath);
  646. goto out;
  647. }
  648. }
  649. if (debugmsg) {
  650. rc = kwboot_debugmsg(tty, debugmsg);
  651. if (rc) {
  652. perror("debugmsg");
  653. goto out;
  654. }
  655. } else if (bootmsg) {
  656. rc = kwboot_bootmsg(tty, bootmsg);
  657. if (rc) {
  658. perror("bootmsg");
  659. goto out;
  660. }
  661. }
  662. if (img) {
  663. rc = kwboot_xmodem(tty, img, size);
  664. if (rc) {
  665. perror("xmodem");
  666. goto out;
  667. }
  668. }
  669. if (term) {
  670. rc = kwboot_terminal(tty);
  671. if (rc && !(errno == EINTR)) {
  672. perror("terminal");
  673. goto out;
  674. }
  675. }
  676. rv = 0;
  677. out:
  678. if (tty >= 0)
  679. close(tty);
  680. if (img)
  681. munmap(img, size);
  682. return rv;
  683. usage:
  684. kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
  685. goto out;
  686. }