]>
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 <xyzModem.h> |
25 | #include <stdarg.h> | |
03de305e | 26 | #include <time.h> |
a740ee91 | 27 | #include <u-boot/crc.h> |
a4773c55 | 28 | #include <watchdog.h> |
1b3e6820 | 29 | #include <env.h> |
03de305e | 30 | #include <vsprintf.h> |
f2841d37 | 31 | |
cf48eb9a | 32 | /* Assumption - run xyzModem protocol over the console port */ |
f2841d37 | 33 | |
cf48eb9a | 34 | /* Values magic to the protocol */ |
f2841d37 MK |
35 | #define SOH 0x01 |
36 | #define STX 0x02 | |
dffeb400 | 37 | #define ETX 0x03 /* ^C for interrupt */ |
f2841d37 MK |
38 | #define EOT 0x04 |
39 | #define ACK 0x06 | |
40 | #define BSP 0x08 | |
41 | #define NAK 0x15 | |
42 | #define CAN 0x18 | |
7d0432c9 | 43 | #define EOF 0x1A /* ^Z for DOS officionados */ |
f2841d37 | 44 | |
cf48eb9a | 45 | /* Data & state local to the protocol */ |
7d0432c9 WD |
46 | static struct |
47 | { | |
7d0432c9 | 48 | int *__chan; |
7d0432c9 WD |
49 | unsigned char pkt[1024], *bufp; |
50 | unsigned char blk, cblk, crc1, crc2; | |
51 | unsigned char next_blk; /* Expected block */ | |
52 | int len, mode, total_retries; | |
53 | int total_SOH, total_STX, total_CAN; | |
54 | bool crc_mode, at_eof, tx_ack; | |
1b3e6820 T |
55 | bool first_xmodem_packet; |
56 | ulong initial_time, timeout; | |
7d0432c9 | 57 | unsigned long file_length, read_length; |
f2841d37 MK |
58 | } xyz; |
59 | ||
7d0432c9 | 60 | #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */ |
f2841d37 MK |
61 | #define xyzModem_MAX_RETRIES 20 |
62 | #define xyzModem_MAX_RETRIES_WITH_CRC 10 | |
7d0432c9 | 63 | #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */ |
f2841d37 MK |
64 | |
65 | ||
f2841d37 | 66 | typedef int cyg_int32; |
199adb60 | 67 | static int |
7d0432c9 WD |
68 | CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c) |
69 | { | |
2c77c0d6 | 70 | |
71 | ulong now = get_timer(0); | |
29caf930 | 72 | schedule(); |
2c77c0d6 | 73 | while (!tstc ()) |
7d0432c9 | 74 | { |
2c77c0d6 | 75 | if (get_timer(now) > xyzModem_CHAR_TIMEOUT) |
76 | break; | |
7d0432c9 WD |
77 | } |
78 | if (tstc ()) | |
79 | { | |
c670aeee | 80 | *c = getchar(); |
7d0432c9 WD |
81 | return 1; |
82 | } | |
83 | return 0; | |
f2841d37 MK |
84 | } |
85 | ||
199adb60 | 86 | static void |
7d0432c9 WD |
87 | CYGACC_COMM_IF_PUTC (char x, char y) |
88 | { | |
89 | putc (y); | |
f2841d37 MK |
90 | } |
91 | ||
cf48eb9a | 92 | /* Validate a hex character */ |
f2841d37 | 93 | __inline__ static bool |
7d0432c9 | 94 | _is_hex (char c) |
f2841d37 | 95 | { |
7d0432c9 WD |
96 | return (((c >= '0') && (c <= '9')) || |
97 | ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))); | |
f2841d37 MK |
98 | } |
99 | ||
cf48eb9a | 100 | /* Convert a single hex nibble */ |
f2841d37 | 101 | __inline__ static int |
7d0432c9 | 102 | _from_hex (char c) |
f2841d37 | 103 | { |
7d0432c9 WD |
104 | int ret = 0; |
105 | ||
106 | if ((c >= '0') && (c <= '9')) | |
107 | { | |
108 | ret = (c - '0'); | |
109 | } | |
110 | else if ((c >= 'a') && (c <= 'f')) | |
111 | { | |
112 | ret = (c - 'a' + 0x0a); | |
f2841d37 | 113 | } |
7d0432c9 WD |
114 | else if ((c >= 'A') && (c <= 'F')) |
115 | { | |
116 | ret = (c - 'A' + 0x0A); | |
117 | } | |
118 | return ret; | |
f2841d37 MK |
119 | } |
120 | ||
cf48eb9a | 121 | /* Convert a character to lower case */ |
f2841d37 | 122 | __inline__ static char |
7d0432c9 | 123 | _tolower (char c) |
f2841d37 | 124 | { |
7d0432c9 WD |
125 | if ((c >= 'A') && (c <= 'Z')) |
126 | { | |
127 | c = (c - 'A') + 'a'; | |
f2841d37 | 128 | } |
7d0432c9 | 129 | return c; |
f2841d37 MK |
130 | } |
131 | ||
cf48eb9a | 132 | /* Parse (scan) a number */ |
199adb60 | 133 | static bool |
7d0432c9 | 134 | parse_num (char *s, unsigned long *val, char **es, char *delim) |
f2841d37 | 135 | { |
7d0432c9 WD |
136 | bool first = true; |
137 | int radix = 10; | |
138 | char c; | |
139 | unsigned long result = 0; | |
140 | int digit; | |
141 | ||
142 | while (*s == ' ') | |
143 | s++; | |
144 | while (*s) | |
145 | { | |
146 | if (first && (s[0] == '0') && (_tolower (s[1]) == 'x')) | |
147 | { | |
148 | radix = 16; | |
149 | s += 2; | |
150 | } | |
151 | first = false; | |
152 | c = *s++; | |
153 | if (_is_hex (c) && ((digit = _from_hex (c)) < radix)) | |
154 | { | |
155 | /* Valid digit */ | |
7d0432c9 | 156 | result = (result * radix) + digit; |
7d0432c9 WD |
157 | } |
158 | else | |
159 | { | |
160 | if (delim != (char *) 0) | |
161 | { | |
162 | /* See if this character is one of the delimiters */ | |
163 | char *dp = delim; | |
164 | while (*dp && (c != *dp)) | |
165 | dp++; | |
166 | if (*dp) | |
167 | break; /* Found a good delimiter */ | |
168 | } | |
169 | return false; /* Malformatted number */ | |
170 | } | |
f2841d37 | 171 | } |
7d0432c9 WD |
172 | *val = result; |
173 | if (es != (char **) 0) | |
174 | { | |
175 | *es = s; | |
f2841d37 | 176 | } |
7d0432c9 | 177 | return true; |
f2841d37 MK |
178 | } |
179 | ||
f2841d37 | 180 | |
27084c03 | 181 | #if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
cf48eb9a WD |
182 | /* |
183 | * Note: this debug setup works by storing the strings in a fixed buffer | |
184 | */ | |
b09ece08 AG |
185 | static char zm_debug_buf[8192]; |
186 | static char *zm_out = zm_debug_buf; | |
187 | static char *zm_out_start = zm_debug_buf; | |
f2841d37 | 188 | |
f2841d37 | 189 | static int |
23c64898 | 190 | zm_dprintf(char *fmt, ...) |
f2841d37 | 191 | { |
23c64898 HS |
192 | int len; |
193 | va_list args; | |
194 | ||
195 | va_start(args, fmt); | |
196 | len = diag_vsprintf(zm_out, fmt, args); | |
197 | va_end(args); | |
198 | zm_out += len; | |
199 | return len; | |
f2841d37 MK |
200 | } |
201 | ||
202 | static void | |
7d0432c9 | 203 | zm_flush (void) |
f2841d37 | 204 | { |
7d0432c9 | 205 | zm_out = zm_out_start; |
f2841d37 | 206 | } |
f2841d37 MK |
207 | |
208 | static void | |
7d0432c9 | 209 | zm_dump_buf (void *buf, int len) |
f2841d37 | 210 | { |
f2841d37 | 211 | |
f2841d37 MK |
212 | } |
213 | ||
214 | static unsigned char zm_buf[2048]; | |
215 | static unsigned char *zm_bp; | |
216 | ||
217 | static void | |
7d0432c9 | 218 | zm_new (void) |
f2841d37 | 219 | { |
7d0432c9 | 220 | zm_bp = zm_buf; |
f2841d37 MK |
221 | } |
222 | ||
223 | static void | |
7d0432c9 | 224 | zm_save (unsigned char c) |
f2841d37 | 225 | { |
7d0432c9 | 226 | *zm_bp++ = c; |
f2841d37 MK |
227 | } |
228 | ||
229 | static void | |
7d0432c9 | 230 | zm_dump (int line) |
f2841d37 | 231 | { |
7d0432c9 WD |
232 | zm_dprintf ("Packet at line: %d\n", line); |
233 | zm_dump_buf (zm_buf, zm_bp - zm_buf); | |
f2841d37 MK |
234 | } |
235 | ||
236 | #define ZM_DEBUG(x) x | |
237 | #else | |
238 | #define ZM_DEBUG(x) | |
239 | #endif | |
240 | ||
cf48eb9a | 241 | /* Wait for the line to go idle */ |
f2841d37 | 242 | static void |
7d0432c9 | 243 | xyzModem_flush (void) |
f2841d37 | 244 | { |
7d0432c9 WD |
245 | int res; |
246 | char c; | |
247 | while (true) | |
248 | { | |
249 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); | |
250 | if (!res) | |
251 | return; | |
f2841d37 MK |
252 | } |
253 | } | |
254 | ||
255 | static int | |
7d0432c9 | 256 | xyzModem_get_hdr (void) |
f2841d37 | 257 | { |
7d0432c9 WD |
258 | char c; |
259 | int res; | |
260 | bool hdr_found = false; | |
261 | int i, can_total, hdr_chars; | |
262 | unsigned short cksum; | |
263 | ||
264 | ZM_DEBUG (zm_new ()); | |
265 | /* Find the start of a header */ | |
266 | can_total = 0; | |
267 | hdr_chars = 0; | |
268 | ||
269 | if (xyz.tx_ack) | |
270 | { | |
271 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
272 | xyz.tx_ack = false; | |
f2841d37 | 273 | } |
7d0432c9 WD |
274 | while (!hdr_found) |
275 | { | |
276 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); | |
277 | ZM_DEBUG (zm_save (c)); | |
278 | if (res) | |
279 | { | |
280 | hdr_chars++; | |
281 | switch (c) | |
282 | { | |
283 | case SOH: | |
284 | xyz.total_SOH++; | |
285 | case STX: | |
286 | if (c == STX) | |
287 | xyz.total_STX++; | |
288 | hdr_found = true; | |
289 | break; | |
290 | case CAN: | |
dffeb400 | 291 | case ETX: |
7d0432c9 WD |
292 | xyz.total_CAN++; |
293 | ZM_DEBUG (zm_dump (__LINE__)); | |
294 | if (++can_total == xyzModem_CAN_COUNT) | |
295 | { | |
296 | return xyzModem_cancel; | |
297 | } | |
298 | else | |
299 | { | |
300 | /* Wait for multiple CAN to avoid early quits */ | |
301 | break; | |
302 | } | |
303 | case EOT: | |
304 | /* EOT only supported if no noise */ | |
305 | if (hdr_chars == 1) | |
306 | { | |
307 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
308 | ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__)); | |
309 | ZM_DEBUG (zm_dump (__LINE__)); | |
310 | return xyzModem_eof; | |
311 | } | |
312 | default: | |
313 | /* Ignore, waiting for start of header */ | |
314 | ; | |
315 | } | |
316 | } | |
317 | else | |
318 | { | |
319 | /* Data stream timed out */ | |
320 | xyzModem_flush (); /* Toss any current input */ | |
321 | ZM_DEBUG (zm_dump (__LINE__)); | |
322 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); | |
323 | return xyzModem_timeout; | |
324 | } | |
f2841d37 MK |
325 | } |
326 | ||
7d0432c9 WD |
327 | /* Header found, now read the data */ |
328 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk); | |
329 | ZM_DEBUG (zm_save (xyz.blk)); | |
330 | if (!res) | |
331 | { | |
332 | ZM_DEBUG (zm_dump (__LINE__)); | |
333 | return xyzModem_timeout; | |
f2841d37 | 334 | } |
7d0432c9 WD |
335 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk); |
336 | ZM_DEBUG (zm_save (xyz.cblk)); | |
337 | if (!res) | |
338 | { | |
339 | ZM_DEBUG (zm_dump (__LINE__)); | |
340 | return xyzModem_timeout; | |
f2841d37 | 341 | } |
7d0432c9 WD |
342 | xyz.len = (c == SOH) ? 128 : 1024; |
343 | xyz.bufp = xyz.pkt; | |
344 | for (i = 0; i < xyz.len; i++) | |
345 | { | |
346 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); | |
347 | ZM_DEBUG (zm_save (c)); | |
348 | if (res) | |
349 | { | |
350 | xyz.pkt[i] = c; | |
351 | } | |
352 | else | |
353 | { | |
354 | ZM_DEBUG (zm_dump (__LINE__)); | |
355 | return xyzModem_timeout; | |
356 | } | |
f2841d37 | 357 | } |
7d0432c9 WD |
358 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1); |
359 | ZM_DEBUG (zm_save (xyz.crc1)); | |
360 | if (!res) | |
361 | { | |
362 | ZM_DEBUG (zm_dump (__LINE__)); | |
363 | return xyzModem_timeout; | |
f2841d37 | 364 | } |
7d0432c9 WD |
365 | if (xyz.crc_mode) |
366 | { | |
367 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2); | |
368 | ZM_DEBUG (zm_save (xyz.crc2)); | |
369 | if (!res) | |
370 | { | |
371 | ZM_DEBUG (zm_dump (__LINE__)); | |
372 | return xyzModem_timeout; | |
373 | } | |
f2841d37 | 374 | } |
7d0432c9 WD |
375 | ZM_DEBUG (zm_dump (__LINE__)); |
376 | /* Validate the message */ | |
377 | if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF) | |
378 | { | |
379 | ZM_DEBUG (zm_dprintf | |
380 | ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk, | |
381 | (xyz.blk ^ xyz.cblk))); | |
382 | ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len)); | |
383 | xyzModem_flush (); | |
384 | return xyzModem_frame; | |
f2841d37 | 385 | } |
7d0432c9 WD |
386 | /* Verify checksum/CRC */ |
387 | if (xyz.crc_mode) | |
388 | { | |
ecb57f69 | 389 | cksum = crc16_ccitt(0, xyz.pkt, xyz.len); |
7d0432c9 WD |
390 | if (cksum != ((xyz.crc1 << 8) | xyz.crc2)) |
391 | { | |
392 | ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n", | |
393 | xyz.crc1, xyz.crc2, cksum & 0xFFFF)); | |
394 | return xyzModem_cksum; | |
395 | } | |
f2841d37 | 396 | } |
7d0432c9 WD |
397 | else |
398 | { | |
399 | cksum = 0; | |
400 | for (i = 0; i < xyz.len; i++) | |
401 | { | |
402 | cksum += xyz.pkt[i]; | |
403 | } | |
404 | if (xyz.crc1 != (cksum & 0xFF)) | |
405 | { | |
406 | ZM_DEBUG (zm_dprintf | |
407 | ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1, | |
408 | cksum & 0xFF)); | |
409 | return xyzModem_cksum; | |
410 | } | |
411 | } | |
412 | /* If we get here, the message passes [structural] muster */ | |
413 | return 0; | |
f2841d37 MK |
414 | } |
415 | ||
1b3e6820 T |
416 | static |
417 | ulong | |
418 | xyzModem_get_initial_timeout (void) | |
419 | { | |
420 | /* timeout is in seconds, non-positive timeout value is infinity */ | |
421 | #if CONFIG_IS_ENABLED(ENV_SUPPORT) | |
422 | const char *timeout_str = env_get("loadxy_timeout"); | |
423 | if (timeout_str) | |
424 | return 1000 * simple_strtol(timeout_str, NULL, 10); | |
425 | #endif | |
426 | return 1000 * CONFIG_CMD_LOADXY_TIMEOUT; | |
427 | } | |
428 | ||
cf48eb9a | 429 | int |
7d0432c9 | 430 | xyzModem_stream_open (connection_info_t * info, int *err) |
f2841d37 | 431 | { |
7d0432c9 WD |
432 | int stat = 0; |
433 | int retries = xyzModem_MAX_RETRIES; | |
434 | int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; | |
f2841d37 | 435 | |
cf48eb9a | 436 | /* ZM_DEBUG(zm_out = zm_out_start); */ |
f2841d37 | 437 | #ifdef xyzModem_zmodem |
7d0432c9 WD |
438 | if (info->mode == xyzModem_zmodem) |
439 | { | |
440 | *err = xyzModem_noZmodem; | |
441 | return -1; | |
f2841d37 MK |
442 | } |
443 | #endif | |
444 | ||
cf48eb9a | 445 | /* TODO: CHECK ! */ |
2a2ed845 | 446 | int dummy = 0; |
7d0432c9 | 447 | xyz.__chan = &dummy; |
7d0432c9 WD |
448 | xyz.len = 0; |
449 | xyz.crc_mode = true; | |
450 | xyz.at_eof = false; | |
451 | xyz.tx_ack = false; | |
452 | xyz.mode = info->mode; | |
453 | xyz.total_retries = 0; | |
454 | xyz.total_SOH = 0; | |
455 | xyz.total_STX = 0; | |
456 | xyz.total_CAN = 0; | |
7d0432c9 WD |
457 | xyz.read_length = 0; |
458 | xyz.file_length = 0; | |
1b3e6820 T |
459 | xyz.first_xmodem_packet = false; |
460 | xyz.initial_time = get_timer(0); | |
461 | xyz.timeout = xyzModem_get_initial_timeout(); | |
cf48eb9a | 462 | |
7d0432c9 | 463 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
f2841d37 | 464 | |
7d0432c9 WD |
465 | if (xyz.mode == xyzModem_xmodem) |
466 | { | |
467 | /* X-modem doesn't have an information header - exit here */ | |
1b3e6820 | 468 | xyz.first_xmodem_packet = true; |
7d0432c9 WD |
469 | xyz.next_blk = 1; |
470 | return 0; | |
f2841d37 MK |
471 | } |
472 | ||
1b3e6820 | 473 | while (!(xyz.timeout && get_timer(xyz.initial_time) > xyz.timeout)) |
7d0432c9 | 474 | { |
1b3e6820 T |
475 | if (--retries <= 0) |
476 | { | |
477 | retries = xyzModem_MAX_RETRIES; | |
478 | crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; | |
479 | xyz.crc_mode = true; | |
480 | } | |
7d0432c9 WD |
481 | stat = xyzModem_get_hdr (); |
482 | if (stat == 0) | |
483 | { | |
484 | /* Y-modem file information header */ | |
485 | if (xyz.blk == 0) | |
486 | { | |
7d0432c9 WD |
487 | /* skip filename */ |
488 | while (*xyz.bufp++); | |
489 | /* get the length */ | |
490 | parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " "); | |
7d0432c9 WD |
491 | /* The rest of the file name data block quietly discarded */ |
492 | xyz.tx_ack = true; | |
493 | } | |
494 | xyz.next_blk = 1; | |
495 | xyz.len = 0; | |
496 | return 0; | |
497 | } | |
498 | else if (stat == xyzModem_timeout) | |
499 | { | |
500 | if (--crc_retries <= 0) | |
501 | xyz.crc_mode = false; | |
502 | CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */ | |
503 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); | |
504 | xyz.total_retries++; | |
505 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); | |
506 | } | |
507 | if (stat == xyzModem_cancel) | |
508 | { | |
509 | break; | |
510 | } | |
f2841d37 | 511 | } |
7d0432c9 WD |
512 | *err = stat; |
513 | ZM_DEBUG (zm_flush ()); | |
514 | return -1; | |
f2841d37 MK |
515 | } |
516 | ||
cf48eb9a | 517 | int |
7d0432c9 | 518 | xyzModem_stream_read (char *buf, int size, int *err) |
f2841d37 | 519 | { |
7d0432c9 WD |
520 | int stat, total, len; |
521 | int retries; | |
522 | ||
523 | total = 0; | |
524 | stat = xyzModem_cancel; | |
525 | /* Try and get 'size' bytes into the buffer */ | |
1e747846 | 526 | while (!xyz.at_eof && xyz.len >= 0 && (size > 0)) |
7d0432c9 WD |
527 | { |
528 | if (xyz.len == 0) | |
529 | { | |
530 | retries = xyzModem_MAX_RETRIES; | |
531 | while (retries-- > 0) | |
532 | { | |
1b3e6820 T |
533 | if (xyz.first_xmodem_packet && xyz.timeout && |
534 | get_timer(xyz.initial_time) > xyz.timeout) | |
535 | { | |
536 | *err = xyzModem_timeout; | |
537 | xyz.len = -1; | |
538 | return total; | |
539 | } | |
540 | ||
7d0432c9 WD |
541 | stat = xyzModem_get_hdr (); |
542 | if (stat == 0) | |
543 | { | |
1b3e6820 T |
544 | if (xyz.mode == xyzModem_xmodem && xyz.first_xmodem_packet) |
545 | xyz.first_xmodem_packet = false; | |
7d0432c9 WD |
546 | if (xyz.blk == xyz.next_blk) |
547 | { | |
548 | xyz.tx_ack = true; | |
549 | ZM_DEBUG (zm_dprintf | |
550 | ("ACK block %d (%d)\n", xyz.blk, __LINE__)); | |
551 | xyz.next_blk = (xyz.next_blk + 1) & 0xFF; | |
f2841d37 | 552 | |
7d0432c9 WD |
553 | if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) |
554 | { | |
7d0432c9 WD |
555 | /* Data blocks can be padded with ^Z (EOF) characters */ |
556 | /* This code tries to detect and remove them */ | |
557 | if ((xyz.bufp[xyz.len - 1] == EOF) && | |
558 | (xyz.bufp[xyz.len - 2] == EOF) && | |
559 | (xyz.bufp[xyz.len - 3] == EOF)) | |
560 | { | |
561 | while (xyz.len | |
562 | && (xyz.bufp[xyz.len - 1] == EOF)) | |
563 | { | |
564 | xyz.len--; | |
565 | } | |
566 | } | |
567 | } | |
f2841d37 | 568 | |
7d0432c9 WD |
569 | /* |
570 | * See if accumulated length exceeds that of the file. | |
571 | * If so, reduce size (i.e., cut out pad bytes) | |
572 | * Only do this for Y-modem (and Z-modem should it ever | |
573 | * be supported since it can fall back to Y-modem mode). | |
574 | */ | |
575 | if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length) | |
576 | { | |
577 | xyz.read_length += xyz.len; | |
578 | if (xyz.read_length > xyz.file_length) | |
579 | { | |
580 | xyz.len -= (xyz.read_length - xyz.file_length); | |
581 | } | |
582 | } | |
7d0432c9 WD |
583 | break; |
584 | } | |
585 | else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF)) | |
586 | { | |
587 | /* Just re-ACK this so sender will get on with it */ | |
588 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
589 | continue; /* Need new header */ | |
590 | } | |
591 | else | |
592 | { | |
593 | stat = xyzModem_sequence; | |
594 | } | |
595 | } | |
596 | if (stat == xyzModem_cancel) | |
597 | { | |
598 | break; | |
599 | } | |
600 | if (stat == xyzModem_eof) | |
601 | { | |
602 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
603 | ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__)); | |
604 | if (xyz.mode == xyzModem_ymodem) | |
605 | { | |
606 | CYGACC_COMM_IF_PUTC (*xyz.__chan, | |
607 | (xyz.crc_mode ? 'C' : NAK)); | |
608 | xyz.total_retries++; | |
609 | ZM_DEBUG (zm_dprintf ("Reading Final Header\n")); | |
610 | stat = xyzModem_get_hdr (); | |
611 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); | |
612 | ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__)); | |
613 | } | |
15c27a5a T |
614 | else |
615 | stat = 0; | |
7d0432c9 WD |
616 | xyz.at_eof = true; |
617 | break; | |
618 | } | |
619 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); | |
620 | xyz.total_retries++; | |
621 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); | |
622 | } | |
1b3e6820 | 623 | if (stat < 0 && (!xyz.first_xmodem_packet || stat != xyzModem_timeout)) |
7d0432c9 WD |
624 | { |
625 | *err = stat; | |
626 | xyz.len = -1; | |
627 | return total; | |
628 | } | |
629 | } | |
630 | /* Don't "read" data from the EOF protocol package */ | |
1e747846 | 631 | if (!xyz.at_eof && xyz.len > 0) |
7d0432c9 WD |
632 | { |
633 | len = xyz.len; | |
634 | if (size < len) | |
635 | len = size; | |
636 | memcpy (buf, xyz.bufp, len); | |
637 | size -= len; | |
638 | buf += len; | |
639 | total += len; | |
640 | xyz.len -= len; | |
641 | xyz.bufp += len; | |
642 | } | |
f2841d37 | 643 | } |
7d0432c9 | 644 | return total; |
f2841d37 MK |
645 | } |
646 | ||
647 | void | |
7d0432c9 | 648 | xyzModem_stream_close (int *err) |
f2841d37 | 649 | { |
f05d69bd | 650 | ZM_DEBUG (zm_dprintf |
7d0432c9 WD |
651 | ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n", |
652 | xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX, | |
f05d69bd | 653 | xyz.total_CAN, xyz.total_retries)); |
7d0432c9 | 654 | ZM_DEBUG (zm_flush ()); |
f2841d37 MK |
655 | } |
656 | ||
cf48eb9a WD |
657 | /* Need to be able to clean out the input buffer, so have to take the */ |
658 | /* getc */ | |
7d0432c9 WD |
659 | void |
660 | xyzModem_stream_terminate (bool abort, int (*getc) (void)) | |
f2841d37 MK |
661 | { |
662 | int c; | |
663 | ||
7d0432c9 WD |
664 | if (abort) |
665 | { | |
666 | ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n")); | |
667 | switch (xyz.mode) | |
668 | { | |
f2841d37 MK |
669 | case xyzModem_xmodem: |
670 | case xyzModem_ymodem: | |
cf48eb9a WD |
671 | /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */ |
672 | /* number of Backspaces is a friendly way to get the other end to abort. */ | |
7d0432c9 WD |
673 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); |
674 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); | |
675 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); | |
676 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); | |
677 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); | |
678 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); | |
679 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); | |
680 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); | |
cf48eb9a | 681 | /* Now consume the rest of what's waiting on the line. */ |
7d0432c9 WD |
682 | ZM_DEBUG (zm_dprintf ("Flushing serial line.\n")); |
683 | xyzModem_flush (); | |
684 | xyz.at_eof = true; | |
685 | break; | |
f2841d37 MK |
686 | #ifdef xyzModem_zmodem |
687 | case xyzModem_zmodem: | |
cf48eb9a | 688 | /* Might support it some day I suppose. */ |
f2841d37 | 689 | #endif |
7d0432c9 WD |
690 | break; |
691 | } | |
692 | } | |
693 | else | |
694 | { | |
695 | ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n")); | |
cf48eb9a WD |
696 | /* |
697 | * Consume any trailing crap left in the inbuffer from | |
16263087 | 698 | * previous received blocks. Since very few files are an exact multiple |
cf48eb9a WD |
699 | * of the transfer block size, there will almost always be some gunk here. |
700 | * If we don't eat it now, RedBoot will think the user typed it. | |
701 | */ | |
7d0432c9 | 702 | ZM_DEBUG (zm_dprintf ("Trailing gunk:\n")); |
e153b13c JH |
703 | while ((c = (*getc) ()) > -1) |
704 | ; | |
7d0432c9 | 705 | ZM_DEBUG (zm_dprintf ("\n")); |
cf48eb9a WD |
706 | /* |
707 | * Make a small delay to give terminal programs like minicom | |
708 | * time to get control again after their file transfer program | |
709 | * exits. | |
710 | */ | |
7d0432c9 WD |
711 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
712 | } | |
f2841d37 MK |
713 | } |
714 | ||
715 | char * | |
7d0432c9 | 716 | xyzModem_error (int err) |
f2841d37 | 717 | { |
7d0432c9 WD |
718 | switch (err) |
719 | { | |
f2841d37 | 720 | case xyzModem_access: |
7d0432c9 WD |
721 | return "Can't access file"; |
722 | break; | |
f2841d37 | 723 | case xyzModem_noZmodem: |
7d0432c9 WD |
724 | return "Sorry, zModem not available yet"; |
725 | break; | |
f2841d37 | 726 | case xyzModem_timeout: |
7d0432c9 WD |
727 | return "Timed out"; |
728 | break; | |
f2841d37 | 729 | case xyzModem_eof: |
7d0432c9 WD |
730 | return "End of file"; |
731 | break; | |
f2841d37 | 732 | case xyzModem_cancel: |
7d0432c9 WD |
733 | return "Cancelled"; |
734 | break; | |
f2841d37 | 735 | case xyzModem_frame: |
7d0432c9 WD |
736 | return "Invalid framing"; |
737 | break; | |
f2841d37 | 738 | case xyzModem_cksum: |
7d0432c9 WD |
739 | return "CRC/checksum error"; |
740 | break; | |
f2841d37 | 741 | case xyzModem_sequence: |
7d0432c9 WD |
742 | return "Block sequence error"; |
743 | break; | |
f2841d37 | 744 | default: |
7d0432c9 WD |
745 | return "Unknown error"; |
746 | break; | |
f2841d37 MK |
747 | } |
748 | } | |
749 | ||
cf48eb9a WD |
750 | /* |
751 | * RedBoot interface | |
752 | */ |