blob: 0832e92b170104a44bcae33000b735ef4463d2c6 [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)
Kim Phillips088f1b12012-10-29 13:34:31 +000037static ulong load_serial_ymodem(ulong offset);
Wolfgang Denk8546e232006-05-02 00:11:25 +020038#endif
39
Jon Loeligerc76fe472007-07-08 18:02:23 -050040#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000041static ulong load_serial(long offset);
42static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050043# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +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)
Kim Phillips088f1b12012-10-29 13:34:31 +000054static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
55 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000056{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020057 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000058 ulong addr;
59 int i;
60 char *env_echo;
61 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000063 int load_baudrate, current_baudrate;
64
65 load_baudrate = current_baudrate = gd->baudrate;
66#endif
67
68 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
69 do_echo = 1;
70 } else {
71 do_echo = 0;
72 }
73
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020074#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000075 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020076 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000077 }
78 if (argc == 3) {
79 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
80
81 /* default to current baudrate */
82 if (load_baudrate == 0)
83 load_baudrate = current_baudrate;
84 }
85 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000086 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000087 load_baudrate);
88 udelay(50000);
89 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000090 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000091 udelay(50000);
92 for (;;) {
93 if (getc() == '\r')
94 break;
95 }
96 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000098 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020099 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000100 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000102
Kim Phillips088f1b12012-10-29 13:34:31 +0000103 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000104
Kim Phillips088f1b12012-10-29 13:34:31 +0000105 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +0000106
107 /*
108 * Gather any trailing characters (for instance, the ^D which
109 * is sent by 'cu' after sending a file), and give the
110 * box some time (100 * 1 ms)
111 */
112 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000113 if (tstc()) {
114 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000115 }
116 udelay(1000);
117 }
118
119 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000120 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000121 rcode = 1;
122 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000123 printf("## Start Addr = 0x%08lX\n", addr);
wdenk8bde7f72003-06-27 21:31:46 +0000124 load_addr = addr;
125 }
126
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200127#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000128 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000129 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000130 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000131 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000132 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000133 serial_setbrg();
134 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000135 for (;;) {
136 if (getc() == 0x1B) /* ESC */
137 break;
138 }
139 }
140#endif
141 return rcode;
142}
143
Kim Phillips088f1b12012-10-29 13:34:31 +0000144static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000145{
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 */
wdenk8bde7f72003-06-27 21:31:46 +0000152 ulong store_addr;
153 ulong start_addr = ~0;
154 ulong end_addr = 0;
155 int line_count = 0;
156
157 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000158 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000159
160 if (type < 0) {
161 return (~0); /* Invalid S-Record */
162 }
163
164 switch (type) {
165 case SREC_DATA2:
166 case SREC_DATA3:
167 case SREC_DATA4:
168 store_addr = addr + offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169#ifndef CONFIG_SYS_NO_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000170 if (addr2info(store_addr)) {
171 int rc;
172
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200173 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000174 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000175 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000176 return (~0);
177 }
178 } else
179#endif
180 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000181 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000182 }
183 if ((store_addr) < start_addr)
184 start_addr = store_addr;
185 if ((store_addr + binlen - 1) > end_addr)
186 end_addr = store_addr + binlen - 1;
187 break;
188 case SREC_END2:
189 case SREC_END3:
190 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000191 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000192 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000193 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000194 "## First Load Addr = 0x%08lX\n"
195 "## Last Load Addr = 0x%08lX\n"
196 "## Total Size = 0x%08lX = %ld Bytes\n",
197 start_addr, end_addr, size, size
198 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000199 flush_cache(start_addr, size);
Simon Glass41ef3722013-02-24 17:33:22 +0000200 setenv_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000201 return (addr);
202 case SREC_START:
203 break;
204 default:
205 break;
206 }
207 if (!do_echo) { /* print a '.' every 100 lines */
208 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000209 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000210 }
211 }
212
213 return (~0); /* Download aborted */
214}
215
Kim Phillips088f1b12012-10-29 13:34:31 +0000216static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000217{
218 char *p;
219 char c;
220
221 --len; /* always leave room for terminating '\0' byte */
222
223 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000224 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000225 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000226 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000227
228 switch (c) {
229 case '\r':
230 case '\n':
231 *p = '\0';
232 return (p - buf);
233 case '\0':
234 case 0x03: /* ^C - Control C */
235 return (-1);
236 default:
237 *p = c;
238 }
239
240 /* Check for the console hangup (if any different from serial) */
wdenk232c1502004-03-12 00:14:09 +0000241 if (gd->jt[XF_getc] != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000242 if (ctrlc()) {
243 return (-1);
244 }
245 }
wdenk8bde7f72003-06-27 21:31:46 +0000246 }
247
248 /* line too long - truncate */
249 *p = '\0';
250 return (p - buf);
251}
252
Jon Loeligerc76fe472007-07-08 18:02:23 -0500253#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000254
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200255int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000256{
257 ulong offset = 0;
258 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200259#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000260 int save_baudrate, current_baudrate;
261
262 save_baudrate = current_baudrate = gd->baudrate;
263#endif
264
265 if (argc >= 2) {
266 offset = simple_strtoul(argv[1], NULL, 16);
267 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200268#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000269 if (argc >= 3) {
270 size = simple_strtoul(argv[2], NULL, 16);
271 }
272 if (argc == 4) {
273 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
274
275 /* default to current baudrate */
276 if (save_baudrate == 0)
277 save_baudrate = current_baudrate;
278 }
279 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000280 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000281 save_baudrate);
282 udelay(50000);
283 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000284 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000285 udelay(50000);
286 for (;;) {
287 if (getc() == '\r')
288 break;
289 }
290 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200291#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000292 if (argc == 3) {
293 size = simple_strtoul(argv[2], NULL, 16);
294 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200295#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000296
Kim Phillips088f1b12012-10-29 13:34:31 +0000297 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000298 for (;;) {
299 if (getc() == '\r')
300 break;
301 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000302 if (save_serial(offset, size)) {
303 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000304 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000305 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000306 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200307#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000308 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000309 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000310 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000311 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000312 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000313 serial_setbrg();
314 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000315 for (;;) {
316 if (getc() == 0x1B) /* ESC */
317 break;
318 }
319 }
320#endif
321 return 0;
322}
323
324#define SREC3_START "S0030000FC\n"
325#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
326#define SREC3_END "S70500000000FA\n"
327#define SREC_BYTES_PER_RECORD 16
328
Kim Phillips088f1b12012-10-29 13:34:31 +0000329static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000330{
331 int i, c, reclen, checksum, length;
332 char *hex = "0123456789ABCDEF";
333 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
334 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
335
336 reclen = 0;
337 checksum = 0;
338
339 if(write_record(SREC3_START)) /* write the header */
340 return (-1);
341 do {
342 if(count) { /* collect hex data in the buffer */
343 c = *(volatile uchar*)(address + reclen); /* get one byte */
344 checksum += c; /* accumulate checksum */
345 data[2*reclen] = hex[(c>>4)&0x0f];
346 data[2*reclen+1] = hex[c & 0x0f];
347 data[2*reclen+2] = '\0';
348 ++reclen;
349 --count;
350 }
351 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
352 /* enough data collected for one record: dump it */
353 if(reclen) { /* build & write a data record: */
354 /* address + data + checksum */
355 length = 4 + reclen + 1;
356
357 /* accumulate length bytes into checksum */
358 for(i = 0; i < 2; i++)
359 checksum += (length >> (8*i)) & 0xff;
360
361 /* accumulate address bytes into checksum: */
362 for(i = 0; i < 4; i++)
363 checksum += (address >> (8*i)) & 0xff;
364
365 /* make proper checksum byte: */
366 checksum = ~checksum & 0xff;
367
368 /* output one record: */
369 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
370 if(write_record(record))
371 return (-1);
372 }
373 address += reclen; /* increment address */
374 checksum = 0;
375 reclen = 0;
376 }
377 }
378 while(count);
379 if(write_record(SREC3_END)) /* write the final record */
380 return (-1);
381 return(0);
382}
383
Kim Phillips088f1b12012-10-29 13:34:31 +0000384static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000385{
386 char c;
387
388 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000389 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000390
391 /* Check for the console hangup (if any different from serial) */
392
393 if (ctrlc()) {
394 return (-1);
395 }
396 return (0);
397}
Jon Loeliger90253172007-07-10 11:02:44 -0500398# endif
wdenk8bde7f72003-06-27 21:31:46 +0000399
Jon Loeliger90253172007-07-10 11:02:44 -0500400#endif
wdenk8bde7f72003-06-27 21:31:46 +0000401
402
Jon Loeligerc76fe472007-07-08 18:02:23 -0500403#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500404/*
405 * loadb command (load binary) included
406 */
wdenk8bde7f72003-06-27 21:31:46 +0000407#define XON_CHAR 17
408#define XOFF_CHAR 19
409#define START_CHAR 0x01
410#define ETX_CHAR 0x03
411#define END_CHAR 0x0D
412#define SPACE 0x20
413#define K_ESCAPE 0x23
414#define SEND_TYPE 'S'
415#define DATA_TYPE 'D'
416#define ACK_TYPE 'Y'
417#define NACK_TYPE 'N'
418#define BREAK_TYPE 'B'
419#define tochar(x) ((char) (((x) + SPACE) & 0xff))
420#define untochar(x) ((int) (((x) - SPACE) & 0xff))
421
wdenk8bde7f72003-06-27 21:31:46 +0000422static void set_kerm_bin_mode(unsigned long *);
423static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000424static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000425
426
Kim Phillips088f1b12012-10-29 13:34:31 +0000427static char his_eol; /* character he needs at end of packet */
428static int his_pad_count; /* number of pad chars he needs */
429static char his_pad_char; /* pad chars he needs */
430static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000431
Kim Phillips088f1b12012-10-29 13:34:31 +0000432static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
433 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000434{
wdenk8bde7f72003-06-27 21:31:46 +0000435 ulong offset = 0;
436 ulong addr;
437 int load_baudrate, current_baudrate;
438 int rcode = 0;
439 char *s;
440
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200441 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
442 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000443
444 /* pre-set offset from $loadaddr */
445 if ((s = getenv("loadaddr")) != NULL) {
446 offset = simple_strtoul(s, NULL, 16);
447 }
448
449 load_baudrate = current_baudrate = gd->baudrate;
450
451 if (argc >= 2) {
452 offset = simple_strtoul(argv[1], NULL, 16);
453 }
454 if (argc == 3) {
455 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
456
457 /* default to current baudrate */
458 if (load_baudrate == 0)
459 load_baudrate = current_baudrate;
460 }
461
462 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000463 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000464 load_baudrate);
465 udelay(50000);
466 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000467 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000468 udelay(50000);
469 for (;;) {
470 if (getc() == '\r')
471 break;
472 }
473 }
474
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200475 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000476 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200477 "to 0x%08lX at %d bps...\n",
478 offset,
479 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000480
Kim Phillips088f1b12012-10-29 13:34:31 +0000481 addr = load_serial_ymodem(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200482
wdenk8bde7f72003-06-27 21:31:46 +0000483 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000484
Kim Phillips088f1b12012-10-29 13:34:31 +0000485 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200486 "to 0x%08lX at %d bps...\n",
487 offset,
488 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000489 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200490
491 if (addr == ~0) {
492 load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000493 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200494 rcode = 1;
495 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000496 printf("## Start Addr = 0x%08lX\n", addr);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200497 load_addr = addr;
498 }
499 }
wdenk8bde7f72003-06-27 21:31:46 +0000500 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000501 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000502 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000503 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000504 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000505 serial_setbrg();
506 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000507 for (;;) {
508 if (getc() == 0x1B) /* ESC */
509 break;
510 }
511 }
512
wdenk8bde7f72003-06-27 21:31:46 +0000513 return rcode;
514}
515
516
Kim Phillips088f1b12012-10-29 13:34:31 +0000517static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000518{
519 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000520
Kim Phillips088f1b12012-10-29 13:34:31 +0000521 set_kerm_bin_mode((ulong *) offset);
522 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000523
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
Kim Phillips088f1b12012-10-29 13:34:31 +0000536 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000537
538 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass41ef3722013-02-24 17:33:22 +0000539 setenv_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000540
541 return offset;
542}
543
Kim Phillips088f1b12012-10-29 13:34:31 +0000544static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000545{
546 int count = his_pad_count;
547
548 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000549 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000550}
551
552/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000553static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000554{
555 if ((in & 0x60) == 0x40) {
556 return (char) (in & ~0x40);
557 } else if ((in & 0x7f) == 0x3f) {
558 return (char) (in | 0x40);
559 } else
560 return in;
561}
562
Kim Phillips088f1b12012-10-29 13:34:31 +0000563static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000564{
565 int total = 0;
566
567 while (*buffer) {
568 total += *buffer++;
569 }
570 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
571}
572
Kim Phillips088f1b12012-10-29 13:34:31 +0000573static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000574{
Kim Phillips088f1b12012-10-29 13:34:31 +0000575 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000576 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000577 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000578 }
579}
580
581static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000582static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000583{
584 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000585 a_b[1] = tochar(3);
586 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000587 a_b[3] = ACK_TYPE;
588 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000589 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000590 a_b[5] = his_eol;
591 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000592 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000593}
594
Kim Phillips088f1b12012-10-29 13:34:31 +0000595static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000596{
597 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000598 a_b[1] = tochar(3);
599 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000600 a_b[3] = NACK_TYPE;
601 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000602 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000603 a_b[5] = his_eol;
604 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000605 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000606}
607
608
Kim Phillips088f1b12012-10-29 13:34:31 +0000609static void (*os_data_init)(void);
610static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000611static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000612static char *os_data_addr, *os_data_addr_saved;
613static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530614
Kim Phillips088f1b12012-10-29 13:34:31 +0000615static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000616{
617 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000618 os_data_addr = bin_start_address;
619}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530620
Kim Phillips088f1b12012-10-29 13:34:31 +0000621static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000622{
623 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000624 os_data_addr_saved = os_data_addr;
625}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530626
Kim Phillips088f1b12012-10-29 13:34:31 +0000627static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000628{
629 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000630 os_data_addr = os_data_addr_saved;
631}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530632
Kim Phillips088f1b12012-10-29 13:34:31 +0000633static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000634{
635 switch (os_data_state) {
636 case 0: /* data */
637 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000638 break;
639 }
640}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530641
Kim Phillips088f1b12012-10-29 13:34:31 +0000642static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000643{
644 bin_start_address = (char *) addr;
645 os_data_init = bin_data_init;
646 os_data_char = bin_data_char;
647}
648
649
650/* k_data_* simply handles the kermit escape translations */
651static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000652static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000653{
654 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000655 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000656}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530657
Kim Phillips088f1b12012-10-29 13:34:31 +0000658static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000659{
660 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000661 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000662}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530663
Kim Phillips088f1b12012-10-29 13:34:31 +0000664static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000665{
666 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000667 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000668}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530669
Kim Phillips088f1b12012-10-29 13:34:31 +0000670static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000671{
672 if (k_data_escape) {
673 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000674 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000675 k_data_escape = 0;
676 } else {
677 if (new_char == his_quote) {
678 /* this char is escape - remember */
679 k_data_escape = 1;
680 } else {
681 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000682 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000683 }
684 }
685}
686
687#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000688static char send_parms[SEND_DATA_SIZE];
689static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000690
691/* handle_send_packet interprits the protocol info and builds and
692 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000693static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000694{
695 int length = 3;
696 int bytes;
697
698 /* initialize some protocol parameters */
699 his_eol = END_CHAR; /* default end of line character */
700 his_pad_count = 0;
701 his_pad_char = '\0';
702 his_quote = K_ESCAPE;
703
704 /* ignore last character if it filled the buffer */
705 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
706 --send_ptr;
707 bytes = send_ptr - send_parms; /* how many bytes we'll process */
708 do {
709 if (bytes-- <= 0)
710 break;
711 /* handle MAXL - max length */
712 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000713 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000714 if (bytes-- <= 0)
715 break;
716 /* handle TIME - time you should wait for my packets */
717 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000718 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000719 if (bytes-- <= 0)
720 break;
721 /* handle NPAD - number of pad chars I need */
722 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000723 his_pad_count = untochar(send_parms[2]);
724 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000725 if (bytes-- <= 0)
726 break;
727 /* handle PADC - pad chars I need */
728 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000729 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000730 a_b[++length] = 0x40; /* He should ignore this */
731 if (bytes-- <= 0)
732 break;
733 /* handle EOL - end of line he needs */
734 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000735 his_eol = untochar(send_parms[4]);
736 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000737 if (bytes-- <= 0)
738 break;
739 /* handle QCTL - quote control char he'll use */
740 /* remember what he says - I'll use '#' */
741 his_quote = send_parms[5];
742 a_b[++length] = '#';
743 if (bytes-- <= 0)
744 break;
745 /* handle QBIN - 8-th bit prefixing */
746 /* ignore what he says - I refuse */
747 a_b[++length] = 'N';
748 if (bytes-- <= 0)
749 break;
750 /* handle CHKT - the clock check type */
751 /* ignore what he says - I do type 1 (for now) */
752 a_b[++length] = '1';
753 if (bytes-- <= 0)
754 break;
755 /* handle REPT - the repeat prefix */
756 /* ignore what he says - I refuse (for now) */
757 a_b[++length] = 'N';
758 if (bytes-- <= 0)
759 break;
760 /* handle CAPAS - the capabilities mask */
761 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000762 a_b[++length] = tochar(2); /* only long packets */
763 a_b[++length] = tochar(0); /* no windows */
764 a_b[++length] = tochar(94); /* large packet msb */
765 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000766 } while (0);
767
768 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000769 a_b[1] = tochar(length);
770 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000771 a_b[3] = ACK_TYPE;
772 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000773 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000774 a_b[++length] = his_eol;
775 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000776 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000777}
778
779/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000780static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000781{
782 char new_char;
783 char k_state, k_state_saved;
784 int sum;
785 int done;
786 int length;
787 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000788 int len_lo, len_hi;
789
790 /* initialize some protocol parameters */
791 his_eol = END_CHAR; /* default end of line character */
792 his_pad_count = 0;
793 his_pad_char = '\0';
794 his_quote = K_ESCAPE;
795
796 /* initialize the k_recv and k_data state machine */
797 done = 0;
798 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000799 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000800 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000801 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000802 n = 0; /* just to get rid of a warning */
803 last_n = -1;
804
805 /* expect this "type" sequence (but don't check):
806 S: send initiate
807 F: file header
808 D: data (multiple)
809 Z: end of file
810 B: break transmission
811 */
812
813 /* enter main loop */
814 while (!done) {
815 /* set the send packet pointer to begining of send packet parms */
816 send_ptr = send_parms;
817
818 /* With each packet, start summing the bytes starting with the length.
819 Save the current sequence number.
820 Note the type of the packet.
821 If a character less than SPACE (0x20) is received - error.
822 */
823
824#if 0
825 /* OLD CODE, Prior to checking sequence numbers */
826 /* first have all state machines save current states */
827 k_state_saved = k_state;
828 k_data_save ();
829#endif
830
831 /* get a packet */
832 /* wait for the starting character or ^C */
833 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000834 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000835 case START_CHAR: /* start packet */
836 goto START;
837 case ETX_CHAR: /* ^C waiting for packet */
838 return (0);
839 default:
840 ;
841 }
842 }
843START:
844 /* get length of packet */
845 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000846 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000847 if ((new_char & 0xE0) == 0)
848 goto packet_error;
849 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000850 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000851 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +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;
Kim Phillips088f1b12012-10-29 13:34:31 +0000856 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000857 --length;
858
859 /* NEW CODE - check sequence numbers for retried packets */
860 /* Note - this new code assumes that the sequence number is correctly
861 * received. Handling an invalid sequence number adds another layer
862 * of complexity that may not be needed - yet! At this time, I'm hoping
863 * that I don't need to buffer the incoming data packets and can write
864 * the data into memory in real time.
865 */
866 if (n == last_n) {
867 /* same sequence number, restore the previous state */
868 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000869 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000870 } else {
871 /* new sequence number, checkpoint the download */
872 last_n = n;
873 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000874 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000875 }
876 /* END NEW CODE */
877
878 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000879 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000880 if ((new_char & 0xE0) == 0)
881 goto packet_error;
882 sum += new_char & 0xff;
883 k_state = new_char;
884 --length;
885 /* check for extended length */
886 if (length == -2) {
887 /* (length byte was 0, decremented twice) */
888 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000889 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000890 if ((new_char & 0xE0) == 0)
891 goto packet_error;
892 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000893 len_hi = untochar(new_char);
894 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000895 if ((new_char & 0xE0) == 0)
896 goto packet_error;
897 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000898 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000899 length = len_hi * 95 + len_lo;
900 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000901 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000902 if ((new_char & 0xE0) == 0)
903 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000904 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000905 goto packet_error;
906 sum += new_char & 0xff;
907/* --length; */ /* new length includes only data and block check to come */
908 }
909 /* bring in rest of packet */
910 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000911 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000912 if ((new_char & 0xE0) == 0)
913 goto packet_error;
914 sum += new_char & 0xff;
915 --length;
916 if (k_state == DATA_TYPE) {
917 /* pass on the data if this is a data packet */
918 k_data_char (new_char);
919 } else if (k_state == SEND_TYPE) {
920 /* save send pack in buffer as is */
921 *send_ptr++ = new_char;
922 /* if too much data, back off the pointer */
923 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
924 --send_ptr;
925 }
926 }
927 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000928 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000929 if ((new_char & 0xE0) == 0)
930 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000931 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000932 goto packet_error;
933 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000934 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000935 if (new_char != END_CHAR) {
936 packet_error:
937 /* restore state machines */
938 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000939 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000940 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000941 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000942 } else if (k_state == SEND_TYPE) {
943 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000944 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000945 } else {
946 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000947 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000948 /* quit if end of transmission */
949 if (k_state == BREAK_TYPE)
950 done = 1;
951 }
wdenk8bde7f72003-06-27 21:31:46 +0000952 }
953 return ((ulong) os_data_addr - (ulong) bin_start_address);
954}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200955
956static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200957 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200958 return (getc());
959 return -1;
960}
Kim Phillips088f1b12012-10-29 13:34:31 +0000961static ulong load_serial_ymodem(ulong offset)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200962{
963 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200964 int err;
965 int res;
966 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200967 char ymodemBuf[1024];
968 ulong store_addr = ~0;
969 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200970
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200971 size = 0;
972 info.mode = xyzModem_ymodem;
Kim Phillips088f1b12012-10-29 13:34:31 +0000973 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200974 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200975
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200976 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000977 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200978 store_addr = addr + offset;
979 size += res;
980 addr += res;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200981#ifndef CONFIG_SYS_NO_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000982 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200983 int rc;
984
Kim Phillips088f1b12012-10-29 13:34:31 +0000985 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200986 store_addr, res);
987 if (rc != 0) {
988 flash_perror (rc);
989 return (~0);
990 }
991 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200992#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200993 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000994 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200995 res);
996 }
997
998 }
999 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +00001000 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001001 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001002
Kim Phillips088f1b12012-10-29 13:34:31 +00001003 xyzModem_stream_close(&err);
1004 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001005
1006
Kim Phillips088f1b12012-10-29 13:34:31 +00001007 flush_cache(offset, size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001008
Kim Phillips088f1b12012-10-29 13:34:31 +00001009 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass41ef3722013-02-24 17:33:22 +00001010 setenv_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001011
1012 return offset;
1013}
1014
Jon Loeliger90253172007-07-10 11:02:44 -05001015#endif
wdenk8bde7f72003-06-27 21:31:46 +00001016
1017/* -------------------------------------------------------------------- */
1018
Jon Loeligerc76fe472007-07-08 18:02:23 -05001019#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001020
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001021#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001022U_BOOT_CMD(
1023 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001024 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001025 "[ off ] [ baud ]\n"
1026 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001027 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001028);
1029
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001030#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001031U_BOOT_CMD(
1032 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001033 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001034 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001035 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001036);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001037#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001038
1039/*
1040 * SAVES always requires LOADS support, but not vice versa
1041 */
1042
1043
Jon Loeligerc76fe472007-07-08 18:02:23 -05001044#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001045#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001046U_BOOT_CMD(
1047 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001048 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001049 "[ off ] [size] [ baud ]\n"
1050 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001051 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001052);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001053#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001054U_BOOT_CMD(
1055 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001056 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001057 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001058 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001059);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001060#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001061#endif /* CONFIG_CMD_SAVES */
1062#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001063
1064
Jon Loeligerc76fe472007-07-08 18:02:23 -05001065#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001066U_BOOT_CMD(
1067 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001068 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001069 "[ off ] [ baud ]\n"
1070 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001071 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001072);
1073
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001074U_BOOT_CMD(
1075 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001076 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001077 "[ off ] [ baud ]\n"
1078 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001079 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001080);
1081
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001082#endif /* CONFIG_CMD_LOADB */
wdenk8bde7f72003-06-27 21:31:46 +00001083
1084/* -------------------------------------------------------------------- */
1085
Jon Loeligerc76fe472007-07-08 18:02:23 -05001086#if defined(CONFIG_CMD_HWFLOW)
Kim Phillips088f1b12012-10-29 13:34:31 +00001087int do_hwflow(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +00001088{
1089 extern int hwflow_onoff(int);
1090
1091 if (argc == 2) {
1092 if (strcmp(argv[1], "off") == 0)
1093 hwflow_onoff(-1);
1094 else
1095 if (strcmp(argv[1], "on") == 0)
1096 hwflow_onoff(1);
1097 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001098 return CMD_RET_USAGE;
wdenk8bde7f72003-06-27 21:31:46 +00001099 }
1100 printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
1101 return 0;
1102}
1103
1104/* -------------------------------------------------------------------- */
1105
wdenk0d498392003-07-01 21:06:45 +00001106U_BOOT_CMD(
wdenkf12e5682003-07-07 20:07:54 +00001107 hwflow, 2, 0, do_hwflow,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001108 "turn RTS/CTS hardware flow control in serial line on/off",
1109 "[on|off]"
wdenk8bde7f72003-06-27 21:31:46 +00001110);
1111
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001112#endif /* CONFIG_CMD_HWFLOW */