source: MondoRescue/branches/3.2/mindi-busybox/miscutils/rx.c

Last change on this file was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
File size: 6.1 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
[2725]2/*
[821]3 * Copyright: Copyright (C) 2001, Hewlett-Packard Company
4 * Author: Christopher Hoover <ch@hpl.hp.com>
5 * Description: xmodem functionality for uploading of kernels
6 * and the like
7 * Created at: Thu Dec 20 01:58:08 PST 2001
8 *
[2725]9 * xmodem functionality for uploading of kernels and the like
10 *
[821]11 * Copyright (C) 2001 Hewlett-Packard Laboratories
12 *
[2725]13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]14 *
15 * This was originally written for blob and then adapted for busybox.
16 */
17
[3232]18//usage:#define rx_trivial_usage
19//usage: "FILE"
20//usage:#define rx_full_usage "\n\n"
21//usage: "Receive a file using the xmodem protocol"
22//usage:
23//usage:#define rx_example_usage
24//usage: "$ rx /tmp/foo\n"
25
[1765]26#include "libbb.h"
[821]27
28#define SOH 0x01
29#define STX 0x02
30#define EOT 0x04
31#define ACK 0x06
32#define NAK 0x15
33#define BS 0x08
[2725]34#define PAD 0x1A
[821]35
36/*
37Cf:
38 http://www.textfiles.com/apple/xmodem
39 http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
40 http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
41 http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
42*/
43
44#define TIMEOUT 1
45#define TIMEOUT_LONG 10
46#define MAXERRORS 10
47
[2725]48#define read_fd STDIN_FILENO
49#define write_fd STDOUT_FILENO
50
51static int read_byte(unsigned timeout)
[1765]52{
[2725]53 unsigned char buf;
[821]54 int n;
55
56 alarm(timeout);
[2725]57 /* NOT safe_read! We want ALRM to interrupt us */
58 n = read(read_fd, &buf, 1);
[821]59 alarm(0);
60 if (n == 1)
[2725]61 return buf;
62 return -1;
[821]63}
64
[2725]65static int receive(/*int read_fd, */int file_fd)
[821]66{
[2725]67 unsigned char blockBuf[1024];
68 unsigned blockLength = 0;
69 unsigned errors = 0;
70 unsigned wantBlockNo = 1;
71 unsigned length = 0;
72 int do_crc = 1;
73 char reply_char;
74 unsigned timeout = TIMEOUT_LONG;
[821]75
76 /* Flush pending input */
[2725]77 tcflush(read_fd, TCIFLUSH);
[821]78
79 /* Ask for CRC; if we get errors, we will go with checksum */
[2725]80 reply_char = 'C';
81 full_write(write_fd, &reply_char, 1);
[821]82
83 for (;;) {
84 int blockBegin;
85 int blockNo, blockNoOnesCompl;
[2725]86 int cksum_or_crc;
87 int expected;
88 int i, j;
[821]89
[2725]90 blockBegin = read_byte(timeout);
[821]91 if (blockBegin < 0)
92 goto timeout;
93
[2725]94 /* If last block, remove padding */
95 if (blockBegin == EOT) {
96 /* Data blocks can be padded with ^Z characters */
97 /* This code tries to detect and remove them */
98 if (blockLength >= 3
99 && blockBuf[blockLength - 1] == PAD
100 && blockBuf[blockLength - 2] == PAD
101 && blockBuf[blockLength - 3] == PAD
102 ) {
103 while (blockLength
104 && blockBuf[blockLength - 1] == PAD
105 ) {
106 blockLength--;
107 }
108 }
109 }
110 /* Write previously received block */
[3232]111 errno = 0;
112 if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
113 bb_perror_msg(bb_msg_write_error);
114 goto fatal;
[2725]115 }
116
[821]117 timeout = TIMEOUT;
[2725]118 reply_char = NAK;
[821]119
120 switch (blockBegin) {
121 case SOH:
122 case STX:
123 break;
124 case EOT:
[2725]125 reply_char = ACK;
126 full_write(write_fd, &reply_char, 1);
127 return length;
[821]128 default:
129 goto error;
130 }
131
[2725]132 /* Block no */
133 blockNo = read_byte(TIMEOUT);
[821]134 if (blockNo < 0)
135 goto timeout;
136
[2725]137 /* Block no, in one's complement form */
138 blockNoOnesCompl = read_byte(TIMEOUT);
[821]139 if (blockNoOnesCompl < 0)
140 goto timeout;
141
142 if (blockNo != (255 - blockNoOnesCompl)) {
[2725]143 bb_error_msg("bad block ones compl");
[821]144 goto error;
145 }
146
147 blockLength = (blockBegin == SOH) ? 128 : 1024;
148
[2725]149 for (i = 0; i < blockLength; i++) {
150 int cc = read_byte(TIMEOUT);
151 if (cc < 0)
152 goto timeout;
153 blockBuf[i] = cc;
[821]154 }
155
[3232]156 cksum_or_crc = read_byte(TIMEOUT);
157 if (cksum_or_crc < 0)
158 goto timeout;
[2725]159 if (do_crc) {
160 cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT);
161 if (cksum_or_crc < 0)
[821]162 goto timeout;
163 }
164
165 if (blockNo == ((wantBlockNo - 1) & 0xff)) {
166 /* a repeat of the last block is ok, just ignore it. */
167 /* this also ignores the initial block 0 which is */
168 /* meta data. */
[3232]169 blockLength = 0;
[821]170 goto next;
[2725]171 }
172 if (blockNo != (wantBlockNo & 0xff)) {
173 bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
[821]174 goto error;
175 }
176
[2725]177 expected = 0;
178 if (do_crc) {
[821]179 for (i = 0; i < blockLength; i++) {
[2725]180 expected = expected ^ blockBuf[i] << 8;
181 for (j = 0; j < 8; j++) {
182 if (expected & 0x8000)
183 expected = (expected << 1) ^ 0x1021;
[821]184 else
[2725]185 expected = (expected << 1);
186 }
[821]187 }
[2725]188 expected &= 0xffff;
[821]189 } else {
190 for (i = 0; i < blockLength; i++)
[2725]191 expected += blockBuf[i];
192 expected &= 0xff;
[821]193 }
[2725]194 if (cksum_or_crc != expected) {
195 bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
[3232]196 : "checksum error, expected 0x%02x, got 0x%02x",
197 expected, cksum_or_crc);
[2725]198 goto error;
199 }
[821]200
201 wantBlockNo++;
202 length += blockLength;
[2725]203 next:
[821]204 errors = 0;
[2725]205 reply_char = ACK;
206 full_write(write_fd, &reply_char, 1);
[821]207 continue;
[2725]208 error:
209 timeout:
[3232]210 blockLength = 0;
[821]211 errors++;
212 if (errors == MAXERRORS) {
213 /* Abort */
214
[2725]215 /* If were asking for crc, try again w/o crc */
216 if (reply_char == 'C') {
217 reply_char = NAK;
[821]218 errors = 0;
[2725]219 do_crc = 0;
[821]220 goto timeout;
221 }
[2725]222 bb_error_msg("too many errors; giving up");
223 fatal:
224 /* 5 CAN followed by 5 BS. Don't try too hard... */
225 safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
[821]226 return -1;
227 }
228
229 /* Flush pending input */
[2725]230 tcflush(read_fd, TCIFLUSH);
[821]231
[2725]232 full_write(write_fd, &reply_char, 1);
233 } /* for (;;) */
[821]234}
235
[2725]236static void sigalrm_handler(int UNUSED_PARAM signum)
[821]237{
238}
239
[2725]240int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
241int rx_main(int argc UNUSED_PARAM, char **argv)
[821]242{
243 struct termios tty, orig_tty;
[2725]244 int termios_err;
245 int file_fd;
[821]246 int n;
247
[2725]248 /* Disabled by vda:
249 * why we can't receive from stdin? Why we *require*
250 * controlling tty?? */
251 /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
252 file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC);
[821]253
[2725]254 termios_err = tcgetattr(read_fd, &tty);
255 if (termios_err == 0) {
256 orig_tty = tty;
257 cfmakeraw(&tty);
258 tcsetattr(read_fd, TCSAFLUSH, &tty);
259 }
[821]260
[2725]261 /* No SA_RESTART: we want ALRM to interrupt read() */
262 signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
[821]263
[2725]264 n = receive(file_fd);
[821]265
[2725]266 if (termios_err == 0)
267 tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
268 if (ENABLE_FEATURE_CLEAN_UP)
269 close(file_fd);
270 fflush_stdout_and_exit(n >= 0);
[821]271}
Note: See TracBrowser for help on using the repository browser.