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