blob: fab30fe894318a9308bc7cb41e948b9b6d229879 [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>
Simon Glass0ee482522019-12-28 10:44:40 -070015#include <flash.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070016#include <image.h>
wdenk8bde7f72003-06-27 21:31:46 +000017#include <s_record.h>
18#include <net.h>
wdenk27b207f2003-07-24 23:38:38 +000019#include <exports.h>
Simon Glassb03e0512019-11-14 12:57:24 -070020#include <serial.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020021#include <xyzModem.h>
wdenk8bde7f72003-06-27 21:31:46 +000022
Wolfgang Denkd87080b2006-03-31 18:32:53 +020023DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000024
Jon Loeligerc76fe472007-07-08 18:02:23 -050025#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060026static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020027#endif
28
Jon Loeligerc76fe472007-07-08 18:02:23 -050029#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000030static ulong load_serial(long offset);
31static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050032# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000033static int save_serial(ulong offset, ulong size);
34static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050035#endif
wdenk8bde7f72003-06-27 21:31:46 +000036
37static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050038#endif
wdenk8bde7f72003-06-27 21:31:46 +000039
40/* -------------------------------------------------------------------- */
41
Jon Loeligerc76fe472007-07-08 18:02:23 -050042#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000043static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
44 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000045{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020046 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000047 ulong addr;
48 int i;
49 char *env_echo;
50 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000052 int load_baudrate, current_baudrate;
53
54 load_baudrate = current_baudrate = gd->baudrate;
55#endif
56
Simon Glass00caae62017-08-03 12:22:12 -060057 env_echo = env_get("loads_echo");
58 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000059 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060060 else
wdenk8bde7f72003-06-27 21:31:46 +000061 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000062
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020063#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000064 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020065 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000066 }
67 if (argc == 3) {
68 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
69
70 /* default to current baudrate */
71 if (load_baudrate == 0)
72 load_baudrate = current_baudrate;
73 }
74 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000075 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000076 load_baudrate);
77 udelay(50000);
78 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000079 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000080 udelay(50000);
81 for (;;) {
82 if (getc() == '\r')
83 break;
84 }
85 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000087 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020088 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000089 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000091
Kim Phillips088f1b12012-10-29 13:34:31 +000092 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000093
Kim Phillips088f1b12012-10-29 13:34:31 +000094 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000095
96 /*
97 * Gather any trailing characters (for instance, the ^D which
98 * is sent by 'cu' after sending a file), and give the
99 * box some time (100 * 1 ms)
100 */
101 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000102 if (tstc()) {
103 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000104 }
105 udelay(1000);
106 }
107
108 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000109 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000110 rcode = 1;
111 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000112 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700113 image_load_addr = addr;
wdenk8bde7f72003-06-27 21:31:46 +0000114 }
115
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200116#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000117 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000118 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000119 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000120 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000121 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000122 serial_setbrg();
123 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000124 for (;;) {
125 if (getc() == 0x1B) /* ESC */
126 break;
127 }
128 }
129#endif
130 return rcode;
131}
132
Kim Phillips088f1b12012-10-29 13:34:31 +0000133static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000134{
135 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
136 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
137 int binlen; /* no. of data bytes in S-Rec. */
138 int type; /* return code for record type */
139 ulong addr; /* load address from S-Record */
140 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000141 ulong store_addr;
142 ulong start_addr = ~0;
143 ulong end_addr = 0;
144 int line_count = 0;
145
146 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000147 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000148
149 if (type < 0) {
150 return (~0); /* Invalid S-Record */
151 }
152
153 switch (type) {
154 case SREC_DATA2:
155 case SREC_DATA3:
156 case SREC_DATA4:
157 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900158#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000159 if (addr2info(store_addr)) {
160 int rc;
161
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200162 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000163 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000164 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000165 return (~0);
166 }
167 } else
168#endif
169 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000170 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000171 }
172 if ((store_addr) < start_addr)
173 start_addr = store_addr;
174 if ((store_addr + binlen - 1) > end_addr)
175 end_addr = store_addr + binlen - 1;
176 break;
177 case SREC_END2:
178 case SREC_END3:
179 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000180 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000181 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000182 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000183 "## First Load Addr = 0x%08lX\n"
184 "## Last Load Addr = 0x%08lX\n"
185 "## Total Size = 0x%08lX = %ld Bytes\n",
186 start_addr, end_addr, size, size
187 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000188 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600189 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000190 return (addr);
191 case SREC_START:
192 break;
193 default:
194 break;
195 }
196 if (!do_echo) { /* print a '.' every 100 lines */
197 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000198 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000199 }
200 }
201
202 return (~0); /* Download aborted */
203}
204
Kim Phillips088f1b12012-10-29 13:34:31 +0000205static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000206{
207 char *p;
208 char c;
209
210 --len; /* always leave room for terminating '\0' byte */
211
212 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000213 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000214 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000215 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000216
217 switch (c) {
218 case '\r':
219 case '\n':
220 *p = '\0';
221 return (p - buf);
222 case '\0':
223 case 0x03: /* ^C - Control C */
224 return (-1);
225 default:
226 *p = c;
227 }
228
229 /* Check for the console hangup (if any different from serial) */
Martin Dorwig49cad542015-01-26 15:22:54 -0700230 if (gd->jt->getc != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000231 if (ctrlc()) {
232 return (-1);
233 }
234 }
wdenk8bde7f72003-06-27 21:31:46 +0000235 }
236
237 /* line too long - truncate */
238 *p = '\0';
239 return (p - buf);
240}
241
Jon Loeligerc76fe472007-07-08 18:02:23 -0500242#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000243
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200244int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000245{
246 ulong offset = 0;
247 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200248#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000249 int save_baudrate, current_baudrate;
250
251 save_baudrate = current_baudrate = gd->baudrate;
252#endif
253
254 if (argc >= 2) {
255 offset = simple_strtoul(argv[1], NULL, 16);
256 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000258 if (argc >= 3) {
259 size = simple_strtoul(argv[2], NULL, 16);
260 }
261 if (argc == 4) {
262 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
263
264 /* default to current baudrate */
265 if (save_baudrate == 0)
266 save_baudrate = current_baudrate;
267 }
268 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000269 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000270 save_baudrate);
271 udelay(50000);
272 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000273 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000274 udelay(50000);
275 for (;;) {
276 if (getc() == '\r')
277 break;
278 }
279 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000281 if (argc == 3) {
282 size = simple_strtoul(argv[2], NULL, 16);
283 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200284#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000285
Kim Phillips088f1b12012-10-29 13:34:31 +0000286 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000287 for (;;) {
288 if (getc() == '\r')
289 break;
290 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000291 if (save_serial(offset, size)) {
292 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000293 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000294 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000295 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000297 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000298 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000299 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000300 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000301 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000302 serial_setbrg();
303 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000304 for (;;) {
305 if (getc() == 0x1B) /* ESC */
306 break;
307 }
308 }
309#endif
310 return 0;
311}
312
313#define SREC3_START "S0030000FC\n"
314#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
315#define SREC3_END "S70500000000FA\n"
316#define SREC_BYTES_PER_RECORD 16
317
Kim Phillips088f1b12012-10-29 13:34:31 +0000318static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000319{
320 int i, c, reclen, checksum, length;
321 char *hex = "0123456789ABCDEF";
322 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
323 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
324
325 reclen = 0;
326 checksum = 0;
327
328 if(write_record(SREC3_START)) /* write the header */
329 return (-1);
330 do {
331 if(count) { /* collect hex data in the buffer */
332 c = *(volatile uchar*)(address + reclen); /* get one byte */
333 checksum += c; /* accumulate checksum */
334 data[2*reclen] = hex[(c>>4)&0x0f];
335 data[2*reclen+1] = hex[c & 0x0f];
336 data[2*reclen+2] = '\0';
337 ++reclen;
338 --count;
339 }
340 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
341 /* enough data collected for one record: dump it */
342 if(reclen) { /* build & write a data record: */
343 /* address + data + checksum */
344 length = 4 + reclen + 1;
345
346 /* accumulate length bytes into checksum */
347 for(i = 0; i < 2; i++)
348 checksum += (length >> (8*i)) & 0xff;
349
350 /* accumulate address bytes into checksum: */
351 for(i = 0; i < 4; i++)
352 checksum += (address >> (8*i)) & 0xff;
353
354 /* make proper checksum byte: */
355 checksum = ~checksum & 0xff;
356
357 /* output one record: */
358 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
359 if(write_record(record))
360 return (-1);
361 }
362 address += reclen; /* increment address */
363 checksum = 0;
364 reclen = 0;
365 }
366 }
367 while(count);
368 if(write_record(SREC3_END)) /* write the final record */
369 return (-1);
370 return(0);
371}
372
Kim Phillips088f1b12012-10-29 13:34:31 +0000373static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000374{
375 char c;
376
377 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000378 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000379
380 /* Check for the console hangup (if any different from serial) */
381
382 if (ctrlc()) {
383 return (-1);
384 }
385 return (0);
386}
Jon Loeliger90253172007-07-10 11:02:44 -0500387# endif
wdenk8bde7f72003-06-27 21:31:46 +0000388
Jon Loeliger90253172007-07-10 11:02:44 -0500389#endif
wdenk8bde7f72003-06-27 21:31:46 +0000390
391
Jon Loeligerc76fe472007-07-08 18:02:23 -0500392#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500393/*
394 * loadb command (load binary) included
395 */
wdenk8bde7f72003-06-27 21:31:46 +0000396#define XON_CHAR 17
397#define XOFF_CHAR 19
398#define START_CHAR 0x01
399#define ETX_CHAR 0x03
400#define END_CHAR 0x0D
401#define SPACE 0x20
402#define K_ESCAPE 0x23
403#define SEND_TYPE 'S'
404#define DATA_TYPE 'D'
405#define ACK_TYPE 'Y'
406#define NACK_TYPE 'N'
407#define BREAK_TYPE 'B'
408#define tochar(x) ((char) (((x) + SPACE) & 0xff))
409#define untochar(x) ((int) (((x) - SPACE) & 0xff))
410
wdenk8bde7f72003-06-27 21:31:46 +0000411static void set_kerm_bin_mode(unsigned long *);
412static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000413static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000414
415
Kim Phillips088f1b12012-10-29 13:34:31 +0000416static char his_eol; /* character he needs at end of packet */
417static int his_pad_count; /* number of pad chars he needs */
418static char his_pad_char; /* pad chars he needs */
419static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000420
Kim Phillips088f1b12012-10-29 13:34:31 +0000421static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
422 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000423{
wdenk8bde7f72003-06-27 21:31:46 +0000424 ulong offset = 0;
425 ulong addr;
426 int load_baudrate, current_baudrate;
427 int rcode = 0;
428 char *s;
429
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200430 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
431 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000432
433 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600434 s = env_get("loadaddr");
435 if (s)
wdenk8bde7f72003-06-27 21:31:46 +0000436 offset = simple_strtoul(s, NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000437
438 load_baudrate = current_baudrate = gd->baudrate;
439
440 if (argc >= 2) {
441 offset = simple_strtoul(argv[1], NULL, 16);
442 }
443 if (argc == 3) {
444 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
445
446 /* default to current baudrate */
447 if (load_baudrate == 0)
448 load_baudrate = current_baudrate;
449 }
450
451 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000452 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000453 load_baudrate);
454 udelay(50000);
455 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000456 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000457 udelay(50000);
458 for (;;) {
459 if (getc() == '\r')
460 break;
461 }
462 }
463
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200464 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000465 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200466 "to 0x%08lX at %d bps...\n",
467 offset,
468 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000469
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600470 addr = load_serial_ymodem(offset, xyzModem_ymodem);
471
472 } else if (strcmp(argv[0],"loadx")==0) {
473 printf("## Ready for binary (xmodem) download "
474 "to 0x%08lX at %d bps...\n",
475 offset,
476 load_baudrate);
477
478 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200479
wdenk8bde7f72003-06-27 21:31:46 +0000480 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000481
Kim Phillips088f1b12012-10-29 13:34:31 +0000482 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200483 "to 0x%08lX at %d bps...\n",
484 offset,
485 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000486 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200487
488 if (addr == ~0) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700489 image_load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000490 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200491 rcode = 1;
492 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000493 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700494 image_load_addr = addr;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200495 }
496 }
wdenk8bde7f72003-06-27 21:31:46 +0000497 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000498 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000499 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000500 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000501 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000502 serial_setbrg();
503 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000504 for (;;) {
505 if (getc() == 0x1B) /* ESC */
506 break;
507 }
508 }
509
wdenk8bde7f72003-06-27 21:31:46 +0000510 return rcode;
511}
512
513
Kim Phillips088f1b12012-10-29 13:34:31 +0000514static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000515{
516 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000517
Kim Phillips088f1b12012-10-29 13:34:31 +0000518 set_kerm_bin_mode((ulong *) offset);
519 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000520
521 /*
522 * Gather any trailing characters (for instance, the ^D which
523 * is sent by 'cu' after sending a file), and give the
524 * box some time (100 * 1 ms)
525 */
526 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000527 if (tstc()) {
528 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000529 }
530 udelay(1000);
531 }
532
Kim Phillips088f1b12012-10-29 13:34:31 +0000533 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000534
535 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600536 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000537
538 return offset;
539}
540
Kim Phillips088f1b12012-10-29 13:34:31 +0000541static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000542{
543 int count = his_pad_count;
544
545 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000546 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000547}
548
549/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000550static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000551{
552 if ((in & 0x60) == 0x40) {
553 return (char) (in & ~0x40);
554 } else if ((in & 0x7f) == 0x3f) {
555 return (char) (in | 0x40);
556 } else
557 return in;
558}
559
Kim Phillips088f1b12012-10-29 13:34:31 +0000560static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000561{
562 int total = 0;
563
564 while (*buffer) {
565 total += *buffer++;
566 }
567 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
568}
569
Kim Phillips088f1b12012-10-29 13:34:31 +0000570static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000571{
Kim Phillips088f1b12012-10-29 13:34:31 +0000572 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000573 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000574 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000575 }
576}
577
578static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000579static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000580{
581 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000582 a_b[1] = tochar(3);
583 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000584 a_b[3] = ACK_TYPE;
585 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000586 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000587 a_b[5] = his_eol;
588 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000589 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000590}
591
Kim Phillips088f1b12012-10-29 13:34:31 +0000592static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000593{
594 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000595 a_b[1] = tochar(3);
596 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000597 a_b[3] = NACK_TYPE;
598 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000599 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000600 a_b[5] = his_eol;
601 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000602 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000603}
604
605
Kim Phillips088f1b12012-10-29 13:34:31 +0000606static void (*os_data_init)(void);
607static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000608static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000609static char *os_data_addr, *os_data_addr_saved;
610static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530611
Kim Phillips088f1b12012-10-29 13:34:31 +0000612static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000613{
614 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000615 os_data_addr = bin_start_address;
616}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530617
Kim Phillips088f1b12012-10-29 13:34:31 +0000618static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000619{
620 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000621 os_data_addr_saved = os_data_addr;
622}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530623
Kim Phillips088f1b12012-10-29 13:34:31 +0000624static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000625{
626 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000627 os_data_addr = os_data_addr_saved;
628}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530629
Kim Phillips088f1b12012-10-29 13:34:31 +0000630static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000631{
632 switch (os_data_state) {
633 case 0: /* data */
634 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000635 break;
636 }
637}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530638
Kim Phillips088f1b12012-10-29 13:34:31 +0000639static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000640{
641 bin_start_address = (char *) addr;
642 os_data_init = bin_data_init;
643 os_data_char = bin_data_char;
644}
645
646
647/* k_data_* simply handles the kermit escape translations */
648static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000649static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000650{
651 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000652 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000653}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530654
Kim Phillips088f1b12012-10-29 13:34:31 +0000655static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000656{
657 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000658 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000659}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530660
Kim Phillips088f1b12012-10-29 13:34:31 +0000661static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000662{
663 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000664 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000665}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530666
Kim Phillips088f1b12012-10-29 13:34:31 +0000667static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000668{
669 if (k_data_escape) {
670 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000671 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000672 k_data_escape = 0;
673 } else {
674 if (new_char == his_quote) {
675 /* this char is escape - remember */
676 k_data_escape = 1;
677 } else {
678 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000679 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000680 }
681 }
682}
683
684#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000685static char send_parms[SEND_DATA_SIZE];
686static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000687
688/* handle_send_packet interprits the protocol info and builds and
689 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000690static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000691{
692 int length = 3;
693 int bytes;
694
695 /* initialize some protocol parameters */
696 his_eol = END_CHAR; /* default end of line character */
697 his_pad_count = 0;
698 his_pad_char = '\0';
699 his_quote = K_ESCAPE;
700
701 /* ignore last character if it filled the buffer */
702 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
703 --send_ptr;
704 bytes = send_ptr - send_parms; /* how many bytes we'll process */
705 do {
706 if (bytes-- <= 0)
707 break;
708 /* handle MAXL - max length */
709 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000710 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000711 if (bytes-- <= 0)
712 break;
713 /* handle TIME - time you should wait for my packets */
714 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000715 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000716 if (bytes-- <= 0)
717 break;
718 /* handle NPAD - number of pad chars I need */
719 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000720 his_pad_count = untochar(send_parms[2]);
721 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000722 if (bytes-- <= 0)
723 break;
724 /* handle PADC - pad chars I need */
725 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000726 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000727 a_b[++length] = 0x40; /* He should ignore this */
728 if (bytes-- <= 0)
729 break;
730 /* handle EOL - end of line he needs */
731 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000732 his_eol = untochar(send_parms[4]);
733 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000734 if (bytes-- <= 0)
735 break;
736 /* handle QCTL - quote control char he'll use */
737 /* remember what he says - I'll use '#' */
738 his_quote = send_parms[5];
739 a_b[++length] = '#';
740 if (bytes-- <= 0)
741 break;
742 /* handle QBIN - 8-th bit prefixing */
743 /* ignore what he says - I refuse */
744 a_b[++length] = 'N';
745 if (bytes-- <= 0)
746 break;
747 /* handle CHKT - the clock check type */
748 /* ignore what he says - I do type 1 (for now) */
749 a_b[++length] = '1';
750 if (bytes-- <= 0)
751 break;
752 /* handle REPT - the repeat prefix */
753 /* ignore what he says - I refuse (for now) */
754 a_b[++length] = 'N';
755 if (bytes-- <= 0)
756 break;
757 /* handle CAPAS - the capabilities mask */
758 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000759 a_b[++length] = tochar(2); /* only long packets */
760 a_b[++length] = tochar(0); /* no windows */
761 a_b[++length] = tochar(94); /* large packet msb */
762 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000763 } while (0);
764
765 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000766 a_b[1] = tochar(length);
767 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000768 a_b[3] = ACK_TYPE;
769 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000770 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000771 a_b[++length] = his_eol;
772 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000773 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000774}
775
776/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000777static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000778{
779 char new_char;
780 char k_state, k_state_saved;
781 int sum;
782 int done;
783 int length;
784 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000785 int len_lo, len_hi;
786
787 /* initialize some protocol parameters */
788 his_eol = END_CHAR; /* default end of line character */
789 his_pad_count = 0;
790 his_pad_char = '\0';
791 his_quote = K_ESCAPE;
792
793 /* initialize the k_recv and k_data state machine */
794 done = 0;
795 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000796 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000797 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000798 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000799 n = 0; /* just to get rid of a warning */
800 last_n = -1;
801
802 /* expect this "type" sequence (but don't check):
803 S: send initiate
804 F: file header
805 D: data (multiple)
806 Z: end of file
807 B: break transmission
808 */
809
810 /* enter main loop */
811 while (!done) {
812 /* set the send packet pointer to begining of send packet parms */
813 send_ptr = send_parms;
814
815 /* With each packet, start summing the bytes starting with the length.
816 Save the current sequence number.
817 Note the type of the packet.
818 If a character less than SPACE (0x20) is received - error.
819 */
820
821#if 0
822 /* OLD CODE, Prior to checking sequence numbers */
823 /* first have all state machines save current states */
824 k_state_saved = k_state;
825 k_data_save ();
826#endif
827
828 /* get a packet */
829 /* wait for the starting character or ^C */
830 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000831 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000832 case START_CHAR: /* start packet */
833 goto START;
834 case ETX_CHAR: /* ^C waiting for packet */
835 return (0);
836 default:
837 ;
838 }
839 }
840START:
841 /* get length of packet */
842 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000843 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000844 if ((new_char & 0xE0) == 0)
845 goto packet_error;
846 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000847 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000848 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +0000849 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000850 if ((new_char & 0xE0) == 0)
851 goto packet_error;
852 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000853 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000854 --length;
855
856 /* NEW CODE - check sequence numbers for retried packets */
857 /* Note - this new code assumes that the sequence number is correctly
858 * received. Handling an invalid sequence number adds another layer
859 * of complexity that may not be needed - yet! At this time, I'm hoping
860 * that I don't need to buffer the incoming data packets and can write
861 * the data into memory in real time.
862 */
863 if (n == last_n) {
864 /* same sequence number, restore the previous state */
865 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000866 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000867 } else {
868 /* new sequence number, checkpoint the download */
869 last_n = n;
870 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000871 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000872 }
873 /* END NEW CODE */
874
875 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000876 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000877 if ((new_char & 0xE0) == 0)
878 goto packet_error;
879 sum += new_char & 0xff;
880 k_state = new_char;
881 --length;
882 /* check for extended length */
883 if (length == -2) {
884 /* (length byte was 0, decremented twice) */
885 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000886 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000887 if ((new_char & 0xE0) == 0)
888 goto packet_error;
889 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000890 len_hi = untochar(new_char);
891 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000892 if ((new_char & 0xE0) == 0)
893 goto packet_error;
894 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000895 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000896 length = len_hi * 95 + len_lo;
897 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000898 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000899 if ((new_char & 0xE0) == 0)
900 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000901 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000902 goto packet_error;
903 sum += new_char & 0xff;
904/* --length; */ /* new length includes only data and block check to come */
905 }
906 /* bring in rest of packet */
907 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000908 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000909 if ((new_char & 0xE0) == 0)
910 goto packet_error;
911 sum += new_char & 0xff;
912 --length;
913 if (k_state == DATA_TYPE) {
914 /* pass on the data if this is a data packet */
915 k_data_char (new_char);
916 } else if (k_state == SEND_TYPE) {
917 /* save send pack in buffer as is */
918 *send_ptr++ = new_char;
919 /* if too much data, back off the pointer */
920 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
921 --send_ptr;
922 }
923 }
924 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000925 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000926 if ((new_char & 0xE0) == 0)
927 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000928 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000929 goto packet_error;
930 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000931 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000932 if (new_char != END_CHAR) {
933 packet_error:
934 /* restore state machines */
935 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000936 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000937 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000938 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000939 } else if (k_state == SEND_TYPE) {
940 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000941 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000942 } else {
943 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000944 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000945 /* quit if end of transmission */
946 if (k_state == BREAK_TYPE)
947 done = 1;
948 }
wdenk8bde7f72003-06-27 21:31:46 +0000949 }
950 return ((ulong) os_data_addr - (ulong) bin_start_address);
951}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200952
953static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200954 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200955 return (getc());
956 return -1;
957}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600958static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200959{
960 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200961 int err;
962 int res;
963 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200964 char ymodemBuf[1024];
965 ulong store_addr = ~0;
966 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200967
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200968 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600969 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000970 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200971 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200972
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200973 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000974 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200975 store_addr = addr + offset;
976 size += res;
977 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900978#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000979 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200980 int rc;
981
Kim Phillips088f1b12012-10-29 13:34:31 +0000982 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200983 store_addr, res);
984 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700985 flash_perror(rc);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200986 return (~0);
987 }
988 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200989#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200990 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000991 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200992 res);
993 }
994
995 }
996 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000997 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200998 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200999
Kim Phillips088f1b12012-10-29 13:34:31 +00001000 xyzModem_stream_close(&err);
1001 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001002
1003
Chris Packham0a6036d2016-10-25 20:22:48 +13001004 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001005
Kim Phillips088f1b12012-10-29 13:34:31 +00001006 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001007 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001008
1009 return offset;
1010}
1011
Jon Loeliger90253172007-07-10 11:02:44 -05001012#endif
wdenk8bde7f72003-06-27 21:31:46 +00001013
1014/* -------------------------------------------------------------------- */
1015
Jon Loeligerc76fe472007-07-08 18:02:23 -05001016#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001017
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001018#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001019U_BOOT_CMD(
1020 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001021 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001022 "[ off ] [ baud ]\n"
1023 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001024 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001025);
1026
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001027#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001028U_BOOT_CMD(
1029 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001030 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001031 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001032 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001033);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001034#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001035
1036/*
1037 * SAVES always requires LOADS support, but not vice versa
1038 */
1039
1040
Jon Loeligerc76fe472007-07-08 18:02:23 -05001041#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001042#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001043U_BOOT_CMD(
1044 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001045 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001046 "[ off ] [size] [ baud ]\n"
1047 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001048 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001049);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001050#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001051U_BOOT_CMD(
1052 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001053 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001054 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001055 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001056);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001057#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001058#endif /* CONFIG_CMD_SAVES */
1059#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001060
1061
Jon Loeligerc76fe472007-07-08 18:02:23 -05001062#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001063U_BOOT_CMD(
1064 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001065 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001066 "[ off ] [ baud ]\n"
1067 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001068 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001069);
1070
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001071U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001072 loadx, 3, 0, do_load_serial_bin,
1073 "load binary file over serial line (xmodem mode)",
1074 "[ off ] [ baud ]\n"
1075 " - load binary file over serial line"
1076 " with offset 'off' and baudrate 'baud'"
1077);
1078
1079U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001080 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001081 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001082 "[ off ] [ baud ]\n"
1083 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001084 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001085);
1086
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001087#endif /* CONFIG_CMD_LOADB */