blob: 9a8fcaa2075d60529999fd2bf1be9881e6c59c0c [file] [log] [blame]
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00002/*-------------------------------------------------------------------------
3 * Filename: xmodem.c
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00004 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
5 * Author: Christopher Hoover <ch@hpl.hp.com>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Description: xmodem functionality for uploading of kernels
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +00007 * and the like
8 * Created at: Thu Dec 20 01:58:08 PST 2001
9 *-----------------------------------------------------------------------*/
10/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000011 * xmodem.c: xmodem functionality for uploading of kernels and
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000012 * the like
13 *
14 * Copyright (C) 2001 Hewlett-Packard Laboratories
15 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000016 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000017 *
18 * This was originally written for blob and then adapted for busybox.
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000019 */
20
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000022
23#define SOH 0x01
24#define STX 0x02
25#define EOT 0x04
26#define ACK 0x06
27#define NAK 0x15
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000028#define BS 0x08
29
30/*
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000031Cf:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000032 http://www.textfiles.com/apple/xmodem
33 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
34 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
35 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000036*/
37
38#define TIMEOUT 1
39#define TIMEOUT_LONG 10
40#define MAXERRORS 10
41
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000042#define read_fd STDIN_FILENO
43#define write_fd STDOUT_FILENO
44
45static int read_byte(unsigned timeout)
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000046{
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000047 char buf[1];
48 int n;
49
50 alarm(timeout);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000051 /* NOT safe_read! We want ALRM to interrupt us */
52 n = read(read_fd, buf, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000053 alarm(0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000054 if (n == 1)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000055 return (unsigned char)buf[0];
56 return -1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000057}
58
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000059static int receive(/*int read_fd, */int file_fd)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000060{
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000061 unsigned char blockBuf[1024];
62 unsigned errors = 0;
63 unsigned wantBlockNo = 1;
64 unsigned length = 0;
65 int do_crc = 1;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000066 char nak = 'C';
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000067 unsigned timeout = TIMEOUT_LONG;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000068
Rob Landley399d45f2006-06-21 01:49:17 +000069 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000070 tcflush(read_fd, TCIFLUSH);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000071
72 /* Ask for CRC; if we get errors, we will go with checksum */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000073 full_write(write_fd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000074
75 for (;;) {
76 int blockBegin;
77 int blockNo, blockNoOnesCompl;
78 int blockLength;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000079 int cksum_crc; /* cksum OR crc */
80 int expected;
81 int i,j;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000082
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000083 blockBegin = read_byte(timeout);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000084 if (blockBegin < 0)
85 goto timeout;
86
87 timeout = TIMEOUT;
88 nak = NAK;
89
90 switch (blockBegin) {
91 case SOH:
92 case STX:
93 break;
94
95 case EOT:
Rob Landley399d45f2006-06-21 01:49:17 +000096 nak = ACK;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000097 full_write(write_fd, &nak, 1);
98 return length;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +000099
100 default:
101 goto error;
102 }
103
104 /* block no */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000105 blockNo = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000106 if (blockNo < 0)
107 goto timeout;
108
109 /* block no one's compliment */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000110 blockNoOnesCompl = read_byte(TIMEOUT);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000111 if (blockNoOnesCompl < 0)
112 goto timeout;
113
114 if (blockNo != (255 - blockNoOnesCompl)) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000115 bb_error_msg("bad block ones compl");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000116 goto error;
117 }
118
119 blockLength = (blockBegin == SOH) ? 128 : 1024;
120
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000121 for (i = 0; i < blockLength; i++) {
122 int cc = read_byte(TIMEOUT);
123 if (cc < 0)
124 goto timeout;
125 blockBuf[i] = cc;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000126 }
127
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000128 if (do_crc) {
129 cksum_crc = read_byte(TIMEOUT);
130 if (cksum_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000131 goto timeout;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000132 cksum_crc = (cksum_crc << 8) | read_byte(TIMEOUT);
133 if (cksum_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000134 goto timeout;
135 } else {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000136 cksum_crc = read_byte(TIMEOUT);
137 if (cksum_crc < 0)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000138 goto timeout;
139 }
140
141 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
142 /* a repeat of the last block is ok, just ignore it. */
143 /* this also ignores the initial block 0 which is */
144 /* meta data. */
145 goto next;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000146 }
147 if (blockNo != (wantBlockNo & 0xff)) {
148 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000149 goto error;
150 }
151
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000152 expected = 0;
153 if (do_crc) {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000154 for (i = 0; i < blockLength; i++) {
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000155 expected = expected ^ blockBuf[i] << 8;
156 for (j = 0; j < 8; j++) {
157 if (expected & 0x8000)
158 expected = expected << 1 ^ 0x1021;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000159 else
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000160 expected = expected << 1;
161 }
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000162 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000163 expected &= 0xffff;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000164 } else {
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000165 for (i = 0; i < blockLength; i++)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000166 expected += blockBuf[i];
167 expected &= 0xff;
168 }
169 if (cksum_crc != expected) {
170 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
171 : "checksum error, expected 0x%02x, got 0x%02x",
172 expected, cksum_crc);
173 goto error;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000174 }
175
176 wantBlockNo++;
177 length += blockLength;
178
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000179 errno = 0;
180 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
181 bb_perror_msg("can't write to file");
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000182 goto fatal;
183 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000184 next:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000185 errors = 0;
Rob Landley399d45f2006-06-21 01:49:17 +0000186 nak = ACK;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000187 full_write(write_fd, &nak, 1);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000188 continue;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000189 error:
190 timeout:
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000191 errors++;
192 if (errors == MAXERRORS) {
193 /* Abort */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000194
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000195 /* if were asking for crc, try again w/o crc */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000196 if (nak == 'C') {
197 nak = NAK;
198 errors = 0;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000199 do_crc = 0;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000200 goto timeout;
201 }
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000202 bb_error_msg("too many errors; giving up");
203 fatal:
204 /* 5 CAN followed by 5 BS. Don't try too hard... */
205 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000206 return -1;
207 }
208
Rob Landley399d45f2006-06-21 01:49:17 +0000209 /* Flush pending input */
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000210 tcflush(read_fd, TCIFLUSH);
Rob Landley399d45f2006-06-21 01:49:17 +0000211
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000212 full_write(write_fd, &nak, 1);
213 } /* for (;;) */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000214}
215
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000216static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000217{
218}
219
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000220int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000221int rx_main(int argc, char **argv)
222{
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000223 struct sigaction act;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000224 struct termios tty, orig_tty;
225 int termios_err;
226 int file_fd;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000227 int n;
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000228
229 if (argc != 2)
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000230 bb_show_usage();
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000231
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000232 /* Disabled by vda:
233 * why we can't receive from stdin? Why we *require*
234 * controlling tty?? */
235 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
236 file_fd = xopen(argv[1], O_RDWR|O_CREAT|O_TRUNC);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000237
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000238 termios_err = tcgetattr(read_fd, &tty);
239 if (termios_err == 0) {
240 orig_tty = tty;
241 cfmakeraw(&tty);
242 tcsetattr(read_fd, TCSAFLUSH, &tty);
243 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000244
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000245 /* No SA_RESTART: we want ALRM to interrupt read() */
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000246 memset(&act, 0, sizeof(act));
247 act.sa_handler = sigalrm_handler;
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000248 sigaction(SIGALRM, &act, NULL);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000249
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000250 n = receive(file_fd);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000251
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000252 if (termios_err == 0)
253 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
254 if (ENABLE_FEATURE_CLEAN_UP)
255 close(file_fd);
256 fflush_stdout_and_exit(n >= 0);
Glenn L McGrath8f3bc4c2003-12-20 07:30:35 +0000257}