1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
8 * Serial up- and download support
22 #include <asm/cache.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 #if defined(CONFIG_CMD_LOADB)
27 static ulong load_serial_ymodem(ulong offset, int mode);
30 #if defined(CONFIG_CMD_LOADS)
31 static ulong load_serial(long offset);
32 static int read_record(char *buf, ulong len);
33 # if defined(CONFIG_CMD_SAVES)
34 static int save_serial(ulong offset, ulong size);
35 static int write_record(char *buf);
38 static int do_echo = 1;
41 /* -------------------------------------------------------------------- */
43 #if defined(CONFIG_CMD_LOADS)
44 static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
52 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
53 int load_baudrate, current_baudrate;
55 load_baudrate = current_baudrate = gd->baudrate;
58 env_echo = env_get("loads_echo");
59 if (env_echo && *env_echo == '1')
64 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
66 offset = simple_strtol(argv[1], NULL, 16);
69 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
71 /* default to current baudrate */
72 if (load_baudrate == 0)
73 load_baudrate = current_baudrate;
75 if (load_baudrate != current_baudrate) {
76 printf("## Switch baudrate to %d bps and press ENTER ...\n",
79 gd->baudrate = load_baudrate;
87 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
89 offset = simple_strtol(argv[1], NULL, 16);
91 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
93 printf("## Ready for S-Record download ...\n");
95 addr = load_serial(offset);
98 * Gather any trailing characters (for instance, the ^D which
99 * is sent by 'cu' after sending a file), and give the
100 * box some time (100 * 1 ms)
102 for (i=0; i<100; ++i) {
110 printf("## S-Record download aborted\n");
113 printf("## Start Addr = 0x%08lX\n", addr);
114 image_load_addr = addr;
117 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
118 if (load_baudrate != current_baudrate) {
119 printf("## Switch baudrate to %d bps and press ESC ...\n",
122 gd->baudrate = current_baudrate;
126 if (getc() == 0x1B) /* ESC */
134 static ulong load_serial(long offset)
136 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
137 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
138 int binlen; /* no. of data bytes in S-Rec. */
139 int type; /* return code for record type */
140 ulong addr; /* load address from S-Record */
141 ulong size; /* number of bytes transferred */
143 ulong start_addr = ~0;
147 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
148 type = srec_decode(record, &binlen, &addr, binbuf);
151 return (~0); /* Invalid S-Record */
158 store_addr = addr + offset;
159 #ifdef CONFIG_MTD_NOR_FLASH
160 if (addr2info(store_addr)) {
163 rc = flash_write((char *)binbuf,store_addr,binlen);
171 memcpy((char *)(store_addr), binbuf, binlen);
173 if ((store_addr) < start_addr)
174 start_addr = store_addr;
175 if ((store_addr + binlen - 1) > end_addr)
176 end_addr = store_addr + binlen - 1;
182 size = end_addr - start_addr + 1;
184 "## First Load Addr = 0x%08lX\n"
185 "## Last Load Addr = 0x%08lX\n"
186 "## Total Size = 0x%08lX = %ld Bytes\n",
187 start_addr, end_addr, size, size
189 flush_cache(start_addr, size);
190 env_set_hex("filesize", size);
197 if (!do_echo) { /* print a '.' every 100 lines */
198 if ((++line_count % 100) == 0)
203 return (~0); /* Download aborted */
206 static int read_record(char *buf, ulong len)
211 --len; /* always leave room for terminating '\0' byte */
213 for (p=buf; p < buf+len; ++p) {
214 c = getc(); /* read character */
216 putc(c); /* ... and echo it */
224 case 0x03: /* ^C - Control C */
230 /* Check for the console hangup (if any different from serial) */
231 if (gd->jt->getc != getc) {
238 /* line too long - truncate */
243 #if defined(CONFIG_CMD_SAVES)
245 int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
250 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
251 int save_baudrate, current_baudrate;
253 save_baudrate = current_baudrate = gd->baudrate;
257 offset = simple_strtoul(argv[1], NULL, 16);
259 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
261 size = simple_strtoul(argv[2], NULL, 16);
264 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
266 /* default to current baudrate */
267 if (save_baudrate == 0)
268 save_baudrate = current_baudrate;
270 if (save_baudrate != current_baudrate) {
271 printf("## Switch baudrate to %d bps and press ENTER ...\n",
274 gd->baudrate = save_baudrate;
282 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
284 size = simple_strtoul(argv[2], NULL, 16);
286 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
288 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
293 if (save_serial(offset, size)) {
294 printf("## S-Record upload aborted\n");
296 printf("## S-Record upload complete\n");
298 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
299 if (save_baudrate != current_baudrate) {
300 printf("## Switch baudrate to %d bps and press ESC ...\n",
301 (int)current_baudrate);
303 gd->baudrate = current_baudrate;
307 if (getc() == 0x1B) /* ESC */
315 #define SREC3_START "S0030000FC\n"
316 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
317 #define SREC3_END "S70500000000FA\n"
318 #define SREC_BYTES_PER_RECORD 16
320 static int save_serial(ulong address, ulong count)
322 int i, c, reclen, checksum, length;
323 char *hex = "0123456789ABCDEF";
324 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
325 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
330 if(write_record(SREC3_START)) /* write the header */
333 if(count) { /* collect hex data in the buffer */
334 c = *(volatile uchar*)(address + reclen); /* get one byte */
335 checksum += c; /* accumulate checksum */
336 data[2*reclen] = hex[(c>>4)&0x0f];
337 data[2*reclen+1] = hex[c & 0x0f];
338 data[2*reclen+2] = '\0';
342 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
343 /* enough data collected for one record: dump it */
344 if(reclen) { /* build & write a data record: */
345 /* address + data + checksum */
346 length = 4 + reclen + 1;
348 /* accumulate length bytes into checksum */
349 for(i = 0; i < 2; i++)
350 checksum += (length >> (8*i)) & 0xff;
352 /* accumulate address bytes into checksum: */
353 for(i = 0; i < 4; i++)
354 checksum += (address >> (8*i)) & 0xff;
356 /* make proper checksum byte: */
357 checksum = ~checksum & 0xff;
359 /* output one record: */
360 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
361 if(write_record(record))
364 address += reclen; /* increment address */
370 if(write_record(SREC3_END)) /* write the final record */
375 static int write_record(char *buf)
382 /* Check for the console hangup (if any different from serial) */
394 #if defined(CONFIG_CMD_LOADB)
396 * loadb command (load binary) included
400 #define START_CHAR 0x01
401 #define ETX_CHAR 0x03
402 #define END_CHAR 0x0D
404 #define K_ESCAPE 0x23
405 #define SEND_TYPE 'S'
406 #define DATA_TYPE 'D'
408 #define NACK_TYPE 'N'
409 #define BREAK_TYPE 'B'
410 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
411 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
413 static void set_kerm_bin_mode(unsigned long *);
414 static int k_recv(void);
415 static ulong load_serial_bin(ulong offset);
418 static char his_eol; /* character he needs at end of packet */
419 static int his_pad_count; /* number of pad chars he needs */
420 static char his_pad_char; /* pad chars he needs */
421 static char his_quote; /* quote chars he'll use */
423 static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
428 int load_baudrate, current_baudrate;
432 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
433 offset = CONFIG_SYS_LOAD_ADDR;
435 /* pre-set offset from $loadaddr */
436 s = env_get("loadaddr");
438 offset = simple_strtoul(s, NULL, 16);
440 load_baudrate = current_baudrate = gd->baudrate;
443 offset = simple_strtoul(argv[1], NULL, 16);
446 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
448 /* default to current baudrate */
449 if (load_baudrate == 0)
450 load_baudrate = current_baudrate;
453 if (load_baudrate != current_baudrate) {
454 printf("## Switch baudrate to %d bps and press ENTER ...\n",
457 gd->baudrate = load_baudrate;
466 if (strcmp(argv[0],"loady")==0) {
467 printf("## Ready for binary (ymodem) download "
468 "to 0x%08lX at %d bps...\n",
472 addr = load_serial_ymodem(offset, xyzModem_ymodem);
474 } else if (strcmp(argv[0],"loadx")==0) {
475 printf("## Ready for binary (xmodem) download "
476 "to 0x%08lX at %d bps...\n",
480 addr = load_serial_ymodem(offset, xyzModem_xmodem);
484 printf("## Ready for binary (kermit) download "
485 "to 0x%08lX at %d bps...\n",
488 addr = load_serial_bin(offset);
492 printf("## Binary (kermit) download aborted\n");
495 printf("## Start Addr = 0x%08lX\n", addr);
496 image_load_addr = addr;
499 if (load_baudrate != current_baudrate) {
500 printf("## Switch baudrate to %d bps and press ESC ...\n",
503 gd->baudrate = current_baudrate;
507 if (getc() == 0x1B) /* ESC */
516 static ulong load_serial_bin(ulong offset)
520 set_kerm_bin_mode((ulong *) offset);
524 * Gather any trailing characters (for instance, the ^D which
525 * is sent by 'cu' after sending a file), and give the
526 * box some time (100 * 1 ms)
528 for (i=0; i<100; ++i) {
535 flush_cache(offset, size);
537 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
538 env_set_hex("filesize", size);
543 static void send_pad(void)
545 int count = his_pad_count;
551 /* converts escaped kermit char to binary char */
552 static char ktrans(char in)
554 if ((in & 0x60) == 0x40) {
555 return (char) (in & ~0x40);
556 } else if ((in & 0x7f) == 0x3f) {
557 return (char) (in | 0x40);
562 static int chk1(char *buffer)
569 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
572 static void s1_sendpacket(char *packet)
581 static void send_ack(int n)
588 a_b[4] = tochar(chk1(&a_b[1]));
594 static void send_nack(int n)
601 a_b[4] = tochar(chk1(&a_b[1]));
608 static void (*os_data_init)(void);
609 static void (*os_data_char)(char new_char);
610 static int os_data_state, os_data_state_saved;
611 static char *os_data_addr, *os_data_addr_saved;
612 static char *bin_start_address;
614 static void bin_data_init(void)
617 os_data_addr = bin_start_address;
620 static void os_data_save(void)
622 os_data_state_saved = os_data_state;
623 os_data_addr_saved = os_data_addr;
626 static void os_data_restore(void)
628 os_data_state = os_data_state_saved;
629 os_data_addr = os_data_addr_saved;
632 static void bin_data_char(char new_char)
634 switch (os_data_state) {
636 *os_data_addr++ = new_char;
641 static void set_kerm_bin_mode(unsigned long *addr)
643 bin_start_address = (char *) addr;
644 os_data_init = bin_data_init;
645 os_data_char = bin_data_char;
649 /* k_data_* simply handles the kermit escape translations */
650 static int k_data_escape, k_data_escape_saved;
651 static void k_data_init(void)
657 static void k_data_save(void)
659 k_data_escape_saved = k_data_escape;
663 static void k_data_restore(void)
665 k_data_escape = k_data_escape_saved;
669 static void k_data_char(char new_char)
672 /* last char was escape - translate this character */
673 os_data_char(ktrans(new_char));
676 if (new_char == his_quote) {
677 /* this char is escape - remember */
680 /* otherwise send this char as-is */
681 os_data_char(new_char);
686 #define SEND_DATA_SIZE 20
687 static char send_parms[SEND_DATA_SIZE];
688 static char *send_ptr;
690 /* handle_send_packet interprits the protocol info and builds and
691 sends an appropriate ack for what we can do */
692 static void handle_send_packet(int n)
697 /* initialize some protocol parameters */
698 his_eol = END_CHAR; /* default end of line character */
701 his_quote = K_ESCAPE;
703 /* ignore last character if it filled the buffer */
704 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
706 bytes = send_ptr - send_parms; /* how many bytes we'll process */
710 /* handle MAXL - max length */
711 /* ignore what he says - most I'll take (here) is 94 */
712 a_b[++length] = tochar(94);
715 /* handle TIME - time you should wait for my packets */
716 /* ignore what he says - don't wait for my ack longer than 1 second */
717 a_b[++length] = tochar(1);
720 /* handle NPAD - number of pad chars I need */
721 /* remember what he says - I need none */
722 his_pad_count = untochar(send_parms[2]);
723 a_b[++length] = tochar(0);
726 /* handle PADC - pad chars I need */
727 /* remember what he says - I need none */
728 his_pad_char = ktrans(send_parms[3]);
729 a_b[++length] = 0x40; /* He should ignore this */
732 /* handle EOL - end of line he needs */
733 /* remember what he says - I need CR */
734 his_eol = untochar(send_parms[4]);
735 a_b[++length] = tochar(END_CHAR);
738 /* handle QCTL - quote control char he'll use */
739 /* remember what he says - I'll use '#' */
740 his_quote = send_parms[5];
744 /* handle QBIN - 8-th bit prefixing */
745 /* ignore what he says - I refuse */
749 /* handle CHKT - the clock check type */
750 /* ignore what he says - I do type 1 (for now) */
754 /* handle REPT - the repeat prefix */
755 /* ignore what he says - I refuse (for now) */
759 /* handle CAPAS - the capabilities mask */
760 /* ignore what he says - I only do long packets - I don't do windows */
761 a_b[++length] = tochar(2); /* only long packets */
762 a_b[++length] = tochar(0); /* no windows */
763 a_b[++length] = tochar(94); /* large packet msb */
764 a_b[++length] = tochar(94); /* large packet lsb */
768 a_b[1] = tochar(length);
771 a_b[++length] = '\0';
772 a_b[length] = tochar(chk1(&a_b[1]));
773 a_b[++length] = his_eol;
774 a_b[++length] = '\0';
778 /* k_recv receives a OS Open image file over kermit line */
779 static int k_recv(void)
782 char k_state, k_state_saved;
789 /* initialize some protocol parameters */
790 his_eol = END_CHAR; /* default end of line character */
793 his_quote = K_ESCAPE;
795 /* initialize the k_recv and k_data state machine */
799 k_state_saved = k_state;
801 n = 0; /* just to get rid of a warning */
804 /* expect this "type" sequence (but don't check):
809 B: break transmission
812 /* enter main loop */
814 /* set the send packet pointer to begining of send packet parms */
815 send_ptr = send_parms;
817 /* With each packet, start summing the bytes starting with the length.
818 Save the current sequence number.
819 Note the type of the packet.
820 If a character less than SPACE (0x20) is received - error.
824 /* OLD CODE, Prior to checking sequence numbers */
825 /* first have all state machines save current states */
826 k_state_saved = k_state;
831 /* wait for the starting character or ^C */
834 case START_CHAR: /* start packet */
836 case ETX_CHAR: /* ^C waiting for packet */
843 /* get length of packet */
846 if ((new_char & 0xE0) == 0)
848 sum += new_char & 0xff;
849 length = untochar(new_char);
850 /* get sequence number */
852 if ((new_char & 0xE0) == 0)
854 sum += new_char & 0xff;
855 n = untochar(new_char);
858 /* NEW CODE - check sequence numbers for retried packets */
859 /* Note - this new code assumes that the sequence number is correctly
860 * received. Handling an invalid sequence number adds another layer
861 * of complexity that may not be needed - yet! At this time, I'm hoping
862 * that I don't need to buffer the incoming data packets and can write
863 * the data into memory in real time.
866 /* same sequence number, restore the previous state */
867 k_state = k_state_saved;
870 /* new sequence number, checkpoint the download */
872 k_state_saved = k_state;
877 /* get packet type */
879 if ((new_char & 0xE0) == 0)
881 sum += new_char & 0xff;
884 /* check for extended length */
886 /* (length byte was 0, decremented twice) */
887 /* get the two length bytes */
889 if ((new_char & 0xE0) == 0)
891 sum += new_char & 0xff;
892 len_hi = untochar(new_char);
894 if ((new_char & 0xE0) == 0)
896 sum += new_char & 0xff;
897 len_lo = untochar(new_char);
898 length = len_hi * 95 + len_lo;
899 /* check header checksum */
901 if ((new_char & 0xE0) == 0)
903 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
905 sum += new_char & 0xff;
906 /* --length; */ /* new length includes only data and block check to come */
908 /* bring in rest of packet */
911 if ((new_char & 0xE0) == 0)
913 sum += new_char & 0xff;
915 if (k_state == DATA_TYPE) {
916 /* pass on the data if this is a data packet */
917 k_data_char (new_char);
918 } else if (k_state == SEND_TYPE) {
919 /* save send pack in buffer as is */
920 *send_ptr++ = new_char;
921 /* if too much data, back off the pointer */
922 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
926 /* get and validate checksum character */
928 if ((new_char & 0xE0) == 0)
930 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
934 if (new_char != END_CHAR) {
936 /* restore state machines */
937 k_state = k_state_saved;
939 /* send a negative acknowledge packet in */
941 } else if (k_state == SEND_TYPE) {
942 /* crack the protocol parms, build an appropriate ack packet */
943 handle_send_packet(n);
945 /* send simple acknowledge packet in */
947 /* quit if end of transmission */
948 if (k_state == BREAK_TYPE)
952 return ((ulong) os_data_addr - (ulong) bin_start_address);
955 static int getcxmodem(void) {
960 static ulong load_serial_ymodem(ulong offset, int mode)
965 connection_info_t info;
966 char ymodemBuf[1024];
967 ulong store_addr = ~0;
972 res = xyzModem_stream_open(&info, &err);
976 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
977 store_addr = addr + offset;
980 #ifdef CONFIG_MTD_NOR_FLASH
981 if (addr2info(store_addr)) {
984 rc = flash_write((char *) ymodemBuf,
993 memcpy((char *)(store_addr), ymodemBuf,
999 printf("%s\n", xyzModem_error(err));
1002 xyzModem_stream_close(&err);
1003 xyzModem_stream_terminate(false, &getcxmodem);
1006 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1008 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1009 env_set_hex("filesize", size);
1016 /* -------------------------------------------------------------------- */
1018 #if defined(CONFIG_CMD_LOADS)
1020 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1022 loads, 3, 0, do_load_serial,
1023 "load S-Record file over serial line",
1024 "[ off ] [ baud ]\n"
1025 " - load S-Record file over serial line"
1026 " with offset 'off' and baudrate 'baud'"
1029 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1031 loads, 2, 0, do_load_serial,
1032 "load S-Record file over serial line",
1034 " - load S-Record file over serial line with offset 'off'"
1036 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1039 * SAVES always requires LOADS support, but not vice versa
1043 #if defined(CONFIG_CMD_SAVES)
1044 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1046 saves, 4, 0, do_save_serial,
1047 "save S-Record file over serial line",
1048 "[ off ] [size] [ baud ]\n"
1049 " - save S-Record file over serial line"
1050 " with offset 'off', size 'size' and baudrate 'baud'"
1052 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1054 saves, 3, 0, do_save_serial,
1055 "save S-Record file over serial line",
1057 " - save S-Record file over serial line with offset 'off' and size 'size'"
1059 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1060 #endif /* CONFIG_CMD_SAVES */
1061 #endif /* CONFIG_CMD_LOADS */
1064 #if defined(CONFIG_CMD_LOADB)
1066 loadb, 3, 0, do_load_serial_bin,
1067 "load binary file over serial line (kermit mode)",
1068 "[ off ] [ baud ]\n"
1069 " - load binary file over serial line"
1070 " with offset 'off' and baudrate 'baud'"
1074 loadx, 3, 0, do_load_serial_bin,
1075 "load binary file over serial line (xmodem mode)",
1076 "[ off ] [ baud ]\n"
1077 " - load binary file over serial line"
1078 " with offset 'off' and baudrate 'baud'"
1082 loady, 3, 0, do_load_serial_bin,
1083 "load binary file over serial line (ymodem mode)",
1084 "[ off ] [ baud ]\n"
1085 " - load binary file over serial line"
1086 " with offset 'off' and baudrate 'baud'"
1089 #endif /* CONFIG_CMD_LOADB */