blob: f6e522cbb3482bf48eeae51cfe0d2e1467404de3 [file] [log] [blame]
wdenk8bde7f72003-06-27 21:31:46 +00001/*
wdenk232c1502004-03-12 00:14:09 +00002 * (C) Copyright 2000-2004
wdenk8bde7f72003-06-27 21:31:46 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk8bde7f72003-06-27 21:31:46 +00006 */
7
8/*
9 * Serial up- and download support
10 */
11#include <common.h>
12#include <command.h>
wdenk8bde7f72003-06-27 21:31:46 +000013#include <s_record.h>
14#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000015#include <exports.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020016#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000017
Wolfgang Denkd87080b2006-03-31 18:32:53 +020018DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000019
Jon Loeligerc76fe472007-07-08 18:02:23 -050020#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060021static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020022#endif
23
Jon Loeligerc76fe472007-07-08 18:02:23 -050024#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000025static ulong load_serial(long offset);
26static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050027# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000028static int save_serial(ulong offset, ulong size);
29static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050030#endif
wdenk8bde7f72003-06-27 21:31:46 +000031
32static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050033#endif
wdenk8bde7f72003-06-27 21:31:46 +000034
35/* -------------------------------------------------------------------- */
36
Jon Loeligerc76fe472007-07-08 18:02:23 -050037#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000038static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
39 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000040{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020041 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000042 ulong addr;
43 int i;
44 char *env_echo;
45 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000047 int load_baudrate, current_baudrate;
48
49 load_baudrate = current_baudrate = gd->baudrate;
50#endif
51
52 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
53 do_echo = 1;
54 } else {
55 do_echo = 0;
56 }
57
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000059 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020060 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000061 }
62 if (argc == 3) {
63 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
64
65 /* default to current baudrate */
66 if (load_baudrate == 0)
67 load_baudrate = current_baudrate;
68 }
69 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000070 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000071 load_baudrate);
72 udelay(50000);
73 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000074 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000075 udelay(50000);
76 for (;;) {
77 if (getc() == '\r')
78 break;
79 }
80 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000082 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020083 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000084 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000086
Kim Phillips088f1b12012-10-29 13:34:31 +000087 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000088
Kim Phillips088f1b12012-10-29 13:34:31 +000089 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000090
91 /*
92 * Gather any trailing characters (for instance, the ^D which
93 * is sent by 'cu' after sending a file), and give the
94 * box some time (100 * 1 ms)
95 */
96 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +000097 if (tstc()) {
98 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +000099 }
100 udelay(1000);
101 }
102
103 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000104 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000105 rcode = 1;
106 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000107 printf("## Start Addr = 0x%08lX\n", addr);
wdenk8bde7f72003-06-27 21:31:46 +0000108 load_addr = addr;
109 }
110
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000112 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000113 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000114 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000115 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000116 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000117 serial_setbrg();
118 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000119 for (;;) {
120 if (getc() == 0x1B) /* ESC */
121 break;
122 }
123 }
124#endif
125 return rcode;
126}
127
Kim Phillips088f1b12012-10-29 13:34:31 +0000128static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000129{
130 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
131 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
132 int binlen; /* no. of data bytes in S-Rec. */
133 int type; /* return code for record type */
134 ulong addr; /* load address from S-Record */
135 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000136 ulong store_addr;
137 ulong start_addr = ~0;
138 ulong end_addr = 0;
139 int line_count = 0;
140
141 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000142 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000143
144 if (type < 0) {
145 return (~0); /* Invalid S-Record */
146 }
147
148 switch (type) {
149 case SREC_DATA2:
150 case SREC_DATA3:
151 case SREC_DATA4:
152 store_addr = addr + offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200153#ifndef CONFIG_SYS_NO_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000154 if (addr2info(store_addr)) {
155 int rc;
156
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200157 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000158 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000159 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000160 return (~0);
161 }
162 } else
163#endif
164 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000165 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000166 }
167 if ((store_addr) < start_addr)
168 start_addr = store_addr;
169 if ((store_addr + binlen - 1) > end_addr)
170 end_addr = store_addr + binlen - 1;
171 break;
172 case SREC_END2:
173 case SREC_END3:
174 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000175 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000176 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000177 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000178 "## First Load Addr = 0x%08lX\n"
179 "## Last Load Addr = 0x%08lX\n"
180 "## Total Size = 0x%08lX = %ld Bytes\n",
181 start_addr, end_addr, size, size
182 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000183 flush_cache(start_addr, size);
Simon Glass41ef3722013-02-24 17:33:22 +0000184 setenv_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000185 return (addr);
186 case SREC_START:
187 break;
188 default:
189 break;
190 }
191 if (!do_echo) { /* print a '.' every 100 lines */
192 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000193 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000194 }
195 }
196
197 return (~0); /* Download aborted */
198}
199
Kim Phillips088f1b12012-10-29 13:34:31 +0000200static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000201{
202 char *p;
203 char c;
204
205 --len; /* always leave room for terminating '\0' byte */
206
207 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000208 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000209 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000210 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000211
212 switch (c) {
213 case '\r':
214 case '\n':
215 *p = '\0';
216 return (p - buf);
217 case '\0':
218 case 0x03: /* ^C - Control C */
219 return (-1);
220 default:
221 *p = c;
222 }
223
224 /* Check for the console hangup (if any different from serial) */
wdenk232c1502004-03-12 00:14:09 +0000225 if (gd->jt[XF_getc] != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000226 if (ctrlc()) {
227 return (-1);
228 }
229 }
wdenk8bde7f72003-06-27 21:31:46 +0000230 }
231
232 /* line too long - truncate */
233 *p = '\0';
234 return (p - buf);
235}
236
Jon Loeligerc76fe472007-07-08 18:02:23 -0500237#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000238
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200239int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000240{
241 ulong offset = 0;
242 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200243#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000244 int save_baudrate, current_baudrate;
245
246 save_baudrate = current_baudrate = gd->baudrate;
247#endif
248
249 if (argc >= 2) {
250 offset = simple_strtoul(argv[1], NULL, 16);
251 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200252#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000253 if (argc >= 3) {
254 size = simple_strtoul(argv[2], NULL, 16);
255 }
256 if (argc == 4) {
257 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
258
259 /* default to current baudrate */
260 if (save_baudrate == 0)
261 save_baudrate = current_baudrate;
262 }
263 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000264 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000265 save_baudrate);
266 udelay(50000);
267 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000268 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000269 udelay(50000);
270 for (;;) {
271 if (getc() == '\r')
272 break;
273 }
274 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200275#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000276 if (argc == 3) {
277 size = simple_strtoul(argv[2], NULL, 16);
278 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200279#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000280
Kim Phillips088f1b12012-10-29 13:34:31 +0000281 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000282 for (;;) {
283 if (getc() == '\r')
284 break;
285 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000286 if (save_serial(offset, size)) {
287 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000288 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000289 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000290 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200291#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000292 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000293 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000294 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000295 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000296 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000297 serial_setbrg();
298 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000299 for (;;) {
300 if (getc() == 0x1B) /* ESC */
301 break;
302 }
303 }
304#endif
305 return 0;
306}
307
308#define SREC3_START "S0030000FC\n"
309#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
310#define SREC3_END "S70500000000FA\n"
311#define SREC_BYTES_PER_RECORD 16
312
Kim Phillips088f1b12012-10-29 13:34:31 +0000313static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000314{
315 int i, c, reclen, checksum, length;
316 char *hex = "0123456789ABCDEF";
317 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
318 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
319
320 reclen = 0;
321 checksum = 0;
322
323 if(write_record(SREC3_START)) /* write the header */
324 return (-1);
325 do {
326 if(count) { /* collect hex data in the buffer */
327 c = *(volatile uchar*)(address + reclen); /* get one byte */
328 checksum += c; /* accumulate checksum */
329 data[2*reclen] = hex[(c>>4)&0x0f];
330 data[2*reclen+1] = hex[c & 0x0f];
331 data[2*reclen+2] = '\0';
332 ++reclen;
333 --count;
334 }
335 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
336 /* enough data collected for one record: dump it */
337 if(reclen) { /* build & write a data record: */
338 /* address + data + checksum */
339 length = 4 + reclen + 1;
340
341 /* accumulate length bytes into checksum */
342 for(i = 0; i < 2; i++)
343 checksum += (length >> (8*i)) & 0xff;
344
345 /* accumulate address bytes into checksum: */
346 for(i = 0; i < 4; i++)
347 checksum += (address >> (8*i)) & 0xff;
348
349 /* make proper checksum byte: */
350 checksum = ~checksum & 0xff;
351
352 /* output one record: */
353 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
354 if(write_record(record))
355 return (-1);
356 }
357 address += reclen; /* increment address */
358 checksum = 0;
359 reclen = 0;
360 }
361 }
362 while(count);
363 if(write_record(SREC3_END)) /* write the final record */
364 return (-1);
365 return(0);
366}
367
Kim Phillips088f1b12012-10-29 13:34:31 +0000368static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000369{
370 char c;
371
372 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000373 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000374
375 /* Check for the console hangup (if any different from serial) */
376
377 if (ctrlc()) {
378 return (-1);
379 }
380 return (0);
381}
Jon Loeliger90253172007-07-10 11:02:44 -0500382# endif
wdenk8bde7f72003-06-27 21:31:46 +0000383
Jon Loeliger90253172007-07-10 11:02:44 -0500384#endif
wdenk8bde7f72003-06-27 21:31:46 +0000385
386
Jon Loeligerc76fe472007-07-08 18:02:23 -0500387#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500388/*
389 * loadb command (load binary) included
390 */
wdenk8bde7f72003-06-27 21:31:46 +0000391#define XON_CHAR 17
392#define XOFF_CHAR 19
393#define START_CHAR 0x01
394#define ETX_CHAR 0x03
395#define END_CHAR 0x0D
396#define SPACE 0x20
397#define K_ESCAPE 0x23
398#define SEND_TYPE 'S'
399#define DATA_TYPE 'D'
400#define ACK_TYPE 'Y'
401#define NACK_TYPE 'N'
402#define BREAK_TYPE 'B'
403#define tochar(x) ((char) (((x) + SPACE) & 0xff))
404#define untochar(x) ((int) (((x) - SPACE) & 0xff))
405
wdenk8bde7f72003-06-27 21:31:46 +0000406static void set_kerm_bin_mode(unsigned long *);
407static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000408static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000409
410
Kim Phillips088f1b12012-10-29 13:34:31 +0000411static char his_eol; /* character he needs at end of packet */
412static int his_pad_count; /* number of pad chars he needs */
413static char his_pad_char; /* pad chars he needs */
414static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000415
Kim Phillips088f1b12012-10-29 13:34:31 +0000416static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
417 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000418{
wdenk8bde7f72003-06-27 21:31:46 +0000419 ulong offset = 0;
420 ulong addr;
421 int load_baudrate, current_baudrate;
422 int rcode = 0;
423 char *s;
424
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
426 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000427
428 /* pre-set offset from $loadaddr */
429 if ((s = getenv("loadaddr")) != NULL) {
430 offset = simple_strtoul(s, NULL, 16);
431 }
432
433 load_baudrate = current_baudrate = gd->baudrate;
434
435 if (argc >= 2) {
436 offset = simple_strtoul(argv[1], NULL, 16);
437 }
438 if (argc == 3) {
439 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
440
441 /* default to current baudrate */
442 if (load_baudrate == 0)
443 load_baudrate = current_baudrate;
444 }
445
446 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000447 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000448 load_baudrate);
449 udelay(50000);
450 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000451 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000452 udelay(50000);
453 for (;;) {
454 if (getc() == '\r')
455 break;
456 }
457 }
458
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200459 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000460 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200461 "to 0x%08lX at %d bps...\n",
462 offset,
463 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000464
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600465 addr = load_serial_ymodem(offset, xyzModem_ymodem);
466
467 } else if (strcmp(argv[0],"loadx")==0) {
468 printf("## Ready for binary (xmodem) download "
469 "to 0x%08lX at %d bps...\n",
470 offset,
471 load_baudrate);
472
473 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200474
wdenk8bde7f72003-06-27 21:31:46 +0000475 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000476
Kim Phillips088f1b12012-10-29 13:34:31 +0000477 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200478 "to 0x%08lX at %d bps...\n",
479 offset,
480 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000481 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200482
483 if (addr == ~0) {
484 load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000485 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200486 rcode = 1;
487 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000488 printf("## Start Addr = 0x%08lX\n", addr);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200489 load_addr = addr;
490 }
491 }
wdenk8bde7f72003-06-27 21:31:46 +0000492 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000493 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000494 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000495 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000496 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000497 serial_setbrg();
498 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000499 for (;;) {
500 if (getc() == 0x1B) /* ESC */
501 break;
502 }
503 }
504
wdenk8bde7f72003-06-27 21:31:46 +0000505 return rcode;
506}
507
508
Kim Phillips088f1b12012-10-29 13:34:31 +0000509static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000510{
511 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000512
Kim Phillips088f1b12012-10-29 13:34:31 +0000513 set_kerm_bin_mode((ulong *) offset);
514 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000515
516 /*
517 * Gather any trailing characters (for instance, the ^D which
518 * is sent by 'cu' after sending a file), and give the
519 * box some time (100 * 1 ms)
520 */
521 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000522 if (tstc()) {
523 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000524 }
525 udelay(1000);
526 }
527
Kim Phillips088f1b12012-10-29 13:34:31 +0000528 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000529
530 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass41ef3722013-02-24 17:33:22 +0000531 setenv_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000532
533 return offset;
534}
535
Kim Phillips088f1b12012-10-29 13:34:31 +0000536static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000537{
538 int count = his_pad_count;
539
540 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000541 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000542}
543
544/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000545static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000546{
547 if ((in & 0x60) == 0x40) {
548 return (char) (in & ~0x40);
549 } else if ((in & 0x7f) == 0x3f) {
550 return (char) (in | 0x40);
551 } else
552 return in;
553}
554
Kim Phillips088f1b12012-10-29 13:34:31 +0000555static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000556{
557 int total = 0;
558
559 while (*buffer) {
560 total += *buffer++;
561 }
562 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
563}
564
Kim Phillips088f1b12012-10-29 13:34:31 +0000565static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000566{
Kim Phillips088f1b12012-10-29 13:34:31 +0000567 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000568 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000569 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000570 }
571}
572
573static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000574static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000575{
576 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000577 a_b[1] = tochar(3);
578 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000579 a_b[3] = ACK_TYPE;
580 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000581 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000582 a_b[5] = his_eol;
583 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000584 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000585}
586
Kim Phillips088f1b12012-10-29 13:34:31 +0000587static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000588{
589 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000590 a_b[1] = tochar(3);
591 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000592 a_b[3] = NACK_TYPE;
593 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000594 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000595 a_b[5] = his_eol;
596 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000597 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000598}
599
600
Kim Phillips088f1b12012-10-29 13:34:31 +0000601static void (*os_data_init)(void);
602static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000603static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000604static char *os_data_addr, *os_data_addr_saved;
605static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530606
Kim Phillips088f1b12012-10-29 13:34:31 +0000607static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000608{
609 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000610 os_data_addr = bin_start_address;
611}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530612
Kim Phillips088f1b12012-10-29 13:34:31 +0000613static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000614{
615 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000616 os_data_addr_saved = os_data_addr;
617}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530618
Kim Phillips088f1b12012-10-29 13:34:31 +0000619static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000620{
621 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000622 os_data_addr = os_data_addr_saved;
623}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530624
Kim Phillips088f1b12012-10-29 13:34:31 +0000625static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000626{
627 switch (os_data_state) {
628 case 0: /* data */
629 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000630 break;
631 }
632}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530633
Kim Phillips088f1b12012-10-29 13:34:31 +0000634static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000635{
636 bin_start_address = (char *) addr;
637 os_data_init = bin_data_init;
638 os_data_char = bin_data_char;
639}
640
641
642/* k_data_* simply handles the kermit escape translations */
643static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000644static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000645{
646 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000647 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000648}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530649
Kim Phillips088f1b12012-10-29 13:34:31 +0000650static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000651{
652 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000653 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000654}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530655
Kim Phillips088f1b12012-10-29 13:34:31 +0000656static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000657{
658 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000659 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000660}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530661
Kim Phillips088f1b12012-10-29 13:34:31 +0000662static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000663{
664 if (k_data_escape) {
665 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000666 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000667 k_data_escape = 0;
668 } else {
669 if (new_char == his_quote) {
670 /* this char is escape - remember */
671 k_data_escape = 1;
672 } else {
673 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000674 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000675 }
676 }
677}
678
679#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000680static char send_parms[SEND_DATA_SIZE];
681static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000682
683/* handle_send_packet interprits the protocol info and builds and
684 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000685static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000686{
687 int length = 3;
688 int bytes;
689
690 /* initialize some protocol parameters */
691 his_eol = END_CHAR; /* default end of line character */
692 his_pad_count = 0;
693 his_pad_char = '\0';
694 his_quote = K_ESCAPE;
695
696 /* ignore last character if it filled the buffer */
697 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
698 --send_ptr;
699 bytes = send_ptr - send_parms; /* how many bytes we'll process */
700 do {
701 if (bytes-- <= 0)
702 break;
703 /* handle MAXL - max length */
704 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000705 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000706 if (bytes-- <= 0)
707 break;
708 /* handle TIME - time you should wait for my packets */
709 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000710 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000711 if (bytes-- <= 0)
712 break;
713 /* handle NPAD - number of pad chars I need */
714 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000715 his_pad_count = untochar(send_parms[2]);
716 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000717 if (bytes-- <= 0)
718 break;
719 /* handle PADC - pad chars I need */
720 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000721 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000722 a_b[++length] = 0x40; /* He should ignore this */
723 if (bytes-- <= 0)
724 break;
725 /* handle EOL - end of line he needs */
726 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000727 his_eol = untochar(send_parms[4]);
728 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000729 if (bytes-- <= 0)
730 break;
731 /* handle QCTL - quote control char he'll use */
732 /* remember what he says - I'll use '#' */
733 his_quote = send_parms[5];
734 a_b[++length] = '#';
735 if (bytes-- <= 0)
736 break;
737 /* handle QBIN - 8-th bit prefixing */
738 /* ignore what he says - I refuse */
739 a_b[++length] = 'N';
740 if (bytes-- <= 0)
741 break;
742 /* handle CHKT - the clock check type */
743 /* ignore what he says - I do type 1 (for now) */
744 a_b[++length] = '1';
745 if (bytes-- <= 0)
746 break;
747 /* handle REPT - the repeat prefix */
748 /* ignore what he says - I refuse (for now) */
749 a_b[++length] = 'N';
750 if (bytes-- <= 0)
751 break;
752 /* handle CAPAS - the capabilities mask */
753 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000754 a_b[++length] = tochar(2); /* only long packets */
755 a_b[++length] = tochar(0); /* no windows */
756 a_b[++length] = tochar(94); /* large packet msb */
757 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000758 } while (0);
759
760 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000761 a_b[1] = tochar(length);
762 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000763 a_b[3] = ACK_TYPE;
764 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000765 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000766 a_b[++length] = his_eol;
767 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000768 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000769}
770
771/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000772static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000773{
774 char new_char;
775 char k_state, k_state_saved;
776 int sum;
777 int done;
778 int length;
779 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000780 int len_lo, len_hi;
781
782 /* initialize some protocol parameters */
783 his_eol = END_CHAR; /* default end of line character */
784 his_pad_count = 0;
785 his_pad_char = '\0';
786 his_quote = K_ESCAPE;
787
788 /* initialize the k_recv and k_data state machine */
789 done = 0;
790 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000791 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000792 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000793 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000794 n = 0; /* just to get rid of a warning */
795 last_n = -1;
796
797 /* expect this "type" sequence (but don't check):
798 S: send initiate
799 F: file header
800 D: data (multiple)
801 Z: end of file
802 B: break transmission
803 */
804
805 /* enter main loop */
806 while (!done) {
807 /* set the send packet pointer to begining of send packet parms */
808 send_ptr = send_parms;
809
810 /* With each packet, start summing the bytes starting with the length.
811 Save the current sequence number.
812 Note the type of the packet.
813 If a character less than SPACE (0x20) is received - error.
814 */
815
816#if 0
817 /* OLD CODE, Prior to checking sequence numbers */
818 /* first have all state machines save current states */
819 k_state_saved = k_state;
820 k_data_save ();
821#endif
822
823 /* get a packet */
824 /* wait for the starting character or ^C */
825 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000826 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000827 case START_CHAR: /* start packet */
828 goto START;
829 case ETX_CHAR: /* ^C waiting for packet */
830 return (0);
831 default:
832 ;
833 }
834 }
835START:
836 /* get length of packet */
837 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000838 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000839 if ((new_char & 0xE0) == 0)
840 goto packet_error;
841 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000842 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000843 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +0000844 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000845 if ((new_char & 0xE0) == 0)
846 goto packet_error;
847 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000848 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000849 --length;
850
851 /* NEW CODE - check sequence numbers for retried packets */
852 /* Note - this new code assumes that the sequence number is correctly
853 * received. Handling an invalid sequence number adds another layer
854 * of complexity that may not be needed - yet! At this time, I'm hoping
855 * that I don't need to buffer the incoming data packets and can write
856 * the data into memory in real time.
857 */
858 if (n == last_n) {
859 /* same sequence number, restore the previous state */
860 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000861 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000862 } else {
863 /* new sequence number, checkpoint the download */
864 last_n = n;
865 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000866 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000867 }
868 /* END NEW CODE */
869
870 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000871 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000872 if ((new_char & 0xE0) == 0)
873 goto packet_error;
874 sum += new_char & 0xff;
875 k_state = new_char;
876 --length;
877 /* check for extended length */
878 if (length == -2) {
879 /* (length byte was 0, decremented twice) */
880 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000881 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000882 if ((new_char & 0xE0) == 0)
883 goto packet_error;
884 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000885 len_hi = untochar(new_char);
886 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000887 if ((new_char & 0xE0) == 0)
888 goto packet_error;
889 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000890 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000891 length = len_hi * 95 + len_lo;
892 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000893 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000894 if ((new_char & 0xE0) == 0)
895 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000896 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000897 goto packet_error;
898 sum += new_char & 0xff;
899/* --length; */ /* new length includes only data and block check to come */
900 }
901 /* bring in rest of packet */
902 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000903 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000904 if ((new_char & 0xE0) == 0)
905 goto packet_error;
906 sum += new_char & 0xff;
907 --length;
908 if (k_state == DATA_TYPE) {
909 /* pass on the data if this is a data packet */
910 k_data_char (new_char);
911 } else if (k_state == SEND_TYPE) {
912 /* save send pack in buffer as is */
913 *send_ptr++ = new_char;
914 /* if too much data, back off the pointer */
915 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
916 --send_ptr;
917 }
918 }
919 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000920 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000921 if ((new_char & 0xE0) == 0)
922 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000923 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000924 goto packet_error;
925 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000926 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000927 if (new_char != END_CHAR) {
928 packet_error:
929 /* restore state machines */
930 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000931 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000932 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000933 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000934 } else if (k_state == SEND_TYPE) {
935 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000936 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000937 } else {
938 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000939 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000940 /* quit if end of transmission */
941 if (k_state == BREAK_TYPE)
942 done = 1;
943 }
wdenk8bde7f72003-06-27 21:31:46 +0000944 }
945 return ((ulong) os_data_addr - (ulong) bin_start_address);
946}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200947
948static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200949 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200950 return (getc());
951 return -1;
952}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600953static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200954{
955 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200956 int err;
957 int res;
958 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200959 char ymodemBuf[1024];
960 ulong store_addr = ~0;
961 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200962
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200963 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600964 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000965 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200966 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200967
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200968 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000969 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200970 store_addr = addr + offset;
971 size += res;
972 addr += res;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200973#ifndef CONFIG_SYS_NO_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000974 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200975 int rc;
976
Kim Phillips088f1b12012-10-29 13:34:31 +0000977 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200978 store_addr, res);
979 if (rc != 0) {
980 flash_perror (rc);
981 return (~0);
982 }
983 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200984#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200985 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000986 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200987 res);
988 }
989
990 }
991 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000992 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200993 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200994
Kim Phillips088f1b12012-10-29 13:34:31 +0000995 xyzModem_stream_close(&err);
996 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200997
998
Kim Phillips088f1b12012-10-29 13:34:31 +0000999 flush_cache(offset, size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001000
Kim Phillips088f1b12012-10-29 13:34:31 +00001001 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass41ef3722013-02-24 17:33:22 +00001002 setenv_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001003
1004 return offset;
1005}
1006
Jon Loeliger90253172007-07-10 11:02:44 -05001007#endif
wdenk8bde7f72003-06-27 21:31:46 +00001008
1009/* -------------------------------------------------------------------- */
1010
Jon Loeligerc76fe472007-07-08 18:02:23 -05001011#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001012
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001013#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001014U_BOOT_CMD(
1015 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001016 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001017 "[ off ] [ baud ]\n"
1018 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001019 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001020);
1021
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001022#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001023U_BOOT_CMD(
1024 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001025 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001026 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001027 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001028);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001029#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001030
1031/*
1032 * SAVES always requires LOADS support, but not vice versa
1033 */
1034
1035
Jon Loeligerc76fe472007-07-08 18:02:23 -05001036#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001037#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001038U_BOOT_CMD(
1039 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001040 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001041 "[ off ] [size] [ baud ]\n"
1042 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001043 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001044);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001045#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001046U_BOOT_CMD(
1047 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001048 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001049 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001050 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001051);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001052#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001053#endif /* CONFIG_CMD_SAVES */
1054#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001055
1056
Jon Loeligerc76fe472007-07-08 18:02:23 -05001057#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001058U_BOOT_CMD(
1059 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001060 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001061 "[ off ] [ baud ]\n"
1062 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001063 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001064);
1065
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001066U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001067 loadx, 3, 0, do_load_serial_bin,
1068 "load binary file over serial line (xmodem mode)",
1069 "[ off ] [ baud ]\n"
1070 " - load binary file over serial line"
1071 " with offset 'off' and baudrate 'baud'"
1072);
1073
1074U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001075 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001076 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001077 "[ off ] [ baud ]\n"
1078 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001079 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001080);
1081
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001082#endif /* CONFIG_CMD_LOADB */
wdenk8bde7f72003-06-27 21:31:46 +00001083
1084/* -------------------------------------------------------------------- */
1085
Jon Loeligerc76fe472007-07-08 18:02:23 -05001086#if defined(CONFIG_CMD_HWFLOW)
Kim Phillips088f1b12012-10-29 13:34:31 +00001087int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +00001088{
1089 extern int hwflow_onoff(int);
1090
1091 if (argc == 2) {
1092 if (strcmp(argv[1], "off") == 0)
1093 hwflow_onoff(-1);
1094 else
1095 if (strcmp(argv[1], "on") == 0)
1096 hwflow_onoff(1);
1097 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001098 return CMD_RET_USAGE;
wdenk8bde7f72003-06-27 21:31:46 +00001099 }
1100 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1101 return 0;
1102}
1103
1104/* -------------------------------------------------------------------- */
1105
wdenk0d498392003-07-01 21:06:45 +00001106U_BOOT_CMD(
wdenkf12e5682003-07-07 20:07:54 +00001107 hwflow, 2, 0, do_hwflow,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001108 "turn RTS/CTS hardware flow control in serial line on/off",
1109 "[on|off]"
wdenk8bde7f72003-06-27 21:31:46 +00001110);
1111
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001112#endif /* CONFIG_CMD_HWFLOW */