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