X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Frx.c;h=874a3f0a33a5667abbaef568212bd1161bb68d02;hb=363fb5ec4091120003b3f204996e7ff0aa354e58;hp=2b16f2e220ff22699f4f64fb8c657ef64f94005a;hpb=8f3bc4ce52448a6d1c188055d6920e5bb05334b1;p=oweals%2Fbusybox.git diff --git a/miscutils/rx.c b/miscutils/rx.c index 2b16f2e22..874a3f0a3 100644 --- a/miscutils/rx.c +++ b/miscutils/rx.c @@ -1,190 +1,174 @@ -/*------------------------------------------------------------------------- - * Filename: xmodem.c - * Version: $Id: rx.c,v 1.1 2003/12/20 07:30:35 bug1 Exp $ +/* vi: set sw=4 ts=4: */ +/* * Copyright: Copyright (C) 2001, Hewlett-Packard Company * Author: Christopher Hoover - * Description: xmodem functionality for uploading of kernels + * Description: xmodem functionality for uploading of kernels * and the like * Created at: Thu Dec 20 01:58:08 PST 2001 - *-----------------------------------------------------------------------*/ -/* - * xmodem.c: xmodem functionality for uploading of kernels and - * the like - * - * Copyright (C) 2001 Hewlett-Packard Laboratories - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * xmodem functionality for uploading of kernels and the like * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Copyright (C) 2001 Hewlett-Packard Laboratories * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. * * This was originally written for blob and then adapted for busybox. - * */ +//config:config RX +//config: bool "rx (2.9 kb)" +//config: default y +//config: select PLATFORM_LINUX +//config: help +//config: Receive files using the Xmodem protocol. + +//applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_RX) += rx.o -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "busybox.h" +//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" +#include "libbb.h" #define SOH 0x01 #define STX 0x02 #define EOT 0x04 #define ACK 0x06 #define NAK 0x15 -#define CAN 0x18 #define BS 0x08 +#define PAD 0x1A /* - 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 -static inline void write_byte(int fd, char cc) { - write(fd, &cc, 1); -} - -static inline void write_flush(int fd) { - tcdrain(fd); -} +#define read_fd STDIN_FILENO +#define write_fd STDOUT_FILENO -static inline void read_flush(int fd) { - tcflush(fd, TCIFLUSH); -} - -static int read_byte(int fd, unsigned int timeout) { - char buf[1]; +static int read_byte(unsigned timeout) +{ + unsigned char buf; int n; alarm(timeout); - - n = read(fd, &buf, 1); - + /* NOT safe_read! We want ALRM to interrupt us */ + n = read(read_fd, &buf, 1); alarm(0); - if (n == 1) - return buf[0] & 0xff; - else - return -1; + return buf; + return -1; } -static int receive(char *error_buf, size_t error_buf_size, - int ttyfd, int filefd) +static int receive(/*int read_fd, */int file_fd) { - char blockBuf[1024]; - unsigned int errors = 0; - unsigned int wantBlockNo = 1; - unsigned int length = 0; - int docrc = 1; - char nak = 'C'; - unsigned int timeout = TIMEOUT_LONG; - -#define note_error(fmt,args...) \ - snprintf(error_buf, error_buf_size, fmt,##args) - - read_flush(ttyfd); + unsigned char blockBuf[1024]; + unsigned blockLength = 0; + unsigned errors = 0; + unsigned wantBlockNo = 1; + unsigned length = 0; + int do_crc = 1; + char reply_char; + unsigned timeout = TIMEOUT_LONG; + + /* Flush pending input */ + tcflush(read_fd, TCIFLUSH); /* Ask for CRC; if we get errors, we will go with checksum */ - write_byte(ttyfd, nak); - write_flush(ttyfd); + reply_char = 'C'; + full_write(write_fd, &reply_char, 1); for (;;) { int blockBegin; int blockNo, blockNoOnesCompl; - int blockLength; - int cksum = 0; - int crcHi = 0; - int crcLo = 0; + int cksum_or_crc; + unsigned expected; + int i, j; - blockBegin = read_byte(ttyfd, timeout); + blockBegin = read_byte(timeout); if (blockBegin < 0) goto timeout; + /* 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 + && blockBuf[blockLength - 1] == PAD + ) { + blockLength--; + } + } + } + /* Write previously received block */ + errno = 0; + if (full_write(file_fd, blockBuf, blockLength) != blockLength) { + bb_perror_msg(bb_msg_write_error); + goto fatal; + } + timeout = TIMEOUT; - nak = NAK; + reply_char = NAK; switch (blockBegin) { case SOH: case STX: break; - case EOT: - write_byte(ttyfd, ACK); - write_flush(ttyfd); - goto done; - + reply_char = ACK; + full_write(write_fd, &reply_char, 1); + return length; default: goto error; } - /* block no */ - blockNo = read_byte(ttyfd, TIMEOUT); + /* Block no */ + blockNo = read_byte(TIMEOUT); if (blockNo < 0) goto timeout; - /* block no one's compliment */ - blockNoOnesCompl = read_byte(ttyfd, TIMEOUT); + /* Block no, in one's complement form */ + blockNoOnesCompl = read_byte(TIMEOUT); if (blockNoOnesCompl < 0) goto timeout; if (blockNo != (255 - blockNoOnesCompl)) { - note_error("bad block ones compl"); + bb_error_msg("bad block ones compl"); goto error; } blockLength = (blockBegin == SOH) ? 128 : 1024; - { - int i; - - for (i = 0; i < blockLength; i++) { - int cc = read_byte(ttyfd, TIMEOUT); - if (cc < 0) - goto timeout; - blockBuf[i] = cc; - } - } - - if (docrc) { - crcHi = read_byte(ttyfd, TIMEOUT); - if (crcHi < 0) + for (i = 0; i < blockLength; i++) { + int cc = read_byte(TIMEOUT); + if (cc < 0) goto timeout; + blockBuf[i] = cc; + } - crcLo = read_byte(ttyfd, TIMEOUT); - if (crcLo < 0) - goto timeout; - } else { - cksum = read_byte(ttyfd, TIMEOUT); - if (cksum < 0) + cksum_or_crc = read_byte(TIMEOUT); + if (cksum_or_crc < 0) + goto timeout; + if (do_crc) { + cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT); + if (cksum_or_crc < 0) goto timeout; } @@ -192,153 +176,107 @@ static int receive(char *error_buf, size_t error_buf_size, /* a repeat of the last block is ok, just ignore it. */ /* this also ignores the initial block 0 which is */ /* meta data. */ + blockLength = 0; goto next; - } else if (blockNo != (wantBlockNo & 0xff)) { - note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo); + } + if (blockNo != (wantBlockNo & 0xff)) { + bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo); goto error; } - if (docrc) { - int crc = 0; - int i, j; - int expectedCrcHi; - int expectedCrcLo; - + expected = 0; + if (do_crc) { for (i = 0; i < blockLength; i++) { - crc = crc ^ (int) blockBuf[i] << 8; - for (j = 0; j < 8; j++) - if (crc & 0x8000) - crc = crc << 1 ^ 0x1021; + expected = expected ^ blockBuf[i] << 8; + for (j = 0; j < 8; j++) { + if (expected & 0x8000) + expected = (expected << 1) ^ 0x1021; else - crc = crc << 1; - } - - expectedCrcHi = (crc >> 8) & 0xff; - expectedCrcLo = crc & 0xff; - - if ((crcHi != expectedCrcHi) || - (crcLo != expectedCrcLo)) { - note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo); - goto error; + expected = (expected << 1); + } } + expected &= 0xffff; } else { - unsigned char expectedCksum = 0; - int i; - for (i = 0; i < blockLength; i++) - expectedCksum += blockBuf[i]; - - if (cksum != expectedCksum) { - note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum); - goto error; - } + expected += blockBuf[i]; + expected &= 0xff; + } + if (cksum_or_crc != expected) { + bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x" + : "checksum error, expected 0x%02x, got 0x%02x", + expected, cksum_or_crc); + goto error; } wantBlockNo++; length += blockLength; - - if (bb_full_write(filefd, blockBuf, blockLength) < 0) { - note_error("write to file failed: %m"); - goto fatal; - } - - next: + next: errors = 0; - write_byte(ttyfd, ACK); - write_flush(ttyfd); + reply_char = ACK; + full_write(write_fd, &reply_char, 1); continue; - - error: - timeout: + error: + timeout: + blockLength = 0; errors++; if (errors == MAXERRORS) { /* Abort */ - int i; - // if using crc, try again w/o crc - if (nak == 'C') { - nak = NAK; + /* If were asking for crc, try again w/o crc */ + if (reply_char == 'C') { + reply_char = NAK; errors = 0; - docrc = 0; + do_crc = 0; goto timeout; } - - note_error("too many errors; giving up"); - - fatal: - for (i = 0; i < 5; i ++) - write_byte(ttyfd, CAN); - for (i = 0; i < 5; i ++) - write_byte(ttyfd, BS); - write_flush(ttyfd); + 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); return -1; } - read_flush(ttyfd); - write_byte(ttyfd, nak); - write_flush(ttyfd); - } - - done: - return length; + /* Flush pending input */ + tcflush(read_fd, TCIFLUSH); -#undef note_error + full_write(write_fd, &reply_char, 1); + } /* for (;;) */ } -static void sigalrm_handler(int signum) +static void sigalrm_handler(int UNUSED_PARAM signum) { } -int rx_main(int argc, char **argv) +int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int rx_main(int argc UNUSED_PARAM, char **argv) { - char *fn; - int ttyfd, filefd; struct termios tty, orig_tty; - struct sigaction act; + int termios_err; + int file_fd; int n; - char error_buf[256]; - - if (argc != 2) - bb_show_usage(); - - fn = argv[1]; - ttyfd = open("/dev/tty", O_RDWR); - if (ttyfd < 0) - bb_error_msg_and_die("%s: open on /dev/tty failed: %m\n", argv[0]); - filefd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0666); - if (filefd < 0) - bb_error_msg_and_die("%s: open on %s failed: %m\n", argv[0], fn); - - if (tcgetattr(ttyfd, &tty) < 0) - bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]); - - orig_tty = tty; - - cfmakeraw(&tty); - tcsetattr(ttyfd, TCSAFLUSH, &tty); - - memset(&act, 0, sizeof(act)); - act.sa_handler = sigalrm_handler; - sigaction(SIGALRM, &act, 0); - - n = receive(error_buf, sizeof(error_buf), ttyfd, filefd); - - close(filefd); + /* Disabled by vda: + * why we can't receive from stdin? Why we *require* + * controlling tty?? */ + /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/ + file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC); + + termios_err = tcgetattr(read_fd, &tty); + if (termios_err == 0) { +//TODO: use set_termios_to_raw() + orig_tty = tty; + cfmakeraw(&tty); + tcsetattr(read_fd, TCSAFLUSH, &tty); + } - tcsetattr(ttyfd, TCSAFLUSH, &orig_tty); + /* No SA_RESTART: we want ALRM to interrupt read() */ + signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler); - if (n < 0) - bb_error_msg_and_die("\n%s: receive failed:\n %s\n", - argv[0], error_buf); + n = receive(file_fd); - bb_fflush_stdout_and_exit(EXIT_SUCCESS); + if (termios_err == 0) + tcsetattr(read_fd, TCSAFLUSH, &orig_tty); + if (ENABLE_FEATURE_CLEAN_UP) + close(file_fd); + fflush_stdout_and_exit(n >= 0); } - -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: -*/