blob: ab167f5ab8f1b29c72bb968e8a6bebd03866dfe8 [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 *
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>
wdenk8bde7f72003-06-27 21:31:46 +000029#include <s_record.h>
30#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000031#include <exports.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020032#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000033
Wolfgang Denkd87080b2006-03-31 18:32:53 +020034DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000035
Jon Loeligerc76fe472007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADB)
Wolfgang Denk8546e232006-05-02 00:11:25 +020037static ulong load_serial_ymodem (ulong offset);
38#endif
39
Jon Loeligerc76fe472007-07-08 18:02:23 -050040#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +000041static ulong load_serial (ulong offset);
42static int read_record (char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050043# if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +000044static int save_serial (ulong offset, ulong size);
45static int write_record (char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050046#endif
wdenk8bde7f72003-06-27 21:31:46 +000047
48static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050049#endif
wdenk8bde7f72003-06-27 21:31:46 +000050
51/* -------------------------------------------------------------------- */
52
Jon Loeligerc76fe472007-07-08 18:02:23 -050053#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +000054int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
55{
56 ulong offset = 0;
57 ulong addr;
58 int i;
59 char *env_echo;
60 int rcode = 0;
61#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000062 int load_baudrate, current_baudrate;
63
64 load_baudrate = current_baudrate = gd->baudrate;
65#endif
66
67 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
68 do_echo = 1;
69 } else {
70 do_echo = 0;
71 }
72
73#ifdef CFG_LOADS_BAUD_CHANGE
74 if (argc >= 2) {
75 offset = simple_strtoul(argv[1], NULL, 16);
76 }
77 if (argc == 3) {
78 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
79
80 /* default to current baudrate */
81 if (load_baudrate == 0)
82 load_baudrate = current_baudrate;
83 }
84 if (load_baudrate != current_baudrate) {
85 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
86 load_baudrate);
87 udelay(50000);
88 gd->baudrate = load_baudrate;
89 serial_setbrg ();
90 udelay(50000);
91 for (;;) {
92 if (getc() == '\r')
93 break;
94 }
95 }
96#else /* ! CFG_LOADS_BAUD_CHANGE */
97 if (argc == 2) {
98 offset = simple_strtoul(argv[1], NULL, 16);
99 }
100#endif /* CFG_LOADS_BAUD_CHANGE */
101
102 printf ("## Ready for S-Record download ...\n");
103
104 addr = load_serial (offset);
105
106 /*
107 * Gather any trailing characters (for instance, the ^D which
108 * is sent by 'cu' after sending a file), and give the
109 * box some time (100 * 1 ms)
110 */
111 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000112 if (tstc()) {
113 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000114 }
115 udelay(1000);
116 }
117
118 if (addr == ~0) {
119 printf ("## S-Record download aborted\n");
120 rcode = 1;
121 } else {
122 printf ("## Start Addr = 0x%08lX\n", addr);
123 load_addr = addr;
124 }
125
126#ifdef CFG_LOADS_BAUD_CHANGE
127 if (load_baudrate != current_baudrate) {
128 printf ("## Switch baudrate to %d bps and press ESC ...\n",
129 current_baudrate);
130 udelay (50000);
131 gd->baudrate = current_baudrate;
132 serial_setbrg ();
133 udelay (50000);
134 for (;;) {
135 if (getc() == 0x1B) /* ESC */
136 break;
137 }
138 }
139#endif
140 return rcode;
141}
142
143static ulong
144load_serial (ulong offset)
145{
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) {
159 type = srec_decode (record, &binlen, &addr, binbuf);
160
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;
170#ifndef CFG_NO_FLASH
171 if (addr2info(store_addr)) {
172 int rc;
173
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200174 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000175 if (rc != 0) {
176 flash_perror (rc);
177 return (~0);
178 }
179 } else
180#endif
181 {
182 memcpy ((char *)(store_addr), binbuf, binlen);
183 }
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:
192 udelay (10000);
193 size = end_addr - start_addr + 1;
194 printf ("\n"
195 "## 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 );
wdenk3f85ce22004-02-23 16:11:30 +0000200 flush_cache (start_addr, size);
wdenk8bde7f72003-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)
211 putc ('.');
212 }
213 }
214
215 return (~0); /* Download aborted */
216}
217
218static int
219read_record (char *buf, ulong len)
220{
221 char *p;
222 char c;
223
224 --len; /* always leave room for terminating '\0' byte */
225
226 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000227 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000228 if (do_echo)
wdenk232c1502004-03-12 00:14:09 +0000229 putc (c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000230
231 switch (c) {
232 case '\r':
233 case '\n':
234 *p = '\0';
235 return (p - buf);
236 case '\0':
237 case 0x03: /* ^C - Control C */
238 return (-1);
239 default:
240 *p = c;
241 }
242
243 /* Check for the console hangup (if any different from serial) */
wdenk232c1502004-03-12 00:14:09 +0000244 if (gd->jt[XF_getc] != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000245 if (ctrlc()) {
246 return (-1);
247 }
248 }
wdenk8bde7f72003-06-27 21:31:46 +0000249 }
250
251 /* line too long - truncate */
252 *p = '\0';
253 return (p - buf);
254}
255
Jon Loeligerc76fe472007-07-08 18:02:23 -0500256#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000257
258int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
259{
260 ulong offset = 0;
261 ulong size = 0;
262#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000263 int save_baudrate, current_baudrate;
264
265 save_baudrate = current_baudrate = gd->baudrate;
266#endif
267
268 if (argc >= 2) {
269 offset = simple_strtoul(argv[1], NULL, 16);
270 }
271#ifdef CFG_LOADS_BAUD_CHANGE
272 if (argc >= 3) {
273 size = simple_strtoul(argv[2], NULL, 16);
274 }
275 if (argc == 4) {
276 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
277
278 /* default to current baudrate */
279 if (save_baudrate == 0)
280 save_baudrate = current_baudrate;
281 }
282 if (save_baudrate != current_baudrate) {
283 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
284 save_baudrate);
285 udelay(50000);
286 gd->baudrate = save_baudrate;
287 serial_setbrg ();
288 udelay(50000);
289 for (;;) {
290 if (getc() == '\r')
291 break;
292 }
293 }
294#else /* ! CFG_LOADS_BAUD_CHANGE */
295 if (argc == 3) {
296 size = simple_strtoul(argv[2], NULL, 16);
297 }
298#endif /* CFG_LOADS_BAUD_CHANGE */
299
300 printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
301 for (;;) {
302 if (getc() == '\r')
303 break;
304 }
305 if(save_serial (offset, size)) {
306 printf ("## S-Record upload aborted\n");
307 } else {
308 printf ("## S-Record upload complete\n");
309 }
310#ifdef CFG_LOADS_BAUD_CHANGE
311 if (save_baudrate != current_baudrate) {
312 printf ("## Switch baudrate to %d bps and press ESC ...\n",
313 (int)current_baudrate);
314 udelay (50000);
315 gd->baudrate = current_baudrate;
316 serial_setbrg ();
317 udelay (50000);
318 for (;;) {
319 if (getc() == 0x1B) /* ESC */
320 break;
321 }
322 }
323#endif
324 return 0;
325}
326
327#define SREC3_START "S0030000FC\n"
328#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
329#define SREC3_END "S70500000000FA\n"
330#define SREC_BYTES_PER_RECORD 16
331
332static int save_serial (ulong address, ulong count)
333{
334 int i, c, reclen, checksum, length;
335 char *hex = "0123456789ABCDEF";
336 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
337 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
338
339 reclen = 0;
340 checksum = 0;
341
342 if(write_record(SREC3_START)) /* write the header */
343 return (-1);
344 do {
345 if(count) { /* collect hex data in the buffer */
346 c = *(volatile uchar*)(address + reclen); /* get one byte */
347 checksum += c; /* accumulate checksum */
348 data[2*reclen] = hex[(c>>4)&0x0f];
349 data[2*reclen+1] = hex[c & 0x0f];
350 data[2*reclen+2] = '\0';
351 ++reclen;
352 --count;
353 }
354 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
355 /* enough data collected for one record: dump it */
356 if(reclen) { /* build & write a data record: */
357 /* address + data + checksum */
358 length = 4 + reclen + 1;
359
360 /* accumulate length bytes into checksum */
361 for(i = 0; i < 2; i++)
362 checksum += (length >> (8*i)) & 0xff;
363
364 /* accumulate address bytes into checksum: */
365 for(i = 0; i < 4; i++)
366 checksum += (address >> (8*i)) & 0xff;
367
368 /* make proper checksum byte: */
369 checksum = ~checksum & 0xff;
370
371 /* output one record: */
372 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
373 if(write_record(record))
374 return (-1);
375 }
376 address += reclen; /* increment address */
377 checksum = 0;
378 reclen = 0;
379 }
380 }
381 while(count);
382 if(write_record(SREC3_END)) /* write the final record */
383 return (-1);
384 return(0);
385}
386
387static int
388write_record (char *buf)
389{
390 char c;
391
392 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000393 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000394
395 /* Check for the console hangup (if any different from serial) */
396
397 if (ctrlc()) {
398 return (-1);
399 }
400 return (0);
401}
Jon Loeliger90253172007-07-10 11:02:44 -0500402# endif
wdenk8bde7f72003-06-27 21:31:46 +0000403
Jon Loeliger90253172007-07-10 11:02:44 -0500404#endif
wdenk8bde7f72003-06-27 21:31:46 +0000405
406
Jon Loeligerc76fe472007-07-08 18:02:23 -0500407#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500408/*
409 * loadb command (load binary) included
410 */
wdenk8bde7f72003-06-27 21:31:46 +0000411#define XON_CHAR 17
412#define XOFF_CHAR 19
413#define START_CHAR 0x01
414#define ETX_CHAR 0x03
415#define END_CHAR 0x0D
416#define SPACE 0x20
417#define K_ESCAPE 0x23
418#define SEND_TYPE 'S'
419#define DATA_TYPE 'D'
420#define ACK_TYPE 'Y'
421#define NACK_TYPE 'N'
422#define BREAK_TYPE 'B'
423#define tochar(x) ((char) (((x) + SPACE) & 0xff))
424#define untochar(x) ((int) (((x) - SPACE) & 0xff))
425
426extern int os_data_count;
wdenk8bde7f72003-06-27 21:31:46 +0000427
428static void set_kerm_bin_mode(unsigned long *);
429static int k_recv(void);
430static ulong load_serial_bin (ulong offset);
431
432
433char his_eol; /* character he needs at end of packet */
434int his_pad_count; /* number of pad chars he needs */
435char his_pad_char; /* pad chars he needs */
436char his_quote; /* quote chars he'll use */
437
438int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
439{
wdenk8bde7f72003-06-27 21:31:46 +0000440 ulong offset = 0;
441 ulong addr;
442 int load_baudrate, current_baudrate;
443 int rcode = 0;
444 char *s;
445
446 /* pre-set offset from CFG_LOAD_ADDR */
447 offset = CFG_LOAD_ADDR;
448
449 /* pre-set offset from $loadaddr */
450 if ((s = getenv("loadaddr")) != NULL) {
451 offset = simple_strtoul(s, NULL, 16);
452 }
453
454 load_baudrate = current_baudrate = gd->baudrate;
455
456 if (argc >= 2) {
457 offset = simple_strtoul(argv[1], NULL, 16);
458 }
459 if (argc == 3) {
460 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
461
462 /* default to current baudrate */
463 if (load_baudrate == 0)
464 load_baudrate = current_baudrate;
465 }
466
467 if (load_baudrate != current_baudrate) {
468 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
469 load_baudrate);
470 udelay(50000);
471 gd->baudrate = load_baudrate;
472 serial_setbrg ();
473 udelay(50000);
474 for (;;) {
475 if (getc() == '\r')
476 break;
477 }
478 }
479
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200480 if (strcmp(argv[0],"loady")==0) {
481 printf ("## Ready for binary (ymodem) download "
482 "to 0x%08lX at %d bps...\n",
483 offset,
484 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000485
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200486 addr = load_serial_ymodem (offset);
487
wdenk8bde7f72003-06-27 21:31:46 +0000488 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000489
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200490 printf ("## Ready for binary (kermit) download "
491 "to 0x%08lX at %d bps...\n",
492 offset,
493 load_baudrate);
494 addr = load_serial_bin (offset);
495
496 if (addr == ~0) {
497 load_addr = 0;
498 printf ("## Binary (kermit) download aborted\n");
499 rcode = 1;
500 } else {
501 printf ("## Start Addr = 0x%08lX\n", addr);
502 load_addr = addr;
503 }
504 }
wdenk8bde7f72003-06-27 21:31:46 +0000505 if (load_baudrate != current_baudrate) {
506 printf ("## Switch baudrate to %d bps and press ESC ...\n",
507 current_baudrate);
508 udelay (50000);
509 gd->baudrate = current_baudrate;
510 serial_setbrg ();
511 udelay (50000);
512 for (;;) {
513 if (getc() == 0x1B) /* ESC */
514 break;
515 }
516 }
517
518#ifdef CONFIG_AUTOSCRIPT
519 if (load_addr) {
520 char *s;
521
522 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
Marian Balakowicz424c4ab2008-03-12 10:33:00 +0100523 printf ("Running autoscript at addr 0x%08lX", load_addr);
524
525 s = getenv ("autoscript_uname");
526 if (s)
527 printf (":%s ...\n", s);
528 else
529 puts (" ...\n");
530
531 rcode = autoscript (load_addr, s);
wdenk8bde7f72003-06-27 21:31:46 +0000532 }
533 }
534#endif
535 return rcode;
536}
537
538
539static ulong load_serial_bin (ulong offset)
540{
541 int size, i;
542 char buf[32];
543
544 set_kerm_bin_mode ((ulong *) offset);
545 size = k_recv ();
546
547 /*
548 * Gather any trailing characters (for instance, the ^D which
549 * is sent by 'cu' after sending a file), and give the
550 * box some time (100 * 1 ms)
551 */
552 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000553 if (tstc()) {
554 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000555 }
556 udelay(1000);
557 }
558
559 flush_cache (offset, size);
560
561 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
562 sprintf(buf, "%X", size);
563 setenv("filesize", buf);
564
565 return offset;
566}
567
568void send_pad (void)
569{
570 int count = his_pad_count;
571
572 while (count-- > 0)
wdenk232c1502004-03-12 00:14:09 +0000573 putc (his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000574}
575
576/* converts escaped kermit char to binary char */
577char ktrans (char in)
578{
579 if ((in & 0x60) == 0x40) {
580 return (char) (in & ~0x40);
581 } else if ((in & 0x7f) == 0x3f) {
582 return (char) (in | 0x40);
583 } else
584 return in;
585}
586
587int chk1 (char *buffer)
588{
589 int total = 0;
590
591 while (*buffer) {
592 total += *buffer++;
593 }
594 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
595}
596
597void s1_sendpacket (char *packet)
598{
599 send_pad ();
600 while (*packet) {
wdenk232c1502004-03-12 00:14:09 +0000601 putc (*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000602 }
603}
604
605static char a_b[24];
606void send_ack (int n)
607{
608 a_b[0] = START_CHAR;
609 a_b[1] = tochar (3);
610 a_b[2] = tochar (n);
611 a_b[3] = ACK_TYPE;
612 a_b[4] = '\0';
613 a_b[4] = tochar (chk1 (&a_b[1]));
614 a_b[5] = his_eol;
615 a_b[6] = '\0';
616 s1_sendpacket (a_b);
617}
618
619void send_nack (int n)
620{
621 a_b[0] = START_CHAR;
622 a_b[1] = tochar (3);
623 a_b[2] = tochar (n);
624 a_b[3] = NACK_TYPE;
625 a_b[4] = '\0';
626 a_b[4] = tochar (chk1 (&a_b[1]));
627 a_b[5] = his_eol;
628 a_b[6] = '\0';
629 s1_sendpacket (a_b);
630}
631
632
wdenk8bde7f72003-06-27 21:31:46 +0000633void (*os_data_init) (void);
634void (*os_data_char) (char new_char);
635static int os_data_state, os_data_state_saved;
636int os_data_count;
637static int os_data_count_saved;
638static char *os_data_addr, *os_data_addr_saved;
639static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530640
wdenk8bde7f72003-06-27 21:31:46 +0000641static void bin_data_init (void)
642{
643 os_data_state = 0;
644 os_data_count = 0;
645 os_data_addr = bin_start_address;
646}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530647
wdenk8bde7f72003-06-27 21:31:46 +0000648static void os_data_save (void)
649{
650 os_data_state_saved = os_data_state;
651 os_data_count_saved = os_data_count;
652 os_data_addr_saved = os_data_addr;
653}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530654
wdenk8bde7f72003-06-27 21:31:46 +0000655static void os_data_restore (void)
656{
657 os_data_state = os_data_state_saved;
658 os_data_count = os_data_count_saved;
659 os_data_addr = os_data_addr_saved;
660}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530661
wdenk8bde7f72003-06-27 21:31:46 +0000662static void bin_data_char (char new_char)
663{
664 switch (os_data_state) {
665 case 0: /* data */
666 *os_data_addr++ = new_char;
667 --os_data_count;
668 break;
669 }
670}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530671
wdenk8bde7f72003-06-27 21:31:46 +0000672static void set_kerm_bin_mode (unsigned long *addr)
673{
674 bin_start_address = (char *) addr;
675 os_data_init = bin_data_init;
676 os_data_char = bin_data_char;
677}
678
679
680/* k_data_* simply handles the kermit escape translations */
681static int k_data_escape, k_data_escape_saved;
682void k_data_init (void)
683{
684 k_data_escape = 0;
685 os_data_init ();
686}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530687
wdenk8bde7f72003-06-27 21:31:46 +0000688void k_data_save (void)
689{
690 k_data_escape_saved = k_data_escape;
691 os_data_save ();
692}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530693
wdenk8bde7f72003-06-27 21:31:46 +0000694void k_data_restore (void)
695{
696 k_data_escape = k_data_escape_saved;
697 os_data_restore ();
698}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530699
wdenk8bde7f72003-06-27 21:31:46 +0000700void k_data_char (char new_char)
701{
702 if (k_data_escape) {
703 /* last char was escape - translate this character */
704 os_data_char (ktrans (new_char));
705 k_data_escape = 0;
706 } else {
707 if (new_char == his_quote) {
708 /* this char is escape - remember */
709 k_data_escape = 1;
710 } else {
711 /* otherwise send this char as-is */
712 os_data_char (new_char);
713 }
714 }
715}
716
717#define SEND_DATA_SIZE 20
718char send_parms[SEND_DATA_SIZE];
719char *send_ptr;
720
721/* handle_send_packet interprits the protocol info and builds and
722 sends an appropriate ack for what we can do */
723void handle_send_packet (int n)
724{
725 int length = 3;
726 int bytes;
727
728 /* initialize some protocol parameters */
729 his_eol = END_CHAR; /* default end of line character */
730 his_pad_count = 0;
731 his_pad_char = '\0';
732 his_quote = K_ESCAPE;
733
734 /* ignore last character if it filled the buffer */
735 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
736 --send_ptr;
737 bytes = send_ptr - send_parms; /* how many bytes we'll process */
738 do {
739 if (bytes-- <= 0)
740 break;
741 /* handle MAXL - max length */
742 /* ignore what he says - most I'll take (here) is 94 */
743 a_b[++length] = tochar (94);
744 if (bytes-- <= 0)
745 break;
746 /* handle TIME - time you should wait for my packets */
747 /* ignore what he says - don't wait for my ack longer than 1 second */
748 a_b[++length] = tochar (1);
749 if (bytes-- <= 0)
750 break;
751 /* handle NPAD - number of pad chars I need */
752 /* remember what he says - I need none */
753 his_pad_count = untochar (send_parms[2]);
754 a_b[++length] = tochar (0);
755 if (bytes-- <= 0)
756 break;
757 /* handle PADC - pad chars I need */
758 /* remember what he says - I need none */
759 his_pad_char = ktrans (send_parms[3]);
760 a_b[++length] = 0x40; /* He should ignore this */
761 if (bytes-- <= 0)
762 break;
763 /* handle EOL - end of line he needs */
764 /* remember what he says - I need CR */
765 his_eol = untochar (send_parms[4]);
766 a_b[++length] = tochar (END_CHAR);
767 if (bytes-- <= 0)
768 break;
769 /* handle QCTL - quote control char he'll use */
770 /* remember what he says - I'll use '#' */
771 his_quote = send_parms[5];
772 a_b[++length] = '#';
773 if (bytes-- <= 0)
774 break;
775 /* handle QBIN - 8-th bit prefixing */
776 /* ignore what he says - I refuse */
777 a_b[++length] = 'N';
778 if (bytes-- <= 0)
779 break;
780 /* handle CHKT - the clock check type */
781 /* ignore what he says - I do type 1 (for now) */
782 a_b[++length] = '1';
783 if (bytes-- <= 0)
784 break;
785 /* handle REPT - the repeat prefix */
786 /* ignore what he says - I refuse (for now) */
787 a_b[++length] = 'N';
788 if (bytes-- <= 0)
789 break;
790 /* handle CAPAS - the capabilities mask */
791 /* ignore what he says - I only do long packets - I don't do windows */
792 a_b[++length] = tochar (2); /* only long packets */
793 a_b[++length] = tochar (0); /* no windows */
794 a_b[++length] = tochar (94); /* large packet msb */
795 a_b[++length] = tochar (94); /* large packet lsb */
796 } while (0);
797
798 a_b[0] = START_CHAR;
799 a_b[1] = tochar (length);
800 a_b[2] = tochar (n);
801 a_b[3] = ACK_TYPE;
802 a_b[++length] = '\0';
803 a_b[length] = tochar (chk1 (&a_b[1]));
804 a_b[++length] = his_eol;
805 a_b[++length] = '\0';
806 s1_sendpacket (a_b);
807}
808
809/* k_recv receives a OS Open image file over kermit line */
810static int k_recv (void)
811{
812 char new_char;
813 char k_state, k_state_saved;
814 int sum;
815 int done;
816 int length;
817 int n, last_n;
818 int z = 0;
819 int len_lo, len_hi;
820
821 /* initialize some protocol parameters */
822 his_eol = END_CHAR; /* default end of line character */
823 his_pad_count = 0;
824 his_pad_char = '\0';
825 his_quote = K_ESCAPE;
826
827 /* initialize the k_recv and k_data state machine */
828 done = 0;
829 k_state = 0;
830 k_data_init ();
831 k_state_saved = k_state;
832 k_data_save ();
833 n = 0; /* just to get rid of a warning */
834 last_n = -1;
835
836 /* expect this "type" sequence (but don't check):
837 S: send initiate
838 F: file header
839 D: data (multiple)
840 Z: end of file
841 B: break transmission
842 */
843
844 /* enter main loop */
845 while (!done) {
846 /* set the send packet pointer to begining of send packet parms */
847 send_ptr = send_parms;
848
849 /* With each packet, start summing the bytes starting with the length.
850 Save the current sequence number.
851 Note the type of the packet.
852 If a character less than SPACE (0x20) is received - error.
853 */
854
855#if 0
856 /* OLD CODE, Prior to checking sequence numbers */
857 /* first have all state machines save current states */
858 k_state_saved = k_state;
859 k_data_save ();
860#endif
861
862 /* get a packet */
863 /* wait for the starting character or ^C */
864 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000865 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000866 case START_CHAR: /* start packet */
867 goto START;
868 case ETX_CHAR: /* ^C waiting for packet */
869 return (0);
870 default:
871 ;
872 }
873 }
874START:
875 /* get length of packet */
876 sum = 0;
wdenk232c1502004-03-12 00:14:09 +0000877 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000878 if ((new_char & 0xE0) == 0)
879 goto packet_error;
880 sum += new_char & 0xff;
881 length = untochar (new_char);
882 /* get sequence number */
wdenk232c1502004-03-12 00:14:09 +0000883 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000884 if ((new_char & 0xE0) == 0)
885 goto packet_error;
886 sum += new_char & 0xff;
887 n = untochar (new_char);
888 --length;
889
890 /* NEW CODE - check sequence numbers for retried packets */
891 /* Note - this new code assumes that the sequence number is correctly
892 * received. Handling an invalid sequence number adds another layer
893 * of complexity that may not be needed - yet! At this time, I'm hoping
894 * that I don't need to buffer the incoming data packets and can write
895 * the data into memory in real time.
896 */
897 if (n == last_n) {
898 /* same sequence number, restore the previous state */
899 k_state = k_state_saved;
900 k_data_restore ();
901 } else {
902 /* new sequence number, checkpoint the download */
903 last_n = n;
904 k_state_saved = k_state;
905 k_data_save ();
906 }
907 /* END NEW CODE */
908
909 /* get packet type */
wdenk232c1502004-03-12 00:14:09 +0000910 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000911 if ((new_char & 0xE0) == 0)
912 goto packet_error;
913 sum += new_char & 0xff;
914 k_state = new_char;
915 --length;
916 /* check for extended length */
917 if (length == -2) {
918 /* (length byte was 0, decremented twice) */
919 /* get the two length bytes */
wdenk232c1502004-03-12 00:14:09 +0000920 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000921 if ((new_char & 0xE0) == 0)
922 goto packet_error;
923 sum += new_char & 0xff;
924 len_hi = untochar (new_char);
wdenk232c1502004-03-12 00:14:09 +0000925 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000926 if ((new_char & 0xE0) == 0)
927 goto packet_error;
928 sum += new_char & 0xff;
929 len_lo = untochar (new_char);
930 length = len_hi * 95 + len_lo;
931 /* check header checksum */
wdenk232c1502004-03-12 00:14:09 +0000932 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000933 if ((new_char & 0xE0) == 0)
934 goto packet_error;
935 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
936 goto packet_error;
937 sum += new_char & 0xff;
938/* --length; */ /* new length includes only data and block check to come */
939 }
940 /* bring in rest of packet */
941 while (length > 1) {
wdenk232c1502004-03-12 00:14:09 +0000942 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000943 if ((new_char & 0xE0) == 0)
944 goto packet_error;
945 sum += new_char & 0xff;
946 --length;
947 if (k_state == DATA_TYPE) {
948 /* pass on the data if this is a data packet */
949 k_data_char (new_char);
950 } else if (k_state == SEND_TYPE) {
951 /* save send pack in buffer as is */
952 *send_ptr++ = new_char;
953 /* if too much data, back off the pointer */
954 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
955 --send_ptr;
956 }
957 }
958 /* get and validate checksum character */
wdenk232c1502004-03-12 00:14:09 +0000959 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000960 if ((new_char & 0xE0) == 0)
961 goto packet_error;
962 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
963 goto packet_error;
964 /* get END_CHAR */
wdenk232c1502004-03-12 00:14:09 +0000965 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000966 if (new_char != END_CHAR) {
967 packet_error:
968 /* restore state machines */
969 k_state = k_state_saved;
970 k_data_restore ();
971 /* send a negative acknowledge packet in */
972 send_nack (n);
973 } else if (k_state == SEND_TYPE) {
974 /* crack the protocol parms, build an appropriate ack packet */
975 handle_send_packet (n);
976 } else {
977 /* send simple acknowledge packet in */
978 send_ack (n);
979 /* quit if end of transmission */
980 if (k_state == BREAK_TYPE)
981 done = 1;
982 }
983 ++z;
984 }
985 return ((ulong) os_data_addr - (ulong) bin_start_address);
986}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200987
988static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200989 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200990 return (getc());
991 return -1;
992}
993static ulong load_serial_ymodem (ulong offset)
994{
995 int size;
996 char buf[32];
997 int err;
998 int res;
999 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001000 char ymodemBuf[1024];
1001 ulong store_addr = ~0;
1002 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001003
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001004 size = 0;
1005 info.mode = xyzModem_ymodem;
1006 res = xyzModem_stream_open (&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001007 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001008
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001009 while ((res =
1010 xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) {
1011 store_addr = addr + offset;
1012 size += res;
1013 addr += res;
1014#ifndef CFG_NO_FLASH
1015 if (addr2info (store_addr)) {
1016 int rc;
1017
1018 rc = flash_write ((char *) ymodemBuf,
1019 store_addr, res);
1020 if (rc != 0) {
1021 flash_perror (rc);
1022 return (~0);
1023 }
1024 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001025#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001026 {
1027 memcpy ((char *) (store_addr), ymodemBuf,
1028 res);
1029 }
1030
1031 }
1032 } else {
1033 printf ("%s\n", xyzModem_error (err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001034 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001035
1036 xyzModem_stream_close (&err);
1037 xyzModem_stream_terminate (false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001038
1039
1040 flush_cache (offset, size);
1041
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001042 printf ("## Total Size = 0x%08x = %d Bytes\n", size, size);
1043 sprintf (buf, "%X", size);
1044 setenv ("filesize", buf);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001045
1046 return offset;
1047}
1048
Jon Loeliger90253172007-07-10 11:02:44 -05001049#endif
wdenk8bde7f72003-06-27 21:31:46 +00001050
1051/* -------------------------------------------------------------------- */
1052
Jon Loeligerc76fe472007-07-08 18:02:23 -05001053#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001054
1055#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001056U_BOOT_CMD(
1057 loads, 3, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001058 "loads - load S-Record file over serial line\n",
1059 "[ off ] [ baud ]\n"
1060 " - load S-Record file over serial line"
1061 " with offset 'off' and baudrate 'baud'\n"
1062);
1063
1064#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001065U_BOOT_CMD(
1066 loads, 2, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001067 "loads - load S-Record file over serial line\n",
1068 "[ off ]\n"
1069 " - load S-Record file over serial line with offset 'off'\n"
1070);
1071#endif /* CFG_LOADS_BAUD_CHANGE */
1072
1073/*
1074 * SAVES always requires LOADS support, but not vice versa
1075 */
1076
1077
Jon Loeligerc76fe472007-07-08 18:02:23 -05001078#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +00001079#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001080U_BOOT_CMD(
1081 saves, 4, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001082 "saves - save S-Record file over serial line\n",
1083 "[ off ] [size] [ baud ]\n"
1084 " - save S-Record file over serial line"
1085 " with offset 'off', size 'size' and baudrate 'baud'\n"
1086);
1087#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001088U_BOOT_CMD(
1089 saves, 3, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001090 "saves - save S-Record file over serial line\n",
1091 "[ off ] [size]\n"
1092 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1093);
1094#endif /* CFG_LOADS_BAUD_CHANGE */
Jon Loeliger90253172007-07-10 11:02:44 -05001095#endif
1096#endif
wdenk8bde7f72003-06-27 21:31:46 +00001097
1098
Jon Loeligerc76fe472007-07-08 18:02:23 -05001099#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001100U_BOOT_CMD(
1101 loadb, 3, 0, do_load_serial_bin,
wdenk8bde7f72003-06-27 21:31:46 +00001102 "loadb - load binary file over serial line (kermit mode)\n",
1103 "[ off ] [ baud ]\n"
1104 " - load binary file over serial line"
1105 " with offset 'off' and baudrate 'baud'\n"
1106);
1107
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001108U_BOOT_CMD(
1109 loady, 3, 0, do_load_serial_bin,
1110 "loady - load binary file over serial line (ymodem mode)\n",
1111 "[ off ] [ baud ]\n"
1112 " - load binary file over serial line"
1113 " with offset 'off' and baudrate 'baud'\n"
1114);
1115
Jon Loeliger90253172007-07-10 11:02:44 -05001116#endif
wdenk8bde7f72003-06-27 21:31:46 +00001117
1118/* -------------------------------------------------------------------- */
1119
Jon Loeligerc76fe472007-07-08 18:02:23 -05001120#if defined(CONFIG_CMD_HWFLOW)
wdenk8bde7f72003-06-27 21:31:46 +00001121int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1122{
1123 extern int hwflow_onoff(int);
1124
1125 if (argc == 2) {
1126 if (strcmp(argv[1], "off") == 0)
1127 hwflow_onoff(-1);
1128 else
1129 if (strcmp(argv[1], "on") == 0)
1130 hwflow_onoff(1);
1131 else
1132 printf("Usage: %s\n", cmdtp->usage);
1133 }
1134 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1135 return 0;
1136}
1137
1138/* -------------------------------------------------------------------- */
1139
wdenk0d498392003-07-01 21:06:45 +00001140U_BOOT_CMD(
wdenkf12e5682003-07-07 20:07:54 +00001141 hwflow, 2, 0, do_hwflow,
wdenk8bde7f72003-06-27 21:31:46 +00001142 "hwflow - turn the harwdare flow control on/off\n",
wdenkf12e5682003-07-07 20:07:54 +00001143 "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
wdenk8bde7f72003-06-27 21:31:46 +00001144);
1145
Jon Loeliger90253172007-07-10 11:02:44 -05001146#endif