]>
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> | |
27 | #include <sys/mman.h> | |
28 | #include <sys/stat.h> | |
29 | ||
d131ad68 LP |
30 | /* |
31 | * Marvell BootROM UART Sensing | |
32 | */ | |
33 | ||
34 | static unsigned char kwboot_msg_boot[] = { | |
35 | 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 | |
36 | }; | |
37 | ||
84899e2d SR |
38 | static unsigned char kwboot_msg_debug[] = { |
39 | 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 | |
40 | }; | |
41 | ||
42 | /* Defines known to work on Kirkwood */ | |
d131ad68 LP |
43 | #define KWBOOT_MSG_REQ_DELAY 10 /* ms */ |
44 | #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */ | |
45 | ||
84899e2d SR |
46 | /* 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 | ||
d131ad68 LP |
50 | /* |
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 | ||
2ef87f75 T |
60 | #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */ |
61 | ||
d131ad68 LP |
62 | struct kwboot_block { |
63 | uint8_t soh; | |
64 | uint8_t pnum; | |
65 | uint8_t _pnum; | |
2ef87f75 | 66 | uint8_t data[KWBOOT_XM_BLKSZ]; |
d131ad68 | 67 | uint8_t csum; |
a107c61b | 68 | } __packed; |
d131ad68 LP |
69 | |
70 | #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */ | |
71 | ||
72 | static int kwboot_verbose; | |
73 | ||
84899e2d SR |
74 | static int msg_req_delay = KWBOOT_MSG_REQ_DELAY; |
75 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; | |
7497a6a1 | 76 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
84899e2d | 77 | |
e453bb42 MB |
78 | static ssize_t |
79 | kwboot_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 | ||
d131ad68 LP |
95 | static void |
96 | kwboot_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 | ||
108 | static 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 | ||
125 | static void | |
126 | kwboot_spinner(void) | |
127 | { | |
128 | if (kwboot_verbose) | |
129 | __spinner(); | |
130 | } | |
131 | ||
132 | static 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"; | |
5a1f8cbe | 145 | pos = (pos + 1) % width; |
d131ad68 LP |
146 | |
147 | if (pct == 100) { | |
5a1f8cbe | 148 | while (pos && pos++ < width) |
d131ad68 LP |
149 | fputc(' ', stdout); |
150 | fputs(nl, stdout); | |
5a1f8cbe T |
151 | nl = ""; |
152 | pos = 0; | |
d131ad68 LP |
153 | } |
154 | ||
155 | fflush(stdout); | |
156 | ||
157 | } | |
158 | ||
159 | static void | |
160 | kwboot_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); | |
5a1f8cbe T |
169 | |
170 | if (pct == 100) | |
171 | pct = 0; | |
d131ad68 LP |
172 | } |
173 | ||
174 | static int | |
175 | kwboot_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); | |
4469bd7b | 204 | if (n <= 0) |
d131ad68 LP |
205 | goto out; |
206 | ||
207 | buf = (char *)buf + n; | |
208 | len -= n; | |
209 | } while (len > 0); | |
210 | ||
211 | rc = 0; | |
212 | out: | |
213 | return rc; | |
214 | } | |
215 | ||
216 | static int | |
217 | kwboot_tty_send(int fd, const void *buf, size_t len) | |
218 | { | |
84899e2d SR |
219 | if (!buf) |
220 | return 0; | |
221 | ||
e453bb42 MB |
222 | if (kwboot_write(fd, buf, len) < 0) |
223 | return -1; | |
d131ad68 | 224 | |
e453bb42 | 225 | return tcdrain(fd); |
d131ad68 LP |
226 | } |
227 | ||
228 | static int | |
229 | kwboot_tty_send_char(int fd, unsigned char c) | |
230 | { | |
231 | return kwboot_tty_send(fd, &c, 1); | |
232 | } | |
233 | ||
234 | static speed_t | |
235 | kwboot_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 | ||
253 | static int | |
254 | kwboot_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; | |
281 | out: | |
282 | if (rc < 0) { | |
283 | if (fd >= 0) | |
284 | close(fd); | |
285 | } | |
286 | ||
287 | return rc; | |
288 | } | |
289 | ||
290 | static int | |
291 | kwboot_bootmsg(int tty, void *msg) | |
292 | { | |
293 | int rc; | |
294 | char c; | |
9ca6fae9 | 295 | int count; |
d131ad68 | 296 | |
84899e2d SR |
297 | 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..."); | |
d131ad68 LP |
301 | |
302 | do { | |
303 | rc = tcflush(tty, TCIOFLUSH); | |
304 | if (rc) | |
305 | break; | |
306 | ||
9ca6fae9 JN |
307 | 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 | } | |
d131ad68 LP |
313 | } |
314 | ||
84899e2d | 315 | rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo); |
d131ad68 LP |
316 | |
317 | kwboot_spinner(); | |
318 | ||
319 | } while (rc || c != NAK); | |
320 | ||
321 | kwboot_printv("\n"); | |
322 | ||
323 | return rc; | |
324 | } | |
325 | ||
84899e2d SR |
326 | static int |
327 | kwboot_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 | ||
c5d666aa | 357 | static size_t |
d131ad68 LP |
358 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
359 | size_t size, int pnum) | |
360 | { | |
d8cc851d | 361 | size_t i, n; |
d131ad68 | 362 | |
84899e2d | 363 | block->soh = SOH; |
d131ad68 LP |
364 | block->pnum = pnum; |
365 | block->_pnum = ~block->pnum; | |
366 | ||
2ef87f75 | 367 | n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ; |
d131ad68 | 368 | memcpy(&block->data[0], data, n); |
2ef87f75 | 369 | memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n); |
d131ad68 LP |
370 | |
371 | block->csum = 0; | |
372 | for (i = 0; i < n; i++) | |
373 | block->csum += block->data[i]; | |
374 | ||
375 | return n; | |
376 | } | |
377 | ||
408ea613 MB |
378 | static int |
379 | _is_xm_reply(char c) | |
380 | { | |
381 | return c == ACK || c == NAK || c == CAN; | |
382 | } | |
383 | ||
d131ad68 | 384 | static int |
2e81b3ab | 385 | kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print) |
48b3ea66 T |
386 | { |
387 | int rc; | |
388 | ||
2e81b3ab MB |
389 | *non_xm_print = 0; |
390 | ||
48b3ea66 T |
391 | while (1) { |
392 | rc = kwboot_tty_recv(fd, c, 1, blk_rsp_timeo); | |
393 | if (rc) { | |
394 | if (errno != ETIMEDOUT) | |
395 | return rc; | |
396 | *c = NAK; | |
397 | } | |
398 | ||
399 | /* If received xmodem reply, end. */ | |
400 | if (_is_xm_reply(*c)) | |
401 | break; | |
402 | ||
403 | /* | |
404 | * If printing non-xmodem text output is allowed and such a byte | |
405 | * was received, print it. | |
406 | */ | |
407 | if (allow_non_xm) { | |
408 | putchar(*c); | |
409 | fflush(stdout); | |
2e81b3ab | 410 | *non_xm_print = 1; |
48b3ea66 T |
411 | } |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
417 | static int | |
418 | kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, | |
419 | int *done_print) | |
d131ad68 | 420 | { |
2e81b3ab | 421 | int non_xm_print; |
d131ad68 LP |
422 | int rc, retries; |
423 | char c; | |
424 | ||
48b3ea66 T |
425 | *done_print = 0; |
426 | ||
d131ad68 LP |
427 | retries = 16; |
428 | do { | |
429 | rc = kwboot_tty_send(fd, block, sizeof(*block)); | |
430 | if (rc) | |
00a1deed | 431 | return rc; |
d131ad68 | 432 | |
48b3ea66 T |
433 | if (allow_non_xm && !*done_print) { |
434 | kwboot_progress(100, '.'); | |
435 | kwboot_printv("Done\n"); | |
436 | *done_print = 1; | |
437 | } | |
84899e2d | 438 | |
2e81b3ab | 439 | rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print); |
48b3ea66 T |
440 | if (rc) |
441 | return rc; | |
d131ad68 | 442 | |
48b3ea66 | 443 | if (!allow_non_xm && c != ACK) |
d131ad68 | 444 | kwboot_progress(-1, '+'); |
d131ad68 LP |
445 | } while (c == NAK && retries-- > 0); |
446 | ||
2e81b3ab MB |
447 | if (non_xm_print) |
448 | kwboot_printv("\n"); | |
449 | ||
d131ad68 LP |
450 | rc = -1; |
451 | ||
452 | switch (c) { | |
453 | case ACK: | |
454 | rc = 0; | |
455 | break; | |
456 | case NAK: | |
457 | errno = EBADMSG; | |
458 | break; | |
459 | case CAN: | |
460 | errno = ECANCELED; | |
461 | break; | |
462 | default: | |
463 | errno = EPROTO; | |
464 | break; | |
465 | } | |
466 | ||
467 | return rc; | |
468 | } | |
469 | ||
470 | static int | |
2ef87f75 T |
471 | kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data, |
472 | size_t size) | |
d131ad68 | 473 | { |
48b3ea66 | 474 | int done_print = 0; |
2ef87f75 T |
475 | size_t sent, left; |
476 | int rc; | |
d131ad68 | 477 | |
2ef87f75 T |
478 | kwboot_printv("Sending boot image %s (%zu bytes)...\n", |
479 | header ? "header" : "data", size); | |
d131ad68 | 480 | |
2ef87f75 T |
481 | left = size; |
482 | sent = 0; | |
d131ad68 | 483 | |
2ef87f75 | 484 | while (sent < size) { |
d131ad68 | 485 | struct kwboot_block block; |
48b3ea66 | 486 | int last_block; |
2ef87f75 | 487 | size_t blksz; |
d131ad68 | 488 | |
2ef87f75 T |
489 | blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++); |
490 | data += blksz; | |
d131ad68 | 491 | |
48b3ea66 T |
492 | last_block = (left <= blksz); |
493 | ||
494 | rc = kwboot_xm_sendblock(tty, &block, header && last_block, | |
495 | &done_print); | |
d131ad68 LP |
496 | if (rc) |
497 | goto out; | |
498 | ||
2ef87f75 T |
499 | sent += blksz; |
500 | left -= blksz; | |
501 | ||
48b3ea66 T |
502 | if (!done_print) |
503 | kwboot_progress(sent * 100 / size, '.'); | |
2ef87f75 | 504 | } |
d131ad68 | 505 | |
48b3ea66 T |
506 | if (!done_print) |
507 | kwboot_printv("Done\n"); | |
d131ad68 | 508 | |
2ef87f75 | 509 | return 0; |
d131ad68 | 510 | out: |
d5ba8dbd | 511 | kwboot_printv("\n"); |
d131ad68 | 512 | return rc; |
2ef87f75 T |
513 | } |
514 | ||
515 | static int | |
516 | kwboot_xmodem(int tty, const void *_img, size_t size) | |
517 | { | |
518 | const uint8_t *img = _img; | |
519 | int rc, pnum; | |
520 | size_t hdrsz; | |
521 | ||
522 | if (image_version(img) == 0) | |
523 | hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img); | |
524 | else | |
525 | hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img); | |
526 | ||
527 | kwboot_printv("Waiting 2s and flushing tty\n"); | |
528 | sleep(2); /* flush isn't effective without it */ | |
529 | tcflush(tty, TCIOFLUSH); | |
530 | ||
531 | pnum = 1; | |
532 | ||
533 | rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz); | |
534 | if (rc) | |
535 | return rc; | |
536 | ||
537 | img += hdrsz; | |
538 | size -= hdrsz; | |
539 | ||
540 | rc = kwboot_xmodem_one(tty, &pnum, 0, img, size); | |
541 | if (rc) | |
542 | return rc; | |
d131ad68 | 543 | |
2ef87f75 | 544 | return kwboot_tty_send_char(tty, EOT); |
d131ad68 LP |
545 | } |
546 | ||
547 | static int | |
46237e63 | 548 | kwboot_term_pipe(int in, int out, const char *quit, int *s) |
d131ad68 | 549 | { |
e453bb42 | 550 | ssize_t nin; |
d131ad68 LP |
551 | char _buf[128], *buf = _buf; |
552 | ||
43fef8d4 | 553 | nin = read(in, buf, sizeof(_buf)); |
4469bd7b | 554 | if (nin <= 0) |
d131ad68 LP |
555 | return -1; |
556 | ||
557 | if (quit) { | |
558 | int i; | |
559 | ||
560 | for (i = 0; i < nin; i++) { | |
561 | if (*buf == quit[*s]) { | |
562 | (*s)++; | |
563 | if (!quit[*s]) | |
564 | return 0; | |
565 | buf++; | |
566 | nin--; | |
b943eee9 | 567 | } else { |
e453bb42 MB |
568 | if (kwboot_write(out, quit, *s) < 0) |
569 | return -1; | |
570 | *s = 0; | |
b943eee9 | 571 | } |
d131ad68 LP |
572 | } |
573 | } | |
574 | ||
e453bb42 MB |
575 | if (kwboot_write(out, buf, nin) < 0) |
576 | return -1; | |
d131ad68 LP |
577 | |
578 | return 0; | |
579 | } | |
580 | ||
581 | static int | |
582 | kwboot_terminal(int tty) | |
583 | { | |
584 | int rc, in, s; | |
46237e63 | 585 | const char *quit = "\34c"; |
d131ad68 LP |
586 | struct termios otio, tio; |
587 | ||
588 | rc = -1; | |
589 | ||
590 | in = STDIN_FILENO; | |
591 | if (isatty(in)) { | |
592 | rc = tcgetattr(in, &otio); | |
593 | if (!rc) { | |
594 | tio = otio; | |
595 | cfmakeraw(&tio); | |
596 | rc = tcsetattr(in, TCSANOW, &tio); | |
597 | } | |
598 | if (rc) { | |
599 | perror("tcsetattr"); | |
600 | goto out; | |
601 | } | |
602 | ||
603 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", | |
604 | quit[0]|0100, quit[1]); | |
605 | } else | |
606 | in = -1; | |
607 | ||
608 | rc = 0; | |
609 | s = 0; | |
610 | ||
611 | do { | |
612 | fd_set rfds; | |
613 | int nfds = 0; | |
614 | ||
615 | FD_SET(tty, &rfds); | |
616 | nfds = nfds < tty ? tty : nfds; | |
617 | ||
618 | if (in >= 0) { | |
619 | FD_SET(in, &rfds); | |
620 | nfds = nfds < in ? in : nfds; | |
621 | } | |
622 | ||
623 | nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); | |
624 | if (nfds < 0) | |
625 | break; | |
626 | ||
627 | if (FD_ISSET(tty, &rfds)) { | |
628 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); | |
629 | if (rc) | |
630 | break; | |
631 | } | |
632 | ||
f30cb0d3 | 633 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
d131ad68 LP |
634 | rc = kwboot_term_pipe(in, tty, quit, &s); |
635 | if (rc) | |
636 | break; | |
637 | } | |
638 | } while (quit[s] != 0); | |
639 | ||
ec0fe5b8 T |
640 | if (in >= 0) |
641 | tcsetattr(in, TCSANOW, &otio); | |
49a0a3b8 | 642 | printf("\n"); |
d131ad68 LP |
643 | out: |
644 | return rc; | |
645 | } | |
646 | ||
647 | static void * | |
648 | kwboot_mmap_image(const char *path, size_t *size, int prot) | |
649 | { | |
650 | int rc, fd, flags; | |
651 | struct stat st; | |
652 | void *img; | |
653 | ||
654 | rc = -1; | |
d131ad68 LP |
655 | img = NULL; |
656 | ||
657 | fd = open(path, O_RDONLY); | |
658 | if (fd < 0) | |
659 | goto out; | |
660 | ||
661 | rc = fstat(fd, &st); | |
662 | if (rc) | |
663 | goto out; | |
664 | ||
665 | flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED; | |
666 | ||
667 | img = mmap(NULL, st.st_size, prot, flags, fd, 0); | |
668 | if (img == MAP_FAILED) { | |
669 | img = NULL; | |
670 | goto out; | |
671 | } | |
672 | ||
673 | rc = 0; | |
674 | *size = st.st_size; | |
675 | out: | |
676 | if (rc && img) { | |
677 | munmap(img, st.st_size); | |
678 | img = NULL; | |
679 | } | |
680 | if (fd >= 0) | |
681 | close(fd); | |
682 | ||
683 | return img; | |
684 | } | |
685 | ||
686 | static uint8_t | |
687 | kwboot_img_csum8(void *_data, size_t size) | |
688 | { | |
689 | uint8_t *data = _data, csum; | |
690 | ||
691 | for (csum = 0; size-- > 0; data++) | |
692 | csum += *data; | |
693 | ||
694 | return csum; | |
695 | } | |
696 | ||
697 | static int | |
698 | kwboot_img_patch_hdr(void *img, size_t size) | |
699 | { | |
700 | int rc; | |
e29f1db3 | 701 | struct main_hdr_v1 *hdr; |
d131ad68 | 702 | uint8_t csum; |
e29f1db3 SR |
703 | size_t hdrsz = sizeof(*hdr); |
704 | int image_ver; | |
d131ad68 LP |
705 | |
706 | rc = -1; | |
707 | hdr = img; | |
708 | ||
709 | if (size < hdrsz) { | |
710 | errno = EINVAL; | |
711 | goto out; | |
712 | } | |
713 | ||
e29f1db3 | 714 | image_ver = image_version(img); |
5029d7bf | 715 | if (image_ver != 0 && image_ver != 1) { |
e29f1db3 SR |
716 | fprintf(stderr, "Invalid image header version\n"); |
717 | errno = EINVAL; | |
718 | goto out; | |
719 | } | |
720 | ||
721 | if (image_ver == 0) | |
722 | hdrsz = sizeof(*hdr); | |
723 | else | |
724 | hdrsz = KWBHEADER_V1_SIZE(hdr); | |
725 | ||
825a2ca0 T |
726 | if (size < hdrsz) { |
727 | errno = EINVAL; | |
728 | goto out; | |
729 | } | |
730 | ||
e29f1db3 SR |
731 | csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum; |
732 | if (csum != hdr->checksum) { | |
d131ad68 LP |
733 | errno = EINVAL; |
734 | goto out; | |
735 | } | |
736 | ||
737 | if (hdr->blockid == IBR_HDR_UART_ID) { | |
738 | rc = 0; | |
739 | goto out; | |
740 | } | |
741 | ||
742 | hdr->blockid = IBR_HDR_UART_ID; | |
743 | ||
e29f1db3 SR |
744 | if (image_ver == 0) { |
745 | struct main_hdr_v0 *hdr_v0 = img; | |
d131ad68 | 746 | |
e29f1db3 SR |
747 | hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED; |
748 | hdr_v0->nandpagesize = 0; | |
749 | ||
750 | hdr_v0->srcaddr = hdr_v0->ext | |
751 | ? sizeof(struct kwb_header) | |
752 | : sizeof(*hdr_v0); | |
753 | } | |
d131ad68 | 754 | |
e29f1db3 | 755 | hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum; |
d131ad68 LP |
756 | |
757 | rc = 0; | |
758 | out: | |
759 | return rc; | |
760 | } | |
761 | ||
762 | static void | |
763 | kwboot_usage(FILE *stream, char *progname) | |
764 | { | |
a050a862 | 765 | fprintf(stream, "kwboot version %s\n", PLAIN_VERSION); |
d131ad68 | 766 | fprintf(stream, |
8669dacf | 767 | "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n", |
84899e2d | 768 | progname); |
d131ad68 | 769 | fprintf(stream, "\n"); |
84899e2d SR |
770 | fprintf(stream, |
771 | " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); | |
d131ad68 | 772 | fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n"); |
84899e2d SR |
773 | fprintf(stream, |
774 | " -D <image>: boot <image> without preamble (Dove)\n"); | |
775 | fprintf(stream, " -d: enter debug mode\n"); | |
776 | fprintf(stream, " -a: use timings for Armada XP\n"); | |
1c0df9ef SR |
777 | fprintf(stream, " -q <req-delay>: use specific request-delay\n"); |
778 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); | |
7497a6a1 KS |
779 | fprintf(stream, |
780 | " -o <block-timeo>: use specific xmodem block timeout\n"); | |
d131ad68 LP |
781 | fprintf(stream, "\n"); |
782 | fprintf(stream, " -t: mini terminal\n"); | |
783 | fprintf(stream, "\n"); | |
784 | fprintf(stream, " -B <baud>: set baud rate\n"); | |
785 | fprintf(stream, "\n"); | |
786 | } | |
787 | ||
788 | int | |
789 | main(int argc, char **argv) | |
790 | { | |
791 | const char *ttypath, *imgpath; | |
792 | int rv, rc, tty, term, prot, patch; | |
793 | void *bootmsg; | |
84899e2d | 794 | void *debugmsg; |
d131ad68 LP |
795 | void *img; |
796 | size_t size; | |
797 | speed_t speed; | |
798 | ||
799 | rv = 1; | |
800 | tty = -1; | |
801 | bootmsg = NULL; | |
84899e2d | 802 | debugmsg = NULL; |
d131ad68 LP |
803 | imgpath = NULL; |
804 | img = NULL; | |
805 | term = 0; | |
806 | patch = 0; | |
807 | size = 0; | |
808 | speed = B115200; | |
809 | ||
810 | kwboot_verbose = isatty(STDOUT_FILENO); | |
811 | ||
812 | do { | |
7497a6a1 | 813 | int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:"); |
d131ad68 LP |
814 | if (c < 0) |
815 | break; | |
816 | ||
817 | switch (c) { | |
818 | case 'b': | |
819 | bootmsg = kwboot_msg_boot; | |
820 | imgpath = optarg; | |
821 | break; | |
822 | ||
84899e2d SR |
823 | case 'D': |
824 | bootmsg = NULL; | |
825 | imgpath = optarg; | |
826 | break; | |
827 | ||
828 | case 'd': | |
829 | debugmsg = kwboot_msg_debug; | |
830 | break; | |
831 | ||
d131ad68 LP |
832 | case 'p': |
833 | patch = 1; | |
834 | break; | |
835 | ||
836 | case 't': | |
837 | term = 1; | |
838 | break; | |
839 | ||
84899e2d SR |
840 | case 'a': |
841 | msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP; | |
842 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; | |
843 | break; | |
844 | ||
1c0df9ef SR |
845 | case 'q': |
846 | msg_req_delay = atoi(optarg); | |
847 | break; | |
848 | ||
849 | case 's': | |
850 | msg_rsp_timeo = atoi(optarg); | |
851 | break; | |
852 | ||
7497a6a1 KS |
853 | case 'o': |
854 | blk_rsp_timeo = atoi(optarg); | |
855 | break; | |
856 | ||
d131ad68 LP |
857 | case 'B': |
858 | speed = kwboot_tty_speed(atoi(optarg)); | |
859 | if (speed == -1) | |
860 | goto usage; | |
861 | break; | |
862 | ||
863 | case 'h': | |
864 | rv = 0; | |
865 | default: | |
866 | goto usage; | |
867 | } | |
868 | } while (1); | |
869 | ||
84899e2d | 870 | if (!bootmsg && !term && !debugmsg) |
d131ad68 LP |
871 | goto usage; |
872 | ||
873 | if (patch && !imgpath) | |
874 | goto usage; | |
875 | ||
876 | if (argc - optind < 1) | |
877 | goto usage; | |
878 | ||
879 | ttypath = argv[optind++]; | |
880 | ||
881 | tty = kwboot_open_tty(ttypath, speed); | |
882 | if (tty < 0) { | |
883 | perror(ttypath); | |
884 | goto out; | |
885 | } | |
886 | ||
887 | if (imgpath) { | |
888 | prot = PROT_READ | (patch ? PROT_WRITE : 0); | |
889 | ||
890 | img = kwboot_mmap_image(imgpath, &size, prot); | |
891 | if (!img) { | |
892 | perror(imgpath); | |
893 | goto out; | |
894 | } | |
895 | } | |
896 | ||
897 | if (patch) { | |
898 | rc = kwboot_img_patch_hdr(img, size); | |
899 | if (rc) { | |
900 | fprintf(stderr, "%s: Invalid image.\n", imgpath); | |
901 | goto out; | |
902 | } | |
903 | } | |
904 | ||
84899e2d SR |
905 | if (debugmsg) { |
906 | rc = kwboot_debugmsg(tty, debugmsg); | |
907 | if (rc) { | |
908 | perror("debugmsg"); | |
909 | goto out; | |
910 | } | |
3475a71d | 911 | } else if (bootmsg) { |
d131ad68 LP |
912 | rc = kwboot_bootmsg(tty, bootmsg); |
913 | if (rc) { | |
914 | perror("bootmsg"); | |
915 | goto out; | |
916 | } | |
917 | } | |
918 | ||
919 | if (img) { | |
920 | rc = kwboot_xmodem(tty, img, size); | |
921 | if (rc) { | |
922 | perror("xmodem"); | |
923 | goto out; | |
924 | } | |
925 | } | |
926 | ||
927 | if (term) { | |
928 | rc = kwboot_terminal(tty); | |
929 | if (rc && !(errno == EINTR)) { | |
930 | perror("terminal"); | |
931 | goto out; | |
932 | } | |
933 | } | |
934 | ||
935 | rv = 0; | |
936 | out: | |
937 | if (tty >= 0) | |
938 | close(tty); | |
939 | ||
940 | if (img) | |
941 | munmap(img, size); | |
942 | ||
943 | return rv; | |
944 | ||
945 | usage: | |
946 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); | |
947 | goto out; | |
948 | } |