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 = _now() + timeout;
417 rc = kwboot_tty_recv(fd, c, 1, timeout);
419 if (errno != ETIMEDOUT)
421 else if (allow_non_xm && *non_xm_print)
427 /* If received xmodem reply, end. */
428 if (_is_xm_reply(*c))
432 * If printing non-xmodem text output is allowed and such a byte
433 * was received, print it and increase receiving time.
434 * Otherwise decrease timeout by time elapsed.
437 recv_until = _now() + timeout;
442 timeout = recv_until - _now();
454 kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
465 rc = kwboot_tty_send(fd, block, sizeof(*block));
469 if (allow_non_xm && !*done_print) {
470 kwboot_progress(100, '.');
471 kwboot_printv("Done\n");
475 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
479 if (!allow_non_xm && c != ACK)
480 kwboot_progress(-1, '+');
481 } while (c == NAK && retries-- > 0);
507 kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
514 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
515 header ? "header" : "data", size);
520 while (sent < size) {
521 struct kwboot_block block;
525 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
528 last_block = (left <= blksz);
530 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
539 kwboot_progress(sent * 100 / size, '.');
543 kwboot_printv("Done\n");
552 kwboot_xmodem(int tty, const void *_img, size_t size)
554 const uint8_t *img = _img;
558 if (image_version(img) == 0)
559 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
561 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
563 kwboot_printv("Waiting 2s and flushing tty\n");
564 sleep(2); /* flush isn't effective without it */
565 tcflush(tty, TCIOFLUSH);
569 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
576 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
580 return kwboot_tty_send_char(tty, EOT);
584 kwboot_term_pipe(int in, int out, const char *quit, int *s)
587 char _buf[128], *buf = _buf;
589 nin = read(in, buf, sizeof(_buf));
596 for (i = 0; i < nin; i++) {
597 if (*buf == quit[*s]) {
604 if (kwboot_write(out, quit, *s) < 0)
611 if (kwboot_write(out, buf, nin) < 0)
618 kwboot_terminal(int tty)
621 const char *quit = "\34c";
622 struct termios otio, tio;
628 rc = tcgetattr(in, &otio);
632 rc = tcsetattr(in, TCSANOW, &tio);
639 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
640 quit[0]|0100, quit[1]);
652 nfds = nfds < tty ? tty : nfds;
656 nfds = nfds < in ? in : nfds;
659 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
663 if (FD_ISSET(tty, &rfds)) {
664 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
669 if (in >= 0 && FD_ISSET(in, &rfds)) {
670 rc = kwboot_term_pipe(in, tty, quit, &s);
674 } while (quit[s] != 0);
677 tcsetattr(in, TCSANOW, &otio);
684 kwboot_mmap_image(const char *path, size_t *size, int prot)
693 fd = open(path, O_RDONLY);
701 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
703 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
704 if (img == MAP_FAILED) {
713 munmap(img, st.st_size);
723 kwboot_img_csum8(void *_data, size_t size)
725 uint8_t *data = _data, csum;
727 for (csum = 0; size-- > 0; data++)
734 kwboot_img_patch_hdr(void *img, size_t size)
737 struct main_hdr_v1 *hdr;
739 size_t hdrsz = sizeof(*hdr);
750 image_ver = image_version(img);
751 if (image_ver != 0 && image_ver != 1) {
752 fprintf(stderr, "Invalid image header version\n");
758 hdrsz = sizeof(*hdr);
760 hdrsz = KWBHEADER_V1_SIZE(hdr);
767 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
768 if (csum != hdr->checksum) {
773 if (hdr->blockid == IBR_HDR_UART_ID) {
778 hdr->blockid = IBR_HDR_UART_ID;
780 if (image_ver == 0) {
781 struct main_hdr_v0 *hdr_v0 = img;
783 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
784 hdr_v0->nandpagesize = 0;
786 hdr_v0->srcaddr = hdr_v0->ext
787 ? sizeof(struct kwb_header)
791 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
799 kwboot_usage(FILE *stream, char *progname)
801 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
803 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
805 fprintf(stream, "\n");
807 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
808 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
810 " -D <image>: boot <image> without preamble (Dove)\n");
811 fprintf(stream, " -d: enter debug mode\n");
812 fprintf(stream, " -a: use timings for Armada XP\n");
813 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
814 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
816 " -o <block-timeo>: use specific xmodem block timeout\n");
817 fprintf(stream, "\n");
818 fprintf(stream, " -t: mini terminal\n");
819 fprintf(stream, "\n");
820 fprintf(stream, " -B <baud>: set baud rate\n");
821 fprintf(stream, "\n");
825 main(int argc, char **argv)
827 const char *ttypath, *imgpath;
828 int rv, rc, tty, term, prot, patch;
846 kwboot_verbose = isatty(STDOUT_FILENO);
849 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
855 bootmsg = kwboot_msg_boot;
865 debugmsg = kwboot_msg_debug;
877 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
878 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
882 msg_req_delay = atoi(optarg);
886 msg_rsp_timeo = atoi(optarg);
890 blk_rsp_timeo = atoi(optarg);
894 speed = kwboot_tty_speed(atoi(optarg));
906 if (!bootmsg && !term && !debugmsg)
909 if (patch && !imgpath)
912 if (argc - optind < 1)
915 ttypath = argv[optind++];
917 tty = kwboot_open_tty(ttypath, speed);
924 prot = PROT_READ | (patch ? PROT_WRITE : 0);
926 img = kwboot_mmap_image(imgpath, &size, prot);
934 rc = kwboot_img_patch_hdr(img, size);
936 fprintf(stderr, "%s: Invalid image.\n", imgpath);
942 rc = kwboot_debugmsg(tty, debugmsg);
947 } else if (bootmsg) {
948 rc = kwboot_bootmsg(tty, bootmsg);
956 rc = kwboot_xmodem(tty, img, size);
964 rc = kwboot_terminal(tty);
965 if (rc && !(errno == EINTR)) {
982 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));