blob: 3bfc1b41ecececf359ec609b57270ad3c819caca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk8bde7f72003-06-27 21:31:46 +00002/*
wdenk232c1502004-03-12 00:14:09 +00003 * (C) Copyright 2000-2004
wdenk8bde7f72003-06-27 21:31:46 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk8bde7f72003-06-27 21:31:46 +00005 */
6
7/*
8 * Serial up- and download support
9 */
10#include <common.h>
11#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060014#include <env.h>
wdenk8bde7f72003-06-27 21:31:46 +000015#include <s_record.h>
16#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000017#include <exports.h>
Simon Glassb03e0512019-11-14 12:57:24 -070018#include <serial.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020019#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000020
Wolfgang Denkd87080b2006-03-31 18:32:53 +020021DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000022
Jon Loeligerc76fe472007-07-08 18:02:23 -050023#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060024static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020025#endif
26
Jon Loeligerc76fe472007-07-08 18:02:23 -050027#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000028static ulong load_serial(long offset);
29static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050030# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000031static int save_serial(ulong offset, ulong size);
32static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050033#endif
wdenk8bde7f72003-06-27 21:31:46 +000034
35static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050036#endif
wdenk8bde7f72003-06-27 21:31:46 +000037
38/* -------------------------------------------------------------------- */
39
Jon Loeligerc76fe472007-07-08 18:02:23 -050040#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000041static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
42 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000043{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020044 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000045 ulong addr;
46 int i;
47 char *env_echo;
48 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000050 int load_baudrate, current_baudrate;
51
52 load_baudrate = current_baudrate = gd->baudrate;
53#endif
54
Simon Glass00caae62017-08-03 12:22:12 -060055 env_echo = env_get("loads_echo");
56 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000057 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060058 else
wdenk8bde7f72003-06-27 21:31:46 +000059 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000060
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020061#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000062 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020063 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000064 }
65 if (argc == 3) {
66 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
67
68 /* default to current baudrate */
69 if (load_baudrate == 0)
70 load_baudrate = current_baudrate;
71 }
72 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000073 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000074 load_baudrate);
75 udelay(50000);
76 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000077 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000078 udelay(50000);
79 for (;;) {
80 if (getc() == '\r')
81 break;
82 }
83 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020084#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000085 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020086 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000087 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000089
Kim Phillips088f1b12012-10-29 13:34:31 +000090 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000091
Kim Phillips088f1b12012-10-29 13:34:31 +000092 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000093
94 /*
95 * Gather any trailing characters (for instance, the ^D which
96 * is sent by 'cu' after sending a file), and give the
97 * box some time (100 * 1 ms)
98 */
99 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000100 if (tstc()) {
101 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000102 }
103 udelay(1000);
104 }
105
106 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000107 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000108 rcode = 1;
109 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000110 printf("## Start Addr = 0x%08lX\n", addr);
wdenk8bde7f72003-06-27 21:31:46 +0000111 load_addr = addr;
112 }
113
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000115 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000116 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000117 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000118 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000119 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000120 serial_setbrg();
121 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000122 for (;;) {
123 if (getc() == 0x1B) /* ESC */
124 break;
125 }
126 }
127#endif
128 return rcode;
129}
130
Kim Phillips088f1b12012-10-29 13:34:31 +0000131static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000132{
133 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
134 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
135 int binlen; /* no. of data bytes in S-Rec. */
136 int type; /* return code for record type */
137 ulong addr; /* load address from S-Record */
138 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000139 ulong store_addr;
140 ulong start_addr = ~0;
141 ulong end_addr = 0;
142 int line_count = 0;
143
144 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000145 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000146
147 if (type < 0) {
148 return (~0); /* Invalid S-Record */
149 }
150
151 switch (type) {
152 case SREC_DATA2:
153 case SREC_DATA3:
154 case SREC_DATA4:
155 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900156#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000157 if (addr2info(store_addr)) {
158 int rc;
159
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200160 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000161 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000162 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000163 return (~0);
164 }
165 } else
166#endif
167 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000168 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000169 }
170 if ((store_addr) < start_addr)
171 start_addr = store_addr;
172 if ((store_addr + binlen - 1) > end_addr)
173 end_addr = store_addr + binlen - 1;
174 break;
175 case SREC_END2:
176 case SREC_END3:
177 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000178 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000179 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000180 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000181 "## First Load Addr = 0x%08lX\n"
182 "## Last Load Addr = 0x%08lX\n"
183 "## Total Size = 0x%08lX = %ld Bytes\n",
184 start_addr, end_addr, size, size
185 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000186 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600187 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000188 return (addr);
189 case SREC_START:
190 break;
191 default:
192 break;
193 }
194 if (!do_echo) { /* print a '.' every 100 lines */
195 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000196 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000197 }
198 }
199
200 return (~0); /* Download aborted */
201}
202
Kim Phillips088f1b12012-10-29 13:34:31 +0000203static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000204{
205 char *p;
206 char c;
207
208 --len; /* always leave room for terminating '\0' byte */
209
210 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000211 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000212 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000213 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000214
215 switch (c) {
216 case '\r':
217 case '\n':
218 *p = '\0';
219 return (p - buf);
220 case '\0':
221 case 0x03: /* ^C - Control C */
222 return (-1);
223 default:
224 *p = c;
225 }
226
227 /* Check for the console hangup (if any different from serial) */
Martin Dorwig49cad542015-01-26 15:22:54 -0700228 if (gd->jt->getc != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000229 if (ctrlc()) {
230 return (-1);
231 }
232 }
wdenk8bde7f72003-06-27 21:31:46 +0000233 }
234
235 /* line too long - truncate */
236 *p = '\0';
237 return (p - buf);
238}
239
Jon Loeligerc76fe472007-07-08 18:02:23 -0500240#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000241
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200242int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000243{
244 ulong offset = 0;
245 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200246#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000247 int save_baudrate, current_baudrate;
248
249 save_baudrate = current_baudrate = gd->baudrate;
250#endif
251
252 if (argc >= 2) {
253 offset = simple_strtoul(argv[1], NULL, 16);
254 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200255#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000256 if (argc >= 3) {
257 size = simple_strtoul(argv[2], NULL, 16);
258 }
259 if (argc == 4) {
260 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
261
262 /* default to current baudrate */
263 if (save_baudrate == 0)
264 save_baudrate = current_baudrate;
265 }
266 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000267 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000268 save_baudrate);
269 udelay(50000);
270 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000271 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000272 udelay(50000);
273 for (;;) {
274 if (getc() == '\r')
275 break;
276 }
277 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200278#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000279 if (argc == 3) {
280 size = simple_strtoul(argv[2], NULL, 16);
281 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200282#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000283
Kim Phillips088f1b12012-10-29 13:34:31 +0000284 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000285 for (;;) {
286 if (getc() == '\r')
287 break;
288 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000289 if (save_serial(offset, size)) {
290 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000291 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000292 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000293 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200294#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000295 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000296 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000297 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000298 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000299 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000300 serial_setbrg();
301 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000302 for (;;) {
303 if (getc() == 0x1B) /* ESC */
304 break;
305 }
306 }
307#endif
308 return 0;
309}
310
311#define SREC3_START "S0030000FC\n"
312#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
313#define SREC3_END "S70500000000FA\n"
314#define SREC_BYTES_PER_RECORD 16
315
Kim Phillips088f1b12012-10-29 13:34:31 +0000316static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000317{
318 int i, c, reclen, checksum, length;
319 char *hex = "0123456789ABCDEF";
320 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
321 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
322
323 reclen = 0;
324 checksum = 0;
325
326 if(write_record(SREC3_START)) /* write the header */
327 return (-1);
328 do {
329 if(count) { /* collect hex data in the buffer */
330 c = *(volatile uchar*)(address + reclen); /* get one byte */
331 checksum += c; /* accumulate checksum */
332 data[2*reclen] = hex[(c>>4)&0x0f];
333 data[2*reclen+1] = hex[c & 0x0f];
334 data[2*reclen+2] = '\0';
335 ++reclen;
336 --count;
337 }
338 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
339 /* enough data collected for one record: dump it */
340 if(reclen) { /* build & write a data record: */
341 /* address + data + checksum */
342 length = 4 + reclen + 1;
343
344 /* accumulate length bytes into checksum */
345 for(i = 0; i < 2; i++)
346 checksum += (length >> (8*i)) & 0xff;
347
348 /* accumulate address bytes into checksum: */
349 for(i = 0; i < 4; i++)
350 checksum += (address >> (8*i)) & 0xff;
351
352 /* make proper checksum byte: */
353 checksum = ~checksum & 0xff;
354
355 /* output one record: */
356 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
357 if(write_record(record))
358 return (-1);
359 }
360 address += reclen; /* increment address */
361 checksum = 0;
362 reclen = 0;
363 }
364 }
365 while(count);
366 if(write_record(SREC3_END)) /* write the final record */
367 return (-1);
368 return(0);
369}
370
Kim Phillips088f1b12012-10-29 13:34:31 +0000371static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000372{
373 char c;
374
375 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000376 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000377
378 /* Check for the console hangup (if any different from serial) */
379
380 if (ctrlc()) {
381 return (-1);
382 }
383 return (0);
384}
Jon Loeliger90253172007-07-10 11:02:44 -0500385# endif
wdenk8bde7f72003-06-27 21:31:46 +0000386
Jon Loeliger90253172007-07-10 11:02:44 -0500387#endif
wdenk8bde7f72003-06-27 21:31:46 +0000388
389
Jon Loeligerc76fe472007-07-08 18:02:23 -0500390#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500391/*
392 * loadb command (load binary) included
393 */
wdenk8bde7f72003-06-27 21:31:46 +0000394#define XON_CHAR 17
395#define XOFF_CHAR 19
396#define START_CHAR 0x01
397#define ETX_CHAR 0x03
398#define END_CHAR 0x0D
399#define SPACE 0x20
400#define K_ESCAPE 0x23
401#define SEND_TYPE 'S'
402#define DATA_TYPE 'D'
403#define ACK_TYPE 'Y'
404#define NACK_TYPE 'N'
405#define BREAK_TYPE 'B'
406#define tochar(x) ((char) (((x) + SPACE) & 0xff))
407#define untochar(x) ((int) (((x) - SPACE) & 0xff))
408
wdenk8bde7f72003-06-27 21:31:46 +0000409static void set_kerm_bin_mode(unsigned long *);
410static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000411static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000412
413
Kim Phillips088f1b12012-10-29 13:34:31 +0000414static char his_eol; /* character he needs at end of packet */
415static int his_pad_count; /* number of pad chars he needs */
416static char his_pad_char; /* pad chars he needs */
417static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000418
Kim Phillips088f1b12012-10-29 13:34:31 +0000419static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
420 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000421{
wdenk8bde7f72003-06-27 21:31:46 +0000422 ulong offset = 0;
423 ulong addr;
424 int load_baudrate, current_baudrate;
425 int rcode = 0;
426 char *s;
427
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200428 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
429 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000430
431 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600432 s = env_get("loadaddr");
433 if (s)
wdenk8bde7f72003-06-27 21:31:46 +0000434 offset = simple_strtoul(s, NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000435
436 load_baudrate = current_baudrate = gd->baudrate;
437
438 if (argc >= 2) {
439 offset = simple_strtoul(argv[1], NULL, 16);
440 }
441 if (argc == 3) {
442 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
443
444 /* default to current baudrate */
445 if (load_baudrate == 0)
446 load_baudrate = current_baudrate;
447 }
448
449 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000450 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000451 load_baudrate);
452 udelay(50000);
453 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000454 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000455 udelay(50000);
456 for (;;) {
457 if (getc() == '\r')
458 break;
459 }
460 }
461
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200462 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000463 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200464 "to 0x%08lX at %d bps...\n",
465 offset,
466 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000467
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600468 addr = load_serial_ymodem(offset, xyzModem_ymodem);
469
470 } else if (strcmp(argv[0],"loadx")==0) {
471 printf("## Ready for binary (xmodem) download "
472 "to 0x%08lX at %d bps...\n",
473 offset,
474 load_baudrate);
475
476 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200477
wdenk8bde7f72003-06-27 21:31:46 +0000478 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000479
Kim Phillips088f1b12012-10-29 13:34:31 +0000480 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200481 "to 0x%08lX at %d bps...\n",
482 offset,
483 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000484 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200485
486 if (addr == ~0) {
487 load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000488 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200489 rcode = 1;
490 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000491 printf("## Start Addr = 0x%08lX\n", addr);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200492 load_addr = addr;
493 }
494 }
wdenk8bde7f72003-06-27 21:31:46 +0000495 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000496 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000497 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000498 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000499 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000500 serial_setbrg();
501 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000502 for (;;) {
503 if (getc() == 0x1B) /* ESC */
504 break;
505 }
506 }
507
wdenk8bde7f72003-06-27 21:31:46 +0000508 return rcode;
509}
510
511
Kim Phillips088f1b12012-10-29 13:34:31 +0000512static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000513{
514 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000515
Kim Phillips088f1b12012-10-29 13:34:31 +0000516 set_kerm_bin_mode((ulong *) offset);
517 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000518
519 /*
520 * Gather any trailing characters (for instance, the ^D which
521 * is sent by 'cu' after sending a file), and give the
522 * box some time (100 * 1 ms)
523 */
524 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000525 if (tstc()) {
526 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000527 }
528 udelay(1000);
529 }
530
Kim Phillips088f1b12012-10-29 13:34:31 +0000531 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000532
533 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600534 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000535
536 return offset;
537}
538
Kim Phillips088f1b12012-10-29 13:34:31 +0000539static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000540{
541 int count = his_pad_count;
542
543 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000544 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000545}
546
547/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000548static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000549{
550 if ((in & 0x60) == 0x40) {
551 return (char) (in & ~0x40);
552 } else if ((in & 0x7f) == 0x3f) {
553 return (char) (in | 0x40);
554 } else
555 return in;
556}
557
Kim Phillips088f1b12012-10-29 13:34:31 +0000558static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000559{
560 int total = 0;
561
562 while (*buffer) {
563 total += *buffer++;
564 }
565 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
566}
567
Kim Phillips088f1b12012-10-29 13:34:31 +0000568static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000569{
Kim Phillips088f1b12012-10-29 13:34:31 +0000570 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000571 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000572 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000573 }
574}
575
576static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000577static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000578{
579 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000580 a_b[1] = tochar(3);
581 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000582 a_b[3] = ACK_TYPE;
583 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000584 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000585 a_b[5] = his_eol;
586 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000587 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000588}
589
Kim Phillips088f1b12012-10-29 13:34:31 +0000590static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000591{
592 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000593 a_b[1] = tochar(3);
594 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000595 a_b[3] = NACK_TYPE;
596 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000597 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000598 a_b[5] = his_eol;
599 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000600 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000601}
602
603
Kim Phillips088f1b12012-10-29 13:34:31 +0000604static void (*os_data_init)(void);
605static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000606static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000607static char *os_data_addr, *os_data_addr_saved;
608static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530609
Kim Phillips088f1b12012-10-29 13:34:31 +0000610static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000611{
612 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000613 os_data_addr = bin_start_address;
614}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530615
Kim Phillips088f1b12012-10-29 13:34:31 +0000616static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000617{
618 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000619 os_data_addr_saved = os_data_addr;
620}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530621
Kim Phillips088f1b12012-10-29 13:34:31 +0000622static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000623{
624 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000625 os_data_addr = os_data_addr_saved;
626}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530627
Kim Phillips088f1b12012-10-29 13:34:31 +0000628static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000629{
630 switch (os_data_state) {
631 case 0: /* data */
632 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000633 break;
634 }
635}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530636
Kim Phillips088f1b12012-10-29 13:34:31 +0000637static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000638{
639 bin_start_address = (char *) addr;
640 os_data_init = bin_data_init;
641 os_data_char = bin_data_char;
642}
643
644
645/* k_data_* simply handles the kermit escape translations */
646static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000647static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000648{
649 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000650 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000651}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530652
Kim Phillips088f1b12012-10-29 13:34:31 +0000653static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000654{
655 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000656 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000657}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530658
Kim Phillips088f1b12012-10-29 13:34:31 +0000659static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000660{
661 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000662 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000663}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530664
Kim Phillips088f1b12012-10-29 13:34:31 +0000665static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000666{
667 if (k_data_escape) {
668 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000669 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000670 k_data_escape = 0;
671 } else {
672 if (new_char == his_quote) {
673 /* this char is escape - remember */
674 k_data_escape = 1;
675 } else {
676 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000677 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000678 }
679 }
680}
681
682#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000683static char send_parms[SEND_DATA_SIZE];
684static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000685
686/* handle_send_packet interprits the protocol info and builds and
687 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000688static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000689{
690 int length = 3;
691 int bytes;
692
693 /* initialize some protocol parameters */
694 his_eol = END_CHAR; /* default end of line character */
695 his_pad_count = 0;
696 his_pad_char = '\0';
697 his_quote = K_ESCAPE;
698
699 /* ignore last character if it filled the buffer */
700 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
701 --send_ptr;
702 bytes = send_ptr - send_parms; /* how many bytes we'll process */
703 do {
704 if (bytes-- <= 0)
705 break;
706 /* handle MAXL - max length */
707 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000708 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000709 if (bytes-- <= 0)
710 break;
711 /* handle TIME - time you should wait for my packets */
712 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000713 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000714 if (bytes-- <= 0)
715 break;
716 /* handle NPAD - number of pad chars I need */
717 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000718 his_pad_count = untochar(send_parms[2]);
719 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000720 if (bytes-- <= 0)
721 break;
722 /* handle PADC - pad chars I need */
723 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000724 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000725 a_b[++length] = 0x40; /* He should ignore this */
726 if (bytes-- <= 0)
727 break;
728 /* handle EOL - end of line he needs */
729 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000730 his_eol = untochar(send_parms[4]);
731 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000732 if (bytes-- <= 0)
733 break;
734 /* handle QCTL - quote control char he'll use */
735 /* remember what he says - I'll use '#' */
736 his_quote = send_parms[5];
737 a_b[++length] = '#';
738 if (bytes-- <= 0)
739 break;
740 /* handle QBIN - 8-th bit prefixing */
741 /* ignore what he says - I refuse */
742 a_b[++length] = 'N';
743 if (bytes-- <= 0)
744 break;
745 /* handle CHKT - the clock check type */
746 /* ignore what he says - I do type 1 (for now) */
747 a_b[++length] = '1';
748 if (bytes-- <= 0)
749 break;
750 /* handle REPT - the repeat prefix */
751 /* ignore what he says - I refuse (for now) */
752 a_b[++length] = 'N';
753 if (bytes-- <= 0)
754 break;
755 /* handle CAPAS - the capabilities mask */
756 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000757 a_b[++length] = tochar(2); /* only long packets */
758 a_b[++length] = tochar(0); /* no windows */
759 a_b[++length] = tochar(94); /* large packet msb */
760 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000761 } while (0);
762
763 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000764 a_b[1] = tochar(length);
765 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000766 a_b[3] = ACK_TYPE;
767 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000768 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000769 a_b[++length] = his_eol;
770 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000771 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000772}
773
774/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000775static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000776{
777 char new_char;
778 char k_state, k_state_saved;
779 int sum;
780 int done;
781 int length;
782 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000783 int len_lo, len_hi;
784
785 /* initialize some protocol parameters */
786 his_eol = END_CHAR; /* default end of line character */
787 his_pad_count = 0;
788 his_pad_char = '\0';
789 his_quote = K_ESCAPE;
790
791 /* initialize the k_recv and k_data state machine */
792 done = 0;
793 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000794 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000795 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000796 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000797 n = 0; /* just to get rid of a warning */
798 last_n = -1;
799
800 /* expect this "type" sequence (but don't check):
801 S: send initiate
802 F: file header
803 D: data (multiple)
804 Z: end of file
805 B: break transmission
806 */
807
808 /* enter main loop */
809 while (!done) {
810 /* set the send packet pointer to begining of send packet parms */
811 send_ptr = send_parms;
812
813 /* With each packet, start summing the bytes starting with the length.
814 Save the current sequence number.
815 Note the type of the packet.
816 If a character less than SPACE (0x20) is received - error.
817 */
818
819#if 0
820 /* OLD CODE, Prior to checking sequence numbers */
821 /* first have all state machines save current states */
822 k_state_saved = k_state;
823 k_data_save ();
824#endif
825
826 /* get a packet */
827 /* wait for the starting character or ^C */
828 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000829 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000830 case START_CHAR: /* start packet */
831 goto START;
832 case ETX_CHAR: /* ^C waiting for packet */
833 return (0);
834 default:
835 ;
836 }
837 }
838START:
839 /* get length of packet */
840 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000841 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000842 if ((new_char & 0xE0) == 0)
843 goto packet_error;
844 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000845 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000846 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +0000847 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000848 if ((new_char & 0xE0) == 0)
849 goto packet_error;
850 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000851 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000852 --length;
853
854 /* NEW CODE - check sequence numbers for retried packets */
855 /* Note - this new code assumes that the sequence number is correctly
856 * received. Handling an invalid sequence number adds another layer
857 * of complexity that may not be needed - yet! At this time, I'm hoping
858 * that I don't need to buffer the incoming data packets and can write
859 * the data into memory in real time.
860 */
861 if (n == last_n) {
862 /* same sequence number, restore the previous state */
863 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000864 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000865 } else {
866 /* new sequence number, checkpoint the download */
867 last_n = n;
868 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000869 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000870 }
871 /* END NEW CODE */
872
873 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000874 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000875 if ((new_char & 0xE0) == 0)
876 goto packet_error;
877 sum += new_char & 0xff;
878 k_state = new_char;
879 --length;
880 /* check for extended length */
881 if (length == -2) {
882 /* (length byte was 0, decremented twice) */
883 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000884 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000885 if ((new_char & 0xE0) == 0)
886 goto packet_error;
887 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000888 len_hi = untochar(new_char);
889 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_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000894 length = len_hi * 95 + len_lo;
895 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000896 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000897 if ((new_char & 0xE0) == 0)
898 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000899 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000900 goto packet_error;
901 sum += new_char & 0xff;
902/* --length; */ /* new length includes only data and block check to come */
903 }
904 /* bring in rest of packet */
905 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000906 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000907 if ((new_char & 0xE0) == 0)
908 goto packet_error;
909 sum += new_char & 0xff;
910 --length;
911 if (k_state == DATA_TYPE) {
912 /* pass on the data if this is a data packet */
913 k_data_char (new_char);
914 } else if (k_state == SEND_TYPE) {
915 /* save send pack in buffer as is */
916 *send_ptr++ = new_char;
917 /* if too much data, back off the pointer */
918 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
919 --send_ptr;
920 }
921 }
922 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000923 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000924 if ((new_char & 0xE0) == 0)
925 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000926 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000927 goto packet_error;
928 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000929 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000930 if (new_char != END_CHAR) {
931 packet_error:
932 /* restore state machines */
933 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000934 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000935 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000936 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000937 } else if (k_state == SEND_TYPE) {
938 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000939 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000940 } else {
941 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000942 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000943 /* quit if end of transmission */
944 if (k_state == BREAK_TYPE)
945 done = 1;
946 }
wdenk8bde7f72003-06-27 21:31:46 +0000947 }
948 return ((ulong) os_data_addr - (ulong) bin_start_address);
949}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200950
951static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200952 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200953 return (getc());
954 return -1;
955}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600956static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200957{
958 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200959 int err;
960 int res;
961 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200962 char ymodemBuf[1024];
963 ulong store_addr = ~0;
964 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200965
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200966 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600967 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000968 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200969 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200970
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200971 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000972 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200973 store_addr = addr + offset;
974 size += res;
975 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900976#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000977 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200978 int rc;
979
Kim Phillips088f1b12012-10-29 13:34:31 +0000980 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200981 store_addr, res);
982 if (rc != 0) {
983 flash_perror (rc);
984 return (~0);
985 }
986 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200987#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200988 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000989 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200990 res);
991 }
992
993 }
994 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000995 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200996 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200997
Kim Phillips088f1b12012-10-29 13:34:31 +0000998 xyzModem_stream_close(&err);
999 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001000
1001
Chris Packham0a6036d2016-10-25 20:22:48 +13001002 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001003
Kim Phillips088f1b12012-10-29 13:34:31 +00001004 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001005 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001006
1007 return offset;
1008}
1009
Jon Loeliger90253172007-07-10 11:02:44 -05001010#endif
wdenk8bde7f72003-06-27 21:31:46 +00001011
1012/* -------------------------------------------------------------------- */
1013
Jon Loeligerc76fe472007-07-08 18:02:23 -05001014#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001015
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001016#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001017U_BOOT_CMD(
1018 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001019 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001020 "[ off ] [ baud ]\n"
1021 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001022 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001023);
1024
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001025#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001026U_BOOT_CMD(
1027 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001028 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001029 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001030 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001031);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001032#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001033
1034/*
1035 * SAVES always requires LOADS support, but not vice versa
1036 */
1037
1038
Jon Loeligerc76fe472007-07-08 18:02:23 -05001039#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001040#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001041U_BOOT_CMD(
1042 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001043 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001044 "[ off ] [size] [ baud ]\n"
1045 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001046 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001047);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001048#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001049U_BOOT_CMD(
1050 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001051 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001052 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001053 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001054);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001055#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001056#endif /* CONFIG_CMD_SAVES */
1057#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001058
1059
Jon Loeligerc76fe472007-07-08 18:02:23 -05001060#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001061U_BOOT_CMD(
1062 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001063 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001064 "[ off ] [ baud ]\n"
1065 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001066 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001067);
1068
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001069U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001070 loadx, 3, 0, do_load_serial_bin,
1071 "load binary file over serial line (xmodem mode)",
1072 "[ off ] [ baud ]\n"
1073 " - load binary file over serial line"
1074 " with offset 'off' and baudrate 'baud'"
1075);
1076
1077U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001078 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001079 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001080 "[ off ] [ baud ]\n"
1081 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001082 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001083);
1084
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001085#endif /* CONFIG_CMD_LOADB */