Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 84899e2 | 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 | d131ad6 | 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 | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 12 | #include "kwbimage.h" |
| 13 | #include "mkimage.h" |
Pali Rohár | a050a86 | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 14 | #include "version.h" |
Stefan Roese | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 15 | |
Luka Perkov | d131ad6 | 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 | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 20 | #include <image.h> |
Luka Perkov | d131ad6 | 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 | d131ad6 | 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 | 84899e2 | 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 | d131ad6 | 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 | 84899e2 | 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 | d131ad6 | 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 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 60 | #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */ |
| 61 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 62 | struct kwboot_block { |
| 63 | uint8_t soh; |
| 64 | uint8_t pnum; |
| 65 | uint8_t _pnum; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 66 | uint8_t data[KWBOOT_XM_BLKSZ]; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 67 | uint8_t csum; |
Pali Rohár | a107c61 | 2021-07-23 11:14:14 +0200 | [diff] [blame] | 68 | } __packed; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 69 | |
| 70 | #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */ |
| 71 | |
| 72 | static int kwboot_verbose; |
| 73 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 74 | static int msg_req_delay = KWBOOT_MSG_REQ_DELAY; |
| 75 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 76 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 77 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 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 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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"; |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 145 | pos = (pos + 1) % width; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 146 | |
| 147 | if (pct == 100) { |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 148 | while (pos && pos++ < width) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 149 | fputc(' ', stdout); |
| 150 | fputs(nl, stdout); |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 151 | nl = ""; |
| 152 | pos = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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); |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 169 | |
| 170 | if (pct == 100) |
| 171 | pct = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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); |
Willy Tarreau | 4469bd7 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 204 | if (n <= 0) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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 | { |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 219 | if (!buf) |
| 220 | return 0; |
| 221 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 222 | if (kwboot_write(fd, buf, len) < 0) |
| 223 | return -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 224 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 225 | return tcdrain(fd); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 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; |
Jon Nettleton | 9ca6fae | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 295 | int count; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 296 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 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..."); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 301 | |
| 302 | do { |
| 303 | rc = tcflush(tty, TCIOFLUSH); |
| 304 | if (rc) |
| 305 | break; |
| 306 | |
Jon Nettleton | 9ca6fae | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 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 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 315 | rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 316 | |
| 317 | kwboot_spinner(); |
| 318 | |
| 319 | } while (rc || c != NAK); |
| 320 | |
| 321 | kwboot_printv("\n"); |
| 322 | |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | static int |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 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 | |
Pali Rohár | c5d666a | 2021-09-24 23:06:44 +0200 | [diff] [blame] | 357 | static size_t |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 358 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
| 359 | size_t size, int pnum) |
| 360 | { |
Marek Behún | d8cc851 | 2021-09-24 23:06:45 +0200 | [diff] [blame] | 361 | size_t i, n; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 362 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 363 | block->soh = SOH; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 364 | block->pnum = pnum; |
| 365 | block->_pnum = ~block->pnum; |
| 366 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 367 | n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 368 | memcpy(&block->data[0], data, n); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 369 | memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 370 | |
| 371 | block->csum = 0; |
| 372 | for (i = 0; i < n; i++) |
| 373 | block->csum += block->data[i]; |
| 374 | |
| 375 | return n; |
| 376 | } |
| 377 | |
| 378 | static int |
| 379 | kwboot_xm_sendblock(int fd, struct kwboot_block *block) |
| 380 | { |
| 381 | int rc, retries; |
| 382 | char c; |
| 383 | |
| 384 | retries = 16; |
| 385 | do { |
| 386 | rc = kwboot_tty_send(fd, block, sizeof(*block)); |
| 387 | if (rc) |
Pali Rohár | 00a1dee | 2021-09-24 23:06:43 +0200 | [diff] [blame] | 388 | return rc; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 389 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 390 | do { |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 391 | rc = kwboot_tty_recv(fd, &c, 1, blk_rsp_timeo); |
Pali Rohár | 00a1dee | 2021-09-24 23:06:43 +0200 | [diff] [blame] | 392 | if (rc) { |
| 393 | if (errno != ETIMEDOUT) |
| 394 | return rc; |
| 395 | c = NAK; |
| 396 | } |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 397 | |
| 398 | if (c != ACK && c != NAK && c != CAN) |
| 399 | printf("%c", c); |
| 400 | |
| 401 | } while (c != ACK && c != NAK && c != CAN); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 402 | |
| 403 | if (c != ACK) |
| 404 | kwboot_progress(-1, '+'); |
| 405 | |
| 406 | } while (c == NAK && retries-- > 0); |
| 407 | |
| 408 | rc = -1; |
| 409 | |
| 410 | switch (c) { |
| 411 | case ACK: |
| 412 | rc = 0; |
| 413 | break; |
| 414 | case NAK: |
| 415 | errno = EBADMSG; |
| 416 | break; |
| 417 | case CAN: |
| 418 | errno = ECANCELED; |
| 419 | break; |
| 420 | default: |
| 421 | errno = EPROTO; |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | static int |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 429 | kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data, |
| 430 | size_t size) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 431 | { |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 432 | size_t sent, left; |
| 433 | int rc; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 434 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 435 | kwboot_printv("Sending boot image %s (%zu bytes)...\n", |
| 436 | header ? "header" : "data", size); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 437 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 438 | left = size; |
| 439 | sent = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 440 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 441 | while (sent < size) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 442 | struct kwboot_block block; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 443 | size_t blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 444 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 445 | blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++); |
| 446 | data += blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 447 | |
| 448 | rc = kwboot_xm_sendblock(tty, &block); |
| 449 | if (rc) |
| 450 | goto out; |
| 451 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 452 | sent += blksz; |
| 453 | left -= blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 454 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 455 | kwboot_progress(sent * 100 / size, '.'); |
| 456 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 457 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 458 | kwboot_printv("Done\n"); |
| 459 | |
| 460 | return 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 461 | out: |
Pali Rohár | d5ba8db | 2021-09-24 23:06:47 +0200 | [diff] [blame] | 462 | kwboot_printv("\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 463 | return rc; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 464 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 465 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame^] | 466 | static int |
| 467 | kwboot_xmodem(int tty, const void *_img, size_t size) |
| 468 | { |
| 469 | const uint8_t *img = _img; |
| 470 | int rc, pnum; |
| 471 | size_t hdrsz; |
| 472 | |
| 473 | if (image_version(img) == 0) |
| 474 | hdrsz = KWBHEADER_V0_SIZE((struct main_hdr_v0 *)img); |
| 475 | else |
| 476 | hdrsz = KWBHEADER_V1_SIZE((struct main_hdr_v1 *)img); |
| 477 | |
| 478 | kwboot_printv("Waiting 2s and flushing tty\n"); |
| 479 | sleep(2); /* flush isn't effective without it */ |
| 480 | tcflush(tty, TCIOFLUSH); |
| 481 | |
| 482 | pnum = 1; |
| 483 | |
| 484 | rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz); |
| 485 | if (rc) |
| 486 | return rc; |
| 487 | |
| 488 | img += hdrsz; |
| 489 | size -= hdrsz; |
| 490 | |
| 491 | rc = kwboot_xmodem_one(tty, &pnum, 0, img, size); |
| 492 | if (rc) |
| 493 | return rc; |
| 494 | |
| 495 | return kwboot_tty_send_char(tty, EOT); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int |
Marek Behún | 46237e6 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 499 | kwboot_term_pipe(int in, int out, const char *quit, int *s) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 500 | { |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 501 | ssize_t nin; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 502 | char _buf[128], *buf = _buf; |
| 503 | |
Pali Rohár | 43fef8d | 2021-07-23 11:14:17 +0200 | [diff] [blame] | 504 | nin = read(in, buf, sizeof(_buf)); |
Willy Tarreau | 4469bd7 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 505 | if (nin <= 0) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 506 | return -1; |
| 507 | |
| 508 | if (quit) { |
| 509 | int i; |
| 510 | |
| 511 | for (i = 0; i < nin; i++) { |
| 512 | if (*buf == quit[*s]) { |
| 513 | (*s)++; |
| 514 | if (!quit[*s]) |
| 515 | return 0; |
| 516 | buf++; |
| 517 | nin--; |
Pali Rohár | b943eee | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 518 | } else { |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 519 | if (kwboot_write(out, quit, *s) < 0) |
| 520 | return -1; |
| 521 | *s = 0; |
Pali Rohár | b943eee | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 522 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 526 | if (kwboot_write(out, buf, nin) < 0) |
| 527 | return -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | kwboot_terminal(int tty) |
| 534 | { |
| 535 | int rc, in, s; |
Marek Behún | 46237e6 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 536 | const char *quit = "\34c"; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 537 | struct termios otio, tio; |
| 538 | |
| 539 | rc = -1; |
| 540 | |
| 541 | in = STDIN_FILENO; |
| 542 | if (isatty(in)) { |
| 543 | rc = tcgetattr(in, &otio); |
| 544 | if (!rc) { |
| 545 | tio = otio; |
| 546 | cfmakeraw(&tio); |
| 547 | rc = tcsetattr(in, TCSANOW, &tio); |
| 548 | } |
| 549 | if (rc) { |
| 550 | perror("tcsetattr"); |
| 551 | goto out; |
| 552 | } |
| 553 | |
| 554 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", |
| 555 | quit[0]|0100, quit[1]); |
| 556 | } else |
| 557 | in = -1; |
| 558 | |
| 559 | rc = 0; |
| 560 | s = 0; |
| 561 | |
| 562 | do { |
| 563 | fd_set rfds; |
| 564 | int nfds = 0; |
| 565 | |
| 566 | FD_SET(tty, &rfds); |
| 567 | nfds = nfds < tty ? tty : nfds; |
| 568 | |
| 569 | if (in >= 0) { |
| 570 | FD_SET(in, &rfds); |
| 571 | nfds = nfds < in ? in : nfds; |
| 572 | } |
| 573 | |
| 574 | nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); |
| 575 | if (nfds < 0) |
| 576 | break; |
| 577 | |
| 578 | if (FD_ISSET(tty, &rfds)) { |
| 579 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); |
| 580 | if (rc) |
| 581 | break; |
| 582 | } |
| 583 | |
Marek Behún | f30cb0d | 2021-09-24 23:06:39 +0200 | [diff] [blame] | 584 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 585 | rc = kwboot_term_pipe(in, tty, quit, &s); |
| 586 | if (rc) |
| 587 | break; |
| 588 | } |
| 589 | } while (quit[s] != 0); |
| 590 | |
Pali Rohár | ec0fe5b | 2021-07-23 11:14:18 +0200 | [diff] [blame] | 591 | if (in >= 0) |
| 592 | tcsetattr(in, TCSANOW, &otio); |
Pali Rohár | 49a0a3b | 2021-07-23 11:14:19 +0200 | [diff] [blame] | 593 | printf("\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 594 | out: |
| 595 | return rc; |
| 596 | } |
| 597 | |
| 598 | static void * |
| 599 | kwboot_mmap_image(const char *path, size_t *size, int prot) |
| 600 | { |
| 601 | int rc, fd, flags; |
| 602 | struct stat st; |
| 603 | void *img; |
| 604 | |
| 605 | rc = -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 606 | img = NULL; |
| 607 | |
| 608 | fd = open(path, O_RDONLY); |
| 609 | if (fd < 0) |
| 610 | goto out; |
| 611 | |
| 612 | rc = fstat(fd, &st); |
| 613 | if (rc) |
| 614 | goto out; |
| 615 | |
| 616 | flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED; |
| 617 | |
| 618 | img = mmap(NULL, st.st_size, prot, flags, fd, 0); |
| 619 | if (img == MAP_FAILED) { |
| 620 | img = NULL; |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | rc = 0; |
| 625 | *size = st.st_size; |
| 626 | out: |
| 627 | if (rc && img) { |
| 628 | munmap(img, st.st_size); |
| 629 | img = NULL; |
| 630 | } |
| 631 | if (fd >= 0) |
| 632 | close(fd); |
| 633 | |
| 634 | return img; |
| 635 | } |
| 636 | |
| 637 | static uint8_t |
| 638 | kwboot_img_csum8(void *_data, size_t size) |
| 639 | { |
| 640 | uint8_t *data = _data, csum; |
| 641 | |
| 642 | for (csum = 0; size-- > 0; data++) |
| 643 | csum += *data; |
| 644 | |
| 645 | return csum; |
| 646 | } |
| 647 | |
| 648 | static int |
| 649 | kwboot_img_patch_hdr(void *img, size_t size) |
| 650 | { |
| 651 | int rc; |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 652 | struct main_hdr_v1 *hdr; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 653 | uint8_t csum; |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 654 | size_t hdrsz = sizeof(*hdr); |
| 655 | int image_ver; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 656 | |
| 657 | rc = -1; |
| 658 | hdr = img; |
| 659 | |
| 660 | if (size < hdrsz) { |
| 661 | errno = EINVAL; |
| 662 | goto out; |
| 663 | } |
| 664 | |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 665 | image_ver = image_version(img); |
Pali Rohár | 5029d7b | 2021-07-23 11:14:22 +0200 | [diff] [blame] | 666 | if (image_ver != 0 && image_ver != 1) { |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 667 | fprintf(stderr, "Invalid image header version\n"); |
| 668 | errno = EINVAL; |
| 669 | goto out; |
| 670 | } |
| 671 | |
| 672 | if (image_ver == 0) |
| 673 | hdrsz = sizeof(*hdr); |
| 674 | else |
| 675 | hdrsz = KWBHEADER_V1_SIZE(hdr); |
| 676 | |
Pali Rohár | 825a2ca | 2021-07-23 11:14:21 +0200 | [diff] [blame] | 677 | if (size < hdrsz) { |
| 678 | errno = EINVAL; |
| 679 | goto out; |
| 680 | } |
| 681 | |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 682 | csum = kwboot_img_csum8(hdr, hdrsz) - hdr->checksum; |
| 683 | if (csum != hdr->checksum) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 684 | errno = EINVAL; |
| 685 | goto out; |
| 686 | } |
| 687 | |
| 688 | if (hdr->blockid == IBR_HDR_UART_ID) { |
| 689 | rc = 0; |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | hdr->blockid = IBR_HDR_UART_ID; |
| 694 | |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 695 | if (image_ver == 0) { |
| 696 | struct main_hdr_v0 *hdr_v0 = img; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 697 | |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 698 | hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED; |
| 699 | hdr_v0->nandpagesize = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 700 | |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 701 | hdr_v0->srcaddr = hdr_v0->ext |
| 702 | ? sizeof(struct kwb_header) |
| 703 | : sizeof(*hdr_v0); |
| 704 | } |
| 705 | |
| 706 | hdr->checksum = kwboot_img_csum8(hdr, hdrsz) - csum; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 707 | |
| 708 | rc = 0; |
| 709 | out: |
| 710 | return rc; |
| 711 | } |
| 712 | |
| 713 | static void |
| 714 | kwboot_usage(FILE *stream, char *progname) |
| 715 | { |
Pali Rohár | a050a86 | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 716 | fprintf(stream, "kwboot version %s\n", PLAIN_VERSION); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 717 | fprintf(stream, |
Kevin Smith | 8669dac | 2016-02-16 21:28:17 +0000 | [diff] [blame] | 718 | "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n", |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 719 | progname); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 720 | fprintf(stream, "\n"); |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 721 | fprintf(stream, |
| 722 | " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 723 | fprintf(stream, " -p: patch <image> to type 0x69 (uart boot)\n"); |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 724 | fprintf(stream, |
| 725 | " -D <image>: boot <image> without preamble (Dove)\n"); |
| 726 | fprintf(stream, " -d: enter debug mode\n"); |
| 727 | fprintf(stream, " -a: use timings for Armada XP\n"); |
Stefan Roese | 1c0df9e | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 728 | fprintf(stream, " -q <req-delay>: use specific request-delay\n"); |
| 729 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 730 | fprintf(stream, |
| 731 | " -o <block-timeo>: use specific xmodem block timeout\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 732 | fprintf(stream, "\n"); |
| 733 | fprintf(stream, " -t: mini terminal\n"); |
| 734 | fprintf(stream, "\n"); |
| 735 | fprintf(stream, " -B <baud>: set baud rate\n"); |
| 736 | fprintf(stream, "\n"); |
| 737 | } |
| 738 | |
| 739 | int |
| 740 | main(int argc, char **argv) |
| 741 | { |
| 742 | const char *ttypath, *imgpath; |
| 743 | int rv, rc, tty, term, prot, patch; |
| 744 | void *bootmsg; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 745 | void *debugmsg; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 746 | void *img; |
| 747 | size_t size; |
| 748 | speed_t speed; |
| 749 | |
| 750 | rv = 1; |
| 751 | tty = -1; |
| 752 | bootmsg = NULL; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 753 | debugmsg = NULL; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 754 | imgpath = NULL; |
| 755 | img = NULL; |
| 756 | term = 0; |
| 757 | patch = 0; |
| 758 | size = 0; |
| 759 | speed = B115200; |
| 760 | |
| 761 | kwboot_verbose = isatty(STDOUT_FILENO); |
| 762 | |
| 763 | do { |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 764 | int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 765 | if (c < 0) |
| 766 | break; |
| 767 | |
| 768 | switch (c) { |
| 769 | case 'b': |
| 770 | bootmsg = kwboot_msg_boot; |
| 771 | imgpath = optarg; |
| 772 | break; |
| 773 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 774 | case 'D': |
| 775 | bootmsg = NULL; |
| 776 | imgpath = optarg; |
| 777 | break; |
| 778 | |
| 779 | case 'd': |
| 780 | debugmsg = kwboot_msg_debug; |
| 781 | break; |
| 782 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 783 | case 'p': |
| 784 | patch = 1; |
| 785 | break; |
| 786 | |
| 787 | case 't': |
| 788 | term = 1; |
| 789 | break; |
| 790 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 791 | case 'a': |
| 792 | msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP; |
| 793 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; |
| 794 | break; |
| 795 | |
Stefan Roese | 1c0df9e | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 796 | case 'q': |
| 797 | msg_req_delay = atoi(optarg); |
| 798 | break; |
| 799 | |
| 800 | case 's': |
| 801 | msg_rsp_timeo = atoi(optarg); |
| 802 | break; |
| 803 | |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 804 | case 'o': |
| 805 | blk_rsp_timeo = atoi(optarg); |
| 806 | break; |
| 807 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 808 | case 'B': |
| 809 | speed = kwboot_tty_speed(atoi(optarg)); |
| 810 | if (speed == -1) |
| 811 | goto usage; |
| 812 | break; |
| 813 | |
| 814 | case 'h': |
| 815 | rv = 0; |
| 816 | default: |
| 817 | goto usage; |
| 818 | } |
| 819 | } while (1); |
| 820 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 821 | if (!bootmsg && !term && !debugmsg) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 822 | goto usage; |
| 823 | |
| 824 | if (patch && !imgpath) |
| 825 | goto usage; |
| 826 | |
| 827 | if (argc - optind < 1) |
| 828 | goto usage; |
| 829 | |
| 830 | ttypath = argv[optind++]; |
| 831 | |
| 832 | tty = kwboot_open_tty(ttypath, speed); |
| 833 | if (tty < 0) { |
| 834 | perror(ttypath); |
| 835 | goto out; |
| 836 | } |
| 837 | |
| 838 | if (imgpath) { |
| 839 | prot = PROT_READ | (patch ? PROT_WRITE : 0); |
| 840 | |
| 841 | img = kwboot_mmap_image(imgpath, &size, prot); |
| 842 | if (!img) { |
| 843 | perror(imgpath); |
| 844 | goto out; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (patch) { |
| 849 | rc = kwboot_img_patch_hdr(img, size); |
| 850 | if (rc) { |
| 851 | fprintf(stderr, "%s: Invalid image.\n", imgpath); |
| 852 | goto out; |
| 853 | } |
| 854 | } |
| 855 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 856 | if (debugmsg) { |
| 857 | rc = kwboot_debugmsg(tty, debugmsg); |
| 858 | if (rc) { |
| 859 | perror("debugmsg"); |
| 860 | goto out; |
| 861 | } |
Willy Tarreau | 3475a71 | 2018-07-03 12:10:30 -0400 | [diff] [blame] | 862 | } else if (bootmsg) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 863 | rc = kwboot_bootmsg(tty, bootmsg); |
| 864 | if (rc) { |
| 865 | perror("bootmsg"); |
| 866 | goto out; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (img) { |
| 871 | rc = kwboot_xmodem(tty, img, size); |
| 872 | if (rc) { |
| 873 | perror("xmodem"); |
| 874 | goto out; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (term) { |
| 879 | rc = kwboot_terminal(tty); |
| 880 | if (rc && !(errno == EINTR)) { |
| 881 | perror("terminal"); |
| 882 | goto out; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | rv = 0; |
| 887 | out: |
| 888 | if (tty >= 0) |
| 889 | close(tty); |
| 890 | |
| 891 | if (img) |
| 892 | munmap(img, size); |
| 893 | |
| 894 | return rv; |
| 895 | |
| 896 | usage: |
| 897 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); |
| 898 | goto out; |
| 899 | } |