1 /* vi: set sw=4 ts=4: */
2 /*-------------------------------------------------------------------------
4 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
5 * Author: Christopher Hoover <ch@hpl.hp.com>
6 * Description: xmodem functionality for uploading of kernels
8 * Created at: Thu Dec 20 01:58:08 PST 2001
9 *-----------------------------------------------------------------------*/
11 * xmodem.c: xmodem functionality for uploading of kernels and
14 * Copyright (C) 2001 Hewlett-Packard Laboratories
16 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 * This was originally written for blob and then adapted for busybox.
32 http://www.textfiles.com/apple/xmodem
33 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
34 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
35 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
39 #define TIMEOUT_LONG 10
42 #define read_fd STDIN_FILENO
43 #define write_fd STDOUT_FILENO
45 static int read_byte(unsigned timeout)
51 /* NOT safe_read! We want ALRM to interrupt us */
52 n = read(read_fd, buf, 1);
55 return (unsigned char)buf[0];
59 static int receive(/*int read_fd, */int file_fd)
61 unsigned char blockBuf[1024];
63 unsigned wantBlockNo = 1;
67 unsigned timeout = TIMEOUT_LONG;
69 /* Flush pending input */
70 tcflush(read_fd, TCIFLUSH);
72 /* Ask for CRC; if we get errors, we will go with checksum */
73 full_write(write_fd, &nak, 1);
77 int blockNo, blockNoOnesCompl;
79 int cksum_crc; /* cksum OR crc */
83 blockBegin = read_byte(timeout);
97 full_write(write_fd, &nak, 1);
105 blockNo = read_byte(TIMEOUT);
109 /* block no one's compliment */
110 blockNoOnesCompl = read_byte(TIMEOUT);
111 if (blockNoOnesCompl < 0)
114 if (blockNo != (255 - blockNoOnesCompl)) {
115 bb_error_msg("bad block ones compl");
119 blockLength = (blockBegin == SOH) ? 128 : 1024;
121 for (i = 0; i < blockLength; i++) {
122 int cc = read_byte(TIMEOUT);
129 cksum_crc = read_byte(TIMEOUT);
132 cksum_crc = (cksum_crc << 8) | read_byte(TIMEOUT);
136 cksum_crc = read_byte(TIMEOUT);
141 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
142 /* a repeat of the last block is ok, just ignore it. */
143 /* this also ignores the initial block 0 which is */
147 if (blockNo != (wantBlockNo & 0xff)) {
148 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
154 for (i = 0; i < blockLength; i++) {
155 expected = expected ^ blockBuf[i] << 8;
156 for (j = 0; j < 8; j++) {
157 if (expected & 0x8000)
158 expected = expected << 1 ^ 0x1021;
160 expected = expected << 1;
165 for (i = 0; i < blockLength; i++)
166 expected += blockBuf[i];
169 if (cksum_crc != expected) {
170 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
171 : "checksum error, expected 0x%02x, got 0x%02x",
172 expected, cksum_crc);
177 length += blockLength;
180 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
181 bb_perror_msg("can't write to file");
187 full_write(write_fd, &nak, 1);
192 if (errors == MAXERRORS) {
195 /* if were asking for crc, try again w/o crc */
202 bb_error_msg("too many errors; giving up");
204 /* 5 CAN followed by 5 BS. Don't try too hard... */
205 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
209 /* Flush pending input */
210 tcflush(read_fd, TCIFLUSH);
212 full_write(write_fd, &nak, 1);
216 static void sigalrm_handler(int UNUSED_PARAM signum)
220 int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
221 int rx_main(int argc, char **argv)
223 struct termios tty, orig_tty;
232 * why we can't receive from stdin? Why we *require*
233 * controlling tty?? */
234 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
235 file_fd = xopen(argv[1], O_RDWR|O_CREAT|O_TRUNC);
237 termios_err = tcgetattr(read_fd, &tty);
238 if (termios_err == 0) {
241 tcsetattr(read_fd, TCSAFLUSH, &tty);
244 /* No SA_RESTART: we want ALRM to interrupt read() */
245 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
247 n = receive(file_fd);
249 if (termios_err == 0)
250 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
251 if (ENABLE_FEATURE_CLEAN_UP)
253 fflush_stdout_and_exit(n >= 0);