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 //usage:#define rx_trivial_usage
20 //usage:#define rx_full_usage "\n\n"
21 //usage: "Receive a file using the xmodem protocol"
23 //usage:#define rx_example_usage
24 //usage: "$ rx /tmp/foo\n"
38 http://www.textfiles.com/apple/xmodem
39 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
40 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
41 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
45 #define TIMEOUT_LONG 10
48 #define read_fd STDIN_FILENO
49 #define write_fd STDOUT_FILENO
51 static int read_byte(unsigned timeout)
57 /* NOT safe_read! We want ALRM to interrupt us */
58 n = read(read_fd, &buf, 1);
65 static int receive(/*int read_fd, */int file_fd)
67 unsigned char blockBuf[1024];
68 unsigned blockLength = 0;
70 unsigned wantBlockNo = 1;
74 unsigned timeout = TIMEOUT_LONG;
76 /* Flush pending input */
77 tcflush(read_fd, TCIFLUSH);
79 /* Ask for CRC; if we get errors, we will go with checksum */
81 full_write(write_fd, &reply_char, 1);
85 int blockNo, blockNoOnesCompl;
90 blockBegin = read_byte(timeout);
94 /* If last block, remove padding */
95 if (blockBegin == EOT) {
96 /* Data blocks can be padded with ^Z characters */
97 /* This code tries to detect and remove them */
99 && blockBuf[blockLength - 1] == PAD
100 && blockBuf[blockLength - 2] == PAD
101 && blockBuf[blockLength - 3] == PAD
104 && blockBuf[blockLength - 1] == PAD
110 /* Write previously received block */
113 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
114 bb_perror_msg("can't write to file");
122 switch (blockBegin) {
128 full_write(write_fd, &reply_char, 1);
135 blockNo = read_byte(TIMEOUT);
139 /* Block no, in one's complement form */
140 blockNoOnesCompl = read_byte(TIMEOUT);
141 if (blockNoOnesCompl < 0)
144 if (blockNo != (255 - blockNoOnesCompl)) {
145 bb_error_msg("bad block ones compl");
149 blockLength = (blockBegin == SOH) ? 128 : 1024;
151 for (i = 0; i < blockLength; i++) {
152 int cc = read_byte(TIMEOUT);
159 cksum_or_crc = read_byte(TIMEOUT);
160 if (cksum_or_crc < 0)
162 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
163 if (cksum_or_crc < 0)
166 cksum_or_crc = read_byte(TIMEOUT);
167 if (cksum_or_crc < 0)
171 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
172 /* a repeat of the last block is ok, just ignore it. */
173 /* this also ignores the initial block 0 which is */
178 if (blockNo != (wantBlockNo & 0xff)) {
179 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
185 for (i = 0; i < blockLength; i++) {
186 expected = expected ^ blockBuf[i] << 8;
187 for (j = 0; j < 8; j++) {
188 if (expected & 0x8000)
189 expected = (expected << 1) ^ 0x1021;
191 expected = (expected << 1);
196 for (i = 0; i < blockLength; i++)
197 expected += blockBuf[i];
200 if (cksum_or_crc != expected) {
201 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
202 : "checksum error, expected 0x%02x, got 0x%02x",
203 expected, cksum_or_crc);
208 length += blockLength;
212 full_write(write_fd, &reply_char, 1);
217 if (errors == MAXERRORS) {
220 /* If were asking for crc, try again w/o crc */
221 if (reply_char == 'C') {
227 bb_error_msg("too many errors; giving up");
229 /* 5 CAN followed by 5 BS. Don't try too hard... */
230 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
234 /* Flush pending input */
235 tcflush(read_fd, TCIFLUSH);
237 full_write(write_fd, &reply_char, 1);
241 static void sigalrm_handler(int UNUSED_PARAM signum)
245 int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
246 int rx_main(int argc UNUSED_PARAM, char **argv)
248 struct termios tty, orig_tty;
254 * why we can't receive from stdin? Why we *require*
255 * controlling tty?? */
256 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
257 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
259 termios_err = tcgetattr(read_fd, &tty);
260 if (termios_err == 0) {
263 tcsetattr(read_fd, TCSAFLUSH, &tty);
266 /* No SA_RESTART: we want ALRM to interrupt read() */
267 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
269 n = receive(file_fd);
271 if (termios_err == 0)
272 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
273 if (ENABLE_FEATURE_CLEAN_UP)
275 fflush_stdout_and_exit(n >= 0);