blob: eb4b3fe2305b600608fb3828b4e3c1120fdcf88c [file] [log] [blame]
Luka Perkovd131ad62012-05-27 11:44:51 +00001/*
Stefan Roese84899e22014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
3 * supports Kirkwood, Dove, Armada 370, Armada XP
Luka Perkovd131ad62012-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 Roesef4db6c92016-01-07 14:12:04 +010012#include "kwbimage.h"
13#include "mkimage.h"
Pali Rohára050a862021-09-24 23:06:42 +020014#include "version.h"
Stefan Roesef4db6c92016-01-07 14:12:04 +010015
Luka Perkovd131ad62012-05-27 11:44:51 +000016#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdarg.h>
Stefan Roesef4db6c92016-01-07 14:12:04 +010020#include <image.h>
Luka Perkovd131ad62012-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 Perkovd131ad62012-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 Roese84899e22014-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 Perkovd131ad62012-05-27 11:44:51 +000043#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
44#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
45
Stefan Roese84899e22014-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 Perkovd131ad62012-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ára107c612021-07-23 11:14:14 +020066} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000067
68#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
69
70static int kwboot_verbose;
71
Stefan Roese84899e22014-10-22 12:13:21 +020072static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
73static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +000074static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +020075
Marek Behúne453bb42021-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 Perkovd131ad62012-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ár5a1f8cb2021-09-24 23:06:46 +0200143 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000144
145 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200146 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000147 fputc(' ', stdout);
148 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200149 nl = "";
150 pos = 0;
Luka Perkovd131ad62012-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ár5a1f8cb2021-09-24 23:06:46 +0200167
168 if (pct == 100)
169 pct = 0;
Luka Perkovd131ad62012-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 Tarreau4469bd72018-07-03 12:10:31 -0400202 if (n <= 0)
Luka Perkovd131ad62012-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 Roese84899e22014-10-22 12:13:21 +0200217 if (!buf)
218 return 0;
219
Marek Behúne453bb42021-09-24 23:06:41 +0200220 if (kwboot_write(fd, buf, len) < 0)
221 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000222
Marek Behúne453bb42021-09-24 23:06:41 +0200223 return tcdrain(fd);
Luka Perkovd131ad62012-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 Nettleton9ca6fae2018-08-13 18:24:38 +0300293 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000294
Stefan Roese84899e22014-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 Perkovd131ad62012-05-27 11:44:51 +0000299
300 do {
301 rc = tcflush(tty, TCIOFLUSH);
302 if (rc)
303 break;
304
Jon Nettleton9ca6fae2018-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 Perkovd131ad62012-05-27 11:44:51 +0000311 }
312
Stefan Roese84899e22014-10-22 12:13:21 +0200313 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-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 Roese84899e22014-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árc5d666a2021-09-24 23:06:44 +0200355static size_t
Luka Perkovd131ad62012-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únd8cc8512021-09-24 23:06:45 +0200360 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000361
Stefan Roese84899e22014-10-22 12:13:21 +0200362 block->soh = SOH;
Luka Perkovd131ad62012-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ár00a1dee2021-09-24 23:06:43 +0200387 return rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000388
Stefan Roese84899e22014-10-22 12:13:21 +0200389 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000390 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
Pali Rohár00a1dee2021-09-24 23:06:43 +0200391 if (rc) {
392 if (errno != ETIMEDOUT)
393 return rc;
394 c = NAK;
395 }
Stefan Roese84899e22014-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 Perkovd131ad62012-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 Nettleton9ca6fae2018-08-13 18:24:38 +0300438 sleep(2); /* flush isn't effective without it */
439 tcflush(tty, TCIOFLUSH);
440
Luka Perkovd131ad62012-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 Perkovd131ad62012-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:
462 return rc;
463
464can:
465 err = errno;
466 kwboot_tty_send_char(tty, CAN);
467 errno = err;
468 goto out;
469}
470
471static int
Marek Behún46237e62021-09-24 23:06:40 +0200472kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +0000473{
Marek Behúne453bb42021-09-24 23:06:41 +0200474 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +0000475 char _buf[128], *buf = _buf;
476
Pali Rohár43fef8d2021-07-23 11:14:17 +0200477 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -0400478 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000479 return -1;
480
481 if (quit) {
482 int i;
483
484 for (i = 0; i < nin; i++) {
485 if (*buf == quit[*s]) {
486 (*s)++;
487 if (!quit[*s])
488 return 0;
489 buf++;
490 nin--;
Pali Rohárb943eee2021-07-23 11:14:20 +0200491 } else {
Marek Behúne453bb42021-09-24 23:06:41 +0200492 if (kwboot_write(out, quit, *s) < 0)
493 return -1;
494 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +0200495 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000496 }
497 }
498
Marek Behúne453bb42021-09-24 23:06:41 +0200499 if (kwboot_write(out, buf, nin) < 0)
500 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000501
502 return 0;
503}
504
505static int
506kwboot_terminal(int tty)
507{
508 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +0200509 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +0000510 struct termios otio, tio;
511
512 rc = -1;
513
514 in = STDIN_FILENO;
515 if (isatty(in)) {
516 rc = tcgetattr(in, &otio);
517 if (!rc) {
518 tio = otio;
519 cfmakeraw(&tio);
520 rc = tcsetattr(in, TCSANOW, &tio);
521 }
522 if (rc) {
523 perror("tcsetattr");
524 goto out;
525 }
526
527 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
528 quit[0]|0100, quit[1]);
529 } else
530 in = -1;
531
532 rc = 0;
533 s = 0;
534
535 do {
536 fd_set rfds;
537 int nfds = 0;
538
539 FD_SET(tty, &rfds);
540 nfds = nfds < tty ? tty : nfds;
541
542 if (in >= 0) {
543 FD_SET(in, &rfds);
544 nfds = nfds < in ? in : nfds;
545 }
546
547 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
548 if (nfds < 0)
549 break;
550
551 if (FD_ISSET(tty, &rfds)) {
552 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
553 if (rc)
554 break;
555 }
556
Marek Behúnf30cb0d2021-09-24 23:06:39 +0200557 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000558 rc = kwboot_term_pipe(in, tty, quit, &s);
559 if (rc)
560 break;
561 }
562 } while (quit[s] != 0);
563
Pali Rohárec0fe5b2021-07-23 11:14:18 +0200564 if (in >= 0)
565 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +0200566 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000567out:
568 return rc;
569}
570
571static void *
572kwboot_mmap_image(const char *path, size_t *size, int prot)
573{
574 int rc, fd, flags;
575 struct stat st;
576 void *img;
577
578 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000579 img = NULL;
580
581 fd = open(path, O_RDONLY);
582 if (fd < 0)
583 goto out;
584
585 rc = fstat(fd, &st);
586 if (rc)
587 goto out;
588
589 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
590
591 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
592 if (img == MAP_FAILED) {
593 img = NULL;
594 goto out;
595 }
596
597 rc = 0;
598 *size = st.st_size;
599out:
600 if (rc && img) {
601 munmap(img, st.st_size);
602 img = NULL;
603 }
604 if (fd >= 0)
605 close(fd);
606
607 return img;
608}
609
610static uint8_t
611kwboot_img_csum8(void *_data, size_t size)
612{
613 uint8_t *data = _data, csum;
614
615 for (csum = 0; size-- > 0; data++)
616 csum += *data;
617
618 return csum;
619}
620
621static int
622kwboot_img_patch_hdr(void *img, size_t size)
623{
624 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200625 struct main_hdr_v1 *hdr;
Luka Perkovd131ad62012-05-27 11:44:51 +0000626 uint8_t csum;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200627 size_t hdrsz = sizeof(*hdr);
628 int image_ver;
Luka Perkovd131ad62012-05-27 11:44:51 +0000629
630 rc = -1;
631 hdr = img;
632
633 if (size < hdrsz) {
634 errno = EINVAL;
635 goto out;
636 }
637
Stefan Roesee29f1db2015-09-29 09:19:59 +0200638 image_ver = image_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +0200639 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +0200640 fprintf(stderr, "Invalid image header version\n");
641 errno = EINVAL;
642 goto out;
643 }
644
645 if (image_ver == 0)
646 hdrsz = sizeof(*hdr);
647 else
648 hdrsz = KWBHEADER_V1_SIZE(hdr);
649
Pali Rohár825a2ca2021-07-23 11:14:21 +0200650 if (size < hdrsz) {
651 errno = EINVAL;
652 goto out;
653 }
654
Stefan Roesee29f1db2015-09-29 09:19:59 +0200655 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
656 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000657 errno = EINVAL;
658 goto out;
659 }
660
661 if (hdr->blockid == IBR_HDR_UART_ID) {
662 rc = 0;
663 goto out;
664 }
665
666 hdr->blockid = IBR_HDR_UART_ID;
667
Stefan Roesee29f1db2015-09-29 09:19:59 +0200668 if (image_ver == 0) {
669 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovd131ad62012-05-27 11:44:51 +0000670
Stefan Roesee29f1db2015-09-29 09:19:59 +0200671 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
672 hdr_v0->nandpagesize = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000673
Stefan Roesee29f1db2015-09-29 09:19:59 +0200674 hdr_v0->srcaddr = hdr_v0->ext
675 ? sizeof(struct kwb_header)
676 : sizeof(*hdr_v0);
677 }
678
679 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +0000680
681 rc = 0;
682out:
683 return rc;
684}
685
686static void
687kwboot_usage(FILE *stream, char *progname)
688{
Pali Rohára050a862021-09-24 23:06:42 +0200689 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovd131ad62012-05-27 11:44:51 +0000690 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +0000691 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +0200692 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +0000693 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200694 fprintf(stream,
695 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000696 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200697 fprintf(stream,
698 " -D <image>: boot <image> without preamble (Dove)\n");
699 fprintf(stream, " -d: enter debug mode\n");
700 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200701 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
702 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +0000703 fprintf(stream,
704 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000705 fprintf(stream, "\n");
706 fprintf(stream, " -t: mini terminal\n");
707 fprintf(stream, "\n");
708 fprintf(stream, " -B <baud>: set baud rate\n");
709 fprintf(stream, "\n");
710}
711
712int
713main(int argc, char **argv)
714{
715 const char *ttypath, *imgpath;
716 int rv, rc, tty, term, prot, patch;
717 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +0200718 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +0000719 void *img;
720 size_t size;
721 speed_t speed;
722
723 rv = 1;
724 tty = -1;
725 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +0200726 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +0000727 imgpath = NULL;
728 img = NULL;
729 term = 0;
730 patch = 0;
731 size = 0;
732 speed = B115200;
733
734 kwboot_verbose = isatty(STDOUT_FILENO);
735
736 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000737 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +0000738 if (c < 0)
739 break;
740
741 switch (c) {
742 case 'b':
743 bootmsg = kwboot_msg_boot;
744 imgpath = optarg;
745 break;
746
Stefan Roese84899e22014-10-22 12:13:21 +0200747 case 'D':
748 bootmsg = NULL;
749 imgpath = optarg;
750 break;
751
752 case 'd':
753 debugmsg = kwboot_msg_debug;
754 break;
755
Luka Perkovd131ad62012-05-27 11:44:51 +0000756 case 'p':
757 patch = 1;
758 break;
759
760 case 't':
761 term = 1;
762 break;
763
Stefan Roese84899e22014-10-22 12:13:21 +0200764 case 'a':
765 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
766 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
767 break;
768
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200769 case 'q':
770 msg_req_delay = atoi(optarg);
771 break;
772
773 case 's':
774 msg_rsp_timeo = atoi(optarg);
775 break;
776
Kevin Smith7497a6a2016-02-16 21:28:19 +0000777 case 'o':
778 blk_rsp_timeo = atoi(optarg);
779 break;
780
Luka Perkovd131ad62012-05-27 11:44:51 +0000781 case 'B':
782 speed = kwboot_tty_speed(atoi(optarg));
783 if (speed == -1)
784 goto usage;
785 break;
786
787 case 'h':
788 rv = 0;
789 default:
790 goto usage;
791 }
792 } while (1);
793
Stefan Roese84899e22014-10-22 12:13:21 +0200794 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +0000795 goto usage;
796
797 if (patch && !imgpath)
798 goto usage;
799
800 if (argc - optind < 1)
801 goto usage;
802
803 ttypath = argv[optind++];
804
805 tty = kwboot_open_tty(ttypath, speed);
806 if (tty < 0) {
807 perror(ttypath);
808 goto out;
809 }
810
811 if (imgpath) {
812 prot = PROT_READ | (patch ? PROT_WRITE : 0);
813
814 img = kwboot_mmap_image(imgpath, &size, prot);
815 if (!img) {
816 perror(imgpath);
817 goto out;
818 }
819 }
820
821 if (patch) {
822 rc = kwboot_img_patch_hdr(img, size);
823 if (rc) {
824 fprintf(stderr, "%s: Invalid image.\n", imgpath);
825 goto out;
826 }
827 }
828
Stefan Roese84899e22014-10-22 12:13:21 +0200829 if (debugmsg) {
830 rc = kwboot_debugmsg(tty, debugmsg);
831 if (rc) {
832 perror("debugmsg");
833 goto out;
834 }
Willy Tarreau3475a712018-07-03 12:10:30 -0400835 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000836 rc = kwboot_bootmsg(tty, bootmsg);
837 if (rc) {
838 perror("bootmsg");
839 goto out;
840 }
841 }
842
843 if (img) {
844 rc = kwboot_xmodem(tty, img, size);
845 if (rc) {
846 perror("xmodem");
847 goto out;
848 }
849 }
850
851 if (term) {
852 rc = kwboot_terminal(tty);
853 if (rc && !(errno == EINTR)) {
854 perror("terminal");
855 goto out;
856 }
857 }
858
859 rv = 0;
860out:
861 if (tty >= 0)
862 close(tty);
863
864 if (img)
865 munmap(img, size);
866
867 return rv;
868
869usage:
870 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
871 goto out;
872}