]>
Commit | Line | Data |
---|---|---|
d131ad68 | 1 | /* |
84899e2d SR |
2 | * Boot a Marvell SoC, with Xmodem over UART0. |
3 | * supports Kirkwood, Dove, Armada 370, Armada XP | |
d131ad68 LP |
4 | * |
5 | * (c) 2012 Daniel Stodden <[email protected]> | |
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 | ||
f4db6c97 SR |
12 | #include "kwbimage.h" |
13 | #include "mkimage.h" | |
a050a862 | 14 | #include "version.h" |
f4db6c97 | 15 | |
d131ad68 LP |
16 | #include <stdlib.h> |
17 | #include <stdio.h> | |
18 | #include <string.h> | |
19 | #include <stdarg.h> | |
f4db6c97 | 20 | #include <image.h> |
d131ad68 LP |
21 | #include <libgen.h> |
22 | #include <fcntl.h> | |
23 | #include <errno.h> | |
24 | #include <unistd.h> | |
25 | #include <stdint.h> | |
26 | #include <termios.h> | |
12df7b79 | 27 | #include <time.h> |
d131ad68 LP |
28 | #include <sys/mman.h> |
29 | #include <sys/stat.h> | |
30 | ||
d131ad68 LP |
31 | /* |
32 | * Marvell BootROM UART Sensing | |
33 | */ | |
34 | ||
35 | static unsigned char kwboot_msg_boot[] = { | |
36 | 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 | |
37 | }; | |
38 | ||
84899e2d SR |
39 | static unsigned char kwboot_msg_debug[] = { |
40 | 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 | |
41 | }; | |
42 | ||
43 | /* Defines known to work on Kirkwood */ | |
d131ad68 LP |
44 | #define KWBOOT_MSG_REQ_DELAY 10 /* ms */ |
45 | #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */ | |
46 | ||
84899e2d SR |
47 | /* 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 | ||
d131ad68 LP |
51 | /* |
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 | ||
2ef87f75 T |
61 | #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */ |
62 | ||
d131ad68 LP |
63 | struct kwboot_block { |
64 | uint8_t soh; | |
65 | uint8_t pnum; | |
66 | uint8_t _pnum; | |
2ef87f75 | 67 | uint8_t data[KWBOOT_XM_BLKSZ]; |
d131ad68 | 68 | uint8_t csum; |
a107c61b | 69 | } __packed; |
d131ad68 LP |
70 | |
71 | #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */ | |
12df7b79 | 72 | #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */ |
d131ad68 LP |
73 | |
74 | static int kwboot_verbose; | |
75 | ||
84899e2d SR |
76 | static int msg_req_delay = KWBOOT_MSG_REQ_DELAY; |
77 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; | |
7497a6a1 | 78 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
84899e2d | 79 | |
e453bb42 MB |
80 | static ssize_t |
81 | kwboot_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 | ||
d131ad68 LP |
97 | static void |
98 | kwboot_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 | ||
110 | static 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 | ||
127 | static void | |
128 | kwboot_spinner(void) | |
129 | { | |
130 | if (kwboot_verbose) | |
131 | __spinner(); | |
132 | } | |
133 | ||
134 | static 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"; | |
5a1f8cbe | 147 | pos = (pos + 1) % width; |
d131ad68 LP |
148 | |
149 | if (pct == 100) { | |
5a1f8cbe | 150 | while (pos && pos++ < width) |
d131ad68 LP |
151 | fputc(' ', stdout); |
152 | fputs(nl, stdout); | |
5a1f8cbe T |
153 | nl = ""; |
154 | pos = 0; | |
d131ad68 LP |
155 | } |
156 | ||
157 | fflush(stdout); | |
158 | ||
159 | } | |
160 | ||
161 | static void | |
162 | kwboot_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); | |
5a1f8cbe T |
171 | |
172 | if (pct == 100) | |
173 | pct = 0; | |
d131ad68 LP |
174 | } |
175 | ||
176 | static int | |
177 | kwboot_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); | |
4469bd7b | 206 | if (n <= 0) |
d131ad68 LP |
207 | goto out; |
208 | ||
209 | buf = (char *)buf + n; | |
210 | len -= n; | |
211 | } while (len > 0); | |
212 | ||
213 | rc = 0; | |
214 | out: | |
215 | return rc; | |
216 | } | |
217 | ||
218 | static int | |
219 | kwboot_tty_send(int fd, const void *buf, size_t len) | |
220 | { | |
84899e2d SR |
221 | if (!buf) |
222 | return 0; | |
223 | ||
e453bb42 MB |
224 | if (kwboot_write(fd, buf, len) < 0) |
225 | return -1; | |
d131ad68 | 226 | |
e453bb42 | 227 | return tcdrain(fd); |
d131ad68 LP |
228 | } |
229 | ||
230 | static int | |
231 | kwboot_tty_send_char(int fd, unsigned char c) | |
232 | { | |
233 | return kwboot_tty_send(fd, &c, 1); | |
234 | } | |
235 | ||
236 | static speed_t | |
237 | kwboot_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 | ||
255 | static int | |
256 | kwboot_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; | |
283 | out: | |
284 | if (rc < 0) { | |
285 | if (fd >= 0) | |
286 | close(fd); | |
287 | } | |
288 | ||
289 | return rc; | |
290 | } | |
291 | ||
292 | static int | |
293 | kwboot_bootmsg(int tty, void *msg) | |
294 | { | |
295 | int rc; | |
296 | char c; | |
9ca6fae9 | 297 | int count; |
d131ad68 | 298 | |
84899e2d SR |
299 | 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..."); | |
d131ad68 LP |
303 | |
304 | do { | |
305 | rc = tcflush(tty, TCIOFLUSH); | |
306 | if (rc) | |
307 | break; | |
308 | ||
9ca6fae9 JN |
309 | 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 | } | |
d131ad68 LP |
315 | } |
316 | ||
84899e2d | 317 | rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo); |
d131ad68 LP |
318 | |
319 | kwboot_spinner(); | |
320 | ||
321 | } while (rc || c != NAK); | |
322 | ||
323 | kwboot_printv("\n"); | |
324 | ||
325 | return rc; | |
326 | } | |
327 | ||
84899e2d SR |
328 | static int |
329 | kwboot_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 | ||
c5d666aa | 359 | static size_t |
d131ad68 LP |
360 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
361 | size_t size, int pnum) | |
362 | { | |
d8cc851d | 363 | size_t i, n; |
d131ad68 | 364 | |
84899e2d | 365 | block->soh = SOH; |
d131ad68 LP |
366 | block->pnum = pnum; |
367 | block->_pnum = ~block->pnum; | |
368 | ||
2ef87f75 | 369 | n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ; |
d131ad68 | 370 | memcpy(&block->data[0], data, n); |
2ef87f75 | 371 | memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n); |
d131ad68 LP |
372 | |
373 | block->csum = 0; | |
374 | for (i = 0; i < n; i++) | |
375 | block->csum += block->data[i]; | |
376 | ||
377 | return n; | |
378 | } | |
379 | ||
12df7b79 MB |
380 | static 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 | ||
408ea613 MB |
400 | static int |
401 | _is_xm_reply(char c) | |
402 | { | |
403 | return c == ACK || c == NAK || c == CAN; | |
404 | } | |
405 | ||
9cdc264e T |
406 | static int |
407 | _xm_reply_to_error(int c) | |
408 | { | |
409 | int rc = -1; | |
410 | ||
411 | switch (c) { | |
412 | case ACK: | |
413 | rc = 0; | |
414 | break; | |
415 | case NAK: | |
416 | errno = EBADMSG; | |
417 | break; | |
418 | case CAN: | |
419 | errno = ECANCELED; | |
420 | break; | |
421 | default: | |
422 | errno = EPROTO; | |
423 | break; | |
424 | } | |
425 | ||
426 | return rc; | |
427 | } | |
428 | ||
d131ad68 | 429 | static int |
2e81b3ab | 430 | kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print) |
48b3ea66 | 431 | { |
12df7b79 | 432 | int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo; |
819cd328 | 433 | uint64_t recv_until = _now() + timeout; |
48b3ea66 T |
434 | int rc; |
435 | ||
819cd328 MB |
436 | if (non_xm_print) |
437 | *non_xm_print = 0; | |
2e81b3ab | 438 | |
48b3ea66 | 439 | while (1) { |
12df7b79 | 440 | rc = kwboot_tty_recv(fd, c, 1, timeout); |
48b3ea66 T |
441 | if (rc) { |
442 | if (errno != ETIMEDOUT) | |
443 | return rc; | |
819cd328 | 444 | else if (allow_non_xm && *non_xm_print) |
12df7b79 MB |
445 | return -1; |
446 | else | |
447 | *c = NAK; | |
48b3ea66 T |
448 | } |
449 | ||
450 | /* If received xmodem reply, end. */ | |
451 | if (_is_xm_reply(*c)) | |
452 | break; | |
453 | ||
454 | /* | |
455 | * If printing non-xmodem text output is allowed and such a byte | |
12df7b79 | 456 | * was received, print it and increase receiving time. |
819cd328 | 457 | * Otherwise decrease timeout by time elapsed. |
48b3ea66 T |
458 | */ |
459 | if (allow_non_xm) { | |
12df7b79 | 460 | recv_until = _now() + timeout; |
48b3ea66 T |
461 | putchar(*c); |
462 | fflush(stdout); | |
2e81b3ab | 463 | *non_xm_print = 1; |
819cd328 MB |
464 | } else { |
465 | timeout = recv_until - _now(); | |
466 | if (timeout < 0) { | |
467 | errno = ETIMEDOUT; | |
468 | return -1; | |
469 | } | |
48b3ea66 T |
470 | } |
471 | } | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
476 | static int | |
477 | kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, | |
478 | int *done_print) | |
d131ad68 | 479 | { |
2e81b3ab | 480 | int non_xm_print; |
d131ad68 LP |
481 | int rc, retries; |
482 | char c; | |
483 | ||
48b3ea66 T |
484 | *done_print = 0; |
485 | ||
d131ad68 LP |
486 | retries = 16; |
487 | do { | |
488 | rc = kwboot_tty_send(fd, block, sizeof(*block)); | |
489 | if (rc) | |
00a1deed | 490 | return rc; |
d131ad68 | 491 | |
48b3ea66 T |
492 | if (allow_non_xm && !*done_print) { |
493 | kwboot_progress(100, '.'); | |
494 | kwboot_printv("Done\n"); | |
495 | *done_print = 1; | |
496 | } | |
84899e2d | 497 | |
2e81b3ab | 498 | rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print); |
48b3ea66 T |
499 | if (rc) |
500 | return rc; | |
d131ad68 | 501 | |
48b3ea66 | 502 | if (!allow_non_xm && c != ACK) |
d131ad68 | 503 | kwboot_progress(-1, '+'); |
d131ad68 LP |
504 | } while (c == NAK && retries-- > 0); |
505 | ||
2e81b3ab MB |
506 | if (non_xm_print) |
507 | kwboot_printv("\n"); | |
508 | ||
9cdc264e T |
509 | return _xm_reply_to_error(c); |
510 | } | |
d131ad68 | 511 | |
9cdc264e T |
512 | static int |
513 | kwboot_xm_finish(int fd) | |
514 | { | |
515 | int rc, retries; | |
516 | char c; | |
d131ad68 | 517 | |
9cdc264e T |
518 | kwboot_printv("Finishing transfer\n"); |
519 | ||
520 | retries = 16; | |
521 | do { | |
522 | rc = kwboot_tty_send_char(fd, EOT); | |
523 | if (rc) | |
524 | return rc; | |
525 | ||
526 | rc = kwboot_xm_recv_reply(fd, &c, 0, NULL); | |
527 | if (rc) | |
528 | return rc; | |
529 | } while (c == NAK && retries-- > 0); | |
530 | ||
531 | return _xm_reply_to_error(c); | |
d131ad68 LP |
532 | } |
533 | ||
534 | static int | |
2ef87f75 T |
535 | kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data, |
536 | size_t size) | |
d131ad68 | 537 | { |
48b3ea66 | 538 | int done_print = 0; |
2ef87f75 T |
539 | size_t sent, left; |
540 | int rc; | |
d131ad68 | 541 | |
2ef87f75 T |
542 | kwboot_printv("Sending boot image %s (%zu bytes)...\n", |
543 | header ? "header" : "data", size); | |
d131ad68 | 544 | |
2ef87f75 T |
545 | left = size; |
546 | sent = 0; | |
d131ad68 | 547 | |
2ef87f75 | 548 | while (sent < size) { |
d131ad68 | 549 | struct kwboot_block block; |
48b3ea66 | 550 | int last_block; |
2ef87f75 | 551 | size_t blksz; |
d131ad68 | 552 | |
2ef87f75 T |
553 | blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++); |
554 | data += blksz; | |
d131ad68 | 555 | |
48b3ea66 T |
556 | last_block = (left <= blksz); |
557 | ||
558 | rc = kwboot_xm_sendblock(tty, &block, header && last_block, | |
559 | &done_print); | |
d131ad68 LP |
560 | if (rc) |
561 | goto out; | |
562 | ||
2ef87f75 T |
563 | sent += blksz; |
564 | left -= blksz; | |
565 | ||
48b3ea66 T |
566 | if (!done_print) |
567 | kwboot_progress(sent * 100 / size, '.'); | |
2ef87f75 | 568 | } |
d131ad68 | 569 | |
48b3ea66 T |
570 | if (!done_print) |
571 | kwboot_printv("Done\n"); | |
d131ad68 | 572 | |
2ef87f75 | 573 | return 0; |
d131ad68 | 574 | out: |
d5ba8dbd | 575 | kwboot_printv("\n"); |
d131ad68 | 576 | return rc; |
2ef87f75 T |
577 | } |
578 | ||
579 | static int | |
580 | kwboot_xmodem(int tty, const void *_img, size_t size) | |
581 | { | |
582 | const uint8_t *img = _img; | |
583 | int rc, pnum; | |
584 | size_t hdrsz; | |
585 | ||
acb0b38d | 586 | if (kwbimage_version(img) == 0) |
2ef87f75 T |
587 | hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img); |
588 | else | |
589 | hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img); | |
590 | ||
591 | kwboot_printv("Waiting 2s and flushing tty\n"); | |
592 | sleep(2); /* flush isn't effective without it */ | |
593 | tcflush(tty, TCIOFLUSH); | |
594 | ||
595 | pnum = 1; | |
596 | ||
597 | rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz); | |
598 | if (rc) | |
599 | return rc; | |
600 | ||
601 | img += hdrsz; | |
602 | size -= hdrsz; | |
603 | ||
604 | rc = kwboot_xmodem_one(tty, &pnum, 0, img, size); | |
605 | if (rc) | |
606 | return rc; | |
d131ad68 | 607 | |
9cdc264e | 608 | return kwboot_xm_finish(tty); |
d131ad68 LP |
609 | } |
610 | ||
611 | static int | |
46237e63 | 612 | kwboot_term_pipe(int in, int out, const char *quit, int *s) |
d131ad68 | 613 | { |
e453bb42 | 614 | ssize_t nin; |
d131ad68 LP |
615 | char _buf[128], *buf = _buf; |
616 | ||
43fef8d4 | 617 | nin = read(in, buf, sizeof(_buf)); |
4469bd7b | 618 | if (nin <= 0) |
d131ad68 LP |
619 | return -1; |
620 | ||
621 | if (quit) { | |
622 | int i; | |
623 | ||
624 | for (i = 0; i < nin; i++) { | |
625 | if (*buf == quit[*s]) { | |
626 | (*s)++; | |
627 | if (!quit[*s]) | |
628 | return 0; | |
629 | buf++; | |
630 | nin--; | |
b943eee9 | 631 | } else { |
e453bb42 MB |
632 | if (kwboot_write(out, quit, *s) < 0) |
633 | return -1; | |
634 | *s = 0; | |
b943eee9 | 635 | } |
d131ad68 LP |
636 | } |
637 | } | |
638 | ||
e453bb42 MB |
639 | if (kwboot_write(out, buf, nin) < 0) |
640 | return -1; | |
d131ad68 LP |
641 | |
642 | return 0; | |
643 | } | |
644 | ||
645 | static int | |
646 | kwboot_terminal(int tty) | |
647 | { | |
648 | int rc, in, s; | |
46237e63 | 649 | const char *quit = "\34c"; |
d131ad68 LP |
650 | struct termios otio, tio; |
651 | ||
652 | rc = -1; | |
653 | ||
654 | in = STDIN_FILENO; | |
655 | if (isatty(in)) { | |
656 | rc = tcgetattr(in, &otio); | |
657 | if (!rc) { | |
658 | tio = otio; | |
659 | cfmakeraw(&tio); | |
660 | rc = tcsetattr(in, TCSANOW, &tio); | |
661 | } | |
662 | if (rc) { | |
663 | perror("tcsetattr"); | |
664 | goto out; | |
665 | } | |
666 | ||
667 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", | |
668 | quit[0]|0100, quit[1]); | |
669 | } else | |
670 | in = -1; | |
671 | ||
672 | rc = 0; | |
673 | s = 0; | |
674 | ||
675 | do { | |
676 | fd_set rfds; | |
677 | int nfds = 0; | |
678 | ||
679 | FD_SET(tty, &rfds); | |
680 | nfds = nfds < tty ? tty : nfds; | |
681 | ||
682 | if (in >= 0) { | |
683 | FD_SET(in, &rfds); | |
684 | nfds = nfds < in ? in : nfds; | |
685 | } | |
686 | ||
687 | nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); | |
688 | if (nfds < 0) | |
689 | break; | |
690 | ||
691 | if (FD_ISSET(tty, &rfds)) { | |
692 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); | |
693 | if (rc) | |
694 | break; | |
695 | } | |
696 | ||
f30cb0d3 | 697 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
d131ad68 LP |
698 | rc = kwboot_term_pipe(in, tty, quit, &s); |
699 | if (rc) | |
700 | break; | |
701 | } | |
702 | } while (quit[s] != 0); | |
703 | ||
ec0fe5b8 T |
704 | if (in >= 0) |
705 | tcsetattr(in, TCSANOW, &otio); | |
49a0a3b8 | 706 | printf("\n"); |
d131ad68 LP |
707 | out: |
708 | return rc; | |
709 | } | |
710 | ||
711 | static void * | |
ddc04fac | 712 | kwboot_mmap_image(const char *path, size_t *size) |
d131ad68 | 713 | { |
ddc04fac | 714 | int rc, fd; |
d131ad68 LP |
715 | struct stat st; |
716 | void *img; | |
717 | ||
718 | rc = -1; | |
d131ad68 LP |
719 | img = NULL; |
720 | ||
721 | fd = open(path, O_RDONLY); | |
722 | if (fd < 0) | |
723 | goto out; | |
724 | ||
725 | rc = fstat(fd, &st); | |
726 | if (rc) | |
727 | goto out; | |
728 | ||
ddc04fac | 729 | img = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
d131ad68 LP |
730 | if (img == MAP_FAILED) { |
731 | img = NULL; | |
732 | goto out; | |
733 | } | |
734 | ||
735 | rc = 0; | |
736 | *size = st.st_size; | |
737 | out: | |
738 | if (rc && img) { | |
739 | munmap(img, st.st_size); | |
740 | img = NULL; | |
741 | } | |
742 | if (fd >= 0) | |
743 | close(fd); | |
744 | ||
745 | return img; | |
746 | } | |
747 | ||
748 | static uint8_t | |
749 | kwboot_img_csum8(void *_data, size_t size) | |
750 | { | |
751 | uint8_t *data = _data, csum; | |
752 | ||
753 | for (csum = 0; size-- > 0; data++) | |
754 | csum += *data; | |
755 | ||
756 | return csum; | |
757 | } | |
758 | ||
550c9308 T |
759 | static int |
760 | kwboot_img_is_secure(void *img) | |
761 | { | |
762 | struct opt_hdr_v1 *ohdr; | |
763 | ||
764 | for_each_opt_hdr_v1 (ohdr, img) | |
765 | if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE) | |
766 | return 1; | |
767 | ||
768 | return 0; | |
769 | } | |
770 | ||
d131ad68 LP |
771 | static int |
772 | kwboot_img_patch_hdr(void *img, size_t size) | |
773 | { | |
774 | int rc; | |
e29f1db3 | 775 | struct main_hdr_v1 *hdr; |
792e4235 | 776 | uint32_t srcaddr; |
d131ad68 | 777 | uint8_t csum; |
e29f1db3 SR |
778 | size_t hdrsz = sizeof(*hdr); |
779 | int image_ver; | |
550c9308 | 780 | int is_secure; |
d131ad68 LP |
781 | |
782 | rc = -1; | |
783 | hdr = img; | |
784 | ||
785 | if (size < hdrsz) { | |
786 | errno = EINVAL; | |
787 | goto out; | |
788 | } | |
789 | ||
acb0b38d | 790 | image_ver = kwbimage_version(img); |
5029d7bf | 791 | if (image_ver != 0 && image_ver != 1) { |
e29f1db3 SR |
792 | fprintf(stderr, "Invalid image header version\n"); |
793 | errno = EINVAL; | |
794 | goto out; | |
795 | } | |
796 | ||
797 | if (image_ver == 0) | |
798 | hdrsz = sizeof(*hdr); | |
799 | else | |
800 | hdrsz = KWBHEADER_V1_SIZE(hdr); | |
801 | ||
825a2ca0 T |
802 | if (size < hdrsz) { |
803 | errno = EINVAL; | |
804 | goto out; | |
805 | } | |
806 | ||
e29f1db3 SR |
807 | csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum; |
808 | if (csum != hdr->checksum) { | |
d131ad68 LP |
809 | errno = EINVAL; |
810 | goto out; | |
811 | } | |
812 | ||
792e4235 T |
813 | if (image_ver == 0) { |
814 | struct main_hdr_v0 *hdr_v0 = img; | |
815 | ||
816 | hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED; | |
817 | hdr_v0->nandpagesize = 0; | |
818 | } | |
819 | ||
820 | srcaddr = le32_to_cpu(hdr->srcaddr); | |
821 | ||
822 | switch (hdr->blockid) { | |
823 | case IBR_HDR_SATA_ID: | |
824 | if (srcaddr < 1) { | |
825 | errno = EINVAL; | |
826 | goto out; | |
827 | } | |
828 | hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512); | |
829 | break; | |
830 | ||
831 | case IBR_HDR_SDIO_ID: | |
832 | hdr->srcaddr = cpu_to_le32(srcaddr * 512); | |
833 | break; | |
834 | ||
835 | case IBR_HDR_PEX_ID: | |
836 | if (srcaddr == 0xFFFFFFFF) | |
837 | hdr->srcaddr = cpu_to_le32(hdrsz); | |
838 | break; | |
f2c644e0 T |
839 | |
840 | case IBR_HDR_SPI_ID: | |
841 | if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) { | |
842 | kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n"); | |
843 | hdr->destaddr = cpu_to_le32(0x00800000); | |
844 | hdr->execaddr = cpu_to_le32(0x00800000); | |
845 | } | |
846 | break; | |
792e4235 T |
847 | } |
848 | ||
550c9308 | 849 | is_secure = kwboot_img_is_secure(img); |
d131ad68 | 850 | |
550c9308 T |
851 | if (hdr->blockid != IBR_HDR_UART_ID) { |
852 | if (is_secure) { | |
853 | fprintf(stderr, | |
854 | "Image has secure header with signature for non-UART booting\n"); | |
855 | errno = EINVAL; | |
856 | goto out; | |
857 | } | |
858 | ||
859 | kwboot_printv("Patching image boot signature to UART\n"); | |
860 | hdr->blockid = IBR_HDR_UART_ID; | |
861 | } | |
d131ad68 | 862 | |
e29f1db3 | 863 | hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum; |
d131ad68 LP |
864 | |
865 | rc = 0; | |
866 | out: | |
867 | return rc; | |
868 | } | |
869 | ||
870 | static void | |
871 | kwboot_usage(FILE *stream, char *progname) | |
872 | { | |
a050a862 | 873 | fprintf(stream, "kwboot version %s\n", PLAIN_VERSION); |
d131ad68 | 874 | fprintf(stream, |
8669dacf | 875 | "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n", |
84899e2d | 876 | progname); |
d131ad68 | 877 | fprintf(stream, "\n"); |
84899e2d SR |
878 | fprintf(stream, |
879 | " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); | |
84899e2d SR |
880 | fprintf(stream, |
881 | " -D <image>: boot <image> without preamble (Dove)\n"); | |
882 | fprintf(stream, " -d: enter debug mode\n"); | |
883 | fprintf(stream, " -a: use timings for Armada XP\n"); | |
1c0df9ef SR |
884 | fprintf(stream, " -q <req-delay>: use specific request-delay\n"); |
885 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); | |
7497a6a1 KS |
886 | fprintf(stream, |
887 | " -o <block-timeo>: use specific xmodem block timeout\n"); | |
d131ad68 LP |
888 | fprintf(stream, "\n"); |
889 | fprintf(stream, " -t: mini terminal\n"); | |
890 | fprintf(stream, "\n"); | |
891 | fprintf(stream, " -B <baud>: set baud rate\n"); | |
892 | fprintf(stream, "\n"); | |
893 | } | |
894 | ||
895 | int | |
896 | main(int argc, char **argv) | |
897 | { | |
898 | const char *ttypath, *imgpath; | |
ddc04fac | 899 | int rv, rc, tty, term; |
d131ad68 | 900 | void *bootmsg; |
84899e2d | 901 | void *debugmsg; |
d131ad68 LP |
902 | void *img; |
903 | size_t size; | |
904 | speed_t speed; | |
905 | ||
906 | rv = 1; | |
907 | tty = -1; | |
908 | bootmsg = NULL; | |
84899e2d | 909 | debugmsg = NULL; |
d131ad68 LP |
910 | imgpath = NULL; |
911 | img = NULL; | |
912 | term = 0; | |
d131ad68 LP |
913 | size = 0; |
914 | speed = B115200; | |
915 | ||
916 | kwboot_verbose = isatty(STDOUT_FILENO); | |
917 | ||
918 | do { | |
7497a6a1 | 919 | int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:"); |
d131ad68 LP |
920 | if (c < 0) |
921 | break; | |
922 | ||
923 | switch (c) { | |
924 | case 'b': | |
925 | bootmsg = kwboot_msg_boot; | |
926 | imgpath = optarg; | |
927 | break; | |
928 | ||
84899e2d SR |
929 | case 'D': |
930 | bootmsg = NULL; | |
931 | imgpath = optarg; | |
932 | break; | |
933 | ||
934 | case 'd': | |
935 | debugmsg = kwboot_msg_debug; | |
936 | break; | |
937 | ||
d131ad68 | 938 | case 'p': |
ddc04fac | 939 | /* nop, for backward compatibility */ |
d131ad68 LP |
940 | break; |
941 | ||
942 | case 't': | |
943 | term = 1; | |
944 | break; | |
945 | ||
84899e2d SR |
946 | case 'a': |
947 | msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP; | |
948 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; | |
949 | break; | |
950 | ||
1c0df9ef SR |
951 | case 'q': |
952 | msg_req_delay = atoi(optarg); | |
953 | break; | |
954 | ||
955 | case 's': | |
956 | msg_rsp_timeo = atoi(optarg); | |
957 | break; | |
958 | ||
7497a6a1 KS |
959 | case 'o': |
960 | blk_rsp_timeo = atoi(optarg); | |
961 | break; | |
962 | ||
d131ad68 LP |
963 | case 'B': |
964 | speed = kwboot_tty_speed(atoi(optarg)); | |
965 | if (speed == -1) | |
966 | goto usage; | |
967 | break; | |
968 | ||
969 | case 'h': | |
970 | rv = 0; | |
971 | default: | |
972 | goto usage; | |
973 | } | |
974 | } while (1); | |
975 | ||
84899e2d | 976 | if (!bootmsg && !term && !debugmsg) |
d131ad68 LP |
977 | goto usage; |
978 | ||
d131ad68 LP |
979 | if (argc - optind < 1) |
980 | goto usage; | |
981 | ||
982 | ttypath = argv[optind++]; | |
983 | ||
984 | tty = kwboot_open_tty(ttypath, speed); | |
985 | if (tty < 0) { | |
986 | perror(ttypath); | |
987 | goto out; | |
988 | } | |
989 | ||
990 | if (imgpath) { | |
ddc04fac | 991 | img = kwboot_mmap_image(imgpath, &size); |
d131ad68 LP |
992 | if (!img) { |
993 | perror(imgpath); | |
994 | goto out; | |
995 | } | |
d131ad68 | 996 | |
d131ad68 LP |
997 | rc = kwboot_img_patch_hdr(img, size); |
998 | if (rc) { | |
999 | fprintf(stderr, "%s: Invalid image.\n", imgpath); | |
1000 | goto out; | |
1001 | } | |
1002 | } | |
1003 | ||
84899e2d SR |
1004 | if (debugmsg) { |
1005 | rc = kwboot_debugmsg(tty, debugmsg); | |
1006 | if (rc) { | |
1007 | perror("debugmsg"); | |
1008 | goto out; | |
1009 | } | |
3475a71d | 1010 | } else if (bootmsg) { |
d131ad68 LP |
1011 | rc = kwboot_bootmsg(tty, bootmsg); |
1012 | if (rc) { | |
1013 | perror("bootmsg"); | |
1014 | goto out; | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | if (img) { | |
1019 | rc = kwboot_xmodem(tty, img, size); | |
1020 | if (rc) { | |
1021 | perror("xmodem"); | |
1022 | goto out; | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | if (term) { | |
1027 | rc = kwboot_terminal(tty); | |
1028 | if (rc && !(errno == EINTR)) { | |
1029 | perror("terminal"); | |
1030 | goto out; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | rv = 0; | |
1035 | out: | |
1036 | if (tty >= 0) | |
1037 | close(tty); | |
1038 | ||
1039 | if (img) | |
1040 | munmap(img, size); | |
1041 | ||
1042 | return rv; | |
1043 | ||
1044 | usage: | |
1045 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); | |
1046 | goto out; | |
1047 | } |