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 * 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.
29 #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 inline void write_byte(int fd, char cc) {
63 static inline void write_flush(int fd) {
67 static inline void read_flush(int fd) {
68 tcflush(fd, TCIFLUSH);
71 static int read_byte(int fd, unsigned int timeout) {
77 n = read(fd, &buf, 1);
87 static int receive(char *error_buf, size_t error_buf_size,
88 int ttyfd, int filefd)
91 unsigned int errors = 0;
92 unsigned int wantBlockNo = 1;
93 unsigned int length = 0;
96 unsigned int timeout = TIMEOUT_LONG;
98 #define note_error(fmt,args...) \
99 snprintf(error_buf, error_buf_size, fmt,##args)
103 /* Ask for CRC; if we get errors, we will go with checksum */
104 write_byte(ttyfd, nak);
109 int blockNo, blockNoOnesCompl;
115 blockBegin = read_byte(ttyfd, timeout);
122 switch (blockBegin) {
128 write_byte(ttyfd, ACK);
137 blockNo = read_byte(ttyfd, TIMEOUT);
141 /* block no one's compliment */
142 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
143 if (blockNoOnesCompl < 0)
146 if (blockNo != (255 - blockNoOnesCompl)) {
147 note_error("bad block ones compl");
151 blockLength = (blockBegin == SOH) ? 128 : 1024;
156 for (i = 0; i < blockLength; i++) {
157 int cc = read_byte(ttyfd, TIMEOUT);
165 crcHi = read_byte(ttyfd, TIMEOUT);
169 crcLo = read_byte(ttyfd, TIMEOUT);
173 cksum = read_byte(ttyfd, TIMEOUT);
178 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
179 /* a repeat of the last block is ok, just ignore it. */
180 /* this also ignores the initial block 0 which is */
183 } else if (blockNo != (wantBlockNo & 0xff)) {
184 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
194 for (i = 0; i < blockLength; i++) {
195 crc = crc ^ (int) blockBuf[i] << 8;
196 for (j = 0; j < 8; j++)
198 crc = crc << 1 ^ 0x1021;
203 expectedCrcHi = (crc >> 8) & 0xff;
204 expectedCrcLo = crc & 0xff;
206 if ((crcHi != expectedCrcHi) ||
207 (crcLo != expectedCrcLo)) {
208 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
212 unsigned char expectedCksum = 0;
215 for (i = 0; i < blockLength; i++)
216 expectedCksum += blockBuf[i];
218 if (cksum != expectedCksum) {
219 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
225 length += blockLength;
227 if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
228 note_error("write to file failed: %m");
234 write_byte(ttyfd, ACK);
241 if (errors == MAXERRORS) {
245 // if using crc, try again w/o crc
253 note_error("too many errors; giving up");
256 for (i = 0; i < 5; i ++)
257 write_byte(ttyfd, CAN);
258 for (i = 0; i < 5; i ++)
259 write_byte(ttyfd, BS);
265 write_byte(ttyfd, nak);
275 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
279 int rx_main(int argc, char **argv)
283 struct termios tty, orig_tty;
284 struct sigaction act;
292 ttyfd = open("/dev/tty", O_RDWR);
294 bb_error_msg_and_die("%s: open on /dev/tty failed: %m\n", argv[0]);
296 filefd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
298 bb_error_msg_and_die("%s: open on %s failed: %m\n", argv[0], fn);
300 if (tcgetattr(ttyfd, &tty) < 0)
301 bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
306 tcsetattr(ttyfd, TCSAFLUSH, &tty);
308 memset(&act, 0, sizeof(act));
309 act.sa_handler = sigalrm_handler;
310 sigaction(SIGALRM, &act, 0);
312 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
316 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
319 bb_error_msg_and_die("\n%s: receive failed:\n %s\n",
322 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
327 c-file-style: "linux"