]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: eCos-2.0 |
cf48eb9a WD |
2 | /* |
3 | *========================================================================== | |
4 | * | |
5 | * xyzModem.c | |
6 | * | |
7 | * RedBoot stream handler for xyzModem protocol | |
8 | * | |
9 | *========================================================================== | |
cf48eb9a WD |
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 | */ | |
f2841d37 MK |
24 | #include <common.h> |
25 | #include <xyzModem.h> | |
26 | #include <stdarg.h> | |
a740ee91 | 27 | #include <u-boot/crc.h> |
a4773c55 | 28 | #include <watchdog.h> |
f2841d37 | 29 | |
cf48eb9a | 30 | /* Assumption - run xyzModem protocol over the console port */ |
f2841d37 | 31 | |
cf48eb9a | 32 | /* Values magic to the protocol */ |
f2841d37 MK |
33 | #define SOH 0x01 |
34 | #define STX 0x02 | |
dffeb400 | 35 | #define ETX 0x03 /* ^C for interrupt */ |
f2841d37 MK |
36 | #define EOT 0x04 |
37 | #define ACK 0x06 | |
38 | #define BSP 0x08 | |
39 | #define NAK 0x15 | |
40 | #define CAN 0x18 | |
7d0432c9 | 41 | #define EOF 0x1A /* ^Z for DOS officionados */ |
f2841d37 | 42 | |
cf48eb9a | 43 | /* Data & state local to the protocol */ |
7d0432c9 WD |
44 | static struct |
45 | { | |
7d0432c9 | 46 | int *__chan; |
7d0432c9 WD |
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; | |
7d0432c9 | 53 | unsigned long file_length, read_length; |
f2841d37 MK |
54 | } xyz; |
55 | ||
7d0432c9 | 56 | #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */ |
f2841d37 MK |
57 | #define xyzModem_MAX_RETRIES 20 |
58 | #define xyzModem_MAX_RETRIES_WITH_CRC 10 | |
7d0432c9 | 59 | #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */ |
f2841d37 MK |
60 | |
61 | ||
f2841d37 | 62 | typedef int cyg_int32; |
199adb60 | 63 | static int |
7d0432c9 WD |
64 | CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c) |
65 | { | |
2c77c0d6 | 66 | |
67 | ulong now = get_timer(0); | |
a4773c55 | 68 | WATCHDOG_RESET(); |
2c77c0d6 | 69 | while (!tstc ()) |
7d0432c9 | 70 | { |
2c77c0d6 | 71 | if (get_timer(now) > xyzModem_CHAR_TIMEOUT) |
72 | break; | |
7d0432c9 WD |
73 | } |
74 | if (tstc ()) | |
75 | { | |
c670aeee | 76 | *c = getchar(); |
7d0432c9 WD |
77 | return 1; |
78 | } | |
79 | return 0; | |
f2841d37 MK |
80 | } |
81 | ||
199adb60 | 82 | static void |
7d0432c9 WD |
83 | CYGACC_COMM_IF_PUTC (char x, char y) |
84 | { | |
85 | putc (y); | |
f2841d37 MK |
86 | } |
87 | ||
cf48eb9a | 88 | /* Validate a hex character */ |
f2841d37 | 89 | __inline__ static bool |
7d0432c9 | 90 | _is_hex (char c) |
f2841d37 | 91 | { |
7d0432c9 WD |
92 | return (((c >= '0') && (c <= '9')) || |
93 | ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))); | |
f2841d37 MK |
94 | } |
95 | ||
cf48eb9a | 96 | /* Convert a single hex nibble */ |
f2841d37 | 97 | __inline__ static int |
7d0432c9 | 98 | _from_hex (char c) |
f2841d37 | 99 | { |
7d0432c9 WD |
100 | int ret = 0; |
101 | ||
102 | if ((c >= '0') && (c <= '9')) | |
103 | { | |
104 | ret = (c - '0'); | |
105 | } | |
106 | else if ((c >= 'a') && (c <= 'f')) | |
107 | { | |
108 | ret = (c - 'a' + 0x0a); | |
f2841d37 | 109 | } |
7d0432c9 WD |
110 | else if ((c >= 'A') && (c <= 'F')) |
111 | { | |
112 | ret = (c - 'A' + 0x0A); | |
113 | } | |
114 | return ret; | |
f2841d37 MK |
115 | } |
116 | ||
cf48eb9a | 117 | /* Convert a character to lower case */ |
f2841d37 | 118 | __inline__ static char |
7d0432c9 | 119 | _tolower (char c) |
f2841d37 | 120 | { |
7d0432c9 WD |
121 | if ((c >= 'A') && (c <= 'Z')) |
122 | { | |
123 | c = (c - 'A') + 'a'; | |
f2841d37 | 124 | } |
7d0432c9 | 125 | return c; |
f2841d37 MK |
126 | } |
127 | ||
cf48eb9a | 128 | /* Parse (scan) a number */ |
199adb60 | 129 | static bool |
7d0432c9 | 130 | parse_num (char *s, unsigned long *val, char **es, char *delim) |
f2841d37 | 131 | { |
7d0432c9 WD |
132 | bool first = true; |
133 | int radix = 10; | |
134 | char c; | |
135 | unsigned long result = 0; | |
136 | int digit; | |
137 | ||
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 */ | |
7d0432c9 | 152 | result = (result * radix) + digit; |
7d0432c9 WD |
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 | } | |
f2841d37 | 167 | } |
7d0432c9 WD |
168 | *val = result; |
169 | if (es != (char **) 0) | |
170 | { | |
171 | *es = s; | |
f2841d37 | 172 | } |
7d0432c9 | 173 | return true; |
f2841d37 MK |
174 | } |
175 | ||
f2841d37 | 176 | |
27084c03 | 177 | #if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
cf48eb9a WD |
178 | /* |
179 | * Note: this debug setup works by storing the strings in a fixed buffer | |
180 | */ | |
b09ece08 AG |
181 | static char zm_debug_buf[8192]; |
182 | static char *zm_out = zm_debug_buf; | |
183 | static char *zm_out_start = zm_debug_buf; | |
f2841d37 | 184 | |
f2841d37 | 185 | static int |
23c64898 | 186 | zm_dprintf(char *fmt, ...) |
f2841d37 | 187 | { |
23c64898 HS |
188 | int len; |
189 | va_list args; | |
190 | ||
191 | va_start(args, fmt); | |
192 | len = diag_vsprintf(zm_out, fmt, args); | |
193 | va_end(args); | |
194 | zm_out += len; | |
195 | return len; | |
f2841d37 MK |
196 | } |
197 | ||
198 | static void | |
7d0432c9 | 199 | zm_flush (void) |
f2841d37 | 200 | { |
7d0432c9 | 201 | zm_out = zm_out_start; |
f2841d37 | 202 | } |
f2841d37 MK |
203 | |
204 | static void | |
7d0432c9 | 205 | zm_dump_buf (void *buf, int len) |
f2841d37 | 206 | { |
f2841d37 | 207 | |
f2841d37 MK |
208 | } |
209 | ||
210 | static unsigned char zm_buf[2048]; | |
211 | static unsigned char *zm_bp; | |
212 | ||
213 | static void | |
7d0432c9 | 214 | zm_new (void) |
f2841d37 | 215 | { |
7d0432c9 | 216 | zm_bp = zm_buf; |
f2841d37 MK |
217 | } |
218 | ||
219 | static void | |
7d0432c9 | 220 | zm_save (unsigned char c) |
f2841d37 | 221 | { |
7d0432c9 | 222 | *zm_bp++ = c; |
f2841d37 MK |
223 | } |
224 | ||
225 | static void | |
7d0432c9 | 226 | zm_dump (int line) |
f2841d37 | 227 | { |
7d0432c9 WD |
228 | zm_dprintf ("Packet at line: %d\n", line); |
229 | zm_dump_buf (zm_buf, zm_bp - zm_buf); | |
f2841d37 MK |
230 | } |
231 | ||
232 | #define ZM_DEBUG(x) x | |
233 | #else | |
234 | #define ZM_DEBUG(x) | |
235 | #endif | |
236 | ||
cf48eb9a | 237 | /* Wait for the line to go idle */ |
f2841d37 | 238 | static void |
7d0432c9 | 239 | xyzModem_flush (void) |
f2841d37 | 240 | { |
7d0432c9 WD |
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; | |
f2841d37 MK |
248 | } |
249 | } | |
250 | ||
251 | static int | |
7d0432c9 | 252 | xyzModem_get_hdr (void) |
f2841d37 | 253 | { |
7d0432c9 WD |
254 | char c; |
255 | int res; | |
256 | bool hdr_found = false; | |
257 | int i, can_total, hdr_chars; | |
258 | unsigned short cksum; | |
259 | ||
260 | ZM_DEBUG (zm_new ()); | |
261 | /* Find the start of a header */ | |
262 | can_total = 0; | |
263 | hdr_chars = 0; | |
264 | ||
265 | if (xyz.tx_ack) | |
266 | { | |
267 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
268 | xyz.tx_ack = false; | |
f2841d37 | 269 | } |
7d0432c9 WD |
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: | |
dffeb400 | 287 | case ETX: |
7d0432c9 WD |
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 | } | |
f2841d37 MK |
321 | } |
322 | ||
7d0432c9 WD |
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; | |
f2841d37 | 330 | } |
7d0432c9 WD |
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; | |
f2841d37 | 337 | } |
7d0432c9 WD |
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 | } | |
f2841d37 | 353 | } |
7d0432c9 WD |
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; | |
f2841d37 | 360 | } |
7d0432c9 WD |
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 | } | |
f2841d37 | 370 | } |
7d0432c9 WD |
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; | |
f2841d37 | 381 | } |
7d0432c9 WD |
382 | /* Verify checksum/CRC */ |
383 | if (xyz.crc_mode) | |
384 | { | |
ecb57f69 | 385 | cksum = crc16_ccitt(0, xyz.pkt, xyz.len); |
7d0432c9 WD |
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 | } | |
f2841d37 | 392 | } |
7d0432c9 WD |
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; | |
f2841d37 MK |
410 | } |
411 | ||
cf48eb9a | 412 | int |
7d0432c9 | 413 | xyzModem_stream_open (connection_info_t * info, int *err) |
f2841d37 | 414 | { |
7d0432c9 WD |
415 | int stat = 0; |
416 | int retries = xyzModem_MAX_RETRIES; | |
417 | int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; | |
f2841d37 | 418 | |
cf48eb9a | 419 | /* ZM_DEBUG(zm_out = zm_out_start); */ |
f2841d37 | 420 | #ifdef xyzModem_zmodem |
7d0432c9 WD |
421 | if (info->mode == xyzModem_zmodem) |
422 | { | |
423 | *err = xyzModem_noZmodem; | |
424 | return -1; | |
f2841d37 MK |
425 | } |
426 | #endif | |
427 | ||
cf48eb9a | 428 | /* TODO: CHECK ! */ |
2a2ed845 | 429 | int dummy = 0; |
7d0432c9 | 430 | xyz.__chan = &dummy; |
7d0432c9 WD |
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; | |
7d0432c9 WD |
440 | xyz.read_length = 0; |
441 | xyz.file_length = 0; | |
cf48eb9a | 442 | |
7d0432c9 | 443 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
f2841d37 | 444 | |
7d0432c9 WD |
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; | |
f2841d37 MK |
450 | } |
451 | ||
7d0432c9 WD |
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 | { | |
7d0432c9 WD |
460 | /* skip filename */ |
461 | while (*xyz.bufp++); | |
462 | /* get the length */ | |
463 | parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " "); | |
7d0432c9 WD |
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 | } | |
f2841d37 | 484 | } |
7d0432c9 WD |
485 | *err = stat; |
486 | ZM_DEBUG (zm_flush ()); | |
487 | return -1; | |
f2841d37 MK |
488 | } |
489 | ||
cf48eb9a | 490 | int |
7d0432c9 | 491 | xyzModem_stream_read (char *buf, int size, int *err) |
f2841d37 | 492 | { |
7d0432c9 WD |
493 | int stat, total, len; |
494 | int retries; | |
495 | ||
496 | total = 0; | |
497 | stat = xyzModem_cancel; | |
498 | /* Try and get 'size' bytes into the buffer */ | |
1e747846 | 499 | while (!xyz.at_eof && xyz.len >= 0 && (size > 0)) |
7d0432c9 WD |
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; | |
f2841d37 | 515 | |
7d0432c9 WD |
516 | if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) |
517 | { | |
7d0432c9 WD |
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 | } | |
f2841d37 | 531 | |
7d0432c9 WD |
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 | } | |
7d0432c9 WD |
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 | } | |
15c27a5a T |
577 | else |
578 | stat = 0; | |
7d0432c9 WD |
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 */ | |
1e747846 | 594 | if (!xyz.at_eof && xyz.len > 0) |
7d0432c9 WD |
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 | } | |
f2841d37 | 606 | } |
7d0432c9 | 607 | return total; |
f2841d37 MK |
608 | } |
609 | ||
610 | void | |
7d0432c9 | 611 | xyzModem_stream_close (int *err) |
f2841d37 | 612 | { |
f05d69bd | 613 | ZM_DEBUG (zm_dprintf |
7d0432c9 WD |
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, | |
f05d69bd | 616 | xyz.total_CAN, xyz.total_retries)); |
7d0432c9 | 617 | ZM_DEBUG (zm_flush ()); |
f2841d37 MK |
618 | } |
619 | ||
cf48eb9a WD |
620 | /* Need to be able to clean out the input buffer, so have to take the */ |
621 | /* getc */ | |
7d0432c9 WD |
622 | void |
623 | xyzModem_stream_terminate (bool abort, int (*getc) (void)) | |
f2841d37 MK |
624 | { |
625 | int c; | |
626 | ||
7d0432c9 WD |
627 | if (abort) |
628 | { | |
629 | ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n")); | |
630 | switch (xyz.mode) | |
631 | { | |
f2841d37 MK |
632 | case xyzModem_xmodem: |
633 | case xyzModem_ymodem: | |
cf48eb9a WD |
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. */ | |
7d0432c9 WD |
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); | |
cf48eb9a | 644 | /* Now consume the rest of what's waiting on the line. */ |
7d0432c9 WD |
645 | ZM_DEBUG (zm_dprintf ("Flushing serial line.\n")); |
646 | xyzModem_flush (); | |
647 | xyz.at_eof = true; | |
648 | break; | |
f2841d37 MK |
649 | #ifdef xyzModem_zmodem |
650 | case xyzModem_zmodem: | |
cf48eb9a | 651 | /* Might support it some day I suppose. */ |
f2841d37 | 652 | #endif |
7d0432c9 WD |
653 | break; |
654 | } | |
655 | } | |
656 | else | |
657 | { | |
658 | ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n")); | |
cf48eb9a WD |
659 | /* |
660 | * Consume any trailing crap left in the inbuffer from | |
16263087 | 661 | * previous received blocks. Since very few files are an exact multiple |
cf48eb9a WD |
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 | */ | |
7d0432c9 | 665 | ZM_DEBUG (zm_dprintf ("Trailing gunk:\n")); |
e153b13c JH |
666 | while ((c = (*getc) ()) > -1) |
667 | ; | |
7d0432c9 | 668 | ZM_DEBUG (zm_dprintf ("\n")); |
cf48eb9a WD |
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 | */ | |
7d0432c9 WD |
674 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
675 | } | |
f2841d37 MK |
676 | } |
677 | ||
678 | char * | |
7d0432c9 | 679 | xyzModem_error (int err) |
f2841d37 | 680 | { |
7d0432c9 WD |
681 | switch (err) |
682 | { | |
f2841d37 | 683 | case xyzModem_access: |
7d0432c9 WD |
684 | return "Can't access file"; |
685 | break; | |
f2841d37 | 686 | case xyzModem_noZmodem: |
7d0432c9 WD |
687 | return "Sorry, zModem not available yet"; |
688 | break; | |
f2841d37 | 689 | case xyzModem_timeout: |
7d0432c9 WD |
690 | return "Timed out"; |
691 | break; | |
f2841d37 | 692 | case xyzModem_eof: |
7d0432c9 WD |
693 | return "End of file"; |
694 | break; | |
f2841d37 | 695 | case xyzModem_cancel: |
7d0432c9 WD |
696 | return "Cancelled"; |
697 | break; | |
f2841d37 | 698 | case xyzModem_frame: |
7d0432c9 WD |
699 | return "Invalid framing"; |
700 | break; | |
f2841d37 | 701 | case xyzModem_cksum: |
7d0432c9 WD |
702 | return "CRC/checksum error"; |
703 | break; | |
f2841d37 | 704 | case xyzModem_sequence: |
7d0432c9 WD |
705 | return "Block sequence error"; |
706 | break; | |
f2841d37 | 707 | default: |
7d0432c9 WD |
708 | return "Unknown error"; |
709 | break; | |
f2841d37 MK |
710 | } |
711 | } | |
712 | ||
cf48eb9a WD |
713 | /* |
714 | * RedBoot interface | |
715 | */ |