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