2006-04-13 18:15:04 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2009-08-02 23:15:31 +05:30
|
|
|
/*
|
2003-12-20 13:00:35 +05:30
|
|
|
* Copyright: Copyright (C) 2001, Hewlett-Packard Company
|
|
|
|
* Author: Christopher Hoover <ch@hpl.hp.com>
|
2004-03-15 13:59:22 +05:30
|
|
|
* Description: xmodem functionality for uploading of kernels
|
2003-12-20 13:00:35 +05:30
|
|
|
* and the like
|
|
|
|
* Created at: Thu Dec 20 01:58:08 PST 2001
|
2009-08-02 23:15:31 +05:30
|
|
|
*
|
|
|
|
* xmodem functionality for uploading of kernels and the like
|
2003-12-20 13:00:35 +05:30
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Hewlett-Packard Laboratories
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2003-12-20 13:00:35 +05:30
|
|
|
*
|
|
|
|
* This was originally written for blob and then adapted for busybox.
|
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config RX
|
|
|
|
//config: bool "rx"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
|
|
|
//config: Receive files using the Xmodem protocol.
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_RX) += rx.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define rx_trivial_usage
|
|
|
|
//usage: "FILE"
|
|
|
|
//usage:#define rx_full_usage "\n\n"
|
|
|
|
//usage: "Receive a file using the xmodem protocol"
|
|
|
|
//usage:
|
|
|
|
//usage:#define rx_example_usage
|
|
|
|
//usage: "$ rx /tmp/foo\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2003-12-20 13:00:35 +05:30
|
|
|
|
|
|
|
#define SOH 0x01
|
|
|
|
#define STX 0x02
|
|
|
|
#define EOT 0x04
|
|
|
|
#define ACK 0x06
|
|
|
|
#define NAK 0x15
|
|
|
|
#define BS 0x08
|
2009-08-02 23:15:31 +05:30
|
|
|
#define PAD 0x1A
|
2003-12-20 13:00:35 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
Cf:
|
|
|
|
http://www.textfiles.com/apple/xmodem
|
|
|
|
http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
|
|
|
|
http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
|
|
|
|
http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TIMEOUT 1
|
|
|
|
#define TIMEOUT_LONG 10
|
|
|
|
#define MAXERRORS 10
|
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
#define read_fd STDIN_FILENO
|
|
|
|
#define write_fd STDOUT_FILENO
|
|
|
|
|
|
|
|
static int read_byte(unsigned timeout)
|
2007-04-17 04:02:04 +05:30
|
|
|
{
|
2009-08-02 23:15:31 +05:30
|
|
|
unsigned char buf;
|
2003-12-20 13:00:35 +05:30
|
|
|
int n;
|
|
|
|
|
|
|
|
alarm(timeout);
|
2008-03-13 04:43:50 +05:30
|
|
|
/* NOT safe_read! We want ALRM to interrupt us */
|
2009-08-02 23:15:31 +05:30
|
|
|
n = read(read_fd, &buf, 1);
|
2003-12-20 13:00:35 +05:30
|
|
|
alarm(0);
|
|
|
|
if (n == 1)
|
2009-08-02 23:15:31 +05:30
|
|
|
return buf;
|
2008-03-13 04:43:50 +05:30
|
|
|
return -1;
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
static int receive(/*int read_fd, */int file_fd)
|
2003-12-20 13:00:35 +05:30
|
|
|
{
|
2008-03-13 04:43:50 +05:30
|
|
|
unsigned char blockBuf[1024];
|
2009-08-02 23:15:31 +05:30
|
|
|
unsigned blockLength = 0;
|
2008-03-13 04:43:50 +05:30
|
|
|
unsigned errors = 0;
|
|
|
|
unsigned wantBlockNo = 1;
|
|
|
|
unsigned length = 0;
|
|
|
|
int do_crc = 1;
|
2009-08-02 23:15:31 +05:30
|
|
|
char reply_char;
|
2008-03-13 04:43:50 +05:30
|
|
|
unsigned timeout = TIMEOUT_LONG;
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2006-06-21 07:19:17 +05:30
|
|
|
/* Flush pending input */
|
2008-03-13 04:43:50 +05:30
|
|
|
tcflush(read_fd, TCIFLUSH);
|
2003-12-20 13:00:35 +05:30
|
|
|
|
|
|
|
/* Ask for CRC; if we get errors, we will go with checksum */
|
2009-08-02 23:15:31 +05:30
|
|
|
reply_char = 'C';
|
|
|
|
full_write(write_fd, &reply_char, 1);
|
2003-12-20 13:00:35 +05:30
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int blockBegin;
|
|
|
|
int blockNo, blockNoOnesCompl;
|
2009-08-02 23:15:31 +05:30
|
|
|
int cksum_or_crc;
|
2017-02-02 00:05:13 +05:30
|
|
|
unsigned expected;
|
2009-08-02 23:15:31 +05:30
|
|
|
int i, j;
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
blockBegin = read_byte(timeout);
|
2003-12-20 13:00:35 +05:30
|
|
|
if (blockBegin < 0)
|
|
|
|
goto timeout;
|
|
|
|
|
2009-08-02 23:15:31 +05:30
|
|
|
/* If last block, remove padding */
|
|
|
|
if (blockBegin == EOT) {
|
|
|
|
/* Data blocks can be padded with ^Z characters */
|
|
|
|
/* This code tries to detect and remove them */
|
|
|
|
if (blockLength >= 3
|
|
|
|
&& blockBuf[blockLength - 1] == PAD
|
|
|
|
&& blockBuf[blockLength - 2] == PAD
|
|
|
|
&& blockBuf[blockLength - 3] == PAD
|
|
|
|
) {
|
|
|
|
while (blockLength
|
2016-11-28 05:52:57 +05:30
|
|
|
&& blockBuf[blockLength - 1] == PAD
|
2009-08-02 23:15:31 +05:30
|
|
|
) {
|
|
|
|
blockLength--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Write previously received block */
|
2011-11-03 14:49:53 +05:30
|
|
|
errno = 0;
|
|
|
|
if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
|
|
|
|
bb_perror_msg(bb_msg_write_error);
|
|
|
|
goto fatal;
|
2009-08-02 23:15:31 +05:30
|
|
|
}
|
|
|
|
|
2003-12-20 13:00:35 +05:30
|
|
|
timeout = TIMEOUT;
|
2009-08-02 23:15:31 +05:30
|
|
|
reply_char = NAK;
|
2003-12-20 13:00:35 +05:30
|
|
|
|
|
|
|
switch (blockBegin) {
|
|
|
|
case SOH:
|
|
|
|
case STX:
|
|
|
|
break;
|
|
|
|
case EOT:
|
2009-08-02 23:15:31 +05:30
|
|
|
reply_char = ACK;
|
|
|
|
full_write(write_fd, &reply_char, 1);
|
2008-03-13 04:43:50 +05:30
|
|
|
return length;
|
2003-12-20 13:00:35 +05:30
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-08-02 23:15:31 +05:30
|
|
|
/* Block no */
|
2008-03-13 04:43:50 +05:30
|
|
|
blockNo = read_byte(TIMEOUT);
|
2003-12-20 13:00:35 +05:30
|
|
|
if (blockNo < 0)
|
|
|
|
goto timeout;
|
|
|
|
|
2009-08-02 23:15:31 +05:30
|
|
|
/* Block no, in one's complement form */
|
2008-03-13 04:43:50 +05:30
|
|
|
blockNoOnesCompl = read_byte(TIMEOUT);
|
2003-12-20 13:00:35 +05:30
|
|
|
if (blockNoOnesCompl < 0)
|
|
|
|
goto timeout;
|
|
|
|
|
|
|
|
if (blockNo != (255 - blockNoOnesCompl)) {
|
2008-03-13 04:43:50 +05:30
|
|
|
bb_error_msg("bad block ones compl");
|
2003-12-20 13:00:35 +05:30
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockLength = (blockBegin == SOH) ? 128 : 1024;
|
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
for (i = 0; i < blockLength; i++) {
|
|
|
|
int cc = read_byte(TIMEOUT);
|
|
|
|
if (cc < 0)
|
|
|
|
goto timeout;
|
|
|
|
blockBuf[i] = cc;
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|
|
|
|
|
2011-11-03 14:49:53 +05:30
|
|
|
cksum_or_crc = read_byte(TIMEOUT);
|
|
|
|
if (cksum_or_crc < 0)
|
|
|
|
goto timeout;
|
2008-03-13 04:43:50 +05:30
|
|
|
if (do_crc) {
|
2009-08-02 23:15:31 +05:30
|
|
|
cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
|
|
|
|
if (cksum_or_crc < 0)
|
2003-12-20 13:00:35 +05:30
|
|
|
goto timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockNo == ((wantBlockNo - 1) & 0xff)) {
|
|
|
|
/* a repeat of the last block is ok, just ignore it. */
|
|
|
|
/* this also ignores the initial block 0 which is */
|
|
|
|
/* meta data. */
|
2011-11-03 14:48:33 +05:30
|
|
|
blockLength = 0;
|
2003-12-20 13:00:35 +05:30
|
|
|
goto next;
|
2008-03-13 04:43:50 +05:30
|
|
|
}
|
|
|
|
if (blockNo != (wantBlockNo & 0xff)) {
|
|
|
|
bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
|
2003-12-20 13:00:35 +05:30
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
expected = 0;
|
|
|
|
if (do_crc) {
|
2003-12-20 13:00:35 +05:30
|
|
|
for (i = 0; i < blockLength; i++) {
|
2008-03-13 04:43:50 +05:30
|
|
|
expected = expected ^ blockBuf[i] << 8;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (expected & 0x8000)
|
2009-08-02 23:15:31 +05:30
|
|
|
expected = (expected << 1) ^ 0x1021;
|
2003-12-20 13:00:35 +05:30
|
|
|
else
|
2009-08-02 23:15:31 +05:30
|
|
|
expected = (expected << 1);
|
2008-03-13 04:43:50 +05:30
|
|
|
}
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|
2008-03-13 04:43:50 +05:30
|
|
|
expected &= 0xffff;
|
2003-12-20 13:00:35 +05:30
|
|
|
} else {
|
|
|
|
for (i = 0; i < blockLength; i++)
|
2008-03-13 04:43:50 +05:30
|
|
|
expected += blockBuf[i];
|
|
|
|
expected &= 0xff;
|
|
|
|
}
|
2009-08-02 23:15:31 +05:30
|
|
|
if (cksum_or_crc != expected) {
|
2008-03-13 04:43:50 +05:30
|
|
|
bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
|
2013-01-15 18:28:01 +05:30
|
|
|
: "checksum error, expected 0x%02x, got 0x%02x",
|
|
|
|
expected, cksum_or_crc);
|
2008-03-13 04:43:50 +05:30
|
|
|
goto error;
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
wantBlockNo++;
|
|
|
|
length += blockLength;
|
2008-03-13 04:43:50 +05:30
|
|
|
next:
|
2003-12-20 13:00:35 +05:30
|
|
|
errors = 0;
|
2009-08-02 23:15:31 +05:30
|
|
|
reply_char = ACK;
|
|
|
|
full_write(write_fd, &reply_char, 1);
|
2003-12-20 13:00:35 +05:30
|
|
|
continue;
|
2008-03-13 04:43:50 +05:30
|
|
|
error:
|
|
|
|
timeout:
|
2011-11-23 16:37:31 +05:30
|
|
|
blockLength = 0;
|
2003-12-20 13:00:35 +05:30
|
|
|
errors++;
|
|
|
|
if (errors == MAXERRORS) {
|
|
|
|
/* Abort */
|
|
|
|
|
2009-08-02 23:15:31 +05:30
|
|
|
/* If were asking for crc, try again w/o crc */
|
|
|
|
if (reply_char == 'C') {
|
|
|
|
reply_char = NAK;
|
2003-12-20 13:00:35 +05:30
|
|
|
errors = 0;
|
2008-03-13 04:43:50 +05:30
|
|
|
do_crc = 0;
|
2003-12-20 13:00:35 +05:30
|
|
|
goto timeout;
|
|
|
|
}
|
2008-03-13 04:43:50 +05:30
|
|
|
bb_error_msg("too many errors; giving up");
|
|
|
|
fatal:
|
|
|
|
/* 5 CAN followed by 5 BS. Don't try too hard... */
|
|
|
|
safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
|
2003-12-20 13:00:35 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-21 07:19:17 +05:30
|
|
|
/* Flush pending input */
|
2008-03-13 04:43:50 +05:30
|
|
|
tcflush(read_fd, TCIFLUSH);
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2009-08-02 23:15:31 +05:30
|
|
|
full_write(write_fd, &reply_char, 1);
|
2008-03-13 04:43:50 +05:30
|
|
|
} /* for (;;) */
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sigalrm_handler(int UNUSED_PARAM signum)
|
2003-12-20 13:00:35 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2009-11-28 19:48:53 +05:30
|
|
|
int rx_main(int argc UNUSED_PARAM, char **argv)
|
2003-12-20 13:00:35 +05:30
|
|
|
{
|
2008-03-13 04:43:50 +05:30
|
|
|
struct termios tty, orig_tty;
|
|
|
|
int termios_err;
|
|
|
|
int file_fd;
|
2003-12-20 13:00:35 +05:30
|
|
|
int n;
|
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
/* Disabled by vda:
|
|
|
|
* why we can't receive from stdin? Why we *require*
|
|
|
|
* controlling tty?? */
|
|
|
|
/*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
|
2009-11-28 19:48:53 +05:30
|
|
|
file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
|
2008-03-13 04:43:50 +05:30
|
|
|
|
|
|
|
termios_err = tcgetattr(read_fd, &tty);
|
|
|
|
if (termios_err == 0) {
|
2017-01-11 20:47:59 +05:30
|
|
|
//TODO: use set_termios_to_raw()
|
2008-03-13 04:43:50 +05:30
|
|
|
orig_tty = tty;
|
|
|
|
cfmakeraw(&tty);
|
|
|
|
tcsetattr(read_fd, TCSAFLUSH, &tty);
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
/* No SA_RESTART: we want ALRM to interrupt read() */
|
2008-03-13 04:49:35 +05:30
|
|
|
signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
n = receive(file_fd);
|
2003-12-20 13:00:35 +05:30
|
|
|
|
2008-03-13 04:43:50 +05:30
|
|
|
if (termios_err == 0)
|
|
|
|
tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(file_fd);
|
|
|
|
fflush_stdout_and_exit(n >= 0);
|
2003-12-20 13:00:35 +05:30
|
|
|
}
|