blob: f252c748948aa1e7d2a019e620bf4fda5cab960c [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>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glassc05ed002020-05-10 11:40:11 -060023#include <linux/delay.h>
wdenk8bde7f72003-06-27 21:31:46 +000024
Wolfgang Denkd87080b2006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000026
Jon Loeligerc76fe472007-07-08 18:02:23 -050027#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060028static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020029#endif
30
Jon Loeligerc76fe472007-07-08 18:02:23 -050031#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000032static ulong load_serial(long offset);
33static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050034# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000035static int save_serial(ulong offset, ulong size);
36static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050037#endif
wdenk8bde7f72003-06-27 21:31:46 +000038
39static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050040#endif
wdenk8bde7f72003-06-27 21:31:46 +000041
42/* -------------------------------------------------------------------- */
43
Jon Loeligerc76fe472007-07-08 18:02:23 -050044#if defined(CONFIG_CMD_LOADS)
Simon Glass09140112020-05-10 11:40:03 -060045static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
46 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000047{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020048 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000049 ulong addr;
50 int i;
51 char *env_echo;
52 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000054 int load_baudrate, current_baudrate;
55
56 load_baudrate = current_baudrate = gd->baudrate;
57#endif
58
Simon Glass00caae62017-08-03 12:22:12 -060059 env_echo = env_get("loads_echo");
60 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000061 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060062 else
wdenk8bde7f72003-06-27 21:31:46 +000063 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000064
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000066 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020067 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000068 }
69 if (argc == 3) {
70 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
71
72 /* default to current baudrate */
73 if (load_baudrate == 0)
74 load_baudrate = current_baudrate;
75 }
76 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000077 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000078 load_baudrate);
79 udelay(50000);
80 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000081 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000082 udelay(50000);
83 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020084 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +000085 break;
86 }
87 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000089 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020090 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000091 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000093
Kim Phillips088f1b12012-10-29 13:34:31 +000094 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000095
Kim Phillips088f1b12012-10-29 13:34:31 +000096 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000097
98 /*
99 * Gather any trailing characters (for instance, the ^D which
100 * is sent by 'cu' after sending a file), and give the
101 * box some time (100 * 1 ms)
102 */
103 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000104 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200105 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000106 }
107 udelay(1000);
108 }
109
110 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000111 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000112 rcode = 1;
113 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000114 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700115 image_load_addr = addr;
wdenk8bde7f72003-06-27 21:31:46 +0000116 }
117
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000119 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000120 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000121 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000122 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000123 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000124 serial_setbrg();
125 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000126 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200127 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000128 break;
129 }
130 }
131#endif
132 return rcode;
133}
134
Kim Phillips088f1b12012-10-29 13:34:31 +0000135static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000136{
137 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
138 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
139 int binlen; /* no. of data bytes in S-Rec. */
140 int type; /* return code for record type */
141 ulong addr; /* load address from S-Record */
142 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000143 ulong store_addr;
144 ulong start_addr = ~0;
145 ulong end_addr = 0;
146 int line_count = 0;
147
148 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000149 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000150
151 if (type < 0) {
152 return (~0); /* Invalid S-Record */
153 }
154
155 switch (type) {
156 case SREC_DATA2:
157 case SREC_DATA3:
158 case SREC_DATA4:
159 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900160#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000161 if (addr2info(store_addr)) {
162 int rc;
163
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200164 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000165 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000166 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000167 return (~0);
168 }
169 } else
170#endif
171 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000172 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000173 }
174 if ((store_addr) < start_addr)
175 start_addr = store_addr;
176 if ((store_addr + binlen - 1) > end_addr)
177 end_addr = store_addr + binlen - 1;
178 break;
179 case SREC_END2:
180 case SREC_END3:
181 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000182 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000183 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000184 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000185 "## First Load Addr = 0x%08lX\n"
186 "## Last Load Addr = 0x%08lX\n"
187 "## Total Size = 0x%08lX = %ld Bytes\n",
188 start_addr, end_addr, size, size
189 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000190 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600191 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000192 return (addr);
193 case SREC_START:
194 break;
195 default:
196 break;
197 }
198 if (!do_echo) { /* print a '.' every 100 lines */
199 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000200 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000201 }
202 }
203
204 return (~0); /* Download aborted */
205}
206
Kim Phillips088f1b12012-10-29 13:34:31 +0000207static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000208{
209 char *p;
210 char c;
211
212 --len; /* always leave room for terminating '\0' byte */
213
214 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200215 c = getchar(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000216 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000217 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000218
219 switch (c) {
220 case '\r':
221 case '\n':
222 *p = '\0';
223 return (p - buf);
224 case '\0':
225 case 0x03: /* ^C - Control C */
226 return (-1);
227 default:
228 *p = c;
229 }
230
Michal Simeke7fcfef2020-12-01 13:58:28 +0100231 /* Check for the console hangup (if any different from serial) */
232 if (gd->jt->getc != getchar) {
233 if (ctrlc())
234 return (-1);
wdenk8bde7f72003-06-27 21:31:46 +0000235 }
wdenk8bde7f72003-06-27 21:31:46 +0000236 }
237
238 /* line too long - truncate */
239 *p = '\0';
240 return (p - buf);
241}
242
Jon Loeligerc76fe472007-07-08 18:02:23 -0500243#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000244
Simon Glass09140112020-05-10 11:40:03 -0600245int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
246 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000247{
248 ulong offset = 0;
249 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200250#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000251 int save_baudrate, current_baudrate;
252
253 save_baudrate = current_baudrate = gd->baudrate;
254#endif
255
256 if (argc >= 2) {
257 offset = simple_strtoul(argv[1], NULL, 16);
258 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200259#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000260 if (argc >= 3) {
261 size = simple_strtoul(argv[2], NULL, 16);
262 }
263 if (argc == 4) {
264 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
265
266 /* default to current baudrate */
267 if (save_baudrate == 0)
268 save_baudrate = current_baudrate;
269 }
270 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000271 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000272 save_baudrate);
273 udelay(50000);
274 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000275 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000276 udelay(50000);
277 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200278 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000279 break;
280 }
281 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200282#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000283 if (argc == 3) {
284 size = simple_strtoul(argv[2], NULL, 16);
285 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200286#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000287
Kim Phillips088f1b12012-10-29 13:34:31 +0000288 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000289 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200290 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000291 break;
292 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000293 if (save_serial(offset, size)) {
294 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000295 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000296 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000297 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200298#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000299 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000300 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000301 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000302 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000303 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000304 serial_setbrg();
305 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000306 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200307 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000308 break;
309 }
310 }
311#endif
312 return 0;
313}
314
315#define SREC3_START "S0030000FC\n"
316#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
317#define SREC3_END "S70500000000FA\n"
318#define SREC_BYTES_PER_RECORD 16
319
Kim Phillips088f1b12012-10-29 13:34:31 +0000320static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000321{
322 int i, c, reclen, checksum, length;
323 char *hex = "0123456789ABCDEF";
324 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
325 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
326
327 reclen = 0;
328 checksum = 0;
329
330 if(write_record(SREC3_START)) /* write the header */
331 return (-1);
332 do {
333 if(count) { /* collect hex data in the buffer */
334 c = *(volatile uchar*)(address + reclen); /* get one byte */
335 checksum += c; /* accumulate checksum */
336 data[2*reclen] = hex[(c>>4)&0x0f];
337 data[2*reclen+1] = hex[c & 0x0f];
338 data[2*reclen+2] = '\0';
339 ++reclen;
340 --count;
341 }
342 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
343 /* enough data collected for one record: dump it */
344 if(reclen) { /* build & write a data record: */
345 /* address + data + checksum */
346 length = 4 + reclen + 1;
347
348 /* accumulate length bytes into checksum */
349 for(i = 0; i < 2; i++)
350 checksum += (length >> (8*i)) & 0xff;
351
352 /* accumulate address bytes into checksum: */
353 for(i = 0; i < 4; i++)
354 checksum += (address >> (8*i)) & 0xff;
355
356 /* make proper checksum byte: */
357 checksum = ~checksum & 0xff;
358
359 /* output one record: */
360 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
361 if(write_record(record))
362 return (-1);
363 }
364 address += reclen; /* increment address */
365 checksum = 0;
366 reclen = 0;
367 }
368 }
369 while(count);
370 if(write_record(SREC3_END)) /* write the final record */
371 return (-1);
372 return(0);
373}
374
Kim Phillips088f1b12012-10-29 13:34:31 +0000375static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000376{
377 char c;
378
379 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000380 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000381
382 /* Check for the console hangup (if any different from serial) */
383
384 if (ctrlc()) {
385 return (-1);
386 }
387 return (0);
388}
Jon Loeliger90253172007-07-10 11:02:44 -0500389# endif
wdenk8bde7f72003-06-27 21:31:46 +0000390
Jon Loeliger90253172007-07-10 11:02:44 -0500391#endif
wdenk8bde7f72003-06-27 21:31:46 +0000392
393
Jon Loeligerc76fe472007-07-08 18:02:23 -0500394#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500395/*
396 * loadb command (load binary) included
397 */
wdenk8bde7f72003-06-27 21:31:46 +0000398#define XON_CHAR 17
399#define XOFF_CHAR 19
400#define START_CHAR 0x01
401#define ETX_CHAR 0x03
402#define END_CHAR 0x0D
403#define SPACE 0x20
404#define K_ESCAPE 0x23
405#define SEND_TYPE 'S'
406#define DATA_TYPE 'D'
407#define ACK_TYPE 'Y'
408#define NACK_TYPE 'N'
409#define BREAK_TYPE 'B'
410#define tochar(x) ((char) (((x) + SPACE) & 0xff))
411#define untochar(x) ((int) (((x) - SPACE) & 0xff))
412
wdenk8bde7f72003-06-27 21:31:46 +0000413static void set_kerm_bin_mode(unsigned long *);
414static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000415static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000416
417
Kim Phillips088f1b12012-10-29 13:34:31 +0000418static char his_eol; /* character he needs at end of packet */
419static int his_pad_count; /* number of pad chars he needs */
420static char his_pad_char; /* pad chars he needs */
421static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000422
Simon Glass09140112020-05-10 11:40:03 -0600423static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
424 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000425{
wdenk8bde7f72003-06-27 21:31:46 +0000426 ulong offset = 0;
427 ulong addr;
428 int load_baudrate, current_baudrate;
429 int rcode = 0;
430 char *s;
431
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
433 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000434
435 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600436 s = env_get("loadaddr");
437 if (s)
wdenk8bde7f72003-06-27 21:31:46 +0000438 offset = simple_strtoul(s, NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000439
440 load_baudrate = current_baudrate = gd->baudrate;
441
442 if (argc >= 2) {
443 offset = simple_strtoul(argv[1], NULL, 16);
444 }
445 if (argc == 3) {
446 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
447
448 /* default to current baudrate */
449 if (load_baudrate == 0)
450 load_baudrate = current_baudrate;
451 }
452
453 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000454 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000455 load_baudrate);
456 udelay(50000);
457 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000458 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000459 udelay(50000);
460 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200461 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000462 break;
463 }
464 }
465
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200466 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000467 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200468 "to 0x%08lX at %d bps...\n",
469 offset,
470 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000471
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600472 addr = load_serial_ymodem(offset, xyzModem_ymodem);
473
474 } else if (strcmp(argv[0],"loadx")==0) {
475 printf("## Ready for binary (xmodem) download "
476 "to 0x%08lX at %d bps...\n",
477 offset,
478 load_baudrate);
479
480 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200481
wdenk8bde7f72003-06-27 21:31:46 +0000482 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000483
Kim Phillips088f1b12012-10-29 13:34:31 +0000484 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200485 "to 0x%08lX at %d bps...\n",
486 offset,
487 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000488 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200489
490 if (addr == ~0) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700491 image_load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000492 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200493 rcode = 1;
494 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000495 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700496 image_load_addr = addr;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200497 }
498 }
wdenk8bde7f72003-06-27 21:31:46 +0000499 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000500 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000501 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000502 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000503 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000504 serial_setbrg();
505 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000506 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200507 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000508 break;
509 }
510 }
511
wdenk8bde7f72003-06-27 21:31:46 +0000512 return rcode;
513}
514
515
Kim Phillips088f1b12012-10-29 13:34:31 +0000516static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000517{
518 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000519
Kim Phillips088f1b12012-10-29 13:34:31 +0000520 set_kerm_bin_mode((ulong *) offset);
521 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000522
523 /*
524 * Gather any trailing characters (for instance, the ^D which
525 * is sent by 'cu' after sending a file), and give the
526 * box some time (100 * 1 ms)
527 */
528 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000529 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200530 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000531 }
532 udelay(1000);
533 }
534
Kim Phillips088f1b12012-10-29 13:34:31 +0000535 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000536
537 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600538 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000539
540 return offset;
541}
542
Kim Phillips088f1b12012-10-29 13:34:31 +0000543static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000544{
545 int count = his_pad_count;
546
547 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000548 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000549}
550
551/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000552static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000553{
554 if ((in & 0x60) == 0x40) {
555 return (char) (in & ~0x40);
556 } else if ((in & 0x7f) == 0x3f) {
557 return (char) (in | 0x40);
558 } else
559 return in;
560}
561
Kim Phillips088f1b12012-10-29 13:34:31 +0000562static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000563{
564 int total = 0;
565
566 while (*buffer) {
567 total += *buffer++;
568 }
569 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
570}
571
Kim Phillips088f1b12012-10-29 13:34:31 +0000572static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000573{
Kim Phillips088f1b12012-10-29 13:34:31 +0000574 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000575 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000576 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000577 }
578}
579
580static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000581static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000582{
583 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000584 a_b[1] = tochar(3);
585 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000586 a_b[3] = ACK_TYPE;
587 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000588 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000589 a_b[5] = his_eol;
590 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000591 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000592}
593
Kim Phillips088f1b12012-10-29 13:34:31 +0000594static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000595{
596 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000597 a_b[1] = tochar(3);
598 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000599 a_b[3] = NACK_TYPE;
600 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000601 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000602 a_b[5] = his_eol;
603 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000604 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000605}
606
607
Kim Phillips088f1b12012-10-29 13:34:31 +0000608static void (*os_data_init)(void);
609static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000610static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000611static char *os_data_addr, *os_data_addr_saved;
612static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530613
Kim Phillips088f1b12012-10-29 13:34:31 +0000614static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000615{
616 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000617 os_data_addr = bin_start_address;
618}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530619
Kim Phillips088f1b12012-10-29 13:34:31 +0000620static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000621{
622 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000623 os_data_addr_saved = os_data_addr;
624}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530625
Kim Phillips088f1b12012-10-29 13:34:31 +0000626static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000627{
628 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000629 os_data_addr = os_data_addr_saved;
630}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530631
Kim Phillips088f1b12012-10-29 13:34:31 +0000632static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000633{
634 switch (os_data_state) {
635 case 0: /* data */
636 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000637 break;
638 }
639}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530640
Kim Phillips088f1b12012-10-29 13:34:31 +0000641static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000642{
643 bin_start_address = (char *) addr;
644 os_data_init = bin_data_init;
645 os_data_char = bin_data_char;
646}
647
648
649/* k_data_* simply handles the kermit escape translations */
650static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000651static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000652{
653 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000654 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000655}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530656
Kim Phillips088f1b12012-10-29 13:34:31 +0000657static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000658{
659 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000660 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000661}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530662
Kim Phillips088f1b12012-10-29 13:34:31 +0000663static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000664{
665 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000666 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000667}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530668
Kim Phillips088f1b12012-10-29 13:34:31 +0000669static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000670{
671 if (k_data_escape) {
672 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000673 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000674 k_data_escape = 0;
675 } else {
676 if (new_char == his_quote) {
677 /* this char is escape - remember */
678 k_data_escape = 1;
679 } else {
680 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000681 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000682 }
683 }
684}
685
686#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000687static char send_parms[SEND_DATA_SIZE];
688static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000689
690/* handle_send_packet interprits the protocol info and builds and
691 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000692static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000693{
694 int length = 3;
695 int bytes;
696
697 /* initialize some protocol parameters */
698 his_eol = END_CHAR; /* default end of line character */
699 his_pad_count = 0;
700 his_pad_char = '\0';
701 his_quote = K_ESCAPE;
702
703 /* ignore last character if it filled the buffer */
704 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
705 --send_ptr;
706 bytes = send_ptr - send_parms; /* how many bytes we'll process */
707 do {
708 if (bytes-- <= 0)
709 break;
710 /* handle MAXL - max length */
711 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000712 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000713 if (bytes-- <= 0)
714 break;
715 /* handle TIME - time you should wait for my packets */
716 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000717 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000718 if (bytes-- <= 0)
719 break;
720 /* handle NPAD - number of pad chars I need */
721 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000722 his_pad_count = untochar(send_parms[2]);
723 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000724 if (bytes-- <= 0)
725 break;
726 /* handle PADC - pad chars I need */
727 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000728 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000729 a_b[++length] = 0x40; /* He should ignore this */
730 if (bytes-- <= 0)
731 break;
732 /* handle EOL - end of line he needs */
733 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000734 his_eol = untochar(send_parms[4]);
735 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000736 if (bytes-- <= 0)
737 break;
738 /* handle QCTL - quote control char he'll use */
739 /* remember what he says - I'll use '#' */
740 his_quote = send_parms[5];
741 a_b[++length] = '#';
742 if (bytes-- <= 0)
743 break;
744 /* handle QBIN - 8-th bit prefixing */
745 /* ignore what he says - I refuse */
746 a_b[++length] = 'N';
747 if (bytes-- <= 0)
748 break;
749 /* handle CHKT - the clock check type */
750 /* ignore what he says - I do type 1 (for now) */
751 a_b[++length] = '1';
752 if (bytes-- <= 0)
753 break;
754 /* handle REPT - the repeat prefix */
755 /* ignore what he says - I refuse (for now) */
756 a_b[++length] = 'N';
757 if (bytes-- <= 0)
758 break;
759 /* handle CAPAS - the capabilities mask */
760 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000761 a_b[++length] = tochar(2); /* only long packets */
762 a_b[++length] = tochar(0); /* no windows */
763 a_b[++length] = tochar(94); /* large packet msb */
764 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000765 } while (0);
766
767 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000768 a_b[1] = tochar(length);
769 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000770 a_b[3] = ACK_TYPE;
771 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000772 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000773 a_b[++length] = his_eol;
774 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000775 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000776}
777
778/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000779static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000780{
781 char new_char;
782 char k_state, k_state_saved;
783 int sum;
784 int done;
785 int length;
786 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000787 int len_lo, len_hi;
788
789 /* initialize some protocol parameters */
790 his_eol = END_CHAR; /* default end of line character */
791 his_pad_count = 0;
792 his_pad_char = '\0';
793 his_quote = K_ESCAPE;
794
795 /* initialize the k_recv and k_data state machine */
796 done = 0;
797 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000798 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000799 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000800 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000801 n = 0; /* just to get rid of a warning */
802 last_n = -1;
803
804 /* expect this "type" sequence (but don't check):
805 S: send initiate
806 F: file header
807 D: data (multiple)
808 Z: end of file
809 B: break transmission
810 */
811
812 /* enter main loop */
813 while (!done) {
814 /* set the send packet pointer to begining of send packet parms */
815 send_ptr = send_parms;
816
817 /* With each packet, start summing the bytes starting with the length.
818 Save the current sequence number.
819 Note the type of the packet.
820 If a character less than SPACE (0x20) is received - error.
821 */
822
823#if 0
824 /* OLD CODE, Prior to checking sequence numbers */
825 /* first have all state machines save current states */
826 k_state_saved = k_state;
827 k_data_save ();
828#endif
829
830 /* get a packet */
831 /* wait for the starting character or ^C */
832 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200833 switch (getchar()) {
wdenk8bde7f72003-06-27 21:31:46 +0000834 case START_CHAR: /* start packet */
835 goto START;
836 case ETX_CHAR: /* ^C waiting for packet */
837 return (0);
838 default:
839 ;
840 }
841 }
842START:
843 /* get length of packet */
844 sum = 0;
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200845 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000846 if ((new_char & 0xE0) == 0)
847 goto packet_error;
848 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000849 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000850 /* get sequence number */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200851 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000852 if ((new_char & 0xE0) == 0)
853 goto packet_error;
854 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000855 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000856 --length;
857
858 /* NEW CODE - check sequence numbers for retried packets */
859 /* Note - this new code assumes that the sequence number is correctly
860 * received. Handling an invalid sequence number adds another layer
861 * of complexity that may not be needed - yet! At this time, I'm hoping
862 * that I don't need to buffer the incoming data packets and can write
863 * the data into memory in real time.
864 */
865 if (n == last_n) {
866 /* same sequence number, restore the previous state */
867 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000868 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000869 } else {
870 /* new sequence number, checkpoint the download */
871 last_n = n;
872 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000873 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000874 }
875 /* END NEW CODE */
876
877 /* get packet type */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200878 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000879 if ((new_char & 0xE0) == 0)
880 goto packet_error;
881 sum += new_char & 0xff;
882 k_state = new_char;
883 --length;
884 /* check for extended length */
885 if (length == -2) {
886 /* (length byte was 0, decremented twice) */
887 /* get the two length bytes */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200888 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000889 if ((new_char & 0xE0) == 0)
890 goto packet_error;
891 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000892 len_hi = untochar(new_char);
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200893 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000894 if ((new_char & 0xE0) == 0)
895 goto packet_error;
896 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000897 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000898 length = len_hi * 95 + len_lo;
899 /* check header checksum */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200900 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000901 if ((new_char & 0xE0) == 0)
902 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000903 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000904 goto packet_error;
905 sum += new_char & 0xff;
906/* --length; */ /* new length includes only data and block check to come */
907 }
908 /* bring in rest of packet */
909 while (length > 1) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200910 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000911 if ((new_char & 0xE0) == 0)
912 goto packet_error;
913 sum += new_char & 0xff;
914 --length;
915 if (k_state == DATA_TYPE) {
916 /* pass on the data if this is a data packet */
917 k_data_char (new_char);
918 } else if (k_state == SEND_TYPE) {
919 /* save send pack in buffer as is */
920 *send_ptr++ = new_char;
921 /* if too much data, back off the pointer */
922 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
923 --send_ptr;
924 }
925 }
926 /* get and validate checksum character */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200927 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000928 if ((new_char & 0xE0) == 0)
929 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000930 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000931 goto packet_error;
932 /* get END_CHAR */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200933 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000934 if (new_char != END_CHAR) {
935 packet_error:
936 /* restore state machines */
937 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000938 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000939 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000940 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000941 } else if (k_state == SEND_TYPE) {
942 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000943 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000944 } else {
945 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000946 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000947 /* quit if end of transmission */
948 if (k_state == BREAK_TYPE)
949 done = 1;
950 }
wdenk8bde7f72003-06-27 21:31:46 +0000951 }
952 return ((ulong) os_data_addr - (ulong) bin_start_address);
953}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200954
955static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200956 if (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200957 return (getchar());
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200958 return -1;
959}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600960static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200961{
962 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200963 int err;
964 int res;
965 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200966 char ymodemBuf[1024];
967 ulong store_addr = ~0;
968 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200969
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200970 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600971 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000972 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200973 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200974
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200975 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000976 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200977 store_addr = addr + offset;
978 size += res;
979 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900980#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000981 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200982 int rc;
983
Kim Phillips088f1b12012-10-29 13:34:31 +0000984 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200985 store_addr, res);
986 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700987 flash_perror(rc);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200988 return (~0);
989 }
990 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200991#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200992 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000993 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200994 res);
995 }
996
997 }
998 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000999 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001000 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001001
Kim Phillips088f1b12012-10-29 13:34:31 +00001002 xyzModem_stream_close(&err);
1003 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001004
1005
Chris Packham0a6036d2016-10-25 20:22:48 +13001006 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001007
Kim Phillips088f1b12012-10-29 13:34:31 +00001008 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001009 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001010
1011 return offset;
1012}
1013
Jon Loeliger90253172007-07-10 11:02:44 -05001014#endif
wdenk8bde7f72003-06-27 21:31:46 +00001015
1016/* -------------------------------------------------------------------- */
1017
Jon Loeligerc76fe472007-07-08 18:02:23 -05001018#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001019
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001020#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001021U_BOOT_CMD(
1022 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001023 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001024 "[ off ] [ baud ]\n"
1025 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001026 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001027);
1028
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001029#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001030U_BOOT_CMD(
1031 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001032 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001033 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001034 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001035);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001036#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001037
1038/*
1039 * SAVES always requires LOADS support, but not vice versa
1040 */
1041
1042
Jon Loeligerc76fe472007-07-08 18:02:23 -05001043#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001044#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001045U_BOOT_CMD(
1046 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001047 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001048 "[ off ] [size] [ baud ]\n"
1049 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001050 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001051);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001052#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001053U_BOOT_CMD(
1054 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001055 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001056 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001057 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001058);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001059#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001060#endif /* CONFIG_CMD_SAVES */
1061#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001062
1063
Jon Loeligerc76fe472007-07-08 18:02:23 -05001064#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001065U_BOOT_CMD(
1066 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001067 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001068 "[ addr [ baud ] ]\n"
wdenk8bde7f72003-06-27 21:31:46 +00001069 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001070 " at address 'addr' with baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001071);
1072
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001073U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001074 loadx, 3, 0, do_load_serial_bin,
1075 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001076 "[ addr [ baud ] ]\n"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001077 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001078 " at address 'addr' with baudrate 'baud'"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001079);
1080
1081U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001082 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001083 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001084 "[ addr [ baud ] ]\n"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001085 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001086 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001087);
1088
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001089#endif /* CONFIG_CMD_LOADB */