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