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 tarball for details.
15 * This was originally written for blob and then adapted for busybox.
30 http://www.textfiles.com/apple/xmodem
31 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
32 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
33 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
37 #define TIMEOUT_LONG 10
40 #define read_fd STDIN_FILENO
41 #define write_fd STDOUT_FILENO
43 static int read_byte(unsigned timeout)
49 /* NOT safe_read! We want ALRM to interrupt us */
50 n = read(read_fd, &buf, 1);
57 static int receive(/*int read_fd, */int file_fd)
59 unsigned char blockBuf[1024];
60 unsigned blockLength = 0;
62 unsigned wantBlockNo = 1;
66 unsigned timeout = TIMEOUT_LONG;
68 /* Flush pending input */
69 tcflush(read_fd, TCIFLUSH);
71 /* Ask for CRC; if we get errors, we will go with checksum */
73 full_write(write_fd, &reply_char, 1);
77 int blockNo, blockNoOnesCompl;
82 blockBegin = read_byte(timeout);
86 /* If last block, remove padding */
87 if (blockBegin == EOT) {
88 /* Data blocks can be padded with ^Z characters */
89 /* This code tries to detect and remove them */
91 && blockBuf[blockLength - 1] == PAD
92 && blockBuf[blockLength - 2] == PAD
93 && blockBuf[blockLength - 3] == PAD
96 && blockBuf[blockLength - 1] == PAD
102 /* Write previously received block */
105 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
106 bb_perror_msg("can't write to file");
114 switch (blockBegin) {
120 full_write(write_fd, &reply_char, 1);
127 blockNo = read_byte(TIMEOUT);
131 /* Block no, in one's complement form */
132 blockNoOnesCompl = read_byte(TIMEOUT);
133 if (blockNoOnesCompl < 0)
136 if (blockNo != (255 - blockNoOnesCompl)) {
137 bb_error_msg("bad block ones compl");
141 blockLength = (blockBegin == SOH) ? 128 : 1024;
143 for (i = 0; i < blockLength; i++) {
144 int cc = read_byte(TIMEOUT);
151 cksum_or_crc = read_byte(TIMEOUT);
152 if (cksum_or_crc < 0)
154 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
155 if (cksum_or_crc < 0)
158 cksum_or_crc = read_byte(TIMEOUT);
159 if (cksum_or_crc < 0)
163 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
164 /* a repeat of the last block is ok, just ignore it. */
165 /* this also ignores the initial block 0 which is */
169 if (blockNo != (wantBlockNo & 0xff)) {
170 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
176 for (i = 0; i < blockLength; i++) {
177 expected = expected ^ blockBuf[i] << 8;
178 for (j = 0; j < 8; j++) {
179 if (expected & 0x8000)
180 expected = (expected << 1) ^ 0x1021;
182 expected = (expected << 1);
187 for (i = 0; i < blockLength; i++)
188 expected += blockBuf[i];
191 if (cksum_or_crc != expected) {
192 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
193 : "checksum error, expected 0x%02x, got 0x%02x",
194 expected, cksum_or_crc);
199 length += blockLength;
203 full_write(write_fd, &reply_char, 1);
208 if (errors == MAXERRORS) {
211 /* If were asking for crc, try again w/o crc */
212 if (reply_char == 'C') {
218 bb_error_msg("too many errors; giving up");
220 /* 5 CAN followed by 5 BS. Don't try too hard... */
221 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
225 /* Flush pending input */
226 tcflush(read_fd, TCIFLUSH);
228 full_write(write_fd, &reply_char, 1);
232 static void sigalrm_handler(int UNUSED_PARAM signum)
236 int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
237 int rx_main(int argc UNUSED_PARAM, char **argv)
239 struct termios tty, orig_tty;
245 * why we can't receive from stdin? Why we *require*
246 * controlling tty?? */
247 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
248 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
250 termios_err = tcgetattr(read_fd, &tty);
251 if (termios_err == 0) {
254 tcsetattr(read_fd, TCSAFLUSH, &tty);
257 /* No SA_RESTART: we want ALRM to interrupt read() */
258 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
260 n = receive(file_fd);
262 if (termios_err == 0)
263 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
264 if (ENABLE_FEATURE_CLEAN_UP)
266 fflush_stdout_and_exit(n >= 0);