add SDHC support in mmc driver
[u-boot-openmoko/mini2440.git] / common / cmd_load.c
blob1b75a7b5eccf15289e03d9f341efd5c193ba444e
1 /*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
25 * Serial up- and download support
27 #include <common.h>
28 #include <command.h>
29 #include <s_record.h>
30 #include <net.h>
31 #include <exports.h>
32 #include <xyzModem.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 #if defined(CONFIG_CMD_LOADB)
37 static ulong load_serial_ymodem (ulong offset);
38 #endif
40 #if defined(CONFIG_CMD_LOADS)
41 static ulong load_serial (ulong offset);
42 static int read_record (char *buf, ulong len);
43 # if defined(CONFIG_CMD_SAVES)
44 static int save_serial (ulong offset, ulong size);
45 static int write_record (char *buf);
46 #endif
48 static int do_echo = 1;
49 #endif
51 /* -------------------------------------------------------------------- */
53 #if defined(CONFIG_CMD_LOADS)
54 int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
56 ulong offset = 0;
57 ulong addr;
58 int i;
59 char *env_echo;
60 int rcode = 0;
61 #ifdef CFG_LOADS_BAUD_CHANGE
62 int load_baudrate, current_baudrate;
64 load_baudrate = current_baudrate = gd->baudrate;
65 #endif
67 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
68 do_echo = 1;
69 } else {
70 do_echo = 0;
73 #ifdef CFG_LOADS_BAUD_CHANGE
74 if (argc >= 2) {
75 offset = simple_strtoul(argv[1], NULL, 16);
77 if (argc == 3) {
78 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
80 /* default to current baudrate */
81 if (load_baudrate == 0)
82 load_baudrate = current_baudrate;
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;
96 #else /* ! CFG_LOADS_BAUD_CHANGE */
97 if (argc == 2) {
98 offset = simple_strtoul(argv[1], NULL, 16);
100 #endif /* CFG_LOADS_BAUD_CHANGE */
102 printf ("## Ready for S-Record download ...\n");
104 addr = load_serial (offset);
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)
111 for (i=0; i<100; ++i) {
112 if (tstc()) {
113 (void) getc();
115 udelay(1000);
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;
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;
139 #endif
140 return rcode;
143 static ulong
144 load_serial (ulong offset)
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;
158 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
159 type = srec_decode (record, &binlen, &addr, binbuf);
161 if (type < 0) {
162 return (~0); /* Invalid S-Record */
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;
174 rc = flash_write((char *)binbuf,store_addr,binlen);
175 if (rc != 0) {
176 flash_perror (rc);
177 return (~0);
179 } else
180 #endif
182 memcpy ((char *)(store_addr), binbuf, binlen);
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
200 flush_cache (start_addr, size);
201 sprintf(buf, "%lX", size);
202 setenv("filesize", buf);
203 return (addr);
204 case SREC_START:
205 break;
206 default:
207 break;
209 if (!do_echo) { /* print a '.' every 100 lines */
210 if ((++line_count % 100) == 0)
211 putc ('.');
215 return (~0); /* Download aborted */
218 static int
219 read_record (char *buf, ulong len)
221 char *p;
222 char c;
224 --len; /* always leave room for terminating '\0' byte */
226 for (p=buf; p < buf+len; ++p) {
227 c = getc(); /* read character */
228 if (do_echo)
229 putc (c); /* ... and echo it */
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;
243 /* Check for the console hangup (if any different from serial) */
244 if (gd->jt[XF_getc] != getc) {
245 if (ctrlc()) {
246 return (-1);
251 /* line too long - truncate */
252 *p = '\0';
253 return (p - buf);
256 #if defined(CONFIG_CMD_SAVES)
258 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
260 ulong offset = 0;
261 ulong size = 0;
262 #ifdef CFG_LOADS_BAUD_CHANGE
263 int save_baudrate, current_baudrate;
265 save_baudrate = current_baudrate = gd->baudrate;
266 #endif
268 if (argc >= 2) {
269 offset = simple_strtoul(argv[1], NULL, 16);
271 #ifdef CFG_LOADS_BAUD_CHANGE
272 if (argc >= 3) {
273 size = simple_strtoul(argv[2], NULL, 16);
275 if (argc == 4) {
276 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
278 /* default to current baudrate */
279 if (save_baudrate == 0)
280 save_baudrate = current_baudrate;
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;
294 #else /* ! CFG_LOADS_BAUD_CHANGE */
295 if (argc == 3) {
296 size = simple_strtoul(argv[2], NULL, 16);
298 #endif /* CFG_LOADS_BAUD_CHANGE */
300 printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
301 for (;;) {
302 if (getc() == '\r')
303 break;
305 if(save_serial (offset, size)) {
306 printf ("## S-Record upload aborted\n");
307 } else {
308 printf ("## S-Record upload complete\n");
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;
323 #endif
324 return 0;
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
332 static int save_serial (ulong address, ulong count)
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 */
339 reclen = 0;
340 checksum = 0;
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;
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;
360 /* accumulate length bytes into checksum */
361 for(i = 0; i < 2; i++)
362 checksum += (length >> (8*i)) & 0xff;
364 /* accumulate address bytes into checksum: */
365 for(i = 0; i < 4; i++)
366 checksum += (address >> (8*i)) & 0xff;
368 /* make proper checksum byte: */
369 checksum = ~checksum & 0xff;
371 /* output one record: */
372 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
373 if(write_record(record))
374 return (-1);
376 address += reclen; /* increment address */
377 checksum = 0;
378 reclen = 0;
381 while(count);
382 if(write_record(SREC3_END)) /* write the final record */
383 return (-1);
384 return(0);
387 static int
388 write_record (char *buf)
390 char c;
392 while((c = *buf++))
393 putc(c);
395 /* Check for the console hangup (if any different from serial) */
397 if (ctrlc()) {
398 return (-1);
400 return (0);
402 # endif
404 #endif
407 #if defined(CONFIG_CMD_LOADB)
409 * loadb command (load binary) included
411 #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))
426 extern int os_data_count;
427 extern int os_data_header[8];
429 static void set_kerm_bin_mode(unsigned long *);
430 static int k_recv(void);
431 static ulong load_serial_bin (ulong offset);
434 char his_eol; /* character he needs at end of packet */
435 int his_pad_count; /* number of pad chars he needs */
436 char his_pad_char; /* pad chars he needs */
437 char his_quote; /* quote chars he'll use */
439 int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
441 ulong offset = 0;
442 ulong addr;
443 int load_baudrate, current_baudrate;
444 int rcode = 0;
445 char *s;
447 /* pre-set offset from CFG_LOAD_ADDR */
448 offset = CFG_LOAD_ADDR;
450 /* pre-set offset from $loadaddr */
451 if ((s = getenv("loadaddr")) != NULL) {
452 offset = simple_strtoul(s, NULL, 16);
455 load_baudrate = current_baudrate = gd->baudrate;
457 if (argc >= 2) {
458 offset = simple_strtoul(argv[1], NULL, 16);
460 if (argc == 3) {
461 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
463 /* default to current baudrate */
464 if (load_baudrate == 0)
465 load_baudrate = current_baudrate;
468 if (load_baudrate != current_baudrate) {
469 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
470 load_baudrate);
471 udelay(50000);
472 gd->baudrate = load_baudrate;
473 serial_setbrg ();
474 udelay(50000);
475 for (;;) {
476 if (getc() == '\r')
477 break;
481 if (strcmp(argv[0],"loady")==0) {
482 printf ("## Ready for binary (ymodem) download "
483 "to 0x%08lX at %d bps...\n",
484 offset,
485 load_baudrate);
487 addr = load_serial_ymodem (offset);
489 } else {
491 printf ("## Ready for binary (kermit) download "
492 "to 0x%08lX at %d bps...\n",
493 offset,
494 load_baudrate);
495 addr = load_serial_bin (offset);
497 if (addr == ~0) {
498 load_addr = 0;
499 printf ("## Binary (kermit) download aborted\n");
500 rcode = 1;
501 } else {
502 printf ("## Start Addr = 0x%08lX\n", addr);
503 load_addr = addr;
506 if (load_baudrate != current_baudrate) {
507 printf ("## Switch baudrate to %d bps and press ESC ...\n",
508 current_baudrate);
509 udelay (50000);
510 gd->baudrate = current_baudrate;
511 serial_setbrg ();
512 udelay (50000);
513 for (;;) {
514 if (getc() == 0x1B) /* ESC */
515 break;
519 #ifdef CONFIG_AUTOSCRIPT
520 if (load_addr) {
521 char *s;
523 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
524 printf ("Running autoscript at addr 0x%08lX", load_addr);
526 s = getenv ("autoscript_uname");
527 if (s)
528 printf (":%s ...\n", s);
529 else
530 puts (" ...\n");
532 rcode = autoscript (load_addr, s);
535 #endif
536 return rcode;
540 static ulong load_serial_bin (ulong offset)
542 int size, i;
543 char buf[32];
545 set_kerm_bin_mode ((ulong *) offset);
546 size = k_recv ();
549 * Gather any trailing characters (for instance, the ^D which
550 * is sent by 'cu' after sending a file), and give the
551 * box some time (100 * 1 ms)
553 for (i=0; i<100; ++i) {
554 if (tstc()) {
555 (void) getc();
557 udelay(1000);
560 flush_cache (offset, size);
562 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
563 sprintf(buf, "%X", size);
564 setenv("filesize", buf);
566 return offset;
569 void send_pad (void)
571 int count = his_pad_count;
573 while (count-- > 0)
574 putc (his_pad_char);
577 /* converts escaped kermit char to binary char */
578 char ktrans (char in)
580 if ((in & 0x60) == 0x40) {
581 return (char) (in & ~0x40);
582 } else if ((in & 0x7f) == 0x3f) {
583 return (char) (in | 0x40);
584 } else
585 return in;
588 int chk1 (char *buffer)
590 int total = 0;
592 while (*buffer) {
593 total += *buffer++;
595 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
598 void s1_sendpacket (char *packet)
600 send_pad ();
601 while (*packet) {
602 putc (*packet++);
606 static char a_b[24];
607 void send_ack (int n)
609 a_b[0] = START_CHAR;
610 a_b[1] = tochar (3);
611 a_b[2] = tochar (n);
612 a_b[3] = ACK_TYPE;
613 a_b[4] = '\0';
614 a_b[4] = tochar (chk1 (&a_b[1]));
615 a_b[5] = his_eol;
616 a_b[6] = '\0';
617 s1_sendpacket (a_b);
620 void send_nack (int n)
622 a_b[0] = START_CHAR;
623 a_b[1] = tochar (3);
624 a_b[2] = tochar (n);
625 a_b[3] = NACK_TYPE;
626 a_b[4] = '\0';
627 a_b[4] = tochar (chk1 (&a_b[1]));
628 a_b[5] = his_eol;
629 a_b[6] = '\0';
630 s1_sendpacket (a_b);
634 /* os_data_* takes an OS Open image and puts it into memory, and
635 puts the boot header in an array named os_data_header
637 if image is binary, no header is stored in os_data_header.
639 void (*os_data_init) (void);
640 void (*os_data_char) (char new_char);
641 static int os_data_state, os_data_state_saved;
642 int os_data_count;
643 static int os_data_count_saved;
644 static char *os_data_addr, *os_data_addr_saved;
645 static char *bin_start_address;
646 int os_data_header[8];
647 static void bin_data_init (void)
649 os_data_state = 0;
650 os_data_count = 0;
651 os_data_addr = bin_start_address;
653 static void os_data_save (void)
655 os_data_state_saved = os_data_state;
656 os_data_count_saved = os_data_count;
657 os_data_addr_saved = os_data_addr;
659 static void os_data_restore (void)
661 os_data_state = os_data_state_saved;
662 os_data_count = os_data_count_saved;
663 os_data_addr = os_data_addr_saved;
665 static void bin_data_char (char new_char)
667 switch (os_data_state) {
668 case 0: /* data */
669 *os_data_addr++ = new_char;
670 --os_data_count;
671 break;
674 static void set_kerm_bin_mode (unsigned long *addr)
676 bin_start_address = (char *) addr;
677 os_data_init = bin_data_init;
678 os_data_char = bin_data_char;
682 /* k_data_* simply handles the kermit escape translations */
683 static int k_data_escape, k_data_escape_saved;
684 void k_data_init (void)
686 k_data_escape = 0;
687 os_data_init ();
689 void k_data_save (void)
691 k_data_escape_saved = k_data_escape;
692 os_data_save ();
694 void k_data_restore (void)
696 k_data_escape = k_data_escape_saved;
697 os_data_restore ();
699 void k_data_char (char new_char)
701 if (k_data_escape) {
702 /* last char was escape - translate this character */
703 os_data_char (ktrans (new_char));
704 k_data_escape = 0;
705 } else {
706 if (new_char == his_quote) {
707 /* this char is escape - remember */
708 k_data_escape = 1;
709 } else {
710 /* otherwise send this char as-is */
711 os_data_char (new_char);
716 #define SEND_DATA_SIZE 20
717 char send_parms[SEND_DATA_SIZE];
718 char *send_ptr;
720 /* handle_send_packet interprits the protocol info and builds and
721 sends an appropriate ack for what we can do */
722 void handle_send_packet (int n)
724 int length = 3;
725 int bytes;
727 /* initialize some protocol parameters */
728 his_eol = END_CHAR; /* default end of line character */
729 his_pad_count = 0;
730 his_pad_char = '\0';
731 his_quote = K_ESCAPE;
733 /* ignore last character if it filled the buffer */
734 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
735 --send_ptr;
736 bytes = send_ptr - send_parms; /* how many bytes we'll process */
737 do {
738 if (bytes-- <= 0)
739 break;
740 /* handle MAXL - max length */
741 /* ignore what he says - most I'll take (here) is 94 */
742 a_b[++length] = tochar (94);
743 if (bytes-- <= 0)
744 break;
745 /* handle TIME - time you should wait for my packets */
746 /* ignore what he says - don't wait for my ack longer than 1 second */
747 a_b[++length] = tochar (1);
748 if (bytes-- <= 0)
749 break;
750 /* handle NPAD - number of pad chars I need */
751 /* remember what he says - I need none */
752 his_pad_count = untochar (send_parms[2]);
753 a_b[++length] = tochar (0);
754 if (bytes-- <= 0)
755 break;
756 /* handle PADC - pad chars I need */
757 /* remember what he says - I need none */
758 his_pad_char = ktrans (send_parms[3]);
759 a_b[++length] = 0x40; /* He should ignore this */
760 if (bytes-- <= 0)
761 break;
762 /* handle EOL - end of line he needs */
763 /* remember what he says - I need CR */
764 his_eol = untochar (send_parms[4]);
765 a_b[++length] = tochar (END_CHAR);
766 if (bytes-- <= 0)
767 break;
768 /* handle QCTL - quote control char he'll use */
769 /* remember what he says - I'll use '#' */
770 his_quote = send_parms[5];
771 a_b[++length] = '#';
772 if (bytes-- <= 0)
773 break;
774 /* handle QBIN - 8-th bit prefixing */
775 /* ignore what he says - I refuse */
776 a_b[++length] = 'N';
777 if (bytes-- <= 0)
778 break;
779 /* handle CHKT - the clock check type */
780 /* ignore what he says - I do type 1 (for now) */
781 a_b[++length] = '1';
782 if (bytes-- <= 0)
783 break;
784 /* handle REPT - the repeat prefix */
785 /* ignore what he says - I refuse (for now) */
786 a_b[++length] = 'N';
787 if (bytes-- <= 0)
788 break;
789 /* handle CAPAS - the capabilities mask */
790 /* ignore what he says - I only do long packets - I don't do windows */
791 a_b[++length] = tochar (2); /* only long packets */
792 a_b[++length] = tochar (0); /* no windows */
793 a_b[++length] = tochar (94); /* large packet msb */
794 a_b[++length] = tochar (94); /* large packet lsb */
795 } while (0);
797 a_b[0] = START_CHAR;
798 a_b[1] = tochar (length);
799 a_b[2] = tochar (n);
800 a_b[3] = ACK_TYPE;
801 a_b[++length] = '\0';
802 a_b[length] = tochar (chk1 (&a_b[1]));
803 a_b[++length] = his_eol;
804 a_b[++length] = '\0';
805 s1_sendpacket (a_b);
808 /* k_recv receives a OS Open image file over kermit line */
809 static int k_recv (void)
811 char new_char;
812 char k_state, k_state_saved;
813 int sum;
814 int done;
815 int length;
816 int n, last_n;
817 int z = 0;
818 int len_lo, len_hi;
820 /* initialize some protocol parameters */
821 his_eol = END_CHAR; /* default end of line character */
822 his_pad_count = 0;
823 his_pad_char = '\0';
824 his_quote = K_ESCAPE;
826 /* initialize the k_recv and k_data state machine */
827 done = 0;
828 k_state = 0;
829 k_data_init ();
830 k_state_saved = k_state;
831 k_data_save ();
832 n = 0; /* just to get rid of a warning */
833 last_n = -1;
835 /* expect this "type" sequence (but don't check):
836 S: send initiate
837 F: file header
838 D: data (multiple)
839 Z: end of file
840 B: break transmission
843 /* enter main loop */
844 while (!done) {
845 /* set the send packet pointer to begining of send packet parms */
846 send_ptr = send_parms;
848 /* With each packet, start summing the bytes starting with the length.
849 Save the current sequence number.
850 Note the type of the packet.
851 If a character less than SPACE (0x20) is received - error.
854 #if 0
855 /* OLD CODE, Prior to checking sequence numbers */
856 /* first have all state machines save current states */
857 k_state_saved = k_state;
858 k_data_save ();
859 #endif
861 /* get a packet */
862 /* wait for the starting character or ^C */
863 for (;;) {
864 switch (getc ()) {
865 case START_CHAR: /* start packet */
866 goto START;
867 case ETX_CHAR: /* ^C waiting for packet */
868 return (0);
869 default:
873 START:
874 /* get length of packet */
875 sum = 0;
876 new_char = getc ();
877 if ((new_char & 0xE0) == 0)
878 goto packet_error;
879 sum += new_char & 0xff;
880 length = untochar (new_char);
881 /* get sequence number */
882 new_char = getc ();
883 if ((new_char & 0xE0) == 0)
884 goto packet_error;
885 sum += new_char & 0xff;
886 n = untochar (new_char);
887 --length;
889 /* NEW CODE - check sequence numbers for retried packets */
890 /* Note - this new code assumes that the sequence number is correctly
891 * received. Handling an invalid sequence number adds another layer
892 * of complexity that may not be needed - yet! At this time, I'm hoping
893 * that I don't need to buffer the incoming data packets and can write
894 * the data into memory in real time.
896 if (n == last_n) {
897 /* same sequence number, restore the previous state */
898 k_state = k_state_saved;
899 k_data_restore ();
900 } else {
901 /* new sequence number, checkpoint the download */
902 last_n = n;
903 k_state_saved = k_state;
904 k_data_save ();
906 /* END NEW CODE */
908 /* get packet type */
909 new_char = getc ();
910 if ((new_char & 0xE0) == 0)
911 goto packet_error;
912 sum += new_char & 0xff;
913 k_state = new_char;
914 --length;
915 /* check for extended length */
916 if (length == -2) {
917 /* (length byte was 0, decremented twice) */
918 /* get the two length bytes */
919 new_char = getc ();
920 if ((new_char & 0xE0) == 0)
921 goto packet_error;
922 sum += new_char & 0xff;
923 len_hi = untochar (new_char);
924 new_char = getc ();
925 if ((new_char & 0xE0) == 0)
926 goto packet_error;
927 sum += new_char & 0xff;
928 len_lo = untochar (new_char);
929 length = len_hi * 95 + len_lo;
930 /* check header checksum */
931 new_char = getc ();
932 if ((new_char & 0xE0) == 0)
933 goto packet_error;
934 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
935 goto packet_error;
936 sum += new_char & 0xff;
937 /* --length; */ /* new length includes only data and block check to come */
939 /* bring in rest of packet */
940 while (length > 1) {
941 new_char = getc ();
942 if ((new_char & 0xE0) == 0)
943 goto packet_error;
944 sum += new_char & 0xff;
945 --length;
946 if (k_state == DATA_TYPE) {
947 /* pass on the data if this is a data packet */
948 k_data_char (new_char);
949 } else if (k_state == SEND_TYPE) {
950 /* save send pack in buffer as is */
951 *send_ptr++ = new_char;
952 /* if too much data, back off the pointer */
953 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
954 --send_ptr;
957 /* get and validate checksum character */
958 new_char = getc ();
959 if ((new_char & 0xE0) == 0)
960 goto packet_error;
961 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
962 goto packet_error;
963 /* get END_CHAR */
964 new_char = getc ();
965 if (new_char != END_CHAR) {
966 packet_error:
967 /* restore state machines */
968 k_state = k_state_saved;
969 k_data_restore ();
970 /* send a negative acknowledge packet in */
971 send_nack (n);
972 } else if (k_state == SEND_TYPE) {
973 /* crack the protocol parms, build an appropriate ack packet */
974 handle_send_packet (n);
975 } else {
976 /* send simple acknowledge packet in */
977 send_ack (n);
978 /* quit if end of transmission */
979 if (k_state == BREAK_TYPE)
980 done = 1;
982 ++z;
984 return ((ulong) os_data_addr - (ulong) bin_start_address);
987 static int getcxmodem(void) {
988 if (tstc())
989 return (getc());
990 return -1;
992 static ulong load_serial_ymodem (ulong offset)
994 int size;
995 char buf[32];
996 int err;
997 int res;
998 connection_info_t info;
999 char ymodemBuf[1024];
1000 ulong store_addr = ~0;
1001 ulong addr = 0;
1003 size = 0;
1004 info.mode = xyzModem_ymodem;
1005 res = xyzModem_stream_open (&info, &err);
1006 if (!res) {
1008 while ((res =
1009 xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) {
1010 store_addr = addr + offset;
1011 size += res;
1012 addr += res;
1013 #ifndef CFG_NO_FLASH
1014 if (addr2info (store_addr)) {
1015 int rc;
1017 rc = flash_write ((char *) ymodemBuf,
1018 store_addr, res);
1019 if (rc != 0) {
1020 flash_perror (rc);
1021 return (~0);
1023 } else
1024 #endif
1026 memcpy ((char *) (store_addr), ymodemBuf,
1027 res);
1031 } else {
1032 printf ("%s\n", xyzModem_error (err));
1035 xyzModem_stream_close (&err);
1036 xyzModem_stream_terminate (false, &getcxmodem);
1039 flush_cache (offset, size);
1041 printf ("## Total Size = 0x%08x = %d Bytes\n", size, size);
1042 sprintf (buf, "%X", size);
1043 setenv ("filesize", buf);
1045 return offset;
1048 #endif
1050 /* -------------------------------------------------------------------- */
1052 #if defined(CONFIG_CMD_LOADS)
1054 #ifdef CFG_LOADS_BAUD_CHANGE
1055 U_BOOT_CMD(
1056 loads, 3, 0, do_load_serial,
1057 "loads - load S-Record file over serial line\n",
1058 "[ off ] [ baud ]\n"
1059 " - load S-Record file over serial line"
1060 " with offset 'off' and baudrate 'baud'\n"
1063 #else /* ! CFG_LOADS_BAUD_CHANGE */
1064 U_BOOT_CMD(
1065 loads, 2, 0, do_load_serial,
1066 "loads - load S-Record file over serial line\n",
1067 "[ off ]\n"
1068 " - load S-Record file over serial line with offset 'off'\n"
1070 #endif /* CFG_LOADS_BAUD_CHANGE */
1073 * SAVES always requires LOADS support, but not vice versa
1077 #if defined(CONFIG_CMD_SAVES)
1078 #ifdef CFG_LOADS_BAUD_CHANGE
1079 U_BOOT_CMD(
1080 saves, 4, 0, do_save_serial,
1081 "saves - save S-Record file over serial line\n",
1082 "[ off ] [size] [ baud ]\n"
1083 " - save S-Record file over serial line"
1084 " with offset 'off', size 'size' and baudrate 'baud'\n"
1086 #else /* ! CFG_LOADS_BAUD_CHANGE */
1087 U_BOOT_CMD(
1088 saves, 3, 0, do_save_serial,
1089 "saves - save S-Record file over serial line\n",
1090 "[ off ] [size]\n"
1091 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1093 #endif /* CFG_LOADS_BAUD_CHANGE */
1094 #endif
1095 #endif
1098 #if defined(CONFIG_CMD_LOADB)
1099 U_BOOT_CMD(
1100 loadb, 3, 0, do_load_serial_bin,
1101 "loadb - load binary file over serial line (kermit mode)\n",
1102 "[ off ] [ baud ]\n"
1103 " - load binary file over serial line"
1104 " with offset 'off' and baudrate 'baud'\n"
1107 U_BOOT_CMD(
1108 loady, 3, 0, do_load_serial_bin,
1109 "loady - load binary file over serial line (ymodem mode)\n",
1110 "[ off ] [ baud ]\n"
1111 " - load binary file over serial line"
1112 " with offset 'off' and baudrate 'baud'\n"
1115 #endif
1117 /* -------------------------------------------------------------------- */
1119 #if defined(CONFIG_CMD_HWFLOW)
1120 int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1122 extern int hwflow_onoff(int);
1124 if (argc == 2) {
1125 if (strcmp(argv[1], "off") == 0)
1126 hwflow_onoff(-1);
1127 else
1128 if (strcmp(argv[1], "on") == 0)
1129 hwflow_onoff(1);
1130 else
1131 printf("Usage: %s\n", cmdtp->usage);
1133 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1134 return 0;
1137 /* -------------------------------------------------------------------- */
1139 U_BOOT_CMD(
1140 hwflow, 2, 0, do_hwflow,
1141 "hwflow - turn the harwdare flow control on/off\n",
1142 "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
1145 #endif