blob: de785d53ca7bffa53c9131aa598b48b05653cd92 [file] [log] [blame]
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +02002/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00003 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
4 * Author: Christopher Hoover <ch@hpl.hp.com>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Description: xmodem functionality for uploading of kernels
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00006 * and the like
7 * Created at: Thu Dec 20 01:58:08 PST 2001
Denys Vlasenkodc9495d2009-08-02 19:45:31 +02008 *
9 * xmodem functionality for uploading of kernels and the like
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000010 *
11 * Copyright (C) 2001 Hewlett-Packard Laboratories
12 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020013 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000014 *
15 * This was originally written for blob and then adapted for busybox.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000016 */
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000019
20#define SOH 0x01
21#define STX 0x02
22#define EOT 0x04
23#define ACK 0x06
24#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000025#define BS 0x08
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020026#define PAD 0x1A
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000027
28/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000029Cf:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000030 http://www.textfiles.com/apple/xmodem
31 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
32 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
33 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000034*/
35
36#define TIMEOUT 1
37#define TIMEOUT_LONG 10
38#define MAXERRORS 10
39
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000040#define read_fd STDIN_FILENO
41#define write_fd STDOUT_FILENO
42
43static int read_byte(unsigned timeout)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000044{
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020045 unsigned char buf;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000046 int n;
47
48 alarm(timeout);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000049 /* NOT safe_read! We want ALRM to interrupt us */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020050 n = read(read_fd, &buf, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000051 alarm(0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000052 if (n == 1)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020053 return buf;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000054 return -1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000055}
56
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000057static int receive(/*int read_fd, */int file_fd)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000058{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000059 unsigned char blockBuf[1024];
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020060 unsigned blockLength = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000061 unsigned errors = 0;
62 unsigned wantBlockNo = 1;
63 unsigned length = 0;
64 int do_crc = 1;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020065 char reply_char;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000066 unsigned timeout = TIMEOUT_LONG;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000067
Rob Landley399d45f2006-06-21 01:49:17 +000068 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000069 tcflush(read_fd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000070
71 /* Ask for CRC; if we get errors, we will go with checksum */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020072 reply_char = 'C';
73 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000074
75 for (;;) {
76 int blockBegin;
77 int blockNo, blockNoOnesCompl;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020078 int cksum_or_crc;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000079 int expected;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020080 int i, j;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000081
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000082 blockBegin = read_byte(timeout);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000083 if (blockBegin < 0)
84 goto timeout;
85
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020086 /* If last block, remove padding */
87 if (blockBegin == EOT) {
88 /* Data blocks can be padded with ^Z characters */
89 /* This code tries to detect and remove them */
90 if (blockLength >= 3
91 && blockBuf[blockLength - 1] == PAD
92 && blockBuf[blockLength - 2] == PAD
93 && blockBuf[blockLength - 3] == PAD
94 ) {
95 while (blockLength
96 && blockBuf[blockLength - 1] == PAD
97 ) {
98 blockLength--;
99 }
100 }
101 }
102 /* Write previously received block */
103 if (blockLength) {
104 errno = 0;
105 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
106 bb_perror_msg("can't write to file");
107 goto fatal;
108 }
109 }
110
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000111 timeout = TIMEOUT;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200112 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000113
114 switch (blockBegin) {
115 case SOH:
116 case STX:
117 break;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000118 case EOT:
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200119 reply_char = ACK;
120 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000121 return length;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000122 default:
123 goto error;
124 }
125
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200126 /* Block no */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000127 blockNo = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000128 if (blockNo < 0)
129 goto timeout;
130
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200131 /* Block no, in one's complement form */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000132 blockNoOnesCompl = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000133 if (blockNoOnesCompl < 0)
134 goto timeout;
135
136 if (blockNo != (255 - blockNoOnesCompl)) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000137 bb_error_msg("bad block ones compl");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000138 goto error;
139 }
140
141 blockLength = (blockBegin == SOH) ? 128 : 1024;
142
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000143 for (i = 0; i < blockLength; i++) {
144 int cc = read_byte(TIMEOUT);
145 if (cc < 0)
146 goto timeout;
147 blockBuf[i] = cc;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000148 }
149
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000150 if (do_crc) {
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200151 cksum_or_crc = read_byte(TIMEOUT);
152 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000153 goto timeout;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200154 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
155 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000156 goto timeout;
157 } else {
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200158 cksum_or_crc = read_byte(TIMEOUT);
159 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000160 goto timeout;
161 }
162
163 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
164 /* a repeat of the last block is ok, just ignore it. */
165 /* this also ignores the initial block 0 which is */
166 /* meta data. */
167 goto next;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000168 }
169 if (blockNo != (wantBlockNo & 0xff)) {
170 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000171 goto error;
172 }
173
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000174 expected = 0;
175 if (do_crc) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000176 for (i = 0; i < blockLength; i++) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000177 expected = expected ^ blockBuf[i] << 8;
178 for (j = 0; j < 8; j++) {
179 if (expected & 0x8000)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200180 expected = (expected << 1) ^ 0x1021;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000181 else
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200182 expected = (expected << 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000183 }
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000184 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000185 expected &= 0xffff;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000186 } else {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000187 for (i = 0; i < blockLength; i++)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000188 expected += blockBuf[i];
189 expected &= 0xff;
190 }
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200191 if (cksum_or_crc != expected) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000192 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
193 : "checksum error, expected 0x%02x, got 0x%02x",
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200194 expected, cksum_or_crc);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000195 goto error;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000196 }
197
198 wantBlockNo++;
199 length += blockLength;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000200 next:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000201 errors = 0;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200202 reply_char = ACK;
203 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000204 continue;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000205 error:
206 timeout:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000207 errors++;
208 if (errors == MAXERRORS) {
209 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000210
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200211 /* If were asking for crc, try again w/o crc */
212 if (reply_char == 'C') {
213 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000214 errors = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000215 do_crc = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000216 goto timeout;
217 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000218 bb_error_msg("too many errors; giving up");
219 fatal:
220 /* 5 CAN followed by 5 BS. Don't try too hard... */
221 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000222 return -1;
223 }
224
Rob Landley399d45f2006-06-21 01:49:17 +0000225 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000226 tcflush(read_fd, TCIFLUSH);
Rob Landley399d45f2006-06-21 01:49:17 +0000227
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200228 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000229 } /* for (;;) */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000230}
231
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000232static void sigalrm_handler(int UNUSED_PARAM signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000233{
234}
235
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000236int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100237int rx_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000238{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000239 struct termios tty, orig_tty;
240 int termios_err;
241 int file_fd;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000242 int n;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000243
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000244 /* Disabled by vda:
245 * why we can't receive from stdin? Why we *require*
246 * controlling tty?? */
247 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100248 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000249
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000250 termios_err = tcgetattr(read_fd, &tty);
251 if (termios_err == 0) {
252 orig_tty = tty;
253 cfmakeraw(&tty);
254 tcsetattr(read_fd, TCSAFLUSH, &tty);
255 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000256
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000257 /* No SA_RESTART: we want ALRM to interrupt read() */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000258 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000259
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000260 n = receive(file_fd);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000261
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000262 if (termios_err == 0)
263 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
264 if (ENABLE_FEATURE_CLEAN_UP)
265 close(file_fd);
266 fflush_stdout_and_exit(n >= 0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000267}