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