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 |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 4 | * Copyright: Copyright (C) 2001, Hewlett-Packard Company |
| 5 | * Author: Christopher Hoover <ch@hpl.hp.com> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Description: xmodem functionality for uploading of kernels |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 7 | * and the like |
| 8 | * Created at: Thu Dec 20 01:58:08 PST 2001 |
| 9 | *-----------------------------------------------------------------------*/ |
| 10 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 11 | * xmodem.c: xmodem functionality for uploading of kernels and |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 12 | * the like |
| 13 | * |
| 14 | * Copyright (C) 2001 Hewlett-Packard Laboratories |
| 15 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 16 | * 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] | 17 | * |
| 18 | * This was originally written for blob and then adapted for busybox. |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 22 | |
| 23 | #define SOH 0x01 |
| 24 | #define STX 0x02 |
| 25 | #define EOT 0x04 |
| 26 | #define ACK 0x06 |
| 27 | #define NAK 0x15 |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 28 | #define BS 0x08 |
| 29 | |
| 30 | /* |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 31 | Cf: |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 32 | http://www.textfiles.com/apple/xmodem |
| 33 | http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt |
| 34 | http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt |
| 35 | http://www.phys.washington.edu/~belonis/xmodem/modmprot.col |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #define TIMEOUT 1 |
| 39 | #define TIMEOUT_LONG 10 |
| 40 | #define MAXERRORS 10 |
| 41 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 42 | #define read_fd STDIN_FILENO |
| 43 | #define write_fd STDOUT_FILENO |
| 44 | |
| 45 | static int read_byte(unsigned timeout) |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 46 | { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 47 | char buf[1]; |
| 48 | int n; |
| 49 | |
| 50 | alarm(timeout); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 51 | /* NOT safe_read! We want ALRM to interrupt us */ |
| 52 | n = read(read_fd, buf, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 53 | alarm(0); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 54 | if (n == 1) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 55 | return (unsigned char)buf[0]; |
| 56 | return -1; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 59 | static int receive(/*int read_fd, */int file_fd) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 61 | unsigned char blockBuf[1024]; |
| 62 | unsigned errors = 0; |
| 63 | unsigned wantBlockNo = 1; |
| 64 | unsigned length = 0; |
| 65 | int do_crc = 1; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 66 | char nak = 'C'; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 67 | unsigned timeout = TIMEOUT_LONG; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 68 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 69 | /* Flush pending input */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 70 | tcflush(read_fd, TCIFLUSH); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 71 | |
| 72 | /* Ask for CRC; if we get errors, we will go with checksum */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 73 | full_write(write_fd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 74 | |
| 75 | for (;;) { |
| 76 | int blockBegin; |
| 77 | int blockNo, blockNoOnesCompl; |
| 78 | int blockLength; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 79 | int cksum_crc; /* cksum OR crc */ |
| 80 | int expected; |
| 81 | int i,j; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 83 | blockBegin = read_byte(timeout); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 84 | if (blockBegin < 0) |
| 85 | goto timeout; |
| 86 | |
| 87 | timeout = TIMEOUT; |
| 88 | nak = NAK; |
| 89 | |
| 90 | switch (blockBegin) { |
| 91 | case SOH: |
| 92 | case STX: |
| 93 | break; |
| 94 | |
| 95 | case EOT: |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 96 | nak = ACK; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 97 | full_write(write_fd, &nak, 1); |
| 98 | return length; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 99 | |
| 100 | default: |
| 101 | goto error; |
| 102 | } |
| 103 | |
| 104 | /* block no */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 105 | blockNo = read_byte(TIMEOUT); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 106 | if (blockNo < 0) |
| 107 | goto timeout; |
| 108 | |
| 109 | /* block no one's compliment */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 110 | blockNoOnesCompl = read_byte(TIMEOUT); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 111 | if (blockNoOnesCompl < 0) |
| 112 | goto timeout; |
| 113 | |
| 114 | if (blockNo != (255 - blockNoOnesCompl)) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 115 | bb_error_msg("bad block ones compl"); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | blockLength = (blockBegin == SOH) ? 128 : 1024; |
| 120 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 121 | for (i = 0; i < blockLength; i++) { |
| 122 | int cc = read_byte(TIMEOUT); |
| 123 | if (cc < 0) |
| 124 | goto timeout; |
| 125 | blockBuf[i] = cc; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 128 | if (do_crc) { |
| 129 | cksum_crc = read_byte(TIMEOUT); |
| 130 | if (cksum_crc < 0) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 131 | goto timeout; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 132 | cksum_crc = (cksum_crc << 8) | read_byte(TIMEOUT); |
| 133 | if (cksum_crc < 0) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 134 | goto timeout; |
| 135 | } else { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 136 | cksum_crc = read_byte(TIMEOUT); |
| 137 | if (cksum_crc < 0) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 138 | goto timeout; |
| 139 | } |
| 140 | |
| 141 | if (blockNo == ((wantBlockNo - 1) & 0xff)) { |
| 142 | /* a repeat of the last block is ok, just ignore it. */ |
| 143 | /* this also ignores the initial block 0 which is */ |
| 144 | /* meta data. */ |
| 145 | goto next; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 146 | } |
| 147 | if (blockNo != (wantBlockNo & 0xff)) { |
| 148 | bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 149 | goto error; |
| 150 | } |
| 151 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 152 | expected = 0; |
| 153 | if (do_crc) { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 154 | for (i = 0; i < blockLength; i++) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 155 | expected = expected ^ blockBuf[i] << 8; |
| 156 | for (j = 0; j < 8; j++) { |
| 157 | if (expected & 0x8000) |
| 158 | expected = expected << 1 ^ 0x1021; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 159 | else |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 160 | expected = expected << 1; |
| 161 | } |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 162 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 163 | expected &= 0xffff; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 164 | } else { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 165 | for (i = 0; i < blockLength; i++) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 166 | expected += blockBuf[i]; |
| 167 | expected &= 0xff; |
| 168 | } |
| 169 | if (cksum_crc != expected) { |
| 170 | bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x" |
| 171 | : "checksum error, expected 0x%02x, got 0x%02x", |
| 172 | expected, cksum_crc); |
| 173 | goto error; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | wantBlockNo++; |
| 177 | length += blockLength; |
| 178 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 179 | errno = 0; |
| 180 | if (full_write(file_fd, blockBuf, blockLength) != blockLength) { |
| 181 | bb_perror_msg("can't write to file"); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 182 | goto fatal; |
| 183 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 184 | next: |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 185 | errors = 0; |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 186 | nak = ACK; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 187 | full_write(write_fd, &nak, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 188 | continue; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 189 | error: |
| 190 | timeout: |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 191 | errors++; |
| 192 | if (errors == MAXERRORS) { |
| 193 | /* Abort */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 194 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 195 | /* if were asking for crc, try again w/o crc */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 196 | if (nak == 'C') { |
| 197 | nak = NAK; |
| 198 | errors = 0; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 199 | do_crc = 0; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 200 | goto timeout; |
| 201 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 202 | bb_error_msg("too many errors; giving up"); |
| 203 | fatal: |
| 204 | /* 5 CAN followed by 5 BS. Don't try too hard... */ |
| 205 | safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 209 | /* Flush pending input */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 210 | tcflush(read_fd, TCIFLUSH); |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 212 | full_write(write_fd, &nak, 1); |
| 213 | } /* for (;;) */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 216 | static void sigalrm_handler(int ATTRIBUTE_UNUSED signum) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 217 | { |
| 218 | } |
| 219 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 220 | int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 221 | int rx_main(int argc, char **argv) |
| 222 | { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 223 | struct sigaction act; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 224 | struct termios tty, orig_tty; |
| 225 | int termios_err; |
| 226 | int file_fd; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 227 | int n; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 228 | |
| 229 | if (argc != 2) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 230 | bb_show_usage(); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 232 | /* Disabled by vda: |
| 233 | * why we can't receive from stdin? Why we *require* |
| 234 | * controlling tty?? */ |
| 235 | /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/ |
| 236 | file_fd = xopen(argv[1], O_RDWR|O_CREAT|O_TRUNC); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 237 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 238 | termios_err = tcgetattr(read_fd, &tty); |
| 239 | if (termios_err == 0) { |
| 240 | orig_tty = tty; |
| 241 | cfmakeraw(&tty); |
| 242 | tcsetattr(read_fd, TCSAFLUSH, &tty); |
| 243 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 244 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 245 | /* No SA_RESTART: we want ALRM to interrupt read() */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 246 | memset(&act, 0, sizeof(act)); |
| 247 | act.sa_handler = sigalrm_handler; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 248 | sigaction(SIGALRM, &act, NULL); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 249 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 250 | n = receive(file_fd); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 251 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 252 | if (termios_err == 0) |
| 253 | tcsetattr(read_fd, TCSAFLUSH, &orig_tty); |
| 254 | if (ENABLE_FEATURE_CLEAN_UP) |
| 255 | close(file_fd); |
| 256 | fflush_stdout_and_exit(n >= 0); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 257 | } |