blob: 713fe56b5557a08a2886f966216ed93f9a5ab2c7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk8bde7f72003-06-27 21:31:46 +00002/*
wdenk232c1502004-03-12 00:14:09 +00003 * (C) Copyright 2000-2004
wdenk8bde7f72003-06-27 21:31:46 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk8bde7f72003-06-27 21:31:46 +00005 */
6
7/*
8 * Serial up- and download support
9 */
10#include <common.h>
11#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060013#include <env.h>
wdenk8bde7f72003-06-27 21:31:46 +000014#include <s_record.h>
15#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000016#include <exports.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020017#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000018
Wolfgang Denkd87080b2006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000020
Jon Loeligerc76fe472007-07-08 18:02:23 -050021#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060022static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020023#endif
24
Jon Loeligerc76fe472007-07-08 18:02:23 -050025#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000026static ulong load_serial(long offset);
27static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050028# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000029static int save_serial(ulong offset, ulong size);
30static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050031#endif
wdenk8bde7f72003-06-27 21:31:46 +000032
33static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050034#endif
wdenk8bde7f72003-06-27 21:31:46 +000035
36/* -------------------------------------------------------------------- */
37
Jon Loeligerc76fe472007-07-08 18:02:23 -050038#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000039static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
40 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000041{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020042 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000043 ulong addr;
44 int i;
45 char *env_echo;
46 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000048 int load_baudrate, current_baudrate;
49
50 load_baudrate = current_baudrate = gd->baudrate;
51#endif
52
Simon Glass00caae62017-08-03 12:22:12 -060053 env_echo = env_get("loads_echo");
54 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000055 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060056 else
wdenk8bde7f72003-06-27 21:31:46 +000057 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000058
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020059#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000060 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020061 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000062 }
63 if (argc == 3) {
64 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
65
66 /* default to current baudrate */
67 if (load_baudrate == 0)
68 load_baudrate = current_baudrate;
69 }
70 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000071 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000072 load_baudrate);
73 udelay(50000);
74 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000075 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000076 udelay(50000);
77 for (;;) {
78 if (getc() == '\r')
79 break;
80 }
81 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000083 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020084 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000085 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000087
Kim Phillips088f1b12012-10-29 13:34:31 +000088 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000089
Kim Phillips088f1b12012-10-29 13:34:31 +000090 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000091
92 /*
93 * Gather any trailing characters (for instance, the ^D which
94 * is sent by 'cu' after sending a file), and give the
95 * box some time (100 * 1 ms)
96 */
97 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +000098 if (tstc()) {
99 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000100 }
101 udelay(1000);
102 }
103
104 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000105 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000106 rcode = 1;
107 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000108 printf("## Start Addr = 0x%08lX\n", addr);
wdenk8bde7f72003-06-27 21:31:46 +0000109 load_addr = addr;
110 }
111
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000113 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000114 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000115 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000116 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000117 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000118 serial_setbrg();
119 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000120 for (;;) {
121 if (getc() == 0x1B) /* ESC */
122 break;
123 }
124 }
125#endif
126 return rcode;
127}
128
Kim Phillips088f1b12012-10-29 13:34:31 +0000129static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000130{
131 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
132 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
133 int binlen; /* no. of data bytes in S-Rec. */
134 int type; /* return code for record type */
135 ulong addr; /* load address from S-Record */
136 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000137 ulong store_addr;
138 ulong start_addr = ~0;
139 ulong end_addr = 0;
140 int line_count = 0;
141
142 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000143 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000144
145 if (type < 0) {
146 return (~0); /* Invalid S-Record */
147 }
148
149 switch (type) {
150 case SREC_DATA2:
151 case SREC_DATA3:
152 case SREC_DATA4:
153 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900154#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000155 if (addr2info(store_addr)) {
156 int rc;
157
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200158 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000159 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000160 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000161 return (~0);
162 }
163 } else
164#endif
165 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000166 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000167 }
168 if ((store_addr) < start_addr)
169 start_addr = store_addr;
170 if ((store_addr + binlen - 1) > end_addr)
171 end_addr = store_addr + binlen - 1;
172 break;
173 case SREC_END2:
174 case SREC_END3:
175 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000176 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000177 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000178 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000179 "## First Load Addr = 0x%08lX\n"
180 "## Last Load Addr = 0x%08lX\n"
181 "## Total Size = 0x%08lX = %ld Bytes\n",
182 start_addr, end_addr, size, size
183 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000184 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600185 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000186 return (addr);
187 case SREC_START:
188 break;
189 default:
190 break;
191 }
192 if (!do_echo) { /* print a '.' every 100 lines */
193 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000194 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000195 }
196 }
197
198 return (~0); /* Download aborted */
199}
200
Kim Phillips088f1b12012-10-29 13:34:31 +0000201static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000202{
203 char *p;
204 char c;
205
206 --len; /* always leave room for terminating '\0' byte */
207
208 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000209 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000210 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000211 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000212
213 switch (c) {
214 case '\r':
215 case '\n':
216 *p = '\0';
217 return (p - buf);
218 case '\0':
219 case 0x03: /* ^C - Control C */
220 return (-1);
221 default:
222 *p = c;
223 }
224
225 /* Check for the console hangup (if any different from serial) */
Martin Dorwig49cad542015-01-26 15:22:54 -0700226 if (gd->jt->getc != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000227 if (ctrlc()) {
228 return (-1);
229 }
230 }
wdenk8bde7f72003-06-27 21:31:46 +0000231 }
232
233 /* line too long - truncate */
234 *p = '\0';
235 return (p - buf);
236}
237
Jon Loeligerc76fe472007-07-08 18:02:23 -0500238#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000239
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200240int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000241{
242 ulong offset = 0;
243 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000245 int save_baudrate, current_baudrate;
246
247 save_baudrate = current_baudrate = gd->baudrate;
248#endif
249
250 if (argc >= 2) {
251 offset = simple_strtoul(argv[1], NULL, 16);
252 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200253#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000254 if (argc >= 3) {
255 size = simple_strtoul(argv[2], NULL, 16);
256 }
257 if (argc == 4) {
258 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
259
260 /* default to current baudrate */
261 if (save_baudrate == 0)
262 save_baudrate = current_baudrate;
263 }
264 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000265 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000266 save_baudrate);
267 udelay(50000);
268 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000269 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000270 udelay(50000);
271 for (;;) {
272 if (getc() == '\r')
273 break;
274 }
275 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200276#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000277 if (argc == 3) {
278 size = simple_strtoul(argv[2], NULL, 16);
279 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000281
Kim Phillips088f1b12012-10-29 13:34:31 +0000282 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000283 for (;;) {
284 if (getc() == '\r')
285 break;
286 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000287 if (save_serial(offset, size)) {
288 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000289 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000290 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000291 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200292#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000293 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000294 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000295 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000296 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000297 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000298 serial_setbrg();
299 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000300 for (;;) {
301 if (getc() == 0x1B) /* ESC */
302 break;
303 }
304 }
305#endif
306 return 0;
307}
308
309#define SREC3_START "S0030000FC\n"
310#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
311#define SREC3_END "S70500000000FA\n"
312#define SREC_BYTES_PER_RECORD 16
313
Kim Phillips088f1b12012-10-29 13:34:31 +0000314static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000315{
316 int i, c, reclen, checksum, length;
317 char *hex = "0123456789ABCDEF";
318 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
319 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
320
321 reclen = 0;
322 checksum = 0;
323
324 if(write_record(SREC3_START)) /* write the header */
325 return (-1);
326 do {
327 if(count) { /* collect hex data in the buffer */
328 c = *(volatile uchar*)(address + reclen); /* get one byte */
329 checksum += c; /* accumulate checksum */
330 data[2*reclen] = hex[(c>>4)&0x0f];
331 data[2*reclen+1] = hex[c & 0x0f];
332 data[2*reclen+2] = '\0';
333 ++reclen;
334 --count;
335 }
336 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
337 /* enough data collected for one record: dump it */
338 if(reclen) { /* build & write a data record: */
339 /* address + data + checksum */
340 length = 4 + reclen + 1;
341
342 /* accumulate length bytes into checksum */
343 for(i = 0; i < 2; i++)
344 checksum += (length >> (8*i)) & 0xff;
345
346 /* accumulate address bytes into checksum: */
347 for(i = 0; i < 4; i++)
348 checksum += (address >> (8*i)) & 0xff;
349
350 /* make proper checksum byte: */
351 checksum = ~checksum & 0xff;
352
353 /* output one record: */
354 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
355 if(write_record(record))
356 return (-1);
357 }
358 address += reclen; /* increment address */
359 checksum = 0;
360 reclen = 0;
361 }
362 }
363 while(count);
364 if(write_record(SREC3_END)) /* write the final record */
365 return (-1);
366 return(0);
367}
368
Kim Phillips088f1b12012-10-29 13:34:31 +0000369static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000370{
371 char c;
372
373 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000374 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000375
376 /* Check for the console hangup (if any different from serial) */
377
378 if (ctrlc()) {
379 return (-1);
380 }
381 return (0);
382}
Jon Loeliger90253172007-07-10 11:02:44 -0500383# endif
wdenk8bde7f72003-06-27 21:31:46 +0000384
Jon Loeliger90253172007-07-10 11:02:44 -0500385#endif
wdenk8bde7f72003-06-27 21:31:46 +0000386
387
Jon Loeligerc76fe472007-07-08 18:02:23 -0500388#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500389/*
390 * loadb command (load binary) included
391 */
wdenk8bde7f72003-06-27 21:31:46 +0000392#define XON_CHAR 17
393#define XOFF_CHAR 19
394#define START_CHAR 0x01
395#define ETX_CHAR 0x03
396#define END_CHAR 0x0D
397#define SPACE 0x20
398#define K_ESCAPE 0x23
399#define SEND_TYPE 'S'
400#define DATA_TYPE 'D'
401#define ACK_TYPE 'Y'
402#define NACK_TYPE 'N'
403#define BREAK_TYPE 'B'
404#define tochar(x) ((char) (((x) + SPACE) & 0xff))
405#define untochar(x) ((int) (((x) - SPACE) & 0xff))
406
wdenk8bde7f72003-06-27 21:31:46 +0000407static void set_kerm_bin_mode(unsigned long *);
408static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000409static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000410
411
Kim Phillips088f1b12012-10-29 13:34:31 +0000412static char his_eol; /* character he needs at end of packet */
413static int his_pad_count; /* number of pad chars he needs */
414static char his_pad_char; /* pad chars he needs */
415static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000416
Kim Phillips088f1b12012-10-29 13:34:31 +0000417static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
418 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000419{
wdenk8bde7f72003-06-27 21:31:46 +0000420 ulong offset = 0;
421 ulong addr;
422 int load_baudrate, current_baudrate;
423 int rcode = 0;
424 char *s;
425
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200426 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
427 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000428
429 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600430 s = env_get("loadaddr");
431 if (s)
wdenk8bde7f72003-06-27 21:31:46 +0000432 offset = simple_strtoul(s, NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000433
434 load_baudrate = current_baudrate = gd->baudrate;
435
436 if (argc >= 2) {
437 offset = simple_strtoul(argv[1], NULL, 16);
438 }
439 if (argc == 3) {
440 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
441
442 /* default to current baudrate */
443 if (load_baudrate == 0)
444 load_baudrate = current_baudrate;
445 }
446
447 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000448 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000449 load_baudrate);
450 udelay(50000);
451 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000452 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000453 udelay(50000);
454 for (;;) {
455 if (getc() == '\r')
456 break;
457 }
458 }
459
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200460 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000461 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200462 "to 0x%08lX at %d bps...\n",
463 offset,
464 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000465
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600466 addr = load_serial_ymodem(offset, xyzModem_ymodem);
467
468 } else if (strcmp(argv[0],"loadx")==0) {
469 printf("## Ready for binary (xmodem) download "
470 "to 0x%08lX at %d bps...\n",
471 offset,
472 load_baudrate);
473
474 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200475
wdenk8bde7f72003-06-27 21:31:46 +0000476 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000477
Kim Phillips088f1b12012-10-29 13:34:31 +0000478 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200479 "to 0x%08lX at %d bps...\n",
480 offset,
481 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000482 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200483
484 if (addr == ~0) {
485 load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000486 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200487 rcode = 1;
488 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000489 printf("## Start Addr = 0x%08lX\n", addr);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200490 load_addr = addr;
491 }
492 }
wdenk8bde7f72003-06-27 21:31:46 +0000493 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000494 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000495 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000496 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000497 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000498 serial_setbrg();
499 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000500 for (;;) {
501 if (getc() == 0x1B) /* ESC */
502 break;
503 }
504 }
505
wdenk8bde7f72003-06-27 21:31:46 +0000506 return rcode;
507}
508
509
Kim Phillips088f1b12012-10-29 13:34:31 +0000510static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000511{
512 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000513
Kim Phillips088f1b12012-10-29 13:34:31 +0000514 set_kerm_bin_mode((ulong *) offset);
515 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000516
517 /*
518 * Gather any trailing characters (for instance, the ^D which
519 * is sent by 'cu' after sending a file), and give the
520 * box some time (100 * 1 ms)
521 */
522 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000523 if (tstc()) {
524 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000525 }
526 udelay(1000);
527 }
528
Kim Phillips088f1b12012-10-29 13:34:31 +0000529 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000530
531 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600532 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000533
534 return offset;
535}
536
Kim Phillips088f1b12012-10-29 13:34:31 +0000537static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000538{
539 int count = his_pad_count;
540
541 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000542 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000543}
544
545/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000546static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000547{
548 if ((in & 0x60) == 0x40) {
549 return (char) (in & ~0x40);
550 } else if ((in & 0x7f) == 0x3f) {
551 return (char) (in | 0x40);
552 } else
553 return in;
554}
555
Kim Phillips088f1b12012-10-29 13:34:31 +0000556static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000557{
558 int total = 0;
559
560 while (*buffer) {
561 total += *buffer++;
562 }
563 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
564}
565
Kim Phillips088f1b12012-10-29 13:34:31 +0000566static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000567{
Kim Phillips088f1b12012-10-29 13:34:31 +0000568 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000569 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000570 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000571 }
572}
573
574static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000575static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000576{
577 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000578 a_b[1] = tochar(3);
579 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000580 a_b[3] = ACK_TYPE;
581 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000582 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000583 a_b[5] = his_eol;
584 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000585 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000586}
587
Kim Phillips088f1b12012-10-29 13:34:31 +0000588static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000589{
590 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000591 a_b[1] = tochar(3);
592 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000593 a_b[3] = NACK_TYPE;
594 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000595 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000596 a_b[5] = his_eol;
597 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000598 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000599}
600
601
Kim Phillips088f1b12012-10-29 13:34:31 +0000602static void (*os_data_init)(void);
603static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000604static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000605static char *os_data_addr, *os_data_addr_saved;
606static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530607
Kim Phillips088f1b12012-10-29 13:34:31 +0000608static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000609{
610 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000611 os_data_addr = bin_start_address;
612}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530613
Kim Phillips088f1b12012-10-29 13:34:31 +0000614static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000615{
616 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000617 os_data_addr_saved = os_data_addr;
618}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530619
Kim Phillips088f1b12012-10-29 13:34:31 +0000620static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000621{
622 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000623 os_data_addr = os_data_addr_saved;
624}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530625
Kim Phillips088f1b12012-10-29 13:34:31 +0000626static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000627{
628 switch (os_data_state) {
629 case 0: /* data */
630 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000631 break;
632 }
633}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530634
Kim Phillips088f1b12012-10-29 13:34:31 +0000635static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000636{
637 bin_start_address = (char *) addr;
638 os_data_init = bin_data_init;
639 os_data_char = bin_data_char;
640}
641
642
643/* k_data_* simply handles the kermit escape translations */
644static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000645static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000646{
647 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000648 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000649}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530650
Kim Phillips088f1b12012-10-29 13:34:31 +0000651static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000652{
653 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000654 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000655}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530656
Kim Phillips088f1b12012-10-29 13:34:31 +0000657static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000658{
659 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000660 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000661}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530662
Kim Phillips088f1b12012-10-29 13:34:31 +0000663static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000664{
665 if (k_data_escape) {
666 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000667 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000668 k_data_escape = 0;
669 } else {
670 if (new_char == his_quote) {
671 /* this char is escape - remember */
672 k_data_escape = 1;
673 } else {
674 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000675 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000676 }
677 }
678}
679
680#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000681static char send_parms[SEND_DATA_SIZE];
682static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000683
684/* handle_send_packet interprits the protocol info and builds and
685 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000686static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000687{
688 int length = 3;
689 int bytes;
690
691 /* initialize some protocol parameters */
692 his_eol = END_CHAR; /* default end of line character */
693 his_pad_count = 0;
694 his_pad_char = '\0';
695 his_quote = K_ESCAPE;
696
697 /* ignore last character if it filled the buffer */
698 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
699 --send_ptr;
700 bytes = send_ptr - send_parms; /* how many bytes we'll process */
701 do {
702 if (bytes-- <= 0)
703 break;
704 /* handle MAXL - max length */
705 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000706 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000707 if (bytes-- <= 0)
708 break;
709 /* handle TIME - time you should wait for my packets */
710 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000711 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000712 if (bytes-- <= 0)
713 break;
714 /* handle NPAD - number of pad chars I need */
715 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000716 his_pad_count = untochar(send_parms[2]);
717 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000718 if (bytes-- <= 0)
719 break;
720 /* handle PADC - pad chars I need */
721 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000722 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000723 a_b[++length] = 0x40; /* He should ignore this */
724 if (bytes-- <= 0)
725 break;
726 /* handle EOL - end of line he needs */
727 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000728 his_eol = untochar(send_parms[4]);
729 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000730 if (bytes-- <= 0)
731 break;
732 /* handle QCTL - quote control char he'll use */
733 /* remember what he says - I'll use '#' */
734 his_quote = send_parms[5];
735 a_b[++length] = '#';
736 if (bytes-- <= 0)
737 break;
738 /* handle QBIN - 8-th bit prefixing */
739 /* ignore what he says - I refuse */
740 a_b[++length] = 'N';
741 if (bytes-- <= 0)
742 break;
743 /* handle CHKT - the clock check type */
744 /* ignore what he says - I do type 1 (for now) */
745 a_b[++length] = '1';
746 if (bytes-- <= 0)
747 break;
748 /* handle REPT - the repeat prefix */
749 /* ignore what he says - I refuse (for now) */
750 a_b[++length] = 'N';
751 if (bytes-- <= 0)
752 break;
753 /* handle CAPAS - the capabilities mask */
754 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000755 a_b[++length] = tochar(2); /* only long packets */
756 a_b[++length] = tochar(0); /* no windows */
757 a_b[++length] = tochar(94); /* large packet msb */
758 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000759 } while (0);
760
761 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000762 a_b[1] = tochar(length);
763 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000764 a_b[3] = ACK_TYPE;
765 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000766 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000767 a_b[++length] = his_eol;
768 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000769 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000770}
771
772/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000773static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000774{
775 char new_char;
776 char k_state, k_state_saved;
777 int sum;
778 int done;
779 int length;
780 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000781 int len_lo, len_hi;
782
783 /* initialize some protocol parameters */
784 his_eol = END_CHAR; /* default end of line character */
785 his_pad_count = 0;
786 his_pad_char = '\0';
787 his_quote = K_ESCAPE;
788
789 /* initialize the k_recv and k_data state machine */
790 done = 0;
791 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000792 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000793 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000794 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000795 n = 0; /* just to get rid of a warning */
796 last_n = -1;
797
798 /* expect this "type" sequence (but don't check):
799 S: send initiate
800 F: file header
801 D: data (multiple)
802 Z: end of file
803 B: break transmission
804 */
805
806 /* enter main loop */
807 while (!done) {
808 /* set the send packet pointer to begining of send packet parms */
809 send_ptr = send_parms;
810
811 /* With each packet, start summing the bytes starting with the length.
812 Save the current sequence number.
813 Note the type of the packet.
814 If a character less than SPACE (0x20) is received - error.
815 */
816
817#if 0
818 /* OLD CODE, Prior to checking sequence numbers */
819 /* first have all state machines save current states */
820 k_state_saved = k_state;
821 k_data_save ();
822#endif
823
824 /* get a packet */
825 /* wait for the starting character or ^C */
826 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000827 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000828 case START_CHAR: /* start packet */
829 goto START;
830 case ETX_CHAR: /* ^C waiting for packet */
831 return (0);
832 default:
833 ;
834 }
835 }
836START:
837 /* get length of packet */
838 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000839 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000840 if ((new_char & 0xE0) == 0)
841 goto packet_error;
842 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000843 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000844 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +0000845 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000846 if ((new_char & 0xE0) == 0)
847 goto packet_error;
848 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000849 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000850 --length;
851
852 /* NEW CODE - check sequence numbers for retried packets */
853 /* Note - this new code assumes that the sequence number is correctly
854 * received. Handling an invalid sequence number adds another layer
855 * of complexity that may not be needed - yet! At this time, I'm hoping
856 * that I don't need to buffer the incoming data packets and can write
857 * the data into memory in real time.
858 */
859 if (n == last_n) {
860 /* same sequence number, restore the previous state */
861 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000862 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000863 } else {
864 /* new sequence number, checkpoint the download */
865 last_n = n;
866 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000867 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000868 }
869 /* END NEW CODE */
870
871 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000872 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000873 if ((new_char & 0xE0) == 0)
874 goto packet_error;
875 sum += new_char & 0xff;
876 k_state = new_char;
877 --length;
878 /* check for extended length */
879 if (length == -2) {
880 /* (length byte was 0, decremented twice) */
881 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000882 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000883 if ((new_char & 0xE0) == 0)
884 goto packet_error;
885 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000886 len_hi = untochar(new_char);
887 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000888 if ((new_char & 0xE0) == 0)
889 goto packet_error;
890 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000891 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000892 length = len_hi * 95 + len_lo;
893 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000894 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000895 if ((new_char & 0xE0) == 0)
896 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000897 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000898 goto packet_error;
899 sum += new_char & 0xff;
900/* --length; */ /* new length includes only data and block check to come */
901 }
902 /* bring in rest of packet */
903 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000904 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000905 if ((new_char & 0xE0) == 0)
906 goto packet_error;
907 sum += new_char & 0xff;
908 --length;
909 if (k_state == DATA_TYPE) {
910 /* pass on the data if this is a data packet */
911 k_data_char (new_char);
912 } else if (k_state == SEND_TYPE) {
913 /* save send pack in buffer as is */
914 *send_ptr++ = new_char;
915 /* if too much data, back off the pointer */
916 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
917 --send_ptr;
918 }
919 }
920 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000921 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000922 if ((new_char & 0xE0) == 0)
923 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000924 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000925 goto packet_error;
926 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000927 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000928 if (new_char != END_CHAR) {
929 packet_error:
930 /* restore state machines */
931 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000932 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000933 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000934 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000935 } else if (k_state == SEND_TYPE) {
936 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000937 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000938 } else {
939 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000940 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000941 /* quit if end of transmission */
942 if (k_state == BREAK_TYPE)
943 done = 1;
944 }
wdenk8bde7f72003-06-27 21:31:46 +0000945 }
946 return ((ulong) os_data_addr - (ulong) bin_start_address);
947}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200948
949static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200950 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200951 return (getc());
952 return -1;
953}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600954static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200955{
956 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200957 int err;
958 int res;
959 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200960 char ymodemBuf[1024];
961 ulong store_addr = ~0;
962 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200963
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200964 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600965 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000966 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200967 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200968
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200969 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000970 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200971 store_addr = addr + offset;
972 size += res;
973 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900974#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000975 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200976 int rc;
977
Kim Phillips088f1b12012-10-29 13:34:31 +0000978 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200979 store_addr, res);
980 if (rc != 0) {
981 flash_perror (rc);
982 return (~0);
983 }
984 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200985#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200986 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000987 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200988 res);
989 }
990
991 }
992 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000993 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200994 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200995
Kim Phillips088f1b12012-10-29 13:34:31 +0000996 xyzModem_stream_close(&err);
997 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200998
999
Chris Packham0a6036d2016-10-25 20:22:48 +13001000 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001001
Kim Phillips088f1b12012-10-29 13:34:31 +00001002 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001003 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001004
1005 return offset;
1006}
1007
Jon Loeliger90253172007-07-10 11:02:44 -05001008#endif
wdenk8bde7f72003-06-27 21:31:46 +00001009
1010/* -------------------------------------------------------------------- */
1011
Jon Loeligerc76fe472007-07-08 18:02:23 -05001012#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001013
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001014#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001015U_BOOT_CMD(
1016 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001017 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001018 "[ off ] [ baud ]\n"
1019 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001020 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001021);
1022
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001023#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001024U_BOOT_CMD(
1025 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001026 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001027 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001028 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001029);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001030#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001031
1032/*
1033 * SAVES always requires LOADS support, but not vice versa
1034 */
1035
1036
Jon Loeligerc76fe472007-07-08 18:02:23 -05001037#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001038#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001039U_BOOT_CMD(
1040 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001041 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001042 "[ off ] [size] [ baud ]\n"
1043 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001044 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001045);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001046#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001047U_BOOT_CMD(
1048 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001049 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001050 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001051 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001052);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001053#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001054#endif /* CONFIG_CMD_SAVES */
1055#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001056
1057
Jon Loeligerc76fe472007-07-08 18:02:23 -05001058#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001059U_BOOT_CMD(
1060 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001061 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001062 "[ off ] [ baud ]\n"
1063 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001064 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001065);
1066
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001067U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001068 loadx, 3, 0, do_load_serial_bin,
1069 "load binary file over serial line (xmodem mode)",
1070 "[ off ] [ baud ]\n"
1071 " - load binary file over serial line"
1072 " with offset 'off' and baudrate 'baud'"
1073);
1074
1075U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001076 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001077 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001078 "[ off ] [ baud ]\n"
1079 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001080 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001081);
1082
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001083#endif /* CONFIG_CMD_LOADB */