1 // SPDX-License-Identifier: eCos-2.0
3 *==========================================================================
7 * RedBoot stream handler for xyzModem protocol
9 *==========================================================================
10 *#####DESCRIPTIONBEGIN####
13 * Contributors: gthomas, tsmith, Yoshinori Sato
18 * This code is part of RedBoot (tm).
20 *####DESCRIPTIONEND####
22 *==========================================================================
29 /* Assumption - run xyzModem protocol over the console port */
31 /* Values magic to the protocol */
39 #define EOF 0x1A /* ^Z for DOS officionados */
41 /* Data & state local to the protocol */
45 unsigned char pkt[1024], *bufp;
46 unsigned char blk, cblk, crc1, crc2;
47 unsigned char next_blk; /* Expected block */
48 int len, mode, total_retries;
49 int total_SOH, total_STX, total_CAN;
50 bool crc_mode, at_eof, tx_ack;
51 unsigned long file_length, read_length;
54 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
55 #define xyzModem_MAX_RETRIES 20
56 #define xyzModem_MAX_RETRIES_WITH_CRC 10
57 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
60 typedef int cyg_int32;
62 CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
65 ulong now = get_timer(0);
68 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
80 CYGACC_COMM_IF_PUTC (char x, char y)
85 /* Validate a hex character */
86 __inline__ static bool
89 return (((c >= '0') && (c <= '9')) ||
90 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
93 /* Convert a single hex nibble */
99 if ((c >= '0') && (c <= '9'))
103 else if ((c >= 'a') && (c <= 'f'))
105 ret = (c - 'a' + 0x0a);
107 else if ((c >= 'A') && (c <= 'F'))
109 ret = (c - 'A' + 0x0A);
114 /* Convert a character to lower case */
115 __inline__ static char
118 if ((c >= 'A') && (c <= 'Z'))
125 /* Parse (scan) a number */
127 parse_num (char *s, unsigned long *val, char **es, char *delim)
132 unsigned long result = 0;
139 if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
146 if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
149 result = (result * radix) + digit;
153 if (delim != (char *) 0)
155 /* See if this character is one of the delimiters */
157 while (*dp && (c != *dp))
160 break; /* Found a good delimiter */
162 return false; /* Malformatted number */
166 if (es != (char **) 0)
176 * Note: this debug setup works by storing the strings in a fixed buffer
178 static char zm_debug_buf[8192];
179 static char *zm_out = zm_debug_buf;
180 static char *zm_out_start = zm_debug_buf;
183 zm_dprintf (char *fmt, ...)
188 va_start (args, fmt);
189 len = diag_vsprintf (zm_out, fmt, args);
197 zm_out = zm_out_start;
201 zm_dump_buf (void *buf, int len)
206 static unsigned char zm_buf[2048];
207 static unsigned char *zm_bp;
216 zm_save (unsigned char c)
224 zm_dprintf ("Packet at line: %d\n", line);
225 zm_dump_buf (zm_buf, zm_bp - zm_buf);
228 #define ZM_DEBUG(x) x
233 /* Wait for the line to go idle */
235 xyzModem_flush (void)
241 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
248 xyzModem_get_hdr (void)
252 bool hdr_found = false;
253 int i, can_total, hdr_chars;
254 unsigned short cksum;
256 ZM_DEBUG (zm_new ());
257 /* Find the start of a header */
263 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
268 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
269 ZM_DEBUG (zm_save (c));
284 ZM_DEBUG (zm_dump (__LINE__));
285 if (++can_total == xyzModem_CAN_COUNT)
287 return xyzModem_cancel;
291 /* Wait for multiple CAN to avoid early quits */
295 /* EOT only supported if no noise */
298 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
299 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
300 ZM_DEBUG (zm_dump (__LINE__));
304 /* Ignore, waiting for start of header */
310 /* Data stream timed out */
311 xyzModem_flush (); /* Toss any current input */
312 ZM_DEBUG (zm_dump (__LINE__));
313 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
314 return xyzModem_timeout;
318 /* Header found, now read the data */
319 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
320 ZM_DEBUG (zm_save (xyz.blk));
323 ZM_DEBUG (zm_dump (__LINE__));
324 return xyzModem_timeout;
326 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
327 ZM_DEBUG (zm_save (xyz.cblk));
330 ZM_DEBUG (zm_dump (__LINE__));
331 return xyzModem_timeout;
333 xyz.len = (c == SOH) ? 128 : 1024;
335 for (i = 0; i < xyz.len; i++)
337 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
338 ZM_DEBUG (zm_save (c));
345 ZM_DEBUG (zm_dump (__LINE__));
346 return xyzModem_timeout;
349 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
350 ZM_DEBUG (zm_save (xyz.crc1));
353 ZM_DEBUG (zm_dump (__LINE__));
354 return xyzModem_timeout;
358 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
359 ZM_DEBUG (zm_save (xyz.crc2));
362 ZM_DEBUG (zm_dump (__LINE__));
363 return xyzModem_timeout;
366 ZM_DEBUG (zm_dump (__LINE__));
367 /* Validate the message */
368 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
371 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
372 (xyz.blk ^ xyz.cblk)));
373 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
375 return xyzModem_frame;
377 /* Verify checksum/CRC */
380 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
381 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
383 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
384 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
385 return xyzModem_cksum;
391 for (i = 0; i < xyz.len; i++)
395 if (xyz.crc1 != (cksum & 0xFF))
398 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
400 return xyzModem_cksum;
403 /* If we get here, the message passes [structural] muster */
408 xyzModem_stream_open (connection_info_t * info, int *err)
411 int retries = xyzModem_MAX_RETRIES;
412 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
414 /* ZM_DEBUG(zm_out = zm_out_start); */
415 #ifdef xyzModem_zmodem
416 if (info->mode == xyzModem_zmodem)
418 *err = xyzModem_noZmodem;
430 xyz.mode = info->mode;
431 xyz.total_retries = 0;
438 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
440 if (xyz.mode == xyzModem_xmodem)
442 /* X-modem doesn't have an information header - exit here */
447 while (retries-- > 0)
449 stat = xyzModem_get_hdr ();
452 /* Y-modem file information header */
458 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
459 /* The rest of the file name data block quietly discarded */
466 else if (stat == xyzModem_timeout)
468 if (--crc_retries <= 0)
469 xyz.crc_mode = false;
470 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
471 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
473 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
475 if (stat == xyzModem_cancel)
481 ZM_DEBUG (zm_flush ());
486 xyzModem_stream_read (char *buf, int size, int *err)
488 int stat, total, len;
492 stat = xyzModem_cancel;
493 /* Try and get 'size' bytes into the buffer */
494 while (!xyz.at_eof && (size > 0))
498 retries = xyzModem_MAX_RETRIES;
499 while (retries-- > 0)
501 stat = xyzModem_get_hdr ();
504 if (xyz.blk == xyz.next_blk)
508 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
509 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
511 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
513 /* Data blocks can be padded with ^Z (EOF) characters */
514 /* This code tries to detect and remove them */
515 if ((xyz.bufp[xyz.len - 1] == EOF) &&
516 (xyz.bufp[xyz.len - 2] == EOF) &&
517 (xyz.bufp[xyz.len - 3] == EOF))
520 && (xyz.bufp[xyz.len - 1] == EOF))
528 * See if accumulated length exceeds that of the file.
529 * If so, reduce size (i.e., cut out pad bytes)
530 * Only do this for Y-modem (and Z-modem should it ever
531 * be supported since it can fall back to Y-modem mode).
533 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
535 xyz.read_length += xyz.len;
536 if (xyz.read_length > xyz.file_length)
538 xyz.len -= (xyz.read_length - xyz.file_length);
543 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
545 /* Just re-ACK this so sender will get on with it */
546 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
547 continue; /* Need new header */
551 stat = xyzModem_sequence;
554 if (stat == xyzModem_cancel)
558 if (stat == xyzModem_eof)
560 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
561 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
562 if (xyz.mode == xyzModem_ymodem)
564 CYGACC_COMM_IF_PUTC (*xyz.__chan,
565 (xyz.crc_mode ? 'C' : NAK));
567 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
568 stat = xyzModem_get_hdr ();
569 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
570 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
575 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
577 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
586 /* Don't "read" data from the EOF protocol package */
592 memcpy (buf, xyz.bufp, len);
604 xyzModem_stream_close (int *err)
607 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
608 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
609 xyz.total_CAN, xyz.total_retries);
610 ZM_DEBUG (zm_flush ());
613 /* Need to be able to clean out the input buffer, so have to take the */
616 xyzModem_stream_terminate (bool abort, int (*getc) (void))
622 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
625 case xyzModem_xmodem:
626 case xyzModem_ymodem:
627 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
628 /* number of Backspaces is a friendly way to get the other end to abort. */
629 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
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, BSP);
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 /* Now consume the rest of what's waiting on the line. */
638 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
642 #ifdef xyzModem_zmodem
643 case xyzModem_zmodem:
644 /* Might support it some day I suppose. */
651 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
653 * Consume any trailing crap left in the inbuffer from
654 * previous received blocks. Since very few files are an exact multiple
655 * of the transfer block size, there will almost always be some gunk here.
656 * If we don't eat it now, RedBoot will think the user typed it.
658 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
659 while ((c = (*getc) ()) > -1)
661 ZM_DEBUG (zm_dprintf ("\n"));
663 * Make a small delay to give terminal programs like minicom
664 * time to get control again after their file transfer program
667 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
672 xyzModem_error (int err)
676 case xyzModem_access:
677 return "Can't access file";
679 case xyzModem_noZmodem:
680 return "Sorry, zModem not available yet";
682 case xyzModem_timeout:
686 return "End of file";
688 case xyzModem_cancel:
692 return "Invalid framing";
695 return "CRC/checksum error";
697 case xyzModem_sequence:
698 return "Block sequence error";
701 return "Unknown error";