blob: 36db571cea69758bb8506fcfe6c72ab58a2c54ae [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>
wdenk8bde7f72003-06-27 21:31:46 +000023
Wolfgang Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000025
Jon Loeligerc76fe472007-07-08 18:02:23 -050026#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060027static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020028#endif
29
Jon Loeligerc76fe472007-07-08 18:02:23 -050030#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000031static ulong load_serial(long offset);
32static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050033# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000034static int save_serial(ulong offset, ulong size);
35static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050036#endif
wdenk8bde7f72003-06-27 21:31:46 +000037
38static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050039#endif
wdenk8bde7f72003-06-27 21:31:46 +000040
41/* -------------------------------------------------------------------- */
42
Jon Loeligerc76fe472007-07-08 18:02:23 -050043#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000044static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
45 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000046{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020047 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000048 ulong addr;
49 int i;
50 char *env_echo;
51 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000053 int load_baudrate, current_baudrate;
54
55 load_baudrate = current_baudrate = gd->baudrate;
56#endif
57
Simon Glass00caae62017-08-03 12:22:12 -060058 env_echo = env_get("loads_echo");
59 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000060 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060061 else
wdenk8bde7f72003-06-27 21:31:46 +000062 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000063
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020064#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000065 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020066 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000067 }
68 if (argc == 3) {
69 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
70
71 /* default to current baudrate */
72 if (load_baudrate == 0)
73 load_baudrate = current_baudrate;
74 }
75 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000076 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000077 load_baudrate);
78 udelay(50000);
79 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000080 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000081 udelay(50000);
82 for (;;) {
83 if (getc() == '\r')
84 break;
85 }
86 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020087#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000088 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020089 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000090 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000092
Kim Phillips088f1b12012-10-29 13:34:31 +000093 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000094
Kim Phillips088f1b12012-10-29 13:34:31 +000095 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +000096
97 /*
98 * Gather any trailing characters (for instance, the ^D which
99 * is sent by 'cu' after sending a file), and give the
100 * box some time (100 * 1 ms)
101 */
102 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000103 if (tstc()) {
104 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000105 }
106 udelay(1000);
107 }
108
109 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000110 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000111 rcode = 1;
112 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000113 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700114 image_load_addr = addr;
wdenk8bde7f72003-06-27 21:31:46 +0000115 }
116
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200117#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000118 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000119 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000120 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000121 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000122 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000123 serial_setbrg();
124 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000125 for (;;) {
126 if (getc() == 0x1B) /* ESC */
127 break;
128 }
129 }
130#endif
131 return rcode;
132}
133
Kim Phillips088f1b12012-10-29 13:34:31 +0000134static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000135{
136 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
137 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
138 int binlen; /* no. of data bytes in S-Rec. */
139 int type; /* return code for record type */
140 ulong addr; /* load address from S-Record */
141 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000142 ulong store_addr;
143 ulong start_addr = ~0;
144 ulong end_addr = 0;
145 int line_count = 0;
146
147 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000148 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000149
150 if (type < 0) {
151 return (~0); /* Invalid S-Record */
152 }
153
154 switch (type) {
155 case SREC_DATA2:
156 case SREC_DATA3:
157 case SREC_DATA4:
158 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900159#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000160 if (addr2info(store_addr)) {
161 int rc;
162
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200163 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000164 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000165 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000166 return (~0);
167 }
168 } else
169#endif
170 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000171 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000172 }
173 if ((store_addr) < start_addr)
174 start_addr = store_addr;
175 if ((store_addr + binlen - 1) > end_addr)
176 end_addr = store_addr + binlen - 1;
177 break;
178 case SREC_END2:
179 case SREC_END3:
180 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000181 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000182 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000183 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000184 "## First Load Addr = 0x%08lX\n"
185 "## Last Load Addr = 0x%08lX\n"
186 "## Total Size = 0x%08lX = %ld Bytes\n",
187 start_addr, end_addr, size, size
188 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000189 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600190 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000191 return (addr);
192 case SREC_START:
193 break;
194 default:
195 break;
196 }
197 if (!do_echo) { /* print a '.' every 100 lines */
198 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000199 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000200 }
201 }
202
203 return (~0); /* Download aborted */
204}
205
Kim Phillips088f1b12012-10-29 13:34:31 +0000206static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000207{
208 char *p;
209 char c;
210
211 --len; /* always leave room for terminating '\0' byte */
212
213 for (p=buf; p < buf+len; ++p) {
wdenk232c1502004-03-12 00:14:09 +0000214 c = getc(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000215 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000216 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000217
218 switch (c) {
219 case '\r':
220 case '\n':
221 *p = '\0';
222 return (p - buf);
223 case '\0':
224 case 0x03: /* ^C - Control C */
225 return (-1);
226 default:
227 *p = c;
228 }
229
230 /* Check for the console hangup (if any different from serial) */
Martin Dorwig49cad542015-01-26 15:22:54 -0700231 if (gd->jt->getc != getc) {
wdenk8bde7f72003-06-27 21:31:46 +0000232 if (ctrlc()) {
233 return (-1);
234 }
235 }
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
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200245int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000246{
247 ulong offset = 0;
248 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200249#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000250 int save_baudrate, current_baudrate;
251
252 save_baudrate = current_baudrate = gd->baudrate;
253#endif
254
255 if (argc >= 2) {
256 offset = simple_strtoul(argv[1], NULL, 16);
257 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200258#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000259 if (argc >= 3) {
260 size = simple_strtoul(argv[2], NULL, 16);
261 }
262 if (argc == 4) {
263 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
264
265 /* default to current baudrate */
266 if (save_baudrate == 0)
267 save_baudrate = current_baudrate;
268 }
269 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000270 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000271 save_baudrate);
272 udelay(50000);
273 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000274 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000275 udelay(50000);
276 for (;;) {
277 if (getc() == '\r')
278 break;
279 }
280 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200281#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000282 if (argc == 3) {
283 size = simple_strtoul(argv[2], NULL, 16);
284 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200285#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000286
Kim Phillips088f1b12012-10-29 13:34:31 +0000287 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000288 for (;;) {
289 if (getc() == '\r')
290 break;
291 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000292 if (save_serial(offset, size)) {
293 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000294 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000295 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000296 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200297#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000298 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000299 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000300 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000301 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000302 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000303 serial_setbrg();
304 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000305 for (;;) {
306 if (getc() == 0x1B) /* ESC */
307 break;
308 }
309 }
310#endif
311 return 0;
312}
313
314#define SREC3_START "S0030000FC\n"
315#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
316#define SREC3_END "S70500000000FA\n"
317#define SREC_BYTES_PER_RECORD 16
318
Kim Phillips088f1b12012-10-29 13:34:31 +0000319static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000320{
321 int i, c, reclen, checksum, length;
322 char *hex = "0123456789ABCDEF";
323 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
324 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
325
326 reclen = 0;
327 checksum = 0;
328
329 if(write_record(SREC3_START)) /* write the header */
330 return (-1);
331 do {
332 if(count) { /* collect hex data in the buffer */
333 c = *(volatile uchar*)(address + reclen); /* get one byte */
334 checksum += c; /* accumulate checksum */
335 data[2*reclen] = hex[(c>>4)&0x0f];
336 data[2*reclen+1] = hex[c & 0x0f];
337 data[2*reclen+2] = '\0';
338 ++reclen;
339 --count;
340 }
341 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
342 /* enough data collected for one record: dump it */
343 if(reclen) { /* build & write a data record: */
344 /* address + data + checksum */
345 length = 4 + reclen + 1;
346
347 /* accumulate length bytes into checksum */
348 for(i = 0; i < 2; i++)
349 checksum += (length >> (8*i)) & 0xff;
350
351 /* accumulate address bytes into checksum: */
352 for(i = 0; i < 4; i++)
353 checksum += (address >> (8*i)) & 0xff;
354
355 /* make proper checksum byte: */
356 checksum = ~checksum & 0xff;
357
358 /* output one record: */
359 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
360 if(write_record(record))
361 return (-1);
362 }
363 address += reclen; /* increment address */
364 checksum = 0;
365 reclen = 0;
366 }
367 }
368 while(count);
369 if(write_record(SREC3_END)) /* write the final record */
370 return (-1);
371 return(0);
372}
373
Kim Phillips088f1b12012-10-29 13:34:31 +0000374static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000375{
376 char c;
377
378 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000379 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000380
381 /* Check for the console hangup (if any different from serial) */
382
383 if (ctrlc()) {
384 return (-1);
385 }
386 return (0);
387}
Jon Loeliger90253172007-07-10 11:02:44 -0500388# endif
wdenk8bde7f72003-06-27 21:31:46 +0000389
Jon Loeliger90253172007-07-10 11:02:44 -0500390#endif
wdenk8bde7f72003-06-27 21:31:46 +0000391
392
Jon Loeligerc76fe472007-07-08 18:02:23 -0500393#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500394/*
395 * loadb command (load binary) included
396 */
wdenk8bde7f72003-06-27 21:31:46 +0000397#define XON_CHAR 17
398#define XOFF_CHAR 19
399#define START_CHAR 0x01
400#define ETX_CHAR 0x03
401#define END_CHAR 0x0D
402#define SPACE 0x20
403#define K_ESCAPE 0x23
404#define SEND_TYPE 'S'
405#define DATA_TYPE 'D'
406#define ACK_TYPE 'Y'
407#define NACK_TYPE 'N'
408#define BREAK_TYPE 'B'
409#define tochar(x) ((char) (((x) + SPACE) & 0xff))
410#define untochar(x) ((int) (((x) - SPACE) & 0xff))
411
wdenk8bde7f72003-06-27 21:31:46 +0000412static void set_kerm_bin_mode(unsigned long *);
413static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000414static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000415
416
Kim Phillips088f1b12012-10-29 13:34:31 +0000417static char his_eol; /* character he needs at end of packet */
418static int his_pad_count; /* number of pad chars he needs */
419static char his_pad_char; /* pad chars he needs */
420static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000421
Kim Phillips088f1b12012-10-29 13:34:31 +0000422static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
423 char * const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000424{
wdenk8bde7f72003-06-27 21:31:46 +0000425 ulong offset = 0;
426 ulong addr;
427 int load_baudrate, current_baudrate;
428 int rcode = 0;
429 char *s;
430
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200431 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
432 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000433
434 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600435 s = env_get("loadaddr");
436 if (s)
wdenk8bde7f72003-06-27 21:31:46 +0000437 offset = simple_strtoul(s, NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +0000438
439 load_baudrate = current_baudrate = gd->baudrate;
440
441 if (argc >= 2) {
442 offset = simple_strtoul(argv[1], NULL, 16);
443 }
444 if (argc == 3) {
445 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
446
447 /* default to current baudrate */
448 if (load_baudrate == 0)
449 load_baudrate = current_baudrate;
450 }
451
452 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000453 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000454 load_baudrate);
455 udelay(50000);
456 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000457 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000458 udelay(50000);
459 for (;;) {
460 if (getc() == '\r')
461 break;
462 }
463 }
464
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200465 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000466 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200467 "to 0x%08lX at %d bps...\n",
468 offset,
469 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000470
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600471 addr = load_serial_ymodem(offset, xyzModem_ymodem);
472
473 } else if (strcmp(argv[0],"loadx")==0) {
474 printf("## Ready for binary (xmodem) download "
475 "to 0x%08lX at %d bps...\n",
476 offset,
477 load_baudrate);
478
479 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200480
wdenk8bde7f72003-06-27 21:31:46 +0000481 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000482
Kim Phillips088f1b12012-10-29 13:34:31 +0000483 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200484 "to 0x%08lX at %d bps...\n",
485 offset,
486 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000487 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200488
489 if (addr == ~0) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700490 image_load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000491 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200492 rcode = 1;
493 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000494 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700495 image_load_addr = addr;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200496 }
497 }
wdenk8bde7f72003-06-27 21:31:46 +0000498 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000499 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000500 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000501 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000502 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000503 serial_setbrg();
504 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000505 for (;;) {
506 if (getc() == 0x1B) /* ESC */
507 break;
508 }
509 }
510
wdenk8bde7f72003-06-27 21:31:46 +0000511 return rcode;
512}
513
514
Kim Phillips088f1b12012-10-29 13:34:31 +0000515static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000516{
517 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000518
Kim Phillips088f1b12012-10-29 13:34:31 +0000519 set_kerm_bin_mode((ulong *) offset);
520 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000521
522 /*
523 * Gather any trailing characters (for instance, the ^D which
524 * is sent by 'cu' after sending a file), and give the
525 * box some time (100 * 1 ms)
526 */
527 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000528 if (tstc()) {
529 (void) getc();
wdenk8bde7f72003-06-27 21:31:46 +0000530 }
531 udelay(1000);
532 }
533
Kim Phillips088f1b12012-10-29 13:34:31 +0000534 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000535
536 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600537 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000538
539 return offset;
540}
541
Kim Phillips088f1b12012-10-29 13:34:31 +0000542static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000543{
544 int count = his_pad_count;
545
546 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000547 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000548}
549
550/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000551static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000552{
553 if ((in & 0x60) == 0x40) {
554 return (char) (in & ~0x40);
555 } else if ((in & 0x7f) == 0x3f) {
556 return (char) (in | 0x40);
557 } else
558 return in;
559}
560
Kim Phillips088f1b12012-10-29 13:34:31 +0000561static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000562{
563 int total = 0;
564
565 while (*buffer) {
566 total += *buffer++;
567 }
568 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
569}
570
Kim Phillips088f1b12012-10-29 13:34:31 +0000571static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000572{
Kim Phillips088f1b12012-10-29 13:34:31 +0000573 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000574 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000575 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000576 }
577}
578
579static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000580static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000581{
582 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000583 a_b[1] = tochar(3);
584 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000585 a_b[3] = ACK_TYPE;
586 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000587 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000588 a_b[5] = his_eol;
589 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000590 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000591}
592
Kim Phillips088f1b12012-10-29 13:34:31 +0000593static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000594{
595 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000596 a_b[1] = tochar(3);
597 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000598 a_b[3] = NACK_TYPE;
599 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000600 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000601 a_b[5] = his_eol;
602 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000603 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000604}
605
606
Kim Phillips088f1b12012-10-29 13:34:31 +0000607static void (*os_data_init)(void);
608static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000609static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000610static char *os_data_addr, *os_data_addr_saved;
611static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530612
Kim Phillips088f1b12012-10-29 13:34:31 +0000613static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000614{
615 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000616 os_data_addr = bin_start_address;
617}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530618
Kim Phillips088f1b12012-10-29 13:34:31 +0000619static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000620{
621 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000622 os_data_addr_saved = os_data_addr;
623}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530624
Kim Phillips088f1b12012-10-29 13:34:31 +0000625static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000626{
627 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000628 os_data_addr = os_data_addr_saved;
629}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530630
Kim Phillips088f1b12012-10-29 13:34:31 +0000631static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000632{
633 switch (os_data_state) {
634 case 0: /* data */
635 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000636 break;
637 }
638}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530639
Kim Phillips088f1b12012-10-29 13:34:31 +0000640static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000641{
642 bin_start_address = (char *) addr;
643 os_data_init = bin_data_init;
644 os_data_char = bin_data_char;
645}
646
647
648/* k_data_* simply handles the kermit escape translations */
649static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000650static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000651{
652 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000653 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000654}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530655
Kim Phillips088f1b12012-10-29 13:34:31 +0000656static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000657{
658 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000659 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000660}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530661
Kim Phillips088f1b12012-10-29 13:34:31 +0000662static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000663{
664 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000665 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000666}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530667
Kim Phillips088f1b12012-10-29 13:34:31 +0000668static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000669{
670 if (k_data_escape) {
671 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000672 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000673 k_data_escape = 0;
674 } else {
675 if (new_char == his_quote) {
676 /* this char is escape - remember */
677 k_data_escape = 1;
678 } else {
679 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000680 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000681 }
682 }
683}
684
685#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000686static char send_parms[SEND_DATA_SIZE];
687static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000688
689/* handle_send_packet interprits the protocol info and builds and
690 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000691static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000692{
693 int length = 3;
694 int bytes;
695
696 /* initialize some protocol parameters */
697 his_eol = END_CHAR; /* default end of line character */
698 his_pad_count = 0;
699 his_pad_char = '\0';
700 his_quote = K_ESCAPE;
701
702 /* ignore last character if it filled the buffer */
703 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
704 --send_ptr;
705 bytes = send_ptr - send_parms; /* how many bytes we'll process */
706 do {
707 if (bytes-- <= 0)
708 break;
709 /* handle MAXL - max length */
710 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000711 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000712 if (bytes-- <= 0)
713 break;
714 /* handle TIME - time you should wait for my packets */
715 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000716 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000717 if (bytes-- <= 0)
718 break;
719 /* handle NPAD - number of pad chars I need */
720 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000721 his_pad_count = untochar(send_parms[2]);
722 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000723 if (bytes-- <= 0)
724 break;
725 /* handle PADC - pad chars I need */
726 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000727 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000728 a_b[++length] = 0x40; /* He should ignore this */
729 if (bytes-- <= 0)
730 break;
731 /* handle EOL - end of line he needs */
732 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000733 his_eol = untochar(send_parms[4]);
734 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000735 if (bytes-- <= 0)
736 break;
737 /* handle QCTL - quote control char he'll use */
738 /* remember what he says - I'll use '#' */
739 his_quote = send_parms[5];
740 a_b[++length] = '#';
741 if (bytes-- <= 0)
742 break;
743 /* handle QBIN - 8-th bit prefixing */
744 /* ignore what he says - I refuse */
745 a_b[++length] = 'N';
746 if (bytes-- <= 0)
747 break;
748 /* handle CHKT - the clock check type */
749 /* ignore what he says - I do type 1 (for now) */
750 a_b[++length] = '1';
751 if (bytes-- <= 0)
752 break;
753 /* handle REPT - the repeat prefix */
754 /* ignore what he says - I refuse (for now) */
755 a_b[++length] = 'N';
756 if (bytes-- <= 0)
757 break;
758 /* handle CAPAS - the capabilities mask */
759 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000760 a_b[++length] = tochar(2); /* only long packets */
761 a_b[++length] = tochar(0); /* no windows */
762 a_b[++length] = tochar(94); /* large packet msb */
763 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000764 } while (0);
765
766 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000767 a_b[1] = tochar(length);
768 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000769 a_b[3] = ACK_TYPE;
770 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000771 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000772 a_b[++length] = his_eol;
773 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000774 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000775}
776
777/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000778static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000779{
780 char new_char;
781 char k_state, k_state_saved;
782 int sum;
783 int done;
784 int length;
785 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000786 int len_lo, len_hi;
787
788 /* initialize some protocol parameters */
789 his_eol = END_CHAR; /* default end of line character */
790 his_pad_count = 0;
791 his_pad_char = '\0';
792 his_quote = K_ESCAPE;
793
794 /* initialize the k_recv and k_data state machine */
795 done = 0;
796 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000797 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000798 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000799 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000800 n = 0; /* just to get rid of a warning */
801 last_n = -1;
802
803 /* expect this "type" sequence (but don't check):
804 S: send initiate
805 F: file header
806 D: data (multiple)
807 Z: end of file
808 B: break transmission
809 */
810
811 /* enter main loop */
812 while (!done) {
813 /* set the send packet pointer to begining of send packet parms */
814 send_ptr = send_parms;
815
816 /* With each packet, start summing the bytes starting with the length.
817 Save the current sequence number.
818 Note the type of the packet.
819 If a character less than SPACE (0x20) is received - error.
820 */
821
822#if 0
823 /* OLD CODE, Prior to checking sequence numbers */
824 /* first have all state machines save current states */
825 k_state_saved = k_state;
826 k_data_save ();
827#endif
828
829 /* get a packet */
830 /* wait for the starting character or ^C */
831 for (;;) {
wdenk232c1502004-03-12 00:14:09 +0000832 switch (getc ()) {
wdenk8bde7f72003-06-27 21:31:46 +0000833 case START_CHAR: /* start packet */
834 goto START;
835 case ETX_CHAR: /* ^C waiting for packet */
836 return (0);
837 default:
838 ;
839 }
840 }
841START:
842 /* get length of packet */
843 sum = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000844 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000845 if ((new_char & 0xE0) == 0)
846 goto packet_error;
847 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000848 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000849 /* get sequence number */
Kim Phillips088f1b12012-10-29 13:34:31 +0000850 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000851 if ((new_char & 0xE0) == 0)
852 goto packet_error;
853 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000854 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000855 --length;
856
857 /* NEW CODE - check sequence numbers for retried packets */
858 /* Note - this new code assumes that the sequence number is correctly
859 * received. Handling an invalid sequence number adds another layer
860 * of complexity that may not be needed - yet! At this time, I'm hoping
861 * that I don't need to buffer the incoming data packets and can write
862 * the data into memory in real time.
863 */
864 if (n == last_n) {
865 /* same sequence number, restore the previous state */
866 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000867 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000868 } else {
869 /* new sequence number, checkpoint the download */
870 last_n = n;
871 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000872 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000873 }
874 /* END NEW CODE */
875
876 /* get packet type */
Kim Phillips088f1b12012-10-29 13:34:31 +0000877 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000878 if ((new_char & 0xE0) == 0)
879 goto packet_error;
880 sum += new_char & 0xff;
881 k_state = new_char;
882 --length;
883 /* check for extended length */
884 if (length == -2) {
885 /* (length byte was 0, decremented twice) */
886 /* get the two length bytes */
Kim Phillips088f1b12012-10-29 13:34:31 +0000887 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000888 if ((new_char & 0xE0) == 0)
889 goto packet_error;
890 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000891 len_hi = untochar(new_char);
892 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000893 if ((new_char & 0xE0) == 0)
894 goto packet_error;
895 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000896 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000897 length = len_hi * 95 + len_lo;
898 /* check header checksum */
Kim Phillips088f1b12012-10-29 13:34:31 +0000899 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000900 if ((new_char & 0xE0) == 0)
901 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000902 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000903 goto packet_error;
904 sum += new_char & 0xff;
905/* --length; */ /* new length includes only data and block check to come */
906 }
907 /* bring in rest of packet */
908 while (length > 1) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000909 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000910 if ((new_char & 0xE0) == 0)
911 goto packet_error;
912 sum += new_char & 0xff;
913 --length;
914 if (k_state == DATA_TYPE) {
915 /* pass on the data if this is a data packet */
916 k_data_char (new_char);
917 } else if (k_state == SEND_TYPE) {
918 /* save send pack in buffer as is */
919 *send_ptr++ = new_char;
920 /* if too much data, back off the pointer */
921 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
922 --send_ptr;
923 }
924 }
925 /* get and validate checksum character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000926 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000927 if ((new_char & 0xE0) == 0)
928 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000929 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000930 goto packet_error;
931 /* get END_CHAR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000932 new_char = getc();
wdenk8bde7f72003-06-27 21:31:46 +0000933 if (new_char != END_CHAR) {
934 packet_error:
935 /* restore state machines */
936 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000937 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000938 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000939 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000940 } else if (k_state == SEND_TYPE) {
941 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000942 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000943 } else {
944 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000945 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000946 /* quit if end of transmission */
947 if (k_state == BREAK_TYPE)
948 done = 1;
949 }
wdenk8bde7f72003-06-27 21:31:46 +0000950 }
951 return ((ulong) os_data_addr - (ulong) bin_start_address);
952}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200953
954static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200955 if (tstc())
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200956 return (getc());
957 return -1;
958}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600959static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200960{
961 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200962 int err;
963 int res;
964 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200965 char ymodemBuf[1024];
966 ulong store_addr = ~0;
967 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200968
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200969 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600970 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000971 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200972 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200973
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200974 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000975 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200976 store_addr = addr + offset;
977 size += res;
978 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900979#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000980 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200981 int rc;
982
Kim Phillips088f1b12012-10-29 13:34:31 +0000983 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200984 store_addr, res);
985 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700986 flash_perror(rc);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200987 return (~0);
988 }
989 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200990#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200991 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000992 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200993 res);
994 }
995
996 }
997 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000998 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200999 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001000
Kim Phillips088f1b12012-10-29 13:34:31 +00001001 xyzModem_stream_close(&err);
1002 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001003
1004
Chris Packham0a6036d2016-10-25 20:22:48 +13001005 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001006
Kim Phillips088f1b12012-10-29 13:34:31 +00001007 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001008 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001009
1010 return offset;
1011}
1012
Jon Loeliger90253172007-07-10 11:02:44 -05001013#endif
wdenk8bde7f72003-06-27 21:31:46 +00001014
1015/* -------------------------------------------------------------------- */
1016
Jon Loeligerc76fe472007-07-08 18:02:23 -05001017#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001018
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001019#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001020U_BOOT_CMD(
1021 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001022 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001023 "[ off ] [ baud ]\n"
1024 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001025 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001026);
1027
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001028#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001029U_BOOT_CMD(
1030 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001031 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001032 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001033 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001034);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001035#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001036
1037/*
1038 * SAVES always requires LOADS support, but not vice versa
1039 */
1040
1041
Jon Loeligerc76fe472007-07-08 18:02:23 -05001042#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001043#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001044U_BOOT_CMD(
1045 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001046 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001047 "[ off ] [size] [ baud ]\n"
1048 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001049 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001050);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001051#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001052U_BOOT_CMD(
1053 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001054 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001055 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001056 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001057);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001058#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001059#endif /* CONFIG_CMD_SAVES */
1060#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001061
1062
Jon Loeligerc76fe472007-07-08 18:02:23 -05001063#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001064U_BOOT_CMD(
1065 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001066 "load binary file over serial line (kermit mode)",
wdenk8bde7f72003-06-27 21:31:46 +00001067 "[ off ] [ baud ]\n"
1068 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001069 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001070);
1071
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001072U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001073 loadx, 3, 0, do_load_serial_bin,
1074 "load binary file over serial line (xmodem mode)",
1075 "[ off ] [ baud ]\n"
1076 " - load binary file over serial line"
1077 " with offset 'off' and baudrate 'baud'"
1078);
1079
1080U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001081 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001082 "load binary file over serial line (ymodem mode)",
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001083 "[ off ] [ baud ]\n"
1084 " - load binary file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001085 " with offset 'off' and baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001086);
1087
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001088#endif /* CONFIG_CMD_LOADB */