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 */
68 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
70 static int kwboot_verbose;
72 static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
73 static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
74 static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
77 kwboot_write(int fd, const char *buf, size_t len)
82 ssize_t wr = write(fd, buf + tot, len - tot);
94 kwboot_printv(const char *fmt, ...)
109 const char seq[] = { '-', '\\', '|', '/' };
111 static int state, bs;
113 if (state % div == 0) {
115 fputc(seq[state / div % sizeof(seq)], stdout);
131 __progress(int pct, char c)
133 const int width = 70;
134 static const char *nl = "";
137 if (pos % width == 0)
138 printf("%s%3d %% [", nl, pct);
146 while (pos++ < width)
156 kwboot_progress(int _pct, char c)
168 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
181 tv.tv_usec = timeo * 1000;
182 if (tv.tv_usec > 1000000) {
183 tv.tv_sec += tv.tv_usec / 1000000;
184 tv.tv_usec %= 1000000;
188 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
196 n = read(fd, buf, len);
200 buf = (char *)buf + n;
210 kwboot_tty_send(int fd, const void *buf, size_t len)
215 if (kwboot_write(fd, buf, len) < 0)
222 kwboot_tty_send_char(int fd, unsigned char c)
224 return kwboot_tty_send(fd, &c, 1);
228 kwboot_tty_speed(int baudrate)
247 kwboot_open_tty(const char *path, speed_t speed)
254 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
258 memset(&tio, 0, sizeof(tio));
261 tio.c_cflag = CREAD|CLOCAL|CS8;
264 tio.c_cc[VTIME] = 10;
266 cfsetospeed(&tio, speed);
267 cfsetispeed(&tio, speed);
269 rc = tcsetattr(fd, TCSANOW, &tio);
284 kwboot_bootmsg(int tty, void *msg)
291 kwboot_printv("Please reboot the target into UART boot mode...");
293 kwboot_printv("Sending boot message. Please reboot the target...");
296 rc = tcflush(tty, TCIOFLUSH);
300 for (count = 0; count < 128; count++) {
301 rc = kwboot_tty_send(tty, msg, 8);
303 usleep(msg_req_delay * 1000);
308 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
312 } while (rc || c != NAK);
320 kwboot_debugmsg(int tty, void *msg)
324 kwboot_printv("Sending debug message. Please reboot the target...");
329 rc = tcflush(tty, TCIOFLUSH);
333 rc = kwboot_tty_send(tty, msg, 8);
335 usleep(msg_req_delay * 1000);
339 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
351 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
352 size_t size, int pnum)
354 const size_t blksz = sizeof(block->data);
360 block->_pnum = ~block->pnum;
362 n = size < blksz ? size : blksz;
363 memcpy(&block->data[0], data, n);
364 memset(&block->data[n], 0, blksz - n);
367 for (i = 0; i < n; i++)
368 block->csum += block->data[i];
374 kwboot_xm_sendblock(int fd, struct kwboot_block *block)
381 rc = kwboot_tty_send(fd, block, sizeof(*block));
386 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
388 if (errno != ETIMEDOUT)
393 if (c != ACK && c != NAK && c != CAN)
396 } while (c != ACK && c != NAK && c != CAN);
399 kwboot_progress(-1, '+');
401 } while (c == NAK && retries-- > 0);
424 kwboot_xmodem(int tty, const void *_data, size_t size)
426 const uint8_t *data = _data;
427 int rc, pnum, N, err;
432 kwboot_printv("Sending boot image...\n");
434 sleep(2); /* flush isn't effective without it */
435 tcflush(tty, TCIOFLUSH);
438 struct kwboot_block block;
441 n = kwboot_xm_makeblock(&block,
450 rc = kwboot_xm_sendblock(tty, &block);
455 kwboot_progress(N * 100 / size, '.');
458 rc = kwboot_tty_send_char(tty, EOT);
465 kwboot_tty_send_char(tty, CAN);
471 kwboot_term_pipe(int in, int out, const char *quit, int *s)
474 char _buf[128], *buf = _buf;
476 nin = read(in, buf, sizeof(_buf));
483 for (i = 0; i < nin; i++) {
484 if (*buf == quit[*s]) {
491 if (kwboot_write(out, quit, *s) < 0)
498 if (kwboot_write(out, buf, nin) < 0)
505 kwboot_terminal(int tty)
508 const char *quit = "\34c";
509 struct termios otio, tio;
515 rc = tcgetattr(in, &otio);
519 rc = tcsetattr(in, TCSANOW, &tio);
526 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
527 quit[0]|0100, quit[1]);
539 nfds = nfds < tty ? tty : nfds;
543 nfds = nfds < in ? in : nfds;
546 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
550 if (FD_ISSET(tty, &rfds)) {
551 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
556 if (in >= 0 && FD_ISSET(in, &rfds)) {
557 rc = kwboot_term_pipe(in, tty, quit, &s);
561 } while (quit[s] != 0);
564 tcsetattr(in, TCSANOW, &otio);
571 kwboot_mmap_image(const char *path, size_t *size, int prot)
580 fd = open(path, O_RDONLY);
588 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
590 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
591 if (img == MAP_FAILED) {
600 munmap(img, st.st_size);
610 kwboot_img_csum8(void *_data, size_t size)
612 uint8_t *data = _data, csum;
614 for (csum = 0; size-- > 0; data++)
621 kwboot_img_patch_hdr(void *img, size_t size)
624 struct main_hdr_v1 *hdr;
626 size_t hdrsz = sizeof(*hdr);
637 image_ver = image_version(img);
638 if (image_ver != 0 && image_ver != 1) {
639 fprintf(stderr, "Invalid image header version\n");
645 hdrsz = sizeof(*hdr);
647 hdrsz = KWBHEADER_V1_SIZE(hdr);
654 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
655 if (csum != hdr->checksum) {
660 if (hdr->blockid == IBR_HDR_UART_ID) {
665 hdr->blockid = IBR_HDR_UART_ID;
667 if (image_ver == 0) {
668 struct main_hdr_v0 *hdr_v0 = img;
670 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
671 hdr_v0->nandpagesize = 0;
673 hdr_v0->srcaddr = hdr_v0->ext
674 ? sizeof(struct kwb_header)
678 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
686 kwboot_usage(FILE *stream, char *progname)
688 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
690 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
692 fprintf(stream, "\n");
694 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
695 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
697 " -D <image>: boot <image> without preamble (Dove)\n");
698 fprintf(stream, " -d: enter debug mode\n");
699 fprintf(stream, " -a: use timings for Armada XP\n");
700 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
701 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
703 " -o <block-timeo>: use specific xmodem block timeout\n");
704 fprintf(stream, "\n");
705 fprintf(stream, " -t: mini terminal\n");
706 fprintf(stream, "\n");
707 fprintf(stream, " -B <baud>: set baud rate\n");
708 fprintf(stream, "\n");
712 main(int argc, char **argv)
714 const char *ttypath, *imgpath;
715 int rv, rc, tty, term, prot, patch;
733 kwboot_verbose = isatty(STDOUT_FILENO);
736 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
742 bootmsg = kwboot_msg_boot;
752 debugmsg = kwboot_msg_debug;
764 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
765 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
769 msg_req_delay = atoi(optarg);
773 msg_rsp_timeo = atoi(optarg);
777 blk_rsp_timeo = atoi(optarg);
781 speed = kwboot_tty_speed(atoi(optarg));
793 if (!bootmsg && !term && !debugmsg)
796 if (patch && !imgpath)
799 if (argc - optind < 1)
802 ttypath = argv[optind++];
804 tty = kwboot_open_tty(ttypath, speed);
811 prot = PROT_READ | (patch ? PROT_WRITE : 0);
813 img = kwboot_mmap_image(imgpath, &size, prot);
821 rc = kwboot_img_patch_hdr(img, size);
823 fprintf(stderr, "%s: Invalid image.\n", imgpath);
829 rc = kwboot_debugmsg(tty, debugmsg);
834 } else if (bootmsg) {
835 rc = kwboot_bootmsg(tty, bootmsg);
843 rc = kwboot_xmodem(tty, img, size);
851 rc = kwboot_terminal(tty);
852 if (rc && !(errno == EINTR)) {
869 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));