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