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