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