2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Serial up- and download support
18 DECLARE_GLOBAL_DATA_PTR;
20 #if defined(CONFIG_CMD_LOADB)
21 static ulong load_serial_ymodem(ulong offset);
24 #if defined(CONFIG_CMD_LOADS)
25 static ulong load_serial(long offset);
26 static int read_record(char *buf, ulong len);
27 # if defined(CONFIG_CMD_SAVES)
28 static int save_serial(ulong offset, ulong size);
29 static int write_record(char *buf);
32 static int do_echo = 1;
35 /* -------------------------------------------------------------------- */
37 #if defined(CONFIG_CMD_LOADS)
38 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
46 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
47 int load_baudrate, current_baudrate;
49 load_baudrate = current_baudrate = gd->baudrate;
52 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
58 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
60 offset = simple_strtol(argv[1], NULL, 16);
63 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
65 /* default to current baudrate */
66 if (load_baudrate == 0)
67 load_baudrate = current_baudrate;
69 if (load_baudrate != current_baudrate) {
70 printf("## Switch baudrate to %d bps and press ENTER ...\n",
73 gd->baudrate = load_baudrate;
81 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
83 offset = simple_strtol(argv[1], NULL, 16);
85 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
87 printf("## Ready for S-Record download ...\n");
89 addr = load_serial(offset);
92 * Gather any trailing characters (for instance, the ^D which
93 * is sent by 'cu' after sending a file), and give the
94 * box some time (100 * 1 ms)
96 for (i=0; i<100; ++i) {
104 printf("## S-Record download aborted\n");
107 printf("## Start Addr = 0x%08lX\n", addr);
111 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
112 if (load_baudrate != current_baudrate) {
113 printf("## Switch baudrate to %d bps and press ESC ...\n",
116 gd->baudrate = current_baudrate;
120 if (getc() == 0x1B) /* ESC */
128 static ulong load_serial(long offset)
130 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
131 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
132 int binlen; /* no. of data bytes in S-Rec. */
133 int type; /* return code for record type */
134 ulong addr; /* load address from S-Record */
135 ulong size; /* number of bytes transferred */
137 ulong start_addr = ~0;
141 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
142 type = srec_decode(record, &binlen, &addr, binbuf);
145 return (~0); /* Invalid S-Record */
152 store_addr = addr + offset;
153 #ifndef CONFIG_SYS_NO_FLASH
154 if (addr2info(store_addr)) {
157 rc = flash_write((char *)binbuf,store_addr,binlen);
165 memcpy((char *)(store_addr), binbuf, binlen);
167 if ((store_addr) < start_addr)
168 start_addr = store_addr;
169 if ((store_addr + binlen - 1) > end_addr)
170 end_addr = store_addr + binlen - 1;
176 size = end_addr - start_addr + 1;
178 "## First Load Addr = 0x%08lX\n"
179 "## Last Load Addr = 0x%08lX\n"
180 "## Total Size = 0x%08lX = %ld Bytes\n",
181 start_addr, end_addr, size, size
183 flush_cache(start_addr, size);
184 setenv_hex("filesize", size);
191 if (!do_echo) { /* print a '.' every 100 lines */
192 if ((++line_count % 100) == 0)
197 return (~0); /* Download aborted */
200 static int read_record(char *buf, ulong len)
205 --len; /* always leave room for terminating '\0' byte */
207 for (p=buf; p < buf+len; ++p) {
208 c = getc(); /* read character */
210 putc(c); /* ... and echo it */
218 case 0x03: /* ^C - Control C */
224 /* Check for the console hangup (if any different from serial) */
225 if (gd->jt[XF_getc] != getc) {
232 /* line too long - truncate */
237 #if defined(CONFIG_CMD_SAVES)
239 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
243 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
244 int save_baudrate, current_baudrate;
246 save_baudrate = current_baudrate = gd->baudrate;
250 offset = simple_strtoul(argv[1], NULL, 16);
252 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
254 size = simple_strtoul(argv[2], NULL, 16);
257 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
259 /* default to current baudrate */
260 if (save_baudrate == 0)
261 save_baudrate = current_baudrate;
263 if (save_baudrate != current_baudrate) {
264 printf("## Switch baudrate to %d bps and press ENTER ...\n",
267 gd->baudrate = save_baudrate;
275 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
277 size = simple_strtoul(argv[2], NULL, 16);
279 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
281 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
286 if (save_serial(offset, size)) {
287 printf("## S-Record upload aborted\n");
289 printf("## S-Record upload complete\n");
291 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
292 if (save_baudrate != current_baudrate) {
293 printf("## Switch baudrate to %d bps and press ESC ...\n",
294 (int)current_baudrate);
296 gd->baudrate = current_baudrate;
300 if (getc() == 0x1B) /* ESC */
308 #define SREC3_START "S0030000FC\n"
309 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
310 #define SREC3_END "S70500000000FA\n"
311 #define SREC_BYTES_PER_RECORD 16
313 static int save_serial(ulong address, ulong count)
315 int i, c, reclen, checksum, length;
316 char *hex = "0123456789ABCDEF";
317 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
318 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
323 if(write_record(SREC3_START)) /* write the header */
326 if(count) { /* collect hex data in the buffer */
327 c = *(volatile uchar*)(address + reclen); /* get one byte */
328 checksum += c; /* accumulate checksum */
329 data[2*reclen] = hex[(c>>4)&0x0f];
330 data[2*reclen+1] = hex[c & 0x0f];
331 data[2*reclen+2] = '\0';
335 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
336 /* enough data collected for one record: dump it */
337 if(reclen) { /* build & write a data record: */
338 /* address + data + checksum */
339 length = 4 + reclen + 1;
341 /* accumulate length bytes into checksum */
342 for(i = 0; i < 2; i++)
343 checksum += (length >> (8*i)) & 0xff;
345 /* accumulate address bytes into checksum: */
346 for(i = 0; i < 4; i++)
347 checksum += (address >> (8*i)) & 0xff;
349 /* make proper checksum byte: */
350 checksum = ~checksum & 0xff;
352 /* output one record: */
353 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
354 if(write_record(record))
357 address += reclen; /* increment address */
363 if(write_record(SREC3_END)) /* write the final record */
368 static int write_record(char *buf)
375 /* Check for the console hangup (if any different from serial) */
387 #if defined(CONFIG_CMD_LOADB)
389 * loadb command (load binary) included
393 #define START_CHAR 0x01
394 #define ETX_CHAR 0x03
395 #define END_CHAR 0x0D
397 #define K_ESCAPE 0x23
398 #define SEND_TYPE 'S'
399 #define DATA_TYPE 'D'
401 #define NACK_TYPE 'N'
402 #define BREAK_TYPE 'B'
403 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
404 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
406 static void set_kerm_bin_mode(unsigned long *);
407 static int k_recv(void);
408 static ulong load_serial_bin(ulong offset);
411 static char his_eol; /* character he needs at end of packet */
412 static int his_pad_count; /* number of pad chars he needs */
413 static char his_pad_char; /* pad chars he needs */
414 static char his_quote; /* quote chars he'll use */
416 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
421 int load_baudrate, current_baudrate;
425 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
426 offset = CONFIG_SYS_LOAD_ADDR;
428 /* pre-set offset from $loadaddr */
429 if ((s = getenv("loadaddr")) != NULL) {
430 offset = simple_strtoul(s, NULL, 16);
433 load_baudrate = current_baudrate = gd->baudrate;
436 offset = simple_strtoul(argv[1], NULL, 16);
439 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
441 /* default to current baudrate */
442 if (load_baudrate == 0)
443 load_baudrate = current_baudrate;
446 if (load_baudrate != current_baudrate) {
447 printf("## Switch baudrate to %d bps and press ENTER ...\n",
450 gd->baudrate = load_baudrate;
459 if (strcmp(argv[0],"loady")==0) {
460 printf("## Ready for binary (ymodem) download "
461 "to 0x%08lX at %d bps...\n",
465 addr = load_serial_ymodem(offset);
469 printf("## Ready for binary (kermit) download "
470 "to 0x%08lX at %d bps...\n",
473 addr = load_serial_bin(offset);
477 printf("## Binary (kermit) download aborted\n");
480 printf("## Start Addr = 0x%08lX\n", addr);
484 if (load_baudrate != current_baudrate) {
485 printf("## Switch baudrate to %d bps and press ESC ...\n",
488 gd->baudrate = current_baudrate;
492 if (getc() == 0x1B) /* ESC */
501 static ulong load_serial_bin(ulong offset)
505 set_kerm_bin_mode((ulong *) offset);
509 * Gather any trailing characters (for instance, the ^D which
510 * is sent by 'cu' after sending a file), and give the
511 * box some time (100 * 1 ms)
513 for (i=0; i<100; ++i) {
520 flush_cache(offset, size);
522 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
523 setenv_hex("filesize", size);
528 static void send_pad(void)
530 int count = his_pad_count;
536 /* converts escaped kermit char to binary char */
537 static char ktrans(char in)
539 if ((in & 0x60) == 0x40) {
540 return (char) (in & ~0x40);
541 } else if ((in & 0x7f) == 0x3f) {
542 return (char) (in | 0x40);
547 static int chk1(char *buffer)
554 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
557 static void s1_sendpacket(char *packet)
566 static void send_ack(int n)
573 a_b[4] = tochar(chk1(&a_b[1]));
579 static void send_nack(int n)
586 a_b[4] = tochar(chk1(&a_b[1]));
593 static void (*os_data_init)(void);
594 static void (*os_data_char)(char new_char);
595 static int os_data_state, os_data_state_saved;
596 static char *os_data_addr, *os_data_addr_saved;
597 static char *bin_start_address;
599 static void bin_data_init(void)
602 os_data_addr = bin_start_address;
605 static void os_data_save(void)
607 os_data_state_saved = os_data_state;
608 os_data_addr_saved = os_data_addr;
611 static void os_data_restore(void)
613 os_data_state = os_data_state_saved;
614 os_data_addr = os_data_addr_saved;
617 static void bin_data_char(char new_char)
619 switch (os_data_state) {
621 *os_data_addr++ = new_char;
626 static void set_kerm_bin_mode(unsigned long *addr)
628 bin_start_address = (char *) addr;
629 os_data_init = bin_data_init;
630 os_data_char = bin_data_char;
634 /* k_data_* simply handles the kermit escape translations */
635 static int k_data_escape, k_data_escape_saved;
636 static void k_data_init(void)
642 static void k_data_save(void)
644 k_data_escape_saved = k_data_escape;
648 static void k_data_restore(void)
650 k_data_escape = k_data_escape_saved;
654 static void k_data_char(char new_char)
657 /* last char was escape - translate this character */
658 os_data_char(ktrans(new_char));
661 if (new_char == his_quote) {
662 /* this char is escape - remember */
665 /* otherwise send this char as-is */
666 os_data_char(new_char);
671 #define SEND_DATA_SIZE 20
672 static char send_parms[SEND_DATA_SIZE];
673 static char *send_ptr;
675 /* handle_send_packet interprits the protocol info and builds and
676 sends an appropriate ack for what we can do */
677 static void handle_send_packet(int n)
682 /* initialize some protocol parameters */
683 his_eol = END_CHAR; /* default end of line character */
686 his_quote = K_ESCAPE;
688 /* ignore last character if it filled the buffer */
689 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
691 bytes = send_ptr - send_parms; /* how many bytes we'll process */
695 /* handle MAXL - max length */
696 /* ignore what he says - most I'll take (here) is 94 */
697 a_b[++length] = tochar(94);
700 /* handle TIME - time you should wait for my packets */
701 /* ignore what he says - don't wait for my ack longer than 1 second */
702 a_b[++length] = tochar(1);
705 /* handle NPAD - number of pad chars I need */
706 /* remember what he says - I need none */
707 his_pad_count = untochar(send_parms[2]);
708 a_b[++length] = tochar(0);
711 /* handle PADC - pad chars I need */
712 /* remember what he says - I need none */
713 his_pad_char = ktrans(send_parms[3]);
714 a_b[++length] = 0x40; /* He should ignore this */
717 /* handle EOL - end of line he needs */
718 /* remember what he says - I need CR */
719 his_eol = untochar(send_parms[4]);
720 a_b[++length] = tochar(END_CHAR);
723 /* handle QCTL - quote control char he'll use */
724 /* remember what he says - I'll use '#' */
725 his_quote = send_parms[5];
729 /* handle QBIN - 8-th bit prefixing */
730 /* ignore what he says - I refuse */
734 /* handle CHKT - the clock check type */
735 /* ignore what he says - I do type 1 (for now) */
739 /* handle REPT - the repeat prefix */
740 /* ignore what he says - I refuse (for now) */
744 /* handle CAPAS - the capabilities mask */
745 /* ignore what he says - I only do long packets - I don't do windows */
746 a_b[++length] = tochar(2); /* only long packets */
747 a_b[++length] = tochar(0); /* no windows */
748 a_b[++length] = tochar(94); /* large packet msb */
749 a_b[++length] = tochar(94); /* large packet lsb */
753 a_b[1] = tochar(length);
756 a_b[++length] = '\0';
757 a_b[length] = tochar(chk1(&a_b[1]));
758 a_b[++length] = his_eol;
759 a_b[++length] = '\0';
763 /* k_recv receives a OS Open image file over kermit line */
764 static int k_recv(void)
767 char k_state, k_state_saved;
774 /* initialize some protocol parameters */
775 his_eol = END_CHAR; /* default end of line character */
778 his_quote = K_ESCAPE;
780 /* initialize the k_recv and k_data state machine */
784 k_state_saved = k_state;
786 n = 0; /* just to get rid of a warning */
789 /* expect this "type" sequence (but don't check):
794 B: break transmission
797 /* enter main loop */
799 /* set the send packet pointer to begining of send packet parms */
800 send_ptr = send_parms;
802 /* With each packet, start summing the bytes starting with the length.
803 Save the current sequence number.
804 Note the type of the packet.
805 If a character less than SPACE (0x20) is received - error.
809 /* OLD CODE, Prior to checking sequence numbers */
810 /* first have all state machines save current states */
811 k_state_saved = k_state;
816 /* wait for the starting character or ^C */
819 case START_CHAR: /* start packet */
821 case ETX_CHAR: /* ^C waiting for packet */
828 /* get length of packet */
831 if ((new_char & 0xE0) == 0)
833 sum += new_char & 0xff;
834 length = untochar(new_char);
835 /* get sequence number */
837 if ((new_char & 0xE0) == 0)
839 sum += new_char & 0xff;
840 n = untochar(new_char);
843 /* NEW CODE - check sequence numbers for retried packets */
844 /* Note - this new code assumes that the sequence number is correctly
845 * received. Handling an invalid sequence number adds another layer
846 * of complexity that may not be needed - yet! At this time, I'm hoping
847 * that I don't need to buffer the incoming data packets and can write
848 * the data into memory in real time.
851 /* same sequence number, restore the previous state */
852 k_state = k_state_saved;
855 /* new sequence number, checkpoint the download */
857 k_state_saved = k_state;
862 /* get packet type */
864 if ((new_char & 0xE0) == 0)
866 sum += new_char & 0xff;
869 /* check for extended length */
871 /* (length byte was 0, decremented twice) */
872 /* get the two length bytes */
874 if ((new_char & 0xE0) == 0)
876 sum += new_char & 0xff;
877 len_hi = untochar(new_char);
879 if ((new_char & 0xE0) == 0)
881 sum += new_char & 0xff;
882 len_lo = untochar(new_char);
883 length = len_hi * 95 + len_lo;
884 /* check header checksum */
886 if ((new_char & 0xE0) == 0)
888 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
890 sum += new_char & 0xff;
891 /* --length; */ /* new length includes only data and block check to come */
893 /* bring in rest of packet */
896 if ((new_char & 0xE0) == 0)
898 sum += new_char & 0xff;
900 if (k_state == DATA_TYPE) {
901 /* pass on the data if this is a data packet */
902 k_data_char (new_char);
903 } else if (k_state == SEND_TYPE) {
904 /* save send pack in buffer as is */
905 *send_ptr++ = new_char;
906 /* if too much data, back off the pointer */
907 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
911 /* get and validate checksum character */
913 if ((new_char & 0xE0) == 0)
915 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
919 if (new_char != END_CHAR) {
921 /* restore state machines */
922 k_state = k_state_saved;
924 /* send a negative acknowledge packet in */
926 } else if (k_state == SEND_TYPE) {
927 /* crack the protocol parms, build an appropriate ack packet */
928 handle_send_packet(n);
930 /* send simple acknowledge packet in */
932 /* quit if end of transmission */
933 if (k_state == BREAK_TYPE)
937 return ((ulong) os_data_addr - (ulong) bin_start_address);
940 static int getcxmodem(void) {
945 static ulong load_serial_ymodem(ulong offset)
950 connection_info_t info;
951 char ymodemBuf[1024];
952 ulong store_addr = ~0;
956 info.mode = xyzModem_ymodem;
957 res = xyzModem_stream_open(&info, &err);
961 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
962 store_addr = addr + offset;
965 #ifndef CONFIG_SYS_NO_FLASH
966 if (addr2info(store_addr)) {
969 rc = flash_write((char *) ymodemBuf,
978 memcpy((char *)(store_addr), ymodemBuf,
984 printf("%s\n", xyzModem_error(err));
987 xyzModem_stream_close(&err);
988 xyzModem_stream_terminate(false, &getcxmodem);
991 flush_cache(offset, size);
993 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
994 setenv_hex("filesize", size);
1001 /* -------------------------------------------------------------------- */
1003 #if defined(CONFIG_CMD_LOADS)
1005 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1007 loads, 3, 0, do_load_serial,
1008 "load S-Record file over serial line",
1009 "[ off ] [ baud ]\n"
1010 " - load S-Record file over serial line"
1011 " with offset 'off' and baudrate 'baud'"
1014 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1016 loads, 2, 0, do_load_serial,
1017 "load S-Record file over serial line",
1019 " - load S-Record file over serial line with offset 'off'"
1021 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1024 * SAVES always requires LOADS support, but not vice versa
1028 #if defined(CONFIG_CMD_SAVES)
1029 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1031 saves, 4, 0, do_save_serial,
1032 "save S-Record file over serial line",
1033 "[ off ] [size] [ baud ]\n"
1034 " - save S-Record file over serial line"
1035 " with offset 'off', size 'size' and baudrate 'baud'"
1037 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1039 saves, 3, 0, do_save_serial,
1040 "save S-Record file over serial line",
1042 " - save S-Record file over serial line with offset 'off' and size 'size'"
1044 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1045 #endif /* CONFIG_CMD_SAVES */
1046 #endif /* CONFIG_CMD_LOADS */
1049 #if defined(CONFIG_CMD_LOADB)
1051 loadb, 3, 0, do_load_serial_bin,
1052 "load binary file over serial line (kermit mode)",
1053 "[ off ] [ baud ]\n"
1054 " - load binary file over serial line"
1055 " with offset 'off' and baudrate 'baud'"
1059 loady, 3, 0, do_load_serial_bin,
1060 "load binary file over serial line (ymodem mode)",
1061 "[ off ] [ baud ]\n"
1062 " - load binary file over serial line"
1063 " with offset 'off' and baudrate 'baud'"
1066 #endif /* CONFIG_CMD_LOADB */
1068 /* -------------------------------------------------------------------- */
1070 #if defined(CONFIG_CMD_HWFLOW)
1071 int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1073 extern int hwflow_onoff(int);
1076 if (strcmp(argv[1], "off") == 0)
1079 if (strcmp(argv[1], "on") == 0)
1082 return CMD_RET_USAGE;
1084 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1088 /* -------------------------------------------------------------------- */
1091 hwflow, 2, 0, do_hwflow,
1092 "turn RTS/CTS hardware flow control in serial line on/off",
1096 #endif /* CONFIG_CMD_HWFLOW */