blob: 77bf5cb80b61b73815ceaf72a9c09d16ca6ebe06 [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/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
Pali Rohár2ef87f72021-09-24 23:06:48 +020060#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
61
Luka Perkovd131ad62012-05-27 11:44:51 +000062struct kwboot_block {
63 uint8_t soh;
64 uint8_t pnum;
65 uint8_t _pnum;
Pali Rohár2ef87f72021-09-24 23:06:48 +020066 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovd131ad62012-05-27 11:44:51 +000067 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +020068} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000069
70#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
Marek Behún12df7b72021-09-24 23:06:52 +020071#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovd131ad62012-05-27 11:44:51 +000072
73static int kwboot_verbose;
74
Stefan Roese84899e22014-10-22 12:13:21 +020075static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
76static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +000077static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +020078
Marek Behúne453bb42021-09-24 23:06:41 +020079static ssize_t
80kwboot_write(int fd, const char *buf, size_t len)
81{
82 size_t tot = 0;
83
84 while (tot < len) {
85 ssize_t wr = write(fd, buf + tot, len - tot);
86
87 if (wr < 0)
88 return -1;
89
90 tot += wr;
91 }
92
93 return tot;
94}
95
Luka Perkovd131ad62012-05-27 11:44:51 +000096static void
97kwboot_printv(const char *fmt, ...)
98{
99 va_list ap;
100
101 if (kwboot_verbose) {
102 va_start(ap, fmt);
103 vprintf(fmt, ap);
104 va_end(ap);
105 fflush(stdout);
106 }
107}
108
109static void
110__spinner(void)
111{
112 const char seq[] = { '-', '\\', '|', '/' };
113 const int div = 8;
114 static int state, bs;
115
116 if (state % div == 0) {
117 fputc(bs, stdout);
118 fputc(seq[state / div % sizeof(seq)], stdout);
119 fflush(stdout);
120 }
121
122 bs = '\b';
123 state++;
124}
125
126static void
127kwboot_spinner(void)
128{
129 if (kwboot_verbose)
130 __spinner();
131}
132
133static void
134__progress(int pct, char c)
135{
136 const int width = 70;
137 static const char *nl = "";
138 static int pos;
139
140 if (pos % width == 0)
141 printf("%s%3d %% [", nl, pct);
142
143 fputc(c, stdout);
144
145 nl = "]\n";
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200146 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000147
148 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200149 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000150 fputc(' ', stdout);
151 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200152 nl = "";
153 pos = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000154 }
155
156 fflush(stdout);
157
158}
159
160static void
161kwboot_progress(int _pct, char c)
162{
163 static int pct;
164
165 if (_pct != -1)
166 pct = _pct;
167
168 if (kwboot_verbose)
169 __progress(pct, c);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200170
171 if (pct == 100)
172 pct = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000173}
174
175static int
176kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
177{
178 int rc, nfds;
179 fd_set rfds;
180 struct timeval tv;
181 ssize_t n;
182
183 rc = -1;
184
185 FD_ZERO(&rfds);
186 FD_SET(fd, &rfds);
187
188 tv.tv_sec = 0;
189 tv.tv_usec = timeo * 1000;
190 if (tv.tv_usec > 1000000) {
191 tv.tv_sec += tv.tv_usec / 1000000;
192 tv.tv_usec %= 1000000;
193 }
194
195 do {
196 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
197 if (nfds < 0)
198 goto out;
199 if (!nfds) {
200 errno = ETIMEDOUT;
201 goto out;
202 }
203
204 n = read(fd, buf, len);
Willy Tarreau4469bd72018-07-03 12:10:31 -0400205 if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000206 goto out;
207
208 buf = (char *)buf + n;
209 len -= n;
210 } while (len > 0);
211
212 rc = 0;
213out:
214 return rc;
215}
216
217static int
218kwboot_tty_send(int fd, const void *buf, size_t len)
219{
Stefan Roese84899e22014-10-22 12:13:21 +0200220 if (!buf)
221 return 0;
222
Marek Behúne453bb42021-09-24 23:06:41 +0200223 if (kwboot_write(fd, buf, len) < 0)
224 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000225
Marek Behúne453bb42021-09-24 23:06:41 +0200226 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000227}
228
229static int
230kwboot_tty_send_char(int fd, unsigned char c)
231{
232 return kwboot_tty_send(fd, &c, 1);
233}
234
235static speed_t
236kwboot_tty_speed(int baudrate)
237{
238 switch (baudrate) {
239 case 115200:
240 return B115200;
241 case 57600:
242 return B57600;
243 case 38400:
244 return B38400;
245 case 19200:
246 return B19200;
247 case 9600:
248 return B9600;
249 }
250
251 return -1;
252}
253
254static int
255kwboot_open_tty(const char *path, speed_t speed)
256{
257 int rc, fd;
258 struct termios tio;
259
260 rc = -1;
261
262 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
263 if (fd < 0)
264 goto out;
265
266 memset(&tio, 0, sizeof(tio));
267
268 tio.c_iflag = 0;
269 tio.c_cflag = CREAD|CLOCAL|CS8;
270
271 tio.c_cc[VMIN] = 1;
272 tio.c_cc[VTIME] = 10;
273
274 cfsetospeed(&tio, speed);
275 cfsetispeed(&tio, speed);
276
277 rc = tcsetattr(fd, TCSANOW, &tio);
278 if (rc)
279 goto out;
280
281 rc = fd;
282out:
283 if (rc < 0) {
284 if (fd >= 0)
285 close(fd);
286 }
287
288 return rc;
289}
290
291static int
292kwboot_bootmsg(int tty, void *msg)
293{
294 int rc;
295 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300296 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000297
Stefan Roese84899e22014-10-22 12:13:21 +0200298 if (msg == NULL)
299 kwboot_printv("Please reboot the target into UART boot mode...");
300 else
301 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000302
303 do {
304 rc = tcflush(tty, TCIOFLUSH);
305 if (rc)
306 break;
307
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300308 for (count = 0; count < 128; count++) {
309 rc = kwboot_tty_send(tty, msg, 8);
310 if (rc) {
311 usleep(msg_req_delay * 1000);
312 continue;
313 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000314 }
315
Stefan Roese84899e22014-10-22 12:13:21 +0200316 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000317
318 kwboot_spinner();
319
320 } while (rc || c != NAK);
321
322 kwboot_printv("\n");
323
324 return rc;
325}
326
327static int
Stefan Roese84899e22014-10-22 12:13:21 +0200328kwboot_debugmsg(int tty, void *msg)
329{
330 int rc;
331
332 kwboot_printv("Sending debug message. Please reboot the target...");
333
334 do {
335 char buf[16];
336
337 rc = tcflush(tty, TCIOFLUSH);
338 if (rc)
339 break;
340
341 rc = kwboot_tty_send(tty, msg, 8);
342 if (rc) {
343 usleep(msg_req_delay * 1000);
344 continue;
345 }
346
347 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
348
349 kwboot_spinner();
350
351 } while (rc);
352
353 kwboot_printv("\n");
354
355 return rc;
356}
357
Pali Rohárc5d666a2021-09-24 23:06:44 +0200358static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +0000359kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
360 size_t size, int pnum)
361{
Marek Behúnd8cc8512021-09-24 23:06:45 +0200362 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000363
Stefan Roese84899e22014-10-22 12:13:21 +0200364 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000365 block->pnum = pnum;
366 block->_pnum = ~block->pnum;
367
Pali Rohár2ef87f72021-09-24 23:06:48 +0200368 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +0000369 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200370 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +0000371
372 block->csum = 0;
373 for (i = 0; i < n; i++)
374 block->csum += block->data[i];
375
376 return n;
377}
378
Marek Behún12df7b72021-09-24 23:06:52 +0200379static uint64_t
380_now(void)
381{
382 struct timespec ts;
383
384 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
385 static int err_print;
386
387 if (!err_print) {
388 perror("clock_gettime() does not work");
389 err_print = 1;
390 }
391
392 /* this will just make the timeout not work */
393 return -1ULL;
394 }
395
396 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
397}
398
Luka Perkovd131ad62012-05-27 11:44:51 +0000399static int
Marek Behún408ea612021-09-24 23:06:49 +0200400_is_xm_reply(char c)
401{
402 return c == ACK || c == NAK || c == CAN;
403}
404
405static int
Pali Rohár9cdc2642021-09-24 23:06:54 +0200406_xm_reply_to_error(int c)
407{
408 int rc = -1;
409
410 switch (c) {
411 case ACK:
412 rc = 0;
413 break;
414 case NAK:
415 errno = EBADMSG;
416 break;
417 case CAN:
418 errno = ECANCELED;
419 break;
420 default:
421 errno = EPROTO;
422 break;
423 }
424
425 return rc;
426}
427
428static int
Marek Behún2e81b3a2021-09-24 23:06:51 +0200429kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
Pali Rohár48b3ea62021-09-24 23:06:50 +0200430{
Marek Behún12df7b72021-09-24 23:06:52 +0200431 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún819cd322021-09-24 23:06:53 +0200432 uint64_t recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200433 int rc;
434
Marek Behún819cd322021-09-24 23:06:53 +0200435 if (non_xm_print)
436 *non_xm_print = 0;
Marek Behún2e81b3a2021-09-24 23:06:51 +0200437
Pali Rohár48b3ea62021-09-24 23:06:50 +0200438 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +0200439 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200440 if (rc) {
441 if (errno != ETIMEDOUT)
442 return rc;
Marek Behún819cd322021-09-24 23:06:53 +0200443 else if (allow_non_xm && *non_xm_print)
Marek Behún12df7b72021-09-24 23:06:52 +0200444 return -1;
445 else
446 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200447 }
448
449 /* If received xmodem reply, end. */
450 if (_is_xm_reply(*c))
451 break;
452
453 /*
454 * If printing non-xmodem text output is allowed and such a byte
Marek Behún12df7b72021-09-24 23:06:52 +0200455 * was received, print it and increase receiving time.
Marek Behún819cd322021-09-24 23:06:53 +0200456 * Otherwise decrease timeout by time elapsed.
Pali Rohár48b3ea62021-09-24 23:06:50 +0200457 */
458 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +0200459 recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200460 putchar(*c);
461 fflush(stdout);
Marek Behún2e81b3a2021-09-24 23:06:51 +0200462 *non_xm_print = 1;
Marek Behún819cd322021-09-24 23:06:53 +0200463 } else {
464 timeout = recv_until - _now();
465 if (timeout < 0) {
466 errno = ETIMEDOUT;
467 return -1;
468 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200469 }
470 }
471
472 return 0;
473}
474
475static int
476kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
477 int *done_print)
Luka Perkovd131ad62012-05-27 11:44:51 +0000478{
Marek Behún2e81b3a2021-09-24 23:06:51 +0200479 int non_xm_print;
Luka Perkovd131ad62012-05-27 11:44:51 +0000480 int rc, retries;
481 char c;
482
Pali Rohár48b3ea62021-09-24 23:06:50 +0200483 *done_print = 0;
484
Luka Perkovd131ad62012-05-27 11:44:51 +0000485 retries = 16;
486 do {
487 rc = kwboot_tty_send(fd, block, sizeof(*block));
488 if (rc)
Pali Rohár00a1dee2021-09-24 23:06:43 +0200489 return rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000490
Pali Rohár48b3ea62021-09-24 23:06:50 +0200491 if (allow_non_xm && !*done_print) {
492 kwboot_progress(100, '.');
493 kwboot_printv("Done\n");
494 *done_print = 1;
495 }
Stefan Roese84899e22014-10-22 12:13:21 +0200496
Marek Behún2e81b3a2021-09-24 23:06:51 +0200497 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200498 if (rc)
499 return rc;
Stefan Roese84899e22014-10-22 12:13:21 +0200500
Pali Rohár48b3ea62021-09-24 23:06:50 +0200501 if (!allow_non_xm && c != ACK)
Luka Perkovd131ad62012-05-27 11:44:51 +0000502 kwboot_progress(-1, '+');
Luka Perkovd131ad62012-05-27 11:44:51 +0000503 } while (c == NAK && retries-- > 0);
504
Marek Behún2e81b3a2021-09-24 23:06:51 +0200505 if (non_xm_print)
506 kwboot_printv("\n");
507
Pali Rohár9cdc2642021-09-24 23:06:54 +0200508 return _xm_reply_to_error(c);
509}
Luka Perkovd131ad62012-05-27 11:44:51 +0000510
Pali Rohár9cdc2642021-09-24 23:06:54 +0200511static int
512kwboot_xm_finish(int fd)
513{
514 int rc, retries;
515 char c;
Luka Perkovd131ad62012-05-27 11:44:51 +0000516
Pali Rohár9cdc2642021-09-24 23:06:54 +0200517 kwboot_printv("Finishing transfer\n");
518
519 retries = 16;
520 do {
521 rc = kwboot_tty_send_char(fd, EOT);
522 if (rc)
523 return rc;
524
525 rc = kwboot_xm_recv_reply(fd, &c, 0, NULL);
526 if (rc)
527 return rc;
528 } while (c == NAK && retries-- > 0);
529
530 return _xm_reply_to_error(c);
Luka Perkovd131ad62012-05-27 11:44:51 +0000531}
532
533static int
Pali Rohár2ef87f72021-09-24 23:06:48 +0200534kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
535 size_t size)
Luka Perkovd131ad62012-05-27 11:44:51 +0000536{
Pali Rohár48b3ea62021-09-24 23:06:50 +0200537 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200538 size_t sent, left;
539 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000540
Pali Rohár2ef87f72021-09-24 23:06:48 +0200541 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
542 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +0000543
Pali Rohár2ef87f72021-09-24 23:06:48 +0200544 left = size;
545 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000546
Pali Rohár2ef87f72021-09-24 23:06:48 +0200547 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000548 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200549 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200550 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000551
Pali Rohár2ef87f72021-09-24 23:06:48 +0200552 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
553 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000554
Pali Rohár48b3ea62021-09-24 23:06:50 +0200555 last_block = (left <= blksz);
556
557 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
558 &done_print);
Luka Perkovd131ad62012-05-27 11:44:51 +0000559 if (rc)
560 goto out;
561
Pali Rohár2ef87f72021-09-24 23:06:48 +0200562 sent += blksz;
563 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000564
Pali Rohár48b3ea62021-09-24 23:06:50 +0200565 if (!done_print)
566 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +0200567 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000568
Pali Rohár48b3ea62021-09-24 23:06:50 +0200569 if (!done_print)
570 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +0200571
572 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000573out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +0200574 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000575 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200576}
Luka Perkovd131ad62012-05-27 11:44:51 +0000577
Pali Rohár2ef87f72021-09-24 23:06:48 +0200578static int
579kwboot_xmodem(int tty, const void *_img, size_t size)
580{
581 const uint8_t *img = _img;
582 int rc, pnum;
583 size_t hdrsz;
584
Marek Behúnfe2fd732021-09-24 23:07:01 +0200585 hdrsz = kwbheader_size(img);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200586
587 kwboot_printv("Waiting 2s and flushing tty\n");
588 sleep(2); /* flush isn't effective without it */
589 tcflush(tty, TCIOFLUSH);
590
591 pnum = 1;
592
593 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
594 if (rc)
595 return rc;
596
597 img += hdrsz;
598 size -= hdrsz;
599
600 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
601 if (rc)
602 return rc;
603
Pali Rohár9cdc2642021-09-24 23:06:54 +0200604 return kwboot_xm_finish(tty);
Luka Perkovd131ad62012-05-27 11:44:51 +0000605}
606
607static int
Marek Behún46237e62021-09-24 23:06:40 +0200608kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +0000609{
Marek Behúne453bb42021-09-24 23:06:41 +0200610 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +0000611 char _buf[128], *buf = _buf;
612
Pali Rohár43fef8d2021-07-23 11:14:17 +0200613 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -0400614 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000615 return -1;
616
617 if (quit) {
618 int i;
619
620 for (i = 0; i < nin; i++) {
621 if (*buf == quit[*s]) {
622 (*s)++;
623 if (!quit[*s])
624 return 0;
625 buf++;
626 nin--;
Pali Rohárb943eee2021-07-23 11:14:20 +0200627 } else {
Marek Behúne453bb42021-09-24 23:06:41 +0200628 if (kwboot_write(out, quit, *s) < 0)
629 return -1;
630 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +0200631 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000632 }
633 }
634
Marek Behúne453bb42021-09-24 23:06:41 +0200635 if (kwboot_write(out, buf, nin) < 0)
636 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000637
638 return 0;
639}
640
641static int
642kwboot_terminal(int tty)
643{
644 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +0200645 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +0000646 struct termios otio, tio;
647
648 rc = -1;
649
650 in = STDIN_FILENO;
651 if (isatty(in)) {
652 rc = tcgetattr(in, &otio);
653 if (!rc) {
654 tio = otio;
655 cfmakeraw(&tio);
656 rc = tcsetattr(in, TCSANOW, &tio);
657 }
658 if (rc) {
659 perror("tcsetattr");
660 goto out;
661 }
662
663 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
664 quit[0]|0100, quit[1]);
665 } else
666 in = -1;
667
668 rc = 0;
669 s = 0;
670
671 do {
672 fd_set rfds;
673 int nfds = 0;
674
675 FD_SET(tty, &rfds);
676 nfds = nfds < tty ? tty : nfds;
677
678 if (in >= 0) {
679 FD_SET(in, &rfds);
680 nfds = nfds < in ? in : nfds;
681 }
682
683 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
684 if (nfds < 0)
685 break;
686
687 if (FD_ISSET(tty, &rfds)) {
688 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
689 if (rc)
690 break;
691 }
692
Marek Behúnf30cb0d2021-09-24 23:06:39 +0200693 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000694 rc = kwboot_term_pipe(in, tty, quit, &s);
695 if (rc)
696 break;
697 }
698 } while (quit[s] != 0);
699
Pali Rohárec0fe5b2021-07-23 11:14:18 +0200700 if (in >= 0)
701 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +0200702 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000703out:
704 return rc;
705}
706
707static void *
Pali Rohár04ced022021-09-24 23:07:03 +0200708kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovd131ad62012-05-27 11:44:51 +0000709{
Pali Rohárddc04fa2021-09-24 23:06:55 +0200710 int rc, fd;
Luka Perkovd131ad62012-05-27 11:44:51 +0000711 struct stat st;
712 void *img;
Pali Rohár04ced022021-09-24 23:07:03 +0200713 off_t tot;
Luka Perkovd131ad62012-05-27 11:44:51 +0000714
715 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000716 img = NULL;
717
718 fd = open(path, O_RDONLY);
719 if (fd < 0)
720 goto out;
721
722 rc = fstat(fd, &st);
723 if (rc)
724 goto out;
725
Pali Rohár04ced022021-09-24 23:07:03 +0200726 img = malloc(st.st_size + reserve);
727 if (!img)
Luka Perkovd131ad62012-05-27 11:44:51 +0000728 goto out;
Pali Rohár04ced022021-09-24 23:07:03 +0200729
730 tot = 0;
731 while (tot < st.st_size) {
732 ssize_t rd = read(fd, img + tot, st.st_size - tot);
733
734 if (rd < 0)
735 goto out;
736
737 tot += rd;
738
739 if (!rd && tot < st.st_size) {
740 errno = EIO;
741 goto out;
742 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000743 }
744
745 rc = 0;
746 *size = st.st_size;
747out:
748 if (rc && img) {
Pali Rohár04ced022021-09-24 23:07:03 +0200749 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +0000750 img = NULL;
751 }
752 if (fd >= 0)
753 close(fd);
754
755 return img;
756}
757
758static uint8_t
Marek Behúnfe2fd732021-09-24 23:07:01 +0200759kwboot_hdr_csum8(const void *hdr)
Luka Perkovd131ad62012-05-27 11:44:51 +0000760{
Marek Behúnfe2fd732021-09-24 23:07:01 +0200761 const uint8_t *data = hdr;
762 uint8_t csum;
763 size_t size;
764
765 size = kwbheader_size_for_csum(hdr);
Luka Perkovd131ad62012-05-27 11:44:51 +0000766
767 for (csum = 0; size-- > 0; data++)
768 csum += *data;
769
770 return csum;
771}
772
773static int
Pali Rohár550c9302021-09-24 23:06:57 +0200774kwboot_img_is_secure(void *img)
775{
776 struct opt_hdr_v1 *ohdr;
777
778 for_each_opt_hdr_v1 (ohdr, img)
779 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
780 return 1;
781
782 return 0;
783}
784
Pali Rohár04ced022021-09-24 23:07:03 +0200785static void
786kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
787{
788 uint32_t hdrsz, datasz, srcaddr;
789 struct main_hdr_v1 *hdr = img;
790 uint8_t *data;
791
792 srcaddr = le32_to_cpu(hdr->srcaddr);
793
794 hdrsz = kwbheader_size(img);
795 data = (uint8_t *)img + srcaddr;
796 datasz = *size - srcaddr;
797
798 /* only move data if there is not enough space */
799 if (hdrsz + grow > srcaddr) {
800 size_t need = hdrsz + grow - srcaddr;
801
802 /* move data by enough bytes */
803 memmove(data + need, data, datasz);
804
805 hdr->srcaddr = cpu_to_le32(srcaddr + need);
806 *size += need;
807 }
808
809 if (kwbimage_version(img) == 1) {
810 hdrsz += grow;
811 hdr->headersz_msb = hdrsz >> 16;
812 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
813 }
814}
815
Pali Rohár550c9302021-09-24 23:06:57 +0200816static int
Pali Rohár04ced022021-09-24 23:07:03 +0200817kwboot_img_patch_hdr(void *img, size_t *size)
Luka Perkovd131ad62012-05-27 11:44:51 +0000818{
819 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200820 struct main_hdr_v1 *hdr;
Pali Rohár792e4232021-09-24 23:06:58 +0200821 uint32_t srcaddr;
Luka Perkovd131ad62012-05-27 11:44:51 +0000822 uint8_t csum;
Marek Behún5c8f8122021-09-24 23:07:04 +0200823 size_t hdrsz;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200824 int image_ver;
Pali Rohár550c9302021-09-24 23:06:57 +0200825 int is_secure;
Luka Perkovd131ad62012-05-27 11:44:51 +0000826
827 rc = -1;
828 hdr = img;
829
Marek Behún5c8f8122021-09-24 23:07:04 +0200830 if (*size < sizeof(struct main_hdr_v1)) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000831 errno = EINVAL;
832 goto out;
833 }
834
Marek Behúnacb0b382021-09-24 23:07:00 +0200835 image_ver = kwbimage_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +0200836 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +0200837 fprintf(stderr, "Invalid image header version\n");
838 errno = EINVAL;
839 goto out;
840 }
841
Marek Behúnfe2fd732021-09-24 23:07:01 +0200842 hdrsz = kwbheader_size(hdr);
Stefan Roesee29f1db2015-09-29 09:19:59 +0200843
Pali Rohár04ced022021-09-24 23:07:03 +0200844 if (*size < hdrsz) {
Pali Rohár825a2ca2021-07-23 11:14:21 +0200845 errno = EINVAL;
846 goto out;
847 }
848
Marek Behúnfe2fd732021-09-24 23:07:01 +0200849 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200850 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000851 errno = EINVAL;
852 goto out;
853 }
854
Pali Rohár792e4232021-09-24 23:06:58 +0200855 if (image_ver == 0) {
856 struct main_hdr_v0 *hdr_v0 = img;
857
858 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
859 hdr_v0->nandpagesize = 0;
860 }
861
862 srcaddr = le32_to_cpu(hdr->srcaddr);
863
864 switch (hdr->blockid) {
865 case IBR_HDR_SATA_ID:
866 if (srcaddr < 1) {
867 errno = EINVAL;
868 goto out;
869 }
870 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
871 break;
872
873 case IBR_HDR_SDIO_ID:
874 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
875 break;
876
877 case IBR_HDR_PEX_ID:
878 if (srcaddr == 0xFFFFFFFF)
879 hdr->srcaddr = cpu_to_le32(hdrsz);
880 break;
Pali Rohárf2c644e2021-09-24 23:06:59 +0200881
882 case IBR_HDR_SPI_ID:
883 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
884 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
885 hdr->destaddr = cpu_to_le32(0x00800000);
886 hdr->execaddr = cpu_to_le32(0x00800000);
887 }
888 break;
Pali Rohár792e4232021-09-24 23:06:58 +0200889 }
890
Pali Rohár04ced022021-09-24 23:07:03 +0200891 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
892 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) {
893 errno = EINVAL;
894 goto out;
895 }
896
Pali Rohár550c9302021-09-24 23:06:57 +0200897 is_secure = kwboot_img_is_secure(img);
Luka Perkovd131ad62012-05-27 11:44:51 +0000898
Pali Rohár550c9302021-09-24 23:06:57 +0200899 if (hdr->blockid != IBR_HDR_UART_ID) {
900 if (is_secure) {
901 fprintf(stderr,
902 "Image has secure header with signature for non-UART booting\n");
903 errno = EINVAL;
904 goto out;
905 }
906
907 kwboot_printv("Patching image boot signature to UART\n");
908 hdr->blockid = IBR_HDR_UART_ID;
909 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000910
Pali Rohár04ced022021-09-24 23:07:03 +0200911 if (hdrsz % KWBOOT_XM_BLKSZ) {
912 size_t offset = (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) %
913 KWBOOT_XM_BLKSZ;
914
915 if (is_secure) {
916 fprintf(stderr, "Cannot align image with secure header\n");
917 errno = EINVAL;
918 goto out;
919 }
920
921 kwboot_printv("Aligning image header to Xmodem block size\n");
922 kwboot_img_grow_hdr(img, size, offset);
923 }
924
Marek Behúnfe2fd732021-09-24 23:07:01 +0200925 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +0000926
Pali Rohár04ced022021-09-24 23:07:03 +0200927 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Luka Perkovd131ad62012-05-27 11:44:51 +0000928 rc = 0;
929out:
930 return rc;
931}
932
933static void
934kwboot_usage(FILE *stream, char *progname)
935{
Pali Rohára050a862021-09-24 23:06:42 +0200936 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovd131ad62012-05-27 11:44:51 +0000937 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +0000938 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +0200939 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +0000940 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200941 fprintf(stream,
942 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roese84899e22014-10-22 12:13:21 +0200943 fprintf(stream,
944 " -D <image>: boot <image> without preamble (Dove)\n");
945 fprintf(stream, " -d: enter debug mode\n");
946 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +0200947 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
948 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +0000949 fprintf(stream,
950 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000951 fprintf(stream, "\n");
952 fprintf(stream, " -t: mini terminal\n");
953 fprintf(stream, "\n");
954 fprintf(stream, " -B <baud>: set baud rate\n");
955 fprintf(stream, "\n");
956}
957
958int
959main(int argc, char **argv)
960{
961 const char *ttypath, *imgpath;
Pali Rohárddc04fa2021-09-24 23:06:55 +0200962 int rv, rc, tty, term;
Luka Perkovd131ad62012-05-27 11:44:51 +0000963 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +0200964 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +0000965 void *img;
966 size_t size;
967 speed_t speed;
968
969 rv = 1;
970 tty = -1;
971 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +0200972 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +0000973 imgpath = NULL;
974 img = NULL;
975 term = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000976 size = 0;
977 speed = B115200;
978
979 kwboot_verbose = isatty(STDOUT_FILENO);
980
981 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +0000982 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +0000983 if (c < 0)
984 break;
985
986 switch (c) {
987 case 'b':
988 bootmsg = kwboot_msg_boot;
989 imgpath = optarg;
990 break;
991
Stefan Roese84899e22014-10-22 12:13:21 +0200992 case 'D':
993 bootmsg = NULL;
994 imgpath = optarg;
995 break;
996
997 case 'd':
998 debugmsg = kwboot_msg_debug;
999 break;
1000
Luka Perkovd131ad62012-05-27 11:44:51 +00001001 case 'p':
Pali Rohárddc04fa2021-09-24 23:06:55 +02001002 /* nop, for backward compatibility */
Luka Perkovd131ad62012-05-27 11:44:51 +00001003 break;
1004
1005 case 't':
1006 term = 1;
1007 break;
1008
Stefan Roese84899e22014-10-22 12:13:21 +02001009 case 'a':
1010 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
1011 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1012 break;
1013
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001014 case 'q':
1015 msg_req_delay = atoi(optarg);
1016 break;
1017
1018 case 's':
1019 msg_rsp_timeo = atoi(optarg);
1020 break;
1021
Kevin Smith7497a6a2016-02-16 21:28:19 +00001022 case 'o':
1023 blk_rsp_timeo = atoi(optarg);
1024 break;
1025
Luka Perkovd131ad62012-05-27 11:44:51 +00001026 case 'B':
1027 speed = kwboot_tty_speed(atoi(optarg));
1028 if (speed == -1)
1029 goto usage;
1030 break;
1031
1032 case 'h':
1033 rv = 0;
1034 default:
1035 goto usage;
1036 }
1037 } while (1);
1038
Stefan Roese84899e22014-10-22 12:13:21 +02001039 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +00001040 goto usage;
1041
Luka Perkovd131ad62012-05-27 11:44:51 +00001042 if (argc - optind < 1)
1043 goto usage;
1044
1045 ttypath = argv[optind++];
1046
1047 tty = kwboot_open_tty(ttypath, speed);
1048 if (tty < 0) {
1049 perror(ttypath);
1050 goto out;
1051 }
1052
1053 if (imgpath) {
Pali Rohár04ced022021-09-24 23:07:03 +02001054 img = kwboot_read_image(imgpath, &size, KWBOOT_XM_BLKSZ);
Luka Perkovd131ad62012-05-27 11:44:51 +00001055 if (!img) {
1056 perror(imgpath);
1057 goto out;
1058 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001059
Pali Rohár04ced022021-09-24 23:07:03 +02001060 rc = kwboot_img_patch_hdr(img, &size);
Luka Perkovd131ad62012-05-27 11:44:51 +00001061 if (rc) {
1062 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1063 goto out;
1064 }
1065 }
1066
Stefan Roese84899e22014-10-22 12:13:21 +02001067 if (debugmsg) {
1068 rc = kwboot_debugmsg(tty, debugmsg);
1069 if (rc) {
1070 perror("debugmsg");
1071 goto out;
1072 }
Willy Tarreau3475a712018-07-03 12:10:30 -04001073 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001074 rc = kwboot_bootmsg(tty, bootmsg);
1075 if (rc) {
1076 perror("bootmsg");
1077 goto out;
1078 }
1079 }
1080
1081 if (img) {
1082 rc = kwboot_xmodem(tty, img, size);
1083 if (rc) {
1084 perror("xmodem");
1085 goto out;
1086 }
1087 }
1088
1089 if (term) {
1090 rc = kwboot_terminal(tty);
1091 if (rc && !(errno == EINTR)) {
1092 perror("terminal");
1093 goto out;
1094 }
1095 }
1096
1097 rv = 0;
1098out:
1099 if (tty >= 0)
1100 close(tty);
1101
1102 if (img)
Pali Rohár04ced022021-09-24 23:07:03 +02001103 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001104
1105 return rv;
1106
1107usage:
1108 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1109 goto out;
1110}