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 */
153 ulong start_addr = ~0;
157 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
158 type = srec_decode(record, &binlen, &addr, binbuf);
161 return (~0); /* Invalid S-Record */
168 store_addr = addr + offset;
169 #ifndef CONFIG_SYS_NO_FLASH
170 if (addr2info(store_addr)) {
173 rc = flash_write((char *)binbuf,store_addr,binlen);
181 memcpy((char *)(store_addr), binbuf, binlen);
183 if ((store_addr) < start_addr)
184 start_addr = store_addr;
185 if ((store_addr + binlen - 1) > end_addr)
186 end_addr = store_addr + binlen - 1;
192 size = end_addr - start_addr + 1;
194 "## First Load Addr = 0x%08lX\n"
195 "## Last Load Addr = 0x%08lX\n"
196 "## Total Size = 0x%08lX = %ld Bytes\n",
197 start_addr, end_addr, size, size
199 flush_cache(start_addr, size);
200 setenv_hex("filesize", size);
207 if (!do_echo) { /* print a '.' every 100 lines */
208 if ((++line_count % 100) == 0)
213 return (~0); /* Download aborted */
216 static int read_record(char *buf, ulong len)
221 --len; /* always leave room for terminating '\0' byte */
223 for (p=buf; p < buf+len; ++p) {
224 c = getc(); /* read character */
226 putc(c); /* ... and echo it */
234 case 0x03: /* ^C - Control C */
240 /* Check for the console hangup (if any different from serial) */
241 if (gd->jt[XF_getc] != getc) {
248 /* line too long - truncate */
253 #if defined(CONFIG_CMD_SAVES)
255 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
259 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
260 int save_baudrate, current_baudrate;
262 save_baudrate = current_baudrate = gd->baudrate;
266 offset = simple_strtoul(argv[1], NULL, 16);
268 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
270 size = simple_strtoul(argv[2], NULL, 16);
273 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
275 /* default to current baudrate */
276 if (save_baudrate == 0)
277 save_baudrate = current_baudrate;
279 if (save_baudrate != current_baudrate) {
280 printf("## Switch baudrate to %d bps and press ENTER ...\n",
283 gd->baudrate = save_baudrate;
291 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
293 size = simple_strtoul(argv[2], NULL, 16);
295 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
297 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
302 if (save_serial(offset, size)) {
303 printf("## S-Record upload aborted\n");
305 printf("## S-Record upload complete\n");
307 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
308 if (save_baudrate != current_baudrate) {
309 printf("## Switch baudrate to %d bps and press ESC ...\n",
310 (int)current_baudrate);
312 gd->baudrate = current_baudrate;
316 if (getc() == 0x1B) /* ESC */
324 #define SREC3_START "S0030000FC\n"
325 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
326 #define SREC3_END "S70500000000FA\n"
327 #define SREC_BYTES_PER_RECORD 16
329 static int save_serial(ulong address, ulong count)
331 int i, c, reclen, checksum, length;
332 char *hex = "0123456789ABCDEF";
333 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
334 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
339 if(write_record(SREC3_START)) /* write the header */
342 if(count) { /* collect hex data in the buffer */
343 c = *(volatile uchar*)(address + reclen); /* get one byte */
344 checksum += c; /* accumulate checksum */
345 data[2*reclen] = hex[(c>>4)&0x0f];
346 data[2*reclen+1] = hex[c & 0x0f];
347 data[2*reclen+2] = '\0';
351 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
352 /* enough data collected for one record: dump it */
353 if(reclen) { /* build & write a data record: */
354 /* address + data + checksum */
355 length = 4 + reclen + 1;
357 /* accumulate length bytes into checksum */
358 for(i = 0; i < 2; i++)
359 checksum += (length >> (8*i)) & 0xff;
361 /* accumulate address bytes into checksum: */
362 for(i = 0; i < 4; i++)
363 checksum += (address >> (8*i)) & 0xff;
365 /* make proper checksum byte: */
366 checksum = ~checksum & 0xff;
368 /* output one record: */
369 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
370 if(write_record(record))
373 address += reclen; /* increment address */
379 if(write_record(SREC3_END)) /* write the final record */
384 static int write_record(char *buf)
391 /* Check for the console hangup (if any different from serial) */
403 #if defined(CONFIG_CMD_LOADB)
405 * loadb command (load binary) included
409 #define START_CHAR 0x01
410 #define ETX_CHAR 0x03
411 #define END_CHAR 0x0D
413 #define K_ESCAPE 0x23
414 #define SEND_TYPE 'S'
415 #define DATA_TYPE 'D'
417 #define NACK_TYPE 'N'
418 #define BREAK_TYPE 'B'
419 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
420 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
422 static void set_kerm_bin_mode(unsigned long *);
423 static int k_recv(void);
424 static ulong load_serial_bin(ulong offset);
427 static char his_eol; /* character he needs at end of packet */
428 static int his_pad_count; /* number of pad chars he needs */
429 static char his_pad_char; /* pad chars he needs */
430 static char his_quote; /* quote chars he'll use */
432 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
437 int load_baudrate, current_baudrate;
441 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
442 offset = CONFIG_SYS_LOAD_ADDR;
444 /* pre-set offset from $loadaddr */
445 if ((s = getenv("loadaddr")) != NULL) {
446 offset = simple_strtoul(s, NULL, 16);
449 load_baudrate = current_baudrate = gd->baudrate;
452 offset = simple_strtoul(argv[1], NULL, 16);
455 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
457 /* default to current baudrate */
458 if (load_baudrate == 0)
459 load_baudrate = current_baudrate;
462 if (load_baudrate != current_baudrate) {
463 printf("## Switch baudrate to %d bps and press ENTER ...\n",
466 gd->baudrate = load_baudrate;
475 if (strcmp(argv[0],"loady")==0) {
476 printf("## Ready for binary (ymodem) download "
477 "to 0x%08lX at %d bps...\n",
481 addr = load_serial_ymodem(offset);
485 printf("## Ready for binary (kermit) download "
486 "to 0x%08lX at %d bps...\n",
489 addr = load_serial_bin(offset);
493 printf("## Binary (kermit) download aborted\n");
496 printf("## Start Addr = 0x%08lX\n", addr);
500 if (load_baudrate != current_baudrate) {
501 printf("## Switch baudrate to %d bps and press ESC ...\n",
504 gd->baudrate = current_baudrate;
508 if (getc() == 0x1B) /* ESC */
517 static ulong load_serial_bin(ulong offset)
521 set_kerm_bin_mode((ulong *) offset);
525 * Gather any trailing characters (for instance, the ^D which
526 * is sent by 'cu' after sending a file), and give the
527 * box some time (100 * 1 ms)
529 for (i=0; i<100; ++i) {
536 flush_cache(offset, size);
538 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
539 setenv_hex("filesize", size);
544 static void send_pad(void)
546 int count = his_pad_count;
552 /* converts escaped kermit char to binary char */
553 static char ktrans(char in)
555 if ((in & 0x60) == 0x40) {
556 return (char) (in & ~0x40);
557 } else if ((in & 0x7f) == 0x3f) {
558 return (char) (in | 0x40);
563 static int chk1(char *buffer)
570 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
573 static void s1_sendpacket(char *packet)
582 static void send_ack(int n)
589 a_b[4] = tochar(chk1(&a_b[1]));
595 static void send_nack(int n)
602 a_b[4] = tochar(chk1(&a_b[1]));
609 static void (*os_data_init)(void);
610 static void (*os_data_char)(char new_char);
611 static int os_data_state, os_data_state_saved;
612 static char *os_data_addr, *os_data_addr_saved;
613 static char *bin_start_address;
615 static void bin_data_init(void)
618 os_data_addr = bin_start_address;
621 static void os_data_save(void)
623 os_data_state_saved = os_data_state;
624 os_data_addr_saved = os_data_addr;
627 static void os_data_restore(void)
629 os_data_state = os_data_state_saved;
630 os_data_addr = os_data_addr_saved;
633 static void bin_data_char(char new_char)
635 switch (os_data_state) {
637 *os_data_addr++ = new_char;
642 static void set_kerm_bin_mode(unsigned long *addr)
644 bin_start_address = (char *) addr;
645 os_data_init = bin_data_init;
646 os_data_char = bin_data_char;
650 /* k_data_* simply handles the kermit escape translations */
651 static int k_data_escape, k_data_escape_saved;
652 static void k_data_init(void)
658 static void k_data_save(void)
660 k_data_escape_saved = k_data_escape;
664 static void k_data_restore(void)
666 k_data_escape = k_data_escape_saved;
670 static void k_data_char(char new_char)
673 /* last char was escape - translate this character */
674 os_data_char(ktrans(new_char));
677 if (new_char == his_quote) {
678 /* this char is escape - remember */
681 /* otherwise send this char as-is */
682 os_data_char(new_char);
687 #define SEND_DATA_SIZE 20
688 static char send_parms[SEND_DATA_SIZE];
689 static char *send_ptr;
691 /* handle_send_packet interprits the protocol info and builds and
692 sends an appropriate ack for what we can do */
693 static void handle_send_packet(int n)
698 /* initialize some protocol parameters */
699 his_eol = END_CHAR; /* default end of line character */
702 his_quote = K_ESCAPE;
704 /* ignore last character if it filled the buffer */
705 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
707 bytes = send_ptr - send_parms; /* how many bytes we'll process */
711 /* handle MAXL - max length */
712 /* ignore what he says - most I'll take (here) is 94 */
713 a_b[++length] = tochar(94);
716 /* handle TIME - time you should wait for my packets */
717 /* ignore what he says - don't wait for my ack longer than 1 second */
718 a_b[++length] = tochar(1);
721 /* handle NPAD - number of pad chars I need */
722 /* remember what he says - I need none */
723 his_pad_count = untochar(send_parms[2]);
724 a_b[++length] = tochar(0);
727 /* handle PADC - pad chars I need */
728 /* remember what he says - I need none */
729 his_pad_char = ktrans(send_parms[3]);
730 a_b[++length] = 0x40; /* He should ignore this */
733 /* handle EOL - end of line he needs */
734 /* remember what he says - I need CR */
735 his_eol = untochar(send_parms[4]);
736 a_b[++length] = tochar(END_CHAR);
739 /* handle QCTL - quote control char he'll use */
740 /* remember what he says - I'll use '#' */
741 his_quote = send_parms[5];
745 /* handle QBIN - 8-th bit prefixing */
746 /* ignore what he says - I refuse */
750 /* handle CHKT - the clock check type */
751 /* ignore what he says - I do type 1 (for now) */
755 /* handle REPT - the repeat prefix */
756 /* ignore what he says - I refuse (for now) */
760 /* handle CAPAS - the capabilities mask */
761 /* ignore what he says - I only do long packets - I don't do windows */
762 a_b[++length] = tochar(2); /* only long packets */
763 a_b[++length] = tochar(0); /* no windows */
764 a_b[++length] = tochar(94); /* large packet msb */
765 a_b[++length] = tochar(94); /* large packet lsb */
769 a_b[1] = tochar(length);
772 a_b[++length] = '\0';
773 a_b[length] = tochar(chk1(&a_b[1]));
774 a_b[++length] = his_eol;
775 a_b[++length] = '\0';
779 /* k_recv receives a OS Open image file over kermit line */
780 static int k_recv(void)
783 char k_state, k_state_saved;
790 /* initialize some protocol parameters */
791 his_eol = END_CHAR; /* default end of line character */
794 his_quote = K_ESCAPE;
796 /* initialize the k_recv and k_data state machine */
800 k_state_saved = k_state;
802 n = 0; /* just to get rid of a warning */
805 /* expect this "type" sequence (but don't check):
810 B: break transmission
813 /* enter main loop */
815 /* set the send packet pointer to begining of send packet parms */
816 send_ptr = send_parms;
818 /* With each packet, start summing the bytes starting with the length.
819 Save the current sequence number.
820 Note the type of the packet.
821 If a character less than SPACE (0x20) is received - error.
825 /* OLD CODE, Prior to checking sequence numbers */
826 /* first have all state machines save current states */
827 k_state_saved = k_state;
832 /* wait for the starting character or ^C */
835 case START_CHAR: /* start packet */
837 case ETX_CHAR: /* ^C waiting for packet */
844 /* get length of packet */
847 if ((new_char & 0xE0) == 0)
849 sum += new_char & 0xff;
850 length = untochar(new_char);
851 /* get sequence number */
853 if ((new_char & 0xE0) == 0)
855 sum += new_char & 0xff;
856 n = untochar(new_char);
859 /* NEW CODE - check sequence numbers for retried packets */
860 /* Note - this new code assumes that the sequence number is correctly
861 * received. Handling an invalid sequence number adds another layer
862 * of complexity that may not be needed - yet! At this time, I'm hoping
863 * that I don't need to buffer the incoming data packets and can write
864 * the data into memory in real time.
867 /* same sequence number, restore the previous state */
868 k_state = k_state_saved;
871 /* new sequence number, checkpoint the download */
873 k_state_saved = k_state;
878 /* get packet type */
880 if ((new_char & 0xE0) == 0)
882 sum += new_char & 0xff;
885 /* check for extended length */
887 /* (length byte was 0, decremented twice) */
888 /* get the two length bytes */
890 if ((new_char & 0xE0) == 0)
892 sum += new_char & 0xff;
893 len_hi = untochar(new_char);
895 if ((new_char & 0xE0) == 0)
897 sum += new_char & 0xff;
898 len_lo = untochar(new_char);
899 length = len_hi * 95 + len_lo;
900 /* check header checksum */
902 if ((new_char & 0xE0) == 0)
904 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
906 sum += new_char & 0xff;
907 /* --length; */ /* new length includes only data and block check to come */
909 /* bring in rest of packet */
912 if ((new_char & 0xE0) == 0)
914 sum += new_char & 0xff;
916 if (k_state == DATA_TYPE) {
917 /* pass on the data if this is a data packet */
918 k_data_char (new_char);
919 } else if (k_state == SEND_TYPE) {
920 /* save send pack in buffer as is */
921 *send_ptr++ = new_char;
922 /* if too much data, back off the pointer */
923 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
927 /* get and validate checksum character */
929 if ((new_char & 0xE0) == 0)
931 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
935 if (new_char != END_CHAR) {
937 /* restore state machines */
938 k_state = k_state_saved;
940 /* send a negative acknowledge packet in */
942 } else if (k_state == SEND_TYPE) {
943 /* crack the protocol parms, build an appropriate ack packet */
944 handle_send_packet(n);
946 /* send simple acknowledge packet in */
948 /* quit if end of transmission */
949 if (k_state == BREAK_TYPE)
953 return ((ulong) os_data_addr - (ulong) bin_start_address);
956 static int getcxmodem(void) {
961 static ulong load_serial_ymodem(ulong offset)
966 connection_info_t info;
967 char ymodemBuf[1024];
968 ulong store_addr = ~0;
972 info.mode = xyzModem_ymodem;
973 res = xyzModem_stream_open(&info, &err);
977 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
978 store_addr = addr + offset;
981 #ifndef CONFIG_SYS_NO_FLASH
982 if (addr2info(store_addr)) {
985 rc = flash_write((char *) ymodemBuf,
994 memcpy((char *)(store_addr), ymodemBuf,
1000 printf("%s\n", xyzModem_error(err));
1003 xyzModem_stream_close(&err);
1004 xyzModem_stream_terminate(false, &getcxmodem);
1007 flush_cache(offset, size);
1009 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1010 setenv_hex("filesize", size);
1017 /* -------------------------------------------------------------------- */
1019 #if defined(CONFIG_CMD_LOADS)
1021 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1023 loads, 3, 0, do_load_serial,
1024 "load S-Record file over serial line",
1025 "[ off ] [ baud ]\n"
1026 " - load S-Record file over serial line"
1027 " with offset 'off' and baudrate 'baud'"
1030 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1032 loads, 2, 0, do_load_serial,
1033 "load S-Record file over serial line",
1035 " - load S-Record file over serial line with offset 'off'"
1037 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1040 * SAVES always requires LOADS support, but not vice versa
1044 #if defined(CONFIG_CMD_SAVES)
1045 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1047 saves, 4, 0, do_save_serial,
1048 "save S-Record file over serial line",
1049 "[ off ] [size] [ baud ]\n"
1050 " - save S-Record file over serial line"
1051 " with offset 'off', size 'size' and baudrate 'baud'"
1053 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1055 saves, 3, 0, do_save_serial,
1056 "save S-Record file over serial line",
1058 " - save S-Record file over serial line with offset 'off' and size 'size'"
1060 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1061 #endif /* CONFIG_CMD_SAVES */
1062 #endif /* CONFIG_CMD_LOADS */
1065 #if defined(CONFIG_CMD_LOADB)
1067 loadb, 3, 0, do_load_serial_bin,
1068 "load binary file over serial line (kermit mode)",
1069 "[ off ] [ baud ]\n"
1070 " - load binary file over serial line"
1071 " with offset 'off' and baudrate 'baud'"
1075 loady, 3, 0, do_load_serial_bin,
1076 "load binary file over serial line (ymodem mode)",
1077 "[ off ] [ baud ]\n"
1078 " - load binary file over serial line"
1079 " with offset 'off' and baudrate 'baud'"
1082 #endif /* CONFIG_CMD_LOADB */
1084 /* -------------------------------------------------------------------- */
1086 #if defined(CONFIG_CMD_HWFLOW)
1087 int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1089 extern int hwflow_onoff(int);
1092 if (strcmp(argv[1], "off") == 0)
1095 if (strcmp(argv[1], "on") == 0)
1098 return CMD_RET_USAGE;
1100 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1104 /* -------------------------------------------------------------------- */
1107 hwflow, 2, 0, do_hwflow,
1108 "turn RTS/CTS hardware flow control in serial line on/off",
1112 #endif /* CONFIG_CMD_HWFLOW */