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