2 * Boot a Marvell SoC, with Xmodem over UART0.
3 * supports Kirkwood, Dove, Armada 370, Armada XP
5 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
7 * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
8 * Integrated Controller: Functional Specifications" December 2,
9 * 2008. Chapter 24.2 "BootROM Firmware".
31 * Marvell BootROM UART Sensing
34 static unsigned char kwboot_msg_boot[] = {
35 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
38 static unsigned char kwboot_msg_debug[] = {
39 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
42 /* Defines known to work on Kirkwood */
43 #define KWBOOT_MSG_REQ_DELAY 10 /* ms */
44 #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
46 /* Defines known to work on Armada XP */
47 #define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
48 #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
54 #define SOH 1 /* sender start of block header */
55 #define EOT 4 /* sender end of block transfer */
56 #define ACK 6 /* target block ack */
57 #define NAK 21 /* target block negative ack */
58 #define CAN 24 /* target/sender transfer cancellation */
60 #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
66 uint8_t data[KWBOOT_XM_BLKSZ];
70 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
72 static int kwboot_verbose;
74 static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
75 static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
76 static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
79 kwboot_write(int fd, const char *buf, size_t len)
84 ssize_t wr = write(fd, buf + tot, len - tot);
96 kwboot_printv(const char *fmt, ...)
100 if (kwboot_verbose) {
111 const char seq[] = { '-', '\\', '|', '/' };
113 static int state, bs;
115 if (state % div == 0) {
117 fputc(seq[state / div % sizeof(seq)], stdout);
133 __progress(int pct, char c)
135 const int width = 70;
136 static const char *nl = "";
139 if (pos % width == 0)
140 printf("%s%3d %% [", nl, pct);
145 pos = (pos + 1) % width;
148 while (pos && pos++ < width)
160 kwboot_progress(int _pct, char c)
175 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
188 tv.tv_usec = timeo * 1000;
189 if (tv.tv_usec > 1000000) {
190 tv.tv_sec += tv.tv_usec / 1000000;
191 tv.tv_usec %= 1000000;
195 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
203 n = read(fd, buf, len);
207 buf = (char *)buf + n;
217 kwboot_tty_send(int fd, const void *buf, size_t len)
222 if (kwboot_write(fd, buf, len) < 0)
229 kwboot_tty_send_char(int fd, unsigned char c)
231 return kwboot_tty_send(fd, &c, 1);
235 kwboot_tty_speed(int baudrate)
254 kwboot_open_tty(const char *path, speed_t speed)
261 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
265 memset(&tio, 0, sizeof(tio));
268 tio.c_cflag = CREAD|CLOCAL|CS8;
271 tio.c_cc[VTIME] = 10;
273 cfsetospeed(&tio, speed);
274 cfsetispeed(&tio, speed);
276 rc = tcsetattr(fd, TCSANOW, &tio);
291 kwboot_bootmsg(int tty, void *msg)
298 kwboot_printv("Please reboot the target into UART boot mode...");
300 kwboot_printv("Sending boot message. Please reboot the target...");
303 rc = tcflush(tty, TCIOFLUSH);
307 for (count = 0; count < 128; count++) {
308 rc = kwboot_tty_send(tty, msg, 8);
310 usleep(msg_req_delay * 1000);
315 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
319 } while (rc || c != NAK);
327 kwboot_debugmsg(int tty, void *msg)
331 kwboot_printv("Sending debug message. Please reboot the target...");
336 rc = tcflush(tty, TCIOFLUSH);
340 rc = kwboot_tty_send(tty, msg, 8);
342 usleep(msg_req_delay * 1000);
346 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
358 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
359 size_t size, int pnum)
365 block->_pnum = ~block->pnum;
367 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
368 memcpy(&block->data[0], data, n);
369 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
372 for (i = 0; i < n; i++)
373 block->csum += block->data[i];
381 return c == ACK || c == NAK || c == CAN;
385 kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
392 rc = kwboot_tty_recv(fd, c, 1, blk_rsp_timeo);
394 if (errno != ETIMEDOUT)
399 /* If received xmodem reply, end. */
400 if (_is_xm_reply(*c))
404 * If printing non-xmodem text output is allowed and such a byte
405 * was received, print it.
418 kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
429 rc = kwboot_tty_send(fd, block, sizeof(*block));
433 if (allow_non_xm && !*done_print) {
434 kwboot_progress(100, '.');
435 kwboot_printv("Done\n");
439 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
443 if (!allow_non_xm && c != ACK)
444 kwboot_progress(-1, '+');
445 } while (c == NAK && retries-- > 0);
471 kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
478 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
479 header ? "header" : "data", size);
484 while (sent < size) {
485 struct kwboot_block block;
489 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
492 last_block = (left <= blksz);
494 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
503 kwboot_progress(sent * 100 / size, '.');
507 kwboot_printv("Done\n");
516 kwboot_xmodem(int tty, const void *_img, size_t size)
518 const uint8_t *img = _img;
522 if (image_version(img) == 0)
523 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
525 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
527 kwboot_printv("Waiting 2s and flushing tty\n");
528 sleep(2); /* flush isn't effective without it */
529 tcflush(tty, TCIOFLUSH);
533 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
540 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
544 return kwboot_tty_send_char(tty, EOT);
548 kwboot_term_pipe(int in, int out, const char *quit, int *s)
551 char _buf[128], *buf = _buf;
553 nin = read(in, buf, sizeof(_buf));
560 for (i = 0; i < nin; i++) {
561 if (*buf == quit[*s]) {
568 if (kwboot_write(out, quit, *s) < 0)
575 if (kwboot_write(out, buf, nin) < 0)
582 kwboot_terminal(int tty)
585 const char *quit = "\34c";
586 struct termios otio, tio;
592 rc = tcgetattr(in, &otio);
596 rc = tcsetattr(in, TCSANOW, &tio);
603 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
604 quit[0]|0100, quit[1]);
616 nfds = nfds < tty ? tty : nfds;
620 nfds = nfds < in ? in : nfds;
623 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
627 if (FD_ISSET(tty, &rfds)) {
628 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
633 if (in >= 0 && FD_ISSET(in, &rfds)) {
634 rc = kwboot_term_pipe(in, tty, quit, &s);
638 } while (quit[s] != 0);
641 tcsetattr(in, TCSANOW, &otio);
648 kwboot_mmap_image(const char *path, size_t *size, int prot)
657 fd = open(path, O_RDONLY);
665 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
667 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
668 if (img == MAP_FAILED) {
677 munmap(img, st.st_size);
687 kwboot_img_csum8(void *_data, size_t size)
689 uint8_t *data = _data, csum;
691 for (csum = 0; size-- > 0; data++)
698 kwboot_img_patch_hdr(void *img, size_t size)
701 struct main_hdr_v1 *hdr;
703 size_t hdrsz = sizeof(*hdr);
714 image_ver = image_version(img);
715 if (image_ver != 0 && image_ver != 1) {
716 fprintf(stderr, "Invalid image header version\n");
722 hdrsz = sizeof(*hdr);
724 hdrsz = KWBHEADER_V1_SIZE(hdr);
731 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
732 if (csum != hdr->checksum) {
737 if (hdr->blockid == IBR_HDR_UART_ID) {
742 hdr->blockid = IBR_HDR_UART_ID;
744 if (image_ver == 0) {
745 struct main_hdr_v0 *hdr_v0 = img;
747 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
748 hdr_v0->nandpagesize = 0;
750 hdr_v0->srcaddr = hdr_v0->ext
751 ? sizeof(struct kwb_header)
755 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
763 kwboot_usage(FILE *stream, char *progname)
765 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
767 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
769 fprintf(stream, "\n");
771 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
772 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
774 " -D <image>: boot <image> without preamble (Dove)\n");
775 fprintf(stream, " -d: enter debug mode\n");
776 fprintf(stream, " -a: use timings for Armada XP\n");
777 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
778 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
780 " -o <block-timeo>: use specific xmodem block timeout\n");
781 fprintf(stream, "\n");
782 fprintf(stream, " -t: mini terminal\n");
783 fprintf(stream, "\n");
784 fprintf(stream, " -B <baud>: set baud rate\n");
785 fprintf(stream, "\n");
789 main(int argc, char **argv)
791 const char *ttypath, *imgpath;
792 int rv, rc, tty, term, prot, patch;
810 kwboot_verbose = isatty(STDOUT_FILENO);
813 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
819 bootmsg = kwboot_msg_boot;
829 debugmsg = kwboot_msg_debug;
841 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
842 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
846 msg_req_delay = atoi(optarg);
850 msg_rsp_timeo = atoi(optarg);
854 blk_rsp_timeo = atoi(optarg);
858 speed = kwboot_tty_speed(atoi(optarg));
870 if (!bootmsg && !term && !debugmsg)
873 if (patch && !imgpath)
876 if (argc - optind < 1)
879 ttypath = argv[optind++];
881 tty = kwboot_open_tty(ttypath, speed);
888 prot = PROT_READ | (patch ? PROT_WRITE : 0);
890 img = kwboot_mmap_image(imgpath, &size, prot);
898 rc = kwboot_img_patch_hdr(img, size);
900 fprintf(stderr, "%s: Invalid image.\n", imgpath);
906 rc = kwboot_debugmsg(tty, debugmsg);
911 } else if (bootmsg) {
912 rc = kwboot_bootmsg(tty, bootmsg);
920 rc = kwboot_xmodem(tty, img, size);
928 rc = kwboot_terminal(tty);
929 if (rc && !(errno == EINTR)) {
946 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));