Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: eCos-2.0 |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 2 | /* |
| 3 | *========================================================================== |
| 4 | * |
| 5 | * xyzModem.c |
| 6 | * |
| 7 | * RedBoot stream handler for xyzModem protocol |
| 8 | * |
| 9 | *========================================================================== |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 10 | *#####DESCRIPTIONBEGIN#### |
| 11 | * |
| 12 | * Author(s): gthomas |
| 13 | * Contributors: gthomas, tsmith, Yoshinori Sato |
| 14 | * Date: 2000-07-14 |
| 15 | * Purpose: |
| 16 | * Description: |
| 17 | * |
| 18 | * This code is part of RedBoot (tm). |
| 19 | * |
| 20 | *####DESCRIPTIONEND#### |
| 21 | * |
| 22 | *========================================================================== |
| 23 | */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 24 | #include <common.h> |
| 25 | #include <xyzModem.h> |
| 26 | #include <stdarg.h> |
Philipp Tomsich | a740ee9 | 2018-11-25 19:22:18 +0100 | [diff] [blame] | 27 | #include <u-boot/crc.h> |
Lokesh Vutla | a4773c5 | 2019-01-08 19:28:35 +0530 | [diff] [blame] | 28 | #include <watchdog.h> |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 29 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 30 | /* Assumption - run xyzModem protocol over the console port */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 31 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 32 | /* Values magic to the protocol */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 33 | #define SOH 0x01 |
| 34 | #define STX 0x02 |
Pali Rohár | dffeb40 | 2021-08-03 16:28:44 +0200 | [diff] [blame] | 35 | #define ETX 0x03 /* ^C for interrupt */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 36 | #define EOT 0x04 |
| 37 | #define ACK 0x06 |
| 38 | #define BSP 0x08 |
| 39 | #define NAK 0x15 |
| 40 | #define CAN 0x18 |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 41 | #define EOF 0x1A /* ^Z for DOS officionados */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 42 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 43 | /* Data & state local to the protocol */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 44 | static struct |
| 45 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 46 | int *__chan; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 53 | unsigned long file_length, read_length; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 54 | } xyz; |
| 55 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 56 | #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 57 | #define xyzModem_MAX_RETRIES 20 |
| 58 | #define xyzModem_MAX_RETRIES_WITH_CRC 10 |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 59 | #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 60 | |
| 61 | |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 62 | typedef int cyg_int32; |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 63 | static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 64 | CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c) |
| 65 | { |
tomas.melin@vaisala.com | 2c77c0d | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 66 | |
| 67 | ulong now = get_timer(0); |
Lokesh Vutla | a4773c5 | 2019-01-08 19:28:35 +0530 | [diff] [blame] | 68 | WATCHDOG_RESET(); |
tomas.melin@vaisala.com | 2c77c0d | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 69 | while (!tstc ()) |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 70 | { |
tomas.melin@vaisala.com | 2c77c0d | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 71 | if (get_timer(now) > xyzModem_CHAR_TIMEOUT) |
| 72 | break; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 73 | } |
| 74 | if (tstc ()) |
| 75 | { |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 76 | *c = getchar(); |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 77 | return 1; |
| 78 | } |
| 79 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 82 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 83 | CYGACC_COMM_IF_PUTC (char x, char y) |
| 84 | { |
| 85 | putc (y); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 88 | /* Validate a hex character */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 89 | __inline__ static bool |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 90 | _is_hex (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 91 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 92 | return (((c >= '0') && (c <= '9')) || |
| 93 | ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 96 | /* Convert a single hex nibble */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 97 | __inline__ static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 98 | _from_hex (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 99 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 100 | int ret = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 101 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 102 | if ((c >= '0') && (c <= '9')) |
| 103 | { |
| 104 | ret = (c - '0'); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 105 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 106 | else if ((c >= 'a') && (c <= 'f')) |
| 107 | { |
| 108 | ret = (c - 'a' + 0x0a); |
| 109 | } |
| 110 | else if ((c >= 'A') && (c <= 'F')) |
| 111 | { |
| 112 | ret = (c - 'A' + 0x0A); |
| 113 | } |
| 114 | return ret; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 117 | /* Convert a character to lower case */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 118 | __inline__ static char |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 119 | _tolower (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 120 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 121 | if ((c >= 'A') && (c <= 'Z')) |
| 122 | { |
| 123 | c = (c - 'A') + 'a'; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 124 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 125 | return c; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 128 | /* Parse (scan) a number */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 129 | static bool |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 130 | parse_num (char *s, unsigned long *val, char **es, char *delim) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 131 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 132 | bool first = true; |
| 133 | int radix = 10; |
| 134 | char c; |
| 135 | unsigned long result = 0; |
| 136 | int digit; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 137 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 138 | while (*s == ' ') |
| 139 | s++; |
| 140 | while (*s) |
| 141 | { |
| 142 | if (first && (s[0] == '0') && (_tolower (s[1]) == 'x')) |
| 143 | { |
| 144 | radix = 16; |
| 145 | s += 2; |
| 146 | } |
| 147 | first = false; |
| 148 | c = *s++; |
| 149 | if (_is_hex (c) && ((digit = _from_hex (c)) < radix)) |
| 150 | { |
| 151 | /* Valid digit */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 152 | result = (result * radix) + digit; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
| 156 | if (delim != (char *) 0) |
| 157 | { |
| 158 | /* See if this character is one of the delimiters */ |
| 159 | char *dp = delim; |
| 160 | while (*dp && (c != *dp)) |
| 161 | dp++; |
| 162 | if (*dp) |
| 163 | break; /* Found a good delimiter */ |
| 164 | } |
| 165 | return false; /* Malformatted number */ |
| 166 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 167 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 168 | *val = result; |
| 169 | if (es != (char **) 0) |
| 170 | { |
| 171 | *es = s; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 172 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 173 | return true; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 176 | |
Simon Glass | 27084c0 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 177 | #if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Note: this debug setup works by storing the strings in a fixed buffer |
| 180 | */ |
Alexandru Gagniuc | b09ece0 | 2017-04-04 10:42:31 -0700 | [diff] [blame] | 181 | static char zm_debug_buf[8192]; |
| 182 | static char *zm_out = zm_debug_buf; |
| 183 | static char *zm_out_start = zm_debug_buf; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 184 | |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 185 | static int |
Heinrich Schuchardt | 23c6489 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 186 | zm_dprintf(char *fmt, ...) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 187 | { |
Heinrich Schuchardt | 23c6489 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 188 | int len; |
| 189 | va_list args; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 190 | |
Heinrich Schuchardt | 23c6489 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 191 | va_start(args, fmt); |
| 192 | len = diag_vsprintf(zm_out, fmt, args); |
| 193 | va_end(args); |
| 194 | zm_out += len; |
| 195 | return len; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 199 | zm_flush (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 200 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 201 | zm_out = zm_out_start; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 202 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 203 | |
| 204 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 205 | zm_dump_buf (void *buf, int len) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 206 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 207 | |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static unsigned char zm_buf[2048]; |
| 211 | static unsigned char *zm_bp; |
| 212 | |
| 213 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 214 | zm_new (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 215 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 216 | zm_bp = zm_buf; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 220 | zm_save (unsigned char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 221 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 222 | *zm_bp++ = c; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 226 | zm_dump (int line) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 227 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 228 | zm_dprintf ("Packet at line: %d\n", line); |
| 229 | zm_dump_buf (zm_buf, zm_bp - zm_buf); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | #define ZM_DEBUG(x) x |
| 233 | #else |
| 234 | #define ZM_DEBUG(x) |
| 235 | #endif |
| 236 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 237 | /* Wait for the line to go idle */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 238 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 239 | xyzModem_flush (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 240 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 241 | int res; |
| 242 | char c; |
| 243 | while (true) |
| 244 | { |
| 245 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 246 | if (!res) |
| 247 | return; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 252 | xyzModem_get_hdr (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 253 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 254 | char c; |
| 255 | int res; |
| 256 | bool hdr_found = false; |
| 257 | int i, can_total, hdr_chars; |
| 258 | unsigned short cksum; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 259 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 260 | ZM_DEBUG (zm_new ()); |
| 261 | /* Find the start of a header */ |
| 262 | can_total = 0; |
| 263 | hdr_chars = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 264 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 265 | if (xyz.tx_ack) |
| 266 | { |
| 267 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 268 | xyz.tx_ack = false; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 269 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 270 | while (!hdr_found) |
| 271 | { |
| 272 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 273 | ZM_DEBUG (zm_save (c)); |
| 274 | if (res) |
| 275 | { |
| 276 | hdr_chars++; |
| 277 | switch (c) |
| 278 | { |
| 279 | case SOH: |
| 280 | xyz.total_SOH++; |
| 281 | case STX: |
| 282 | if (c == STX) |
| 283 | xyz.total_STX++; |
| 284 | hdr_found = true; |
| 285 | break; |
| 286 | case CAN: |
Pali Rohár | dffeb40 | 2021-08-03 16:28:44 +0200 | [diff] [blame] | 287 | case ETX: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 288 | xyz.total_CAN++; |
| 289 | ZM_DEBUG (zm_dump (__LINE__)); |
| 290 | if (++can_total == xyzModem_CAN_COUNT) |
| 291 | { |
| 292 | return xyzModem_cancel; |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | /* Wait for multiple CAN to avoid early quits */ |
| 297 | break; |
| 298 | } |
| 299 | case EOT: |
| 300 | /* EOT only supported if no noise */ |
| 301 | if (hdr_chars == 1) |
| 302 | { |
| 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__)); |
| 306 | return xyzModem_eof; |
| 307 | } |
| 308 | default: |
| 309 | /* Ignore, waiting for start of header */ |
| 310 | ; |
| 311 | } |
| 312 | } |
| 313 | else |
| 314 | { |
| 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; |
| 320 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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)); |
| 326 | if (!res) |
| 327 | { |
| 328 | ZM_DEBUG (zm_dump (__LINE__)); |
| 329 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 330 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 331 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk); |
| 332 | ZM_DEBUG (zm_save (xyz.cblk)); |
| 333 | if (!res) |
| 334 | { |
| 335 | ZM_DEBUG (zm_dump (__LINE__)); |
| 336 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 337 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 338 | xyz.len = (c == SOH) ? 128 : 1024; |
| 339 | xyz.bufp = xyz.pkt; |
| 340 | for (i = 0; i < xyz.len; i++) |
| 341 | { |
| 342 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 343 | ZM_DEBUG (zm_save (c)); |
| 344 | if (res) |
| 345 | { |
| 346 | xyz.pkt[i] = c; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | ZM_DEBUG (zm_dump (__LINE__)); |
| 351 | return xyzModem_timeout; |
| 352 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 353 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 354 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1); |
| 355 | ZM_DEBUG (zm_save (xyz.crc1)); |
| 356 | if (!res) |
| 357 | { |
| 358 | ZM_DEBUG (zm_dump (__LINE__)); |
| 359 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 360 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 361 | if (xyz.crc_mode) |
| 362 | { |
| 363 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2); |
| 364 | ZM_DEBUG (zm_save (xyz.crc2)); |
| 365 | if (!res) |
| 366 | { |
| 367 | ZM_DEBUG (zm_dump (__LINE__)); |
| 368 | return xyzModem_timeout; |
| 369 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 370 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 371 | ZM_DEBUG (zm_dump (__LINE__)); |
| 372 | /* Validate the message */ |
| 373 | if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF) |
| 374 | { |
| 375 | ZM_DEBUG (zm_dprintf |
| 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)); |
| 379 | xyzModem_flush (); |
| 380 | return xyzModem_frame; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 381 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 382 | /* Verify checksum/CRC */ |
| 383 | if (xyz.crc_mode) |
| 384 | { |
Stefan Roese | ecb57f6 | 2016-03-03 09:34:12 +0100 | [diff] [blame] | 385 | cksum = crc16_ccitt(0, xyz.pkt, xyz.len); |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 386 | if (cksum != ((xyz.crc1 << 8) | xyz.crc2)) |
| 387 | { |
| 388 | ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n", |
| 389 | xyz.crc1, xyz.crc2, cksum & 0xFFFF)); |
| 390 | return xyzModem_cksum; |
| 391 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 392 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 393 | else |
| 394 | { |
| 395 | cksum = 0; |
| 396 | for (i = 0; i < xyz.len; i++) |
| 397 | { |
| 398 | cksum += xyz.pkt[i]; |
| 399 | } |
| 400 | if (xyz.crc1 != (cksum & 0xFF)) |
| 401 | { |
| 402 | ZM_DEBUG (zm_dprintf |
| 403 | ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1, |
| 404 | cksum & 0xFF)); |
| 405 | return xyzModem_cksum; |
| 406 | } |
| 407 | } |
| 408 | /* If we get here, the message passes [structural] muster */ |
| 409 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 412 | int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 413 | xyzModem_stream_open (connection_info_t * info, int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 414 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 415 | int stat = 0; |
| 416 | int retries = xyzModem_MAX_RETRIES; |
| 417 | int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 418 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 419 | /* ZM_DEBUG(zm_out = zm_out_start); */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 420 | #ifdef xyzModem_zmodem |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 421 | if (info->mode == xyzModem_zmodem) |
| 422 | { |
| 423 | *err = xyzModem_noZmodem; |
| 424 | return -1; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 425 | } |
| 426 | #endif |
| 427 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 428 | /* TODO: CHECK ! */ |
Kim Phillips | 2a2ed84 | 2009-06-15 11:50:40 -0500 | [diff] [blame] | 429 | int dummy = 0; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 430 | xyz.__chan = &dummy; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 431 | xyz.len = 0; |
| 432 | xyz.crc_mode = true; |
| 433 | xyz.at_eof = false; |
| 434 | xyz.tx_ack = false; |
| 435 | xyz.mode = info->mode; |
| 436 | xyz.total_retries = 0; |
| 437 | xyz.total_SOH = 0; |
| 438 | xyz.total_STX = 0; |
| 439 | xyz.total_CAN = 0; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 440 | xyz.read_length = 0; |
| 441 | xyz.file_length = 0; |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 442 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 443 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 444 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 445 | if (xyz.mode == xyzModem_xmodem) |
| 446 | { |
| 447 | /* X-modem doesn't have an information header - exit here */ |
| 448 | xyz.next_blk = 1; |
| 449 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 452 | while (retries-- > 0) |
| 453 | { |
| 454 | stat = xyzModem_get_hdr (); |
| 455 | if (stat == 0) |
| 456 | { |
| 457 | /* Y-modem file information header */ |
| 458 | if (xyz.blk == 0) |
| 459 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 460 | /* skip filename */ |
| 461 | while (*xyz.bufp++); |
| 462 | /* get the length */ |
| 463 | parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " "); |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 464 | /* The rest of the file name data block quietly discarded */ |
| 465 | xyz.tx_ack = true; |
| 466 | } |
| 467 | xyz.next_blk = 1; |
| 468 | xyz.len = 0; |
| 469 | return 0; |
| 470 | } |
| 471 | else if (stat == xyzModem_timeout) |
| 472 | { |
| 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)); |
| 477 | xyz.total_retries++; |
| 478 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); |
| 479 | } |
| 480 | if (stat == xyzModem_cancel) |
| 481 | { |
| 482 | break; |
| 483 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 484 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 485 | *err = stat; |
| 486 | ZM_DEBUG (zm_flush ()); |
| 487 | return -1; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 490 | int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 491 | xyzModem_stream_read (char *buf, int size, int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 492 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 493 | int stat, total, len; |
| 494 | int retries; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 495 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 496 | total = 0; |
| 497 | stat = xyzModem_cancel; |
| 498 | /* Try and get 'size' bytes into the buffer */ |
Pali Rohár | 1e747846 | 2021-08-03 16:28:38 +0200 | [diff] [blame] | 499 | while (!xyz.at_eof && xyz.len >= 0 && (size > 0)) |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 500 | { |
| 501 | if (xyz.len == 0) |
| 502 | { |
| 503 | retries = xyzModem_MAX_RETRIES; |
| 504 | while (retries-- > 0) |
| 505 | { |
| 506 | stat = xyzModem_get_hdr (); |
| 507 | if (stat == 0) |
| 508 | { |
| 509 | if (xyz.blk == xyz.next_blk) |
| 510 | { |
| 511 | xyz.tx_ack = true; |
| 512 | ZM_DEBUG (zm_dprintf |
| 513 | ("ACK block %d (%d)\n", xyz.blk, __LINE__)); |
| 514 | xyz.next_blk = (xyz.next_blk + 1) & 0xFF; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 515 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 516 | if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) |
| 517 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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)) |
| 523 | { |
| 524 | while (xyz.len |
| 525 | && (xyz.bufp[xyz.len - 1] == EOF)) |
| 526 | { |
| 527 | xyz.len--; |
| 528 | } |
| 529 | } |
| 530 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 531 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 532 | /* |
| 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). |
| 537 | */ |
| 538 | if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length) |
| 539 | { |
| 540 | xyz.read_length += xyz.len; |
| 541 | if (xyz.read_length > xyz.file_length) |
| 542 | { |
| 543 | xyz.len -= (xyz.read_length - xyz.file_length); |
| 544 | } |
| 545 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF)) |
| 549 | { |
| 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 */ |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | stat = xyzModem_sequence; |
| 557 | } |
| 558 | } |
| 559 | if (stat == xyzModem_cancel) |
| 560 | { |
| 561 | break; |
| 562 | } |
| 563 | if (stat == xyzModem_eof) |
| 564 | { |
| 565 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 566 | ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__)); |
| 567 | if (xyz.mode == xyzModem_ymodem) |
| 568 | { |
| 569 | CYGACC_COMM_IF_PUTC (*xyz.__chan, |
| 570 | (xyz.crc_mode ? 'C' : NAK)); |
| 571 | xyz.total_retries++; |
| 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__)); |
| 576 | } |
Pali Rohár | 15c27a5 | 2021-08-03 16:28:39 +0200 | [diff] [blame] | 577 | else |
| 578 | stat = 0; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 579 | xyz.at_eof = true; |
| 580 | break; |
| 581 | } |
| 582 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
| 583 | xyz.total_retries++; |
| 584 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); |
| 585 | } |
| 586 | if (stat < 0) |
| 587 | { |
| 588 | *err = stat; |
| 589 | xyz.len = -1; |
| 590 | return total; |
| 591 | } |
| 592 | } |
| 593 | /* Don't "read" data from the EOF protocol package */ |
Pali Rohár | 1e747846 | 2021-08-03 16:28:38 +0200 | [diff] [blame] | 594 | if (!xyz.at_eof && xyz.len > 0) |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 595 | { |
| 596 | len = xyz.len; |
| 597 | if (size < len) |
| 598 | len = size; |
| 599 | memcpy (buf, xyz.bufp, len); |
| 600 | size -= len; |
| 601 | buf += len; |
| 602 | total += len; |
| 603 | xyz.len -= len; |
| 604 | xyz.bufp += len; |
| 605 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 606 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 607 | return total; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 611 | xyzModem_stream_close (int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 612 | { |
Pali Rohár | f05d69b | 2021-08-03 16:28:40 +0200 | [diff] [blame] | 613 | ZM_DEBUG (zm_dprintf |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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, |
Pali Rohár | f05d69b | 2021-08-03 16:28:40 +0200 | [diff] [blame] | 616 | xyz.total_CAN, xyz.total_retries)); |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 617 | ZM_DEBUG (zm_flush ()); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 620 | /* Need to be able to clean out the input buffer, so have to take the */ |
| 621 | /* getc */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 622 | void |
| 623 | xyzModem_stream_terminate (bool abort, int (*getc) (void)) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 624 | { |
| 625 | int c; |
| 626 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 627 | if (abort) |
| 628 | { |
| 629 | ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n")); |
| 630 | switch (xyz.mode) |
| 631 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 632 | case xyzModem_xmodem: |
| 633 | case xyzModem_ymodem: |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 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. */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 644 | /* Now consume the rest of what's waiting on the line. */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 645 | ZM_DEBUG (zm_dprintf ("Flushing serial line.\n")); |
| 646 | xyzModem_flush (); |
| 647 | xyz.at_eof = true; |
| 648 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 649 | #ifdef xyzModem_zmodem |
| 650 | case xyzModem_zmodem: |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 651 | /* Might support it some day I suppose. */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 652 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n")); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 659 | /* |
| 660 | * Consume any trailing crap left in the inbuffer from |
Mike Williams | 1626308 | 2011-07-22 04:01:30 +0000 | [diff] [blame] | 661 | * previous received blocks. Since very few files are an exact multiple |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 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. |
| 664 | */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 665 | ZM_DEBUG (zm_dprintf ("Trailing gunk:\n")); |
Jeroen Hofstee | e153b13 | 2014-06-11 01:04:42 +0200 | [diff] [blame] | 666 | while ((c = (*getc) ()) > -1) |
| 667 | ; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 668 | ZM_DEBUG (zm_dprintf ("\n")); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 669 | /* |
| 670 | * Make a small delay to give terminal programs like minicom |
| 671 | * time to get control again after their file transfer program |
| 672 | * exits. |
| 673 | */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 674 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
| 675 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | char * |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 679 | xyzModem_error (int err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 680 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 681 | switch (err) |
| 682 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 683 | case xyzModem_access: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 684 | return "Can't access file"; |
| 685 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 686 | case xyzModem_noZmodem: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 687 | return "Sorry, zModem not available yet"; |
| 688 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 689 | case xyzModem_timeout: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 690 | return "Timed out"; |
| 691 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 692 | case xyzModem_eof: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 693 | return "End of file"; |
| 694 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 695 | case xyzModem_cancel: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 696 | return "Cancelled"; |
| 697 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 698 | case xyzModem_frame: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 699 | return "Invalid framing"; |
| 700 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 701 | case xyzModem_cksum: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 702 | return "CRC/checksum error"; |
| 703 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 704 | case xyzModem_sequence: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 705 | return "Block sequence error"; |
| 706 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 707 | default: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 708 | return "Unknown error"; |
| 709 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 713 | /* |
| 714 | * RedBoot interface |
| 715 | */ |