blob: 540361b43f02e09fdb9ec4d5a89ca37029279800 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk57b2d802003-06-27 21:31:46 +00002/*
wdenk29e7f5a2004-03-12 00:14:09 +00003 * (C) Copyright 2000-2004
wdenk57b2d802003-06-27 21:31:46 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk57b2d802003-06-27 21:31:46 +00005 */
6
7/*
8 * Serial up- and download support
9 */
Tom Riniabb9a042024-05-18 20:20:43 -060010#include <common.h>
wdenk57b2d802003-06-27 21:31:46 +000011#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000014#include <efi_loader.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000016#include <exports.h>
Tom Rini063c9382022-07-23 13:05:03 -040017#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa606ffc2019-12-28 10:44:40 -070018#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040019#endif
Simon Glass85f13782019-12-28 10:45:03 -070020#include <image.h>
Marek Vasut4d2be382021-10-10 23:52:41 +020021#include <lmb.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000022#include <mapmem.h>
wdenk57b2d802003-06-27 21:31:46 +000023#include <net.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000024#include <s_record.h>
Simon Glass36736182019-11-14 12:57:24 -070025#include <serial.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020026#include <xyzModem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060027#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060028#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
wdenk57b2d802003-06-27 21:31:46 +000030
Wolfgang Denk6405a152006-03-31 18:32:53 +020031DECLARE_GLOBAL_DATA_PTR;
wdenk57b2d802003-06-27 21:31:46 +000032
Jon Loeligerd76b5c12007-07-08 18:02:23 -050033#if defined(CONFIG_CMD_LOADB)
Angus Ainslief283bd02013-06-26 16:54:24 -060034static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denkda916932006-05-02 00:11:25 +020035#endif
36
Jon Loeligerd76b5c12007-07-08 18:02:23 -050037#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000038static ulong load_serial(long offset);
39static int read_record(char *buf, ulong len);
Jon Loeligerd76b5c12007-07-08 18:02:23 -050040# if defined(CONFIG_CMD_SAVES)
Kim Phillipsdc00a682012-10-29 13:34:31 +000041static int save_serial(ulong offset, ulong size);
42static int write_record(char *buf);
Jon Loeligerd704d912007-07-10 11:02:44 -050043#endif
wdenk57b2d802003-06-27 21:31:46 +000044
45static int do_echo = 1;
Jon Loeligerd704d912007-07-10 11:02:44 -050046#endif
wdenk57b2d802003-06-27 21:31:46 +000047
48/* -------------------------------------------------------------------- */
49
Jon Loeligerd76b5c12007-07-08 18:02:23 -050050#if defined(CONFIG_CMD_LOADS)
Simon Glassed38aef2020-05-10 11:40:03 -060051static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
52 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +000053{
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020054 long offset = 0;
wdenk57b2d802003-06-27 21:31:46 +000055 ulong addr;
56 int i;
57 char *env_echo;
58 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020059#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000060 int load_baudrate, current_baudrate;
61
62 load_baudrate = current_baudrate = gd->baudrate;
63#endif
64
Simon Glass64b723f2017-08-03 12:22:12 -060065 env_echo = env_get("loads_echo");
66 if (env_echo && *env_echo == '1')
wdenk57b2d802003-06-27 21:31:46 +000067 do_echo = 1;
Simon Glass64b723f2017-08-03 12:22:12 -060068 else
wdenk57b2d802003-06-27 21:31:46 +000069 do_echo = 0;
wdenk57b2d802003-06-27 21:31:46 +000070
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000072 if (argc >= 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020073 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000074 }
75 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -060076 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +000077
78 /* default to current baudrate */
79 if (load_baudrate == 0)
80 load_baudrate = current_baudrate;
81 }
82 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +000083 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +000084 load_baudrate);
85 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +020086 flush();
wdenk57b2d802003-06-27 21:31:46 +000087 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +000088 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +000089 udelay(50000);
90 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020091 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +000092 break;
93 }
94 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000096 if (argc == 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020097 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000098 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000100
Kim Phillipsdc00a682012-10-29 13:34:31 +0000101 printf("## Ready for S-Record download ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000102
Kim Phillipsdc00a682012-10-29 13:34:31 +0000103 addr = load_serial(offset);
wdenk57b2d802003-06-27 21:31:46 +0000104
105 /*
106 * Gather any trailing characters (for instance, the ^D which
107 * is sent by 'cu' after sending a file), and give the
108 * box some time (100 * 1 ms)
109 */
110 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000111 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200112 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000113 }
114 udelay(1000);
115 }
116
117 if (addr == ~0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000118 printf("## S-Record download aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000119 rcode = 1;
120 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000121 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700122 image_load_addr = addr;
wdenk57b2d802003-06-27 21:31:46 +0000123 }
124
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200125#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000126 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000127 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000128 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000129 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200130 flush();
wdenk57b2d802003-06-27 21:31:46 +0000131 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000132 serial_setbrg();
133 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000134 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200135 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000136 break;
137 }
138 }
139#endif
140 return rcode;
141}
142
Kim Phillipsdc00a682012-10-29 13:34:31 +0000143static ulong load_serial(long offset)
wdenk57b2d802003-06-27 21:31:46 +0000144{
Marek Vasut4d2be382021-10-10 23:52:41 +0200145 struct lmb lmb;
wdenk57b2d802003-06-27 21:31:46 +0000146 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
147 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
148 int binlen; /* no. of data bytes in S-Rec. */
149 int type; /* return code for record type */
150 ulong addr; /* load address from S-Record */
151 ulong size; /* number of bytes transferred */
wdenk57b2d802003-06-27 21:31:46 +0000152 ulong store_addr;
153 ulong start_addr = ~0;
154 ulong end_addr = 0;
155 int line_count = 0;
Marek Vasut4d2be382021-10-10 23:52:41 +0200156 long ret;
157
158 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
wdenk57b2d802003-06-27 21:31:46 +0000159
160 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000161 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk57b2d802003-06-27 21:31:46 +0000162
163 if (type < 0) {
164 return (~0); /* Invalid S-Record */
165 }
166
167 switch (type) {
168 case SREC_DATA2:
169 case SREC_DATA3:
170 case SREC_DATA4:
171 store_addr = addr + offset;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900172#ifdef CONFIG_MTD_NOR_FLASH
wdenk57b2d802003-06-27 21:31:46 +0000173 if (addr2info(store_addr)) {
174 int rc;
175
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200176 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk57b2d802003-06-27 21:31:46 +0000177 if (rc != 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000178 flash_perror(rc);
wdenk57b2d802003-06-27 21:31:46 +0000179 return (~0);
180 }
181 } else
182#endif
183 {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200184 void *dst;
185
Marek Vasut4d2be382021-10-10 23:52:41 +0200186 ret = lmb_reserve(&lmb, store_addr, binlen);
187 if (ret) {
188 printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
189 store_addr, store_addr + binlen);
190 return ret;
191 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200192 dst = map_sysmem(store_addr, binlen);
193 memcpy(dst, binbuf, binlen);
194 unmap_sysmem(dst);
Marek Vasut4d2be382021-10-10 23:52:41 +0200195 lmb_free(&lmb, store_addr, binlen);
wdenk57b2d802003-06-27 21:31:46 +0000196 }
197 if ((store_addr) < start_addr)
198 start_addr = store_addr;
199 if ((store_addr + binlen - 1) > end_addr)
200 end_addr = store_addr + binlen - 1;
201 break;
202 case SREC_END2:
203 case SREC_END3:
204 case SREC_END4:
Kim Phillipsdc00a682012-10-29 13:34:31 +0000205 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000206 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000207 printf("\n"
wdenk57b2d802003-06-27 21:31:46 +0000208 "## First Load Addr = 0x%08lX\n"
209 "## Last Load Addr = 0x%08lX\n"
210 "## Total Size = 0x%08lX = %ld Bytes\n",
211 start_addr, end_addr, size, size
212 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000213 flush_cache(start_addr, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600214 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000215 return (addr);
216 case SREC_START:
217 break;
218 default:
219 break;
220 }
221 if (!do_echo) { /* print a '.' every 100 lines */
222 if ((++line_count % 100) == 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000223 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000224 }
225 }
226
227 return (~0); /* Download aborted */
228}
229
Kim Phillipsdc00a682012-10-29 13:34:31 +0000230static int read_record(char *buf, ulong len)
wdenk57b2d802003-06-27 21:31:46 +0000231{
232 char *p;
Tom Rini479f54a2024-01-09 17:57:16 -0500233 int c;
wdenk57b2d802003-06-27 21:31:46 +0000234
235 --len; /* always leave room for terminating '\0' byte */
236
237 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200238 c = getchar(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000239 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000240 putc(c); /* ... and echo it */
wdenk57b2d802003-06-27 21:31:46 +0000241
242 switch (c) {
243 case '\r':
244 case '\n':
245 *p = '\0';
246 return (p - buf);
247 case '\0':
248 case 0x03: /* ^C - Control C */
249 return (-1);
250 default:
251 *p = c;
252 }
253
Michal Simekb6e8ba12020-12-01 13:58:28 +0100254 /* Check for the console hangup (if any different from serial) */
255 if (gd->jt->getc != getchar) {
256 if (ctrlc())
257 return (-1);
wdenk57b2d802003-06-27 21:31:46 +0000258 }
wdenk57b2d802003-06-27 21:31:46 +0000259 }
260
261 /* line too long - truncate */
262 *p = '\0';
263 return (p - buf);
264}
265
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500266#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000267
Simon Glassed38aef2020-05-10 11:40:03 -0600268int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
269 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000270{
271 ulong offset = 0;
272 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200273#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000274 int save_baudrate, current_baudrate;
275
276 save_baudrate = current_baudrate = gd->baudrate;
277#endif
278
279 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600280 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000281 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200282#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000283 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600284 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000285 }
286 if (argc == 4) {
Simon Glassff9b9032021-07-24 09:03:30 -0600287 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000288
289 /* default to current baudrate */
290 if (save_baudrate == 0)
291 save_baudrate = current_baudrate;
292 }
293 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000294 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000295 save_baudrate);
296 udelay(50000);
297 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000298 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000299 udelay(50000);
300 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200301 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000302 break;
303 }
304 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200305#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000306 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600307 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000308 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200309#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000310
Kim Phillipsdc00a682012-10-29 13:34:31 +0000311 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000312 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200313 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000314 break;
315 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000316 if (save_serial(offset, size)) {
317 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000318 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000319 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000320 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200321#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000322 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000323 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000324 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000325 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200326 flush();
wdenk57b2d802003-06-27 21:31:46 +0000327 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000328 serial_setbrg();
329 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000330 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200331 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000332 break;
333 }
334 }
335#endif
336 return 0;
337}
338
339#define SREC3_START "S0030000FC\n"
340#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
341#define SREC3_END "S70500000000FA\n"
342#define SREC_BYTES_PER_RECORD 16
343
Kim Phillipsdc00a682012-10-29 13:34:31 +0000344static int save_serial(ulong address, ulong count)
wdenk57b2d802003-06-27 21:31:46 +0000345{
346 int i, c, reclen, checksum, length;
347 char *hex = "0123456789ABCDEF";
348 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
349 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
350
351 reclen = 0;
352 checksum = 0;
353
354 if(write_record(SREC3_START)) /* write the header */
355 return (-1);
356 do {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200357 volatile uchar *src;
358
359 src = map_sysmem(address, count);
360 if (count) { /* collect hex data in the buffer */
361 c = src[reclen]; /* get one byte */
362 checksum += c; /* accumulate checksum */
wdenk57b2d802003-06-27 21:31:46 +0000363 data[2*reclen] = hex[(c>>4)&0x0f];
364 data[2*reclen+1] = hex[c & 0x0f];
365 data[2*reclen+2] = '\0';
366 ++reclen;
367 --count;
368 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200369 unmap_sysmem((void *)src);
wdenk57b2d802003-06-27 21:31:46 +0000370 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
371 /* enough data collected for one record: dump it */
372 if(reclen) { /* build & write a data record: */
373 /* address + data + checksum */
374 length = 4 + reclen + 1;
375
376 /* accumulate length bytes into checksum */
377 for(i = 0; i < 2; i++)
378 checksum += (length >> (8*i)) & 0xff;
379
380 /* accumulate address bytes into checksum: */
381 for(i = 0; i < 4; i++)
382 checksum += (address >> (8*i)) & 0xff;
383
384 /* make proper checksum byte: */
385 checksum = ~checksum & 0xff;
386
387 /* output one record: */
388 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
389 if(write_record(record))
390 return (-1);
391 }
392 address += reclen; /* increment address */
393 checksum = 0;
394 reclen = 0;
395 }
396 }
397 while(count);
398 if(write_record(SREC3_END)) /* write the final record */
399 return (-1);
400 return(0);
401}
402
Kim Phillipsdc00a682012-10-29 13:34:31 +0000403static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000404{
405 char c;
406
407 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000408 putc(c);
wdenk57b2d802003-06-27 21:31:46 +0000409
410 /* Check for the console hangup (if any different from serial) */
411
412 if (ctrlc()) {
413 return (-1);
414 }
415 return (0);
416}
Jon Loeligerd704d912007-07-10 11:02:44 -0500417# endif
wdenk57b2d802003-06-27 21:31:46 +0000418
Jon Loeligerd704d912007-07-10 11:02:44 -0500419#endif
wdenk57b2d802003-06-27 21:31:46 +0000420
421
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500422#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500423/*
424 * loadb command (load binary) included
425 */
wdenk57b2d802003-06-27 21:31:46 +0000426#define XON_CHAR 17
427#define XOFF_CHAR 19
428#define START_CHAR 0x01
429#define ETX_CHAR 0x03
430#define END_CHAR 0x0D
431#define SPACE 0x20
432#define K_ESCAPE 0x23
433#define SEND_TYPE 'S'
434#define DATA_TYPE 'D'
435#define ACK_TYPE 'Y'
436#define NACK_TYPE 'N'
437#define BREAK_TYPE 'B'
438#define tochar(x) ((char) (((x) + SPACE) & 0xff))
439#define untochar(x) ((int) (((x) - SPACE) & 0xff))
440
wdenk57b2d802003-06-27 21:31:46 +0000441static void set_kerm_bin_mode(unsigned long *);
442static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000443static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000444
445
Kim Phillipsdc00a682012-10-29 13:34:31 +0000446static char his_eol; /* character he needs at end of packet */
447static int his_pad_count; /* number of pad chars he needs */
448static char his_pad_char; /* pad chars he needs */
449static char his_quote; /* quote chars he'll use */
wdenk57b2d802003-06-27 21:31:46 +0000450
Simon Glassed38aef2020-05-10 11:40:03 -0600451static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
452 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000453{
wdenk57b2d802003-06-27 21:31:46 +0000454 ulong offset = 0;
455 ulong addr;
456 int load_baudrate, current_baudrate;
457 int rcode = 0;
458 char *s;
459
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200460 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
461 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000462
463 /* pre-set offset from $loadaddr */
Simon Glass64b723f2017-08-03 12:22:12 -0600464 s = env_get("loadaddr");
465 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600466 offset = hextoul(s, NULL);
wdenk57b2d802003-06-27 21:31:46 +0000467
468 load_baudrate = current_baudrate = gd->baudrate;
469
470 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600471 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000472 }
473 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600474 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000475
476 /* default to current baudrate */
477 if (load_baudrate == 0)
478 load_baudrate = current_baudrate;
479 }
480
481 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000482 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000483 load_baudrate);
484 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200485 flush();
wdenk57b2d802003-06-27 21:31:46 +0000486 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000487 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000488 udelay(50000);
489 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200490 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000491 break;
492 }
493 }
494
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200495 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000496 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200497 "to 0x%08lX at %d bps...\n",
498 offset,
499 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000500
Angus Ainslief283bd02013-06-26 16:54:24 -0600501 addr = load_serial_ymodem(offset, xyzModem_ymodem);
502
Pali Roháre37155f2021-08-03 16:28:43 +0200503 if (addr == ~0) {
504 image_load_addr = 0;
505 printf("## Binary (ymodem) download aborted\n");
506 rcode = 1;
507 } else {
508 printf("## Start Addr = 0x%08lX\n", addr);
509 image_load_addr = addr;
510 }
Angus Ainslief283bd02013-06-26 16:54:24 -0600511 } else if (strcmp(argv[0],"loadx")==0) {
512 printf("## Ready for binary (xmodem) download "
513 "to 0x%08lX at %d bps...\n",
514 offset,
515 load_baudrate);
516
517 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200518
Pali Roháre37155f2021-08-03 16:28:43 +0200519 if (addr == ~0) {
520 image_load_addr = 0;
521 printf("## Binary (xmodem) download aborted\n");
522 rcode = 1;
523 } else {
524 printf("## Start Addr = 0x%08lX\n", addr);
525 image_load_addr = addr;
526 }
wdenk57b2d802003-06-27 21:31:46 +0000527 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200528
Kim Phillipsdc00a682012-10-29 13:34:31 +0000529 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200530 "to 0x%08lX at %d bps...\n",
531 offset,
532 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000533 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000534
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200535 if (addr == ~0) {
Simon Glass892265d2019-12-28 10:45:02 -0700536 image_load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000537 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200538 rcode = 1;
539 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000540 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700541 image_load_addr = addr;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200542 }
543 }
wdenk57b2d802003-06-27 21:31:46 +0000544 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000545 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000546 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000547 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200548 flush();
wdenk57b2d802003-06-27 21:31:46 +0000549 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000550 serial_setbrg();
551 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000552 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200553 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000554 break;
555 }
556 }
557
wdenk57b2d802003-06-27 21:31:46 +0000558 return rcode;
559}
560
561
Kim Phillipsdc00a682012-10-29 13:34:31 +0000562static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000563{
564 int size, i;
wdenk57b2d802003-06-27 21:31:46 +0000565
Kim Phillipsdc00a682012-10-29 13:34:31 +0000566 set_kerm_bin_mode((ulong *) offset);
567 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000568
569 /*
570 * Gather any trailing characters (for instance, the ^D which
571 * is sent by 'cu' after sending a file), and give the
572 * box some time (100 * 1 ms)
573 */
574 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000575 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200576 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000577 }
578 udelay(1000);
579 }
580
Pali Rohár8398dd32021-08-06 18:07:39 +0200581 if (size == 0)
582 return ~0; /* Download aborted */
583
Kim Phillipsdc00a682012-10-29 13:34:31 +0000584 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000585
586 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600587 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000588
589 return offset;
590}
591
Kim Phillipsdc00a682012-10-29 13:34:31 +0000592static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000593{
594 int count = his_pad_count;
595
596 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000597 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000598}
599
600/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000601static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000602{
603 if ((in & 0x60) == 0x40) {
604 return (char) (in & ~0x40);
605 } else if ((in & 0x7f) == 0x3f) {
606 return (char) (in | 0x40);
607 } else
608 return in;
609}
610
Kim Phillipsdc00a682012-10-29 13:34:31 +0000611static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000612{
613 int total = 0;
614
615 while (*buffer) {
616 total += *buffer++;
617 }
618 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
619}
620
Kim Phillipsdc00a682012-10-29 13:34:31 +0000621static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000622{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000623 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000624 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000625 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000626 }
627}
628
629static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000630static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000631{
632 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000633 a_b[1] = tochar(3);
634 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000635 a_b[3] = ACK_TYPE;
636 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000637 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000638 a_b[5] = his_eol;
639 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000640 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000641}
642
Kim Phillipsdc00a682012-10-29 13:34:31 +0000643static void send_nack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000644{
645 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000646 a_b[1] = tochar(3);
647 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000648 a_b[3] = NACK_TYPE;
649 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000650 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000651 a_b[5] = his_eol;
652 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000653 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000654}
655
656
Kim Phillipsdc00a682012-10-29 13:34:31 +0000657static void (*os_data_init)(void);
658static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000659static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000660static char *os_data_addr, *os_data_addr_saved;
661static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530662
Kim Phillipsdc00a682012-10-29 13:34:31 +0000663static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000664{
665 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000666 os_data_addr = bin_start_address;
667}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530668
Kim Phillipsdc00a682012-10-29 13:34:31 +0000669static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000670{
671 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000672 os_data_addr_saved = os_data_addr;
673}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530674
Kim Phillipsdc00a682012-10-29 13:34:31 +0000675static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000676{
677 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000678 os_data_addr = os_data_addr_saved;
679}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530680
Kim Phillipsdc00a682012-10-29 13:34:31 +0000681static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000682{
683 switch (os_data_state) {
684 case 0: /* data */
685 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000686 break;
687 }
688}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530689
Kim Phillipsdc00a682012-10-29 13:34:31 +0000690static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000691{
692 bin_start_address = (char *) addr;
693 os_data_init = bin_data_init;
694 os_data_char = bin_data_char;
695}
696
697
698/* k_data_* simply handles the kermit escape translations */
699static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000700static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000701{
702 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000703 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000704}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530705
Kim Phillipsdc00a682012-10-29 13:34:31 +0000706static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000707{
708 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000709 os_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000710}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530711
Kim Phillipsdc00a682012-10-29 13:34:31 +0000712static void k_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000713{
714 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000715 os_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000716}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530717
Kim Phillipsdc00a682012-10-29 13:34:31 +0000718static void k_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000719{
720 if (k_data_escape) {
721 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000722 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000723 k_data_escape = 0;
724 } else {
725 if (new_char == his_quote) {
726 /* this char is escape - remember */
727 k_data_escape = 1;
728 } else {
729 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000730 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000731 }
732 }
733}
734
735#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000736static char send_parms[SEND_DATA_SIZE];
737static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000738
739/* handle_send_packet interprits the protocol info and builds and
740 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000741static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000742{
743 int length = 3;
744 int bytes;
745
746 /* initialize some protocol parameters */
747 his_eol = END_CHAR; /* default end of line character */
748 his_pad_count = 0;
749 his_pad_char = '\0';
750 his_quote = K_ESCAPE;
751
752 /* ignore last character if it filled the buffer */
753 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
754 --send_ptr;
755 bytes = send_ptr - send_parms; /* how many bytes we'll process */
756 do {
757 if (bytes-- <= 0)
758 break;
759 /* handle MAXL - max length */
760 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000761 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000762 if (bytes-- <= 0)
763 break;
764 /* handle TIME - time you should wait for my packets */
765 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000766 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000767 if (bytes-- <= 0)
768 break;
769 /* handle NPAD - number of pad chars I need */
770 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000771 his_pad_count = untochar(send_parms[2]);
772 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000773 if (bytes-- <= 0)
774 break;
775 /* handle PADC - pad chars I need */
776 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000777 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000778 a_b[++length] = 0x40; /* He should ignore this */
779 if (bytes-- <= 0)
780 break;
781 /* handle EOL - end of line he needs */
782 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000783 his_eol = untochar(send_parms[4]);
784 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000785 if (bytes-- <= 0)
786 break;
787 /* handle QCTL - quote control char he'll use */
788 /* remember what he says - I'll use '#' */
789 his_quote = send_parms[5];
790 a_b[++length] = '#';
791 if (bytes-- <= 0)
792 break;
793 /* handle QBIN - 8-th bit prefixing */
794 /* ignore what he says - I refuse */
795 a_b[++length] = 'N';
796 if (bytes-- <= 0)
797 break;
798 /* handle CHKT - the clock check type */
799 /* ignore what he says - I do type 1 (for now) */
800 a_b[++length] = '1';
801 if (bytes-- <= 0)
802 break;
803 /* handle REPT - the repeat prefix */
804 /* ignore what he says - I refuse (for now) */
805 a_b[++length] = 'N';
806 if (bytes-- <= 0)
807 break;
808 /* handle CAPAS - the capabilities mask */
809 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000810 a_b[++length] = tochar(2); /* only long packets */
811 a_b[++length] = tochar(0); /* no windows */
812 a_b[++length] = tochar(94); /* large packet msb */
813 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000814 } while (0);
815
816 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000817 a_b[1] = tochar(length);
818 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000819 a_b[3] = ACK_TYPE;
820 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000821 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000822 a_b[++length] = his_eol;
823 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000824 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000825}
826
827/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000828static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000829{
Tom Rini479f54a2024-01-09 17:57:16 -0500830 int new_char;
wdenk57b2d802003-06-27 21:31:46 +0000831 char k_state, k_state_saved;
832 int sum;
833 int done;
834 int length;
835 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000836 int len_lo, len_hi;
837
838 /* initialize some protocol parameters */
839 his_eol = END_CHAR; /* default end of line character */
840 his_pad_count = 0;
841 his_pad_char = '\0';
842 his_quote = K_ESCAPE;
843
844 /* initialize the k_recv and k_data state machine */
845 done = 0;
846 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000847 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000848 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000849 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000850 n = 0; /* just to get rid of a warning */
851 last_n = -1;
852
853 /* expect this "type" sequence (but don't check):
854 S: send initiate
855 F: file header
856 D: data (multiple)
857 Z: end of file
858 B: break transmission
859 */
860
861 /* enter main loop */
862 while (!done) {
863 /* set the send packet pointer to begining of send packet parms */
864 send_ptr = send_parms;
865
866 /* With each packet, start summing the bytes starting with the length.
867 Save the current sequence number.
868 Note the type of the packet.
869 If a character less than SPACE (0x20) is received - error.
870 */
871
872#if 0
873 /* OLD CODE, Prior to checking sequence numbers */
874 /* first have all state machines save current states */
875 k_state_saved = k_state;
876 k_data_save ();
877#endif
878
879 /* get a packet */
880 /* wait for the starting character or ^C */
881 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200882 switch (getchar()) {
wdenk57b2d802003-06-27 21:31:46 +0000883 case START_CHAR: /* start packet */
884 goto START;
885 case ETX_CHAR: /* ^C waiting for packet */
886 return (0);
887 default:
888 ;
889 }
890 }
891START:
892 /* get length of packet */
893 sum = 0;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200894 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000895 if ((new_char & 0xE0) == 0)
896 goto packet_error;
897 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000898 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000899 /* get sequence number */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200900 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000901 if ((new_char & 0xE0) == 0)
902 goto packet_error;
903 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000904 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000905 --length;
906
907 /* NEW CODE - check sequence numbers for retried packets */
908 /* Note - this new code assumes that the sequence number is correctly
909 * received. Handling an invalid sequence number adds another layer
910 * of complexity that may not be needed - yet! At this time, I'm hoping
911 * that I don't need to buffer the incoming data packets and can write
912 * the data into memory in real time.
913 */
914 if (n == last_n) {
915 /* same sequence number, restore the previous state */
916 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000917 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000918 } else {
919 /* new sequence number, checkpoint the download */
920 last_n = n;
921 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000922 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000923 }
924 /* END NEW CODE */
925
926 /* get packet type */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200927 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000928 if ((new_char & 0xE0) == 0)
929 goto packet_error;
930 sum += new_char & 0xff;
931 k_state = new_char;
932 --length;
933 /* check for extended length */
934 if (length == -2) {
935 /* (length byte was 0, decremented twice) */
936 /* get the two length bytes */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200937 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000938 if ((new_char & 0xE0) == 0)
939 goto packet_error;
940 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000941 len_hi = untochar(new_char);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200942 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000943 if ((new_char & 0xE0) == 0)
944 goto packet_error;
945 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000946 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000947 length = len_hi * 95 + len_lo;
948 /* check header checksum */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200949 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000950 if ((new_char & 0xE0) == 0)
951 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000952 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000953 goto packet_error;
954 sum += new_char & 0xff;
955/* --length; */ /* new length includes only data and block check to come */
956 }
957 /* bring in rest of packet */
958 while (length > 1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200959 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000960 if ((new_char & 0xE0) == 0)
961 goto packet_error;
962 sum += new_char & 0xff;
963 --length;
964 if (k_state == DATA_TYPE) {
965 /* pass on the data if this is a data packet */
966 k_data_char (new_char);
967 } else if (k_state == SEND_TYPE) {
968 /* save send pack in buffer as is */
969 *send_ptr++ = new_char;
970 /* if too much data, back off the pointer */
971 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
972 --send_ptr;
973 }
974 }
975 /* get and validate checksum character */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200976 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000977 if ((new_char & 0xE0) == 0)
978 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000979 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000980 goto packet_error;
981 /* get END_CHAR */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200982 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000983 if (new_char != END_CHAR) {
984 packet_error:
985 /* restore state machines */
986 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000987 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000988 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000989 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000990 } else if (k_state == SEND_TYPE) {
991 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000992 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000993 } else {
994 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000995 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000996 /* quit if end of transmission */
997 if (k_state == BREAK_TYPE)
998 done = 1;
999 }
wdenk57b2d802003-06-27 21:31:46 +00001000 }
1001 return ((ulong) os_data_addr - (ulong) bin_start_address);
1002}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001003
1004static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001005 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +02001006 return (getchar());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001007 return -1;
1008}
Angus Ainslief283bd02013-06-26 16:54:24 -06001009static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001010{
1011 int size;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001012 int err;
1013 int res;
1014 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001015 char ymodemBuf[1024];
1016 ulong store_addr = ~0;
1017 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001018
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001019 size = 0;
Angus Ainslief283bd02013-06-26 16:54:24 -06001020 info.mode = mode;
Kim Phillipsdc00a682012-10-29 13:34:31 +00001021 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001022 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001023
Pali Rohárb6f177b2021-08-03 16:28:42 +02001024 err = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001025 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +00001026 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001027 store_addr = addr + offset;
1028 size += res;
1029 addr += res;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +09001030#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +00001031 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001032 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001033
Kim Phillipsdc00a682012-10-29 13:34:31 +00001034 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001035 store_addr, res);
1036 if (rc != 0) {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001037 xyzModem_stream_terminate(true, &getcxmodem);
1038 xyzModem_stream_close(&err);
1039 printf("\n");
Simon Glassa606ffc2019-12-28 10:44:40 -07001040 flash_perror(rc);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001041 return (~0);
1042 }
1043 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001044#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001045 {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001046 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001047 res);
1048 }
1049
1050 }
Pali Rohárb6f177b2021-08-03 16:28:42 +02001051 if (err) {
1052 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1053 xyzModem_stream_close(&err);
1054 printf("\n%s\n", xyzModem_error(err));
1055 return (~0); /* Download aborted */
1056 }
1057
Heinrich Schuchardta89adb02021-03-19 02:50:57 +00001058 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1059 efi_set_bootdev("Uart", "", "",
1060 map_sysmem(offset, 0), size);
1061
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001062 } else {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001063 printf("\n%s\n", xyzModem_error(err));
1064 return (~0); /* Download aborted */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001065 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001066
Kim Phillipsdc00a682012-10-29 13:34:31 +00001067 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohárd9c805e2021-08-03 16:28:41 +02001068 xyzModem_stream_close(&err);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001069
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001070
Chris Packhambbaf4242016-10-25 20:22:48 +13001071 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001072
Kim Phillipsdc00a682012-10-29 13:34:31 +00001073 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -06001074 env_set_hex("filesize", size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001075
1076 return offset;
1077}
1078
Jon Loeligerd704d912007-07-10 11:02:44 -05001079#endif
wdenk57b2d802003-06-27 21:31:46 +00001080
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001081#if defined(CONFIG_CMD_LOADM)
1082static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
1083 char *const argv[])
1084{
1085 ulong addr, dest, size;
1086 void *src, *dst;
1087
1088 if (argc != 4)
1089 return CMD_RET_USAGE;
1090
1091 addr = simple_strtoul(argv[1], NULL, 16);
1092
1093 dest = simple_strtoul(argv[2], NULL, 16);
1094
1095 size = simple_strtoul(argv[3], NULL, 16);
1096
1097 if (!size) {
1098 printf("loadm: can not load zero bytes\n");
1099 return 1;
1100 }
1101
1102 src = map_sysmem(addr, size);
1103 dst = map_sysmem(dest, size);
1104
1105 memcpy(dst, src, size);
1106
1107 unmap_sysmem(src);
1108 unmap_sysmem(dst);
1109
1110 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1111 efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
1112
1113 printf("loaded bin to memory: size: %lu\n", size);
1114
1115 return 0;
1116}
1117#endif
1118
wdenk57b2d802003-06-27 21:31:46 +00001119/* -------------------------------------------------------------------- */
1120
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001121#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001122
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001123#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001124U_BOOT_CMD(
1125 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001126 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001127 "[ off ] [ baud ]\n"
1128 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001129 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001130);
1131
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001132#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001133U_BOOT_CMD(
1134 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001135 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001136 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001137 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001138);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001139#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001140
1141/*
1142 * SAVES always requires LOADS support, but not vice versa
1143 */
1144
1145
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001146#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001147#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001148U_BOOT_CMD(
1149 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001150 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001151 "[ off ] [size] [ baud ]\n"
1152 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001153 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001154);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001155#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001156U_BOOT_CMD(
1157 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001158 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001159 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001160 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001161);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001162#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001163#endif /* CONFIG_CMD_SAVES */
1164#endif /* CONFIG_CMD_LOADS */
wdenk57b2d802003-06-27 21:31:46 +00001165
1166
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001167#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001168U_BOOT_CMD(
1169 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001170 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001171 "[ addr [ baud ] ]\n"
wdenk57b2d802003-06-27 21:31:46 +00001172 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001173 " at address 'addr' with baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001174);
1175
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001176U_BOOT_CMD(
Angus Ainslief283bd02013-06-26 16:54:24 -06001177 loadx, 3, 0, do_load_serial_bin,
1178 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001179 "[ addr [ baud ] ]\n"
Angus Ainslief283bd02013-06-26 16:54:24 -06001180 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001181 " at address 'addr' with baudrate 'baud'"
Angus Ainslief283bd02013-06-26 16:54:24 -06001182);
1183
1184U_BOOT_CMD(
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001185 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001186 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001187 "[ addr [ baud ] ]\n"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001188 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001189 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001190);
1191
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001192#endif /* CONFIG_CMD_LOADB */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001193
1194#if defined(CONFIG_CMD_LOADM)
1195U_BOOT_CMD(
1196 loadm, 4, 0, do_load_memory_bin,
1197 "load binary blob from source address to destination address",
1198 "[src_addr] [dst_addr] [size]\n"
1199 " - load a binary blob from one memory location to other"
1200 " from src_addr to dst_addr by size bytes"
1201);
1202#endif /* CONFIG_CMD_LOADM */