- remove emacs layout block as suggested by Robert P.J. Day
[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 <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <termios.h>
29 #include <signal.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include "busybox.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 CAN 0x18
43 #define BS  0x08
44
45 /*
46
47 Cf:
48
49   http://www.textfiles.com/apple/xmodem
50   http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
51   http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
52   http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
53
54 */
55
56 #define TIMEOUT 1
57 #define TIMEOUT_LONG 10
58 #define MAXERRORS 10
59
60 static inline void write_byte(int fd, char cc) {
61         write(fd, &cc, 1);
62 }
63
64 static inline void write_flush(int fd) {
65         tcdrain(fd);
66 }
67
68 static inline void read_flush(int fd) {
69         tcflush(fd, TCIFLUSH);
70 }
71
72 static int read_byte(int fd, unsigned int timeout) {
73         char buf[1];
74         int n;
75
76         alarm(timeout);
77
78         n = read(fd, &buf, 1);
79
80         alarm(0);
81
82         if (n == 1)
83                 return buf[0] & 0xff;
84         else
85                 return -1;
86 }
87
88 static int receive(char *error_buf, size_t error_buf_size,
89                                    int ttyfd, int filefd)
90 {
91         char blockBuf[1024];
92         unsigned int errors = 0;
93         unsigned int wantBlockNo = 1;
94         unsigned int length = 0;
95         int docrc = 1;
96         char nak = 'C';
97         unsigned int timeout = TIMEOUT_LONG;
98
99 #define note_error(fmt,args...) \
100         snprintf(error_buf, error_buf_size, fmt,##args)
101
102         read_flush(ttyfd);
103
104         /* Ask for CRC; if we get errors, we will go with checksum */
105         write_byte(ttyfd, nak);
106         write_flush(ttyfd);
107
108         for (;;) {
109                 int blockBegin;
110                 int blockNo, blockNoOnesCompl;
111                 int blockLength;
112                 int cksum = 0;
113                 int crcHi = 0;
114                 int crcLo = 0;
115
116                 blockBegin = read_byte(ttyfd, timeout);
117                 if (blockBegin < 0)
118                         goto timeout;
119
120                 timeout = TIMEOUT;
121                 nak = NAK;
122
123                 switch (blockBegin) {
124                 case SOH:
125                 case STX:
126                         break;
127
128                 case EOT:
129                         write_byte(ttyfd, ACK);
130                         write_flush(ttyfd);
131                         goto done;
132
133                 default:
134                         goto error;
135                 }
136
137                 /* block no */
138                 blockNo = read_byte(ttyfd, TIMEOUT);
139                 if (blockNo < 0)
140                         goto timeout;
141
142                 /* block no one's compliment */
143                 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
144                 if (blockNoOnesCompl < 0)
145                         goto timeout;
146
147                 if (blockNo != (255 - blockNoOnesCompl)) {
148                         note_error("bad block ones compl");
149                         goto error;
150                 }
151
152                 blockLength = (blockBegin == SOH) ? 128 : 1024;
153
154                 {
155                         int i;
156
157                         for (i = 0; i < blockLength; i++) {
158                                 int cc = read_byte(ttyfd, TIMEOUT);
159                                 if (cc < 0)
160                                         goto timeout;
161                                 blockBuf[i] = cc;
162                         }
163                 }
164
165                 if (docrc) {
166                         crcHi = read_byte(ttyfd, TIMEOUT);
167                         if (crcHi < 0)
168                                 goto timeout;
169
170                         crcLo = read_byte(ttyfd, TIMEOUT);
171                         if (crcLo < 0)
172                                 goto timeout;
173                 } else {
174                         cksum = read_byte(ttyfd, TIMEOUT);
175                         if (cksum < 0)
176                                 goto timeout;
177                 }
178
179                 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
180                         /* a repeat of the last block is ok, just ignore it. */
181                         /* this also ignores the initial block 0 which is */
182                         /* meta data. */
183                         goto next;
184                 } else if (blockNo != (wantBlockNo & 0xff)) {
185                         note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
186                         goto error;
187                 }
188
189                 if (docrc) {
190                         int crc = 0;
191                         int i, j;
192                         int expectedCrcHi;
193                         int expectedCrcLo;
194
195                         for (i = 0; i < blockLength; i++) {
196                                 crc = crc ^ (int) blockBuf[i] << 8;
197                                 for (j = 0; j < 8; j++)
198                                         if (crc & 0x8000)
199                                                 crc = crc << 1 ^ 0x1021;
200                                         else
201                                                 crc = crc << 1;
202                         }
203
204                         expectedCrcHi = (crc >> 8) & 0xff;
205                         expectedCrcLo = crc & 0xff;
206
207                         if ((crcHi != expectedCrcHi) ||
208                             (crcLo != expectedCrcLo)) {
209                                 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
210                                 goto error;
211                         }
212                 } else {
213                         unsigned char expectedCksum = 0;
214                         int i;
215
216                         for (i = 0; i < blockLength; i++)
217                                 expectedCksum += blockBuf[i];
218
219                         if (cksum != expectedCksum) {
220                                 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
221                                 goto error;
222                         }
223                 }
224
225                 wantBlockNo++;
226                 length += blockLength;
227
228                 if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
229                         note_error("write to file failed: %m");
230                         goto fatal;
231                 }
232
233         next:
234                 errors = 0;
235                 write_byte(ttyfd, ACK);
236                 write_flush(ttyfd);
237                 continue;
238
239         error:
240         timeout:
241                 errors++;
242                 if (errors == MAXERRORS) {
243                         /* Abort */
244                         int i;
245
246                         // if using crc, try again w/o crc
247                         if (nak == 'C') {
248                                 nak = NAK;
249                                 errors = 0;
250                                 docrc = 0;
251                                 goto timeout;
252                         }
253
254                         note_error("too many errors; giving up");
255
256                 fatal:
257                         for (i = 0; i < 5; i ++)
258                                 write_byte(ttyfd, CAN);
259                         for (i = 0; i < 5; i ++)
260                                 write_byte(ttyfd, BS);
261                         write_flush(ttyfd);
262                         return -1;
263                 }
264
265                 read_flush(ttyfd);
266                 write_byte(ttyfd, nak);
267                 write_flush(ttyfd);
268         }
269
270  done:
271         return length;
272
273 #undef note_error
274 }
275
276 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
277 {
278 }
279
280 int rx_main(int argc, char **argv)
281 {
282         char *fn;
283         int ttyfd, filefd;
284         struct termios tty, orig_tty;
285         struct sigaction act;
286         int n;
287         char error_buf[256];
288
289         if (argc != 2)
290                         bb_show_usage();
291
292         fn = argv[1];
293         ttyfd = bb_xopen3("/dev/tty", O_RDWR, 0);
294         filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
295
296         if (tcgetattr(ttyfd, &tty) < 0)
297                         bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
298
299         orig_tty = tty;
300
301         cfmakeraw(&tty);
302         tcsetattr(ttyfd, TCSAFLUSH, &tty);
303
304         memset(&act, 0, sizeof(act));
305         act.sa_handler = sigalrm_handler;
306         sigaction(SIGALRM, &act, 0);
307
308         n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
309
310         close(filefd);
311
312         tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
313
314         if (n < 0)
315                         bb_error_msg_and_die("\n%s: receive failed:\n  %s\n",
316                                                            argv[0], error_buf);
317
318         bb_fflush_stdout_and_exit(EXIT_SUCCESS);
319 }