blob: 20d802502ae65aea69718ea16dec549ce6d17bed [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk57b2d802003-06-27 21:31:46 +00002/*
wdenk29e7f5a2004-03-12 00:14:09 +00003 * (C) Copyright 2000-2004
wdenk57b2d802003-06-27 21:31:46 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk57b2d802003-06-27 21:31:46 +00005 */
6
7/*
8 * Serial up- and download support
9 */
wdenk57b2d802003-06-27 21:31:46 +000010#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000013#include <efi_loader.h>
Simon Glass313112a2019-08-01 09:46:46 -060014#include <env.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000015#include <exports.h>
Tom Rini063c9382022-07-23 13:05:03 -040016#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa606ffc2019-12-28 10:44:40 -070017#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040018#endif
Simon Glass85f13782019-12-28 10:45:03 -070019#include <image.h>
Marek Vasut4d2be382021-10-10 23:52:41 +020020#include <lmb.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000021#include <mapmem.h>
wdenk57b2d802003-06-27 21:31:46 +000022#include <net.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000023#include <s_record.h>
Simon Glass36736182019-11-14 12:57:24 -070024#include <serial.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020025#include <xyzModem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060026#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060027#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
wdenk57b2d802003-06-27 21:31:46 +000029
Wolfgang Denk6405a152006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
wdenk57b2d802003-06-27 21:31:46 +000031
Jon Loeligerd76b5c12007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_LOADB)
Angus Ainslief283bd02013-06-26 16:54:24 -060033static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denkda916932006-05-02 00:11:25 +020034#endif
35
Jon Loeligerd76b5c12007-07-08 18:02:23 -050036#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000037static ulong load_serial(long offset);
38static int read_record(char *buf, ulong len);
Jon Loeligerd76b5c12007-07-08 18:02:23 -050039# if defined(CONFIG_CMD_SAVES)
Kim Phillipsdc00a682012-10-29 13:34:31 +000040static int save_serial(ulong offset, ulong size);
41static int write_record(char *buf);
Jon Loeligerd704d912007-07-10 11:02:44 -050042#endif
wdenk57b2d802003-06-27 21:31:46 +000043
44static int do_echo = 1;
Jon Loeligerd704d912007-07-10 11:02:44 -050045#endif
wdenk57b2d802003-06-27 21:31:46 +000046
47/* -------------------------------------------------------------------- */
48
Jon Loeligerd76b5c12007-07-08 18:02:23 -050049#if defined(CONFIG_CMD_LOADS)
Simon Glassed38aef2020-05-10 11:40:03 -060050static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +000052{
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020053 long offset = 0;
wdenk57b2d802003-06-27 21:31:46 +000054 ulong addr;
55 int i;
56 char *env_echo;
57 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020058#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000059 int load_baudrate, current_baudrate;
60
61 load_baudrate = current_baudrate = gd->baudrate;
62#endif
63
Simon Glass64b723f2017-08-03 12:22:12 -060064 env_echo = env_get("loads_echo");
65 if (env_echo && *env_echo == '1')
wdenk57b2d802003-06-27 21:31:46 +000066 do_echo = 1;
Simon Glass64b723f2017-08-03 12:22:12 -060067 else
wdenk57b2d802003-06-27 21:31:46 +000068 do_echo = 0;
wdenk57b2d802003-06-27 21:31:46 +000069
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000071 if (argc >= 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020072 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000073 }
74 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -060075 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +000082 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +000083 load_baudrate);
84 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +020085 flush();
wdenk57b2d802003-06-27 21:31:46 +000086 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +000087 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +000088 udelay(50000);
89 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020090 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +000091 break;
92 }
93 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020094#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000095 if (argc == 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020096 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000097 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020098#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000099
Kim Phillipsdc00a682012-10-29 13:34:31 +0000100 printf("## Ready for S-Record download ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000101
Kim Phillipsdc00a682012-10-29 13:34:31 +0000102 addr = load_serial(offset);
wdenk57b2d802003-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) {
wdenk29e7f5a2004-03-12 00:14:09 +0000110 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200111 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000112 }
113 udelay(1000);
114 }
115
116 if (addr == ~0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000117 printf("## S-Record download aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000118 rcode = 1;
119 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000120 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700121 image_load_addr = addr;
wdenk57b2d802003-06-27 21:31:46 +0000122 }
123
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200124#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000125 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000126 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000127 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000128 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200129 flush();
wdenk57b2d802003-06-27 21:31:46 +0000130 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000131 serial_setbrg();
132 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000133 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200134 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000135 break;
136 }
137 }
138#endif
139 return rcode;
140}
141
Kim Phillipsdc00a682012-10-29 13:34:31 +0000142static ulong load_serial(long offset)
wdenk57b2d802003-06-27 21:31:46 +0000143{
144 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
145 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
146 int binlen; /* no. of data bytes in S-Rec. */
147 int type; /* return code for record type */
148 ulong addr; /* load address from S-Record */
149 ulong size; /* number of bytes transferred */
wdenk57b2d802003-06-27 21:31:46 +0000150 ulong store_addr;
151 ulong start_addr = ~0;
152 ulong end_addr = 0;
153 int line_count = 0;
Marek Vasut4d2be382021-10-10 23:52:41 +0200154 long ret;
155
wdenk57b2d802003-06-27 21:31:46 +0000156 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000157 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk57b2d802003-06-27 21:31:46 +0000158
159 if (type < 0) {
160 return (~0); /* Invalid S-Record */
161 }
162
163 switch (type) {
164 case SREC_DATA2:
165 case SREC_DATA3:
166 case SREC_DATA4:
167 store_addr = addr + offset;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900168#ifdef CONFIG_MTD_NOR_FLASH
wdenk57b2d802003-06-27 21:31:46 +0000169 if (addr2info(store_addr)) {
170 int rc;
171
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200172 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk57b2d802003-06-27 21:31:46 +0000173 if (rc != 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000174 flash_perror(rc);
wdenk57b2d802003-06-27 21:31:46 +0000175 return (~0);
176 }
177 } else
178#endif
179 {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200180 void *dst;
181
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530182 ret = lmb_reserve(store_addr, binlen);
Marek Vasut4d2be382021-10-10 23:52:41 +0200183 if (ret) {
184 printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
185 store_addr, store_addr + binlen);
186 return ret;
187 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200188 dst = map_sysmem(store_addr, binlen);
189 memcpy(dst, binbuf, binlen);
190 unmap_sysmem(dst);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530191 lmb_free(store_addr, binlen);
wdenk57b2d802003-06-27 21:31:46 +0000192 }
193 if ((store_addr) < start_addr)
194 start_addr = store_addr;
195 if ((store_addr + binlen - 1) > end_addr)
196 end_addr = store_addr + binlen - 1;
197 break;
198 case SREC_END2:
199 case SREC_END3:
200 case SREC_END4:
Kim Phillipsdc00a682012-10-29 13:34:31 +0000201 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000202 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000203 printf("\n"
wdenk57b2d802003-06-27 21:31:46 +0000204 "## First Load Addr = 0x%08lX\n"
205 "## Last Load Addr = 0x%08lX\n"
206 "## Total Size = 0x%08lX = %ld Bytes\n",
207 start_addr, end_addr, size, size
208 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000209 flush_cache(start_addr, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600210 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000211 return (addr);
212 case SREC_START:
213 break;
214 default:
215 break;
216 }
217 if (!do_echo) { /* print a '.' every 100 lines */
218 if ((++line_count % 100) == 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000219 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000220 }
221 }
222
223 return (~0); /* Download aborted */
224}
225
Kim Phillipsdc00a682012-10-29 13:34:31 +0000226static int read_record(char *buf, ulong len)
wdenk57b2d802003-06-27 21:31:46 +0000227{
228 char *p;
Tom Rini479f54a2024-01-09 17:57:16 -0500229 int c;
wdenk57b2d802003-06-27 21:31:46 +0000230
231 --len; /* always leave room for terminating '\0' byte */
232
233 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200234 c = getchar(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000235 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000236 putc(c); /* ... and echo it */
wdenk57b2d802003-06-27 21:31:46 +0000237
238 switch (c) {
239 case '\r':
240 case '\n':
241 *p = '\0';
242 return (p - buf);
243 case '\0':
244 case 0x03: /* ^C - Control C */
245 return (-1);
246 default:
247 *p = c;
248 }
249
Michal Simekb6e8ba12020-12-01 13:58:28 +0100250 /* Check for the console hangup (if any different from serial) */
251 if (gd->jt->getc != getchar) {
252 if (ctrlc())
253 return (-1);
wdenk57b2d802003-06-27 21:31:46 +0000254 }
wdenk57b2d802003-06-27 21:31:46 +0000255 }
256
257 /* line too long - truncate */
258 *p = '\0';
259 return (p - buf);
260}
261
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500262#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000263
Simon Glassed38aef2020-05-10 11:40:03 -0600264int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
265 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000266{
267 ulong offset = 0;
268 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200269#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000270 int save_baudrate, current_baudrate;
271
272 save_baudrate = current_baudrate = gd->baudrate;
273#endif
274
275 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600276 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000277 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200278#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000279 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600280 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000281 }
282 if (argc == 4) {
Simon Glassff9b9032021-07-24 09:03:30 -0600283 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000284
285 /* default to current baudrate */
286 if (save_baudrate == 0)
287 save_baudrate = current_baudrate;
288 }
289 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000290 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000291 save_baudrate);
292 udelay(50000);
293 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000294 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000295 udelay(50000);
296 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200297 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000298 break;
299 }
300 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200301#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000302 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600303 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000304 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200305#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000306
Kim Phillipsdc00a682012-10-29 13:34:31 +0000307 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000308 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200309 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000310 break;
311 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000312 if (save_serial(offset, size)) {
313 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000314 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000315 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000316 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200317#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000318 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000319 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000320 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000321 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200322 flush();
wdenk57b2d802003-06-27 21:31:46 +0000323 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000324 serial_setbrg();
325 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000326 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200327 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000328 break;
329 }
330 }
331#endif
332 return 0;
333}
334
335#define SREC3_START "S0030000FC\n"
336#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
337#define SREC3_END "S70500000000FA\n"
338#define SREC_BYTES_PER_RECORD 16
339
Kim Phillipsdc00a682012-10-29 13:34:31 +0000340static int save_serial(ulong address, ulong count)
wdenk57b2d802003-06-27 21:31:46 +0000341{
342 int i, c, reclen, checksum, length;
343 char *hex = "0123456789ABCDEF";
344 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
345 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
346
347 reclen = 0;
348 checksum = 0;
349
350 if(write_record(SREC3_START)) /* write the header */
351 return (-1);
352 do {
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200353 volatile uchar *src;
354
355 src = map_sysmem(address, count);
356 if (count) { /* collect hex data in the buffer */
357 c = src[reclen]; /* get one byte */
358 checksum += c; /* accumulate checksum */
wdenk57b2d802003-06-27 21:31:46 +0000359 data[2*reclen] = hex[(c>>4)&0x0f];
360 data[2*reclen+1] = hex[c & 0x0f];
361 data[2*reclen+2] = '\0';
362 ++reclen;
363 --count;
364 }
Heinrich Schuchardt604404b2023-06-25 11:54:23 +0200365 unmap_sysmem((void *)src);
wdenk57b2d802003-06-27 21:31:46 +0000366 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
367 /* enough data collected for one record: dump it */
368 if(reclen) { /* build & write a data record: */
369 /* address + data + checksum */
370 length = 4 + reclen + 1;
371
372 /* accumulate length bytes into checksum */
373 for(i = 0; i < 2; i++)
374 checksum += (length >> (8*i)) & 0xff;
375
376 /* accumulate address bytes into checksum: */
377 for(i = 0; i < 4; i++)
378 checksum += (address >> (8*i)) & 0xff;
379
380 /* make proper checksum byte: */
381 checksum = ~checksum & 0xff;
382
383 /* output one record: */
384 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
385 if(write_record(record))
386 return (-1);
387 }
388 address += reclen; /* increment address */
389 checksum = 0;
390 reclen = 0;
391 }
392 }
393 while(count);
394 if(write_record(SREC3_END)) /* write the final record */
395 return (-1);
396 return(0);
397}
398
Kim Phillipsdc00a682012-10-29 13:34:31 +0000399static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000400{
401 char c;
402
403 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000404 putc(c);
wdenk57b2d802003-06-27 21:31:46 +0000405
406 /* Check for the console hangup (if any different from serial) */
407
408 if (ctrlc()) {
409 return (-1);
410 }
411 return (0);
412}
Jon Loeligerd704d912007-07-10 11:02:44 -0500413# endif
wdenk57b2d802003-06-27 21:31:46 +0000414
Jon Loeligerd704d912007-07-10 11:02:44 -0500415#endif
wdenk57b2d802003-06-27 21:31:46 +0000416
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500417#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500418/*
419 * loadb command (load binary) included
420 */
wdenk57b2d802003-06-27 21:31:46 +0000421#define XON_CHAR 17
422#define XOFF_CHAR 19
423#define START_CHAR 0x01
424#define ETX_CHAR 0x03
425#define END_CHAR 0x0D
426#define SPACE 0x20
427#define K_ESCAPE 0x23
428#define SEND_TYPE 'S'
429#define DATA_TYPE 'D'
430#define ACK_TYPE 'Y'
431#define NACK_TYPE 'N'
432#define BREAK_TYPE 'B'
433#define tochar(x) ((char) (((x) + SPACE) & 0xff))
434#define untochar(x) ((int) (((x) - SPACE) & 0xff))
435
wdenk57b2d802003-06-27 21:31:46 +0000436static void set_kerm_bin_mode(unsigned long *);
437static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000438static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000439
Kim Phillipsdc00a682012-10-29 13:34:31 +0000440static char his_eol; /* character he needs at end of packet */
441static int his_pad_count; /* number of pad chars he needs */
442static char his_pad_char; /* pad chars he needs */
443static char his_quote; /* quote chars he'll use */
wdenk57b2d802003-06-27 21:31:46 +0000444
Simon Glassed38aef2020-05-10 11:40:03 -0600445static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
446 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000447{
wdenk57b2d802003-06-27 21:31:46 +0000448 ulong offset = 0;
449 ulong addr;
450 int load_baudrate, current_baudrate;
451 int rcode = 0;
452 char *s;
453
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200454 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
455 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000456
457 /* pre-set offset from $loadaddr */
Simon Glass64b723f2017-08-03 12:22:12 -0600458 s = env_get("loadaddr");
459 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600460 offset = hextoul(s, NULL);
wdenk57b2d802003-06-27 21:31:46 +0000461
462 load_baudrate = current_baudrate = gd->baudrate;
463
464 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600465 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000466 }
467 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600468 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000469
470 /* default to current baudrate */
471 if (load_baudrate == 0)
472 load_baudrate = current_baudrate;
473 }
474
475 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000476 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000477 load_baudrate);
478 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200479 flush();
wdenk57b2d802003-06-27 21:31:46 +0000480 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000481 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000482 udelay(50000);
483 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200484 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000485 break;
486 }
487 }
488
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200489 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000490 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200491 "to 0x%08lX at %d bps...\n",
492 offset,
493 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000494
Angus Ainslief283bd02013-06-26 16:54:24 -0600495 addr = load_serial_ymodem(offset, xyzModem_ymodem);
496
Pali Roháre37155f2021-08-03 16:28:43 +0200497 if (addr == ~0) {
498 image_load_addr = 0;
499 printf("## Binary (ymodem) download aborted\n");
500 rcode = 1;
501 } else {
502 printf("## Start Addr = 0x%08lX\n", addr);
503 image_load_addr = addr;
504 }
Angus Ainslief283bd02013-06-26 16:54:24 -0600505 } else if (strcmp(argv[0],"loadx")==0) {
506 printf("## Ready for binary (xmodem) download "
507 "to 0x%08lX at %d bps...\n",
508 offset,
509 load_baudrate);
510
511 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200512
Pali Roháre37155f2021-08-03 16:28:43 +0200513 if (addr == ~0) {
514 image_load_addr = 0;
515 printf("## Binary (xmodem) download aborted\n");
516 rcode = 1;
517 } else {
518 printf("## Start Addr = 0x%08lX\n", addr);
519 image_load_addr = addr;
520 }
wdenk57b2d802003-06-27 21:31:46 +0000521 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200522
Kim Phillipsdc00a682012-10-29 13:34:31 +0000523 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200524 "to 0x%08lX at %d bps...\n",
525 offset,
526 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000527 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000528
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200529 if (addr == ~0) {
Simon Glass892265d2019-12-28 10:45:02 -0700530 image_load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000531 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200532 rcode = 1;
533 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000534 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700535 image_load_addr = addr;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200536 }
537 }
wdenk57b2d802003-06-27 21:31:46 +0000538 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000539 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000540 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000541 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200542 flush();
wdenk57b2d802003-06-27 21:31:46 +0000543 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000544 serial_setbrg();
545 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000546 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200547 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000548 break;
549 }
550 }
551
wdenk57b2d802003-06-27 21:31:46 +0000552 return rcode;
553}
554
Kim Phillipsdc00a682012-10-29 13:34:31 +0000555static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000556{
557 int size, i;
wdenk57b2d802003-06-27 21:31:46 +0000558
Kim Phillipsdc00a682012-10-29 13:34:31 +0000559 set_kerm_bin_mode((ulong *) offset);
560 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000561
562 /*
563 * Gather any trailing characters (for instance, the ^D which
564 * is sent by 'cu' after sending a file), and give the
565 * box some time (100 * 1 ms)
566 */
567 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000568 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200569 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000570 }
571 udelay(1000);
572 }
573
Pali Rohár8398dd32021-08-06 18:07:39 +0200574 if (size == 0)
575 return ~0; /* Download aborted */
576
Kim Phillipsdc00a682012-10-29 13:34:31 +0000577 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000578
579 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600580 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000581
582 return offset;
583}
584
Kim Phillipsdc00a682012-10-29 13:34:31 +0000585static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000586{
587 int count = his_pad_count;
588
589 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000590 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000591}
592
593/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000594static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000595{
596 if ((in & 0x60) == 0x40) {
597 return (char) (in & ~0x40);
598 } else if ((in & 0x7f) == 0x3f) {
599 return (char) (in | 0x40);
600 } else
601 return in;
602}
603
Kim Phillipsdc00a682012-10-29 13:34:31 +0000604static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000605{
606 int total = 0;
607
608 while (*buffer) {
609 total += *buffer++;
610 }
611 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
612}
613
Kim Phillipsdc00a682012-10-29 13:34:31 +0000614static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000615{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000616 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000617 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000618 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000619 }
620}
621
622static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000623static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000624{
625 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000626 a_b[1] = tochar(3);
627 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000628 a_b[3] = ACK_TYPE;
629 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000630 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000631 a_b[5] = his_eol;
632 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000633 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000634}
635
Kim Phillipsdc00a682012-10-29 13:34:31 +0000636static void send_nack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000637{
638 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000639 a_b[1] = tochar(3);
640 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000641 a_b[3] = NACK_TYPE;
642 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000643 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000644 a_b[5] = his_eol;
645 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000646 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000647}
648
Kim Phillipsdc00a682012-10-29 13:34:31 +0000649static void (*os_data_init)(void);
650static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000651static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000652static char *os_data_addr, *os_data_addr_saved;
653static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530654
Kim Phillipsdc00a682012-10-29 13:34:31 +0000655static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000656{
657 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000658 os_data_addr = bin_start_address;
659}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530660
Kim Phillipsdc00a682012-10-29 13:34:31 +0000661static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000662{
663 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000664 os_data_addr_saved = os_data_addr;
665}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530666
Kim Phillipsdc00a682012-10-29 13:34:31 +0000667static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000668{
669 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000670 os_data_addr = os_data_addr_saved;
671}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530672
Kim Phillipsdc00a682012-10-29 13:34:31 +0000673static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000674{
675 switch (os_data_state) {
676 case 0: /* data */
677 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000678 break;
679 }
680}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530681
Kim Phillipsdc00a682012-10-29 13:34:31 +0000682static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000683{
684 bin_start_address = (char *) addr;
685 os_data_init = bin_data_init;
686 os_data_char = bin_data_char;
687}
688
wdenk57b2d802003-06-27 21:31:46 +0000689/* k_data_* simply handles the kermit escape translations */
690static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000691static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000692{
693 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000694 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000695}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530696
Kim Phillipsdc00a682012-10-29 13:34:31 +0000697static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000698{
699 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000700 os_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000701}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530702
Kim Phillipsdc00a682012-10-29 13:34:31 +0000703static void k_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000704{
705 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000706 os_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000707}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530708
Kim Phillipsdc00a682012-10-29 13:34:31 +0000709static void k_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000710{
711 if (k_data_escape) {
712 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000713 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000714 k_data_escape = 0;
715 } else {
716 if (new_char == his_quote) {
717 /* this char is escape - remember */
718 k_data_escape = 1;
719 } else {
720 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000721 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000722 }
723 }
724}
725
726#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000727static char send_parms[SEND_DATA_SIZE];
728static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000729
730/* handle_send_packet interprits the protocol info and builds and
731 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000732static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000733{
734 int length = 3;
735 int bytes;
736
737 /* initialize some protocol parameters */
738 his_eol = END_CHAR; /* default end of line character */
739 his_pad_count = 0;
740 his_pad_char = '\0';
741 his_quote = K_ESCAPE;
742
743 /* ignore last character if it filled the buffer */
744 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
745 --send_ptr;
746 bytes = send_ptr - send_parms; /* how many bytes we'll process */
747 do {
748 if (bytes-- <= 0)
749 break;
750 /* handle MAXL - max length */
751 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000752 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000753 if (bytes-- <= 0)
754 break;
755 /* handle TIME - time you should wait for my packets */
756 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000757 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000758 if (bytes-- <= 0)
759 break;
760 /* handle NPAD - number of pad chars I need */
761 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000762 his_pad_count = untochar(send_parms[2]);
763 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000764 if (bytes-- <= 0)
765 break;
766 /* handle PADC - pad chars I need */
767 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000768 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000769 a_b[++length] = 0x40; /* He should ignore this */
770 if (bytes-- <= 0)
771 break;
772 /* handle EOL - end of line he needs */
773 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000774 his_eol = untochar(send_parms[4]);
775 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000776 if (bytes-- <= 0)
777 break;
778 /* handle QCTL - quote control char he'll use */
779 /* remember what he says - I'll use '#' */
780 his_quote = send_parms[5];
781 a_b[++length] = '#';
782 if (bytes-- <= 0)
783 break;
784 /* handle QBIN - 8-th bit prefixing */
785 /* ignore what he says - I refuse */
786 a_b[++length] = 'N';
787 if (bytes-- <= 0)
788 break;
789 /* handle CHKT - the clock check type */
790 /* ignore what he says - I do type 1 (for now) */
791 a_b[++length] = '1';
792 if (bytes-- <= 0)
793 break;
794 /* handle REPT - the repeat prefix */
795 /* ignore what he says - I refuse (for now) */
796 a_b[++length] = 'N';
797 if (bytes-- <= 0)
798 break;
799 /* handle CAPAS - the capabilities mask */
800 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000801 a_b[++length] = tochar(2); /* only long packets */
802 a_b[++length] = tochar(0); /* no windows */
803 a_b[++length] = tochar(94); /* large packet msb */
804 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000805 } while (0);
806
807 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000808 a_b[1] = tochar(length);
809 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000810 a_b[3] = ACK_TYPE;
811 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000812 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000813 a_b[++length] = his_eol;
814 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000815 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000816}
817
818/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000819static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000820{
Tom Rini479f54a2024-01-09 17:57:16 -0500821 int new_char;
wdenk57b2d802003-06-27 21:31:46 +0000822 char k_state, k_state_saved;
823 int sum;
824 int done;
825 int length;
826 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000827 int len_lo, len_hi;
828
829 /* initialize some protocol parameters */
830 his_eol = END_CHAR; /* default end of line character */
831 his_pad_count = 0;
832 his_pad_char = '\0';
833 his_quote = K_ESCAPE;
834
835 /* initialize the k_recv and k_data state machine */
836 done = 0;
837 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000838 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000839 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000840 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000841 n = 0; /* just to get rid of a warning */
842 last_n = -1;
843
844 /* expect this "type" sequence (but don't check):
845 S: send initiate
846 F: file header
847 D: data (multiple)
848 Z: end of file
849 B: break transmission
850 */
851
852 /* enter main loop */
853 while (!done) {
854 /* set the send packet pointer to begining of send packet parms */
855 send_ptr = send_parms;
856
857 /* With each packet, start summing the bytes starting with the length.
858 Save the current sequence number.
859 Note the type of the packet.
860 If a character less than SPACE (0x20) is received - error.
861 */
862
863#if 0
864 /* OLD CODE, Prior to checking sequence numbers */
865 /* first have all state machines save current states */
866 k_state_saved = k_state;
867 k_data_save ();
868#endif
869
870 /* get a packet */
871 /* wait for the starting character or ^C */
872 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200873 switch (getchar()) {
wdenk57b2d802003-06-27 21:31:46 +0000874 case START_CHAR: /* start packet */
875 goto START;
876 case ETX_CHAR: /* ^C waiting for packet */
877 return (0);
878 default:
879 ;
880 }
881 }
882START:
883 /* get length of packet */
884 sum = 0;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200885 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000886 if ((new_char & 0xE0) == 0)
887 goto packet_error;
888 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000889 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000890 /* get sequence number */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200891 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000892 if ((new_char & 0xE0) == 0)
893 goto packet_error;
894 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000895 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000896 --length;
897
898 /* NEW CODE - check sequence numbers for retried packets */
899 /* Note - this new code assumes that the sequence number is correctly
900 * received. Handling an invalid sequence number adds another layer
901 * of complexity that may not be needed - yet! At this time, I'm hoping
902 * that I don't need to buffer the incoming data packets and can write
903 * the data into memory in real time.
904 */
905 if (n == last_n) {
906 /* same sequence number, restore the previous state */
907 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000908 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000909 } else {
910 /* new sequence number, checkpoint the download */
911 last_n = n;
912 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000913 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000914 }
915 /* END NEW CODE */
916
917 /* get packet type */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200918 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000919 if ((new_char & 0xE0) == 0)
920 goto packet_error;
921 sum += new_char & 0xff;
922 k_state = new_char;
923 --length;
924 /* check for extended length */
925 if (length == -2) {
926 /* (length byte was 0, decremented twice) */
927 /* get the two length bytes */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200928 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000929 if ((new_char & 0xE0) == 0)
930 goto packet_error;
931 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000932 len_hi = untochar(new_char);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200933 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000934 if ((new_char & 0xE0) == 0)
935 goto packet_error;
936 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000937 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000938 length = len_hi * 95 + len_lo;
939 /* check header checksum */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200940 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000941 if ((new_char & 0xE0) == 0)
942 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000943 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000944 goto packet_error;
945 sum += new_char & 0xff;
946/* --length; */ /* new length includes only data and block check to come */
947 }
948 /* bring in rest of packet */
949 while (length > 1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200950 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000951 if ((new_char & 0xE0) == 0)
952 goto packet_error;
953 sum += new_char & 0xff;
954 --length;
955 if (k_state == DATA_TYPE) {
956 /* pass on the data if this is a data packet */
957 k_data_char (new_char);
958 } else if (k_state == SEND_TYPE) {
959 /* save send pack in buffer as is */
960 *send_ptr++ = new_char;
961 /* if too much data, back off the pointer */
962 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
963 --send_ptr;
964 }
965 }
966 /* get and validate checksum character */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200967 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000968 if ((new_char & 0xE0) == 0)
969 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000970 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000971 goto packet_error;
972 /* get END_CHAR */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200973 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000974 if (new_char != END_CHAR) {
975 packet_error:
976 /* restore state machines */
977 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000978 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000979 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000980 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000981 } else if (k_state == SEND_TYPE) {
982 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000983 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000984 } else {
985 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000986 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000987 /* quit if end of transmission */
988 if (k_state == BREAK_TYPE)
989 done = 1;
990 }
wdenk57b2d802003-06-27 21:31:46 +0000991 }
992 return ((ulong) os_data_addr - (ulong) bin_start_address);
993}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200994
995static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200996 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200997 return (getchar());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200998 return -1;
999}
Angus Ainslief283bd02013-06-26 16:54:24 -06001000static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001001{
1002 int size;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001003 int err;
1004 int res;
1005 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001006 char ymodemBuf[1024];
1007 ulong store_addr = ~0;
1008 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001009
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001010 size = 0;
Angus Ainslief283bd02013-06-26 16:54:24 -06001011 info.mode = mode;
Kim Phillipsdc00a682012-10-29 13:34:31 +00001012 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001013 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001014
Pali Rohárb6f177b2021-08-03 16:28:42 +02001015 err = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001016 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +00001017 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001018 store_addr = addr + offset;
1019 size += res;
1020 addr += res;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +09001021#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +00001022 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001023 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001024
Kim Phillipsdc00a682012-10-29 13:34:31 +00001025 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001026 store_addr, res);
1027 if (rc != 0) {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001028 xyzModem_stream_terminate(true, &getcxmodem);
1029 xyzModem_stream_close(&err);
1030 printf("\n");
Simon Glassa606ffc2019-12-28 10:44:40 -07001031 flash_perror(rc);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001032 return (~0);
1033 }
1034 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001035#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001036 {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001037 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001038 res);
1039 }
1040
1041 }
Pali Rohárb6f177b2021-08-03 16:28:42 +02001042 if (err) {
1043 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1044 xyzModem_stream_close(&err);
1045 printf("\n%s\n", xyzModem_error(err));
1046 return (~0); /* Download aborted */
1047 }
1048
Heinrich Schuchardta89adb02021-03-19 02:50:57 +00001049 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1050 efi_set_bootdev("Uart", "", "",
1051 map_sysmem(offset, 0), size);
1052
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001053 } else {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001054 printf("\n%s\n", xyzModem_error(err));
1055 return (~0); /* Download aborted */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001056 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001057
Kim Phillipsdc00a682012-10-29 13:34:31 +00001058 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohárd9c805e2021-08-03 16:28:41 +02001059 xyzModem_stream_close(&err);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001060
Chris Packhambbaf4242016-10-25 20:22:48 +13001061 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001062
Kim Phillipsdc00a682012-10-29 13:34:31 +00001063 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -06001064 env_set_hex("filesize", size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001065
1066 return offset;
1067}
1068
Jon Loeligerd704d912007-07-10 11:02:44 -05001069#endif
wdenk57b2d802003-06-27 21:31:46 +00001070
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001071#if defined(CONFIG_CMD_LOADM)
1072static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
1073 char *const argv[])
1074{
1075 ulong addr, dest, size;
1076 void *src, *dst;
1077
1078 if (argc != 4)
1079 return CMD_RET_USAGE;
1080
1081 addr = simple_strtoul(argv[1], NULL, 16);
1082
1083 dest = simple_strtoul(argv[2], NULL, 16);
1084
1085 size = simple_strtoul(argv[3], NULL, 16);
1086
1087 if (!size) {
1088 printf("loadm: can not load zero bytes\n");
1089 return 1;
1090 }
1091
1092 src = map_sysmem(addr, size);
1093 dst = map_sysmem(dest, size);
1094
1095 memcpy(dst, src, size);
1096
1097 unmap_sysmem(src);
1098 unmap_sysmem(dst);
1099
1100 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1101 efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
1102
1103 printf("loaded bin to memory: size: %lu\n", size);
1104
1105 return 0;
1106}
1107#endif
1108
wdenk57b2d802003-06-27 21:31:46 +00001109/* -------------------------------------------------------------------- */
1110
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001111#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001112
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001113#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001114U_BOOT_CMD(
1115 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001116 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001117 "[ off ] [ baud ]\n"
1118 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001119 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001120);
1121
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001122#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001123U_BOOT_CMD(
1124 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001125 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001126 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001127 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001128);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001129#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001130
1131/*
1132 * SAVES always requires LOADS support, but not vice versa
1133 */
1134
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001135#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001136#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001137U_BOOT_CMD(
1138 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001139 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001140 "[ off ] [size] [ baud ]\n"
1141 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001142 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001143);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001144#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001145U_BOOT_CMD(
1146 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001147 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001148 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001149 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001150);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001151#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001152#endif /* CONFIG_CMD_SAVES */
1153#endif /* CONFIG_CMD_LOADS */
wdenk57b2d802003-06-27 21:31:46 +00001154
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001155#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001156U_BOOT_CMD(
1157 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001158 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001159 "[ addr [ baud ] ]\n"
wdenk57b2d802003-06-27 21:31:46 +00001160 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001161 " at address 'addr' with baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001162);
1163
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001164U_BOOT_CMD(
Angus Ainslief283bd02013-06-26 16:54:24 -06001165 loadx, 3, 0, do_load_serial_bin,
1166 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001167 "[ addr [ baud ] ]\n"
Angus Ainslief283bd02013-06-26 16:54:24 -06001168 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001169 " at address 'addr' with baudrate 'baud'"
Angus Ainslief283bd02013-06-26 16:54:24 -06001170);
1171
1172U_BOOT_CMD(
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001173 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001174 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001175 "[ addr [ baud ] ]\n"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001176 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001177 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001178);
1179
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001180#endif /* CONFIG_CMD_LOADB */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001181
1182#if defined(CONFIG_CMD_LOADM)
1183U_BOOT_CMD(
1184 loadm, 4, 0, do_load_memory_bin,
1185 "load binary blob from source address to destination address",
1186 "[src_addr] [dst_addr] [size]\n"
1187 " - load a binary blob from one memory location to other"
1188 " from src_addr to dst_addr by size bytes"
1189);
1190#endif /* CONFIG_CMD_LOADM */