blob: cf6e32c6fa8259f738054047182192c3a0a6df10 [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>
Marek Behún12df7b72021-09-24 23:06:52 +020027#include <time.h>
Luka Perkovd131ad62012-05-27 11:44:51 +000028#include <sys/mman.h>
29#include <sys/stat.h>
30
Luka Perkovd131ad62012-05-27 11:44:51 +000031/*
32 * Marvell BootROM UART Sensing
33 */
34
35static unsigned char kwboot_msg_boot[] = {
36 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
37};
38
Stefan Roese84899e22014-10-22 12:13:21 +020039static unsigned char kwboot_msg_debug[] = {
40 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
41};
42
43/* Defines known to work on Kirkwood */
Luka Perkovd131ad62012-05-27 11:44:51 +000044#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
45#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
46
Stefan Roese84899e22014-10-22 12:13:21 +020047/* Defines known to work on Armada XP */
48#define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
49#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
50
Luka Perkovd131ad62012-05-27 11:44:51 +000051/*
52 * Xmodem Transfers
53 */
54
55#define SOH 1 /* sender start of block header */
56#define EOT 4 /* sender end of block transfer */
57#define ACK 6 /* target block ack */
58#define NAK 21 /* target block negative ack */
59#define CAN 24 /* target/sender transfer cancellation */
60
Pali Rohár2ef87f72021-09-24 23:06:48 +020061#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
62
Luka Perkovd131ad62012-05-27 11:44:51 +000063struct kwboot_block {
64 uint8_t soh;
65 uint8_t pnum;
66 uint8_t _pnum;
Pali Rohár2ef87f72021-09-24 23:06:48 +020067 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovd131ad62012-05-27 11:44:51 +000068 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +020069} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000070
71#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
Marek Behún12df7b72021-09-24 23:06:52 +020072#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovd131ad62012-05-27 11:44:51 +000073
74static int kwboot_verbose;
75
Stefan Roese84899e22014-10-22 12:13:21 +020076static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
77static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +000078static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +020079
Marek Behúne453bb42021-09-24 23:06:41 +020080static ssize_t
81kwboot_write(int fd, const char *buf, size_t len)
82{
83 size_t tot = 0;
84
85 while (tot < len) {
86 ssize_t wr = write(fd, buf + tot, len - tot);
87
88 if (wr < 0)
89 return -1;
90
91 tot += wr;
92 }
93
94 return tot;
95}
96
Luka Perkovd131ad62012-05-27 11:44:51 +000097static void
98kwboot_printv(const char *fmt, ...)
99{
100 va_list ap;
101
102 if (kwboot_verbose) {
103 va_start(ap, fmt);
104 vprintf(fmt, ap);
105 va_end(ap);
106 fflush(stdout);
107 }
108}
109
110static void
111__spinner(void)
112{
113 const char seq[] = { '-', '\\', '|', '/' };
114 const int div = 8;
115 static int state, bs;
116
117 if (state % div == 0) {
118 fputc(bs, stdout);
119 fputc(seq[state / div % sizeof(seq)], stdout);
120 fflush(stdout);
121 }
122
123 bs = '\b';
124 state++;
125}
126
127static void
128kwboot_spinner(void)
129{
130 if (kwboot_verbose)
131 __spinner();
132}
133
134static void
135__progress(int pct, char c)
136{
137 const int width = 70;
138 static const char *nl = "";
139 static int pos;
140
141 if (pos % width == 0)
142 printf("%s%3d %% [", nl, pct);
143
144 fputc(c, stdout);
145
146 nl = "]\n";
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200147 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000148
149 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200150 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000151 fputc(' ', stdout);
152 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200153 nl = "";
154 pos = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000155 }
156
157 fflush(stdout);
158
159}
160
161static void
162kwboot_progress(int _pct, char c)
163{
164 static int pct;
165
166 if (_pct != -1)
167 pct = _pct;
168
169 if (kwboot_verbose)
170 __progress(pct, c);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200171
172 if (pct == 100)
173 pct = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000174}
175
176static int
177kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
178{
179 int rc, nfds;
180 fd_set rfds;
181 struct timeval tv;
182 ssize_t n;
183
184 rc = -1;
185
186 FD_ZERO(&rfds);
187 FD_SET(fd, &rfds);
188
189 tv.tv_sec = 0;
190 tv.tv_usec = timeo * 1000;
191 if (tv.tv_usec > 1000000) {
192 tv.tv_sec += tv.tv_usec / 1000000;
193 tv.tv_usec %= 1000000;
194 }
195
196 do {
197 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
198 if (nfds < 0)
199 goto out;
200 if (!nfds) {
201 errno = ETIMEDOUT;
202 goto out;
203 }
204
205 n = read(fd, buf, len);
Willy Tarreau4469bd72018-07-03 12:10:31 -0400206 if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000207 goto out;
208
209 buf = (char *)buf + n;
210 len -= n;
211 } while (len > 0);
212
213 rc = 0;
214out:
215 return rc;
216}
217
218static int
219kwboot_tty_send(int fd, const void *buf, size_t len)
220{
Stefan Roese84899e22014-10-22 12:13:21 +0200221 if (!buf)
222 return 0;
223
Marek Behúne453bb42021-09-24 23:06:41 +0200224 if (kwboot_write(fd, buf, len) < 0)
225 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000226
Marek Behúne453bb42021-09-24 23:06:41 +0200227 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000228}
229
230static int
231kwboot_tty_send_char(int fd, unsigned char c)
232{
233 return kwboot_tty_send(fd, &c, 1);
234}
235
236static speed_t
237kwboot_tty_speed(int baudrate)
238{
239 switch (baudrate) {
240 case 115200:
241 return B115200;
242 case 57600:
243 return B57600;
244 case 38400:
245 return B38400;
246 case 19200:
247 return B19200;
248 case 9600:
249 return B9600;
250 }
251
252 return -1;
253}
254
255static int
256kwboot_open_tty(const char *path, speed_t speed)
257{
258 int rc, fd;
259 struct termios tio;
260
261 rc = -1;
262
263 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
264 if (fd < 0)
265 goto out;
266
267 memset(&tio, 0, sizeof(tio));
268
269 tio.c_iflag = 0;
270 tio.c_cflag = CREAD|CLOCAL|CS8;
271
272 tio.c_cc[VMIN] = 1;
273 tio.c_cc[VTIME] = 10;
274
275 cfsetospeed(&tio, speed);
276 cfsetispeed(&tio, speed);
277
278 rc = tcsetattr(fd, TCSANOW, &tio);
279 if (rc)
280 goto out;
281
282 rc = fd;
283out:
284 if (rc < 0) {
285 if (fd >= 0)
286 close(fd);
287 }
288
289 return rc;
290}
291
292static int
293kwboot_bootmsg(int tty, void *msg)
294{
295 int rc;
296 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300297 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000298
Stefan Roese84899e22014-10-22 12:13:21 +0200299 if (msg == NULL)
300 kwboot_printv("Please reboot the target into UART boot mode...");
301 else
302 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000303
304 do {
305 rc = tcflush(tty, TCIOFLUSH);
306 if (rc)
307 break;
308
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300309 for (count = 0; count < 128; count++) {
310 rc = kwboot_tty_send(tty, msg, 8);
311 if (rc) {
312 usleep(msg_req_delay * 1000);
313 continue;
314 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000315 }
316
Stefan Roese84899e22014-10-22 12:13:21 +0200317 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000318
319 kwboot_spinner();
320
321 } while (rc || c != NAK);
322
323 kwboot_printv("\n");
324
325 return rc;
326}
327
328static int
Stefan Roese84899e22014-10-22 12:13:21 +0200329kwboot_debugmsg(int tty, void *msg)
330{
331 int rc;
332
333 kwboot_printv("Sending debug message. Please reboot the target...");
334
335 do {
336 char buf[16];
337
338 rc = tcflush(tty, TCIOFLUSH);
339 if (rc)
340 break;
341
342 rc = kwboot_tty_send(tty, msg, 8);
343 if (rc) {
344 usleep(msg_req_delay * 1000);
345 continue;
346 }
347
348 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
349
350 kwboot_spinner();
351
352 } while (rc);
353
354 kwboot_printv("\n");
355
356 return rc;
357}
358
Pali Rohárc5d666a2021-09-24 23:06:44 +0200359static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +0000360kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
361 size_t size, int pnum)
362{
Marek Behúnd8cc8512021-09-24 23:06:45 +0200363 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000364
Stefan Roese84899e22014-10-22 12:13:21 +0200365 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000366 block->pnum = pnum;
367 block->_pnum = ~block->pnum;
368
Pali Rohár2ef87f72021-09-24 23:06:48 +0200369 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +0000370 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200371 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +0000372
373 block->csum = 0;
374 for (i = 0; i < n; i++)
375 block->csum += block->data[i];
376
377 return n;
378}
379
Marek Behún12df7b72021-09-24 23:06:52 +0200380static uint64_t
381_now(void)
382{
383 struct timespec ts;
384
385 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
386 static int err_print;
387
388 if (!err_print) {
389 perror("clock_gettime() does not work");
390 err_print = 1;
391 }
392
393 /* this will just make the timeout not work */
394 return -1ULL;
395 }
396
397 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
398}
399
Luka Perkovd131ad62012-05-27 11:44:51 +0000400static int
Marek Behún408ea612021-09-24 23:06:49 +0200401_is_xm_reply(char c)
402{
403 return c == ACK || c == NAK || c == CAN;
404}
405
406static int
Marek Behún2e81b3a2021-09-24 23:06:51 +0200407kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
Pali Rohár48b3ea62021-09-24 23:06:50 +0200408{
Marek Behún12df7b72021-09-24 23:06:52 +0200409 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
410 uint64_t recv_until = 0;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200411 int rc;
412
Marek Behún2e81b3a2021-09-24 23:06:51 +0200413 *non_xm_print = 0;
414
Pali Rohár48b3ea62021-09-24 23:06:50 +0200415 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +0200416 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200417 if (rc) {
418 if (errno != ETIMEDOUT)
419 return rc;
Marek Behún12df7b72021-09-24 23:06:52 +0200420 else if (recv_until && recv_until < _now())
421 return -1;
422 else
423 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200424 }
425
426 /* If received xmodem reply, end. */
427 if (_is_xm_reply(*c))
428 break;
429
430 /*
431 * If printing non-xmodem text output is allowed and such a byte
Marek Behún12df7b72021-09-24 23:06:52 +0200432 * was received, print it and increase receiving time.
Pali Rohár48b3ea62021-09-24 23:06:50 +0200433 */
434 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +0200435 recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200436 putchar(*c);
437 fflush(stdout);
Marek Behún2e81b3a2021-09-24 23:06:51 +0200438 *non_xm_print = 1;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200439 }
440 }
441
442 return 0;
443}
444
445static int
446kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
447 int *done_print)
Luka Perkovd131ad62012-05-27 11:44:51 +0000448{
Marek Behún2e81b3a2021-09-24 23:06:51 +0200449 int non_xm_print;
Luka Perkovd131ad62012-05-27 11:44:51 +0000450 int rc, retries;
451 char c;
452
Pali Rohár48b3ea62021-09-24 23:06:50 +0200453 *done_print = 0;
454
Luka Perkovd131ad62012-05-27 11:44:51 +0000455 retries = 16;
456 do {
457 rc = kwboot_tty_send(fd, block, sizeof(*block));
458 if (rc)
Pali Rohár00a1dee2021-09-24 23:06:43 +0200459 return rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000460
Pali Rohár48b3ea62021-09-24 23:06:50 +0200461 if (allow_non_xm && !*done_print) {
462 kwboot_progress(100, '.');
463 kwboot_printv("Done\n");
464 *done_print = 1;
465 }
Stefan Roese84899e22014-10-22 12:13:21 +0200466
Marek Behún2e81b3a2021-09-24 23:06:51 +0200467 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200468 if (rc)
469 return rc;
Stefan Roese84899e22014-10-22 12:13:21 +0200470
Pali Rohár48b3ea62021-09-24 23:06:50 +0200471 if (!allow_non_xm && c != ACK)
Luka Perkovd131ad62012-05-27 11:44:51 +0000472 kwboot_progress(-1, '+');
Luka Perkovd131ad62012-05-27 11:44:51 +0000473 } while (c == NAK && retries-- > 0);
474
Marek Behún2e81b3a2021-09-24 23:06:51 +0200475 if (non_xm_print)
476 kwboot_printv("\n");
477
Luka Perkovd131ad62012-05-27 11:44:51 +0000478 rc = -1;
479
480 switch (c) {
481 case ACK:
482 rc = 0;
483 break;
484 case NAK:
485 errno = EBADMSG;
486 break;
487 case CAN:
488 errno = ECANCELED;
489 break;
490 default:
491 errno = EPROTO;
492 break;
493 }
494
495 return rc;
496}
497
498static int
Pali Rohár2ef87f72021-09-24 23:06:48 +0200499kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
500 size_t size)
Luka Perkovd131ad62012-05-27 11:44:51 +0000501{
Pali Rohár48b3ea62021-09-24 23:06:50 +0200502 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200503 size_t sent, left;
504 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000505
Pali Rohár2ef87f72021-09-24 23:06:48 +0200506 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
507 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +0000508
Pali Rohár2ef87f72021-09-24 23:06:48 +0200509 left = size;
510 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000511
Pali Rohár2ef87f72021-09-24 23:06:48 +0200512 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000513 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200514 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200515 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000516
Pali Rohár2ef87f72021-09-24 23:06:48 +0200517 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
518 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000519
Pali Rohár48b3ea62021-09-24 23:06:50 +0200520 last_block = (left <= blksz);
521
522 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
523 &done_print);
Luka Perkovd131ad62012-05-27 11:44:51 +0000524 if (rc)
525 goto out;
526
Pali Rohár2ef87f72021-09-24 23:06:48 +0200527 sent += blksz;
528 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000529
Pali Rohár48b3ea62021-09-24 23:06:50 +0200530 if (!done_print)
531 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +0200532 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000533
Pali Rohár48b3ea62021-09-24 23:06:50 +0200534 if (!done_print)
535 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +0200536
537 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000538out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +0200539 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000540 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200541}
Luka Perkovd131ad62012-05-27 11:44:51 +0000542
Pali Rohár2ef87f72021-09-24 23:06:48 +0200543static int
544kwboot_xmodem(int tty, const void *_img, size_t size)
545{
546 const uint8_t *img = _img;
547 int rc, pnum;
548 size_t hdrsz;
549
550 if (image_version(img) == 0)
551 hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img);
552 else
553 hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img);
554
555 kwboot_printv("Waiting 2s and flushing tty\n");
556 sleep(2); /* flush isn't effective without it */
557 tcflush(tty, TCIOFLUSH);
558
559 pnum = 1;
560
561 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
562 if (rc)
563 return rc;
564
565 img += hdrsz;
566 size -= hdrsz;
567
568 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
569 if (rc)
570 return rc;
571
572 return kwboot_tty_send_char(tty, EOT);
Luka Perkovd131ad62012-05-27 11:44:51 +0000573}
574
575static int
Marek Behún46237e62021-09-24 23:06:40 +0200576kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +0000577{
Marek Behúne453bb42021-09-24 23:06:41 +0200578 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +0000579 char _buf[128], *buf = _buf;
580
Pali Rohár43fef8d2021-07-23 11:14:17 +0200581 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -0400582 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000583 return -1;
584
585 if (quit) {
586 int i;
587
588 for (i = 0; i < nin; i++) {
589 if (*buf == quit[*s]) {
590 (*s)++;
591 if (!quit[*s])
592 return 0;
593 buf++;
594 nin--;
Pali Rohárb943eee2021-07-23 11:14:20 +0200595 } else {
Marek Behúne453bb42021-09-24 23:06:41 +0200596 if (kwboot_write(out, quit, *s) < 0)
597 return -1;
598 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +0200599 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000600 }
601 }
602
Marek Behúne453bb42021-09-24 23:06:41 +0200603 if (kwboot_write(out, buf, nin) < 0)
604 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000605
606 return 0;
607}
608
609static int
610kwboot_terminal(int tty)
611{
612 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +0200613 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +0000614 struct termios otio, tio;
615
616 rc = -1;
617
618 in = STDIN_FILENO;
619 if (isatty(in)) {
620 rc = tcgetattr(in, &otio);
621 if (!rc) {
622 tio = otio;
623 cfmakeraw(&tio);
624 rc = tcsetattr(in, TCSANOW, &tio);
625 }
626 if (rc) {
627 perror("tcsetattr");
628 goto out;
629 }
630
631 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
632 quit[0]|0100, quit[1]);
633 } else
634 in = -1;
635
636 rc = 0;
637 s = 0;
638
639 do {
640 fd_set rfds;
641 int nfds = 0;
642
643 FD_SET(tty, &rfds);
644 nfds = nfds < tty ? tty : nfds;
645
646 if (in >= 0) {
647 FD_SET(in, &rfds);
648 nfds = nfds < in ? in : nfds;
649 }
650
651 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
652 if (nfds < 0)
653 break;
654
655 if (FD_ISSET(tty, &rfds)) {
656 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
657 if (rc)
658 break;
659 }
660
Marek Behúnf30cb0d2021-09-24 23:06:39 +0200661 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000662 rc = kwboot_term_pipe(in, tty, quit, &s);
663 if (rc)
664 break;
665 }
666 } while (quit[s] != 0);
667
Pali Rohárec0fe5b2021-07-23 11:14:18 +0200668 if (in >= 0)
669 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +0200670 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000671out:
672 return rc;
673}
674
675static void *
676kwboot_mmap_image(const char *path, size_t *size, int prot)
677{
678 int rc, fd, flags;
679 struct stat st;
680 void *img;
681
682 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000683 img = NULL;
684
685 fd = open(path, O_RDONLY);
686 if (fd < 0)
687 goto out;
688
689 rc = fstat(fd, &st);
690 if (rc)
691 goto out;
692
693 flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
694
695 img = mmap(NULL, st.st_size, prot, flags, fd, 0);
696 if (img == MAP_FAILED) {
697 img = NULL;
698 goto out;
699 }
700
701 rc = 0;
702 *size = st.st_size;
703out:
704 if (rc && img) {
705 munmap(img, st.st_size);
706 img = NULL;
707 }
708 if (fd >= 0)
709 close(fd);
710
711 return img;
712}
713
714static uint8_t
715kwboot_img_csum8(void *_data, size_t size)
716{
717 uint8_t *data = _data, csum;
718
719 for (csum = 0; size-- > 0; data++)
720 csum += *data;
721
722 return csum;
723}
724
725static int
726kwboot_img_patch_hdr(void *img, size_t size)
727{
728 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200729 struct main_hdr_v1 *hdr;
Luka Perkovd131ad62012-05-27 11:44:51 +0000730 uint8_t csum;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200731 size_t hdrsz = sizeof(*hdr);
732 int image_ver;
Luka Perkovd131ad62012-05-27 11:44:51 +0000733
734 rc = -1;
735 hdr = img;
736
737 if (size < hdrsz) {
738 errno = EINVAL;
739 goto out;
740 }
741
Stefan Roesee29f1db2015-09-29 09:19:59 +0200742 image_ver = image_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +0200743 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +0200744 fprintf(stderr, "Invalid image header version\n");
745 errno = EINVAL;
746 goto out;
747 }
748
749 if (image_ver == 0)
750 hdrsz = sizeof(*hdr);
751 else
752 hdrsz = KWBHEADER_V1_SIZE(hdr);
753
Pali Rohár825a2ca2021-07-23 11:14:21 +0200754 if (size < hdrsz) {
755 errno = EINVAL;
756 goto out;
757 }
758
Stefan Roesee29f1db2015-09-29 09:19:59 +0200759 csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum;
760 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000761 errno = EINVAL;
762 goto out;
763 }
764
765 if (hdr->blockid == IBR_HDR_UART_ID) {
766 rc = 0;
767 goto out;
768 }
769
770 hdr->blockid = IBR_HDR_UART_ID;
771
Stefan Roesee29f1db2015-09-29 09:19:59 +0200772 if (image_ver == 0) {
773 struct main_hdr_v0 *hdr_v0 = img;
Luka Perkovd131ad62012-05-27 11:44:51 +0000774
Stefan Roesee29f1db2015-09-29 09:19:59 +0200775 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
776 hdr_v0->nandpagesize = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000777
Stefan Roesee29f1db2015-09-29 09:19:59 +0200778 hdr_v0->srcaddr = hdr_v0->ext
779 ? sizeof(struct kwb_header)
780 : sizeof(*hdr_v0);
781 }
782
783 hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +0000784
785 rc = 0;
786out:
787 return rc;
788}
789
790static void
791kwboot_usage(FILE *stream, char *progname)
792{
Pali Rohára050a862021-09-24 23:06:42 +0200793 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovd131ad62012-05-27 11:44:51 +0000794 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +0000795 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +0200796 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +0000797 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200798 fprintf(stream,
799 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000800 fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200801 fprintf(stream,
802 " -D <image>: boot <image> without preamble (Dove)\n");
803 fprintf(stream, " -d: enter debug mode\n");
804 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200805 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
806 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +0000807 fprintf(stream,
808 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000809 fprintf(stream, "\n");
810 fprintf(stream, " -t: mini terminal\n");
811 fprintf(stream, "\n");
812 fprintf(stream, " -B <baud>: set baud rate\n");
813 fprintf(stream, "\n");
814}
815
816int
817main(int argc, char **argv)
818{
819 const char *ttypath, *imgpath;
820 int rv, rc, tty, term, prot, patch;
821 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +0200822 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +0000823 void *img;
824 size_t size;
825 speed_t speed;
826
827 rv = 1;
828 tty = -1;
829 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +0200830 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +0000831 imgpath = NULL;
832 img = NULL;
833 term = 0;
834 patch = 0;
835 size = 0;
836 speed = B115200;
837
838 kwboot_verbose = isatty(STDOUT_FILENO);
839
840 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000841 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +0000842 if (c < 0)
843 break;
844
845 switch (c) {
846 case 'b':
847 bootmsg = kwboot_msg_boot;
848 imgpath = optarg;
849 break;
850
Stefan Roese84899e22014-10-22 12:13:21 +0200851 case 'D':
852 bootmsg = NULL;
853 imgpath = optarg;
854 break;
855
856 case 'd':
857 debugmsg = kwboot_msg_debug;
858 break;
859
Luka Perkovd131ad62012-05-27 11:44:51 +0000860 case 'p':
861 patch = 1;
862 break;
863
864 case 't':
865 term = 1;
866 break;
867
Stefan Roese84899e22014-10-22 12:13:21 +0200868 case 'a':
869 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
870 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
871 break;
872
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200873 case 'q':
874 msg_req_delay = atoi(optarg);
875 break;
876
877 case 's':
878 msg_rsp_timeo = atoi(optarg);
879 break;
880
Kevin Smith7497a6a2016-02-16 21:28:19 +0000881 case 'o':
882 blk_rsp_timeo = atoi(optarg);
883 break;
884
Luka Perkovd131ad62012-05-27 11:44:51 +0000885 case 'B':
886 speed = kwboot_tty_speed(atoi(optarg));
887 if (speed == -1)
888 goto usage;
889 break;
890
891 case 'h':
892 rv = 0;
893 default:
894 goto usage;
895 }
896 } while (1);
897
Stefan Roese84899e22014-10-22 12:13:21 +0200898 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +0000899 goto usage;
900
901 if (patch && !imgpath)
902 goto usage;
903
904 if (argc - optind < 1)
905 goto usage;
906
907 ttypath = argv[optind++];
908
909 tty = kwboot_open_tty(ttypath, speed);
910 if (tty < 0) {
911 perror(ttypath);
912 goto out;
913 }
914
915 if (imgpath) {
916 prot = PROT_READ | (patch ? PROT_WRITE : 0);
917
918 img = kwboot_mmap_image(imgpath, &size, prot);
919 if (!img) {
920 perror(imgpath);
921 goto out;
922 }
923 }
924
925 if (patch) {
926 rc = kwboot_img_patch_hdr(img, size);
927 if (rc) {
928 fprintf(stderr, "%s: Invalid image.\n", imgpath);
929 goto out;
930 }
931 }
932
Stefan Roese84899e22014-10-22 12:13:21 +0200933 if (debugmsg) {
934 rc = kwboot_debugmsg(tty, debugmsg);
935 if (rc) {
936 perror("debugmsg");
937 goto out;
938 }
Willy Tarreau3475a712018-07-03 12:10:30 -0400939 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000940 rc = kwboot_bootmsg(tty, bootmsg);
941 if (rc) {
942 perror("bootmsg");
943 goto out;
944 }
945 }
946
947 if (img) {
948 rc = kwboot_xmodem(tty, img, size);
949 if (rc) {
950 perror("xmodem");
951 goto out;
952 }
953 }
954
955 if (term) {
956 rc = kwboot_terminal(tty);
957 if (rc && !(errno == EINTR)) {
958 perror("terminal");
959 goto out;
960 }
961 }
962
963 rv = 0;
964out:
965 if (tty >= 0)
966 close(tty);
967
968 if (img)
969 munmap(img, size);
970
971 return rv;
972
973usage:
974 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
975 goto out;
976}