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