tools: kwboot: Round up header size to 128 B when patching
[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 <time.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 #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
61
62 struct kwboot_block {
63         uint8_t soh;
64         uint8_t pnum;
65         uint8_t _pnum;
66         uint8_t data[KWBOOT_XM_BLKSZ];
67         uint8_t csum;
68 } __packed;
69
70 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
71 #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
72
73 static int kwboot_verbose;
74
75 static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
76 static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
77 static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
78
79 static ssize_t
80 kwboot_write(int fd, const char *buf, size_t len)
81 {
82         size_t tot = 0;
83
84         while (tot < len) {
85                 ssize_t wr = write(fd, buf + tot, len - tot);
86
87                 if (wr < 0)
88                         return -1;
89
90                 tot += wr;
91         }
92
93         return tot;
94 }
95
96 static void
97 kwboot_printv(const char *fmt, ...)
98 {
99         va_list ap;
100
101         if (kwboot_verbose) {
102                 va_start(ap, fmt);
103                 vprintf(fmt, ap);
104                 va_end(ap);
105                 fflush(stdout);
106         }
107 }
108
109 static void
110 __spinner(void)
111 {
112         const char seq[] = { '-', '\\', '|', '/' };
113         const int div = 8;
114         static int state, bs;
115
116         if (state % div == 0) {
117                 fputc(bs, stdout);
118                 fputc(seq[state / div % sizeof(seq)], stdout);
119                 fflush(stdout);
120         }
121
122         bs = '\b';
123         state++;
124 }
125
126 static void
127 kwboot_spinner(void)
128 {
129         if (kwboot_verbose)
130                 __spinner();
131 }
132
133 static void
134 __progress(int pct, char c)
135 {
136         const int width = 70;
137         static const char *nl = "";
138         static int pos;
139
140         if (pos % width == 0)
141                 printf("%s%3d %% [", nl, pct);
142
143         fputc(c, stdout);
144
145         nl = "]\n";
146         pos = (pos + 1) % width;
147
148         if (pct == 100) {
149                 while (pos && pos++ < width)
150                         fputc(' ', stdout);
151                 fputs(nl, stdout);
152                 nl = "";
153                 pos = 0;
154         }
155
156         fflush(stdout);
157
158 }
159
160 static void
161 kwboot_progress(int _pct, char c)
162 {
163         static int pct;
164
165         if (_pct != -1)
166                 pct = _pct;
167
168         if (kwboot_verbose)
169                 __progress(pct, c);
170
171         if (pct == 100)
172                 pct = 0;
173 }
174
175 static int
176 kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
177 {
178         int rc, nfds;
179         fd_set rfds;
180         struct timeval tv;
181         ssize_t n;
182
183         rc = -1;
184
185         FD_ZERO(&rfds);
186         FD_SET(fd, &rfds);
187
188         tv.tv_sec = 0;
189         tv.tv_usec = timeo * 1000;
190         if (tv.tv_usec > 1000000) {
191                 tv.tv_sec += tv.tv_usec / 1000000;
192                 tv.tv_usec %= 1000000;
193         }
194
195         do {
196                 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
197                 if (nfds < 0)
198                         goto out;
199                 if (!nfds) {
200                         errno = ETIMEDOUT;
201                         goto out;
202                 }
203
204                 n = read(fd, buf, len);
205                 if (n <= 0)
206                         goto out;
207
208                 buf = (char *)buf + n;
209                 len -= n;
210         } while (len > 0);
211
212         rc = 0;
213 out:
214         return rc;
215 }
216
217 static int
218 kwboot_tty_send(int fd, const void *buf, size_t len)
219 {
220         if (!buf)
221                 return 0;
222
223         if (kwboot_write(fd, buf, len) < 0)
224                 return -1;
225
226         return tcdrain(fd);
227 }
228
229 static int
230 kwboot_tty_send_char(int fd, unsigned char c)
231 {
232         return kwboot_tty_send(fd, &c, 1);
233 }
234
235 static speed_t
236 kwboot_tty_speed(int baudrate)
237 {
238         switch (baudrate) {
239         case 115200:
240                 return B115200;
241         case 57600:
242                 return B57600;
243         case 38400:
244                 return B38400;
245         case 19200:
246                 return B19200;
247         case 9600:
248                 return B9600;
249         }
250
251         return -1;
252 }
253
254 static int
255 kwboot_open_tty(const char *path, speed_t speed)
256 {
257         int rc, fd;
258         struct termios tio;
259
260         rc = -1;
261
262         fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
263         if (fd < 0)
264                 goto out;
265
266         memset(&tio, 0, sizeof(tio));
267
268         tio.c_iflag = 0;
269         tio.c_cflag = CREAD|CLOCAL|CS8;
270
271         tio.c_cc[VMIN] = 1;
272         tio.c_cc[VTIME] = 10;
273
274         cfsetospeed(&tio, speed);
275         cfsetispeed(&tio, speed);
276
277         rc = tcsetattr(fd, TCSANOW, &tio);
278         if (rc)
279                 goto out;
280
281         rc = fd;
282 out:
283         if (rc < 0) {
284                 if (fd >= 0)
285                         close(fd);
286         }
287
288         return rc;
289 }
290
291 static int
292 kwboot_bootmsg(int tty, void *msg)
293 {
294         int rc;
295         char c;
296         int count;
297
298         if (msg == NULL)
299                 kwboot_printv("Please reboot the target into UART boot mode...");
300         else
301                 kwboot_printv("Sending boot message. Please reboot the target...");
302
303         do {
304                 rc = tcflush(tty, TCIOFLUSH);
305                 if (rc)
306                         break;
307
308                 for (count = 0; count < 128; count++) {
309                         rc = kwboot_tty_send(tty, msg, 8);
310                         if (rc) {
311                                 usleep(msg_req_delay * 1000);
312                                 continue;
313                         }
314                 }
315
316                 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
317
318                 kwboot_spinner();
319
320         } while (rc || c != NAK);
321
322         kwboot_printv("\n");
323
324         return rc;
325 }
326
327 static int
328 kwboot_debugmsg(int tty, void *msg)
329 {
330         int rc;
331
332         kwboot_printv("Sending debug message. Please reboot the target...");
333
334         do {
335                 char buf[16];
336
337                 rc = tcflush(tty, TCIOFLUSH);
338                 if (rc)
339                         break;
340
341                 rc = kwboot_tty_send(tty, msg, 8);
342                 if (rc) {
343                         usleep(msg_req_delay * 1000);
344                         continue;
345                 }
346
347                 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
348
349                 kwboot_spinner();
350
351         } while (rc);
352
353         kwboot_printv("\n");
354
355         return rc;
356 }
357
358 static size_t
359 kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
360                     size_t size, int pnum)
361 {
362         size_t i, n;
363
364         block->soh = SOH;
365         block->pnum = pnum;
366         block->_pnum = ~block->pnum;
367
368         n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
369         memcpy(&block->data[0], data, n);
370         memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
371
372         block->csum = 0;
373         for (i = 0; i < n; i++)
374                 block->csum += block->data[i];
375
376         return n;
377 }
378
379 static uint64_t
380 _now(void)
381 {
382         struct timespec ts;
383
384         if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
385                 static int err_print;
386
387                 if (!err_print) {
388                         perror("clock_gettime() does not work");
389                         err_print = 1;
390                 }
391
392                 /* this will just make the timeout not work */
393                 return -1ULL;
394         }
395
396         return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
397 }
398
399 static int
400 _is_xm_reply(char c)
401 {
402         return c == ACK || c == NAK || c == CAN;
403 }
404
405 static int
406 _xm_reply_to_error(int c)
407 {
408         int rc = -1;
409
410         switch (c) {
411         case ACK:
412                 rc = 0;
413                 break;
414         case NAK:
415                 errno = EBADMSG;
416                 break;
417         case CAN:
418                 errno = ECANCELED;
419                 break;
420         default:
421                 errno = EPROTO;
422                 break;
423         }
424
425         return rc;
426 }
427
428 static int
429 kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
430 {
431         int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
432         uint64_t recv_until = _now() + timeout;
433         int rc;
434
435         if (non_xm_print)
436                 *non_xm_print = 0;
437
438         while (1) {
439                 rc = kwboot_tty_recv(fd, c, 1, timeout);
440                 if (rc) {
441                         if (errno != ETIMEDOUT)
442                                 return rc;
443                         else if (allow_non_xm && *non_xm_print)
444                                 return -1;
445                         else
446                                 *c = NAK;
447                 }
448
449                 /* If received xmodem reply, end. */
450                 if (_is_xm_reply(*c))
451                         break;
452
453                 /*
454                  * If printing non-xmodem text output is allowed and such a byte
455                  * was received, print it and increase receiving time.
456                  * Otherwise decrease timeout by time elapsed.
457                  */
458                 if (allow_non_xm) {
459                         recv_until = _now() + timeout;
460                         putchar(*c);
461                         fflush(stdout);
462                         *non_xm_print = 1;
463                 } else {
464                         timeout = recv_until - _now();
465                         if (timeout < 0) {
466                                 errno = ETIMEDOUT;
467                                 return -1;
468                         }
469                 }
470         }
471
472         return 0;
473 }
474
475 static int
476 kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
477                     int *done_print)
478 {
479         int non_xm_print;
480         int rc, retries;
481         char c;
482
483         *done_print = 0;
484
485         retries = 16;
486         do {
487                 rc = kwboot_tty_send(fd, block, sizeof(*block));
488                 if (rc)
489                         return rc;
490
491                 if (allow_non_xm && !*done_print) {
492                         kwboot_progress(100, '.');
493                         kwboot_printv("Done\n");
494                         *done_print = 1;
495                 }
496
497                 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
498                 if (rc)
499                         return rc;
500
501                 if (!allow_non_xm && c != ACK)
502                         kwboot_progress(-1, '+');
503         } while (c == NAK && retries-- > 0);
504
505         if (non_xm_print)
506                 kwboot_printv("\n");
507
508         return _xm_reply_to_error(c);
509 }
510
511 static int
512 kwboot_xm_finish(int fd)
513 {
514         int rc, retries;
515         char c;
516
517         kwboot_printv("Finishing transfer\n");
518
519         retries = 16;
520         do {
521                 rc = kwboot_tty_send_char(fd, EOT);
522                 if (rc)
523                         return rc;
524
525                 rc = kwboot_xm_recv_reply(fd, &c, 0, NULL);
526                 if (rc)
527                         return rc;
528         } while (c == NAK && retries-- > 0);
529
530         return _xm_reply_to_error(c);
531 }
532
533 static int
534 kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
535                   size_t size)
536 {
537         int done_print = 0;
538         size_t sent, left;
539         int rc;
540
541         kwboot_printv("Sending boot image %s (%zu bytes)...\n",
542                       header ? "header" : "data", size);
543
544         left = size;
545         sent = 0;
546
547         while (sent < size) {
548                 struct kwboot_block block;
549                 int last_block;
550                 size_t blksz;
551
552                 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
553                 data += blksz;
554
555                 last_block = (left <= blksz);
556
557                 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
558                                          &done_print);
559                 if (rc)
560                         goto out;
561
562                 sent += blksz;
563                 left -= blksz;
564
565                 if (!done_print)
566                         kwboot_progress(sent * 100 / size, '.');
567         }
568
569         if (!done_print)
570                 kwboot_printv("Done\n");
571
572         return 0;
573 out:
574         kwboot_printv("\n");
575         return rc;
576 }
577
578 static int
579 kwboot_xmodem(int tty, const void *_img, size_t size)
580 {
581         const uint8_t *img = _img;
582         int rc, pnum;
583         size_t hdrsz;
584
585         hdrsz = kwbheader_size(img);
586
587         kwboot_printv("Waiting 2s and flushing tty\n");
588         sleep(2); /* flush isn't effective without it */
589         tcflush(tty, TCIOFLUSH);
590
591         pnum = 1;
592
593         rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
594         if (rc)
595                 return rc;
596
597         img += hdrsz;
598         size -= hdrsz;
599
600         rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
601         if (rc)
602                 return rc;
603
604         return kwboot_xm_finish(tty);
605 }
606
607 static int
608 kwboot_term_pipe(int in, int out, const char *quit, int *s)
609 {
610         ssize_t nin;
611         char _buf[128], *buf = _buf;
612
613         nin = read(in, buf, sizeof(_buf));
614         if (nin <= 0)
615                 return -1;
616
617         if (quit) {
618                 int i;
619
620                 for (i = 0; i < nin; i++) {
621                         if (*buf == quit[*s]) {
622                                 (*s)++;
623                                 if (!quit[*s])
624                                         return 0;
625                                 buf++;
626                                 nin--;
627                         } else {
628                                 if (kwboot_write(out, quit, *s) < 0)
629                                         return -1;
630                                 *s = 0;
631                         }
632                 }
633         }
634
635         if (kwboot_write(out, buf, nin) < 0)
636                 return -1;
637
638         return 0;
639 }
640
641 static int
642 kwboot_terminal(int tty)
643 {
644         int rc, in, s;
645         const char *quit = "\34c";
646         struct termios otio, tio;
647
648         rc = -1;
649
650         in = STDIN_FILENO;
651         if (isatty(in)) {
652                 rc = tcgetattr(in, &otio);
653                 if (!rc) {
654                         tio = otio;
655                         cfmakeraw(&tio);
656                         rc = tcsetattr(in, TCSANOW, &tio);
657                 }
658                 if (rc) {
659                         perror("tcsetattr");
660                         goto out;
661                 }
662
663                 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
664                               quit[0]|0100, quit[1]);
665         } else
666                 in = -1;
667
668         rc = 0;
669         s = 0;
670
671         do {
672                 fd_set rfds;
673                 int nfds = 0;
674
675                 FD_SET(tty, &rfds);
676                 nfds = nfds < tty ? tty : nfds;
677
678                 if (in >= 0) {
679                         FD_SET(in, &rfds);
680                         nfds = nfds < in ? in : nfds;
681                 }
682
683                 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
684                 if (nfds < 0)
685                         break;
686
687                 if (FD_ISSET(tty, &rfds)) {
688                         rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
689                         if (rc)
690                                 break;
691                 }
692
693                 if (in >= 0 && FD_ISSET(in, &rfds)) {
694                         rc = kwboot_term_pipe(in, tty, quit, &s);
695                         if (rc)
696                                 break;
697                 }
698         } while (quit[s] != 0);
699
700         if (in >= 0)
701                 tcsetattr(in, TCSANOW, &otio);
702         printf("\n");
703 out:
704         return rc;
705 }
706
707 static void *
708 kwboot_read_image(const char *path, size_t *size, size_t reserve)
709 {
710         int rc, fd;
711         struct stat st;
712         void *img;
713         off_t tot;
714
715         rc = -1;
716         img = NULL;
717
718         fd = open(path, O_RDONLY);
719         if (fd < 0)
720                 goto out;
721
722         rc = fstat(fd, &st);
723         if (rc)
724                 goto out;
725
726         img = malloc(st.st_size + reserve);
727         if (!img)
728                 goto out;
729
730         tot = 0;
731         while (tot < st.st_size) {
732                 ssize_t rd = read(fd, img + tot, st.st_size - tot);
733
734                 if (rd < 0)
735                         goto out;
736
737                 tot += rd;
738
739                 if (!rd && tot < st.st_size) {
740                         errno = EIO;
741                         goto out;
742                 }
743         }
744
745         rc = 0;
746         *size = st.st_size;
747 out:
748         if (rc && img) {
749                 free(img);
750                 img = NULL;
751         }
752         if (fd >= 0)
753                 close(fd);
754
755         return img;
756 }
757
758 static uint8_t
759 kwboot_hdr_csum8(const void *hdr)
760 {
761         const uint8_t *data = hdr;
762         uint8_t csum;
763         size_t size;
764
765         size = kwbheader_size_for_csum(hdr);
766
767         for (csum = 0; size-- > 0; data++)
768                 csum += *data;
769
770         return csum;
771 }
772
773 static int
774 kwboot_img_is_secure(void *img)
775 {
776         struct opt_hdr_v1 *ohdr;
777
778         for_each_opt_hdr_v1 (ohdr, img)
779                 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
780                         return 1;
781
782         return 0;
783 }
784
785 static void
786 kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
787 {
788         uint32_t hdrsz, datasz, srcaddr;
789         struct main_hdr_v1 *hdr = img;
790         uint8_t *data;
791
792         srcaddr = le32_to_cpu(hdr->srcaddr);
793
794         hdrsz = kwbheader_size(img);
795         data = (uint8_t *)img + srcaddr;
796         datasz = *size - srcaddr;
797
798         /* only move data if there is not enough space */
799         if (hdrsz + grow > srcaddr) {
800                 size_t need = hdrsz + grow - srcaddr;
801
802                 /* move data by enough bytes */
803                 memmove(data + need, data, datasz);
804
805                 hdr->srcaddr = cpu_to_le32(srcaddr + need);
806                 *size += need;
807         }
808
809         if (kwbimage_version(img) == 1) {
810                 hdrsz += grow;
811                 hdr->headersz_msb = hdrsz >> 16;
812                 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
813         }
814 }
815
816 static int
817 kwboot_img_patch_hdr(void *img, size_t *size)
818 {
819         int rc;
820         struct main_hdr_v1 *hdr;
821         uint32_t srcaddr;
822         uint8_t csum;
823         size_t hdrsz = sizeof(*hdr);
824         int image_ver;
825         int is_secure;
826
827         rc = -1;
828         hdr = img;
829
830         if (*size < hdrsz) {
831                 errno = EINVAL;
832                 goto out;
833         }
834
835         image_ver = kwbimage_version(img);
836         if (image_ver != 0 && image_ver != 1) {
837                 fprintf(stderr, "Invalid image header version\n");
838                 errno = EINVAL;
839                 goto out;
840         }
841
842         hdrsz = kwbheader_size(hdr);
843
844         if (*size < hdrsz) {
845                 errno = EINVAL;
846                 goto out;
847         }
848
849         csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
850         if (csum != hdr->checksum) {
851                 errno = EINVAL;
852                 goto out;
853         }
854
855         if (image_ver == 0) {
856                 struct main_hdr_v0 *hdr_v0 = img;
857
858                 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
859                 hdr_v0->nandpagesize = 0;
860         }
861
862         srcaddr = le32_to_cpu(hdr->srcaddr);
863
864         switch (hdr->blockid) {
865         case IBR_HDR_SATA_ID:
866                 if (srcaddr < 1) {
867                         errno = EINVAL;
868                         goto out;
869                 }
870                 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
871                 break;
872
873         case IBR_HDR_SDIO_ID:
874                 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
875                 break;
876
877         case IBR_HDR_PEX_ID:
878                 if (srcaddr == 0xFFFFFFFF)
879                         hdr->srcaddr = cpu_to_le32(hdrsz);
880                 break;
881
882         case IBR_HDR_SPI_ID:
883                 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
884                         kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
885                         hdr->destaddr = cpu_to_le32(0x00800000);
886                         hdr->execaddr = cpu_to_le32(0x00800000);
887                 }
888                 break;
889         }
890
891         if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
892             *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) {
893                 errno = EINVAL;
894                 goto out;
895         }
896
897         is_secure = kwboot_img_is_secure(img);
898
899         if (hdr->blockid != IBR_HDR_UART_ID) {
900                 if (is_secure) {
901                         fprintf(stderr,
902                                 "Image has secure header with signature for non-UART booting\n");
903                         errno = EINVAL;
904                         goto out;
905                 }
906
907                 kwboot_printv("Patching image boot signature to UART\n");
908                 hdr->blockid = IBR_HDR_UART_ID;
909         }
910
911         if (hdrsz % KWBOOT_XM_BLKSZ) {
912                 size_t offset = (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) %
913                                 KWBOOT_XM_BLKSZ;
914
915                 if (is_secure) {
916                         fprintf(stderr, "Cannot align image with secure header\n");
917                         errno = EINVAL;
918                         goto out;
919                 }
920
921                 kwboot_printv("Aligning image header to Xmodem block size\n");
922                 kwboot_img_grow_hdr(img, size, offset);
923         }
924
925         hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
926
927         *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
928         rc = 0;
929 out:
930         return rc;
931 }
932
933 static void
934 kwboot_usage(FILE *stream, char *progname)
935 {
936         fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
937         fprintf(stream,
938                 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
939                 progname);
940         fprintf(stream, "\n");
941         fprintf(stream,
942                 "  -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
943         fprintf(stream,
944                 "  -D <image>: boot <image> without preamble (Dove)\n");
945         fprintf(stream, "  -d: enter debug mode\n");
946         fprintf(stream, "  -a: use timings for Armada XP\n");
947         fprintf(stream, "  -q <req-delay>:  use specific request-delay\n");
948         fprintf(stream, "  -s <resp-timeo>: use specific response-timeout\n");
949         fprintf(stream,
950                 "  -o <block-timeo>: use specific xmodem block timeout\n");
951         fprintf(stream, "\n");
952         fprintf(stream, "  -t: mini terminal\n");
953         fprintf(stream, "\n");
954         fprintf(stream, "  -B <baud>: set baud rate\n");
955         fprintf(stream, "\n");
956 }
957
958 int
959 main(int argc, char **argv)
960 {
961         const char *ttypath, *imgpath;
962         int rv, rc, tty, term;
963         void *bootmsg;
964         void *debugmsg;
965         void *img;
966         size_t size;
967         speed_t speed;
968
969         rv = 1;
970         tty = -1;
971         bootmsg = NULL;
972         debugmsg = NULL;
973         imgpath = NULL;
974         img = NULL;
975         term = 0;
976         size = 0;
977         speed = B115200;
978
979         kwboot_verbose = isatty(STDOUT_FILENO);
980
981         do {
982                 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
983                 if (c < 0)
984                         break;
985
986                 switch (c) {
987                 case 'b':
988                         bootmsg = kwboot_msg_boot;
989                         imgpath = optarg;
990                         break;
991
992                 case 'D':
993                         bootmsg = NULL;
994                         imgpath = optarg;
995                         break;
996
997                 case 'd':
998                         debugmsg = kwboot_msg_debug;
999                         break;
1000
1001                 case 'p':
1002                         /* nop, for backward compatibility */
1003                         break;
1004
1005                 case 't':
1006                         term = 1;
1007                         break;
1008
1009                 case 'a':
1010                         msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
1011                         msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1012                         break;
1013
1014                 case 'q':
1015                         msg_req_delay = atoi(optarg);
1016                         break;
1017
1018                 case 's':
1019                         msg_rsp_timeo = atoi(optarg);
1020                         break;
1021
1022                 case 'o':
1023                         blk_rsp_timeo = atoi(optarg);
1024                         break;
1025
1026                 case 'B':
1027                         speed = kwboot_tty_speed(atoi(optarg));
1028                         if (speed == -1)
1029                                 goto usage;
1030                         break;
1031
1032                 case 'h':
1033                         rv = 0;
1034                 default:
1035                         goto usage;
1036                 }
1037         } while (1);
1038
1039         if (!bootmsg && !term && !debugmsg)
1040                 goto usage;
1041
1042         if (argc - optind < 1)
1043                 goto usage;
1044
1045         ttypath = argv[optind++];
1046
1047         tty = kwboot_open_tty(ttypath, speed);
1048         if (tty < 0) {
1049                 perror(ttypath);
1050                 goto out;
1051         }
1052
1053         if (imgpath) {
1054                 img = kwboot_read_image(imgpath, &size, KWBOOT_XM_BLKSZ);
1055                 if (!img) {
1056                         perror(imgpath);
1057                         goto out;
1058                 }
1059
1060                 rc = kwboot_img_patch_hdr(img, &size);
1061                 if (rc) {
1062                         fprintf(stderr, "%s: Invalid image.\n", imgpath);
1063                         goto out;
1064                 }
1065         }
1066
1067         if (debugmsg) {
1068                 rc = kwboot_debugmsg(tty, debugmsg);
1069                 if (rc) {
1070                         perror("debugmsg");
1071                         goto out;
1072                 }
1073         } else if (bootmsg) {
1074                 rc = kwboot_bootmsg(tty, bootmsg);
1075                 if (rc) {
1076                         perror("bootmsg");
1077                         goto out;
1078                 }
1079         }
1080
1081         if (img) {
1082                 rc = kwboot_xmodem(tty, img, size);
1083                 if (rc) {
1084                         perror("xmodem");
1085                         goto out;
1086                 }
1087         }
1088
1089         if (term) {
1090                 rc = kwboot_terminal(tty);
1091                 if (rc && !(errno == EINTR)) {
1092                         perror("terminal");
1093                         goto out;
1094                 }
1095         }
1096
1097         rv = 0;
1098 out:
1099         if (tty >= 0)
1100                 close(tty);
1101
1102         if (img)
1103                 free(img);
1104
1105         return rv;
1106
1107 usage:
1108         kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1109         goto out;
1110 }