blob: ece25acb183b57cfd3ff70c10fcb62c6947cb258 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: eCos-2.0
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02002/*
3 *==========================================================================
4 *
5 * xyzModem.c
6 *
7 * RedBoot stream handler for xyzModem protocol
8 *
9 *==========================================================================
Wolfgang Denkcf48eb92006-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 Klotzbuecherf2841d32006-03-30 13:40:55 +020024#include <common.h>
25#include <xyzModem.h>
26#include <stdarg.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010027#include <u-boot/crc.h>
Lokesh Vutlaa4773c52019-01-08 19:28:35 +053028#include <watchdog.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020029
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020030/* Assumption - run xyzModem protocol over the console port */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020031
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020032/* Values magic to the protocol */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020033#define SOH 0x01
34#define STX 0x02
Pali Rohárdffeb402021-08-03 16:28:44 +020035#define ETX 0x03 /* ^C for interrupt */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020036#define EOT 0x04
37#define ACK 0x06
38#define BSP 0x08
39#define NAK 0x15
40#define CAN 0x18
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020041#define EOF 0x1A /* ^Z for DOS officionados */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020042
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020043/* Data & state local to the protocol */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020044static struct
45{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020046 int *__chan;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020047 unsigned char pkt[1024], *bufp;
48 unsigned char blk, cblk, crc1, crc2;
49 unsigned char next_blk; /* Expected block */
50 int len, mode, total_retries;
51 int total_SOH, total_STX, total_CAN;
52 bool crc_mode, at_eof, tx_ack;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020053 unsigned long file_length, read_length;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020054} xyz;
55
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020056#define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020057#define xyzModem_MAX_RETRIES 20
58#define xyzModem_MAX_RETRIES_WITH_CRC 10
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020059#define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020060
61
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020062typedef int cyg_int32;
Kim Phillips199adb62012-10-29 13:34:32 +000063static int
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020064CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
65{
tomas.melin@vaisala.com2c77c0d2016-11-21 10:18:51 +020066
67 ulong now = get_timer(0);
Lokesh Vutlaa4773c52019-01-08 19:28:35 +053068 WATCHDOG_RESET();
tomas.melin@vaisala.com2c77c0d2016-11-21 10:18:51 +020069 while (!tstc ())
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020070 {
tomas.melin@vaisala.com2c77c0d2016-11-21 10:18:51 +020071 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
72 break;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020073 }
74 if (tstc ())
75 {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020076 *c = getchar();
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020077 return 1;
78 }
79 return 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020080}
81
Kim Phillips199adb62012-10-29 13:34:32 +000082static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020083CYGACC_COMM_IF_PUTC (char x, char y)
84{
85 putc (y);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020086}
87
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020088/* Validate a hex character */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020089__inline__ static bool
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020090_is_hex (char c)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020091{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020092 return (((c >= '0') && (c <= '9')) ||
93 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020094}
95
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020096/* Convert a single hex nibble */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020097__inline__ static int
Wolfgang Denk7d0432c2006-08-31 16:46:53 +020098_from_hex (char c)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020099{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200100 int ret = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200101
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200102 if ((c >= '0') && (c <= '9'))
103 {
104 ret = (c - '0');
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200105 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200106 else if ((c >= 'a') && (c <= 'f'))
107 {
108 ret = (c - 'a' + 0x0a);
109 }
110 else if ((c >= 'A') && (c <= 'F'))
111 {
112 ret = (c - 'A' + 0x0A);
113 }
114 return ret;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200115}
116
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200117/* Convert a character to lower case */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200118__inline__ static char
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200119_tolower (char c)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200120{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200121 if ((c >= 'A') && (c <= 'Z'))
122 {
123 c = (c - 'A') + 'a';
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200124 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200125 return c;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200126}
127
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200128/* Parse (scan) a number */
Kim Phillips199adb62012-10-29 13:34:32 +0000129static bool
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200130parse_num (char *s, unsigned long *val, char **es, char *delim)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200131{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200132 bool first = true;
133 int radix = 10;
134 char c;
135 unsigned long result = 0;
136 int digit;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200137
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200138 while (*s == ' ')
139 s++;
140 while (*s)
141 {
142 if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
143 {
144 radix = 16;
145 s += 2;
146 }
147 first = false;
148 c = *s++;
149 if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
150 {
151 /* Valid digit */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200152 result = (result * radix) + digit;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200153 }
154 else
155 {
156 if (delim != (char *) 0)
157 {
158 /* See if this character is one of the delimiters */
159 char *dp = delim;
160 while (*dp && (c != *dp))
161 dp++;
162 if (*dp)
163 break; /* Found a good delimiter */
164 }
165 return false; /* Malformatted number */
166 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200167 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200168 *val = result;
169 if (es != (char **) 0)
170 {
171 *es = s;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200172 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200173 return true;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200174}
175
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200176
Simon Glass27084c02019-09-25 08:56:27 -0600177#if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200178/*
179 * Note: this debug setup works by storing the strings in a fixed buffer
180 */
Alexandru Gagniucb09ece02017-04-04 10:42:31 -0700181static char zm_debug_buf[8192];
182static char *zm_out = zm_debug_buf;
183static char *zm_out_start = zm_debug_buf;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200184
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200185static int
Heinrich Schuchardt23c64892018-05-07 21:59:34 +0200186zm_dprintf(char *fmt, ...)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200187{
Heinrich Schuchardt23c64892018-05-07 21:59:34 +0200188 int len;
189 va_list args;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200190
Heinrich Schuchardt23c64892018-05-07 21:59:34 +0200191 va_start(args, fmt);
192 len = diag_vsprintf(zm_out, fmt, args);
193 va_end(args);
194 zm_out += len;
195 return len;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200196}
197
198static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200199zm_flush (void)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200200{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200201 zm_out = zm_out_start;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200202}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200203
204static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200205zm_dump_buf (void *buf, int len)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200206{
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200207
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200208}
209
210static unsigned char zm_buf[2048];
211static unsigned char *zm_bp;
212
213static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200214zm_new (void)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200215{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200216 zm_bp = zm_buf;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200217}
218
219static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200220zm_save (unsigned char c)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200221{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200222 *zm_bp++ = c;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200223}
224
225static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200226zm_dump (int line)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200227{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200228 zm_dprintf ("Packet at line: %d\n", line);
229 zm_dump_buf (zm_buf, zm_bp - zm_buf);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200230}
231
232#define ZM_DEBUG(x) x
233#else
234#define ZM_DEBUG(x)
235#endif
236
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200237/* Wait for the line to go idle */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200238static void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200239xyzModem_flush (void)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200240{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200241 int res;
242 char c;
243 while (true)
244 {
245 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
246 if (!res)
247 return;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200248 }
249}
250
251static int
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200252xyzModem_get_hdr (void)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200253{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200254 char c;
255 int res;
256 bool hdr_found = false;
257 int i, can_total, hdr_chars;
258 unsigned short cksum;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200259
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200260 ZM_DEBUG (zm_new ());
261 /* Find the start of a header */
262 can_total = 0;
263 hdr_chars = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200264
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200265 if (xyz.tx_ack)
266 {
267 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
268 xyz.tx_ack = false;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200269 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200270 while (!hdr_found)
271 {
272 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
273 ZM_DEBUG (zm_save (c));
274 if (res)
275 {
276 hdr_chars++;
277 switch (c)
278 {
279 case SOH:
280 xyz.total_SOH++;
281 case STX:
282 if (c == STX)
283 xyz.total_STX++;
284 hdr_found = true;
285 break;
286 case CAN:
Pali Rohárdffeb402021-08-03 16:28:44 +0200287 case ETX:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200288 xyz.total_CAN++;
289 ZM_DEBUG (zm_dump (__LINE__));
290 if (++can_total == xyzModem_CAN_COUNT)
291 {
292 return xyzModem_cancel;
293 }
294 else
295 {
296 /* Wait for multiple CAN to avoid early quits */
297 break;
298 }
299 case EOT:
300 /* EOT only supported if no noise */
301 if (hdr_chars == 1)
302 {
303 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
304 ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
305 ZM_DEBUG (zm_dump (__LINE__));
306 return xyzModem_eof;
307 }
308 default:
309 /* Ignore, waiting for start of header */
310 ;
311 }
312 }
313 else
314 {
315 /* Data stream timed out */
316 xyzModem_flush (); /* Toss any current input */
317 ZM_DEBUG (zm_dump (__LINE__));
318 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
319 return xyzModem_timeout;
320 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200321 }
322
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200323 /* Header found, now read the data */
324 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
325 ZM_DEBUG (zm_save (xyz.blk));
326 if (!res)
327 {
328 ZM_DEBUG (zm_dump (__LINE__));
329 return xyzModem_timeout;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200330 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200331 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
332 ZM_DEBUG (zm_save (xyz.cblk));
333 if (!res)
334 {
335 ZM_DEBUG (zm_dump (__LINE__));
336 return xyzModem_timeout;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200337 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200338 xyz.len = (c == SOH) ? 128 : 1024;
339 xyz.bufp = xyz.pkt;
340 for (i = 0; i < xyz.len; i++)
341 {
342 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
343 ZM_DEBUG (zm_save (c));
344 if (res)
345 {
346 xyz.pkt[i] = c;
347 }
348 else
349 {
350 ZM_DEBUG (zm_dump (__LINE__));
351 return xyzModem_timeout;
352 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200353 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200354 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
355 ZM_DEBUG (zm_save (xyz.crc1));
356 if (!res)
357 {
358 ZM_DEBUG (zm_dump (__LINE__));
359 return xyzModem_timeout;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200360 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200361 if (xyz.crc_mode)
362 {
363 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
364 ZM_DEBUG (zm_save (xyz.crc2));
365 if (!res)
366 {
367 ZM_DEBUG (zm_dump (__LINE__));
368 return xyzModem_timeout;
369 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200370 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200371 ZM_DEBUG (zm_dump (__LINE__));
372 /* Validate the message */
373 if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
374 {
375 ZM_DEBUG (zm_dprintf
376 ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
377 (xyz.blk ^ xyz.cblk)));
378 ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
379 xyzModem_flush ();
380 return xyzModem_frame;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200381 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200382 /* Verify checksum/CRC */
383 if (xyz.crc_mode)
384 {
Stefan Roeseecb57f62016-03-03 09:34:12 +0100385 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200386 if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
387 {
388 ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
389 xyz.crc1, xyz.crc2, cksum & 0xFFFF));
390 return xyzModem_cksum;
391 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200392 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200393 else
394 {
395 cksum = 0;
396 for (i = 0; i < xyz.len; i++)
397 {
398 cksum += xyz.pkt[i];
399 }
400 if (xyz.crc1 != (cksum & 0xFF))
401 {
402 ZM_DEBUG (zm_dprintf
403 ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
404 cksum & 0xFF));
405 return xyzModem_cksum;
406 }
407 }
408 /* If we get here, the message passes [structural] muster */
409 return 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200410}
411
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200412int
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200413xyzModem_stream_open (connection_info_t * info, int *err)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200414{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200415 int stat = 0;
416 int retries = xyzModem_MAX_RETRIES;
417 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200418
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200419/* ZM_DEBUG(zm_out = zm_out_start); */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200420#ifdef xyzModem_zmodem
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200421 if (info->mode == xyzModem_zmodem)
422 {
423 *err = xyzModem_noZmodem;
424 return -1;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200425 }
426#endif
427
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200428/* TODO: CHECK ! */
Kim Phillips2a2ed842009-06-15 11:50:40 -0500429 int dummy = 0;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200430 xyz.__chan = &dummy;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200431 xyz.len = 0;
432 xyz.crc_mode = true;
433 xyz.at_eof = false;
434 xyz.tx_ack = false;
435 xyz.mode = info->mode;
436 xyz.total_retries = 0;
437 xyz.total_SOH = 0;
438 xyz.total_STX = 0;
439 xyz.total_CAN = 0;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200440 xyz.read_length = 0;
441 xyz.file_length = 0;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200442
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200443 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200444
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200445 if (xyz.mode == xyzModem_xmodem)
446 {
447 /* X-modem doesn't have an information header - exit here */
448 xyz.next_blk = 1;
449 return 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200450 }
451
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200452 while (retries-- > 0)
453 {
454 stat = xyzModem_get_hdr ();
455 if (stat == 0)
456 {
457 /* Y-modem file information header */
458 if (xyz.blk == 0)
459 {
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200460 /* skip filename */
461 while (*xyz.bufp++);
462 /* get the length */
463 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200464 /* The rest of the file name data block quietly discarded */
465 xyz.tx_ack = true;
466 }
467 xyz.next_blk = 1;
468 xyz.len = 0;
469 return 0;
470 }
471 else if (stat == xyzModem_timeout)
472 {
473 if (--crc_retries <= 0)
474 xyz.crc_mode = false;
475 CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
476 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
477 xyz.total_retries++;
478 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
479 }
480 if (stat == xyzModem_cancel)
481 {
482 break;
483 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200484 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200485 *err = stat;
486 ZM_DEBUG (zm_flush ());
487 return -1;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200488}
489
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200490int
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200491xyzModem_stream_read (char *buf, int size, int *err)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200492{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200493 int stat, total, len;
494 int retries;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200495
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200496 total = 0;
497 stat = xyzModem_cancel;
498 /* Try and get 'size' bytes into the buffer */
Pali Rohár1e7478462021-08-03 16:28:38 +0200499 while (!xyz.at_eof && xyz.len >= 0 && (size > 0))
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200500 {
501 if (xyz.len == 0)
502 {
503 retries = xyzModem_MAX_RETRIES;
504 while (retries-- > 0)
505 {
506 stat = xyzModem_get_hdr ();
507 if (stat == 0)
508 {
509 if (xyz.blk == xyz.next_blk)
510 {
511 xyz.tx_ack = true;
512 ZM_DEBUG (zm_dprintf
513 ("ACK block %d (%d)\n", xyz.blk, __LINE__));
514 xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200515
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200516 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
517 {
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200518 /* Data blocks can be padded with ^Z (EOF) characters */
519 /* This code tries to detect and remove them */
520 if ((xyz.bufp[xyz.len - 1] == EOF) &&
521 (xyz.bufp[xyz.len - 2] == EOF) &&
522 (xyz.bufp[xyz.len - 3] == EOF))
523 {
524 while (xyz.len
525 && (xyz.bufp[xyz.len - 1] == EOF))
526 {
527 xyz.len--;
528 }
529 }
530 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200531
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200532 /*
533 * See if accumulated length exceeds that of the file.
534 * If so, reduce size (i.e., cut out pad bytes)
535 * Only do this for Y-modem (and Z-modem should it ever
536 * be supported since it can fall back to Y-modem mode).
537 */
538 if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
539 {
540 xyz.read_length += xyz.len;
541 if (xyz.read_length > xyz.file_length)
542 {
543 xyz.len -= (xyz.read_length - xyz.file_length);
544 }
545 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200546 break;
547 }
548 else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
549 {
550 /* Just re-ACK this so sender will get on with it */
551 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
552 continue; /* Need new header */
553 }
554 else
555 {
556 stat = xyzModem_sequence;
557 }
558 }
559 if (stat == xyzModem_cancel)
560 {
561 break;
562 }
563 if (stat == xyzModem_eof)
564 {
565 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
566 ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
567 if (xyz.mode == xyzModem_ymodem)
568 {
569 CYGACC_COMM_IF_PUTC (*xyz.__chan,
570 (xyz.crc_mode ? 'C' : NAK));
571 xyz.total_retries++;
572 ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
573 stat = xyzModem_get_hdr ();
574 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
575 ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
576 }
Pali Rohár15c27a52021-08-03 16:28:39 +0200577 else
578 stat = 0;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200579 xyz.at_eof = true;
580 break;
581 }
582 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
583 xyz.total_retries++;
584 ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
585 }
586 if (stat < 0)
587 {
588 *err = stat;
589 xyz.len = -1;
590 return total;
591 }
592 }
593 /* Don't "read" data from the EOF protocol package */
Pali Rohár1e7478462021-08-03 16:28:38 +0200594 if (!xyz.at_eof && xyz.len > 0)
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200595 {
596 len = xyz.len;
597 if (size < len)
598 len = size;
599 memcpy (buf, xyz.bufp, len);
600 size -= len;
601 buf += len;
602 total += len;
603 xyz.len -= len;
604 xyz.bufp += len;
605 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200606 }
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200607 return total;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200608}
609
610void
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200611xyzModem_stream_close (int *err)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200612{
Pali Rohárf05d69b2021-08-03 16:28:40 +0200613 ZM_DEBUG (zm_dprintf
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200614 ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
615 xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
Pali Rohárf05d69b2021-08-03 16:28:40 +0200616 xyz.total_CAN, xyz.total_retries));
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200617 ZM_DEBUG (zm_flush ());
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200618}
619
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200620/* Need to be able to clean out the input buffer, so have to take the */
621/* getc */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200622void
623xyzModem_stream_terminate (bool abort, int (*getc) (void))
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200624{
625 int c;
626
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200627 if (abort)
628 {
629 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
630 switch (xyz.mode)
631 {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200632 case xyzModem_xmodem:
633 case xyzModem_ymodem:
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200634 /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
635 /* number of Backspaces is a friendly way to get the other end to abort. */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200636 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
637 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
638 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
639 CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
640 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
641 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
642 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
643 CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200644 /* Now consume the rest of what's waiting on the line. */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200645 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
646 xyzModem_flush ();
647 xyz.at_eof = true;
648 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200649#ifdef xyzModem_zmodem
650 case xyzModem_zmodem:
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200651 /* Might support it some day I suppose. */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200652#endif
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200653 break;
654 }
655 }
656 else
657 {
658 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200659 /*
660 * Consume any trailing crap left in the inbuffer from
Mike Williams16263082011-07-22 04:01:30 +0000661 * previous received blocks. Since very few files are an exact multiple
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200662 * of the transfer block size, there will almost always be some gunk here.
663 * If we don't eat it now, RedBoot will think the user typed it.
664 */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200665 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
Jeroen Hofsteee153b132014-06-11 01:04:42 +0200666 while ((c = (*getc) ()) > -1)
667 ;
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200668 ZM_DEBUG (zm_dprintf ("\n"));
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200669 /*
670 * Make a small delay to give terminal programs like minicom
671 * time to get control again after their file transfer program
672 * exits.
673 */
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200674 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
675 }
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200676}
677
678char *
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200679xyzModem_error (int err)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200680{
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200681 switch (err)
682 {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200683 case xyzModem_access:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200684 return "Can't access file";
685 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200686 case xyzModem_noZmodem:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200687 return "Sorry, zModem not available yet";
688 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200689 case xyzModem_timeout:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200690 return "Timed out";
691 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200692 case xyzModem_eof:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200693 return "End of file";
694 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200695 case xyzModem_cancel:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200696 return "Cancelled";
697 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200698 case xyzModem_frame:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200699 return "Invalid framing";
700 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200701 case xyzModem_cksum:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200702 return "CRC/checksum error";
703 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200704 case xyzModem_sequence:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200705 return "Block sequence error";
706 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200707 default:
Wolfgang Denk7d0432c2006-08-31 16:46:53 +0200708 return "Unknown error";
709 break;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200710 }
711}
712
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200713/*
714 * RedBoot interface
715 */