blob: 56da3a4c5de7844575d21a60b110b9d4eb9c1f2e [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 */
wdenk57b2d802003-06-27 21:31:46 +000010#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000013#include <efi_loader.h>
Simon Glass313112a2019-08-01 09:46:46 -060014#include <env.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000015#include <exports.h>
Tom Rini063c9382022-07-23 13:05:03 -040016#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa606ffc2019-12-28 10:44:40 -070017#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040018#endif
Simon Glass85f13782019-12-28 10:45:03 -070019#include <image.h>
Marek Vasut4d2be382021-10-10 23:52:41 +020020#include <lmb.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000021#include <mapmem.h>
wdenk57b2d802003-06-27 21:31:46 +000022#include <net.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000023#include <s_record.h>
Simon Glass36736182019-11-14 12:57:24 -070024#include <serial.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020025#include <xyzModem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060026#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060027#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
wdenk57b2d802003-06-27 21:31:46 +000029
Wolfgang Denk6405a152006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
wdenk57b2d802003-06-27 21:31:46 +000031
Jon Loeligerd76b5c12007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_LOADB)
Angus Ainslief283bd02013-06-26 16:54:24 -060033static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denkda916932006-05-02 00:11:25 +020034#endif
35
Jon Loeligerd76b5c12007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000037static ulong load_serial(long offset);
38static int read_record(char *buf, ulong len);
Jon Loeligerd76b5c12007-07-08 18:02:23 -050039# if defined(CONFIG_CMD_SAVES)
Kim Phillipsdc00a682012-10-29 13:34:31 +000040static int save_serial(ulong offset, ulong size);
41static int write_record(char *buf);
Jon Loeligerd704d912007-07-10 11:02:44 -050042#endif
wdenk57b2d802003-06-27 21:31:46 +000043
44static int do_echo = 1;
Jon Loeligerd704d912007-07-10 11:02:44 -050045#endif
wdenk57b2d802003-06-27 21:31:46 +000046
47/* -------------------------------------------------------------------- */
48
Jon Loeligerd76b5c12007-07-08 18:02:23 -050049#if defined(CONFIG_CMD_LOADS)
Simon Glassed38aef2020-05-10 11:40:03 -060050static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +000052{
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020053 long offset = 0;
wdenk57b2d802003-06-27 21:31:46 +000054 ulong addr;
55 int i;
56 char *env_echo;
57 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020058#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000059 int load_baudrate, current_baudrate;
60
61 load_baudrate = current_baudrate = gd->baudrate;
62#endif
63
Simon Glass64b723f2017-08-03 12:22:12 -060064 env_echo = env_get("loads_echo");
65 if (env_echo && *env_echo == '1')
wdenk57b2d802003-06-27 21:31:46 +000066 do_echo = 1;
Simon Glass64b723f2017-08-03 12:22:12 -060067 else
wdenk57b2d802003-06-27 21:31:46 +000068 do_echo = 0;
wdenk57b2d802003-06-27 21:31:46 +000069
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000071 if (argc >= 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020072 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000073 }
74 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -060075 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +000076
77 /* default to current baudrate */
78 if (load_baudrate == 0)
79 load_baudrate = current_baudrate;
80 }
81 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +000082 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +000083 load_baudrate);
84 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +020085 flush();
wdenk57b2d802003-06-27 21:31:46 +000086 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +000087 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +000088 udelay(50000);
89 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020090 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +000091 break;
92 }
93 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020094#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000095 if (argc == 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020096 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000097 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020098#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000099
Kim Phillipsdc00a682012-10-29 13:34:31 +0000100 printf("## Ready for S-Record download ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000101
Kim Phillipsdc00a682012-10-29 13:34:31 +0000102 addr = load_serial(offset);
wdenk57b2d802003-06-27 21:31:46 +0000103
104 /*
105 * Gather any trailing characters (for instance, the ^D which
106 * is sent by 'cu' after sending a file), and give the
107 * box some time (100 * 1 ms)
108 */
109 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000110 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200111 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000112 }
113 udelay(1000);
114 }
115
116 if (addr == ~0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000117 printf("## S-Record download aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000118 rcode = 1;
119 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000120 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700121 image_load_addr = addr;
wdenk57b2d802003-06-27 21:31:46 +0000122 }
123
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200124#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000125 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000126 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000127 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000128 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200129 flush();
wdenk57b2d802003-06-27 21:31:46 +0000130 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000131 serial_setbrg();
132 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000133 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200134 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000135 break;
136 }
137 }
138#endif
139 return rcode;
140}
141
Kim Phillipsdc00a682012-10-29 13:34:31 +0000142static ulong load_serial(long offset)
wdenk57b2d802003-06-27 21:31:46 +0000143{
144 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
145 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
146 int binlen; /* no. of data bytes in S-Rec. */
147 int type; /* return code for record type */
148 ulong addr; /* load address from S-Record */
149 ulong size; /* number of bytes transferred */
wdenk57b2d802003-06-27 21:31:46 +0000150 ulong store_addr;
151 ulong start_addr = ~0;
152 ulong end_addr = 0;
153 int line_count = 0;
Marek Vasut4d2be382021-10-10 23:52:41 +0200154 long ret;
155
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530156 lmb_init_and_reserve(gd->bd, (void *)gd->fdt_blob);
wdenk57b2d802003-06-27 21:31:46 +0000157
158 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000159 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk57b2d802003-06-27 21:31:46 +0000160
161 if (type < 0) {
162 return (~0); /* Invalid S-Record */
163 }
164
165 switch (type) {
166 case SREC_DATA2:
167 case SREC_DATA3:
168 case SREC_DATA4:
169 store_addr = addr + offset;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900170#ifdef CONFIG_MTD_NOR_FLASH
wdenk57b2d802003-06-27 21:31:46 +0000171 if (addr2info(store_addr)) {
172 int rc;
173
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200174 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk57b2d802003-06-27 21:31:46 +0000175 if (rc != 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000176 flash_perror(rc);
wdenk57b2d802003-06-27 21:31:46 +0000177 return (~0);
178 }
179 } else
180#endif
181 {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200182 void *dst;
183
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530184 ret = lmb_reserve(store_addr, binlen);
Marek Vasut4d2be382021-10-10 23:52:41 +0200185 if (ret) {
186 printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
187 store_addr, store_addr + binlen);
188 return ret;
189 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200190 dst = map_sysmem(store_addr, binlen);
191 memcpy(dst, binbuf, binlen);
192 unmap_sysmem(dst);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530193 lmb_free(store_addr, binlen);
wdenk57b2d802003-06-27 21:31:46 +0000194 }
195 if ((store_addr) < start_addr)
196 start_addr = store_addr;
197 if ((store_addr + binlen - 1) > end_addr)
198 end_addr = store_addr + binlen - 1;
199 break;
200 case SREC_END2:
201 case SREC_END3:
202 case SREC_END4:
Kim Phillipsdc00a682012-10-29 13:34:31 +0000203 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000204 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000205 printf("\n"
wdenk57b2d802003-06-27 21:31:46 +0000206 "## First Load Addr = 0x%08lX\n"
207 "## Last Load Addr = 0x%08lX\n"
208 "## Total Size = 0x%08lX = %ld Bytes\n",
209 start_addr, end_addr, size, size
210 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000211 flush_cache(start_addr, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600212 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000213 return (addr);
214 case SREC_START:
215 break;
216 default:
217 break;
218 }
219 if (!do_echo) { /* print a '.' every 100 lines */
220 if ((++line_count % 100) == 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000221 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000222 }
223 }
224
225 return (~0); /* Download aborted */
226}
227
Kim Phillipsdc00a682012-10-29 13:34:31 +0000228static int read_record(char *buf, ulong len)
wdenk57b2d802003-06-27 21:31:46 +0000229{
230 char *p;
Tom Rini479f54a2024-01-09 17:57:16 -0500231 int c;
wdenk57b2d802003-06-27 21:31:46 +0000232
233 --len; /* always leave room for terminating '\0' byte */
234
235 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200236 c = getchar(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000237 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000238 putc(c); /* ... and echo it */
wdenk57b2d802003-06-27 21:31:46 +0000239
240 switch (c) {
241 case '\r':
242 case '\n':
243 *p = '\0';
244 return (p - buf);
245 case '\0':
246 case 0x03: /* ^C - Control C */
247 return (-1);
248 default:
249 *p = c;
250 }
251
Michal Simekb6e8ba12020-12-01 13:58:28 +0100252 /* Check for the console hangup (if any different from serial) */
253 if (gd->jt->getc != getchar) {
254 if (ctrlc())
255 return (-1);
wdenk57b2d802003-06-27 21:31:46 +0000256 }
wdenk57b2d802003-06-27 21:31:46 +0000257 }
258
259 /* line too long - truncate */
260 *p = '\0';
261 return (p - buf);
262}
263
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500264#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000265
Simon Glassed38aef2020-05-10 11:40:03 -0600266int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
267 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000268{
269 ulong offset = 0;
270 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200271#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000272 int save_baudrate, current_baudrate;
273
274 save_baudrate = current_baudrate = gd->baudrate;
275#endif
276
277 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600278 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000279 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200280#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000281 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600282 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000283 }
284 if (argc == 4) {
Simon Glassff9b9032021-07-24 09:03:30 -0600285 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000286
287 /* default to current baudrate */
288 if (save_baudrate == 0)
289 save_baudrate = current_baudrate;
290 }
291 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000292 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000293 save_baudrate);
294 udelay(50000);
295 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000296 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000297 udelay(50000);
298 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200299 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000300 break;
301 }
302 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200303#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000304 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600305 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000306 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200307#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000308
Kim Phillipsdc00a682012-10-29 13:34:31 +0000309 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000310 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200311 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000312 break;
313 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000314 if (save_serial(offset, size)) {
315 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000316 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000317 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000318 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200319#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000320 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000321 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000322 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000323 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200324 flush();
wdenk57b2d802003-06-27 21:31:46 +0000325 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000326 serial_setbrg();
327 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000328 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200329 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000330 break;
331 }
332 }
333#endif
334 return 0;
335}
336
337#define SREC3_START "S0030000FC\n"
338#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
339#define SREC3_END "S70500000000FA\n"
340#define SREC_BYTES_PER_RECORD 16
341
Kim Phillipsdc00a682012-10-29 13:34:31 +0000342static int save_serial(ulong address, ulong count)
wdenk57b2d802003-06-27 21:31:46 +0000343{
344 int i, c, reclen, checksum, length;
345 char *hex = "0123456789ABCDEF";
346 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
347 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
348
349 reclen = 0;
350 checksum = 0;
351
352 if(write_record(SREC3_START)) /* write the header */
353 return (-1);
354 do {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200355 volatile uchar *src;
356
357 src = map_sysmem(address, count);
358 if (count) { /* collect hex data in the buffer */
359 c = src[reclen]; /* get one byte */
360 checksum += c; /* accumulate checksum */
wdenk57b2d802003-06-27 21:31:46 +0000361 data[2*reclen] = hex[(c>>4)&0x0f];
362 data[2*reclen+1] = hex[c & 0x0f];
363 data[2*reclen+2] = '\0';
364 ++reclen;
365 --count;
366 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200367 unmap_sysmem((void *)src);
wdenk57b2d802003-06-27 21:31:46 +0000368 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
369 /* enough data collected for one record: dump it */
370 if(reclen) { /* build & write a data record: */
371 /* address + data + checksum */
372 length = 4 + reclen + 1;
373
374 /* accumulate length bytes into checksum */
375 for(i = 0; i < 2; i++)
376 checksum += (length >> (8*i)) & 0xff;
377
378 /* accumulate address bytes into checksum: */
379 for(i = 0; i < 4; i++)
380 checksum += (address >> (8*i)) & 0xff;
381
382 /* make proper checksum byte: */
383 checksum = ~checksum & 0xff;
384
385 /* output one record: */
386 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
387 if(write_record(record))
388 return (-1);
389 }
390 address += reclen; /* increment address */
391 checksum = 0;
392 reclen = 0;
393 }
394 }
395 while(count);
396 if(write_record(SREC3_END)) /* write the final record */
397 return (-1);
398 return(0);
399}
400
Kim Phillipsdc00a682012-10-29 13:34:31 +0000401static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000402{
403 char c;
404
405 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000406 putc(c);
wdenk57b2d802003-06-27 21:31:46 +0000407
408 /* Check for the console hangup (if any different from serial) */
409
410 if (ctrlc()) {
411 return (-1);
412 }
413 return (0);
414}
Jon Loeligerd704d912007-07-10 11:02:44 -0500415# endif
wdenk57b2d802003-06-27 21:31:46 +0000416
Jon Loeligerd704d912007-07-10 11:02:44 -0500417#endif
wdenk57b2d802003-06-27 21:31:46 +0000418
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500419#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500420/*
421 * loadb command (load binary) included
422 */
wdenk57b2d802003-06-27 21:31:46 +0000423#define XON_CHAR 17
424#define XOFF_CHAR 19
425#define START_CHAR 0x01
426#define ETX_CHAR 0x03
427#define END_CHAR 0x0D
428#define SPACE 0x20
429#define K_ESCAPE 0x23
430#define SEND_TYPE 'S'
431#define DATA_TYPE 'D'
432#define ACK_TYPE 'Y'
433#define NACK_TYPE 'N'
434#define BREAK_TYPE 'B'
435#define tochar(x) ((char) (((x) + SPACE) & 0xff))
436#define untochar(x) ((int) (((x) - SPACE) & 0xff))
437
wdenk57b2d802003-06-27 21:31:46 +0000438static void set_kerm_bin_mode(unsigned long *);
439static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000440static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000441
Kim Phillipsdc00a682012-10-29 13:34:31 +0000442static char his_eol; /* character he needs at end of packet */
443static int his_pad_count; /* number of pad chars he needs */
444static char his_pad_char; /* pad chars he needs */
445static char his_quote; /* quote chars he'll use */
wdenk57b2d802003-06-27 21:31:46 +0000446
Simon Glassed38aef2020-05-10 11:40:03 -0600447static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
448 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000449{
wdenk57b2d802003-06-27 21:31:46 +0000450 ulong offset = 0;
451 ulong addr;
452 int load_baudrate, current_baudrate;
453 int rcode = 0;
454 char *s;
455
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200456 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
457 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000458
459 /* pre-set offset from $loadaddr */
Simon Glass64b723f2017-08-03 12:22:12 -0600460 s = env_get("loadaddr");
461 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600462 offset = hextoul(s, NULL);
wdenk57b2d802003-06-27 21:31:46 +0000463
464 load_baudrate = current_baudrate = gd->baudrate;
465
466 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600467 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000468 }
469 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600470 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000471
472 /* default to current baudrate */
473 if (load_baudrate == 0)
474 load_baudrate = current_baudrate;
475 }
476
477 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000478 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000479 load_baudrate);
480 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200481 flush();
wdenk57b2d802003-06-27 21:31:46 +0000482 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000483 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000484 udelay(50000);
485 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200486 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000487 break;
488 }
489 }
490
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200491 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000492 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200493 "to 0x%08lX at %d bps...\n",
494 offset,
495 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000496
Angus Ainslief283bd02013-06-26 16:54:24 -0600497 addr = load_serial_ymodem(offset, xyzModem_ymodem);
498
Pali Roháre37155f2021-08-03 16:28:43 +0200499 if (addr == ~0) {
500 image_load_addr = 0;
501 printf("## Binary (ymodem) download aborted\n");
502 rcode = 1;
503 } else {
504 printf("## Start Addr = 0x%08lX\n", addr);
505 image_load_addr = addr;
506 }
Angus Ainslief283bd02013-06-26 16:54:24 -0600507 } else if (strcmp(argv[0],"loadx")==0) {
508 printf("## Ready for binary (xmodem) download "
509 "to 0x%08lX at %d bps...\n",
510 offset,
511 load_baudrate);
512
513 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200514
Pali Roháre37155f2021-08-03 16:28:43 +0200515 if (addr == ~0) {
516 image_load_addr = 0;
517 printf("## Binary (xmodem) download aborted\n");
518 rcode = 1;
519 } else {
520 printf("## Start Addr = 0x%08lX\n", addr);
521 image_load_addr = addr;
522 }
wdenk57b2d802003-06-27 21:31:46 +0000523 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200524
Kim Phillipsdc00a682012-10-29 13:34:31 +0000525 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200526 "to 0x%08lX at %d bps...\n",
527 offset,
528 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000529 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000530
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200531 if (addr == ~0) {
Simon Glass892265d2019-12-28 10:45:02 -0700532 image_load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000533 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200534 rcode = 1;
535 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000536 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700537 image_load_addr = addr;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200538 }
539 }
wdenk57b2d802003-06-27 21:31:46 +0000540 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000541 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000542 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000543 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200544 flush();
wdenk57b2d802003-06-27 21:31:46 +0000545 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000546 serial_setbrg();
547 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000548 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200549 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000550 break;
551 }
552 }
553
wdenk57b2d802003-06-27 21:31:46 +0000554 return rcode;
555}
556
Kim Phillipsdc00a682012-10-29 13:34:31 +0000557static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000558{
559 int size, i;
wdenk57b2d802003-06-27 21:31:46 +0000560
Kim Phillipsdc00a682012-10-29 13:34:31 +0000561 set_kerm_bin_mode((ulong *) offset);
562 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000563
564 /*
565 * Gather any trailing characters (for instance, the ^D which
566 * is sent by 'cu' after sending a file), and give the
567 * box some time (100 * 1 ms)
568 */
569 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000570 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200571 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000572 }
573 udelay(1000);
574 }
575
Pali Rohár8398dd32021-08-06 18:07:39 +0200576 if (size == 0)
577 return ~0; /* Download aborted */
578
Kim Phillipsdc00a682012-10-29 13:34:31 +0000579 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000580
581 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600582 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000583
584 return offset;
585}
586
Kim Phillipsdc00a682012-10-29 13:34:31 +0000587static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000588{
589 int count = his_pad_count;
590
591 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000592 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000593}
594
595/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000596static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000597{
598 if ((in & 0x60) == 0x40) {
599 return (char) (in & ~0x40);
600 } else if ((in & 0x7f) == 0x3f) {
601 return (char) (in | 0x40);
602 } else
603 return in;
604}
605
Kim Phillipsdc00a682012-10-29 13:34:31 +0000606static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000607{
608 int total = 0;
609
610 while (*buffer) {
611 total += *buffer++;
612 }
613 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
614}
615
Kim Phillipsdc00a682012-10-29 13:34:31 +0000616static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000617{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000618 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000619 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000620 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000621 }
622}
623
624static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000625static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000626{
627 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000628 a_b[1] = tochar(3);
629 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000630 a_b[3] = ACK_TYPE;
631 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000632 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000633 a_b[5] = his_eol;
634 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000635 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000636}
637
Kim Phillipsdc00a682012-10-29 13:34:31 +0000638static void send_nack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000639{
640 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000641 a_b[1] = tochar(3);
642 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000643 a_b[3] = NACK_TYPE;
644 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000645 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000646 a_b[5] = his_eol;
647 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000648 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000649}
650
Kim Phillipsdc00a682012-10-29 13:34:31 +0000651static void (*os_data_init)(void);
652static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000653static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000654static char *os_data_addr, *os_data_addr_saved;
655static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530656
Kim Phillipsdc00a682012-10-29 13:34:31 +0000657static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000658{
659 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000660 os_data_addr = bin_start_address;
661}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530662
Kim Phillipsdc00a682012-10-29 13:34:31 +0000663static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000664{
665 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000666 os_data_addr_saved = os_data_addr;
667}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530668
Kim Phillipsdc00a682012-10-29 13:34:31 +0000669static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000670{
671 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000672 os_data_addr = os_data_addr_saved;
673}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530674
Kim Phillipsdc00a682012-10-29 13:34:31 +0000675static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000676{
677 switch (os_data_state) {
678 case 0: /* data */
679 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000680 break;
681 }
682}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530683
Kim Phillipsdc00a682012-10-29 13:34:31 +0000684static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000685{
686 bin_start_address = (char *) addr;
687 os_data_init = bin_data_init;
688 os_data_char = bin_data_char;
689}
690
wdenk57b2d802003-06-27 21:31:46 +0000691/* k_data_* simply handles the kermit escape translations */
692static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000693static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000694{
695 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000696 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000697}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530698
Kim Phillipsdc00a682012-10-29 13:34:31 +0000699static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000700{
701 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000702 os_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000703}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530704
Kim Phillipsdc00a682012-10-29 13:34:31 +0000705static void k_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000706{
707 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000708 os_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000709}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530710
Kim Phillipsdc00a682012-10-29 13:34:31 +0000711static void k_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000712{
713 if (k_data_escape) {
714 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000715 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000716 k_data_escape = 0;
717 } else {
718 if (new_char == his_quote) {
719 /* this char is escape - remember */
720 k_data_escape = 1;
721 } else {
722 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000723 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000724 }
725 }
726}
727
728#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000729static char send_parms[SEND_DATA_SIZE];
730static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000731
732/* handle_send_packet interprits the protocol info and builds and
733 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000734static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000735{
736 int length = 3;
737 int bytes;
738
739 /* initialize some protocol parameters */
740 his_eol = END_CHAR; /* default end of line character */
741 his_pad_count = 0;
742 his_pad_char = '\0';
743 his_quote = K_ESCAPE;
744
745 /* ignore last character if it filled the buffer */
746 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
747 --send_ptr;
748 bytes = send_ptr - send_parms; /* how many bytes we'll process */
749 do {
750 if (bytes-- <= 0)
751 break;
752 /* handle MAXL - max length */
753 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000754 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000755 if (bytes-- <= 0)
756 break;
757 /* handle TIME - time you should wait for my packets */
758 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000759 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000760 if (bytes-- <= 0)
761 break;
762 /* handle NPAD - number of pad chars I need */
763 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000764 his_pad_count = untochar(send_parms[2]);
765 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000766 if (bytes-- <= 0)
767 break;
768 /* handle PADC - pad chars I need */
769 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000770 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000771 a_b[++length] = 0x40; /* He should ignore this */
772 if (bytes-- <= 0)
773 break;
774 /* handle EOL - end of line he needs */
775 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000776 his_eol = untochar(send_parms[4]);
777 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000778 if (bytes-- <= 0)
779 break;
780 /* handle QCTL - quote control char he'll use */
781 /* remember what he says - I'll use '#' */
782 his_quote = send_parms[5];
783 a_b[++length] = '#';
784 if (bytes-- <= 0)
785 break;
786 /* handle QBIN - 8-th bit prefixing */
787 /* ignore what he says - I refuse */
788 a_b[++length] = 'N';
789 if (bytes-- <= 0)
790 break;
791 /* handle CHKT - the clock check type */
792 /* ignore what he says - I do type 1 (for now) */
793 a_b[++length] = '1';
794 if (bytes-- <= 0)
795 break;
796 /* handle REPT - the repeat prefix */
797 /* ignore what he says - I refuse (for now) */
798 a_b[++length] = 'N';
799 if (bytes-- <= 0)
800 break;
801 /* handle CAPAS - the capabilities mask */
802 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000803 a_b[++length] = tochar(2); /* only long packets */
804 a_b[++length] = tochar(0); /* no windows */
805 a_b[++length] = tochar(94); /* large packet msb */
806 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000807 } while (0);
808
809 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000810 a_b[1] = tochar(length);
811 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000812 a_b[3] = ACK_TYPE;
813 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000814 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000815 a_b[++length] = his_eol;
816 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000817 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000818}
819
820/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000821static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000822{
Tom Rini479f54a2024-01-09 17:57:16 -0500823 int new_char;
wdenk57b2d802003-06-27 21:31:46 +0000824 char k_state, k_state_saved;
825 int sum;
826 int done;
827 int length;
828 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000829 int len_lo, len_hi;
830
831 /* initialize some protocol parameters */
832 his_eol = END_CHAR; /* default end of line character */
833 his_pad_count = 0;
834 his_pad_char = '\0';
835 his_quote = K_ESCAPE;
836
837 /* initialize the k_recv and k_data state machine */
838 done = 0;
839 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000840 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000841 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000842 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000843 n = 0; /* just to get rid of a warning */
844 last_n = -1;
845
846 /* expect this "type" sequence (but don't check):
847 S: send initiate
848 F: file header
849 D: data (multiple)
850 Z: end of file
851 B: break transmission
852 */
853
854 /* enter main loop */
855 while (!done) {
856 /* set the send packet pointer to begining of send packet parms */
857 send_ptr = send_parms;
858
859 /* With each packet, start summing the bytes starting with the length.
860 Save the current sequence number.
861 Note the type of the packet.
862 If a character less than SPACE (0x20) is received - error.
863 */
864
865#if 0
866 /* OLD CODE, Prior to checking sequence numbers */
867 /* first have all state machines save current states */
868 k_state_saved = k_state;
869 k_data_save ();
870#endif
871
872 /* get a packet */
873 /* wait for the starting character or ^C */
874 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200875 switch (getchar()) {
wdenk57b2d802003-06-27 21:31:46 +0000876 case START_CHAR: /* start packet */
877 goto START;
878 case ETX_CHAR: /* ^C waiting for packet */
879 return (0);
880 default:
881 ;
882 }
883 }
884START:
885 /* get length of packet */
886 sum = 0;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200887 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000888 if ((new_char & 0xE0) == 0)
889 goto packet_error;
890 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000891 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000892 /* get sequence number */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200893 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000894 if ((new_char & 0xE0) == 0)
895 goto packet_error;
896 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000897 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000898 --length;
899
900 /* NEW CODE - check sequence numbers for retried packets */
901 /* Note - this new code assumes that the sequence number is correctly
902 * received. Handling an invalid sequence number adds another layer
903 * of complexity that may not be needed - yet! At this time, I'm hoping
904 * that I don't need to buffer the incoming data packets and can write
905 * the data into memory in real time.
906 */
907 if (n == last_n) {
908 /* same sequence number, restore the previous state */
909 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000910 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000911 } else {
912 /* new sequence number, checkpoint the download */
913 last_n = n;
914 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000915 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000916 }
917 /* END NEW CODE */
918
919 /* get packet type */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200920 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000921 if ((new_char & 0xE0) == 0)
922 goto packet_error;
923 sum += new_char & 0xff;
924 k_state = new_char;
925 --length;
926 /* check for extended length */
927 if (length == -2) {
928 /* (length byte was 0, decremented twice) */
929 /* get the two length bytes */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200930 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000931 if ((new_char & 0xE0) == 0)
932 goto packet_error;
933 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000934 len_hi = untochar(new_char);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200935 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000936 if ((new_char & 0xE0) == 0)
937 goto packet_error;
938 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000939 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000940 length = len_hi * 95 + len_lo;
941 /* check header checksum */
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;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000945 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000946 goto packet_error;
947 sum += new_char & 0xff;
948/* --length; */ /* new length includes only data and block check to come */
949 }
950 /* bring in rest of packet */
951 while (length > 1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200952 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000953 if ((new_char & 0xE0) == 0)
954 goto packet_error;
955 sum += new_char & 0xff;
956 --length;
957 if (k_state == DATA_TYPE) {
958 /* pass on the data if this is a data packet */
959 k_data_char (new_char);
960 } else if (k_state == SEND_TYPE) {
961 /* save send pack in buffer as is */
962 *send_ptr++ = new_char;
963 /* if too much data, back off the pointer */
964 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
965 --send_ptr;
966 }
967 }
968 /* get and validate checksum character */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200969 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000970 if ((new_char & 0xE0) == 0)
971 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000972 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000973 goto packet_error;
974 /* get END_CHAR */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200975 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000976 if (new_char != END_CHAR) {
977 packet_error:
978 /* restore state machines */
979 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000980 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000981 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000982 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000983 } else if (k_state == SEND_TYPE) {
984 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000985 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000986 } else {
987 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000988 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000989 /* quit if end of transmission */
990 if (k_state == BREAK_TYPE)
991 done = 1;
992 }
wdenk57b2d802003-06-27 21:31:46 +0000993 }
994 return ((ulong) os_data_addr - (ulong) bin_start_address);
995}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200996
997static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200998 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200999 return (getchar());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001000 return -1;
1001}
Angus Ainslief283bd02013-06-26 16:54:24 -06001002static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001003{
1004 int size;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001005 int err;
1006 int res;
1007 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001008 char ymodemBuf[1024];
1009 ulong store_addr = ~0;
1010 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001011
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001012 size = 0;
Angus Ainslief283bd02013-06-26 16:54:24 -06001013 info.mode = mode;
Kim Phillipsdc00a682012-10-29 13:34:31 +00001014 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001015 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001016
Pali Rohárb6f177b2021-08-03 16:28:42 +02001017 err = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001018 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +00001019 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001020 store_addr = addr + offset;
1021 size += res;
1022 addr += res;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +09001023#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +00001024 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001025 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001026
Kim Phillipsdc00a682012-10-29 13:34:31 +00001027 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001028 store_addr, res);
1029 if (rc != 0) {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001030 xyzModem_stream_terminate(true, &getcxmodem);
1031 xyzModem_stream_close(&err);
1032 printf("\n");
Simon Glassa606ffc2019-12-28 10:44:40 -07001033 flash_perror(rc);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001034 return (~0);
1035 }
1036 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001037#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001038 {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001039 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001040 res);
1041 }
1042
1043 }
Pali Rohárb6f177b2021-08-03 16:28:42 +02001044 if (err) {
1045 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1046 xyzModem_stream_close(&err);
1047 printf("\n%s\n", xyzModem_error(err));
1048 return (~0); /* Download aborted */
1049 }
1050
Heinrich Schuchardta89adb02021-03-19 02:50:57 +00001051 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1052 efi_set_bootdev("Uart", "", "",
1053 map_sysmem(offset, 0), size);
1054
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001055 } else {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001056 printf("\n%s\n", xyzModem_error(err));
1057 return (~0); /* Download aborted */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001058 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001059
Kim Phillipsdc00a682012-10-29 13:34:31 +00001060 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohárd9c805e2021-08-03 16:28:41 +02001061 xyzModem_stream_close(&err);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001062
Chris Packhambbaf4242016-10-25 20:22:48 +13001063 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001064
Kim Phillipsdc00a682012-10-29 13:34:31 +00001065 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -06001066 env_set_hex("filesize", size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001067
1068 return offset;
1069}
1070
Jon Loeligerd704d912007-07-10 11:02:44 -05001071#endif
wdenk57b2d802003-06-27 21:31:46 +00001072
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001073#if defined(CONFIG_CMD_LOADM)
1074static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
1075 char *const argv[])
1076{
1077 ulong addr, dest, size;
1078 void *src, *dst;
1079
1080 if (argc != 4)
1081 return CMD_RET_USAGE;
1082
1083 addr = simple_strtoul(argv[1], NULL, 16);
1084
1085 dest = simple_strtoul(argv[2], NULL, 16);
1086
1087 size = simple_strtoul(argv[3], NULL, 16);
1088
1089 if (!size) {
1090 printf("loadm: can not load zero bytes\n");
1091 return 1;
1092 }
1093
1094 src = map_sysmem(addr, size);
1095 dst = map_sysmem(dest, size);
1096
1097 memcpy(dst, src, size);
1098
1099 unmap_sysmem(src);
1100 unmap_sysmem(dst);
1101
1102 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1103 efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
1104
1105 printf("loaded bin to memory: size: %lu\n", size);
1106
1107 return 0;
1108}
1109#endif
1110
wdenk57b2d802003-06-27 21:31:46 +00001111/* -------------------------------------------------------------------- */
1112
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001113#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001114
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001115#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001116U_BOOT_CMD(
1117 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001118 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001119 "[ off ] [ baud ]\n"
1120 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001121 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001122);
1123
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001124#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001125U_BOOT_CMD(
1126 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001127 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001128 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001129 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001130);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001131#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001132
1133/*
1134 * SAVES always requires LOADS support, but not vice versa
1135 */
1136
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001137#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001138#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001139U_BOOT_CMD(
1140 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001141 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001142 "[ off ] [size] [ baud ]\n"
1143 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001144 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001145);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001146#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001147U_BOOT_CMD(
1148 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001149 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001150 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001151 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001152);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001153#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001154#endif /* CONFIG_CMD_SAVES */
1155#endif /* CONFIG_CMD_LOADS */
wdenk57b2d802003-06-27 21:31:46 +00001156
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001157#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001158U_BOOT_CMD(
1159 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001160 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001161 "[ addr [ baud ] ]\n"
wdenk57b2d802003-06-27 21:31:46 +00001162 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001163 " at address 'addr' with baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001164);
1165
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001166U_BOOT_CMD(
Angus Ainslief283bd02013-06-26 16:54:24 -06001167 loadx, 3, 0, do_load_serial_bin,
1168 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001169 "[ addr [ baud ] ]\n"
Angus Ainslief283bd02013-06-26 16:54:24 -06001170 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001171 " at address 'addr' with baudrate 'baud'"
Angus Ainslief283bd02013-06-26 16:54:24 -06001172);
1173
1174U_BOOT_CMD(
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001175 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001176 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001177 "[ addr [ baud ] ]\n"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001178 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001179 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001180);
1181
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001182#endif /* CONFIG_CMD_LOADB */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001183
1184#if defined(CONFIG_CMD_LOADM)
1185U_BOOT_CMD(
1186 loadm, 4, 0, do_load_memory_bin,
1187 "load binary blob from source address to destination address",
1188 "[src_addr] [dst_addr] [size]\n"
1189 " - load a binary blob from one memory location to other"
1190 " from src_addr to dst_addr by size bytes"
1191);
1192#endif /* CONFIG_CMD_LOADM */