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