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