blob: ace1c52f90added1f7906cad4acf2369d7893457 [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 */
wdenk8bde7f72003-06-27 21:31:46 +000010#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070011#include <console.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070012#include <cpu_func.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000013#include <efi_loader.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060014#include <env.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000015#include <exports.h>
Tom Rini17ead042022-07-23 13:05:03 -040016#ifdef CONFIG_MTD_NOR_FLASH
Simon Glass0ee482522019-12-28 10:44:40 -070017#include <flash.h>
Tom Rini17ead042022-07-23 13:05:03 -040018#endif
Simon Glass8e8ccfe2019-12-28 10:45:03 -070019#include <image.h>
Marek Vasutc6855192021-10-10 23:52:41 +020020#include <lmb.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000021#include <mapmem.h>
wdenk8bde7f72003-06-27 21:31:46 +000022#include <net.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000023#include <s_record.h>
Simon Glassb03e0512019-11-14 12:57:24 -070024#include <serial.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020025#include <xyzModem.h>
Simon Glass90526e92020-05-10 11:39:56 -060026#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060027#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060028#include <linux/delay.h>
wdenk8bde7f72003-06-27 21:31:46 +000029
Wolfgang Denkd87080b2006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000031
Jon Loeligerc76fe472007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060033static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020034#endif
35
Jon Loeligerc76fe472007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000037static ulong load_serial(long offset);
38static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050039# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000040static int save_serial(ulong offset, ulong size);
41static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050042#endif
wdenk8bde7f72003-06-27 21:31:46 +000043
44static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050045#endif
wdenk8bde7f72003-06-27 21:31:46 +000046
47/* -------------------------------------------------------------------- */
48
Jon Loeligerc76fe472007-07-08 18:02:23 -050049#if defined(CONFIG_CMD_LOADS)
Simon Glass09140112020-05-10 11:40:03 -060050static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000052{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020053 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000054 ulong addr;
55 int i;
56 char *env_echo;
57 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000059 int load_baudrate, current_baudrate;
60
61 load_baudrate = current_baudrate = gd->baudrate;
62#endif
63
Simon Glass00caae62017-08-03 12:22:12 -060064 env_echo = env_get("loads_echo");
65 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000066 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060067 else
wdenk8bde7f72003-06-27 21:31:46 +000068 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000069
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020070#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000071 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020072 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000073 }
74 if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -060075 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +000076
77 /* default to current baudrate */
78 if (load_baudrate == 0)
79 load_baudrate = current_baudrate;
80 }
81 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000082 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000083 load_baudrate);
84 udelay(50000);
Pali Rohár989cc402022-09-05 11:31:20 +020085 flush();
wdenk8bde7f72003-06-27 21:31:46 +000086 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000087 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000088 udelay(50000);
89 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020090 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +000091 break;
92 }
93 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020094#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000095 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020096 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000097 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020098#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000099
Kim Phillips088f1b12012-10-29 13:34:31 +0000100 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000101
Kim Phillips088f1b12012-10-29 13:34:31 +0000102 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +0000103
104 /*
105 * Gather any trailing characters (for instance, the ^D which
106 * is sent by 'cu' after sending a file), and give the
107 * box some time (100 * 1 ms)
108 */
109 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000110 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200111 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000112 }
113 udelay(1000);
114 }
115
116 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000117 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000118 rcode = 1;
119 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000120 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700121 image_load_addr = addr;
wdenk8bde7f72003-06-27 21:31:46 +0000122 }
123
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200124#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000125 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000126 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000127 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000128 udelay(50000);
Pali Rohár989cc402022-09-05 11:31:20 +0200129 flush();
wdenk8bde7f72003-06-27 21:31:46 +0000130 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000131 serial_setbrg();
132 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000133 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200134 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000135 break;
136 }
137 }
138#endif
139 return rcode;
140}
141
Kim Phillips088f1b12012-10-29 13:34:31 +0000142static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000143{
Marek Vasutc6855192021-10-10 23:52:41 +0200144 struct lmb lmb;
wdenk8bde7f72003-06-27 21:31:46 +0000145 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
146 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
147 int binlen; /* no. of data bytes in S-Rec. */
148 int type; /* return code for record type */
149 ulong addr; /* load address from S-Record */
150 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000151 ulong store_addr;
152 ulong start_addr = ~0;
153 ulong end_addr = 0;
154 int line_count = 0;
Marek Vasutc6855192021-10-10 23:52:41 +0200155 long ret;
156
157 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
wdenk8bde7f72003-06-27 21:31:46 +0000158
159 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000160 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000161
162 if (type < 0) {
163 return (~0); /* Invalid S-Record */
164 }
165
166 switch (type) {
167 case SREC_DATA2:
168 case SREC_DATA3:
169 case SREC_DATA4:
170 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900171#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000172 if (addr2info(store_addr)) {
173 int rc;
174
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200175 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000176 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000177 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000178 return (~0);
179 }
180 } else
181#endif
182 {
Heinrich Schuchardt338b67f2023-06-25 11:54:23 +0200183 void *dst;
184
Marek Vasutc6855192021-10-10 23:52:41 +0200185 ret = lmb_reserve(&lmb, store_addr, binlen);
186 if (ret) {
187 printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
188 store_addr, store_addr + binlen);
189 return ret;
190 }
Heinrich Schuchardt338b67f2023-06-25 11:54:23 +0200191 dst = map_sysmem(store_addr, binlen);
192 memcpy(dst, binbuf, binlen);
193 unmap_sysmem(dst);
Marek Vasutc6855192021-10-10 23:52:41 +0200194 lmb_free(&lmb, store_addr, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000195 }
196 if ((store_addr) < start_addr)
197 start_addr = store_addr;
198 if ((store_addr + binlen - 1) > end_addr)
199 end_addr = store_addr + binlen - 1;
200 break;
201 case SREC_END2:
202 case SREC_END3:
203 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000204 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000205 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000206 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000207 "## First Load Addr = 0x%08lX\n"
208 "## Last Load Addr = 0x%08lX\n"
209 "## Total Size = 0x%08lX = %ld Bytes\n",
210 start_addr, end_addr, size, size
211 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000212 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600213 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000214 return (addr);
215 case SREC_START:
216 break;
217 default:
218 break;
219 }
220 if (!do_echo) { /* print a '.' every 100 lines */
221 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000222 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000223 }
224 }
225
226 return (~0); /* Download aborted */
227}
228
Kim Phillips088f1b12012-10-29 13:34:31 +0000229static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000230{
231 char *p;
Tom Rini21a2c122024-01-09 17:57:16 -0500232 int c;
wdenk8bde7f72003-06-27 21:31:46 +0000233
234 --len; /* always leave room for terminating '\0' byte */
235
236 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200237 c = getchar(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000238 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000239 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000240
241 switch (c) {
242 case '\r':
243 case '\n':
244 *p = '\0';
245 return (p - buf);
246 case '\0':
247 case 0x03: /* ^C - Control C */
248 return (-1);
249 default:
250 *p = c;
251 }
252
Michal Simeke7fcfef2020-12-01 13:58:28 +0100253 /* Check for the console hangup (if any different from serial) */
254 if (gd->jt->getc != getchar) {
255 if (ctrlc())
256 return (-1);
wdenk8bde7f72003-06-27 21:31:46 +0000257 }
wdenk8bde7f72003-06-27 21:31:46 +0000258 }
259
260 /* line too long - truncate */
261 *p = '\0';
262 return (p - buf);
263}
264
Jon Loeligerc76fe472007-07-08 18:02:23 -0500265#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000266
Simon Glass09140112020-05-10 11:40:03 -0600267int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
268 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000269{
270 ulong offset = 0;
271 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200272#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000273 int save_baudrate, current_baudrate;
274
275 save_baudrate = current_baudrate = gd->baudrate;
276#endif
277
278 if (argc >= 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600279 offset = hextoul(argv[1], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000280 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200281#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000282 if (argc >= 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600283 size = hextoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000284 }
285 if (argc == 4) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600286 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000287
288 /* default to current baudrate */
289 if (save_baudrate == 0)
290 save_baudrate = current_baudrate;
291 }
292 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000293 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000294 save_baudrate);
295 udelay(50000);
296 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000297 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000298 udelay(50000);
299 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200300 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000301 break;
302 }
303 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200304#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000305 if (argc == 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600306 size = hextoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000307 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200308#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000309
Kim Phillips088f1b12012-10-29 13:34:31 +0000310 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000311 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200312 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000313 break;
314 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000315 if (save_serial(offset, size)) {
316 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000317 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000318 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000319 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200320#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000321 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000322 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000323 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000324 udelay(50000);
Pali Rohár989cc402022-09-05 11:31:20 +0200325 flush();
wdenk8bde7f72003-06-27 21:31:46 +0000326 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000327 serial_setbrg();
328 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000329 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200330 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000331 break;
332 }
333 }
334#endif
335 return 0;
336}
337
338#define SREC3_START "S0030000FC\n"
339#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
340#define SREC3_END "S70500000000FA\n"
341#define SREC_BYTES_PER_RECORD 16
342
Kim Phillips088f1b12012-10-29 13:34:31 +0000343static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000344{
345 int i, c, reclen, checksum, length;
346 char *hex = "0123456789ABCDEF";
347 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
348 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
349
350 reclen = 0;
351 checksum = 0;
352
353 if(write_record(SREC3_START)) /* write the header */
354 return (-1);
355 do {
Heinrich Schuchardt338b67f2023-06-25 11:54:23 +0200356 volatile uchar *src;
357
358 src = map_sysmem(address, count);
359 if (count) { /* collect hex data in the buffer */
360 c = src[reclen]; /* get one byte */
361 checksum += c; /* accumulate checksum */
wdenk8bde7f72003-06-27 21:31:46 +0000362 data[2*reclen] = hex[(c>>4)&0x0f];
363 data[2*reclen+1] = hex[c & 0x0f];
364 data[2*reclen+2] = '\0';
365 ++reclen;
366 --count;
367 }
Heinrich Schuchardt338b67f2023-06-25 11:54:23 +0200368 unmap_sysmem((void *)src);
wdenk8bde7f72003-06-27 21:31:46 +0000369 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
370 /* enough data collected for one record: dump it */
371 if(reclen) { /* build & write a data record: */
372 /* address + data + checksum */
373 length = 4 + reclen + 1;
374
375 /* accumulate length bytes into checksum */
376 for(i = 0; i < 2; i++)
377 checksum += (length >> (8*i)) & 0xff;
378
379 /* accumulate address bytes into checksum: */
380 for(i = 0; i < 4; i++)
381 checksum += (address >> (8*i)) & 0xff;
382
383 /* make proper checksum byte: */
384 checksum = ~checksum & 0xff;
385
386 /* output one record: */
387 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
388 if(write_record(record))
389 return (-1);
390 }
391 address += reclen; /* increment address */
392 checksum = 0;
393 reclen = 0;
394 }
395 }
396 while(count);
397 if(write_record(SREC3_END)) /* write the final record */
398 return (-1);
399 return(0);
400}
401
Kim Phillips088f1b12012-10-29 13:34:31 +0000402static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000403{
404 char c;
405
406 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000407 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000408
409 /* Check for the console hangup (if any different from serial) */
410
411 if (ctrlc()) {
412 return (-1);
413 }
414 return (0);
415}
Jon Loeliger90253172007-07-10 11:02:44 -0500416# endif
wdenk8bde7f72003-06-27 21:31:46 +0000417
Jon Loeliger90253172007-07-10 11:02:44 -0500418#endif
wdenk8bde7f72003-06-27 21:31:46 +0000419
420
Jon Loeligerc76fe472007-07-08 18:02:23 -0500421#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500422/*
423 * loadb command (load binary) included
424 */
wdenk8bde7f72003-06-27 21:31:46 +0000425#define XON_CHAR 17
426#define XOFF_CHAR 19
427#define START_CHAR 0x01
428#define ETX_CHAR 0x03
429#define END_CHAR 0x0D
430#define SPACE 0x20
431#define K_ESCAPE 0x23
432#define SEND_TYPE 'S'
433#define DATA_TYPE 'D'
434#define ACK_TYPE 'Y'
435#define NACK_TYPE 'N'
436#define BREAK_TYPE 'B'
437#define tochar(x) ((char) (((x) + SPACE) & 0xff))
438#define untochar(x) ((int) (((x) - SPACE) & 0xff))
439
wdenk8bde7f72003-06-27 21:31:46 +0000440static void set_kerm_bin_mode(unsigned long *);
441static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000442static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000443
444
Kim Phillips088f1b12012-10-29 13:34:31 +0000445static char his_eol; /* character he needs at end of packet */
446static int his_pad_count; /* number of pad chars he needs */
447static char his_pad_char; /* pad chars he needs */
448static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000449
Simon Glass09140112020-05-10 11:40:03 -0600450static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
451 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000452{
wdenk8bde7f72003-06-27 21:31:46 +0000453 ulong offset = 0;
454 ulong addr;
455 int load_baudrate, current_baudrate;
456 int rcode = 0;
457 char *s;
458
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200459 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
460 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000461
462 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600463 s = env_get("loadaddr");
464 if (s)
Simon Glass7e5f4602021-07-24 09:03:29 -0600465 offset = hextoul(s, NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000466
467 load_baudrate = current_baudrate = gd->baudrate;
468
469 if (argc >= 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600470 offset = hextoul(argv[1], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000471 }
472 if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600473 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000474
475 /* default to current baudrate */
476 if (load_baudrate == 0)
477 load_baudrate = current_baudrate;
478 }
479
480 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000481 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000482 load_baudrate);
483 udelay(50000);
Pali Rohár989cc402022-09-05 11:31:20 +0200484 flush();
wdenk8bde7f72003-06-27 21:31:46 +0000485 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000486 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000487 udelay(50000);
488 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200489 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000490 break;
491 }
492 }
493
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200494 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000495 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200496 "to 0x%08lX at %d bps...\n",
497 offset,
498 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000499
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600500 addr = load_serial_ymodem(offset, xyzModem_ymodem);
501
Pali Rohárc97b2552021-08-03 16:28:43 +0200502 if (addr == ~0) {
503 image_load_addr = 0;
504 printf("## Binary (ymodem) download aborted\n");
505 rcode = 1;
506 } else {
507 printf("## Start Addr = 0x%08lX\n", addr);
508 image_load_addr = addr;
509 }
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600510 } else if (strcmp(argv[0],"loadx")==0) {
511 printf("## Ready for binary (xmodem) download "
512 "to 0x%08lX at %d bps...\n",
513 offset,
514 load_baudrate);
515
516 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200517
Pali Rohárc97b2552021-08-03 16:28:43 +0200518 if (addr == ~0) {
519 image_load_addr = 0;
520 printf("## Binary (xmodem) download aborted\n");
521 rcode = 1;
522 } else {
523 printf("## Start Addr = 0x%08lX\n", addr);
524 image_load_addr = addr;
525 }
wdenk8bde7f72003-06-27 21:31:46 +0000526 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000527
Kim Phillips088f1b12012-10-29 13:34:31 +0000528 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200529 "to 0x%08lX at %d bps...\n",
530 offset,
531 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000532 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200533
534 if (addr == ~0) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700535 image_load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000536 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200537 rcode = 1;
538 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000539 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700540 image_load_addr = addr;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200541 }
542 }
wdenk8bde7f72003-06-27 21:31:46 +0000543 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000544 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000545 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000546 udelay(50000);
Pali Rohár989cc402022-09-05 11:31:20 +0200547 flush();
wdenk8bde7f72003-06-27 21:31:46 +0000548 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000549 serial_setbrg();
550 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000551 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200552 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000553 break;
554 }
555 }
556
wdenk8bde7f72003-06-27 21:31:46 +0000557 return rcode;
558}
559
560
Kim Phillips088f1b12012-10-29 13:34:31 +0000561static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000562{
563 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000564
Kim Phillips088f1b12012-10-29 13:34:31 +0000565 set_kerm_bin_mode((ulong *) offset);
566 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000567
568 /*
569 * Gather any trailing characters (for instance, the ^D which
570 * is sent by 'cu' after sending a file), and give the
571 * box some time (100 * 1 ms)
572 */
573 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000574 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200575 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000576 }
577 udelay(1000);
578 }
579
Pali Rohár51538142021-08-06 18:07:39 +0200580 if (size == 0)
581 return ~0; /* Download aborted */
582
Kim Phillips088f1b12012-10-29 13:34:31 +0000583 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000584
585 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600586 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000587
588 return offset;
589}
590
Kim Phillips088f1b12012-10-29 13:34:31 +0000591static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000592{
593 int count = his_pad_count;
594
595 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000596 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000597}
598
599/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000600static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000601{
602 if ((in & 0x60) == 0x40) {
603 return (char) (in & ~0x40);
604 } else if ((in & 0x7f) == 0x3f) {
605 return (char) (in | 0x40);
606 } else
607 return in;
608}
609
Kim Phillips088f1b12012-10-29 13:34:31 +0000610static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000611{
612 int total = 0;
613
614 while (*buffer) {
615 total += *buffer++;
616 }
617 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
618}
619
Kim Phillips088f1b12012-10-29 13:34:31 +0000620static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000621{
Kim Phillips088f1b12012-10-29 13:34:31 +0000622 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000623 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000624 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000625 }
626}
627
628static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000629static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000630{
631 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000632 a_b[1] = tochar(3);
633 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000634 a_b[3] = ACK_TYPE;
635 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000636 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000637 a_b[5] = his_eol;
638 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000639 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000640}
641
Kim Phillips088f1b12012-10-29 13:34:31 +0000642static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000643{
644 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000645 a_b[1] = tochar(3);
646 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000647 a_b[3] = NACK_TYPE;
648 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000649 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000650 a_b[5] = his_eol;
651 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000652 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000653}
654
655
Kim Phillips088f1b12012-10-29 13:34:31 +0000656static void (*os_data_init)(void);
657static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000658static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000659static char *os_data_addr, *os_data_addr_saved;
660static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530661
Kim Phillips088f1b12012-10-29 13:34:31 +0000662static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000663{
664 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000665 os_data_addr = bin_start_address;
666}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530667
Kim Phillips088f1b12012-10-29 13:34:31 +0000668static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000669{
670 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000671 os_data_addr_saved = os_data_addr;
672}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530673
Kim Phillips088f1b12012-10-29 13:34:31 +0000674static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000675{
676 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000677 os_data_addr = os_data_addr_saved;
678}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530679
Kim Phillips088f1b12012-10-29 13:34:31 +0000680static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000681{
682 switch (os_data_state) {
683 case 0: /* data */
684 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000685 break;
686 }
687}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530688
Kim Phillips088f1b12012-10-29 13:34:31 +0000689static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000690{
691 bin_start_address = (char *) addr;
692 os_data_init = bin_data_init;
693 os_data_char = bin_data_char;
694}
695
696
697/* k_data_* simply handles the kermit escape translations */
698static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000699static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000700{
701 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000702 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000703}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530704
Kim Phillips088f1b12012-10-29 13:34:31 +0000705static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000706{
707 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000708 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000709}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530710
Kim Phillips088f1b12012-10-29 13:34:31 +0000711static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000712{
713 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000714 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000715}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530716
Kim Phillips088f1b12012-10-29 13:34:31 +0000717static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000718{
719 if (k_data_escape) {
720 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000721 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000722 k_data_escape = 0;
723 } else {
724 if (new_char == his_quote) {
725 /* this char is escape - remember */
726 k_data_escape = 1;
727 } else {
728 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000729 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000730 }
731 }
732}
733
734#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000735static char send_parms[SEND_DATA_SIZE];
736static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000737
738/* handle_send_packet interprits the protocol info and builds and
739 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000740static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000741{
742 int length = 3;
743 int bytes;
744
745 /* initialize some protocol parameters */
746 his_eol = END_CHAR; /* default end of line character */
747 his_pad_count = 0;
748 his_pad_char = '\0';
749 his_quote = K_ESCAPE;
750
751 /* ignore last character if it filled the buffer */
752 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
753 --send_ptr;
754 bytes = send_ptr - send_parms; /* how many bytes we'll process */
755 do {
756 if (bytes-- <= 0)
757 break;
758 /* handle MAXL - max length */
759 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000760 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000761 if (bytes-- <= 0)
762 break;
763 /* handle TIME - time you should wait for my packets */
764 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000765 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000766 if (bytes-- <= 0)
767 break;
768 /* handle NPAD - number of pad chars I need */
769 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000770 his_pad_count = untochar(send_parms[2]);
771 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000772 if (bytes-- <= 0)
773 break;
774 /* handle PADC - pad chars I need */
775 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000776 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000777 a_b[++length] = 0x40; /* He should ignore this */
778 if (bytes-- <= 0)
779 break;
780 /* handle EOL - end of line he needs */
781 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000782 his_eol = untochar(send_parms[4]);
783 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000784 if (bytes-- <= 0)
785 break;
786 /* handle QCTL - quote control char he'll use */
787 /* remember what he says - I'll use '#' */
788 his_quote = send_parms[5];
789 a_b[++length] = '#';
790 if (bytes-- <= 0)
791 break;
792 /* handle QBIN - 8-th bit prefixing */
793 /* ignore what he says - I refuse */
794 a_b[++length] = 'N';
795 if (bytes-- <= 0)
796 break;
797 /* handle CHKT - the clock check type */
798 /* ignore what he says - I do type 1 (for now) */
799 a_b[++length] = '1';
800 if (bytes-- <= 0)
801 break;
802 /* handle REPT - the repeat prefix */
803 /* ignore what he says - I refuse (for now) */
804 a_b[++length] = 'N';
805 if (bytes-- <= 0)
806 break;
807 /* handle CAPAS - the capabilities mask */
808 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000809 a_b[++length] = tochar(2); /* only long packets */
810 a_b[++length] = tochar(0); /* no windows */
811 a_b[++length] = tochar(94); /* large packet msb */
812 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000813 } while (0);
814
815 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000816 a_b[1] = tochar(length);
817 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000818 a_b[3] = ACK_TYPE;
819 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000820 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000821 a_b[++length] = his_eol;
822 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000823 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000824}
825
826/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000827static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000828{
Tom Rini21a2c122024-01-09 17:57:16 -0500829 int new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000830 char k_state, k_state_saved;
831 int sum;
832 int done;
833 int length;
834 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000835 int len_lo, len_hi;
836
837 /* initialize some protocol parameters */
838 his_eol = END_CHAR; /* default end of line character */
839 his_pad_count = 0;
840 his_pad_char = '\0';
841 his_quote = K_ESCAPE;
842
843 /* initialize the k_recv and k_data state machine */
844 done = 0;
845 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000846 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000847 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000848 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000849 n = 0; /* just to get rid of a warning */
850 last_n = -1;
851
852 /* expect this "type" sequence (but don't check):
853 S: send initiate
854 F: file header
855 D: data (multiple)
856 Z: end of file
857 B: break transmission
858 */
859
860 /* enter main loop */
861 while (!done) {
862 /* set the send packet pointer to begining of send packet parms */
863 send_ptr = send_parms;
864
865 /* With each packet, start summing the bytes starting with the length.
866 Save the current sequence number.
867 Note the type of the packet.
868 If a character less than SPACE (0x20) is received - error.
869 */
870
871#if 0
872 /* OLD CODE, Prior to checking sequence numbers */
873 /* first have all state machines save current states */
874 k_state_saved = k_state;
875 k_data_save ();
876#endif
877
878 /* get a packet */
879 /* wait for the starting character or ^C */
880 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200881 switch (getchar()) {
wdenk8bde7f72003-06-27 21:31:46 +0000882 case START_CHAR: /* start packet */
883 goto START;
884 case ETX_CHAR: /* ^C waiting for packet */
885 return (0);
886 default:
887 ;
888 }
889 }
890START:
891 /* get length of packet */
892 sum = 0;
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 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000898 /* get sequence number */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200899 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000900 if ((new_char & 0xE0) == 0)
901 goto packet_error;
902 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000903 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000904 --length;
905
906 /* NEW CODE - check sequence numbers for retried packets */
907 /* Note - this new code assumes that the sequence number is correctly
908 * received. Handling an invalid sequence number adds another layer
909 * of complexity that may not be needed - yet! At this time, I'm hoping
910 * that I don't need to buffer the incoming data packets and can write
911 * the data into memory in real time.
912 */
913 if (n == last_n) {
914 /* same sequence number, restore the previous state */
915 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000916 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000917 } else {
918 /* new sequence number, checkpoint the download */
919 last_n = n;
920 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000921 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000922 }
923 /* END NEW CODE */
924
925 /* get packet type */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200926 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000927 if ((new_char & 0xE0) == 0)
928 goto packet_error;
929 sum += new_char & 0xff;
930 k_state = new_char;
931 --length;
932 /* check for extended length */
933 if (length == -2) {
934 /* (length byte was 0, decremented twice) */
935 /* get the two length bytes */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200936 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000937 if ((new_char & 0xE0) == 0)
938 goto packet_error;
939 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000940 len_hi = untochar(new_char);
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200941 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000942 if ((new_char & 0xE0) == 0)
943 goto packet_error;
944 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000945 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000946 length = len_hi * 95 + len_lo;
947 /* check header checksum */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200948 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000949 if ((new_char & 0xE0) == 0)
950 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000951 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000952 goto packet_error;
953 sum += new_char & 0xff;
954/* --length; */ /* new length includes only data and block check to come */
955 }
956 /* bring in rest of packet */
957 while (length > 1) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200958 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000959 if ((new_char & 0xE0) == 0)
960 goto packet_error;
961 sum += new_char & 0xff;
962 --length;
963 if (k_state == DATA_TYPE) {
964 /* pass on the data if this is a data packet */
965 k_data_char (new_char);
966 } else if (k_state == SEND_TYPE) {
967 /* save send pack in buffer as is */
968 *send_ptr++ = new_char;
969 /* if too much data, back off the pointer */
970 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
971 --send_ptr;
972 }
973 }
974 /* get and validate checksum character */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200975 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000976 if ((new_char & 0xE0) == 0)
977 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000978 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000979 goto packet_error;
980 /* get END_CHAR */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200981 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000982 if (new_char != END_CHAR) {
983 packet_error:
984 /* restore state machines */
985 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000986 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000987 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000988 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000989 } else if (k_state == SEND_TYPE) {
990 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000991 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000992 } else {
993 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000994 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000995 /* quit if end of transmission */
996 if (k_state == BREAK_TYPE)
997 done = 1;
998 }
wdenk8bde7f72003-06-27 21:31:46 +0000999 }
1000 return ((ulong) os_data_addr - (ulong) bin_start_address);
1001}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001002
1003static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001004 if (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +02001005 return (getchar());
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001006 return -1;
1007}
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001008static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001009{
1010 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001011 int err;
1012 int res;
1013 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001014 char ymodemBuf[1024];
1015 ulong store_addr = ~0;
1016 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001017
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001018 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001019 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +00001020 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001021 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001022
Pali Rohár081bd242021-08-03 16:28:42 +02001023 err = 0;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001024 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +00001025 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001026 store_addr = addr + offset;
1027 size += res;
1028 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +09001029#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +00001030 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001031 int rc;
1032
Kim Phillips088f1b12012-10-29 13:34:31 +00001033 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001034 store_addr, res);
1035 if (rc != 0) {
Pali Rohár081bd242021-08-03 16:28:42 +02001036 xyzModem_stream_terminate(true, &getcxmodem);
1037 xyzModem_stream_close(&err);
1038 printf("\n");
Simon Glass0ee482522019-12-28 10:44:40 -07001039 flash_perror(rc);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001040 return (~0);
1041 }
1042 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001043#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001044 {
Kim Phillips088f1b12012-10-29 13:34:31 +00001045 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001046 res);
1047 }
1048
1049 }
Pali Rohár081bd242021-08-03 16:28:42 +02001050 if (err) {
1051 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1052 xyzModem_stream_close(&err);
1053 printf("\n%s\n", xyzModem_error(err));
1054 return (~0); /* Download aborted */
1055 }
1056
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +00001057 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1058 efi_set_bootdev("Uart", "", "",
1059 map_sysmem(offset, 0), size);
1060
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001061 } else {
Pali Rohár081bd242021-08-03 16:28:42 +02001062 printf("\n%s\n", xyzModem_error(err));
1063 return (~0); /* Download aborted */
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001064 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001065
Kim Phillips088f1b12012-10-29 13:34:31 +00001066 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohár1f26c492021-08-03 16:28:41 +02001067 xyzModem_stream_close(&err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001068
1069
Chris Packham0a6036d2016-10-25 20:22:48 +13001070 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001071
Kim Phillips088f1b12012-10-29 13:34:31 +00001072 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001073 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001074
1075 return offset;
1076}
1077
Jon Loeliger90253172007-07-10 11:02:44 -05001078#endif
wdenk8bde7f72003-06-27 21:31:46 +00001079
Rui Miguel Silvabfef72e2022-05-11 10:55:40 +01001080#if defined(CONFIG_CMD_LOADM)
1081static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
1082 char *const argv[])
1083{
1084 ulong addr, dest, size;
1085 void *src, *dst;
1086
1087 if (argc != 4)
1088 return CMD_RET_USAGE;
1089
1090 addr = simple_strtoul(argv[1], NULL, 16);
1091
1092 dest = simple_strtoul(argv[2], NULL, 16);
1093
1094 size = simple_strtoul(argv[3], NULL, 16);
1095
1096 if (!size) {
1097 printf("loadm: can not load zero bytes\n");
1098 return 1;
1099 }
1100
1101 src = map_sysmem(addr, size);
1102 dst = map_sysmem(dest, size);
1103
1104 memcpy(dst, src, size);
1105
1106 unmap_sysmem(src);
1107 unmap_sysmem(dst);
1108
1109 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1110 efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
1111
1112 printf("loaded bin to memory: size: %lu\n", size);
1113
1114 return 0;
1115}
1116#endif
1117
wdenk8bde7f72003-06-27 21:31:46 +00001118/* -------------------------------------------------------------------- */
1119
Jon Loeligerc76fe472007-07-08 18:02:23 -05001120#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001121
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001122#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001123U_BOOT_CMD(
1124 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001125 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001126 "[ off ] [ baud ]\n"
1127 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001128 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001129);
1130
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001131#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001132U_BOOT_CMD(
1133 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001134 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001135 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001136 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001137);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001138#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001139
1140/*
1141 * SAVES always requires LOADS support, but not vice versa
1142 */
1143
1144
Jon Loeligerc76fe472007-07-08 18:02:23 -05001145#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001146#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001147U_BOOT_CMD(
1148 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001149 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001150 "[ off ] [size] [ baud ]\n"
1151 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001152 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001153);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001154#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001155U_BOOT_CMD(
1156 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001157 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001158 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001159 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001160);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001161#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001162#endif /* CONFIG_CMD_SAVES */
1163#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001164
1165
Jon Loeligerc76fe472007-07-08 18:02:23 -05001166#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001167U_BOOT_CMD(
1168 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001169 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001170 "[ addr [ baud ] ]\n"
wdenk8bde7f72003-06-27 21:31:46 +00001171 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001172 " at address 'addr' with baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001173);
1174
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001175U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001176 loadx, 3, 0, do_load_serial_bin,
1177 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001178 "[ addr [ baud ] ]\n"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001179 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001180 " at address 'addr' with baudrate 'baud'"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001181);
1182
1183U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001184 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001185 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001186 "[ addr [ baud ] ]\n"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001187 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001188 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001189);
1190
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001191#endif /* CONFIG_CMD_LOADB */
Rui Miguel Silvabfef72e2022-05-11 10:55:40 +01001192
1193#if defined(CONFIG_CMD_LOADM)
1194U_BOOT_CMD(
1195 loadm, 4, 0, do_load_memory_bin,
1196 "load binary blob from source address to destination address",
1197 "[src_addr] [dst_addr] [size]\n"
1198 " - load a binary blob from one memory location to other"
1199 " from src_addr to dst_addr by size bytes"
1200);
1201#endif /* CONFIG_CMD_LOADM */