2 *==========================================================================
6 * RedBoot stream handler for xyzModem protocol
8 *==========================================================================
9 *####ECOSGPLCOPYRIGHTBEGIN####
10 * -------------------------------------------
11 * This file is part of eCos, the Embedded Configurable Operating System.
12 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 * Copyright (C) 2002 Gary Thomas
15 * eCos is free software; you can redistribute it and/or modify it under
16 * the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 or (at your option) any later version.
19 * eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with eCos; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 * As a special exception, if other files instantiate templates or use macros
29 * or inline functions from this file, or you compile this file and link it
30 * with other works to produce a work based on this file, this file does not
31 * by itself cause the resulting work to be covered by the GNU General Public
32 * License. However the source code for this file must still be made available
33 * in accordance with section (3) of the GNU General Public License.
35 * This exception does not invalidate any other reasons why a work based on
36 * this file might be covered by the GNU General Public License.
38 * Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
39 * at http: *sources.redhat.com/ecos/ecos-license/
40 * -------------------------------------------
41 *####ECOSGPLCOPYRIGHTEND####
42 *==========================================================================
43 *#####DESCRIPTIONBEGIN####
46 * Contributors: gthomas, tsmith, Yoshinori Sato
51 * This code is part of RedBoot (tm).
53 *####DESCRIPTIONEND####
55 *==========================================================================
62 /* Assumption - run xyzModem protocol over the console port */
64 /* Values magic to the protocol */
72 #define EOF 0x1A /* ^Z for DOS officionados */
74 #define USE_YMODEM_LENGTH
76 /* Data & state local to the protocol */
79 hal_virtual_comm_table_t* __chan;
83 unsigned char pkt[1024], *bufp;
84 unsigned char blk,cblk,crc1,crc2;
85 unsigned char next_blk; /* Expected block */
86 int len, mode, total_retries;
87 int total_SOH, total_STX, total_CAN;
88 bool crc_mode, at_eof, tx_ack;
89 #ifdef USE_YMODEM_LENGTH
90 unsigned long file_length, read_length;
94 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
95 #define xyzModem_MAX_RETRIES 20
96 #define xyzModem_MAX_RETRIES_WITH_CRC 10
97 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
100 #ifndef REDBOOT /*SB */
101 typedef int cyg_int32;
102 int CYGACC_COMM_IF_GETC_TIMEOUT (char chan,char *c) {
104 unsigned long counter=0;
105 while (!tstc() && (counter < xyzModem_CHAR_TIMEOUT*1000/DELAY)) {
116 void CYGACC_COMM_IF_PUTC(char x,char y) {
120 /* Validate a hex character */
121 __inline__ static bool
124 return (((c >= '0') && (c <= '9')) ||
125 ((c >= 'A') && (c <= 'F')) ||
126 ((c >= 'a') && (c <= 'f')));
129 /* Convert a single hex nibble */
130 __inline__ static int
135 if ((c >= '0') && (c <= '9')) {
137 } else if ((c >= 'a') && (c <= 'f')) {
138 ret = (c - 'a' + 0x0a);
139 } else if ((c >= 'A') && (c <= 'F')) {
140 ret = (c - 'A' + 0x0A);
145 /* Convert a character to lower case */
146 __inline__ static char
149 if ((c >= 'A') && (c <= 'Z')) {
155 /* Parse (scan) a number */
157 parse_num(char *s, unsigned long *val, char **es, char *delim)
162 unsigned long result = 0;
165 while (*s == ' ') s++;
167 if (first && (s[0] == '0') && (_tolower(s[1]) == 'x')) {
173 if (_is_hex(c) && ((digit = _from_hex(c)) < radix)) {
175 #ifdef CYGPKG_HAL_MIPS
176 /* FIXME: tx49 compiler generates 0x2539018 for MUL which */
177 /* isn't any good. */
179 result = result << 4;
181 result = 10 * result;
184 result = (result * radix) + digit;
187 if (delim != (char *)0) {
188 /* See if this character is one of the delimiters */
190 while (*dp && (c != *dp)) dp++;
191 if (*dp) break; /* Found a good delimiter */
193 return false; /* Malformatted number */
197 if (es != (char **)0) {
209 * Note: this debug setup only works if the target platform has two serial ports
210 * available so that the other one (currently only port 1) can be used for debug
214 zm_dprintf(char *fmt, ...)
221 cur_console = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
222 CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
224 diag_vprintf(fmt, args);
226 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur_console);
237 * Note: this debug setup works by storing the strings in a fixed buffer
241 static char *zm_out = (char *)0x00380000;
242 static char *zm_out_start = (char *)0x00380000;
244 static char zm_buf[8192];
245 static char *zm_out=zm_buf;
246 static char *zm_out_start = zm_buf;
250 zm_dprintf(char *fmt, ...)
256 len = diag_vsprintf(zm_out, fmt, args);
264 char *p = zm_out_start;
266 while (*p) mon_write_char(*p++);
268 zm_out = zm_out_start;
273 zm_dump_buf(void *buf, int len)
276 diag_vdump_buf_with_offset(zm_dprintf, buf, len, 0);
282 static unsigned char zm_buf[2048];
283 static unsigned char *zm_bp;
292 zm_save(unsigned char c)
300 zm_dprintf("Packet at line: %d\n", line);
301 zm_dump_buf(zm_buf, zm_bp-zm_buf);
304 #define ZM_DEBUG(x) x
309 /* Wait for the line to go idle */
316 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &c);
322 xyzModem_get_hdr(void)
326 bool hdr_found = false;
327 int i, can_total, hdr_chars;
328 unsigned short cksum;
331 /* Find the start of a header */
336 CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
340 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &c);
341 ZM_DEBUG(zm_save(c));
348 if (c == STX) xyz.total_STX++;
353 ZM_DEBUG(zm_dump(__LINE__));
354 if (++can_total == xyzModem_CAN_COUNT) {
355 return xyzModem_cancel;
357 /* Wait for multiple CAN to avoid early quits */
361 /* EOT only supported if no noise */
362 if (hdr_chars == 1) {
363 CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
364 ZM_DEBUG(zm_dprintf("ACK on EOT #%d\n", __LINE__));
365 ZM_DEBUG(zm_dump(__LINE__));
369 /* Ignore, waiting for start of header */
373 /* Data stream timed out */
374 xyzModem_flush(); /* Toss any current input */
375 ZM_DEBUG(zm_dump(__LINE__));
376 CYGACC_CALL_IF_DELAY_US((cyg_int32)250000);
377 return xyzModem_timeout;
381 /* Header found, now read the data */
382 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &xyz.blk);
383 ZM_DEBUG(zm_save(xyz.blk));
385 ZM_DEBUG(zm_dump(__LINE__));
386 return xyzModem_timeout;
388 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &xyz.cblk);
389 ZM_DEBUG(zm_save(xyz.cblk));
391 ZM_DEBUG(zm_dump(__LINE__));
392 return xyzModem_timeout;
394 xyz.len = (c == SOH) ? 128 : 1024;
396 for (i = 0; i < xyz.len; i++) {
397 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &c);
398 ZM_DEBUG(zm_save(c));
402 ZM_DEBUG(zm_dump(__LINE__));
403 return xyzModem_timeout;
406 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &xyz.crc1);
407 ZM_DEBUG(zm_save(xyz.crc1));
409 ZM_DEBUG(zm_dump(__LINE__));
410 return xyzModem_timeout;
413 res = CYGACC_COMM_IF_GETC_TIMEOUT(*xyz.__chan, &xyz.crc2);
414 ZM_DEBUG(zm_save(xyz.crc2));
416 ZM_DEBUG(zm_dump(__LINE__));
417 return xyzModem_timeout;
420 ZM_DEBUG(zm_dump(__LINE__));
421 /* Validate the message */
422 if ((xyz.blk ^ xyz.cblk) != (unsigned char)0xFF) {
423 ZM_DEBUG(zm_dprintf("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk, (xyz.blk ^ xyz.cblk)));
424 ZM_DEBUG(zm_dump_buf(xyz.pkt, xyz.len));
426 return xyzModem_frame;
428 /* Verify checksum/CRC */
430 cksum = cyg_crc16(xyz.pkt, xyz.len);
431 if (cksum != ((xyz.crc1 << 8) | xyz.crc2)) {
432 ZM_DEBUG(zm_dprintf("CRC error - recvd: %02x%02x, computed: %x\n",
433 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
434 return xyzModem_cksum;
438 for (i = 0; i < xyz.len; i++) {
441 if (xyz.crc1 != (cksum & 0xFF)) {
442 ZM_DEBUG(zm_dprintf("Checksum error - recvd: %x, computed: %x\n", xyz.crc1, cksum & 0xFF));
443 return xyzModem_cksum;
446 /* If we get here, the message passes [structural] muster */
451 xyzModem_stream_open(connection_info_t *info, int *err)
453 int console_chan, stat=0;
454 int retries = xyzModem_MAX_RETRIES;
455 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
457 /* ZM_DEBUG(zm_out = zm_out_start); */
458 #ifdef xyzModem_zmodem
459 if (info->mode == xyzModem_zmodem) {
460 *err = xyzModem_noZmodem;
466 /* Set up the I/O channel. Note: this allows for using a different port in the future */
467 console_chan = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
468 if (info->chan >= 0) {
469 CYGACC_CALL_IF_SET_CONSOLE_COMM(info->chan);
471 CYGACC_CALL_IF_SET_CONSOLE_COMM(console_chan);
473 xyz.__chan = CYGACC_CALL_IF_CONSOLE_PROCS();
475 CYGACC_CALL_IF_SET_CONSOLE_COMM(console_chan);
476 CYGACC_COMM_IF_CONTROL(*xyz.__chan, __COMMCTL_SET_TIMEOUT, xyzModem_CHAR_TIMEOUT);
486 xyz.mode = info->mode;
487 xyz.total_retries = 0;
491 #ifdef USE_YMODEM_LENGTH
496 CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
498 if (xyz.mode == xyzModem_xmodem) {
499 /* X-modem doesn't have an information header - exit here */
504 while (retries-- > 0) {
505 stat = xyzModem_get_hdr();
507 /* Y-modem file information header */
509 #ifdef USE_YMODEM_LENGTH
513 parse_num(xyz.bufp, &xyz.file_length, NULL, " ");
515 /* The rest of the file name data block quietly discarded */
522 if (stat == xyzModem_timeout) {
523 if (--crc_retries <= 0) xyz.crc_mode = false;
524 CYGACC_CALL_IF_DELAY_US(5*100000); /* Extra delay for startup */
525 CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
527 ZM_DEBUG(zm_dprintf("NAK (%d)\n", __LINE__));
529 if (stat == xyzModem_cancel) {
534 ZM_DEBUG(zm_flush());
539 xyzModem_stream_read(char *buf, int size, int *err)
541 int stat, total, len;
545 stat = xyzModem_cancel;
546 /* Try and get 'size' bytes into the buffer */
547 while (!xyz.at_eof && (size > 0)) {
549 retries = xyzModem_MAX_RETRIES;
550 while (retries-- > 0) {
551 stat = xyzModem_get_hdr();
553 if (xyz.blk == xyz.next_blk) {
555 ZM_DEBUG(zm_dprintf("ACK block %d (%d)\n", xyz.blk, __LINE__));
556 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
558 #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
559 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) {
563 /* Data blocks can be padded with ^Z (EOF) characters */
564 /* This code tries to detect and remove them */
565 if ((xyz.bufp[xyz.len-1] == EOF) &&
566 (xyz.bufp[xyz.len-2] == EOF) &&
567 (xyz.bufp[xyz.len-3] == EOF)) {
568 while (xyz.len && (xyz.bufp[xyz.len-1] == EOF)) {
574 #ifdef USE_YMODEM_LENGTH
576 * See if accumulated length exceeds that of the file.
577 * If so, reduce size (i.e., cut out pad bytes)
578 * Only do this for Y-modem (and Z-modem should it ever
579 * be supported since it can fall back to Y-modem mode).
581 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length) {
582 xyz.read_length += xyz.len;
583 if (xyz.read_length > xyz.file_length) {
584 xyz.len -= (xyz.read_length - xyz.file_length);
589 } else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF)) {
590 /* Just re-ACK this so sender will get on with it */
591 CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
592 continue; /* Need new header */
594 stat = xyzModem_sequence;
597 if (stat == xyzModem_cancel) {
600 if (stat == xyzModem_eof) {
601 CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
602 ZM_DEBUG(zm_dprintf("ACK (%d)\n", __LINE__));
603 if (xyz.mode == xyzModem_ymodem) {
604 CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
606 ZM_DEBUG(zm_dprintf("Reading Final Header\n"));
607 stat = xyzModem_get_hdr();
608 CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
609 ZM_DEBUG(zm_dprintf("FINAL ACK (%d)\n", __LINE__));
614 CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
616 ZM_DEBUG(zm_dprintf("NAK (%d)\n", __LINE__));
624 /* Don't "read" data from the EOF protocol package */
627 if (size < len) len = size;
628 memcpy(buf, xyz.bufp, len);
640 xyzModem_stream_close(int *err)
642 diag_printf("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
643 xyz.crc_mode ? "CRC" : "Cksum",
644 xyz.total_SOH, xyz.total_STX, xyz.total_CAN,
646 ZM_DEBUG(zm_flush());
649 /* Need to be able to clean out the input buffer, so have to take the */
651 void xyzModem_stream_terminate(bool abort, int (*getc)(void))
656 ZM_DEBUG(zm_dprintf("!!!! TRANSFER ABORT !!!!\n"));
658 case xyzModem_xmodem:
659 case xyzModem_ymodem:
660 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
661 /* number of Backspaces is a friendly way to get the other end to abort. */
662 CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
663 CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
664 CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
665 CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
666 CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
667 CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
668 CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
669 CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
670 /* Now consume the rest of what's waiting on the line. */
671 ZM_DEBUG(zm_dprintf("Flushing serial line.\n"));
675 #ifdef xyzModem_zmodem
676 case xyzModem_zmodem:
677 /* Might support it some day I suppose. */
682 ZM_DEBUG(zm_dprintf("Engaging cleanup mode...\n"));
684 * Consume any trailing crap left in the inbuffer from
685 * previous recieved blocks. Since very few files are an exact multiple
686 * of the transfer block size, there will almost always be some gunk here.
687 * If we don't eat it now, RedBoot will think the user typed it.
689 ZM_DEBUG(zm_dprintf("Trailing gunk:\n"));
690 while ((c = (*getc)()) > -1) ;
691 ZM_DEBUG(zm_dprintf("\n"));
693 * Make a small delay to give terminal programs like minicom
694 * time to get control again after their file transfer program
697 CYGACC_CALL_IF_DELAY_US((cyg_int32)250000);
702 xyzModem_error(int err)
705 case xyzModem_access:
706 return "Can't access file";
708 case xyzModem_noZmodem:
709 return "Sorry, zModem not available yet";
711 case xyzModem_timeout:
715 return "End of file";
717 case xyzModem_cancel:
721 return "Invalid framing";
724 return "CRC/checksum error";
726 case xyzModem_sequence:
727 return "Block sequence error";
730 return "Unknown error";
739 GETC_IO_FUNCS(xyzModem_io, xyzModem_stream_open, xyzModem_stream_close,
740 xyzModem_stream_terminate, xyzModem_stream_read, xyzModem_error);
741 RedBoot_load(xmodem, xyzModem_io, false, false, xyzModem_xmodem);
742 RedBoot_load(ymodem, xyzModem_io, false, false, xyzModem_ymodem);