Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 2 | /*------------------------------------------------------------------------- |
| 3 | * Filename: xmodem.c |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4 | * Version: $Id: rx.c,v 1.2 2004/03/15 08:28:46 andersen Exp $ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 5 | * Copyright: Copyright (C) 2001, Hewlett-Packard Company |
| 6 | * Author: Christopher Hoover <ch@hpl.hp.com> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 7 | * Description: xmodem functionality for uploading of kernels |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 8 | * and the like |
| 9 | * Created at: Thu Dec 20 01:58:08 PST 2001 |
| 10 | *-----------------------------------------------------------------------*/ |
| 11 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 12 | * xmodem.c: xmodem functionality for uploading of kernels and |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 13 | * the like |
| 14 | * |
| 15 | * Copyright (C) 2001 Hewlett-Packard Laboratories |
| 16 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 18 | * |
| 19 | * This was originally written for blob and then adapted for busybox. |
| 20 | * |
| 21 | */ |
| 22 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | #include <termios.h> |
| 30 | #include <signal.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <string.h> |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | #define SOH 0x01 |
| 38 | #define STX 0x02 |
| 39 | #define EOT 0x04 |
| 40 | #define ACK 0x06 |
| 41 | #define NAK 0x15 |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 42 | #define BS 0x08 |
| 43 | |
| 44 | /* |
| 45 | |
| 46 | Cf: |
| 47 | |
| 48 | http://www.textfiles.com/apple/xmodem |
| 49 | http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt |
| 50 | http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt |
| 51 | http://www.phys.washington.edu/~belonis/xmodem/modmprot.col |
| 52 | |
| 53 | */ |
| 54 | |
| 55 | #define TIMEOUT 1 |
| 56 | #define TIMEOUT_LONG 10 |
| 57 | #define MAXERRORS 10 |
| 58 | |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 59 | static int read_byte(int fd, unsigned int timeout) { |
| 60 | char buf[1]; |
| 61 | int n; |
| 62 | |
| 63 | alarm(timeout); |
| 64 | |
| 65 | n = read(fd, &buf, 1); |
| 66 | |
| 67 | alarm(0); |
| 68 | |
| 69 | if (n == 1) |
| 70 | return buf[0] & 0xff; |
| 71 | else |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static int receive(char *error_buf, size_t error_buf_size, |
| 76 | int ttyfd, int filefd) |
| 77 | { |
| 78 | char blockBuf[1024]; |
| 79 | unsigned int errors = 0; |
| 80 | unsigned int wantBlockNo = 1; |
| 81 | unsigned int length = 0; |
| 82 | int docrc = 1; |
| 83 | char nak = 'C'; |
| 84 | unsigned int timeout = TIMEOUT_LONG; |
| 85 | |
| 86 | #define note_error(fmt,args...) \ |
| 87 | snprintf(error_buf, error_buf_size, fmt,##args) |
| 88 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 89 | /* Flush pending input */ |
| 90 | tcflush(ttyfd, TCIFLUSH); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 91 | |
| 92 | /* Ask for CRC; if we get errors, we will go with checksum */ |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 93 | write(ttyfd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 94 | |
| 95 | for (;;) { |
| 96 | int blockBegin; |
| 97 | int blockNo, blockNoOnesCompl; |
| 98 | int blockLength; |
| 99 | int cksum = 0; |
| 100 | int crcHi = 0; |
| 101 | int crcLo = 0; |
| 102 | |
| 103 | blockBegin = read_byte(ttyfd, timeout); |
| 104 | if (blockBegin < 0) |
| 105 | goto timeout; |
| 106 | |
| 107 | timeout = TIMEOUT; |
| 108 | nak = NAK; |
| 109 | |
| 110 | switch (blockBegin) { |
| 111 | case SOH: |
| 112 | case STX: |
| 113 | break; |
| 114 | |
| 115 | case EOT: |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 116 | nak = ACK; |
| 117 | write(ttyfd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 118 | goto done; |
| 119 | |
| 120 | default: |
| 121 | goto error; |
| 122 | } |
| 123 | |
| 124 | /* block no */ |
| 125 | blockNo = read_byte(ttyfd, TIMEOUT); |
| 126 | if (blockNo < 0) |
| 127 | goto timeout; |
| 128 | |
| 129 | /* block no one's compliment */ |
| 130 | blockNoOnesCompl = read_byte(ttyfd, TIMEOUT); |
| 131 | if (blockNoOnesCompl < 0) |
| 132 | goto timeout; |
| 133 | |
| 134 | if (blockNo != (255 - blockNoOnesCompl)) { |
| 135 | note_error("bad block ones compl"); |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | blockLength = (blockBegin == SOH) ? 128 : 1024; |
| 140 | |
| 141 | { |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < blockLength; i++) { |
| 145 | int cc = read_byte(ttyfd, TIMEOUT); |
| 146 | if (cc < 0) |
| 147 | goto timeout; |
| 148 | blockBuf[i] = cc; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (docrc) { |
| 153 | crcHi = read_byte(ttyfd, TIMEOUT); |
| 154 | if (crcHi < 0) |
| 155 | goto timeout; |
| 156 | |
| 157 | crcLo = read_byte(ttyfd, TIMEOUT); |
| 158 | if (crcLo < 0) |
| 159 | goto timeout; |
| 160 | } else { |
| 161 | cksum = read_byte(ttyfd, TIMEOUT); |
| 162 | if (cksum < 0) |
| 163 | goto timeout; |
| 164 | } |
| 165 | |
| 166 | if (blockNo == ((wantBlockNo - 1) & 0xff)) { |
| 167 | /* a repeat of the last block is ok, just ignore it. */ |
| 168 | /* this also ignores the initial block 0 which is */ |
| 169 | /* meta data. */ |
| 170 | goto next; |
| 171 | } else if (blockNo != (wantBlockNo & 0xff)) { |
| 172 | note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo); |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | if (docrc) { |
| 177 | int crc = 0; |
| 178 | int i, j; |
| 179 | int expectedCrcHi; |
| 180 | int expectedCrcLo; |
| 181 | |
| 182 | for (i = 0; i < blockLength; i++) { |
| 183 | crc = crc ^ (int) blockBuf[i] << 8; |
| 184 | for (j = 0; j < 8; j++) |
| 185 | if (crc & 0x8000) |
| 186 | crc = crc << 1 ^ 0x1021; |
| 187 | else |
| 188 | crc = crc << 1; |
| 189 | } |
| 190 | |
| 191 | expectedCrcHi = (crc >> 8) & 0xff; |
| 192 | expectedCrcLo = crc & 0xff; |
| 193 | |
| 194 | if ((crcHi != expectedCrcHi) || |
| 195 | (crcLo != expectedCrcLo)) { |
| 196 | note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo); |
| 197 | goto error; |
| 198 | } |
| 199 | } else { |
| 200 | unsigned char expectedCksum = 0; |
| 201 | int i; |
| 202 | |
| 203 | for (i = 0; i < blockLength; i++) |
| 204 | expectedCksum += blockBuf[i]; |
| 205 | |
| 206 | if (cksum != expectedCksum) { |
| 207 | note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum); |
| 208 | goto error; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | wantBlockNo++; |
| 213 | length += blockLength; |
| 214 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 215 | if (full_write(filefd, blockBuf, blockLength) < 0) { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 216 | note_error("write to file failed: %m"); |
| 217 | goto fatal; |
| 218 | } |
| 219 | |
| 220 | next: |
| 221 | errors = 0; |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 222 | nak = ACK; |
| 223 | write(ttyfd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 224 | continue; |
| 225 | |
| 226 | error: |
| 227 | timeout: |
| 228 | errors++; |
| 229 | if (errors == MAXERRORS) { |
| 230 | /* Abort */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 231 | |
| 232 | // if using crc, try again w/o crc |
| 233 | if (nak == 'C') { |
| 234 | nak = NAK; |
| 235 | errors = 0; |
| 236 | docrc = 0; |
| 237 | goto timeout; |
| 238 | } |
| 239 | |
| 240 | note_error("too many errors; giving up"); |
| 241 | |
| 242 | fatal: |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 243 | /* 5 CAN followed by 5 BS */ |
| 244 | write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 245 | return -1; |
| 246 | } |
| 247 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 248 | /* Flush pending input */ |
| 249 | tcflush(ttyfd, TCIFLUSH); |
| 250 | |
| 251 | write(ttyfd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | done: |
| 255 | return length; |
| 256 | |
| 257 | #undef note_error |
| 258 | } |
| 259 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 260 | static void sigalrm_handler(int ATTRIBUTE_UNUSED signum) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 261 | { |
| 262 | } |
| 263 | |
| 264 | int rx_main(int argc, char **argv) |
| 265 | { |
| 266 | char *fn; |
| 267 | int ttyfd, filefd; |
| 268 | struct termios tty, orig_tty; |
| 269 | struct sigaction act; |
| 270 | int n; |
| 271 | char error_buf[256]; |
| 272 | |
| 273 | if (argc != 2) |
| 274 | bb_show_usage(); |
| 275 | |
| 276 | fn = argv[1]; |
Bernhard Reutner-Fischer | 95a040f | 2006-05-27 09:36:43 +0000 | [diff] [blame] | 277 | ttyfd = bb_xopen3(CURRENT_TTY, O_RDWR, 0); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 278 | filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 279 | |
| 280 | if (tcgetattr(ttyfd, &tty) < 0) |
| 281 | bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 282 | |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 283 | orig_tty = tty; |
| 284 | |
| 285 | cfmakeraw(&tty); |
| 286 | tcsetattr(ttyfd, TCSAFLUSH, &tty); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 287 | |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 288 | memset(&act, 0, sizeof(act)); |
| 289 | act.sa_handler = sigalrm_handler; |
| 290 | sigaction(SIGALRM, &act, 0); |
| 291 | |
| 292 | n = receive(error_buf, sizeof(error_buf), ttyfd, filefd); |
| 293 | |
| 294 | close(filefd); |
| 295 | |
| 296 | tcsetattr(ttyfd, TCSAFLUSH, &orig_tty); |
| 297 | |
| 298 | if (n < 0) |
| 299 | bb_error_msg_and_die("\n%s: receive failed:\n %s\n", |
| 300 | argv[0], error_buf); |
| 301 | |
| 302 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
| 303 | } |