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