blob: 9feb240de28143d3caa516febbd7a1c3289ebd84 [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 <xyzModem.h>
25#include <stdarg.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060026#include <time.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>
Pali Rohár5dc80cc2022-08-27 16:37:55 +020029#include <env.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060030#include <vsprintf.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020031
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020032/* Assumption - run xyzModem protocol over the console port */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020033
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020034/* Values magic to the protocol */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020035#define SOH 0x01
36#define STX 0x02
Pali Rohár647ae2c2021-08-03 16:28:44 +020037#define ETX 0x03 /* ^C for interrupt */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020038#define EOT 0x04
39#define ACK 0x06
40#define BSP 0x08
41#define NAK 0x15
42#define CAN 0x18
Wolfgang Denk95d11692006-08-31 16:46:53 +020043#define EOF 0x1A /* ^Z for DOS officionados */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020044
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020045/* Data & state local to the protocol */
Wolfgang Denk95d11692006-08-31 16:46:53 +020046static struct
47{
Wolfgang Denk95d11692006-08-31 16:46:53 +020048 int *__chan;
Wolfgang Denk95d11692006-08-31 16:46:53 +020049 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;
Pali Rohár5dc80cc2022-08-27 16:37:55 +020055 bool first_xmodem_packet;
56 ulong initial_time, timeout;
Wolfgang Denk95d11692006-08-31 16:46:53 +020057 unsigned long file_length, read_length;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020058} xyz;
59
Wolfgang Denk95d11692006-08-31 16:46:53 +020060#define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020061#define xyzModem_MAX_RETRIES 20
62#define xyzModem_MAX_RETRIES_WITH_CRC 10
Wolfgang Denk95d11692006-08-31 16:46:53 +020063#define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020064
65
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020066typedef int cyg_int32;
Kim Phillipsb052b602012-10-29 13:34:32 +000067static int
Wolfgang Denk95d11692006-08-31 16:46:53 +020068CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
69{
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020070
71 ulong now = get_timer(0);
Stefan Roese80877fa2022-09-02 14:10:46 +020072 schedule();
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020073 while (!tstc ())
Wolfgang Denk95d11692006-08-31 16:46:53 +020074 {
tomas.melin@vaisala.comd5681a52016-11-21 10:18:51 +020075 if (get_timer(now) > xyzModem_CHAR_TIMEOUT)
76 break;
Wolfgang Denk95d11692006-08-31 16:46:53 +020077 }
78 if (tstc ())
79 {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020080 *c = getchar();
Wolfgang Denk95d11692006-08-31 16:46:53 +020081 return 1;
82 }
83 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020084}
85
Kim Phillipsb052b602012-10-29 13:34:32 +000086static void
Wolfgang Denk95d11692006-08-31 16:46:53 +020087CYGACC_COMM_IF_PUTC (char x, char y)
88{
89 putc (y);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020090}
91
Wolfgang Denkebd3deb2006-04-16 10:51:58 +020092/* Validate a hex character */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020093__inline__ static bool
Wolfgang Denk95d11692006-08-31 16:46:53 +020094_is_hex (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020095{
Wolfgang Denk95d11692006-08-31 16:46:53 +020096 return (((c >= '0') && (c <= '9')) ||
97 ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020098}
99
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200100/* Convert a single hex nibble */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200101__inline__ static int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200102_from_hex (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200103{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200104 int ret = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200105
Wolfgang Denk95d11692006-08-31 16:46:53 +0200106 if ((c >= '0') && (c <= '9'))
107 {
108 ret = (c - '0');
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200109 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200110 else if ((c >= 'a') && (c <= 'f'))
111 {
112 ret = (c - 'a' + 0x0a);
113 }
114 else if ((c >= 'A') && (c <= 'F'))
115 {
116 ret = (c - 'A' + 0x0A);
117 }
118 return ret;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200119}
120
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200121/* Convert a character to lower case */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200122__inline__ static char
Wolfgang Denk95d11692006-08-31 16:46:53 +0200123_tolower (char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200124{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200125 if ((c >= 'A') && (c <= 'Z'))
126 {
127 c = (c - 'A') + 'a';
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200128 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200129 return c;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200130}
131
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200132/* Parse (scan) a number */
Kim Phillipsb052b602012-10-29 13:34:32 +0000133static bool
Wolfgang Denk95d11692006-08-31 16:46:53 +0200134parse_num (char *s, unsigned long *val, char **es, char *delim)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200135{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200136 bool first = true;
137 int radix = 10;
138 char c;
139 unsigned long result = 0;
140 int digit;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200141
Wolfgang Denk95d11692006-08-31 16:46:53 +0200142 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 */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200156 result = (result * radix) + digit;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200157 }
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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200171 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200172 *val = result;
173 if (es != (char **) 0)
174 {
175 *es = s;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200176 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200177 return true;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200178}
179
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200180
Simon Glass7611ac62019-09-25 08:56:27 -0600181#if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200182/*
183 * Note: this debug setup works by storing the strings in a fixed buffer
184 */
Alexandru Gagniuc39671012017-04-04 10:42:31 -0700185static char zm_debug_buf[8192];
186static char *zm_out = zm_debug_buf;
187static char *zm_out_start = zm_debug_buf;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200188
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200189static int
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200190zm_dprintf(char *fmt, ...)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200191{
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200192 int len;
193 va_list args;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200194
Heinrich Schuchardtb65e1a22018-05-07 21:59:34 +0200195 va_start(args, fmt);
196 len = diag_vsprintf(zm_out, fmt, args);
197 va_end(args);
198 zm_out += len;
199 return len;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200200}
201
202static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200203zm_flush (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200204{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200205 zm_out = zm_out_start;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200206}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200207
208static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200209zm_dump_buf (void *buf, int len)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200210{
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200211
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200212}
213
214static unsigned char zm_buf[2048];
215static unsigned char *zm_bp;
216
217static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200218zm_new (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200219{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200220 zm_bp = zm_buf;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200221}
222
223static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200224zm_save (unsigned char c)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200225{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200226 *zm_bp++ = c;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200227}
228
229static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200230zm_dump (int line)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200231{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200232 zm_dprintf ("Packet at line: %d\n", line);
233 zm_dump_buf (zm_buf, zm_bp - zm_buf);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200234}
235
236#define ZM_DEBUG(x) x
237#else
238#define ZM_DEBUG(x)
239#endif
240
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200241/* Wait for the line to go idle */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200242static void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200243xyzModem_flush (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200244{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200245 int res;
246 char c;
247 while (true)
248 {
249 res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
250 if (!res)
251 return;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200252 }
253}
254
255static int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200256xyzModem_get_hdr (void)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200257{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200258 char c;
259 int res;
260 bool hdr_found = false;
261 int i, can_total, hdr_chars;
262 unsigned short cksum;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200263
Wolfgang Denk95d11692006-08-31 16:46:53 +0200264 ZM_DEBUG (zm_new ());
265 /* Find the start of a header */
266 can_total = 0;
267 hdr_chars = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200268
Wolfgang Denk95d11692006-08-31 16:46:53 +0200269 if (xyz.tx_ack)
270 {
271 CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
272 xyz.tx_ack = false;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200273 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200274 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:
Pali Rohár647ae2c2021-08-03 16:28:44 +0200291 case ETX:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200292 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200325 }
326
Wolfgang Denk95d11692006-08-31 16:46:53 +0200327 /* 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;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200334 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200335 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;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200341 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200342 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 }
357 }
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;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200364 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200365 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200374 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200375 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;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200385 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200386 /* Verify checksum/CRC */
387 if (xyz.crc_mode)
388 {
Stefan Roese084ff1e2016-03-03 09:34:12 +0100389 cksum = crc16_ccitt(0, xyz.pkt, xyz.len);
Wolfgang Denk95d11692006-08-31 16:46:53 +0200390 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200396 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200397 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200411 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200412 /* If we get here, the message passes [structural] muster */
413 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200414}
415
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200416static
417ulong
418xyzModem_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
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200429int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200430xyzModem_stream_open (connection_info_t * info, int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200431{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200432 int stat = 0;
433 int retries = xyzModem_MAX_RETRIES;
434 int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200435
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200436/* ZM_DEBUG(zm_out = zm_out_start); */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200437#ifdef xyzModem_zmodem
Wolfgang Denk95d11692006-08-31 16:46:53 +0200438 if (info->mode == xyzModem_zmodem)
439 {
440 *err = xyzModem_noZmodem;
441 return -1;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200442 }
443#endif
444
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200445/* TODO: CHECK ! */
Kim Phillips66b07262009-06-15 11:50:40 -0500446 int dummy = 0;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200447 xyz.__chan = &dummy;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200448 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;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200457 xyz.read_length = 0;
458 xyz.file_length = 0;
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200459 xyz.first_xmodem_packet = false;
460 xyz.initial_time = get_timer(0);
461 xyz.timeout = xyzModem_get_initial_timeout();
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200462
Wolfgang Denk95d11692006-08-31 16:46:53 +0200463 CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200464
Wolfgang Denk95d11692006-08-31 16:46:53 +0200465 if (xyz.mode == xyzModem_xmodem)
466 {
467 /* X-modem doesn't have an information header - exit here */
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200468 xyz.first_xmodem_packet = true;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200469 xyz.next_blk = 1;
470 return 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200471 }
472
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200473 while (!(xyz.timeout && get_timer(xyz.initial_time) > xyz.timeout))
Wolfgang Denk95d11692006-08-31 16:46:53 +0200474 {
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200475 if (--retries <= 0)
476 {
477 retries = xyzModem_MAX_RETRIES;
478 crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
479 xyz.crc_mode = true;
480 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200481 stat = xyzModem_get_hdr ();
482 if (stat == 0)
483 {
484 /* Y-modem file information header */
485 if (xyz.blk == 0)
486 {
Wolfgang Denk95d11692006-08-31 16:46:53 +0200487 /* skip filename */
488 while (*xyz.bufp++);
489 /* get the length */
490 parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
Wolfgang Denk95d11692006-08-31 16:46:53 +0200491 /* 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200511 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200512 *err = stat;
513 ZM_DEBUG (zm_flush ());
514 return -1;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200515}
516
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200517int
Wolfgang Denk95d11692006-08-31 16:46:53 +0200518xyzModem_stream_read (char *buf, int size, int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200519{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200520 int stat, total, len;
521 int retries;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200522
Wolfgang Denk95d11692006-08-31 16:46:53 +0200523 total = 0;
524 stat = xyzModem_cancel;
525 /* Try and get 'size' bytes into the buffer */
Pali Rohár094cfbb2021-08-03 16:28:38 +0200526 while (!xyz.at_eof && xyz.len >= 0 && (size > 0))
Wolfgang Denk95d11692006-08-31 16:46:53 +0200527 {
528 if (xyz.len == 0)
529 {
530 retries = xyzModem_MAX_RETRIES;
531 while (retries-- > 0)
532 {
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200533 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
Wolfgang Denk95d11692006-08-31 16:46:53 +0200541 stat = xyzModem_get_hdr ();
542 if (stat == 0)
543 {
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200544 if (xyz.mode == xyzModem_xmodem && xyz.first_xmodem_packet)
545 xyz.first_xmodem_packet = false;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200546 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;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200552
Wolfgang Denk95d11692006-08-31 16:46:53 +0200553 if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
554 {
Wolfgang Denk95d11692006-08-31 16:46:53 +0200555 /* 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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200568
Wolfgang Denk95d11692006-08-31 16:46:53 +0200569 /*
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 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200583 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 }
Pali Rohárc2d20c72021-08-03 16:28:39 +0200614 else
615 stat = 0;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200616 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 }
Pali Rohár5dc80cc2022-08-27 16:37:55 +0200623 if (stat < 0 && (!xyz.first_xmodem_packet || stat != xyzModem_timeout))
Wolfgang Denk95d11692006-08-31 16:46:53 +0200624 {
625 *err = stat;
626 xyz.len = -1;
627 return total;
628 }
629 }
630 /* Don't "read" data from the EOF protocol package */
Pali Rohár094cfbb2021-08-03 16:28:38 +0200631 if (!xyz.at_eof && xyz.len > 0)
Wolfgang Denk95d11692006-08-31 16:46:53 +0200632 {
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 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200643 }
Wolfgang Denk95d11692006-08-31 16:46:53 +0200644 return total;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200645}
646
647void
Wolfgang Denk95d11692006-08-31 16:46:53 +0200648xyzModem_stream_close (int *err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200649{
Pali Rohár81f04912021-08-03 16:28:40 +0200650 ZM_DEBUG (zm_dprintf
Wolfgang Denk95d11692006-08-31 16:46:53 +0200651 ("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,
Pali Rohár81f04912021-08-03 16:28:40 +0200653 xyz.total_CAN, xyz.total_retries));
Wolfgang Denk95d11692006-08-31 16:46:53 +0200654 ZM_DEBUG (zm_flush ());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200655}
656
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200657/* Need to be able to clean out the input buffer, so have to take the */
658/* getc */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200659void
660xyzModem_stream_terminate (bool abort, int (*getc) (void))
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200661{
662 int c;
663
Wolfgang Denk95d11692006-08-31 16:46:53 +0200664 if (abort)
665 {
666 ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
667 switch (xyz.mode)
668 {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200669 case xyzModem_xmodem:
670 case xyzModem_ymodem:
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200671 /* 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. */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200673 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);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200681 /* Now consume the rest of what's waiting on the line. */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200682 ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
683 xyzModem_flush ();
684 xyz.at_eof = true;
685 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200686#ifdef xyzModem_zmodem
687 case xyzModem_zmodem:
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200688 /* Might support it some day I suppose. */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200689#endif
Wolfgang Denk95d11692006-08-31 16:46:53 +0200690 break;
691 }
692 }
693 else
694 {
695 ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200696 /*
697 * Consume any trailing crap left in the inbuffer from
Mike Williamsbf895ad2011-07-22 04:01:30 +0000698 * previous received blocks. Since very few files are an exact multiple
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200699 * 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 */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200702 ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
Jeroen Hofstee419b6a42014-06-11 01:04:42 +0200703 while ((c = (*getc) ()) > -1)
704 ;
Wolfgang Denk95d11692006-08-31 16:46:53 +0200705 ZM_DEBUG (zm_dprintf ("\n"));
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200706 /*
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 */
Wolfgang Denk95d11692006-08-31 16:46:53 +0200711 CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
712 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200713}
714
715char *
Wolfgang Denk95d11692006-08-31 16:46:53 +0200716xyzModem_error (int err)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200717{
Wolfgang Denk95d11692006-08-31 16:46:53 +0200718 switch (err)
719 {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200720 case xyzModem_access:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200721 return "Can't access file";
722 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200723 case xyzModem_noZmodem:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200724 return "Sorry, zModem not available yet";
725 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200726 case xyzModem_timeout:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200727 return "Timed out";
728 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200729 case xyzModem_eof:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200730 return "End of file";
731 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200732 case xyzModem_cancel:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200733 return "Cancelled";
734 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200735 case xyzModem_frame:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200736 return "Invalid framing";
737 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200738 case xyzModem_cksum:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200739 return "CRC/checksum error";
740 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200741 case xyzModem_sequence:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200742 return "Block sequence error";
743 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200744 default:
Wolfgang Denk95d11692006-08-31 16:46:53 +0200745 return "Unknown error";
746 break;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200747 }
748}
749
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200750/*
751 * RedBoot interface
752 */