blob: b18709a47f594bedabe827c104c88791250a33d6 [file] [log] [blame]
wdenk8bde7f72003-06-27 21:31:46 +00001/*
wdenk232c1502004-03-12 00:14:09 +00002 * (C) Copyright 2000-2004
wdenk8bde7f72003-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>
wdenk8bde7f72003-06-27 21:31:46 +000029#include <s_record.h>
30#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000031#include <exports.h>
wdenk8bde7f72003-06-27 21:31:46 +000032
Wolfgang Denkd87080b2006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000034
35#if (CONFIG_COMMANDS & CFG_CMD_LOADS)
36static ulong load_serial (ulong offset);
37static int read_record (char *buf, ulong len);
38# if (CONFIG_COMMANDS & CFG_CMD_SAVES)
39static int save_serial (ulong offset, ulong size);
40static int write_record (char *buf);
41# endif /* CFG_CMD_SAVES */
42
43static int do_echo = 1;
44#endif /* CFG_CMD_LOADS */
45
46/* -------------------------------------------------------------------- */
47
48#if (CONFIG_COMMANDS & CFG_CMD_LOADS)
49int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
50{
51 ulong offset = 0;
52 ulong addr;
53 int i;
54 char *env_echo;
55 int rcode = 0;
56#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000057 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) {
wdenk232c1502004-03-12 00:14:09 +0000107 if (tstc()) {
108 (void) getc();
wdenk8bde7f72003-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 Denk77ddac92005-10-13 16:45:02 +0200169 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-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 );
wdenk3f85ce22004-02-23 16:11:30 +0000195 flush_cache (start_addr, size);
wdenk8bde7f72003-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{
216 char *p;
217 char c;
218
219 --len; /* always leave room for terminating '\0' byte */
220
221 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000222 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000223 if (do_echo)
wdenk232c1502004-03-12 00:14:09 +0000224 putc (c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000225
226 switch (c) {
227 case '\r':
228 case '\n':
229 *p = '\0';
230 return (p - buf);
231 case '\0':
232 case 0x03: /* ^C - Control C */
233 return (-1);
234 default:
235 *p = c;
236 }
237
238 /* Check for the console hangup (if any different from serial) */
wdenk232c1502004-03-12 00:14:09 +0000239 if (gd->jt[XF_getc] != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000240 if (ctrlc()) {
241 return (-1);
242 }
243 }
wdenk8bde7f72003-06-27 21:31:46 +0000244 }
245
246 /* line too long - truncate */
247 *p = '\0';
248 return (p - buf);
249}
250
251#if (CONFIG_COMMANDS & CFG_CMD_SAVES)
252
253int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
254{
255 ulong offset = 0;
256 ulong size = 0;
257#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000258 int save_baudrate, current_baudrate;
259
260 save_baudrate = current_baudrate = gd->baudrate;
261#endif
262
263 if (argc >= 2) {
264 offset = simple_strtoul(argv[1], NULL, 16);
265 }
266#ifdef CFG_LOADS_BAUD_CHANGE
267 if (argc >= 3) {
268 size = simple_strtoul(argv[2], NULL, 16);
269 }
270 if (argc == 4) {
271 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
272
273 /* default to current baudrate */
274 if (save_baudrate == 0)
275 save_baudrate = current_baudrate;
276 }
277 if (save_baudrate != current_baudrate) {
278 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
279 save_baudrate);
280 udelay(50000);
281 gd->baudrate = save_baudrate;
282 serial_setbrg ();
283 udelay(50000);
284 for (;;) {
285 if (getc() == '\r')
286 break;
287 }
288 }
289#else /* ! CFG_LOADS_BAUD_CHANGE */
290 if (argc == 3) {
291 size = simple_strtoul(argv[2], NULL, 16);
292 }
293#endif /* CFG_LOADS_BAUD_CHANGE */
294
295 printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
296 for (;;) {
297 if (getc() == '\r')
298 break;
299 }
300 if(save_serial (offset, size)) {
301 printf ("## S-Record upload aborted\n");
302 } else {
303 printf ("## S-Record upload complete\n");
304 }
305#ifdef CFG_LOADS_BAUD_CHANGE
306 if (save_baudrate != current_baudrate) {
307 printf ("## Switch baudrate to %d bps and press ESC ...\n",
308 (int)current_baudrate);
309 udelay (50000);
310 gd->baudrate = current_baudrate;
311 serial_setbrg ();
312 udelay (50000);
313 for (;;) {
314 if (getc() == 0x1B) /* ESC */
315 break;
316 }
317 }
318#endif
319 return 0;
320}
321
322#define SREC3_START "S0030000FC\n"
323#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
324#define SREC3_END "S70500000000FA\n"
325#define SREC_BYTES_PER_RECORD 16
326
327static int save_serial (ulong address, ulong count)
328{
329 int i, c, reclen, checksum, length;
330 char *hex = "0123456789ABCDEF";
331 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
332 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
333
334 reclen = 0;
335 checksum = 0;
336
337 if(write_record(SREC3_START)) /* write the header */
338 return (-1);
339 do {
340 if(count) { /* collect hex data in the buffer */
341 c = *(volatile uchar*)(address + reclen); /* get one byte */
342 checksum += c; /* accumulate checksum */
343 data[2*reclen] = hex[(c>>4)&0x0f];
344 data[2*reclen+1] = hex[c & 0x0f];
345 data[2*reclen+2] = '\0';
346 ++reclen;
347 --count;
348 }
349 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
350 /* enough data collected for one record: dump it */
351 if(reclen) { /* build & write a data record: */
352 /* address + data + checksum */
353 length = 4 + reclen + 1;
354
355 /* accumulate length bytes into checksum */
356 for(i = 0; i < 2; i++)
357 checksum += (length >> (8*i)) & 0xff;
358
359 /* accumulate address bytes into checksum: */
360 for(i = 0; i < 4; i++)
361 checksum += (address >> (8*i)) & 0xff;
362
363 /* make proper checksum byte: */
364 checksum = ~checksum & 0xff;
365
366 /* output one record: */
367 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
368 if(write_record(record))
369 return (-1);
370 }
371 address += reclen; /* increment address */
372 checksum = 0;
373 reclen = 0;
374 }
375 }
376 while(count);
377 if(write_record(SREC3_END)) /* write the final record */
378 return (-1);
379 return(0);
380}
381
382static int
383write_record (char *buf)
384{
385 char c;
386
387 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000388 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000389
390 /* Check for the console hangup (if any different from serial) */
391
392 if (ctrlc()) {
393 return (-1);
394 }
395 return (0);
396}
397# endif /* CFG_CMD_SAVES */
398
399#endif /* CFG_CMD_LOADS */
400
401
402#if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
403
404#define XON_CHAR 17
405#define XOFF_CHAR 19
406#define START_CHAR 0x01
407#define ETX_CHAR 0x03
408#define END_CHAR 0x0D
409#define SPACE 0x20
410#define K_ESCAPE 0x23
411#define SEND_TYPE 'S'
412#define DATA_TYPE 'D'
413#define ACK_TYPE 'Y'
414#define NACK_TYPE 'N'
415#define BREAK_TYPE 'B'
416#define tochar(x) ((char) (((x) + SPACE) & 0xff))
417#define untochar(x) ((int) (((x) - SPACE) & 0xff))
418
419extern int os_data_count;
420extern int os_data_header[8];
421
422static void set_kerm_bin_mode(unsigned long *);
423static int k_recv(void);
424static ulong load_serial_bin (ulong offset);
425
426
427char his_eol; /* character he needs at end of packet */
428int his_pad_count; /* number of pad chars he needs */
429char his_pad_char; /* pad chars he needs */
430char his_quote; /* quote chars he'll use */
431
432int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
433{
wdenk8bde7f72003-06-27 21:31:46 +0000434 ulong offset = 0;
435 ulong addr;
436 int load_baudrate, current_baudrate;
437 int rcode = 0;
438 char *s;
439
440 /* pre-set offset from CFG_LOAD_ADDR */
441 offset = CFG_LOAD_ADDR;
442
443 /* pre-set offset from $loadaddr */
444 if ((s = getenv("loadaddr")) != NULL) {
445 offset = simple_strtoul(s, NULL, 16);
446 }
447
448 load_baudrate = current_baudrate = gd->baudrate;
449
450 if (argc >= 2) {
451 offset = simple_strtoul(argv[1], NULL, 16);
452 }
453 if (argc == 3) {
454 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
455
456 /* default to current baudrate */
457 if (load_baudrate == 0)
458 load_baudrate = current_baudrate;
459 }
460
461 if (load_baudrate != current_baudrate) {
462 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
463 load_baudrate);
464 udelay(50000);
465 gd->baudrate = load_baudrate;
466 serial_setbrg ();
467 udelay(50000);
468 for (;;) {
469 if (getc() == '\r')
470 break;
471 }
472 }
473
474 printf ("## Ready for binary (kermit) download "
475 "to 0x%08lX at %d bps...\n",
476 offset,
wdenk27b207f2003-07-24 23:38:38 +0000477 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000478 addr = load_serial_bin (offset);
479
480 if (addr == ~0) {
481 load_addr = 0;
482 printf ("## Binary (kermit) download aborted\n");
483 rcode = 1;
484 } else {
485 printf ("## Start Addr = 0x%08lX\n", addr);
486 load_addr = addr;
487 }
488
489 if (load_baudrate != current_baudrate) {
490 printf ("## Switch baudrate to %d bps and press ESC ...\n",
491 current_baudrate);
492 udelay (50000);
493 gd->baudrate = current_baudrate;
494 serial_setbrg ();
495 udelay (50000);
496 for (;;) {
497 if (getc() == 0x1B) /* ESC */
498 break;
499 }
500 }
501
502#ifdef CONFIG_AUTOSCRIPT
503 if (load_addr) {
504 char *s;
505
506 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
507 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
508 rcode = autoscript (load_addr);
509 }
510 }
511#endif
512 return rcode;
513}
514
515
516static ulong load_serial_bin (ulong offset)
517{
518 int size, i;
519 char buf[32];
520
521 set_kerm_bin_mode ((ulong *) offset);
522 size = k_recv ();
523
524 /*
525 * Gather any trailing characters (for instance, the ^D which
526 * is sent by 'cu' after sending a file), and give the
527 * box some time (100 * 1 ms)
528 */
529 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000530 if (tstc()) {
531 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000532 }
533 udelay(1000);
534 }
535
536 flush_cache (offset, size);
537
538 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
539 sprintf(buf, "%X", size);
540 setenv("filesize", buf);
541
542 return offset;
543}
544
545void send_pad (void)
546{
547 int count = his_pad_count;
548
549 while (count-- > 0)
wdenk232c1502004-03-12 00:14:09 +0000550 putc (his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000551}
552
553/* converts escaped kermit char to binary char */
554char ktrans (char in)
555{
556 if ((in & 0x60) == 0x40) {
557 return (char) (in & ~0x40);
558 } else if ((in & 0x7f) == 0x3f) {
559 return (char) (in | 0x40);
560 } else
561 return in;
562}
563
564int chk1 (char *buffer)
565{
566 int total = 0;
567
568 while (*buffer) {
569 total += *buffer++;
570 }
571 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
572}
573
574void s1_sendpacket (char *packet)
575{
576 send_pad ();
577 while (*packet) {
wdenk232c1502004-03-12 00:14:09 +0000578 putc (*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000579 }
580}
581
582static char a_b[24];
583void send_ack (int n)
584{
585 a_b[0] = START_CHAR;
586 a_b[1] = tochar (3);
587 a_b[2] = tochar (n);
588 a_b[3] = ACK_TYPE;
589 a_b[4] = '\0';
590 a_b[4] = tochar (chk1 (&a_b[1]));
591 a_b[5] = his_eol;
592 a_b[6] = '\0';
593 s1_sendpacket (a_b);
594}
595
596void send_nack (int n)
597{
598 a_b[0] = START_CHAR;
599 a_b[1] = tochar (3);
600 a_b[2] = tochar (n);
601 a_b[3] = NACK_TYPE;
602 a_b[4] = '\0';
603 a_b[4] = tochar (chk1 (&a_b[1]));
604 a_b[5] = his_eol;
605 a_b[6] = '\0';
606 s1_sendpacket (a_b);
607}
608
609
610/* os_data_* takes an OS Open image and puts it into memory, and
611 puts the boot header in an array named os_data_header
612
613 if image is binary, no header is stored in os_data_header.
614*/
615void (*os_data_init) (void);
616void (*os_data_char) (char new_char);
617static int os_data_state, os_data_state_saved;
618int os_data_count;
619static int os_data_count_saved;
620static char *os_data_addr, *os_data_addr_saved;
621static char *bin_start_address;
622int os_data_header[8];
623static void bin_data_init (void)
624{
625 os_data_state = 0;
626 os_data_count = 0;
627 os_data_addr = bin_start_address;
628}
629static void os_data_save (void)
630{
631 os_data_state_saved = os_data_state;
632 os_data_count_saved = os_data_count;
633 os_data_addr_saved = os_data_addr;
634}
635static void os_data_restore (void)
636{
637 os_data_state = os_data_state_saved;
638 os_data_count = os_data_count_saved;
639 os_data_addr = os_data_addr_saved;
640}
641static void bin_data_char (char new_char)
642{
643 switch (os_data_state) {
644 case 0: /* data */
645 *os_data_addr++ = new_char;
646 --os_data_count;
647 break;
648 }
649}
650static void set_kerm_bin_mode (unsigned long *addr)
651{
652 bin_start_address = (char *) addr;
653 os_data_init = bin_data_init;
654 os_data_char = bin_data_char;
655}
656
657
658/* k_data_* simply handles the kermit escape translations */
659static int k_data_escape, k_data_escape_saved;
660void k_data_init (void)
661{
662 k_data_escape = 0;
663 os_data_init ();
664}
665void k_data_save (void)
666{
667 k_data_escape_saved = k_data_escape;
668 os_data_save ();
669}
670void k_data_restore (void)
671{
672 k_data_escape = k_data_escape_saved;
673 os_data_restore ();
674}
675void k_data_char (char new_char)
676{
677 if (k_data_escape) {
678 /* last char was escape - translate this character */
679 os_data_char (ktrans (new_char));
680 k_data_escape = 0;
681 } else {
682 if (new_char == his_quote) {
683 /* this char is escape - remember */
684 k_data_escape = 1;
685 } else {
686 /* otherwise send this char as-is */
687 os_data_char (new_char);
688 }
689 }
690}
691
692#define SEND_DATA_SIZE 20
693char send_parms[SEND_DATA_SIZE];
694char *send_ptr;
695
696/* handle_send_packet interprits the protocol info and builds and
697 sends an appropriate ack for what we can do */
698void handle_send_packet (int n)
699{
700 int length = 3;
701 int bytes;
702
703 /* initialize some protocol parameters */
704 his_eol = END_CHAR; /* default end of line character */
705 his_pad_count = 0;
706 his_pad_char = '\0';
707 his_quote = K_ESCAPE;
708
709 /* ignore last character if it filled the buffer */
710 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
711 --send_ptr;
712 bytes = send_ptr - send_parms; /* how many bytes we'll process */
713 do {
714 if (bytes-- <= 0)
715 break;
716 /* handle MAXL - max length */
717 /* ignore what he says - most I'll take (here) is 94 */
718 a_b[++length] = tochar (94);
719 if (bytes-- <= 0)
720 break;
721 /* handle TIME - time you should wait for my packets */
722 /* ignore what he says - don't wait for my ack longer than 1 second */
723 a_b[++length] = tochar (1);
724 if (bytes-- <= 0)
725 break;
726 /* handle NPAD - number of pad chars I need */
727 /* remember what he says - I need none */
728 his_pad_count = untochar (send_parms[2]);
729 a_b[++length] = tochar (0);
730 if (bytes-- <= 0)
731 break;
732 /* handle PADC - pad chars I need */
733 /* remember what he says - I need none */
734 his_pad_char = ktrans (send_parms[3]);
735 a_b[++length] = 0x40; /* He should ignore this */
736 if (bytes-- <= 0)
737 break;
738 /* handle EOL - end of line he needs */
739 /* remember what he says - I need CR */
740 his_eol = untochar (send_parms[4]);
741 a_b[++length] = tochar (END_CHAR);
742 if (bytes-- <= 0)
743 break;
744 /* handle QCTL - quote control char he'll use */
745 /* remember what he says - I'll use '#' */
746 his_quote = send_parms[5];
747 a_b[++length] = '#';
748 if (bytes-- <= 0)
749 break;
750 /* handle QBIN - 8-th bit prefixing */
751 /* ignore what he says - I refuse */
752 a_b[++length] = 'N';
753 if (bytes-- <= 0)
754 break;
755 /* handle CHKT - the clock check type */
756 /* ignore what he says - I do type 1 (for now) */
757 a_b[++length] = '1';
758 if (bytes-- <= 0)
759 break;
760 /* handle REPT - the repeat prefix */
761 /* ignore what he says - I refuse (for now) */
762 a_b[++length] = 'N';
763 if (bytes-- <= 0)
764 break;
765 /* handle CAPAS - the capabilities mask */
766 /* ignore what he says - I only do long packets - I don't do windows */
767 a_b[++length] = tochar (2); /* only long packets */
768 a_b[++length] = tochar (0); /* no windows */
769 a_b[++length] = tochar (94); /* large packet msb */
770 a_b[++length] = tochar (94); /* large packet lsb */
771 } while (0);
772
773 a_b[0] = START_CHAR;
774 a_b[1] = tochar (length);
775 a_b[2] = tochar (n);
776 a_b[3] = ACK_TYPE;
777 a_b[++length] = '\0';
778 a_b[length] = tochar (chk1 (&a_b[1]));
779 a_b[++length] = his_eol;
780 a_b[++length] = '\0';
781 s1_sendpacket (a_b);
782}
783
784/* k_recv receives a OS Open image file over kermit line */
785static int k_recv (void)
786{
787 char new_char;
788 char k_state, k_state_saved;
789 int sum;
790 int done;
791 int length;
792 int n, last_n;
793 int z = 0;
794 int len_lo, len_hi;
795
796 /* initialize some protocol parameters */
797 his_eol = END_CHAR; /* default end of line character */
798 his_pad_count = 0;
799 his_pad_char = '\0';
800 his_quote = K_ESCAPE;
801
802 /* initialize the k_recv and k_data state machine */
803 done = 0;
804 k_state = 0;
805 k_data_init ();
806 k_state_saved = k_state;
807 k_data_save ();
808 n = 0; /* just to get rid of a warning */
809 last_n = -1;
810
811 /* expect this "type" sequence (but don't check):
812 S: send initiate
813 F: file header
814 D: data (multiple)
815 Z: end of file
816 B: break transmission
817 */
818
819 /* enter main loop */
820 while (!done) {
821 /* set the send packet pointer to begining of send packet parms */
822 send_ptr = send_parms;
823
824 /* With each packet, start summing the bytes starting with the length.
825 Save the current sequence number.
826 Note the type of the packet.
827 If a character less than SPACE (0x20) is received - error.
828 */
829
830#if 0
831 /* OLD CODE, Prior to checking sequence numbers */
832 /* first have all state machines save current states */
833 k_state_saved = k_state;
834 k_data_save ();
835#endif
836
837 /* get a packet */
838 /* wait for the starting character or ^C */
839 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000840 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000841 case START_CHAR: /* start packet */
842 goto START;
843 case ETX_CHAR: /* ^C waiting for packet */
844 return (0);
845 default:
846 ;
847 }
848 }
849START:
850 /* get length of packet */
851 sum = 0;
wdenk232c1502004-03-12 00:14:09 +0000852 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000853 if ((new_char & 0xE0) == 0)
854 goto packet_error;
855 sum += new_char & 0xff;
856 length = untochar (new_char);
857 /* get sequence number */
wdenk232c1502004-03-12 00:14:09 +0000858 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000859 if ((new_char & 0xE0) == 0)
860 goto packet_error;
861 sum += new_char & 0xff;
862 n = untochar (new_char);
863 --length;
864
865 /* NEW CODE - check sequence numbers for retried packets */
866 /* Note - this new code assumes that the sequence number is correctly
867 * received. Handling an invalid sequence number adds another layer
868 * of complexity that may not be needed - yet! At this time, I'm hoping
869 * that I don't need to buffer the incoming data packets and can write
870 * the data into memory in real time.
871 */
872 if (n == last_n) {
873 /* same sequence number, restore the previous state */
874 k_state = k_state_saved;
875 k_data_restore ();
876 } else {
877 /* new sequence number, checkpoint the download */
878 last_n = n;
879 k_state_saved = k_state;
880 k_data_save ();
881 }
882 /* END NEW CODE */
883
884 /* get packet type */
wdenk232c1502004-03-12 00:14:09 +0000885 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000886 if ((new_char & 0xE0) == 0)
887 goto packet_error;
888 sum += new_char & 0xff;
889 k_state = new_char;
890 --length;
891 /* check for extended length */
892 if (length == -2) {
893 /* (length byte was 0, decremented twice) */
894 /* get the two length bytes */
wdenk232c1502004-03-12 00:14:09 +0000895 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000896 if ((new_char & 0xE0) == 0)
897 goto packet_error;
898 sum += new_char & 0xff;
899 len_hi = untochar (new_char);
wdenk232c1502004-03-12 00:14:09 +0000900 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000901 if ((new_char & 0xE0) == 0)
902 goto packet_error;
903 sum += new_char & 0xff;
904 len_lo = untochar (new_char);
905 length = len_hi * 95 + len_lo;
906 /* check header checksum */
wdenk232c1502004-03-12 00:14:09 +0000907 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000908 if ((new_char & 0xE0) == 0)
909 goto packet_error;
910 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
911 goto packet_error;
912 sum += new_char & 0xff;
913/* --length; */ /* new length includes only data and block check to come */
914 }
915 /* bring in rest of packet */
916 while (length > 1) {
wdenk232c1502004-03-12 00:14:09 +0000917 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000918 if ((new_char & 0xE0) == 0)
919 goto packet_error;
920 sum += new_char & 0xff;
921 --length;
922 if (k_state == DATA_TYPE) {
923 /* pass on the data if this is a data packet */
924 k_data_char (new_char);
925 } else if (k_state == SEND_TYPE) {
926 /* save send pack in buffer as is */
927 *send_ptr++ = new_char;
928 /* if too much data, back off the pointer */
929 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
930 --send_ptr;
931 }
932 }
933 /* get and validate checksum character */
wdenk232c1502004-03-12 00:14:09 +0000934 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000935 if ((new_char & 0xE0) == 0)
936 goto packet_error;
937 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
938 goto packet_error;
939 /* get END_CHAR */
wdenk232c1502004-03-12 00:14:09 +0000940 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000941 if (new_char != END_CHAR) {
942 packet_error:
943 /* restore state machines */
944 k_state = k_state_saved;
945 k_data_restore ();
946 /* send a negative acknowledge packet in */
947 send_nack (n);
948 } else if (k_state == SEND_TYPE) {
949 /* crack the protocol parms, build an appropriate ack packet */
950 handle_send_packet (n);
951 } else {
952 /* send simple acknowledge packet in */
953 send_ack (n);
954 /* quit if end of transmission */
955 if (k_state == BREAK_TYPE)
956 done = 1;
957 }
958 ++z;
959 }
960 return ((ulong) os_data_addr - (ulong) bin_start_address);
961}
962#endif /* CFG_CMD_LOADB */
963
964/* -------------------------------------------------------------------- */
965
966#if (CONFIG_COMMANDS & CFG_CMD_LOADS)
967
968#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +0000969U_BOOT_CMD(
970 loads, 3, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +0000971 "loads - load S-Record file over serial line\n",
972 "[ off ] [ baud ]\n"
973 " - load S-Record file over serial line"
974 " with offset 'off' and baudrate 'baud'\n"
975);
976
977#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +0000978U_BOOT_CMD(
979 loads, 2, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +0000980 "loads - load S-Record file over serial line\n",
981 "[ off ]\n"
982 " - load S-Record file over serial line with offset 'off'\n"
983);
984#endif /* CFG_LOADS_BAUD_CHANGE */
985
986/*
987 * SAVES always requires LOADS support, but not vice versa
988 */
989
990
991#if (CONFIG_COMMANDS & CFG_CMD_SAVES)
992#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +0000993U_BOOT_CMD(
994 saves, 4, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +0000995 "saves - save S-Record file over serial line\n",
996 "[ off ] [size] [ baud ]\n"
997 " - save S-Record file over serial line"
998 " with offset 'off', size 'size' and baudrate 'baud'\n"
999);
1000#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001001U_BOOT_CMD(
1002 saves, 3, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001003 "saves - save S-Record file over serial line\n",
1004 "[ off ] [size]\n"
1005 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1006);
1007#endif /* CFG_LOADS_BAUD_CHANGE */
1008#endif /* CFG_CMD_SAVES */
1009#endif /* CFG_CMD_LOADS */
1010
1011
1012#if (CONFIG_COMMANDS & CFG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001013U_BOOT_CMD(
1014 loadb, 3, 0, do_load_serial_bin,
wdenk8bde7f72003-06-27 21:31:46 +00001015 "loadb - load binary file over serial line (kermit mode)\n",
1016 "[ off ] [ baud ]\n"
1017 " - load binary file over serial line"
1018 " with offset 'off' and baudrate 'baud'\n"
1019);
1020
1021#endif /* CFG_CMD_LOADB */
1022
1023/* -------------------------------------------------------------------- */
1024
1025#if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
1026int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1027{
1028 extern int hwflow_onoff(int);
1029
1030 if (argc == 2) {
1031 if (strcmp(argv[1], "off") == 0)
1032 hwflow_onoff(-1);
1033 else
1034 if (strcmp(argv[1], "on") == 0)
1035 hwflow_onoff(1);
1036 else
1037 printf("Usage: %s\n", cmdtp->usage);
1038 }
1039 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1040 return 0;
1041}
1042
1043/* -------------------------------------------------------------------- */
1044
wdenk0d498392003-07-01 21:06:45 +00001045U_BOOT_CMD(
wdenkf12e5682003-07-07 20:07:54 +00001046 hwflow, 2, 0, do_hwflow,
wdenk8bde7f72003-06-27 21:31:46 +00001047 "hwflow - turn the harwdare flow control on/off\n",
wdenkf12e5682003-07-07 20:07:54 +00001048 "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
wdenk8bde7f72003-06-27 21:31:46 +00001049);
1050
1051#endif /* CFG_CMD_HWFLOW */