1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7 /*
8 * Serial up- and download support
9 */
10 #include <common.h>
11 #include <command.h>
12 #include <console.h>
13 #include <cpu_func.h>
14 #include <env.h>
15 #include <s_record.h>
16 #include <net.h>
17 #include <exports.h>
18 #include <serial.h>
19 #include <xyzModem.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #if defined(CONFIG_CMD_LOADB)
24 static ulong load_serial_ymodem(ulong offset, int mode);
25 #endif
26
27 #if defined(CONFIG_CMD_LOADS)
28 static ulong load_serial(long offset);
29 static int read_record(char *buf, ulong len);
30 # if defined(CONFIG_CMD_SAVES)
31 static int save_serial(ulong offset, ulong size);
32 static int write_record(char *buf);
33 #endif
34
35 static int do_echo = 1;
36 #endif
37
38 /* -------------------------------------------------------------------- */
39
40 #if defined(CONFIG_CMD_LOADS)
do_load_serial(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])41 static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
42 char * const argv[])
43 {
44 long offset = 0;
45 ulong addr;
46 int i;
47 char *env_echo;
48 int rcode = 0;
49 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
50 int load_baudrate, current_baudrate;
51
52 load_baudrate = current_baudrate = gd->baudrate;
53 #endif
54
55 env_echo = env_get("loads_echo");
56 if (env_echo && *env_echo == '1')
57 do_echo = 1;
58 else
59 do_echo = 0;
60
61 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
62 if (argc >= 2) {
63 offset = simple_strtol(argv[1], NULL, 16);
64 }
65 if (argc == 3) {
66 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
67
68 /* default to current baudrate */
69 if (load_baudrate == 0)
70 load_baudrate = current_baudrate;
71 }
72 if (load_baudrate != current_baudrate) {
73 printf("## Switch baudrate to %d bps and press ENTER ...\n",
74 load_baudrate);
75 udelay(50000);
76 gd->baudrate = load_baudrate;
77 serial_setbrg();
78 udelay(50000);
79 for (;;) {
80 if (getc() == '\r')
81 break;
82 }
83 }
84 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
85 if (argc == 2) {
86 offset = simple_strtol(argv[1], NULL, 16);
87 }
88 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
89
90 printf("## Ready for S-Record download ...\n");
91
92 addr = load_serial(offset);
93
94 /*
95 * Gather any trailing characters (for instance, the ^D which
96 * is sent by 'cu' after sending a file), and give the
97 * box some time (100 * 1 ms)
98 */
99 for (i=0; i<100; ++i) {
100 if (tstc()) {
101 (void) getc();
102 }
103 udelay(1000);
104 }
105
106 if (addr == ~0) {
107 printf("## S-Record download aborted\n");
108 rcode = 1;
109 } else {
110 printf("## Start Addr = 0x%08lX\n", addr);
111 load_addr = addr;
112 }
113
114 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
115 if (load_baudrate != current_baudrate) {
116 printf("## Switch baudrate to %d bps and press ESC ...\n",
117 current_baudrate);
118 udelay(50000);
119 gd->baudrate = current_baudrate;
120 serial_setbrg();
121 udelay(50000);
122 for (;;) {
123 if (getc() == 0x1B) /* ESC */
124 break;
125 }
126 }
127 #endif
128 return rcode;
129 }
130
load_serial(long offset)131 static ulong load_serial(long offset)
132 {
133 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
134 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
135 int binlen; /* no. of data bytes in S-Rec. */
136 int type; /* return code for record type */
137 ulong addr; /* load address from S-Record */
138 ulong size; /* number of bytes transferred */
139 ulong store_addr;
140 ulong start_addr = ~0;
141 ulong end_addr = 0;
142 int line_count = 0;
143
144 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
145 type = srec_decode(record, &binlen, &addr, binbuf);
146
147 if (type < 0) {
148 return (~0); /* Invalid S-Record */
149 }
150
151 switch (type) {
152 case SREC_DATA2:
153 case SREC_DATA3:
154 case SREC_DATA4:
155 store_addr = addr + offset;
156 #ifdef CONFIG_MTD_NOR_FLASH
157 if (addr2info(store_addr)) {
158 int rc;
159
160 rc = flash_write((char *)binbuf,store_addr,binlen);
161 if (rc != 0) {
162 flash_perror(rc);
163 return (~0);
164 }
165 } else
166 #endif
167 {
168 memcpy((char *)(store_addr), binbuf, binlen);
169 }
170 if ((store_addr) < start_addr)
171 start_addr = store_addr;
172 if ((store_addr + binlen - 1) > end_addr)
173 end_addr = store_addr + binlen - 1;
174 break;
175 case SREC_END2:
176 case SREC_END3:
177 case SREC_END4:
178 udelay(10000);
179 size = end_addr - start_addr + 1;
180 printf("\n"
181 "## First Load Addr = 0x%08lX\n"
182 "## Last Load Addr = 0x%08lX\n"
183 "## Total Size = 0x%08lX = %ld Bytes\n",
184 start_addr, end_addr, size, size
185 );
186 flush_cache(start_addr, size);
187 env_set_hex("filesize", size);
188 return (addr);
189 case SREC_START:
190 break;
191 default:
192 break;
193 }
194 if (!do_echo) { /* print a '.' every 100 lines */
195 if ((++line_count % 100) == 0)
196 putc('.');
197 }
198 }
199
200 return (~0); /* Download aborted */
201 }
202
read_record(char * buf,ulong len)203 static int read_record(char *buf, ulong len)
204 {
205 char *p;
206 char c;
207
208 --len; /* always leave room for terminating '\0' byte */
209
210 for (p=buf; p < buf+len; ++p) {
211 c = getc(); /* read character */
212 if (do_echo)
213 putc(c); /* ... and echo it */
214
215 switch (c) {
216 case '\r':
217 case '\n':
218 *p = '\0';
219 return (p - buf);
220 case '\0':
221 case 0x03: /* ^C - Control C */
222 return (-1);
223 default:
224 *p = c;
225 }
226
227 /* Check for the console hangup (if any different from serial) */
228 if (gd->jt->getc != getc) {
229 if (ctrlc()) {
230 return (-1);
231 }
232 }
233 }
234
235 /* line too long - truncate */
236 *p = '\0';
237 return (p - buf);
238 }
239
240 #if defined(CONFIG_CMD_SAVES)
241
do_save_serial(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])242 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
243 {
244 ulong offset = 0;
245 ulong size = 0;
246 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
247 int save_baudrate, current_baudrate;
248
249 save_baudrate = current_baudrate = gd->baudrate;
250 #endif
251
252 if (argc >= 2) {
253 offset = simple_strtoul(argv[1], NULL, 16);
254 }
255 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
256 if (argc >= 3) {
257 size = simple_strtoul(argv[2], NULL, 16);
258 }
259 if (argc == 4) {
260 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
261
262 /* default to current baudrate */
263 if (save_baudrate == 0)
264 save_baudrate = current_baudrate;
265 }
266 if (save_baudrate != current_baudrate) {
267 printf("## Switch baudrate to %d bps and press ENTER ...\n",
268 save_baudrate);
269 udelay(50000);
270 gd->baudrate = save_baudrate;
271 serial_setbrg();
272 udelay(50000);
273 for (;;) {
274 if (getc() == '\r')
275 break;
276 }
277 }
278 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
279 if (argc == 3) {
280 size = simple_strtoul(argv[2], NULL, 16);
281 }
282 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
283
284 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
285 for (;;) {
286 if (getc() == '\r')
287 break;
288 }
289 if (save_serial(offset, size)) {
290 printf("## S-Record upload aborted\n");
291 } else {
292 printf("## S-Record upload complete\n");
293 }
294 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
295 if (save_baudrate != current_baudrate) {
296 printf("## Switch baudrate to %d bps and press ESC ...\n",
297 (int)current_baudrate);
298 udelay(50000);
299 gd->baudrate = current_baudrate;
300 serial_setbrg();
301 udelay(50000);
302 for (;;) {
303 if (getc() == 0x1B) /* ESC */
304 break;
305 }
306 }
307 #endif
308 return 0;
309 }
310
311 #define SREC3_START "S0030000FC\n"
312 #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
313 #define SREC3_END "S70500000000FA\n"
314 #define SREC_BYTES_PER_RECORD 16
315
save_serial(ulong address,ulong count)316 static int save_serial(ulong address, ulong count)
317 {
318 int i, c, reclen, checksum, length;
319 char *hex = "0123456789ABCDEF";
320 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
321 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
322
323 reclen = 0;
324 checksum = 0;
325
326 if(write_record(SREC3_START)) /* write the header */
327 return (-1);
328 do {
329 if(count) { /* collect hex data in the buffer */
330 c = *(volatile uchar*)(address + reclen); /* get one byte */
331 checksum += c; /* accumulate checksum */
332 data[2*reclen] = hex[(c>>4)&0x0f];
333 data[2*reclen+1] = hex[c & 0x0f];
334 data[2*reclen+2] = '\0';
335 ++reclen;
336 --count;
337 }
338 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
339 /* enough data collected for one record: dump it */
340 if(reclen) { /* build & write a data record: */
341 /* address + data + checksum */
342 length = 4 + reclen + 1;
343
344 /* accumulate length bytes into checksum */
345 for(i = 0; i < 2; i++)
346 checksum += (length >> (8*i)) & 0xff;
347
348 /* accumulate address bytes into checksum: */
349 for(i = 0; i < 4; i++)
350 checksum += (address >> (8*i)) & 0xff;
351
352 /* make proper checksum byte: */
353 checksum = ~checksum & 0xff;
354
355 /* output one record: */
356 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
357 if(write_record(record))
358 return (-1);
359 }
360 address += reclen; /* increment address */
361 checksum = 0;
362 reclen = 0;
363 }
364 }
365 while(count);
366 if(write_record(SREC3_END)) /* write the final record */
367 return (-1);
368 return(0);
369 }
370
write_record(char * buf)371 static int write_record(char *buf)
372 {
373 char c;
374
375 while((c = *buf++))
376 putc(c);
377
378 /* Check for the console hangup (if any different from serial) */
379
380 if (ctrlc()) {
381 return (-1);
382 }
383 return (0);
384 }
385 # endif
386
387 #endif
388
389
390 #if defined(CONFIG_CMD_LOADB)
391 /*
392 * loadb command (load binary) included
393 */
394 #define XON_CHAR 17
395 #define XOFF_CHAR 19
396 #define START_CHAR 0x01
397 #define ETX_CHAR 0x03
398 #define END_CHAR 0x0D
399 #define SPACE 0x20
400 #define K_ESCAPE 0x23
401 #define SEND_TYPE 'S'
402 #define DATA_TYPE 'D'
403 #define ACK_TYPE 'Y'
404 #define NACK_TYPE 'N'
405 #define BREAK_TYPE 'B'
406 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
407 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
408
409 static void set_kerm_bin_mode(unsigned long *);
410 static int k_recv(void);
411 static ulong load_serial_bin(ulong offset);
412
413
414 static char his_eol; /* character he needs at end of packet */
415 static int his_pad_count; /* number of pad chars he needs */
416 static char his_pad_char; /* pad chars he needs */
417 static char his_quote; /* quote chars he'll use */
418
do_load_serial_bin(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])419 static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
420 char * const argv[])
421 {
422 ulong offset = 0;
423 ulong addr;
424 int load_baudrate, current_baudrate;
425 int rcode = 0;
426 char *s;
427
428 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
429 offset = CONFIG_SYS_LOAD_ADDR;
430
431 /* pre-set offset from $loadaddr */
432 s = env_get("loadaddr");
433 if (s)
434 offset = simple_strtoul(s, NULL, 16);
435
436 load_baudrate = current_baudrate = gd->baudrate;
437
438 if (argc >= 2) {
439 offset = simple_strtoul(argv[1], NULL, 16);
440 }
441 if (argc == 3) {
442 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
443
444 /* default to current baudrate */
445 if (load_baudrate == 0)
446 load_baudrate = current_baudrate;
447 }
448
449 if (load_baudrate != current_baudrate) {
450 printf("## Switch baudrate to %d bps and press ENTER ...\n",
451 load_baudrate);
452 udelay(50000);
453 gd->baudrate = load_baudrate;
454 serial_setbrg();
455 udelay(50000);
456 for (;;) {
457 if (getc() == '\r')
458 break;
459 }
460 }
461
462 if (strcmp(argv[0],"loady")==0) {
463 printf("## Ready for binary (ymodem) download "
464 "to 0x%08lX at %d bps...\n",
465 offset,
466 load_baudrate);
467
468 addr = load_serial_ymodem(offset, xyzModem_ymodem);
469
470 } else if (strcmp(argv[0],"loadx")==0) {
471 printf("## Ready for binary (xmodem) download "
472 "to 0x%08lX at %d bps...\n",
473 offset,
474 load_baudrate);
475
476 addr = load_serial_ymodem(offset, xyzModem_xmodem);
477
478 } else {
479
480 printf("## Ready for binary (kermit) download "
481 "to 0x%08lX at %d bps...\n",
482 offset,
483 load_baudrate);
484 addr = load_serial_bin(offset);
485
486 if (addr == ~0) {
487 load_addr = 0;
488 printf("## Binary (kermit) download aborted\n");
489 rcode = 1;
490 } else {
491 printf("## Start Addr = 0x%08lX\n", addr);
492 load_addr = addr;
493 }
494 }
495 if (load_baudrate != current_baudrate) {
496 printf("## Switch baudrate to %d bps and press ESC ...\n",
497 current_baudrate);
498 udelay(50000);
499 gd->baudrate = current_baudrate;
500 serial_setbrg();
501 udelay(50000);
502 for (;;) {
503 if (getc() == 0x1B) /* ESC */
504 break;
505 }
506 }
507
508 return rcode;
509 }
510
511
load_serial_bin(ulong offset)512 static ulong load_serial_bin(ulong offset)
513 {
514 int size, i;
515
516 set_kerm_bin_mode((ulong *) offset);
517 size = k_recv();
518
519 /*
520 * Gather any trailing characters (for instance, the ^D which
521 * is sent by 'cu' after sending a file), and give the
522 * box some time (100 * 1 ms)
523 */
524 for (i=0; i<100; ++i) {
525 if (tstc()) {
526 (void) getc();
527 }
528 udelay(1000);
529 }
530
531 flush_cache(offset, size);
532
533 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
534 env_set_hex("filesize", size);
535
536 return offset;
537 }
538
send_pad(void)539 static void send_pad(void)
540 {
541 int count = his_pad_count;
542
543 while (count-- > 0)
544 putc(his_pad_char);
545 }
546
547 /* converts escaped kermit char to binary char */
ktrans(char in)548 static char ktrans(char in)
549 {
550 if ((in & 0x60) == 0x40) {
551 return (char) (in & ~0x40);
552 } else if ((in & 0x7f) == 0x3f) {
553 return (char) (in | 0x40);
554 } else
555 return in;
556 }
557
chk1(char * buffer)558 static int chk1(char *buffer)
559 {
560 int total = 0;
561
562 while (*buffer) {
563 total += *buffer++;
564 }
565 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
566 }
567
s1_sendpacket(char * packet)568 static void s1_sendpacket(char *packet)
569 {
570 send_pad();
571 while (*packet) {
572 putc(*packet++);
573 }
574 }
575
576 static char a_b[24];
send_ack(int n)577 static void send_ack(int n)
578 {
579 a_b[0] = START_CHAR;
580 a_b[1] = tochar(3);
581 a_b[2] = tochar(n);
582 a_b[3] = ACK_TYPE;
583 a_b[4] = '\0';
584 a_b[4] = tochar(chk1(&a_b[1]));
585 a_b[5] = his_eol;
586 a_b[6] = '\0';
587 s1_sendpacket(a_b);
588 }
589
send_nack(int n)590 static void send_nack(int n)
591 {
592 a_b[0] = START_CHAR;
593 a_b[1] = tochar(3);
594 a_b[2] = tochar(n);
595 a_b[3] = NACK_TYPE;
596 a_b[4] = '\0';
597 a_b[4] = tochar(chk1(&a_b[1]));
598 a_b[5] = his_eol;
599 a_b[6] = '\0';
600 s1_sendpacket(a_b);
601 }
602
603
604 static void (*os_data_init)(void);
605 static void (*os_data_char)(char new_char);
606 static int os_data_state, os_data_state_saved;
607 static char *os_data_addr, *os_data_addr_saved;
608 static char *bin_start_address;
609
bin_data_init(void)610 static void bin_data_init(void)
611 {
612 os_data_state = 0;
613 os_data_addr = bin_start_address;
614 }
615
os_data_save(void)616 static void os_data_save(void)
617 {
618 os_data_state_saved = os_data_state;
619 os_data_addr_saved = os_data_addr;
620 }
621
os_data_restore(void)622 static void os_data_restore(void)
623 {
624 os_data_state = os_data_state_saved;
625 os_data_addr = os_data_addr_saved;
626 }
627
bin_data_char(char new_char)628 static void bin_data_char(char new_char)
629 {
630 switch (os_data_state) {
631 case 0: /* data */
632 *os_data_addr++ = new_char;
633 break;
634 }
635 }
636
set_kerm_bin_mode(unsigned long * addr)637 static void set_kerm_bin_mode(unsigned long *addr)
638 {
639 bin_start_address = (char *) addr;
640 os_data_init = bin_data_init;
641 os_data_char = bin_data_char;
642 }
643
644
645 /* k_data_* simply handles the kermit escape translations */
646 static int k_data_escape, k_data_escape_saved;
k_data_init(void)647 static void k_data_init(void)
648 {
649 k_data_escape = 0;
650 os_data_init();
651 }
652
k_data_save(void)653 static void k_data_save(void)
654 {
655 k_data_escape_saved = k_data_escape;
656 os_data_save();
657 }
658
k_data_restore(void)659 static void k_data_restore(void)
660 {
661 k_data_escape = k_data_escape_saved;
662 os_data_restore();
663 }
664
k_data_char(char new_char)665 static void k_data_char(char new_char)
666 {
667 if (k_data_escape) {
668 /* last char was escape - translate this character */
669 os_data_char(ktrans(new_char));
670 k_data_escape = 0;
671 } else {
672 if (new_char == his_quote) {
673 /* this char is escape - remember */
674 k_data_escape = 1;
675 } else {
676 /* otherwise send this char as-is */
677 os_data_char(new_char);
678 }
679 }
680 }
681
682 #define SEND_DATA_SIZE 20
683 static char send_parms[SEND_DATA_SIZE];
684 static char *send_ptr;
685
686 /* handle_send_packet interprits the protocol info and builds and
687 sends an appropriate ack for what we can do */
handle_send_packet(int n)688 static void handle_send_packet(int n)
689 {
690 int length = 3;
691 int bytes;
692
693 /* initialize some protocol parameters */
694 his_eol = END_CHAR; /* default end of line character */
695 his_pad_count = 0;
696 his_pad_char = '\0';
697 his_quote = K_ESCAPE;
698
699 /* ignore last character if it filled the buffer */
700 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
701 --send_ptr;
702 bytes = send_ptr - send_parms; /* how many bytes we'll process */
703 do {
704 if (bytes-- <= 0)
705 break;
706 /* handle MAXL - max length */
707 /* ignore what he says - most I'll take (here) is 94 */
708 a_b[++length] = tochar(94);
709 if (bytes-- <= 0)
710 break;
711 /* handle TIME - time you should wait for my packets */
712 /* ignore what he says - don't wait for my ack longer than 1 second */
713 a_b[++length] = tochar(1);
714 if (bytes-- <= 0)
715 break;
716 /* handle NPAD - number of pad chars I need */
717 /* remember what he says - I need none */
718 his_pad_count = untochar(send_parms[2]);
719 a_b[++length] = tochar(0);
720 if (bytes-- <= 0)
721 break;
722 /* handle PADC - pad chars I need */
723 /* remember what he says - I need none */
724 his_pad_char = ktrans(send_parms[3]);
725 a_b[++length] = 0x40; /* He should ignore this */
726 if (bytes-- <= 0)
727 break;
728 /* handle EOL - end of line he needs */
729 /* remember what he says - I need CR */
730 his_eol = untochar(send_parms[4]);
731 a_b[++length] = tochar(END_CHAR);
732 if (bytes-- <= 0)
733 break;
734 /* handle QCTL - quote control char he'll use */
735 /* remember what he says - I'll use '#' */
736 his_quote = send_parms[5];
737 a_b[++length] = '#';
738 if (bytes-- <= 0)
739 break;
740 /* handle QBIN - 8-th bit prefixing */
741 /* ignore what he says - I refuse */
742 a_b[++length] = 'N';
743 if (bytes-- <= 0)
744 break;
745 /* handle CHKT - the clock check type */
746 /* ignore what he says - I do type 1 (for now) */
747 a_b[++length] = '1';
748 if (bytes-- <= 0)
749 break;
750 /* handle REPT - the repeat prefix */
751 /* ignore what he says - I refuse (for now) */
752 a_b[++length] = 'N';
753 if (bytes-- <= 0)
754 break;
755 /* handle CAPAS - the capabilities mask */
756 /* ignore what he says - I only do long packets - I don't do windows */
757 a_b[++length] = tochar(2); /* only long packets */
758 a_b[++length] = tochar(0); /* no windows */
759 a_b[++length] = tochar(94); /* large packet msb */
760 a_b[++length] = tochar(94); /* large packet lsb */
761 } while (0);
762
763 a_b[0] = START_CHAR;
764 a_b[1] = tochar(length);
765 a_b[2] = tochar(n);
766 a_b[3] = ACK_TYPE;
767 a_b[++length] = '\0';
768 a_b[length] = tochar(chk1(&a_b[1]));
769 a_b[++length] = his_eol;
770 a_b[++length] = '\0';
771 s1_sendpacket(a_b);
772 }
773
774 /* k_recv receives a OS Open image file over kermit line */
k_recv(void)775 static int k_recv(void)
776 {
777 char new_char;
778 char k_state, k_state_saved;
779 int sum;
780 int done;
781 int length;
782 int n, last_n;
783 int len_lo, len_hi;
784
785 /* initialize some protocol parameters */
786 his_eol = END_CHAR; /* default end of line character */
787 his_pad_count = 0;
788 his_pad_char = '\0';
789 his_quote = K_ESCAPE;
790
791 /* initialize the k_recv and k_data state machine */
792 done = 0;
793 k_state = 0;
794 k_data_init();
795 k_state_saved = k_state;
796 k_data_save();
797 n = 0; /* just to get rid of a warning */
798 last_n = -1;
799
800 /* expect this "type" sequence (but don't check):
801 S: send initiate
802 F: file header
803 D: data (multiple)
804 Z: end of file
805 B: break transmission
806 */
807
808 /* enter main loop */
809 while (!done) {
810 /* set the send packet pointer to begining of send packet parms */
811 send_ptr = send_parms;
812
813 /* With each packet, start summing the bytes starting with the length.
814 Save the current sequence number.
815 Note the type of the packet.
816 If a character less than SPACE (0x20) is received - error.
817 */
818
819 #if 0
820 /* OLD CODE, Prior to checking sequence numbers */
821 /* first have all state machines save current states */
822 k_state_saved = k_state;
823 k_data_save ();
824 #endif
825
826 /* get a packet */
827 /* wait for the starting character or ^C */
828 for (;;) {
829 switch (getc ()) {
830 case START_CHAR: /* start packet */
831 goto START;
832 case ETX_CHAR: /* ^C waiting for packet */
833 return (0);
834 default:
835 ;
836 }
837 }
838 START:
839 /* get length of packet */
840 sum = 0;
841 new_char = getc();
842 if ((new_char & 0xE0) == 0)
843 goto packet_error;
844 sum += new_char & 0xff;
845 length = untochar(new_char);
846 /* get sequence number */
847 new_char = getc();
848 if ((new_char & 0xE0) == 0)
849 goto packet_error;
850 sum += new_char & 0xff;
851 n = untochar(new_char);
852 --length;
853
854 /* NEW CODE - check sequence numbers for retried packets */
855 /* Note - this new code assumes that the sequence number is correctly
856 * received. Handling an invalid sequence number adds another layer
857 * of complexity that may not be needed - yet! At this time, I'm hoping
858 * that I don't need to buffer the incoming data packets and can write
859 * the data into memory in real time.
860 */
861 if (n == last_n) {
862 /* same sequence number, restore the previous state */
863 k_state = k_state_saved;
864 k_data_restore();
865 } else {
866 /* new sequence number, checkpoint the download */
867 last_n = n;
868 k_state_saved = k_state;
869 k_data_save();
870 }
871 /* END NEW CODE */
872
873 /* get packet type */
874 new_char = getc();
875 if ((new_char & 0xE0) == 0)
876 goto packet_error;
877 sum += new_char & 0xff;
878 k_state = new_char;
879 --length;
880 /* check for extended length */
881 if (length == -2) {
882 /* (length byte was 0, decremented twice) */
883 /* get the two length bytes */
884 new_char = getc();
885 if ((new_char & 0xE0) == 0)
886 goto packet_error;
887 sum += new_char & 0xff;
888 len_hi = untochar(new_char);
889 new_char = getc();
890 if ((new_char & 0xE0) == 0)
891 goto packet_error;
892 sum += new_char & 0xff;
893 len_lo = untochar(new_char);
894 length = len_hi * 95 + len_lo;
895 /* check header checksum */
896 new_char = getc();
897 if ((new_char & 0xE0) == 0)
898 goto packet_error;
899 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
900 goto packet_error;
901 sum += new_char & 0xff;
902 /* --length; */ /* new length includes only data and block check to come */
903 }
904 /* bring in rest of packet */
905 while (length > 1) {
906 new_char = getc();
907 if ((new_char & 0xE0) == 0)
908 goto packet_error;
909 sum += new_char & 0xff;
910 --length;
911 if (k_state == DATA_TYPE) {
912 /* pass on the data if this is a data packet */
913 k_data_char (new_char);
914 } else if (k_state == SEND_TYPE) {
915 /* save send pack in buffer as is */
916 *send_ptr++ = new_char;
917 /* if too much data, back off the pointer */
918 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
919 --send_ptr;
920 }
921 }
922 /* get and validate checksum character */
923 new_char = getc();
924 if ((new_char & 0xE0) == 0)
925 goto packet_error;
926 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
927 goto packet_error;
928 /* get END_CHAR */
929 new_char = getc();
930 if (new_char != END_CHAR) {
931 packet_error:
932 /* restore state machines */
933 k_state = k_state_saved;
934 k_data_restore();
935 /* send a negative acknowledge packet in */
936 send_nack(n);
937 } else if (k_state == SEND_TYPE) {
938 /* crack the protocol parms, build an appropriate ack packet */
939 handle_send_packet(n);
940 } else {
941 /* send simple acknowledge packet in */
942 send_ack(n);
943 /* quit if end of transmission */
944 if (k_state == BREAK_TYPE)
945 done = 1;
946 }
947 }
948 return ((ulong) os_data_addr - (ulong) bin_start_address);
949 }
950
getcxmodem(void)951 static int getcxmodem(void) {
952 if (tstc())
953 return (getc());
954 return -1;
955 }
load_serial_ymodem(ulong offset,int mode)956 static ulong load_serial_ymodem(ulong offset, int mode)
957 {
958 int size;
959 int err;
960 int res;
961 connection_info_t info;
962 char ymodemBuf[1024];
963 ulong store_addr = ~0;
964 ulong addr = 0;
965
966 size = 0;
967 info.mode = mode;
968 res = xyzModem_stream_open(&info, &err);
969 if (!res) {
970
971 while ((res =
972 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
973 store_addr = addr + offset;
974 size += res;
975 addr += res;
976 #ifdef CONFIG_MTD_NOR_FLASH
977 if (addr2info(store_addr)) {
978 int rc;
979
980 rc = flash_write((char *) ymodemBuf,
981 store_addr, res);
982 if (rc != 0) {
983 flash_perror (rc);
984 return (~0);
985 }
986 } else
987 #endif
988 {
989 memcpy((char *)(store_addr), ymodemBuf,
990 res);
991 }
992
993 }
994 } else {
995 printf("%s\n", xyzModem_error(err));
996 }
997
998 xyzModem_stream_close(&err);
999 xyzModem_stream_terminate(false, &getcxmodem);
1000
1001
1002 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
1003
1004 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
1005 env_set_hex("filesize", size);
1006
1007 return offset;
1008 }
1009
1010 #endif
1011
1012 /* -------------------------------------------------------------------- */
1013
1014 #if defined(CONFIG_CMD_LOADS)
1015
1016 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1017 U_BOOT_CMD(
1018 loads, 3, 0, do_load_serial,
1019 "load S-Record file over serial line",
1020 "[ off ] [ baud ]\n"
1021 " - load S-Record file over serial line"
1022 " with offset 'off' and baudrate 'baud'"
1023 );
1024
1025 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1026 U_BOOT_CMD(
1027 loads, 2, 0, do_load_serial,
1028 "load S-Record file over serial line",
1029 "[ off ]\n"
1030 " - load S-Record file over serial line with offset 'off'"
1031 );
1032 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1033
1034 /*
1035 * SAVES always requires LOADS support, but not vice versa
1036 */
1037
1038
1039 #if defined(CONFIG_CMD_SAVES)
1040 #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
1041 U_BOOT_CMD(
1042 saves, 4, 0, do_save_serial,
1043 "save S-Record file over serial line",
1044 "[ off ] [size] [ baud ]\n"
1045 " - save S-Record file over serial line"
1046 " with offset 'off', size 'size' and baudrate 'baud'"
1047 );
1048 #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
1049 U_BOOT_CMD(
1050 saves, 3, 0, do_save_serial,
1051 "save S-Record file over serial line",
1052 "[ off ] [size]\n"
1053 " - save S-Record file over serial line with offset 'off' and size 'size'"
1054 );
1055 #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
1056 #endif /* CONFIG_CMD_SAVES */
1057 #endif /* CONFIG_CMD_LOADS */
1058
1059
1060 #if defined(CONFIG_CMD_LOADB)
1061 U_BOOT_CMD(
1062 loadb, 3, 0, do_load_serial_bin,
1063 "load binary file over serial line (kermit mode)",
1064 "[ off ] [ baud ]\n"
1065 " - load binary file over serial line"
1066 " with offset 'off' and baudrate 'baud'"
1067 );
1068
1069 U_BOOT_CMD(
1070 loadx, 3, 0, do_load_serial_bin,
1071 "load binary file over serial line (xmodem mode)",
1072 "[ off ] [ baud ]\n"
1073 " - load binary file over serial line"
1074 " with offset 'off' and baudrate 'baud'"
1075 );
1076
1077 U_BOOT_CMD(
1078 loady, 3, 0, do_load_serial_bin,
1079 "load binary file over serial line (ymodem mode)",
1080 "[ off ] [ baud ]\n"
1081 " - load binary file over serial line"
1082 " with offset 'off' and baudrate 'baud'"
1083 );
1084
1085 #endif /* CONFIG_CMD_LOADB */
1086