blob: 1dffb593afc70dfe771b9717c822d659f9a172e2 [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
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage:#define rx_trivial_usage
19//usage: "FILE"
20//usage:#define rx_full_usage "\n\n"
21//usage: "Receive a file using the xmodem protocol"
22//usage:
23//usage:#define rx_example_usage
24//usage: "$ rx /tmp/foo\n"
25
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000027
28#define SOH 0x01
29#define STX 0x02
30#define EOT 0x04
31#define ACK 0x06
32#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000033#define BS 0x08
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020034#define PAD 0x1A
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000035
36/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000037Cf:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000038 http://www.textfiles.com/apple/xmodem
39 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
40 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
41 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000042*/
43
44#define TIMEOUT 1
45#define TIMEOUT_LONG 10
46#define MAXERRORS 10
47
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000048#define read_fd STDIN_FILENO
49#define write_fd STDOUT_FILENO
50
51static int read_byte(unsigned timeout)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000052{
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020053 unsigned char buf;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000054 int n;
55
56 alarm(timeout);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000057 /* NOT safe_read! We want ALRM to interrupt us */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020058 n = read(read_fd, &buf, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000059 alarm(0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000060 if (n == 1)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020061 return buf;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000062 return -1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000063}
64
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000065static int receive(/*int read_fd, */int file_fd)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000066{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000067 unsigned char blockBuf[1024];
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020068 unsigned blockLength = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000069 unsigned errors = 0;
70 unsigned wantBlockNo = 1;
71 unsigned length = 0;
72 int do_crc = 1;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020073 char reply_char;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000074 unsigned timeout = TIMEOUT_LONG;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000075
Rob Landley399d45f2006-06-21 01:49:17 +000076 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000077 tcflush(read_fd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000078
79 /* Ask for CRC; if we get errors, we will go with checksum */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020080 reply_char = 'C';
81 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000082
83 for (;;) {
84 int blockBegin;
85 int blockNo, blockNoOnesCompl;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020086 int cksum_or_crc;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000087 int expected;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020088 int i, j;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000089
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000090 blockBegin = read_byte(timeout);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000091 if (blockBegin < 0)
92 goto timeout;
93
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020094 /* If last block, remove padding */
95 if (blockBegin == EOT) {
96 /* Data blocks can be padded with ^Z characters */
97 /* This code tries to detect and remove them */
98 if (blockLength >= 3
99 && blockBuf[blockLength - 1] == PAD
100 && blockBuf[blockLength - 2] == PAD
101 && blockBuf[blockLength - 3] == PAD
102 ) {
103 while (blockLength
104 && blockBuf[blockLength - 1] == PAD
105 ) {
106 blockLength--;
107 }
108 }
109 }
110 /* Write previously received block */
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100111 errno = 0;
112 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
113 bb_perror_msg(bb_msg_write_error);
114 goto fatal;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200115 }
116
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000117 timeout = TIMEOUT;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200118 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000119
120 switch (blockBegin) {
121 case SOH:
122 case STX:
123 break;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000124 case EOT:
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200125 reply_char = ACK;
126 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000127 return length;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000128 default:
129 goto error;
130 }
131
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200132 /* Block no */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000133 blockNo = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000134 if (blockNo < 0)
135 goto timeout;
136
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200137 /* Block no, in one's complement form */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000138 blockNoOnesCompl = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000139 if (blockNoOnesCompl < 0)
140 goto timeout;
141
142 if (blockNo != (255 - blockNoOnesCompl)) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000143 bb_error_msg("bad block ones compl");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000144 goto error;
145 }
146
147 blockLength = (blockBegin == SOH) ? 128 : 1024;
148
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000149 for (i = 0; i < blockLength; i++) {
150 int cc = read_byte(TIMEOUT);
151 if (cc < 0)
152 goto timeout;
153 blockBuf[i] = cc;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000154 }
155
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100156 cksum_or_crc = read_byte(TIMEOUT);
157 if (cksum_or_crc < 0)
158 goto timeout;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000159 if (do_crc) {
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200160 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
161 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000162 goto timeout;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000163 }
164
165 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
166 /* a repeat of the last block is ok, just ignore it. */
167 /* this also ignores the initial block 0 which is */
168 /* meta data. */
Dan Fandrichf808e772011-11-03 10:18:33 +0100169 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000170 goto next;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000171 }
172 if (blockNo != (wantBlockNo & 0xff)) {
173 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000174 goto error;
175 }
176
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000177 expected = 0;
178 if (do_crc) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000179 for (i = 0; i < blockLength; i++) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000180 expected = expected ^ blockBuf[i] << 8;
181 for (j = 0; j < 8; j++) {
182 if (expected & 0x8000)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200183 expected = (expected << 1) ^ 0x1021;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000184 else
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200185 expected = (expected << 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000186 }
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000187 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000188 expected &= 0xffff;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000189 } else {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000190 for (i = 0; i < blockLength; i++)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000191 expected += blockBuf[i];
192 expected &= 0xff;
193 }
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200194 if (cksum_or_crc != expected) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000195 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100196 : "checksum error, expected 0x%02x, got 0x%02x",
197 expected, cksum_or_crc);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000198 goto error;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000199 }
200
201 wantBlockNo++;
202 length += blockLength;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000203 next:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000204 errors = 0;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200205 reply_char = ACK;
206 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000207 continue;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000208 error:
209 timeout:
Daniel Fandrich65a1ee92011-11-23 12:07:31 +0100210 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000211 errors++;
212 if (errors == MAXERRORS) {
213 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000214
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200215 /* If were asking for crc, try again w/o crc */
216 if (reply_char == 'C') {
217 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000218 errors = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000219 do_crc = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000220 goto timeout;
221 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000222 bb_error_msg("too many errors; giving up");
223 fatal:
224 /* 5 CAN followed by 5 BS. Don't try too hard... */
225 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000226 return -1;
227 }
228
Rob Landley399d45f2006-06-21 01:49:17 +0000229 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000230 tcflush(read_fd, TCIFLUSH);
Rob Landley399d45f2006-06-21 01:49:17 +0000231
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200232 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000233 } /* for (;;) */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000234}
235
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000236static void sigalrm_handler(int UNUSED_PARAM signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000237{
238}
239
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000240int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100241int rx_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000242{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000243 struct termios tty, orig_tty;
244 int termios_err;
245 int file_fd;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000246 int n;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000247
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000248 /* Disabled by vda:
249 * why we can't receive from stdin? Why we *require*
250 * controlling tty?? */
251 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100252 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000253
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000254 termios_err = tcgetattr(read_fd, &tty);
255 if (termios_err == 0) {
256 orig_tty = tty;
257 cfmakeraw(&tty);
258 tcsetattr(read_fd, TCSAFLUSH, &tty);
259 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000260
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000261 /* No SA_RESTART: we want ALRM to interrupt read() */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000262 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000263
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000264 n = receive(file_fd);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000265
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000266 if (termios_err == 0)
267 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
268 if (ENABLE_FEATURE_CLEAN_UP)
269 close(file_fd);
270 fflush_stdout_and_exit(n >= 0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000271}