1 /* vi: set sw=4 ts=4: */
2 /*-------------------------------------------------------------------------
4 * Version: $Id: rx.c,v 1.2 2004/03/15 08:28:46 andersen Exp $
5 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
6 * Author: Christopher Hoover <ch@hpl.hp.com>
7 * Description: xmodem functionality for uploading of kernels
9 * Created at: Thu Dec 20 01:58:08 PST 2001
10 *-----------------------------------------------------------------------*/
12 * xmodem.c: xmodem functionality for uploading of kernels and
15 * Copyright (C) 2001 Hewlett-Packard Laboratories
17 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 * This was originally written for blob and then adapted for busybox.
31 #include <sys/types.h>
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
56 #define TIMEOUT_LONG 10
59 static int read_byte(int fd, unsigned int timeout) {
65 n = read(fd, &buf, 1);
75 static int receive(char *error_buf, size_t error_buf_size,
76 int ttyfd, int filefd)
79 unsigned int errors = 0;
80 unsigned int wantBlockNo = 1;
81 unsigned int length = 0;
84 unsigned int timeout = TIMEOUT_LONG;
86 #define note_error(fmt,args...) \
87 snprintf(error_buf, error_buf_size, fmt,##args)
89 /* Flush pending input */
90 tcflush(ttyfd, TCIFLUSH);
92 /* Ask for CRC; if we get errors, we will go with checksum */
93 write(ttyfd, &nak, 1);
97 int blockNo, blockNoOnesCompl;
103 blockBegin = read_byte(ttyfd, timeout);
110 switch (blockBegin) {
117 write(ttyfd, &nak, 1);
125 blockNo = read_byte(ttyfd, TIMEOUT);
129 /* block no one's compliment */
130 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
131 if (blockNoOnesCompl < 0)
134 if (blockNo != (255 - blockNoOnesCompl)) {
135 note_error("bad block ones compl");
139 blockLength = (blockBegin == SOH) ? 128 : 1024;
144 for (i = 0; i < blockLength; i++) {
145 int cc = read_byte(ttyfd, TIMEOUT);
153 crcHi = read_byte(ttyfd, TIMEOUT);
157 crcLo = read_byte(ttyfd, TIMEOUT);
161 cksum = read_byte(ttyfd, TIMEOUT);
166 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
167 /* a repeat of the last block is ok, just ignore it. */
168 /* this also ignores the initial block 0 which is */
171 } else if (blockNo != (wantBlockNo & 0xff)) {
172 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
182 for (i = 0; i < blockLength; i++) {
183 crc = crc ^ (int) blockBuf[i] << 8;
184 for (j = 0; j < 8; j++)
186 crc = crc << 1 ^ 0x1021;
191 expectedCrcHi = (crc >> 8) & 0xff;
192 expectedCrcLo = crc & 0xff;
194 if ((crcHi != expectedCrcHi) ||
195 (crcLo != expectedCrcLo)) {
196 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
200 unsigned char expectedCksum = 0;
203 for (i = 0; i < blockLength; i++)
204 expectedCksum += blockBuf[i];
206 if (cksum != expectedCksum) {
207 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
213 length += blockLength;
215 if (full_write(filefd, blockBuf, blockLength) < 0) {
216 note_error("write to file failed: %m");
223 write(ttyfd, &nak, 1);
229 if (errors == MAXERRORS) {
232 // if using crc, try again w/o crc
240 note_error("too many errors; giving up");
243 /* 5 CAN followed by 5 BS */
244 write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
248 /* Flush pending input */
249 tcflush(ttyfd, TCIFLUSH);
251 write(ttyfd, &nak, 1);
260 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
264 int rx_main(int argc, char **argv)
268 struct termios tty, orig_tty;
269 struct sigaction act;
277 ttyfd = bb_xopen3(CURRENT_TTY, O_RDWR, 0);
278 filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
280 if (tcgetattr(ttyfd, &tty) < 0)
281 bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
286 tcsetattr(ttyfd, TCSAFLUSH, &tty);
288 memset(&act, 0, sizeof(act));
289 act.sa_handler = sigalrm_handler;
290 sigaction(SIGALRM, &act, 0);
292 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
296 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
299 bb_error_msg_and_die("\n%s: receive failed:\n %s\n",
302 bb_fflush_stdout_and_exit(EXIT_SUCCESS);