blob: 22cdd137ad5a51530e3d15c7ed18ff8bb139f738 [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"
14
Luka Perkovd131ad62012-05-27 11:44:51 +000015#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <stdarg.h>
Stefan Roesef4db6c92016-01-07 14:12:04 +010019#include <image.h>
Luka Perkovd131ad62012-05-27 11:44:51 +000020#include <libgen.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <unistd.h>
24#include <stdint.h>
25#include <termios.h>
26#include <sys/mman.h>
27#include <sys/stat.h>
28
Luka Perkovd131ad62012-05-27 11:44:51 +000029/*
30 * Marvell BootROM UART Sensing
31 */
32
33static unsigned char kwboot_msg_boot[] = {
34 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
35};
36
Stefan Roese84899e22014-10-22 12:13:21 +020037static unsigned char kwboot_msg_debug[] = {
38 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
39};
40
41/* Defines known to work on Kirkwood */
Luka Perkovd131ad62012-05-27 11:44:51 +000042#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
43#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
44
Stefan Roese84899e22014-10-22 12:13:21 +020045/* Defines known to work on Armada XP */
46#define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
47#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
48
Luka Perkovd131ad62012-05-27 11:44:51 +000049/*
50 * Xmodem Transfers
51 */
52
53#define SOH 1 /* sender start of block header */
54#define EOT 4 /* sender end of block transfer */
55#define ACK 6 /* target block ack */
56#define NAK 21 /* target block negative ack */
57#define CAN 24 /* target/sender transfer cancellation */
58
59struct kwboot_block {
60 uint8_t soh;
61 uint8_t pnum;
62 uint8_t _pnum;
63 uint8_t data[128];
64 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +020065} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000066
67#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
68
69static int kwboot_verbose;
70
Stefan Roese84899e22014-10-22 12:13:21 +020071static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
72static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +000073static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +020074
Marek Behúne453bb42021-09-24 23:06:41 +020075static ssize_t
76kwboot_write(int fd, const char *buf, size_t len)
77{
78 size_t tot = 0;
79
80 while (tot < len) {
81 ssize_t wr = write(fd, buf + tot, len - tot);
82
83 if (wr < 0)
84 return -1;
85
86 tot += wr;
87 }
88
89 return tot;
90}
91
Luka Perkovd131ad62012-05-27 11:44:51 +000092static void
93kwboot_printv(const char *fmt, ...)
94{
95 va_list ap;
96
97 if (kwboot_verbose) {
98 va_start(ap, fmt);
99 vprintf(fmt, ap);
100 va_end(ap);
101 fflush(stdout);
102 }
103}
104
105static void
106__spinner(void)
107{
108 const char seq[] = { '-', '\\', '|', '/' };
109 const int div = 8;
110 static int state, bs;
111
112 if (state % div == 0) {
113 fputc(bs, stdout);
114 fputc(seq[state / div % sizeof(seq)], stdout);
115 fflush(stdout);
116 }
117
118 bs = '\b';
119 state++;
120}
121
122static void
123kwboot_spinner(void)
124{
125 if (kwboot_verbose)
126 __spinner();
127}
128
129static void
130__progress(int pct, char c)
131{
132 const int width = 70;
133 static const char *nl = "";
134 static int pos;
135
136 if (pos % width == 0)
137 printf("%s%3d %% [", nl, pct);
138
139 fputc(c, stdout);
140
141 nl = "]\n";
142 pos++;
143
144 if (pct == 100) {
145 while (pos++ < width)
146 fputc(' ', stdout);
147 fputs(nl, stdout);
148 }
149
150 fflush(stdout);
151
152}
153
154static void
155kwboot_progress(int _pct, char c)
156{
157 static int pct;
158
159 if (_pct != -1)
160 pct = _pct;
161
162 if (kwboot_verbose)
163 __progress(pct, c);
164}
165
166static int
167kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
168{
169 int rc, nfds;
170 fd_set rfds;
171 struct timeval tv;
172 ssize_t n;
173
174 rc = -1;
175
176 FD_ZERO(&rfds);
177 FD_SET(fd, &rfds);
178
179 tv.tv_sec = 0;
180 tv.tv_usec = timeo * 1000;
181 if (tv.tv_usec > 1000000) {
182 tv.tv_sec += tv.tv_usec / 1000000;
183 tv.tv_usec %= 1000000;
184 }
185
186 do {
187 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
188 if (nfds < 0)
189 goto out;
190 if (!nfds) {
191 errno = ETIMEDOUT;
192 goto out;
193 }
194
195 n = read(fd, buf, len);
Willy Tarreau4469bd72018-07-03 12:10:31 -0400196 if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000197 goto out;
198
199 buf = (char *)buf + n;
200 len -= n;
201 } while (len > 0);
202
203 rc = 0;
204out:
205 return rc;
206}
207
208static int
209kwboot_tty_send(int fd, const void *buf, size_t len)
210{
Stefan Roese84899e22014-10-22 12:13:21 +0200211 if (!buf)
212 return 0;
213
Marek Behúne453bb42021-09-24 23:06:41 +0200214 if (kwboot_write(fd, buf, len) < 0)
215 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000216
Marek Behúne453bb42021-09-24 23:06:41 +0200217 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000218}
219
220static int
221kwboot_tty_send_char(int fd, unsigned char c)
222{
223 return kwboot_tty_send(fd, &c, 1);
224}
225
226static speed_t
227kwboot_tty_speed(int baudrate)
228{
229 switch (baudrate) {
230 case 115200:
231 return B115200;
232 case 57600:
233 return B57600;
234 case 38400:
235 return B38400;
236 case 19200:
237 return B19200;
238 case 9600:
239 return B9600;
240 }
241
242 return -1;
243}
244
245static int
246kwboot_open_tty(const char *path, speed_t speed)
247{
248 int rc, fd;
249 struct termios tio;
250
251 rc = -1;
252
253 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
254 if (fd < 0)
255 goto out;
256
257 memset(&tio, 0, sizeof(tio));
258
259 tio.c_iflag = 0;
260 tio.c_cflag = CREAD|CLOCAL|CS8;
261
262 tio.c_cc[VMIN] = 1;
263 tio.c_cc[VTIME] = 10;
264
265 cfsetospeed(&tio, speed);
266 cfsetispeed(&tio, speed);
267
268 rc = tcsetattr(fd, TCSANOW, &tio);
269 if (rc)
270 goto out;
271
272 rc = fd;
273out:
274 if (rc < 0) {
275 if (fd >= 0)
276 close(fd);
277 }
278
279 return rc;
280}
281
282static int
283kwboot_bootmsg(int tty, void *msg)
284{
285 int rc;
286 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300287 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000288
Stefan Roese84899e22014-10-22 12:13:21 +0200289 if (msg == NULL)
290 kwboot_printv("Please reboot the target into UART boot mode...");
291 else
292 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000293
294 do {
295 rc = tcflush(tty, TCIOFLUSH);
296 if (rc)
297 break;
298
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300299 for (count = 0; count < 128; count++) {
300 rc = kwboot_tty_send(tty, msg, 8);
301 if (rc) {
302 usleep(msg_req_delay * 1000);
303 continue;
304 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000305 }
306
Stefan Roese84899e22014-10-22 12:13:21 +0200307 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000308
309 kwboot_spinner();
310
311 } while (rc || c != NAK);
312
313 kwboot_printv("\n");
314
315 return rc;
316}
317
318static int
Stefan Roese84899e22014-10-22 12:13:21 +0200319kwboot_debugmsg(int tty, void *msg)
320{
321 int rc;
322
323 kwboot_printv("Sending debug message. Please reboot the target...");
324
325 do {
326 char buf[16];
327
328 rc = tcflush(tty, TCIOFLUSH);
329 if (rc)
330 break;
331
332 rc = kwboot_tty_send(tty, msg, 8);
333 if (rc) {
334 usleep(msg_req_delay * 1000);
335 continue;
336 }
337
338 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
339
340 kwboot_spinner();
341
342 } while (rc);
343
344 kwboot_printv("\n");
345
346 return rc;
347}
348
349static int
Luka Perkovd131ad62012-05-27 11:44:51 +0000350kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
351 size_t size, int pnum)
352{
353 const size_t blksz = sizeof(block->data);
354 size_t n;
355 int i;
356
Stefan Roese84899e22014-10-22 12:13:21 +0200357 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000358 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
372static int
373kwboot_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 break;
383
Stefan Roese84899e22014-10-22 12:13:21 +0200384 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000385 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
Stefan Roese84899e22014-10-22 12:13:21 +0200386 if (rc)
387 break;
388
389 if (c != ACK && c != NAK && c != CAN)
390 printf("%c", c);
391
392 } while (c != ACK && c != NAK && c != CAN);
Luka Perkovd131ad62012-05-27 11:44:51 +0000393
394 if (c != ACK)
395 kwboot_progress(-1, '+');
396
397 } while (c == NAK && retries-- > 0);
398
399 rc = -1;
400
401 switch (c) {
402 case ACK:
403 rc = 0;
404 break;
405 case NAK:
406 errno = EBADMSG;
407 break;
408 case CAN:
409 errno = ECANCELED;
410 break;
411 default:
412 errno = EPROTO;
413 break;
414 }
415
416 return rc;
417}
418
419static int
420kwboot_xmodem(int tty, const void *_data, size_t size)
421{
422 const uint8_t *data = _data;
423 int rc, pnum, N, err;
424
425 pnum = 1;
426 N = 0;
427
428 kwboot_printv("Sending boot image...\n");
429
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300430 sleep(2); /* flush isn't effective without it */
431 tcflush(tty, TCIOFLUSH);
432
Luka Perkovd131ad62012-05-27 11:44:51 +0000433 do {
434 struct kwboot_block block;
435 int n;
436
437 n = kwboot_xm_makeblock(&block,
438 data + N, size - N,
439 pnum++);
440 if (n < 0)
441 goto can;
442
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
456out:
457 return rc;
458
459can:
460 err = errno;
461 kwboot_tty_send_char(tty, CAN);
462 errno = err;
463 goto out;
464}
465
466static int
Marek Behún46237e62021-09-24 23:06:40 +0200467kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +0000468{
Marek Behúne453bb42021-09-24 23:06:41 +0200469 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +0000470 char _buf[128], *buf = _buf;
471
Pali Rohár43fef8d2021-07-23 11:14:17 +0200472 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -0400473 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000474 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--;
Pali Rohárb943eee2021-07-23 11:14:20 +0200486 } else {
Marek Behúne453bb42021-09-24 23:06:41 +0200487 if (kwboot_write(out, quit, *s) < 0)
488 return -1;
489 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +0200490 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000491 }
492 }
493
Marek Behúne453bb42021-09-24 23:06:41 +0200494 if (kwboot_write(out, buf, nin) < 0)
495 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000496
497 return 0;
498}
499
500static int
501kwboot_terminal(int tty)
502{
503 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +0200504 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +0000505 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
Marek Behúnf30cb0d2021-09-24 23:06:39 +0200552 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000553 rc = kwboot_term_pipe(in, tty, quit, &s);
554 if (rc)
555 break;
556 }
557 } while (quit[s] != 0);
558
Pali Rohárec0fe5b2021-07-23 11:14:18 +0200559 if (in >= 0)
560 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +0200561 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000562out:
563 return rc;
564}
565
566static void *
567kwboot_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;
Luka Perkovd131ad62012-05-27 11:44:51 +0000574 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;
594out:
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
605static uint8_t
606kwboot_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
616static int
617kwboot_img_patch_hdr(void *img, size_t size)
618{
619 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200620 struct main_hdr_v1 *hdr;
Luka Perkovd131ad62012-05-27 11:44:51 +0000621 uint8_t csum;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200622 size_t hdrsz = sizeof(*hdr);
623 int image_ver;
Luka Perkovd131ad62012-05-27 11:44:51 +0000624
625 rc = -1;
626 hdr = img;
627
628 if (size < hdrsz) {
629 errno = EINVAL;
630 goto out;
631 }
632
Stefan Roesee29f1db2015-09-29 09:19:59 +0200633 image_ver = image_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +0200634 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +0200635 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
Pali Rohár825a2ca2021-07-23 11:14:21 +0200645 if (size < hdrsz) {
646 errno = EINVAL;
647 goto out;
648 }
649
Stefan Roesee29f1db2015-09-29 09:19:59 +0200650 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
651 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000652 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
Stefan Roesee29f1db2015-09-29 09:19:59 +0200663 if (image_ver == 0) {
664 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovd131ad62012-05-27 11:44:51 +0000665
Stefan Roesee29f1db2015-09-29 09:19:59 +0200666 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
667 hdr_v0->nandpagesize = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000668
Stefan Roesee29f1db2015-09-29 09:19:59 +0200669 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;
Luka Perkovd131ad62012-05-27 11:44:51 +0000675
676 rc = 0;
677out:
678 return rc;
679}
680
681static void
682kwboot_usage(FILE *stream, char *progname)
683{
684 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +0000685 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +0200686 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +0000687 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200688 fprintf(stream,
689 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000690 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200691 fprintf(stream,
692 " -D <image>: boot <image> without preamble (Dove)\n");
693 fprintf(stream, " -d: enter debug mode\n");
694 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200695 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
696 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +0000697 fprintf(stream,
698 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000699 fprintf(stream, "\n");
700 fprintf(stream, " -t: mini terminal\n");
701 fprintf(stream, "\n");
702 fprintf(stream, " -B <baud>: set baud rate\n");
703 fprintf(stream, "\n");
704}
705
706int
707main(int argc, char **argv)
708{
709 const char *ttypath, *imgpath;
710 int rv, rc, tty, term, prot, patch;
711 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +0200712 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +0000713 void *img;
714 size_t size;
715 speed_t speed;
716
717 rv = 1;
718 tty = -1;
719 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +0200720 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +0000721 imgpath = NULL;
722 img = NULL;
723 term = 0;
724 patch = 0;
725 size = 0;
726 speed = B115200;
727
728 kwboot_verbose = isatty(STDOUT_FILENO);
729
730 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000731 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +0000732 if (c < 0)
733 break;
734
735 switch (c) {
736 case 'b':
737 bootmsg = kwboot_msg_boot;
738 imgpath = optarg;
739 break;
740
Stefan Roese84899e22014-10-22 12:13:21 +0200741 case 'D':
742 bootmsg = NULL;
743 imgpath = optarg;
744 break;
745
746 case 'd':
747 debugmsg = kwboot_msg_debug;
748 break;
749
Luka Perkovd131ad62012-05-27 11:44:51 +0000750 case 'p':
751 patch = 1;
752 break;
753
754 case 't':
755 term = 1;
756 break;
757
Stefan Roese84899e22014-10-22 12:13:21 +0200758 case 'a':
759 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
760 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
761 break;
762
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200763 case 'q':
764 msg_req_delay = atoi(optarg);
765 break;
766
767 case 's':
768 msg_rsp_timeo = atoi(optarg);
769 break;
770
Kevin Smith7497a6a2016-02-16 21:28:19 +0000771 case 'o':
772 blk_rsp_timeo = atoi(optarg);
773 break;
774
Luka Perkovd131ad62012-05-27 11:44:51 +0000775 case 'B':
776 speed = kwboot_tty_speed(atoi(optarg));
777 if (speed == -1)
778 goto usage;
779 break;
780
781 case 'h':
782 rv = 0;
783 default:
784 goto usage;
785 }
786 } while (1);
787
Stefan Roese84899e22014-10-22 12:13:21 +0200788 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +0000789 goto usage;
790
791 if (patch && !imgpath)
792 goto usage;
793
794 if (argc - optind < 1)
795 goto usage;
796
797 ttypath = argv[optind++];
798
799 tty = kwboot_open_tty(ttypath, speed);
800 if (tty < 0) {
801 perror(ttypath);
802 goto out;
803 }
804
805 if (imgpath) {
806 prot = PROT_READ | (patch ? PROT_WRITE : 0);
807
808 img = kwboot_mmap_image(imgpath, &size, prot);
809 if (!img) {
810 perror(imgpath);
811 goto out;
812 }
813 }
814
815 if (patch) {
816 rc = kwboot_img_patch_hdr(img, size);
817 if (rc) {
818 fprintf(stderr, "%s: Invalid image.\n", imgpath);
819 goto out;
820 }
821 }
822
Stefan Roese84899e22014-10-22 12:13:21 +0200823 if (debugmsg) {
824 rc = kwboot_debugmsg(tty, debugmsg);
825 if (rc) {
826 perror("debugmsg");
827 goto out;
828 }
Willy Tarreau3475a712018-07-03 12:10:30 -0400829 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000830 rc = kwboot_bootmsg(tty, bootmsg);
831 if (rc) {
832 perror("bootmsg");
833 goto out;
834 }
835 }
836
837 if (img) {
838 rc = kwboot_xmodem(tty, img, size);
839 if (rc) {
840 perror("xmodem");
841 goto out;
842 }
843 }
844
845 if (term) {
846 rc = kwboot_terminal(tty);
847 if (rc && !(errno == EINTR)) {
848 perror("terminal");
849 goto out;
850 }
851 }
852
853 rv = 0;
854out:
855 if (tty >= 0)
856 close(tty);
857
858 if (img)
859 munmap(img, size);
860
861 return rv;
862
863usage:
864 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
865 goto out;
866}