blob: 204c3ebf1930ddbb3dcce843402e1a3be9a558f4 [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>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020032#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000033
Wolfgang Denkd87080b2006-03-31 18:32:53 +020034DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000035
Jon Loeligerc76fe472007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADB)
Wolfgang Denk8546e232006-05-02 00:11:25 +020037static ulong load_serial_ymodem (ulong offset);
38#endif
39
Jon Loeligerc76fe472007-07-08 18:02:23 -050040#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +000041static ulong load_serial (ulong offset);
42static int read_record (char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050043# if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +000044static int save_serial (ulong offset, ulong size);
45static int write_record (char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050046#endif
wdenk8bde7f72003-06-27 21:31:46 +000047
48static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050049#endif
wdenk8bde7f72003-06-27 21:31:46 +000050
51/* -------------------------------------------------------------------- */
52
Jon Loeligerc76fe472007-07-08 18:02:23 -050053#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +000054int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
55{
56 ulong offset = 0;
57 ulong addr;
58 int i;
59 char *env_echo;
60 int rcode = 0;
61#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000062 int load_baudrate, current_baudrate;
63
64 load_baudrate = current_baudrate = gd->baudrate;
65#endif
66
67 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
68 do_echo = 1;
69 } else {
70 do_echo = 0;
71 }
72
73#ifdef CFG_LOADS_BAUD_CHANGE
74 if (argc >= 2) {
75 offset = simple_strtoul(argv[1], NULL, 16);
76 }
77 if (argc == 3) {
78 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
79
80 /* default to current baudrate */
81 if (load_baudrate == 0)
82 load_baudrate = current_baudrate;
83 }
84 if (load_baudrate != current_baudrate) {
85 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
86 load_baudrate);
87 udelay(50000);
88 gd->baudrate = load_baudrate;
89 serial_setbrg ();
90 udelay(50000);
91 for (;;) {
92 if (getc() == '\r')
93 break;
94 }
95 }
96#else /* ! CFG_LOADS_BAUD_CHANGE */
97 if (argc == 2) {
98 offset = simple_strtoul(argv[1], NULL, 16);
99 }
100#endif /* CFG_LOADS_BAUD_CHANGE */
101
102 printf ("## Ready for S-Record download ...\n");
103
104 addr = load_serial (offset);
105
106 /*
107 * Gather any trailing characters (for instance, the ^D which
108 * is sent by 'cu' after sending a file), and give the
109 * box some time (100 * 1 ms)
110 */
111 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000112 if (tstc()) {
113 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000114 }
115 udelay(1000);
116 }
117
118 if (addr == ~0) {
119 printf ("## S-Record download aborted\n");
120 rcode = 1;
121 } else {
122 printf ("## Start Addr = 0x%08lX\n", addr);
123 load_addr = addr;
124 }
125
126#ifdef CFG_LOADS_BAUD_CHANGE
127 if (load_baudrate != current_baudrate) {
128 printf ("## Switch baudrate to %d bps and press ESC ...\n",
129 current_baudrate);
130 udelay (50000);
131 gd->baudrate = current_baudrate;
132 serial_setbrg ();
133 udelay (50000);
134 for (;;) {
135 if (getc() == 0x1B) /* ESC */
136 break;
137 }
138 }
139#endif
140 return rcode;
141}
142
143static ulong
144load_serial (ulong offset)
145{
146 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
147 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
148 int binlen; /* no. of data bytes in S-Rec. */
149 int type; /* return code for record type */
150 ulong addr; /* load address from S-Record */
151 ulong size; /* number of bytes transferred */
152 char buf[32];
153 ulong store_addr;
154 ulong start_addr = ~0;
155 ulong end_addr = 0;
156 int line_count = 0;
157
158 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
159 type = srec_decode (record, &binlen, &addr, binbuf);
160
161 if (type < 0) {
162 return (~0); /* Invalid S-Record */
163 }
164
165 switch (type) {
166 case SREC_DATA2:
167 case SREC_DATA3:
168 case SREC_DATA4:
169 store_addr = addr + offset;
170#ifndef CFG_NO_FLASH
171 if (addr2info(store_addr)) {
172 int rc;
173
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200174 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000175 if (rc != 0) {
176 flash_perror (rc);
177 return (~0);
178 }
179 } else
180#endif
181 {
182 memcpy ((char *)(store_addr), binbuf, binlen);
183 }
184 if ((store_addr) < start_addr)
185 start_addr = store_addr;
186 if ((store_addr + binlen - 1) > end_addr)
187 end_addr = store_addr + binlen - 1;
188 break;
189 case SREC_END2:
190 case SREC_END3:
191 case SREC_END4:
192 udelay (10000);
193 size = end_addr - start_addr + 1;
194 printf ("\n"
195 "## First Load Addr = 0x%08lX\n"
196 "## Last Load Addr = 0x%08lX\n"
197 "## Total Size = 0x%08lX = %ld Bytes\n",
198 start_addr, end_addr, size, size
199 );
wdenk3f85ce22004-02-23 16:11:30 +0000200 flush_cache (start_addr, size);
wdenk8bde7f72003-06-27 21:31:46 +0000201 sprintf(buf, "%lX", size);
202 setenv("filesize", buf);
203 return (addr);
204 case SREC_START:
205 break;
206 default:
207 break;
208 }
209 if (!do_echo) { /* print a '.' every 100 lines */
210 if ((++line_count % 100) == 0)
211 putc ('.');
212 }
213 }
214
215 return (~0); /* Download aborted */
216}
217
218static int
219read_record (char *buf, ulong len)
220{
221 char *p;
222 char c;
223
224 --len; /* always leave room for terminating '\0' byte */
225
226 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000227 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000228 if (do_echo)
wdenk232c1502004-03-12 00:14:09 +0000229 putc (c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000230
231 switch (c) {
232 case '\r':
233 case '\n':
234 *p = '\0';
235 return (p - buf);
236 case '\0':
237 case 0x03: /* ^C - Control C */
238 return (-1);
239 default:
240 *p = c;
241 }
242
243 /* Check for the console hangup (if any different from serial) */
wdenk232c1502004-03-12 00:14:09 +0000244 if (gd->jt[XF_getc] != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000245 if (ctrlc()) {
246 return (-1);
247 }
248 }
wdenk8bde7f72003-06-27 21:31:46 +0000249 }
250
251 /* line too long - truncate */
252 *p = '\0';
253 return (p - buf);
254}
255
Jon Loeligerc76fe472007-07-08 18:02:23 -0500256#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000257
258int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
259{
260 ulong offset = 0;
261 ulong size = 0;
262#ifdef CFG_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000263 int save_baudrate, current_baudrate;
264
265 save_baudrate = current_baudrate = gd->baudrate;
266#endif
267
268 if (argc >= 2) {
269 offset = simple_strtoul(argv[1], NULL, 16);
270 }
271#ifdef CFG_LOADS_BAUD_CHANGE
272 if (argc >= 3) {
273 size = simple_strtoul(argv[2], NULL, 16);
274 }
275 if (argc == 4) {
276 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
277
278 /* default to current baudrate */
279 if (save_baudrate == 0)
280 save_baudrate = current_baudrate;
281 }
282 if (save_baudrate != current_baudrate) {
283 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
284 save_baudrate);
285 udelay(50000);
286 gd->baudrate = save_baudrate;
287 serial_setbrg ();
288 udelay(50000);
289 for (;;) {
290 if (getc() == '\r')
291 break;
292 }
293 }
294#else /* ! CFG_LOADS_BAUD_CHANGE */
295 if (argc == 3) {
296 size = simple_strtoul(argv[2], NULL, 16);
297 }
298#endif /* CFG_LOADS_BAUD_CHANGE */
299
300 printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
301 for (;;) {
302 if (getc() == '\r')
303 break;
304 }
305 if(save_serial (offset, size)) {
306 printf ("## S-Record upload aborted\n");
307 } else {
308 printf ("## S-Record upload complete\n");
309 }
310#ifdef CFG_LOADS_BAUD_CHANGE
311 if (save_baudrate != current_baudrate) {
312 printf ("## Switch baudrate to %d bps and press ESC ...\n",
313 (int)current_baudrate);
314 udelay (50000);
315 gd->baudrate = current_baudrate;
316 serial_setbrg ();
317 udelay (50000);
318 for (;;) {
319 if (getc() == 0x1B) /* ESC */
320 break;
321 }
322 }
323#endif
324 return 0;
325}
326
327#define SREC3_START "S0030000FC\n"
328#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
329#define SREC3_END "S70500000000FA\n"
330#define SREC_BYTES_PER_RECORD 16
331
332static int save_serial (ulong address, ulong count)
333{
334 int i, c, reclen, checksum, length;
335 char *hex = "0123456789ABCDEF";
336 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
337 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
338
339 reclen = 0;
340 checksum = 0;
341
342 if(write_record(SREC3_START)) /* write the header */
343 return (-1);
344 do {
345 if(count) { /* collect hex data in the buffer */
346 c = *(volatile uchar*)(address + reclen); /* get one byte */
347 checksum += c; /* accumulate checksum */
348 data[2*reclen] = hex[(c>>4)&0x0f];
349 data[2*reclen+1] = hex[c & 0x0f];
350 data[2*reclen+2] = '\0';
351 ++reclen;
352 --count;
353 }
354 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
355 /* enough data collected for one record: dump it */
356 if(reclen) { /* build & write a data record: */
357 /* address + data + checksum */
358 length = 4 + reclen + 1;
359
360 /* accumulate length bytes into checksum */
361 for(i = 0; i < 2; i++)
362 checksum += (length >> (8*i)) & 0xff;
363
364 /* accumulate address bytes into checksum: */
365 for(i = 0; i < 4; i++)
366 checksum += (address >> (8*i)) & 0xff;
367
368 /* make proper checksum byte: */
369 checksum = ~checksum & 0xff;
370
371 /* output one record: */
372 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
373 if(write_record(record))
374 return (-1);
375 }
376 address += reclen; /* increment address */
377 checksum = 0;
378 reclen = 0;
379 }
380 }
381 while(count);
382 if(write_record(SREC3_END)) /* write the final record */
383 return (-1);
384 return(0);
385}
386
387static int
388write_record (char *buf)
389{
390 char c;
391
392 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000393 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000394
395 /* Check for the console hangup (if any different from serial) */
396
397 if (ctrlc()) {
398 return (-1);
399 }
400 return (0);
401}
Jon Loeliger90253172007-07-10 11:02:44 -0500402# endif
wdenk8bde7f72003-06-27 21:31:46 +0000403
Jon Loeliger90253172007-07-10 11:02:44 -0500404#endif
wdenk8bde7f72003-06-27 21:31:46 +0000405
406
Jon Loeligerc76fe472007-07-08 18:02:23 -0500407#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500408/*
409 * loadb command (load binary) included
410 */
wdenk8bde7f72003-06-27 21:31:46 +0000411#define XON_CHAR 17
412#define XOFF_CHAR 19
413#define START_CHAR 0x01
414#define ETX_CHAR 0x03
415#define END_CHAR 0x0D
416#define SPACE 0x20
417#define K_ESCAPE 0x23
418#define SEND_TYPE 'S'
419#define DATA_TYPE 'D'
420#define ACK_TYPE 'Y'
421#define NACK_TYPE 'N'
422#define BREAK_TYPE 'B'
423#define tochar(x) ((char) (((x) + SPACE) & 0xff))
424#define untochar(x) ((int) (((x) - SPACE) & 0xff))
425
426extern int os_data_count;
427extern int os_data_header[8];
428
429static void set_kerm_bin_mode(unsigned long *);
430static int k_recv(void);
431static ulong load_serial_bin (ulong offset);
432
433
434char his_eol; /* character he needs at end of packet */
435int his_pad_count; /* number of pad chars he needs */
436char his_pad_char; /* pad chars he needs */
437char his_quote; /* quote chars he'll use */
438
439int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
440{
wdenk8bde7f72003-06-27 21:31:46 +0000441 ulong offset = 0;
442 ulong addr;
443 int load_baudrate, current_baudrate;
444 int rcode = 0;
445 char *s;
446
447 /* pre-set offset from CFG_LOAD_ADDR */
448 offset = CFG_LOAD_ADDR;
449
450 /* pre-set offset from $loadaddr */
451 if ((s = getenv("loadaddr")) != NULL) {
452 offset = simple_strtoul(s, NULL, 16);
453 }
454
455 load_baudrate = current_baudrate = gd->baudrate;
456
457 if (argc >= 2) {
458 offset = simple_strtoul(argv[1], NULL, 16);
459 }
460 if (argc == 3) {
461 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
462
463 /* default to current baudrate */
464 if (load_baudrate == 0)
465 load_baudrate = current_baudrate;
466 }
467
468 if (load_baudrate != current_baudrate) {
469 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
470 load_baudrate);
471 udelay(50000);
472 gd->baudrate = load_baudrate;
473 serial_setbrg ();
474 udelay(50000);
475 for (;;) {
476 if (getc() == '\r')
477 break;
478 }
479 }
480
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200481 if (strcmp(argv[0],"loady")==0) {
482 printf ("## Ready for binary (ymodem) download "
483 "to 0x%08lX at %d bps...\n",
484 offset,
485 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000486
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200487 addr = load_serial_ymodem (offset);
488
wdenk8bde7f72003-06-27 21:31:46 +0000489 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000490
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200491 printf ("## Ready for binary (kermit) download "
492 "to 0x%08lX at %d bps...\n",
493 offset,
494 load_baudrate);
495 addr = load_serial_bin (offset);
496
497 if (addr == ~0) {
498 load_addr = 0;
499 printf ("## Binary (kermit) download aborted\n");
500 rcode = 1;
501 } else {
502 printf ("## Start Addr = 0x%08lX\n", addr);
503 load_addr = addr;
504 }
505 }
wdenk8bde7f72003-06-27 21:31:46 +0000506 if (load_baudrate != current_baudrate) {
507 printf ("## Switch baudrate to %d bps and press ESC ...\n",
508 current_baudrate);
509 udelay (50000);
510 gd->baudrate = current_baudrate;
511 serial_setbrg ();
512 udelay (50000);
513 for (;;) {
514 if (getc() == 0x1B) /* ESC */
515 break;
516 }
517 }
518
519#ifdef CONFIG_AUTOSCRIPT
520 if (load_addr) {
521 char *s;
522
523 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
524 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
525 rcode = autoscript (load_addr);
526 }
527 }
528#endif
529 return rcode;
530}
531
532
533static ulong load_serial_bin (ulong offset)
534{
535 int size, i;
536 char buf[32];
537
538 set_kerm_bin_mode ((ulong *) offset);
539 size = k_recv ();
540
541 /*
542 * Gather any trailing characters (for instance, the ^D which
543 * is sent by 'cu' after sending a file), and give the
544 * box some time (100 * 1 ms)
545 */
546 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000547 if (tstc()) {
548 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000549 }
550 udelay(1000);
551 }
552
553 flush_cache (offset, size);
554
555 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
556 sprintf(buf, "%X", size);
557 setenv("filesize", buf);
558
559 return offset;
560}
561
562void send_pad (void)
563{
564 int count = his_pad_count;
565
566 while (count-- > 0)
wdenk232c1502004-03-12 00:14:09 +0000567 putc (his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000568}
569
570/* converts escaped kermit char to binary char */
571char ktrans (char in)
572{
573 if ((in & 0x60) == 0x40) {
574 return (char) (in & ~0x40);
575 } else if ((in & 0x7f) == 0x3f) {
576 return (char) (in | 0x40);
577 } else
578 return in;
579}
580
581int chk1 (char *buffer)
582{
583 int total = 0;
584
585 while (*buffer) {
586 total += *buffer++;
587 }
588 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
589}
590
591void s1_sendpacket (char *packet)
592{
593 send_pad ();
594 while (*packet) {
wdenk232c1502004-03-12 00:14:09 +0000595 putc (*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000596 }
597}
598
599static char a_b[24];
600void send_ack (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] = ACK_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
613void send_nack (int n)
614{
615 a_b[0] = START_CHAR;
616 a_b[1] = tochar (3);
617 a_b[2] = tochar (n);
618 a_b[3] = NACK_TYPE;
619 a_b[4] = '\0';
620 a_b[4] = tochar (chk1 (&a_b[1]));
621 a_b[5] = his_eol;
622 a_b[6] = '\0';
623 s1_sendpacket (a_b);
624}
625
626
627/* os_data_* takes an OS Open image and puts it into memory, and
628 puts the boot header in an array named os_data_header
629
630 if image is binary, no header is stored in os_data_header.
631*/
632void (*os_data_init) (void);
633void (*os_data_char) (char new_char);
634static int os_data_state, os_data_state_saved;
635int os_data_count;
636static int os_data_count_saved;
637static char *os_data_addr, *os_data_addr_saved;
638static char *bin_start_address;
639int os_data_header[8];
640static void bin_data_init (void)
641{
642 os_data_state = 0;
643 os_data_count = 0;
644 os_data_addr = bin_start_address;
645}
646static void os_data_save (void)
647{
648 os_data_state_saved = os_data_state;
649 os_data_count_saved = os_data_count;
650 os_data_addr_saved = os_data_addr;
651}
652static void os_data_restore (void)
653{
654 os_data_state = os_data_state_saved;
655 os_data_count = os_data_count_saved;
656 os_data_addr = os_data_addr_saved;
657}
658static void bin_data_char (char new_char)
659{
660 switch (os_data_state) {
661 case 0: /* data */
662 *os_data_addr++ = new_char;
663 --os_data_count;
664 break;
665 }
666}
667static void set_kerm_bin_mode (unsigned long *addr)
668{
669 bin_start_address = (char *) addr;
670 os_data_init = bin_data_init;
671 os_data_char = bin_data_char;
672}
673
674
675/* k_data_* simply handles the kermit escape translations */
676static int k_data_escape, k_data_escape_saved;
677void k_data_init (void)
678{
679 k_data_escape = 0;
680 os_data_init ();
681}
682void k_data_save (void)
683{
684 k_data_escape_saved = k_data_escape;
685 os_data_save ();
686}
687void k_data_restore (void)
688{
689 k_data_escape = k_data_escape_saved;
690 os_data_restore ();
691}
692void k_data_char (char new_char)
693{
694 if (k_data_escape) {
695 /* last char was escape - translate this character */
696 os_data_char (ktrans (new_char));
697 k_data_escape = 0;
698 } else {
699 if (new_char == his_quote) {
700 /* this char is escape - remember */
701 k_data_escape = 1;
702 } else {
703 /* otherwise send this char as-is */
704 os_data_char (new_char);
705 }
706 }
707}
708
709#define SEND_DATA_SIZE 20
710char send_parms[SEND_DATA_SIZE];
711char *send_ptr;
712
713/* handle_send_packet interprits the protocol info and builds and
714 sends an appropriate ack for what we can do */
715void handle_send_packet (int n)
716{
717 int length = 3;
718 int bytes;
719
720 /* initialize some protocol parameters */
721 his_eol = END_CHAR; /* default end of line character */
722 his_pad_count = 0;
723 his_pad_char = '\0';
724 his_quote = K_ESCAPE;
725
726 /* ignore last character if it filled the buffer */
727 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
728 --send_ptr;
729 bytes = send_ptr - send_parms; /* how many bytes we'll process */
730 do {
731 if (bytes-- <= 0)
732 break;
733 /* handle MAXL - max length */
734 /* ignore what he says - most I'll take (here) is 94 */
735 a_b[++length] = tochar (94);
736 if (bytes-- <= 0)
737 break;
738 /* handle TIME - time you should wait for my packets */
739 /* ignore what he says - don't wait for my ack longer than 1 second */
740 a_b[++length] = tochar (1);
741 if (bytes-- <= 0)
742 break;
743 /* handle NPAD - number of pad chars I need */
744 /* remember what he says - I need none */
745 his_pad_count = untochar (send_parms[2]);
746 a_b[++length] = tochar (0);
747 if (bytes-- <= 0)
748 break;
749 /* handle PADC - pad chars I need */
750 /* remember what he says - I need none */
751 his_pad_char = ktrans (send_parms[3]);
752 a_b[++length] = 0x40; /* He should ignore this */
753 if (bytes-- <= 0)
754 break;
755 /* handle EOL - end of line he needs */
756 /* remember what he says - I need CR */
757 his_eol = untochar (send_parms[4]);
758 a_b[++length] = tochar (END_CHAR);
759 if (bytes-- <= 0)
760 break;
761 /* handle QCTL - quote control char he'll use */
762 /* remember what he says - I'll use '#' */
763 his_quote = send_parms[5];
764 a_b[++length] = '#';
765 if (bytes-- <= 0)
766 break;
767 /* handle QBIN - 8-th bit prefixing */
768 /* ignore what he says - I refuse */
769 a_b[++length] = 'N';
770 if (bytes-- <= 0)
771 break;
772 /* handle CHKT - the clock check type */
773 /* ignore what he says - I do type 1 (for now) */
774 a_b[++length] = '1';
775 if (bytes-- <= 0)
776 break;
777 /* handle REPT - the repeat prefix */
778 /* ignore what he says - I refuse (for now) */
779 a_b[++length] = 'N';
780 if (bytes-- <= 0)
781 break;
782 /* handle CAPAS - the capabilities mask */
783 /* ignore what he says - I only do long packets - I don't do windows */
784 a_b[++length] = tochar (2); /* only long packets */
785 a_b[++length] = tochar (0); /* no windows */
786 a_b[++length] = tochar (94); /* large packet msb */
787 a_b[++length] = tochar (94); /* large packet lsb */
788 } while (0);
789
790 a_b[0] = START_CHAR;
791 a_b[1] = tochar (length);
792 a_b[2] = tochar (n);
793 a_b[3] = ACK_TYPE;
794 a_b[++length] = '\0';
795 a_b[length] = tochar (chk1 (&a_b[1]));
796 a_b[++length] = his_eol;
797 a_b[++length] = '\0';
798 s1_sendpacket (a_b);
799}
800
801/* k_recv receives a OS Open image file over kermit line */
802static int k_recv (void)
803{
804 char new_char;
805 char k_state, k_state_saved;
806 int sum;
807 int done;
808 int length;
809 int n, last_n;
810 int z = 0;
811 int len_lo, len_hi;
812
813 /* initialize some protocol parameters */
814 his_eol = END_CHAR; /* default end of line character */
815 his_pad_count = 0;
816 his_pad_char = '\0';
817 his_quote = K_ESCAPE;
818
819 /* initialize the k_recv and k_data state machine */
820 done = 0;
821 k_state = 0;
822 k_data_init ();
823 k_state_saved = k_state;
824 k_data_save ();
825 n = 0; /* just to get rid of a warning */
826 last_n = -1;
827
828 /* expect this "type" sequence (but don't check):
829 S: send initiate
830 F: file header
831 D: data (multiple)
832 Z: end of file
833 B: break transmission
834 */
835
836 /* enter main loop */
837 while (!done) {
838 /* set the send packet pointer to begining of send packet parms */
839 send_ptr = send_parms;
840
841 /* With each packet, start summing the bytes starting with the length.
842 Save the current sequence number.
843 Note the type of the packet.
844 If a character less than SPACE (0x20) is received - error.
845 */
846
847#if 0
848 /* OLD CODE, Prior to checking sequence numbers */
849 /* first have all state machines save current states */
850 k_state_saved = k_state;
851 k_data_save ();
852#endif
853
854 /* get a packet */
855 /* wait for the starting character or ^C */
856 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000857 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000858 case START_CHAR: /* start packet */
859 goto START;
860 case ETX_CHAR: /* ^C waiting for packet */
861 return (0);
862 default:
863 ;
864 }
865 }
866START:
867 /* get length of packet */
868 sum = 0;
wdenk232c1502004-03-12 00:14:09 +0000869 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000870 if ((new_char & 0xE0) == 0)
871 goto packet_error;
872 sum += new_char & 0xff;
873 length = untochar (new_char);
874 /* get sequence number */
wdenk232c1502004-03-12 00:14:09 +0000875 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000876 if ((new_char & 0xE0) == 0)
877 goto packet_error;
878 sum += new_char & 0xff;
879 n = untochar (new_char);
880 --length;
881
882 /* NEW CODE - check sequence numbers for retried packets */
883 /* Note - this new code assumes that the sequence number is correctly
884 * received. Handling an invalid sequence number adds another layer
885 * of complexity that may not be needed - yet! At this time, I'm hoping
886 * that I don't need to buffer the incoming data packets and can write
887 * the data into memory in real time.
888 */
889 if (n == last_n) {
890 /* same sequence number, restore the previous state */
891 k_state = k_state_saved;
892 k_data_restore ();
893 } else {
894 /* new sequence number, checkpoint the download */
895 last_n = n;
896 k_state_saved = k_state;
897 k_data_save ();
898 }
899 /* END NEW CODE */
900
901 /* get packet type */
wdenk232c1502004-03-12 00:14:09 +0000902 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000903 if ((new_char & 0xE0) == 0)
904 goto packet_error;
905 sum += new_char & 0xff;
906 k_state = new_char;
907 --length;
908 /* check for extended length */
909 if (length == -2) {
910 /* (length byte was 0, decremented twice) */
911 /* get the two length bytes */
wdenk232c1502004-03-12 00:14:09 +0000912 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000913 if ((new_char & 0xE0) == 0)
914 goto packet_error;
915 sum += new_char & 0xff;
916 len_hi = untochar (new_char);
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 len_lo = untochar (new_char);
922 length = len_hi * 95 + len_lo;
923 /* check header checksum */
wdenk232c1502004-03-12 00:14:09 +0000924 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000925 if ((new_char & 0xE0) == 0)
926 goto packet_error;
927 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
928 goto packet_error;
929 sum += new_char & 0xff;
930/* --length; */ /* new length includes only data and block check to come */
931 }
932 /* bring in rest of packet */
933 while (length > 1) {
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 sum += new_char & 0xff;
938 --length;
939 if (k_state == DATA_TYPE) {
940 /* pass on the data if this is a data packet */
941 k_data_char (new_char);
942 } else if (k_state == SEND_TYPE) {
943 /* save send pack in buffer as is */
944 *send_ptr++ = new_char;
945 /* if too much data, back off the pointer */
946 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
947 --send_ptr;
948 }
949 }
950 /* get and validate checksum character */
wdenk232c1502004-03-12 00:14:09 +0000951 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000952 if ((new_char & 0xE0) == 0)
953 goto packet_error;
954 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
955 goto packet_error;
956 /* get END_CHAR */
wdenk232c1502004-03-12 00:14:09 +0000957 new_char = getc ();
wdenk8bde7f72003-06-27 21:31:46 +0000958 if (new_char != END_CHAR) {
959 packet_error:
960 /* restore state machines */
961 k_state = k_state_saved;
962 k_data_restore ();
963 /* send a negative acknowledge packet in */
964 send_nack (n);
965 } else if (k_state == SEND_TYPE) {
966 /* crack the protocol parms, build an appropriate ack packet */
967 handle_send_packet (n);
968 } else {
969 /* send simple acknowledge packet in */
970 send_ack (n);
971 /* quit if end of transmission */
972 if (k_state == BREAK_TYPE)
973 done = 1;
974 }
975 ++z;
976 }
977 return ((ulong) os_data_addr - (ulong) bin_start_address);
978}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200979
980static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200981 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200982 return (getc());
983 return -1;
984}
985static ulong load_serial_ymodem (ulong offset)
986{
987 int size;
988 char buf[32];
989 int err;
990 int res;
991 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200992 char ymodemBuf[1024];
993 ulong store_addr = ~0;
994 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200995
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200996 size = 0;
997 info.mode = xyzModem_ymodem;
998 res = xyzModem_stream_open (&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200999 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001000
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001001 while ((res =
1002 xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) {
1003 store_addr = addr + offset;
1004 size += res;
1005 addr += res;
1006#ifndef CFG_NO_FLASH
1007 if (addr2info (store_addr)) {
1008 int rc;
1009
1010 rc = flash_write ((char *) ymodemBuf,
1011 store_addr, res);
1012 if (rc != 0) {
1013 flash_perror (rc);
1014 return (~0);
1015 }
1016 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001017#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001018 {
1019 memcpy ((char *) (store_addr), ymodemBuf,
1020 res);
1021 }
1022
1023 }
1024 } else {
1025 printf ("%s\n", xyzModem_error (err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001026 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001027
1028 xyzModem_stream_close (&err);
1029 xyzModem_stream_terminate (false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001030
1031
1032 flush_cache (offset, size);
1033
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001034 printf ("## Total Size = 0x%08x = %d Bytes\n", size, size);
1035 sprintf (buf, "%X", size);
1036 setenv ("filesize", buf);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001037
1038 return offset;
1039}
1040
Jon Loeliger90253172007-07-10 11:02:44 -05001041#endif
wdenk8bde7f72003-06-27 21:31:46 +00001042
1043/* -------------------------------------------------------------------- */
1044
Jon Loeligerc76fe472007-07-08 18:02:23 -05001045#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001046
1047#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001048U_BOOT_CMD(
1049 loads, 3, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001050 "loads - load S-Record file over serial line\n",
1051 "[ off ] [ baud ]\n"
1052 " - load S-Record file over serial line"
1053 " with offset 'off' and baudrate 'baud'\n"
1054);
1055
1056#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001057U_BOOT_CMD(
1058 loads, 2, 0, do_load_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001059 "loads - load S-Record file over serial line\n",
1060 "[ off ]\n"
1061 " - load S-Record file over serial line with offset 'off'\n"
1062);
1063#endif /* CFG_LOADS_BAUD_CHANGE */
1064
1065/*
1066 * SAVES always requires LOADS support, but not vice versa
1067 */
1068
1069
Jon Loeligerc76fe472007-07-08 18:02:23 -05001070#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +00001071#ifdef CFG_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001072U_BOOT_CMD(
1073 saves, 4, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001074 "saves - save S-Record file over serial line\n",
1075 "[ off ] [size] [ baud ]\n"
1076 " - save S-Record file over serial line"
1077 " with offset 'off', size 'size' and baudrate 'baud'\n"
1078);
1079#else /* ! CFG_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001080U_BOOT_CMD(
1081 saves, 3, 0, do_save_serial,
wdenk8bde7f72003-06-27 21:31:46 +00001082 "saves - save S-Record file over serial line\n",
1083 "[ off ] [size]\n"
1084 " - save S-Record file over serial line with offset 'off' and size 'size'\n"
1085);
1086#endif /* CFG_LOADS_BAUD_CHANGE */
Jon Loeliger90253172007-07-10 11:02:44 -05001087#endif
1088#endif
wdenk8bde7f72003-06-27 21:31:46 +00001089
1090
Jon Loeligerc76fe472007-07-08 18:02:23 -05001091#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001092U_BOOT_CMD(
1093 loadb, 3, 0, do_load_serial_bin,
wdenk8bde7f72003-06-27 21:31:46 +00001094 "loadb - load binary file over serial line (kermit mode)\n",
1095 "[ off ] [ baud ]\n"
1096 " - load binary file over serial line"
1097 " with offset 'off' and baudrate 'baud'\n"
1098);
1099
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001100U_BOOT_CMD(
1101 loady, 3, 0, do_load_serial_bin,
1102 "loady - load binary file over serial line (ymodem mode)\n",
1103 "[ off ] [ baud ]\n"
1104 " - load binary file over serial line"
1105 " with offset 'off' and baudrate 'baud'\n"
1106);
1107
Jon Loeliger90253172007-07-10 11:02:44 -05001108#endif
wdenk8bde7f72003-06-27 21:31:46 +00001109
1110/* -------------------------------------------------------------------- */
1111
Jon Loeligerc76fe472007-07-08 18:02:23 -05001112#if defined(CONFIG_CMD_HWFLOW)
wdenk8bde7f72003-06-27 21:31:46 +00001113int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1114{
1115 extern int hwflow_onoff(int);
1116
1117 if (argc == 2) {
1118 if (strcmp(argv[1], "off") == 0)
1119 hwflow_onoff(-1);
1120 else
1121 if (strcmp(argv[1], "on") == 0)
1122 hwflow_onoff(1);
1123 else
1124 printf("Usage: %s\n", cmdtp->usage);
1125 }
1126 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1127 return 0;
1128}
1129
1130/* -------------------------------------------------------------------- */
1131
wdenk0d498392003-07-01 21:06:45 +00001132U_BOOT_CMD(
wdenkf12e5682003-07-07 20:07:54 +00001133 hwflow, 2, 0, do_hwflow,
wdenk8bde7f72003-06-27 21:31:46 +00001134 "hwflow - turn the harwdare flow control on/off\n",
wdenkf12e5682003-07-07 20:07:54 +00001135 "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
wdenk8bde7f72003-06-27 21:31:46 +00001136);
1137
Jon Loeliger90253172007-07-10 11:02:44 -05001138#endif