blob: 2e5684b91c98395442d20d5737b179b7093cbc78 [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
Pali Rohárbed18ef2021-09-24 23:06:48 +020060#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
61
Luka Perkovdff289642012-05-27 11:44:51 +000062struct kwboot_block {
63 uint8_t soh;
64 uint8_t pnum;
65 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +020066 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +000067 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +020068} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +000069
70#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
71
72static int kwboot_verbose;
73
Stefan Roesebb5c4282014-10-22 12:13:21 +020074static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
75static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +000076static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +020077
Marek Behúnd3bc5c32021-09-24 23:06:41 +020078static ssize_t
79kwboot_write(int fd, const char *buf, size_t len)
80{
81 size_t tot = 0;
82
83 while (tot < len) {
84 ssize_t wr = write(fd, buf + tot, len - tot);
85
86 if (wr < 0)
87 return -1;
88
89 tot += wr;
90 }
91
92 return tot;
93}
94
Luka Perkovdff289642012-05-27 11:44:51 +000095static void
96kwboot_printv(const char *fmt, ...)
97{
98 va_list ap;
99
100 if (kwboot_verbose) {
101 va_start(ap, fmt);
102 vprintf(fmt, ap);
103 va_end(ap);
104 fflush(stdout);
105 }
106}
107
108static void
109__spinner(void)
110{
111 const char seq[] = { '-', '\\', '|', '/' };
112 const int div = 8;
113 static int state, bs;
114
115 if (state % div == 0) {
116 fputc(bs, stdout);
117 fputc(seq[state / div % sizeof(seq)], stdout);
118 fflush(stdout);
119 }
120
121 bs = '\b';
122 state++;
123}
124
125static void
126kwboot_spinner(void)
127{
128 if (kwboot_verbose)
129 __spinner();
130}
131
132static void
133__progress(int pct, char c)
134{
135 const int width = 70;
136 static const char *nl = "";
137 static int pos;
138
139 if (pos % width == 0)
140 printf("%s%3d %% [", nl, pct);
141
142 fputc(c, stdout);
143
144 nl = "]\n";
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200145 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000146
147 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200148 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000149 fputc(' ', stdout);
150 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200151 nl = "";
152 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000153 }
154
155 fflush(stdout);
156
157}
158
159static void
160kwboot_progress(int _pct, char c)
161{
162 static int pct;
163
164 if (_pct != -1)
165 pct = _pct;
166
167 if (kwboot_verbose)
168 __progress(pct, c);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200169
170 if (pct == 100)
171 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000172}
173
174static int
175kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
176{
177 int rc, nfds;
178 fd_set rfds;
179 struct timeval tv;
180 ssize_t n;
181
182 rc = -1;
183
184 FD_ZERO(&rfds);
185 FD_SET(fd, &rfds);
186
187 tv.tv_sec = 0;
188 tv.tv_usec = timeo * 1000;
189 if (tv.tv_usec > 1000000) {
190 tv.tv_sec += tv.tv_usec / 1000000;
191 tv.tv_usec %= 1000000;
192 }
193
194 do {
195 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
196 if (nfds < 0)
197 goto out;
198 if (!nfds) {
199 errno = ETIMEDOUT;
200 goto out;
201 }
202
203 n = read(fd, buf, len);
Willy Tarreauab7e8a12018-07-03 12:10:31 -0400204 if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000205 goto out;
206
207 buf = (char *)buf + n;
208 len -= n;
209 } while (len > 0);
210
211 rc = 0;
212out:
213 return rc;
214}
215
216static int
217kwboot_tty_send(int fd, const void *buf, size_t len)
218{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200219 if (!buf)
220 return 0;
221
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200222 if (kwboot_write(fd, buf, len) < 0)
223 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000224
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200225 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000226}
227
228static int
229kwboot_tty_send_char(int fd, unsigned char c)
230{
231 return kwboot_tty_send(fd, &c, 1);
232}
233
234static speed_t
235kwboot_tty_speed(int baudrate)
236{
237 switch (baudrate) {
238 case 115200:
239 return B115200;
240 case 57600:
241 return B57600;
242 case 38400:
243 return B38400;
244 case 19200:
245 return B19200;
246 case 9600:
247 return B9600;
248 }
249
250 return -1;
251}
252
253static int
254kwboot_open_tty(const char *path, speed_t speed)
255{
256 int rc, fd;
257 struct termios tio;
258
259 rc = -1;
260
261 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
262 if (fd < 0)
263 goto out;
264
265 memset(&tio, 0, sizeof(tio));
266
267 tio.c_iflag = 0;
268 tio.c_cflag = CREAD|CLOCAL|CS8;
269
270 tio.c_cc[VMIN] = 1;
271 tio.c_cc[VTIME] = 10;
272
273 cfsetospeed(&tio, speed);
274 cfsetispeed(&tio, speed);
275
276 rc = tcsetattr(fd, TCSANOW, &tio);
277 if (rc)
278 goto out;
279
280 rc = fd;
281out:
282 if (rc < 0) {
283 if (fd >= 0)
284 close(fd);
285 }
286
287 return rc;
288}
289
290static int
291kwboot_bootmsg(int tty, void *msg)
292{
293 int rc;
294 char c;
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300295 int count;
Luka Perkovdff289642012-05-27 11:44:51 +0000296
Stefan Roesebb5c4282014-10-22 12:13:21 +0200297 if (msg == NULL)
298 kwboot_printv("Please reboot the target into UART boot mode...");
299 else
300 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000301
302 do {
303 rc = tcflush(tty, TCIOFLUSH);
304 if (rc)
305 break;
306
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300307 for (count = 0; count < 128; count++) {
308 rc = kwboot_tty_send(tty, msg, 8);
309 if (rc) {
310 usleep(msg_req_delay * 1000);
311 continue;
312 }
Luka Perkovdff289642012-05-27 11:44:51 +0000313 }
314
Stefan Roesebb5c4282014-10-22 12:13:21 +0200315 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovdff289642012-05-27 11:44:51 +0000316
317 kwboot_spinner();
318
319 } while (rc || c != NAK);
320
321 kwboot_printv("\n");
322
323 return rc;
324}
325
326static int
Stefan Roesebb5c4282014-10-22 12:13:21 +0200327kwboot_debugmsg(int tty, void *msg)
328{
329 int rc;
330
331 kwboot_printv("Sending debug message. Please reboot the target...");
332
333 do {
334 char buf[16];
335
336 rc = tcflush(tty, TCIOFLUSH);
337 if (rc)
338 break;
339
340 rc = kwboot_tty_send(tty, msg, 8);
341 if (rc) {
342 usleep(msg_req_delay * 1000);
343 continue;
344 }
345
346 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
347
348 kwboot_spinner();
349
350 } while (rc);
351
352 kwboot_printv("\n");
353
354 return rc;
355}
356
Pali Rohár58cf04de2021-09-24 23:06:44 +0200357static size_t
Luka Perkovdff289642012-05-27 11:44:51 +0000358kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
359 size_t size, int pnum)
360{
Marek Behúnedb63242021-09-24 23:06:45 +0200361 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +0000362
Stefan Roesebb5c4282014-10-22 12:13:21 +0200363 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +0000364 block->pnum = pnum;
365 block->_pnum = ~block->pnum;
366
Pali Rohárbed18ef2021-09-24 23:06:48 +0200367 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +0000368 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +0200369 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-05-27 11:44:51 +0000370
371 block->csum = 0;
372 for (i = 0; i < n; i++)
373 block->csum += block->data[i];
374
375 return n;
376}
377
378static int
Marek Behúneabacb82021-09-24 23:06:49 +0200379_is_xm_reply(char c)
380{
381 return c == ACK || c == NAK || c == CAN;
382}
383
384static int
Luka Perkovdff289642012-05-27 11:44:51 +0000385kwboot_xm_sendblock(int fd, struct kwboot_block *block)
386{
387 int rc, retries;
388 char c;
389
390 retries = 16;
391 do {
392 rc = kwboot_tty_send(fd, block, sizeof(*block));
393 if (rc)
Pali Rohár2c02a342021-09-24 23:06:43 +0200394 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000395
Stefan Roesebb5c4282014-10-22 12:13:21 +0200396 do {
Kevin Smith4d31a842016-02-16 21:28:19 +0000397 rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo);
Pali Rohár2c02a342021-09-24 23:06:43 +0200398 if (rc) {
399 if (errno != ETIMEDOUT)
400 return rc;
401 c = NAK;
402 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200403
Marek Behúneabacb82021-09-24 23:06:49 +0200404 if (!_is_xm_reply(c))
Stefan Roesebb5c4282014-10-22 12:13:21 +0200405 printf("%c", c);
406
Marek Behúneabacb82021-09-24 23:06:49 +0200407 } while (!_is_xm_reply(c));
Luka Perkovdff289642012-05-27 11:44:51 +0000408
409 if (c != ACK)
410 kwboot_progress(-1, '+');
411
412 } while (c == NAK && retries-- > 0);
413
414 rc = -1;
415
416 switch (c) {
417 case ACK:
418 rc = 0;
419 break;
420 case NAK:
421 errno = EBADMSG;
422 break;
423 case CAN:
424 errno = ECANCELED;
425 break;
426 default:
427 errno = EPROTO;
428 break;
429 }
430
431 return rc;
432}
433
434static int
Pali Rohárbed18ef2021-09-24 23:06:48 +0200435kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
436 size_t size)
Luka Perkovdff289642012-05-27 11:44:51 +0000437{
Pali Rohárbed18ef2021-09-24 23:06:48 +0200438 size_t sent, left;
439 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000440
Pali Rohárbed18ef2021-09-24 23:06:48 +0200441 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
442 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +0000443
Pali Rohárbed18ef2021-09-24 23:06:48 +0200444 left = size;
445 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000446
Pali Rohárbed18ef2021-09-24 23:06:48 +0200447 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +0000448 struct kwboot_block block;
Pali Rohárbed18ef2021-09-24 23:06:48 +0200449 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +0000450
Pali Rohárbed18ef2021-09-24 23:06:48 +0200451 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
452 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +0000453
454 rc = kwboot_xm_sendblock(tty, &block);
455 if (rc)
456 goto out;
457
Pali Rohárbed18ef2021-09-24 23:06:48 +0200458 sent += blksz;
459 left -= blksz;
460
461 kwboot_progress(sent * 100 / size, '.');
462 }
Luka Perkovdff289642012-05-27 11:44:51 +0000463
Pali Rohárbed18ef2021-09-24 23:06:48 +0200464 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000465
Pali Rohárbed18ef2021-09-24 23:06:48 +0200466 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000467out:
Pali Rohár5ed896f2021-09-24 23:06:47 +0200468 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000469 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +0200470}
471
472static int
473kwboot_xmodem(int tty, const void *_img, size_t size)
474{
475 const uint8_t *img = _img;
476 int rc, pnum;
477 size_t hdrsz;
478
479 if (image_version(img) == 0)
480 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
481 else
482 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
483
484 kwboot_printv("Waiting 2s and flushing tty\n");
485 sleep(2); /* flush isn't effective without it */
486 tcflush(tty, TCIOFLUSH);
487
488 pnum = 1;
489
490 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
491 if (rc)
492 return rc;
493
494 img += hdrsz;
495 size -= hdrsz;
496
497 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
498 if (rc)
499 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000500
Pali Rohárbed18ef2021-09-24 23:06:48 +0200501 return kwboot_tty_send_char(tty, EOT);
Luka Perkovdff289642012-05-27 11:44:51 +0000502}
503
504static int
Marek Behúnea5b2b32021-09-24 23:06:40 +0200505kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovdff289642012-05-27 11:44:51 +0000506{
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200507 ssize_t nin;
Luka Perkovdff289642012-05-27 11:44:51 +0000508 char _buf[128], *buf = _buf;
509
Pali Rohár8210ea02021-07-23 11:14:17 +0200510 nin = read(in, buf, sizeof(_buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -0400511 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000512 return -1;
513
514 if (quit) {
515 int i;
516
517 for (i = 0; i < nin; i++) {
518 if (*buf == quit[*s]) {
519 (*s)++;
520 if (!quit[*s])
521 return 0;
522 buf++;
523 nin--;
Pali Rohár48615ba2021-07-23 11:14:20 +0200524 } else {
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200525 if (kwboot_write(out, quit, *s) < 0)
526 return -1;
527 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +0200528 }
Luka Perkovdff289642012-05-27 11:44:51 +0000529 }
530 }
531
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200532 if (kwboot_write(out, buf, nin) < 0)
533 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000534
535 return 0;
536}
537
538static int
539kwboot_terminal(int tty)
540{
541 int rc, in, s;
Marek Behúnea5b2b32021-09-24 23:06:40 +0200542 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +0000543 struct termios otio, tio;
544
545 rc = -1;
546
547 in = STDIN_FILENO;
548 if (isatty(in)) {
549 rc = tcgetattr(in, &otio);
550 if (!rc) {
551 tio = otio;
552 cfmakeraw(&tio);
553 rc = tcsetattr(in, TCSANOW, &tio);
554 }
555 if (rc) {
556 perror("tcsetattr");
557 goto out;
558 }
559
560 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
561 quit[0]|0100, quit[1]);
562 } else
563 in = -1;
564
565 rc = 0;
566 s = 0;
567
568 do {
569 fd_set rfds;
570 int nfds = 0;
571
572 FD_SET(tty, &rfds);
573 nfds = nfds < tty ? tty : nfds;
574
575 if (in >= 0) {
576 FD_SET(in, &rfds);
577 nfds = nfds < in ? in : nfds;
578 }
579
580 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
581 if (nfds < 0)
582 break;
583
584 if (FD_ISSET(tty, &rfds)) {
585 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
586 if (rc)
587 break;
588 }
589
Marek Behún4eb55de2021-09-24 23:06:39 +0200590 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovdff289642012-05-27 11:44:51 +0000591 rc = kwboot_term_pipe(in, tty, quit, &s);
592 if (rc)
593 break;
594 }
595 } while (quit[s] != 0);
596
Pali Roháreafc9692021-07-23 11:14:18 +0200597 if (in >= 0)
598 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +0200599 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000600out:
601 return rc;
602}
603
604static void *
605kwboot_mmap_image(const char *path, size_t *size, int prot)
606{
607 int rc, fd, flags;
608 struct stat st;
609 void *img;
610
611 rc = -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000612 img = NULL;
613
614 fd = open(path, O_RDONLY);
615 if (fd < 0)
616 goto out;
617
618 rc = fstat(fd, &st);
619 if (rc)
620 goto out;
621
622 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
623
624 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
625 if (img == MAP_FAILED) {
626 img = NULL;
627 goto out;
628 }
629
630 rc = 0;
631 *size = st.st_size;
632out:
633 if (rc && img) {
634 munmap(img, st.st_size);
635 img = NULL;
636 }
637 if (fd >= 0)
638 close(fd);
639
640 return img;
641}
642
643static uint8_t
644kwboot_img_csum8(void *_data, size_t size)
645{
646 uint8_t *data = _data, csum;
647
648 for (csum = 0; size-- > 0; data++)
649 csum += *data;
650
651 return csum;
652}
653
654static int
655kwboot_img_patch_hdr(void *img, size_t size)
656{
657 int rc;
Stefan Roesec74165d2015-09-29 09:19:59 +0200658 struct main_hdr_v1 *hdr;
Luka Perkovdff289642012-05-27 11:44:51 +0000659 uint8_t csum;
Stefan Roesec74165d2015-09-29 09:19:59 +0200660 size_t hdrsz = sizeof(*hdr);
661 int image_ver;
Luka Perkovdff289642012-05-27 11:44:51 +0000662
663 rc = -1;
664 hdr = img;
665
666 if (size < hdrsz) {
667 errno = EINVAL;
668 goto out;
669 }
670
Stefan Roesec74165d2015-09-29 09:19:59 +0200671 image_ver = image_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +0200672 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +0200673 fprintf(stderr, "Invalid image header version\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000674 errno = EINVAL;
675 goto out;
676 }
677
Stefan Roesec74165d2015-09-29 09:19:59 +0200678 if (image_ver == 0)
679 hdrsz = sizeof(*hdr);
680 else
681 hdrsz = KWBHEADER_V1_SIZE(hdr);
682
Pali Rohár12281162021-07-23 11:14:21 +0200683 if (size < hdrsz) {
684 errno = EINVAL;
685 goto out;
686 }
687
Stefan Roesec74165d2015-09-29 09:19:59 +0200688 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
689 if (csum != hdr->checksum) {
690 errno = EINVAL;
691 goto out;
692 }
693
Luka Perkovdff289642012-05-27 11:44:51 +0000694 if (hdr->blockid == IBR_HDR_UART_ID) {
695 rc = 0;
696 goto out;
697 }
698
699 hdr->blockid = IBR_HDR_UART_ID;
700
Stefan Roesec74165d2015-09-29 09:19:59 +0200701 if (image_ver == 0) {
702 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovdff289642012-05-27 11:44:51 +0000703
Stefan Roesec74165d2015-09-29 09:19:59 +0200704 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
705 hdr_v0->nandpagesize = 0;
706
707 hdr_v0->srcaddr = hdr_v0->ext
708 ? sizeof(struct kwb_header)
709 : sizeof(*hdr_v0);
710 }
Luka Perkovdff289642012-05-27 11:44:51 +0000711
Stefan Roesec74165d2015-09-29 09:19:59 +0200712 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +0000713
714 rc = 0;
715out:
716 return rc;
717}
718
719static void
720kwboot_usage(FILE *stream, char *progname)
721{
Pali Rohár3c703aaf2021-09-24 23:06:42 +0200722 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovdff289642012-05-27 11:44:51 +0000723 fprintf(stream,
Kevin Smith6c7c1ba2016-02-16 21:28:17 +0000724 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +0200725 progname);
Luka Perkovdff289642012-05-27 11:44:51 +0000726 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +0200727 fprintf(stream,
728 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000729 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +0200730 fprintf(stream,
731 " -D <image>: boot <image> without preamble (Dove)\n");
732 fprintf(stream, " -d: enter debug mode\n");
733 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roesef7f509d2015-05-29 13:25:04 +0200734 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
735 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +0000736 fprintf(stream,
737 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +0000738 fprintf(stream, "\n");
739 fprintf(stream, " -t: mini terminal\n");
740 fprintf(stream, "\n");
741 fprintf(stream, " -B <baud>: set baud rate\n");
742 fprintf(stream, "\n");
743}
744
745int
746main(int argc, char **argv)
747{
748 const char *ttypath, *imgpath;
749 int rv, rc, tty, term, prot, patch;
750 void *bootmsg;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200751 void *debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +0000752 void *img;
753 size_t size;
754 speed_t speed;
755
756 rv = 1;
757 tty = -1;
758 bootmsg = NULL;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200759 debugmsg = NULL;
Luka Perkovdff289642012-05-27 11:44:51 +0000760 imgpath = NULL;
761 img = NULL;
762 term = 0;
763 patch = 0;
764 size = 0;
765 speed = B115200;
766
767 kwboot_verbose = isatty(STDOUT_FILENO);
768
769 do {
Kevin Smith4d31a842016-02-16 21:28:19 +0000770 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +0000771 if (c < 0)
772 break;
773
774 switch (c) {
775 case 'b':
776 bootmsg = kwboot_msg_boot;
777 imgpath = optarg;
778 break;
779
Stefan Roesebb5c4282014-10-22 12:13:21 +0200780 case 'D':
781 bootmsg = NULL;
782 imgpath = optarg;
783 break;
784
785 case 'd':
786 debugmsg = kwboot_msg_debug;
787 break;
788
Luka Perkovdff289642012-05-27 11:44:51 +0000789 case 'p':
790 patch = 1;
791 break;
792
793 case 't':
794 term = 1;
795 break;
796
Stefan Roesebb5c4282014-10-22 12:13:21 +0200797 case 'a':
798 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
799 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
800 break;
801
Stefan Roesef7f509d2015-05-29 13:25:04 +0200802 case 'q':
803 msg_req_delay = atoi(optarg);
804 break;
805
806 case 's':
807 msg_rsp_timeo = atoi(optarg);
808 break;
809
Kevin Smith4d31a842016-02-16 21:28:19 +0000810 case 'o':
811 blk_rsp_timeo = atoi(optarg);
812 break;
813
Luka Perkovdff289642012-05-27 11:44:51 +0000814 case 'B':
815 speed = kwboot_tty_speed(atoi(optarg));
816 if (speed == -1)
817 goto usage;
818 break;
819
820 case 'h':
821 rv = 0;
822 default:
823 goto usage;
824 }
825 } while (1);
826
Stefan Roesebb5c4282014-10-22 12:13:21 +0200827 if (!bootmsg && !term && !debugmsg)
Luka Perkovdff289642012-05-27 11:44:51 +0000828 goto usage;
829
830 if (patch && !imgpath)
831 goto usage;
832
833 if (argc - optind < 1)
834 goto usage;
835
836 ttypath = argv[optind++];
837
838 tty = kwboot_open_tty(ttypath, speed);
839 if (tty < 0) {
840 perror(ttypath);
841 goto out;
842 }
843
844 if (imgpath) {
845 prot = PROT_READ | (patch ? PROT_WRITE : 0);
846
847 img = kwboot_mmap_image(imgpath, &size, prot);
848 if (!img) {
849 perror(imgpath);
850 goto out;
851 }
852 }
853
854 if (patch) {
855 rc = kwboot_img_patch_hdr(img, size);
856 if (rc) {
857 fprintf(stderr, "%s: Invalid image.\n", imgpath);
858 goto out;
859 }
860 }
861
Stefan Roesebb5c4282014-10-22 12:13:21 +0200862 if (debugmsg) {
863 rc = kwboot_debugmsg(tty, debugmsg);
864 if (rc) {
865 perror("debugmsg");
866 goto out;
867 }
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -0400868 } else if (bootmsg) {
Luka Perkovdff289642012-05-27 11:44:51 +0000869 rc = kwboot_bootmsg(tty, bootmsg);
870 if (rc) {
871 perror("bootmsg");
872 goto out;
873 }
874 }
875
876 if (img) {
877 rc = kwboot_xmodem(tty, img, size);
878 if (rc) {
879 perror("xmodem");
880 goto out;
881 }
882 }
883
884 if (term) {
885 rc = kwboot_terminal(tty);
886 if (rc && !(errno == EINTR)) {
887 perror("terminal");
888 goto out;
889 }
890 }
891
892 rv = 0;
893out:
894 if (tty >= 0)
895 close(tty);
896
897 if (img)
898 munmap(img, size);
899
900 return rv;
901
902usage:
903 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
904 goto out;
905}