1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
8 * Serial up- and download support
23 DECLARE_GLOBAL_DATA_PTR;
25 #if defined(CONFIG_CMD_LOADB)
26 static ulong load_serial_ymodem(ulong offset, int mode);
29 #if defined(CONFIG_CMD_LOADS)
30 static ulong load_serial(long offset);
31 static int read_record(char *buf, ulong len);
32 # if defined(CONFIG_CMD_SAVES)
33 static int save_serial(ulong offset, ulong size);
34 static int write_record(char *buf);
37 static int do_echo = 1;
40 /* -------------------------------------------------------------------- */
42 #if defined(CONFIG_CMD_LOADS)
43 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
51 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
52 int load_baudrate, current_baudrate;
54 load_baudrate = current_baudrate = gd->baudrate;
57 env_echo = env_get("loads_echo");
58 if (env_echo && *env_echo == '1')
63 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
65 offset = simple_strtol(argv[1], NULL, 16);
68 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
70 /* default to current baudrate */
71 if (load_baudrate == 0)
72 load_baudrate = current_baudrate;
74 if (load_baudrate != current_baudrate) {
75 printf("## Switch baudrate to %d bps and press ENTER ...\n",
78 gd->baudrate = load_baudrate;
86 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
88 offset = simple_strtol(argv[1], NULL, 16);
90 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
92 printf("## Ready for S-Record download ...\n");
94 addr = load_serial(offset);
97 * Gather any trailing characters (for instance, the ^D which
98 * is sent by 'cu' after sending a file), and give the
99 * box some time (100 * 1 ms)
101 for (i=0; i<100; ++i) {
109 printf("## S-Record download aborted\n");
112 printf("## Start Addr = 0x%08lX\n", addr);
113 image_load_addr = addr;
116 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
117 if (load_baudrate != current_baudrate) {
118 printf("## Switch baudrate to %d bps and press ESC ...\n",
121 gd->baudrate = current_baudrate;
125 if (getc() == 0x1B) /* ESC */
133 static ulong load_serial(long offset)
135 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
136 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
137 int binlen; /* no. of data bytes in S-Rec. */
138 int type; /* return code for record type */
139 ulong addr; /* load address from S-Record */
140 ulong size; /* number of bytes transferred */
142 ulong start_addr = ~0;
146 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
147 type = srec_decode(record, &binlen, &addr, binbuf);
150 return (~0); /* Invalid S-Record */
157 store_addr = addr + offset;
158 #ifdef CONFIG_MTD_NOR_FLASH
159 if (addr2info(store_addr)) {
162 rc = flash_write((char *)binbuf,store_addr,binlen);
170 memcpy((char *)(store_addr), binbuf, binlen);
172 if ((store_addr) < start_addr)
173 start_addr = store_addr;
174 if ((store_addr + binlen - 1) > end_addr)
175 end_addr = store_addr + binlen - 1;
181 size = end_addr - start_addr + 1;
183 "## First Load Addr = 0x%08lX\n"
184 "## Last Load Addr = 0x%08lX\n"
185 "## Total Size = 0x%08lX = %ld Bytes\n",
186 start_addr, end_addr, size, size
188 flush_cache(start_addr, size);
189 env_set_hex("filesize", size);
196 if (!do_echo) { /* print a '.' every 100 lines */
197 if ((++line_count % 100) == 0)
202 return (~0); /* Download aborted */
205 static int read_record(char *buf, ulong len)
210 --len; /* always leave room for terminating '\0' byte */
212 for (p=buf; p < buf+len; ++p) {
213 c = getc(); /* read character */
215 putc(c); /* ... and echo it */
223 case 0x03: /* ^C - Control C */
229 /* Check for the console hangup (if any different from serial) */
230 if (gd->jt->getc != getc) {
237 /* line too long - truncate */
242 #if defined(CONFIG_CMD_SAVES)
244 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
248 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
249 int save_baudrate, current_baudrate;
251 save_baudrate = current_baudrate = gd->baudrate;
255 offset = simple_strtoul(argv[1], NULL, 16);
257 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
259 size = simple_strtoul(argv[2], NULL, 16);
262 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
264 /* default to current baudrate */
265 if (save_baudrate == 0)
266 save_baudrate = current_baudrate;
268 if (save_baudrate != current_baudrate) {
269 printf("## Switch baudrate to %d bps and press ENTER ...\n",
272 gd->baudrate = save_baudrate;
280 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
282 size = simple_strtoul(argv[2], NULL, 16);
284 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
286 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
291 if (save_serial(offset, size)) {
292 printf("## S-Record upload aborted\n");
294 printf("## S-Record upload complete\n");
296 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
297 if (save_baudrate != current_baudrate) {
298 printf("## Switch baudrate to %d bps and press ESC ...\n",
299 (int)current_baudrate);
301 gd->baudrate = current_baudrate;
305 if (getc() == 0x1B) /* ESC */
313 #define SREC3_START "S0030000FC\n"
314 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
315 #define SREC3_END "S70500000000FA\n"
316 #define SREC_BYTES_PER_RECORD 16
318 static int save_serial(ulong address, ulong count)
320 int i, c, reclen, checksum, length;
321 char *hex = "0123456789ABCDEF";
322 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
323 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
328 if(write_record(SREC3_START)) /* write the header */
331 if(count) { /* collect hex data in the buffer */
332 c = *(volatile uchar*)(address + reclen); /* get one byte */
333 checksum += c; /* accumulate checksum */
334 data[2*reclen] = hex[(c>>4)&0x0f];
335 data[2*reclen+1] = hex[c & 0x0f];
336 data[2*reclen+2] = '\0';
340 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
341 /* enough data collected for one record: dump it */
342 if(reclen) { /* build & write a data record: */
343 /* address + data + checksum */
344 length = 4 + reclen + 1;
346 /* accumulate length bytes into checksum */
347 for(i = 0; i < 2; i++)
348 checksum += (length >> (8*i)) & 0xff;
350 /* accumulate address bytes into checksum: */
351 for(i = 0; i < 4; i++)
352 checksum += (address >> (8*i)) & 0xff;
354 /* make proper checksum byte: */
355 checksum = ~checksum & 0xff;
357 /* output one record: */
358 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
359 if(write_record(record))
362 address += reclen; /* increment address */
368 if(write_record(SREC3_END)) /* write the final record */
373 static int write_record(char *buf)
380 /* Check for the console hangup (if any different from serial) */
392 #if defined(CONFIG_CMD_LOADB)
394 * loadb command (load binary) included
398 #define START_CHAR 0x01
399 #define ETX_CHAR 0x03
400 #define END_CHAR 0x0D
402 #define K_ESCAPE 0x23
403 #define SEND_TYPE 'S'
404 #define DATA_TYPE 'D'
406 #define NACK_TYPE 'N'
407 #define BREAK_TYPE 'B'
408 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
409 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
411 static void set_kerm_bin_mode(unsigned long *);
412 static int k_recv(void);
413 static ulong load_serial_bin(ulong offset);
416 static char his_eol; /* character he needs at end of packet */
417 static int his_pad_count; /* number of pad chars he needs */
418 static char his_pad_char; /* pad chars he needs */
419 static char his_quote; /* quote chars he'll use */
421 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
426 int load_baudrate, current_baudrate;
430 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
431 offset = CONFIG_SYS_LOAD_ADDR;
433 /* pre-set offset from $loadaddr */
434 s = env_get("loadaddr");
436 offset = simple_strtoul(s, NULL, 16);
438 load_baudrate = current_baudrate = gd->baudrate;
441 offset = simple_strtoul(argv[1], NULL, 16);
444 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
446 /* default to current baudrate */
447 if (load_baudrate == 0)
448 load_baudrate = current_baudrate;
451 if (load_baudrate != current_baudrate) {
452 printf("## Switch baudrate to %d bps and press ENTER ...\n",
455 gd->baudrate = load_baudrate;
464 if (strcmp(argv[0],"loady")==0) {
465 printf("## Ready for binary (ymodem) download "
466 "to 0x%08lX at %d bps...\n",
470 addr = load_serial_ymodem(offset, xyzModem_ymodem);
472 } else if (strcmp(argv[0],"loadx")==0) {
473 printf("## Ready for binary (xmodem) download "
474 "to 0x%08lX at %d bps...\n",
478 addr = load_serial_ymodem(offset, xyzModem_xmodem);
482 printf("## Ready for binary (kermit) download "
483 "to 0x%08lX at %d bps...\n",
486 addr = load_serial_bin(offset);
490 printf("## Binary (kermit) download aborted\n");
493 printf("## Start Addr = 0x%08lX\n", addr);
494 image_load_addr = addr;
497 if (load_baudrate != current_baudrate) {
498 printf("## Switch baudrate to %d bps and press ESC ...\n",
501 gd->baudrate = current_baudrate;
505 if (getc() == 0x1B) /* ESC */
514 static ulong load_serial_bin(ulong offset)
518 set_kerm_bin_mode((ulong *) offset);
522 * Gather any trailing characters (for instance, the ^D which
523 * is sent by 'cu' after sending a file), and give the
524 * box some time (100 * 1 ms)
526 for (i=0; i<100; ++i) {
533 flush_cache(offset, size);
535 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
536 env_set_hex("filesize", size);
541 static void send_pad(void)
543 int count = his_pad_count;
549 /* converts escaped kermit char to binary char */
550 static char ktrans(char in)
552 if ((in & 0x60) == 0x40) {
553 return (char) (in & ~0x40);
554 } else if ((in & 0x7f) == 0x3f) {
555 return (char) (in | 0x40);
560 static int chk1(char *buffer)
567 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
570 static void s1_sendpacket(char *packet)
579 static void send_ack(int n)
586 a_b[4] = tochar(chk1(&a_b[1]));
592 static void send_nack(int n)
599 a_b[4] = tochar(chk1(&a_b[1]));
606 static void (*os_data_init)(void);
607 static void (*os_data_char)(char new_char);
608 static int os_data_state, os_data_state_saved;
609 static char *os_data_addr, *os_data_addr_saved;
610 static char *bin_start_address;
612 static void bin_data_init(void)
615 os_data_addr = bin_start_address;
618 static void os_data_save(void)
620 os_data_state_saved = os_data_state;
621 os_data_addr_saved = os_data_addr;
624 static void os_data_restore(void)
626 os_data_state = os_data_state_saved;
627 os_data_addr = os_data_addr_saved;
630 static void bin_data_char(char new_char)
632 switch (os_data_state) {
634 *os_data_addr++ = new_char;
639 static void set_kerm_bin_mode(unsigned long *addr)
641 bin_start_address = (char *) addr;
642 os_data_init = bin_data_init;
643 os_data_char = bin_data_char;
647 /* k_data_* simply handles the kermit escape translations */
648 static int k_data_escape, k_data_escape_saved;
649 static void k_data_init(void)
655 static void k_data_save(void)
657 k_data_escape_saved = k_data_escape;
661 static void k_data_restore(void)
663 k_data_escape = k_data_escape_saved;
667 static void k_data_char(char new_char)
670 /* last char was escape - translate this character */
671 os_data_char(ktrans(new_char));
674 if (new_char == his_quote) {
675 /* this char is escape - remember */
678 /* otherwise send this char as-is */
679 os_data_char(new_char);
684 #define SEND_DATA_SIZE 20
685 static char send_parms[SEND_DATA_SIZE];
686 static char *send_ptr;
688 /* handle_send_packet interprits the protocol info and builds and
689 sends an appropriate ack for what we can do */
690 static void handle_send_packet(int n)
695 /* initialize some protocol parameters */
696 his_eol = END_CHAR; /* default end of line character */
699 his_quote = K_ESCAPE;
701 /* ignore last character if it filled the buffer */
702 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
704 bytes = send_ptr - send_parms; /* how many bytes we'll process */
708 /* handle MAXL - max length */
709 /* ignore what he says - most I'll take (here) is 94 */
710 a_b[++length] = tochar(94);
713 /* handle TIME - time you should wait for my packets */
714 /* ignore what he says - don't wait for my ack longer than 1 second */
715 a_b[++length] = tochar(1);
718 /* handle NPAD - number of pad chars I need */
719 /* remember what he says - I need none */
720 his_pad_count = untochar(send_parms[2]);
721 a_b[++length] = tochar(0);
724 /* handle PADC - pad chars I need */
725 /* remember what he says - I need none */
726 his_pad_char = ktrans(send_parms[3]);
727 a_b[++length] = 0x40; /* He should ignore this */
730 /* handle EOL - end of line he needs */
731 /* remember what he says - I need CR */
732 his_eol = untochar(send_parms[4]);
733 a_b[++length] = tochar(END_CHAR);
736 /* handle QCTL - quote control char he'll use */
737 /* remember what he says - I'll use '#' */
738 his_quote = send_parms[5];
742 /* handle QBIN - 8-th bit prefixing */
743 /* ignore what he says - I refuse */
747 /* handle CHKT - the clock check type */
748 /* ignore what he says - I do type 1 (for now) */
752 /* handle REPT - the repeat prefix */
753 /* ignore what he says - I refuse (for now) */
757 /* handle CAPAS - the capabilities mask */
758 /* ignore what he says - I only do long packets - I don't do windows */
759 a_b[++length] = tochar(2); /* only long packets */
760 a_b[++length] = tochar(0); /* no windows */
761 a_b[++length] = tochar(94); /* large packet msb */
762 a_b[++length] = tochar(94); /* large packet lsb */
766 a_b[1] = tochar(length);
769 a_b[++length] = '\0';
770 a_b[length] = tochar(chk1(&a_b[1]));
771 a_b[++length] = his_eol;
772 a_b[++length] = '\0';
776 /* k_recv receives a OS Open image file over kermit line */
777 static int k_recv(void)
780 char k_state, k_state_saved;
787 /* initialize some protocol parameters */
788 his_eol = END_CHAR; /* default end of line character */
791 his_quote = K_ESCAPE;
793 /* initialize the k_recv and k_data state machine */
797 k_state_saved = k_state;
799 n = 0; /* just to get rid of a warning */
802 /* expect this "type" sequence (but don't check):
807 B: break transmission
810 /* enter main loop */
812 /* set the send packet pointer to begining of send packet parms */
813 send_ptr = send_parms;
815 /* With each packet, start summing the bytes starting with the length.
816 Save the current sequence number.
817 Note the type of the packet.
818 If a character less than SPACE (0x20) is received - error.
822 /* OLD CODE, Prior to checking sequence numbers */
823 /* first have all state machines save current states */
824 k_state_saved = k_state;
829 /* wait for the starting character or ^C */
832 case START_CHAR: /* start packet */
834 case ETX_CHAR: /* ^C waiting for packet */
841 /* get length of packet */
844 if ((new_char & 0xE0) == 0)
846 sum += new_char & 0xff;
847 length = untochar(new_char);
848 /* get sequence number */
850 if ((new_char & 0xE0) == 0)
852 sum += new_char & 0xff;
853 n = untochar(new_char);
856 /* NEW CODE - check sequence numbers for retried packets */
857 /* Note - this new code assumes that the sequence number is correctly
858 * received. Handling an invalid sequence number adds another layer
859 * of complexity that may not be needed - yet! At this time, I'm hoping
860 * that I don't need to buffer the incoming data packets and can write
861 * the data into memory in real time.
864 /* same sequence number, restore the previous state */
865 k_state = k_state_saved;
868 /* new sequence number, checkpoint the download */
870 k_state_saved = k_state;
875 /* get packet type */
877 if ((new_char & 0xE0) == 0)
879 sum += new_char & 0xff;
882 /* check for extended length */
884 /* (length byte was 0, decremented twice) */
885 /* get the two length bytes */
887 if ((new_char & 0xE0) == 0)
889 sum += new_char & 0xff;
890 len_hi = untochar(new_char);
892 if ((new_char & 0xE0) == 0)
894 sum += new_char & 0xff;
895 len_lo = untochar(new_char);
896 length = len_hi * 95 + len_lo;
897 /* check header checksum */
899 if ((new_char & 0xE0) == 0)
901 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
903 sum += new_char & 0xff;
904 /* --length; */ /* new length includes only data and block check to come */
906 /* bring in rest of packet */
909 if ((new_char & 0xE0) == 0)
911 sum += new_char & 0xff;
913 if (k_state == DATA_TYPE) {
914 /* pass on the data if this is a data packet */
915 k_data_char (new_char);
916 } else if (k_state == SEND_TYPE) {
917 /* save send pack in buffer as is */
918 *send_ptr++ = new_char;
919 /* if too much data, back off the pointer */
920 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
924 /* get and validate checksum character */
926 if ((new_char & 0xE0) == 0)
928 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
932 if (new_char != END_CHAR) {
934 /* restore state machines */
935 k_state = k_state_saved;
937 /* send a negative acknowledge packet in */
939 } else if (k_state == SEND_TYPE) {
940 /* crack the protocol parms, build an appropriate ack packet */
941 handle_send_packet(n);
943 /* send simple acknowledge packet in */
945 /* quit if end of transmission */
946 if (k_state == BREAK_TYPE)
950 return ((ulong) os_data_addr - (ulong) bin_start_address);
953 static int getcxmodem(void) {
958 static ulong load_serial_ymodem(ulong offset, int mode)
963 connection_info_t info;
964 char ymodemBuf[1024];
965 ulong store_addr = ~0;
970 res = xyzModem_stream_open(&info, &err);
974 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
975 store_addr = addr + offset;
978 #ifdef CONFIG_MTD_NOR_FLASH
979 if (addr2info(store_addr)) {
982 rc = flash_write((char *) ymodemBuf,
991 memcpy((char *)(store_addr), ymodemBuf,
997 printf("%s\n", xyzModem_error(err));
1000 xyzModem_stream_close(&err);
1001 xyzModem_stream_terminate(false, &getcxmodem);
1004 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1006 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1007 env_set_hex("filesize", size);
1014 /* -------------------------------------------------------------------- */
1016 #if defined(CONFIG_CMD_LOADS)
1018 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1020 loads, 3, 0, do_load_serial,
1021 "load S-Record file over serial line",
1022 "[ off ] [ baud ]\n"
1023 " - load S-Record file over serial line"
1024 " with offset 'off' and baudrate 'baud'"
1027 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1029 loads, 2, 0, do_load_serial,
1030 "load S-Record file over serial line",
1032 " - load S-Record file over serial line with offset 'off'"
1034 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1037 * SAVES always requires LOADS support, but not vice versa
1041 #if defined(CONFIG_CMD_SAVES)
1042 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1044 saves, 4, 0, do_save_serial,
1045 "save S-Record file over serial line",
1046 "[ off ] [size] [ baud ]\n"
1047 " - save S-Record file over serial line"
1048 " with offset 'off', size 'size' and baudrate 'baud'"
1050 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1052 saves, 3, 0, do_save_serial,
1053 "save S-Record file over serial line",
1055 " - save S-Record file over serial line with offset 'off' and size 'size'"
1057 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1058 #endif /* CONFIG_CMD_SAVES */
1059 #endif /* CONFIG_CMD_LOADS */
1062 #if defined(CONFIG_CMD_LOADB)
1064 loadb, 3, 0, do_load_serial_bin,
1065 "load binary file over serial line (kermit mode)",
1066 "[ off ] [ baud ]\n"
1067 " - load binary file over serial line"
1068 " with offset 'off' and baudrate 'baud'"
1072 loadx, 3, 0, do_load_serial_bin,
1073 "load binary file over serial line (xmodem mode)",
1074 "[ off ] [ baud ]\n"
1075 " - load binary file over serial line"
1076 " with offset 'off' and baudrate 'baud'"
1080 loady, 3, 0, do_load_serial_bin,
1081 "load binary file over serial line (ymodem mode)",
1082 "[ off ] [ baud ]\n"
1083 " - load binary file over serial line"
1084 " with offset 'off' and baudrate 'baud'"
1087 #endif /* CONFIG_CMD_LOADB */