kwboot.c 13 KB

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