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