Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2 | * Boot a Marvell SoC, with Xmodem over UART0. |
| 3 | * supports Kirkwood, Dove, Armada 370, Armada XP |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 4 | * |
| 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 Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 12 | #include "kwbimage.h" |
| 13 | #include "mkimage.h" |
Pali Rohár | 3c703aaf | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 14 | #include "version.h" |
Stefan Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 15 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <stdarg.h> |
Stefan Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 20 | #include <image.h> |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 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 */ |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 43 | #define KWBOOT_MSG_REQ_DELAY 10 /* ms */ |
| 44 | #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */ |
| 45 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 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 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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 | |
| 60 | struct kwboot_block { |
| 61 | uint8_t soh; |
| 62 | uint8_t pnum; |
| 63 | uint8_t _pnum; |
| 64 | uint8_t data[128]; |
| 65 | uint8_t csum; |
Pali Rohár | f01adfd | 2021-07-23 11:14:14 +0200 | [diff] [blame] | 66 | } __packed; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 67 | |
| 68 | #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */ |
| 69 | |
| 70 | static int kwboot_verbose; |
| 71 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 72 | static int msg_req_delay = KWBOOT_MSG_REQ_DELAY; |
| 73 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 74 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 75 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 76 | static ssize_t |
| 77 | kwboot_write(int fd, const char *buf, size_t len) |
| 78 | { |
| 79 | size_t tot = 0; |
| 80 | |
| 81 | while (tot < len) { |
| 82 | ssize_t wr = write(fd, buf + tot, len - tot); |
| 83 | |
| 84 | if (wr < 0) |
| 85 | return -1; |
| 86 | |
| 87 | tot += wr; |
| 88 | } |
| 89 | |
| 90 | return tot; |
| 91 | } |
| 92 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 93 | static void |
| 94 | kwboot_printv(const char *fmt, ...) |
| 95 | { |
| 96 | va_list ap; |
| 97 | |
| 98 | if (kwboot_verbose) { |
| 99 | va_start(ap, fmt); |
| 100 | vprintf(fmt, ap); |
| 101 | va_end(ap); |
| 102 | fflush(stdout); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | __spinner(void) |
| 108 | { |
| 109 | const char seq[] = { '-', '\\', '|', '/' }; |
| 110 | const int div = 8; |
| 111 | static int state, bs; |
| 112 | |
| 113 | if (state % div == 0) { |
| 114 | fputc(bs, stdout); |
| 115 | fputc(seq[state / div % sizeof(seq)], stdout); |
| 116 | fflush(stdout); |
| 117 | } |
| 118 | |
| 119 | bs = '\b'; |
| 120 | state++; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | kwboot_spinner(void) |
| 125 | { |
| 126 | if (kwboot_verbose) |
| 127 | __spinner(); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | __progress(int pct, char c) |
| 132 | { |
| 133 | const int width = 70; |
| 134 | static const char *nl = ""; |
| 135 | static int pos; |
| 136 | |
| 137 | if (pos % width == 0) |
| 138 | printf("%s%3d %% [", nl, pct); |
| 139 | |
| 140 | fputc(c, stdout); |
| 141 | |
| 142 | nl = "]\n"; |
| 143 | pos++; |
| 144 | |
| 145 | if (pct == 100) { |
| 146 | while (pos++ < width) |
| 147 | fputc(' ', stdout); |
| 148 | fputs(nl, stdout); |
| 149 | } |
| 150 | |
| 151 | fflush(stdout); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | static void |
| 156 | kwboot_progress(int _pct, char c) |
| 157 | { |
| 158 | static int pct; |
| 159 | |
| 160 | if (_pct != -1) |
| 161 | pct = _pct; |
| 162 | |
| 163 | if (kwboot_verbose) |
| 164 | __progress(pct, c); |
| 165 | } |
| 166 | |
| 167 | static int |
| 168 | kwboot_tty_recv(int fd, void *buf, size_t len, int timeo) |
| 169 | { |
| 170 | int rc, nfds; |
| 171 | fd_set rfds; |
| 172 | struct timeval tv; |
| 173 | ssize_t n; |
| 174 | |
| 175 | rc = -1; |
| 176 | |
| 177 | FD_ZERO(&rfds); |
| 178 | FD_SET(fd, &rfds); |
| 179 | |
| 180 | tv.tv_sec = 0; |
| 181 | tv.tv_usec = timeo * 1000; |
| 182 | if (tv.tv_usec > 1000000) { |
| 183 | tv.tv_sec += tv.tv_usec / 1000000; |
| 184 | tv.tv_usec %= 1000000; |
| 185 | } |
| 186 | |
| 187 | do { |
| 188 | nfds = select(fd + 1, &rfds, NULL, NULL, &tv); |
| 189 | if (nfds < 0) |
| 190 | goto out; |
| 191 | if (!nfds) { |
| 192 | errno = ETIMEDOUT; |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | n = read(fd, buf, len); |
Willy Tarreau | ab7e8a1 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 197 | if (n <= 0) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 198 | goto out; |
| 199 | |
| 200 | buf = (char *)buf + n; |
| 201 | len -= n; |
| 202 | } while (len > 0); |
| 203 | |
| 204 | rc = 0; |
| 205 | out: |
| 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | static int |
| 210 | kwboot_tty_send(int fd, const void *buf, size_t len) |
| 211 | { |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 212 | if (!buf) |
| 213 | return 0; |
| 214 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 215 | if (kwboot_write(fd, buf, len) < 0) |
| 216 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 217 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 218 | return tcdrain(fd); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int |
| 222 | kwboot_tty_send_char(int fd, unsigned char c) |
| 223 | { |
| 224 | return kwboot_tty_send(fd, &c, 1); |
| 225 | } |
| 226 | |
| 227 | static speed_t |
| 228 | kwboot_tty_speed(int baudrate) |
| 229 | { |
| 230 | switch (baudrate) { |
| 231 | case 115200: |
| 232 | return B115200; |
| 233 | case 57600: |
| 234 | return B57600; |
| 235 | case 38400: |
| 236 | return B38400; |
| 237 | case 19200: |
| 238 | return B19200; |
| 239 | case 9600: |
| 240 | return B9600; |
| 241 | } |
| 242 | |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | static int |
| 247 | kwboot_open_tty(const char *path, speed_t speed) |
| 248 | { |
| 249 | int rc, fd; |
| 250 | struct termios tio; |
| 251 | |
| 252 | rc = -1; |
| 253 | |
| 254 | fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY); |
| 255 | if (fd < 0) |
| 256 | goto out; |
| 257 | |
| 258 | memset(&tio, 0, sizeof(tio)); |
| 259 | |
| 260 | tio.c_iflag = 0; |
| 261 | tio.c_cflag = CREAD|CLOCAL|CS8; |
| 262 | |
| 263 | tio.c_cc[VMIN] = 1; |
| 264 | tio.c_cc[VTIME] = 10; |
| 265 | |
| 266 | cfsetospeed(&tio, speed); |
| 267 | cfsetispeed(&tio, speed); |
| 268 | |
| 269 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 270 | if (rc) |
| 271 | goto out; |
| 272 | |
| 273 | rc = fd; |
| 274 | out: |
| 275 | if (rc < 0) { |
| 276 | if (fd >= 0) |
| 277 | close(fd); |
| 278 | } |
| 279 | |
| 280 | return rc; |
| 281 | } |
| 282 | |
| 283 | static int |
| 284 | kwboot_bootmsg(int tty, void *msg) |
| 285 | { |
| 286 | int rc; |
| 287 | char c; |
Jon Nettleton | 0e428ee | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 288 | int count; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 289 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 290 | if (msg == NULL) |
| 291 | kwboot_printv("Please reboot the target into UART boot mode..."); |
| 292 | else |
| 293 | kwboot_printv("Sending boot message. Please reboot the target..."); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 294 | |
| 295 | do { |
| 296 | rc = tcflush(tty, TCIOFLUSH); |
| 297 | if (rc) |
| 298 | break; |
| 299 | |
Jon Nettleton | 0e428ee | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 300 | for (count = 0; count < 128; count++) { |
| 301 | rc = kwboot_tty_send(tty, msg, 8); |
| 302 | if (rc) { |
| 303 | usleep(msg_req_delay * 1000); |
| 304 | continue; |
| 305 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 308 | rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 309 | |
| 310 | kwboot_spinner(); |
| 311 | |
| 312 | } while (rc || c != NAK); |
| 313 | |
| 314 | kwboot_printv("\n"); |
| 315 | |
| 316 | return rc; |
| 317 | } |
| 318 | |
| 319 | static int |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 320 | kwboot_debugmsg(int tty, void *msg) |
| 321 | { |
| 322 | int rc; |
| 323 | |
| 324 | kwboot_printv("Sending debug message. Please reboot the target..."); |
| 325 | |
| 326 | do { |
| 327 | char buf[16]; |
| 328 | |
| 329 | rc = tcflush(tty, TCIOFLUSH); |
| 330 | if (rc) |
| 331 | break; |
| 332 | |
| 333 | rc = kwboot_tty_send(tty, msg, 8); |
| 334 | if (rc) { |
| 335 | usleep(msg_req_delay * 1000); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo); |
| 340 | |
| 341 | kwboot_spinner(); |
| 342 | |
| 343 | } while (rc); |
| 344 | |
| 345 | kwboot_printv("\n"); |
| 346 | |
| 347 | return rc; |
| 348 | } |
| 349 | |
Pali Rohár | 58cf04de | 2021-09-24 23:06:44 +0200 | [diff] [blame^] | 350 | static size_t |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 351 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
| 352 | size_t size, int pnum) |
| 353 | { |
| 354 | const size_t blksz = sizeof(block->data); |
| 355 | size_t n; |
| 356 | int i; |
| 357 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 358 | block->soh = SOH; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 359 | block->pnum = pnum; |
| 360 | block->_pnum = ~block->pnum; |
| 361 | |
| 362 | n = size < blksz ? size : blksz; |
| 363 | memcpy(&block->data[0], data, n); |
| 364 | memset(&block->data[n], 0, blksz - n); |
| 365 | |
| 366 | block->csum = 0; |
| 367 | for (i = 0; i < n; i++) |
| 368 | block->csum += block->data[i]; |
| 369 | |
| 370 | return n; |
| 371 | } |
| 372 | |
| 373 | static int |
| 374 | kwboot_xm_sendblock(int fd, struct kwboot_block *block) |
| 375 | { |
| 376 | int rc, retries; |
| 377 | char c; |
| 378 | |
| 379 | retries = 16; |
| 380 | do { |
| 381 | rc = kwboot_tty_send(fd, block, sizeof(*block)); |
| 382 | if (rc) |
Pali Rohár | 2c02a34 | 2021-09-24 23:06:43 +0200 | [diff] [blame] | 383 | return rc; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 384 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 385 | do { |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 386 | rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo); |
Pali Rohár | 2c02a34 | 2021-09-24 23:06:43 +0200 | [diff] [blame] | 387 | if (rc) { |
| 388 | if (errno != ETIMEDOUT) |
| 389 | return rc; |
| 390 | c = NAK; |
| 391 | } |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 392 | |
| 393 | if (c != ACK && c != NAK && c != CAN) |
| 394 | printf("%c", c); |
| 395 | |
| 396 | } while (c != ACK && c != NAK && c != CAN); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 397 | |
| 398 | if (c != ACK) |
| 399 | kwboot_progress(-1, '+'); |
| 400 | |
| 401 | } while (c == NAK && retries-- > 0); |
| 402 | |
| 403 | rc = -1; |
| 404 | |
| 405 | switch (c) { |
| 406 | case ACK: |
| 407 | rc = 0; |
| 408 | break; |
| 409 | case NAK: |
| 410 | errno = EBADMSG; |
| 411 | break; |
| 412 | case CAN: |
| 413 | errno = ECANCELED; |
| 414 | break; |
| 415 | default: |
| 416 | errno = EPROTO; |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | static int |
| 424 | kwboot_xmodem(int tty, const void *_data, size_t size) |
| 425 | { |
| 426 | const uint8_t *data = _data; |
| 427 | int rc, pnum, N, err; |
| 428 | |
| 429 | pnum = 1; |
| 430 | N = 0; |
| 431 | |
| 432 | kwboot_printv("Sending boot image...\n"); |
| 433 | |
Jon Nettleton | 0e428ee | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 434 | sleep(2); /* flush isn't effective without it */ |
| 435 | tcflush(tty, TCIOFLUSH); |
| 436 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 437 | do { |
| 438 | struct kwboot_block block; |
| 439 | int n; |
| 440 | |
| 441 | n = kwboot_xm_makeblock(&block, |
| 442 | data + N, size - N, |
| 443 | pnum++); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 444 | if (!n) |
| 445 | break; |
| 446 | |
| 447 | rc = kwboot_xm_sendblock(tty, &block); |
| 448 | if (rc) |
| 449 | goto out; |
| 450 | |
| 451 | N += n; |
| 452 | kwboot_progress(N * 100 / size, '.'); |
| 453 | } while (1); |
| 454 | |
| 455 | rc = kwboot_tty_send_char(tty, EOT); |
| 456 | |
| 457 | out: |
| 458 | return rc; |
| 459 | |
| 460 | can: |
| 461 | err = errno; |
| 462 | kwboot_tty_send_char(tty, CAN); |
| 463 | errno = err; |
| 464 | goto out; |
| 465 | } |
| 466 | |
| 467 | static int |
Marek Behún | ea5b2b3 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 468 | kwboot_term_pipe(int in, int out, const char *quit, int *s) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 469 | { |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 470 | ssize_t nin; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 471 | char _buf[128], *buf = _buf; |
| 472 | |
Pali Rohár | 8210ea0 | 2021-07-23 11:14:17 +0200 | [diff] [blame] | 473 | nin = read(in, buf, sizeof(_buf)); |
Willy Tarreau | ab7e8a1 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 474 | if (nin <= 0) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 475 | return -1; |
| 476 | |
| 477 | if (quit) { |
| 478 | int i; |
| 479 | |
| 480 | for (i = 0; i < nin; i++) { |
| 481 | if (*buf == quit[*s]) { |
| 482 | (*s)++; |
| 483 | if (!quit[*s]) |
| 484 | return 0; |
| 485 | buf++; |
| 486 | nin--; |
Pali Rohár | 48615ba | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 487 | } else { |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 488 | if (kwboot_write(out, quit, *s) < 0) |
| 489 | return -1; |
| 490 | *s = 0; |
Pali Rohár | 48615ba | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 491 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 495 | if (kwboot_write(out, buf, nin) < 0) |
| 496 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int |
| 502 | kwboot_terminal(int tty) |
| 503 | { |
| 504 | int rc, in, s; |
Marek Behún | ea5b2b3 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 505 | const char *quit = "\34c"; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 506 | struct termios otio, tio; |
| 507 | |
| 508 | rc = -1; |
| 509 | |
| 510 | in = STDIN_FILENO; |
| 511 | if (isatty(in)) { |
| 512 | rc = tcgetattr(in, &otio); |
| 513 | if (!rc) { |
| 514 | tio = otio; |
| 515 | cfmakeraw(&tio); |
| 516 | rc = tcsetattr(in, TCSANOW, &tio); |
| 517 | } |
| 518 | if (rc) { |
| 519 | perror("tcsetattr"); |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", |
| 524 | quit[0]|0100, quit[1]); |
| 525 | } else |
| 526 | in = -1; |
| 527 | |
| 528 | rc = 0; |
| 529 | s = 0; |
| 530 | |
| 531 | do { |
| 532 | fd_set rfds; |
| 533 | int nfds = 0; |
| 534 | |
| 535 | FD_SET(tty, &rfds); |
| 536 | nfds = nfds < tty ? tty : nfds; |
| 537 | |
| 538 | if (in >= 0) { |
| 539 | FD_SET(in, &rfds); |
| 540 | nfds = nfds < in ? in : nfds; |
| 541 | } |
| 542 | |
| 543 | nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); |
| 544 | if (nfds < 0) |
| 545 | break; |
| 546 | |
| 547 | if (FD_ISSET(tty, &rfds)) { |
| 548 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); |
| 549 | if (rc) |
| 550 | break; |
| 551 | } |
| 552 | |
Marek Behún | 4eb55de | 2021-09-24 23:06:39 +0200 | [diff] [blame] | 553 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 554 | rc = kwboot_term_pipe(in, tty, quit, &s); |
| 555 | if (rc) |
| 556 | break; |
| 557 | } |
| 558 | } while (quit[s] != 0); |
| 559 | |
Pali Rohár | eafc969 | 2021-07-23 11:14:18 +0200 | [diff] [blame] | 560 | if (in >= 0) |
| 561 | tcsetattr(in, TCSANOW, &otio); |
Pali Rohár | 049cf91 | 2021-07-23 11:14:19 +0200 | [diff] [blame] | 562 | printf("\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 563 | out: |
| 564 | return rc; |
| 565 | } |
| 566 | |
| 567 | static void * |
| 568 | kwboot_mmap_image(const char *path, size_t *size, int prot) |
| 569 | { |
| 570 | int rc, fd, flags; |
| 571 | struct stat st; |
| 572 | void *img; |
| 573 | |
| 574 | rc = -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 575 | img = NULL; |
| 576 | |
| 577 | fd = open(path, O_RDONLY); |
| 578 | if (fd < 0) |
| 579 | goto out; |
| 580 | |
| 581 | rc = fstat(fd, &st); |
| 582 | if (rc) |
| 583 | goto out; |
| 584 | |
| 585 | flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED; |
| 586 | |
| 587 | img = mmap(NULL, st.st_size, prot, flags, fd, 0); |
| 588 | if (img == MAP_FAILED) { |
| 589 | img = NULL; |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | rc = 0; |
| 594 | *size = st.st_size; |
| 595 | out: |
| 596 | if (rc && img) { |
| 597 | munmap(img, st.st_size); |
| 598 | img = NULL; |
| 599 | } |
| 600 | if (fd >= 0) |
| 601 | close(fd); |
| 602 | |
| 603 | return img; |
| 604 | } |
| 605 | |
| 606 | static uint8_t |
| 607 | kwboot_img_csum8(void *_data, size_t size) |
| 608 | { |
| 609 | uint8_t *data = _data, csum; |
| 610 | |
| 611 | for (csum = 0; size-- > 0; data++) |
| 612 | csum += *data; |
| 613 | |
| 614 | return csum; |
| 615 | } |
| 616 | |
| 617 | static int |
| 618 | kwboot_img_patch_hdr(void *img, size_t size) |
| 619 | { |
| 620 | int rc; |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 621 | struct main_hdr_v1 *hdr; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 622 | uint8_t csum; |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 623 | size_t hdrsz = sizeof(*hdr); |
| 624 | int image_ver; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 625 | |
| 626 | rc = -1; |
| 627 | hdr = img; |
| 628 | |
| 629 | if (size < hdrsz) { |
| 630 | errno = EINVAL; |
| 631 | goto out; |
| 632 | } |
| 633 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 634 | image_ver = image_version(img); |
Pali Rohár | b572ac4 | 2021-07-23 11:14:22 +0200 | [diff] [blame] | 635 | if (image_ver != 0 && image_ver != 1) { |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 636 | fprintf(stderr, "Invalid image header version\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 637 | errno = EINVAL; |
| 638 | goto out; |
| 639 | } |
| 640 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 641 | if (image_ver == 0) |
| 642 | hdrsz = sizeof(*hdr); |
| 643 | else |
| 644 | hdrsz = KWBHEADER_V1_SIZE(hdr); |
| 645 | |
Pali Rohár | 1228116 | 2021-07-23 11:14:21 +0200 | [diff] [blame] | 646 | if (size < hdrsz) { |
| 647 | errno = EINVAL; |
| 648 | goto out; |
| 649 | } |
| 650 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 651 | csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum; |
| 652 | if (csum != hdr->checksum) { |
| 653 | errno = EINVAL; |
| 654 | goto out; |
| 655 | } |
| 656 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 657 | if (hdr->blockid == IBR_HDR_UART_ID) { |
| 658 | rc = 0; |
| 659 | goto out; |
| 660 | } |
| 661 | |
| 662 | hdr->blockid = IBR_HDR_UART_ID; |
| 663 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 664 | if (image_ver == 0) { |
| 665 | struct main_hdr_v0 *hdr_v0 = img; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 666 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 667 | hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED; |
| 668 | hdr_v0->nandpagesize = 0; |
| 669 | |
| 670 | hdr_v0->srcaddr = hdr_v0->ext |
| 671 | ? sizeof(struct kwb_header) |
| 672 | : sizeof(*hdr_v0); |
| 673 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 674 | |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 675 | hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 676 | |
| 677 | rc = 0; |
| 678 | out: |
| 679 | return rc; |
| 680 | } |
| 681 | |
| 682 | static void |
| 683 | kwboot_usage(FILE *stream, char *progname) |
| 684 | { |
Pali Rohár | 3c703aaf | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 685 | fprintf(stream, "kwboot version %s\n", PLAIN_VERSION); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 686 | fprintf(stream, |
Kevin Smith | 6c7c1ba | 2016-02-16 21:28:17 +0000 | [diff] [blame] | 687 | "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n", |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 688 | progname); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 689 | fprintf(stream, "\n"); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 690 | fprintf(stream, |
| 691 | " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 692 | fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n"); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 693 | fprintf(stream, |
| 694 | " -D <image>: boot <image> without preamble (Dove)\n"); |
| 695 | fprintf(stream, " -d: enter debug mode\n"); |
| 696 | fprintf(stream, " -a: use timings for Armada XP\n"); |
Stefan Roese | f7f509d | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 697 | fprintf(stream, " -q <req-delay>: use specific request-delay\n"); |
| 698 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 699 | fprintf(stream, |
| 700 | " -o <block-timeo>: use specific xmodem block timeout\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 701 | fprintf(stream, "\n"); |
| 702 | fprintf(stream, " -t: mini terminal\n"); |
| 703 | fprintf(stream, "\n"); |
| 704 | fprintf(stream, " -B <baud>: set baud rate\n"); |
| 705 | fprintf(stream, "\n"); |
| 706 | } |
| 707 | |
| 708 | int |
| 709 | main(int argc, char **argv) |
| 710 | { |
| 711 | const char *ttypath, *imgpath; |
| 712 | int rv, rc, tty, term, prot, patch; |
| 713 | void *bootmsg; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 714 | void *debugmsg; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 715 | void *img; |
| 716 | size_t size; |
| 717 | speed_t speed; |
| 718 | |
| 719 | rv = 1; |
| 720 | tty = -1; |
| 721 | bootmsg = NULL; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 722 | debugmsg = NULL; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 723 | imgpath = NULL; |
| 724 | img = NULL; |
| 725 | term = 0; |
| 726 | patch = 0; |
| 727 | size = 0; |
| 728 | speed = B115200; |
| 729 | |
| 730 | kwboot_verbose = isatty(STDOUT_FILENO); |
| 731 | |
| 732 | do { |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 733 | int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 734 | if (c < 0) |
| 735 | break; |
| 736 | |
| 737 | switch (c) { |
| 738 | case 'b': |
| 739 | bootmsg = kwboot_msg_boot; |
| 740 | imgpath = optarg; |
| 741 | break; |
| 742 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 743 | case 'D': |
| 744 | bootmsg = NULL; |
| 745 | imgpath = optarg; |
| 746 | break; |
| 747 | |
| 748 | case 'd': |
| 749 | debugmsg = kwboot_msg_debug; |
| 750 | break; |
| 751 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 752 | case 'p': |
| 753 | patch = 1; |
| 754 | break; |
| 755 | |
| 756 | case 't': |
| 757 | term = 1; |
| 758 | break; |
| 759 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 760 | case 'a': |
| 761 | msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP; |
| 762 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; |
| 763 | break; |
| 764 | |
Stefan Roese | f7f509d | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 765 | case 'q': |
| 766 | msg_req_delay = atoi(optarg); |
| 767 | break; |
| 768 | |
| 769 | case 's': |
| 770 | msg_rsp_timeo = atoi(optarg); |
| 771 | break; |
| 772 | |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 773 | case 'o': |
| 774 | blk_rsp_timeo = atoi(optarg); |
| 775 | break; |
| 776 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 777 | case 'B': |
| 778 | speed = kwboot_tty_speed(atoi(optarg)); |
| 779 | if (speed == -1) |
| 780 | goto usage; |
| 781 | break; |
| 782 | |
| 783 | case 'h': |
| 784 | rv = 0; |
| 785 | default: |
| 786 | goto usage; |
| 787 | } |
| 788 | } while (1); |
| 789 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 790 | if (!bootmsg && !term && !debugmsg) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 791 | goto usage; |
| 792 | |
| 793 | if (patch && !imgpath) |
| 794 | goto usage; |
| 795 | |
| 796 | if (argc - optind < 1) |
| 797 | goto usage; |
| 798 | |
| 799 | ttypath = argv[optind++]; |
| 800 | |
| 801 | tty = kwboot_open_tty(ttypath, speed); |
| 802 | if (tty < 0) { |
| 803 | perror(ttypath); |
| 804 | goto out; |
| 805 | } |
| 806 | |
| 807 | if (imgpath) { |
| 808 | prot = PROT_READ | (patch ? PROT_WRITE : 0); |
| 809 | |
| 810 | img = kwboot_mmap_image(imgpath, &size, prot); |
| 811 | if (!img) { |
| 812 | perror(imgpath); |
| 813 | goto out; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (patch) { |
| 818 | rc = kwboot_img_patch_hdr(img, size); |
| 819 | if (rc) { |
| 820 | fprintf(stderr, "%s: Invalid image.\n", imgpath); |
| 821 | goto out; |
| 822 | } |
| 823 | } |
| 824 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 825 | if (debugmsg) { |
| 826 | rc = kwboot_debugmsg(tty, debugmsg); |
| 827 | if (rc) { |
| 828 | perror("debugmsg"); |
| 829 | goto out; |
| 830 | } |
Willy Tarreau | e8f8a7c | 2018-07-03 12:10:30 -0400 | [diff] [blame] | 831 | } else if (bootmsg) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 832 | rc = kwboot_bootmsg(tty, bootmsg); |
| 833 | if (rc) { |
| 834 | perror("bootmsg"); |
| 835 | goto out; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if (img) { |
| 840 | rc = kwboot_xmodem(tty, img, size); |
| 841 | if (rc) { |
| 842 | perror("xmodem"); |
| 843 | goto out; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (term) { |
| 848 | rc = kwboot_terminal(tty); |
| 849 | if (rc && !(errno == EINTR)) { |
| 850 | perror("terminal"); |
| 851 | goto out; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | rv = 0; |
| 856 | out: |
| 857 | if (tty >= 0) |
| 858 | close(tty); |
| 859 | |
| 860 | if (img) |
| 861 | munmap(img, size); |
| 862 | |
| 863 | return rv; |
| 864 | |
| 865 | usage: |
| 866 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); |
| 867 | goto out; |
| 868 | } |