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