2 * (C) Copyright 2000-2003
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
29 #include <cmd_autoscript.h>
35 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
36 static ulong load_serial (ulong offset);
37 static int read_record (char *buf, ulong len);
38 # if (CONFIG_COMMANDS & CFG_CMD_SAVES)
39 static int save_serial (ulong offset, ulong size);
40 static int write_record (char *buf);
41 # endif /* CFG_CMD_SAVES */
43 static int do_echo = 1;
44 #endif /* CFG_CMD_LOADS */
46 /* -------------------------------------------------------------------- */
48 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
49 int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
56 #ifdef CFG_LOADS_BAUD_CHANGE
57 DECLARE_GLOBAL_DATA_PTR;
58 int load_baudrate, current_baudrate;
60 load_baudrate = current_baudrate = gd->baudrate;
63 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
69 #ifdef CFG_LOADS_BAUD_CHANGE
71 offset = simple_strtoul(argv[1], NULL, 16);
74 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
76 /* default to current baudrate */
77 if (load_baudrate == 0)
78 load_baudrate = current_baudrate;
80 if (load_baudrate != current_baudrate) {
81 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
84 gd->baudrate = load_baudrate;
92 #else /* ! CFG_LOADS_BAUD_CHANGE */
94 offset = simple_strtoul(argv[1], NULL, 16);
96 #endif /* CFG_LOADS_BAUD_CHANGE */
98 printf ("## Ready for S-Record download ...\n");
100 addr = load_serial (offset);
103 * Gather any trailing characters (for instance, the ^D which
104 * is sent by 'cu' after sending a file), and give the
105 * box some time (100 * 1 ms)
107 for (i=0; i<100; ++i) {
109 (void) serial_getc();
115 printf ("## S-Record download aborted\n");
118 printf ("## Start Addr = 0x%08lX\n", addr);
122 #ifdef CFG_LOADS_BAUD_CHANGE
123 if (load_baudrate != current_baudrate) {
124 printf ("## Switch baudrate to %d bps and press ESC ...\n",
127 gd->baudrate = current_baudrate;
131 if (getc() == 0x1B) /* ESC */
140 load_serial (ulong offset)
142 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
143 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
144 int binlen; /* no. of data bytes in S-Rec. */
145 int type; /* return code for record type */
146 ulong addr; /* load address from S-Record */
147 ulong size; /* number of bytes transferred */
150 ulong start_addr = ~0;
154 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
155 type = srec_decode (record, &binlen, &addr, binbuf);
158 return (~0); /* Invalid S-Record */
165 store_addr = addr + offset;
167 if (addr2info(store_addr)) {
170 rc = flash_write((uchar *)binbuf,store_addr,binlen);
178 memcpy ((char *)(store_addr), binbuf, binlen);
180 if ((store_addr) < start_addr)
181 start_addr = store_addr;
182 if ((store_addr + binlen - 1) > end_addr)
183 end_addr = store_addr + binlen - 1;
189 size = end_addr - start_addr + 1;
191 "## First Load Addr = 0x%08lX\n"
192 "## Last Load Addr = 0x%08lX\n"
193 "## Total Size = 0x%08lX = %ld Bytes\n",
194 start_addr, end_addr, size, size
196 flush_cache (addr, size);
197 sprintf(buf, "%lX", size);
198 setenv("filesize", buf);
205 if (!do_echo) { /* print a '.' every 100 lines */
206 if ((++line_count % 100) == 0)
211 return (~0); /* Download aborted */
215 read_record (char *buf, ulong len)
220 --len; /* always leave room for terminating '\0' byte */
222 for (p=buf; p < buf+len; ++p) {
223 c = serial_getc(); /* read character */
225 serial_putc (c); /* ... and echo it */
233 case 0x03: /* ^C - Control C */
239 /* Check for the console hangup (if any different from serial) */
240 #ifdef CONFIG_PPC /* we don't have syscall_tbl anywhere else */
241 if (syscall_tbl[SYSCALL_GETC] != serial_getc) {
249 /* line too long - truncate */
254 #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
256 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
260 #ifdef CFG_LOADS_BAUD_CHANGE
261 DECLARE_GLOBAL_DATA_PTR;
262 int save_baudrate, current_baudrate;
264 save_baudrate = current_baudrate = gd->baudrate;
268 offset = simple_strtoul(argv[1], NULL, 16);
270 #ifdef CFG_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 /* ! CFG_LOADS_BAUD_CHANGE */
295 size = simple_strtoul(argv[2], NULL, 16);
297 #endif /* CFG_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 CFG_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 */
387 write_record (char *buf)
394 /* Check for the console hangup (if any different from serial) */
401 # endif /* CFG_CMD_SAVES */
403 #endif /* CFG_CMD_LOADS */
406 #if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
410 #define START_CHAR 0x01
411 #define ETX_CHAR 0x03
412 #define END_CHAR 0x0D
414 #define K_ESCAPE 0x23
415 #define SEND_TYPE 'S'
416 #define DATA_TYPE 'D'
418 #define NACK_TYPE 'N'
419 #define BREAK_TYPE 'B'
420 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
421 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
423 extern int os_data_count;
424 extern int os_data_header[8];
426 static void set_kerm_bin_mode(unsigned long *);
427 static int k_recv(void);
428 static ulong load_serial_bin (ulong offset);
431 char his_eol; /* character he needs at end of packet */
432 int his_pad_count; /* number of pad chars he needs */
433 char his_pad_char; /* pad chars he needs */
434 char his_quote; /* quote chars he'll use */
436 int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
438 DECLARE_GLOBAL_DATA_PTR;
442 int load_baudrate, current_baudrate;
446 /* pre-set offset from CFG_LOAD_ADDR */
447 offset = CFG_LOAD_ADDR;
449 /* pre-set offset from $loadaddr */
450 if ((s = getenv("loadaddr")) != NULL) {
451 offset = simple_strtoul(s, NULL, 16);
454 load_baudrate = current_baudrate = gd->baudrate;
457 offset = simple_strtoul(argv[1], NULL, 16);
460 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
462 /* default to current baudrate */
463 if (load_baudrate == 0)
464 load_baudrate = current_baudrate;
467 if (load_baudrate != current_baudrate) {
468 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
471 gd->baudrate = load_baudrate;
480 printf ("## Ready for binary (kermit) download "
481 "to 0x%08lX at %d bps...\n",
484 addr = load_serial_bin (offset);
488 printf ("## Binary (kermit) download aborted\n");
491 printf ("## Start Addr = 0x%08lX\n", addr);
495 if (load_baudrate != current_baudrate) {
496 printf ("## Switch baudrate to %d bps and press ESC ...\n",
499 gd->baudrate = current_baudrate;
503 if (getc() == 0x1B) /* ESC */
508 #ifdef CONFIG_AUTOSCRIPT
512 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
513 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
514 rcode = autoscript (load_addr);
522 static ulong load_serial_bin (ulong offset)
527 set_kerm_bin_mode ((ulong *) offset);
531 * Gather any trailing characters (for instance, the ^D which
532 * is sent by 'cu' after sending a file), and give the
533 * box some time (100 * 1 ms)
535 for (i=0; i<100; ++i) {
537 (void) serial_getc();
542 flush_cache (offset, size);
544 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
545 sprintf(buf, "%X", size);
546 setenv("filesize", buf);
553 int count = his_pad_count;
556 serial_putc (his_pad_char);
559 /* converts escaped kermit char to binary char */
560 char ktrans (char in)
562 if ((in & 0x60) == 0x40) {
563 return (char) (in & ~0x40);
564 } else if ((in & 0x7f) == 0x3f) {
565 return (char) (in | 0x40);
570 int chk1 (char *buffer)
577 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
580 void s1_sendpacket (char *packet)
584 serial_putc (*packet++);
589 void send_ack (int n)
596 a_b[4] = tochar (chk1 (&a_b[1]));
602 void send_nack (int n)
609 a_b[4] = tochar (chk1 (&a_b[1]));
616 /* os_data_* takes an OS Open image and puts it into memory, and
617 puts the boot header in an array named os_data_header
619 if image is binary, no header is stored in os_data_header.
621 void (*os_data_init) (void);
622 void (*os_data_char) (char new_char);
623 static int os_data_state, os_data_state_saved;
625 static int os_data_count_saved;
626 static char *os_data_addr, *os_data_addr_saved;
627 static char *bin_start_address;
628 int os_data_header[8];
629 static void bin_data_init (void)
633 os_data_addr = bin_start_address;
635 static void os_data_save (void)
637 os_data_state_saved = os_data_state;
638 os_data_count_saved = os_data_count;
639 os_data_addr_saved = os_data_addr;
641 static void os_data_restore (void)
643 os_data_state = os_data_state_saved;
644 os_data_count = os_data_count_saved;
645 os_data_addr = os_data_addr_saved;
647 static void bin_data_char (char new_char)
649 switch (os_data_state) {
651 *os_data_addr++ = new_char;
656 static void set_kerm_bin_mode (unsigned long *addr)
658 bin_start_address = (char *) addr;
659 os_data_init = bin_data_init;
660 os_data_char = bin_data_char;
664 /* k_data_* simply handles the kermit escape translations */
665 static int k_data_escape, k_data_escape_saved;
666 void k_data_init (void)
671 void k_data_save (void)
673 k_data_escape_saved = k_data_escape;
676 void k_data_restore (void)
678 k_data_escape = k_data_escape_saved;
681 void k_data_char (char new_char)
684 /* last char was escape - translate this character */
685 os_data_char (ktrans (new_char));
688 if (new_char == his_quote) {
689 /* this char is escape - remember */
692 /* otherwise send this char as-is */
693 os_data_char (new_char);
698 #define SEND_DATA_SIZE 20
699 char send_parms[SEND_DATA_SIZE];
702 /* handle_send_packet interprits the protocol info and builds and
703 sends an appropriate ack for what we can do */
704 void handle_send_packet (int n)
709 /* initialize some protocol parameters */
710 his_eol = END_CHAR; /* default end of line character */
713 his_quote = K_ESCAPE;
715 /* ignore last character if it filled the buffer */
716 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
718 bytes = send_ptr - send_parms; /* how many bytes we'll process */
722 /* handle MAXL - max length */
723 /* ignore what he says - most I'll take (here) is 94 */
724 a_b[++length] = tochar (94);
727 /* handle TIME - time you should wait for my packets */
728 /* ignore what he says - don't wait for my ack longer than 1 second */
729 a_b[++length] = tochar (1);
732 /* handle NPAD - number of pad chars I need */
733 /* remember what he says - I need none */
734 his_pad_count = untochar (send_parms[2]);
735 a_b[++length] = tochar (0);
738 /* handle PADC - pad chars I need */
739 /* remember what he says - I need none */
740 his_pad_char = ktrans (send_parms[3]);
741 a_b[++length] = 0x40; /* He should ignore this */
744 /* handle EOL - end of line he needs */
745 /* remember what he says - I need CR */
746 his_eol = untochar (send_parms[4]);
747 a_b[++length] = tochar (END_CHAR);
750 /* handle QCTL - quote control char he'll use */
751 /* remember what he says - I'll use '#' */
752 his_quote = send_parms[5];
756 /* handle QBIN - 8-th bit prefixing */
757 /* ignore what he says - I refuse */
761 /* handle CHKT - the clock check type */
762 /* ignore what he says - I do type 1 (for now) */
766 /* handle REPT - the repeat prefix */
767 /* ignore what he says - I refuse (for now) */
771 /* handle CAPAS - the capabilities mask */
772 /* ignore what he says - I only do long packets - I don't do windows */
773 a_b[++length] = tochar (2); /* only long packets */
774 a_b[++length] = tochar (0); /* no windows */
775 a_b[++length] = tochar (94); /* large packet msb */
776 a_b[++length] = tochar (94); /* large packet lsb */
780 a_b[1] = tochar (length);
783 a_b[++length] = '\0';
784 a_b[length] = tochar (chk1 (&a_b[1]));
785 a_b[++length] = his_eol;
786 a_b[++length] = '\0';
790 /* k_recv receives a OS Open image file over kermit line */
791 static int k_recv (void)
794 char k_state, k_state_saved;
802 /* initialize some protocol parameters */
803 his_eol = END_CHAR; /* default end of line character */
806 his_quote = K_ESCAPE;
808 /* initialize the k_recv and k_data state machine */
812 k_state_saved = k_state;
814 n = 0; /* just to get rid of a warning */
817 /* expect this "type" sequence (but don't check):
822 B: break transmission
825 /* enter main loop */
827 /* set the send packet pointer to begining of send packet parms */
828 send_ptr = send_parms;
830 /* With each packet, start summing the bytes starting with the length.
831 Save the current sequence number.
832 Note the type of the packet.
833 If a character less than SPACE (0x20) is received - error.
837 /* OLD CODE, Prior to checking sequence numbers */
838 /* first have all state machines save current states */
839 k_state_saved = k_state;
844 /* wait for the starting character or ^C */
846 switch (serial_getc ()) {
847 case START_CHAR: /* start packet */
849 case ETX_CHAR: /* ^C waiting for packet */
856 /* get length of packet */
858 new_char = serial_getc ();
859 if ((new_char & 0xE0) == 0)
861 sum += new_char & 0xff;
862 length = untochar (new_char);
863 /* get sequence number */
864 new_char = serial_getc ();
865 if ((new_char & 0xE0) == 0)
867 sum += new_char & 0xff;
868 n = untochar (new_char);
871 /* NEW CODE - check sequence numbers for retried packets */
872 /* Note - this new code assumes that the sequence number is correctly
873 * received. Handling an invalid sequence number adds another layer
874 * of complexity that may not be needed - yet! At this time, I'm hoping
875 * that I don't need to buffer the incoming data packets and can write
876 * the data into memory in real time.
879 /* same sequence number, restore the previous state */
880 k_state = k_state_saved;
883 /* new sequence number, checkpoint the download */
885 k_state_saved = k_state;
890 /* get packet type */
891 new_char = serial_getc ();
892 if ((new_char & 0xE0) == 0)
894 sum += new_char & 0xff;
897 /* check for extended length */
899 /* (length byte was 0, decremented twice) */
900 /* get the two length bytes */
901 new_char = serial_getc ();
902 if ((new_char & 0xE0) == 0)
904 sum += new_char & 0xff;
905 len_hi = untochar (new_char);
906 new_char = serial_getc ();
907 if ((new_char & 0xE0) == 0)
909 sum += new_char & 0xff;
910 len_lo = untochar (new_char);
911 length = len_hi * 95 + len_lo;
912 /* check header checksum */
913 new_char = serial_getc ();
914 if ((new_char & 0xE0) == 0)
916 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
918 sum += new_char & 0xff;
919 /* --length; */ /* new length includes only data and block check to come */
921 /* bring in rest of packet */
923 new_char = serial_getc ();
924 if ((new_char & 0xE0) == 0)
926 sum += new_char & 0xff;
928 if (k_state == DATA_TYPE) {
929 /* pass on the data if this is a data packet */
930 k_data_char (new_char);
931 } else if (k_state == SEND_TYPE) {
932 /* save send pack in buffer as is */
933 *send_ptr++ = new_char;
934 /* if too much data, back off the pointer */
935 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
939 /* get and validate checksum character */
940 new_char = serial_getc ();
941 if ((new_char & 0xE0) == 0)
943 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
946 new_char = serial_getc ();
947 if (new_char != END_CHAR) {
949 /* restore state machines */
950 k_state = k_state_saved;
952 /* send a negative acknowledge packet in */
954 } else if (k_state == SEND_TYPE) {
955 /* crack the protocol parms, build an appropriate ack packet */
956 handle_send_packet (n);
958 /* send simple acknowledge packet in */
960 /* quit if end of transmission */
961 if (k_state == BREAK_TYPE)
966 return ((ulong) os_data_addr - (ulong) bin_start_address);
968 #endif /* CFG_CMD_LOADB */
970 /* -------------------------------------------------------------------- */
972 #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
974 #ifdef CFG_LOADS_BAUD_CHANGE
975 cmd_tbl_t U_BOOT_CMD(LOADS) = MK_CMD_ENTRY(
976 "loads", 3, 0, do_load_serial,
977 "loads - load S-Record file over serial line\n",
979 " - load S-Record file over serial line"
980 " with offset 'off' and baudrate 'baud'\n"
983 #else /* ! CFG_LOADS_BAUD_CHANGE */
984 cmd_tbl_t U_BOOT_CMD(LOADS) = MK_CMD_ENTRY(
985 "loads", 2, 0, do_load_serial,
986 "loads - load S-Record file over serial line\n",
988 " - load S-Record file over serial line with offset 'off'\n"
990 #endif /* CFG_LOADS_BAUD_CHANGE */
993 * SAVES always requires LOADS support, but not vice versa
997 #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
998 #ifdef CFG_LOADS_BAUD_CHANGE
999 cmd_tbl_t U_BOOT_CMD(SAVES) = MK_CMD_ENTRY(
1000 "saves", 4, 0, do_save_serial,
1001 "saves - save S-Record file over serial line\n",
1002 "[ off ] [size] [ baud ]\n"
1003 " - save S-Record file over serial line"
1004 " with offset 'off', size 'size' and baudrate 'baud'\n"
1006 #else /* ! CFG_LOADS_BAUD_CHANGE */
1007 cmd_tbl_t U_BOOT_CMD(SAVES) = MK_CMD_ENTRY(
1008 "saves", 3, 0, do_save_serial,
1009 "saves - save S-Record file over serial line\n",
1011 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1013 #endif /* CFG_LOADS_BAUD_CHANGE */
1014 #endif /* CFG_CMD_SAVES */
1015 #endif /* CFG_CMD_LOADS */
1018 #if (CONFIG_COMMANDS & CFG_CMD_LOADB)
1019 cmd_tbl_t U_BOOT_CMD(LOADB) = MK_CMD_ENTRY(
1020 "loadb", 3, 0, do_load_serial_bin,
1021 "loadb - load binary file over serial line (kermit mode)\n",
1022 "[ off ] [ baud ]\n"
1023 " - load binary file over serial line"
1024 " with offset 'off' and baudrate 'baud'\n"
1027 #endif /* CFG_CMD_LOADB */
1029 /* -------------------------------------------------------------------- */
1031 #if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
1032 int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1034 extern int hwflow_onoff(int);
1037 if (strcmp(argv[1], "off") == 0)
1040 if (strcmp(argv[1], "on") == 0)
1043 printf("Usage: %s\n", cmdtp->usage);
1045 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1049 /* -------------------------------------------------------------------- */
1051 cmd_tbl_t U_BOOT_CMD(HWFLOW) = MK_CMD_ENTRY(
1052 "hwflow [on|off]", 2, 0, do_hwflow,
1053 "hwflow - turn the harwdare flow control on/off\n",
1054 "\n - change RTS/CTS hardware flow control over serial line\n"
1057 #endif /* CFG_CMD_HWFLOW */