blob: 319ec1d497c115f392028c4adcb61e77f0ec6137 [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 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010017//config:config RX
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020018//config: bool "rx (2.9 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010019//config: default y
20//config: select PLATFORM_LINUX
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Receive files using the Xmodem protocol.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000023
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010024//applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP))
25
26//kbuild:lib-$(CONFIG_RX) += rx.o
27
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage:#define rx_trivial_usage
29//usage: "FILE"
30//usage:#define rx_full_usage "\n\n"
31//usage: "Receive a file using the xmodem protocol"
32//usage:
33//usage:#define rx_example_usage
34//usage: "$ rx /tmp/foo\n"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000037
38#define SOH 0x01
39#define STX 0x02
40#define EOT 0x04
41#define ACK 0x06
42#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000043#define BS 0x08
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020044#define PAD 0x1A
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000045
46/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000047Cf:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000048 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
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000052*/
53
54#define TIMEOUT 1
55#define TIMEOUT_LONG 10
56#define MAXERRORS 10
57
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000058#define read_fd STDIN_FILENO
59#define write_fd STDOUT_FILENO
60
61static int read_byte(unsigned timeout)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000062{
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020063 unsigned char buf;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000064 int n;
65
66 alarm(timeout);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000067 /* NOT safe_read! We want ALRM to interrupt us */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020068 n = read(read_fd, &buf, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000069 alarm(0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000070 if (n == 1)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020071 return buf;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000072 return -1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000073}
74
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000075static int receive(/*int read_fd, */int file_fd)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000076{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000077 unsigned char blockBuf[1024];
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020078 unsigned blockLength = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000079 unsigned errors = 0;
80 unsigned wantBlockNo = 1;
81 unsigned length = 0;
82 int do_crc = 1;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020083 char reply_char;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000084 unsigned timeout = TIMEOUT_LONG;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000085
Rob Landley399d45f2006-06-21 01:49:17 +000086 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000087 tcflush(read_fd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000088
89 /* Ask for CRC; if we get errors, we will go with checksum */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020090 reply_char = 'C';
91 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000092
93 for (;;) {
94 int blockBegin;
95 int blockNo, blockNoOnesCompl;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020096 int cksum_or_crc;
Rostislav Skudnov87625122017-02-01 18:35:13 +000097 unsigned expected;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020098 int i, j;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000099
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000100 blockBegin = read_byte(timeout);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000101 if (blockBegin < 0)
102 goto timeout;
103
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200104 /* If last block, remove padding */
105 if (blockBegin == EOT) {
106 /* Data blocks can be padded with ^Z characters */
107 /* This code tries to detect and remove them */
108 if (blockLength >= 3
109 && blockBuf[blockLength - 1] == PAD
110 && blockBuf[blockLength - 2] == PAD
111 && blockBuf[blockLength - 3] == PAD
112 ) {
113 while (blockLength
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100114 && blockBuf[blockLength - 1] == PAD
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200115 ) {
116 blockLength--;
117 }
118 }
119 }
120 /* Write previously received block */
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100121 errno = 0;
122 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
James Byrne69374872019-07-02 11:35:03 +0200123 bb_simple_perror_msg(bb_msg_write_error);
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100124 goto fatal;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200125 }
126
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000127 timeout = TIMEOUT;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200128 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000129
130 switch (blockBegin) {
131 case SOH:
132 case STX:
133 break;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000134 case EOT:
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200135 reply_char = ACK;
136 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000137 return length;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000138 default:
139 goto error;
140 }
141
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200142 /* Block no */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000143 blockNo = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000144 if (blockNo < 0)
145 goto timeout;
146
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200147 /* Block no, in one's complement form */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000148 blockNoOnesCompl = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000149 if (blockNoOnesCompl < 0)
150 goto timeout;
151
152 if (blockNo != (255 - blockNoOnesCompl)) {
James Byrne69374872019-07-02 11:35:03 +0200153 bb_simple_error_msg("bad block ones compl");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000154 goto error;
155 }
156
157 blockLength = (blockBegin == SOH) ? 128 : 1024;
158
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000159 for (i = 0; i < blockLength; i++) {
160 int cc = read_byte(TIMEOUT);
161 if (cc < 0)
162 goto timeout;
163 blockBuf[i] = cc;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000164 }
165
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100166 cksum_or_crc = read_byte(TIMEOUT);
167 if (cksum_or_crc < 0)
168 goto timeout;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000169 if (do_crc) {
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200170 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
171 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000172 goto timeout;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000173 }
174
175 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
176 /* a repeat of the last block is ok, just ignore it. */
177 /* this also ignores the initial block 0 which is */
178 /* meta data. */
Dan Fandrichf808e772011-11-03 10:18:33 +0100179 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000180 goto next;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000181 }
182 if (blockNo != (wantBlockNo & 0xff)) {
183 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000184 goto error;
185 }
186
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000187 expected = 0;
188 if (do_crc) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000189 for (i = 0; i < blockLength; i++) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000190 expected = expected ^ blockBuf[i] << 8;
191 for (j = 0; j < 8; j++) {
192 if (expected & 0x8000)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200193 expected = (expected << 1) ^ 0x1021;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000194 else
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200195 expected = (expected << 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000196 }
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000197 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000198 expected &= 0xffff;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000199 } else {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000200 for (i = 0; i < blockLength; i++)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000201 expected += blockBuf[i];
202 expected &= 0xff;
203 }
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200204 if (cksum_or_crc != expected) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000205 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100206 : "checksum error, expected 0x%02x, got 0x%02x",
207 expected, cksum_or_crc);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000208 goto error;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000209 }
210
211 wantBlockNo++;
212 length += blockLength;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000213 next:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000214 errors = 0;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200215 reply_char = ACK;
216 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000217 continue;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000218 error:
219 timeout:
Daniel Fandrich65a1ee92011-11-23 12:07:31 +0100220 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000221 errors++;
222 if (errors == MAXERRORS) {
223 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000224
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200225 /* If were asking for crc, try again w/o crc */
226 if (reply_char == 'C') {
227 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000228 errors = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000229 do_crc = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000230 goto timeout;
231 }
James Byrne69374872019-07-02 11:35:03 +0200232 bb_simple_error_msg("too many errors; giving up");
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000233 fatal:
234 /* 5 CAN followed by 5 BS. Don't try too hard... */
235 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000236 return -1;
237 }
238
Rob Landley399d45f2006-06-21 01:49:17 +0000239 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000240 tcflush(read_fd, TCIFLUSH);
Rob Landley399d45f2006-06-21 01:49:17 +0000241
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200242 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000243 } /* for (;;) */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000244}
245
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000246static void sigalrm_handler(int UNUSED_PARAM signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000247{
248}
249
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000250int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100251int rx_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000252{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000253 struct termios tty, orig_tty;
254 int termios_err;
255 int file_fd;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000256 int n;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000257
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000258 /* Disabled by vda:
259 * why we can't receive from stdin? Why we *require*
260 * controlling tty?? */
261 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100262 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000263
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000264 termios_err = tcgetattr(read_fd, &tty);
265 if (termios_err == 0) {
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100266//TODO: use set_termios_to_raw()
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000267 orig_tty = tty;
268 cfmakeraw(&tty);
269 tcsetattr(read_fd, TCSAFLUSH, &tty);
270 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000271
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000272 /* No SA_RESTART: we want ALRM to interrupt read() */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000273 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000274
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000275 n = receive(file_fd);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000276
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000277 if (termios_err == 0)
278 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
279 if (ENABLE_FEATURE_CLEAN_UP)
280 close(file_fd);
281 fflush_stdout_and_exit(n >= 0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000282}