wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1 | /* |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2004 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Serial up- and download support |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 29 | #include <s_record.h> |
| 30 | #include <net.h> |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 31 | #include <exports.h> |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 32 | #include <xyzModem.h> |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 33 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 35 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 36 | #if defined(CONFIG_CMD_LOADB) |
Wolfgang Denk | da91693 | 2006-05-02 00:11:25 +0200 | [diff] [blame] | 37 | static ulong load_serial_ymodem (ulong offset); |
| 38 | #endif |
| 39 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 40 | #if defined(CONFIG_CMD_LOADS) |
Ricardo Ribalda Delgado | 9cde830 | 2008-07-30 12:39:29 +0200 | [diff] [blame] | 41 | static ulong load_serial (long offset); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 42 | static int read_record (char *buf, ulong len); |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 43 | # if defined(CONFIG_CMD_SAVES) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 44 | static int save_serial (ulong offset, ulong size); |
| 45 | static int write_record (char *buf); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 46 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 47 | |
| 48 | static int do_echo = 1; |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 49 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 50 | |
| 51 | /* -------------------------------------------------------------------- */ |
| 52 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 53 | #if defined(CONFIG_CMD_LOADS) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 54 | int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 55 | { |
Ricardo Ribalda Delgado | 9cde830 | 2008-07-30 12:39:29 +0200 | [diff] [blame] | 56 | long offset = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 57 | ulong addr; |
| 58 | int i; |
| 59 | char *env_echo; |
| 60 | int rcode = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 61 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 62 | int load_baudrate, current_baudrate; |
| 63 | |
| 64 | load_baudrate = current_baudrate = gd->baudrate; |
| 65 | #endif |
| 66 | |
| 67 | if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) { |
| 68 | do_echo = 1; |
| 69 | } else { |
| 70 | do_echo = 0; |
| 71 | } |
| 72 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | if (argc >= 2) { |
Ricardo Ribalda Delgado | 9cde830 | 2008-07-30 12:39:29 +0200 | [diff] [blame] | 75 | offset = simple_strtol(argv[1], NULL, 16); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | } |
| 77 | if (argc == 3) { |
| 78 | load_baudrate = (int)simple_strtoul(argv[2], NULL, 10); |
| 79 | |
| 80 | /* default to current baudrate */ |
| 81 | if (load_baudrate == 0) |
| 82 | load_baudrate = current_baudrate; |
| 83 | } |
| 84 | if (load_baudrate != current_baudrate) { |
| 85 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 86 | load_baudrate); |
| 87 | udelay(50000); |
| 88 | gd->baudrate = load_baudrate; |
| 89 | serial_setbrg (); |
| 90 | udelay(50000); |
| 91 | for (;;) { |
| 92 | if (getc() == '\r') |
| 93 | break; |
| 94 | } |
| 95 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 97 | if (argc == 2) { |
Ricardo Ribalda Delgado | 9cde830 | 2008-07-30 12:39:29 +0200 | [diff] [blame] | 98 | offset = simple_strtol(argv[1], NULL, 16); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 99 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
| 102 | printf ("## Ready for S-Record download ...\n"); |
| 103 | |
| 104 | addr = load_serial (offset); |
| 105 | |
| 106 | /* |
| 107 | * Gather any trailing characters (for instance, the ^D which |
| 108 | * is sent by 'cu' after sending a file), and give the |
| 109 | * box some time (100 * 1 ms) |
| 110 | */ |
| 111 | for (i=0; i<100; ++i) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 112 | if (tstc()) { |
| 113 | (void) getc(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | } |
| 115 | udelay(1000); |
| 116 | } |
| 117 | |
| 118 | if (addr == ~0) { |
| 119 | printf ("## S-Record download aborted\n"); |
| 120 | rcode = 1; |
| 121 | } else { |
| 122 | printf ("## Start Addr = 0x%08lX\n", addr); |
| 123 | load_addr = addr; |
| 124 | } |
| 125 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 126 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | if (load_baudrate != current_baudrate) { |
| 128 | printf ("## Switch baudrate to %d bps and press ESC ...\n", |
| 129 | current_baudrate); |
| 130 | udelay (50000); |
| 131 | gd->baudrate = current_baudrate; |
| 132 | serial_setbrg (); |
| 133 | udelay (50000); |
| 134 | for (;;) { |
| 135 | if (getc() == 0x1B) /* ESC */ |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | #endif |
| 140 | return rcode; |
| 141 | } |
| 142 | |
| 143 | static ulong |
Ricardo Ribalda Delgado | 9cde830 | 2008-07-30 12:39:29 +0200 | [diff] [blame] | 144 | load_serial (long offset) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 145 | { |
| 146 | char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */ |
| 147 | char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */ |
| 148 | int binlen; /* no. of data bytes in S-Rec. */ |
| 149 | int type; /* return code for record type */ |
| 150 | ulong addr; /* load address from S-Record */ |
| 151 | ulong size; /* number of bytes transferred */ |
| 152 | char buf[32]; |
| 153 | ulong store_addr; |
| 154 | ulong start_addr = ~0; |
| 155 | ulong end_addr = 0; |
| 156 | int line_count = 0; |
| 157 | |
| 158 | while (read_record(record, SREC_MAXRECLEN + 1) >= 0) { |
| 159 | type = srec_decode (record, &binlen, &addr, binbuf); |
| 160 | |
| 161 | if (type < 0) { |
| 162 | return (~0); /* Invalid S-Record */ |
| 163 | } |
| 164 | |
| 165 | switch (type) { |
| 166 | case SREC_DATA2: |
| 167 | case SREC_DATA3: |
| 168 | case SREC_DATA4: |
| 169 | store_addr = addr + offset; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 170 | #ifndef CONFIG_SYS_NO_FLASH |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | if (addr2info(store_addr)) { |
| 172 | int rc; |
| 173 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 174 | rc = flash_write((char *)binbuf,store_addr,binlen); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | if (rc != 0) { |
| 176 | flash_perror (rc); |
| 177 | return (~0); |
| 178 | } |
| 179 | } else |
| 180 | #endif |
| 181 | { |
| 182 | memcpy ((char *)(store_addr), binbuf, binlen); |
| 183 | } |
| 184 | if ((store_addr) < start_addr) |
| 185 | start_addr = store_addr; |
| 186 | if ((store_addr + binlen - 1) > end_addr) |
| 187 | end_addr = store_addr + binlen - 1; |
| 188 | break; |
| 189 | case SREC_END2: |
| 190 | case SREC_END3: |
| 191 | case SREC_END4: |
| 192 | udelay (10000); |
| 193 | size = end_addr - start_addr + 1; |
| 194 | printf ("\n" |
| 195 | "## First Load Addr = 0x%08lX\n" |
| 196 | "## Last Load Addr = 0x%08lX\n" |
| 197 | "## Total Size = 0x%08lX = %ld Bytes\n", |
| 198 | start_addr, end_addr, size, size |
| 199 | ); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 200 | flush_cache (start_addr, size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | sprintf(buf, "%lX", size); |
| 202 | setenv("filesize", buf); |
| 203 | return (addr); |
| 204 | case SREC_START: |
| 205 | break; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | if (!do_echo) { /* print a '.' every 100 lines */ |
| 210 | if ((++line_count % 100) == 0) |
| 211 | putc ('.'); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return (~0); /* Download aborted */ |
| 216 | } |
| 217 | |
| 218 | static int |
| 219 | read_record (char *buf, ulong len) |
| 220 | { |
| 221 | char *p; |
| 222 | char c; |
| 223 | |
| 224 | --len; /* always leave room for terminating '\0' byte */ |
| 225 | |
| 226 | for (p=buf; p < buf+len; ++p) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 227 | c = getc(); /* read character */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 228 | if (do_echo) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 229 | putc (c); /* ... and echo it */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 230 | |
| 231 | switch (c) { |
| 232 | case '\r': |
| 233 | case '\n': |
| 234 | *p = '\0'; |
| 235 | return (p - buf); |
| 236 | case '\0': |
| 237 | case 0x03: /* ^C - Control C */ |
| 238 | return (-1); |
| 239 | default: |
| 240 | *p = c; |
| 241 | } |
| 242 | |
| 243 | /* Check for the console hangup (if any different from serial) */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 244 | if (gd->jt[XF_getc] != getc) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 245 | if (ctrlc()) { |
| 246 | return (-1); |
| 247 | } |
| 248 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* line too long - truncate */ |
| 252 | *p = '\0'; |
| 253 | return (p - buf); |
| 254 | } |
| 255 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 256 | #if defined(CONFIG_CMD_SAVES) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 257 | |
| 258 | int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 259 | { |
| 260 | ulong offset = 0; |
| 261 | ulong size = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 262 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 263 | int save_baudrate, current_baudrate; |
| 264 | |
| 265 | save_baudrate = current_baudrate = gd->baudrate; |
| 266 | #endif |
| 267 | |
| 268 | if (argc >= 2) { |
| 269 | offset = simple_strtoul(argv[1], NULL, 16); |
| 270 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 272 | if (argc >= 3) { |
| 273 | size = simple_strtoul(argv[2], NULL, 16); |
| 274 | } |
| 275 | if (argc == 4) { |
| 276 | save_baudrate = (int)simple_strtoul(argv[3], NULL, 10); |
| 277 | |
| 278 | /* default to current baudrate */ |
| 279 | if (save_baudrate == 0) |
| 280 | save_baudrate = current_baudrate; |
| 281 | } |
| 282 | if (save_baudrate != current_baudrate) { |
| 283 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 284 | save_baudrate); |
| 285 | udelay(50000); |
| 286 | gd->baudrate = save_baudrate; |
| 287 | serial_setbrg (); |
| 288 | udelay(50000); |
| 289 | for (;;) { |
| 290 | if (getc() == '\r') |
| 291 | break; |
| 292 | } |
| 293 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 294 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 295 | if (argc == 3) { |
| 296 | size = simple_strtoul(argv[2], NULL, 16); |
| 297 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 298 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 299 | |
| 300 | printf ("## Ready for S-Record upload, press ENTER to proceed ...\n"); |
| 301 | for (;;) { |
| 302 | if (getc() == '\r') |
| 303 | break; |
| 304 | } |
| 305 | if(save_serial (offset, size)) { |
| 306 | printf ("## S-Record upload aborted\n"); |
| 307 | } else { |
| 308 | printf ("## S-Record upload complete\n"); |
| 309 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 310 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 311 | if (save_baudrate != current_baudrate) { |
| 312 | printf ("## Switch baudrate to %d bps and press ESC ...\n", |
| 313 | (int)current_baudrate); |
| 314 | udelay (50000); |
| 315 | gd->baudrate = current_baudrate; |
| 316 | serial_setbrg (); |
| 317 | udelay (50000); |
| 318 | for (;;) { |
| 319 | if (getc() == 0x1B) /* ESC */ |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | #endif |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | #define SREC3_START "S0030000FC\n" |
| 328 | #define SREC3_FORMAT "S3%02X%08lX%s%02X\n" |
| 329 | #define SREC3_END "S70500000000FA\n" |
| 330 | #define SREC_BYTES_PER_RECORD 16 |
| 331 | |
| 332 | static int save_serial (ulong address, ulong count) |
| 333 | { |
| 334 | int i, c, reclen, checksum, length; |
| 335 | char *hex = "0123456789ABCDEF"; |
| 336 | char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */ |
| 337 | char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */ |
| 338 | |
| 339 | reclen = 0; |
| 340 | checksum = 0; |
| 341 | |
| 342 | if(write_record(SREC3_START)) /* write the header */ |
| 343 | return (-1); |
| 344 | do { |
| 345 | if(count) { /* collect hex data in the buffer */ |
| 346 | c = *(volatile uchar*)(address + reclen); /* get one byte */ |
| 347 | checksum += c; /* accumulate checksum */ |
| 348 | data[2*reclen] = hex[(c>>4)&0x0f]; |
| 349 | data[2*reclen+1] = hex[c & 0x0f]; |
| 350 | data[2*reclen+2] = '\0'; |
| 351 | ++reclen; |
| 352 | --count; |
| 353 | } |
| 354 | if(reclen == SREC_BYTES_PER_RECORD || count == 0) { |
| 355 | /* enough data collected for one record: dump it */ |
| 356 | if(reclen) { /* build & write a data record: */ |
| 357 | /* address + data + checksum */ |
| 358 | length = 4 + reclen + 1; |
| 359 | |
| 360 | /* accumulate length bytes into checksum */ |
| 361 | for(i = 0; i < 2; i++) |
| 362 | checksum += (length >> (8*i)) & 0xff; |
| 363 | |
| 364 | /* accumulate address bytes into checksum: */ |
| 365 | for(i = 0; i < 4; i++) |
| 366 | checksum += (address >> (8*i)) & 0xff; |
| 367 | |
| 368 | /* make proper checksum byte: */ |
| 369 | checksum = ~checksum & 0xff; |
| 370 | |
| 371 | /* output one record: */ |
| 372 | sprintf(record, SREC3_FORMAT, length, address, data, checksum); |
| 373 | if(write_record(record)) |
| 374 | return (-1); |
| 375 | } |
| 376 | address += reclen; /* increment address */ |
| 377 | checksum = 0; |
| 378 | reclen = 0; |
| 379 | } |
| 380 | } |
| 381 | while(count); |
| 382 | if(write_record(SREC3_END)) /* write the final record */ |
| 383 | return (-1); |
| 384 | return(0); |
| 385 | } |
| 386 | |
| 387 | static int |
| 388 | write_record (char *buf) |
| 389 | { |
| 390 | char c; |
| 391 | |
| 392 | while((c = *buf++)) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 393 | putc(c); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 394 | |
| 395 | /* Check for the console hangup (if any different from serial) */ |
| 396 | |
| 397 | if (ctrlc()) { |
| 398 | return (-1); |
| 399 | } |
| 400 | return (0); |
| 401 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 402 | # endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 403 | |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 404 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 405 | |
| 406 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 407 | #if defined(CONFIG_CMD_LOADB) |
Jon Loeliger | 3de8b24 | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 408 | /* |
| 409 | * loadb command (load binary) included |
| 410 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 411 | #define XON_CHAR 17 |
| 412 | #define XOFF_CHAR 19 |
| 413 | #define START_CHAR 0x01 |
| 414 | #define ETX_CHAR 0x03 |
| 415 | #define END_CHAR 0x0D |
| 416 | #define SPACE 0x20 |
| 417 | #define K_ESCAPE 0x23 |
| 418 | #define SEND_TYPE 'S' |
| 419 | #define DATA_TYPE 'D' |
| 420 | #define ACK_TYPE 'Y' |
| 421 | #define NACK_TYPE 'N' |
| 422 | #define BREAK_TYPE 'B' |
| 423 | #define tochar(x) ((char) (((x) + SPACE) & 0xff)) |
| 424 | #define untochar(x) ((int) (((x) - SPACE) & 0xff)) |
| 425 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 426 | static void set_kerm_bin_mode(unsigned long *); |
| 427 | static int k_recv(void); |
| 428 | static ulong load_serial_bin (ulong offset); |
| 429 | |
| 430 | |
| 431 | char his_eol; /* character he needs at end of packet */ |
| 432 | int his_pad_count; /* number of pad chars he needs */ |
| 433 | char his_pad_char; /* pad chars he needs */ |
| 434 | char his_quote; /* quote chars he'll use */ |
| 435 | |
| 436 | int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 437 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 438 | ulong offset = 0; |
| 439 | ulong addr; |
| 440 | int load_baudrate, current_baudrate; |
| 441 | int rcode = 0; |
| 442 | char *s; |
| 443 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 444 | /* pre-set offset from CONFIG_SYS_LOAD_ADDR */ |
| 445 | offset = CONFIG_SYS_LOAD_ADDR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 446 | |
| 447 | /* pre-set offset from $loadaddr */ |
| 448 | if ((s = getenv("loadaddr")) != NULL) { |
| 449 | offset = simple_strtoul(s, NULL, 16); |
| 450 | } |
| 451 | |
| 452 | load_baudrate = current_baudrate = gd->baudrate; |
| 453 | |
| 454 | if (argc >= 2) { |
| 455 | offset = simple_strtoul(argv[1], NULL, 16); |
| 456 | } |
| 457 | if (argc == 3) { |
| 458 | load_baudrate = (int)simple_strtoul(argv[2], NULL, 10); |
| 459 | |
| 460 | /* default to current baudrate */ |
| 461 | if (load_baudrate == 0) |
| 462 | load_baudrate = current_baudrate; |
| 463 | } |
| 464 | |
| 465 | if (load_baudrate != current_baudrate) { |
| 466 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 467 | load_baudrate); |
| 468 | udelay(50000); |
| 469 | gd->baudrate = load_baudrate; |
| 470 | serial_setbrg (); |
| 471 | udelay(50000); |
| 472 | for (;;) { |
| 473 | if (getc() == '\r') |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 478 | if (strcmp(argv[0],"loady")==0) { |
| 479 | printf ("## Ready for binary (ymodem) download " |
| 480 | "to 0x%08lX at %d bps...\n", |
| 481 | offset, |
| 482 | load_baudrate); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 483 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 484 | addr = load_serial_ymodem (offset); |
| 485 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 486 | } else { |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 487 | |
| 488 | printf ("## Ready for binary (kermit) download " |
| 489 | "to 0x%08lX at %d bps...\n", |
| 490 | offset, |
| 491 | load_baudrate); |
| 492 | addr = load_serial_bin (offset); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 493 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 494 | if (addr == ~0) { |
| 495 | load_addr = 0; |
| 496 | printf ("## Binary (kermit) download aborted\n"); |
| 497 | rcode = 1; |
| 498 | } else { |
| 499 | printf ("## Start Addr = 0x%08lX\n", addr); |
| 500 | load_addr = addr; |
| 501 | } |
| 502 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 503 | if (load_baudrate != current_baudrate) { |
| 504 | printf ("## Switch baudrate to %d bps and press ESC ...\n", |
| 505 | current_baudrate); |
| 506 | udelay (50000); |
| 507 | gd->baudrate = current_baudrate; |
| 508 | serial_setbrg (); |
| 509 | udelay (50000); |
| 510 | for (;;) { |
| 511 | if (getc() == 0x1B) /* ESC */ |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
Wolfgang Denk | 85c25df | 2009-04-01 23:34:12 +0200 | [diff] [blame] | 516 | #ifdef CONFIG_SOURCE |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 517 | if (load_addr) { |
| 518 | char *s; |
| 519 | |
| 520 | if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) { |
Wolfgang Denk | 4f44a6b | 2009-04-04 16:57:50 +0200 | [diff] [blame] | 521 | printf ("Running \"source\" command at addr 0x%08lX", |
Wolfgang Denk | 85c25df | 2009-04-01 23:34:12 +0200 | [diff] [blame] | 522 | load_addr); |
Marian Balakowicz | 23b77a2 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 523 | |
| 524 | s = getenv ("autoscript_uname"); |
| 525 | if (s) |
| 526 | printf (":%s ...\n", s); |
| 527 | else |
| 528 | puts (" ...\n"); |
| 529 | |
Wolfgang Denk | 85c25df | 2009-04-01 23:34:12 +0200 | [diff] [blame] | 530 | rcode = source (load_addr, s); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | #endif |
| 534 | return rcode; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | static ulong load_serial_bin (ulong offset) |
| 539 | { |
| 540 | int size, i; |
| 541 | char buf[32]; |
| 542 | |
| 543 | set_kerm_bin_mode ((ulong *) offset); |
| 544 | size = k_recv (); |
| 545 | |
| 546 | /* |
| 547 | * Gather any trailing characters (for instance, the ^D which |
| 548 | * is sent by 'cu' after sending a file), and give the |
| 549 | * box some time (100 * 1 ms) |
| 550 | */ |
| 551 | for (i=0; i<100; ++i) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 552 | if (tstc()) { |
| 553 | (void) getc(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 554 | } |
| 555 | udelay(1000); |
| 556 | } |
| 557 | |
| 558 | flush_cache (offset, size); |
| 559 | |
| 560 | printf("## Total Size = 0x%08x = %d Bytes\n", size, size); |
| 561 | sprintf(buf, "%X", size); |
| 562 | setenv("filesize", buf); |
| 563 | |
| 564 | return offset; |
| 565 | } |
| 566 | |
| 567 | void send_pad (void) |
| 568 | { |
| 569 | int count = his_pad_count; |
| 570 | |
| 571 | while (count-- > 0) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 572 | putc (his_pad_char); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* converts escaped kermit char to binary char */ |
| 576 | char ktrans (char in) |
| 577 | { |
| 578 | if ((in & 0x60) == 0x40) { |
| 579 | return (char) (in & ~0x40); |
| 580 | } else if ((in & 0x7f) == 0x3f) { |
| 581 | return (char) (in | 0x40); |
| 582 | } else |
| 583 | return in; |
| 584 | } |
| 585 | |
| 586 | int chk1 (char *buffer) |
| 587 | { |
| 588 | int total = 0; |
| 589 | |
| 590 | while (*buffer) { |
| 591 | total += *buffer++; |
| 592 | } |
| 593 | return (int) ((total + ((total >> 6) & 0x03)) & 0x3f); |
| 594 | } |
| 595 | |
| 596 | void s1_sendpacket (char *packet) |
| 597 | { |
| 598 | send_pad (); |
| 599 | while (*packet) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 600 | putc (*packet++); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | static char a_b[24]; |
| 605 | void send_ack (int n) |
| 606 | { |
| 607 | a_b[0] = START_CHAR; |
| 608 | a_b[1] = tochar (3); |
| 609 | a_b[2] = tochar (n); |
| 610 | a_b[3] = ACK_TYPE; |
| 611 | a_b[4] = '\0'; |
| 612 | a_b[4] = tochar (chk1 (&a_b[1])); |
| 613 | a_b[5] = his_eol; |
| 614 | a_b[6] = '\0'; |
| 615 | s1_sendpacket (a_b); |
| 616 | } |
| 617 | |
| 618 | void send_nack (int n) |
| 619 | { |
| 620 | a_b[0] = START_CHAR; |
| 621 | a_b[1] = tochar (3); |
| 622 | a_b[2] = tochar (n); |
| 623 | a_b[3] = NACK_TYPE; |
| 624 | a_b[4] = '\0'; |
| 625 | a_b[4] = tochar (chk1 (&a_b[1])); |
| 626 | a_b[5] = his_eol; |
| 627 | a_b[6] = '\0'; |
| 628 | s1_sendpacket (a_b); |
| 629 | } |
| 630 | |
| 631 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 632 | void (*os_data_init) (void); |
| 633 | void (*os_data_char) (char new_char); |
| 634 | static int os_data_state, os_data_state_saved; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 635 | static char *os_data_addr, *os_data_addr_saved; |
| 636 | static char *bin_start_address; |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 637 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 638 | static void bin_data_init (void) |
| 639 | { |
| 640 | os_data_state = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 641 | os_data_addr = bin_start_address; |
| 642 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 643 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 644 | static void os_data_save (void) |
| 645 | { |
| 646 | os_data_state_saved = os_data_state; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 647 | os_data_addr_saved = os_data_addr; |
| 648 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 649 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 650 | static void os_data_restore (void) |
| 651 | { |
| 652 | os_data_state = os_data_state_saved; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 653 | os_data_addr = os_data_addr_saved; |
| 654 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 655 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 656 | static void bin_data_char (char new_char) |
| 657 | { |
| 658 | switch (os_data_state) { |
| 659 | case 0: /* data */ |
| 660 | *os_data_addr++ = new_char; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 664 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 665 | static void set_kerm_bin_mode (unsigned long *addr) |
| 666 | { |
| 667 | bin_start_address = (char *) addr; |
| 668 | os_data_init = bin_data_init; |
| 669 | os_data_char = bin_data_char; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | /* k_data_* simply handles the kermit escape translations */ |
| 674 | static int k_data_escape, k_data_escape_saved; |
| 675 | void k_data_init (void) |
| 676 | { |
| 677 | k_data_escape = 0; |
| 678 | os_data_init (); |
| 679 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 680 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 681 | void k_data_save (void) |
| 682 | { |
| 683 | k_data_escape_saved = k_data_escape; |
| 684 | os_data_save (); |
| 685 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 686 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 687 | void k_data_restore (void) |
| 688 | { |
| 689 | k_data_escape = k_data_escape_saved; |
| 690 | os_data_restore (); |
| 691 | } |
Gururaja Hebbar K R | b4d7eb2 | 2008-08-07 13:13:27 +0530 | [diff] [blame] | 692 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 693 | void k_data_char (char new_char) |
| 694 | { |
| 695 | if (k_data_escape) { |
| 696 | /* last char was escape - translate this character */ |
| 697 | os_data_char (ktrans (new_char)); |
| 698 | k_data_escape = 0; |
| 699 | } else { |
| 700 | if (new_char == his_quote) { |
| 701 | /* this char is escape - remember */ |
| 702 | k_data_escape = 1; |
| 703 | } else { |
| 704 | /* otherwise send this char as-is */ |
| 705 | os_data_char (new_char); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | #define SEND_DATA_SIZE 20 |
| 711 | char send_parms[SEND_DATA_SIZE]; |
| 712 | char *send_ptr; |
| 713 | |
| 714 | /* handle_send_packet interprits the protocol info and builds and |
| 715 | sends an appropriate ack for what we can do */ |
| 716 | void handle_send_packet (int n) |
| 717 | { |
| 718 | int length = 3; |
| 719 | int bytes; |
| 720 | |
| 721 | /* initialize some protocol parameters */ |
| 722 | his_eol = END_CHAR; /* default end of line character */ |
| 723 | his_pad_count = 0; |
| 724 | his_pad_char = '\0'; |
| 725 | his_quote = K_ESCAPE; |
| 726 | |
| 727 | /* ignore last character if it filled the buffer */ |
| 728 | if (send_ptr == &send_parms[SEND_DATA_SIZE - 1]) |
| 729 | --send_ptr; |
| 730 | bytes = send_ptr - send_parms; /* how many bytes we'll process */ |
| 731 | do { |
| 732 | if (bytes-- <= 0) |
| 733 | break; |
| 734 | /* handle MAXL - max length */ |
| 735 | /* ignore what he says - most I'll take (here) is 94 */ |
| 736 | a_b[++length] = tochar (94); |
| 737 | if (bytes-- <= 0) |
| 738 | break; |
| 739 | /* handle TIME - time you should wait for my packets */ |
| 740 | /* ignore what he says - don't wait for my ack longer than 1 second */ |
| 741 | a_b[++length] = tochar (1); |
| 742 | if (bytes-- <= 0) |
| 743 | break; |
| 744 | /* handle NPAD - number of pad chars I need */ |
| 745 | /* remember what he says - I need none */ |
| 746 | his_pad_count = untochar (send_parms[2]); |
| 747 | a_b[++length] = tochar (0); |
| 748 | if (bytes-- <= 0) |
| 749 | break; |
| 750 | /* handle PADC - pad chars I need */ |
| 751 | /* remember what he says - I need none */ |
| 752 | his_pad_char = ktrans (send_parms[3]); |
| 753 | a_b[++length] = 0x40; /* He should ignore this */ |
| 754 | if (bytes-- <= 0) |
| 755 | break; |
| 756 | /* handle EOL - end of line he needs */ |
| 757 | /* remember what he says - I need CR */ |
| 758 | his_eol = untochar (send_parms[4]); |
| 759 | a_b[++length] = tochar (END_CHAR); |
| 760 | if (bytes-- <= 0) |
| 761 | break; |
| 762 | /* handle QCTL - quote control char he'll use */ |
| 763 | /* remember what he says - I'll use '#' */ |
| 764 | his_quote = send_parms[5]; |
| 765 | a_b[++length] = '#'; |
| 766 | if (bytes-- <= 0) |
| 767 | break; |
| 768 | /* handle QBIN - 8-th bit prefixing */ |
| 769 | /* ignore what he says - I refuse */ |
| 770 | a_b[++length] = 'N'; |
| 771 | if (bytes-- <= 0) |
| 772 | break; |
| 773 | /* handle CHKT - the clock check type */ |
| 774 | /* ignore what he says - I do type 1 (for now) */ |
| 775 | a_b[++length] = '1'; |
| 776 | if (bytes-- <= 0) |
| 777 | break; |
| 778 | /* handle REPT - the repeat prefix */ |
| 779 | /* ignore what he says - I refuse (for now) */ |
| 780 | a_b[++length] = 'N'; |
| 781 | if (bytes-- <= 0) |
| 782 | break; |
| 783 | /* handle CAPAS - the capabilities mask */ |
| 784 | /* ignore what he says - I only do long packets - I don't do windows */ |
| 785 | a_b[++length] = tochar (2); /* only long packets */ |
| 786 | a_b[++length] = tochar (0); /* no windows */ |
| 787 | a_b[++length] = tochar (94); /* large packet msb */ |
| 788 | a_b[++length] = tochar (94); /* large packet lsb */ |
| 789 | } while (0); |
| 790 | |
| 791 | a_b[0] = START_CHAR; |
| 792 | a_b[1] = tochar (length); |
| 793 | a_b[2] = tochar (n); |
| 794 | a_b[3] = ACK_TYPE; |
| 795 | a_b[++length] = '\0'; |
| 796 | a_b[length] = tochar (chk1 (&a_b[1])); |
| 797 | a_b[++length] = his_eol; |
| 798 | a_b[++length] = '\0'; |
| 799 | s1_sendpacket (a_b); |
| 800 | } |
| 801 | |
| 802 | /* k_recv receives a OS Open image file over kermit line */ |
| 803 | static int k_recv (void) |
| 804 | { |
| 805 | char new_char; |
| 806 | char k_state, k_state_saved; |
| 807 | int sum; |
| 808 | int done; |
| 809 | int length; |
| 810 | int n, last_n; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 811 | int len_lo, len_hi; |
| 812 | |
| 813 | /* initialize some protocol parameters */ |
| 814 | his_eol = END_CHAR; /* default end of line character */ |
| 815 | his_pad_count = 0; |
| 816 | his_pad_char = '\0'; |
| 817 | his_quote = K_ESCAPE; |
| 818 | |
| 819 | /* initialize the k_recv and k_data state machine */ |
| 820 | done = 0; |
| 821 | k_state = 0; |
| 822 | k_data_init (); |
| 823 | k_state_saved = k_state; |
| 824 | k_data_save (); |
| 825 | n = 0; /* just to get rid of a warning */ |
| 826 | last_n = -1; |
| 827 | |
| 828 | /* expect this "type" sequence (but don't check): |
| 829 | S: send initiate |
| 830 | F: file header |
| 831 | D: data (multiple) |
| 832 | Z: end of file |
| 833 | B: break transmission |
| 834 | */ |
| 835 | |
| 836 | /* enter main loop */ |
| 837 | while (!done) { |
| 838 | /* set the send packet pointer to begining of send packet parms */ |
| 839 | send_ptr = send_parms; |
| 840 | |
| 841 | /* With each packet, start summing the bytes starting with the length. |
| 842 | Save the current sequence number. |
| 843 | Note the type of the packet. |
| 844 | If a character less than SPACE (0x20) is received - error. |
| 845 | */ |
| 846 | |
| 847 | #if 0 |
| 848 | /* OLD CODE, Prior to checking sequence numbers */ |
| 849 | /* first have all state machines save current states */ |
| 850 | k_state_saved = k_state; |
| 851 | k_data_save (); |
| 852 | #endif |
| 853 | |
| 854 | /* get a packet */ |
| 855 | /* wait for the starting character or ^C */ |
| 856 | for (;;) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 857 | switch (getc ()) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 858 | case START_CHAR: /* start packet */ |
| 859 | goto START; |
| 860 | case ETX_CHAR: /* ^C waiting for packet */ |
| 861 | return (0); |
| 862 | default: |
| 863 | ; |
| 864 | } |
| 865 | } |
| 866 | START: |
| 867 | /* get length of packet */ |
| 868 | sum = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 869 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 870 | if ((new_char & 0xE0) == 0) |
| 871 | goto packet_error; |
| 872 | sum += new_char & 0xff; |
| 873 | length = untochar (new_char); |
| 874 | /* get sequence number */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 875 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 876 | if ((new_char & 0xE0) == 0) |
| 877 | goto packet_error; |
| 878 | sum += new_char & 0xff; |
| 879 | n = untochar (new_char); |
| 880 | --length; |
| 881 | |
| 882 | /* NEW CODE - check sequence numbers for retried packets */ |
| 883 | /* Note - this new code assumes that the sequence number is correctly |
| 884 | * received. Handling an invalid sequence number adds another layer |
| 885 | * of complexity that may not be needed - yet! At this time, I'm hoping |
| 886 | * that I don't need to buffer the incoming data packets and can write |
| 887 | * the data into memory in real time. |
| 888 | */ |
| 889 | if (n == last_n) { |
| 890 | /* same sequence number, restore the previous state */ |
| 891 | k_state = k_state_saved; |
| 892 | k_data_restore (); |
| 893 | } else { |
| 894 | /* new sequence number, checkpoint the download */ |
| 895 | last_n = n; |
| 896 | k_state_saved = k_state; |
| 897 | k_data_save (); |
| 898 | } |
| 899 | /* END NEW CODE */ |
| 900 | |
| 901 | /* get packet type */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 902 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 903 | if ((new_char & 0xE0) == 0) |
| 904 | goto packet_error; |
| 905 | sum += new_char & 0xff; |
| 906 | k_state = new_char; |
| 907 | --length; |
| 908 | /* check for extended length */ |
| 909 | if (length == -2) { |
| 910 | /* (length byte was 0, decremented twice) */ |
| 911 | /* get the two length bytes */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 912 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 913 | if ((new_char & 0xE0) == 0) |
| 914 | goto packet_error; |
| 915 | sum += new_char & 0xff; |
| 916 | len_hi = untochar (new_char); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 917 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 918 | if ((new_char & 0xE0) == 0) |
| 919 | goto packet_error; |
| 920 | sum += new_char & 0xff; |
| 921 | len_lo = untochar (new_char); |
| 922 | length = len_hi * 95 + len_lo; |
| 923 | /* check header checksum */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 924 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 925 | if ((new_char & 0xE0) == 0) |
| 926 | goto packet_error; |
| 927 | if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f)) |
| 928 | goto packet_error; |
| 929 | sum += new_char & 0xff; |
| 930 | /* --length; */ /* new length includes only data and block check to come */ |
| 931 | } |
| 932 | /* bring in rest of packet */ |
| 933 | while (length > 1) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 934 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 935 | if ((new_char & 0xE0) == 0) |
| 936 | goto packet_error; |
| 937 | sum += new_char & 0xff; |
| 938 | --length; |
| 939 | if (k_state == DATA_TYPE) { |
| 940 | /* pass on the data if this is a data packet */ |
| 941 | k_data_char (new_char); |
| 942 | } else if (k_state == SEND_TYPE) { |
| 943 | /* save send pack in buffer as is */ |
| 944 | *send_ptr++ = new_char; |
| 945 | /* if too much data, back off the pointer */ |
| 946 | if (send_ptr >= &send_parms[SEND_DATA_SIZE]) |
| 947 | --send_ptr; |
| 948 | } |
| 949 | } |
| 950 | /* get and validate checksum character */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 951 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 952 | if ((new_char & 0xE0) == 0) |
| 953 | goto packet_error; |
| 954 | if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f)) |
| 955 | goto packet_error; |
| 956 | /* get END_CHAR */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 957 | new_char = getc (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 958 | if (new_char != END_CHAR) { |
| 959 | packet_error: |
| 960 | /* restore state machines */ |
| 961 | k_state = k_state_saved; |
| 962 | k_data_restore (); |
| 963 | /* send a negative acknowledge packet in */ |
| 964 | send_nack (n); |
| 965 | } else if (k_state == SEND_TYPE) { |
| 966 | /* crack the protocol parms, build an appropriate ack packet */ |
| 967 | handle_send_packet (n); |
| 968 | } else { |
| 969 | /* send simple acknowledge packet in */ |
| 970 | send_ack (n); |
| 971 | /* quit if end of transmission */ |
| 972 | if (k_state == BREAK_TYPE) |
| 973 | done = 1; |
| 974 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 975 | } |
| 976 | return ((ulong) os_data_addr - (ulong) bin_start_address); |
| 977 | } |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 978 | |
| 979 | static int getcxmodem(void) { |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 980 | if (tstc()) |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 981 | return (getc()); |
| 982 | return -1; |
| 983 | } |
| 984 | static ulong load_serial_ymodem (ulong offset) |
| 985 | { |
| 986 | int size; |
| 987 | char buf[32]; |
| 988 | int err; |
| 989 | int res; |
| 990 | connection_info_t info; |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 991 | char ymodemBuf[1024]; |
| 992 | ulong store_addr = ~0; |
| 993 | ulong addr = 0; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 994 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 995 | size = 0; |
| 996 | info.mode = xyzModem_ymodem; |
| 997 | res = xyzModem_stream_open (&info, &err); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 998 | if (!res) { |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 999 | |
| 1000 | while ((res = |
| 1001 | xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) { |
| 1002 | store_addr = addr + offset; |
| 1003 | size += res; |
| 1004 | addr += res; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1005 | #ifndef CONFIG_SYS_NO_FLASH |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1006 | if (addr2info (store_addr)) { |
| 1007 | int rc; |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1008 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1009 | rc = flash_write ((char *) ymodemBuf, |
| 1010 | store_addr, res); |
| 1011 | if (rc != 0) { |
| 1012 | flash_perror (rc); |
| 1013 | return (~0); |
| 1014 | } |
| 1015 | } else |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1016 | #endif |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1017 | { |
| 1018 | memcpy ((char *) (store_addr), ymodemBuf, |
| 1019 | res); |
| 1020 | } |
| 1021 | |
| 1022 | } |
| 1023 | } else { |
| 1024 | printf ("%s\n", xyzModem_error (err)); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1025 | } |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1026 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1027 | xyzModem_stream_close (&err); |
| 1028 | xyzModem_stream_terminate (false, &getcxmodem); |
| 1029 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1030 | |
| 1031 | flush_cache (offset, size); |
| 1032 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 1033 | printf ("## Total Size = 0x%08x = %d Bytes\n", size, size); |
| 1034 | sprintf (buf, "%X", size); |
| 1035 | setenv ("filesize", buf); |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1036 | |
| 1037 | return offset; |
| 1038 | } |
| 1039 | |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1040 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* -------------------------------------------------------------------- */ |
| 1043 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1044 | #if defined(CONFIG_CMD_LOADS) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1045 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1046 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1047 | U_BOOT_CMD( |
| 1048 | loads, 3, 0, do_load_serial, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1049 | "load S-Record file over serial line", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1050 | "[ off ] [ baud ]\n" |
| 1051 | " - load S-Record file over serial line" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1052 | " with offset 'off' and baudrate 'baud'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1053 | ); |
| 1054 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1055 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1056 | U_BOOT_CMD( |
| 1057 | loads, 2, 0, do_load_serial, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1058 | "load S-Record file over serial line", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1059 | "[ off ]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1060 | " - load S-Record file over serial line with offset 'off'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1061 | ); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1062 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * SAVES always requires LOADS support, but not vice versa |
| 1066 | */ |
| 1067 | |
| 1068 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1069 | #if defined(CONFIG_CMD_SAVES) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1070 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1071 | U_BOOT_CMD( |
| 1072 | saves, 4, 0, do_save_serial, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1073 | "save S-Record file over serial line", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1074 | "[ off ] [size] [ baud ]\n" |
| 1075 | " - save S-Record file over serial line" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1076 | " with offset 'off', size 'size' and baudrate 'baud'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1077 | ); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1078 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1079 | U_BOOT_CMD( |
| 1080 | saves, 3, 0, do_save_serial, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1081 | "save S-Record file over serial line", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1082 | "[ off ] [size]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1083 | " - save S-Record file over serial line with offset 'off' and size 'size'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1084 | ); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1085 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1086 | #endif |
| 1087 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1088 | |
| 1089 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1090 | #if defined(CONFIG_CMD_LOADB) |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1091 | U_BOOT_CMD( |
| 1092 | loadb, 3, 0, do_load_serial_bin, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1093 | "load binary file over serial line (kermit mode)", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1094 | "[ off ] [ baud ]\n" |
| 1095 | " - load binary file over serial line" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1096 | " with offset 'off' and baudrate 'baud'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1097 | ); |
| 1098 | |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1099 | U_BOOT_CMD( |
| 1100 | loady, 3, 0, do_load_serial_bin, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1101 | "load binary file over serial line (ymodem mode)", |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1102 | "[ off ] [ baud ]\n" |
| 1103 | " - load binary file over serial line" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1104 | " with offset 'off' and baudrate 'baud'" |
Markus Klotzbuecher | 387f541 | 2006-03-30 13:40:55 +0200 | [diff] [blame] | 1105 | ); |
| 1106 | |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1107 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* -------------------------------------------------------------------- */ |
| 1110 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1111 | #if defined(CONFIG_CMD_HWFLOW) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1112 | int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 1113 | { |
| 1114 | extern int hwflow_onoff(int); |
| 1115 | |
| 1116 | if (argc == 2) { |
| 1117 | if (strcmp(argv[1], "off") == 0) |
| 1118 | hwflow_onoff(-1); |
| 1119 | else |
| 1120 | if (strcmp(argv[1], "on") == 0) |
| 1121 | hwflow_onoff(1); |
| 1122 | else |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 1123 | cmd_usage(cmdtp); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1124 | } |
| 1125 | printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off"); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | /* -------------------------------------------------------------------- */ |
| 1130 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1131 | U_BOOT_CMD( |
wdenk | 6914128 | 2003-07-07 20:07:54 +0000 | [diff] [blame] | 1132 | hwflow, 2, 0, do_hwflow, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1133 | "turn RTS/CTS hardware flow control in serial line on/off", |
| 1134 | "[on|off]" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1135 | ); |
| 1136 | |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1137 | #endif |