blob: 0e533e369838a5db8c17b7ee54f4d48ada426235 [file] [log] [blame]
Luka Perkovdff289642012-05-27 11:44:51 +00001/*
Stefan Roesebb5c4282014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
3 * supports Kirkwood, Dove, Armada 370, Armada XP
Luka Perkovdff289642012-05-27 11:44:51 +00004 *
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
Stefan Roese04ec0d32016-01-07 14:12:04 +010012#include "kwbimage.h"
13#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +020014#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +010015
Luka Perkovdff289642012-05-27 11:44:51 +000016#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +010020#include <image.h>
Luka Perkovdff289642012-05-27 11:44:51 +000021#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
Luka Perkovdff289642012-05-27 11:44:51 +000030/*
31 * Marvell BootROM UART Sensing
32 */
33
34static unsigned char kwboot_msg_boot[] = {
35 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
36};
37
Stefan Roesebb5c4282014-10-22 12:13:21 +020038static unsigned char kwboot_msg_debug[] = {
39 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
40};
41
42/* Defines known to work on Kirkwood */
Luka Perkovdff289642012-05-27 11:44:51 +000043#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
44#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
45
Stefan Roesebb5c4282014-10-22 12:13:21 +020046/* 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
Luka Perkovdff289642012-05-27 11:44:51 +000050/*
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
60struct kwboot_block {
61 uint8_t soh;
62 uint8_t pnum;
63 uint8_t _pnum;
64 uint8_t data[128];
65 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +020066} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +000067
68#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
69
70static int kwboot_verbose;
71
Stefan Roesebb5c4282014-10-22 12:13:21 +020072static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
73static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +000074static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +020075
Marek Behúnd3bc5c32021-09-24 23:06:41 +020076static ssize_t
77kwboot_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
Luka Perkovdff289642012-05-27 11:44:51 +000093static void
94kwboot_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
106static 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
123static void
124kwboot_spinner(void)
125{
126 if (kwboot_verbose)
127 __spinner();
128}
129
130static 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";
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200143 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000144
145 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200146 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000147 fputc(' ', stdout);
148 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200149 nl = "";
150 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000151 }
152
153 fflush(stdout);
154
155}
156
157static void
158kwboot_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);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200167
168 if (pct == 100)
169 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000170}
171
172static int
173kwboot_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);
Willy Tarreauab7e8a12018-07-03 12:10:31 -0400202 if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000203 goto out;
204
205 buf = (char *)buf + n;
206 len -= n;
207 } while (len > 0);
208
209 rc = 0;
210out:
211 return rc;
212}
213
214static int
215kwboot_tty_send(int fd, const void *buf, size_t len)
216{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200217 if (!buf)
218 return 0;
219
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200220 if (kwboot_write(fd, buf, len) < 0)
221 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000222
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200223 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000224}
225
226static int
227kwboot_tty_send_char(int fd, unsigned char c)
228{
229 return kwboot_tty_send(fd, &c, 1);
230}
231
232static speed_t
233kwboot_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
251static int
252kwboot_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;
279out:
280 if (rc < 0) {
281 if (fd >= 0)
282 close(fd);
283 }
284
285 return rc;
286}
287
288static int
289kwboot_bootmsg(int tty, void *msg)
290{
291 int rc;
292 char c;
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300293 int count;
Luka Perkovdff289642012-05-27 11:44:51 +0000294
Stefan Roesebb5c4282014-10-22 12:13:21 +0200295 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...");
Luka Perkovdff289642012-05-27 11:44:51 +0000299
300 do {
301 rc = tcflush(tty, TCIOFLUSH);
302 if (rc)
303 break;
304
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300305 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 }
Luka Perkovdff289642012-05-27 11:44:51 +0000311 }
312
Stefan Roesebb5c4282014-10-22 12:13:21 +0200313 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovdff289642012-05-27 11:44:51 +0000314
315 kwboot_spinner();
316
317 } while (rc || c != NAK);
318
319 kwboot_printv("\n");
320
321 return rc;
322}
323
324static int
Stefan Roesebb5c4282014-10-22 12:13:21 +0200325kwboot_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
Pali Rohár58cf04de2021-09-24 23:06:44 +0200355static size_t
Luka Perkovdff289642012-05-27 11:44:51 +0000356kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
357 size_t size, int pnum)
358{
359 const size_t blksz = sizeof(block->data);
Marek Behúnedb63242021-09-24 23:06:45 +0200360 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +0000361
Stefan Roesebb5c4282014-10-22 12:13:21 +0200362 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +0000363 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
377static int
378kwboot_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)
Pali Rohár2c02a342021-09-24 23:06:43 +0200387 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000388
Stefan Roesebb5c4282014-10-22 12:13:21 +0200389 do {
Kevin Smith4d31a842016-02-16 21:28:19 +0000390 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
Pali Rohár2c02a342021-09-24 23:06:43 +0200391 if (rc) {
392 if (errno != ETIMEDOUT)
393 return rc;
394 c = NAK;
395 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200396
397 if (c != ACK && c != NAK && c != CAN)
398 printf("%c", c);
399
400 } while (c != ACK && c != NAK && c != CAN);
Luka Perkovdff289642012-05-27 11:44:51 +0000401
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
427static int
428kwboot_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
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300438 sleep(2); /* flush isn't effective without it */
439 tcflush(tty, TCIOFLUSH);
440
Luka Perkovdff289642012-05-27 11:44:51 +0000441 do {
442 struct kwboot_block block;
443 int n;
444
445 n = kwboot_xm_makeblock(&block,
446 data + N, size - N,
447 pnum++);
Luka Perkovdff289642012-05-27 11:44:51 +0000448 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
461out:
Pali Rohár5ed896f2021-09-24 23:06:47 +0200462 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000463 return rc;
464
465can:
466 err = errno;
467 kwboot_tty_send_char(tty, CAN);
468 errno = err;
469 goto out;
470}
471
472static int
Marek Behúnea5b2b32021-09-24 23:06:40 +0200473kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovdff289642012-05-27 11:44:51 +0000474{
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200475 ssize_t nin;
Luka Perkovdff289642012-05-27 11:44:51 +0000476 char _buf[128], *buf = _buf;
477
Pali Rohár8210ea02021-07-23 11:14:17 +0200478 nin = read(in, buf, sizeof(_buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -0400479 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000480 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--;
Pali Rohár48615ba2021-07-23 11:14:20 +0200492 } else {
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200493 if (kwboot_write(out, quit, *s) < 0)
494 return -1;
495 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +0200496 }
Luka Perkovdff289642012-05-27 11:44:51 +0000497 }
498 }
499
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200500 if (kwboot_write(out, buf, nin) < 0)
501 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000502
503 return 0;
504}
505
506static int
507kwboot_terminal(int tty)
508{
509 int rc, in, s;
Marek Behúnea5b2b32021-09-24 23:06:40 +0200510 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +0000511 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
Marek Behún4eb55de2021-09-24 23:06:39 +0200558 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovdff289642012-05-27 11:44:51 +0000559 rc = kwboot_term_pipe(in, tty, quit, &s);
560 if (rc)
561 break;
562 }
563 } while (quit[s] != 0);
564
Pali Roháreafc9692021-07-23 11:14:18 +0200565 if (in >= 0)
566 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +0200567 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000568out:
569 return rc;
570}
571
572static void *
573kwboot_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;
Luka Perkovdff289642012-05-27 11:44:51 +0000580 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;
600out:
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
611static uint8_t
612kwboot_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
622static int
623kwboot_img_patch_hdr(void *img, size_t size)
624{
625 int rc;
Stefan Roesec74165d2015-09-29 09:19:59 +0200626 struct main_hdr_v1 *hdr;
Luka Perkovdff289642012-05-27 11:44:51 +0000627 uint8_t csum;
Stefan Roesec74165d2015-09-29 09:19:59 +0200628 size_t hdrsz = sizeof(*hdr);
629 int image_ver;
Luka Perkovdff289642012-05-27 11:44:51 +0000630
631 rc = -1;
632 hdr = img;
633
634 if (size < hdrsz) {
635 errno = EINVAL;
636 goto out;
637 }
638
Stefan Roesec74165d2015-09-29 09:19:59 +0200639 image_ver = image_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +0200640 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +0200641 fprintf(stderr, "Invalid image header version\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000642 errno = EINVAL;
643 goto out;
644 }
645
Stefan Roesec74165d2015-09-29 09:19:59 +0200646 if (image_ver == 0)
647 hdrsz = sizeof(*hdr);
648 else
649 hdrsz = KWBHEADER_V1_SIZE(hdr);
650
Pali Rohár12281162021-07-23 11:14:21 +0200651 if (size < hdrsz) {
652 errno = EINVAL;
653 goto out;
654 }
655
Stefan Roesec74165d2015-09-29 09:19:59 +0200656 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
657 if (csum != hdr->checksum) {
658 errno = EINVAL;
659 goto out;
660 }
661
Luka Perkovdff289642012-05-27 11:44:51 +0000662 if (hdr->blockid == IBR_HDR_UART_ID) {
663 rc = 0;
664 goto out;
665 }
666
667 hdr->blockid = IBR_HDR_UART_ID;
668
Stefan Roesec74165d2015-09-29 09:19:59 +0200669 if (image_ver == 0) {
670 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovdff289642012-05-27 11:44:51 +0000671
Stefan Roesec74165d2015-09-29 09:19:59 +0200672 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 }
Luka Perkovdff289642012-05-27 11:44:51 +0000679
Stefan Roesec74165d2015-09-29 09:19:59 +0200680 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +0000681
682 rc = 0;
683out:
684 return rc;
685}
686
687static void
688kwboot_usage(FILE *stream, char *progname)
689{
Pali Rohár3c703aaf2021-09-24 23:06:42 +0200690 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovdff289642012-05-27 11:44:51 +0000691 fprintf(stream,
Kevin Smith6c7c1ba2016-02-16 21:28:17 +0000692 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +0200693 progname);
Luka Perkovdff289642012-05-27 11:44:51 +0000694 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +0200695 fprintf(stream,
696 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000697 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +0200698 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");
Stefan Roesef7f509d2015-05-29 13:25:04 +0200702 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
703 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +0000704 fprintf(stream,
705 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000706 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
713int
714main(int argc, char **argv)
715{
716 const char *ttypath, *imgpath;
717 int rv, rc, tty, term, prot, patch;
718 void *bootmsg;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200719 void *debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +0000720 void *img;
721 size_t size;
722 speed_t speed;
723
724 rv = 1;
725 tty = -1;
726 bootmsg = NULL;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200727 debugmsg = NULL;
Luka Perkovdff289642012-05-27 11:44:51 +0000728 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 {
Kevin Smith4d31a842016-02-16 21:28:19 +0000738 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +0000739 if (c < 0)
740 break;
741
742 switch (c) {
743 case 'b':
744 bootmsg = kwboot_msg_boot;
745 imgpath = optarg;
746 break;
747
Stefan Roesebb5c4282014-10-22 12:13:21 +0200748 case 'D':
749 bootmsg = NULL;
750 imgpath = optarg;
751 break;
752
753 case 'd':
754 debugmsg = kwboot_msg_debug;
755 break;
756
Luka Perkovdff289642012-05-27 11:44:51 +0000757 case 'p':
758 patch = 1;
759 break;
760
761 case 't':
762 term = 1;
763 break;
764
Stefan Roesebb5c4282014-10-22 12:13:21 +0200765 case 'a':
766 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
767 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
768 break;
769
Stefan Roesef7f509d2015-05-29 13:25:04 +0200770 case 'q':
771 msg_req_delay = atoi(optarg);
772 break;
773
774 case 's':
775 msg_rsp_timeo = atoi(optarg);
776 break;
777
Kevin Smith4d31a842016-02-16 21:28:19 +0000778 case 'o':
779 blk_rsp_timeo = atoi(optarg);
780 break;
781
Luka Perkovdff289642012-05-27 11:44:51 +0000782 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
Stefan Roesebb5c4282014-10-22 12:13:21 +0200795 if (!bootmsg && !term && !debugmsg)
Luka Perkovdff289642012-05-27 11:44:51 +0000796 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
Stefan Roesebb5c4282014-10-22 12:13:21 +0200830 if (debugmsg) {
831 rc = kwboot_debugmsg(tty, debugmsg);
832 if (rc) {
833 perror("debugmsg");
834 goto out;
835 }
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -0400836 } else if (bootmsg) {
Luka Perkovdff289642012-05-27 11:44:51 +0000837 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;
861out:
862 if (tty >= 0)
863 close(tty);
864
865 if (img)
866 munmap(img, size);
867
868 return rv;
869
870usage:
871 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
872 goto out;
873}