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".
30 * Marvell BootROM UART Sensing
33 static unsigned char kwboot_msg_boot[] = {
34 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
37 static unsigned char kwboot_msg_debug[] = {
38 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
41 /* Defines known to work on Kirkwood */
42 #define KWBOOT_MSG_REQ_DELAY 10 /* ms */
43 #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
45 /* Defines known to work on Armada XP */
46 #define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
47 #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
53 #define SOH 1 /* sender start of block header */
54 #define EOT 4 /* sender end of block transfer */
55 #define ACK 6 /* target block ack */
56 #define NAK 21 /* target block negative ack */
57 #define CAN 24 /* target/sender transfer cancellation */
67 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
69 static int kwboot_verbose;
71 static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
72 static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
73 static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
76 kwboot_printv(const char *fmt, ...)
91 const char seq[] = { '-', '\\', '|', '/' };
95 if (state % div == 0) {
97 fputc(seq[state / div % sizeof(seq)], stdout);
113 __progress(int pct, char c)
115 const int width = 70;
116 static const char *nl = "";
119 if (pos % width == 0)
120 printf("%s%3d %% [", nl, pct);
128 while (pos++ < width)
138 kwboot_progress(int _pct, char c)
150 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
163 tv.tv_usec = timeo * 1000;
164 if (tv.tv_usec > 1000000) {
165 tv.tv_sec += tv.tv_usec / 1000000;
166 tv.tv_usec %= 1000000;
170 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
178 n = read(fd, buf, len);
182 buf = (char *)buf + n;
192 kwboot_tty_send(int fd, const void *buf, size_t len)
203 n = write(fd, buf, len);
207 buf = (char *)buf + n;
217 kwboot_tty_send_char(int fd, unsigned char c)
219 return kwboot_tty_send(fd, &c, 1);
223 kwboot_tty_speed(int baudrate)
242 kwboot_open_tty(const char *path, speed_t speed)
249 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
253 memset(&tio, 0, sizeof(tio));
256 tio.c_cflag = CREAD|CLOCAL|CS8;
259 tio.c_cc[VTIME] = 10;
261 cfsetospeed(&tio, speed);
262 cfsetispeed(&tio, speed);
264 rc = tcsetattr(fd, TCSANOW, &tio);
279 kwboot_bootmsg(int tty, void *msg)
286 kwboot_printv("Please reboot the target into UART boot mode...");
288 kwboot_printv("Sending boot message. Please reboot the target...");
291 rc = tcflush(tty, TCIOFLUSH);
295 for (count = 0; count < 128; count++) {
296 rc = kwboot_tty_send(tty, msg, 8);
298 usleep(msg_req_delay * 1000);
303 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
307 } while (rc || c != NAK);
315 kwboot_debugmsg(int tty, void *msg)
319 kwboot_printv("Sending debug message. Please reboot the target...");
324 rc = tcflush(tty, TCIOFLUSH);
328 rc = kwboot_tty_send(tty, msg, 8);
330 usleep(msg_req_delay * 1000);
334 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
346 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
347 size_t size, int pnum)
349 const size_t blksz = sizeof(block->data);
355 block->_pnum = ~block->pnum;
357 n = size < blksz ? size : blksz;
358 memcpy(&block->data[0], data, n);
359 memset(&block->data[n], 0, blksz - n);
362 for (i = 0; i < n; i++)
363 block->csum += block->data[i];
369 kwboot_xm_sendblock(int fd, struct kwboot_block *block)
376 rc = kwboot_tty_send(fd, block, sizeof(*block));
381 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
385 if (c != ACK && c != NAK && c != CAN)
388 } while (c != ACK && c != NAK && c != CAN);
391 kwboot_progress(-1, '+');
393 } while (c == NAK && retries-- > 0);
416 kwboot_xmodem(int tty, const void *_data, size_t size)
418 const uint8_t *data = _data;
419 int rc, pnum, N, err;
424 kwboot_printv("Sending boot image...\n");
426 sleep(2); /* flush isn't effective without it */
427 tcflush(tty, TCIOFLUSH);
430 struct kwboot_block block;
433 n = kwboot_xm_makeblock(&block,
442 rc = kwboot_xm_sendblock(tty, &block);
447 kwboot_progress(N * 100 / size, '.');
450 rc = kwboot_tty_send_char(tty, EOT);
457 kwboot_tty_send_char(tty, CAN);
463 kwboot_term_pipe(int in, int out, char *quit, int *s)
466 char _buf[128], *buf = _buf;
468 nin = read(in, buf, sizeof(_buf));
475 for (i = 0; i < nin; i++) {
476 if (*buf == quit[*s]) {
484 nout = write(out, quit, *s);
494 nout = write(out, buf, nin);
504 kwboot_terminal(int tty)
508 struct termios otio, tio;
514 rc = tcgetattr(in, &otio);
518 rc = tcsetattr(in, TCSANOW, &tio);
525 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
526 quit[0]|0100, quit[1]);
538 nfds = nfds < tty ? tty : nfds;
542 nfds = nfds < in ? in : nfds;
545 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
549 if (FD_ISSET(tty, &rfds)) {
550 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
555 if (FD_ISSET(in, &rfds)) {
556 rc = kwboot_term_pipe(in, tty, quit, &s);
560 } while (quit[s] != 0);
563 tcsetattr(in, TCSANOW, &otio);
570 kwboot_mmap_image(const char *path, size_t *size, int prot)
579 fd = open(path, O_RDONLY);
587 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
589 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
590 if (img == MAP_FAILED) {
599 munmap(img, st.st_size);
609 kwboot_img_csum8(void *_data, size_t size)
611 uint8_t *data = _data, csum;
613 for (csum = 0; size-- > 0; data++)
620 kwboot_img_patch_hdr(void *img, size_t size)
623 struct main_hdr_v1 *hdr;
625 size_t hdrsz = sizeof(*hdr);
636 image_ver = image_version(img);
637 if (image_ver != 0 && image_ver != 1) {
638 fprintf(stderr, "Invalid image header version\n");
644 hdrsz = sizeof(*hdr);
646 hdrsz = KWBHEADER_V1_SIZE(hdr);
653 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
654 if (csum != hdr->checksum) {
659 if (hdr->blockid == IBR_HDR_UART_ID) {
664 hdr->blockid = IBR_HDR_UART_ID;
666 if (image_ver == 0) {
667 struct main_hdr_v0 *hdr_v0 = img;
669 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
670 hdr_v0->nandpagesize = 0;
672 hdr_v0->srcaddr = hdr_v0->ext
673 ? sizeof(struct kwb_header)
677 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
685 kwboot_usage(FILE *stream, char *progname)
688 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
690 fprintf(stream, "\n");
692 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
693 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
695 " -D <image>: boot <image> without preamble (Dove)\n");
696 fprintf(stream, " -d: enter debug mode\n");
697 fprintf(stream, " -a: use timings for Armada XP\n");
698 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
699 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
701 " -o <block-timeo>: use specific xmodem block timeout\n");
702 fprintf(stream, "\n");
703 fprintf(stream, " -t: mini terminal\n");
704 fprintf(stream, "\n");
705 fprintf(stream, " -B <baud>: set baud rate\n");
706 fprintf(stream, "\n");
710 main(int argc, char **argv)
712 const char *ttypath, *imgpath;
713 int rv, rc, tty, term, prot, patch;
731 kwboot_verbose = isatty(STDOUT_FILENO);
734 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
740 bootmsg = kwboot_msg_boot;
750 debugmsg = kwboot_msg_debug;
762 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
763 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
767 msg_req_delay = atoi(optarg);
771 msg_rsp_timeo = atoi(optarg);
775 blk_rsp_timeo = atoi(optarg);
779 speed = kwboot_tty_speed(atoi(optarg));
791 if (!bootmsg && !term && !debugmsg)
794 if (patch && !imgpath)
797 if (argc - optind < 1)
800 ttypath = argv[optind++];
802 tty = kwboot_open_tty(ttypath, speed);
809 prot = PROT_READ | (patch ? PROT_WRITE : 0);
811 img = kwboot_mmap_image(imgpath, &size, prot);
819 rc = kwboot_img_patch_hdr(img, size);
821 fprintf(stderr, "%s: Invalid image.\n", imgpath);
827 rc = kwboot_debugmsg(tty, debugmsg);
832 } else if (bootmsg) {
833 rc = kwboot_bootmsg(tty, bootmsg);
841 rc = kwboot_xmodem(tty, img, size);
849 rc = kwboot_terminal(tty);
850 if (rc && !(errno == EINTR)) {
867 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));