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