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