blob: e85da74a698f373c837987228f2a99b4a318a136 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: eCos-2.0
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02002/*
3 *==========================================================================
4 *
5 * xyzModem.c
6 *
7 * RedBoot stream handler for xyzModem protocol
8 *
9 *==========================================================================
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020010 *#####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 Klotzbuecher387f5412006-03-30 13:40:55 +020024#include <common.h>
25#include <xyzModem.h>
26#include <stdarg.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010027#include <u-boot/crc.h>
Lokesh Vutlaf2091382019-01-08 19:28:35 +053028#include <watchdog.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020029
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020030/* Assumption - run xyzModem protocol over the console port */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020031
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020032/* Values magic to the protocol */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020033#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
Wolfgang Denk95d11692006-08-31 16:46:53 +020040#define EOF 0x1A /* ^Z for DOS officionados */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020041
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020042/* Data & state local to the protocol */
Wolfgang Denk95d11692006-08-31 16:46:53 +020043static struct
44{
Wolfgang Denk95d11692006-08-31 16:46:53 +020045 int *__chan;
Wolfgang Denk95d11692006-08-31 16:46:53 +020046 unsigned char pkt[1024], *bufp;
47 unsigned char blk, cblk, crc1, crc2;
48 unsigned char next_blk; /* Expected block */
49 int len, mode, total_retries;
50 int total_SOH, total_STX, total_CAN;
51 bool crc_mode, at_eof, tx_ack;
Wolfgang Denk95d11692006-08-31 16:46:53 +020052 unsigned long file_length, read_length;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020053} xyz;
54
Wolfgang Denk95d11692006-08-31 16:46:53 +020055#define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020056#define xyzModem_MAX_RETRIES 20
57#define xyzModem_MAX_RETRIES_WITH_CRC 10
Wolfgang Denk95d11692006-08-31 16:46:53 +020058#define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020059
60
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020061typedef int cyg_int32;
Kim Phillipsb052b602012-10-29 13:34:32 +000062static int
Wolfgang Denk95d11692006-08-31 16:46:53 +020063CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
64{
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020065
66 ulong now = get_timer(0);
Lokesh Vutlaf2091382019-01-08 19:28:35 +053067 WATCHDOG_RESET();
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020068 while (!tstc ())
Wolfgang Denk95d11692006-08-31 16:46:53 +020069 {
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020070 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
71 break;
Wolfgang Denk95d11692006-08-31 16:46:53 +020072 }
73 if (tstc ())
74 {
75 *c = getc ();
76 return 1;
77 }
78 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020079}
80
Kim Phillipsb052b602012-10-29 13:34:32 +000081static void
Wolfgang Denk95d11692006-08-31 16:46:53 +020082CYGACC_COMM_IF_PUTC (char x, char y)
83{
84 putc (y);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020085}
86
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020087/* Validate a hex character */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020088__inline__ static bool
Wolfgang Denk95d11692006-08-31 16:46:53 +020089_is_hex (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020090{
Wolfgang Denk95d11692006-08-31 16:46:53 +020091 return (((c >= '0') && (c <= '9')) ||
92 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020093}
94
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020095/* Convert a single hex nibble */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020096__inline__ static int
Wolfgang Denk95d11692006-08-31 16:46:53 +020097_from_hex (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020098{
Wolfgang Denk95d11692006-08-31 16:46:53 +020099 int ret = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200100
Wolfgang Denk95d11692006-08-31 16:46:53 +0200101 if ((c >= '0') && (c <= '9'))
102 {
103 ret = (c - '0');
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200104 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200105 else if ((c >= 'a') && (c <= 'f'))
106 {
107 ret = (c - 'a' + 0x0a);
108 }
109 else if ((c >= 'A') && (c <= 'F'))
110 {
111 ret = (c - 'A' + 0x0A);
112 }
113 return ret;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200114}
115
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200116/* Convert a character to lower case */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200117__inline__ static char
Wolfgang Denk95d11692006-08-31 16:46:53 +0200118_tolower (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200119{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200120 if ((c >= 'A') && (c <= 'Z'))
121 {
122 c = (c - 'A') + 'a';
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200123 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200124 return c;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200125}
126
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200127/* Parse (scan) a number */
Kim Phillipsb052b602012-10-29 13:34:32 +0000128static bool
Wolfgang Denk95d11692006-08-31 16:46:53 +0200129parse_num (char *s, unsigned long *val, char **es, char *delim)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200130{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200131 bool first = true;
132 int radix = 10;
133 char c;
134 unsigned long result = 0;
135 int digit;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200136
Wolfgang Denk95d11692006-08-31 16:46:53 +0200137 while (*s == ' ')
138 s++;
139 while (*s)
140 {
141 if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
142 {
143 radix = 16;
144 s += 2;
145 }
146 first = false;
147 c = *s++;
148 if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
149 {
150 /* Valid digit */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200151 result = (result * radix) + digit;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200152 }
153 else
154 {
155 if (delim != (char *) 0)
156 {
157 /* See if this character is one of the delimiters */
158 char *dp = delim;
159 while (*dp && (c != *dp))
160 dp++;
161 if (*dp)
162 break; /* Found a good delimiter */
163 }
164 return false; /* Malformatted number */
165 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200166 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200167 *val = result;
168 if (es != (char **) 0)
169 {
170 *es = s;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200171 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200172 return true;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200173}
174
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200175
Alex Kiernandbb275d2018-05-03 11:45:08 +0000176#if defined(DEBUG) && !defined(CONFIG_USE_TINY_PRINTF)
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200177/*
178 * Note: this debug setup works by storing the strings in a fixed buffer
179 */
Alexandru Gagniuc39671012017-04-04 10:42:31 -0700180static char zm_debug_buf[8192];
181static char *zm_out = zm_debug_buf;
182static char *zm_out_start = zm_debug_buf;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200183
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200184static int
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200185zm_dprintf(char *fmt, ...)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200186{
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200187 int len;
188 va_list args;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200189
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200190 va_start(args, fmt);
191 len = diag_vsprintf(zm_out, fmt, args);
192 va_end(args);
193 zm_out += len;
194 return len;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200195}
196
197static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200198zm_flush (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200199{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200200 zm_out = zm_out_start;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200201}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200202
203static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200204zm_dump_buf (void *buf, int len)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200205{
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200206
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200207}
208
209static unsigned char zm_buf[2048];
210static unsigned char *zm_bp;
211
212static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200213zm_new (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200214{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200215 zm_bp = zm_buf;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200216}
217
218static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200219zm_save (unsigned char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200220{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200221 *zm_bp++ = c;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200222}
223
224static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200225zm_dump (int line)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200226{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200227 zm_dprintf ("Packet at line: %d\n", line);
228 zm_dump_buf (zm_buf, zm_bp - zm_buf);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200229}
230
231#define ZM_DEBUG(x) x
232#else
233#define ZM_DEBUG(x)
234#endif
235
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200236/* Wait for the line to go idle */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200237static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200238xyzModem_flush (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200239{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200240 int res;
241 char c;
242 while (true)
243 {
244 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
245 if (!res)
246 return;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200247 }
248}
249
250static int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200251xyzModem_get_hdr (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200252{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200253 char c;
254 int res;
255 bool hdr_found = false;
256 int i, can_total, hdr_chars;
257 unsigned short cksum;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200258
Wolfgang Denk95d11692006-08-31 16:46:53 +0200259 ZM_DEBUG (zm_new ());
260 /* Find the start of a header */
261 can_total = 0;
262 hdr_chars = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200263
Wolfgang Denk95d11692006-08-31 16:46:53 +0200264 if (xyz.tx_ack)
265 {
266 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
267 xyz.tx_ack = false;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200268 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200269 while (!hdr_found)
270 {
271 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
272 ZM_DEBUG (zm_save (c));
273 if (res)
274 {
275 hdr_chars++;
276 switch (c)
277 {
278 case SOH:
279 xyz.total_SOH++;
280 case STX:
281 if (c == STX)
282 xyz.total_STX++;
283 hdr_found = true;
284 break;
285 case CAN:
286 xyz.total_CAN++;
287 ZM_DEBUG (zm_dump (__LINE__));
288 if (++can_total == xyzModem_CAN_COUNT)
289 {
290 return xyzModem_cancel;
291 }
292 else
293 {
294 /* Wait for multiple CAN to avoid early quits */
295 break;
296 }
297 case EOT:
298 /* EOT only supported if no noise */
299 if (hdr_chars == 1)
300 {
301 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
302 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
303 ZM_DEBUG (zm_dump (__LINE__));
304 return xyzModem_eof;
305 }
306 default:
307 /* Ignore, waiting for start of header */
308 ;
309 }
310 }
311 else
312 {
313 /* Data stream timed out */
314 xyzModem_flush (); /* Toss any current input */
315 ZM_DEBUG (zm_dump (__LINE__));
316 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
317 return xyzModem_timeout;
318 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200319 }
320
Wolfgang Denk95d11692006-08-31 16:46:53 +0200321 /* Header found, now read the data */
322 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
323 ZM_DEBUG (zm_save (xyz.blk));
324 if (!res)
325 {
326 ZM_DEBUG (zm_dump (__LINE__));
327 return xyzModem_timeout;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200328 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200329 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
330 ZM_DEBUG (zm_save (xyz.cblk));
331 if (!res)
332 {
333 ZM_DEBUG (zm_dump (__LINE__));
334 return xyzModem_timeout;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200335 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200336 xyz.len = (c == SOH) ? 128 : 1024;
337 xyz.bufp = xyz.pkt;
338 for (i = 0; i < xyz.len; i++)
339 {
340 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
341 ZM_DEBUG (zm_save (c));
342 if (res)
343 {
344 xyz.pkt[i] = c;
345 }
346 else
347 {
348 ZM_DEBUG (zm_dump (__LINE__));
349 return xyzModem_timeout;
350 }
351 }
352 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
353 ZM_DEBUG (zm_save (xyz.crc1));
354 if (!res)
355 {
356 ZM_DEBUG (zm_dump (__LINE__));
357 return xyzModem_timeout;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200358 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200359 if (xyz.crc_mode)
360 {
361 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
362 ZM_DEBUG (zm_save (xyz.crc2));
363 if (!res)
364 {
365 ZM_DEBUG (zm_dump (__LINE__));
366 return xyzModem_timeout;
367 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200368 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200369 ZM_DEBUG (zm_dump (__LINE__));
370 /* Validate the message */
371 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
372 {
373 ZM_DEBUG (zm_dprintf
374 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
375 (xyz.blk ^ xyz.cblk)));
376 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
377 xyzModem_flush ();
378 return xyzModem_frame;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200379 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200380 /* Verify checksum/CRC */
381 if (xyz.crc_mode)
382 {
Stefan Roese084ff1e2016-03-03 09:34:12 +0100383 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
Wolfgang Denk95d11692006-08-31 16:46:53 +0200384 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
385 {
386 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
387 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
388 return xyzModem_cksum;
389 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200390 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200391 else
392 {
393 cksum = 0;
394 for (i = 0; i < xyz.len; i++)
395 {
396 cksum += xyz.pkt[i];
397 }
398 if (xyz.crc1 != (cksum & 0xFF))
399 {
400 ZM_DEBUG (zm_dprintf
401 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
402 cksum & 0xFF));
403 return xyzModem_cksum;
404 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200405 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200406 /* If we get here, the message passes [structural] muster */
407 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200408}
409
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200410int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200411xyzModem_stream_open (connection_info_t * info, int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200412{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200413 int stat = 0;
414 int retries = xyzModem_MAX_RETRIES;
415 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200416
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200417/* ZM_DEBUG(zm_out = zm_out_start); */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200418#ifdef xyzModem_zmodem
Wolfgang Denk95d11692006-08-31 16:46:53 +0200419 if (info->mode == xyzModem_zmodem)
420 {
421 *err = xyzModem_noZmodem;
422 return -1;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200423 }
424#endif
425
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200426/* TODO: CHECK ! */
Kim Phillips66b07262009-06-15 11:50:40 -0500427 int dummy = 0;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200428 xyz.__chan = &dummy;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200429 xyz.len = 0;
430 xyz.crc_mode = true;
431 xyz.at_eof = false;
432 xyz.tx_ack = false;
433 xyz.mode = info->mode;
434 xyz.total_retries = 0;
435 xyz.total_SOH = 0;
436 xyz.total_STX = 0;
437 xyz.total_CAN = 0;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200438 xyz.read_length = 0;
439 xyz.file_length = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200440
Wolfgang Denk95d11692006-08-31 16:46:53 +0200441 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200442
Wolfgang Denk95d11692006-08-31 16:46:53 +0200443 if (xyz.mode == xyzModem_xmodem)
444 {
445 /* X-modem doesn't have an information header - exit here */
446 xyz.next_blk = 1;
447 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200448 }
449
Wolfgang Denk95d11692006-08-31 16:46:53 +0200450 while (retries-- > 0)
451 {
452 stat = xyzModem_get_hdr ();
453 if (stat == 0)
454 {
455 /* Y-modem file information header */
456 if (xyz.blk == 0)
457 {
Wolfgang Denk95d11692006-08-31 16:46:53 +0200458 /* skip filename */
459 while (*xyz.bufp++);
460 /* get the length */
461 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
Wolfgang Denk95d11692006-08-31 16:46:53 +0200462 /* The rest of the file name data block quietly discarded */
463 xyz.tx_ack = true;
464 }
465 xyz.next_blk = 1;
466 xyz.len = 0;
467 return 0;
468 }
469 else if (stat == xyzModem_timeout)
470 {
471 if (--crc_retries <= 0)
472 xyz.crc_mode = false;
473 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
474 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
475 xyz.total_retries++;
476 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
477 }
478 if (stat == xyzModem_cancel)
479 {
480 break;
481 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200482 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200483 *err = stat;
484 ZM_DEBUG (zm_flush ());
485 return -1;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200486}
487
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200488int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200489xyzModem_stream_read (char *buf, int size, int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200490{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200491 int stat, total, len;
492 int retries;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200493
Wolfgang Denk95d11692006-08-31 16:46:53 +0200494 total = 0;
495 stat = xyzModem_cancel;
496 /* Try and get 'size' bytes into the buffer */
497 while (!xyz.at_eof && (size > 0))
498 {
499 if (xyz.len == 0)
500 {
501 retries = xyzModem_MAX_RETRIES;
502 while (retries-- > 0)
503 {
504 stat = xyzModem_get_hdr ();
505 if (stat == 0)
506 {
507 if (xyz.blk == xyz.next_blk)
508 {
509 xyz.tx_ack = true;
510 ZM_DEBUG (zm_dprintf
511 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
512 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200513
Wolfgang Denk95d11692006-08-31 16:46:53 +0200514 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
515 {
Wolfgang Denk95d11692006-08-31 16:46:53 +0200516 /* Data blocks can be padded with ^Z (EOF) characters */
517 /* This code tries to detect and remove them */
518 if ((xyz.bufp[xyz.len - 1] == EOF) &&
519 (xyz.bufp[xyz.len - 2] == EOF) &&
520 (xyz.bufp[xyz.len - 3] == EOF))
521 {
522 while (xyz.len
523 && (xyz.bufp[xyz.len - 1] == EOF))
524 {
525 xyz.len--;
526 }
527 }
528 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200529
Wolfgang Denk95d11692006-08-31 16:46:53 +0200530 /*
531 * See if accumulated length exceeds that of the file.
532 * If so, reduce size (i.e., cut out pad bytes)
533 * Only do this for Y-modem (and Z-modem should it ever
534 * be supported since it can fall back to Y-modem mode).
535 */
536 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
537 {
538 xyz.read_length += xyz.len;
539 if (xyz.read_length > xyz.file_length)
540 {
541 xyz.len -= (xyz.read_length - xyz.file_length);
542 }
543 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200544 break;
545 }
546 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
547 {
548 /* Just re-ACK this so sender will get on with it */
549 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
550 continue; /* Need new header */
551 }
552 else
553 {
554 stat = xyzModem_sequence;
555 }
556 }
557 if (stat == xyzModem_cancel)
558 {
559 break;
560 }
561 if (stat == xyzModem_eof)
562 {
563 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
564 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
565 if (xyz.mode == xyzModem_ymodem)
566 {
567 CYGACC_COMM_IF_PUTC (*xyz.__chan,
568 (xyz.crc_mode ? 'C' : NAK));
569 xyz.total_retries++;
570 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
571 stat = xyzModem_get_hdr ();
572 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
573 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
574 }
575 xyz.at_eof = true;
576 break;
577 }
578 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
579 xyz.total_retries++;
580 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
581 }
582 if (stat < 0)
583 {
584 *err = stat;
585 xyz.len = -1;
586 return total;
587 }
588 }
589 /* Don't "read" data from the EOF protocol package */
590 if (!xyz.at_eof)
591 {
592 len = xyz.len;
593 if (size < len)
594 len = size;
595 memcpy (buf, xyz.bufp, len);
596 size -= len;
597 buf += len;
598 total += len;
599 xyz.len -= len;
600 xyz.bufp += len;
601 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200602 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200603 return total;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200604}
605
606void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200607xyzModem_stream_close (int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200608{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200609 diag_printf
610 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
611 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
612 xyz.total_CAN, xyz.total_retries);
613 ZM_DEBUG (zm_flush ());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200614}
615
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200616/* Need to be able to clean out the input buffer, so have to take the */
617/* getc */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200618void
619xyzModem_stream_terminate (bool abort, int (*getc) (void))
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200620{
621 int c;
622
Wolfgang Denk95d11692006-08-31 16:46:53 +0200623 if (abort)
624 {
625 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
626 switch (xyz.mode)
627 {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200628 case xyzModem_xmodem:
629 case xyzModem_ymodem:
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200630 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
631 /* number of Backspaces is a friendly way to get the other end to abort. */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200632 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
633 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
634 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
635 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
636 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
637 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
638 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
639 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200640 /* Now consume the rest of what's waiting on the line. */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200641 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
642 xyzModem_flush ();
643 xyz.at_eof = true;
644 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200645#ifdef xyzModem_zmodem
646 case xyzModem_zmodem:
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200647 /* Might support it some day I suppose. */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200648#endif
Wolfgang Denk95d11692006-08-31 16:46:53 +0200649 break;
650 }
651 }
652 else
653 {
654 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200655 /*
656 * Consume any trailing crap left in the inbuffer from
Mike Williamsbf895ad2011-07-22 04:01:30 +0000657 * previous received blocks. Since very few files are an exact multiple
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200658 * of the transfer block size, there will almost always be some gunk here.
659 * If we don't eat it now, RedBoot will think the user typed it.
660 */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200661 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
Jeroen Hofstee419b6a42014-06-11 01:04:42 +0200662 while ((c = (*getc) ()) > -1)
663 ;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200664 ZM_DEBUG (zm_dprintf ("\n"));
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200665 /*
666 * Make a small delay to give terminal programs like minicom
667 * time to get control again after their file transfer program
668 * exits.
669 */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200670 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
671 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200672}
673
674char *
Wolfgang Denk95d11692006-08-31 16:46:53 +0200675xyzModem_error (int err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200676{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200677 switch (err)
678 {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200679 case xyzModem_access:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200680 return "Can't access file";
681 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200682 case xyzModem_noZmodem:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200683 return "Sorry, zModem not available yet";
684 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200685 case xyzModem_timeout:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200686 return "Timed out";
687 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200688 case xyzModem_eof:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200689 return "End of file";
690 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200691 case xyzModem_cancel:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200692 return "Cancelled";
693 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200694 case xyzModem_frame:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200695 return "Invalid framing";
696 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200697 case xyzModem_cksum:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200698 return "CRC/checksum error";
699 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200700 case xyzModem_sequence:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200701 return "Block sequence error";
702 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200703 default:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200704 return "Unknown error";
705 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200706 }
707}
708
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200709/*
710 * RedBoot interface
711 */