1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
8 * Serial up- and download support
22 DECLARE_GLOBAL_DATA_PTR;
24 #if defined(CONFIG_CMD_LOADB)
25 static ulong load_serial_ymodem(ulong offset, int mode);
28 #if defined(CONFIG_CMD_LOADS)
29 static ulong load_serial(long offset);
30 static int read_record(char *buf, ulong len);
31 # if defined(CONFIG_CMD_SAVES)
32 static int save_serial(ulong offset, ulong size);
33 static int write_record(char *buf);
36 static int do_echo = 1;
39 /* -------------------------------------------------------------------- */
41 #if defined(CONFIG_CMD_LOADS)
42 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
50 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
51 int load_baudrate, current_baudrate;
53 load_baudrate = current_baudrate = gd->baudrate;
56 env_echo = env_get("loads_echo");
57 if (env_echo && *env_echo == '1')
62 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
64 offset = simple_strtol(argv[1], NULL, 16);
67 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
69 /* default to current baudrate */
70 if (load_baudrate == 0)
71 load_baudrate = current_baudrate;
73 if (load_baudrate != current_baudrate) {
74 printf("## Switch baudrate to %d bps and press ENTER ...\n",
77 gd->baudrate = load_baudrate;
85 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
87 offset = simple_strtol(argv[1], NULL, 16);
89 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
91 printf("## Ready for S-Record download ...\n");
93 addr = load_serial(offset);
96 * Gather any trailing characters (for instance, the ^D which
97 * is sent by 'cu' after sending a file), and give the
98 * box some time (100 * 1 ms)
100 for (i=0; i<100; ++i) {
108 printf("## S-Record download aborted\n");
111 printf("## Start Addr = 0x%08lX\n", addr);
112 image_load_addr = addr;
115 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
116 if (load_baudrate != current_baudrate) {
117 printf("## Switch baudrate to %d bps and press ESC ...\n",
120 gd->baudrate = current_baudrate;
124 if (getc() == 0x1B) /* ESC */
132 static ulong load_serial(long offset)
134 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
135 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
136 int binlen; /* no. of data bytes in S-Rec. */
137 int type; /* return code for record type */
138 ulong addr; /* load address from S-Record */
139 ulong size; /* number of bytes transferred */
141 ulong start_addr = ~0;
145 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
146 type = srec_decode(record, &binlen, &addr, binbuf);
149 return (~0); /* Invalid S-Record */
156 store_addr = addr + offset;
157 #ifdef CONFIG_MTD_NOR_FLASH
158 if (addr2info(store_addr)) {
161 rc = flash_write((char *)binbuf,store_addr,binlen);
169 memcpy((char *)(store_addr), binbuf, binlen);
171 if ((store_addr) < start_addr)
172 start_addr = store_addr;
173 if ((store_addr + binlen - 1) > end_addr)
174 end_addr = store_addr + binlen - 1;
180 size = end_addr - start_addr + 1;
182 "## First Load Addr = 0x%08lX\n"
183 "## Last Load Addr = 0x%08lX\n"
184 "## Total Size = 0x%08lX = %ld Bytes\n",
185 start_addr, end_addr, size, size
187 flush_cache(start_addr, size);
188 env_set_hex("filesize", size);
195 if (!do_echo) { /* print a '.' every 100 lines */
196 if ((++line_count % 100) == 0)
201 return (~0); /* Download aborted */
204 static int read_record(char *buf, ulong len)
209 --len; /* always leave room for terminating '\0' byte */
211 for (p=buf; p < buf+len; ++p) {
212 c = getc(); /* read character */
214 putc(c); /* ... and echo it */
222 case 0x03: /* ^C - Control C */
228 /* Check for the console hangup (if any different from serial) */
229 if (gd->jt->getc != getc) {
236 /* line too long - truncate */
241 #if defined(CONFIG_CMD_SAVES)
243 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
247 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
248 int save_baudrate, current_baudrate;
250 save_baudrate = current_baudrate = gd->baudrate;
254 offset = simple_strtoul(argv[1], NULL, 16);
256 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
258 size = simple_strtoul(argv[2], NULL, 16);
261 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
263 /* default to current baudrate */
264 if (save_baudrate == 0)
265 save_baudrate = current_baudrate;
267 if (save_baudrate != current_baudrate) {
268 printf("## Switch baudrate to %d bps and press ENTER ...\n",
271 gd->baudrate = save_baudrate;
279 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
281 size = simple_strtoul(argv[2], NULL, 16);
283 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
285 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
290 if (save_serial(offset, size)) {
291 printf("## S-Record upload aborted\n");
293 printf("## S-Record upload complete\n");
295 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
296 if (save_baudrate != current_baudrate) {
297 printf("## Switch baudrate to %d bps and press ESC ...\n",
298 (int)current_baudrate);
300 gd->baudrate = current_baudrate;
304 if (getc() == 0x1B) /* ESC */
312 #define SREC3_START "S0030000FC\n"
313 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
314 #define SREC3_END "S70500000000FA\n"
315 #define SREC_BYTES_PER_RECORD 16
317 static int save_serial(ulong address, ulong count)
319 int i, c, reclen, checksum, length;
320 char *hex = "0123456789ABCDEF";
321 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
322 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
327 if(write_record(SREC3_START)) /* write the header */
330 if(count) { /* collect hex data in the buffer */
331 c = *(volatile uchar*)(address + reclen); /* get one byte */
332 checksum += c; /* accumulate checksum */
333 data[2*reclen] = hex[(c>>4)&0x0f];
334 data[2*reclen+1] = hex[c & 0x0f];
335 data[2*reclen+2] = '\0';
339 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
340 /* enough data collected for one record: dump it */
341 if(reclen) { /* build & write a data record: */
342 /* address + data + checksum */
343 length = 4 + reclen + 1;
345 /* accumulate length bytes into checksum */
346 for(i = 0; i < 2; i++)
347 checksum += (length >> (8*i)) & 0xff;
349 /* accumulate address bytes into checksum: */
350 for(i = 0; i < 4; i++)
351 checksum += (address >> (8*i)) & 0xff;
353 /* make proper checksum byte: */
354 checksum = ~checksum & 0xff;
356 /* output one record: */
357 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
358 if(write_record(record))
361 address += reclen; /* increment address */
367 if(write_record(SREC3_END)) /* write the final record */
372 static int write_record(char *buf)
379 /* Check for the console hangup (if any different from serial) */
391 #if defined(CONFIG_CMD_LOADB)
393 * loadb command (load binary) included
397 #define START_CHAR 0x01
398 #define ETX_CHAR 0x03
399 #define END_CHAR 0x0D
401 #define K_ESCAPE 0x23
402 #define SEND_TYPE 'S'
403 #define DATA_TYPE 'D'
405 #define NACK_TYPE 'N'
406 #define BREAK_TYPE 'B'
407 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
408 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
410 static void set_kerm_bin_mode(unsigned long *);
411 static int k_recv(void);
412 static ulong load_serial_bin(ulong offset);
415 static char his_eol; /* character he needs at end of packet */
416 static int his_pad_count; /* number of pad chars he needs */
417 static char his_pad_char; /* pad chars he needs */
418 static char his_quote; /* quote chars he'll use */
420 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
425 int load_baudrate, current_baudrate;
429 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
430 offset = CONFIG_SYS_LOAD_ADDR;
432 /* pre-set offset from $loadaddr */
433 s = env_get("loadaddr");
435 offset = simple_strtoul(s, NULL, 16);
437 load_baudrate = current_baudrate = gd->baudrate;
440 offset = simple_strtoul(argv[1], NULL, 16);
443 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
445 /* default to current baudrate */
446 if (load_baudrate == 0)
447 load_baudrate = current_baudrate;
450 if (load_baudrate != current_baudrate) {
451 printf("## Switch baudrate to %d bps and press ENTER ...\n",
454 gd->baudrate = load_baudrate;
463 if (strcmp(argv[0],"loady")==0) {
464 printf("## Ready for binary (ymodem) download "
465 "to 0x%08lX at %d bps...\n",
469 addr = load_serial_ymodem(offset, xyzModem_ymodem);
471 } else if (strcmp(argv[0],"loadx")==0) {
472 printf("## Ready for binary (xmodem) download "
473 "to 0x%08lX at %d bps...\n",
477 addr = load_serial_ymodem(offset, xyzModem_xmodem);
481 printf("## Ready for binary (kermit) download "
482 "to 0x%08lX at %d bps...\n",
485 addr = load_serial_bin(offset);
489 printf("## Binary (kermit) download aborted\n");
492 printf("## Start Addr = 0x%08lX\n", addr);
493 image_load_addr = addr;
496 if (load_baudrate != current_baudrate) {
497 printf("## Switch baudrate to %d bps and press ESC ...\n",
500 gd->baudrate = current_baudrate;
504 if (getc() == 0x1B) /* ESC */
513 static ulong load_serial_bin(ulong offset)
517 set_kerm_bin_mode((ulong *) offset);
521 * Gather any trailing characters (for instance, the ^D which
522 * is sent by 'cu' after sending a file), and give the
523 * box some time (100 * 1 ms)
525 for (i=0; i<100; ++i) {
532 flush_cache(offset, size);
534 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
535 env_set_hex("filesize", size);
540 static void send_pad(void)
542 int count = his_pad_count;
548 /* converts escaped kermit char to binary char */
549 static char ktrans(char in)
551 if ((in & 0x60) == 0x40) {
552 return (char) (in & ~0x40);
553 } else if ((in & 0x7f) == 0x3f) {
554 return (char) (in | 0x40);
559 static int chk1(char *buffer)
566 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
569 static void s1_sendpacket(char *packet)
578 static void send_ack(int n)
585 a_b[4] = tochar(chk1(&a_b[1]));
591 static void send_nack(int n)
598 a_b[4] = tochar(chk1(&a_b[1]));
605 static void (*os_data_init)(void);
606 static void (*os_data_char)(char new_char);
607 static int os_data_state, os_data_state_saved;
608 static char *os_data_addr, *os_data_addr_saved;
609 static char *bin_start_address;
611 static void bin_data_init(void)
614 os_data_addr = bin_start_address;
617 static void os_data_save(void)
619 os_data_state_saved = os_data_state;
620 os_data_addr_saved = os_data_addr;
623 static void os_data_restore(void)
625 os_data_state = os_data_state_saved;
626 os_data_addr = os_data_addr_saved;
629 static void bin_data_char(char new_char)
631 switch (os_data_state) {
633 *os_data_addr++ = new_char;
638 static void set_kerm_bin_mode(unsigned long *addr)
640 bin_start_address = (char *) addr;
641 os_data_init = bin_data_init;
642 os_data_char = bin_data_char;
646 /* k_data_* simply handles the kermit escape translations */
647 static int k_data_escape, k_data_escape_saved;
648 static void k_data_init(void)
654 static void k_data_save(void)
656 k_data_escape_saved = k_data_escape;
660 static void k_data_restore(void)
662 k_data_escape = k_data_escape_saved;
666 static void k_data_char(char new_char)
669 /* last char was escape - translate this character */
670 os_data_char(ktrans(new_char));
673 if (new_char == his_quote) {
674 /* this char is escape - remember */
677 /* otherwise send this char as-is */
678 os_data_char(new_char);
683 #define SEND_DATA_SIZE 20
684 static char send_parms[SEND_DATA_SIZE];
685 static char *send_ptr;
687 /* handle_send_packet interprits the protocol info and builds and
688 sends an appropriate ack for what we can do */
689 static void handle_send_packet(int n)
694 /* initialize some protocol parameters */
695 his_eol = END_CHAR; /* default end of line character */
698 his_quote = K_ESCAPE;
700 /* ignore last character if it filled the buffer */
701 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
703 bytes = send_ptr - send_parms; /* how many bytes we'll process */
707 /* handle MAXL - max length */
708 /* ignore what he says - most I'll take (here) is 94 */
709 a_b[++length] = tochar(94);
712 /* handle TIME - time you should wait for my packets */
713 /* ignore what he says - don't wait for my ack longer than 1 second */
714 a_b[++length] = tochar(1);
717 /* handle NPAD - number of pad chars I need */
718 /* remember what he says - I need none */
719 his_pad_count = untochar(send_parms[2]);
720 a_b[++length] = tochar(0);
723 /* handle PADC - pad chars I need */
724 /* remember what he says - I need none */
725 his_pad_char = ktrans(send_parms[3]);
726 a_b[++length] = 0x40; /* He should ignore this */
729 /* handle EOL - end of line he needs */
730 /* remember what he says - I need CR */
731 his_eol = untochar(send_parms[4]);
732 a_b[++length] = tochar(END_CHAR);
735 /* handle QCTL - quote control char he'll use */
736 /* remember what he says - I'll use '#' */
737 his_quote = send_parms[5];
741 /* handle QBIN - 8-th bit prefixing */
742 /* ignore what he says - I refuse */
746 /* handle CHKT - the clock check type */
747 /* ignore what he says - I do type 1 (for now) */
751 /* handle REPT - the repeat prefix */
752 /* ignore what he says - I refuse (for now) */
756 /* handle CAPAS - the capabilities mask */
757 /* ignore what he says - I only do long packets - I don't do windows */
758 a_b[++length] = tochar(2); /* only long packets */
759 a_b[++length] = tochar(0); /* no windows */
760 a_b[++length] = tochar(94); /* large packet msb */
761 a_b[++length] = tochar(94); /* large packet lsb */
765 a_b[1] = tochar(length);
768 a_b[++length] = '\0';
769 a_b[length] = tochar(chk1(&a_b[1]));
770 a_b[++length] = his_eol;
771 a_b[++length] = '\0';
775 /* k_recv receives a OS Open image file over kermit line */
776 static int k_recv(void)
779 char k_state, k_state_saved;
786 /* initialize some protocol parameters */
787 his_eol = END_CHAR; /* default end of line character */
790 his_quote = K_ESCAPE;
792 /* initialize the k_recv and k_data state machine */
796 k_state_saved = k_state;
798 n = 0; /* just to get rid of a warning */
801 /* expect this "type" sequence (but don't check):
806 B: break transmission
809 /* enter main loop */
811 /* set the send packet pointer to begining of send packet parms */
812 send_ptr = send_parms;
814 /* With each packet, start summing the bytes starting with the length.
815 Save the current sequence number.
816 Note the type of the packet.
817 If a character less than SPACE (0x20) is received - error.
821 /* OLD CODE, Prior to checking sequence numbers */
822 /* first have all state machines save current states */
823 k_state_saved = k_state;
828 /* wait for the starting character or ^C */
831 case START_CHAR: /* start packet */
833 case ETX_CHAR: /* ^C waiting for packet */
840 /* get length of packet */
843 if ((new_char & 0xE0) == 0)
845 sum += new_char & 0xff;
846 length = untochar(new_char);
847 /* get sequence number */
849 if ((new_char & 0xE0) == 0)
851 sum += new_char & 0xff;
852 n = untochar(new_char);
855 /* NEW CODE - check sequence numbers for retried packets */
856 /* Note - this new code assumes that the sequence number is correctly
857 * received. Handling an invalid sequence number adds another layer
858 * of complexity that may not be needed - yet! At this time, I'm hoping
859 * that I don't need to buffer the incoming data packets and can write
860 * the data into memory in real time.
863 /* same sequence number, restore the previous state */
864 k_state = k_state_saved;
867 /* new sequence number, checkpoint the download */
869 k_state_saved = k_state;
874 /* get packet type */
876 if ((new_char & 0xE0) == 0)
878 sum += new_char & 0xff;
881 /* check for extended length */
883 /* (length byte was 0, decremented twice) */
884 /* get the two length bytes */
886 if ((new_char & 0xE0) == 0)
888 sum += new_char & 0xff;
889 len_hi = untochar(new_char);
891 if ((new_char & 0xE0) == 0)
893 sum += new_char & 0xff;
894 len_lo = untochar(new_char);
895 length = len_hi * 95 + len_lo;
896 /* check header checksum */
898 if ((new_char & 0xE0) == 0)
900 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
902 sum += new_char & 0xff;
903 /* --length; */ /* new length includes only data and block check to come */
905 /* bring in rest of packet */
908 if ((new_char & 0xE0) == 0)
910 sum += new_char & 0xff;
912 if (k_state == DATA_TYPE) {
913 /* pass on the data if this is a data packet */
914 k_data_char (new_char);
915 } else if (k_state == SEND_TYPE) {
916 /* save send pack in buffer as is */
917 *send_ptr++ = new_char;
918 /* if too much data, back off the pointer */
919 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
923 /* get and validate checksum character */
925 if ((new_char & 0xE0) == 0)
927 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
931 if (new_char != END_CHAR) {
933 /* restore state machines */
934 k_state = k_state_saved;
936 /* send a negative acknowledge packet in */
938 } else if (k_state == SEND_TYPE) {
939 /* crack the protocol parms, build an appropriate ack packet */
940 handle_send_packet(n);
942 /* send simple acknowledge packet in */
944 /* quit if end of transmission */
945 if (k_state == BREAK_TYPE)
949 return ((ulong) os_data_addr - (ulong) bin_start_address);
952 static int getcxmodem(void) {
957 static ulong load_serial_ymodem(ulong offset, int mode)
962 connection_info_t info;
963 char ymodemBuf[1024];
964 ulong store_addr = ~0;
969 res = xyzModem_stream_open(&info, &err);
973 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
974 store_addr = addr + offset;
977 #ifdef CONFIG_MTD_NOR_FLASH
978 if (addr2info(store_addr)) {
981 rc = flash_write((char *) ymodemBuf,
990 memcpy((char *)(store_addr), ymodemBuf,
996 printf("%s\n", xyzModem_error(err));
999 xyzModem_stream_close(&err);
1000 xyzModem_stream_terminate(false, &getcxmodem);
1003 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1005 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1006 env_set_hex("filesize", size);
1013 /* -------------------------------------------------------------------- */
1015 #if defined(CONFIG_CMD_LOADS)
1017 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1019 loads, 3, 0, do_load_serial,
1020 "load S-Record file over serial line",
1021 "[ off ] [ baud ]\n"
1022 " - load S-Record file over serial line"
1023 " with offset 'off' and baudrate 'baud'"
1026 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1028 loads, 2, 0, do_load_serial,
1029 "load S-Record file over serial line",
1031 " - load S-Record file over serial line with offset 'off'"
1033 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1036 * SAVES always requires LOADS support, but not vice versa
1040 #if defined(CONFIG_CMD_SAVES)
1041 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1043 saves, 4, 0, do_save_serial,
1044 "save S-Record file over serial line",
1045 "[ off ] [size] [ baud ]\n"
1046 " - save S-Record file over serial line"
1047 " with offset 'off', size 'size' and baudrate 'baud'"
1049 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1051 saves, 3, 0, do_save_serial,
1052 "save S-Record file over serial line",
1054 " - save S-Record file over serial line with offset 'off' and size 'size'"
1056 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1057 #endif /* CONFIG_CMD_SAVES */
1058 #endif /* CONFIG_CMD_LOADS */
1061 #if defined(CONFIG_CMD_LOADB)
1063 loadb, 3, 0, do_load_serial_bin,
1064 "load binary file over serial line (kermit mode)",
1065 "[ off ] [ baud ]\n"
1066 " - load binary file over serial line"
1067 " with offset 'off' and baudrate 'baud'"
1071 loadx, 3, 0, do_load_serial_bin,
1072 "load binary file over serial line (xmodem mode)",
1073 "[ off ] [ baud ]\n"
1074 " - load binary file over serial line"
1075 " with offset 'off' and baudrate 'baud'"
1079 loady, 3, 0, do_load_serial_bin,
1080 "load binary file over serial line (ymodem mode)",
1081 "[ off ] [ baud ]\n"
1082 " - load binary file over serial line"
1083 " with offset 'off' and baudrate 'baud'"
1086 #endif /* CONFIG_CMD_LOADB */