1 http://lists.denx.de/pipermail/u-boot/2012-May/125296.html
2 http://patchwork.ozlabs.org/patch/161566/
5 diff --git a/doc/kwboot.1 b/doc/kwboot.1
11 +.TH KWBOOT 1 "2012-05-19"
14 +kwboot \- Boot Marvell Kirkwood SoCs over a serial link.
17 +.RB [ "-b \fIimage\fP" ]
20 +.RB [ "-B \fIbaudrate\fP" ]
24 +The \fBmkimage\fP program boots boards based on Marvell's Kirkwood
25 +platform over their integrated UART. Boot image files will typically
26 +contain a second stage boot loader, such as U-Boot. The image file
27 +must conform to Marvell's BootROM firmware image format
28 +(\fIkwbimage\fP), created using a tool such as \fBmkimage\fP.
30 +Following power-up or a system reset, system BootROM code polls the
31 +UART for a brief period of time, sensing a handshake message which
32 +initiates an image upload. This program sends this boot message until
33 +it receives a positive acknowledgement. The image is transfered using
36 +Additionally, this program implements a minimal terminal mode, which
37 +can be used either standalone, or entered immediately following boot
38 +image transfer completion. This is often useful to catch early boot
39 +messages, or to manually interrupt a default boot procedure performed
40 +by the second-stage loader.
45 +.BI "\-b \fIimage\fP"
46 +Handshake; then upload file \fIimage\fP over \fITTY\fP.
48 +Note that for the encapsulated boot code to be executed, \fIimage\fP
49 +must be of type "UART boot" (0x69). Boot images of different types,
50 +such as backup images of vendor firmware downloaded from flash memory
51 +(type 0x8B), will not work (or not as expected). See \fB-p\fP for a
54 +This mode writes handshake status and upload progress indication to
59 +In combination with \fB-b\fP, patches the header in \fIimage\fP prior
60 +to upload, to "UART boot" type.
62 +This option attempts on-the-fly conversion of some none-UART image
63 +types, such as images which were originally formatted to be stored in
66 +Conversion is performed in memory. The contents of \fIimage\fP will
71 +Run a terminal program, connecting standard input and output to
74 +If used in combination with \fB-b\fP, terminal mode is entered
75 +immediately following a successful image upload.
77 +If standard I/O streams connect to a console, this mode will terminate
78 +after receiving 'ctrl-\\' followed by 'c' from console input.
81 +.BI "\-B \fIbaudrate\fP"
82 +Adjust the baud rate on \fITTY\fP. Default rate is 115200.
90 +Daniel Stodden <daniel.stodden@gmail.com>
92 +Luka Perkov <uboot@lukaperkov.net>
94 +David Purdy <david.c.purdy@gmail.com>
95 diff --git a/tools/Makefile b/tools/Makefile
96 index 8993fdd..8097d95 100644
99 @@ -72,6 +72,7 @@ BIN_FILES-$(CONFIG_SMDK5250) += mksmdk5250spl$(SFX)
100 BIN_FILES-$(CONFIG_MX28) += mxsboot$(SFX)
101 BIN_FILES-$(CONFIG_NETCONSOLE) += ncb$(SFX)
102 BIN_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1$(SFX)
103 +BIN_FILES-$(CONFIG_KIRKWOOD) += kwboot$(SFX)
105 # Source files which exist outside the tools directory
106 EXT_OBJ_FILES-$(CONFIG_BUILD_ENVCRC) += common/env_embedded.o
107 @@ -101,6 +102,7 @@ OBJ_FILES-$(CONFIG_NETCONSOLE) += ncb.o
108 NOPED_OBJ_FILES-y += os_support.o
109 OBJ_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1.o
110 NOPED_OBJ_FILES-y += ublimage.o
111 +OBJ_FILES-$(CONFIG_KIRKWOOD) += kwboot.o
113 # Don't build by default
115 @@ -234,6 +236,10 @@ $(obj)ncb$(SFX): $(obj)ncb.o
116 $(obj)ubsha1$(SFX): $(obj)os_support.o $(obj)sha1.o $(obj)ubsha1.o
117 $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
119 +$(obj)kwboot$(SFX): $(obj)kwboot.o
120 + $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
123 # Some of the tool objects need to be accessed from outside the tools directory
124 $(obj)%.o: $(SRCTREE)/common/%.c
125 $(HOSTCC) -g $(HOSTCFLAGS_NOPED) -c -o $@ $<
126 diff --git a/tools/kwboot.c b/tools/kwboot.c
128 index 0000000..e773f01
133 + * Boot a Marvell Kirkwood SoC, with Xmodem over UART0.
135 + * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
137 + * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
138 + * Integrated Controller: Functional Specifications" December 2,
139 + * 2008. Chapter 24.2 "BootROM Firmware".
151 +#include <termios.h>
152 +#include <sys/mman.h>
153 +#include <sys/stat.h>
155 +#include "kwbimage.h"
158 +#define PACKED __attribute((packed))
164 + * Marvell BootROM UART Sensing
167 +static unsigned char kwboot_msg_boot[] = {
168 + 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
171 +#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
172 +#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
178 +#define SOH 1 /* sender start of block header */
179 +#define EOT 4 /* sender end of block transfer */
180 +#define ACK 6 /* target block ack */
181 +#define NAK 21 /* target block negative ack */
182 +#define CAN 24 /* target/sender transfer cancellation */
184 +struct kwboot_block {
192 +#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
194 +static int kwboot_verbose;
197 +kwboot_printv(const char *fmt, ...)
201 + if (kwboot_verbose) {
212 + const char seq[] = { '-', '\\', '|', '/' };
214 + static int state, bs;
216 + if (state % div == 0) {
218 + fputc(seq[state / div % sizeof(seq)], stdout);
227 +kwboot_spinner(void)
229 + if (kwboot_verbose)
234 +__progress(int pct, char c)
236 + const int width = 70;
237 + static const char *nl = "";
240 + if (pos % width == 0)
241 + printf("%s%3d %% [", nl, pct);
249 + while (pos++ < width)
250 + fputc(' ', stdout);
259 +kwboot_progress(int _pct, char c)
266 + if (kwboot_verbose)
267 + __progress(pct, c);
271 +kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
284 + tv.tv_usec = timeo * 1000;
285 + if (tv.tv_usec > 1000000) {
286 + tv.tv_sec += tv.tv_usec / 1000000;
287 + tv.tv_usec %= 1000000;
291 + nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
299 + n = read(fd, buf, len);
303 + buf = (char *)buf + n;
313 +kwboot_tty_send(int fd, const void *buf, size_t len)
321 + n = write(fd, buf, len);
325 + buf = (char *)buf + n;
335 +kwboot_tty_send_char(int fd, unsigned char c)
337 + return kwboot_tty_send(fd, &c, 1);
341 +kwboot_tty_speed(int baudrate)
343 + switch (baudrate) {
360 +kwboot_open_tty(const char *path, speed_t speed)
363 + struct termios tio;
367 + fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
371 + memset(&tio, 0, sizeof(tio));
374 + tio.c_cflag = CREAD|CLOCAL|CS8;
376 + tio.c_cc[VMIN] = 1;
377 + tio.c_cc[VTIME] = 10;
379 + cfsetospeed(&tio, speed);
380 + cfsetispeed(&tio, speed);
382 + rc = tcsetattr(fd, TCSANOW, &tio);
397 +kwboot_bootmsg(int tty, void *msg)
402 + kwboot_printv("Sending boot message. Please reboot the target...");
405 + rc = tcflush(tty, TCIOFLUSH);
409 + rc = kwboot_tty_send(tty, msg, 8);
411 + usleep(KWBOOT_MSG_REQ_DELAY * 1000);
415 + rc = kwboot_tty_recv(tty, &c, 1, KWBOOT_MSG_RSP_TIMEO);
419 + } while (rc || c != NAK);
421 + kwboot_printv("\n");
427 +kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
428 + size_t size, int pnum)
430 + const size_t blksz = sizeof(block->data);
434 + block->pnum = pnum;
435 + block->_pnum = ~block->pnum;
437 + n = size < blksz ? size : blksz;
438 + memcpy(&block->data[0], data, n);
439 + memset(&block->data[n], 0, blksz - n);
442 + for (i = 0; i < n; i++)
443 + block->csum += block->data[i];
449 +kwboot_xm_sendblock(int fd, struct kwboot_block *block)
456 + rc = kwboot_tty_send(fd, block, sizeof(*block));
460 + rc = kwboot_tty_recv(fd, &c, 1, KWBOOT_BLK_RSP_TIMEO);
465 + kwboot_progress(-1, '+');
467 + } while (c == NAK && retries-- > 0);
490 +kwboot_xmodem(int tty, const void *_data, size_t size)
492 + const uint8_t *data = _data;
493 + int rc, pnum, N, err;
498 + kwboot_printv("Sending boot image...\n");
501 + struct kwboot_block block;
504 + n = kwboot_xm_makeblock(&block,
505 + data + N, size - N,
513 + rc = kwboot_xm_sendblock(tty, &block);
518 + kwboot_progress(N * 100 / size, '.');
521 + rc = kwboot_tty_send_char(tty, EOT);
528 + kwboot_tty_send_char(tty, CAN);
534 +kwboot_term_pipe(int in, int out, char *quit, int *s)
537 + char _buf[128], *buf = _buf;
539 + nin = read(in, buf, sizeof(buf));
546 + for (i = 0; i < nin; i++) {
547 + if (*buf == quit[*s]) {
555 + nout = write(out, quit, *s);
564 + nout = write(out, buf, nin);
574 +kwboot_terminal(int tty)
577 + char *quit = "\34c";
578 + struct termios otio, tio;
584 + rc = tcgetattr(in, &otio);
588 + rc = tcsetattr(in, TCSANOW, &tio);
591 + perror("tcsetattr");
595 + kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
596 + quit[0]|0100, quit[1]);
607 + FD_SET(tty, &rfds);
608 + nfds = nfds < tty ? tty : nfds;
612 + nfds = nfds < in ? in : nfds;
615 + nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
619 + if (FD_ISSET(tty, &rfds)) {
620 + rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
625 + if (FD_ISSET(in, &rfds)) {
626 + rc = kwboot_term_pipe(in, tty, quit, &s);
630 + } while (quit[s] != 0);
632 + tcsetattr(in, TCSANOW, &otio);
638 +kwboot_mmap_image(const char *path, size_t *size, int prot)
648 + fd = open(path, O_RDONLY);
652 + rc = fstat(fd, &st);
656 + flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
658 + img = mmap(NULL, st.st_size, prot, flags, fd, 0);
659 + if (img == MAP_FAILED) {
665 + *size = st.st_size;
668 + munmap(img, st.st_size);
678 +kwboot_img_csum8(void *_data, size_t size)
680 + uint8_t *data = _data, csum;
682 + for (csum = 0; size-- > 0; data++)
689 +kwboot_img_patch_hdr(void *img, size_t size)
694 + const size_t hdrsz = sizeof(*hdr);
699 + if (size < hdrsz) {
704 + csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checkSum;
705 + if (csum != hdr->checkSum) {
710 + if (hdr->blockid == IBR_HDR_UART_ID) {
715 + hdr->blockid = IBR_HDR_UART_ID;
717 + hdr->nandeccmode = IBR_HDR_ECC_DISABLED;
718 + hdr->nandpagesize = 0;
720 + hdr->srcaddr = hdr->ext
721 + ? sizeof(struct kwb_header)
724 + hdr->checkSum = kwboot_img_csum8(hdr, hdrsz) - csum;
732 +kwboot_usage(FILE *stream, char *progname)
735 + "Usage: %s -b <image> [ -p ] [ -t ] "
736 + "[-B <baud> ] <TTY>\n", progname);
737 + fprintf(stream, "\n");
738 + fprintf(stream, " -b <image>: boot <image>\n");
739 + fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
740 + fprintf(stream, "\n");
741 + fprintf(stream, " -t: mini terminal\n");
742 + fprintf(stream, "\n");
743 + fprintf(stream, " -B <baud>: set baud rate\n");
744 + fprintf(stream, "\n");
748 +main(int argc, char **argv)
750 + const char *ttypath, *imgpath;
751 + int rv, rc, tty, term, prot, patch;
767 + kwboot_verbose = isatty(STDOUT_FILENO);
770 + int c = getopt(argc, argv, "hb:ptB:");
776 + bootmsg = kwboot_msg_boot;
789 + speed = kwboot_tty_speed(atoi(optarg));
801 + if (!bootmsg && !term)
804 + if (patch && !imgpath)
807 + if (argc - optind < 1)
810 + ttypath = argv[optind++];
812 + tty = kwboot_open_tty(ttypath, speed);
819 + prot = PROT_READ | (patch ? PROT_WRITE : 0);
821 + img = kwboot_mmap_image(imgpath, &size, prot);
829 + rc = kwboot_img_patch_hdr(img, size);
831 + fprintf(stderr, "%s: Invalid image.\n", imgpath);
837 + rc = kwboot_bootmsg(tty, bootmsg);
845 + rc = kwboot_xmodem(tty, img, size);
853 + rc = kwboot_terminal(tty);
854 + if (rc && !(errno == EINTR)) {
855 + perror("terminal");
871 + kwboot_usage(rv ? stderr : stdout, basename(argv[0]));