Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

862 lines
13 KiB

  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. if (msg == NULL)
  232. kwboot_printv("Please reboot the target into UART boot mode...");
  233. else
  234. kwboot_printv("Sending boot message. Please reboot the target...");
  235. do {
  236. rc = tcflush(tty, TCIOFLUSH);
  237. if (rc)
  238. break;
  239. rc = kwboot_tty_send(tty, msg, 8);
  240. if (rc) {
  241. usleep(msg_req_delay * 1000);
  242. continue;
  243. }
  244. rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
  245. kwboot_spinner();
  246. } while (rc || c != NAK);
  247. kwboot_printv("\n");
  248. return rc;
  249. }
  250. static int
  251. kwboot_debugmsg(int tty, void *msg)
  252. {
  253. int rc;
  254. kwboot_printv("Sending debug message. Please reboot the target...");
  255. do {
  256. char buf[16];
  257. rc = tcflush(tty, TCIOFLUSH);
  258. if (rc)
  259. break;
  260. rc = kwboot_tty_send(tty, msg, 8);
  261. if (rc) {
  262. usleep(msg_req_delay * 1000);
  263. continue;
  264. }
  265. rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
  266. kwboot_spinner();
  267. } while (rc);
  268. kwboot_printv("\n");
  269. return rc;
  270. }
  271. static int
  272. kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
  273. size_t size, int pnum)
  274. {
  275. const size_t blksz = sizeof(block->data);
  276. size_t n;
  277. int i;
  278. block->soh = SOH;
  279. block->pnum = pnum;
  280. block->_pnum = ~block->pnum;
  281. n = size < blksz ? size : blksz;
  282. memcpy(&block->data[0], data, n);
  283. memset(&block->data[n], 0, blksz - n);
  284. block->csum = 0;
  285. for (i = 0; i < n; i++)
  286. block->csum += block->data[i];
  287. return n;
  288. }
  289. static int
  290. kwboot_xm_sendblock(int fd, struct kwboot_block *block)
  291. {
  292. int rc, retries;
  293. char c;
  294. retries = 16;
  295. do {
  296. rc = kwboot_tty_send(fd, block, sizeof(*block));
  297. if (rc)
  298. break;
  299. do {
  300. rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
  301. if (rc)
  302. break;
  303. if (c != ACK && c != NAK && c != CAN)
  304. printf("%c", c);
  305. } while (c != ACK && c != NAK && c != CAN);
  306. if (c != ACK)
  307. kwboot_progress(-1, '+');
  308. } while (c == NAK && retries-- > 0);
  309. rc = -1;
  310. switch (c) {
  311. case ACK:
  312. rc = 0;
  313. break;
  314. case NAK:
  315. errno = EBADMSG;
  316. break;
  317. case CAN:
  318. errno = ECANCELED;
  319. break;
  320. default:
  321. errno = EPROTO;
  322. break;
  323. }
  324. return rc;
  325. }
  326. static int
  327. kwboot_xmodem(int tty, const void *_data, size_t size)
  328. {
  329. const uint8_t *data = _data;
  330. int rc, pnum, N, err;
  331. pnum = 1;
  332. N = 0;
  333. kwboot_printv("Sending boot image...\n");
  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 {
  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. }