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 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
35 static ulong load_serial (ulong offset);
36 static int read_record (char *buf, ulong len);
37 # if (CONFIG_COMMANDS & CFG_CMD_SAVES)
38 static int save_serial (ulong offset, ulong size);
39 static int write_record (char *buf);
40 # endif /* CFG_CMD_SAVES */
42 static int do_echo = 1;
43 #endif /* CFG_CMD_LOADS */
45 /* -------------------------------------------------------------------- */
47 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
48 int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
55 #ifdef CFG_LOADS_BAUD_CHANGE
56 DECLARE_GLOBAL_DATA_PTR;
57 int load_baudrate, current_baudrate;
59 load_baudrate = current_baudrate = gd->baudrate;
62 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
68 #ifdef CFG_LOADS_BAUD_CHANGE
70 offset = simple_strtoul(argv[1], NULL, 16);
73 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
75 /* default to current baudrate */
76 if (load_baudrate == 0)
77 load_baudrate = current_baudrate;
79 if (load_baudrate != current_baudrate) {
80 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
83 gd->baudrate = load_baudrate;
91 #else /* ! CFG_LOADS_BAUD_CHANGE */
93 offset = simple_strtoul(argv[1], NULL, 16);
95 #endif /* CFG_LOADS_BAUD_CHANGE */
97 printf ("## Ready for S-Record download ...\n");
99 addr = load_serial (offset);
102 * Gather any trailing characters (for instance, the ^D which
103 * is sent by 'cu' after sending a file), and give the
104 * box some time (100 * 1 ms)
106 for (i=0; i<100; ++i) {
114 printf ("## S-Record download aborted\n");
117 printf ("## Start Addr = 0x%08lX\n", addr);
121 #ifdef CFG_LOADS_BAUD_CHANGE
122 if (load_baudrate != current_baudrate) {
123 printf ("## Switch baudrate to %d bps and press ESC ...\n",
126 gd->baudrate = current_baudrate;
130 if (getc() == 0x1B) /* ESC */
139 load_serial (ulong offset)
141 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
142 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
143 int binlen; /* no. of data bytes in S-Rec. */
144 int type; /* return code for record type */
145 ulong addr; /* load address from S-Record */
146 ulong size; /* number of bytes transferred */
149 ulong start_addr = ~0;
153 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
154 type = srec_decode (record, &binlen, &addr, binbuf);
157 return (~0); /* Invalid S-Record */
164 store_addr = addr + offset;
166 if (addr2info(store_addr)) {
169 rc = flash_write((char *)binbuf,store_addr,binlen);
177 memcpy ((char *)(store_addr), binbuf, binlen);
179 if ((store_addr) < start_addr)
180 start_addr = store_addr;
181 if ((store_addr + binlen - 1) > end_addr)
182 end_addr = store_addr + binlen - 1;
188 size = end_addr - start_addr + 1;
190 "## First Load Addr = 0x%08lX\n"
191 "## Last Load Addr = 0x%08lX\n"
192 "## Total Size = 0x%08lX = %ld Bytes\n",
193 start_addr, end_addr, size, size
195 flush_cache (start_addr, size);
196 sprintf(buf, "%lX", size);
197 setenv("filesize", buf);
204 if (!do_echo) { /* print a '.' every 100 lines */
205 if ((++line_count % 100) == 0)
210 return (~0); /* Download aborted */
214 read_record (char *buf, ulong len)
216 DECLARE_GLOBAL_DATA_PTR;
220 --len; /* always leave room for terminating '\0' byte */
222 for (p=buf; p < buf+len; ++p) {
223 c = getc(); /* read character */
225 putc (c); /* ... and echo it */
233 case 0x03: /* ^C - Control C */
239 /* Check for the console hangup (if any different from serial) */
240 if (gd->jt[XF_getc] != getc) {
247 /* line too long - truncate */
252 #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
254 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
258 #ifdef CFG_LOADS_BAUD_CHANGE
259 DECLARE_GLOBAL_DATA_PTR;
260 int save_baudrate, current_baudrate;
262 save_baudrate = current_baudrate = gd->baudrate;
266 offset = simple_strtoul(argv[1], NULL, 16);
268 #ifdef CFG_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 /* ! CFG_LOADS_BAUD_CHANGE */
293 size = simple_strtoul(argv[2], NULL, 16);
295 #endif /* CFG_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 CFG_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 */
385 write_record (char *buf)
392 /* Check for the console hangup (if any different from serial) */
399 # endif /* CFG_CMD_SAVES */
401 #endif /* CFG_CMD_LOADS */
404 #if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
408 #define START_CHAR 0x01
409 #define ETX_CHAR 0x03
410 #define END_CHAR 0x0D
412 #define K_ESCAPE 0x23
413 #define SEND_TYPE 'S'
414 #define DATA_TYPE 'D'
416 #define NACK_TYPE 'N'
417 #define BREAK_TYPE 'B'
418 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
419 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
421 extern int os_data_count;
422 extern int os_data_header[8];
424 static void set_kerm_bin_mode(unsigned long *);
425 static int k_recv(void);
426 static ulong load_serial_bin (ulong offset);
429 char his_eol; /* character he needs at end of packet */
430 int his_pad_count; /* number of pad chars he needs */
431 char his_pad_char; /* pad chars he needs */
432 char his_quote; /* quote chars he'll use */
434 int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
436 DECLARE_GLOBAL_DATA_PTR;
440 int load_baudrate, current_baudrate;
444 /* pre-set offset from CFG_LOAD_ADDR */
445 offset = CFG_LOAD_ADDR;
447 /* pre-set offset from $loadaddr */
448 if ((s = getenv("loadaddr")) != NULL) {
449 offset = simple_strtoul(s, NULL, 16);
452 load_baudrate = current_baudrate = gd->baudrate;
455 offset = simple_strtoul(argv[1], NULL, 16);
458 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
460 /* default to current baudrate */
461 if (load_baudrate == 0)
462 load_baudrate = current_baudrate;
465 if (load_baudrate != current_baudrate) {
466 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
469 gd->baudrate = load_baudrate;
478 printf ("## Ready for binary (kermit) download "
479 "to 0x%08lX at %d bps...\n",
482 addr = load_serial_bin (offset);
486 printf ("## Binary (kermit) download aborted\n");
489 printf ("## Start Addr = 0x%08lX\n", addr);
493 if (load_baudrate != current_baudrate) {
494 printf ("## Switch baudrate to %d bps and press ESC ...\n",
497 gd->baudrate = current_baudrate;
501 if (getc() == 0x1B) /* ESC */
506 #ifdef CONFIG_AUTOSCRIPT
510 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
511 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
512 rcode = autoscript (load_addr);
520 static ulong load_serial_bin (ulong offset)
525 set_kerm_bin_mode ((ulong *) offset);
529 * Gather any trailing characters (for instance, the ^D which
530 * is sent by 'cu' after sending a file), and give the
531 * box some time (100 * 1 ms)
533 for (i=0; i<100; ++i) {
540 flush_cache (offset, size);
542 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
543 sprintf(buf, "%X", size);
544 setenv("filesize", buf);
551 int count = his_pad_count;
557 /* converts escaped kermit char to binary char */
558 char ktrans (char in)
560 if ((in & 0x60) == 0x40) {
561 return (char) (in & ~0x40);
562 } else if ((in & 0x7f) == 0x3f) {
563 return (char) (in | 0x40);
568 int chk1 (char *buffer)
575 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
578 void s1_sendpacket (char *packet)
587 void send_ack (int n)
594 a_b[4] = tochar (chk1 (&a_b[1]));
600 void send_nack (int n)
607 a_b[4] = tochar (chk1 (&a_b[1]));
614 /* os_data_* takes an OS Open image and puts it into memory, and
615 puts the boot header in an array named os_data_header
617 if image is binary, no header is stored in os_data_header.
619 void (*os_data_init) (void);
620 void (*os_data_char) (char new_char);
621 static int os_data_state, os_data_state_saved;
623 static int os_data_count_saved;
624 static char *os_data_addr, *os_data_addr_saved;
625 static char *bin_start_address;
626 int os_data_header[8];
627 static void bin_data_init (void)
631 os_data_addr = bin_start_address;
633 static void os_data_save (void)
635 os_data_state_saved = os_data_state;
636 os_data_count_saved = os_data_count;
637 os_data_addr_saved = os_data_addr;
639 static void os_data_restore (void)
641 os_data_state = os_data_state_saved;
642 os_data_count = os_data_count_saved;
643 os_data_addr = os_data_addr_saved;
645 static void bin_data_char (char new_char)
647 switch (os_data_state) {
649 *os_data_addr++ = new_char;
654 static void set_kerm_bin_mode (unsigned long *addr)
656 bin_start_address = (char *) addr;
657 os_data_init = bin_data_init;
658 os_data_char = bin_data_char;
662 /* k_data_* simply handles the kermit escape translations */
663 static int k_data_escape, k_data_escape_saved;
664 void k_data_init (void)
669 void k_data_save (void)
671 k_data_escape_saved = k_data_escape;
674 void k_data_restore (void)
676 k_data_escape = k_data_escape_saved;
679 void k_data_char (char new_char)
682 /* last char was escape - translate this character */
683 os_data_char (ktrans (new_char));
686 if (new_char == his_quote) {
687 /* this char is escape - remember */
690 /* otherwise send this char as-is */
691 os_data_char (new_char);
696 #define SEND_DATA_SIZE 20
697 char send_parms[SEND_DATA_SIZE];
700 /* handle_send_packet interprits the protocol info and builds and
701 sends an appropriate ack for what we can do */
702 void handle_send_packet (int n)
707 /* initialize some protocol parameters */
708 his_eol = END_CHAR; /* default end of line character */
711 his_quote = K_ESCAPE;
713 /* ignore last character if it filled the buffer */
714 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
716 bytes = send_ptr - send_parms; /* how many bytes we'll process */
720 /* handle MAXL - max length */
721 /* ignore what he says - most I'll take (here) is 94 */
722 a_b[++length] = tochar (94);
725 /* handle TIME - time you should wait for my packets */
726 /* ignore what he says - don't wait for my ack longer than 1 second */
727 a_b[++length] = tochar (1);
730 /* handle NPAD - number of pad chars I need */
731 /* remember what he says - I need none */
732 his_pad_count = untochar (send_parms[2]);
733 a_b[++length] = tochar (0);
736 /* handle PADC - pad chars I need */
737 /* remember what he says - I need none */
738 his_pad_char = ktrans (send_parms[3]);
739 a_b[++length] = 0x40; /* He should ignore this */
742 /* handle EOL - end of line he needs */
743 /* remember what he says - I need CR */
744 his_eol = untochar (send_parms[4]);
745 a_b[++length] = tochar (END_CHAR);
748 /* handle QCTL - quote control char he'll use */
749 /* remember what he says - I'll use '#' */
750 his_quote = send_parms[5];
754 /* handle QBIN - 8-th bit prefixing */
755 /* ignore what he says - I refuse */
759 /* handle CHKT - the clock check type */
760 /* ignore what he says - I do type 1 (for now) */
764 /* handle REPT - the repeat prefix */
765 /* ignore what he says - I refuse (for now) */
769 /* handle CAPAS - the capabilities mask */
770 /* ignore what he says - I only do long packets - I don't do windows */
771 a_b[++length] = tochar (2); /* only long packets */
772 a_b[++length] = tochar (0); /* no windows */
773 a_b[++length] = tochar (94); /* large packet msb */
774 a_b[++length] = tochar (94); /* large packet lsb */
778 a_b[1] = tochar (length);
781 a_b[++length] = '\0';
782 a_b[length] = tochar (chk1 (&a_b[1]));
783 a_b[++length] = his_eol;
784 a_b[++length] = '\0';
788 /* k_recv receives a OS Open image file over kermit line */
789 static int k_recv (void)
792 char k_state, k_state_saved;
800 /* initialize some protocol parameters */
801 his_eol = END_CHAR; /* default end of line character */
804 his_quote = K_ESCAPE;
806 /* initialize the k_recv and k_data state machine */
810 k_state_saved = k_state;
812 n = 0; /* just to get rid of a warning */
815 /* expect this "type" sequence (but don't check):
820 B: break transmission
823 /* enter main loop */
825 /* set the send packet pointer to begining of send packet parms */
826 send_ptr = send_parms;
828 /* With each packet, start summing the bytes starting with the length.
829 Save the current sequence number.
830 Note the type of the packet.
831 If a character less than SPACE (0x20) is received - error.
835 /* OLD CODE, Prior to checking sequence numbers */
836 /* first have all state machines save current states */
837 k_state_saved = k_state;
842 /* wait for the starting character or ^C */
845 case START_CHAR: /* start packet */
847 case ETX_CHAR: /* ^C waiting for packet */
854 /* get length of packet */
857 if ((new_char & 0xE0) == 0)
859 sum += new_char & 0xff;
860 length = untochar (new_char);
861 /* get sequence number */
863 if ((new_char & 0xE0) == 0)
865 sum += new_char & 0xff;
866 n = untochar (new_char);
869 /* NEW CODE - check sequence numbers for retried packets */
870 /* Note - this new code assumes that the sequence number is correctly
871 * received. Handling an invalid sequence number adds another layer
872 * of complexity that may not be needed - yet! At this time, I'm hoping
873 * that I don't need to buffer the incoming data packets and can write
874 * the data into memory in real time.
877 /* same sequence number, restore the previous state */
878 k_state = k_state_saved;
881 /* new sequence number, checkpoint the download */
883 k_state_saved = k_state;
888 /* get packet type */
890 if ((new_char & 0xE0) == 0)
892 sum += new_char & 0xff;
895 /* check for extended length */
897 /* (length byte was 0, decremented twice) */
898 /* get the two length bytes */
900 if ((new_char & 0xE0) == 0)
902 sum += new_char & 0xff;
903 len_hi = untochar (new_char);
905 if ((new_char & 0xE0) == 0)
907 sum += new_char & 0xff;
908 len_lo = untochar (new_char);
909 length = len_hi * 95 + len_lo;
910 /* check header checksum */
912 if ((new_char & 0xE0) == 0)
914 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
916 sum += new_char & 0xff;
917 /* --length; */ /* new length includes only data and block check to come */
919 /* bring in rest of packet */
922 if ((new_char & 0xE0) == 0)
924 sum += new_char & 0xff;
926 if (k_state == DATA_TYPE) {
927 /* pass on the data if this is a data packet */
928 k_data_char (new_char);
929 } else if (k_state == SEND_TYPE) {
930 /* save send pack in buffer as is */
931 *send_ptr++ = new_char;
932 /* if too much data, back off the pointer */
933 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
937 /* get and validate checksum character */
939 if ((new_char & 0xE0) == 0)
941 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
945 if (new_char != END_CHAR) {
947 /* restore state machines */
948 k_state = k_state_saved;
950 /* send a negative acknowledge packet in */
952 } else if (k_state == SEND_TYPE) {
953 /* crack the protocol parms, build an appropriate ack packet */
954 handle_send_packet (n);
956 /* send simple acknowledge packet in */
958 /* quit if end of transmission */
959 if (k_state == BREAK_TYPE)
964 return ((ulong) os_data_addr - (ulong) bin_start_address);
966 #endif /* CFG_CMD_LOADB */
968 /* -------------------------------------------------------------------- */
970 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
972 #ifdef CFG_LOADS_BAUD_CHANGE
974 loads, 3, 0, do_load_serial,
975 "loads - load S-Record file over serial line\n",
977 " - load S-Record file over serial line"
978 " with offset 'off' and baudrate 'baud'\n"
981 #else /* ! CFG_LOADS_BAUD_CHANGE */
983 loads, 2, 0, do_load_serial,
984 "loads - load S-Record file over serial line\n",
986 " - load S-Record file over serial line with offset 'off'\n"
988 #endif /* CFG_LOADS_BAUD_CHANGE */
991 * SAVES always requires LOADS support, but not vice versa
995 #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
996 #ifdef CFG_LOADS_BAUD_CHANGE
998 saves, 4, 0, do_save_serial,
999 "saves - save S-Record file over serial line\n",
1000 "[ off ] [size] [ baud ]\n"
1001 " - save S-Record file over serial line"
1002 " with offset 'off', size 'size' and baudrate 'baud'\n"
1004 #else /* ! CFG_LOADS_BAUD_CHANGE */
1006 saves, 3, 0, do_save_serial,
1007 "saves - save S-Record file over serial line\n",
1009 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1011 #endif /* CFG_LOADS_BAUD_CHANGE */
1012 #endif /* CFG_CMD_SAVES */
1013 #endif /* CFG_CMD_LOADS */
1016 #if (CONFIG_COMMANDS & CFG_CMD_LOADB)
1018 loadb, 3, 0, do_load_serial_bin,
1019 "loadb - load binary file over serial line (kermit mode)\n",
1020 "[ off ] [ baud ]\n"
1021 " - load binary file over serial line"
1022 " with offset 'off' and baudrate 'baud'\n"
1025 #endif /* CFG_CMD_LOADB */
1027 /* -------------------------------------------------------------------- */
1029 #if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
1030 int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1032 extern int hwflow_onoff(int);
1035 if (strcmp(argv[1], "off") == 0)
1038 if (strcmp(argv[1], "on") == 0)
1041 printf("Usage: %s\n", cmdtp->usage);
1043 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1047 /* -------------------------------------------------------------------- */
1050 hwflow, 2, 0, do_hwflow,
1051 "hwflow - turn the harwdare flow control on/off\n",
1052 "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
1055 #endif /* CFG_CMD_HWFLOW */