]>
Commit | Line | Data |
---|---|---|
8bde7f77 | 1 | /* |
232c150a | 2 | * (C) Copyright 2000-2004 |
8bde7f77 WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
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> | |
8bde7f77 WD |
29 | #include <s_record.h> |
30 | #include <net.h> | |
27b207fd | 31 | #include <exports.h> |
f2841d37 | 32 | #include <xyzModem.h> |
8bde7f77 | 33 | |
d87080b7 | 34 | DECLARE_GLOBAL_DATA_PTR; |
8bde7f77 | 35 | |
c76fe474 | 36 | #if defined(CONFIG_CMD_LOADB) |
8546e239 WD |
37 | static ulong load_serial_ymodem (ulong offset); |
38 | #endif | |
39 | ||
c76fe474 | 40 | #if defined(CONFIG_CMD_LOADS) |
2b22d608 | 41 | static ulong load_serial (long offset); |
8bde7f77 | 42 | static int read_record (char *buf, ulong len); |
c76fe474 | 43 | # if defined(CONFIG_CMD_SAVES) |
8bde7f77 WD |
44 | static int save_serial (ulong offset, ulong size); |
45 | static int write_record (char *buf); | |
90253178 | 46 | #endif |
8bde7f77 WD |
47 | |
48 | static int do_echo = 1; | |
90253178 | 49 | #endif |
8bde7f77 WD |
50 | |
51 | /* -------------------------------------------------------------------- */ | |
52 | ||
c76fe474 | 53 | #if defined(CONFIG_CMD_LOADS) |
8bde7f77 WD |
54 | int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
55 | { | |
2b22d608 | 56 | long offset = 0; |
8bde7f77 WD |
57 | ulong addr; |
58 | int i; | |
59 | char *env_echo; | |
60 | int rcode = 0; | |
6d0f6bcf | 61 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 WD |
62 | 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 | ||
6d0f6bcf | 73 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 | 74 | if (argc >= 2) { |
2b22d608 | 75 | offset = simple_strtol(argv[1], NULL, 16); |
8bde7f77 WD |
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 | } | |
6d0f6bcf | 96 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
8bde7f77 | 97 | if (argc == 2) { |
2b22d608 | 98 | offset = simple_strtol(argv[1], NULL, 16); |
8bde7f77 | 99 | } |
6d0f6bcf | 100 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
8bde7f77 WD |
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) { | |
232c150a WD |
112 | if (tstc()) { |
113 | (void) getc(); | |
8bde7f77 WD |
114 | } |
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 | ||
6d0f6bcf | 126 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 WD |
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 | ||
143 | static ulong | |
2b22d608 | 144 | load_serial (long offset) |
8bde7f77 WD |
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; | |
6d0f6bcf | 170 | #ifndef CONFIG_SYS_NO_FLASH |
8bde7f77 WD |
171 | if (addr2info(store_addr)) { |
172 | int rc; | |
173 | ||
77ddac94 | 174 | rc = flash_write((char *)binbuf,store_addr,binlen); |
8bde7f77 WD |
175 | 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 | ); | |
3f85ce27 | 200 | flush_cache (start_addr, size); |
8bde7f77 WD |
201 | 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 | ||
218 | static int | |
219 | read_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) { | |
232c150a | 227 | c = getc(); /* read character */ |
8bde7f77 | 228 | if (do_echo) |
232c150a | 229 | putc (c); /* ... and echo it */ |
8bde7f77 WD |
230 | |
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) */ | |
232c150a | 244 | if (gd->jt[XF_getc] != getc) { |
8bde7f77 WD |
245 | if (ctrlc()) { |
246 | return (-1); | |
247 | } | |
248 | } | |
8bde7f77 WD |
249 | } |
250 | ||
251 | /* line too long - truncate */ | |
252 | *p = '\0'; | |
253 | return (p - buf); | |
254 | } | |
255 | ||
c76fe474 | 256 | #if defined(CONFIG_CMD_SAVES) |
8bde7f77 WD |
257 | |
258 | int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
259 | { | |
260 | ulong offset = 0; | |
261 | ulong size = 0; | |
6d0f6bcf | 262 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 WD |
263 | 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 | } | |
6d0f6bcf | 271 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 WD |
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 | } | |
6d0f6bcf | 294 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
8bde7f77 WD |
295 | if (argc == 3) { |
296 | size = simple_strtoul(argv[2], NULL, 16); | |
297 | } | |
6d0f6bcf | 298 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
8bde7f77 WD |
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 | } | |
6d0f6bcf | 310 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
8bde7f77 WD |
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 | ||
332 | static 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 | ||
387 | static int | |
388 | write_record (char *buf) | |
389 | { | |
390 | char c; | |
391 | ||
392 | while((c = *buf++)) | |
232c150a | 393 | putc(c); |
8bde7f77 WD |
394 | |
395 | /* Check for the console hangup (if any different from serial) */ | |
396 | ||
397 | if (ctrlc()) { | |
398 | return (-1); | |
399 | } | |
400 | return (0); | |
401 | } | |
90253178 | 402 | # endif |
8bde7f77 | 403 | |
90253178 | 404 | #endif |
8bde7f77 WD |
405 | |
406 | ||
c76fe474 | 407 | #if defined(CONFIG_CMD_LOADB) |
65c450b4 JL |
408 | /* |
409 | * loadb command (load binary) included | |
410 | */ | |
8bde7f77 WD |
411 | #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 | ||
8bde7f77 WD |
426 | static void set_kerm_bin_mode(unsigned long *); |
427 | static int k_recv(void); | |
428 | static ulong load_serial_bin (ulong offset); | |
429 | ||
430 | ||
431 | char his_eol; /* character he needs at end of packet */ | |
432 | int his_pad_count; /* number of pad chars he needs */ | |
433 | char his_pad_char; /* pad chars he needs */ | |
434 | char his_quote; /* quote chars he'll use */ | |
435 | ||
436 | int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
437 | { | |
8bde7f77 WD |
438 | ulong offset = 0; |
439 | ulong addr; | |
440 | int load_baudrate, current_baudrate; | |
441 | int rcode = 0; | |
442 | char *s; | |
443 | ||
6d0f6bcf JCPV |
444 | /* pre-set offset from CONFIG_SYS_LOAD_ADDR */ |
445 | offset = CONFIG_SYS_LOAD_ADDR; | |
8bde7f77 WD |
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 | ||
f2841d37 MK |
478 | if (strcmp(argv[0],"loady")==0) { |
479 | printf ("## Ready for binary (ymodem) download " | |
480 | "to 0x%08lX at %d bps...\n", | |
481 | offset, | |
482 | load_baudrate); | |
483 | ||
484 | addr = load_serial_ymodem (offset); | |
8bde7f77 | 485 | |
8bde7f77 | 486 | } else { |
8bde7f77 | 487 | |
f2841d37 MK |
488 | printf ("## Ready for binary (kermit) download " |
489 | "to 0x%08lX at %d bps...\n", | |
490 | offset, | |
491 | load_baudrate); | |
492 | addr = load_serial_bin (offset); | |
493 | ||
494 | if (addr == ~0) { | |
495 | load_addr = 0; | |
496 | printf ("## Binary (kermit) download aborted\n"); | |
497 | rcode = 1; | |
498 | } else { | |
499 | printf ("## Start Addr = 0x%08lX\n", addr); | |
500 | load_addr = addr; | |
501 | } | |
502 | } | |
8bde7f77 WD |
503 | if (load_baudrate != current_baudrate) { |
504 | printf ("## Switch baudrate to %d bps and press ESC ...\n", | |
505 | current_baudrate); | |
506 | udelay (50000); | |
507 | gd->baudrate = current_baudrate; | |
508 | serial_setbrg (); | |
509 | udelay (50000); | |
510 | for (;;) { | |
511 | if (getc() == 0x1B) /* ESC */ | |
512 | break; | |
513 | } | |
514 | } | |
515 | ||
516 | #ifdef CONFIG_AUTOSCRIPT | |
517 | if (load_addr) { | |
518 | char *s; | |
519 | ||
520 | if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) { | |
424c4abd MB |
521 | printf ("Running autoscript at addr 0x%08lX", load_addr); |
522 | ||
523 | s = getenv ("autoscript_uname"); | |
524 | if (s) | |
525 | printf (":%s ...\n", s); | |
526 | else | |
527 | puts (" ...\n"); | |
528 | ||
529 | rcode = autoscript (load_addr, s); | |
8bde7f77 WD |
530 | } |
531 | } | |
532 | #endif | |
533 | return rcode; | |
534 | } | |
535 | ||
536 | ||
537 | static ulong load_serial_bin (ulong offset) | |
538 | { | |
539 | int size, i; | |
540 | char buf[32]; | |
541 | ||
542 | set_kerm_bin_mode ((ulong *) offset); | |
543 | size = k_recv (); | |
544 | ||
545 | /* | |
546 | * Gather any trailing characters (for instance, the ^D which | |
547 | * is sent by 'cu' after sending a file), and give the | |
548 | * box some time (100 * 1 ms) | |
549 | */ | |
550 | for (i=0; i<100; ++i) { | |
232c150a WD |
551 | if (tstc()) { |
552 | (void) getc(); | |
8bde7f77 WD |
553 | } |
554 | udelay(1000); | |
555 | } | |
556 | ||
557 | flush_cache (offset, size); | |
558 | ||
559 | printf("## Total Size = 0x%08x = %d Bytes\n", size, size); | |
560 | sprintf(buf, "%X", size); | |
561 | setenv("filesize", buf); | |
562 | ||
563 | return offset; | |
564 | } | |
565 | ||
566 | void send_pad (void) | |
567 | { | |
568 | int count = his_pad_count; | |
569 | ||
570 | while (count-- > 0) | |
232c150a | 571 | putc (his_pad_char); |
8bde7f77 WD |
572 | } |
573 | ||
574 | /* converts escaped kermit char to binary char */ | |
575 | char ktrans (char in) | |
576 | { | |
577 | if ((in & 0x60) == 0x40) { | |
578 | return (char) (in & ~0x40); | |
579 | } else if ((in & 0x7f) == 0x3f) { | |
580 | return (char) (in | 0x40); | |
581 | } else | |
582 | return in; | |
583 | } | |
584 | ||
585 | int chk1 (char *buffer) | |
586 | { | |
587 | int total = 0; | |
588 | ||
589 | while (*buffer) { | |
590 | total += *buffer++; | |
591 | } | |
592 | return (int) ((total + ((total >> 6) & 0x03)) & 0x3f); | |
593 | } | |
594 | ||
595 | void s1_sendpacket (char *packet) | |
596 | { | |
597 | send_pad (); | |
598 | while (*packet) { | |
232c150a | 599 | putc (*packet++); |
8bde7f77 WD |
600 | } |
601 | } | |
602 | ||
603 | static char a_b[24]; | |
604 | void send_ack (int n) | |
605 | { | |
606 | a_b[0] = START_CHAR; | |
607 | a_b[1] = tochar (3); | |
608 | a_b[2] = tochar (n); | |
609 | a_b[3] = ACK_TYPE; | |
610 | a_b[4] = '\0'; | |
611 | a_b[4] = tochar (chk1 (&a_b[1])); | |
612 | a_b[5] = his_eol; | |
613 | a_b[6] = '\0'; | |
614 | s1_sendpacket (a_b); | |
615 | } | |
616 | ||
617 | void send_nack (int n) | |
618 | { | |
619 | a_b[0] = START_CHAR; | |
620 | a_b[1] = tochar (3); | |
621 | a_b[2] = tochar (n); | |
622 | a_b[3] = NACK_TYPE; | |
623 | a_b[4] = '\0'; | |
624 | a_b[4] = tochar (chk1 (&a_b[1])); | |
625 | a_b[5] = his_eol; | |
626 | a_b[6] = '\0'; | |
627 | s1_sendpacket (a_b); | |
628 | } | |
629 | ||
630 | ||
8bde7f77 WD |
631 | void (*os_data_init) (void); |
632 | void (*os_data_char) (char new_char); | |
633 | static int os_data_state, os_data_state_saved; | |
8bde7f77 WD |
634 | static char *os_data_addr, *os_data_addr_saved; |
635 | static char *bin_start_address; | |
a9fe0c3e | 636 | |
8bde7f77 WD |
637 | static void bin_data_init (void) |
638 | { | |
639 | os_data_state = 0; | |
8bde7f77 WD |
640 | os_data_addr = bin_start_address; |
641 | } | |
a9fe0c3e | 642 | |
8bde7f77 WD |
643 | static void os_data_save (void) |
644 | { | |
645 | os_data_state_saved = os_data_state; | |
8bde7f77 WD |
646 | os_data_addr_saved = os_data_addr; |
647 | } | |
a9fe0c3e | 648 | |
8bde7f77 WD |
649 | static void os_data_restore (void) |
650 | { | |
651 | os_data_state = os_data_state_saved; | |
8bde7f77 WD |
652 | os_data_addr = os_data_addr_saved; |
653 | } | |
a9fe0c3e | 654 | |
8bde7f77 WD |
655 | static void bin_data_char (char new_char) |
656 | { | |
657 | switch (os_data_state) { | |
658 | case 0: /* data */ | |
659 | *os_data_addr++ = new_char; | |
8bde7f77 WD |
660 | break; |
661 | } | |
662 | } | |
a9fe0c3e | 663 | |
8bde7f77 WD |
664 | static void set_kerm_bin_mode (unsigned long *addr) |
665 | { | |
666 | bin_start_address = (char *) addr; | |
667 | os_data_init = bin_data_init; | |
668 | os_data_char = bin_data_char; | |
669 | } | |
670 | ||
671 | ||
672 | /* k_data_* simply handles the kermit escape translations */ | |
673 | static int k_data_escape, k_data_escape_saved; | |
674 | void k_data_init (void) | |
675 | { | |
676 | k_data_escape = 0; | |
677 | os_data_init (); | |
678 | } | |
a9fe0c3e | 679 | |
8bde7f77 WD |
680 | void k_data_save (void) |
681 | { | |
682 | k_data_escape_saved = k_data_escape; | |
683 | os_data_save (); | |
684 | } | |
a9fe0c3e | 685 | |
8bde7f77 WD |
686 | void k_data_restore (void) |
687 | { | |
688 | k_data_escape = k_data_escape_saved; | |
689 | os_data_restore (); | |
690 | } | |
a9fe0c3e | 691 | |
8bde7f77 WD |
692 | void 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 | |
710 | char send_parms[SEND_DATA_SIZE]; | |
711 | char *send_ptr; | |
712 | ||
713 | /* handle_send_packet interprits the protocol info and builds and | |
714 | sends an appropriate ack for what we can do */ | |
715 | void 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 */ | |
802 | static 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; | |
8bde7f77 WD |
810 | int len_lo, len_hi; |
811 | ||
812 | /* initialize some protocol parameters */ | |
813 | his_eol = END_CHAR; /* default end of line character */ | |
814 | his_pad_count = 0; | |
815 | his_pad_char = '\0'; | |
816 | his_quote = K_ESCAPE; | |
817 | ||
818 | /* initialize the k_recv and k_data state machine */ | |
819 | done = 0; | |
820 | k_state = 0; | |
821 | k_data_init (); | |
822 | k_state_saved = k_state; | |
823 | k_data_save (); | |
824 | n = 0; /* just to get rid of a warning */ | |
825 | last_n = -1; | |
826 | ||
827 | /* expect this "type" sequence (but don't check): | |
828 | S: send initiate | |
829 | F: file header | |
830 | D: data (multiple) | |
831 | Z: end of file | |
832 | B: break transmission | |
833 | */ | |
834 | ||
835 | /* enter main loop */ | |
836 | while (!done) { | |
837 | /* set the send packet pointer to begining of send packet parms */ | |
838 | send_ptr = send_parms; | |
839 | ||
840 | /* With each packet, start summing the bytes starting with the length. | |
841 | Save the current sequence number. | |
842 | Note the type of the packet. | |
843 | If a character less than SPACE (0x20) is received - error. | |
844 | */ | |
845 | ||
846 | #if 0 | |
847 | /* OLD CODE, Prior to checking sequence numbers */ | |
848 | /* first have all state machines save current states */ | |
849 | k_state_saved = k_state; | |
850 | k_data_save (); | |
851 | #endif | |
852 | ||
853 | /* get a packet */ | |
854 | /* wait for the starting character or ^C */ | |
855 | for (;;) { | |
232c150a | 856 | switch (getc ()) { |
8bde7f77 WD |
857 | case START_CHAR: /* start packet */ |
858 | goto START; | |
859 | case ETX_CHAR: /* ^C waiting for packet */ | |
860 | return (0); | |
861 | default: | |
862 | ; | |
863 | } | |
864 | } | |
865 | START: | |
866 | /* get length of packet */ | |
867 | sum = 0; | |
232c150a | 868 | new_char = getc (); |
8bde7f77 WD |
869 | if ((new_char & 0xE0) == 0) |
870 | goto packet_error; | |
871 | sum += new_char & 0xff; | |
872 | length = untochar (new_char); | |
873 | /* get sequence number */ | |
232c150a | 874 | new_char = getc (); |
8bde7f77 WD |
875 | if ((new_char & 0xE0) == 0) |
876 | goto packet_error; | |
877 | sum += new_char & 0xff; | |
878 | n = untochar (new_char); | |
879 | --length; | |
880 | ||
881 | /* NEW CODE - check sequence numbers for retried packets */ | |
882 | /* Note - this new code assumes that the sequence number is correctly | |
883 | * received. Handling an invalid sequence number adds another layer | |
884 | * of complexity that may not be needed - yet! At this time, I'm hoping | |
885 | * that I don't need to buffer the incoming data packets and can write | |
886 | * the data into memory in real time. | |
887 | */ | |
888 | if (n == last_n) { | |
889 | /* same sequence number, restore the previous state */ | |
890 | k_state = k_state_saved; | |
891 | k_data_restore (); | |
892 | } else { | |
893 | /* new sequence number, checkpoint the download */ | |
894 | last_n = n; | |
895 | k_state_saved = k_state; | |
896 | k_data_save (); | |
897 | } | |
898 | /* END NEW CODE */ | |
899 | ||
900 | /* get packet type */ | |
232c150a | 901 | new_char = getc (); |
8bde7f77 WD |
902 | if ((new_char & 0xE0) == 0) |
903 | goto packet_error; | |
904 | sum += new_char & 0xff; | |
905 | k_state = new_char; | |
906 | --length; | |
907 | /* check for extended length */ | |
908 | if (length == -2) { | |
909 | /* (length byte was 0, decremented twice) */ | |
910 | /* get the two length bytes */ | |
232c150a | 911 | new_char = getc (); |
8bde7f77 WD |
912 | if ((new_char & 0xE0) == 0) |
913 | goto packet_error; | |
914 | sum += new_char & 0xff; | |
915 | len_hi = untochar (new_char); | |
232c150a | 916 | new_char = getc (); |
8bde7f77 WD |
917 | if ((new_char & 0xE0) == 0) |
918 | goto packet_error; | |
919 | sum += new_char & 0xff; | |
920 | len_lo = untochar (new_char); | |
921 | length = len_hi * 95 + len_lo; | |
922 | /* check header checksum */ | |
232c150a | 923 | new_char = getc (); |
8bde7f77 WD |
924 | if ((new_char & 0xE0) == 0) |
925 | goto packet_error; | |
926 | if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f)) | |
927 | goto packet_error; | |
928 | sum += new_char & 0xff; | |
929 | /* --length; */ /* new length includes only data and block check to come */ | |
930 | } | |
931 | /* bring in rest of packet */ | |
932 | while (length > 1) { | |
232c150a | 933 | new_char = getc (); |
8bde7f77 WD |
934 | if ((new_char & 0xE0) == 0) |
935 | goto packet_error; | |
936 | sum += new_char & 0xff; | |
937 | --length; | |
938 | if (k_state == DATA_TYPE) { | |
939 | /* pass on the data if this is a data packet */ | |
940 | k_data_char (new_char); | |
941 | } else if (k_state == SEND_TYPE) { | |
942 | /* save send pack in buffer as is */ | |
943 | *send_ptr++ = new_char; | |
944 | /* if too much data, back off the pointer */ | |
945 | if (send_ptr >= &send_parms[SEND_DATA_SIZE]) | |
946 | --send_ptr; | |
947 | } | |
948 | } | |
949 | /* get and validate checksum character */ | |
232c150a | 950 | new_char = getc (); |
8bde7f77 WD |
951 | if ((new_char & 0xE0) == 0) |
952 | goto packet_error; | |
953 | if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f)) | |
954 | goto packet_error; | |
955 | /* get END_CHAR */ | |
232c150a | 956 | new_char = getc (); |
8bde7f77 WD |
957 | if (new_char != END_CHAR) { |
958 | packet_error: | |
959 | /* restore state machines */ | |
960 | k_state = k_state_saved; | |
961 | k_data_restore (); | |
962 | /* send a negative acknowledge packet in */ | |
963 | send_nack (n); | |
964 | } else if (k_state == SEND_TYPE) { | |
965 | /* crack the protocol parms, build an appropriate ack packet */ | |
966 | handle_send_packet (n); | |
967 | } else { | |
968 | /* send simple acknowledge packet in */ | |
969 | send_ack (n); | |
970 | /* quit if end of transmission */ | |
971 | if (k_state == BREAK_TYPE) | |
972 | done = 1; | |
973 | } | |
8bde7f77 WD |
974 | } |
975 | return ((ulong) os_data_addr - (ulong) bin_start_address); | |
976 | } | |
f2841d37 MK |
977 | |
978 | static int getcxmodem(void) { | |
cf48eb9a | 979 | if (tstc()) |
f2841d37 MK |
980 | return (getc()); |
981 | return -1; | |
982 | } | |
983 | static ulong load_serial_ymodem (ulong offset) | |
984 | { | |
985 | int size; | |
986 | char buf[32]; | |
987 | int err; | |
988 | int res; | |
989 | connection_info_t info; | |
cf48eb9a WD |
990 | char ymodemBuf[1024]; |
991 | ulong store_addr = ~0; | |
992 | ulong addr = 0; | |
f2841d37 | 993 | |
cf48eb9a WD |
994 | size = 0; |
995 | info.mode = xyzModem_ymodem; | |
996 | res = xyzModem_stream_open (&info, &err); | |
f2841d37 | 997 | if (!res) { |
f2841d37 | 998 | |
cf48eb9a WD |
999 | while ((res = |
1000 | xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) { | |
1001 | store_addr = addr + offset; | |
1002 | size += res; | |
1003 | addr += res; | |
6d0f6bcf | 1004 | #ifndef CONFIG_SYS_NO_FLASH |
cf48eb9a WD |
1005 | if (addr2info (store_addr)) { |
1006 | int rc; | |
1007 | ||
1008 | rc = flash_write ((char *) ymodemBuf, | |
1009 | store_addr, res); | |
1010 | if (rc != 0) { | |
1011 | flash_perror (rc); | |
1012 | return (~0); | |
1013 | } | |
1014 | } else | |
f2841d37 | 1015 | #endif |
cf48eb9a WD |
1016 | { |
1017 | memcpy ((char *) (store_addr), ymodemBuf, | |
1018 | res); | |
1019 | } | |
1020 | ||
1021 | } | |
1022 | } else { | |
1023 | printf ("%s\n", xyzModem_error (err)); | |
f2841d37 | 1024 | } |
cf48eb9a WD |
1025 | |
1026 | xyzModem_stream_close (&err); | |
1027 | xyzModem_stream_terminate (false, &getcxmodem); | |
f2841d37 MK |
1028 | |
1029 | ||
1030 | flush_cache (offset, size); | |
1031 | ||
cf48eb9a WD |
1032 | printf ("## Total Size = 0x%08x = %d Bytes\n", size, size); |
1033 | sprintf (buf, "%X", size); | |
1034 | setenv ("filesize", buf); | |
f2841d37 MK |
1035 | |
1036 | return offset; | |
1037 | } | |
1038 | ||
90253178 | 1039 | #endif |
8bde7f77 WD |
1040 | |
1041 | /* -------------------------------------------------------------------- */ | |
1042 | ||
c76fe474 | 1043 | #if defined(CONFIG_CMD_LOADS) |
8bde7f77 | 1044 | |
6d0f6bcf | 1045 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
0d498393 WD |
1046 | U_BOOT_CMD( |
1047 | loads, 3, 0, do_load_serial, | |
2fb2604d | 1048 | "load S-Record file over serial line", |
8bde7f77 WD |
1049 | "[ off ] [ baud ]\n" |
1050 | " - load S-Record file over serial line" | |
1051 | " with offset 'off' and baudrate 'baud'\n" | |
1052 | ); | |
1053 | ||
6d0f6bcf | 1054 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
0d498393 WD |
1055 | U_BOOT_CMD( |
1056 | loads, 2, 0, do_load_serial, | |
2fb2604d | 1057 | "load S-Record file over serial line", |
8bde7f77 WD |
1058 | "[ off ]\n" |
1059 | " - load S-Record file over serial line with offset 'off'\n" | |
1060 | ); | |
6d0f6bcf | 1061 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
8bde7f77 WD |
1062 | |
1063 | /* | |
1064 | * SAVES always requires LOADS support, but not vice versa | |
1065 | */ | |
1066 | ||
1067 | ||
c76fe474 | 1068 | #if defined(CONFIG_CMD_SAVES) |
6d0f6bcf | 1069 | #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE |
0d498393 WD |
1070 | U_BOOT_CMD( |
1071 | saves, 4, 0, do_save_serial, | |
2fb2604d | 1072 | "save S-Record file over serial line", |
8bde7f77 WD |
1073 | "[ off ] [size] [ baud ]\n" |
1074 | " - save S-Record file over serial line" | |
1075 | " with offset 'off', size 'size' and baudrate 'baud'\n" | |
1076 | ); | |
6d0f6bcf | 1077 | #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */ |
0d498393 WD |
1078 | U_BOOT_CMD( |
1079 | saves, 3, 0, do_save_serial, | |
2fb2604d | 1080 | "save S-Record file over serial line", |
8bde7f77 WD |
1081 | "[ off ] [size]\n" |
1082 | " - save S-Record file over serial line with offset 'off' and size 'size'\n" | |
1083 | ); | |
6d0f6bcf | 1084 | #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */ |
90253178 JL |
1085 | #endif |
1086 | #endif | |
8bde7f77 WD |
1087 | |
1088 | ||
c76fe474 | 1089 | #if defined(CONFIG_CMD_LOADB) |
0d498393 WD |
1090 | U_BOOT_CMD( |
1091 | loadb, 3, 0, do_load_serial_bin, | |
2fb2604d | 1092 | "load binary file over serial line (kermit mode)", |
8bde7f77 WD |
1093 | "[ off ] [ baud ]\n" |
1094 | " - load binary file over serial line" | |
1095 | " with offset 'off' and baudrate 'baud'\n" | |
1096 | ); | |
1097 | ||
f2841d37 MK |
1098 | U_BOOT_CMD( |
1099 | loady, 3, 0, do_load_serial_bin, | |
2fb2604d | 1100 | "load binary file over serial line (ymodem mode)", |
f2841d37 MK |
1101 | "[ off ] [ baud ]\n" |
1102 | " - load binary file over serial line" | |
1103 | " with offset 'off' and baudrate 'baud'\n" | |
1104 | ); | |
1105 | ||
90253178 | 1106 | #endif |
8bde7f77 WD |
1107 | |
1108 | /* -------------------------------------------------------------------- */ | |
1109 | ||
c76fe474 | 1110 | #if defined(CONFIG_CMD_HWFLOW) |
8bde7f77 WD |
1111 | int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
1112 | { | |
1113 | extern int hwflow_onoff(int); | |
1114 | ||
1115 | if (argc == 2) { | |
1116 | if (strcmp(argv[1], "off") == 0) | |
1117 | hwflow_onoff(-1); | |
1118 | else | |
1119 | if (strcmp(argv[1], "on") == 0) | |
1120 | hwflow_onoff(1); | |
1121 | else | |
62c3ae7c | 1122 | cmd_usage(cmdtp); |
8bde7f77 WD |
1123 | } |
1124 | printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off"); | |
1125 | return 0; | |
1126 | } | |
1127 | ||
1128 | /* -------------------------------------------------------------------- */ | |
1129 | ||
0d498393 | 1130 | U_BOOT_CMD( |
f12e568c | 1131 | hwflow, 2, 0, do_hwflow, |
2fb2604d | 1132 | "turn the harwdare flow control on/off", |
f12e568c | 1133 | "[on|off]\n - change RTS/CTS hardware flow control over serial line\n" |
8bde7f77 WD |
1134 | ); |
1135 | ||
90253178 | 1136 | #endif |