tools: kwboot: Print newline on error when progress was not completed
[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         kwboot_printv("\n");
463         return rc;
464
465 can:
466         err = errno;
467         kwboot_tty_send_char(tty, CAN);
468         errno = err;
469         goto out;
470 }
471
472 static int
473 kwboot_term_pipe(int in, int out, const char *quit, int *s)
474 {
475         ssize_t nin;
476         char _buf[128], *buf = _buf;
477
478         nin = read(in, buf, sizeof(_buf));
479         if (nin <= 0)
480                 return -1;
481
482         if (quit) {
483                 int i;
484
485                 for (i = 0; i < nin; i++) {
486                         if (*buf == quit[*s]) {
487                                 (*s)++;
488                                 if (!quit[*s])
489                                         return 0;
490                                 buf++;
491                                 nin--;
492                         } else {
493                                 if (kwboot_write(out, quit, *s) < 0)
494                                         return -1;
495                                 *s = 0;
496                         }
497                 }
498         }
499
500         if (kwboot_write(out, buf, nin) < 0)
501                 return -1;
502
503         return 0;
504 }
505
506 static int
507 kwboot_terminal(int tty)
508 {
509         int rc, in, s;
510         const char *quit = "\34c";
511         struct termios otio, tio;
512
513         rc = -1;
514
515         in = STDIN_FILENO;
516         if (isatty(in)) {
517                 rc = tcgetattr(in, &otio);
518                 if (!rc) {
519                         tio = otio;
520                         cfmakeraw(&tio);
521                         rc = tcsetattr(in, TCSANOW, &tio);
522                 }
523                 if (rc) {
524                         perror("tcsetattr");
525                         goto out;
526                 }
527
528                 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
529                               quit[0]|0100, quit[1]);
530         } else
531                 in = -1;
532
533         rc = 0;
534         s = 0;
535
536         do {
537                 fd_set rfds;
538                 int nfds = 0;
539
540                 FD_SET(tty, &rfds);
541                 nfds = nfds < tty ? tty : nfds;
542
543                 if (in >= 0) {
544                         FD_SET(in, &rfds);
545                         nfds = nfds < in ? in : nfds;
546                 }
547
548                 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
549                 if (nfds < 0)
550                         break;
551
552                 if (FD_ISSET(tty, &rfds)) {
553                         rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
554                         if (rc)
555                                 break;
556                 }
557
558                 if (in >= 0 && FD_ISSET(in, &rfds)) {
559                         rc = kwboot_term_pipe(in, tty, quit, &s);
560                         if (rc)
561                                 break;
562                 }
563         } while (quit[s] != 0);
564
565         if (in >= 0)
566                 tcsetattr(in, TCSANOW, &otio);
567         printf("\n");
568 out:
569         return rc;
570 }
571
572 static void *
573 kwboot_mmap_image(const char *path, size_t *size, int prot)
574 {
575         int rc, fd, flags;
576         struct stat st;
577         void *img;
578
579         rc = -1;
580         img = NULL;
581
582         fd = open(path, O_RDONLY);
583         if (fd < 0)
584                 goto out;
585
586         rc = fstat(fd, &st);
587         if (rc)
588                 goto out;
589
590         flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
591
592         img = mmap(NULL, st.st_size, prot, flags, fd, 0);
593         if (img == MAP_FAILED) {
594                 img = NULL;
595                 goto out;
596         }
597
598         rc = 0;
599         *size = st.st_size;
600 out:
601         if (rc && img) {
602                 munmap(img, st.st_size);
603                 img = NULL;
604         }
605         if (fd >= 0)
606                 close(fd);
607
608         return img;
609 }
610
611 static uint8_t
612 kwboot_img_csum8(void *_data, size_t size)
613 {
614         uint8_t *data = _data, csum;
615
616         for (csum = 0; size-- > 0; data++)
617                 csum += *data;
618
619         return csum;
620 }
621
622 static int
623 kwboot_img_patch_hdr(void *img, size_t size)
624 {
625         int rc;
626         struct main_hdr_v1 *hdr;
627         uint8_t csum;
628         size_t hdrsz = sizeof(*hdr);
629         int image_ver;
630
631         rc = -1;
632         hdr = img;
633
634         if (size < hdrsz) {
635                 errno = EINVAL;
636                 goto out;
637         }
638
639         image_ver = image_version(img);
640         if (image_ver != 0 && image_ver != 1) {
641                 fprintf(stderr, "Invalid image header version\n");
642                 errno = EINVAL;
643                 goto out;
644         }
645
646         if (image_ver == 0)
647                 hdrsz = sizeof(*hdr);
648         else
649                 hdrsz = KWBHEADER_V1_SIZE(hdr);
650
651         if (size < hdrsz) {
652                 errno = EINVAL;
653                 goto out;
654         }
655
656         csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
657         if (csum != hdr->checksum) {
658                 errno = EINVAL;
659                 goto out;
660         }
661
662         if (hdr->blockid == IBR_HDR_UART_ID) {
663                 rc = 0;
664                 goto out;
665         }
666
667         hdr->blockid = IBR_HDR_UART_ID;
668
669         if (image_ver == 0) {
670                 struct main_hdr_v0 *hdr_v0 = img;
671
672                 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
673                 hdr_v0->nandpagesize = 0;
674
675                 hdr_v0->srcaddr = hdr_v0->ext
676                         ? sizeof(struct kwb_header)
677                         : sizeof(*hdr_v0);
678         }
679
680         hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
681
682         rc = 0;
683 out:
684         return rc;
685 }
686
687 static void
688 kwboot_usage(FILE *stream, char *progname)
689 {
690         fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
691         fprintf(stream,
692                 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
693                 progname);
694         fprintf(stream, "\n");
695         fprintf(stream,
696                 "  -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
697         fprintf(stream, "  -p: patch <image> to type 0x69 (uart boot)\n");
698         fprintf(stream,
699                 "  -D <image>: boot <image> without preamble (Dove)\n");
700         fprintf(stream, "  -d: enter debug mode\n");
701         fprintf(stream, "  -a: use timings for Armada XP\n");
702         fprintf(stream, "  -q <req-delay>:  use specific request-delay\n");
703         fprintf(stream, "  -s <resp-timeo>: use specific response-timeout\n");
704         fprintf(stream,
705                 "  -o <block-timeo>: use specific xmodem block timeout\n");
706         fprintf(stream, "\n");
707         fprintf(stream, "  -t: mini terminal\n");
708         fprintf(stream, "\n");
709         fprintf(stream, "  -B <baud>: set baud rate\n");
710         fprintf(stream, "\n");
711 }
712
713 int
714 main(int argc, char **argv)
715 {
716         const char *ttypath, *imgpath;
717         int rv, rc, tty, term, prot, patch;
718         void *bootmsg;
719         void *debugmsg;
720         void *img;
721         size_t size;
722         speed_t speed;
723
724         rv = 1;
725         tty = -1;
726         bootmsg = NULL;
727         debugmsg = NULL;
728         imgpath = NULL;
729         img = NULL;
730         term = 0;
731         patch = 0;
732         size = 0;
733         speed = B115200;
734
735         kwboot_verbose = isatty(STDOUT_FILENO);
736
737         do {
738                 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
739                 if (c < 0)
740                         break;
741
742                 switch (c) {
743                 case 'b':
744                         bootmsg = kwboot_msg_boot;
745                         imgpath = optarg;
746                         break;
747
748                 case 'D':
749                         bootmsg = NULL;
750                         imgpath = optarg;
751                         break;
752
753                 case 'd':
754                         debugmsg = kwboot_msg_debug;
755                         break;
756
757                 case 'p':
758                         patch = 1;
759                         break;
760
761                 case 't':
762                         term = 1;
763                         break;
764
765                 case 'a':
766                         msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
767                         msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
768                         break;
769
770                 case 'q':
771                         msg_req_delay = atoi(optarg);
772                         break;
773
774                 case 's':
775                         msg_rsp_timeo = atoi(optarg);
776                         break;
777
778                 case 'o':
779                         blk_rsp_timeo = atoi(optarg);
780                         break;
781
782                 case 'B':
783                         speed = kwboot_tty_speed(atoi(optarg));
784                         if (speed == -1)
785                                 goto usage;
786                         break;
787
788                 case 'h':
789                         rv = 0;
790                 default:
791                         goto usage;
792                 }
793         } while (1);
794
795         if (!bootmsg && !term && !debugmsg)
796                 goto usage;
797
798         if (patch && !imgpath)
799                 goto usage;
800
801         if (argc - optind < 1)
802                 goto usage;
803
804         ttypath = argv[optind++];
805
806         tty = kwboot_open_tty(ttypath, speed);
807         if (tty < 0) {
808                 perror(ttypath);
809                 goto out;
810         }
811
812         if (imgpath) {
813                 prot = PROT_READ | (patch ? PROT_WRITE : 0);
814
815                 img = kwboot_mmap_image(imgpath, &size, prot);
816                 if (!img) {
817                         perror(imgpath);
818                         goto out;
819                 }
820         }
821
822         if (patch) {
823                 rc = kwboot_img_patch_hdr(img, size);
824                 if (rc) {
825                         fprintf(stderr, "%s: Invalid image.\n", imgpath);
826                         goto out;
827                 }
828         }
829
830         if (debugmsg) {
831                 rc = kwboot_debugmsg(tty, debugmsg);
832                 if (rc) {
833                         perror("debugmsg");
834                         goto out;
835                 }
836         } else if (bootmsg) {
837                 rc = kwboot_bootmsg(tty, bootmsg);
838                 if (rc) {
839                         perror("bootmsg");
840                         goto out;
841                 }
842         }
843
844         if (img) {
845                 rc = kwboot_xmodem(tty, img, size);
846                 if (rc) {
847                         perror("xmodem");
848                         goto out;
849                 }
850         }
851
852         if (term) {
853                 rc = kwboot_terminal(tty);
854                 if (rc && !(errno == EINTR)) {
855                         perror("terminal");
856                         goto out;
857                 }
858         }
859
860         rv = 0;
861 out:
862         if (tty >= 0)
863                 close(tty);
864
865         if (img)
866                 munmap(img, size);
867
868         return rv;
869
870 usage:
871         kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
872         goto out;
873 }