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
179 static char zm_debug_buf[8192];
180 static char *zm_out = zm_debug_buf;
181 static char *zm_out_start = zm_debug_buf;
184 zm_dprintf (char *fmt, ...)
189 va_start (args, fmt);
190 len = diag_vsprintf (zm_out, fmt, args);
198 zm_out = zm_out_start;
202 zm_dump_buf (void *buf, int len)
207 static unsigned char zm_buf[2048];
208 static unsigned char *zm_bp;
217 zm_save (unsigned char c)
225 zm_dprintf ("Packet at line: %d\n", line);
226 zm_dump_buf (zm_buf, zm_bp - zm_buf);
229 #define ZM_DEBUG(x) x
234 /* Wait for the line to go idle */
236 xyzModem_flush (void)
242 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
249 xyzModem_get_hdr (void)
253 bool hdr_found = false;
254 int i, can_total, hdr_chars;
255 unsigned short cksum;
257 ZM_DEBUG (zm_new ());
258 /* Find the start of a header */
264 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
269 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
270 ZM_DEBUG (zm_save (c));
285 ZM_DEBUG (zm_dump (__LINE__));
286 if (++can_total == xyzModem_CAN_COUNT)
288 return xyzModem_cancel;
292 /* Wait for multiple CAN to avoid early quits */
296 /* EOT only supported if no noise */
299 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
300 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
301 ZM_DEBUG (zm_dump (__LINE__));
305 /* Ignore, waiting for start of header */
311 /* Data stream timed out */
312 xyzModem_flush (); /* Toss any current input */
313 ZM_DEBUG (zm_dump (__LINE__));
314 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
315 return xyzModem_timeout;
319 /* Header found, now read the data */
320 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
321 ZM_DEBUG (zm_save (xyz.blk));
324 ZM_DEBUG (zm_dump (__LINE__));
325 return xyzModem_timeout;
327 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
328 ZM_DEBUG (zm_save (xyz.cblk));
331 ZM_DEBUG (zm_dump (__LINE__));
332 return xyzModem_timeout;
334 xyz.len = (c == SOH) ? 128 : 1024;
336 for (i = 0; i < xyz.len; i++)
338 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
339 ZM_DEBUG (zm_save (c));
346 ZM_DEBUG (zm_dump (__LINE__));
347 return xyzModem_timeout;
350 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
351 ZM_DEBUG (zm_save (xyz.crc1));
354 ZM_DEBUG (zm_dump (__LINE__));
355 return xyzModem_timeout;
359 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
360 ZM_DEBUG (zm_save (xyz.crc2));
363 ZM_DEBUG (zm_dump (__LINE__));
364 return xyzModem_timeout;
367 ZM_DEBUG (zm_dump (__LINE__));
368 /* Validate the message */
369 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
372 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
373 (xyz.blk ^ xyz.cblk)));
374 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
376 return xyzModem_frame;
378 /* Verify checksum/CRC */
381 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
382 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
384 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
385 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
386 return xyzModem_cksum;
392 for (i = 0; i < xyz.len; i++)
396 if (xyz.crc1 != (cksum & 0xFF))
399 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
401 return xyzModem_cksum;
404 /* If we get here, the message passes [structural] muster */
409 xyzModem_stream_open (connection_info_t * info, int *err)
412 int retries = xyzModem_MAX_RETRIES;
413 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
415 /* ZM_DEBUG(zm_out = zm_out_start); */
416 #ifdef xyzModem_zmodem
417 if (info->mode == xyzModem_zmodem)
419 *err = xyzModem_noZmodem;
431 xyz.mode = info->mode;
432 xyz.total_retries = 0;
439 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
441 if (xyz.mode == xyzModem_xmodem)
443 /* X-modem doesn't have an information header - exit here */
448 while (retries-- > 0)
450 stat = xyzModem_get_hdr ();
453 /* Y-modem file information header */
459 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
460 /* The rest of the file name data block quietly discarded */
467 else if (stat == xyzModem_timeout)
469 if (--crc_retries <= 0)
470 xyz.crc_mode = false;
471 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
472 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
474 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
476 if (stat == xyzModem_cancel)
482 ZM_DEBUG (zm_flush ());
487 xyzModem_stream_read (char *buf, int size, int *err)
489 int stat, total, len;
493 stat = xyzModem_cancel;
494 /* Try and get 'size' bytes into the buffer */
495 while (!xyz.at_eof && (size > 0))
499 retries = xyzModem_MAX_RETRIES;
500 while (retries-- > 0)
502 stat = xyzModem_get_hdr ();
505 if (xyz.blk == xyz.next_blk)
509 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
510 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
512 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
514 /* Data blocks can be padded with ^Z (EOF) characters */
515 /* This code tries to detect and remove them */
516 if ((xyz.bufp[xyz.len - 1] == EOF) &&
517 (xyz.bufp[xyz.len - 2] == EOF) &&
518 (xyz.bufp[xyz.len - 3] == EOF))
521 && (xyz.bufp[xyz.len - 1] == EOF))
529 * See if accumulated length exceeds that of the file.
530 * If so, reduce size (i.e., cut out pad bytes)
531 * Only do this for Y-modem (and Z-modem should it ever
532 * be supported since it can fall back to Y-modem mode).
534 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
536 xyz.read_length += xyz.len;
537 if (xyz.read_length > xyz.file_length)
539 xyz.len -= (xyz.read_length - xyz.file_length);
544 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
546 /* Just re-ACK this so sender will get on with it */
547 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
548 continue; /* Need new header */
552 stat = xyzModem_sequence;
555 if (stat == xyzModem_cancel)
559 if (stat == xyzModem_eof)
561 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
562 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
563 if (xyz.mode == xyzModem_ymodem)
565 CYGACC_COMM_IF_PUTC (*xyz.__chan,
566 (xyz.crc_mode ? 'C' : NAK));
568 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
569 stat = xyzModem_get_hdr ();
570 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
571 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
576 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
578 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
587 /* Don't "read" data from the EOF protocol package */
593 memcpy (buf, xyz.bufp, len);
605 xyzModem_stream_close (int *err)
608 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
609 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
610 xyz.total_CAN, xyz.total_retries);
611 ZM_DEBUG (zm_flush ());
614 /* Need to be able to clean out the input buffer, so have to take the */
617 xyzModem_stream_terminate (bool abort, int (*getc) (void))
623 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
626 case xyzModem_xmodem:
627 case xyzModem_ymodem:
628 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
629 /* number of Backspaces is a friendly way to get the other end to abort. */
630 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
631 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
632 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
633 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
634 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
635 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
636 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
637 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
638 /* Now consume the rest of what's waiting on the line. */
639 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
643 #ifdef xyzModem_zmodem
644 case xyzModem_zmodem:
645 /* Might support it some day I suppose. */
652 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
654 * Consume any trailing crap left in the inbuffer from
655 * previous received blocks. Since very few files are an exact multiple
656 * of the transfer block size, there will almost always be some gunk here.
657 * If we don't eat it now, RedBoot will think the user typed it.
659 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
660 while ((c = (*getc) ()) > -1)
662 ZM_DEBUG (zm_dprintf ("\n"));
664 * Make a small delay to give terminal programs like minicom
665 * time to get control again after their file transfer program
668 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
673 xyzModem_error (int err)
677 case xyzModem_access:
678 return "Can't access file";
680 case xyzModem_noZmodem:
681 return "Sorry, zModem not available yet";
683 case xyzModem_timeout:
687 return "End of file";
689 case xyzModem_cancel:
693 return "Invalid framing";
696 return "CRC/checksum error";
698 case xyzModem_sequence:
699 return "Block sequence error";
702 return "Unknown error";