"formated" -> "formatted" throughout the code base.
[oweals/busybox.git] / miscutils / rx.c
1 /* vi: set sw=4 ts=4: */
2 /*-------------------------------------------------------------------------
3  * Filename:      xmodem.c
4  * Version:       $Id: rx.c,v 1.2 2004/03/15 08:28:46 andersen Exp $
5  * Copyright:     Copyright (C) 2001, Hewlett-Packard Company
6  * Author:        Christopher Hoover <ch@hpl.hp.com>
7  * Description:   xmodem functionality for uploading of kernels
8  *                and the like
9  * Created at:    Thu Dec 20 01:58:08 PST 2001
10  *-----------------------------------------------------------------------*/
11 /*
12  * xmodem.c: xmodem functionality for uploading of kernels and
13  *            the like
14  *
15  * Copyright (C) 2001 Hewlett-Packard Laboratories
16  *
17  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18  *
19  * This was originally written for blob and then adapted for busybox.
20  *
21  */
22
23 #include "busybox.h"
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>
35
36
37 #define SOH 0x01
38 #define STX 0x02
39 #define EOT 0x04
40 #define ACK 0x06
41 #define NAK 0x15
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
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
89         /* Flush pending input */
90         tcflush(ttyfd, TCIFLUSH);
91
92         /* Ask for CRC; if we get errors, we will go with checksum */
93         write(ttyfd, &nak, 1);
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:
116                         nak = ACK;
117                         write(ttyfd, &nak, 1);
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
215                 if (full_write(filefd, blockBuf, blockLength) < 0) {
216                         note_error("write to file failed: %m");
217                         goto fatal;
218                 }
219
220         next:
221                 errors = 0;
222                 nak = ACK;
223                 write(ttyfd, &nak, 1);
224                 continue;
225
226         error:
227         timeout:
228                 errors++;
229                 if (errors == MAXERRORS) {
230                         /* Abort */
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:
243                         /* 5 CAN followed by 5 BS */
244                         write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
245                         return -1;
246                 }
247
248                 /* Flush pending input */
249                 tcflush(ttyfd, TCIFLUSH);
250
251                 write(ttyfd, &nak, 1);
252         }
253
254  done:
255         return length;
256
257 #undef note_error
258 }
259
260 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
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];
277         ttyfd = bb_xopen3(CURRENT_TTY, O_RDWR, 0);
278         filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
279
280         if (tcgetattr(ttyfd, &tty) < 0)
281                         bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
282
283         orig_tty = tty;
284
285         cfmakeraw(&tty);
286         tcsetattr(ttyfd, TCSAFLUSH, &tty);
287
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 }