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.
30 #include <sys/types.h>
49 http://www.textfiles.com/apple/xmodem
50 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
51 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
52 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
57 #define TIMEOUT_LONG 10
60 static inline void write_byte(int fd, char cc) {
64 static inline void write_flush(int fd) {
68 static inline void read_flush(int fd) {
69 tcflush(fd, TCIFLUSH);
72 static int read_byte(int fd, unsigned int timeout) {
78 n = read(fd, &buf, 1);
88 static int receive(char *error_buf, size_t error_buf_size,
89 int ttyfd, int filefd)
92 unsigned int errors = 0;
93 unsigned int wantBlockNo = 1;
94 unsigned int length = 0;
97 unsigned int timeout = TIMEOUT_LONG;
99 #define note_error(fmt,args...) \
100 snprintf(error_buf, error_buf_size, fmt,##args)
104 /* Ask for CRC; if we get errors, we will go with checksum */
105 write_byte(ttyfd, nak);
110 int blockNo, blockNoOnesCompl;
116 blockBegin = read_byte(ttyfd, timeout);
123 switch (blockBegin) {
129 write_byte(ttyfd, ACK);
138 blockNo = read_byte(ttyfd, TIMEOUT);
142 /* block no one's compliment */
143 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
144 if (blockNoOnesCompl < 0)
147 if (blockNo != (255 - blockNoOnesCompl)) {
148 note_error("bad block ones compl");
152 blockLength = (blockBegin == SOH) ? 128 : 1024;
157 for (i = 0; i < blockLength; i++) {
158 int cc = read_byte(ttyfd, TIMEOUT);
166 crcHi = read_byte(ttyfd, TIMEOUT);
170 crcLo = read_byte(ttyfd, TIMEOUT);
174 cksum = read_byte(ttyfd, TIMEOUT);
179 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
180 /* a repeat of the last block is ok, just ignore it. */
181 /* this also ignores the initial block 0 which is */
184 } else if (blockNo != (wantBlockNo & 0xff)) {
185 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
195 for (i = 0; i < blockLength; i++) {
196 crc = crc ^ (int) blockBuf[i] << 8;
197 for (j = 0; j < 8; j++)
199 crc = crc << 1 ^ 0x1021;
204 expectedCrcHi = (crc >> 8) & 0xff;
205 expectedCrcLo = crc & 0xff;
207 if ((crcHi != expectedCrcHi) ||
208 (crcLo != expectedCrcLo)) {
209 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
213 unsigned char expectedCksum = 0;
216 for (i = 0; i < blockLength; i++)
217 expectedCksum += blockBuf[i];
219 if (cksum != expectedCksum) {
220 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
226 length += blockLength;
228 if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
229 note_error("write to file failed: %m");
235 write_byte(ttyfd, ACK);
242 if (errors == MAXERRORS) {
246 // if using crc, try again w/o crc
254 note_error("too many errors; giving up");
257 for (i = 0; i < 5; i ++)
258 write_byte(ttyfd, CAN);
259 for (i = 0; i < 5; i ++)
260 write_byte(ttyfd, BS);
266 write_byte(ttyfd, nak);
276 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
280 int rx_main(int argc, char **argv)
284 struct termios tty, orig_tty;
285 struct sigaction act;
293 ttyfd = bb_xopen3("/dev/tty", O_RDWR, 0);
294 filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
296 if (tcgetattr(ttyfd, &tty) < 0)
297 bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
302 tcsetattr(ttyfd, TCSAFLUSH, &tty);
304 memset(&act, 0, sizeof(act));
305 act.sa_handler = sigalrm_handler;
306 sigaction(SIGALRM, &act, 0);
308 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
312 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
315 bb_error_msg_and_die("\n%s: receive failed:\n %s\n",
318 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
323 c-file-style: "linux"