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