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