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 /* Data & state local to the protocol */
46 unsigned char pkt[1024], *bufp;
47 unsigned char blk, cblk, crc1, crc2;
48 unsigned char next_blk; /* Expected block */
49 int len, mode, total_retries;
50 int total_SOH, total_STX, total_CAN;
51 bool crc_mode, at_eof, tx_ack;
52 unsigned long file_length, read_length;
55 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
56 #define xyzModem_MAX_RETRIES 20
57 #define xyzModem_MAX_RETRIES_WITH_CRC 10
58 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
61 typedef int cyg_int32;
63 CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
66 ulong now = get_timer(0);
69 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
81 CYGACC_COMM_IF_PUTC (char x, char y)
86 /* Validate a hex character */
87 __inline__ static bool
90 return (((c >= '0') && (c <= '9')) ||
91 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
94 /* Convert a single hex nibble */
100 if ((c >= '0') && (c <= '9'))
104 else if ((c >= 'a') && (c <= 'f'))
106 ret = (c - 'a' + 0x0a);
108 else if ((c >= 'A') && (c <= 'F'))
110 ret = (c - 'A' + 0x0A);
115 /* Convert a character to lower case */
116 __inline__ static char
119 if ((c >= 'A') && (c <= 'Z'))
126 /* Parse (scan) a number */
128 parse_num (char *s, unsigned long *val, char **es, char *delim)
133 unsigned long result = 0;
140 if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
147 if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
150 result = (result * radix) + digit;
154 if (delim != (char *) 0)
156 /* See if this character is one of the delimiters */
158 while (*dp && (c != *dp))
161 break; /* Found a good delimiter */
163 return false; /* Malformatted number */
167 if (es != (char **) 0)
177 * Note: this debug setup works by storing the strings in a fixed buffer
181 static char *zm_out = (char *) 0x00380000;
182 static char *zm_out_start = (char *) 0x00380000;
184 static char zm_buf[8192];
185 static char *zm_out = zm_buf;
186 static char *zm_out_start = zm_buf;
190 zm_dprintf (char *fmt, ...)
195 va_start (args, fmt);
196 len = diag_vsprintf (zm_out, fmt, args);
204 zm_out = zm_out_start;
208 zm_dump_buf (void *buf, int len)
213 static unsigned char zm_buf[2048];
214 static unsigned char *zm_bp;
223 zm_save (unsigned char c)
231 zm_dprintf ("Packet at line: %d\n", line);
232 zm_dump_buf (zm_buf, zm_bp - zm_buf);
235 #define ZM_DEBUG(x) x
240 /* Wait for the line to go idle */
242 xyzModem_flush (void)
248 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
255 xyzModem_get_hdr (void)
259 bool hdr_found = false;
260 int i, can_total, hdr_chars;
261 unsigned short cksum;
263 ZM_DEBUG (zm_new ());
264 /* Find the start of a header */
270 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
275 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
276 ZM_DEBUG (zm_save (c));
291 ZM_DEBUG (zm_dump (__LINE__));
292 if (++can_total == xyzModem_CAN_COUNT)
294 return xyzModem_cancel;
298 /* Wait for multiple CAN to avoid early quits */
302 /* EOT only supported if no noise */
305 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
306 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
307 ZM_DEBUG (zm_dump (__LINE__));
311 /* Ignore, waiting for start of header */
317 /* Data stream timed out */
318 xyzModem_flush (); /* Toss any current input */
319 ZM_DEBUG (zm_dump (__LINE__));
320 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
321 return xyzModem_timeout;
325 /* Header found, now read the data */
326 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
327 ZM_DEBUG (zm_save (xyz.blk));
330 ZM_DEBUG (zm_dump (__LINE__));
331 return xyzModem_timeout;
333 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
334 ZM_DEBUG (zm_save (xyz.cblk));
337 ZM_DEBUG (zm_dump (__LINE__));
338 return xyzModem_timeout;
340 xyz.len = (c == SOH) ? 128 : 1024;
342 for (i = 0; i < xyz.len; i++)
344 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
345 ZM_DEBUG (zm_save (c));
352 ZM_DEBUG (zm_dump (__LINE__));
353 return xyzModem_timeout;
356 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
357 ZM_DEBUG (zm_save (xyz.crc1));
360 ZM_DEBUG (zm_dump (__LINE__));
361 return xyzModem_timeout;
365 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
366 ZM_DEBUG (zm_save (xyz.crc2));
369 ZM_DEBUG (zm_dump (__LINE__));
370 return xyzModem_timeout;
373 ZM_DEBUG (zm_dump (__LINE__));
374 /* Validate the message */
375 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
378 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
379 (xyz.blk ^ xyz.cblk)));
380 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
382 return xyzModem_frame;
384 /* Verify checksum/CRC */
387 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
388 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
390 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
391 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
392 return xyzModem_cksum;
398 for (i = 0; i < xyz.len; i++)
402 if (xyz.crc1 != (cksum & 0xFF))
405 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
407 return xyzModem_cksum;
410 /* If we get here, the message passes [structural] muster */
415 xyzModem_stream_open (connection_info_t * info, int *err)
418 int retries = xyzModem_MAX_RETRIES;
419 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
421 /* ZM_DEBUG(zm_out = zm_out_start); */
422 #ifdef xyzModem_zmodem
423 if (info->mode == xyzModem_zmodem)
425 *err = xyzModem_noZmodem;
437 xyz.mode = info->mode;
438 xyz.total_retries = 0;
445 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
447 if (xyz.mode == xyzModem_xmodem)
449 /* X-modem doesn't have an information header - exit here */
454 while (retries-- > 0)
456 stat = xyzModem_get_hdr ();
459 /* Y-modem file information header */
465 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
466 /* The rest of the file name data block quietly discarded */
473 else if (stat == xyzModem_timeout)
475 if (--crc_retries <= 0)
476 xyz.crc_mode = false;
477 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
478 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
480 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
482 if (stat == xyzModem_cancel)
488 ZM_DEBUG (zm_flush ());
493 xyzModem_stream_read (char *buf, int size, int *err)
495 int stat, total, len;
499 stat = xyzModem_cancel;
500 /* Try and get 'size' bytes into the buffer */
501 while (!xyz.at_eof && (size > 0))
505 retries = xyzModem_MAX_RETRIES;
506 while (retries-- > 0)
508 stat = xyzModem_get_hdr ();
511 if (xyz.blk == xyz.next_blk)
515 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
516 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
518 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
520 /* Data blocks can be padded with ^Z (EOF) characters */
521 /* This code tries to detect and remove them */
522 if ((xyz.bufp[xyz.len - 1] == EOF) &&
523 (xyz.bufp[xyz.len - 2] == EOF) &&
524 (xyz.bufp[xyz.len - 3] == EOF))
527 && (xyz.bufp[xyz.len - 1] == EOF))
535 * See if accumulated length exceeds that of the file.
536 * If so, reduce size (i.e., cut out pad bytes)
537 * Only do this for Y-modem (and Z-modem should it ever
538 * be supported since it can fall back to Y-modem mode).
540 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
542 xyz.read_length += xyz.len;
543 if (xyz.read_length > xyz.file_length)
545 xyz.len -= (xyz.read_length - xyz.file_length);
550 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
552 /* Just re-ACK this so sender will get on with it */
553 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
554 continue; /* Need new header */
558 stat = xyzModem_sequence;
561 if (stat == xyzModem_cancel)
565 if (stat == xyzModem_eof)
567 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
568 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
569 if (xyz.mode == xyzModem_ymodem)
571 CYGACC_COMM_IF_PUTC (*xyz.__chan,
572 (xyz.crc_mode ? 'C' : NAK));
574 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
575 stat = xyzModem_get_hdr ();
576 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
577 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
582 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
584 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
593 /* Don't "read" data from the EOF protocol package */
599 memcpy (buf, xyz.bufp, len);
611 xyzModem_stream_close (int *err)
614 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
615 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
616 xyz.total_CAN, xyz.total_retries);
617 ZM_DEBUG (zm_flush ());
620 /* Need to be able to clean out the input buffer, so have to take the */
623 xyzModem_stream_terminate (bool abort, int (*getc) (void))
629 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
632 case xyzModem_xmodem:
633 case xyzModem_ymodem:
634 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
635 /* number of Backspaces is a friendly way to get the other end to abort. */
636 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
637 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
638 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
639 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
640 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
641 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
642 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
643 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
644 /* Now consume the rest of what's waiting on the line. */
645 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
649 #ifdef xyzModem_zmodem
650 case xyzModem_zmodem:
651 /* Might support it some day I suppose. */
658 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
660 * Consume any trailing crap left in the inbuffer from
661 * previous received blocks. Since very few files are an exact multiple
662 * of the transfer block size, there will almost always be some gunk here.
663 * If we don't eat it now, RedBoot will think the user typed it.
665 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
666 while ((c = (*getc) ()) > -1)
668 ZM_DEBUG (zm_dprintf ("\n"));
670 * Make a small delay to give terminal programs like minicom
671 * time to get control again after their file transfer program
674 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
679 xyzModem_error (int err)
683 case xyzModem_access:
684 return "Can't access file";
686 case xyzModem_noZmodem:
687 return "Sorry, zModem not available yet";
689 case xyzModem_timeout:
693 return "End of file";
695 case xyzModem_cancel:
699 return "Invalid framing";
702 return "CRC/checksum error";
704 case xyzModem_sequence:
705 return "Block sequence error";
708 return "Unknown error";