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.
35 http://www.textfiles.com/apple/xmodem
36 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
37 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
38 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
43 #define TIMEOUT_LONG 10
46 static int read_byte(int fd, unsigned timeout)
53 n = read(fd, &buf, 1);
63 static int receive(char *error_buf, size_t error_buf_size,
64 int ttyfd, int filefd)
67 unsigned int errors = 0;
68 unsigned int wantBlockNo = 1;
69 unsigned int length = 0;
72 unsigned int timeout = TIMEOUT_LONG;
74 #define note_error(fmt,args...) \
75 snprintf(error_buf, error_buf_size, fmt,##args)
77 /* Flush pending input */
78 tcflush(ttyfd, TCIFLUSH);
80 /* Ask for CRC; if we get errors, we will go with checksum */
81 write(ttyfd, &nak, 1);
85 int blockNo, blockNoOnesCompl;
91 blockBegin = read_byte(ttyfd, timeout);
105 write(ttyfd, &nak, 1);
113 blockNo = read_byte(ttyfd, TIMEOUT);
117 /* block no one's compliment */
118 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
119 if (blockNoOnesCompl < 0)
122 if (blockNo != (255 - blockNoOnesCompl)) {
123 note_error("bad block ones compl");
127 blockLength = (blockBegin == SOH) ? 128 : 1024;
132 for (i = 0; i < blockLength; i++) {
133 int cc = read_byte(ttyfd, TIMEOUT);
141 crcHi = read_byte(ttyfd, TIMEOUT);
145 crcLo = read_byte(ttyfd, TIMEOUT);
149 cksum = read_byte(ttyfd, TIMEOUT);
154 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
155 /* a repeat of the last block is ok, just ignore it. */
156 /* this also ignores the initial block 0 which is */
159 } else if (blockNo != (wantBlockNo & 0xff)) {
160 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
170 for (i = 0; i < blockLength; i++) {
171 crc = crc ^ (int) blockBuf[i] << 8;
172 for (j = 0; j < 8; j++)
174 crc = crc << 1 ^ 0x1021;
179 expectedCrcHi = (crc >> 8) & 0xff;
180 expectedCrcLo = crc & 0xff;
182 if ((crcHi != expectedCrcHi) ||
183 (crcLo != expectedCrcLo)) {
184 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
188 unsigned char expectedCksum = 0;
191 for (i = 0; i < blockLength; i++)
192 expectedCksum += blockBuf[i];
194 if (cksum != expectedCksum) {
195 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
201 length += blockLength;
203 if (full_write(filefd, blockBuf, blockLength) < 0) {
204 note_error("write to file failed: %m");
211 write(ttyfd, &nak, 1);
217 if (errors == MAXERRORS) {
220 // if using crc, try again w/o crc
228 note_error("too many errors; giving up");
231 /* 5 CAN followed by 5 BS */
232 write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
236 /* Flush pending input */
237 tcflush(ttyfd, TCIFLUSH);
239 write(ttyfd, &nak, 1);
248 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
252 int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
253 int rx_main(int argc, char **argv)
257 struct termios tty, orig_tty;
258 struct sigaction act;
266 ttyfd = xopen(CURRENT_TTY, O_RDWR);
267 filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
269 if (tcgetattr(ttyfd, &tty) < 0)
270 bb_perror_msg_and_die("tcgetattr");
275 tcsetattr(ttyfd, TCSAFLUSH, &tty);
277 memset(&act, 0, sizeof(act));
278 act.sa_handler = sigalrm_handler;
279 sigaction(SIGALRM, &act, 0);
281 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
285 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
288 bb_error_msg_and_die("\nreceive failed:\n %s", error_buf);
290 fflush_stdout_and_exit(EXIT_SUCCESS);