2 *==========================================================================
6 * RedBoot stream handler for xyzModem protocol
8 *==========================================================================
9 * SPDX-License-Identifier: eCos-2.0
10 *==========================================================================
11 *#####DESCRIPTIONBEGIN####
14 * Contributors: gthomas, tsmith, Yoshinori Sato
19 * This code is part of RedBoot (tm).
21 *####DESCRIPTIONEND####
23 *==========================================================================
30 /* Assumption - run xyzModem protocol over the console port */
32 /* Values magic to the protocol */
40 #define EOF 0x1A /* ^Z for DOS officionados */
42 #define USE_YMODEM_LENGTH
44 /* Data & state local to the protocol */
48 hal_virtual_comm_table_t
*__chan
;
52 unsigned char pkt
[1024], *bufp
;
53 unsigned char blk
, cblk
, crc1
, crc2
;
54 unsigned char next_blk
; /* Expected block */
55 int len
, mode
, total_retries
;
56 int total_SOH
, total_STX
, total_CAN
;
57 bool crc_mode
, at_eof
, tx_ack
;
58 #ifdef USE_YMODEM_LENGTH
59 unsigned long file_length
, read_length
;
63 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
64 #define xyzModem_MAX_RETRIES 20
65 #define xyzModem_MAX_RETRIES_WITH_CRC 10
66 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
69 #ifndef REDBOOT /*SB */
70 typedef int cyg_int32
;
72 CYGACC_COMM_IF_GETC_TIMEOUT (char chan
, char *c
)
75 unsigned long counter
= 0;
76 while (!tstc () && (counter
< xyzModem_CHAR_TIMEOUT
* 1000 / DELAY
))
90 CYGACC_COMM_IF_PUTC (char x
, char y
)
95 /* Validate a hex character */
96 __inline__
static bool
99 return (((c
>= '0') && (c
<= '9')) ||
100 ((c
>= 'A') && (c
<= 'F')) || ((c
>= 'a') && (c
<= 'f')));
103 /* Convert a single hex nibble */
104 __inline__
static int
109 if ((c
>= '0') && (c
<= '9'))
113 else if ((c
>= 'a') && (c
<= 'f'))
115 ret
= (c
- 'a' + 0x0a);
117 else if ((c
>= 'A') && (c
<= 'F'))
119 ret
= (c
- 'A' + 0x0A);
124 /* Convert a character to lower case */
125 __inline__
static char
128 if ((c
>= 'A') && (c
<= 'Z'))
135 /* Parse (scan) a number */
137 parse_num (char *s
, unsigned long *val
, char **es
, char *delim
)
142 unsigned long result
= 0;
149 if (first
&& (s
[0] == '0') && (_tolower (s
[1]) == 'x'))
156 if (_is_hex (c
) && ((digit
= _from_hex (c
)) < radix
))
159 #ifdef CYGPKG_HAL_MIPS
160 /* FIXME: tx49 compiler generates 0x2539018 for MUL which */
161 /* isn't any good. */
163 result
= result
<< 4;
165 result
= 10 * result
;
168 result
= (result
* radix
) + digit
;
173 if (delim
!= (char *) 0)
175 /* See if this character is one of the delimiters */
177 while (*dp
&& (c
!= *dp
))
180 break; /* Found a good delimiter */
182 return false; /* Malformatted number */
186 if (es
!= (char **) 0)
199 * Note: this debug setup only works if the target platform has two serial ports
200 * available so that the other one (currently only port 1) can be used for debug
204 zm_dprintf (char *fmt
, ...)
209 va_start (args
, fmt
);
212 CYGACC_CALL_IF_SET_CONSOLE_COMM
213 (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT
);
214 CYGACC_CALL_IF_SET_CONSOLE_COMM (1);
216 diag_vprintf (fmt
, args
);
218 CYGACC_CALL_IF_SET_CONSOLE_COMM (cur_console
);
229 * Note: this debug setup works by storing the strings in a fixed buffer
233 static char *zm_out
= (char *) 0x00380000;
234 static char *zm_out_start
= (char *) 0x00380000;
236 static char zm_buf
[8192];
237 static char *zm_out
= zm_buf
;
238 static char *zm_out_start
= zm_buf
;
242 zm_dprintf (char *fmt
, ...)
247 va_start (args
, fmt
);
248 len
= diag_vsprintf (zm_out
, fmt
, args
);
257 char *p
= zm_out_start
;
259 mon_write_char (*p
++);
261 zm_out
= zm_out_start
;
266 zm_dump_buf (void *buf
, int len
)
269 diag_vdump_buf_with_offset (zm_dprintf
, buf
, len
, 0);
275 static unsigned char zm_buf
[2048];
276 static unsigned char *zm_bp
;
285 zm_save (unsigned char c
)
293 zm_dprintf ("Packet at line: %d\n", line
);
294 zm_dump_buf (zm_buf
, zm_bp
- zm_buf
);
297 #define ZM_DEBUG(x) x
302 /* Wait for the line to go idle */
304 xyzModem_flush (void)
310 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
317 xyzModem_get_hdr (void)
321 bool hdr_found
= false;
322 int i
, can_total
, hdr_chars
;
323 unsigned short cksum
;
325 ZM_DEBUG (zm_new ());
326 /* Find the start of a header */
332 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
337 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
338 ZM_DEBUG (zm_save (c
));
353 ZM_DEBUG (zm_dump (__LINE__
));
354 if (++can_total
== xyzModem_CAN_COUNT
)
356 return xyzModem_cancel
;
360 /* Wait for multiple CAN to avoid early quits */
364 /* EOT only supported if no noise */
367 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
368 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__
));
369 ZM_DEBUG (zm_dump (__LINE__
));
373 /* Ignore, waiting for start of header */
379 /* Data stream timed out */
380 xyzModem_flush (); /* Toss any current input */
381 ZM_DEBUG (zm_dump (__LINE__
));
382 CYGACC_CALL_IF_DELAY_US ((cyg_int32
) 250000);
383 return xyzModem_timeout
;
387 /* Header found, now read the data */
388 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.blk
);
389 ZM_DEBUG (zm_save (xyz
.blk
));
392 ZM_DEBUG (zm_dump (__LINE__
));
393 return xyzModem_timeout
;
395 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.cblk
);
396 ZM_DEBUG (zm_save (xyz
.cblk
));
399 ZM_DEBUG (zm_dump (__LINE__
));
400 return xyzModem_timeout
;
402 xyz
.len
= (c
== SOH
) ? 128 : 1024;
404 for (i
= 0; i
< xyz
.len
; i
++)
406 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, &c
);
407 ZM_DEBUG (zm_save (c
));
414 ZM_DEBUG (zm_dump (__LINE__
));
415 return xyzModem_timeout
;
418 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.crc1
);
419 ZM_DEBUG (zm_save (xyz
.crc1
));
422 ZM_DEBUG (zm_dump (__LINE__
));
423 return xyzModem_timeout
;
427 res
= CYGACC_COMM_IF_GETC_TIMEOUT (*xyz
.__chan
, (char *) &xyz
.crc2
);
428 ZM_DEBUG (zm_save (xyz
.crc2
));
431 ZM_DEBUG (zm_dump (__LINE__
));
432 return xyzModem_timeout
;
435 ZM_DEBUG (zm_dump (__LINE__
));
436 /* Validate the message */
437 if ((xyz
.blk
^ xyz
.cblk
) != (unsigned char) 0xFF)
440 ("Framing error - blk: %x/%x/%x\n", xyz
.blk
, xyz
.cblk
,
441 (xyz
.blk
^ xyz
.cblk
)));
442 ZM_DEBUG (zm_dump_buf (xyz
.pkt
, xyz
.len
));
444 return xyzModem_frame
;
446 /* Verify checksum/CRC */
449 cksum
= cyg_crc16 (xyz
.pkt
, xyz
.len
);
450 if (cksum
!= ((xyz
.crc1
<< 8) | xyz
.crc2
))
452 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
453 xyz
.crc1
, xyz
.crc2
, cksum
& 0xFFFF));
454 return xyzModem_cksum
;
460 for (i
= 0; i
< xyz
.len
; i
++)
464 if (xyz
.crc1
!= (cksum
& 0xFF))
467 ("Checksum error - recvd: %x, computed: %x\n", xyz
.crc1
,
469 return xyzModem_cksum
;
472 /* If we get here, the message passes [structural] muster */
477 xyzModem_stream_open (connection_info_t
* info
, int *err
)
483 int retries
= xyzModem_MAX_RETRIES
;
484 int crc_retries
= xyzModem_MAX_RETRIES_WITH_CRC
;
486 /* ZM_DEBUG(zm_out = zm_out_start); */
487 #ifdef xyzModem_zmodem
488 if (info
->mode
== xyzModem_zmodem
)
490 *err
= xyzModem_noZmodem
;
496 /* Set up the I/O channel. Note: this allows for using a different port in the future */
498 CYGACC_CALL_IF_SET_CONSOLE_COMM
499 (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT
);
502 CYGACC_CALL_IF_SET_CONSOLE_COMM (info
->chan
);
506 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan
);
508 xyz
.__chan
= CYGACC_CALL_IF_CONSOLE_PROCS ();
510 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan
);
511 CYGACC_COMM_IF_CONTROL (*xyz
.__chan
, __COMMCTL_SET_TIMEOUT
,
512 xyzModem_CHAR_TIMEOUT
);
522 xyz
.mode
= info
->mode
;
523 xyz
.total_retries
= 0;
527 #ifdef USE_YMODEM_LENGTH
532 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
534 if (xyz
.mode
== xyzModem_xmodem
)
536 /* X-modem doesn't have an information header - exit here */
541 while (retries
-- > 0)
543 stat
= xyzModem_get_hdr ();
546 /* Y-modem file information header */
549 #ifdef USE_YMODEM_LENGTH
553 parse_num ((char *) xyz
.bufp
, &xyz
.file_length
, NULL
, " ");
555 /* The rest of the file name data block quietly discarded */
562 else if (stat
== xyzModem_timeout
)
564 if (--crc_retries
<= 0)
565 xyz
.crc_mode
= false;
566 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
567 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
569 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__
));
571 if (stat
== xyzModem_cancel
)
577 ZM_DEBUG (zm_flush ());
582 xyzModem_stream_read (char *buf
, int size
, int *err
)
584 int stat
, total
, len
;
588 stat
= xyzModem_cancel
;
589 /* Try and get 'size' bytes into the buffer */
590 while (!xyz
.at_eof
&& (size
> 0))
594 retries
= xyzModem_MAX_RETRIES
;
595 while (retries
-- > 0)
597 stat
= xyzModem_get_hdr ();
600 if (xyz
.blk
== xyz
.next_blk
)
604 ("ACK block %d (%d)\n", xyz
.blk
, __LINE__
));
605 xyz
.next_blk
= (xyz
.next_blk
+ 1) & 0xFF;
607 #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
608 if (xyz
.mode
== xyzModem_xmodem
|| xyz
.file_length
== 0)
614 /* Data blocks can be padded with ^Z (EOF) characters */
615 /* This code tries to detect and remove them */
616 if ((xyz
.bufp
[xyz
.len
- 1] == EOF
) &&
617 (xyz
.bufp
[xyz
.len
- 2] == EOF
) &&
618 (xyz
.bufp
[xyz
.len
- 3] == EOF
))
621 && (xyz
.bufp
[xyz
.len
- 1] == EOF
))
628 #ifdef USE_YMODEM_LENGTH
630 * See if accumulated length exceeds that of the file.
631 * If so, reduce size (i.e., cut out pad bytes)
632 * Only do this for Y-modem (and Z-modem should it ever
633 * be supported since it can fall back to Y-modem mode).
635 if (xyz
.mode
!= xyzModem_xmodem
&& 0 != xyz
.file_length
)
637 xyz
.read_length
+= xyz
.len
;
638 if (xyz
.read_length
> xyz
.file_length
)
640 xyz
.len
-= (xyz
.read_length
- xyz
.file_length
);
646 else if (xyz
.blk
== ((xyz
.next_blk
- 1) & 0xFF))
648 /* Just re-ACK this so sender will get on with it */
649 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
650 continue; /* Need new header */
654 stat
= xyzModem_sequence
;
657 if (stat
== xyzModem_cancel
)
661 if (stat
== xyzModem_eof
)
663 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
664 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__
));
665 if (xyz
.mode
== xyzModem_ymodem
)
667 CYGACC_COMM_IF_PUTC (*xyz
.__chan
,
668 (xyz
.crc_mode
? 'C' : NAK
));
670 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
671 stat
= xyzModem_get_hdr ();
672 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, ACK
);
673 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__
));
678 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, (xyz
.crc_mode
? 'C' : NAK
));
680 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__
));
689 /* Don't "read" data from the EOF protocol package */
695 memcpy (buf
, xyz
.bufp
, len
);
707 xyzModem_stream_close (int *err
)
710 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
711 xyz
.crc_mode
? "CRC" : "Cksum", xyz
.total_SOH
, xyz
.total_STX
,
712 xyz
.total_CAN
, xyz
.total_retries
);
713 ZM_DEBUG (zm_flush ());
716 /* Need to be able to clean out the input buffer, so have to take the */
719 xyzModem_stream_terminate (bool abort
, int (*getc
) (void))
725 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
728 case xyzModem_xmodem
:
729 case xyzModem_ymodem
:
730 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
731 /* number of Backspaces is a friendly way to get the other end to abort. */
732 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
733 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
734 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
735 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, CAN
);
736 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
737 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
738 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
739 CYGACC_COMM_IF_PUTC (*xyz
.__chan
, BSP
);
740 /* Now consume the rest of what's waiting on the line. */
741 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
745 #ifdef xyzModem_zmodem
746 case xyzModem_zmodem
:
747 /* Might support it some day I suppose. */
754 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
756 * Consume any trailing crap left in the inbuffer from
757 * previous received blocks. Since very few files are an exact multiple
758 * of the transfer block size, there will almost always be some gunk here.
759 * If we don't eat it now, RedBoot will think the user typed it.
761 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
762 while ((c
= (*getc
) ()) > -1)
764 ZM_DEBUG (zm_dprintf ("\n"));
766 * Make a small delay to give terminal programs like minicom
767 * time to get control again after their file transfer program
770 CYGACC_CALL_IF_DELAY_US ((cyg_int32
) 250000);
775 xyzModem_error (int err
)
779 case xyzModem_access
:
780 return "Can't access file";
782 case xyzModem_noZmodem
:
783 return "Sorry, zModem not available yet";
785 case xyzModem_timeout
:
789 return "End of file";
791 case xyzModem_cancel
:
795 return "Invalid framing";
798 return "CRC/checksum error";
800 case xyzModem_sequence
:
801 return "Block sequence error";
804 return "Unknown error";
813 GETC_IO_FUNCS (xyzModem_io
, xyzModem_stream_open
, xyzModem_stream_close
,
814 xyzModem_stream_terminate
, xyzModem_stream_read
,
816 RedBoot_load (xmodem
, xyzModem_io
, false, false, xyzModem_xmodem
);
817 RedBoot_load (ymodem
, xyzModem_io
, false, false, xyzModem_ymodem
);