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 *==========================================================================
27 #include <u-boot/crc.h>
30 /* Assumption - run xyzModem protocol over the console port */
32 /* Values magic to the protocol */
35 #define ETX 0x03 /* ^C for interrupt */
41 #define EOF 0x1A /* ^Z for DOS officionados */
43 /* Data & state local to the protocol */
47 unsigned char pkt[1024], *bufp;
48 unsigned char blk, cblk, crc1, crc2;
49 unsigned char next_blk; /* Expected block */
50 int len, mode, total_retries;
51 int total_SOH, total_STX, total_CAN;
52 bool crc_mode, at_eof, tx_ack;
53 unsigned long file_length, read_length;
56 #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
57 #define xyzModem_MAX_RETRIES 20
58 #define xyzModem_MAX_RETRIES_WITH_CRC 10
59 #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
62 typedef int cyg_int32;
64 CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
67 ulong now = get_timer(0);
71 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
83 CYGACC_COMM_IF_PUTC (char x, char y)
88 /* Validate a hex character */
89 __inline__ static bool
92 return (((c >= '0') && (c <= '9')) ||
93 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
96 /* Convert a single hex nibble */
102 if ((c >= '0') && (c <= '9'))
106 else if ((c >= 'a') && (c <= 'f'))
108 ret = (c - 'a' + 0x0a);
110 else if ((c >= 'A') && (c <= 'F'))
112 ret = (c - 'A' + 0x0A);
117 /* Convert a character to lower case */
118 __inline__ static char
121 if ((c >= 'A') && (c <= 'Z'))
128 /* Parse (scan) a number */
130 parse_num (char *s, unsigned long *val, char **es, char *delim)
135 unsigned long result = 0;
142 if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
149 if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
152 result = (result * radix) + digit;
156 if (delim != (char *) 0)
158 /* See if this character is one of the delimiters */
160 while (*dp && (c != *dp))
163 break; /* Found a good delimiter */
165 return false; /* Malformatted number */
169 if (es != (char **) 0)
177 #if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
179 * Note: this debug setup works by storing the strings in a fixed buffer
181 static char zm_debug_buf[8192];
182 static char *zm_out = zm_debug_buf;
183 static char *zm_out_start = zm_debug_buf;
186 zm_dprintf(char *fmt, ...)
192 len = diag_vsprintf(zm_out, fmt, args);
201 zm_out = zm_out_start;
205 zm_dump_buf (void *buf, int len)
210 static unsigned char zm_buf[2048];
211 static unsigned char *zm_bp;
220 zm_save (unsigned char c)
228 zm_dprintf ("Packet at line: %d\n", line);
229 zm_dump_buf (zm_buf, zm_bp - zm_buf);
232 #define ZM_DEBUG(x) x
237 /* Wait for the line to go idle */
239 xyzModem_flush (void)
245 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
252 xyzModem_get_hdr (void)
256 bool hdr_found = false;
257 int i, can_total, hdr_chars;
258 unsigned short cksum;
260 ZM_DEBUG (zm_new ());
261 /* Find the start of a header */
267 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
272 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
273 ZM_DEBUG (zm_save (c));
289 ZM_DEBUG (zm_dump (__LINE__));
290 if (++can_total == xyzModem_CAN_COUNT)
292 return xyzModem_cancel;
296 /* Wait for multiple CAN to avoid early quits */
300 /* EOT only supported if no noise */
303 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
304 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
305 ZM_DEBUG (zm_dump (__LINE__));
309 /* Ignore, waiting for start of header */
315 /* Data stream timed out */
316 xyzModem_flush (); /* Toss any current input */
317 ZM_DEBUG (zm_dump (__LINE__));
318 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
319 return xyzModem_timeout;
323 /* Header found, now read the data */
324 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
325 ZM_DEBUG (zm_save (xyz.blk));
328 ZM_DEBUG (zm_dump (__LINE__));
329 return xyzModem_timeout;
331 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
332 ZM_DEBUG (zm_save (xyz.cblk));
335 ZM_DEBUG (zm_dump (__LINE__));
336 return xyzModem_timeout;
338 xyz.len = (c == SOH) ? 128 : 1024;
340 for (i = 0; i < xyz.len; i++)
342 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
343 ZM_DEBUG (zm_save (c));
350 ZM_DEBUG (zm_dump (__LINE__));
351 return xyzModem_timeout;
354 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
355 ZM_DEBUG (zm_save (xyz.crc1));
358 ZM_DEBUG (zm_dump (__LINE__));
359 return xyzModem_timeout;
363 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
364 ZM_DEBUG (zm_save (xyz.crc2));
367 ZM_DEBUG (zm_dump (__LINE__));
368 return xyzModem_timeout;
371 ZM_DEBUG (zm_dump (__LINE__));
372 /* Validate the message */
373 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
376 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
377 (xyz.blk ^ xyz.cblk)));
378 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
380 return xyzModem_frame;
382 /* Verify checksum/CRC */
385 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
386 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
388 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
389 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
390 return xyzModem_cksum;
396 for (i = 0; i < xyz.len; i++)
400 if (xyz.crc1 != (cksum & 0xFF))
403 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
405 return xyzModem_cksum;
408 /* If we get here, the message passes [structural] muster */
413 xyzModem_stream_open (connection_info_t * info, int *err)
416 int retries = xyzModem_MAX_RETRIES;
417 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
419 /* ZM_DEBUG(zm_out = zm_out_start); */
420 #ifdef xyzModem_zmodem
421 if (info->mode == xyzModem_zmodem)
423 *err = xyzModem_noZmodem;
435 xyz.mode = info->mode;
436 xyz.total_retries = 0;
443 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
445 if (xyz.mode == xyzModem_xmodem)
447 /* X-modem doesn't have an information header - exit here */
452 while (retries-- > 0)
454 stat = xyzModem_get_hdr ();
457 /* Y-modem file information header */
463 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
464 /* The rest of the file name data block quietly discarded */
471 else if (stat == xyzModem_timeout)
473 if (--crc_retries <= 0)
474 xyz.crc_mode = false;
475 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
476 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
478 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
480 if (stat == xyzModem_cancel)
486 ZM_DEBUG (zm_flush ());
491 xyzModem_stream_read (char *buf, int size, int *err)
493 int stat, total, len;
497 stat = xyzModem_cancel;
498 /* Try and get 'size' bytes into the buffer */
499 while (!xyz.at_eof && xyz.len >= 0 && (size > 0))
503 retries = xyzModem_MAX_RETRIES;
504 while (retries-- > 0)
506 stat = xyzModem_get_hdr ();
509 if (xyz.blk == xyz.next_blk)
513 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
514 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
516 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
518 /* Data blocks can be padded with ^Z (EOF) characters */
519 /* This code tries to detect and remove them */
520 if ((xyz.bufp[xyz.len - 1] == EOF) &&
521 (xyz.bufp[xyz.len - 2] == EOF) &&
522 (xyz.bufp[xyz.len - 3] == EOF))
525 && (xyz.bufp[xyz.len - 1] == EOF))
533 * See if accumulated length exceeds that of the file.
534 * If so, reduce size (i.e., cut out pad bytes)
535 * Only do this for Y-modem (and Z-modem should it ever
536 * be supported since it can fall back to Y-modem mode).
538 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
540 xyz.read_length += xyz.len;
541 if (xyz.read_length > xyz.file_length)
543 xyz.len -= (xyz.read_length - xyz.file_length);
548 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
550 /* Just re-ACK this so sender will get on with it */
551 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
552 continue; /* Need new header */
556 stat = xyzModem_sequence;
559 if (stat == xyzModem_cancel)
563 if (stat == xyzModem_eof)
565 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
566 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
567 if (xyz.mode == xyzModem_ymodem)
569 CYGACC_COMM_IF_PUTC (*xyz.__chan,
570 (xyz.crc_mode ? 'C' : NAK));
572 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
573 stat = xyzModem_get_hdr ();
574 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
575 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 */
594 if (!xyz.at_eof && xyz.len > 0)
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";