1 /* vi: set sw=4 ts=4: */
3 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
4 * Author: Christopher Hoover <ch@hpl.hp.com>
5 * Description: xmodem functionality for uploading of kernels
7 * Created at: Thu Dec 20 01:58:08 PST 2001
9 * xmodem functionality for uploading of kernels and the like
11 * Copyright (C) 2001 Hewlett-Packard Laboratories
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
15 * This was originally written for blob and then adapted for busybox.
18 //config: bool "rx (2.9 kb)"
20 //config: select PLATFORM_LINUX
22 //config: Receive files using the Xmodem protocol.
24 //applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP))
26 //kbuild:lib-$(CONFIG_RX) += rx.o
28 //usage:#define rx_trivial_usage
30 //usage:#define rx_full_usage "\n\n"
31 //usage: "Receive a file using the xmodem protocol"
33 //usage:#define rx_example_usage
34 //usage: "$ rx /tmp/foo\n"
48 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
55 #define TIMEOUT_LONG 10
58 #define read_fd STDIN_FILENO
59 #define write_fd STDOUT_FILENO
61 static int read_byte(unsigned timeout)
67 /* NOT safe_read! We want ALRM to interrupt us */
68 n = read(read_fd, &buf, 1);
75 static int receive(/*int read_fd, */int file_fd)
77 unsigned char blockBuf[1024];
78 unsigned blockLength = 0;
80 unsigned wantBlockNo = 1;
84 unsigned timeout = TIMEOUT_LONG;
86 /* Flush pending input */
87 tcflush(read_fd, TCIFLUSH);
89 /* Ask for CRC; if we get errors, we will go with checksum */
91 full_write(write_fd, &reply_char, 1);
95 int blockNo, blockNoOnesCompl;
100 blockBegin = read_byte(timeout);
104 /* 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 */
109 && blockBuf[blockLength - 1] == PAD
110 && blockBuf[blockLength - 2] == PAD
111 && blockBuf[blockLength - 3] == PAD
114 && blockBuf[blockLength - 1] == PAD
120 /* Write previously received block */
122 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
123 bb_perror_msg(bb_msg_write_error);
130 switch (blockBegin) {
136 full_write(write_fd, &reply_char, 1);
143 blockNo = read_byte(TIMEOUT);
147 /* Block no, in one's complement form */
148 blockNoOnesCompl = read_byte(TIMEOUT);
149 if (blockNoOnesCompl < 0)
152 if (blockNo != (255 - blockNoOnesCompl)) {
153 bb_error_msg("bad block ones compl");
157 blockLength = (blockBegin == SOH) ? 128 : 1024;
159 for (i = 0; i < blockLength; i++) {
160 int cc = read_byte(TIMEOUT);
166 cksum_or_crc = read_byte(TIMEOUT);
167 if (cksum_or_crc < 0)
170 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
171 if (cksum_or_crc < 0)
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 */
182 if (blockNo != (wantBlockNo & 0xff)) {
183 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
189 for (i = 0; i < blockLength; i++) {
190 expected = expected ^ blockBuf[i] << 8;
191 for (j = 0; j < 8; j++) {
192 if (expected & 0x8000)
193 expected = (expected << 1) ^ 0x1021;
195 expected = (expected << 1);
200 for (i = 0; i < blockLength; i++)
201 expected += blockBuf[i];
204 if (cksum_or_crc != expected) {
205 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
206 : "checksum error, expected 0x%02x, got 0x%02x",
207 expected, cksum_or_crc);
212 length += blockLength;
216 full_write(write_fd, &reply_char, 1);
222 if (errors == MAXERRORS) {
225 /* If were asking for crc, try again w/o crc */
226 if (reply_char == 'C') {
232 bb_error_msg("too many errors; giving up");
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);
239 /* Flush pending input */
240 tcflush(read_fd, TCIFLUSH);
242 full_write(write_fd, &reply_char, 1);
246 static void sigalrm_handler(int UNUSED_PARAM signum)
250 int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
251 int rx_main(int argc UNUSED_PARAM, char **argv)
253 struct termios tty, orig_tty;
259 * why we can't receive from stdin? Why we *require*
260 * controlling tty?? */
261 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
262 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
264 termios_err = tcgetattr(read_fd, &tty);
265 if (termios_err == 0) {
266 //TODO: use set_termios_to_raw()
269 tcsetattr(read_fd, TCSAFLUSH, &tty);
272 /* No SA_RESTART: we want ALRM to interrupt read() */
273 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
275 n = receive(file_fd);
277 if (termios_err == 0)
278 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
279 if (ENABLE_FEATURE_CLEAN_UP)
281 fflush_stdout_and_exit(n >= 0);