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
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,
25 * Serial up- and download support
34 DECLARE_GLOBAL_DATA_PTR;
36 #if defined(CONFIG_CMD_LOADB)
37 static ulong load_serial_ymodem(ulong offset);
40 #if defined(CONFIG_CMD_LOADS)
41 static ulong load_serial(long 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);
48 static int do_echo = 1;
51 /* -------------------------------------------------------------------- */
53 #if defined(CONFIG_CMD_LOADS)
54 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
62 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
63 int load_baudrate, current_baudrate;
65 load_baudrate = current_baudrate = gd->baudrate;
68 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
74 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
76 offset = simple_strtol(argv[1], NULL, 16);
79 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
81 /* default to current baudrate */
82 if (load_baudrate == 0)
83 load_baudrate = current_baudrate;
85 if (load_baudrate != current_baudrate) {
86 printf("## Switch baudrate to %d bps and press ENTER ...\n",
89 gd->baudrate = load_baudrate;
97 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
99 offset = simple_strtol(argv[1], NULL, 16);
101 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
103 printf("## Ready for S-Record download ...\n");
105 addr = load_serial(offset);
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)
112 for (i=0; i<100; ++i) {
120 printf("## S-Record download aborted\n");
123 printf("## Start Addr = 0x%08lX\n", addr);
127 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
128 if (load_baudrate != current_baudrate) {
129 printf("## Switch baudrate to %d bps and press ESC ...\n",
132 gd->baudrate = current_baudrate;
136 if (getc() == 0x1B) /* ESC */
144 static ulong load_serial(long 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 */
154 ulong start_addr = ~0;
158 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
159 type = srec_decode(record, &binlen, &addr, binbuf);
162 return (~0); /* Invalid S-Record */
169 store_addr = addr + offset;
170 #ifndef CONFIG_SYS_NO_FLASH
171 if (addr2info(store_addr)) {
174 rc = flash_write((char *)binbuf,store_addr,binlen);
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;
193 size = end_addr - start_addr + 1;
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);
209 if (!do_echo) { /* print a '.' every 100 lines */
210 if ((++line_count % 100) == 0)
215 return (~0); /* Download aborted */
218 static int read_record(char *buf, ulong len)
223 --len; /* always leave room for terminating '\0' byte */
225 for (p=buf; p < buf+len; ++p) {
226 c = getc(); /* read character */
228 putc(c); /* ... and echo it */
236 case 0x03: /* ^C - Control C */
242 /* Check for the console hangup (if any different from serial) */
243 if (gd->jt[XF_getc] != getc) {
250 /* line too long - truncate */
255 #if defined(CONFIG_CMD_SAVES)
257 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
261 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
262 int save_baudrate, current_baudrate;
264 save_baudrate = current_baudrate = gd->baudrate;
268 offset = simple_strtoul(argv[1], NULL, 16);
270 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
272 size = simple_strtoul(argv[2], NULL, 16);
275 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
277 /* default to current baudrate */
278 if (save_baudrate == 0)
279 save_baudrate = current_baudrate;
281 if (save_baudrate != current_baudrate) {
282 printf("## Switch baudrate to %d bps and press ENTER ...\n",
285 gd->baudrate = save_baudrate;
293 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
295 size = simple_strtoul(argv[2], NULL, 16);
297 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
299 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
304 if (save_serial(offset, size)) {
305 printf("## S-Record upload aborted\n");
307 printf("## S-Record upload complete\n");
309 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
310 if (save_baudrate != current_baudrate) {
311 printf("## Switch baudrate to %d bps and press ESC ...\n",
312 (int)current_baudrate);
314 gd->baudrate = current_baudrate;
318 if (getc() == 0x1B) /* ESC */
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
331 static int save_serial(ulong address, ulong count)
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 */
341 if(write_record(SREC3_START)) /* write the header */
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';
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;
359 /* accumulate length bytes into checksum */
360 for(i = 0; i < 2; i++)
361 checksum += (length >> (8*i)) & 0xff;
363 /* accumulate address bytes into checksum: */
364 for(i = 0; i < 4; i++)
365 checksum += (address >> (8*i)) & 0xff;
367 /* make proper checksum byte: */
368 checksum = ~checksum & 0xff;
370 /* output one record: */
371 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
372 if(write_record(record))
375 address += reclen; /* increment address */
381 if(write_record(SREC3_END)) /* write the final record */
386 static int write_record(char *buf)
393 /* Check for the console hangup (if any different from serial) */
405 #if defined(CONFIG_CMD_LOADB)
407 * loadb command (load binary) included
411 #define START_CHAR 0x01
412 #define ETX_CHAR 0x03
413 #define END_CHAR 0x0D
415 #define K_ESCAPE 0x23
416 #define SEND_TYPE 'S'
417 #define DATA_TYPE 'D'
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))
424 static void set_kerm_bin_mode(unsigned long *);
425 static int k_recv(void);
426 static ulong load_serial_bin(ulong offset);
429 static char his_eol; /* character he needs at end of packet */
430 static int his_pad_count; /* number of pad chars he needs */
431 static char his_pad_char; /* pad chars he needs */
432 static char his_quote; /* quote chars he'll use */
434 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
439 int load_baudrate, current_baudrate;
443 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
444 offset = CONFIG_SYS_LOAD_ADDR;
446 /* pre-set offset from $loadaddr */
447 if ((s = getenv("loadaddr")) != NULL) {
448 offset = simple_strtoul(s, NULL, 16);
451 load_baudrate = current_baudrate = gd->baudrate;
454 offset = simple_strtoul(argv[1], NULL, 16);
457 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
459 /* default to current baudrate */
460 if (load_baudrate == 0)
461 load_baudrate = current_baudrate;
464 if (load_baudrate != current_baudrate) {
465 printf("## Switch baudrate to %d bps and press ENTER ...\n",
468 gd->baudrate = load_baudrate;
477 if (strcmp(argv[0],"loady")==0) {
478 printf("## Ready for binary (ymodem) download "
479 "to 0x%08lX at %d bps...\n",
483 addr = load_serial_ymodem(offset);
487 printf("## Ready for binary (kermit) download "
488 "to 0x%08lX at %d bps...\n",
491 addr = load_serial_bin(offset);
495 printf("## Binary (kermit) download aborted\n");
498 printf("## Start Addr = 0x%08lX\n", addr);
502 if (load_baudrate != current_baudrate) {
503 printf("## Switch baudrate to %d bps and press ESC ...\n",
506 gd->baudrate = current_baudrate;
510 if (getc() == 0x1B) /* ESC */
519 static ulong load_serial_bin(ulong offset)
524 set_kerm_bin_mode((ulong *) offset);
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)
532 for (i=0; i<100; ++i) {
539 flush_cache(offset, size);
541 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
542 sprintf(buf, "%X", size);
543 setenv("filesize", buf);
548 static void send_pad(void)
550 int count = his_pad_count;
556 /* converts escaped kermit char to binary char */
557 static char ktrans(char in)
559 if ((in & 0x60) == 0x40) {
560 return (char) (in & ~0x40);
561 } else if ((in & 0x7f) == 0x3f) {
562 return (char) (in | 0x40);
567 static int chk1(char *buffer)
574 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
577 static void s1_sendpacket(char *packet)
586 static void send_ack(int n)
593 a_b[4] = tochar(chk1(&a_b[1]));
599 static void send_nack(int n)
606 a_b[4] = tochar(chk1(&a_b[1]));
613 static void (*os_data_init)(void);
614 static void (*os_data_char)(char new_char);
615 static int os_data_state, os_data_state_saved;
616 static char *os_data_addr, *os_data_addr_saved;
617 static char *bin_start_address;
619 static void bin_data_init(void)
622 os_data_addr = bin_start_address;
625 static void os_data_save(void)
627 os_data_state_saved = os_data_state;
628 os_data_addr_saved = os_data_addr;
631 static void os_data_restore(void)
633 os_data_state = os_data_state_saved;
634 os_data_addr = os_data_addr_saved;
637 static void bin_data_char(char new_char)
639 switch (os_data_state) {
641 *os_data_addr++ = new_char;
646 static void set_kerm_bin_mode(unsigned long *addr)
648 bin_start_address = (char *) addr;
649 os_data_init = bin_data_init;
650 os_data_char = bin_data_char;
654 /* k_data_* simply handles the kermit escape translations */
655 static int k_data_escape, k_data_escape_saved;
656 static void k_data_init(void)
662 static void k_data_save(void)
664 k_data_escape_saved = k_data_escape;
668 static void k_data_restore(void)
670 k_data_escape = k_data_escape_saved;
674 static void k_data_char(char new_char)
677 /* last char was escape - translate this character */
678 os_data_char(ktrans(new_char));
681 if (new_char == his_quote) {
682 /* this char is escape - remember */
685 /* otherwise send this char as-is */
686 os_data_char(new_char);
691 #define SEND_DATA_SIZE 20
692 static char send_parms[SEND_DATA_SIZE];
693 static char *send_ptr;
695 /* handle_send_packet interprits the protocol info and builds and
696 sends an appropriate ack for what we can do */
697 static void handle_send_packet(int n)
702 /* initialize some protocol parameters */
703 his_eol = END_CHAR; /* default end of line character */
706 his_quote = K_ESCAPE;
708 /* ignore last character if it filled the buffer */
709 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
711 bytes = send_ptr - send_parms; /* how many bytes we'll process */
715 /* handle MAXL - max length */
716 /* ignore what he says - most I'll take (here) is 94 */
717 a_b[++length] = tochar(94);
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 */
722 a_b[++length] = tochar(1);
725 /* handle NPAD - number of pad chars I need */
726 /* remember what he says - I need none */
727 his_pad_count = untochar(send_parms[2]);
728 a_b[++length] = tochar(0);
731 /* handle PADC - pad chars I need */
732 /* remember what he says - I need none */
733 his_pad_char = ktrans(send_parms[3]);
734 a_b[++length] = 0x40; /* He should ignore this */
737 /* handle EOL - end of line he needs */
738 /* remember what he says - I need CR */
739 his_eol = untochar(send_parms[4]);
740 a_b[++length] = tochar(END_CHAR);
743 /* handle QCTL - quote control char he'll use */
744 /* remember what he says - I'll use '#' */
745 his_quote = send_parms[5];
749 /* handle QBIN - 8-th bit prefixing */
750 /* ignore what he says - I refuse */
754 /* handle CHKT - the clock check type */
755 /* ignore what he says - I do type 1 (for now) */
759 /* handle REPT - the repeat prefix */
760 /* ignore what he says - I refuse (for now) */
764 /* handle CAPAS - the capabilities mask */
765 /* ignore what he says - I only do long packets - I don't do windows */
766 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 */
773 a_b[1] = tochar(length);
776 a_b[++length] = '\0';
777 a_b[length] = tochar(chk1(&a_b[1]));
778 a_b[++length] = his_eol;
779 a_b[++length] = '\0';
783 /* k_recv receives a OS Open image file over kermit line */
784 static int k_recv(void)
787 char k_state, k_state_saved;
794 /* initialize some protocol parameters */
795 his_eol = END_CHAR; /* default end of line character */
798 his_quote = K_ESCAPE;
800 /* initialize the k_recv and k_data state machine */
804 k_state_saved = k_state;
806 n = 0; /* just to get rid of a warning */
809 /* expect this "type" sequence (but don't check):
814 B: break transmission
817 /* enter main loop */
819 /* set the send packet pointer to begining of send packet parms */
820 send_ptr = send_parms;
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.
829 /* OLD CODE, Prior to checking sequence numbers */
830 /* first have all state machines save current states */
831 k_state_saved = k_state;
836 /* wait for the starting character or ^C */
839 case START_CHAR: /* start packet */
841 case ETX_CHAR: /* ^C waiting for packet */
848 /* get length of packet */
851 if ((new_char & 0xE0) == 0)
853 sum += new_char & 0xff;
854 length = untochar(new_char);
855 /* get sequence number */
857 if ((new_char & 0xE0) == 0)
859 sum += new_char & 0xff;
860 n = untochar(new_char);
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.
871 /* same sequence number, restore the previous state */
872 k_state = k_state_saved;
875 /* new sequence number, checkpoint the download */
877 k_state_saved = k_state;
882 /* get packet type */
884 if ((new_char & 0xE0) == 0)
886 sum += new_char & 0xff;
889 /* check for extended length */
891 /* (length byte was 0, decremented twice) */
892 /* get the two length bytes */
894 if ((new_char & 0xE0) == 0)
896 sum += new_char & 0xff;
897 len_hi = untochar(new_char);
899 if ((new_char & 0xE0) == 0)
901 sum += new_char & 0xff;
902 len_lo = untochar(new_char);
903 length = len_hi * 95 + len_lo;
904 /* check header checksum */
906 if ((new_char & 0xE0) == 0)
908 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
910 sum += new_char & 0xff;
911 /* --length; */ /* new length includes only data and block check to come */
913 /* bring in rest of packet */
916 if ((new_char & 0xE0) == 0)
918 sum += new_char & 0xff;
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])
931 /* get and validate checksum character */
933 if ((new_char & 0xE0) == 0)
935 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
939 if (new_char != END_CHAR) {
941 /* restore state machines */
942 k_state = k_state_saved;
944 /* send a negative acknowledge packet in */
946 } else if (k_state == SEND_TYPE) {
947 /* crack the protocol parms, build an appropriate ack packet */
948 handle_send_packet(n);
950 /* send simple acknowledge packet in */
952 /* quit if end of transmission */
953 if (k_state == BREAK_TYPE)
957 return ((ulong) os_data_addr - (ulong) bin_start_address);
960 static int getcxmodem(void) {
965 static ulong load_serial_ymodem(ulong offset)
971 connection_info_t info;
972 char ymodemBuf[1024];
973 ulong store_addr = ~0;
977 info.mode = xyzModem_ymodem;
978 res = xyzModem_stream_open(&info, &err);
982 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
983 store_addr = addr + offset;
986 #ifndef CONFIG_SYS_NO_FLASH
987 if (addr2info(store_addr)) {
990 rc = flash_write((char *) ymodemBuf,
999 memcpy((char *)(store_addr), ymodemBuf,
1005 printf("%s\n", xyzModem_error(err));
1008 xyzModem_stream_close(&err);
1009 xyzModem_stream_terminate(false, &getcxmodem);
1012 flush_cache(offset, size);
1014 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1015 sprintf(buf, "%X", size);
1016 setenv("filesize", buf);
1023 /* -------------------------------------------------------------------- */
1025 #if defined(CONFIG_CMD_LOADS)
1027 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1029 loads, 3, 0, do_load_serial,
1030 "load S-Record file over serial line",
1031 "[ off ] [ baud ]\n"
1032 " - load S-Record file over serial line"
1033 " with offset 'off' and baudrate 'baud'"
1036 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1038 loads, 2, 0, do_load_serial,
1039 "load S-Record file over serial line",
1041 " - load S-Record file over serial line with offset 'off'"
1043 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1046 * SAVES always requires LOADS support, but not vice versa
1050 #if defined(CONFIG_CMD_SAVES)
1051 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1053 saves, 4, 0, do_save_serial,
1054 "save S-Record file over serial line",
1055 "[ off ] [size] [ baud ]\n"
1056 " - save S-Record file over serial line"
1057 " with offset 'off', size 'size' and baudrate 'baud'"
1059 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1061 saves, 3, 0, do_save_serial,
1062 "save S-Record file over serial line",
1064 " - save S-Record file over serial line with offset 'off' and size 'size'"
1066 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1071 #if defined(CONFIG_CMD_LOADB)
1073 loadb, 3, 0, do_load_serial_bin,
1074 "load binary file over serial line (kermit mode)",
1075 "[ off ] [ baud ]\n"
1076 " - load binary file over serial line"
1077 " with offset 'off' and baudrate 'baud'"
1081 loady, 3, 0, do_load_serial_bin,
1082 "load binary file over serial line (ymodem mode)",
1083 "[ off ] [ baud ]\n"
1084 " - load binary file over serial line"
1085 " with offset 'off' and baudrate 'baud'"
1090 /* -------------------------------------------------------------------- */
1092 #if defined(CONFIG_CMD_HWFLOW)
1093 int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1095 extern int hwflow_onoff(int);
1098 if (strcmp(argv[1], "off") == 0)
1101 if (strcmp(argv[1], "on") == 0)
1104 return CMD_RET_USAGE;
1106 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1110 /* -------------------------------------------------------------------- */
1113 hwflow, 2, 0, do_hwflow,
1114 "turn RTS/CTS hardware flow control in serial line on/off",