Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | *========================================================================== |
| 3 | * |
| 4 | * xyzModem.c |
| 5 | * |
| 6 | * RedBoot stream handler for xyzModem protocol |
| 7 | * |
| 8 | *========================================================================== |
| 9 | *####ECOSGPLCOPYRIGHTBEGIN#### |
| 10 | * ------------------------------------------- |
| 11 | * This file is part of eCos, the Embedded Configurable Operating System. |
| 12 | * Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. |
| 13 | * Copyright (C) 2002 Gary Thomas |
| 14 | * |
| 15 | * eCos is free software; you can redistribute it and/or modify it under |
| 16 | * the terms of the GNU General Public License as published by the Free |
| 17 | * Software Foundation; either version 2 or (at your option) any later version. |
| 18 | * |
| 19 | * eCos is distributed in the hope that it will be useful, but WITHOUT ANY |
| 20 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 22 | * for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License along |
| 25 | * with eCos; if not, write to the Free Software Foundation, Inc., |
| 26 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 27 | * |
| 28 | * As a special exception, if other files instantiate templates or use macros |
| 29 | * or inline functions from this file, or you compile this file and link it |
| 30 | * with other works to produce a work based on this file, this file does not |
| 31 | * by itself cause the resulting work to be covered by the GNU General Public |
| 32 | * License. However the source code for this file must still be made available |
| 33 | * in accordance with section (3) of the GNU General Public License. |
| 34 | * |
| 35 | * This exception does not invalidate any other reasons why a work based on |
| 36 | * this file might be covered by the GNU General Public License. |
| 37 | * |
| 38 | * Alternative licenses for eCos may be arranged by contacting Red Hat, Inc. |
| 39 | * at http: *sources.redhat.com/ecos/ecos-license/ |
| 40 | * ------------------------------------------- |
| 41 | *####ECOSGPLCOPYRIGHTEND#### |
| 42 | *========================================================================== |
| 43 | *#####DESCRIPTIONBEGIN#### |
| 44 | * |
| 45 | * Author(s): gthomas |
| 46 | * Contributors: gthomas, tsmith, Yoshinori Sato |
| 47 | * Date: 2000-07-14 |
| 48 | * Purpose: |
| 49 | * Description: |
| 50 | * |
| 51 | * This code is part of RedBoot (tm). |
| 52 | * |
| 53 | *####DESCRIPTIONEND#### |
| 54 | * |
| 55 | *========================================================================== |
| 56 | */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 57 | #include <common.h> |
| 58 | #include <xyzModem.h> |
| 59 | #include <stdarg.h> |
| 60 | #include <crc.h> |
| 61 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 62 | /* Assumption - run xyzModem protocol over the console port */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 63 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 64 | /* Values magic to the protocol */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 65 | #define SOH 0x01 |
| 66 | #define STX 0x02 |
| 67 | #define EOT 0x04 |
| 68 | #define ACK 0x06 |
| 69 | #define BSP 0x08 |
| 70 | #define NAK 0x15 |
| 71 | #define CAN 0x18 |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 72 | #define EOF 0x1A /* ^Z for DOS officionados */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 73 | |
| 74 | #define USE_YMODEM_LENGTH |
| 75 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 76 | /* Data & state local to the protocol */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 77 | static struct |
| 78 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 79 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 80 | hal_virtual_comm_table_t *__chan; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 81 | #else |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 82 | int *__chan; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 83 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 84 | unsigned char pkt[1024], *bufp; |
| 85 | unsigned char blk, cblk, crc1, crc2; |
| 86 | unsigned char next_blk; /* Expected block */ |
| 87 | int len, mode, total_retries; |
| 88 | int total_SOH, total_STX, total_CAN; |
| 89 | bool crc_mode, at_eof, tx_ack; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 90 | #ifdef USE_YMODEM_LENGTH |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 91 | unsigned long file_length, read_length; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 92 | #endif |
| 93 | } xyz; |
| 94 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 95 | #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 96 | #define xyzModem_MAX_RETRIES 20 |
| 97 | #define xyzModem_MAX_RETRIES_WITH_CRC 10 |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 98 | #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 99 | |
| 100 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 101 | #ifndef REDBOOT /*SB */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 102 | typedef int cyg_int32; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 103 | int |
| 104 | CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c) |
| 105 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 106 | #define DELAY 20 |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 107 | unsigned long counter = 0; |
| 108 | while (!tstc () && (counter < xyzModem_CHAR_TIMEOUT * 1000 / DELAY)) |
| 109 | { |
| 110 | udelay (DELAY); |
| 111 | counter++; |
| 112 | } |
| 113 | if (tstc ()) |
| 114 | { |
| 115 | *c = getc (); |
| 116 | return 1; |
| 117 | } |
| 118 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 121 | void |
| 122 | CYGACC_COMM_IF_PUTC (char x, char y) |
| 123 | { |
| 124 | putc (y); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 127 | /* Validate a hex character */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 128 | __inline__ static bool |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 129 | _is_hex (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 130 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 131 | return (((c >= '0') && (c <= '9')) || |
| 132 | ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f'))); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 135 | /* Convert a single hex nibble */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 136 | __inline__ static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 137 | _from_hex (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 138 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 139 | int ret = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 140 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 141 | if ((c >= '0') && (c <= '9')) |
| 142 | { |
| 143 | ret = (c - '0'); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 144 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 145 | else if ((c >= 'a') && (c <= 'f')) |
| 146 | { |
| 147 | ret = (c - 'a' + 0x0a); |
| 148 | } |
| 149 | else if ((c >= 'A') && (c <= 'F')) |
| 150 | { |
| 151 | ret = (c - 'A' + 0x0A); |
| 152 | } |
| 153 | return ret; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 156 | /* Convert a character to lower case */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 157 | __inline__ static char |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 158 | _tolower (char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 159 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 160 | if ((c >= 'A') && (c <= 'Z')) |
| 161 | { |
| 162 | c = (c - 'A') + 'a'; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 163 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 164 | return c; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 167 | /* Parse (scan) a number */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 168 | bool |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 169 | parse_num (char *s, unsigned long *val, char **es, char *delim) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 170 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 171 | bool first = true; |
| 172 | int radix = 10; |
| 173 | char c; |
| 174 | unsigned long result = 0; |
| 175 | int digit; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 176 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 177 | while (*s == ' ') |
| 178 | s++; |
| 179 | while (*s) |
| 180 | { |
| 181 | if (first && (s[0] == '0') && (_tolower (s[1]) == 'x')) |
| 182 | { |
| 183 | radix = 16; |
| 184 | s += 2; |
| 185 | } |
| 186 | first = false; |
| 187 | c = *s++; |
| 188 | if (_is_hex (c) && ((digit = _from_hex (c)) < radix)) |
| 189 | { |
| 190 | /* Valid digit */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 191 | #ifdef CYGPKG_HAL_MIPS |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 192 | /* FIXME: tx49 compiler generates 0x2539018 for MUL which */ |
| 193 | /* isn't any good. */ |
| 194 | if (16 == radix) |
| 195 | result = result << 4; |
| 196 | else |
| 197 | result = 10 * result; |
| 198 | result += digit; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 199 | #else |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 200 | result = (result * radix) + digit; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 201 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
| 205 | if (delim != (char *) 0) |
| 206 | { |
| 207 | /* See if this character is one of the delimiters */ |
| 208 | char *dp = delim; |
| 209 | while (*dp && (c != *dp)) |
| 210 | dp++; |
| 211 | if (*dp) |
| 212 | break; /* Found a good delimiter */ |
| 213 | } |
| 214 | return false; /* Malformatted number */ |
| 215 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 216 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 217 | *val = result; |
| 218 | if (es != (char **) 0) |
| 219 | { |
| 220 | *es = s; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 221 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 222 | return true; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | #endif |
| 226 | |
| 227 | #define USE_SPRINTF |
| 228 | #ifdef DEBUG |
| 229 | #ifndef USE_SPRINTF |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 230 | /* |
| 231 | * Note: this debug setup only works if the target platform has two serial ports |
| 232 | * available so that the other one (currently only port 1) can be used for debug |
| 233 | * messages. |
| 234 | */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 235 | static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 236 | zm_dprintf (char *fmt, ...) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 237 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 238 | int cur_console; |
| 239 | va_list args; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 240 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 241 | va_start (args, fmt); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 242 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 243 | cur_console = |
| 244 | CYGACC_CALL_IF_SET_CONSOLE_COMM |
| 245 | (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); |
| 246 | CYGACC_CALL_IF_SET_CONSOLE_COMM (1); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 247 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 248 | diag_vprintf (fmt, args); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 249 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 250 | CYGACC_CALL_IF_SET_CONSOLE_COMM (cur_console); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 251 | #endif |
| 252 | } |
| 253 | |
| 254 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 255 | zm_flush (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 256 | { |
| 257 | } |
| 258 | |
| 259 | #else |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 260 | /* |
| 261 | * Note: this debug setup works by storing the strings in a fixed buffer |
| 262 | */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 263 | #define FINAL |
| 264 | #ifdef FINAL |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 265 | static char *zm_out = (char *) 0x00380000; |
| 266 | static char *zm_out_start = (char *) 0x00380000; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 267 | #else |
| 268 | static char zm_buf[8192]; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 269 | static char *zm_out = zm_buf; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 270 | static char *zm_out_start = zm_buf; |
| 271 | |
| 272 | #endif |
| 273 | static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 274 | zm_dprintf (char *fmt, ...) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 275 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 276 | int len; |
| 277 | va_list args; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 278 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 279 | va_start (args, fmt); |
| 280 | len = diag_vsprintf (zm_out, fmt, args); |
| 281 | zm_out += len; |
| 282 | return len; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 286 | zm_flush (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 287 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 288 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 289 | char *p = zm_out_start; |
| 290 | while (*p) |
| 291 | mon_write_char (*p++); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 292 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 293 | zm_out = zm_out_start; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 294 | } |
| 295 | #endif |
| 296 | |
| 297 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 298 | zm_dump_buf (void *buf, int len) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 299 | { |
| 300 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 301 | diag_vdump_buf_with_offset (zm_dprintf, buf, len, 0); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 302 | #else |
| 303 | |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | static unsigned char zm_buf[2048]; |
| 308 | static unsigned char *zm_bp; |
| 309 | |
| 310 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 311 | zm_new (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 312 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 313 | zm_bp = zm_buf; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 317 | zm_save (unsigned char c) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 318 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 319 | *zm_bp++ = c; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 323 | zm_dump (int line) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 324 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 325 | zm_dprintf ("Packet at line: %d\n", line); |
| 326 | zm_dump_buf (zm_buf, zm_bp - zm_buf); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | #define ZM_DEBUG(x) x |
| 330 | #else |
| 331 | #define ZM_DEBUG(x) |
| 332 | #endif |
| 333 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 334 | /* Wait for the line to go idle */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 335 | static void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 336 | xyzModem_flush (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 337 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 338 | int res; |
| 339 | char c; |
| 340 | while (true) |
| 341 | { |
| 342 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 343 | if (!res) |
| 344 | return; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | static int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 349 | xyzModem_get_hdr (void) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 350 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 351 | char c; |
| 352 | int res; |
| 353 | bool hdr_found = false; |
| 354 | int i, can_total, hdr_chars; |
| 355 | unsigned short cksum; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 356 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 357 | ZM_DEBUG (zm_new ()); |
| 358 | /* Find the start of a header */ |
| 359 | can_total = 0; |
| 360 | hdr_chars = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 361 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 362 | if (xyz.tx_ack) |
| 363 | { |
| 364 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 365 | xyz.tx_ack = false; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 366 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 367 | while (!hdr_found) |
| 368 | { |
| 369 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 370 | ZM_DEBUG (zm_save (c)); |
| 371 | if (res) |
| 372 | { |
| 373 | hdr_chars++; |
| 374 | switch (c) |
| 375 | { |
| 376 | case SOH: |
| 377 | xyz.total_SOH++; |
| 378 | case STX: |
| 379 | if (c == STX) |
| 380 | xyz.total_STX++; |
| 381 | hdr_found = true; |
| 382 | break; |
| 383 | case CAN: |
| 384 | xyz.total_CAN++; |
| 385 | ZM_DEBUG (zm_dump (__LINE__)); |
| 386 | if (++can_total == xyzModem_CAN_COUNT) |
| 387 | { |
| 388 | return xyzModem_cancel; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | /* Wait for multiple CAN to avoid early quits */ |
| 393 | break; |
| 394 | } |
| 395 | case EOT: |
| 396 | /* EOT only supported if no noise */ |
| 397 | if (hdr_chars == 1) |
| 398 | { |
| 399 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 400 | ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__)); |
| 401 | ZM_DEBUG (zm_dump (__LINE__)); |
| 402 | return xyzModem_eof; |
| 403 | } |
| 404 | default: |
| 405 | /* Ignore, waiting for start of header */ |
| 406 | ; |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | /* Data stream timed out */ |
| 412 | xyzModem_flush (); /* Toss any current input */ |
| 413 | ZM_DEBUG (zm_dump (__LINE__)); |
| 414 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
| 415 | return xyzModem_timeout; |
| 416 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 419 | /* Header found, now read the data */ |
| 420 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk); |
| 421 | ZM_DEBUG (zm_save (xyz.blk)); |
| 422 | if (!res) |
| 423 | { |
| 424 | ZM_DEBUG (zm_dump (__LINE__)); |
| 425 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 426 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 427 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk); |
| 428 | ZM_DEBUG (zm_save (xyz.cblk)); |
| 429 | if (!res) |
| 430 | { |
| 431 | ZM_DEBUG (zm_dump (__LINE__)); |
| 432 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 433 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 434 | xyz.len = (c == SOH) ? 128 : 1024; |
| 435 | xyz.bufp = xyz.pkt; |
| 436 | for (i = 0; i < xyz.len; i++) |
| 437 | { |
| 438 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c); |
| 439 | ZM_DEBUG (zm_save (c)); |
| 440 | if (res) |
| 441 | { |
| 442 | xyz.pkt[i] = c; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | ZM_DEBUG (zm_dump (__LINE__)); |
| 447 | return xyzModem_timeout; |
| 448 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 449 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 450 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1); |
| 451 | ZM_DEBUG (zm_save (xyz.crc1)); |
| 452 | if (!res) |
| 453 | { |
| 454 | ZM_DEBUG (zm_dump (__LINE__)); |
| 455 | return xyzModem_timeout; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 456 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 457 | if (xyz.crc_mode) |
| 458 | { |
| 459 | res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2); |
| 460 | ZM_DEBUG (zm_save (xyz.crc2)); |
| 461 | if (!res) |
| 462 | { |
| 463 | ZM_DEBUG (zm_dump (__LINE__)); |
| 464 | return xyzModem_timeout; |
| 465 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 466 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 467 | ZM_DEBUG (zm_dump (__LINE__)); |
| 468 | /* Validate the message */ |
| 469 | if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF) |
| 470 | { |
| 471 | ZM_DEBUG (zm_dprintf |
| 472 | ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk, |
| 473 | (xyz.blk ^ xyz.cblk))); |
| 474 | ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len)); |
| 475 | xyzModem_flush (); |
| 476 | return xyzModem_frame; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 477 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 478 | /* Verify checksum/CRC */ |
| 479 | if (xyz.crc_mode) |
| 480 | { |
| 481 | cksum = cyg_crc16 (xyz.pkt, xyz.len); |
| 482 | if (cksum != ((xyz.crc1 << 8) | xyz.crc2)) |
| 483 | { |
| 484 | ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n", |
| 485 | xyz.crc1, xyz.crc2, cksum & 0xFFFF)); |
| 486 | return xyzModem_cksum; |
| 487 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 488 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 489 | else |
| 490 | { |
| 491 | cksum = 0; |
| 492 | for (i = 0; i < xyz.len; i++) |
| 493 | { |
| 494 | cksum += xyz.pkt[i]; |
| 495 | } |
| 496 | if (xyz.crc1 != (cksum & 0xFF)) |
| 497 | { |
| 498 | ZM_DEBUG (zm_dprintf |
| 499 | ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1, |
| 500 | cksum & 0xFF)); |
| 501 | return xyzModem_cksum; |
| 502 | } |
| 503 | } |
| 504 | /* If we get here, the message passes [structural] muster */ |
| 505 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 508 | int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 509 | xyzModem_stream_open (connection_info_t * info, int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 510 | { |
Stefan Roese | f90a392 | 2006-05-10 15:01:40 +0200 | [diff] [blame] | 511 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 512 | int console_chan; |
Stefan Roese | f90a392 | 2006-05-10 15:01:40 +0200 | [diff] [blame] | 513 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 514 | int stat = 0; |
| 515 | int retries = xyzModem_MAX_RETRIES; |
| 516 | int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 517 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 518 | /* ZM_DEBUG(zm_out = zm_out_start); */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 519 | #ifdef xyzModem_zmodem |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 520 | if (info->mode == xyzModem_zmodem) |
| 521 | { |
| 522 | *err = xyzModem_noZmodem; |
| 523 | return -1; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 524 | } |
| 525 | #endif |
| 526 | |
| 527 | #ifdef REDBOOT |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 528 | /* Set up the I/O channel. Note: this allows for using a different port in the future */ |
| 529 | console_chan = |
| 530 | CYGACC_CALL_IF_SET_CONSOLE_COMM |
| 531 | (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); |
| 532 | if (info->chan >= 0) |
| 533 | { |
| 534 | CYGACC_CALL_IF_SET_CONSOLE_COMM (info->chan); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 535 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 536 | else |
| 537 | { |
| 538 | CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan); |
| 539 | } |
| 540 | xyz.__chan = CYGACC_CALL_IF_CONSOLE_PROCS (); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 541 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 542 | CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan); |
| 543 | CYGACC_COMM_IF_CONTROL (*xyz.__chan, __COMMCTL_SET_TIMEOUT, |
| 544 | xyzModem_CHAR_TIMEOUT); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 545 | #else |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 546 | /* TODO: CHECK ! */ |
Kim Phillips | 2a2ed84 | 2009-06-15 11:50:40 -0500 | [diff] [blame] | 547 | int dummy = 0; |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 548 | xyz.__chan = &dummy; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 549 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 550 | xyz.len = 0; |
| 551 | xyz.crc_mode = true; |
| 552 | xyz.at_eof = false; |
| 553 | xyz.tx_ack = false; |
| 554 | xyz.mode = info->mode; |
| 555 | xyz.total_retries = 0; |
| 556 | xyz.total_SOH = 0; |
| 557 | xyz.total_STX = 0; |
| 558 | xyz.total_CAN = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 559 | #ifdef USE_YMODEM_LENGTH |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 560 | xyz.read_length = 0; |
| 561 | xyz.file_length = 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 562 | #endif |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 563 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 564 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 565 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 566 | if (xyz.mode == xyzModem_xmodem) |
| 567 | { |
| 568 | /* X-modem doesn't have an information header - exit here */ |
| 569 | xyz.next_blk = 1; |
| 570 | return 0; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 573 | while (retries-- > 0) |
| 574 | { |
| 575 | stat = xyzModem_get_hdr (); |
| 576 | if (stat == 0) |
| 577 | { |
| 578 | /* Y-modem file information header */ |
| 579 | if (xyz.blk == 0) |
| 580 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 581 | #ifdef USE_YMODEM_LENGTH |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 582 | /* skip filename */ |
| 583 | while (*xyz.bufp++); |
| 584 | /* get the length */ |
| 585 | parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " "); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 586 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 587 | /* The rest of the file name data block quietly discarded */ |
| 588 | xyz.tx_ack = true; |
| 589 | } |
| 590 | xyz.next_blk = 1; |
| 591 | xyz.len = 0; |
| 592 | return 0; |
| 593 | } |
| 594 | else if (stat == xyzModem_timeout) |
| 595 | { |
| 596 | if (--crc_retries <= 0) |
| 597 | xyz.crc_mode = false; |
| 598 | CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */ |
| 599 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
| 600 | xyz.total_retries++; |
| 601 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); |
| 602 | } |
| 603 | if (stat == xyzModem_cancel) |
| 604 | { |
| 605 | break; |
| 606 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 607 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 608 | *err = stat; |
| 609 | ZM_DEBUG (zm_flush ()); |
| 610 | return -1; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 613 | int |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 614 | xyzModem_stream_read (char *buf, int size, int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 615 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 616 | int stat, total, len; |
| 617 | int retries; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 618 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 619 | total = 0; |
| 620 | stat = xyzModem_cancel; |
| 621 | /* Try and get 'size' bytes into the buffer */ |
| 622 | while (!xyz.at_eof && (size > 0)) |
| 623 | { |
| 624 | if (xyz.len == 0) |
| 625 | { |
| 626 | retries = xyzModem_MAX_RETRIES; |
| 627 | while (retries-- > 0) |
| 628 | { |
| 629 | stat = xyzModem_get_hdr (); |
| 630 | if (stat == 0) |
| 631 | { |
| 632 | if (xyz.blk == xyz.next_blk) |
| 633 | { |
| 634 | xyz.tx_ack = true; |
| 635 | ZM_DEBUG (zm_dprintf |
| 636 | ("ACK block %d (%d)\n", xyz.blk, __LINE__)); |
| 637 | xyz.next_blk = (xyz.next_blk + 1) & 0xFF; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 638 | |
| 639 | #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH) |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 640 | if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) |
| 641 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 642 | #else |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 643 | if (1) |
| 644 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 645 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 646 | /* Data blocks can be padded with ^Z (EOF) characters */ |
| 647 | /* This code tries to detect and remove them */ |
| 648 | if ((xyz.bufp[xyz.len - 1] == EOF) && |
| 649 | (xyz.bufp[xyz.len - 2] == EOF) && |
| 650 | (xyz.bufp[xyz.len - 3] == EOF)) |
| 651 | { |
| 652 | while (xyz.len |
| 653 | && (xyz.bufp[xyz.len - 1] == EOF)) |
| 654 | { |
| 655 | xyz.len--; |
| 656 | } |
| 657 | } |
| 658 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 659 | |
| 660 | #ifdef USE_YMODEM_LENGTH |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 661 | /* |
| 662 | * See if accumulated length exceeds that of the file. |
| 663 | * If so, reduce size (i.e., cut out pad bytes) |
| 664 | * Only do this for Y-modem (and Z-modem should it ever |
| 665 | * be supported since it can fall back to Y-modem mode). |
| 666 | */ |
| 667 | if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length) |
| 668 | { |
| 669 | xyz.read_length += xyz.len; |
| 670 | if (xyz.read_length > xyz.file_length) |
| 671 | { |
| 672 | xyz.len -= (xyz.read_length - xyz.file_length); |
| 673 | } |
| 674 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 675 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF)) |
| 679 | { |
| 680 | /* Just re-ACK this so sender will get on with it */ |
| 681 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 682 | continue; /* Need new header */ |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | stat = xyzModem_sequence; |
| 687 | } |
| 688 | } |
| 689 | if (stat == xyzModem_cancel) |
| 690 | { |
| 691 | break; |
| 692 | } |
| 693 | if (stat == xyzModem_eof) |
| 694 | { |
| 695 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 696 | ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__)); |
| 697 | if (xyz.mode == xyzModem_ymodem) |
| 698 | { |
| 699 | CYGACC_COMM_IF_PUTC (*xyz.__chan, |
| 700 | (xyz.crc_mode ? 'C' : NAK)); |
| 701 | xyz.total_retries++; |
| 702 | ZM_DEBUG (zm_dprintf ("Reading Final Header\n")); |
| 703 | stat = xyzModem_get_hdr (); |
| 704 | CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK); |
| 705 | ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__)); |
| 706 | } |
| 707 | xyz.at_eof = true; |
| 708 | break; |
| 709 | } |
| 710 | CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK)); |
| 711 | xyz.total_retries++; |
| 712 | ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__)); |
| 713 | } |
| 714 | if (stat < 0) |
| 715 | { |
| 716 | *err = stat; |
| 717 | xyz.len = -1; |
| 718 | return total; |
| 719 | } |
| 720 | } |
| 721 | /* Don't "read" data from the EOF protocol package */ |
| 722 | if (!xyz.at_eof) |
| 723 | { |
| 724 | len = xyz.len; |
| 725 | if (size < len) |
| 726 | len = size; |
| 727 | memcpy (buf, xyz.bufp, len); |
| 728 | size -= len; |
| 729 | buf += len; |
| 730 | total += len; |
| 731 | xyz.len -= len; |
| 732 | xyz.bufp += len; |
| 733 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 734 | } |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 735 | return total; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | void |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 739 | xyzModem_stream_close (int *err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 740 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 741 | diag_printf |
| 742 | ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n", |
| 743 | xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX, |
| 744 | xyz.total_CAN, xyz.total_retries); |
| 745 | ZM_DEBUG (zm_flush ()); |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 748 | /* Need to be able to clean out the input buffer, so have to take the */ |
| 749 | /* getc */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 750 | void |
| 751 | xyzModem_stream_terminate (bool abort, int (*getc) (void)) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 752 | { |
| 753 | int c; |
| 754 | |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 755 | if (abort) |
| 756 | { |
| 757 | ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n")); |
| 758 | switch (xyz.mode) |
| 759 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 760 | case xyzModem_xmodem: |
| 761 | case xyzModem_ymodem: |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 762 | /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */ |
| 763 | /* number of Backspaces is a friendly way to get the other end to abort. */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 764 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); |
| 765 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); |
| 766 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); |
| 767 | CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN); |
| 768 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); |
| 769 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); |
| 770 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); |
| 771 | CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 772 | /* Now consume the rest of what's waiting on the line. */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 773 | ZM_DEBUG (zm_dprintf ("Flushing serial line.\n")); |
| 774 | xyzModem_flush (); |
| 775 | xyz.at_eof = true; |
| 776 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 777 | #ifdef xyzModem_zmodem |
| 778 | case xyzModem_zmodem: |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 779 | /* Might support it some day I suppose. */ |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 780 | #endif |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n")); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 787 | /* |
| 788 | * Consume any trailing crap left in the inbuffer from |
| 789 | * previous recieved blocks. Since very few files are an exact multiple |
| 790 | * of the transfer block size, there will almost always be some gunk here. |
| 791 | * If we don't eat it now, RedBoot will think the user typed it. |
| 792 | */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 793 | ZM_DEBUG (zm_dprintf ("Trailing gunk:\n")); |
| 794 | while ((c = (*getc) ()) > -1); |
| 795 | ZM_DEBUG (zm_dprintf ("\n")); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 796 | /* |
| 797 | * Make a small delay to give terminal programs like minicom |
| 798 | * time to get control again after their file transfer program |
| 799 | * exits. |
| 800 | */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 801 | CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000); |
| 802 | } |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | char * |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 806 | xyzModem_error (int err) |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 807 | { |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 808 | switch (err) |
| 809 | { |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 810 | case xyzModem_access: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 811 | return "Can't access file"; |
| 812 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 813 | case xyzModem_noZmodem: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 814 | return "Sorry, zModem not available yet"; |
| 815 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 816 | case xyzModem_timeout: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 817 | return "Timed out"; |
| 818 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 819 | case xyzModem_eof: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 820 | return "End of file"; |
| 821 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 822 | case xyzModem_cancel: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 823 | return "Cancelled"; |
| 824 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 825 | case xyzModem_frame: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 826 | return "Invalid framing"; |
| 827 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 828 | case xyzModem_cksum: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 829 | return "CRC/checksum error"; |
| 830 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 831 | case xyzModem_sequence: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 832 | return "Block sequence error"; |
| 833 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 834 | default: |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 835 | return "Unknown error"; |
| 836 | break; |
Markus Klotzbuecher | f2841d3 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 840 | /* |
| 841 | * RedBoot interface |
| 842 | */ |
Wolfgang Denk | 7d0432c | 2006-08-31 16:46:53 +0200 | [diff] [blame] | 843 | #if 0 /* SB */ |
| 844 | GETC_IO_FUNCS (xyzModem_io, xyzModem_stream_open, xyzModem_stream_close, |
| 845 | xyzModem_stream_terminate, xyzModem_stream_read, |
| 846 | xyzModem_error); |
| 847 | RedBoot_load (xmodem, xyzModem_io, false, false, xyzModem_xmodem); |
| 848 | RedBoot_load (ymodem, xyzModem_io, false, false, xyzModem_ymodem); |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 849 | #endif |