source: branches/stable/mindi-busybox/miscutils/rx.c @ 1770

Last change on this file since 1770 was 1770, checked in by Bruno Cornec, 13 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod? silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian? keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René? Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File size: 5.7 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*-------------------------------------------------------------------------
3 * Filename:      xmodem.c
4 * Copyright:     Copyright (C) 2001, Hewlett-Packard Company
5 * Author:        Christopher Hoover <ch@hpl.hp.com>
6 * Description:   xmodem functionality for uploading of kernels
7 *                and the like
8 * Created at:    Thu Dec 20 01:58:08 PST 2001
9 *-----------------------------------------------------------------------*/
10/*
11 * xmodem.c: xmodem functionality for uploading of kernels and
12 *            the like
13 *
14 * Copyright (C) 2001 Hewlett-Packard Laboratories
15 *
16 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 *
18 * This was originally written for blob and then adapted for busybox.
19 *
20 */
21
22#include "libbb.h"
23
24#define SOH 0x01
25#define STX 0x02
26#define EOT 0x04
27#define ACK 0x06
28#define NAK 0x15
29#define BS  0x08
30
31/*
32
33Cf:
34
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
39
40*/
41
42#define TIMEOUT 1
43#define TIMEOUT_LONG 10
44#define MAXERRORS 10
45
46static int read_byte(int fd, unsigned int timeout)
47{
48    char buf[1];
49    int n;
50
51    alarm(timeout);
52
53    n = read(fd, &buf, 1);
54
55    alarm(0);
56
57    if (n == 1)
58        return buf[0] & 0xff;
59    else
60        return -1;
61}
62
63static int receive(char *error_buf, size_t error_buf_size,
64                   int ttyfd, int filefd)
65{
66    char blockBuf[1024];
67    unsigned int errors = 0;
68    unsigned int wantBlockNo = 1;
69    unsigned int length = 0;
70    int docrc = 1;
71    char nak = 'C';
72    unsigned int timeout = TIMEOUT_LONG;
73
74#define note_error(fmt,args...) \
75    snprintf(error_buf, error_buf_size, fmt,##args)
76
77    /* Flush pending input */
78    tcflush(ttyfd, TCIFLUSH);
79
80    /* Ask for CRC; if we get errors, we will go with checksum */
81    write(ttyfd, &nak, 1);
82
83    for (;;) {
84        int blockBegin;
85        int blockNo, blockNoOnesCompl;
86        int blockLength;
87        int cksum = 0;
88        int crcHi = 0;
89        int crcLo = 0;
90
91        blockBegin = read_byte(ttyfd, timeout);
92        if (blockBegin < 0)
93            goto timeout;
94
95        timeout = TIMEOUT;
96        nak = NAK;
97
98        switch (blockBegin) {
99        case SOH:
100        case STX:
101            break;
102
103        case EOT:
104            nak = ACK;
105            write(ttyfd, &nak, 1);
106            goto done;
107
108        default:
109            goto error;
110        }
111
112        /* block no */
113        blockNo = read_byte(ttyfd, TIMEOUT);
114        if (blockNo < 0)
115            goto timeout;
116
117        /* block no one's compliment */
118        blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
119        if (blockNoOnesCompl < 0)
120            goto timeout;
121
122        if (blockNo != (255 - blockNoOnesCompl)) {
123            note_error("bad block ones compl");
124            goto error;
125        }
126
127        blockLength = (blockBegin == SOH) ? 128 : 1024;
128
129        {
130            int i;
131
132            for (i = 0; i < blockLength; i++) {
133                int cc = read_byte(ttyfd, TIMEOUT);
134                if (cc < 0)
135                    goto timeout;
136                blockBuf[i] = cc;
137            }
138        }
139
140        if (docrc) {
141            crcHi = read_byte(ttyfd, TIMEOUT);
142            if (crcHi < 0)
143                goto timeout;
144
145            crcLo = read_byte(ttyfd, TIMEOUT);
146            if (crcLo < 0)
147                goto timeout;
148        } else {
149            cksum = read_byte(ttyfd, TIMEOUT);
150            if (cksum < 0)
151                goto timeout;
152        }
153
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 */
157            /* meta data. */
158            goto next;
159        } else if (blockNo != (wantBlockNo & 0xff)) {
160            note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
161            goto error;
162        }
163
164        if (docrc) {
165            int crc = 0;
166            int i, j;
167            int expectedCrcHi;
168            int expectedCrcLo;
169
170            for (i = 0; i < blockLength; i++) {
171                crc = crc ^ (int) blockBuf[i] << 8;
172                for (j = 0; j < 8; j++)
173                    if (crc & 0x8000)
174                        crc = crc << 1 ^ 0x1021;
175                    else
176                        crc = crc << 1;
177            }
178
179            expectedCrcHi = (crc >> 8) & 0xff;
180            expectedCrcLo = crc & 0xff;
181
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);
185                goto error;
186            }
187        } else {
188            unsigned char expectedCksum = 0;
189            int i;
190
191            for (i = 0; i < blockLength; i++)
192                expectedCksum += blockBuf[i];
193
194            if (cksum != expectedCksum) {
195                note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
196                goto error;
197            }
198        }
199
200        wantBlockNo++;
201        length += blockLength;
202
203        if (full_write(filefd, blockBuf, blockLength) < 0) {
204            note_error("write to file failed: %m");
205            goto fatal;
206        }
207
208    next:
209        errors = 0;
210        nak = ACK;
211        write(ttyfd, &nak, 1);
212        continue;
213
214    error:
215    timeout:
216        errors++;
217        if (errors == MAXERRORS) {
218            /* Abort */
219
220            // if using crc, try again w/o crc
221            if (nak == 'C') {
222                nak = NAK;
223                errors = 0;
224                docrc = 0;
225                goto timeout;
226            }
227
228            note_error("too many errors; giving up");
229
230        fatal:
231            /* 5 CAN followed by 5 BS */
232            write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
233            return -1;
234        }
235
236        /* Flush pending input */
237        tcflush(ttyfd, TCIFLUSH);
238
239        write(ttyfd, &nak, 1);
240    }
241
242 done:
243    return length;
244
245#undef note_error
246}
247
248static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
249{
250}
251
252int rx_main(int argc, char **argv);
253int rx_main(int argc, char **argv)
254{
255    char *fn;
256    int ttyfd, filefd;
257    struct termios tty, orig_tty;
258    struct sigaction act;
259    int n;
260    char error_buf[256];
261
262    if (argc != 2)
263            bb_show_usage();
264
265    fn = argv[1];
266    ttyfd = xopen(CURRENT_TTY, O_RDWR);
267    filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
268
269    if (tcgetattr(ttyfd, &tty) < 0)
270            bb_perror_msg_and_die("tcgetattr");
271
272    orig_tty = tty;
273
274    cfmakeraw(&tty);
275    tcsetattr(ttyfd, TCSAFLUSH, &tty);
276
277    memset(&act, 0, sizeof(act));
278    act.sa_handler = sigalrm_handler;
279    sigaction(SIGALRM, &act, 0);
280
281    n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
282
283    close(filefd);
284
285    tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
286
287    if (n < 0)
288        bb_error_msg_and_die("\nreceive failed:\n  %s", error_buf);
289
290    fflush_stdout_and_exit(EXIT_SUCCESS);
291}
Note: See TracBrowser for help on using the repository browser.