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 | |
| 65 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 66 | typedef int cyg_int32; |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 67 | static int |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 68 | CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c) |
| 69 | { |
tomas.melin@vaisala.com | d5681a5 | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 70 | |
| 71 | ulong now = get_timer(0); |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 72 | schedule(); |
tomas.melin@vaisala.com | d5681a5 | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 73 | while (!tstc ()) |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 74 | { |
tomas.melin@vaisala.com | d5681a5 | 2016-11-21 10:18:51 +0200 | [diff] [blame] | 75 | if (get_timer(now) > xyzModem_CHAR_TIMEOUT) |
| 76 | break; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 77 | } |
| 78 | if (tstc ()) |
| 79 | { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 80 | *c = getchar(); |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | return 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 86 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 87 | CYGACC_COMM_IF_PUTC (char x, char y) |
| 88 | { |
| 89 | putc (y); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 92 | /* Validate a hex character */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 93 | __inline__ static bool |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 94 | _is_hex (char c) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 95 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 96 | return (((c >= '0') && (c <= '9')) || |
| 97 | ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 100 | /* Convert a single hex nibble */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 101 | __inline__ static int |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 102 | _from_hex (char c) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 103 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 104 | int ret = 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 105 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 106 | if ((c >= '0') && (c <= '9')) |
| 107 | { |
| 108 | ret = (c - '0'); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 109 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 110 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 121 | /* Convert a character to lower case */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 122 | __inline__ static char |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 123 | _tolower (char c) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 124 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 125 | if ((c >= 'A') && (c <= 'Z')) |
| 126 | { |
| 127 | c = (c - 'A') + 'a'; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 128 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 129 | return c; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 132 | /* Parse (scan) a number */ |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 133 | static bool |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 134 | parse_num (char *s, unsigned long *val, char **es, char *delim) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 135 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 136 | bool first = true; |
| 137 | int radix = 10; |
| 138 | char c; |
| 139 | unsigned long result = 0; |
| 140 | int digit; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 141 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 142 | 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 156 | result = (result * radix) + digit; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 157 | } |
| 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 171 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 172 | *val = result; |
| 173 | if (es != (char **) 0) |
| 174 | { |
| 175 | *es = s; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 176 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 177 | return true; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 180 | |
Simon Glass | 7611ac6 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 181 | #if defined(DEBUG) && !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 182 | /* |
| 183 | * Note: this debug setup works by storing the strings in a fixed buffer |
| 184 | */ |
Alexandru Gagniuc | 3967101 | 2017-04-04 10:42:31 -0700 | [diff] [blame] | 185 | static char zm_debug_buf[8192]; |
| 186 | static char *zm_out = zm_debug_buf; |
| 187 | static char *zm_out_start = zm_debug_buf; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 188 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 189 | static int |
Heinrich Schuchardt | b65e1a2 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 190 | zm_dprintf(char *fmt, ...) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 191 | { |
Heinrich Schuchardt | b65e1a2 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 192 | int len; |
| 193 | va_list args; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 194 | |
Heinrich Schuchardt | b65e1a2 | 2018-05-07 21:59:34 +0200 | [diff] [blame] | 195 | va_start(args, fmt); |
| 196 | len = diag_vsprintf(zm_out, fmt, args); |
| 197 | va_end(args); |
| 198 | zm_out += len; |
| 199 | return len; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 203 | zm_flush (void) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 204 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 205 | zm_out = zm_out_start; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 206 | } |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 207 | |
| 208 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 209 | zm_dump_buf (void *buf, int len) |
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 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static unsigned char zm_buf[2048]; |
| 215 | static unsigned char *zm_bp; |
| 216 | |
| 217 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 218 | zm_new (void) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 219 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 220 | zm_bp = zm_buf; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 224 | zm_save (unsigned char c) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 225 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 226 | *zm_bp++ = c; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 230 | zm_dump (int line) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 231 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 232 | zm_dprintf ("Packet at line: %d\n", line); |
| 233 | zm_dump_buf (zm_buf, zm_bp - zm_buf); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | #define ZM_DEBUG(x) x |
| 237 | #else |
| 238 | #define ZM_DEBUG(x) |
| 239 | #endif |
| 240 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 241 | /* Wait for the line to go idle */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 242 | static void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 243 | xyzModem_flush (void) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 244 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 245 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | static int |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 256 | xyzModem_get_hdr (void) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 257 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 258 | char c; |
| 259 | int res; |
| 260 | bool hdr_found = false; |
| 261 | int i, can_total, hdr_chars; |
| 262 | unsigned short cksum; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 263 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 264 | ZM_DEBUG (zm_new ()); |
| 265 | /* Find the start of a header */ |
| 266 | can_total = 0; |
| 267 | hdr_chars = 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 268 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 269 | if (xyz.tx_ack) |
| 270 | { |
| 271 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 272 | xyz.tx_ack = false; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 273 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 274 | 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ár | 647ae2c | 2021-08-03 16:28:44 +0200 | [diff] [blame] | 291 | case ETX: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 292 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 327 | /* 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 334 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 335 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 341 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 342 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 364 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 365 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 374 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 375 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 385 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 386 | /* Verify checksum/CRC */ |
| 387 | if (xyz.crc_mode) |
| 388 | { |
Stefan Roese | 084ff1e | 2016-03-03 09:34:12 +0100 | [diff] [blame] | 389 | cksum = crc16_ccitt(0, xyz.pkt, xyz.len); |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 390 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 396 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 397 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 411 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 412 | /* If we get here, the message passes [structural] muster */ |
| 413 | return 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 416 | static |
| 417 | ulong |
| 418 | xyzModem_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 Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 429 | int |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 430 | xyzModem_stream_open (connection_info_t * info, int *err) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 431 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 432 | int stat = 0; |
| 433 | int retries = xyzModem_MAX_RETRIES; |
| 434 | int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 435 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 436 | /* ZM_DEBUG(zm_out = zm_out_start); */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 437 | #ifdef xyzModem_zmodem |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 438 | if (info->mode == xyzModem_zmodem) |
| 439 | { |
| 440 | *err = xyzModem_noZmodem; |
| 441 | return -1; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 442 | } |
| 443 | #endif |
| 444 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 445 | /* TODO: CHECK ! */ |
Kim Phillips | 66b0726 | 2009-06-15 11:50:40 -0500 | [diff] [blame] | 446 | int dummy = 0; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 447 | xyz.__chan = &dummy; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 448 | 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 457 | xyz.read_length = 0; |
| 458 | xyz.file_length = 0; |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 459 | xyz.first_xmodem_packet = false; |
| 460 | xyz.initial_time = get_timer(0); |
| 461 | xyz.timeout = xyzModem_get_initial_timeout(); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 462 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 463 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 464 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 465 | if (xyz.mode == xyzModem_xmodem) |
| 466 | { |
| 467 | /* X-modem doesn't have an information header - exit here */ |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 468 | xyz.first_xmodem_packet = true; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 469 | xyz.next_blk = 1; |
| 470 | return 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 473 | while (!(xyz.timeout && get_timer(xyz.initial_time) > xyz.timeout)) |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 474 | { |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 475 | if (--retries <= 0) |
| 476 | { |
| 477 | retries = xyzModem_MAX_RETRIES; |
| 478 | crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; |
| 479 | xyz.crc_mode = true; |
| 480 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 481 | stat = xyzModem_get_hdr (); |
| 482 | if (stat == 0) |
| 483 | { |
| 484 | /* Y-modem file information header */ |
| 485 | if (xyz.blk == 0) |
| 486 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 487 | /* skip filename */ |
| 488 | while (*xyz.bufp++); |
| 489 | /* get the length */ |
| 490 | parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " "); |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 491 | /* 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 511 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 512 | *err = stat; |
| 513 | ZM_DEBUG (zm_flush ()); |
| 514 | return -1; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 517 | int |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 518 | xyzModem_stream_read (char *buf, int size, int *err) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 519 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 520 | int stat, total, len; |
| 521 | int retries; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 522 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 523 | total = 0; |
| 524 | stat = xyzModem_cancel; |
| 525 | /* Try and get 'size' bytes into the buffer */ |
Pali Rohár | 094cfbb | 2021-08-03 16:28:38 +0200 | [diff] [blame] | 526 | while (!xyz.at_eof && xyz.len >= 0 && (size > 0)) |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 527 | { |
| 528 | if (xyz.len == 0) |
| 529 | { |
| 530 | retries = xyzModem_MAX_RETRIES; |
| 531 | while (retries-- > 0) |
| 532 | { |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 533 | 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 541 | stat = xyzModem_get_hdr (); |
| 542 | if (stat == 0) |
| 543 | { |
Pali Rohár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 544 | if (xyz.mode == xyzModem_xmodem && xyz.first_xmodem_packet) |
| 545 | xyz.first_xmodem_packet = false; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 546 | 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 552 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 553 | if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) |
| 554 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 555 | /* 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 568 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 569 | /* |
| 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 583 | 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ár | c2d20c7 | 2021-08-03 16:28:39 +0200 | [diff] [blame] | 614 | else |
| 615 | stat = 0; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 616 | 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ár | 5dc80cc | 2022-08-27 16:37:55 +0200 | [diff] [blame] | 623 | if (stat < 0 && (!xyz.first_xmodem_packet || stat != xyzModem_timeout)) |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 624 | { |
| 625 | *err = stat; |
| 626 | xyz.len = -1; |
| 627 | return total; |
| 628 | } |
| 629 | } |
| 630 | /* Don't "read" data from the EOF protocol package */ |
Pali Rohár | 094cfbb | 2021-08-03 16:28:38 +0200 | [diff] [blame] | 631 | if (!xyz.at_eof && xyz.len > 0) |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 632 | { |
| 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 Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 643 | } |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 644 | return total; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 648 | xyzModem_stream_close (int *err) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 649 | { |
Pali Rohár | 81f0491 | 2021-08-03 16:28:40 +0200 | [diff] [blame] | 650 | ZM_DEBUG (zm_dprintf |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 651 | ("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ár | 81f0491 | 2021-08-03 16:28:40 +0200 | [diff] [blame] | 653 | xyz.total_CAN, xyz.total_retries)); |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 654 | ZM_DEBUG (zm_flush ()); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 657 | /* Need to be able to clean out the input buffer, so have to take the */ |
| 658 | /* getc */ |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 659 | void |
| 660 | xyzModem_stream_terminate (bool abort, int (*getc) (void)) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 661 | { |
| 662 | int c; |
| 663 | |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 664 | if (abort) |
| 665 | { |
| 666 | ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n")); |
| 667 | switch (xyz.mode) |
| 668 | { |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 669 | case xyzModem_xmodem: |
| 670 | case xyzModem_ymodem: |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 671 | /* 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 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, 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 Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 681 | /* Now consume the rest of what's waiting on the line. */ |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 682 | ZM_DEBUG (zm_dprintf ("Flushing serial line.\n")); |
| 683 | xyzModem_flush (); |
| 684 | xyz.at_eof = true; |
| 685 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 686 | #ifdef xyzModem_zmodem |
| 687 | case xyzModem_zmodem: |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 688 | /* Might support it some day I suppose. */ |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 689 | #endif |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n")); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 696 | /* |
| 697 | * Consume any trailing crap left in the inbuffer from |
Mike Williams | bf895ad | 2011-07-22 04:01:30 +0000 | [diff] [blame] | 698 | * previous received blocks. Since very few files are an exact multiple |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 699 | * 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 702 | ZM_DEBUG (zm_dprintf ("Trailing gunk:\n")); |
Jeroen Hofstee | 419b6a4 | 2014-06-11 01:04:42 +0200 | [diff] [blame] | 703 | while ((c = (*getc) ()) > -1) |
| 704 | ; |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 705 | ZM_DEBUG (zm_dprintf ("\n")); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 706 | /* |
| 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 Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 711 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
| 712 | } |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | char * |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 716 | xyzModem_error (int err) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 717 | { |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 718 | switch (err) |
| 719 | { |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 720 | case xyzModem_access: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 721 | return "Can't access file"; |
| 722 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 723 | case xyzModem_noZmodem: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 724 | return "Sorry, zModem not available yet"; |
| 725 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 726 | case xyzModem_timeout: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 727 | return "Timed out"; |
| 728 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 729 | case xyzModem_eof: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 730 | return "End of file"; |
| 731 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 732 | case xyzModem_cancel: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 733 | return "Cancelled"; |
| 734 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 735 | case xyzModem_frame: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 736 | return "Invalid framing"; |
| 737 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 738 | case xyzModem_cksum: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 739 | return "CRC/checksum error"; |
| 740 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 741 | case xyzModem_sequence: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 742 | return "Block sequence error"; |
| 743 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 744 | default: |
Wolfgang Denk | 95d1169 | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 745 | return "Unknown error"; |
| 746 | break; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 750 | /* |
| 751 | * RedBoot interface |
| 752 | */ |