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 int timeout) {
52 n = read(fd, &buf, 1);
62 static int receive(char *error_buf, size_t error_buf_size,
63 int ttyfd, int filefd)
66 unsigned int errors = 0;
67 unsigned int wantBlockNo = 1;
68 unsigned int length = 0;
71 unsigned int timeout = TIMEOUT_LONG;
73 #define note_error(fmt,args...) \
74 snprintf(error_buf, error_buf_size, fmt,##args)
76 /* Flush pending input */
77 tcflush(ttyfd, TCIFLUSH);
79 /* Ask for CRC; if we get errors, we will go with checksum */
80 write(ttyfd, &nak, 1);
84 int blockNo, blockNoOnesCompl;
90 blockBegin = read_byte(ttyfd, timeout);
104 write(ttyfd, &nak, 1);
112 blockNo = read_byte(ttyfd, TIMEOUT);
116 /* block no one's compliment */
117 blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
118 if (blockNoOnesCompl < 0)
121 if (blockNo != (255 - blockNoOnesCompl)) {
122 note_error("bad block ones compl");
126 blockLength = (blockBegin == SOH) ? 128 : 1024;
131 for (i = 0; i < blockLength; i++) {
132 int cc = read_byte(ttyfd, TIMEOUT);
140 crcHi = read_byte(ttyfd, TIMEOUT);
144 crcLo = read_byte(ttyfd, TIMEOUT);
148 cksum = read_byte(ttyfd, TIMEOUT);
153 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
154 /* a repeat of the last block is ok, just ignore it. */
155 /* this also ignores the initial block 0 which is */
158 } else if (blockNo != (wantBlockNo & 0xff)) {
159 note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
169 for (i = 0; i < blockLength; i++) {
170 crc = crc ^ (int) blockBuf[i] << 8;
171 for (j = 0; j < 8; j++)
173 crc = crc << 1 ^ 0x1021;
178 expectedCrcHi = (crc >> 8) & 0xff;
179 expectedCrcLo = crc & 0xff;
181 if ((crcHi != expectedCrcHi) ||
182 (crcLo != expectedCrcLo)) {
183 note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
187 unsigned char expectedCksum = 0;
190 for (i = 0; i < blockLength; i++)
191 expectedCksum += blockBuf[i];
193 if (cksum != expectedCksum) {
194 note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
200 length += blockLength;
202 if (full_write(filefd, blockBuf, blockLength) < 0) {
203 note_error("write to file failed: %m");
210 write(ttyfd, &nak, 1);
216 if (errors == MAXERRORS) {
219 // if using crc, try again w/o crc
227 note_error("too many errors; giving up");
230 /* 5 CAN followed by 5 BS */
231 write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
235 /* Flush pending input */
236 tcflush(ttyfd, TCIFLUSH);
238 write(ttyfd, &nak, 1);
247 static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
251 int rx_main(int argc, char **argv)
255 struct termios tty, orig_tty;
256 struct sigaction act;
264 ttyfd = xopen(CURRENT_TTY, O_RDWR);
265 filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
267 if (tcgetattr(ttyfd, &tty) < 0)
268 bb_perror_msg_and_die("tcgetattr");
273 tcsetattr(ttyfd, TCSAFLUSH, &tty);
275 memset(&act, 0, sizeof(act));
276 act.sa_handler = sigalrm_handler;
277 sigaction(SIGALRM, &act, 0);
279 n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
283 tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
286 bb_error_msg_and_die("\nreceive failed:\n %s", error_buf);
288 fflush_stdout_and_exit(EXIT_SUCCESS);