blob: 2c8dab1a0a6618dda63c174ff3bc6801b11b617b [file] [log] [blame]
wdenk57b2d802003-06-27 21:31:46 +00001/*
wdenk29e7f5a2004-03-12 00:14:09 +00002 * (C) Copyright 2000-2004
wdenk57b2d802003-06-27 21:31:46 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Serial up- and download support
26 */
27#include <common.h>
28#include <command.h>
wdenk57b2d802003-06-27 21:31:46 +000029#include <s_record.h>
30#include <net.h>
wdenk874ac262003-07-24 23:38:38 +000031#include <exports.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020032#include <xyzModem.h>
wdenk57b2d802003-06-27 21:31:46 +000033
Wolfgang Denk6405a152006-03-31 18:32:53 +020034DECLARE_GLOBAL_DATA_PTR;
wdenk57b2d802003-06-27 21:31:46 +000035
Jon Loeligerd76b5c12007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADB)
Kim Phillipsdc00a682012-10-29 13:34:31 +000037static ulong load_serial_ymodem(ulong offset);
Wolfgang Denkda916932006-05-02 00:11:25 +020038#endif
39
Jon Loeligerd76b5c12007-07-08 18:02:23 -050040#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000041static ulong load_serial(long offset);
42static int read_record(char *buf, ulong len);
Jon Loeligerd76b5c12007-07-08 18:02:23 -050043# if defined(CONFIG_CMD_SAVES)
Kim Phillipsdc00a682012-10-29 13:34:31 +000044static int save_serial(ulong offset, ulong size);
45static int write_record(char *buf);
Jon Loeligerd704d912007-07-10 11:02:44 -050046#endif
wdenk57b2d802003-06-27 21:31:46 +000047
48static int do_echo = 1;
Jon Loeligerd704d912007-07-10 11:02:44 -050049#endif
wdenk57b2d802003-06-27 21:31:46 +000050
51/* -------------------------------------------------------------------- */
52
Jon Loeligerd76b5c12007-07-08 18:02:23 -050053#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000054static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
55 char * const argv[])
wdenk57b2d802003-06-27 21:31:46 +000056{
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020057 long offset = 0;
wdenk57b2d802003-06-27 21:31:46 +000058 ulong addr;
59 int i;
60 char *env_echo;
61 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000063 int load_baudrate, current_baudrate;
64
65 load_baudrate = current_baudrate = gd->baudrate;
66#endif
67
68 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
69 do_echo = 1;
70 } else {
71 do_echo = 0;
72 }
73
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020074#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000075 if (argc >= 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020076 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000077 }
78 if (argc == 3) {
79 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
80
81 /* default to current baudrate */
82 if (load_baudrate == 0)
83 load_baudrate = current_baudrate;
84 }
85 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +000086 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +000087 load_baudrate);
88 udelay(50000);
89 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +000090 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +000091 udelay(50000);
92 for (;;) {
93 if (getc() == '\r')
94 break;
95 }
96 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020097#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000098 if (argc == 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020099 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +0000100 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200101#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000102
Kim Phillipsdc00a682012-10-29 13:34:31 +0000103 printf("## Ready for S-Record download ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000104
Kim Phillipsdc00a682012-10-29 13:34:31 +0000105 addr = load_serial(offset);
wdenk57b2d802003-06-27 21:31:46 +0000106
107 /*
108 * Gather any trailing characters (for instance, the ^D which
109 * is sent by 'cu' after sending a file), and give the
110 * box some time (100 * 1 ms)
111 */
112 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000113 if (tstc()) {
114 (void) getc();
wdenk57b2d802003-06-27 21:31:46 +0000115 }
116 udelay(1000);
117 }
118
119 if (addr == ~0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000120 printf("## S-Record download aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000121 rcode = 1;
122 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000123 printf("## Start Addr = 0x%08lX\n", addr);
wdenk57b2d802003-06-27 21:31:46 +0000124 load_addr = addr;
125 }
126
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200127#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000128 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000129 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000130 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000131 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000132 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000133 serial_setbrg();
134 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000135 for (;;) {
136 if (getc() == 0x1B) /* ESC */
137 break;
138 }
139 }
140#endif
141 return rcode;
142}
143
Kim Phillipsdc00a682012-10-29 13:34:31 +0000144static ulong load_serial(long offset)
wdenk57b2d802003-06-27 21:31:46 +0000145{
146 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
147 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
148 int binlen; /* no. of data bytes in S-Rec. */
149 int type; /* return code for record type */
150 ulong addr; /* load address from S-Record */
151 ulong size; /* number of bytes transferred */
152 char buf[32];
153 ulong store_addr;
154 ulong start_addr = ~0;
155 ulong end_addr = 0;
156 int line_count = 0;
157
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;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200170#ifndef CONFIG_SYS_NO_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 {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000182 memcpy((char *)(store_addr), binbuf, binlen);
wdenk57b2d802003-06-27 21:31:46 +0000183 }
184 if ((store_addr) < start_addr)
185 start_addr = store_addr;
186 if ((store_addr + binlen - 1) > end_addr)
187 end_addr = store_addr + binlen - 1;
188 break;
189 case SREC_END2:
190 case SREC_END3:
191 case SREC_END4:
Kim Phillipsdc00a682012-10-29 13:34:31 +0000192 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000193 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000194 printf("\n"
wdenk57b2d802003-06-27 21:31:46 +0000195 "## First Load Addr = 0x%08lX\n"
196 "## Last Load Addr = 0x%08lX\n"
197 "## Total Size = 0x%08lX = %ld Bytes\n",
198 start_addr, end_addr, size, size
199 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000200 flush_cache(start_addr, size);
wdenk57b2d802003-06-27 21:31:46 +0000201 sprintf(buf, "%lX", size);
202 setenv("filesize", buf);
203 return (addr);
204 case SREC_START:
205 break;
206 default:
207 break;
208 }
209 if (!do_echo) { /* print a '.' every 100 lines */
210 if ((++line_count % 100) == 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000211 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000212 }
213 }
214
215 return (~0); /* Download aborted */
216}
217
Kim Phillipsdc00a682012-10-29 13:34:31 +0000218static int read_record(char *buf, ulong len)
wdenk57b2d802003-06-27 21:31:46 +0000219{
220 char *p;
221 char c;
222
223 --len; /* always leave room for terminating '\0' byte */
224
225 for (p=buf; p < buf+len; ++p) {
wdenk29e7f5a2004-03-12 00:14:09 +0000226 c = getc(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000227 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000228 putc(c); /* ... and echo it */
wdenk57b2d802003-06-27 21:31:46 +0000229
230 switch (c) {
231 case '\r':
232 case '\n':
233 *p = '\0';
234 return (p - buf);
235 case '\0':
236 case 0x03: /* ^C - Control C */
237 return (-1);
238 default:
239 *p = c;
240 }
241
242 /* Check for the console hangup (if any different from serial) */
wdenk29e7f5a2004-03-12 00:14:09 +0000243 if (gd->jt[XF_getc] != getc) {
wdenk57b2d802003-06-27 21:31:46 +0000244 if (ctrlc()) {
245 return (-1);
246 }
247 }
wdenk57b2d802003-06-27 21:31:46 +0000248 }
249
250 /* line too long - truncate */
251 *p = '\0';
252 return (p - buf);
253}
254
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500255#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000256
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200257int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000258{
259 ulong offset = 0;
260 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200261#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000262 int save_baudrate, current_baudrate;
263
264 save_baudrate = current_baudrate = gd->baudrate;
265#endif
266
267 if (argc >= 2) {
268 offset = simple_strtoul(argv[1], NULL, 16);
269 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200270#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000271 if (argc >= 3) {
272 size = simple_strtoul(argv[2], NULL, 16);
273 }
274 if (argc == 4) {
275 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
276
277 /* default to current baudrate */
278 if (save_baudrate == 0)
279 save_baudrate = current_baudrate;
280 }
281 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000282 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000283 save_baudrate);
284 udelay(50000);
285 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000286 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000287 udelay(50000);
288 for (;;) {
289 if (getc() == '\r')
290 break;
291 }
292 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200293#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000294 if (argc == 3) {
295 size = simple_strtoul(argv[2], NULL, 16);
296 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200297#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000298
Kim Phillipsdc00a682012-10-29 13:34:31 +0000299 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000300 for (;;) {
301 if (getc() == '\r')
302 break;
303 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000304 if (save_serial(offset, size)) {
305 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000306 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000307 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000308 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200309#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000310 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000311 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000312 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000313 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000314 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000315 serial_setbrg();
316 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000317 for (;;) {
318 if (getc() == 0x1B) /* ESC */
319 break;
320 }
321 }
322#endif
323 return 0;
324}
325
326#define SREC3_START "S0030000FC\n"
327#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
328#define SREC3_END "S70500000000FA\n"
329#define SREC_BYTES_PER_RECORD 16
330
Kim Phillipsdc00a682012-10-29 13:34:31 +0000331static int save_serial(ulong address, ulong count)
wdenk57b2d802003-06-27 21:31:46 +0000332{
333 int i, c, reclen, checksum, length;
334 char *hex = "0123456789ABCDEF";
335 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
336 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
337
338 reclen = 0;
339 checksum = 0;
340
341 if(write_record(SREC3_START)) /* write the header */
342 return (-1);
343 do {
344 if(count) { /* collect hex data in the buffer */
345 c = *(volatile uchar*)(address + reclen); /* get one byte */
346 checksum += c; /* accumulate checksum */
347 data[2*reclen] = hex[(c>>4)&0x0f];
348 data[2*reclen+1] = hex[c & 0x0f];
349 data[2*reclen+2] = '\0';
350 ++reclen;
351 --count;
352 }
353 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
354 /* enough data collected for one record: dump it */
355 if(reclen) { /* build & write a data record: */
356 /* address + data + checksum */
357 length = 4 + reclen + 1;
358
359 /* accumulate length bytes into checksum */
360 for(i = 0; i < 2; i++)
361 checksum += (length >> (8*i)) & 0xff;
362
363 /* accumulate address bytes into checksum: */
364 for(i = 0; i < 4; i++)
365 checksum += (address >> (8*i)) & 0xff;
366
367 /* make proper checksum byte: */
368 checksum = ~checksum & 0xff;
369
370 /* output one record: */
371 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
372 if(write_record(record))
373 return (-1);
374 }
375 address += reclen; /* increment address */
376 checksum = 0;
377 reclen = 0;
378 }
379 }
380 while(count);
381 if(write_record(SREC3_END)) /* write the final record */
382 return (-1);
383 return(0);
384}
385
Kim Phillipsdc00a682012-10-29 13:34:31 +0000386static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000387{
388 char c;
389
390 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000391 putc(c);
wdenk57b2d802003-06-27 21:31:46 +0000392
393 /* Check for the console hangup (if any different from serial) */
394
395 if (ctrlc()) {
396 return (-1);
397 }
398 return (0);
399}
Jon Loeligerd704d912007-07-10 11:02:44 -0500400# endif
wdenk57b2d802003-06-27 21:31:46 +0000401
Jon Loeligerd704d912007-07-10 11:02:44 -0500402#endif
wdenk57b2d802003-06-27 21:31:46 +0000403
404
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500405#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500406/*
407 * loadb command (load binary) included
408 */
wdenk57b2d802003-06-27 21:31:46 +0000409#define XON_CHAR 17
410#define XOFF_CHAR 19
411#define START_CHAR 0x01
412#define ETX_CHAR 0x03
413#define END_CHAR 0x0D
414#define SPACE 0x20
415#define K_ESCAPE 0x23
416#define SEND_TYPE 'S'
417#define DATA_TYPE 'D'
418#define ACK_TYPE 'Y'
419#define NACK_TYPE 'N'
420#define BREAK_TYPE 'B'
421#define tochar(x) ((char) (((x) + SPACE) & 0xff))
422#define untochar(x) ((int) (((x) - SPACE) & 0xff))
423
wdenk57b2d802003-06-27 21:31:46 +0000424static void set_kerm_bin_mode(unsigned long *);
425static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000426static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000427
428
Kim Phillipsdc00a682012-10-29 13:34:31 +0000429static char his_eol; /* character he needs at end of packet */
430static int his_pad_count; /* number of pad chars he needs */
431static char his_pad_char; /* pad chars he needs */
432static char his_quote; /* quote chars he'll use */
wdenk57b2d802003-06-27 21:31:46 +0000433
Kim Phillipsdc00a682012-10-29 13:34:31 +0000434static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
435 char * const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000436{
wdenk57b2d802003-06-27 21:31:46 +0000437 ulong offset = 0;
438 ulong addr;
439 int load_baudrate, current_baudrate;
440 int rcode = 0;
441 char *s;
442
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200443 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
444 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000445
446 /* pre-set offset from $loadaddr */
447 if ((s = getenv("loadaddr")) != NULL) {
448 offset = simple_strtoul(s, NULL, 16);
449 }
450
451 load_baudrate = current_baudrate = gd->baudrate;
452
453 if (argc >= 2) {
454 offset = simple_strtoul(argv[1], NULL, 16);
455 }
456 if (argc == 3) {
457 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
458
459 /* default to current baudrate */
460 if (load_baudrate == 0)
461 load_baudrate = current_baudrate;
462 }
463
464 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000465 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000466 load_baudrate);
467 udelay(50000);
468 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000469 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000470 udelay(50000);
471 for (;;) {
472 if (getc() == '\r')
473 break;
474 }
475 }
476
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200477 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000478 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200479 "to 0x%08lX at %d bps...\n",
480 offset,
481 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000482
Kim Phillipsdc00a682012-10-29 13:34:31 +0000483 addr = load_serial_ymodem(offset);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200484
wdenk57b2d802003-06-27 21:31:46 +0000485 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200486
Kim Phillipsdc00a682012-10-29 13:34:31 +0000487 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200488 "to 0x%08lX at %d bps...\n",
489 offset,
490 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000491 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000492
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200493 if (addr == ~0) {
494 load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000495 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200496 rcode = 1;
497 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000498 printf("## Start Addr = 0x%08lX\n", addr);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200499 load_addr = addr;
500 }
501 }
wdenk57b2d802003-06-27 21:31:46 +0000502 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000503 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000504 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000505 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000506 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000507 serial_setbrg();
508 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000509 for (;;) {
510 if (getc() == 0x1B) /* ESC */
511 break;
512 }
513 }
514
wdenk57b2d802003-06-27 21:31:46 +0000515 return rcode;
516}
517
518
Kim Phillipsdc00a682012-10-29 13:34:31 +0000519static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000520{
521 int size, i;
522 char buf[32];
523
Kim Phillipsdc00a682012-10-29 13:34:31 +0000524 set_kerm_bin_mode((ulong *) offset);
525 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000526
527 /*
528 * Gather any trailing characters (for instance, the ^D which
529 * is sent by 'cu' after sending a file), and give the
530 * box some time (100 * 1 ms)
531 */
532 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000533 if (tstc()) {
534 (void) getc();
wdenk57b2d802003-06-27 21:31:46 +0000535 }
536 udelay(1000);
537 }
538
Kim Phillipsdc00a682012-10-29 13:34:31 +0000539 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000540
541 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
542 sprintf(buf, "%X", size);
543 setenv("filesize", buf);
544
545 return offset;
546}
547
Kim Phillipsdc00a682012-10-29 13:34:31 +0000548static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000549{
550 int count = his_pad_count;
551
552 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000553 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000554}
555
556/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000557static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000558{
559 if ((in & 0x60) == 0x40) {
560 return (char) (in & ~0x40);
561 } else if ((in & 0x7f) == 0x3f) {
562 return (char) (in | 0x40);
563 } else
564 return in;
565}
566
Kim Phillipsdc00a682012-10-29 13:34:31 +0000567static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000568{
569 int total = 0;
570
571 while (*buffer) {
572 total += *buffer++;
573 }
574 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
575}
576
Kim Phillipsdc00a682012-10-29 13:34:31 +0000577static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000578{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000579 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000580 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000581 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000582 }
583}
584
585static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000586static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000587{
588 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000589 a_b[1] = tochar(3);
590 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000591 a_b[3] = ACK_TYPE;
592 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000593 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000594 a_b[5] = his_eol;
595 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000596 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000597}
598
Kim Phillipsdc00a682012-10-29 13:34:31 +0000599static void send_nack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000600{
601 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000602 a_b[1] = tochar(3);
603 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000604 a_b[3] = NACK_TYPE;
605 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000606 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000607 a_b[5] = his_eol;
608 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000609 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000610}
611
612
Kim Phillipsdc00a682012-10-29 13:34:31 +0000613static void (*os_data_init)(void);
614static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000615static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000616static char *os_data_addr, *os_data_addr_saved;
617static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530618
Kim Phillipsdc00a682012-10-29 13:34:31 +0000619static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000620{
621 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000622 os_data_addr = bin_start_address;
623}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530624
Kim Phillipsdc00a682012-10-29 13:34:31 +0000625static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000626{
627 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000628 os_data_addr_saved = os_data_addr;
629}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530630
Kim Phillipsdc00a682012-10-29 13:34:31 +0000631static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000632{
633 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000634 os_data_addr = os_data_addr_saved;
635}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530636
Kim Phillipsdc00a682012-10-29 13:34:31 +0000637static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000638{
639 switch (os_data_state) {
640 case 0: /* data */
641 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000642 break;
643 }
644}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530645
Kim Phillipsdc00a682012-10-29 13:34:31 +0000646static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000647{
648 bin_start_address = (char *) addr;
649 os_data_init = bin_data_init;
650 os_data_char = bin_data_char;
651}
652
653
654/* k_data_* simply handles the kermit escape translations */
655static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000656static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000657{
658 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000659 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000660}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530661
Kim Phillipsdc00a682012-10-29 13:34:31 +0000662static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000663{
664 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000665 os_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000666}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530667
Kim Phillipsdc00a682012-10-29 13:34:31 +0000668static void k_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000669{
670 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000671 os_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000672}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530673
Kim Phillipsdc00a682012-10-29 13:34:31 +0000674static void k_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000675{
676 if (k_data_escape) {
677 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000678 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000679 k_data_escape = 0;
680 } else {
681 if (new_char == his_quote) {
682 /* this char is escape - remember */
683 k_data_escape = 1;
684 } else {
685 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000686 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000687 }
688 }
689}
690
691#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000692static char send_parms[SEND_DATA_SIZE];
693static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000694
695/* handle_send_packet interprits the protocol info and builds and
696 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000697static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000698{
699 int length = 3;
700 int bytes;
701
702 /* initialize some protocol parameters */
703 his_eol = END_CHAR; /* default end of line character */
704 his_pad_count = 0;
705 his_pad_char = '\0';
706 his_quote = K_ESCAPE;
707
708 /* ignore last character if it filled the buffer */
709 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
710 --send_ptr;
711 bytes = send_ptr - send_parms; /* how many bytes we'll process */
712 do {
713 if (bytes-- <= 0)
714 break;
715 /* handle MAXL - max length */
716 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000717 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000718 if (bytes-- <= 0)
719 break;
720 /* handle TIME - time you should wait for my packets */
721 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000722 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000723 if (bytes-- <= 0)
724 break;
725 /* handle NPAD - number of pad chars I need */
726 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000727 his_pad_count = untochar(send_parms[2]);
728 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000729 if (bytes-- <= 0)
730 break;
731 /* handle PADC - pad chars I need */
732 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000733 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000734 a_b[++length] = 0x40; /* He should ignore this */
735 if (bytes-- <= 0)
736 break;
737 /* handle EOL - end of line he needs */
738 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000739 his_eol = untochar(send_parms[4]);
740 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000741 if (bytes-- <= 0)
742 break;
743 /* handle QCTL - quote control char he'll use */
744 /* remember what he says - I'll use '#' */
745 his_quote = send_parms[5];
746 a_b[++length] = '#';
747 if (bytes-- <= 0)
748 break;
749 /* handle QBIN - 8-th bit prefixing */
750 /* ignore what he says - I refuse */
751 a_b[++length] = 'N';
752 if (bytes-- <= 0)
753 break;
754 /* handle CHKT - the clock check type */
755 /* ignore what he says - I do type 1 (for now) */
756 a_b[++length] = '1';
757 if (bytes-- <= 0)
758 break;
759 /* handle REPT - the repeat prefix */
760 /* ignore what he says - I refuse (for now) */
761 a_b[++length] = 'N';
762 if (bytes-- <= 0)
763 break;
764 /* handle CAPAS - the capabilities mask */
765 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000766 a_b[++length] = tochar(2); /* only long packets */
767 a_b[++length] = tochar(0); /* no windows */
768 a_b[++length] = tochar(94); /* large packet msb */
769 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000770 } while (0);
771
772 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000773 a_b[1] = tochar(length);
774 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000775 a_b[3] = ACK_TYPE;
776 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000777 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000778 a_b[++length] = his_eol;
779 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000780 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000781}
782
783/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000784static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000785{
786 char new_char;
787 char k_state, k_state_saved;
788 int sum;
789 int done;
790 int length;
791 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000792 int len_lo, len_hi;
793
794 /* initialize some protocol parameters */
795 his_eol = END_CHAR; /* default end of line character */
796 his_pad_count = 0;
797 his_pad_char = '\0';
798 his_quote = K_ESCAPE;
799
800 /* initialize the k_recv and k_data state machine */
801 done = 0;
802 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000803 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000804 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000805 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000806 n = 0; /* just to get rid of a warning */
807 last_n = -1;
808
809 /* expect this "type" sequence (but don't check):
810 S: send initiate
811 F: file header
812 D: data (multiple)
813 Z: end of file
814 B: break transmission
815 */
816
817 /* enter main loop */
818 while (!done) {
819 /* set the send packet pointer to begining of send packet parms */
820 send_ptr = send_parms;
821
822 /* With each packet, start summing the bytes starting with the length.
823 Save the current sequence number.
824 Note the type of the packet.
825 If a character less than SPACE (0x20) is received - error.
826 */
827
828#if 0
829 /* OLD CODE, Prior to checking sequence numbers */
830 /* first have all state machines save current states */
831 k_state_saved = k_state;
832 k_data_save ();
833#endif
834
835 /* get a packet */
836 /* wait for the starting character or ^C */
837 for (;;) {
wdenk29e7f5a2004-03-12 00:14:09 +0000838 switch (getc ()) {
wdenk57b2d802003-06-27 21:31:46 +0000839 case START_CHAR: /* start packet */
840 goto START;
841 case ETX_CHAR: /* ^C waiting for packet */
842 return (0);
843 default:
844 ;
845 }
846 }
847START:
848 /* get length of packet */
849 sum = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000850 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000851 if ((new_char & 0xE0) == 0)
852 goto packet_error;
853 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000854 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000855 /* get sequence number */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000856 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000857 if ((new_char & 0xE0) == 0)
858 goto packet_error;
859 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000860 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000861 --length;
862
863 /* NEW CODE - check sequence numbers for retried packets */
864 /* Note - this new code assumes that the sequence number is correctly
865 * received. Handling an invalid sequence number adds another layer
866 * of complexity that may not be needed - yet! At this time, I'm hoping
867 * that I don't need to buffer the incoming data packets and can write
868 * the data into memory in real time.
869 */
870 if (n == last_n) {
871 /* same sequence number, restore the previous state */
872 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000873 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000874 } else {
875 /* new sequence number, checkpoint the download */
876 last_n = n;
877 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000878 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000879 }
880 /* END NEW CODE */
881
882 /* get packet type */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000883 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000884 if ((new_char & 0xE0) == 0)
885 goto packet_error;
886 sum += new_char & 0xff;
887 k_state = new_char;
888 --length;
889 /* check for extended length */
890 if (length == -2) {
891 /* (length byte was 0, decremented twice) */
892 /* get the two length bytes */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000893 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000894 if ((new_char & 0xE0) == 0)
895 goto packet_error;
896 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000897 len_hi = untochar(new_char);
898 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000899 if ((new_char & 0xE0) == 0)
900 goto packet_error;
901 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000902 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000903 length = len_hi * 95 + len_lo;
904 /* check header checksum */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000905 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000906 if ((new_char & 0xE0) == 0)
907 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000908 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000909 goto packet_error;
910 sum += new_char & 0xff;
911/* --length; */ /* new length includes only data and block check to come */
912 }
913 /* bring in rest of packet */
914 while (length > 1) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000915 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000916 if ((new_char & 0xE0) == 0)
917 goto packet_error;
918 sum += new_char & 0xff;
919 --length;
920 if (k_state == DATA_TYPE) {
921 /* pass on the data if this is a data packet */
922 k_data_char (new_char);
923 } else if (k_state == SEND_TYPE) {
924 /* save send pack in buffer as is */
925 *send_ptr++ = new_char;
926 /* if too much data, back off the pointer */
927 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
928 --send_ptr;
929 }
930 }
931 /* get and validate checksum character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000932 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000933 if ((new_char & 0xE0) == 0)
934 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000935 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000936 goto packet_error;
937 /* get END_CHAR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000938 new_char = getc();
wdenk57b2d802003-06-27 21:31:46 +0000939 if (new_char != END_CHAR) {
940 packet_error:
941 /* restore state machines */
942 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000943 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000944 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000945 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000946 } else if (k_state == SEND_TYPE) {
947 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000948 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000949 } else {
950 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000951 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000952 /* quit if end of transmission */
953 if (k_state == BREAK_TYPE)
954 done = 1;
955 }
wdenk57b2d802003-06-27 21:31:46 +0000956 }
957 return ((ulong) os_data_addr - (ulong) bin_start_address);
958}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200959
960static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200961 if (tstc())
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200962 return (getc());
963 return -1;
964}
Kim Phillipsdc00a682012-10-29 13:34:31 +0000965static ulong load_serial_ymodem(ulong offset)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200966{
967 int size;
968 char buf[32];
969 int err;
970 int res;
971 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200972 char ymodemBuf[1024];
973 ulong store_addr = ~0;
974 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200975
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200976 size = 0;
977 info.mode = xyzModem_ymodem;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000978 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200979 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200980
981 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +0000982 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200983 store_addr = addr + offset;
984 size += res;
985 addr += res;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200986#ifndef CONFIG_SYS_NO_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +0000987 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200988 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200989
Kim Phillipsdc00a682012-10-29 13:34:31 +0000990 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200991 store_addr, res);
992 if (rc != 0) {
993 flash_perror (rc);
994 return (~0);
995 }
996 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200997#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200998 {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000999 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001000 res);
1001 }
1002
1003 }
1004 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001005 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001006 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001007
Kim Phillipsdc00a682012-10-29 13:34:31 +00001008 xyzModem_stream_close(&err);
1009 xyzModem_stream_terminate(false, &getcxmodem);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001010
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001011
Kim Phillipsdc00a682012-10-29 13:34:31 +00001012 flush_cache(offset, size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001013
Kim Phillipsdc00a682012-10-29 13:34:31 +00001014 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1015 sprintf(buf, "%X", size);
1016 setenv("filesize", buf);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001017
1018 return offset;
1019}
1020
Jon Loeligerd704d912007-07-10 11:02:44 -05001021#endif
wdenk57b2d802003-06-27 21:31:46 +00001022
1023/* -------------------------------------------------------------------- */
1024
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001025#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001026
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001027#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001028U_BOOT_CMD(
1029 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001030 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001031 "[ off ] [ baud ]\n"
1032 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001033 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001034);
1035
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001036#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001037U_BOOT_CMD(
1038 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001039 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001040 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001041 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001042);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001043#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001044
1045/*
1046 * SAVES always requires LOADS support, but not vice versa
1047 */
1048
1049
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001050#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001051#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001052U_BOOT_CMD(
1053 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001054 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001055 "[ off ] [size] [ baud ]\n"
1056 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001057 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001058);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001059#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001060U_BOOT_CMD(
1061 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001062 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001063 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001064 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001065);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001066#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Jon Loeligerd704d912007-07-10 11:02:44 -05001067#endif
1068#endif
wdenk57b2d802003-06-27 21:31:46 +00001069
1070
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001071#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001072U_BOOT_CMD(
1073 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001074 "load binary file over serial line (kermit mode)",
wdenk57b2d802003-06-27 21:31:46 +00001075 "[ off ] [ baud ]\n"
1076 " - load binary file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001077 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001078);
1079
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001080U_BOOT_CMD(
1081 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001082 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001083 "[ off ] [ baud ]\n"
1084 " - load binary file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001085 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001086);
1087
Jon Loeligerd704d912007-07-10 11:02:44 -05001088#endif
wdenk57b2d802003-06-27 21:31:46 +00001089
1090/* -------------------------------------------------------------------- */
1091
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001092#if defined(CONFIG_CMD_HWFLOW)
Kim Phillipsdc00a682012-10-29 13:34:31 +00001093int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk57b2d802003-06-27 21:31:46 +00001094{
1095 extern int hwflow_onoff(int);
1096
1097 if (argc == 2) {
1098 if (strcmp(argv[1], "off") == 0)
1099 hwflow_onoff(-1);
1100 else
1101 if (strcmp(argv[1], "on") == 0)
1102 hwflow_onoff(1);
1103 else
Simon Glassa06dfc72011-12-10 08:44:01 +00001104 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +00001105 }
1106 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1107 return 0;
1108}
1109
1110/* -------------------------------------------------------------------- */
1111
wdenkf287a242003-07-01 21:06:45 +00001112U_BOOT_CMD(
wdenk69141282003-07-07 20:07:54 +00001113 hwflow, 2, 0, do_hwflow,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001114 "turn RTS/CTS hardware flow control in serial line on/off",
1115 "[on|off]"
wdenk57b2d802003-06-27 21:31:46 +00001116);
1117
Jon Loeligerd704d912007-07-10 11:02:44 -05001118#endif