blob: 381ed1b3e25bb2ace17a607bbd9a2394e6df2b7b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk8bde7f72003-06-27 21:31:46 +00002/*
wdenk232c1502004-03-12 00:14:09 +00003 * (C) Copyright 2000-2004
wdenk8bde7f72003-06-27 21:31:46 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk8bde7f72003-06-27 21:31:46 +00005 */
6
7/*
8 * Serial up- and download support
9 */
10#include <common.h>
11#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000014#include <efi_loader.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060015#include <env.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000016#include <exports.h>
Simon Glass0ee482522019-12-28 10:44:40 -070017#include <flash.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070018#include <image.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000019#include <mapmem.h>
wdenk8bde7f72003-06-27 21:31:46 +000020#include <net.h>
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +000021#include <s_record.h>
Simon Glassb03e0512019-11-14 12:57:24 -070022#include <serial.h>
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +020023#include <xyzModem.h>
Simon Glass90526e92020-05-10 11:39:56 -060024#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060026#include <linux/delay.h>
wdenk8bde7f72003-06-27 21:31:46 +000027
Wolfgang Denkd87080b2006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
wdenk8bde7f72003-06-27 21:31:46 +000029
Jon Loeligerc76fe472007-07-08 18:02:23 -050030#if defined(CONFIG_CMD_LOADB)
Angus Ainslie6e66bd52013-06-26 16:54:24 -060031static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denk8546e232006-05-02 00:11:25 +020032#endif
33
Jon Loeligerc76fe472007-07-08 18:02:23 -050034#if defined(CONFIG_CMD_LOADS)
Kim Phillips088f1b12012-10-29 13:34:31 +000035static ulong load_serial(long offset);
36static int read_record(char *buf, ulong len);
Jon Loeligerc76fe472007-07-08 18:02:23 -050037# if defined(CONFIG_CMD_SAVES)
Kim Phillips088f1b12012-10-29 13:34:31 +000038static int save_serial(ulong offset, ulong size);
39static int write_record(char *buf);
Jon Loeliger90253172007-07-10 11:02:44 -050040#endif
wdenk8bde7f72003-06-27 21:31:46 +000041
42static int do_echo = 1;
Jon Loeliger90253172007-07-10 11:02:44 -050043#endif
wdenk8bde7f72003-06-27 21:31:46 +000044
45/* -------------------------------------------------------------------- */
46
Jon Loeligerc76fe472007-07-08 18:02:23 -050047#if defined(CONFIG_CMD_LOADS)
Simon Glass09140112020-05-10 11:40:03 -060048static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
49 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +000050{
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020051 long offset = 0;
wdenk8bde7f72003-06-27 21:31:46 +000052 ulong addr;
53 int i;
54 char *env_echo;
55 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000057 int load_baudrate, current_baudrate;
58
59 load_baudrate = current_baudrate = gd->baudrate;
60#endif
61
Simon Glass00caae62017-08-03 12:22:12 -060062 env_echo = env_get("loads_echo");
63 if (env_echo && *env_echo == '1')
wdenk8bde7f72003-06-27 21:31:46 +000064 do_echo = 1;
Simon Glass00caae62017-08-03 12:22:12 -060065 else
wdenk8bde7f72003-06-27 21:31:46 +000066 do_echo = 0;
wdenk8bde7f72003-06-27 21:31:46 +000067
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +000069 if (argc >= 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020070 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000071 }
72 if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -060073 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +000074
75 /* default to current baudrate */
76 if (load_baudrate == 0)
77 load_baudrate = current_baudrate;
78 }
79 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +000080 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +000081 load_baudrate);
82 udelay(50000);
83 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +000084 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +000085 udelay(50000);
86 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020087 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +000088 break;
89 }
90 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000092 if (argc == 2) {
Ricardo Ribalda Delgado2b22d602008-07-30 12:39:29 +020093 offset = simple_strtol(argv[1], NULL, 16);
wdenk8bde7f72003-06-27 21:31:46 +000094 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +000096
Kim Phillips088f1b12012-10-29 13:34:31 +000097 printf("## Ready for S-Record download ...\n");
wdenk8bde7f72003-06-27 21:31:46 +000098
Kim Phillips088f1b12012-10-29 13:34:31 +000099 addr = load_serial(offset);
wdenk8bde7f72003-06-27 21:31:46 +0000100
101 /*
102 * Gather any trailing characters (for instance, the ^D which
103 * is sent by 'cu' after sending a file), and give the
104 * box some time (100 * 1 ms)
105 */
106 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000107 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200108 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000109 }
110 udelay(1000);
111 }
112
113 if (addr == ~0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000114 printf("## S-Record download aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000115 rcode = 1;
116 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000117 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700118 image_load_addr = addr;
wdenk8bde7f72003-06-27 21:31:46 +0000119 }
120
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200121#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000122 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000123 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000124 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000125 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000126 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000127 serial_setbrg();
128 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000129 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200130 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000131 break;
132 }
133 }
134#endif
135 return rcode;
136}
137
Kim Phillips088f1b12012-10-29 13:34:31 +0000138static ulong load_serial(long offset)
wdenk8bde7f72003-06-27 21:31:46 +0000139{
140 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
141 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
142 int binlen; /* no. of data bytes in S-Rec. */
143 int type; /* return code for record type */
144 ulong addr; /* load address from S-Record */
145 ulong size; /* number of bytes transferred */
wdenk8bde7f72003-06-27 21:31:46 +0000146 ulong store_addr;
147 ulong start_addr = ~0;
148 ulong end_addr = 0;
149 int line_count = 0;
150
151 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000152 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk8bde7f72003-06-27 21:31:46 +0000153
154 if (type < 0) {
155 return (~0); /* Invalid S-Record */
156 }
157
158 switch (type) {
159 case SREC_DATA2:
160 case SREC_DATA3:
161 case SREC_DATA4:
162 store_addr = addr + offset;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900163#ifdef CONFIG_MTD_NOR_FLASH
wdenk8bde7f72003-06-27 21:31:46 +0000164 if (addr2info(store_addr)) {
165 int rc;
166
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200167 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000168 if (rc != 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000169 flash_perror(rc);
wdenk8bde7f72003-06-27 21:31:46 +0000170 return (~0);
171 }
172 } else
173#endif
174 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000175 memcpy((char *)(store_addr), binbuf, binlen);
wdenk8bde7f72003-06-27 21:31:46 +0000176 }
177 if ((store_addr) < start_addr)
178 start_addr = store_addr;
179 if ((store_addr + binlen - 1) > end_addr)
180 end_addr = store_addr + binlen - 1;
181 break;
182 case SREC_END2:
183 case SREC_END3:
184 case SREC_END4:
Kim Phillips088f1b12012-10-29 13:34:31 +0000185 udelay(10000);
wdenk8bde7f72003-06-27 21:31:46 +0000186 size = end_addr - start_addr + 1;
Kim Phillips088f1b12012-10-29 13:34:31 +0000187 printf("\n"
wdenk8bde7f72003-06-27 21:31:46 +0000188 "## First Load Addr = 0x%08lX\n"
189 "## Last Load Addr = 0x%08lX\n"
190 "## Total Size = 0x%08lX = %ld Bytes\n",
191 start_addr, end_addr, size, size
192 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000193 flush_cache(start_addr, size);
Simon Glass018f5302017-08-03 12:22:10 -0600194 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000195 return (addr);
196 case SREC_START:
197 break;
198 default:
199 break;
200 }
201 if (!do_echo) { /* print a '.' every 100 lines */
202 if ((++line_count % 100) == 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000203 putc('.');
wdenk8bde7f72003-06-27 21:31:46 +0000204 }
205 }
206
207 return (~0); /* Download aborted */
208}
209
Kim Phillips088f1b12012-10-29 13:34:31 +0000210static int read_record(char *buf, ulong len)
wdenk8bde7f72003-06-27 21:31:46 +0000211{
212 char *p;
213 char c;
214
215 --len; /* always leave room for terminating '\0' byte */
216
217 for (p=buf; p < buf+len; ++p) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200218 c = getchar(); /* read character */
wdenk8bde7f72003-06-27 21:31:46 +0000219 if (do_echo)
Kim Phillips088f1b12012-10-29 13:34:31 +0000220 putc(c); /* ... and echo it */
wdenk8bde7f72003-06-27 21:31:46 +0000221
222 switch (c) {
223 case '\r':
224 case '\n':
225 *p = '\0';
226 return (p - buf);
227 case '\0':
228 case 0x03: /* ^C - Control C */
229 return (-1);
230 default:
231 *p = c;
232 }
233
Michal Simeke7fcfef2020-12-01 13:58:28 +0100234 /* Check for the console hangup (if any different from serial) */
235 if (gd->jt->getc != getchar) {
236 if (ctrlc())
237 return (-1);
wdenk8bde7f72003-06-27 21:31:46 +0000238 }
wdenk8bde7f72003-06-27 21:31:46 +0000239 }
240
241 /* line too long - truncate */
242 *p = '\0';
243 return (p - buf);
244}
245
Jon Loeligerc76fe472007-07-08 18:02:23 -0500246#if defined(CONFIG_CMD_SAVES)
wdenk8bde7f72003-06-27 21:31:46 +0000247
Simon Glass09140112020-05-10 11:40:03 -0600248int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
249 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000250{
251 ulong offset = 0;
252 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200253#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000254 int save_baudrate, current_baudrate;
255
256 save_baudrate = current_baudrate = gd->baudrate;
257#endif
258
259 if (argc >= 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600260 offset = hextoul(argv[1], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000261 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200262#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000263 if (argc >= 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600264 size = hextoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000265 }
266 if (argc == 4) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600267 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000268
269 /* default to current baudrate */
270 if (save_baudrate == 0)
271 save_baudrate = current_baudrate;
272 }
273 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000274 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000275 save_baudrate);
276 udelay(50000);
277 gd->baudrate = save_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000278 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000279 udelay(50000);
280 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200281 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000282 break;
283 }
284 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200285#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000286 if (argc == 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600287 size = hextoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000288 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200289#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +0000290
Kim Phillips088f1b12012-10-29 13:34:31 +0000291 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk8bde7f72003-06-27 21:31:46 +0000292 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200293 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000294 break;
295 }
Kim Phillips088f1b12012-10-29 13:34:31 +0000296 if (save_serial(offset, size)) {
297 printf("## S-Record upload aborted\n");
wdenk8bde7f72003-06-27 21:31:46 +0000298 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000299 printf("## S-Record upload complete\n");
wdenk8bde7f72003-06-27 21:31:46 +0000300 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200301#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk8bde7f72003-06-27 21:31:46 +0000302 if (save_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000303 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000304 (int)current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000305 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000306 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000307 serial_setbrg();
308 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000309 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200310 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000311 break;
312 }
313 }
314#endif
315 return 0;
316}
317
318#define SREC3_START "S0030000FC\n"
319#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
320#define SREC3_END "S70500000000FA\n"
321#define SREC_BYTES_PER_RECORD 16
322
Kim Phillips088f1b12012-10-29 13:34:31 +0000323static int save_serial(ulong address, ulong count)
wdenk8bde7f72003-06-27 21:31:46 +0000324{
325 int i, c, reclen, checksum, length;
326 char *hex = "0123456789ABCDEF";
327 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
328 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
329
330 reclen = 0;
331 checksum = 0;
332
333 if(write_record(SREC3_START)) /* write the header */
334 return (-1);
335 do {
336 if(count) { /* collect hex data in the buffer */
337 c = *(volatile uchar*)(address + reclen); /* get one byte */
338 checksum += c; /* accumulate checksum */
339 data[2*reclen] = hex[(c>>4)&0x0f];
340 data[2*reclen+1] = hex[c & 0x0f];
341 data[2*reclen+2] = '\0';
342 ++reclen;
343 --count;
344 }
345 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
346 /* enough data collected for one record: dump it */
347 if(reclen) { /* build & write a data record: */
348 /* address + data + checksum */
349 length = 4 + reclen + 1;
350
351 /* accumulate length bytes into checksum */
352 for(i = 0; i < 2; i++)
353 checksum += (length >> (8*i)) & 0xff;
354
355 /* accumulate address bytes into checksum: */
356 for(i = 0; i < 4; i++)
357 checksum += (address >> (8*i)) & 0xff;
358
359 /* make proper checksum byte: */
360 checksum = ~checksum & 0xff;
361
362 /* output one record: */
363 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
364 if(write_record(record))
365 return (-1);
366 }
367 address += reclen; /* increment address */
368 checksum = 0;
369 reclen = 0;
370 }
371 }
372 while(count);
373 if(write_record(SREC3_END)) /* write the final record */
374 return (-1);
375 return(0);
376}
377
Kim Phillips088f1b12012-10-29 13:34:31 +0000378static int write_record(char *buf)
wdenk8bde7f72003-06-27 21:31:46 +0000379{
380 char c;
381
382 while((c = *buf++))
wdenk232c1502004-03-12 00:14:09 +0000383 putc(c);
wdenk8bde7f72003-06-27 21:31:46 +0000384
385 /* Check for the console hangup (if any different from serial) */
386
387 if (ctrlc()) {
388 return (-1);
389 }
390 return (0);
391}
Jon Loeliger90253172007-07-10 11:02:44 -0500392# endif
wdenk8bde7f72003-06-27 21:31:46 +0000393
Jon Loeliger90253172007-07-10 11:02:44 -0500394#endif
wdenk8bde7f72003-06-27 21:31:46 +0000395
396
Jon Loeligerc76fe472007-07-08 18:02:23 -0500397#if defined(CONFIG_CMD_LOADB)
Jon Loeliger65c450b2007-06-11 19:01:54 -0500398/*
399 * loadb command (load binary) included
400 */
wdenk8bde7f72003-06-27 21:31:46 +0000401#define XON_CHAR 17
402#define XOFF_CHAR 19
403#define START_CHAR 0x01
404#define ETX_CHAR 0x03
405#define END_CHAR 0x0D
406#define SPACE 0x20
407#define K_ESCAPE 0x23
408#define SEND_TYPE 'S'
409#define DATA_TYPE 'D'
410#define ACK_TYPE 'Y'
411#define NACK_TYPE 'N'
412#define BREAK_TYPE 'B'
413#define tochar(x) ((char) (((x) + SPACE) & 0xff))
414#define untochar(x) ((int) (((x) - SPACE) & 0xff))
415
wdenk8bde7f72003-06-27 21:31:46 +0000416static void set_kerm_bin_mode(unsigned long *);
417static int k_recv(void);
Kim Phillips088f1b12012-10-29 13:34:31 +0000418static ulong load_serial_bin(ulong offset);
wdenk8bde7f72003-06-27 21:31:46 +0000419
420
Kim Phillips088f1b12012-10-29 13:34:31 +0000421static char his_eol; /* character he needs at end of packet */
422static int his_pad_count; /* number of pad chars he needs */
423static char his_pad_char; /* pad chars he needs */
424static char his_quote; /* quote chars he'll use */
wdenk8bde7f72003-06-27 21:31:46 +0000425
Simon Glass09140112020-05-10 11:40:03 -0600426static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
427 char *const argv[])
wdenk8bde7f72003-06-27 21:31:46 +0000428{
wdenk8bde7f72003-06-27 21:31:46 +0000429 ulong offset = 0;
430 ulong addr;
431 int load_baudrate, current_baudrate;
432 int rcode = 0;
433 char *s;
434
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200435 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
436 offset = CONFIG_SYS_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000437
438 /* pre-set offset from $loadaddr */
Simon Glass00caae62017-08-03 12:22:12 -0600439 s = env_get("loadaddr");
440 if (s)
Simon Glass7e5f4602021-07-24 09:03:29 -0600441 offset = hextoul(s, NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000442
443 load_baudrate = current_baudrate = gd->baudrate;
444
445 if (argc >= 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600446 offset = hextoul(argv[1], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000447 }
448 if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600449 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk8bde7f72003-06-27 21:31:46 +0000450
451 /* default to current baudrate */
452 if (load_baudrate == 0)
453 load_baudrate = current_baudrate;
454 }
455
456 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000457 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000458 load_baudrate);
459 udelay(50000);
460 gd->baudrate = load_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000461 serial_setbrg();
wdenk8bde7f72003-06-27 21:31:46 +0000462 udelay(50000);
463 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200464 if (getchar() == '\r')
wdenk8bde7f72003-06-27 21:31:46 +0000465 break;
466 }
467 }
468
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200469 if (strcmp(argv[0],"loady")==0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000470 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200471 "to 0x%08lX at %d bps...\n",
472 offset,
473 load_baudrate);
wdenk8bde7f72003-06-27 21:31:46 +0000474
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600475 addr = load_serial_ymodem(offset, xyzModem_ymodem);
476
477 } else if (strcmp(argv[0],"loadx")==0) {
478 printf("## Ready for binary (xmodem) download "
479 "to 0x%08lX at %d bps...\n",
480 offset,
481 load_baudrate);
482
483 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200484
wdenk8bde7f72003-06-27 21:31:46 +0000485 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000486
Kim Phillips088f1b12012-10-29 13:34:31 +0000487 printf("## Ready for binary (kermit) download "
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200488 "to 0x%08lX at %d bps...\n",
489 offset,
490 load_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000491 addr = load_serial_bin(offset);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200492
493 if (addr == ~0) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700494 image_load_addr = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000495 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200496 rcode = 1;
497 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000498 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700499 image_load_addr = addr;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200500 }
501 }
wdenk8bde7f72003-06-27 21:31:46 +0000502 if (load_baudrate != current_baudrate) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000503 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk8bde7f72003-06-27 21:31:46 +0000504 current_baudrate);
Kim Phillips088f1b12012-10-29 13:34:31 +0000505 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000506 gd->baudrate = current_baudrate;
Kim Phillips088f1b12012-10-29 13:34:31 +0000507 serial_setbrg();
508 udelay(50000);
wdenk8bde7f72003-06-27 21:31:46 +0000509 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200510 if (getchar() == 0x1B) /* ESC */
wdenk8bde7f72003-06-27 21:31:46 +0000511 break;
512 }
513 }
514
wdenk8bde7f72003-06-27 21:31:46 +0000515 return rcode;
516}
517
518
Kim Phillips088f1b12012-10-29 13:34:31 +0000519static ulong load_serial_bin(ulong offset)
wdenk8bde7f72003-06-27 21:31:46 +0000520{
521 int size, i;
wdenk8bde7f72003-06-27 21:31:46 +0000522
Kim Phillips088f1b12012-10-29 13:34:31 +0000523 set_kerm_bin_mode((ulong *) offset);
524 size = k_recv();
wdenk8bde7f72003-06-27 21:31:46 +0000525
526 /*
527 * Gather any trailing characters (for instance, the ^D which
528 * is sent by 'cu' after sending a file), and give the
529 * box some time (100 * 1 ms)
530 */
531 for (i=0; i<100; ++i) {
wdenk232c1502004-03-12 00:14:09 +0000532 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200533 getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000534 }
535 udelay(1000);
536 }
537
Kim Phillips088f1b12012-10-29 13:34:31 +0000538 flush_cache(offset, size);
wdenk8bde7f72003-06-27 21:31:46 +0000539
540 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -0600541 env_set_hex("filesize", size);
wdenk8bde7f72003-06-27 21:31:46 +0000542
543 return offset;
544}
545
Kim Phillips088f1b12012-10-29 13:34:31 +0000546static void send_pad(void)
wdenk8bde7f72003-06-27 21:31:46 +0000547{
548 int count = his_pad_count;
549
550 while (count-- > 0)
Kim Phillips088f1b12012-10-29 13:34:31 +0000551 putc(his_pad_char);
wdenk8bde7f72003-06-27 21:31:46 +0000552}
553
554/* converts escaped kermit char to binary char */
Kim Phillips088f1b12012-10-29 13:34:31 +0000555static char ktrans(char in)
wdenk8bde7f72003-06-27 21:31:46 +0000556{
557 if ((in & 0x60) == 0x40) {
558 return (char) (in & ~0x40);
559 } else if ((in & 0x7f) == 0x3f) {
560 return (char) (in | 0x40);
561 } else
562 return in;
563}
564
Kim Phillips088f1b12012-10-29 13:34:31 +0000565static int chk1(char *buffer)
wdenk8bde7f72003-06-27 21:31:46 +0000566{
567 int total = 0;
568
569 while (*buffer) {
570 total += *buffer++;
571 }
572 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
573}
574
Kim Phillips088f1b12012-10-29 13:34:31 +0000575static void s1_sendpacket(char *packet)
wdenk8bde7f72003-06-27 21:31:46 +0000576{
Kim Phillips088f1b12012-10-29 13:34:31 +0000577 send_pad();
wdenk8bde7f72003-06-27 21:31:46 +0000578 while (*packet) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000579 putc(*packet++);
wdenk8bde7f72003-06-27 21:31:46 +0000580 }
581}
582
583static char a_b[24];
Kim Phillips088f1b12012-10-29 13:34:31 +0000584static void send_ack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000585{
586 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000587 a_b[1] = tochar(3);
588 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000589 a_b[3] = ACK_TYPE;
590 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000591 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000592 a_b[5] = his_eol;
593 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000594 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000595}
596
Kim Phillips088f1b12012-10-29 13:34:31 +0000597static void send_nack(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000598{
599 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000600 a_b[1] = tochar(3);
601 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000602 a_b[3] = NACK_TYPE;
603 a_b[4] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000604 a_b[4] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000605 a_b[5] = his_eol;
606 a_b[6] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000607 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000608}
609
610
Kim Phillips088f1b12012-10-29 13:34:31 +0000611static void (*os_data_init)(void);
612static void (*os_data_char)(char new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000613static int os_data_state, os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000614static char *os_data_addr, *os_data_addr_saved;
615static char *bin_start_address;
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530616
Kim Phillips088f1b12012-10-29 13:34:31 +0000617static void bin_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000618{
619 os_data_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000620 os_data_addr = bin_start_address;
621}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530622
Kim Phillips088f1b12012-10-29 13:34:31 +0000623static void os_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000624{
625 os_data_state_saved = os_data_state;
wdenk8bde7f72003-06-27 21:31:46 +0000626 os_data_addr_saved = os_data_addr;
627}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530628
Kim Phillips088f1b12012-10-29 13:34:31 +0000629static void os_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000630{
631 os_data_state = os_data_state_saved;
wdenk8bde7f72003-06-27 21:31:46 +0000632 os_data_addr = os_data_addr_saved;
633}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530634
Kim Phillips088f1b12012-10-29 13:34:31 +0000635static void bin_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000636{
637 switch (os_data_state) {
638 case 0: /* data */
639 *os_data_addr++ = new_char;
wdenk8bde7f72003-06-27 21:31:46 +0000640 break;
641 }
642}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530643
Kim Phillips088f1b12012-10-29 13:34:31 +0000644static void set_kerm_bin_mode(unsigned long *addr)
wdenk8bde7f72003-06-27 21:31:46 +0000645{
646 bin_start_address = (char *) addr;
647 os_data_init = bin_data_init;
648 os_data_char = bin_data_char;
649}
650
651
652/* k_data_* simply handles the kermit escape translations */
653static int k_data_escape, k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000654static void k_data_init(void)
wdenk8bde7f72003-06-27 21:31:46 +0000655{
656 k_data_escape = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000657 os_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000658}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530659
Kim Phillips088f1b12012-10-29 13:34:31 +0000660static void k_data_save(void)
wdenk8bde7f72003-06-27 21:31:46 +0000661{
662 k_data_escape_saved = k_data_escape;
Kim Phillips088f1b12012-10-29 13:34:31 +0000663 os_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000664}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530665
Kim Phillips088f1b12012-10-29 13:34:31 +0000666static void k_data_restore(void)
wdenk8bde7f72003-06-27 21:31:46 +0000667{
668 k_data_escape = k_data_escape_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000669 os_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000670}
Gururaja Hebbar K Ra9fe0c32008-08-07 13:13:27 +0530671
Kim Phillips088f1b12012-10-29 13:34:31 +0000672static void k_data_char(char new_char)
wdenk8bde7f72003-06-27 21:31:46 +0000673{
674 if (k_data_escape) {
675 /* last char was escape - translate this character */
Kim Phillips088f1b12012-10-29 13:34:31 +0000676 os_data_char(ktrans(new_char));
wdenk8bde7f72003-06-27 21:31:46 +0000677 k_data_escape = 0;
678 } else {
679 if (new_char == his_quote) {
680 /* this char is escape - remember */
681 k_data_escape = 1;
682 } else {
683 /* otherwise send this char as-is */
Kim Phillips088f1b12012-10-29 13:34:31 +0000684 os_data_char(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000685 }
686 }
687}
688
689#define SEND_DATA_SIZE 20
Kim Phillips088f1b12012-10-29 13:34:31 +0000690static char send_parms[SEND_DATA_SIZE];
691static char *send_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000692
693/* handle_send_packet interprits the protocol info and builds and
694 sends an appropriate ack for what we can do */
Kim Phillips088f1b12012-10-29 13:34:31 +0000695static void handle_send_packet(int n)
wdenk8bde7f72003-06-27 21:31:46 +0000696{
697 int length = 3;
698 int bytes;
699
700 /* initialize some protocol parameters */
701 his_eol = END_CHAR; /* default end of line character */
702 his_pad_count = 0;
703 his_pad_char = '\0';
704 his_quote = K_ESCAPE;
705
706 /* ignore last character if it filled the buffer */
707 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
708 --send_ptr;
709 bytes = send_ptr - send_parms; /* how many bytes we'll process */
710 do {
711 if (bytes-- <= 0)
712 break;
713 /* handle MAXL - max length */
714 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000715 a_b[++length] = tochar(94);
wdenk8bde7f72003-06-27 21:31:46 +0000716 if (bytes-- <= 0)
717 break;
718 /* handle TIME - time you should wait for my packets */
719 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillips088f1b12012-10-29 13:34:31 +0000720 a_b[++length] = tochar(1);
wdenk8bde7f72003-06-27 21:31:46 +0000721 if (bytes-- <= 0)
722 break;
723 /* handle NPAD - number of pad chars I need */
724 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000725 his_pad_count = untochar(send_parms[2]);
726 a_b[++length] = tochar(0);
wdenk8bde7f72003-06-27 21:31:46 +0000727 if (bytes-- <= 0)
728 break;
729 /* handle PADC - pad chars I need */
730 /* remember what he says - I need none */
Kim Phillips088f1b12012-10-29 13:34:31 +0000731 his_pad_char = ktrans(send_parms[3]);
wdenk8bde7f72003-06-27 21:31:46 +0000732 a_b[++length] = 0x40; /* He should ignore this */
733 if (bytes-- <= 0)
734 break;
735 /* handle EOL - end of line he needs */
736 /* remember what he says - I need CR */
Kim Phillips088f1b12012-10-29 13:34:31 +0000737 his_eol = untochar(send_parms[4]);
738 a_b[++length] = tochar(END_CHAR);
wdenk8bde7f72003-06-27 21:31:46 +0000739 if (bytes-- <= 0)
740 break;
741 /* handle QCTL - quote control char he'll use */
742 /* remember what he says - I'll use '#' */
743 his_quote = send_parms[5];
744 a_b[++length] = '#';
745 if (bytes-- <= 0)
746 break;
747 /* handle QBIN - 8-th bit prefixing */
748 /* ignore what he says - I refuse */
749 a_b[++length] = 'N';
750 if (bytes-- <= 0)
751 break;
752 /* handle CHKT - the clock check type */
753 /* ignore what he says - I do type 1 (for now) */
754 a_b[++length] = '1';
755 if (bytes-- <= 0)
756 break;
757 /* handle REPT - the repeat prefix */
758 /* ignore what he says - I refuse (for now) */
759 a_b[++length] = 'N';
760 if (bytes-- <= 0)
761 break;
762 /* handle CAPAS - the capabilities mask */
763 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillips088f1b12012-10-29 13:34:31 +0000764 a_b[++length] = tochar(2); /* only long packets */
765 a_b[++length] = tochar(0); /* no windows */
766 a_b[++length] = tochar(94); /* large packet msb */
767 a_b[++length] = tochar(94); /* large packet lsb */
wdenk8bde7f72003-06-27 21:31:46 +0000768 } while (0);
769
770 a_b[0] = START_CHAR;
Kim Phillips088f1b12012-10-29 13:34:31 +0000771 a_b[1] = tochar(length);
772 a_b[2] = tochar(n);
wdenk8bde7f72003-06-27 21:31:46 +0000773 a_b[3] = ACK_TYPE;
774 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000775 a_b[length] = tochar(chk1(&a_b[1]));
wdenk8bde7f72003-06-27 21:31:46 +0000776 a_b[++length] = his_eol;
777 a_b[++length] = '\0';
Kim Phillips088f1b12012-10-29 13:34:31 +0000778 s1_sendpacket(a_b);
wdenk8bde7f72003-06-27 21:31:46 +0000779}
780
781/* k_recv receives a OS Open image file over kermit line */
Kim Phillips088f1b12012-10-29 13:34:31 +0000782static int k_recv(void)
wdenk8bde7f72003-06-27 21:31:46 +0000783{
784 char new_char;
785 char k_state, k_state_saved;
786 int sum;
787 int done;
788 int length;
789 int n, last_n;
wdenk8bde7f72003-06-27 21:31:46 +0000790 int len_lo, len_hi;
791
792 /* initialize some protocol parameters */
793 his_eol = END_CHAR; /* default end of line character */
794 his_pad_count = 0;
795 his_pad_char = '\0';
796 his_quote = K_ESCAPE;
797
798 /* initialize the k_recv and k_data state machine */
799 done = 0;
800 k_state = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000801 k_data_init();
wdenk8bde7f72003-06-27 21:31:46 +0000802 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000803 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000804 n = 0; /* just to get rid of a warning */
805 last_n = -1;
806
807 /* expect this "type" sequence (but don't check):
808 S: send initiate
809 F: file header
810 D: data (multiple)
811 Z: end of file
812 B: break transmission
813 */
814
815 /* enter main loop */
816 while (!done) {
817 /* set the send packet pointer to begining of send packet parms */
818 send_ptr = send_parms;
819
820 /* With each packet, start summing the bytes starting with the length.
821 Save the current sequence number.
822 Note the type of the packet.
823 If a character less than SPACE (0x20) is received - error.
824 */
825
826#if 0
827 /* OLD CODE, Prior to checking sequence numbers */
828 /* first have all state machines save current states */
829 k_state_saved = k_state;
830 k_data_save ();
831#endif
832
833 /* get a packet */
834 /* wait for the starting character or ^C */
835 for (;;) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200836 switch (getchar()) {
wdenk8bde7f72003-06-27 21:31:46 +0000837 case START_CHAR: /* start packet */
838 goto START;
839 case ETX_CHAR: /* ^C waiting for packet */
840 return (0);
841 default:
842 ;
843 }
844 }
845START:
846 /* get length of packet */
847 sum = 0;
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200848 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000849 if ((new_char & 0xE0) == 0)
850 goto packet_error;
851 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000852 length = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000853 /* get sequence number */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200854 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000855 if ((new_char & 0xE0) == 0)
856 goto packet_error;
857 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000858 n = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000859 --length;
860
861 /* NEW CODE - check sequence numbers for retried packets */
862 /* Note - this new code assumes that the sequence number is correctly
863 * received. Handling an invalid sequence number adds another layer
864 * of complexity that may not be needed - yet! At this time, I'm hoping
865 * that I don't need to buffer the incoming data packets and can write
866 * the data into memory in real time.
867 */
868 if (n == last_n) {
869 /* same sequence number, restore the previous state */
870 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000871 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000872 } else {
873 /* new sequence number, checkpoint the download */
874 last_n = n;
875 k_state_saved = k_state;
Kim Phillips088f1b12012-10-29 13:34:31 +0000876 k_data_save();
wdenk8bde7f72003-06-27 21:31:46 +0000877 }
878 /* END NEW CODE */
879
880 /* get packet type */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200881 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000882 if ((new_char & 0xE0) == 0)
883 goto packet_error;
884 sum += new_char & 0xff;
885 k_state = new_char;
886 --length;
887 /* check for extended length */
888 if (length == -2) {
889 /* (length byte was 0, decremented twice) */
890 /* get the two length bytes */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200891 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000892 if ((new_char & 0xE0) == 0)
893 goto packet_error;
894 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000895 len_hi = untochar(new_char);
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200896 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000897 if ((new_char & 0xE0) == 0)
898 goto packet_error;
899 sum += new_char & 0xff;
Kim Phillips088f1b12012-10-29 13:34:31 +0000900 len_lo = untochar(new_char);
wdenk8bde7f72003-06-27 21:31:46 +0000901 length = len_hi * 95 + len_lo;
902 /* check header checksum */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200903 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000904 if ((new_char & 0xE0) == 0)
905 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000906 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000907 goto packet_error;
908 sum += new_char & 0xff;
909/* --length; */ /* new length includes only data and block check to come */
910 }
911 /* bring in rest of packet */
912 while (length > 1) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200913 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000914 if ((new_char & 0xE0) == 0)
915 goto packet_error;
916 sum += new_char & 0xff;
917 --length;
918 if (k_state == DATA_TYPE) {
919 /* pass on the data if this is a data packet */
920 k_data_char (new_char);
921 } else if (k_state == SEND_TYPE) {
922 /* save send pack in buffer as is */
923 *send_ptr++ = new_char;
924 /* if too much data, back off the pointer */
925 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
926 --send_ptr;
927 }
928 }
929 /* get and validate checksum character */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200930 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000931 if ((new_char & 0xE0) == 0)
932 goto packet_error;
Kim Phillips088f1b12012-10-29 13:34:31 +0000933 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk8bde7f72003-06-27 21:31:46 +0000934 goto packet_error;
935 /* get END_CHAR */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200936 new_char = getchar();
wdenk8bde7f72003-06-27 21:31:46 +0000937 if (new_char != END_CHAR) {
938 packet_error:
939 /* restore state machines */
940 k_state = k_state_saved;
Kim Phillips088f1b12012-10-29 13:34:31 +0000941 k_data_restore();
wdenk8bde7f72003-06-27 21:31:46 +0000942 /* send a negative acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000943 send_nack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000944 } else if (k_state == SEND_TYPE) {
945 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillips088f1b12012-10-29 13:34:31 +0000946 handle_send_packet(n);
wdenk8bde7f72003-06-27 21:31:46 +0000947 } else {
948 /* send simple acknowledge packet in */
Kim Phillips088f1b12012-10-29 13:34:31 +0000949 send_ack(n);
wdenk8bde7f72003-06-27 21:31:46 +0000950 /* quit if end of transmission */
951 if (k_state == BREAK_TYPE)
952 done = 1;
953 }
wdenk8bde7f72003-06-27 21:31:46 +0000954 }
955 return ((ulong) os_data_addr - (ulong) bin_start_address);
956}
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200957
958static int getcxmodem(void) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200959 if (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200960 return (getchar());
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200961 return -1;
962}
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600963static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200964{
965 int size;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200966 int err;
967 int res;
968 connection_info_t info;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200969 char ymodemBuf[1024];
970 ulong store_addr = ~0;
971 ulong addr = 0;
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200972
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200973 size = 0;
Angus Ainslie6e66bd52013-06-26 16:54:24 -0600974 info.mode = mode;
Kim Phillips088f1b12012-10-29 13:34:31 +0000975 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200976 if (!res) {
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200977
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200978 while ((res =
Kim Phillips088f1b12012-10-29 13:34:31 +0000979 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200980 store_addr = addr + offset;
981 size += res;
982 addr += res;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900983#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillips088f1b12012-10-29 13:34:31 +0000984 if (addr2info(store_addr)) {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200985 int rc;
986
Kim Phillips088f1b12012-10-29 13:34:31 +0000987 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200988 store_addr, res);
989 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700990 flash_perror(rc);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200991 return (~0);
992 }
993 } else
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +0200994#endif
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200995 {
Kim Phillips088f1b12012-10-29 13:34:31 +0000996 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200997 res);
998 }
999
1000 }
Heinrich Schuchardt76b0a192021-03-19 02:50:57 +00001001 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1002 efi_set_bootdev("Uart", "", "",
1003 map_sysmem(offset, 0), size);
1004
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001005 } else {
Kim Phillips088f1b12012-10-29 13:34:31 +00001006 printf("%s\n", xyzModem_error(err));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001007 }
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02001008
Kim Phillips088f1b12012-10-29 13:34:31 +00001009 xyzModem_stream_close(&err);
1010 xyzModem_stream_terminate(false, &getcxmodem);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001011
1012
Chris Packham0a6036d2016-10-25 20:22:48 +13001013 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001014
Kim Phillips088f1b12012-10-29 13:34:31 +00001015 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass018f5302017-08-03 12:22:10 -06001016 env_set_hex("filesize", size);
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001017
1018 return offset;
1019}
1020
Jon Loeliger90253172007-07-10 11:02:44 -05001021#endif
wdenk8bde7f72003-06-27 21:31:46 +00001022
1023/* -------------------------------------------------------------------- */
1024
Jon Loeligerc76fe472007-07-08 18:02:23 -05001025#if defined(CONFIG_CMD_LOADS)
wdenk8bde7f72003-06-27 21:31:46 +00001026
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001027#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001028U_BOOT_CMD(
1029 loads, 3, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001030 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001031 "[ off ] [ baud ]\n"
1032 " - load S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001033 " with offset 'off' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001034);
1035
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001036#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001037U_BOOT_CMD(
1038 loads, 2, 0, do_load_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001039 "load S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001040 "[ off ]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001041 " - load S-Record file over serial line with offset 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001042);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001043#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk8bde7f72003-06-27 21:31:46 +00001044
1045/*
1046 * SAVES always requires LOADS support, but not vice versa
1047 */
1048
1049
Jon Loeligerc76fe472007-07-08 18:02:23 -05001050#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001051#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk0d498392003-07-01 21:06:45 +00001052U_BOOT_CMD(
1053 saves, 4, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001054 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001055 "[ off ] [size] [ baud ]\n"
1056 " - save S-Record file over serial line"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001057 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001058);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001059#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk0d498392003-07-01 21:06:45 +00001060U_BOOT_CMD(
1061 saves, 3, 0, do_save_serial,
Peter Tyser2fb26042009-01-27 18:03:12 -06001062 "save S-Record file over serial line",
wdenk8bde7f72003-06-27 21:31:46 +00001063 "[ off ] [size]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001064 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk8bde7f72003-06-27 21:31:46 +00001065);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001066#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001067#endif /* CONFIG_CMD_SAVES */
1068#endif /* CONFIG_CMD_LOADS */
wdenk8bde7f72003-06-27 21:31:46 +00001069
1070
Jon Loeligerc76fe472007-07-08 18:02:23 -05001071#if defined(CONFIG_CMD_LOADB)
wdenk0d498392003-07-01 21:06:45 +00001072U_BOOT_CMD(
1073 loadb, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001074 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001075 "[ addr [ baud ] ]\n"
wdenk8bde7f72003-06-27 21:31:46 +00001076 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001077 " at address 'addr' with baudrate 'baud'"
wdenk8bde7f72003-06-27 21:31:46 +00001078);
1079
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001080U_BOOT_CMD(
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001081 loadx, 3, 0, do_load_serial_bin,
1082 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001083 "[ addr [ baud ] ]\n"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001084 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001085 " at address 'addr' with baudrate 'baud'"
Angus Ainslie6e66bd52013-06-26 16:54:24 -06001086);
1087
1088U_BOOT_CMD(
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001089 loady, 3, 0, do_load_serial_bin,
Peter Tyser2fb26042009-01-27 18:03:12 -06001090 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001091 "[ addr [ baud ] ]\n"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001092 " - load binary file over serial line"
Heinrich Schuchardtb9112082021-01-22 13:16:04 +01001093 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecherf2841d32006-03-30 13:40:55 +02001094);
1095
Robert P. J. Day70d7cb92013-02-03 05:46:13 +00001096#endif /* CONFIG_CMD_LOADB */