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