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