blob: 6fb23b97b41334e5755ba76ed64757effab97d06 [file] [log] [blame]
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00002/*-------------------------------------------------------------------------
3 * Filename: xmodem.c
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00004 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
5 * Author: Christopher Hoover <ch@hpl.hp.com>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Description: xmodem functionality for uploading of kernels
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00007 * and the like
8 * Created at: Thu Dec 20 01:58:08 PST 2001
9 *-----------------------------------------------------------------------*/
10/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000011 * xmodem.c: xmodem functionality for uploading of kernels and
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000012 * the like
13 *
14 * Copyright (C) 2001 Hewlett-Packard Laboratories
15 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000016 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000017 *
18 * This was originally written for blob and then adapted for busybox.
19 *
20 */
21
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000022#include "busybox.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000023
24#define SOH 0x01
25#define STX 0x02
26#define EOT 0x04
27#define ACK 0x06
28#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000029#define BS 0x08
30
31/*
32
33Cf:
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
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000046static 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
62static 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
Rob Landley399d45f2006-06-21 01:49:17 +000076 /* Flush pending input */
77 tcflush(ttyfd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000078
79 /* Ask for CRC; if we get errors, we will go with checksum */
Rob Landley399d45f2006-06-21 01:49:17 +000080 write(ttyfd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000081
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:
Rob Landley399d45f2006-06-21 01:49:17 +0000103 nak = ACK;
104 write(ttyfd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000105 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
Rob Landley53437472006-07-16 08:14:35 +0000202 if (full_write(filefd, blockBuf, blockLength) < 0) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000203 note_error("write to file failed: %m");
204 goto fatal;
205 }
206
207 next:
208 errors = 0;
Rob Landley399d45f2006-06-21 01:49:17 +0000209 nak = ACK;
210 write(ttyfd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000211 continue;
212
213 error:
214 timeout:
215 errors++;
216 if (errors == MAXERRORS) {
217 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000218
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:
Rob Landley399d45f2006-06-21 01:49:17 +0000230 /* 5 CAN followed by 5 BS */
231 write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000232 return -1;
233 }
234
Rob Landley399d45f2006-06-21 01:49:17 +0000235 /* Flush pending input */
236 tcflush(ttyfd, TCIFLUSH);
237
238 write(ttyfd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000239 }
240
241 done:
242 return length;
243
244#undef note_error
245}
246
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000247static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000248{
249}
250
251int 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];
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000264 ttyfd = xopen(CURRENT_TTY, O_RDWR);
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000265 filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000266
267 if (tcgetattr(ttyfd, &tty) < 0)
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000268 bb_perror_msg_and_die("tcgetattr");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000269
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000270 orig_tty = tty;
271
272 cfmakeraw(&tty);
273 tcsetattr(ttyfd, TCSAFLUSH, &tty);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000274
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000275 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)
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000286 bb_error_msg_and_die("\nreceive failed:\n %s", error_buf);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000287
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000288 fflush_stdout_and_exit(EXIT_SUCCESS);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000289}