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 */
80 hal_virtual_comm_table_t
*__chan
;
84 unsigned char pkt
[1024], *bufp
;
85 unsigned char blk
, cblk
, crc1
, crc2
;
86 unsigned char next_blk
; /* Expected block */
87 int len
, mode
, total_retries
;
88 int total_SOH
, total_STX
, total_CAN
;
89 bool crc_mode
, at_eof
, tx_ack
;
90 #ifdef USE_YMODEM_LENGTH
91 unsigned long file_length
, read_length
;
95 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
96 #define xyzModem_MAX_RETRIES 20
97 #define xyzModem_MAX_RETRIES_WITH_CRC 10
98 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
101 #ifndef REDBOOT /*SB */
102 typedef int cyg_int32
;
104 CYGACC_COMM_IF_GETC_TIMEOUT (char chan
, char *c
)
107 unsigned long counter
= 0;
108 while (!tstc () && (counter
< xyzModem_CHAR_TIMEOUT
* 1000 / DELAY
))
122 CYGACC_COMM_IF_PUTC (char x
, char y
)
127 /* Validate a hex character */
128 __inline__
static bool
131 return (((c
>= '0') && (c
<= '9')) ||
132 ((c
>= 'A') && (c
<= 'F')) || ((c
>= 'a') && (c
<= 'f')));
135 /* Convert a single hex nibble */
136 __inline__
static int
141 if ((c
>= '0') && (c
<= '9'))
145 else if ((c
>= 'a') && (c
<= 'f'))
147 ret
= (c
- 'a' + 0x0a);
149 else if ((c
>= 'A') && (c
<= 'F'))
151 ret
= (c
- 'A' + 0x0A);
156 /* Convert a character to lower case */
157 __inline__
static char
160 if ((c
>= 'A') && (c
<= 'Z'))
167 /* Parse (scan) a number */
169 parse_num (char *s
, unsigned long *val
, char **es
, char *delim
)
174 unsigned long result
= 0;
181 if (first
&& (s
[0] == '0') && (_tolower (s
[1]) == 'x'))
188 if (_is_hex (c
) && ((digit
= _from_hex (c
)) < radix
))
191 #ifdef CYGPKG_HAL_MIPS
192 /* FIXME: tx49 compiler generates 0x2539018 for MUL which */
193 /* isn't any good. */
195 result
= result
<< 4;
197 result
= 10 * result
;
200 result
= (result
* radix
) + digit
;
205 if (delim
!= (char *) 0)
207 /* See if this character is one of the delimiters */
209 while (*dp
&& (c
!= *dp
))
212 break; /* Found a good delimiter */
214 return false; /* Malformatted number */
218 if (es
!= (char **) 0)
231 * Note: this debug setup only works if the target platform has two serial ports
232 * available so that the other one (currently only port 1) can be used for debug
236 zm_dprintf (char *fmt
, ...)
241 va_start (args
, fmt
);
244 CYGACC_CALL_IF_SET_CONSOLE_COMM
245 (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT
);
246 CYGACC_CALL_IF_SET_CONSOLE_COMM (1);
248 diag_vprintf (fmt
, args
);
250 CYGACC_CALL_IF_SET_CONSOLE_COMM (cur_console
);
261 * Note: this debug setup works by storing the strings in a fixed buffer
265 static char *zm_out
= (char *) 0x00380000;
266 static char *zm_out_start
= (char *) 0x00380000;
268 static char zm_buf
[8192];
269 static char *zm_out
= zm_buf
;
270 static char *zm_out_start
= zm_buf
;
274 zm_dprintf (char *fmt
, ...)
279 va_start (args
, fmt
);
280 len
= diag_vsprintf (zm_out
, fmt
, args
);
289 char *p
= zm_out_start
;
291 mon_write_char (*p
++);
293 zm_out
= zm_out_start
;
298 zm_dump_buf (void *buf
, int len
)
301 diag_vdump_buf_with_offset (zm_dprintf
, buf
, len
, 0);
307 static unsigned char zm_buf
[2048];
308 static unsigned char *zm_bp
;
317 zm_save (unsigned char c
)
325 zm_dprintf ("Packet at line: %d\n", line
);
326 zm_dump_buf (zm_buf
, zm_bp
- zm_buf
);
329 #define ZM_DEBUG(x) x
334 /* Wait for the line to go idle */
336 xyzModem_flush (void)
342 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
349 xyzModem_get_hdr (void)
353 bool hdr_found
= false;
354 int i
, can_total
, hdr_chars
;
355 unsigned short cksum
;
357 ZM_DEBUG (zm_new ());
358 /* Find the start of a header */
364 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
369 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
370 ZM_DEBUG (zm_save (c
));
385 ZM_DEBUG (zm_dump (__LINE__
));
386 if (++can_total
== xyzModem_CAN_COUNT
)
388 return xyzModem_cancel
;
392 /* Wait for multiple CAN to avoid early quits */
396 /* EOT only supported if no noise */
399 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
400 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__
));
401 ZM_DEBUG (zm_dump (__LINE__
));
405 /* Ignore, waiting for start of header */
411 /* Data stream timed out */
412 xyzModem_flush (); /* Toss any current input */
413 ZM_DEBUG (zm_dump (__LINE__
));
414 CYGACC_CALL_IF_DELAY_US ((cyg_int32
) 250000);
415 return xyzModem_timeout
;
419 /* Header found, now read the data */
420 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.blk
);
421 ZM_DEBUG (zm_save (xyz
.blk
));
424 ZM_DEBUG (zm_dump (__LINE__
));
425 return xyzModem_timeout
;
427 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.cblk
);
428 ZM_DEBUG (zm_save (xyz
.cblk
));
431 ZM_DEBUG (zm_dump (__LINE__
));
432 return xyzModem_timeout
;
434 xyz
.len
= (c
== SOH
) ? 128 : 1024;
436 for (i
= 0; i
< xyz
.len
; i
++)
438 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
439 ZM_DEBUG (zm_save (c
));
446 ZM_DEBUG (zm_dump (__LINE__
));
447 return xyzModem_timeout
;
450 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.crc1
);
451 ZM_DEBUG (zm_save (xyz
.crc1
));
454 ZM_DEBUG (zm_dump (__LINE__
));
455 return xyzModem_timeout
;
459 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.crc2
);
460 ZM_DEBUG (zm_save (xyz
.crc2
));
463 ZM_DEBUG (zm_dump (__LINE__
));
464 return xyzModem_timeout
;
467 ZM_DEBUG (zm_dump (__LINE__
));
468 /* Validate the message */
469 if ((xyz
.blk
^ xyz
.cblk
) != (unsigned char) 0xFF)
472 ("Framing error - blk: %x/%x/%x\n", xyz
.blk
, xyz
.cblk
,
473 (xyz
.blk
^ xyz
.cblk
)));
474 ZM_DEBUG (zm_dump_buf (xyz
.pkt
, xyz
.len
));
476 return xyzModem_frame
;
478 /* Verify checksum/CRC */
481 cksum
= cyg_crc16 (xyz
.pkt
, xyz
.len
);
482 if (cksum
!= ((xyz
.crc1
<< 8) | xyz
.crc2
))
484 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
485 xyz
.crc1
, xyz
.crc2
, cksum
& 0xFFFF));
486 return xyzModem_cksum
;
492 for (i
= 0; i
< xyz
.len
; i
++)
496 if (xyz
.crc1
!= (cksum
& 0xFF))
499 ("Checksum error - recvd: %x, computed: %x\n", xyz
.crc1
,
501 return xyzModem_cksum
;
504 /* If we get here, the message passes [structural] muster */
509 xyzModem_stream_open (connection_info_t
* info
, int *err
)
515 int retries
= xyzModem_MAX_RETRIES
;
516 int crc_retries
= xyzModem_MAX_RETRIES_WITH_CRC
;
518 /* ZM_DEBUG(zm_out = zm_out_start); */
519 #ifdef xyzModem_zmodem
520 if (info
->mode
== xyzModem_zmodem
)
522 *err
= xyzModem_noZmodem
;
528 /* Set up the I/O channel. Note: this allows for using a different port in the future */
530 CYGACC_CALL_IF_SET_CONSOLE_COMM
531 (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT
);
534 CYGACC_CALL_IF_SET_CONSOLE_COMM (info
->chan
);
538 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan
);
540 xyz
.__chan
= CYGACC_CALL_IF_CONSOLE_PROCS ();
542 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan
);
543 CYGACC_COMM_IF_CONTROL (*xyz
.__chan
, __COMMCTL_SET_TIMEOUT
,
544 xyzModem_CHAR_TIMEOUT
);
554 xyz
.mode
= info
->mode
;
555 xyz
.total_retries
= 0;
559 #ifdef USE_YMODEM_LENGTH
564 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
566 if (xyz
.mode
== xyzModem_xmodem
)
568 /* X-modem doesn't have an information header - exit here */
573 while (retries
-- > 0)
575 stat
= xyzModem_get_hdr ();
578 /* Y-modem file information header */
581 #ifdef USE_YMODEM_LENGTH
585 parse_num ((char *) xyz
.bufp
, &xyz
.file_length
, NULL
, " ");
587 /* The rest of the file name data block quietly discarded */
594 else if (stat
== xyzModem_timeout
)
596 if (--crc_retries
<= 0)
597 xyz
.crc_mode
= false;
598 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
599 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
601 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__
));
603 if (stat
== xyzModem_cancel
)
609 ZM_DEBUG (zm_flush ());
614 xyzModem_stream_read (char *buf
, int size
, int *err
)
616 int stat
, total
, len
;
620 stat
= xyzModem_cancel
;
621 /* Try and get 'size' bytes into the buffer */
622 while (!xyz
.at_eof
&& (size
> 0))
626 retries
= xyzModem_MAX_RETRIES
;
627 while (retries
-- > 0)
629 stat
= xyzModem_get_hdr ();
632 if (xyz
.blk
== xyz
.next_blk
)
636 ("ACK block %d (%d)\n", xyz
.blk
, __LINE__
));
637 xyz
.next_blk
= (xyz
.next_blk
+ 1) & 0xFF;
639 #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
640 if (xyz
.mode
== xyzModem_xmodem
|| xyz
.file_length
== 0)
646 /* Data blocks can be padded with ^Z (EOF) characters */
647 /* This code tries to detect and remove them */
648 if ((xyz
.bufp
[xyz
.len
- 1] == EOF
) &&
649 (xyz
.bufp
[xyz
.len
- 2] == EOF
) &&
650 (xyz
.bufp
[xyz
.len
- 3] == EOF
))
653 && (xyz
.bufp
[xyz
.len
- 1] == EOF
))
660 #ifdef USE_YMODEM_LENGTH
662 * See if accumulated length exceeds that of the file.
663 * If so, reduce size (i.e., cut out pad bytes)
664 * Only do this for Y-modem (and Z-modem should it ever
665 * be supported since it can fall back to Y-modem mode).
667 if (xyz
.mode
!= xyzModem_xmodem
&& 0 != xyz
.file_length
)
669 xyz
.read_length
+= xyz
.len
;
670 if (xyz
.read_length
> xyz
.file_length
)
672 xyz
.len
-= (xyz
.read_length
- xyz
.file_length
);
678 else if (xyz
.blk
== ((xyz
.next_blk
- 1) & 0xFF))
680 /* Just re-ACK this so sender will get on with it */
681 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
682 continue; /* Need new header */
686 stat
= xyzModem_sequence
;
689 if (stat
== xyzModem_cancel
)
693 if (stat
== xyzModem_eof
)
695 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
696 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__
));
697 if (xyz
.mode
== xyzModem_ymodem
)
699 CYGACC_COMM_IF_PUTC (*xyz
.__chan
,
700 (xyz
.crc_mode
? 'C' : NAK
));
702 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
703 stat
= xyzModem_get_hdr ();
704 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
705 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__
));
710 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
712 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__
));
721 /* Don't "read" data from the EOF protocol package */
727 memcpy (buf
, xyz
.bufp
, len
);
739 xyzModem_stream_close (int *err
)
742 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
743 xyz
.crc_mode
? "CRC" : "Cksum", xyz
.total_SOH
, xyz
.total_STX
,
744 xyz
.total_CAN
, xyz
.total_retries
);
745 ZM_DEBUG (zm_flush ());
748 /* Need to be able to clean out the input buffer, so have to take the */
751 xyzModem_stream_terminate (bool abort
, int (*getc
) (void))
757 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
760 case xyzModem_xmodem
:
761 case xyzModem_ymodem
:
762 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
763 /* number of Backspaces is a friendly way to get the other end to abort. */
764 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
765 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
766 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
767 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
768 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
769 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
770 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
771 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
772 /* Now consume the rest of what's waiting on the line. */
773 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
777 #ifdef xyzModem_zmodem
778 case xyzModem_zmodem
:
779 /* Might support it some day I suppose. */
786 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
788 * Consume any trailing crap left in the inbuffer from
789 * previous recieved blocks. Since very few files are an exact multiple
790 * of the transfer block size, there will almost always be some gunk here.
791 * If we don't eat it now, RedBoot will think the user typed it.
793 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
794 while ((c
= (*getc
) ()) > -1);
795 ZM_DEBUG (zm_dprintf ("\n"));
797 * Make a small delay to give terminal programs like minicom
798 * time to get control again after their file transfer program
801 CYGACC_CALL_IF_DELAY_US ((cyg_int32
) 250000);
806 xyzModem_error (int err
)
810 case xyzModem_access
:
811 return "Can't access file";
813 case xyzModem_noZmodem
:
814 return "Sorry, zModem not available yet";
816 case xyzModem_timeout
:
820 return "End of file";
822 case xyzModem_cancel
:
826 return "Invalid framing";
829 return "CRC/checksum error";
831 case xyzModem_sequence
:
832 return "Block sequence error";
835 return "Unknown error";
844 GETC_IO_FUNCS (xyzModem_io
, xyzModem_stream_open
, xyzModem_stream_close
,
845 xyzModem_stream_terminate
, xyzModem_stream_read
,
847 RedBoot_load (xmodem
, xyzModem_io
, false, false, xyzModem_xmodem
);
848 RedBoot_load (ymodem
, xyzModem_io
, false, false, xyzModem_ymodem
);