1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
8 * Serial up- and download support
20 DECLARE_GLOBAL_DATA_PTR;
22 #if defined(CONFIG_CMD_LOADB)
23 static ulong load_serial_ymodem(ulong offset, int mode);
26 #if defined(CONFIG_CMD_LOADS)
27 static ulong load_serial(long offset);
28 static int read_record(char *buf, ulong len);
29 # if defined(CONFIG_CMD_SAVES)
30 static int save_serial(ulong offset, ulong size);
31 static int write_record(char *buf);
34 static int do_echo = 1;
37 /* -------------------------------------------------------------------- */
39 #if defined(CONFIG_CMD_LOADS)
40 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
48 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
49 int load_baudrate, current_baudrate;
51 load_baudrate = current_baudrate = gd->baudrate;
54 env_echo = env_get("loads_echo");
55 if (env_echo && *env_echo == '1')
60 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
62 offset = simple_strtol(argv[1], NULL, 16);
65 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
67 /* default to current baudrate */
68 if (load_baudrate == 0)
69 load_baudrate = current_baudrate;
71 if (load_baudrate != current_baudrate) {
72 printf("## Switch baudrate to %d bps and press ENTER ...\n",
75 gd->baudrate = load_baudrate;
83 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
85 offset = simple_strtol(argv[1], NULL, 16);
87 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
89 printf("## Ready for S-Record download ...\n");
91 addr = load_serial(offset);
94 * Gather any trailing characters (for instance, the ^D which
95 * is sent by 'cu' after sending a file), and give the
96 * box some time (100 * 1 ms)
98 for (i=0; i<100; ++i) {
106 printf("## S-Record download aborted\n");
109 printf("## Start Addr = 0x%08lX\n", addr);
113 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
114 if (load_baudrate != current_baudrate) {
115 printf("## Switch baudrate to %d bps and press ESC ...\n",
118 gd->baudrate = current_baudrate;
122 if (getc() == 0x1B) /* ESC */
130 static ulong load_serial(long offset)
132 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
133 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
134 int binlen; /* no. of data bytes in S-Rec. */
135 int type; /* return code for record type */
136 ulong addr; /* load address from S-Record */
137 ulong size; /* number of bytes transferred */
139 ulong start_addr = ~0;
143 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
144 type = srec_decode(record, &binlen, &addr, binbuf);
147 return (~0); /* Invalid S-Record */
154 store_addr = addr + offset;
155 #ifdef CONFIG_MTD_NOR_FLASH
156 if (addr2info(store_addr)) {
159 rc = flash_write((char *)binbuf,store_addr,binlen);
167 memcpy((char *)(store_addr), binbuf, binlen);
169 if ((store_addr) < start_addr)
170 start_addr = store_addr;
171 if ((store_addr + binlen - 1) > end_addr)
172 end_addr = store_addr + binlen - 1;
178 size = end_addr - start_addr + 1;
180 "## First Load Addr = 0x%08lX\n"
181 "## Last Load Addr = 0x%08lX\n"
182 "## Total Size = 0x%08lX = %ld Bytes\n",
183 start_addr, end_addr, size, size
185 flush_cache(start_addr, size);
186 env_set_hex("filesize", size);
193 if (!do_echo) { /* print a '.' every 100 lines */
194 if ((++line_count % 100) == 0)
199 return (~0); /* Download aborted */
202 static int read_record(char *buf, ulong len)
207 --len; /* always leave room for terminating '\0' byte */
209 for (p=buf; p < buf+len; ++p) {
210 c = getc(); /* read character */
212 putc(c); /* ... and echo it */
220 case 0x03: /* ^C - Control C */
226 /* Check for the console hangup (if any different from serial) */
227 if (gd->jt->getc != getc) {
234 /* line too long - truncate */
239 #if defined(CONFIG_CMD_SAVES)
241 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
245 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
246 int save_baudrate, current_baudrate;
248 save_baudrate = current_baudrate = gd->baudrate;
252 offset = simple_strtoul(argv[1], NULL, 16);
254 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
256 size = simple_strtoul(argv[2], NULL, 16);
259 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
261 /* default to current baudrate */
262 if (save_baudrate == 0)
263 save_baudrate = current_baudrate;
265 if (save_baudrate != current_baudrate) {
266 printf("## Switch baudrate to %d bps and press ENTER ...\n",
269 gd->baudrate = save_baudrate;
277 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
279 size = simple_strtoul(argv[2], NULL, 16);
281 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
283 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
288 if (save_serial(offset, size)) {
289 printf("## S-Record upload aborted\n");
291 printf("## S-Record upload complete\n");
293 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
294 if (save_baudrate != current_baudrate) {
295 printf("## Switch baudrate to %d bps and press ESC ...\n",
296 (int)current_baudrate);
298 gd->baudrate = current_baudrate;
302 if (getc() == 0x1B) /* ESC */
310 #define SREC3_START "S0030000FC\n"
311 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
312 #define SREC3_END "S70500000000FA\n"
313 #define SREC_BYTES_PER_RECORD 16
315 static int save_serial(ulong address, ulong count)
317 int i, c, reclen, checksum, length;
318 char *hex = "0123456789ABCDEF";
319 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
320 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
325 if(write_record(SREC3_START)) /* write the header */
328 if(count) { /* collect hex data in the buffer */
329 c = *(volatile uchar*)(address + reclen); /* get one byte */
330 checksum += c; /* accumulate checksum */
331 data[2*reclen] = hex[(c>>4)&0x0f];
332 data[2*reclen+1] = hex[c & 0x0f];
333 data[2*reclen+2] = '\0';
337 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
338 /* enough data collected for one record: dump it */
339 if(reclen) { /* build & write a data record: */
340 /* address + data + checksum */
341 length = 4 + reclen + 1;
343 /* accumulate length bytes into checksum */
344 for(i = 0; i < 2; i++)
345 checksum += (length >> (8*i)) & 0xff;
347 /* accumulate address bytes into checksum: */
348 for(i = 0; i < 4; i++)
349 checksum += (address >> (8*i)) & 0xff;
351 /* make proper checksum byte: */
352 checksum = ~checksum & 0xff;
354 /* output one record: */
355 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
356 if(write_record(record))
359 address += reclen; /* increment address */
365 if(write_record(SREC3_END)) /* write the final record */
370 static int write_record(char *buf)
377 /* Check for the console hangup (if any different from serial) */
389 #if defined(CONFIG_CMD_LOADB)
391 * loadb command (load binary) included
395 #define START_CHAR 0x01
396 #define ETX_CHAR 0x03
397 #define END_CHAR 0x0D
399 #define K_ESCAPE 0x23
400 #define SEND_TYPE 'S'
401 #define DATA_TYPE 'D'
403 #define NACK_TYPE 'N'
404 #define BREAK_TYPE 'B'
405 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
406 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
408 static void set_kerm_bin_mode(unsigned long *);
409 static int k_recv(void);
410 static ulong load_serial_bin(ulong offset);
413 static char his_eol; /* character he needs at end of packet */
414 static int his_pad_count; /* number of pad chars he needs */
415 static char his_pad_char; /* pad chars he needs */
416 static char his_quote; /* quote chars he'll use */
418 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
423 int load_baudrate, current_baudrate;
427 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
428 offset = CONFIG_SYS_LOAD_ADDR;
430 /* pre-set offset from $loadaddr */
431 s = env_get("loadaddr");
433 offset = simple_strtoul(s, NULL, 16);
435 load_baudrate = current_baudrate = gd->baudrate;
438 offset = simple_strtoul(argv[1], NULL, 16);
441 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
443 /* default to current baudrate */
444 if (load_baudrate == 0)
445 load_baudrate = current_baudrate;
448 if (load_baudrate != current_baudrate) {
449 printf("## Switch baudrate to %d bps and press ENTER ...\n",
452 gd->baudrate = load_baudrate;
461 if (strcmp(argv[0],"loady")==0) {
462 printf("## Ready for binary (ymodem) download "
463 "to 0x%08lX at %d bps...\n",
467 addr = load_serial_ymodem(offset, xyzModem_ymodem);
469 } else if (strcmp(argv[0],"loadx")==0) {
470 printf("## Ready for binary (xmodem) download "
471 "to 0x%08lX at %d bps...\n",
475 addr = load_serial_ymodem(offset, xyzModem_xmodem);
479 printf("## Ready for binary (kermit) download "
480 "to 0x%08lX at %d bps...\n",
483 addr = load_serial_bin(offset);
487 printf("## Binary (kermit) download aborted\n");
490 printf("## Start Addr = 0x%08lX\n", addr);
494 if (load_baudrate != current_baudrate) {
495 printf("## Switch baudrate to %d bps and press ESC ...\n",
498 gd->baudrate = current_baudrate;
502 if (getc() == 0x1B) /* ESC */
511 static ulong load_serial_bin(ulong offset)
515 set_kerm_bin_mode((ulong *) offset);
519 * Gather any trailing characters (for instance, the ^D which
520 * is sent by 'cu' after sending a file), and give the
521 * box some time (100 * 1 ms)
523 for (i=0; i<100; ++i) {
530 flush_cache(offset, size);
532 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
533 env_set_hex("filesize", size);
538 static void send_pad(void)
540 int count = his_pad_count;
546 /* converts escaped kermit char to binary char */
547 static char ktrans(char in)
549 if ((in & 0x60) == 0x40) {
550 return (char) (in & ~0x40);
551 } else if ((in & 0x7f) == 0x3f) {
552 return (char) (in | 0x40);
557 static int chk1(char *buffer)
564 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
567 static void s1_sendpacket(char *packet)
576 static void send_ack(int n)
583 a_b[4] = tochar(chk1(&a_b[1]));
589 static void send_nack(int n)
596 a_b[4] = tochar(chk1(&a_b[1]));
603 static void (*os_data_init)(void);
604 static void (*os_data_char)(char new_char);
605 static int os_data_state, os_data_state_saved;
606 static char *os_data_addr, *os_data_addr_saved;
607 static char *bin_start_address;
609 static void bin_data_init(void)
612 os_data_addr = bin_start_address;
615 static void os_data_save(void)
617 os_data_state_saved = os_data_state;
618 os_data_addr_saved = os_data_addr;
621 static void os_data_restore(void)
623 os_data_state = os_data_state_saved;
624 os_data_addr = os_data_addr_saved;
627 static void bin_data_char(char new_char)
629 switch (os_data_state) {
631 *os_data_addr++ = new_char;
636 static void set_kerm_bin_mode(unsigned long *addr)
638 bin_start_address = (char *) addr;
639 os_data_init = bin_data_init;
640 os_data_char = bin_data_char;
644 /* k_data_* simply handles the kermit escape translations */
645 static int k_data_escape, k_data_escape_saved;
646 static void k_data_init(void)
652 static void k_data_save(void)
654 k_data_escape_saved = k_data_escape;
658 static void k_data_restore(void)
660 k_data_escape = k_data_escape_saved;
664 static void k_data_char(char new_char)
667 /* last char was escape - translate this character */
668 os_data_char(ktrans(new_char));
671 if (new_char == his_quote) {
672 /* this char is escape - remember */
675 /* otherwise send this char as-is */
676 os_data_char(new_char);
681 #define SEND_DATA_SIZE 20
682 static char send_parms[SEND_DATA_SIZE];
683 static char *send_ptr;
685 /* handle_send_packet interprits the protocol info and builds and
686 sends an appropriate ack for what we can do */
687 static void handle_send_packet(int n)
692 /* initialize some protocol parameters */
693 his_eol = END_CHAR; /* default end of line character */
696 his_quote = K_ESCAPE;
698 /* ignore last character if it filled the buffer */
699 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
701 bytes = send_ptr - send_parms; /* how many bytes we'll process */
705 /* handle MAXL - max length */
706 /* ignore what he says - most I'll take (here) is 94 */
707 a_b[++length] = tochar(94);
710 /* handle TIME - time you should wait for my packets */
711 /* ignore what he says - don't wait for my ack longer than 1 second */
712 a_b[++length] = tochar(1);
715 /* handle NPAD - number of pad chars I need */
716 /* remember what he says - I need none */
717 his_pad_count = untochar(send_parms[2]);
718 a_b[++length] = tochar(0);
721 /* handle PADC - pad chars I need */
722 /* remember what he says - I need none */
723 his_pad_char = ktrans(send_parms[3]);
724 a_b[++length] = 0x40; /* He should ignore this */
727 /* handle EOL - end of line he needs */
728 /* remember what he says - I need CR */
729 his_eol = untochar(send_parms[4]);
730 a_b[++length] = tochar(END_CHAR);
733 /* handle QCTL - quote control char he'll use */
734 /* remember what he says - I'll use '#' */
735 his_quote = send_parms[5];
739 /* handle QBIN - 8-th bit prefixing */
740 /* ignore what he says - I refuse */
744 /* handle CHKT - the clock check type */
745 /* ignore what he says - I do type 1 (for now) */
749 /* handle REPT - the repeat prefix */
750 /* ignore what he says - I refuse (for now) */
754 /* handle CAPAS - the capabilities mask */
755 /* ignore what he says - I only do long packets - I don't do windows */
756 a_b[++length] = tochar(2); /* only long packets */
757 a_b[++length] = tochar(0); /* no windows */
758 a_b[++length] = tochar(94); /* large packet msb */
759 a_b[++length] = tochar(94); /* large packet lsb */
763 a_b[1] = tochar(length);
766 a_b[++length] = '\0';
767 a_b[length] = tochar(chk1(&a_b[1]));
768 a_b[++length] = his_eol;
769 a_b[++length] = '\0';
773 /* k_recv receives a OS Open image file over kermit line */
774 static int k_recv(void)
777 char k_state, k_state_saved;
784 /* initialize some protocol parameters */
785 his_eol = END_CHAR; /* default end of line character */
788 his_quote = K_ESCAPE;
790 /* initialize the k_recv and k_data state machine */
794 k_state_saved = k_state;
796 n = 0; /* just to get rid of a warning */
799 /* expect this "type" sequence (but don't check):
804 B: break transmission
807 /* enter main loop */
809 /* set the send packet pointer to begining of send packet parms */
810 send_ptr = send_parms;
812 /* With each packet, start summing the bytes starting with the length.
813 Save the current sequence number.
814 Note the type of the packet.
815 If a character less than SPACE (0x20) is received - error.
819 /* OLD CODE, Prior to checking sequence numbers */
820 /* first have all state machines save current states */
821 k_state_saved = k_state;
826 /* wait for the starting character or ^C */
829 case START_CHAR: /* start packet */
831 case ETX_CHAR: /* ^C waiting for packet */
838 /* get length of packet */
841 if ((new_char & 0xE0) == 0)
843 sum += new_char & 0xff;
844 length = untochar(new_char);
845 /* get sequence number */
847 if ((new_char & 0xE0) == 0)
849 sum += new_char & 0xff;
850 n = untochar(new_char);
853 /* NEW CODE - check sequence numbers for retried packets */
854 /* Note - this new code assumes that the sequence number is correctly
855 * received. Handling an invalid sequence number adds another layer
856 * of complexity that may not be needed - yet! At this time, I'm hoping
857 * that I don't need to buffer the incoming data packets and can write
858 * the data into memory in real time.
861 /* same sequence number, restore the previous state */
862 k_state = k_state_saved;
865 /* new sequence number, checkpoint the download */
867 k_state_saved = k_state;
872 /* get packet type */
874 if ((new_char & 0xE0) == 0)
876 sum += new_char & 0xff;
879 /* check for extended length */
881 /* (length byte was 0, decremented twice) */
882 /* get the two length bytes */
884 if ((new_char & 0xE0) == 0)
886 sum += new_char & 0xff;
887 len_hi = untochar(new_char);
889 if ((new_char & 0xE0) == 0)
891 sum += new_char & 0xff;
892 len_lo = untochar(new_char);
893 length = len_hi * 95 + len_lo;
894 /* check header checksum */
896 if ((new_char & 0xE0) == 0)
898 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
900 sum += new_char & 0xff;
901 /* --length; */ /* new length includes only data and block check to come */
903 /* bring in rest of packet */
906 if ((new_char & 0xE0) == 0)
908 sum += new_char & 0xff;
910 if (k_state == DATA_TYPE) {
911 /* pass on the data if this is a data packet */
912 k_data_char (new_char);
913 } else if (k_state == SEND_TYPE) {
914 /* save send pack in buffer as is */
915 *send_ptr++ = new_char;
916 /* if too much data, back off the pointer */
917 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
921 /* get and validate checksum character */
923 if ((new_char & 0xE0) == 0)
925 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
929 if (new_char != END_CHAR) {
931 /* restore state machines */
932 k_state = k_state_saved;
934 /* send a negative acknowledge packet in */
936 } else if (k_state == SEND_TYPE) {
937 /* crack the protocol parms, build an appropriate ack packet */
938 handle_send_packet(n);
940 /* send simple acknowledge packet in */
942 /* quit if end of transmission */
943 if (k_state == BREAK_TYPE)
947 return ((ulong) os_data_addr - (ulong) bin_start_address);
950 static int getcxmodem(void) {
955 static ulong load_serial_ymodem(ulong offset, int mode)
960 connection_info_t info;
961 char ymodemBuf[1024];
962 ulong store_addr = ~0;
967 res = xyzModem_stream_open(&info, &err);
971 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
972 store_addr = addr + offset;
975 #ifdef CONFIG_MTD_NOR_FLASH
976 if (addr2info(store_addr)) {
979 rc = flash_write((char *) ymodemBuf,
988 memcpy((char *)(store_addr), ymodemBuf,
994 printf("%s\n", xyzModem_error(err));
997 xyzModem_stream_close(&err);
998 xyzModem_stream_terminate(false, &getcxmodem);
1001 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1003 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1004 env_set_hex("filesize", size);
1011 /* -------------------------------------------------------------------- */
1013 #if defined(CONFIG_CMD_LOADS)
1015 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1017 loads, 3, 0, do_load_serial,
1018 "load S-Record file over serial line",
1019 "[ off ] [ baud ]\n"
1020 " - load S-Record file over serial line"
1021 " with offset 'off' and baudrate 'baud'"
1024 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1026 loads, 2, 0, do_load_serial,
1027 "load S-Record file over serial line",
1029 " - load S-Record file over serial line with offset 'off'"
1031 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1034 * SAVES always requires LOADS support, but not vice versa
1038 #if defined(CONFIG_CMD_SAVES)
1039 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1041 saves, 4, 0, do_save_serial,
1042 "save S-Record file over serial line",
1043 "[ off ] [size] [ baud ]\n"
1044 " - save S-Record file over serial line"
1045 " with offset 'off', size 'size' and baudrate 'baud'"
1047 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1049 saves, 3, 0, do_save_serial,
1050 "save S-Record file over serial line",
1052 " - save S-Record file over serial line with offset 'off' and size 'size'"
1054 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1055 #endif /* CONFIG_CMD_SAVES */
1056 #endif /* CONFIG_CMD_LOADS */
1059 #if defined(CONFIG_CMD_LOADB)
1061 loadb, 3, 0, do_load_serial_bin,
1062 "load binary file over serial line (kermit mode)",
1063 "[ off ] [ baud ]\n"
1064 " - load binary file over serial line"
1065 " with offset 'off' and baudrate 'baud'"
1069 loadx, 3, 0, do_load_serial_bin,
1070 "load binary file over serial line (xmodem mode)",
1071 "[ off ] [ baud ]\n"
1072 " - load binary file over serial line"
1073 " with offset 'off' and baudrate 'baud'"
1077 loady, 3, 0, do_load_serial_bin,
1078 "load binary file over serial line (ymodem mode)",
1079 "[ off ] [ baud ]\n"
1080 " - load binary file over serial line"
1081 " with offset 'off' and baudrate 'baud'"
1084 #endif /* CONFIG_CMD_LOADB */