blob: d8b04148014a371cbc4eff6287a0b4eb140c3bdb [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
Denys Vlasenkofb4da162016-11-22 23:14:24 +010020//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: Receive files using the Xmodem protocol.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000022
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010023//applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP))
24
25//kbuild:lib-$(CONFIG_RX) += rx.o
26
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage:#define rx_trivial_usage
28//usage: "FILE"
29//usage:#define rx_full_usage "\n\n"
30//usage: "Receive a file using the xmodem protocol"
31//usage:
32//usage:#define rx_example_usage
33//usage: "$ rx /tmp/foo\n"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000036
37#define SOH 0x01
38#define STX 0x02
39#define EOT 0x04
40#define ACK 0x06
41#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000042#define BS 0x08
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020043#define PAD 0x1A
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000044
45/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000046Cf:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000047 http://www.textfiles.com/apple/xmodem
48 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
49 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
50 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000051*/
52
53#define TIMEOUT 1
54#define TIMEOUT_LONG 10
55#define MAXERRORS 10
56
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000057#define read_fd STDIN_FILENO
58#define write_fd STDOUT_FILENO
59
60static int read_byte(unsigned timeout)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000061{
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020062 unsigned char buf;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000063 int n;
64
65 alarm(timeout);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000066 /* NOT safe_read! We want ALRM to interrupt us */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020067 n = read(read_fd, &buf, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000068 alarm(0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000069 if (n == 1)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020070 return buf;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000071 return -1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000072}
73
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000074static int receive(/*int read_fd, */int file_fd)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000075{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000076 unsigned char blockBuf[1024];
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020077 unsigned blockLength = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000078 unsigned errors = 0;
79 unsigned wantBlockNo = 1;
80 unsigned length = 0;
81 int do_crc = 1;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020082 char reply_char;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000083 unsigned timeout = TIMEOUT_LONG;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000084
Rob Landley399d45f2006-06-21 01:49:17 +000085 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000086 tcflush(read_fd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000087
88 /* Ask for CRC; if we get errors, we will go with checksum */
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020089 reply_char = 'C';
90 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000091
92 for (;;) {
93 int blockBegin;
94 int blockNo, blockNoOnesCompl;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020095 int cksum_or_crc;
Rostislav Skudnov87625122017-02-01 18:35:13 +000096 unsigned expected;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +020097 int i, j;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000098
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000099 blockBegin = read_byte(timeout);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000100 if (blockBegin < 0)
101 goto timeout;
102
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200103 /* If last block, remove padding */
104 if (blockBegin == EOT) {
105 /* Data blocks can be padded with ^Z characters */
106 /* This code tries to detect and remove them */
107 if (blockLength >= 3
108 && blockBuf[blockLength - 1] == PAD
109 && blockBuf[blockLength - 2] == PAD
110 && blockBuf[blockLength - 3] == PAD
111 ) {
112 while (blockLength
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100113 && blockBuf[blockLength - 1] == PAD
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200114 ) {
115 blockLength--;
116 }
117 }
118 }
119 /* Write previously received block */
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100120 errno = 0;
121 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
James Byrne69374872019-07-02 11:35:03 +0200122 bb_simple_perror_msg(bb_msg_write_error);
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100123 goto fatal;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200124 }
125
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000126 timeout = TIMEOUT;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200127 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000128
129 switch (blockBegin) {
130 case SOH:
131 case STX:
132 break;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000133 case EOT:
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200134 reply_char = ACK;
135 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000136 return length;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000137 default:
138 goto error;
139 }
140
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200141 /* Block no */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000142 blockNo = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000143 if (blockNo < 0)
144 goto timeout;
145
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200146 /* Block no, in one's complement form */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000147 blockNoOnesCompl = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000148 if (blockNoOnesCompl < 0)
149 goto timeout;
150
151 if (blockNo != (255 - blockNoOnesCompl)) {
James Byrne69374872019-07-02 11:35:03 +0200152 bb_simple_error_msg("bad block ones compl");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000153 goto error;
154 }
155
156 blockLength = (blockBegin == SOH) ? 128 : 1024;
157
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000158 for (i = 0; i < blockLength; i++) {
159 int cc = read_byte(TIMEOUT);
160 if (cc < 0)
161 goto timeout;
162 blockBuf[i] = cc;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000163 }
164
Denys Vlasenkof3efd3c2011-11-03 10:19:53 +0100165 cksum_or_crc = read_byte(TIMEOUT);
166 if (cksum_or_crc < 0)
167 goto timeout;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000168 if (do_crc) {
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200169 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
170 if (cksum_or_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000171 goto timeout;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000172 }
173
174 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
175 /* a repeat of the last block is ok, just ignore it. */
176 /* this also ignores the initial block 0 which is */
177 /* meta data. */
Dan Fandrichf808e772011-11-03 10:18:33 +0100178 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000179 goto next;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000180 }
181 if (blockNo != (wantBlockNo & 0xff)) {
182 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000183 goto error;
184 }
185
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000186 expected = 0;
187 if (do_crc) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000188 for (i = 0; i < blockLength; i++) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000189 expected = expected ^ blockBuf[i] << 8;
190 for (j = 0; j < 8; j++) {
191 if (expected & 0x8000)
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200192 expected = (expected << 1) ^ 0x1021;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000193 else
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200194 expected = (expected << 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000195 }
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000196 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000197 expected &= 0xffff;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000198 } else {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000199 for (i = 0; i < blockLength; i++)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000200 expected += blockBuf[i];
201 expected &= 0xff;
202 }
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200203 if (cksum_or_crc != expected) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000204 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100205 : "checksum error, expected 0x%02x, got 0x%02x",
206 expected, cksum_or_crc);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000207 goto error;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000208 }
209
210 wantBlockNo++;
211 length += blockLength;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000212 next:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000213 errors = 0;
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200214 reply_char = ACK;
215 full_write(write_fd, &reply_char, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000216 continue;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000217 error:
218 timeout:
Daniel Fandrich65a1ee92011-11-23 12:07:31 +0100219 blockLength = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000220 errors++;
221 if (errors == MAXERRORS) {
222 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000223
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200224 /* If were asking for crc, try again w/o crc */
225 if (reply_char == 'C') {
226 reply_char = NAK;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000227 errors = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000228 do_crc = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000229 goto timeout;
230 }
James Byrne69374872019-07-02 11:35:03 +0200231 bb_simple_error_msg("too many errors; giving up");
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000232 fatal:
233 /* 5 CAN followed by 5 BS. Don't try too hard... */
234 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000235 return -1;
236 }
237
Rob Landley399d45f2006-06-21 01:49:17 +0000238 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000239 tcflush(read_fd, TCIFLUSH);
Rob Landley399d45f2006-06-21 01:49:17 +0000240
Denys Vlasenkodc9495d2009-08-02 19:45:31 +0200241 full_write(write_fd, &reply_char, 1);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000242 } /* for (;;) */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000243}
244
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000245static void sigalrm_handler(int UNUSED_PARAM signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000246{
247}
248
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000249int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100250int rx_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000251{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000252 struct termios tty, orig_tty;
253 int termios_err;
254 int file_fd;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000255 int n;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000256
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000257 /* Disabled by vda:
258 * why we can't receive from stdin? Why we *require*
259 * controlling tty?? */
260 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100261 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000262
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000263 termios_err = tcgetattr(read_fd, &tty);
264 if (termios_err == 0) {
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100265//TODO: use set_termios_to_raw()
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000266 orig_tty = tty;
267 cfmakeraw(&tty);
268 tcsetattr(read_fd, TCSAFLUSH, &tty);
269 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000270
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000271 /* No SA_RESTART: we want ALRM to interrupt read() */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000272 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000273
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000274 n = receive(file_fd);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000275
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000276 if (termios_err == 0)
277 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
278 if (ENABLE_FEATURE_CLEAN_UP)
279 close(file_fd);
280 fflush_stdout_and_exit(n >= 0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000281}