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