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