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 aficionados */
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, ...)
206 int cur_console __attribute__((unused));
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);
230 * Note: this debug setup works by storing the strings in a fixed buffer
234 static char *zm_out = (char *) 0x00380000;
235 static char *zm_out_start = (char *) 0x00380000;
237 static char zm_buf[8192];
238 static char *zm_out = zm_buf;
239 static char *zm_out_start = zm_buf;
243 zm_dprintf (char *fmt, ...)
248 va_start (args, fmt);
249 len = diag_vsprintf (zm_out, fmt, args);
258 char *p = zm_out_start;
260 mon_write_char (*p++);
262 zm_out = zm_out_start;
267 zm_dump_buf (void *buf, int len)
270 diag_vdump_buf_with_offset (zm_dprintf, buf, len, 0);
276 static unsigned char zm_buf[2048];
277 static unsigned char *zm_bp;
286 zm_save (unsigned char c)
294 zm_dprintf ("Packet at line: %d\n", line);
295 zm_dump_buf (zm_buf, zm_bp - zm_buf);
298 #define ZM_DEBUG(x) x
303 /* Wait for the line to go idle */
305 xyzModem_flush (void)
311 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
318 xyzModem_get_hdr (void)
322 bool hdr_found = false;
323 int i, can_total, hdr_chars;
324 unsigned short cksum;
326 ZM_DEBUG (zm_new ());
327 /* Find the start of a header */
333 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
338 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
339 ZM_DEBUG (zm_save (c));
354 ZM_DEBUG (zm_dump (__LINE__));
355 if (++can_total == xyzModem_CAN_COUNT)
357 return xyzModem_cancel;
361 /* Wait for multiple CAN to avoid early quits */
365 /* EOT only supported if no noise */
368 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
369 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
370 ZM_DEBUG (zm_dump (__LINE__));
374 /* Ignore, waiting for start of header */
380 /* Data stream timed out */
381 xyzModem_flush (); /* Toss any current input */
382 ZM_DEBUG (zm_dump (__LINE__));
383 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
384 return xyzModem_timeout;
388 /* Header found, now read the data */
389 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
390 ZM_DEBUG (zm_save (xyz.blk));
393 ZM_DEBUG (zm_dump (__LINE__));
394 return xyzModem_timeout;
396 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
397 ZM_DEBUG (zm_save (xyz.cblk));
400 ZM_DEBUG (zm_dump (__LINE__));
401 return xyzModem_timeout;
403 xyz.len = (c == SOH) ? 128 : 1024;
405 for (i = 0; i < xyz.len; i++)
407 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
408 ZM_DEBUG (zm_save (c));
415 ZM_DEBUG (zm_dump (__LINE__));
416 return xyzModem_timeout;
419 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
420 ZM_DEBUG (zm_save (xyz.crc1));
423 ZM_DEBUG (zm_dump (__LINE__));
424 return xyzModem_timeout;
428 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
429 ZM_DEBUG (zm_save (xyz.crc2));
432 ZM_DEBUG (zm_dump (__LINE__));
433 return xyzModem_timeout;
436 ZM_DEBUG (zm_dump (__LINE__));
437 /* Validate the message */
438 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
441 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
442 (xyz.blk ^ xyz.cblk)));
443 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
445 return xyzModem_frame;
447 /* Verify checksum/CRC */
450 cksum = cyg_crc16 (xyz.pkt, xyz.len);
451 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
453 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
454 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
455 return xyzModem_cksum;
461 for (i = 0; i < xyz.len; i++)
465 if (xyz.crc1 != (cksum & 0xFF))
468 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
470 return xyzModem_cksum;
473 /* If we get here, the message passes [structural] muster */
478 xyzModem_stream_open (connection_info_t * info, int *err)
484 int retries = xyzModem_MAX_RETRIES;
485 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
487 /* ZM_DEBUG(zm_out = zm_out_start); */
488 #ifdef xyzModem_zmodem
489 if (info->mode == xyzModem_zmodem)
491 *err = xyzModem_noZmodem;
497 /* Set up the I/O channel. Note: this allows for using a different port in the future */
499 CYGACC_CALL_IF_SET_CONSOLE_COMM
500 (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
503 CYGACC_CALL_IF_SET_CONSOLE_COMM (info->chan);
507 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan);
509 xyz.__chan = CYGACC_CALL_IF_CONSOLE_PROCS ();
511 CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan);
512 CYGACC_COMM_IF_CONTROL (*xyz.__chan, __COMMCTL_SET_TIMEOUT,
513 xyzModem_CHAR_TIMEOUT);
523 xyz.mode = info->mode;
524 xyz.total_retries = 0;
528 #ifdef USE_YMODEM_LENGTH
533 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
535 if (xyz.mode == xyzModem_xmodem)
537 /* X-modem doesn't have an information header - exit here */
542 while (retries-- > 0)
544 stat = xyzModem_get_hdr ();
547 /* Y-modem file information header */
550 #ifdef USE_YMODEM_LENGTH
554 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
556 /* The rest of the file name data block quietly discarded */
563 else if (stat == xyzModem_timeout)
565 if (--crc_retries <= 0)
566 xyz.crc_mode = false;
567 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
568 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
570 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
572 if (stat == xyzModem_cancel)
578 ZM_DEBUG (zm_flush ());
583 xyzModem_stream_read (char *buf, int size, int *err)
585 int stat, total, len;
589 stat = xyzModem_cancel;
590 /* Try and get 'size' bytes into the buffer */
591 while (!xyz.at_eof && (size > 0))
595 retries = xyzModem_MAX_RETRIES;
596 while (retries-- > 0)
598 stat = xyzModem_get_hdr ();
601 if (xyz.blk == xyz.next_blk)
605 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
606 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
608 #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
609 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
615 /* Data blocks can be padded with ^Z (EOF) characters */
616 /* This code tries to detect and remove them */
617 if ((xyz.bufp[xyz.len - 1] == EOF) &&
618 (xyz.bufp[xyz.len - 2] == EOF) &&
619 (xyz.bufp[xyz.len - 3] == EOF))
622 && (xyz.bufp[xyz.len - 1] == EOF))
629 #ifdef USE_YMODEM_LENGTH
631 * See if accumulated length exceeds that of the file.
632 * If so, reduce size (i.e., cut out pad bytes)
633 * Only do this for Y-modem (and Z-modem should it ever
634 * be supported since it can fall back to Y-modem mode).
636 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
638 xyz.read_length += xyz.len;
639 if (xyz.read_length > xyz.file_length)
641 xyz.len -= (xyz.read_length - xyz.file_length);
647 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
649 /* Just re-ACK this so sender will get on with it */
650 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
651 continue; /* Need new header */
655 stat = xyzModem_sequence;
658 if (stat == xyzModem_cancel)
662 if (stat == xyzModem_eof)
664 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
665 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
666 if (xyz.mode == xyzModem_ymodem)
668 CYGACC_COMM_IF_PUTC (*xyz.__chan,
669 (xyz.crc_mode ? 'C' : NAK));
671 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
672 stat = xyzModem_get_hdr ();
673 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
674 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
679 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
681 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
690 /* Don't "read" data from the EOF protocol package */
696 memcpy (buf, xyz.bufp, len);
708 xyzModem_stream_close (int *err)
711 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
712 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
713 xyz.total_CAN, xyz.total_retries);
714 ZM_DEBUG (zm_flush ());
717 /* Need to be able to clean out the input buffer, so have to take the */
720 xyzModem_stream_terminate (bool abort, int (*getc) (void))
726 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
729 case xyzModem_xmodem:
730 case xyzModem_ymodem:
731 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
732 /* number of Backspaces is a friendly way to get the other end to abort. */
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, CAN);
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 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
741 /* Now consume the rest of what's waiting on the line. */
742 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
746 #ifdef xyzModem_zmodem
747 case xyzModem_zmodem:
748 /* Might support it some day I suppose. */
755 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
757 * Consume any trailing crap left in the inbuffer from
758 * previous received blocks. Since very few files are an exact multiple
759 * of the transfer block size, there will almost always be some gunk here.
760 * If we don't eat it now, RedBoot will think the user typed it.
762 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
763 while ((c = (*getc) ()) > -1)
765 ZM_DEBUG (zm_dprintf ("\n"));
767 * Make a small delay to give terminal programs like minicom
768 * time to get control again after their file transfer program
771 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
776 xyzModem_error (int err)
780 case xyzModem_access:
781 return "Can't access file";
783 case xyzModem_noZmodem:
784 return "Sorry, zModem not available yet";
786 case xyzModem_timeout:
790 return "End of file";
792 case xyzModem_cancel:
796 return "Invalid framing";
799 return "CRC/checksum error";
801 case xyzModem_sequence:
802 return "Block sequence error";
805 return "Unknown error";
814 GETC_IO_FUNCS (xyzModem_io, xyzModem_stream_open, xyzModem_stream_close,
815 xyzModem_stream_terminate, xyzModem_stream_read,
817 RedBoot_load (xmodem, xyzModem_io, false, false, xyzModem_xmodem);
818 RedBoot_load (ymodem, xyzModem_io, false, false, xyzModem_ymodem);