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
19 DECLARE_GLOBAL_DATA_PTR;
21 #if defined(CONFIG_CMD_LOADB)
22 static ulong load_serial_ymodem(ulong offset, int mode);
25 #if defined(CONFIG_CMD_LOADS)
26 static ulong load_serial(long offset);
27 static int read_record(char *buf, ulong len);
28 # if defined(CONFIG_CMD_SAVES)
29 static int save_serial(ulong offset, ulong size);
30 static int write_record(char *buf);
33 static int do_echo = 1;
36 /* -------------------------------------------------------------------- */
38 #if defined(CONFIG_CMD_LOADS)
39 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
47 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
48 int load_baudrate, current_baudrate;
50 load_baudrate = current_baudrate = gd->baudrate;
53 env_echo = env_get("loads_echo");
54 if (env_echo && *env_echo == '1')
59 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
61 offset = simple_strtol(argv[1], NULL, 16);
64 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
66 /* default to current baudrate */
67 if (load_baudrate == 0)
68 load_baudrate = current_baudrate;
70 if (load_baudrate != current_baudrate) {
71 printf("## Switch baudrate to %d bps and press ENTER ...\n",
74 gd->baudrate = load_baudrate;
82 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
84 offset = simple_strtol(argv[1], NULL, 16);
86 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
88 printf("## Ready for S-Record download ...\n");
90 addr = load_serial(offset);
93 * Gather any trailing characters (for instance, the ^D which
94 * is sent by 'cu' after sending a file), and give the
95 * box some time (100 * 1 ms)
97 for (i=0; i<100; ++i) {
105 printf("## S-Record download aborted\n");
108 printf("## Start Addr = 0x%08lX\n", addr);
112 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
113 if (load_baudrate != current_baudrate) {
114 printf("## Switch baudrate to %d bps and press ESC ...\n",
117 gd->baudrate = current_baudrate;
121 if (getc() == 0x1B) /* ESC */
129 static ulong load_serial(long offset)
131 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
132 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
133 int binlen; /* no. of data bytes in S-Rec. */
134 int type; /* return code for record type */
135 ulong addr; /* load address from S-Record */
136 ulong size; /* number of bytes transferred */
138 ulong start_addr = ~0;
142 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
143 type = srec_decode(record, &binlen, &addr, binbuf);
146 return (~0); /* Invalid S-Record */
153 store_addr = addr + offset;
154 #ifdef CONFIG_MTD_NOR_FLASH
155 if (addr2info(store_addr)) {
158 rc = flash_write((char *)binbuf,store_addr,binlen);
166 memcpy((char *)(store_addr), binbuf, binlen);
168 if ((store_addr) < start_addr)
169 start_addr = store_addr;
170 if ((store_addr + binlen - 1) > end_addr)
171 end_addr = store_addr + binlen - 1;
177 size = end_addr - start_addr + 1;
179 "## First Load Addr = 0x%08lX\n"
180 "## Last Load Addr = 0x%08lX\n"
181 "## Total Size = 0x%08lX = %ld Bytes\n",
182 start_addr, end_addr, size, size
184 flush_cache(start_addr, size);
185 env_set_hex("filesize", size);
192 if (!do_echo) { /* print a '.' every 100 lines */
193 if ((++line_count % 100) == 0)
198 return (~0); /* Download aborted */
201 static int read_record(char *buf, ulong len)
206 --len; /* always leave room for terminating '\0' byte */
208 for (p=buf; p < buf+len; ++p) {
209 c = getc(); /* read character */
211 putc(c); /* ... and echo it */
219 case 0x03: /* ^C - Control C */
225 /* Check for the console hangup (if any different from serial) */
226 if (gd->jt->getc != getc) {
233 /* line too long - truncate */
238 #if defined(CONFIG_CMD_SAVES)
240 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
244 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
245 int save_baudrate, current_baudrate;
247 save_baudrate = current_baudrate = gd->baudrate;
251 offset = simple_strtoul(argv[1], NULL, 16);
253 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
255 size = simple_strtoul(argv[2], NULL, 16);
258 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
260 /* default to current baudrate */
261 if (save_baudrate == 0)
262 save_baudrate = current_baudrate;
264 if (save_baudrate != current_baudrate) {
265 printf("## Switch baudrate to %d bps and press ENTER ...\n",
268 gd->baudrate = save_baudrate;
276 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
278 size = simple_strtoul(argv[2], NULL, 16);
280 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
282 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
287 if (save_serial(offset, size)) {
288 printf("## S-Record upload aborted\n");
290 printf("## S-Record upload complete\n");
292 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
293 if (save_baudrate != current_baudrate) {
294 printf("## Switch baudrate to %d bps and press ESC ...\n",
295 (int)current_baudrate);
297 gd->baudrate = current_baudrate;
301 if (getc() == 0x1B) /* ESC */
309 #define SREC3_START "S0030000FC\n"
310 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
311 #define SREC3_END "S70500000000FA\n"
312 #define SREC_BYTES_PER_RECORD 16
314 static int save_serial(ulong address, ulong count)
316 int i, c, reclen, checksum, length;
317 char *hex = "0123456789ABCDEF";
318 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
319 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
324 if(write_record(SREC3_START)) /* write the header */
327 if(count) { /* collect hex data in the buffer */
328 c = *(volatile uchar*)(address + reclen); /* get one byte */
329 checksum += c; /* accumulate checksum */
330 data[2*reclen] = hex[(c>>4)&0x0f];
331 data[2*reclen+1] = hex[c & 0x0f];
332 data[2*reclen+2] = '\0';
336 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
337 /* enough data collected for one record: dump it */
338 if(reclen) { /* build & write a data record: */
339 /* address + data + checksum */
340 length = 4 + reclen + 1;
342 /* accumulate length bytes into checksum */
343 for(i = 0; i < 2; i++)
344 checksum += (length >> (8*i)) & 0xff;
346 /* accumulate address bytes into checksum: */
347 for(i = 0; i < 4; i++)
348 checksum += (address >> (8*i)) & 0xff;
350 /* make proper checksum byte: */
351 checksum = ~checksum & 0xff;
353 /* output one record: */
354 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
355 if(write_record(record))
358 address += reclen; /* increment address */
364 if(write_record(SREC3_END)) /* write the final record */
369 static int write_record(char *buf)
376 /* Check for the console hangup (if any different from serial) */
388 #if defined(CONFIG_CMD_LOADB)
390 * loadb command (load binary) included
394 #define START_CHAR 0x01
395 #define ETX_CHAR 0x03
396 #define END_CHAR 0x0D
398 #define K_ESCAPE 0x23
399 #define SEND_TYPE 'S'
400 #define DATA_TYPE 'D'
402 #define NACK_TYPE 'N'
403 #define BREAK_TYPE 'B'
404 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
405 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
407 static void set_kerm_bin_mode(unsigned long *);
408 static int k_recv(void);
409 static ulong load_serial_bin(ulong offset);
412 static char his_eol; /* character he needs at end of packet */
413 static int his_pad_count; /* number of pad chars he needs */
414 static char his_pad_char; /* pad chars he needs */
415 static char his_quote; /* quote chars he'll use */
417 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
422 int load_baudrate, current_baudrate;
426 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
427 offset = CONFIG_SYS_LOAD_ADDR;
429 /* pre-set offset from $loadaddr */
430 s = env_get("loadaddr");
432 offset = simple_strtoul(s, NULL, 16);
434 load_baudrate = current_baudrate = gd->baudrate;
437 offset = simple_strtoul(argv[1], NULL, 16);
440 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
442 /* default to current baudrate */
443 if (load_baudrate == 0)
444 load_baudrate = current_baudrate;
447 if (load_baudrate != current_baudrate) {
448 printf("## Switch baudrate to %d bps and press ENTER ...\n",
451 gd->baudrate = load_baudrate;
460 if (strcmp(argv[0],"loady")==0) {
461 printf("## Ready for binary (ymodem) download "
462 "to 0x%08lX at %d bps...\n",
466 addr = load_serial_ymodem(offset, xyzModem_ymodem);
468 } else if (strcmp(argv[0],"loadx")==0) {
469 printf("## Ready for binary (xmodem) download "
470 "to 0x%08lX at %d bps...\n",
474 addr = load_serial_ymodem(offset, xyzModem_xmodem);
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 */
510 static ulong load_serial_bin(ulong offset)
514 set_kerm_bin_mode((ulong *) offset);
518 * Gather any trailing characters (for instance, the ^D which
519 * is sent by 'cu' after sending a file), and give the
520 * box some time (100 * 1 ms)
522 for (i=0; i<100; ++i) {
529 flush_cache(offset, size);
531 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
532 env_set_hex("filesize", size);
537 static void send_pad(void)
539 int count = his_pad_count;
545 /* converts escaped kermit char to binary char */
546 static char ktrans(char in)
548 if ((in & 0x60) == 0x40) {
549 return (char) (in & ~0x40);
550 } else if ((in & 0x7f) == 0x3f) {
551 return (char) (in | 0x40);
556 static int chk1(char *buffer)
563 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
566 static void s1_sendpacket(char *packet)
575 static void send_ack(int n)
582 a_b[4] = tochar(chk1(&a_b[1]));
588 static void send_nack(int n)
595 a_b[4] = tochar(chk1(&a_b[1]));
602 static void (*os_data_init)(void);
603 static void (*os_data_char)(char new_char);
604 static int os_data_state, os_data_state_saved;
605 static char *os_data_addr, *os_data_addr_saved;
606 static char *bin_start_address;
608 static void bin_data_init(void)
611 os_data_addr = bin_start_address;
614 static void os_data_save(void)
616 os_data_state_saved = os_data_state;
617 os_data_addr_saved = os_data_addr;
620 static void os_data_restore(void)
622 os_data_state = os_data_state_saved;
623 os_data_addr = os_data_addr_saved;
626 static void bin_data_char(char new_char)
628 switch (os_data_state) {
630 *os_data_addr++ = new_char;
635 static void set_kerm_bin_mode(unsigned long *addr)
637 bin_start_address = (char *) addr;
638 os_data_init = bin_data_init;
639 os_data_char = bin_data_char;
643 /* k_data_* simply handles the kermit escape translations */
644 static int k_data_escape, k_data_escape_saved;
645 static void k_data_init(void)
651 static void k_data_save(void)
653 k_data_escape_saved = k_data_escape;
657 static void k_data_restore(void)
659 k_data_escape = k_data_escape_saved;
663 static void k_data_char(char new_char)
666 /* last char was escape - translate this character */
667 os_data_char(ktrans(new_char));
670 if (new_char == his_quote) {
671 /* this char is escape - remember */
674 /* otherwise send this char as-is */
675 os_data_char(new_char);
680 #define SEND_DATA_SIZE 20
681 static char send_parms[SEND_DATA_SIZE];
682 static char *send_ptr;
684 /* handle_send_packet interprits the protocol info and builds and
685 sends an appropriate ack for what we can do */
686 static void handle_send_packet(int n)
691 /* initialize some protocol parameters */
692 his_eol = END_CHAR; /* default end of line character */
695 his_quote = K_ESCAPE;
697 /* ignore last character if it filled the buffer */
698 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
700 bytes = send_ptr - send_parms; /* how many bytes we'll process */
704 /* handle MAXL - max length */
705 /* ignore what he says - most I'll take (here) is 94 */
706 a_b[++length] = tochar(94);
709 /* handle TIME - time you should wait for my packets */
710 /* ignore what he says - don't wait for my ack longer than 1 second */
711 a_b[++length] = tochar(1);
714 /* handle NPAD - number of pad chars I need */
715 /* remember what he says - I need none */
716 his_pad_count = untochar(send_parms[2]);
717 a_b[++length] = tochar(0);
720 /* handle PADC - pad chars I need */
721 /* remember what he says - I need none */
722 his_pad_char = ktrans(send_parms[3]);
723 a_b[++length] = 0x40; /* He should ignore this */
726 /* handle EOL - end of line he needs */
727 /* remember what he says - I need CR */
728 his_eol = untochar(send_parms[4]);
729 a_b[++length] = tochar(END_CHAR);
732 /* handle QCTL - quote control char he'll use */
733 /* remember what he says - I'll use '#' */
734 his_quote = send_parms[5];
738 /* handle QBIN - 8-th bit prefixing */
739 /* ignore what he says - I refuse */
743 /* handle CHKT - the clock check type */
744 /* ignore what he says - I do type 1 (for now) */
748 /* handle REPT - the repeat prefix */
749 /* ignore what he says - I refuse (for now) */
753 /* handle CAPAS - the capabilities mask */
754 /* ignore what he says - I only do long packets - I don't do windows */
755 a_b[++length] = tochar(2); /* only long packets */
756 a_b[++length] = tochar(0); /* no windows */
757 a_b[++length] = tochar(94); /* large packet msb */
758 a_b[++length] = tochar(94); /* large packet lsb */
762 a_b[1] = tochar(length);
765 a_b[++length] = '\0';
766 a_b[length] = tochar(chk1(&a_b[1]));
767 a_b[++length] = his_eol;
768 a_b[++length] = '\0';
772 /* k_recv receives a OS Open image file over kermit line */
773 static int k_recv(void)
776 char k_state, k_state_saved;
783 /* initialize some protocol parameters */
784 his_eol = END_CHAR; /* default end of line character */
787 his_quote = K_ESCAPE;
789 /* initialize the k_recv and k_data state machine */
793 k_state_saved = k_state;
795 n = 0; /* just to get rid of a warning */
798 /* expect this "type" sequence (but don't check):
803 B: break transmission
806 /* enter main loop */
808 /* set the send packet pointer to begining of send packet parms */
809 send_ptr = send_parms;
811 /* With each packet, start summing the bytes starting with the length.
812 Save the current sequence number.
813 Note the type of the packet.
814 If a character less than SPACE (0x20) is received - error.
818 /* OLD CODE, Prior to checking sequence numbers */
819 /* first have all state machines save current states */
820 k_state_saved = k_state;
825 /* wait for the starting character or ^C */
828 case START_CHAR: /* start packet */
830 case ETX_CHAR: /* ^C waiting for packet */
837 /* get length of packet */
840 if ((new_char & 0xE0) == 0)
842 sum += new_char & 0xff;
843 length = untochar(new_char);
844 /* get sequence number */
846 if ((new_char & 0xE0) == 0)
848 sum += new_char & 0xff;
849 n = untochar(new_char);
852 /* NEW CODE - check sequence numbers for retried packets */
853 /* Note - this new code assumes that the sequence number is correctly
854 * received. Handling an invalid sequence number adds another layer
855 * of complexity that may not be needed - yet! At this time, I'm hoping
856 * that I don't need to buffer the incoming data packets and can write
857 * the data into memory in real time.
860 /* same sequence number, restore the previous state */
861 k_state = k_state_saved;
864 /* new sequence number, checkpoint the download */
866 k_state_saved = k_state;
871 /* get packet type */
873 if ((new_char & 0xE0) == 0)
875 sum += new_char & 0xff;
878 /* check for extended length */
880 /* (length byte was 0, decremented twice) */
881 /* get the two length bytes */
883 if ((new_char & 0xE0) == 0)
885 sum += new_char & 0xff;
886 len_hi = untochar(new_char);
888 if ((new_char & 0xE0) == 0)
890 sum += new_char & 0xff;
891 len_lo = untochar(new_char);
892 length = len_hi * 95 + len_lo;
893 /* check header checksum */
895 if ((new_char & 0xE0) == 0)
897 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
899 sum += new_char & 0xff;
900 /* --length; */ /* new length includes only data and block check to come */
902 /* bring in rest of packet */
905 if ((new_char & 0xE0) == 0)
907 sum += new_char & 0xff;
909 if (k_state == DATA_TYPE) {
910 /* pass on the data if this is a data packet */
911 k_data_char (new_char);
912 } else if (k_state == SEND_TYPE) {
913 /* save send pack in buffer as is */
914 *send_ptr++ = new_char;
915 /* if too much data, back off the pointer */
916 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
920 /* get and validate checksum character */
922 if ((new_char & 0xE0) == 0)
924 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
928 if (new_char != END_CHAR) {
930 /* restore state machines */
931 k_state = k_state_saved;
933 /* send a negative acknowledge packet in */
935 } else if (k_state == SEND_TYPE) {
936 /* crack the protocol parms, build an appropriate ack packet */
937 handle_send_packet(n);
939 /* send simple acknowledge packet in */
941 /* quit if end of transmission */
942 if (k_state == BREAK_TYPE)
946 return ((ulong) os_data_addr - (ulong) bin_start_address);
949 static int getcxmodem(void) {
954 static ulong load_serial_ymodem(ulong offset, int mode)
959 connection_info_t info;
960 char ymodemBuf[1024];
961 ulong store_addr = ~0;
966 res = xyzModem_stream_open(&info, &err);
970 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
971 store_addr = addr + offset;
974 #ifdef CONFIG_MTD_NOR_FLASH
975 if (addr2info(store_addr)) {
978 rc = flash_write((char *) ymodemBuf,
987 memcpy((char *)(store_addr), ymodemBuf,
993 printf("%s\n", xyzModem_error(err));
996 xyzModem_stream_close(&err);
997 xyzModem_stream_terminate(false, &getcxmodem);
1000 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1002 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1003 env_set_hex("filesize", size);
1010 /* -------------------------------------------------------------------- */
1012 #if defined(CONFIG_CMD_LOADS)
1014 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1016 loads, 3, 0, do_load_serial,
1017 "load S-Record file over serial line",
1018 "[ off ] [ baud ]\n"
1019 " - load S-Record file over serial line"
1020 " with offset 'off' and baudrate 'baud'"
1023 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1025 loads, 2, 0, do_load_serial,
1026 "load S-Record file over serial line",
1028 " - load S-Record file over serial line with offset 'off'"
1030 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1033 * SAVES always requires LOADS support, but not vice versa
1037 #if defined(CONFIG_CMD_SAVES)
1038 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1040 saves, 4, 0, do_save_serial,
1041 "save S-Record file over serial line",
1042 "[ off ] [size] [ baud ]\n"
1043 " - save S-Record file over serial line"
1044 " with offset 'off', size 'size' and baudrate 'baud'"
1046 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1048 saves, 3, 0, do_save_serial,
1049 "save S-Record file over serial line",
1051 " - save S-Record file over serial line with offset 'off' and size 'size'"
1053 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1054 #endif /* CONFIG_CMD_SAVES */
1055 #endif /* CONFIG_CMD_LOADS */
1058 #if defined(CONFIG_CMD_LOADB)
1060 loadb, 3, 0, do_load_serial_bin,
1061 "load binary file over serial line (kermit mode)",
1062 "[ off ] [ baud ]\n"
1063 " - load binary file over serial line"
1064 " with offset 'off' and baudrate 'baud'"
1068 loadx, 3, 0, do_load_serial_bin,
1069 "load binary file over serial line (xmodem mode)",
1070 "[ off ] [ baud ]\n"
1071 " - load binary file over serial line"
1072 " with offset 'off' and baudrate 'baud'"
1076 loady, 3, 0, do_load_serial_bin,
1077 "load binary file over serial line (ymodem mode)",
1078 "[ off ] [ baud ]\n"
1079 " - load binary file over serial line"
1080 " with offset 'off' and baudrate 'baud'"
1083 #endif /* CONFIG_CMD_LOADB */