blob: e44ae0d56b750c4311ff006fbcf8cd864ac77edc [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 */
10#include <common.h>
11#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);
86 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);
wdenk57b2d802003-06-27 21:31:46 +0000129 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000130 serial_setbrg();
131 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000132 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200133 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000134 break;
135 }
136 }
137#endif
138 return rcode;
139}
140
Kim Phillipsdc00a682012-10-29 13:34:31 +0000141static ulong load_serial(long offset)
wdenk57b2d802003-06-27 21:31:46 +0000142{
Marek Vasut4d2be382021-10-10 23:52:41 +0200143 struct lmb lmb;
wdenk57b2d802003-06-27 21:31:46 +0000144 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
156 lmb_init_and_reserve(&lmb, 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 {
Marek Vasut4d2be382021-10-10 23:52:41 +0200182 ret = lmb_reserve(&lmb, store_addr, binlen);
183 if (ret) {
184 printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
185 store_addr, store_addr + binlen);
186 return ret;
187 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000188 memcpy((char *)(store_addr), binbuf, binlen);
Marek Vasut4d2be382021-10-10 23:52:41 +0200189 lmb_free(&lmb, store_addr, binlen);
wdenk57b2d802003-06-27 21:31:46 +0000190 }
191 if ((store_addr) < start_addr)
192 start_addr = store_addr;
193 if ((store_addr + binlen - 1) > end_addr)
194 end_addr = store_addr + binlen - 1;
195 break;
196 case SREC_END2:
197 case SREC_END3:
198 case SREC_END4:
Kim Phillipsdc00a682012-10-29 13:34:31 +0000199 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000200 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000201 printf("\n"
wdenk57b2d802003-06-27 21:31:46 +0000202 "## First Load Addr = 0x%08lX\n"
203 "## Last Load Addr = 0x%08lX\n"
204 "## Total Size = 0x%08lX = %ld Bytes\n",
205 start_addr, end_addr, size, size
206 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000207 flush_cache(start_addr, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600208 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000209 return (addr);
210 case SREC_START:
211 break;
212 default:
213 break;
214 }
215 if (!do_echo) { /* print a '.' every 100 lines */
216 if ((++line_count % 100) == 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000217 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000218 }
219 }
220
221 return (~0); /* Download aborted */
222}
223
Kim Phillipsdc00a682012-10-29 13:34:31 +0000224static int read_record(char *buf, ulong len)
wdenk57b2d802003-06-27 21:31:46 +0000225{
226 char *p;
227 char c;
228
229 --len; /* always leave room for terminating '\0' byte */
230
231 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200232 c = getchar(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000233 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000234 putc(c); /* ... and echo it */
wdenk57b2d802003-06-27 21:31:46 +0000235
236 switch (c) {
237 case '\r':
238 case '\n':
239 *p = '\0';
240 return (p - buf);
241 case '\0':
242 case 0x03: /* ^C - Control C */
243 return (-1);
244 default:
245 *p = c;
246 }
247
Michal Simekb6e8ba12020-12-01 13:58:28 +0100248 /* Check for the console hangup (if any different from serial) */
249 if (gd->jt->getc != getchar) {
250 if (ctrlc())
251 return (-1);
wdenk57b2d802003-06-27 21:31:46 +0000252 }
wdenk57b2d802003-06-27 21:31:46 +0000253 }
254
255 /* line too long - truncate */
256 *p = '\0';
257 return (p - buf);
258}
259
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500260#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000261
Simon Glassed38aef2020-05-10 11:40:03 -0600262int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
263 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000264{
265 ulong offset = 0;
266 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200267#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000268 int save_baudrate, current_baudrate;
269
270 save_baudrate = current_baudrate = gd->baudrate;
271#endif
272
273 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600274 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000275 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200276#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000277 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600278 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000279 }
280 if (argc == 4) {
Simon Glassff9b9032021-07-24 09:03:30 -0600281 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000282
283 /* default to current baudrate */
284 if (save_baudrate == 0)
285 save_baudrate = current_baudrate;
286 }
287 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000288 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000289 save_baudrate);
290 udelay(50000);
291 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000292 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000293 udelay(50000);
294 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200295 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000296 break;
297 }
298 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200299#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000300 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600301 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000302 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200303#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000304
Kim Phillipsdc00a682012-10-29 13:34:31 +0000305 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000306 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200307 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000308 break;
309 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000310 if (save_serial(offset, size)) {
311 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000312 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000313 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000314 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200315#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000316 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000317 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000318 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000319 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000320 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000321 serial_setbrg();
322 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000323 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200324 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000325 break;
326 }
327 }
328#endif
329 return 0;
330}
331
332#define SREC3_START "S0030000FC\n"
333#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
334#define SREC3_END "S70500000000FA\n"
335#define SREC_BYTES_PER_RECORD 16
336
Kim Phillipsdc00a682012-10-29 13:34:31 +0000337static int save_serial(ulong address, ulong count)
wdenk57b2d802003-06-27 21:31:46 +0000338{
339 int i, c, reclen, checksum, length;
340 char *hex = "0123456789ABCDEF";
341 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
342 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
343
344 reclen = 0;
345 checksum = 0;
346
347 if(write_record(SREC3_START)) /* write the header */
348 return (-1);
349 do {
350 if(count) { /* collect hex data in the buffer */
351 c = *(volatile uchar*)(address + reclen); /* get one byte */
352 checksum += c; /* accumulate checksum */
353 data[2*reclen] = hex[(c>>4)&0x0f];
354 data[2*reclen+1] = hex[c & 0x0f];
355 data[2*reclen+2] = '\0';
356 ++reclen;
357 --count;
358 }
359 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
360 /* enough data collected for one record: dump it */
361 if(reclen) { /* build & write a data record: */
362 /* address + data + checksum */
363 length = 4 + reclen + 1;
364
365 /* accumulate length bytes into checksum */
366 for(i = 0; i < 2; i++)
367 checksum += (length >> (8*i)) & 0xff;
368
369 /* accumulate address bytes into checksum: */
370 for(i = 0; i < 4; i++)
371 checksum += (address >> (8*i)) & 0xff;
372
373 /* make proper checksum byte: */
374 checksum = ~checksum & 0xff;
375
376 /* output one record: */
377 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
378 if(write_record(record))
379 return (-1);
380 }
381 address += reclen; /* increment address */
382 checksum = 0;
383 reclen = 0;
384 }
385 }
386 while(count);
387 if(write_record(SREC3_END)) /* write the final record */
388 return (-1);
389 return(0);
390}
391
Kim Phillipsdc00a682012-10-29 13:34:31 +0000392static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000393{
394 char c;
395
396 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000397 putc(c);
wdenk57b2d802003-06-27 21:31:46 +0000398
399 /* Check for the console hangup (if any different from serial) */
400
401 if (ctrlc()) {
402 return (-1);
403 }
404 return (0);
405}
Jon Loeligerd704d912007-07-10 11:02:44 -0500406# endif
wdenk57b2d802003-06-27 21:31:46 +0000407
Jon Loeligerd704d912007-07-10 11:02:44 -0500408#endif
wdenk57b2d802003-06-27 21:31:46 +0000409
410
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500411#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500412/*
413 * loadb command (load binary) included
414 */
wdenk57b2d802003-06-27 21:31:46 +0000415#define XON_CHAR 17
416#define XOFF_CHAR 19
417#define START_CHAR 0x01
418#define ETX_CHAR 0x03
419#define END_CHAR 0x0D
420#define SPACE 0x20
421#define K_ESCAPE 0x23
422#define SEND_TYPE 'S'
423#define DATA_TYPE 'D'
424#define ACK_TYPE 'Y'
425#define NACK_TYPE 'N'
426#define BREAK_TYPE 'B'
427#define tochar(x) ((char) (((x) + SPACE) & 0xff))
428#define untochar(x) ((int) (((x) - SPACE) & 0xff))
429
wdenk57b2d802003-06-27 21:31:46 +0000430static void set_kerm_bin_mode(unsigned long *);
431static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000432static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000433
434
Kim Phillipsdc00a682012-10-29 13:34:31 +0000435static char his_eol; /* character he needs at end of packet */
436static int his_pad_count; /* number of pad chars he needs */
437static char his_pad_char; /* pad chars he needs */
438static char his_quote; /* quote chars he'll use */
wdenk57b2d802003-06-27 21:31:46 +0000439
Simon Glassed38aef2020-05-10 11:40:03 -0600440static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
441 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000442{
wdenk57b2d802003-06-27 21:31:46 +0000443 ulong offset = 0;
444 ulong addr;
445 int load_baudrate, current_baudrate;
446 int rcode = 0;
447 char *s;
448
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200449 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
450 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000451
452 /* pre-set offset from $loadaddr */
Simon Glass64b723f2017-08-03 12:22:12 -0600453 s = env_get("loadaddr");
454 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600455 offset = hextoul(s, NULL);
wdenk57b2d802003-06-27 21:31:46 +0000456
457 load_baudrate = current_baudrate = gd->baudrate;
458
459 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600460 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000461 }
462 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600463 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000464
465 /* default to current baudrate */
466 if (load_baudrate == 0)
467 load_baudrate = current_baudrate;
468 }
469
470 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000471 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000472 load_baudrate);
473 udelay(50000);
474 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000475 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000476 udelay(50000);
477 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200478 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000479 break;
480 }
481 }
482
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200483 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000484 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200485 "to 0x%08lX at %d bps...\n",
486 offset,
487 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000488
Angus Ainslief283bd02013-06-26 16:54:24 -0600489 addr = load_serial_ymodem(offset, xyzModem_ymodem);
490
Pali Roháre37155f2021-08-03 16:28:43 +0200491 if (addr == ~0) {
492 image_load_addr = 0;
493 printf("## Binary (ymodem) download aborted\n");
494 rcode = 1;
495 } else {
496 printf("## Start Addr = 0x%08lX\n", addr);
497 image_load_addr = addr;
498 }
Angus Ainslief283bd02013-06-26 16:54:24 -0600499 } else if (strcmp(argv[0],"loadx")==0) {
500 printf("## Ready for binary (xmodem) download "
501 "to 0x%08lX at %d bps...\n",
502 offset,
503 load_baudrate);
504
505 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200506
Pali Roháre37155f2021-08-03 16:28:43 +0200507 if (addr == ~0) {
508 image_load_addr = 0;
509 printf("## Binary (xmodem) download aborted\n");
510 rcode = 1;
511 } else {
512 printf("## Start Addr = 0x%08lX\n", addr);
513 image_load_addr = addr;
514 }
wdenk57b2d802003-06-27 21:31:46 +0000515 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200516
Kim Phillipsdc00a682012-10-29 13:34:31 +0000517 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200518 "to 0x%08lX at %d bps...\n",
519 offset,
520 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000521 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000522
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200523 if (addr == ~0) {
Simon Glass892265d2019-12-28 10:45:02 -0700524 image_load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000525 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200526 rcode = 1;
527 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000528 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700529 image_load_addr = addr;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200530 }
531 }
wdenk57b2d802003-06-27 21:31:46 +0000532 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000533 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000534 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000535 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000536 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000537 serial_setbrg();
538 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000539 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200540 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000541 break;
542 }
543 }
544
wdenk57b2d802003-06-27 21:31:46 +0000545 return rcode;
546}
547
548
Kim Phillipsdc00a682012-10-29 13:34:31 +0000549static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000550{
551 int size, i;
wdenk57b2d802003-06-27 21:31:46 +0000552
Kim Phillipsdc00a682012-10-29 13:34:31 +0000553 set_kerm_bin_mode((ulong *) offset);
554 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000555
556 /*
557 * Gather any trailing characters (for instance, the ^D which
558 * is sent by 'cu' after sending a file), and give the
559 * box some time (100 * 1 ms)
560 */
561 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000562 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200563 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000564 }
565 udelay(1000);
566 }
567
Pali Rohár8398dd32021-08-06 18:07:39 +0200568 if (size == 0)
569 return ~0; /* Download aborted */
570
Kim Phillipsdc00a682012-10-29 13:34:31 +0000571 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000572
573 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600574 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000575
576 return offset;
577}
578
Kim Phillipsdc00a682012-10-29 13:34:31 +0000579static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000580{
581 int count = his_pad_count;
582
583 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000584 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000585}
586
587/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000588static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000589{
590 if ((in & 0x60) == 0x40) {
591 return (char) (in & ~0x40);
592 } else if ((in & 0x7f) == 0x3f) {
593 return (char) (in | 0x40);
594 } else
595 return in;
596}
597
Kim Phillipsdc00a682012-10-29 13:34:31 +0000598static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000599{
600 int total = 0;
601
602 while (*buffer) {
603 total += *buffer++;
604 }
605 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
606}
607
Kim Phillipsdc00a682012-10-29 13:34:31 +0000608static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000609{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000610 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000611 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000612 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000613 }
614}
615
616static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000617static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000618{
619 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000620 a_b[1] = tochar(3);
621 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000622 a_b[3] = ACK_TYPE;
623 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000624 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000625 a_b[5] = his_eol;
626 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000627 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000628}
629
Kim Phillipsdc00a682012-10-29 13:34:31 +0000630static void send_nack(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] = NACK_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
643
Kim Phillipsdc00a682012-10-29 13:34:31 +0000644static void (*os_data_init)(void);
645static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000646static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000647static char *os_data_addr, *os_data_addr_saved;
648static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530649
Kim Phillipsdc00a682012-10-29 13:34:31 +0000650static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000651{
652 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000653 os_data_addr = bin_start_address;
654}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530655
Kim Phillipsdc00a682012-10-29 13:34:31 +0000656static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000657{
658 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000659 os_data_addr_saved = os_data_addr;
660}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530661
Kim Phillipsdc00a682012-10-29 13:34:31 +0000662static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000663{
664 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000665 os_data_addr = os_data_addr_saved;
666}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530667
Kim Phillipsdc00a682012-10-29 13:34:31 +0000668static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000669{
670 switch (os_data_state) {
671 case 0: /* data */
672 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000673 break;
674 }
675}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530676
Kim Phillipsdc00a682012-10-29 13:34:31 +0000677static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000678{
679 bin_start_address = (char *) addr;
680 os_data_init = bin_data_init;
681 os_data_char = bin_data_char;
682}
683
684
685/* k_data_* simply handles the kermit escape translations */
686static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000687static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000688{
689 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000690 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000691}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530692
Kim Phillipsdc00a682012-10-29 13:34:31 +0000693static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000694{
695 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000696 os_data_save();
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_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000700{
701 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000702 os_data_restore();
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_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000706{
707 if (k_data_escape) {
708 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000709 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000710 k_data_escape = 0;
711 } else {
712 if (new_char == his_quote) {
713 /* this char is escape - remember */
714 k_data_escape = 1;
715 } else {
716 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000717 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000718 }
719 }
720}
721
722#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000723static char send_parms[SEND_DATA_SIZE];
724static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000725
726/* handle_send_packet interprits the protocol info and builds and
727 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000728static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000729{
730 int length = 3;
731 int bytes;
732
733 /* initialize some protocol parameters */
734 his_eol = END_CHAR; /* default end of line character */
735 his_pad_count = 0;
736 his_pad_char = '\0';
737 his_quote = K_ESCAPE;
738
739 /* ignore last character if it filled the buffer */
740 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
741 --send_ptr;
742 bytes = send_ptr - send_parms; /* how many bytes we'll process */
743 do {
744 if (bytes-- <= 0)
745 break;
746 /* handle MAXL - max length */
747 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000748 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000749 if (bytes-- <= 0)
750 break;
751 /* handle TIME - time you should wait for my packets */
752 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000753 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000754 if (bytes-- <= 0)
755 break;
756 /* handle NPAD - number of pad chars I need */
757 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000758 his_pad_count = untochar(send_parms[2]);
759 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000760 if (bytes-- <= 0)
761 break;
762 /* handle PADC - pad chars I need */
763 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000764 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000765 a_b[++length] = 0x40; /* He should ignore this */
766 if (bytes-- <= 0)
767 break;
768 /* handle EOL - end of line he needs */
769 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000770 his_eol = untochar(send_parms[4]);
771 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000772 if (bytes-- <= 0)
773 break;
774 /* handle QCTL - quote control char he'll use */
775 /* remember what he says - I'll use '#' */
776 his_quote = send_parms[5];
777 a_b[++length] = '#';
778 if (bytes-- <= 0)
779 break;
780 /* handle QBIN - 8-th bit prefixing */
781 /* ignore what he says - I refuse */
782 a_b[++length] = 'N';
783 if (bytes-- <= 0)
784 break;
785 /* handle CHKT - the clock check type */
786 /* ignore what he says - I do type 1 (for now) */
787 a_b[++length] = '1';
788 if (bytes-- <= 0)
789 break;
790 /* handle REPT - the repeat prefix */
791 /* ignore what he says - I refuse (for now) */
792 a_b[++length] = 'N';
793 if (bytes-- <= 0)
794 break;
795 /* handle CAPAS - the capabilities mask */
796 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000797 a_b[++length] = tochar(2); /* only long packets */
798 a_b[++length] = tochar(0); /* no windows */
799 a_b[++length] = tochar(94); /* large packet msb */
800 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000801 } while (0);
802
803 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000804 a_b[1] = tochar(length);
805 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000806 a_b[3] = ACK_TYPE;
807 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000808 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000809 a_b[++length] = his_eol;
810 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000811 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000812}
813
814/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000815static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000816{
817 char new_char;
818 char k_state, k_state_saved;
819 int sum;
820 int done;
821 int length;
822 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000823 int len_lo, len_hi;
824
825 /* initialize some protocol parameters */
826 his_eol = END_CHAR; /* default end of line character */
827 his_pad_count = 0;
828 his_pad_char = '\0';
829 his_quote = K_ESCAPE;
830
831 /* initialize the k_recv and k_data state machine */
832 done = 0;
833 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000834 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000835 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000836 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000837 n = 0; /* just to get rid of a warning */
838 last_n = -1;
839
840 /* expect this "type" sequence (but don't check):
841 S: send initiate
842 F: file header
843 D: data (multiple)
844 Z: end of file
845 B: break transmission
846 */
847
848 /* enter main loop */
849 while (!done) {
850 /* set the send packet pointer to begining of send packet parms */
851 send_ptr = send_parms;
852
853 /* With each packet, start summing the bytes starting with the length.
854 Save the current sequence number.
855 Note the type of the packet.
856 If a character less than SPACE (0x20) is received - error.
857 */
858
859#if 0
860 /* OLD CODE, Prior to checking sequence numbers */
861 /* first have all state machines save current states */
862 k_state_saved = k_state;
863 k_data_save ();
864#endif
865
866 /* get a packet */
867 /* wait for the starting character or ^C */
868 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200869 switch (getchar()) {
wdenk57b2d802003-06-27 21:31:46 +0000870 case START_CHAR: /* start packet */
871 goto START;
872 case ETX_CHAR: /* ^C waiting for packet */
873 return (0);
874 default:
875 ;
876 }
877 }
878START:
879 /* get length of packet */
880 sum = 0;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200881 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000882 if ((new_char & 0xE0) == 0)
883 goto packet_error;
884 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000885 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000886 /* get sequence number */
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 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000892 --length;
893
894 /* NEW CODE - check sequence numbers for retried packets */
895 /* Note - this new code assumes that the sequence number is correctly
896 * received. Handling an invalid sequence number adds another layer
897 * of complexity that may not be needed - yet! At this time, I'm hoping
898 * that I don't need to buffer the incoming data packets and can write
899 * the data into memory in real time.
900 */
901 if (n == last_n) {
902 /* same sequence number, restore the previous state */
903 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000904 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000905 } else {
906 /* new sequence number, checkpoint the download */
907 last_n = n;
908 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000909 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000910 }
911 /* END NEW CODE */
912
913 /* get packet type */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200914 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000915 if ((new_char & 0xE0) == 0)
916 goto packet_error;
917 sum += new_char & 0xff;
918 k_state = new_char;
919 --length;
920 /* check for extended length */
921 if (length == -2) {
922 /* (length byte was 0, decremented twice) */
923 /* get the two length bytes */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200924 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000925 if ((new_char & 0xE0) == 0)
926 goto packet_error;
927 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000928 len_hi = untochar(new_char);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200929 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000930 if ((new_char & 0xE0) == 0)
931 goto packet_error;
932 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000933 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000934 length = len_hi * 95 + len_lo;
935 /* check header checksum */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200936 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000937 if ((new_char & 0xE0) == 0)
938 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000939 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000940 goto packet_error;
941 sum += new_char & 0xff;
942/* --length; */ /* new length includes only data and block check to come */
943 }
944 /* bring in rest of packet */
945 while (length > 1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200946 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000947 if ((new_char & 0xE0) == 0)
948 goto packet_error;
949 sum += new_char & 0xff;
950 --length;
951 if (k_state == DATA_TYPE) {
952 /* pass on the data if this is a data packet */
953 k_data_char (new_char);
954 } else if (k_state == SEND_TYPE) {
955 /* save send pack in buffer as is */
956 *send_ptr++ = new_char;
957 /* if too much data, back off the pointer */
958 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
959 --send_ptr;
960 }
961 }
962 /* get and validate checksum character */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200963 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000964 if ((new_char & 0xE0) == 0)
965 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000966 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000967 goto packet_error;
968 /* get END_CHAR */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200969 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000970 if (new_char != END_CHAR) {
971 packet_error:
972 /* restore state machines */
973 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000974 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000975 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000976 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000977 } else if (k_state == SEND_TYPE) {
978 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000979 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000980 } else {
981 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000982 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000983 /* quit if end of transmission */
984 if (k_state == BREAK_TYPE)
985 done = 1;
986 }
wdenk57b2d802003-06-27 21:31:46 +0000987 }
988 return ((ulong) os_data_addr - (ulong) bin_start_address);
989}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200990
991static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200992 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200993 return (getchar());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200994 return -1;
995}
Angus Ainslief283bd02013-06-26 16:54:24 -0600996static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200997{
998 int size;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200999 int err;
1000 int res;
1001 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001002 char ymodemBuf[1024];
1003 ulong store_addr = ~0;
1004 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001005
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001006 size = 0;
Angus Ainslief283bd02013-06-26 16:54:24 -06001007 info.mode = mode;
Kim Phillipsdc00a682012-10-29 13:34:31 +00001008 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001009 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001010
Pali Rohárb6f177b2021-08-03 16:28:42 +02001011 err = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001012 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +00001013 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001014 store_addr = addr + offset;
1015 size += res;
1016 addr += res;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +09001017#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +00001018 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001019 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001020
Kim Phillipsdc00a682012-10-29 13:34:31 +00001021 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001022 store_addr, res);
1023 if (rc != 0) {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001024 xyzModem_stream_terminate(true, &getcxmodem);
1025 xyzModem_stream_close(&err);
1026 printf("\n");
Simon Glassa606ffc2019-12-28 10:44:40 -07001027 flash_perror(rc);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001028 return (~0);
1029 }
1030 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001031#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001032 {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001033 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001034 res);
1035 }
1036
1037 }
Pali Rohárb6f177b2021-08-03 16:28:42 +02001038 if (err) {
1039 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1040 xyzModem_stream_close(&err);
1041 printf("\n%s\n", xyzModem_error(err));
1042 return (~0); /* Download aborted */
1043 }
1044
Heinrich Schuchardta89adb02021-03-19 02:50:57 +00001045 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1046 efi_set_bootdev("Uart", "", "",
1047 map_sysmem(offset, 0), size);
1048
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001049 } else {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001050 printf("\n%s\n", xyzModem_error(err));
1051 return (~0); /* Download aborted */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001052 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001053
Kim Phillipsdc00a682012-10-29 13:34:31 +00001054 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohárd9c805e2021-08-03 16:28:41 +02001055 xyzModem_stream_close(&err);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001056
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001057
Chris Packhambbaf4242016-10-25 20:22:48 +13001058 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001059
Kim Phillipsdc00a682012-10-29 13:34:31 +00001060 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -06001061 env_set_hex("filesize", size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001062
1063 return offset;
1064}
1065
Jon Loeligerd704d912007-07-10 11:02:44 -05001066#endif
wdenk57b2d802003-06-27 21:31:46 +00001067
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001068#if defined(CONFIG_CMD_LOADM)
1069static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
1070 char *const argv[])
1071{
1072 ulong addr, dest, size;
1073 void *src, *dst;
1074
1075 if (argc != 4)
1076 return CMD_RET_USAGE;
1077
1078 addr = simple_strtoul(argv[1], NULL, 16);
1079
1080 dest = simple_strtoul(argv[2], NULL, 16);
1081
1082 size = simple_strtoul(argv[3], NULL, 16);
1083
1084 if (!size) {
1085 printf("loadm: can not load zero bytes\n");
1086 return 1;
1087 }
1088
1089 src = map_sysmem(addr, size);
1090 dst = map_sysmem(dest, size);
1091
1092 memcpy(dst, src, size);
1093
1094 unmap_sysmem(src);
1095 unmap_sysmem(dst);
1096
1097 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1098 efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
1099
1100 printf("loaded bin to memory: size: %lu\n", size);
1101
1102 return 0;
1103}
1104#endif
1105
wdenk57b2d802003-06-27 21:31:46 +00001106/* -------------------------------------------------------------------- */
1107
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001108#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001109
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001110#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001111U_BOOT_CMD(
1112 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001113 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001114 "[ off ] [ baud ]\n"
1115 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001116 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001117);
1118
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001119#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001120U_BOOT_CMD(
1121 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001122 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001123 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001124 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001125);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001126#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001127
1128/*
1129 * SAVES always requires LOADS support, but not vice versa
1130 */
1131
1132
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001133#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001134#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001135U_BOOT_CMD(
1136 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001137 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001138 "[ off ] [size] [ baud ]\n"
1139 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001140 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001141);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001142#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001143U_BOOT_CMD(
1144 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001145 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001146 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001147 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001148);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001149#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001150#endif /* CONFIG_CMD_SAVES */
1151#endif /* CONFIG_CMD_LOADS */
wdenk57b2d802003-06-27 21:31:46 +00001152
1153
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001154#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001155U_BOOT_CMD(
1156 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001157 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001158 "[ addr [ baud ] ]\n"
wdenk57b2d802003-06-27 21:31:46 +00001159 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001160 " at address 'addr' with baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001161);
1162
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001163U_BOOT_CMD(
Angus Ainslief283bd02013-06-26 16:54:24 -06001164 loadx, 3, 0, do_load_serial_bin,
1165 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001166 "[ addr [ baud ] ]\n"
Angus Ainslief283bd02013-06-26 16:54:24 -06001167 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001168 " at address 'addr' with baudrate 'baud'"
Angus Ainslief283bd02013-06-26 16:54:24 -06001169);
1170
1171U_BOOT_CMD(
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001172 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001173 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001174 "[ addr [ baud ] ]\n"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001175 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001176 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001177);
1178
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001179#endif /* CONFIG_CMD_LOADB */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001180
1181#if defined(CONFIG_CMD_LOADM)
1182U_BOOT_CMD(
1183 loadm, 4, 0, do_load_memory_bin,
1184 "load binary blob from source address to destination address",
1185 "[src_addr] [dst_addr] [size]\n"
1186 " - load a binary blob from one memory location to other"
1187 " from src_addr to dst_addr by size bytes"
1188);
1189#endif /* CONFIG_CMD_LOADM */