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