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 _xm_reply_to_error(int c)
430 kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
432 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
433 uint64_t recv_until = _now() + timeout;
440 rc = kwboot_tty_recv(fd, c, 1, timeout);
442 if (errno != ETIMEDOUT)
444 else if (allow_non_xm && *non_xm_print)
450 /* If received xmodem reply, end. */
451 if (_is_xm_reply(*c))
455 * If printing non-xmodem text output is allowed and such a byte
456 * was received, print it and increase receiving time.
457 * Otherwise decrease timeout by time elapsed.
460 recv_until = _now() + timeout;
465 timeout = recv_until - _now();
477 kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
488 rc = kwboot_tty_send(fd, block, sizeof(*block));
492 if (allow_non_xm && !*done_print) {
493 kwboot_progress(100, '.');
494 kwboot_printv("Done\n");
498 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
502 if (!allow_non_xm && c != ACK)
503 kwboot_progress(-1, '+');
504 } while (c == NAK && retries-- > 0);
509 return _xm_reply_to_error(c);
513 kwboot_xm_finish(int fd)
518 kwboot_printv("Finishing transfer\n");
522 rc = kwboot_tty_send_char(fd, EOT);
526 rc = kwboot_xm_recv_reply(fd, &c, 0, NULL);
529 } while (c == NAK && retries-- > 0);
531 return _xm_reply_to_error(c);
535 kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
542 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
543 header ? "header" : "data", size);
548 while (sent < size) {
549 struct kwboot_block block;
553 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
556 last_block = (left <= blksz);
558 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
567 kwboot_progress(sent * 100 / size, '.');
571 kwboot_printv("Done\n");
580 kwboot_xmodem(int tty, const void *_img, size_t size)
582 const uint8_t *img = _img;
586 if (image_version(img) == 0)
587 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
589 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
591 kwboot_printv("Waiting 2s and flushing tty\n");
592 sleep(2); /* flush isn't effective without it */
593 tcflush(tty, TCIOFLUSH);
597 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
604 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
608 return kwboot_xm_finish(tty);
612 kwboot_term_pipe(int in, int out, const char *quit, int *s)
615 char _buf[128], *buf = _buf;
617 nin = read(in, buf, sizeof(_buf));
624 for (i = 0; i < nin; i++) {
625 if (*buf == quit[*s]) {
632 if (kwboot_write(out, quit, *s) < 0)
639 if (kwboot_write(out, buf, nin) < 0)
646 kwboot_terminal(int tty)
649 const char *quit = "\34c";
650 struct termios otio, tio;
656 rc = tcgetattr(in, &otio);
660 rc = tcsetattr(in, TCSANOW, &tio);
667 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
668 quit[0]|0100, quit[1]);
680 nfds = nfds < tty ? tty : nfds;
684 nfds = nfds < in ? in : nfds;
687 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
691 if (FD_ISSET(tty, &rfds)) {
692 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
697 if (in >= 0 && FD_ISSET(in, &rfds)) {
698 rc = kwboot_term_pipe(in, tty, quit, &s);
702 } while (quit[s] != 0);
705 tcsetattr(in, TCSANOW, &otio);
712 kwboot_mmap_image(const char *path, size_t *size)
721 fd = open(path, O_RDONLY);
729 img = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
730 if (img == MAP_FAILED) {
739 munmap(img, st.st_size);
749 kwboot_img_csum8(void *_data, size_t size)
751 uint8_t *data = _data, csum;
753 for (csum = 0; size-- > 0; data++)
760 kwboot_img_is_secure(void *img)
762 struct opt_hdr_v1 *ohdr;
764 for_each_opt_hdr_v1 (ohdr, img)
765 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
772 kwboot_img_patch_hdr(void *img, size_t size)
775 struct main_hdr_v1 *hdr;
778 size_t hdrsz = sizeof(*hdr);
790 image_ver = image_version(img);
791 if (image_ver != 0 && image_ver != 1) {
792 fprintf(stderr, "Invalid image header version\n");
798 hdrsz = sizeof(*hdr);
800 hdrsz = KWBHEADER_V1_SIZE(hdr);
807 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
808 if (csum != hdr->checksum) {
813 if (image_ver == 0) {
814 struct main_hdr_v0 *hdr_v0 = img;
816 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
817 hdr_v0->nandpagesize = 0;
820 srcaddr = le32_to_cpu(hdr->srcaddr);
822 switch (hdr->blockid) {
823 case IBR_HDR_SATA_ID:
828 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
831 case IBR_HDR_SDIO_ID:
832 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
836 if (srcaddr == 0xFFFFFFFF)
837 hdr->srcaddr = cpu_to_le32(hdrsz);
841 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
842 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
843 hdr->destaddr = cpu_to_le32(0x00800000);
844 hdr->execaddr = cpu_to_le32(0x00800000);
849 is_secure = kwboot_img_is_secure(img);
851 if (hdr->blockid != IBR_HDR_UART_ID) {
854 "Image has secure header with signature for non-UART booting\n");
859 kwboot_printv("Patching image boot signature to UART\n");
860 hdr->blockid = IBR_HDR_UART_ID;
863 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
871 kwboot_usage(FILE *stream, char *progname)
873 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
875 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
877 fprintf(stream, "\n");
879 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
881 " -D <image>: boot <image> without preamble (Dove)\n");
882 fprintf(stream, " -d: enter debug mode\n");
883 fprintf(stream, " -a: use timings for Armada XP\n");
884 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
885 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
887 " -o <block-timeo>: use specific xmodem block timeout\n");
888 fprintf(stream, "\n");
889 fprintf(stream, " -t: mini terminal\n");
890 fprintf(stream, "\n");
891 fprintf(stream, " -B <baud>: set baud rate\n");
892 fprintf(stream, "\n");
896 main(int argc, char **argv)
898 const char *ttypath, *imgpath;
899 int rv, rc, tty, term;
916 kwboot_verbose = isatty(STDOUT_FILENO);
919 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
925 bootmsg = kwboot_msg_boot;
935 debugmsg = kwboot_msg_debug;
939 /* nop, for backward compatibility */
947 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
948 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
952 msg_req_delay = atoi(optarg);
956 msg_rsp_timeo = atoi(optarg);
960 blk_rsp_timeo = atoi(optarg);
964 speed = kwboot_tty_speed(atoi(optarg));
976 if (!bootmsg && !term && !debugmsg)
979 if (argc - optind < 1)
982 ttypath = argv[optind++];
984 tty = kwboot_open_tty(ttypath, speed);
991 img = kwboot_mmap_image(imgpath, &size);
997 rc = kwboot_img_patch_hdr(img, size);
999 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1005 rc = kwboot_debugmsg(tty, debugmsg);
1010 } else if (bootmsg) {
1011 rc = kwboot_bootmsg(tty, bootmsg);
1019 rc = kwboot_xmodem(tty, img, size);
1027 rc = kwboot_terminal(tty);
1028 if (rc && !(errno == EINTR)) {
1045 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));