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