]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_boot.c
* Get (mostly) rid of CFG_MONITOR_LEN definition; compute real length
[people/ms/u-boot.git] / common / cmd_boot.c
CommitLineData
fe8c2806
WD
1/*
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
4d75a504 53#ifdef CONFIG_PPC
fe8c2806
WD
54static 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 );
0db5bca8 74#if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260)
fe8c2806
WD
75 print_num ("immr_base", bd->bi_immr_base );
76#endif
77 print_num ("bootflags", bd->bi_bootflags );
b867d705 78#if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_405EP)
fe8c2806
WD
79 print_str ("procfreq", strmhz(buf, bd->bi_procfreq));
80 print_str ("plb_busfreq", strmhz(buf, bd->bi_plb_busfreq));
b867d705 81#if defined(CONFIG_405GP) || defined(CONFIG_405EP)
fe8c2806
WD
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
4d75a504
WD
114#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
fe8c2806
WD
142#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) {
dc7c9a1a
WD
156 print_num("DRAM bank", i);
157 print_num("-> start", bd->bi_dram[i].start);
158 print_num("-> size", bd->bi_dram[i].size);
fe8c2806
WD
159 }
160
161 printf ("ethaddr =");
162 for (i=0; i<6; ++i) {
163 printf ("%c%02X", i ? ':' : ' ', bd->bi_enetaddr[i]);
164 }
165 printf ("\n"
85ec0bcc 166 "ip_addr = ");
fe8c2806
WD
167 print_IPaddr (bd->bi_ip_addr);
168 printf ("\n"
85ec0bcc 169 "baudrate = %d bps\n", bd->bi_baudrate);
fe8c2806
WD
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
4d75a504 181#ifdef CONFIG_PPC
fe8c2806
WD
182static void print_str(const char *name, const char *str)
183{
184 printf ("%-12s= %6s MHz\n", name, str);
185}
4d75a504 186#endif /* CONFIG_PPC */
fe8c2806
WD
187
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
dc7c9a1a 202 printf ("## Starting application at 0x%08lX ...\n", addr);
fe8c2806
WD
203
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
dc7c9a1a 211 printf ("## Application terminated, rc = 0x%lX\n", rc);
fe8c2806
WD
212 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 {
dc7c9a1a 287 printf ("## Start Addr = 0x%08lX\n", addr);
fe8c2806
WD
288 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;
59de2ed6 335#ifndef CFG_NO_FLASH
fe8c2806
WD
336 if (addr2info(store_addr)) {
337 int rc;
338
339 rc = flash_write((uchar *)binbuf,store_addr,binlen);
340 if (rc != 0) {
341 flash_perror (rc);
342 return (~0);
343 }
59de2ed6
WD
344 } else
345#endif
346 {
fe8c2806
WD
347 memcpy ((char *)(store_addr), binbuf, binlen);
348 }
349 if ((store_addr) < start_addr)
dc7c9a1a 350 start_addr = store_addr;
fe8c2806 351 if ((store_addr + binlen - 1) > end_addr)
dc7c9a1a 352 end_addr = store_addr + binlen - 1;
fe8c2806
WD
353 break;
354 case SREC_END2:
355 case SREC_END3:
356 case SREC_END4:
357 udelay (10000);
358 size = end_addr - start_addr + 1;
359 printf ("\n"
360 "## First Load Addr = 0x%08lX\n"
361 "## Last Load Addr = 0x%08lX\n"
362 "## Total Size = 0x%08lX = %ld Bytes\n",
363 start_addr, end_addr, size, size
364 );
365 flush_cache (addr, size);
366 sprintf(buf, "%lX", size);
367 setenv("filesize", buf);
368 return (addr);
369 case SREC_START:
370 break;
371 default:
372 break;
373 }
374 if (!do_echo) { /* print a '.' every 100 lines */
375 if ((++line_count % 100) == 0)
376 putc ('.');
377 }
378 }
379
380 return (~0); /* Download aborted */
381}
382
383static int
384read_record (char *buf, ulong len)
385{
386 char *p;
387 char c;
388
389 --len; /* always leave room for terminating '\0' byte */
390
391 for (p=buf; p < buf+len; ++p) {
392 c = serial_getc(); /* read character */
393 if (do_echo)
394 serial_putc (c); /* ... and echo it */
395
396 switch (c) {
397 case '\r':
398 case '\n':
399 *p = '\0';
400 return (p - buf);
401 case '\0':
402 case 0x03: /* ^C - Control C */
403 return (-1);
404 default:
405 *p = c;
406 }
407
408 /* Check for the console hangup (if any different from serial) */
409
410 if (syscall_tbl[SYSCALL_GETC] != serial_getc) {
411 if (ctrlc()) {
412 return (-1);
413 }
414 }
415 }
416
417 /* line too long - truncate */
418 *p = '\0';
419 return (p - buf);
420}
421
422#if (CONFIG_COMMANDS & CFG_CMD_SAVES)
423
424int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
425{
426 ulong offset = 0;
427 ulong size = 0;
428#ifdef CFG_LOADS_BAUD_CHANGE
429 DECLARE_GLOBAL_DATA_PTR;
430 int save_baudrate, current_baudrate;
431
432 save_baudrate = current_baudrate = gd->baudrate;
433#endif
434
435 if (argc >= 2) {
436 offset = simple_strtoul(argv[1], NULL, 16);
437 }
438#ifdef CFG_LOADS_BAUD_CHANGE
439 if (argc >= 3) {
440 size = simple_strtoul(argv[2], NULL, 16);
441 }
442 if (argc == 4) {
443 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
444
445 /* default to current baudrate */
446 if (save_baudrate == 0)
447 save_baudrate = current_baudrate;
448 }
449#else /* ! CFG_LOADS_BAUD_CHANGE */
450 if (argc == 3) {
451 size = simple_strtoul(argv[2], NULL, 16);
452 }
453#endif /* CFG_LOADS_BAUD_CHANGE */
454
455#ifdef CFG_LOADS_BAUD_CHANGE
456 if (save_baudrate != current_baudrate) {
457 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
458 save_baudrate);
459 udelay(50000);
460 gd->baudrate = save_baudrate;
461 serial_setbrg ();
462 udelay(50000);
463 for (;;) {
464 if (getc() == '\r')
465 break;
466 }
467 }
468#endif /* CFG_LOADS_BAUD_CHANGE */
469 printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
470 for (;;) {
471 if (getc() == '\r')
472 break;
473 }
474 if(save_serial (offset, size)) {
475 printf ("## S-Record upload aborted\n");
476 } else {
477 printf ("## S-Record upload complete\n");
478 }
479#ifdef CFG_LOADS_BAUD_CHANGE
480 if (save_baudrate != current_baudrate) {
481 printf ("## Switch baudrate to %d bps and press ESC ...\n",
482 (int)current_baudrate);
483 udelay (50000);
484 gd->baudrate = current_baudrate;
485 serial_setbrg ();
486 udelay (50000);
487 for (;;) {
488 if (getc() == 0x1B) /* ESC */
489 break;
490 }
491 }
492#endif
493 return 0;
494}
495
496#define SREC3_START "S0030000FC\n"
497#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
498#define SREC3_END "S70500000000FA\n"
499#define SREC_BYTES_PER_RECORD 16
500
501static int save_serial (ulong address, ulong count)
502{
503 int i, c, reclen, checksum, length;
504 char *hex = "0123456789ABCDEF";
505 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
506 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
507
508 reclen = 0;
509 checksum = 0;
510
511 if(write_record(SREC3_START)) /* write the header */
512 return (-1);
513 do {
514 if(count) { /* collect hex data in the buffer */
515 c = *(volatile uchar*)(address + reclen); /* get one byte */
516 checksum += c; /* accumulate checksum */
517 data[2*reclen] = hex[(c>>4)&0x0f];
518 data[2*reclen+1] = hex[c & 0x0f];
519 data[2*reclen+2] = '\0';
520 ++reclen;
521 --count;
522 }
523 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
524 /* enough data collected for one record: dump it */
525 if(reclen) { /* build & write a data record: */
526 /* address + data + checksum */
527 length = 4 + reclen + 1;
528
529 /* accumulate length bytes into checksum */
530 for(i = 0; i < 2; i++)
531 checksum += (length >> (8*i)) & 0xff;
532
533 /* accumulate address bytes into checksum: */
534 for(i = 0; i < 4; i++)
535 checksum += (address >> (8*i)) & 0xff;
536
537 /* make proper checksum byte: */
538 checksum = ~checksum & 0xff;
539
540 /* output one record: */
541 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
542 if(write_record(record))
543 return (-1);
544 }
545 address += reclen; /* increment address */
546 checksum = 0;
547 reclen = 0;
548 }
549 }
550 while(count);
551 if(write_record(SREC3_END)) /* write the final record */
552 return (-1);
553 return(0);
554}
555
556static int
557write_record (char *buf)
558{
559 char c;
560
561 while((c = *buf++))
562 serial_putc(c);
563
564 /* Check for the console hangup (if any different from serial) */
565
566 if (ctrlc()) {
567 return (-1);
568 }
569 return (0);
570}
571# endif /* CFG_CMD_SAVES */
572
573#endif /* CFG_CMD_LOADS */
574
575
576#if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
577
578#define XON_CHAR 17
579#define XOFF_CHAR 19
580#define START_CHAR 0x01
85ec0bcc 581#define ETX_CHAR 0x03
fe8c2806
WD
582#define END_CHAR 0x0D
583#define SPACE 0x20
584#define K_ESCAPE 0x23
585#define SEND_TYPE 'S'
586#define DATA_TYPE 'D'
587#define ACK_TYPE 'Y'
588#define NACK_TYPE 'N'
589#define BREAK_TYPE 'B'
590#define tochar(x) ((char) (((x) + SPACE) & 0xff))
591#define untochar(x) ((int) (((x) - SPACE) & 0xff))
592
593extern int os_data_count;
594extern int os_data_header[8];
595
596static void set_kerm_bin_mode(unsigned long *);
597static int k_recv(void);
598static ulong load_serial_bin (ulong offset);
599
600
601char his_eol; /* character he needs at end of packet */
602int his_pad_count; /* number of pad chars he needs */
603char his_pad_char; /* pad chars he needs */
604char his_quote; /* quote chars he'll use */
605
606int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
607{
608 DECLARE_GLOBAL_DATA_PTR;
609
610 ulong offset = 0;
611 ulong addr;
fe8c2806
WD
612 int load_baudrate, current_baudrate;
613 int rcode = 0;
dc7c9a1a
WD
614 char *s;
615
616 /* pre-set offset from CFG_LOAD_ADDR */
617 offset = CFG_LOAD_ADDR;
618
619 /* pre-set offset from $loadaddr */
620 if ((s = getenv("loadaddr")) != NULL) {
621 offset = simple_strtoul(s, NULL, 16);
622 }
fe8c2806
WD
623
624 load_baudrate = current_baudrate = gd->baudrate;
625
626 if (argc >= 2) {
627 offset = simple_strtoul(argv[1], NULL, 16);
628 }
629 if (argc == 3) {
630 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
631
632 /* default to current baudrate */
633 if (load_baudrate == 0)
634 load_baudrate = current_baudrate;
635 }
636
637 if (load_baudrate != current_baudrate) {
638 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
639 load_baudrate);
640 udelay(50000);
641 gd->baudrate = load_baudrate;
642 serial_setbrg ();
643 udelay(50000);
644 for (;;) {
645 if (getc() == '\r')
646 break;
647 }
648 }
fe8c2806 649
dc7c9a1a
WD
650 printf ("## Ready for binary (kermit) download "
651 "to 0x%08lX at %d bps...\n",
652 offset,
653 current_baudrate);
fe8c2806
WD
654 addr = load_serial_bin (offset);
655
fe8c2806
WD
656 if (addr == ~0) {
657 load_addr = 0;
658 printf ("## Binary (kermit) download aborted\n");
659 rcode = 1;
660 } else {
dc7c9a1a 661 printf ("## Start Addr = 0x%08lX\n", addr);
fe8c2806
WD
662 load_addr = addr;
663 }
664
665 if (load_baudrate != current_baudrate) {
666 printf ("## Switch baudrate to %d bps and press ESC ...\n",
667 current_baudrate);
668 udelay (50000);
669 gd->baudrate = current_baudrate;
670 serial_setbrg ();
671 udelay (50000);
672 for (;;) {
673 if (getc() == 0x1B) /* ESC */
674 break;
675 }
676 }
677
678#ifdef CONFIG_AUTOSCRIPT
679 if (load_addr) {
680 char *s;
681
682 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
683 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
684 rcode = autoscript (load_addr);
685 }
686 }
687#endif
688 return rcode;
689}
690
691
692static ulong load_serial_bin (ulong offset)
693{
694 int size;
695 char buf[32];
696
697 set_kerm_bin_mode ((ulong *) offset);
698 size = k_recv ();
699 flush_cache (offset, size);
700
701 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
702 sprintf(buf, "%X", size);
703 setenv("filesize", buf);
704
705 return offset;
706}
707
708void send_pad (void)
709{
710 int count = his_pad_count;
711
712 while (count-- > 0)
713 serial_putc (his_pad_char);
714}
715
716/* converts escaped kermit char to binary char */
717char ktrans (char in)
718{
719 if ((in & 0x60) == 0x40) {
720 return (char) (in & ~0x40);
721 } else if ((in & 0x7f) == 0x3f) {
722 return (char) (in | 0x40);
723 } else
724 return in;
725}
726
727int chk1 (char *buffer)
728{
729 int total = 0;
730
731 while (*buffer) {
732 total += *buffer++;
733 }
734 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
735}
736
737void s1_sendpacket (char *packet)
738{
739 send_pad ();
740 while (*packet) {
741 serial_putc (*packet++);
742 }
743}
744
745static char a_b[24];
746void send_ack (int n)
747{
748 a_b[0] = START_CHAR;
749 a_b[1] = tochar (3);
750 a_b[2] = tochar (n);
751 a_b[3] = ACK_TYPE;
752 a_b[4] = '\0';
753 a_b[4] = tochar (chk1 (&a_b[1]));
754 a_b[5] = his_eol;
755 a_b[6] = '\0';
756 s1_sendpacket (a_b);
757}
758
759void send_nack (int n)
760{
761 a_b[0] = START_CHAR;
762 a_b[1] = tochar (3);
763 a_b[2] = tochar (n);
764 a_b[3] = NACK_TYPE;
765 a_b[4] = '\0';
766 a_b[4] = tochar (chk1 (&a_b[1]));
767 a_b[5] = his_eol;
768 a_b[6] = '\0';
769 s1_sendpacket (a_b);
770}
771
772
773
774/* os_data_* takes an OS Open image and puts it into memory, and
775 puts the boot header in an array named os_data_header
776
777 if image is binary, no header is stored in os_data_header.
778*/
779void (*os_data_init) (void);
780void (*os_data_char) (char new_char);
781static int os_data_state, os_data_state_saved;
782int os_data_count;
783static int os_data_count_saved;
784static char *os_data_addr, *os_data_addr_saved;
785static char *bin_start_address;
786int os_data_header[8];
787static void bin_data_init (void)
788{
789 os_data_state = 0;
790 os_data_count = 0;
791 os_data_addr = bin_start_address;
792}
793static void os_data_save (void)
794{
795 os_data_state_saved = os_data_state;
796 os_data_count_saved = os_data_count;
797 os_data_addr_saved = os_data_addr;
798}
799static void os_data_restore (void)
800{
801 os_data_state = os_data_state_saved;
802 os_data_count = os_data_count_saved;
803 os_data_addr = os_data_addr_saved;
804}
805static void bin_data_char (char new_char)
806{
807 switch (os_data_state) {
808 case 0: /* data */
809 *os_data_addr++ = new_char;
810 --os_data_count;
811 break;
812 }
813}
814static void set_kerm_bin_mode (unsigned long *addr)
815{
816 bin_start_address = (char *) addr;
817 os_data_init = bin_data_init;
818 os_data_char = bin_data_char;
819}
820
821
822/* k_data_* simply handles the kermit escape translations */
823static int k_data_escape, k_data_escape_saved;
824void k_data_init (void)
825{
826 k_data_escape = 0;
827 os_data_init ();
828}
829void k_data_save (void)
830{
831 k_data_escape_saved = k_data_escape;
832 os_data_save ();
833}
834void k_data_restore (void)
835{
836 k_data_escape = k_data_escape_saved;
837 os_data_restore ();
838}
839void k_data_char (char new_char)
840{
841 if (k_data_escape) {
842 /* last char was escape - translate this character */
843 os_data_char (ktrans (new_char));
844 k_data_escape = 0;
845 } else {
846 if (new_char == his_quote) {
847 /* this char is escape - remember */
848 k_data_escape = 1;
849 } else {
850 /* otherwise send this char as-is */
851 os_data_char (new_char);
852 }
853 }
854}
855
856#define SEND_DATA_SIZE 20
857char send_parms[SEND_DATA_SIZE];
858char *send_ptr;
859
860/* handle_send_packet interprits the protocol info and builds and
861 sends an appropriate ack for what we can do */
862void handle_send_packet (int n)
863{
864 int length = 3;
865 int bytes;
866
867 /* initialize some protocol parameters */
868 his_eol = END_CHAR; /* default end of line character */
869 his_pad_count = 0;
870 his_pad_char = '\0';
871 his_quote = K_ESCAPE;
872
873 /* ignore last character if it filled the buffer */
874 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
875 --send_ptr;
876 bytes = send_ptr - send_parms; /* how many bytes we'll process */
877 do {
878 if (bytes-- <= 0)
879 break;
880 /* handle MAXL - max length */
881 /* ignore what he says - most I'll take (here) is 94 */
882 a_b[++length] = tochar (94);
883 if (bytes-- <= 0)
884 break;
885 /* handle TIME - time you should wait for my packets */
886 /* ignore what he says - don't wait for my ack longer than 1 second */
887 a_b[++length] = tochar (1);
888 if (bytes-- <= 0)
889 break;
890 /* handle NPAD - number of pad chars I need */
891 /* remember what he says - I need none */
892 his_pad_count = untochar (send_parms[2]);
893 a_b[++length] = tochar (0);
894 if (bytes-- <= 0)
895 break;
896 /* handle PADC - pad chars I need */
897 /* remember what he says - I need none */
898 his_pad_char = ktrans (send_parms[3]);
899 a_b[++length] = 0x40; /* He should ignore this */
900 if (bytes-- <= 0)
901 break;
902 /* handle EOL - end of line he needs */
903 /* remember what he says - I need CR */
904 his_eol = untochar (send_parms[4]);
905 a_b[++length] = tochar (END_CHAR);
906 if (bytes-- <= 0)
907 break;
908 /* handle QCTL - quote control char he'll use */
909 /* remember what he says - I'll use '#' */
910 his_quote = send_parms[5];
911 a_b[++length] = '#';
912 if (bytes-- <= 0)
913 break;
914 /* handle QBIN - 8-th bit prefixing */
915 /* ignore what he says - I refuse */
916 a_b[++length] = 'N';
917 if (bytes-- <= 0)
918 break;
919 /* handle CHKT - the clock check type */
920 /* ignore what he says - I do type 1 (for now) */
921 a_b[++length] = '1';
922 if (bytes-- <= 0)
923 break;
924 /* handle REPT - the repeat prefix */
925 /* ignore what he says - I refuse (for now) */
926 a_b[++length] = 'N';
927 if (bytes-- <= 0)
928 break;
929 /* handle CAPAS - the capabilities mask */
930 /* ignore what he says - I only do long packets - I don't do windows */
931 a_b[++length] = tochar (2); /* only long packets */
932 a_b[++length] = tochar (0); /* no windows */
933 a_b[++length] = tochar (94); /* large packet msb */
934 a_b[++length] = tochar (94); /* large packet lsb */
935 } while (0);
936
937 a_b[0] = START_CHAR;
938 a_b[1] = tochar (length);
939 a_b[2] = tochar (n);
940 a_b[3] = ACK_TYPE;
941 a_b[++length] = '\0';
942 a_b[length] = tochar (chk1 (&a_b[1]));
943 a_b[++length] = his_eol;
944 a_b[++length] = '\0';
945 s1_sendpacket (a_b);
946}
947
948/* k_recv receives a OS Open image file over kermit line */
949static int k_recv (void)
950{
951 char new_char;
952 char k_state, k_state_saved;
953 int sum;
954 int done;
955 int length;
956 int n, last_n;
957 int z = 0;
958 int len_lo, len_hi;
959
960 /* initialize some protocol parameters */
961 his_eol = END_CHAR; /* default end of line character */
962 his_pad_count = 0;
963 his_pad_char = '\0';
964 his_quote = K_ESCAPE;
965
966 /* initialize the k_recv and k_data state machine */
967 done = 0;
968 k_state = 0;
969 k_data_init ();
970 k_state_saved = k_state;
971 k_data_save ();
972 n = 0; /* just to get rid of a warning */
973 last_n = -1;
974
975 /* expect this "type" sequence (but don't check):
976 S: send initiate
977 F: file header
978 D: data (multiple)
979 Z: end of file
980 B: break transmission
981 */
982
983 /* enter main loop */
984 while (!done) {
985 /* set the send packet pointer to begining of send packet parms */
986 send_ptr = send_parms;
987
988 /* With each packet, start summing the bytes starting with the length.
989 Save the current sequence number.
990 Note the type of the packet.
991 If a character less than SPACE (0x20) is received - error.
992 */
993
994#if 0
995 /* OLD CODE, Prior to checking sequence numbers */
996 /* first have all state machines save current states */
997 k_state_saved = k_state;
998 k_data_save ();
999#endif
1000
1001 /* get a packet */
85ec0bcc
WD
1002 /* wait for the starting character or ^C */
1003 for (;;) {
1004 switch (serial_getc ()) {
1005 case START_CHAR: /* start packet */
60fbe254 1006 goto START;
85ec0bcc
WD
1007 case ETX_CHAR: /* ^C waiting for packet */
1008 return (0);
1009 default:
1010 ;
1011 }
1012 }
60fbe254 1013START:
fe8c2806
WD
1014 /* get length of packet */
1015 sum = 0;
1016 new_char = serial_getc ();
1017 if ((new_char & 0xE0) == 0)
1018 goto packet_error;
1019 sum += new_char & 0xff;
1020 length = untochar (new_char);
1021 /* get sequence number */
1022 new_char = serial_getc ();
1023 if ((new_char & 0xE0) == 0)
1024 goto packet_error;
1025 sum += new_char & 0xff;
1026 n = untochar (new_char);
1027 --length;
1028
1029 /* NEW CODE - check sequence numbers for retried packets */
1030 /* Note - this new code assumes that the sequence number is correctly
1031 * received. Handling an invalid sequence number adds another layer
1032 * of complexity that may not be needed - yet! At this time, I'm hoping
1033 * that I don't need to buffer the incoming data packets and can write
1034 * the data into memory in real time.
1035 */
1036 if (n == last_n) {
1037 /* same sequence number, restore the previous state */
1038 k_state = k_state_saved;
1039 k_data_restore ();
1040 } else {
1041 /* new sequence number, checkpoint the download */
1042 last_n = n;
1043 k_state_saved = k_state;
1044 k_data_save ();
1045 }
1046 /* END NEW CODE */
1047
1048 /* get packet type */
1049 new_char = serial_getc ();
1050 if ((new_char & 0xE0) == 0)
1051 goto packet_error;
1052 sum += new_char & 0xff;
1053 k_state = new_char;
1054 --length;
1055 /* check for extended length */
1056 if (length == -2) {
1057 /* (length byte was 0, decremented twice) */
1058 /* get the two length bytes */
1059 new_char = serial_getc ();
1060 if ((new_char & 0xE0) == 0)
1061 goto packet_error;
1062 sum += new_char & 0xff;
1063 len_hi = untochar (new_char);
1064 new_char = serial_getc ();
1065 if ((new_char & 0xE0) == 0)
1066 goto packet_error;
1067 sum += new_char & 0xff;
1068 len_lo = untochar (new_char);
1069 length = len_hi * 95 + len_lo;
1070 /* check header checksum */
1071 new_char = serial_getc ();
1072 if ((new_char & 0xE0) == 0)
1073 goto packet_error;
1074 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
1075 goto packet_error;
1076 sum += new_char & 0xff;
1077/* --length; */ /* new length includes only data and block check to come */
1078 }
1079 /* bring in rest of packet */
1080 while (length > 1) {
1081 new_char = serial_getc ();
1082 if ((new_char & 0xE0) == 0)
1083 goto packet_error;
1084 sum += new_char & 0xff;
1085 --length;
1086 if (k_state == DATA_TYPE) {
1087 /* pass on the data if this is a data packet */
1088 k_data_char (new_char);
1089 } else if (k_state == SEND_TYPE) {
1090 /* save send pack in buffer as is */
1091 *send_ptr++ = new_char;
1092 /* if too much data, back off the pointer */
1093 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
1094 --send_ptr;
1095 }
1096 }
1097 /* get and validate checksum character */
1098 new_char = serial_getc ();
1099 if ((new_char & 0xE0) == 0)
1100 goto packet_error;
1101 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
1102 goto packet_error;
1103 /* get END_CHAR */
1104 new_char = serial_getc ();
1105 if (new_char != END_CHAR) {
1106 packet_error:
1107 /* restore state machines */
1108 k_state = k_state_saved;
1109 k_data_restore ();
1110 /* send a negative acknowledge packet in */
1111 send_nack (n);
1112 } else if (k_state == SEND_TYPE) {
1113 /* crack the protocol parms, build an appropriate ack packet */
1114 handle_send_packet (n);
1115 } else {
1116 /* send simple acknowledge packet in */
1117 send_ack (n);
1118 /* quit if end of transmission */
1119 if (k_state == BREAK_TYPE)
1120 done = 1;
1121 }
1122 ++z;
1123 }
1124 return ((ulong) os_data_addr - (ulong) bin_start_address);
1125}
1126#endif /* CFG_CMD_LOADB */
1127#if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
1128int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1129{
1130 extern int hwflow_onoff(int);
1131
1132 if (argc == 2) {
1133 if (strcmp(argv[1], "off") == 0)
1134 hwflow_onoff(-1);
1135 else
1136 if (strcmp(argv[1], "on") == 0)
1137 hwflow_onoff(1);
1138 else
1139 printf("Usage: %s\n", cmdtp->usage);
1140 }
1141 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1142 return 0;
1143}
1144#endif /* CFG_CMD_HWFLOW */