blob: 941f4228f9828f13831a0c934bf12f3fc717f689 [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
Luka Perkovd131ad62012-05-27 11:44:51 +000075static void
76kwboot_printv(const char *fmt, ...)
77{
78 va_list ap;
79
80 if (kwboot_verbose) {
81 va_start(ap, fmt);
82 vprintf(fmt, ap);
83 va_end(ap);
84 fflush(stdout);
85 }
86}
87
88static void
89__spinner(void)
90{
91 const char seq[] = { '-', '\\', '|', '/' };
92 const int div = 8;
93 static int state, bs;
94
95 if (state % div == 0) {
96 fputc(bs, stdout);
97 fputc(seq[state / div % sizeof(seq)], stdout);
98 fflush(stdout);
99 }
100
101 bs = '\b';
102 state++;
103}
104
105static void
106kwboot_spinner(void)
107{
108 if (kwboot_verbose)
109 __spinner();
110}
111
112static void
113__progress(int pct, char c)
114{
115 const int width = 70;
116 static const char *nl = "";
117 static int pos;
118
119 if (pos % width == 0)
120 printf("%s%3d %% [", nl, pct);
121
122 fputc(c, stdout);
123
124 nl = "]\n";
125 pos++;
126
127 if (pct == 100) {
128 while (pos++ < width)
129 fputc(' ', stdout);
130 fputs(nl, stdout);
131 }
132
133 fflush(stdout);
134
135}
136
137static void
138kwboot_progress(int _pct, char c)
139{
140 static int pct;
141
142 if (_pct != -1)
143 pct = _pct;
144
145 if (kwboot_verbose)
146 __progress(pct, c);
147}
148
149static int
150kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
151{
152 int rc, nfds;
153 fd_set rfds;
154 struct timeval tv;
155 ssize_t n;
156
157 rc = -1;
158
159 FD_ZERO(&rfds);
160 FD_SET(fd, &rfds);
161
162 tv.tv_sec = 0;
163 tv.tv_usec = timeo * 1000;
164 if (tv.tv_usec > 1000000) {
165 tv.tv_sec += tv.tv_usec / 1000000;
166 tv.tv_usec %= 1000000;
167 }
168
169 do {
170 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
171 if (nfds < 0)
172 goto out;
173 if (!nfds) {
174 errno = ETIMEDOUT;
175 goto out;
176 }
177
178 n = read(fd, buf, len);
Willy Tarreau4469bd72018-07-03 12:10:31 -0400179 if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000180 goto out;
181
182 buf = (char *)buf + n;
183 len -= n;
184 } while (len > 0);
185
186 rc = 0;
187out:
188 return rc;
189}
190
191static int
192kwboot_tty_send(int fd, const void *buf, size_t len)
193{
194 int rc;
195 ssize_t n;
196
Stefan Roese84899e22014-10-22 12:13:21 +0200197 if (!buf)
198 return 0;
199
Luka Perkovd131ad62012-05-27 11:44:51 +0000200 rc = -1;
201
202 do {
203 n = write(fd, buf, len);
204 if (n < 0)
205 goto out;
206
207 buf = (char *)buf + n;
208 len -= n;
209 } while (len > 0);
210
211 rc = tcdrain(fd);
212out:
213 return rc;
214}
215
216static int
217kwboot_tty_send_char(int fd, unsigned char c)
218{
219 return kwboot_tty_send(fd, &c, 1);
220}
221
222static speed_t
223kwboot_tty_speed(int baudrate)
224{
225 switch (baudrate) {
226 case 115200:
227 return B115200;
228 case 57600:
229 return B57600;
230 case 38400:
231 return B38400;
232 case 19200:
233 return B19200;
234 case 9600:
235 return B9600;
236 }
237
238 return -1;
239}
240
241static int
242kwboot_open_tty(const char *path, speed_t speed)
243{
244 int rc, fd;
245 struct termios tio;
246
247 rc = -1;
248
249 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
250 if (fd < 0)
251 goto out;
252
253 memset(&tio, 0, sizeof(tio));
254
255 tio.c_iflag = 0;
256 tio.c_cflag = CREAD|CLOCAL|CS8;
257
258 tio.c_cc[VMIN] = 1;
259 tio.c_cc[VTIME] = 10;
260
261 cfsetospeed(&tio, speed);
262 cfsetispeed(&tio, speed);
263
264 rc = tcsetattr(fd, TCSANOW, &tio);
265 if (rc)
266 goto out;
267
268 rc = fd;
269out:
270 if (rc < 0) {
271 if (fd >= 0)
272 close(fd);
273 }
274
275 return rc;
276}
277
278static int
279kwboot_bootmsg(int tty, void *msg)
280{
281 int rc;
282 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300283 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000284
Stefan Roese84899e22014-10-22 12:13:21 +0200285 if (msg == NULL)
286 kwboot_printv("Please reboot the target into UART boot mode...");
287 else
288 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000289
290 do {
291 rc = tcflush(tty, TCIOFLUSH);
292 if (rc)
293 break;
294
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300295 for (count = 0; count < 128; count++) {
296 rc = kwboot_tty_send(tty, msg, 8);
297 if (rc) {
298 usleep(msg_req_delay * 1000);
299 continue;
300 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000301 }
302
Stefan Roese84899e22014-10-22 12:13:21 +0200303 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000304
305 kwboot_spinner();
306
307 } while (rc || c != NAK);
308
309 kwboot_printv("\n");
310
311 return rc;
312}
313
314static int
Stefan Roese84899e22014-10-22 12:13:21 +0200315kwboot_debugmsg(int tty, void *msg)
316{
317 int rc;
318
319 kwboot_printv("Sending debug message. Please reboot the target...");
320
321 do {
322 char buf[16];
323
324 rc = tcflush(tty, TCIOFLUSH);
325 if (rc)
326 break;
327
328 rc = kwboot_tty_send(tty, msg, 8);
329 if (rc) {
330 usleep(msg_req_delay * 1000);
331 continue;
332 }
333
334 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
335
336 kwboot_spinner();
337
338 } while (rc);
339
340 kwboot_printv("\n");
341
342 return rc;
343}
344
345static int
Luka Perkovd131ad62012-05-27 11:44:51 +0000346kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
347 size_t size, int pnum)
348{
349 const size_t blksz = sizeof(block->data);
350 size_t n;
351 int i;
352
Stefan Roese84899e22014-10-22 12:13:21 +0200353 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000354 block->pnum = pnum;
355 block->_pnum = ~block->pnum;
356
357 n = size < blksz ? size : blksz;
358 memcpy(&block->data[0], data, n);
359 memset(&block->data[n], 0, blksz - n);
360
361 block->csum = 0;
362 for (i = 0; i < n; i++)
363 block->csum += block->data[i];
364
365 return n;
366}
367
368static int
369kwboot_xm_sendblock(int fd, struct kwboot_block *block)
370{
371 int rc, retries;
372 char c;
373
374 retries = 16;
375 do {
376 rc = kwboot_tty_send(fd, block, sizeof(*block));
377 if (rc)
378 break;
379
Stefan Roese84899e22014-10-22 12:13:21 +0200380 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000381 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
Stefan Roese84899e22014-10-22 12:13:21 +0200382 if (rc)
383 break;
384
385 if (c != ACK && c != NAK && c != CAN)
386 printf("%c", c);
387
388 } while (c != ACK && c != NAK && c != CAN);
Luka Perkovd131ad62012-05-27 11:44:51 +0000389
390 if (c != ACK)
391 kwboot_progress(-1, '+');
392
393 } while (c == NAK && retries-- > 0);
394
395 rc = -1;
396
397 switch (c) {
398 case ACK:
399 rc = 0;
400 break;
401 case NAK:
402 errno = EBADMSG;
403 break;
404 case CAN:
405 errno = ECANCELED;
406 break;
407 default:
408 errno = EPROTO;
409 break;
410 }
411
412 return rc;
413}
414
415static int
416kwboot_xmodem(int tty, const void *_data, size_t size)
417{
418 const uint8_t *data = _data;
419 int rc, pnum, N, err;
420
421 pnum = 1;
422 N = 0;
423
424 kwboot_printv("Sending boot image...\n");
425
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300426 sleep(2); /* flush isn't effective without it */
427 tcflush(tty, TCIOFLUSH);
428
Luka Perkovd131ad62012-05-27 11:44:51 +0000429 do {
430 struct kwboot_block block;
431 int n;
432
433 n = kwboot_xm_makeblock(&block,
434 data + N, size - N,
435 pnum++);
436 if (n < 0)
437 goto can;
438
439 if (!n)
440 break;
441
442 rc = kwboot_xm_sendblock(tty, &block);
443 if (rc)
444 goto out;
445
446 N += n;
447 kwboot_progress(N * 100 / size, '.');
448 } while (1);
449
450 rc = kwboot_tty_send_char(tty, EOT);
451
452out:
453 return rc;
454
455can:
456 err = errno;
457 kwboot_tty_send_char(tty, CAN);
458 errno = err;
459 goto out;
460}
461
462static int
463kwboot_term_pipe(int in, int out, char *quit, int *s)
464{
465 ssize_t nin, nout;
466 char _buf[128], *buf = _buf;
467
Pali Rohár43fef8d2021-07-23 11:14:17 +0200468 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -0400469 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000470 return -1;
471
472 if (quit) {
473 int i;
474
475 for (i = 0; i < nin; i++) {
476 if (*buf == quit[*s]) {
477 (*s)++;
478 if (!quit[*s])
479 return 0;
480 buf++;
481 nin--;
482 } else
483 while (*s > 0) {
484 nout = write(out, quit, *s);
485 if (nout <= 0)
486 return -1;
487 (*s) -= nout;
488 }
489 }
490 }
491
492 while (nin > 0) {
493 nout = write(out, buf, nin);
494 if (nout <= 0)
495 return -1;
496 nin -= nout;
497 }
498
499 return 0;
500}
501
502static int
503kwboot_terminal(int tty)
504{
505 int rc, in, s;
506 char *quit = "\34c";
507 struct termios otio, tio;
508
509 rc = -1;
510
511 in = STDIN_FILENO;
512 if (isatty(in)) {
513 rc = tcgetattr(in, &otio);
514 if (!rc) {
515 tio = otio;
516 cfmakeraw(&tio);
517 rc = tcsetattr(in, TCSANOW, &tio);
518 }
519 if (rc) {
520 perror("tcsetattr");
521 goto out;
522 }
523
524 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
525 quit[0]|0100, quit[1]);
526 } else
527 in = -1;
528
529 rc = 0;
530 s = 0;
531
532 do {
533 fd_set rfds;
534 int nfds = 0;
535
536 FD_SET(tty, &rfds);
537 nfds = nfds < tty ? tty : nfds;
538
539 if (in >= 0) {
540 FD_SET(in, &rfds);
541 nfds = nfds < in ? in : nfds;
542 }
543
544 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
545 if (nfds < 0)
546 break;
547
548 if (FD_ISSET(tty, &rfds)) {
549 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
550 if (rc)
551 break;
552 }
553
554 if (FD_ISSET(in, &rfds)) {
555 rc = kwboot_term_pipe(in, tty, quit, &s);
556 if (rc)
557 break;
558 }
559 } while (quit[s] != 0);
560
Pali Rohárec0fe5b2021-07-23 11:14:18 +0200561 if (in >= 0)
562 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +0200563 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000564out:
565 return rc;
566}
567
568static void *
569kwboot_mmap_image(const char *path, size_t *size, int prot)
570{
571 int rc, fd, flags;
572 struct stat st;
573 void *img;
574
575 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000576 img = NULL;
577
578 fd = open(path, O_RDONLY);
579 if (fd < 0)
580 goto out;
581
582 rc = fstat(fd, &st);
583 if (rc)
584 goto out;
585
586 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
587
588 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
589 if (img == MAP_FAILED) {
590 img = NULL;
591 goto out;
592 }
593
594 rc = 0;
595 *size = st.st_size;
596out:
597 if (rc && img) {
598 munmap(img, st.st_size);
599 img = NULL;
600 }
601 if (fd >= 0)
602 close(fd);
603
604 return img;
605}
606
607static uint8_t
608kwboot_img_csum8(void *_data, size_t size)
609{
610 uint8_t *data = _data, csum;
611
612 for (csum = 0; size-- > 0; data++)
613 csum += *data;
614
615 return csum;
616}
617
618static int
619kwboot_img_patch_hdr(void *img, size_t size)
620{
621 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200622 struct main_hdr_v1 *hdr;
Luka Perkovd131ad62012-05-27 11:44:51 +0000623 uint8_t csum;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200624 size_t hdrsz = sizeof(*hdr);
625 int image_ver;
Luka Perkovd131ad62012-05-27 11:44:51 +0000626
627 rc = -1;
628 hdr = img;
629
630 if (size < hdrsz) {
631 errno = EINVAL;
632 goto out;
633 }
634
Stefan Roesee29f1db2015-09-29 09:19:59 +0200635 image_ver = image_version(img);
636 if (image_ver < 0) {
637 fprintf(stderr, "Invalid image header version\n");
638 errno = EINVAL;
639 goto out;
640 }
641
642 if (image_ver == 0)
643 hdrsz = sizeof(*hdr);
644 else
645 hdrsz = KWBHEADER_V1_SIZE(hdr);
646
647 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
648 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000649 errno = EINVAL;
650 goto out;
651 }
652
653 if (hdr->blockid == IBR_HDR_UART_ID) {
654 rc = 0;
655 goto out;
656 }
657
658 hdr->blockid = IBR_HDR_UART_ID;
659
Stefan Roesee29f1db2015-09-29 09:19:59 +0200660 if (image_ver == 0) {
661 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovd131ad62012-05-27 11:44:51 +0000662
Stefan Roesee29f1db2015-09-29 09:19:59 +0200663 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
664 hdr_v0->nandpagesize = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000665
Stefan Roesee29f1db2015-09-29 09:19:59 +0200666 hdr_v0->srcaddr = hdr_v0->ext
667 ? sizeof(struct kwb_header)
668 : sizeof(*hdr_v0);
669 }
670
671 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +0000672
673 rc = 0;
674out:
675 return rc;
676}
677
678static void
679kwboot_usage(FILE *stream, char *progname)
680{
681 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +0000682 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +0200683 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +0000684 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200685 fprintf(stream,
686 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000687 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200688 fprintf(stream,
689 " -D <image>: boot <image> without preamble (Dove)\n");
690 fprintf(stream, " -d: enter debug mode\n");
691 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200692 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
693 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +0000694 fprintf(stream,
695 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000696 fprintf(stream, "\n");
697 fprintf(stream, " -t: mini terminal\n");
698 fprintf(stream, "\n");
699 fprintf(stream, " -B <baud>: set baud rate\n");
700 fprintf(stream, "\n");
701}
702
703int
704main(int argc, char **argv)
705{
706 const char *ttypath, *imgpath;
707 int rv, rc, tty, term, prot, patch;
708 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +0200709 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +0000710 void *img;
711 size_t size;
712 speed_t speed;
713
714 rv = 1;
715 tty = -1;
716 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +0200717 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +0000718 imgpath = NULL;
719 img = NULL;
720 term = 0;
721 patch = 0;
722 size = 0;
723 speed = B115200;
724
725 kwboot_verbose = isatty(STDOUT_FILENO);
726
727 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000728 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +0000729 if (c < 0)
730 break;
731
732 switch (c) {
733 case 'b':
734 bootmsg = kwboot_msg_boot;
735 imgpath = optarg;
736 break;
737
Stefan Roese84899e22014-10-22 12:13:21 +0200738 case 'D':
739 bootmsg = NULL;
740 imgpath = optarg;
741 break;
742
743 case 'd':
744 debugmsg = kwboot_msg_debug;
745 break;
746
Luka Perkovd131ad62012-05-27 11:44:51 +0000747 case 'p':
748 patch = 1;
749 break;
750
751 case 't':
752 term = 1;
753 break;
754
Stefan Roese84899e22014-10-22 12:13:21 +0200755 case 'a':
756 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
757 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
758 break;
759
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200760 case 'q':
761 msg_req_delay = atoi(optarg);
762 break;
763
764 case 's':
765 msg_rsp_timeo = atoi(optarg);
766 break;
767
Kevin Smith7497a6a2016-02-16 21:28:19 +0000768 case 'o':
769 blk_rsp_timeo = atoi(optarg);
770 break;
771
Luka Perkovd131ad62012-05-27 11:44:51 +0000772 case 'B':
773 speed = kwboot_tty_speed(atoi(optarg));
774 if (speed == -1)
775 goto usage;
776 break;
777
778 case 'h':
779 rv = 0;
780 default:
781 goto usage;
782 }
783 } while (1);
784
Stefan Roese84899e22014-10-22 12:13:21 +0200785 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +0000786 goto usage;
787
788 if (patch && !imgpath)
789 goto usage;
790
791 if (argc - optind < 1)
792 goto usage;
793
794 ttypath = argv[optind++];
795
796 tty = kwboot_open_tty(ttypath, speed);
797 if (tty < 0) {
798 perror(ttypath);
799 goto out;
800 }
801
802 if (imgpath) {
803 prot = PROT_READ | (patch ? PROT_WRITE : 0);
804
805 img = kwboot_mmap_image(imgpath, &size, prot);
806 if (!img) {
807 perror(imgpath);
808 goto out;
809 }
810 }
811
812 if (patch) {
813 rc = kwboot_img_patch_hdr(img, size);
814 if (rc) {
815 fprintf(stderr, "%s: Invalid image.\n", imgpath);
816 goto out;
817 }
818 }
819
Stefan Roese84899e22014-10-22 12:13:21 +0200820 if (debugmsg) {
821 rc = kwboot_debugmsg(tty, debugmsg);
822 if (rc) {
823 perror("debugmsg");
824 goto out;
825 }
Willy Tarreau3475a712018-07-03 12:10:30 -0400826 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000827 rc = kwboot_bootmsg(tty, bootmsg);
828 if (rc) {
829 perror("bootmsg");
830 goto out;
831 }
832 }
833
834 if (img) {
835 rc = kwboot_xmodem(tty, img, size);
836 if (rc) {
837 perror("xmodem");
838 goto out;
839 }
840 }
841
842 if (term) {
843 rc = kwboot_terminal(tty);
844 if (rc && !(errno == EINTR)) {
845 perror("terminal");
846 goto out;
847 }
848 }
849
850 rv = 0;
851out:
852 if (tty >= 0)
853 close(tty);
854
855 if (img)
856 munmap(img, size);
857
858 return rv;
859
860usage:
861 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
862 goto out;
863}