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