1 /*-------------------------------------------------------------------------
3 * Version: $Id: rx.c,v 1.2 2004/03/15 08:28:46 andersen Exp $
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 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * This was originally written for blob and then adapted for busybox.
42 #include <sys/types.h>
61 http://www.textfiles.com/apple/xmodem
62 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
63 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
64 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
69 #define TIMEOUT_LONG 10
72 static inline void write_byte(int fd, char cc) {
76 static inline void write_flush(int fd) {
80 static inline void read_flush(int fd) {
81 tcflush(fd, TCIFLUSH);
84 static int read_byte(int fd, unsigned int timeout) {
90 n = read(fd, &buf, 1);
100 static int receive(char *error_buf, size_t error_buf_size,
101 int ttyfd, int filefd)
104 unsigned int errors = 0;
105 unsigned int wantBlockNo = 1;
106 unsigned int length = 0;
109 unsigned int timeout = TIMEOUT_LONG;
111 #define note_error(fmt,args...) \
112 snprintf(error_buf, error_buf_size, fmt,##args)
116 /* Ask for CRC; if we get errors, we will go with checksum */
117 write_byte(ttyfd, nak);
122 int blockNo, blockNoOnesCompl;
128 blockBegin = read_byte(ttyfd, timeout);
135 switch (blockBegin) {
141 write_byte(ttyfd, ACK);
150 blockNo = read_byte(ttyfd, TIMEOUT);
154 /* block no one's compliment */
155 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
156 if (blockNoOnesCompl < 0)
159 if (blockNo != (255 - blockNoOnesCompl)) {
160 note_error("bad block ones compl");
164 blockLength = (blockBegin == SOH) ? 128 : 1024;
169 for (i = 0; i < blockLength; i++) {
170 int cc = read_byte(ttyfd, TIMEOUT);
178 crcHi = read_byte(ttyfd, TIMEOUT);
182 crcLo = read_byte(ttyfd, TIMEOUT);
186 cksum = read_byte(ttyfd, TIMEOUT);
191 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
192 /* a repeat of the last block is ok, just ignore it. */
193 /* this also ignores the initial block 0 which is */
196 } else if (blockNo != (wantBlockNo & 0xff)) {
197 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
207 for (i = 0; i < blockLength; i++) {
208 crc = crc ^ (int) blockBuf[i] << 8;
209 for (j = 0; j < 8; j++)
211 crc = crc << 1 ^ 0x1021;
216 expectedCrcHi = (crc >> 8) & 0xff;
217 expectedCrcLo = crc & 0xff;
219 if ((crcHi != expectedCrcHi) ||
220 (crcLo != expectedCrcLo)) {
221 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
225 unsigned char expectedCksum = 0;
228 for (i = 0; i < blockLength; i++)
229 expectedCksum += blockBuf[i];
231 if (cksum != expectedCksum) {
232 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
238 length += blockLength;
240 if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
241 note_error("write to file failed: %m");
247 write_byte(ttyfd, ACK);
254 if (errors == MAXERRORS) {
258 // if using crc, try again w/o crc
266 note_error("too many errors; giving up");
269 for (i = 0; i < 5; i ++)
270 write_byte(ttyfd, CAN);
271 for (i = 0; i < 5; i ++)
272 write_byte(ttyfd, BS);
278 write_byte(ttyfd, nak);
288 static void sigalrm_handler(int signum)
292 int rx_main(int argc, char **argv)
296 struct termios tty, orig_tty;
297 struct sigaction act;
305 ttyfd = open("/dev/tty", O_RDWR);
307 bb_error_msg_and_die("%s: open on /dev/tty failed: %m\n", argv[0]);
309 filefd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
311 bb_error_msg_and_die("%s: open on %s failed: %m\n", argv[0], fn);
313 if (tcgetattr(ttyfd, &tty) < 0)
314 bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
319 tcsetattr(ttyfd, TCSAFLUSH, &tty);
321 memset(&act, 0, sizeof(act));
322 act.sa_handler = sigalrm_handler;
323 sigaction(SIGALRM, &act, 0);
325 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
329 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
332 bb_error_msg_and_die("\n%s: receive failed:\n %s\n",
335 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
340 c-file-style: "linux"