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