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
14 #include <efi_loader.h>
24 #include <asm/cache.h>
25 #include <asm/global_data.h>
26 #include <linux/delay.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #if defined(CONFIG_CMD_LOADB)
31 static ulong load_serial_ymodem(ulong offset, int mode);
34 #if defined(CONFIG_CMD_LOADS)
35 static ulong load_serial(long offset);
36 static int read_record(char *buf, ulong len);
37 # if defined(CONFIG_CMD_SAVES)
38 static int save_serial(ulong offset, ulong size);
39 static int write_record(char *buf);
42 static int do_echo = 1;
45 /* -------------------------------------------------------------------- */
47 #if defined(CONFIG_CMD_LOADS)
48 static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
56 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
57 int load_baudrate, current_baudrate;
59 load_baudrate = current_baudrate = gd->baudrate;
62 env_echo = env_get("loads_echo");
63 if (env_echo && *env_echo == '1')
68 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
70 offset = simple_strtol(argv[1], NULL, 16);
73 load_baudrate = (int)dectoul(argv[2], NULL);
75 /* default to current baudrate */
76 if (load_baudrate == 0)
77 load_baudrate = current_baudrate;
79 if (load_baudrate != current_baudrate) {
80 printf("## Switch baudrate to %d bps and press ENTER ...\n",
83 gd->baudrate = load_baudrate;
87 if (getchar() == '\r')
91 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
93 offset = simple_strtol(argv[1], NULL, 16);
95 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
97 printf("## Ready for S-Record download ...\n");
99 addr = load_serial(offset);
102 * Gather any trailing characters (for instance, the ^D which
103 * is sent by 'cu' after sending a file), and give the
104 * box some time (100 * 1 ms)
106 for (i=0; i<100; ++i) {
114 printf("## S-Record download aborted\n");
117 printf("## Start Addr = 0x%08lX\n", addr);
118 image_load_addr = addr;
121 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
122 if (load_baudrate != current_baudrate) {
123 printf("## Switch baudrate to %d bps and press ESC ...\n",
126 gd->baudrate = current_baudrate;
130 if (getchar() == 0x1B) /* ESC */
138 static ulong load_serial(long offset)
140 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
141 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
142 int binlen; /* no. of data bytes in S-Rec. */
143 int type; /* return code for record type */
144 ulong addr; /* load address from S-Record */
145 ulong size; /* number of bytes transferred */
147 ulong start_addr = ~0;
151 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
152 type = srec_decode(record, &binlen, &addr, binbuf);
155 return (~0); /* Invalid S-Record */
162 store_addr = addr + offset;
163 #ifdef CONFIG_MTD_NOR_FLASH
164 if (addr2info(store_addr)) {
167 rc = flash_write((char *)binbuf,store_addr,binlen);
175 memcpy((char *)(store_addr), binbuf, binlen);
177 if ((store_addr) < start_addr)
178 start_addr = store_addr;
179 if ((store_addr + binlen - 1) > end_addr)
180 end_addr = store_addr + binlen - 1;
186 size = end_addr - start_addr + 1;
188 "## First Load Addr = 0x%08lX\n"
189 "## Last Load Addr = 0x%08lX\n"
190 "## Total Size = 0x%08lX = %ld Bytes\n",
191 start_addr, end_addr, size, size
193 flush_cache(start_addr, size);
194 env_set_hex("filesize", size);
201 if (!do_echo) { /* print a '.' every 100 lines */
202 if ((++line_count % 100) == 0)
207 return (~0); /* Download aborted */
210 static int read_record(char *buf, ulong len)
215 --len; /* always leave room for terminating '\0' byte */
217 for (p=buf; p < buf+len; ++p) {
218 c = getchar(); /* read character */
220 putc(c); /* ... and echo it */
228 case 0x03: /* ^C - Control C */
234 /* Check for the console hangup (if any different from serial) */
235 if (gd->jt->getc != getchar) {
241 /* line too long - truncate */
246 #if defined(CONFIG_CMD_SAVES)
248 int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
253 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
254 int save_baudrate, current_baudrate;
256 save_baudrate = current_baudrate = gd->baudrate;
260 offset = hextoul(argv[1], NULL);
262 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
264 size = hextoul(argv[2], NULL);
267 save_baudrate = (int)dectoul(argv[3], NULL);
269 /* default to current baudrate */
270 if (save_baudrate == 0)
271 save_baudrate = current_baudrate;
273 if (save_baudrate != current_baudrate) {
274 printf("## Switch baudrate to %d bps and press ENTER ...\n",
277 gd->baudrate = save_baudrate;
281 if (getchar() == '\r')
285 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
287 size = hextoul(argv[2], NULL);
289 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
291 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
293 if (getchar() == '\r')
296 if (save_serial(offset, size)) {
297 printf("## S-Record upload aborted\n");
299 printf("## S-Record upload complete\n");
301 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
302 if (save_baudrate != current_baudrate) {
303 printf("## Switch baudrate to %d bps and press ESC ...\n",
304 (int)current_baudrate);
306 gd->baudrate = current_baudrate;
310 if (getchar() == 0x1B) /* ESC */
318 #define SREC3_START "S0030000FC\n"
319 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
320 #define SREC3_END "S70500000000FA\n"
321 #define SREC_BYTES_PER_RECORD 16
323 static int save_serial(ulong address, ulong count)
325 int i, c, reclen, checksum, length;
326 char *hex = "0123456789ABCDEF";
327 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
328 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
333 if(write_record(SREC3_START)) /* write the header */
336 if(count) { /* collect hex data in the buffer */
337 c = *(volatile uchar*)(address + reclen); /* get one byte */
338 checksum += c; /* accumulate checksum */
339 data[2*reclen] = hex[(c>>4)&0x0f];
340 data[2*reclen+1] = hex[c & 0x0f];
341 data[2*reclen+2] = '\0';
345 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
346 /* enough data collected for one record: dump it */
347 if(reclen) { /* build & write a data record: */
348 /* address + data + checksum */
349 length = 4 + reclen + 1;
351 /* accumulate length bytes into checksum */
352 for(i = 0; i < 2; i++)
353 checksum += (length >> (8*i)) & 0xff;
355 /* accumulate address bytes into checksum: */
356 for(i = 0; i < 4; i++)
357 checksum += (address >> (8*i)) & 0xff;
359 /* make proper checksum byte: */
360 checksum = ~checksum & 0xff;
362 /* output one record: */
363 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
364 if(write_record(record))
367 address += reclen; /* increment address */
373 if(write_record(SREC3_END)) /* write the final record */
378 static int write_record(char *buf)
385 /* Check for the console hangup (if any different from serial) */
397 #if defined(CONFIG_CMD_LOADB)
399 * loadb command (load binary) included
403 #define START_CHAR 0x01
404 #define ETX_CHAR 0x03
405 #define END_CHAR 0x0D
407 #define K_ESCAPE 0x23
408 #define SEND_TYPE 'S'
409 #define DATA_TYPE 'D'
411 #define NACK_TYPE 'N'
412 #define BREAK_TYPE 'B'
413 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
414 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
416 static void set_kerm_bin_mode(unsigned long *);
417 static int k_recv(void);
418 static ulong load_serial_bin(ulong offset);
421 static char his_eol; /* character he needs at end of packet */
422 static int his_pad_count; /* number of pad chars he needs */
423 static char his_pad_char; /* pad chars he needs */
424 static char his_quote; /* quote chars he'll use */
426 static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
431 int load_baudrate, current_baudrate;
435 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
436 offset = CONFIG_SYS_LOAD_ADDR;
438 /* pre-set offset from $loadaddr */
439 s = env_get("loadaddr");
441 offset = hextoul(s, NULL);
443 load_baudrate = current_baudrate = gd->baudrate;
446 offset = hextoul(argv[1], NULL);
449 load_baudrate = (int)dectoul(argv[2], NULL);
451 /* default to current baudrate */
452 if (load_baudrate == 0)
453 load_baudrate = current_baudrate;
456 if (load_baudrate != current_baudrate) {
457 printf("## Switch baudrate to %d bps and press ENTER ...\n",
460 gd->baudrate = load_baudrate;
464 if (getchar() == '\r')
469 if (strcmp(argv[0],"loady")==0) {
470 printf("## Ready for binary (ymodem) download "
471 "to 0x%08lX at %d bps...\n",
475 addr = load_serial_ymodem(offset, xyzModem_ymodem);
479 printf("## Binary (ymodem) download aborted\n");
482 printf("## Start Addr = 0x%08lX\n", addr);
483 image_load_addr = addr;
485 } else if (strcmp(argv[0],"loadx")==0) {
486 printf("## Ready for binary (xmodem) download "
487 "to 0x%08lX at %d bps...\n",
491 addr = load_serial_ymodem(offset, xyzModem_xmodem);
495 printf("## Binary (xmodem) download aborted\n");
498 printf("## Start Addr = 0x%08lX\n", addr);
499 image_load_addr = addr;
503 printf("## Ready for binary (kermit) download "
504 "to 0x%08lX at %d bps...\n",
507 addr = load_serial_bin(offset);
511 printf("## Binary (kermit) download aborted\n");
514 printf("## Start Addr = 0x%08lX\n", addr);
515 image_load_addr = addr;
518 if (load_baudrate != current_baudrate) {
519 printf("## Switch baudrate to %d bps and press ESC ...\n",
522 gd->baudrate = current_baudrate;
526 if (getchar() == 0x1B) /* ESC */
535 static ulong load_serial_bin(ulong offset)
539 set_kerm_bin_mode((ulong *) offset);
543 * Gather any trailing characters (for instance, the ^D which
544 * is sent by 'cu' after sending a file), and give the
545 * box some time (100 * 1 ms)
547 for (i=0; i<100; ++i) {
555 return ~0; /* Download aborted */
557 flush_cache(offset, size);
559 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
560 env_set_hex("filesize", size);
565 static void send_pad(void)
567 int count = his_pad_count;
573 /* converts escaped kermit char to binary char */
574 static char ktrans(char in)
576 if ((in & 0x60) == 0x40) {
577 return (char) (in & ~0x40);
578 } else if ((in & 0x7f) == 0x3f) {
579 return (char) (in | 0x40);
584 static int chk1(char *buffer)
591 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
594 static void s1_sendpacket(char *packet)
603 static void send_ack(int n)
610 a_b[4] = tochar(chk1(&a_b[1]));
616 static void send_nack(int n)
623 a_b[4] = tochar(chk1(&a_b[1]));
630 static void (*os_data_init)(void);
631 static void (*os_data_char)(char new_char);
632 static int os_data_state, os_data_state_saved;
633 static char *os_data_addr, *os_data_addr_saved;
634 static char *bin_start_address;
636 static void bin_data_init(void)
639 os_data_addr = bin_start_address;
642 static void os_data_save(void)
644 os_data_state_saved = os_data_state;
645 os_data_addr_saved = os_data_addr;
648 static void os_data_restore(void)
650 os_data_state = os_data_state_saved;
651 os_data_addr = os_data_addr_saved;
654 static void bin_data_char(char new_char)
656 switch (os_data_state) {
658 *os_data_addr++ = new_char;
663 static void set_kerm_bin_mode(unsigned long *addr)
665 bin_start_address = (char *) addr;
666 os_data_init = bin_data_init;
667 os_data_char = bin_data_char;
671 /* k_data_* simply handles the kermit escape translations */
672 static int k_data_escape, k_data_escape_saved;
673 static void k_data_init(void)
679 static void k_data_save(void)
681 k_data_escape_saved = k_data_escape;
685 static void k_data_restore(void)
687 k_data_escape = k_data_escape_saved;
691 static void k_data_char(char new_char)
694 /* last char was escape - translate this character */
695 os_data_char(ktrans(new_char));
698 if (new_char == his_quote) {
699 /* this char is escape - remember */
702 /* otherwise send this char as-is */
703 os_data_char(new_char);
708 #define SEND_DATA_SIZE 20
709 static char send_parms[SEND_DATA_SIZE];
710 static char *send_ptr;
712 /* handle_send_packet interprits the protocol info and builds and
713 sends an appropriate ack for what we can do */
714 static void handle_send_packet(int n)
719 /* initialize some protocol parameters */
720 his_eol = END_CHAR; /* default end of line character */
723 his_quote = K_ESCAPE;
725 /* ignore last character if it filled the buffer */
726 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
728 bytes = send_ptr - send_parms; /* how many bytes we'll process */
732 /* handle MAXL - max length */
733 /* ignore what he says - most I'll take (here) is 94 */
734 a_b[++length] = tochar(94);
737 /* handle TIME - time you should wait for my packets */
738 /* ignore what he says - don't wait for my ack longer than 1 second */
739 a_b[++length] = tochar(1);
742 /* handle NPAD - number of pad chars I need */
743 /* remember what he says - I need none */
744 his_pad_count = untochar(send_parms[2]);
745 a_b[++length] = tochar(0);
748 /* handle PADC - pad chars I need */
749 /* remember what he says - I need none */
750 his_pad_char = ktrans(send_parms[3]);
751 a_b[++length] = 0x40; /* He should ignore this */
754 /* handle EOL - end of line he needs */
755 /* remember what he says - I need CR */
756 his_eol = untochar(send_parms[4]);
757 a_b[++length] = tochar(END_CHAR);
760 /* handle QCTL - quote control char he'll use */
761 /* remember what he says - I'll use '#' */
762 his_quote = send_parms[5];
766 /* handle QBIN - 8-th bit prefixing */
767 /* ignore what he says - I refuse */
771 /* handle CHKT - the clock check type */
772 /* ignore what he says - I do type 1 (for now) */
776 /* handle REPT - the repeat prefix */
777 /* ignore what he says - I refuse (for now) */
781 /* handle CAPAS - the capabilities mask */
782 /* ignore what he says - I only do long packets - I don't do windows */
783 a_b[++length] = tochar(2); /* only long packets */
784 a_b[++length] = tochar(0); /* no windows */
785 a_b[++length] = tochar(94); /* large packet msb */
786 a_b[++length] = tochar(94); /* large packet lsb */
790 a_b[1] = tochar(length);
793 a_b[++length] = '\0';
794 a_b[length] = tochar(chk1(&a_b[1]));
795 a_b[++length] = his_eol;
796 a_b[++length] = '\0';
800 /* k_recv receives a OS Open image file over kermit line */
801 static int k_recv(void)
804 char k_state, k_state_saved;
811 /* initialize some protocol parameters */
812 his_eol = END_CHAR; /* default end of line character */
815 his_quote = K_ESCAPE;
817 /* initialize the k_recv and k_data state machine */
821 k_state_saved = k_state;
823 n = 0; /* just to get rid of a warning */
826 /* expect this "type" sequence (but don't check):
831 B: break transmission
834 /* enter main loop */
836 /* set the send packet pointer to begining of send packet parms */
837 send_ptr = send_parms;
839 /* With each packet, start summing the bytes starting with the length.
840 Save the current sequence number.
841 Note the type of the packet.
842 If a character less than SPACE (0x20) is received - error.
846 /* OLD CODE, Prior to checking sequence numbers */
847 /* first have all state machines save current states */
848 k_state_saved = k_state;
853 /* wait for the starting character or ^C */
856 case START_CHAR: /* start packet */
858 case ETX_CHAR: /* ^C waiting for packet */
865 /* get length of packet */
867 new_char = getchar();
868 if ((new_char & 0xE0) == 0)
870 sum += new_char & 0xff;
871 length = untochar(new_char);
872 /* get sequence number */
873 new_char = getchar();
874 if ((new_char & 0xE0) == 0)
876 sum += new_char & 0xff;
877 n = untochar(new_char);
880 /* NEW CODE - check sequence numbers for retried packets */
881 /* Note - this new code assumes that the sequence number is correctly
882 * received. Handling an invalid sequence number adds another layer
883 * of complexity that may not be needed - yet! At this time, I'm hoping
884 * that I don't need to buffer the incoming data packets and can write
885 * the data into memory in real time.
888 /* same sequence number, restore the previous state */
889 k_state = k_state_saved;
892 /* new sequence number, checkpoint the download */
894 k_state_saved = k_state;
899 /* get packet type */
900 new_char = getchar();
901 if ((new_char & 0xE0) == 0)
903 sum += new_char & 0xff;
906 /* check for extended length */
908 /* (length byte was 0, decremented twice) */
909 /* get the two length bytes */
910 new_char = getchar();
911 if ((new_char & 0xE0) == 0)
913 sum += new_char & 0xff;
914 len_hi = untochar(new_char);
915 new_char = getchar();
916 if ((new_char & 0xE0) == 0)
918 sum += new_char & 0xff;
919 len_lo = untochar(new_char);
920 length = len_hi * 95 + len_lo;
921 /* check header checksum */
922 new_char = getchar();
923 if ((new_char & 0xE0) == 0)
925 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
927 sum += new_char & 0xff;
928 /* --length; */ /* new length includes only data and block check to come */
930 /* bring in rest of packet */
932 new_char = getchar();
933 if ((new_char & 0xE0) == 0)
935 sum += new_char & 0xff;
937 if (k_state == DATA_TYPE) {
938 /* pass on the data if this is a data packet */
939 k_data_char (new_char);
940 } else if (k_state == SEND_TYPE) {
941 /* save send pack in buffer as is */
942 *send_ptr++ = new_char;
943 /* if too much data, back off the pointer */
944 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
948 /* get and validate checksum character */
949 new_char = getchar();
950 if ((new_char & 0xE0) == 0)
952 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
955 new_char = getchar();
956 if (new_char != END_CHAR) {
958 /* restore state machines */
959 k_state = k_state_saved;
961 /* send a negative acknowledge packet in */
963 } else if (k_state == SEND_TYPE) {
964 /* crack the protocol parms, build an appropriate ack packet */
965 handle_send_packet(n);
967 /* send simple acknowledge packet in */
969 /* quit if end of transmission */
970 if (k_state == BREAK_TYPE)
974 return ((ulong) os_data_addr - (ulong) bin_start_address);
977 static int getcxmodem(void) {
982 static ulong load_serial_ymodem(ulong offset, int mode)
987 connection_info_t info;
988 char ymodemBuf[1024];
989 ulong store_addr = ~0;
994 res = xyzModem_stream_open(&info, &err);
999 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
1000 store_addr = addr + offset;
1003 #ifdef CONFIG_MTD_NOR_FLASH
1004 if (addr2info(store_addr)) {
1007 rc = flash_write((char *) ymodemBuf,
1010 xyzModem_stream_terminate(true, &getcxmodem);
1011 xyzModem_stream_close(&err);
1019 memcpy((char *)(store_addr), ymodemBuf,
1025 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1026 xyzModem_stream_close(&err);
1027 printf("\n%s\n", xyzModem_error(err));
1028 return (~0); /* Download aborted */
1031 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1032 efi_set_bootdev("Uart", "", "",
1033 map_sysmem(offset, 0), size);
1036 printf("\n%s\n", xyzModem_error(err));
1037 return (~0); /* Download aborted */
1040 xyzModem_stream_terminate(false, &getcxmodem);
1041 xyzModem_stream_close(&err);
1044 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1046 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1047 env_set_hex("filesize", size);
1054 /* -------------------------------------------------------------------- */
1056 #if defined(CONFIG_CMD_LOADS)
1058 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1060 loads, 3, 0, do_load_serial,
1061 "load S-Record file over serial line",
1062 "[ off ] [ baud ]\n"
1063 " - load S-Record file over serial line"
1064 " with offset 'off' and baudrate 'baud'"
1067 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1069 loads, 2, 0, do_load_serial,
1070 "load S-Record file over serial line",
1072 " - load S-Record file over serial line with offset 'off'"
1074 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1077 * SAVES always requires LOADS support, but not vice versa
1081 #if defined(CONFIG_CMD_SAVES)
1082 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1084 saves, 4, 0, do_save_serial,
1085 "save S-Record file over serial line",
1086 "[ off ] [size] [ baud ]\n"
1087 " - save S-Record file over serial line"
1088 " with offset 'off', size 'size' and baudrate 'baud'"
1090 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1092 saves, 3, 0, do_save_serial,
1093 "save S-Record file over serial line",
1095 " - save S-Record file over serial line with offset 'off' and size 'size'"
1097 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1098 #endif /* CONFIG_CMD_SAVES */
1099 #endif /* CONFIG_CMD_LOADS */
1102 #if defined(CONFIG_CMD_LOADB)
1104 loadb, 3, 0, do_load_serial_bin,
1105 "load binary file over serial line (kermit mode)",
1106 "[ addr [ baud ] ]\n"
1107 " - load binary file over serial line"
1108 " at address 'addr' with baudrate 'baud'"
1112 loadx, 3, 0, do_load_serial_bin,
1113 "load binary file over serial line (xmodem mode)",
1114 "[ addr [ baud ] ]\n"
1115 " - load binary file over serial line"
1116 " at address 'addr' with baudrate 'baud'"
1120 loady, 3, 0, do_load_serial_bin,
1121 "load binary file over serial line (ymodem mode)",
1122 "[ addr [ baud ] ]\n"
1123 " - load binary file over serial line"
1124 " at address 'addr' with baudrate 'baud'"
1127 #endif /* CONFIG_CMD_LOADB */