1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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(cmd_tbl_t *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 (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
249 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
250 int save_baudrate, current_baudrate;
252 save_baudrate = current_baudrate = gd->baudrate;
256 offset = simple_strtoul(argv[1], NULL, 16);
258 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
260 size = simple_strtoul(argv[2], NULL, 16);
263 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
265 /* default to current baudrate */
266 if (save_baudrate == 0)
267 save_baudrate = current_baudrate;
269 if (save_baudrate != current_baudrate) {
270 printf("## Switch baudrate to %d bps and press ENTER ...\n",
273 gd->baudrate = save_baudrate;
281 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
283 size = simple_strtoul(argv[2], NULL, 16);
285 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
287 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
292 if (save_serial(offset, size)) {
293 printf("## S-Record upload aborted\n");
295 printf("## S-Record upload complete\n");
297 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
298 if (save_baudrate != current_baudrate) {
299 printf("## Switch baudrate to %d bps and press ESC ...\n",
300 (int)current_baudrate);
302 gd->baudrate = current_baudrate;
306 if (getc() == 0x1B) /* ESC */
314 #define SREC3_START "S0030000FC\n"
315 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
316 #define SREC3_END "S70500000000FA\n"
317 #define SREC_BYTES_PER_RECORD 16
319 static int save_serial(ulong address, ulong count)
321 int i, c, reclen, checksum, length;
322 char *hex = "0123456789ABCDEF";
323 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
324 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
329 if(write_record(SREC3_START)) /* write the header */
332 if(count) { /* collect hex data in the buffer */
333 c = *(volatile uchar*)(address + reclen); /* get one byte */
334 checksum += c; /* accumulate checksum */
335 data[2*reclen] = hex[(c>>4)&0x0f];
336 data[2*reclen+1] = hex[c & 0x0f];
337 data[2*reclen+2] = '\0';
341 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
342 /* enough data collected for one record: dump it */
343 if(reclen) { /* build & write a data record: */
344 /* address + data + checksum */
345 length = 4 + reclen + 1;
347 /* accumulate length bytes into checksum */
348 for(i = 0; i < 2; i++)
349 checksum += (length >> (8*i)) & 0xff;
351 /* accumulate address bytes into checksum: */
352 for(i = 0; i < 4; i++)
353 checksum += (address >> (8*i)) & 0xff;
355 /* make proper checksum byte: */
356 checksum = ~checksum & 0xff;
358 /* output one record: */
359 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
360 if(write_record(record))
363 address += reclen; /* increment address */
369 if(write_record(SREC3_END)) /* write the final record */
374 static int write_record(char *buf)
381 /* Check for the console hangup (if any different from serial) */
393 #if defined(CONFIG_CMD_LOADB)
395 * loadb command (load binary) included
399 #define START_CHAR 0x01
400 #define ETX_CHAR 0x03
401 #define END_CHAR 0x0D
403 #define K_ESCAPE 0x23
404 #define SEND_TYPE 'S'
405 #define DATA_TYPE 'D'
407 #define NACK_TYPE 'N'
408 #define BREAK_TYPE 'B'
409 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
410 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
412 static void set_kerm_bin_mode(unsigned long *);
413 static int k_recv(void);
414 static ulong load_serial_bin(ulong offset);
417 static char his_eol; /* character he needs at end of packet */
418 static int his_pad_count; /* number of pad chars he needs */
419 static char his_pad_char; /* pad chars he needs */
420 static char his_quote; /* quote chars he'll use */
422 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
427 int load_baudrate, current_baudrate;
431 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
432 offset = CONFIG_SYS_LOAD_ADDR;
434 /* pre-set offset from $loadaddr */
435 s = env_get("loadaddr");
437 offset = simple_strtoul(s, NULL, 16);
439 load_baudrate = current_baudrate = gd->baudrate;
442 offset = simple_strtoul(argv[1], NULL, 16);
445 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
447 /* default to current baudrate */
448 if (load_baudrate == 0)
449 load_baudrate = current_baudrate;
452 if (load_baudrate != current_baudrate) {
453 printf("## Switch baudrate to %d bps and press ENTER ...\n",
456 gd->baudrate = load_baudrate;
465 if (strcmp(argv[0],"loady")==0) {
466 printf("## Ready for binary (ymodem) download "
467 "to 0x%08lX at %d bps...\n",
471 addr = load_serial_ymodem(offset, xyzModem_ymodem);
473 } else if (strcmp(argv[0],"loadx")==0) {
474 printf("## Ready for binary (xmodem) download "
475 "to 0x%08lX at %d bps...\n",
479 addr = load_serial_ymodem(offset, xyzModem_xmodem);
483 printf("## Ready for binary (kermit) download "
484 "to 0x%08lX at %d bps...\n",
487 addr = load_serial_bin(offset);
491 printf("## Binary (kermit) download aborted\n");
494 printf("## Start Addr = 0x%08lX\n", addr);
495 image_load_addr = addr;
498 if (load_baudrate != current_baudrate) {
499 printf("## Switch baudrate to %d bps and press ESC ...\n",
502 gd->baudrate = current_baudrate;
506 if (getc() == 0x1B) /* ESC */
515 static ulong load_serial_bin(ulong offset)
519 set_kerm_bin_mode((ulong *) offset);
523 * Gather any trailing characters (for instance, the ^D which
524 * is sent by 'cu' after sending a file), and give the
525 * box some time (100 * 1 ms)
527 for (i=0; i<100; ++i) {
534 flush_cache(offset, size);
536 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
537 env_set_hex("filesize", size);
542 static void send_pad(void)
544 int count = his_pad_count;
550 /* converts escaped kermit char to binary char */
551 static char ktrans(char in)
553 if ((in & 0x60) == 0x40) {
554 return (char) (in & ~0x40);
555 } else if ((in & 0x7f) == 0x3f) {
556 return (char) (in | 0x40);
561 static int chk1(char *buffer)
568 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
571 static void s1_sendpacket(char *packet)
580 static void send_ack(int n)
587 a_b[4] = tochar(chk1(&a_b[1]));
593 static void send_nack(int n)
600 a_b[4] = tochar(chk1(&a_b[1]));
607 static void (*os_data_init)(void);
608 static void (*os_data_char)(char new_char);
609 static int os_data_state, os_data_state_saved;
610 static char *os_data_addr, *os_data_addr_saved;
611 static char *bin_start_address;
613 static void bin_data_init(void)
616 os_data_addr = bin_start_address;
619 static void os_data_save(void)
621 os_data_state_saved = os_data_state;
622 os_data_addr_saved = os_data_addr;
625 static void os_data_restore(void)
627 os_data_state = os_data_state_saved;
628 os_data_addr = os_data_addr_saved;
631 static void bin_data_char(char new_char)
633 switch (os_data_state) {
635 *os_data_addr++ = new_char;
640 static void set_kerm_bin_mode(unsigned long *addr)
642 bin_start_address = (char *) addr;
643 os_data_init = bin_data_init;
644 os_data_char = bin_data_char;
648 /* k_data_* simply handles the kermit escape translations */
649 static int k_data_escape, k_data_escape_saved;
650 static void k_data_init(void)
656 static void k_data_save(void)
658 k_data_escape_saved = k_data_escape;
662 static void k_data_restore(void)
664 k_data_escape = k_data_escape_saved;
668 static void k_data_char(char new_char)
671 /* last char was escape - translate this character */
672 os_data_char(ktrans(new_char));
675 if (new_char == his_quote) {
676 /* this char is escape - remember */
679 /* otherwise send this char as-is */
680 os_data_char(new_char);
685 #define SEND_DATA_SIZE 20
686 static char send_parms[SEND_DATA_SIZE];
687 static char *send_ptr;
689 /* handle_send_packet interprits the protocol info and builds and
690 sends an appropriate ack for what we can do */
691 static void handle_send_packet(int n)
696 /* initialize some protocol parameters */
697 his_eol = END_CHAR; /* default end of line character */
700 his_quote = K_ESCAPE;
702 /* ignore last character if it filled the buffer */
703 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
705 bytes = send_ptr - send_parms; /* how many bytes we'll process */
709 /* handle MAXL - max length */
710 /* ignore what he says - most I'll take (here) is 94 */
711 a_b[++length] = tochar(94);
714 /* handle TIME - time you should wait for my packets */
715 /* ignore what he says - don't wait for my ack longer than 1 second */
716 a_b[++length] = tochar(1);
719 /* handle NPAD - number of pad chars I need */
720 /* remember what he says - I need none */
721 his_pad_count = untochar(send_parms[2]);
722 a_b[++length] = tochar(0);
725 /* handle PADC - pad chars I need */
726 /* remember what he says - I need none */
727 his_pad_char = ktrans(send_parms[3]);
728 a_b[++length] = 0x40; /* He should ignore this */
731 /* handle EOL - end of line he needs */
732 /* remember what he says - I need CR */
733 his_eol = untochar(send_parms[4]);
734 a_b[++length] = tochar(END_CHAR);
737 /* handle QCTL - quote control char he'll use */
738 /* remember what he says - I'll use '#' */
739 his_quote = send_parms[5];
743 /* handle QBIN - 8-th bit prefixing */
744 /* ignore what he says - I refuse */
748 /* handle CHKT - the clock check type */
749 /* ignore what he says - I do type 1 (for now) */
753 /* handle REPT - the repeat prefix */
754 /* ignore what he says - I refuse (for now) */
758 /* handle CAPAS - the capabilities mask */
759 /* ignore what he says - I only do long packets - I don't do windows */
760 a_b[++length] = tochar(2); /* only long packets */
761 a_b[++length] = tochar(0); /* no windows */
762 a_b[++length] = tochar(94); /* large packet msb */
763 a_b[++length] = tochar(94); /* large packet lsb */
767 a_b[1] = tochar(length);
770 a_b[++length] = '\0';
771 a_b[length] = tochar(chk1(&a_b[1]));
772 a_b[++length] = his_eol;
773 a_b[++length] = '\0';
777 /* k_recv receives a OS Open image file over kermit line */
778 static int k_recv(void)
781 char k_state, k_state_saved;
788 /* initialize some protocol parameters */
789 his_eol = END_CHAR; /* default end of line character */
792 his_quote = K_ESCAPE;
794 /* initialize the k_recv and k_data state machine */
798 k_state_saved = k_state;
800 n = 0; /* just to get rid of a warning */
803 /* expect this "type" sequence (but don't check):
808 B: break transmission
811 /* enter main loop */
813 /* set the send packet pointer to begining of send packet parms */
814 send_ptr = send_parms;
816 /* With each packet, start summing the bytes starting with the length.
817 Save the current sequence number.
818 Note the type of the packet.
819 If a character less than SPACE (0x20) is received - error.
823 /* OLD CODE, Prior to checking sequence numbers */
824 /* first have all state machines save current states */
825 k_state_saved = k_state;
830 /* wait for the starting character or ^C */
833 case START_CHAR: /* start packet */
835 case ETX_CHAR: /* ^C waiting for packet */
842 /* get length of packet */
845 if ((new_char & 0xE0) == 0)
847 sum += new_char & 0xff;
848 length = untochar(new_char);
849 /* get sequence number */
851 if ((new_char & 0xE0) == 0)
853 sum += new_char & 0xff;
854 n = untochar(new_char);
857 /* NEW CODE - check sequence numbers for retried packets */
858 /* Note - this new code assumes that the sequence number is correctly
859 * received. Handling an invalid sequence number adds another layer
860 * of complexity that may not be needed - yet! At this time, I'm hoping
861 * that I don't need to buffer the incoming data packets and can write
862 * the data into memory in real time.
865 /* same sequence number, restore the previous state */
866 k_state = k_state_saved;
869 /* new sequence number, checkpoint the download */
871 k_state_saved = k_state;
876 /* get packet type */
878 if ((new_char & 0xE0) == 0)
880 sum += new_char & 0xff;
883 /* check for extended length */
885 /* (length byte was 0, decremented twice) */
886 /* get the two length bytes */
888 if ((new_char & 0xE0) == 0)
890 sum += new_char & 0xff;
891 len_hi = untochar(new_char);
893 if ((new_char & 0xE0) == 0)
895 sum += new_char & 0xff;
896 len_lo = untochar(new_char);
897 length = len_hi * 95 + len_lo;
898 /* check header checksum */
900 if ((new_char & 0xE0) == 0)
902 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
904 sum += new_char & 0xff;
905 /* --length; */ /* new length includes only data and block check to come */
907 /* bring in rest of packet */
910 if ((new_char & 0xE0) == 0)
912 sum += new_char & 0xff;
914 if (k_state == DATA_TYPE) {
915 /* pass on the data if this is a data packet */
916 k_data_char (new_char);
917 } else if (k_state == SEND_TYPE) {
918 /* save send pack in buffer as is */
919 *send_ptr++ = new_char;
920 /* if too much data, back off the pointer */
921 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
925 /* get and validate checksum character */
927 if ((new_char & 0xE0) == 0)
929 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
933 if (new_char != END_CHAR) {
935 /* restore state machines */
936 k_state = k_state_saved;
938 /* send a negative acknowledge packet in */
940 } else if (k_state == SEND_TYPE) {
941 /* crack the protocol parms, build an appropriate ack packet */
942 handle_send_packet(n);
944 /* send simple acknowledge packet in */
946 /* quit if end of transmission */
947 if (k_state == BREAK_TYPE)
951 return ((ulong) os_data_addr - (ulong) bin_start_address);
954 static int getcxmodem(void) {
959 static ulong load_serial_ymodem(ulong offset, int mode)
964 connection_info_t info;
965 char ymodemBuf[1024];
966 ulong store_addr = ~0;
971 res = xyzModem_stream_open(&info, &err);
975 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
976 store_addr = addr + offset;
979 #ifdef CONFIG_MTD_NOR_FLASH
980 if (addr2info(store_addr)) {
983 rc = flash_write((char *) ymodemBuf,
992 memcpy((char *)(store_addr), ymodemBuf,
998 printf("%s\n", xyzModem_error(err));
1001 xyzModem_stream_close(&err);
1002 xyzModem_stream_terminate(false, &getcxmodem);
1005 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1007 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1008 env_set_hex("filesize", size);
1015 /* -------------------------------------------------------------------- */
1017 #if defined(CONFIG_CMD_LOADS)
1019 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1021 loads, 3, 0, do_load_serial,
1022 "load S-Record file over serial line",
1023 "[ off ] [ baud ]\n"
1024 " - load S-Record file over serial line"
1025 " with offset 'off' and baudrate 'baud'"
1028 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1030 loads, 2, 0, do_load_serial,
1031 "load S-Record file over serial line",
1033 " - load S-Record file over serial line with offset 'off'"
1035 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1038 * SAVES always requires LOADS support, but not vice versa
1042 #if defined(CONFIG_CMD_SAVES)
1043 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1045 saves, 4, 0, do_save_serial,
1046 "save S-Record file over serial line",
1047 "[ off ] [size] [ baud ]\n"
1048 " - save S-Record file over serial line"
1049 " with offset 'off', size 'size' and baudrate 'baud'"
1051 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1053 saves, 3, 0, do_save_serial,
1054 "save S-Record file over serial line",
1056 " - save S-Record file over serial line with offset 'off' and size 'size'"
1058 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1059 #endif /* CONFIG_CMD_SAVES */
1060 #endif /* CONFIG_CMD_LOADS */
1063 #if defined(CONFIG_CMD_LOADB)
1065 loadb, 3, 0, do_load_serial_bin,
1066 "load binary file over serial line (kermit mode)",
1067 "[ off ] [ baud ]\n"
1068 " - load binary file over serial line"
1069 " with offset 'off' and baudrate 'baud'"
1073 loadx, 3, 0, do_load_serial_bin,
1074 "load binary file over serial line (xmodem mode)",
1075 "[ off ] [ baud ]\n"
1076 " - load binary file over serial line"
1077 " with offset 'off' and baudrate 'baud'"
1081 loady, 3, 0, do_load_serial_bin,
1082 "load binary file over serial line (ymodem mode)",
1083 "[ off ] [ baud ]\n"
1084 " - load binary file over serial line"
1085 " with offset 'off' and baudrate 'baud'"
1088 #endif /* CONFIG_CMD_LOADB */