tools: kwboot: Fix printing progress
[platform/kernel/u-boot.git] / tools / kwboot.c
1 /*
2  * Boot a Marvell SoC, with Xmodem over UART0.
3  *  supports Kirkwood, Dove, Armada 370, Armada XP
4  *
5  * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
6  *
7  * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
8  *   Integrated Controller: Functional Specifications" December 2,
9  *   2008. Chapter 24.2 "BootROM Firmware".
10  */
11
12 #include "kwbimage.h"
13 #include "mkimage.h"
14 #include "version.h"
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <image.h>
21 #include <libgen.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <termios.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29
30 /*
31  * Marvell BootROM UART Sensing
32  */
33
34 static unsigned char kwboot_msg_boot[] = {
35         0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
36 };
37
38 static unsigned char kwboot_msg_debug[] = {
39         0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
40 };
41
42 /* Defines known to work on Kirkwood */
43 #define KWBOOT_MSG_REQ_DELAY    10 /* ms */
44 #define KWBOOT_MSG_RSP_TIMEO    50 /* ms */
45
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 */
49
50 /*
51  * Xmodem Transfers
52  */
53
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 */
59
60 struct kwboot_block {
61         uint8_t soh;
62         uint8_t pnum;
63         uint8_t _pnum;
64         uint8_t data[128];
65         uint8_t csum;
66 } __packed;
67
68 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
69
70 static int kwboot_verbose;
71
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;
75
76 static ssize_t
77 kwboot_write(int fd, const char *buf, size_t len)
78 {
79         size_t tot = 0;
80
81         while (tot < len) {
82                 ssize_t wr = write(fd, buf + tot, len - tot);
83
84                 if (wr < 0)
85                         return -1;
86
87                 tot += wr;
88         }
89
90         return tot;
91 }
92
93 static void
94 kwboot_printv(const char *fmt, ...)
95 {
96         va_list ap;
97
98         if (kwboot_verbose) {
99                 va_start(ap, fmt);
100                 vprintf(fmt, ap);
101                 va_end(ap);
102                 fflush(stdout);
103         }
104 }
105
106 static void
107 __spinner(void)
108 {
109         const char seq[] = { '-', '\\', '|', '/' };
110         const int div = 8;
111         static int state, bs;
112
113         if (state % div == 0) {
114                 fputc(bs, stdout);
115                 fputc(seq[state / div % sizeof(seq)], stdout);
116                 fflush(stdout);
117         }
118
119         bs = '\b';
120         state++;
121 }
122
123 static void
124 kwboot_spinner(void)
125 {
126         if (kwboot_verbose)
127                 __spinner();
128 }
129
130 static void
131 __progress(int pct, char c)
132 {
133         const int width = 70;
134         static const char *nl = "";
135         static int pos;
136
137         if (pos % width == 0)
138                 printf("%s%3d %% [", nl, pct);
139
140         fputc(c, stdout);
141
142         nl = "]\n";
143         pos = (pos + 1) % width;
144
145         if (pct == 100) {
146                 while (pos && pos++ < width)
147                         fputc(' ', stdout);
148                 fputs(nl, stdout);
149                 nl = "";
150                 pos = 0;
151         }
152
153         fflush(stdout);
154
155 }
156
157 static void
158 kwboot_progress(int _pct, char c)
159 {
160         static int pct;
161
162         if (_pct != -1)
163                 pct = _pct;
164
165         if (kwboot_verbose)
166                 __progress(pct, c);
167
168         if (pct == 100)
169                 pct = 0;
170 }
171
172 static int
173 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
174 {
175         int rc, nfds;
176         fd_set rfds;
177         struct timeval tv;
178         ssize_t n;
179
180         rc = -1;
181
182         FD_ZERO(&rfds);
183         FD_SET(fd, &rfds);
184
185         tv.tv_sec = 0;
186         tv.tv_usec = timeo * 1000;
187         if (tv.tv_usec > 1000000) {
188                 tv.tv_sec += tv.tv_usec / 1000000;
189                 tv.tv_usec %= 1000000;
190         }
191
192         do {
193                 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
194                 if (nfds < 0)
195                         goto out;
196                 if (!nfds) {
197                         errno = ETIMEDOUT;
198                         goto out;
199                 }
200
201                 n = read(fd, buf, len);
202                 if (n <= 0)
203                         goto out;
204
205                 buf = (char *)buf + n;
206                 len -= n;
207         } while (len > 0);
208
209         rc = 0;
210 out:
211         return rc;
212 }
213
214 static int
215 kwboot_tty_send(int fd, const void *buf, size_t len)
216 {
217         if (!buf)
218                 return 0;
219
220         if (kwboot_write(fd, buf, len) < 0)
221                 return -1;
222
223         return tcdrain(fd);
224 }
225
226 static int
227 kwboot_tty_send_char(int fd, unsigned char c)
228 {
229         return kwboot_tty_send(fd, &c, 1);
230 }
231
232 static speed_t
233 kwboot_tty_speed(int baudrate)
234 {
235         switch (baudrate) {
236         case 115200:
237                 return B115200;
238         case 57600:
239                 return B57600;
240         case 38400:
241                 return B38400;
242         case 19200:
243                 return B19200;
244         case 9600:
245                 return B9600;
246         }
247
248         return -1;
249 }
250
251 static int
252 kwboot_open_tty(const char *path, speed_t speed)
253 {
254         int rc, fd;
255         struct termios tio;
256
257         rc = -1;
258
259         fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
260         if (fd < 0)
261                 goto out;
262
263         memset(&tio, 0, sizeof(tio));
264
265         tio.c_iflag = 0;
266         tio.c_cflag = CREAD|CLOCAL|CS8;
267
268         tio.c_cc[VMIN] = 1;
269         tio.c_cc[VTIME] = 10;
270
271         cfsetospeed(&tio, speed);
272         cfsetispeed(&tio, speed);
273
274         rc = tcsetattr(fd, TCSANOW, &tio);
275         if (rc)
276                 goto out;
277
278         rc = fd;
279 out:
280         if (rc < 0) {
281                 if (fd >= 0)
282                         close(fd);
283         }
284
285         return rc;
286 }
287
288 static int
289 kwboot_bootmsg(int tty, void *msg)
290 {
291         int rc;
292         char c;
293         int count;
294
295         if (msg == NULL)
296                 kwboot_printv("Please reboot the target into UART boot mode...");
297         else
298                 kwboot_printv("Sending boot message. Please reboot the target...");
299
300         do {
301                 rc = tcflush(tty, TCIOFLUSH);
302                 if (rc)
303                         break;
304
305                 for (count = 0; count < 128; count++) {
306                         rc = kwboot_tty_send(tty, msg, 8);
307                         if (rc) {
308                                 usleep(msg_req_delay * 1000);
309                                 continue;
310                         }
311                 }
312
313                 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
314
315                 kwboot_spinner();
316
317         } while (rc || c != NAK);
318
319         kwboot_printv("\n");
320
321         return rc;
322 }
323
324 static int
325 kwboot_debugmsg(int tty, void *msg)
326 {
327         int rc;
328
329         kwboot_printv("Sending debug message. Please reboot the target...");
330
331         do {
332                 char buf[16];
333
334                 rc = tcflush(tty, TCIOFLUSH);
335                 if (rc)
336                         break;
337
338                 rc = kwboot_tty_send(tty, msg, 8);
339                 if (rc) {
340                         usleep(msg_req_delay * 1000);
341                         continue;
342                 }
343
344                 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
345
346                 kwboot_spinner();
347
348         } while (rc);
349
350         kwboot_printv("\n");
351
352         return rc;
353 }
354
355 static size_t
356 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
357                     size_t size, int pnum)
358 {
359         const size_t blksz = sizeof(block->data);
360         size_t i, n;
361
362         block->soh = SOH;
363         block->pnum = pnum;
364         block->_pnum = ~block->pnum;
365
366         n = size < blksz ? size : blksz;
367         memcpy(&block->data[0], data, n);
368         memset(&block->data[n], 0, blksz - n);
369
370         block->csum = 0;
371         for (i = 0; i < n; i++)
372                 block->csum += block->data[i];
373
374         return n;
375 }
376
377 static int
378 kwboot_xm_sendblock(int fd, struct kwboot_block *block)
379 {
380         int rc, retries;
381         char c;
382
383         retries = 16;
384         do {
385                 rc = kwboot_tty_send(fd, block, sizeof(*block));
386                 if (rc)
387                         return rc;
388
389                 do {
390                         rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
391                         if (rc) {
392                                 if (errno != ETIMEDOUT)
393                                         return rc;
394                                 c = NAK;
395                         }
396
397                         if (c != ACK && c != NAK && c != CAN)
398                                 printf("%c", c);
399
400                 } while (c != ACK && c != NAK && c != CAN);
401
402                 if (c != ACK)
403                         kwboot_progress(-1, '+');
404
405         } while (c == NAK && retries-- > 0);
406
407         rc = -1;
408
409         switch (c) {
410         case ACK:
411                 rc = 0;
412                 break;
413         case NAK:
414                 errno = EBADMSG;
415                 break;
416         case CAN:
417                 errno = ECANCELED;
418                 break;
419         default:
420                 errno = EPROTO;
421                 break;
422         }
423
424         return rc;
425 }
426
427 static int
428 kwboot_xmodem(int tty, const void *_data, size_t size)
429 {
430         const uint8_t *data = _data;
431         int rc, pnum, N, err;
432
433         pnum = 1;
434         N = 0;
435
436         kwboot_printv("Sending boot image...\n");
437
438         sleep(2); /* flush isn't effective without it */
439         tcflush(tty, TCIOFLUSH);
440
441         do {
442                 struct kwboot_block block;
443                 int n;
444
445                 n = kwboot_xm_makeblock(&block,
446                                         data + N, size - N,
447                                         pnum++);
448                 if (!n)
449                         break;
450
451                 rc = kwboot_xm_sendblock(tty, &block);
452                 if (rc)
453                         goto out;
454
455                 N += n;
456                 kwboot_progress(N * 100 / size, '.');
457         } while (1);
458
459         rc = kwboot_tty_send_char(tty, EOT);
460
461 out:
462         return rc;
463
464 can:
465         err = errno;
466         kwboot_tty_send_char(tty, CAN);
467         errno = err;
468         goto out;
469 }
470
471 static int
472 kwboot_term_pipe(int in, int out, const char *quit, int *s)
473 {
474         ssize_t nin;
475         char _buf[128], *buf = _buf;
476
477         nin = read(in, buf, sizeof(_buf));
478         if (nin <= 0)
479                 return -1;
480
481         if (quit) {
482                 int i;
483
484                 for (i = 0; i < nin; i++) {
485                         if (*buf == quit[*s]) {
486                                 (*s)++;
487                                 if (!quit[*s])
488                                         return 0;
489                                 buf++;
490                                 nin--;
491                         } else {
492                                 if (kwboot_write(out, quit, *s) < 0)
493                                         return -1;
494                                 *s = 0;
495                         }
496                 }
497         }
498
499         if (kwboot_write(out, buf, nin) < 0)
500                 return -1;
501
502         return 0;
503 }
504
505 static int
506 kwboot_terminal(int tty)
507 {
508         int rc, in, s;
509         const char *quit = "\34c";
510         struct termios otio, tio;
511
512         rc = -1;
513
514         in = STDIN_FILENO;
515         if (isatty(in)) {
516                 rc = tcgetattr(in, &otio);
517                 if (!rc) {
518                         tio = otio;
519                         cfmakeraw(&tio);
520                         rc = tcsetattr(in, TCSANOW, &tio);
521                 }
522                 if (rc) {
523                         perror("tcsetattr");
524                         goto out;
525                 }
526
527                 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
528                               quit[0]|0100, quit[1]);
529         } else
530                 in = -1;
531
532         rc = 0;
533         s = 0;
534
535         do {
536                 fd_set rfds;
537                 int nfds = 0;
538
539                 FD_SET(tty, &rfds);
540                 nfds = nfds < tty ? tty : nfds;
541
542                 if (in >= 0) {
543                         FD_SET(in, &rfds);
544                         nfds = nfds < in ? in : nfds;
545                 }
546
547                 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
548                 if (nfds < 0)
549                         break;
550
551                 if (FD_ISSET(tty, &rfds)) {
552                         rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
553                         if (rc)
554                                 break;
555                 }
556
557                 if (in >= 0 && FD_ISSET(in, &rfds)) {
558                         rc = kwboot_term_pipe(in, tty, quit, &s);
559                         if (rc)
560                                 break;
561                 }
562         } while (quit[s] != 0);
563
564         if (in >= 0)
565                 tcsetattr(in, TCSANOW, &otio);
566         printf("\n");
567 out:
568         return rc;
569 }
570
571 static void *
572 kwboot_mmap_image(const char *path, size_t *size, int prot)
573 {
574         int rc, fd, flags;
575         struct stat st;
576         void *img;
577
578         rc = -1;
579         img = NULL;
580
581         fd = open(path, O_RDONLY);
582         if (fd < 0)
583                 goto out;
584
585         rc = fstat(fd, &st);
586         if (rc)
587                 goto out;
588
589         flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
590
591         img = mmap(NULL, st.st_size, prot, flags, fd, 0);
592         if (img == MAP_FAILED) {
593                 img = NULL;
594                 goto out;
595         }
596
597         rc = 0;
598         *size = st.st_size;
599 out:
600         if (rc && img) {
601                 munmap(img, st.st_size);
602                 img = NULL;
603         }
604         if (fd >= 0)
605                 close(fd);
606
607         return img;
608 }
609
610 static uint8_t
611 kwboot_img_csum8(void *_data, size_t size)
612 {
613         uint8_t *data = _data, csum;
614
615         for (csum = 0; size-- > 0; data++)
616                 csum += *data;
617
618         return csum;
619 }
620
621 static int
622 kwboot_img_patch_hdr(void *img, size_t size)
623 {
624         int rc;
625         struct main_hdr_v1 *hdr;
626         uint8_t csum;
627         size_t hdrsz = sizeof(*hdr);
628         int image_ver;
629
630         rc = -1;
631         hdr = img;
632
633         if (size < hdrsz) {
634                 errno = EINVAL;
635                 goto out;
636         }
637
638         image_ver = image_version(img);
639         if (image_ver != 0 && image_ver != 1) {
640                 fprintf(stderr, "Invalid image header version\n");
641                 errno = EINVAL;
642                 goto out;
643         }
644
645         if (image_ver == 0)
646                 hdrsz = sizeof(*hdr);
647         else
648                 hdrsz = KWBHEADER_V1_SIZE(hdr);
649
650         if (size < hdrsz) {
651                 errno = EINVAL;
652                 goto out;
653         }
654
655         csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
656         if (csum != hdr->checksum) {
657                 errno = EINVAL;
658                 goto out;
659         }
660
661         if (hdr->blockid == IBR_HDR_UART_ID) {
662                 rc = 0;
663                 goto out;
664         }
665
666         hdr->blockid = IBR_HDR_UART_ID;
667
668         if (image_ver == 0) {
669                 struct main_hdr_v0 *hdr_v0 = img;
670
671                 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
672                 hdr_v0->nandpagesize = 0;
673
674                 hdr_v0->srcaddr = hdr_v0->ext
675                         ? sizeof(struct kwb_header)
676                         : sizeof(*hdr_v0);
677         }
678
679         hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
680
681         rc = 0;
682 out:
683         return rc;
684 }
685
686 static void
687 kwboot_usage(FILE *stream, char *progname)
688 {
689         fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
690         fprintf(stream,
691                 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
692                 progname);
693         fprintf(stream, "\n");
694         fprintf(stream,
695                 "  -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
696         fprintf(stream, "  -p: patch <image> to type 0x69 (uart boot)\n");
697         fprintf(stream,
698                 "  -D <image>: boot <image> without preamble (Dove)\n");
699         fprintf(stream, "  -d: enter debug mode\n");
700         fprintf(stream, "  -a: use timings for Armada XP\n");
701         fprintf(stream, "  -q <req-delay>:  use specific request-delay\n");
702         fprintf(stream, "  -s <resp-timeo>: use specific response-timeout\n");
703         fprintf(stream,
704                 "  -o <block-timeo>: use specific xmodem block timeout\n");
705         fprintf(stream, "\n");
706         fprintf(stream, "  -t: mini terminal\n");
707         fprintf(stream, "\n");
708         fprintf(stream, "  -B <baud>: set baud rate\n");
709         fprintf(stream, "\n");
710 }
711
712 int
713 main(int argc, char **argv)
714 {
715         const char *ttypath, *imgpath;
716         int rv, rc, tty, term, prot, patch;
717         void *bootmsg;
718         void *debugmsg;
719         void *img;
720         size_t size;
721         speed_t speed;
722
723         rv = 1;
724         tty = -1;
725         bootmsg = NULL;
726         debugmsg = NULL;
727         imgpath = NULL;
728         img = NULL;
729         term = 0;
730         patch = 0;
731         size = 0;
732         speed = B115200;
733
734         kwboot_verbose = isatty(STDOUT_FILENO);
735
736         do {
737                 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
738                 if (c < 0)
739                         break;
740
741                 switch (c) {
742                 case 'b':
743                         bootmsg = kwboot_msg_boot;
744                         imgpath = optarg;
745                         break;
746
747                 case 'D':
748                         bootmsg = NULL;
749                         imgpath = optarg;
750                         break;
751
752                 case 'd':
753                         debugmsg = kwboot_msg_debug;
754                         break;
755
756                 case 'p':
757                         patch = 1;
758                         break;
759
760                 case 't':
761                         term = 1;
762                         break;
763
764                 case 'a':
765                         msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
766                         msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
767                         break;
768
769                 case 'q':
770                         msg_req_delay = atoi(optarg);
771                         break;
772
773                 case 's':
774                         msg_rsp_timeo = atoi(optarg);
775                         break;
776
777                 case 'o':
778                         blk_rsp_timeo = atoi(optarg);
779                         break;
780
781                 case 'B':
782                         speed = kwboot_tty_speed(atoi(optarg));
783                         if (speed == -1)
784                                 goto usage;
785                         break;
786
787                 case 'h':
788                         rv = 0;
789                 default:
790                         goto usage;
791                 }
792         } while (1);
793
794         if (!bootmsg && !term && !debugmsg)
795                 goto usage;
796
797         if (patch && !imgpath)
798                 goto usage;
799
800         if (argc - optind < 1)
801                 goto usage;
802
803         ttypath = argv[optind++];
804
805         tty = kwboot_open_tty(ttypath, speed);
806         if (tty < 0) {
807                 perror(ttypath);
808                 goto out;
809         }
810
811         if (imgpath) {
812                 prot = PROT_READ | (patch ? PROT_WRITE : 0);
813
814                 img = kwboot_mmap_image(imgpath, &size, prot);
815                 if (!img) {
816                         perror(imgpath);
817                         goto out;
818                 }
819         }
820
821         if (patch) {
822                 rc = kwboot_img_patch_hdr(img, size);
823                 if (rc) {
824                         fprintf(stderr, "%s: Invalid image.\n", imgpath);
825                         goto out;
826                 }
827         }
828
829         if (debugmsg) {
830                 rc = kwboot_debugmsg(tty, debugmsg);
831                 if (rc) {
832                         perror("debugmsg");
833                         goto out;
834                 }
835         } else if (bootmsg) {
836                 rc = kwboot_bootmsg(tty, bootmsg);
837                 if (rc) {
838                         perror("bootmsg");
839                         goto out;
840                 }
841         }
842
843         if (img) {
844                 rc = kwboot_xmodem(tty, img, size);
845                 if (rc) {
846                         perror("xmodem");
847                         goto out;
848                 }
849         }
850
851         if (term) {
852                 rc = kwboot_terminal(tty);
853                 if (rc && !(errno == EINTR)) {
854                         perror("terminal");
855                         goto out;
856                 }
857         }
858
859         rv = 0;
860 out:
861         if (tty >= 0)
862                 close(tty);
863
864         if (img)
865                 munmap(img, size);
866
867         return rv;
868
869 usage:
870         kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
871         goto out;
872 }