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".
32 * Marvell BootROM UART Sensing
35 static unsigned char kwboot_msg_boot[] = {
36 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
39 static unsigned char kwboot_msg_debug[] = {
40 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
43 /* Defines known to work on Kirkwood */
44 #define KWBOOT_MSG_REQ_DELAY 10 /* ms */
45 #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
47 /* Defines known to work on Armada XP */
48 #define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
49 #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
55 #define SOH 1 /* sender start of block header */
56 #define EOT 4 /* sender end of block transfer */
57 #define ACK 6 /* target block ack */
58 #define NAK 21 /* target block negative ack */
59 #define CAN 24 /* target/sender transfer cancellation */
61 #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
67 uint8_t data[KWBOOT_XM_BLKSZ];
71 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
72 #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
74 static int kwboot_verbose;
76 static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
77 static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
78 static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
81 kwboot_write(int fd, const char *buf, size_t len)
86 ssize_t wr = write(fd, buf + tot, len - tot);
98 kwboot_printv(const char *fmt, ...)
102 if (kwboot_verbose) {
113 const char seq[] = { '-', '\\', '|', '/' };
115 static int state, bs;
117 if (state % div == 0) {
119 fputc(seq[state / div % sizeof(seq)], stdout);
135 __progress(int pct, char c)
137 const int width = 70;
138 static const char *nl = "";
141 if (pos % width == 0)
142 printf("%s%3d %% [", nl, pct);
147 pos = (pos + 1) % width;
150 while (pos && pos++ < width)
162 kwboot_progress(int _pct, char c)
177 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
190 tv.tv_usec = timeo * 1000;
191 if (tv.tv_usec > 1000000) {
192 tv.tv_sec += tv.tv_usec / 1000000;
193 tv.tv_usec %= 1000000;
197 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
205 n = read(fd, buf, len);
209 buf = (char *)buf + n;
219 kwboot_tty_send(int fd, const void *buf, size_t len)
224 if (kwboot_write(fd, buf, len) < 0)
231 kwboot_tty_send_char(int fd, unsigned char c)
233 return kwboot_tty_send(fd, &c, 1);
237 kwboot_tty_speed(int baudrate)
256 kwboot_open_tty(const char *path, speed_t speed)
263 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
267 memset(&tio, 0, sizeof(tio));
270 tio.c_cflag = CREAD|CLOCAL|CS8;
273 tio.c_cc[VTIME] = 10;
275 cfsetospeed(&tio, speed);
276 cfsetispeed(&tio, speed);
278 rc = tcsetattr(fd, TCSANOW, &tio);
293 kwboot_bootmsg(int tty, void *msg)
300 kwboot_printv("Please reboot the target into UART boot mode...");
302 kwboot_printv("Sending boot message. Please reboot the target...");
305 rc = tcflush(tty, TCIOFLUSH);
309 for (count = 0; count < 128; count++) {
310 rc = kwboot_tty_send(tty, msg, 8);
312 usleep(msg_req_delay * 1000);
317 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
321 } while (rc || c != NAK);
329 kwboot_debugmsg(int tty, void *msg)
333 kwboot_printv("Sending debug message. Please reboot the target...");
338 rc = tcflush(tty, TCIOFLUSH);
342 rc = kwboot_tty_send(tty, msg, 8);
344 usleep(msg_req_delay * 1000);
348 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
360 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
361 size_t size, int pnum)
367 block->_pnum = ~block->pnum;
369 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
370 memcpy(&block->data[0], data, n);
371 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
374 for (i = 0; i < n; i++)
375 block->csum += block->data[i];
385 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
386 static int err_print;
389 perror("clock_gettime() does not work");
393 /* this will just make the timeout not work */
397 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
403 return c == ACK || c == NAK || c == CAN;
407 kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
409 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
410 uint64_t recv_until = 0;
416 rc = kwboot_tty_recv(fd, c, 1, timeout);
418 if (errno != ETIMEDOUT)
420 else if (recv_until && recv_until < _now())
426 /* If received xmodem reply, end. */
427 if (_is_xm_reply(*c))
431 * If printing non-xmodem text output is allowed and such a byte
432 * was received, print it and increase receiving time.
435 recv_until = _now() + timeout;
446 kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
457 rc = kwboot_tty_send(fd, block, sizeof(*block));
461 if (allow_non_xm && !*done_print) {
462 kwboot_progress(100, '.');
463 kwboot_printv("Done\n");
467 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
471 if (!allow_non_xm && c != ACK)
472 kwboot_progress(-1, '+');
473 } while (c == NAK && retries-- > 0);
499 kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
506 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
507 header ? "header" : "data", size);
512 while (sent < size) {
513 struct kwboot_block block;
517 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
520 last_block = (left <= blksz);
522 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
531 kwboot_progress(sent * 100 / size, '.');
535 kwboot_printv("Done\n");
544 kwboot_xmodem(int tty, const void *_img, size_t size)
546 const uint8_t *img = _img;
550 if (image_version(img) == 0)
551 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
553 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
555 kwboot_printv("Waiting 2s and flushing tty\n");
556 sleep(2); /* flush isn't effective without it */
557 tcflush(tty, TCIOFLUSH);
561 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
568 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
572 return kwboot_tty_send_char(tty, EOT);
576 kwboot_term_pipe(int in, int out, const char *quit, int *s)
579 char _buf[128], *buf = _buf;
581 nin = read(in, buf, sizeof(_buf));
588 for (i = 0; i < nin; i++) {
589 if (*buf == quit[*s]) {
596 if (kwboot_write(out, quit, *s) < 0)
603 if (kwboot_write(out, buf, nin) < 0)
610 kwboot_terminal(int tty)
613 const char *quit = "\34c";
614 struct termios otio, tio;
620 rc = tcgetattr(in, &otio);
624 rc = tcsetattr(in, TCSANOW, &tio);
631 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
632 quit[0]|0100, quit[1]);
644 nfds = nfds < tty ? tty : nfds;
648 nfds = nfds < in ? in : nfds;
651 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
655 if (FD_ISSET(tty, &rfds)) {
656 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
661 if (in >= 0 && FD_ISSET(in, &rfds)) {
662 rc = kwboot_term_pipe(in, tty, quit, &s);
666 } while (quit[s] != 0);
669 tcsetattr(in, TCSANOW, &otio);
676 kwboot_mmap_image(const char *path, size_t *size, int prot)
685 fd = open(path, O_RDONLY);
693 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
695 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
696 if (img == MAP_FAILED) {
705 munmap(img, st.st_size);
715 kwboot_img_csum8(void *_data, size_t size)
717 uint8_t *data = _data, csum;
719 for (csum = 0; size-- > 0; data++)
726 kwboot_img_patch_hdr(void *img, size_t size)
729 struct main_hdr_v1 *hdr;
731 size_t hdrsz = sizeof(*hdr);
742 image_ver = image_version(img);
743 if (image_ver != 0 && image_ver != 1) {
744 fprintf(stderr, "Invalid image header version\n");
750 hdrsz = sizeof(*hdr);
752 hdrsz = KWBHEADER_V1_SIZE(hdr);
759 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
760 if (csum != hdr->checksum) {
765 if (hdr->blockid == IBR_HDR_UART_ID) {
770 hdr->blockid = IBR_HDR_UART_ID;
772 if (image_ver == 0) {
773 struct main_hdr_v0 *hdr_v0 = img;
775 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
776 hdr_v0->nandpagesize = 0;
778 hdr_v0->srcaddr = hdr_v0->ext
779 ? sizeof(struct kwb_header)
783 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
791 kwboot_usage(FILE *stream, char *progname)
793 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
795 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
797 fprintf(stream, "\n");
799 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
800 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
802 " -D <image>: boot <image> without preamble (Dove)\n");
803 fprintf(stream, " -d: enter debug mode\n");
804 fprintf(stream, " -a: use timings for Armada XP\n");
805 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
806 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
808 " -o <block-timeo>: use specific xmodem block timeout\n");
809 fprintf(stream, "\n");
810 fprintf(stream, " -t: mini terminal\n");
811 fprintf(stream, "\n");
812 fprintf(stream, " -B <baud>: set baud rate\n");
813 fprintf(stream, "\n");
817 main(int argc, char **argv)
819 const char *ttypath, *imgpath;
820 int rv, rc, tty, term, prot, patch;
838 kwboot_verbose = isatty(STDOUT_FILENO);
841 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
847 bootmsg = kwboot_msg_boot;
857 debugmsg = kwboot_msg_debug;
869 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
870 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
874 msg_req_delay = atoi(optarg);
878 msg_rsp_timeo = atoi(optarg);
882 blk_rsp_timeo = atoi(optarg);
886 speed = kwboot_tty_speed(atoi(optarg));
898 if (!bootmsg && !term && !debugmsg)
901 if (patch && !imgpath)
904 if (argc - optind < 1)
907 ttypath = argv[optind++];
909 tty = kwboot_open_tty(ttypath, speed);
916 prot = PROT_READ | (patch ? PROT_WRITE : 0);
918 img = kwboot_mmap_image(imgpath, &size, prot);
926 rc = kwboot_img_patch_hdr(img, size);
928 fprintf(stderr, "%s: Invalid image.\n", imgpath);
934 rc = kwboot_debugmsg(tty, debugmsg);
939 } else if (bootmsg) {
940 rc = kwboot_bootmsg(tty, bootmsg);
948 rc = kwboot_xmodem(tty, img, size);
956 rc = kwboot_terminal(tty);
957 if (rc && !(errno == EINTR)) {
974 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));