blob: 249ebd4ae078a267848bfcf54c0a881eae31fc48 [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 */
10#include <common.h>
11#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000014#include <efi_loader.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000016#include <exports.h>
Simon Glassa606ffc2019-12-28 10:44:40 -070017#include <flash.h>
Simon Glass85f13782019-12-28 10:45:03 -070018#include <image.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000019#include <mapmem.h>
wdenk57b2d802003-06-27 21:31:46 +000020#include <net.h>
Heinrich Schuchardta89adb02021-03-19 02:50:57 +000021#include <s_record.h>
Simon Glass36736182019-11-14 12:57:24 -070022#include <serial.h>
Markus Klotzbuecher387f5412006-03-30 13:40:55 +020023#include <xyzModem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060024#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
wdenk57b2d802003-06-27 21:31:46 +000027
Wolfgang Denk6405a152006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
wdenk57b2d802003-06-27 21:31:46 +000029
Jon Loeligerd76b5c12007-07-08 18:02:23 -050030#if defined(CONFIG_CMD_LOADB)
Angus Ainslief283bd02013-06-26 16:54:24 -060031static ulong load_serial_ymodem(ulong offset, int mode);
Wolfgang Denkda916932006-05-02 00:11:25 +020032#endif
33
Jon Loeligerd76b5c12007-07-08 18:02:23 -050034#if defined(CONFIG_CMD_LOADS)
Kim Phillipsdc00a682012-10-29 13:34:31 +000035static ulong load_serial(long offset);
36static int read_record(char *buf, ulong len);
Jon Loeligerd76b5c12007-07-08 18:02:23 -050037# if defined(CONFIG_CMD_SAVES)
Kim Phillipsdc00a682012-10-29 13:34:31 +000038static int save_serial(ulong offset, ulong size);
39static int write_record(char *buf);
Jon Loeligerd704d912007-07-10 11:02:44 -050040#endif
wdenk57b2d802003-06-27 21:31:46 +000041
42static int do_echo = 1;
Jon Loeligerd704d912007-07-10 11:02:44 -050043#endif
wdenk57b2d802003-06-27 21:31:46 +000044
45/* -------------------------------------------------------------------- */
46
Jon Loeligerd76b5c12007-07-08 18:02:23 -050047#if defined(CONFIG_CMD_LOADS)
Simon Glassed38aef2020-05-10 11:40:03 -060048static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
49 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +000050{
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020051 long offset = 0;
wdenk57b2d802003-06-27 21:31:46 +000052 ulong addr;
53 int i;
54 char *env_echo;
55 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020056#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000057 int load_baudrate, current_baudrate;
58
59 load_baudrate = current_baudrate = gd->baudrate;
60#endif
61
Simon Glass64b723f2017-08-03 12:22:12 -060062 env_echo = env_get("loads_echo");
63 if (env_echo && *env_echo == '1')
wdenk57b2d802003-06-27 21:31:46 +000064 do_echo = 1;
Simon Glass64b723f2017-08-03 12:22:12 -060065 else
wdenk57b2d802003-06-27 21:31:46 +000066 do_echo = 0;
wdenk57b2d802003-06-27 21:31:46 +000067
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +000069 if (argc >= 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020070 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000071 }
72 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -060073 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +000080 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +000081 load_baudrate);
82 udelay(50000);
83 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +000084 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +000085 udelay(50000);
86 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020087 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +000088 break;
89 }
90 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020091#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000092 if (argc == 2) {
Ricardo Ribalda Delgado9cde8302008-07-30 12:39:29 +020093 offset = simple_strtol(argv[1], NULL, 16);
wdenk57b2d802003-06-27 21:31:46 +000094 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +000096
Kim Phillipsdc00a682012-10-29 13:34:31 +000097 printf("## Ready for S-Record download ...\n");
wdenk57b2d802003-06-27 21:31:46 +000098
Kim Phillipsdc00a682012-10-29 13:34:31 +000099 addr = load_serial(offset);
wdenk57b2d802003-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) {
wdenk29e7f5a2004-03-12 00:14:09 +0000107 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200108 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000109 }
110 udelay(1000);
111 }
112
113 if (addr == ~0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000114 printf("## S-Record download aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000115 rcode = 1;
116 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000117 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700118 image_load_addr = addr;
wdenk57b2d802003-06-27 21:31:46 +0000119 }
120
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000122 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000123 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000124 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000125 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000126 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000127 serial_setbrg();
128 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000129 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200130 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000131 break;
132 }
133 }
134#endif
135 return rcode;
136}
137
Kim Phillipsdc00a682012-10-29 13:34:31 +0000138static ulong load_serial(long offset)
wdenk57b2d802003-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 */
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000152 type = srec_decode(record, &binlen, &addr, binbuf);
wdenk57b2d802003-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 Yamada8cea9b52017-02-11 22:43:54 +0900163#ifdef CONFIG_MTD_NOR_FLASH
wdenk57b2d802003-06-27 21:31:46 +0000164 if (addr2info(store_addr)) {
165 int rc;
166
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200167 rc = flash_write((char *)binbuf,store_addr,binlen);
wdenk57b2d802003-06-27 21:31:46 +0000168 if (rc != 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000169 flash_perror(rc);
wdenk57b2d802003-06-27 21:31:46 +0000170 return (~0);
171 }
172 } else
173#endif
174 {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000175 memcpy((char *)(store_addr), binbuf, binlen);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000185 udelay(10000);
wdenk57b2d802003-06-27 21:31:46 +0000186 size = end_addr - start_addr + 1;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000187 printf("\n"
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000193 flush_cache(start_addr, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600194 env_set_hex("filesize", size);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000203 putc('.');
wdenk57b2d802003-06-27 21:31:46 +0000204 }
205 }
206
207 return (~0); /* Download aborted */
208}
209
Kim Phillipsdc00a682012-10-29 13:34:31 +0000210static int read_record(char *buf, ulong len)
wdenk57b2d802003-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 Schuchardtc4954fb2020-10-07 18:11:48 +0200218 c = getchar(); /* read character */
wdenk57b2d802003-06-27 21:31:46 +0000219 if (do_echo)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000220 putc(c); /* ... and echo it */
wdenk57b2d802003-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 Simekb6e8ba12020-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);
wdenk57b2d802003-06-27 21:31:46 +0000238 }
wdenk57b2d802003-06-27 21:31:46 +0000239 }
240
241 /* line too long - truncate */
242 *p = '\0';
243 return (p - buf);
244}
245
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500246#if defined(CONFIG_CMD_SAVES)
wdenk57b2d802003-06-27 21:31:46 +0000247
Simon Glassed38aef2020-05-10 11:40:03 -0600248int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,
249 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000250{
251 ulong offset = 0;
252 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200253#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-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 Glass3ff49ec2021-07-24 09:03:29 -0600260 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000261 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200262#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000263 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600264 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000265 }
266 if (argc == 4) {
Simon Glassff9b9032021-07-24 09:03:30 -0600267 save_baudrate = (int)dectoul(argv[3], NULL);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000274 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000275 save_baudrate);
276 udelay(50000);
277 gd->baudrate = save_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000278 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000279 udelay(50000);
280 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200281 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000282 break;
283 }
284 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200285#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000286 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600287 size = hextoul(argv[2], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000288 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200289#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +0000290
Kim Phillipsdc00a682012-10-29 13:34:31 +0000291 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
wdenk57b2d802003-06-27 21:31:46 +0000292 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200293 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000294 break;
295 }
Kim Phillipsdc00a682012-10-29 13:34:31 +0000296 if (save_serial(offset, size)) {
297 printf("## S-Record upload aborted\n");
wdenk57b2d802003-06-27 21:31:46 +0000298 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000299 printf("## S-Record upload complete\n");
wdenk57b2d802003-06-27 21:31:46 +0000300 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200301#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenk57b2d802003-06-27 21:31:46 +0000302 if (save_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000303 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000304 (int)current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000305 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000306 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000307 serial_setbrg();
308 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000309 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200310 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000323static int save_serial(ulong address, ulong count)
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000378static int write_record(char *buf)
wdenk57b2d802003-06-27 21:31:46 +0000379{
380 char c;
381
382 while((c = *buf++))
wdenk29e7f5a2004-03-12 00:14:09 +0000383 putc(c);
wdenk57b2d802003-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 Loeligerd704d912007-07-10 11:02:44 -0500392# endif
wdenk57b2d802003-06-27 21:31:46 +0000393
Jon Loeligerd704d912007-07-10 11:02:44 -0500394#endif
wdenk57b2d802003-06-27 21:31:46 +0000395
396
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500397#if defined(CONFIG_CMD_LOADB)
Jon Loeliger3de8b242007-06-11 19:01:54 -0500398/*
399 * loadb command (load binary) included
400 */
wdenk57b2d802003-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
wdenk57b2d802003-06-27 21:31:46 +0000416static void set_kerm_bin_mode(unsigned long *);
417static int k_recv(void);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000418static ulong load_serial_bin(ulong offset);
wdenk57b2d802003-06-27 21:31:46 +0000419
420
Kim Phillipsdc00a682012-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 */
wdenk57b2d802003-06-27 21:31:46 +0000425
Simon Glassed38aef2020-05-10 11:40:03 -0600426static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
427 char *const argv[])
wdenk57b2d802003-06-27 21:31:46 +0000428{
wdenk57b2d802003-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-VILLARD03836942008-10-16 15:01:15 +0200435 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
436 offset = CONFIG_SYS_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000437
438 /* pre-set offset from $loadaddr */
Simon Glass64b723f2017-08-03 12:22:12 -0600439 s = env_get("loadaddr");
440 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600441 offset = hextoul(s, NULL);
wdenk57b2d802003-06-27 21:31:46 +0000442
443 load_baudrate = current_baudrate = gd->baudrate;
444
445 if (argc >= 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600446 offset = hextoul(argv[1], NULL);
wdenk57b2d802003-06-27 21:31:46 +0000447 }
448 if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600449 load_baudrate = (int)dectoul(argv[2], NULL);
wdenk57b2d802003-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 Phillipsdc00a682012-10-29 13:34:31 +0000457 printf("## Switch baudrate to %d bps and press ENTER ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000458 load_baudrate);
459 udelay(50000);
460 gd->baudrate = load_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000461 serial_setbrg();
wdenk57b2d802003-06-27 21:31:46 +0000462 udelay(50000);
463 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200464 if (getchar() == '\r')
wdenk57b2d802003-06-27 21:31:46 +0000465 break;
466 }
467 }
468
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200469 if (strcmp(argv[0],"loady")==0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000470 printf("## Ready for binary (ymodem) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200471 "to 0x%08lX at %d bps...\n",
472 offset,
473 load_baudrate);
wdenk57b2d802003-06-27 21:31:46 +0000474
Angus Ainslief283bd02013-06-26 16:54:24 -0600475 addr = load_serial_ymodem(offset, xyzModem_ymodem);
476
Pali Roháre37155f2021-08-03 16:28:43 +0200477 if (addr == ~0) {
478 image_load_addr = 0;
479 printf("## Binary (ymodem) download aborted\n");
480 rcode = 1;
481 } else {
482 printf("## Start Addr = 0x%08lX\n", addr);
483 image_load_addr = addr;
484 }
Angus Ainslief283bd02013-06-26 16:54:24 -0600485 } else if (strcmp(argv[0],"loadx")==0) {
486 printf("## Ready for binary (xmodem) download "
487 "to 0x%08lX at %d bps...\n",
488 offset,
489 load_baudrate);
490
491 addr = load_serial_ymodem(offset, xyzModem_xmodem);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200492
Pali Roháre37155f2021-08-03 16:28:43 +0200493 if (addr == ~0) {
494 image_load_addr = 0;
495 printf("## Binary (xmodem) download aborted\n");
496 rcode = 1;
497 } else {
498 printf("## Start Addr = 0x%08lX\n", addr);
499 image_load_addr = addr;
500 }
wdenk57b2d802003-06-27 21:31:46 +0000501 } else {
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200502
Kim Phillipsdc00a682012-10-29 13:34:31 +0000503 printf("## Ready for binary (kermit) download "
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200504 "to 0x%08lX at %d bps...\n",
505 offset,
506 load_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000507 addr = load_serial_bin(offset);
wdenk57b2d802003-06-27 21:31:46 +0000508
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200509 if (addr == ~0) {
Simon Glass892265d2019-12-28 10:45:02 -0700510 image_load_addr = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000511 printf("## Binary (kermit) download aborted\n");
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200512 rcode = 1;
513 } else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000514 printf("## Start Addr = 0x%08lX\n", addr);
Simon Glass892265d2019-12-28 10:45:02 -0700515 image_load_addr = addr;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200516 }
517 }
wdenk57b2d802003-06-27 21:31:46 +0000518 if (load_baudrate != current_baudrate) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000519 printf("## Switch baudrate to %d bps and press ESC ...\n",
wdenk57b2d802003-06-27 21:31:46 +0000520 current_baudrate);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000521 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000522 gd->baudrate = current_baudrate;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000523 serial_setbrg();
524 udelay(50000);
wdenk57b2d802003-06-27 21:31:46 +0000525 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200526 if (getchar() == 0x1B) /* ESC */
wdenk57b2d802003-06-27 21:31:46 +0000527 break;
528 }
529 }
530
wdenk57b2d802003-06-27 21:31:46 +0000531 return rcode;
532}
533
534
Kim Phillipsdc00a682012-10-29 13:34:31 +0000535static ulong load_serial_bin(ulong offset)
wdenk57b2d802003-06-27 21:31:46 +0000536{
537 int size, i;
wdenk57b2d802003-06-27 21:31:46 +0000538
Kim Phillipsdc00a682012-10-29 13:34:31 +0000539 set_kerm_bin_mode((ulong *) offset);
540 size = k_recv();
wdenk57b2d802003-06-27 21:31:46 +0000541
542 /*
543 * Gather any trailing characters (for instance, the ^D which
544 * is sent by 'cu' after sending a file), and give the
545 * box some time (100 * 1 ms)
546 */
547 for (i=0; i<100; ++i) {
wdenk29e7f5a2004-03-12 00:14:09 +0000548 if (tstc()) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200549 getchar();
wdenk57b2d802003-06-27 21:31:46 +0000550 }
551 udelay(1000);
552 }
553
Pali Rohár8398dd32021-08-06 18:07:39 +0200554 if (size == 0)
555 return ~0; /* Download aborted */
556
Kim Phillipsdc00a682012-10-29 13:34:31 +0000557 flush_cache(offset, size);
wdenk57b2d802003-06-27 21:31:46 +0000558
559 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -0600560 env_set_hex("filesize", size);
wdenk57b2d802003-06-27 21:31:46 +0000561
562 return offset;
563}
564
Kim Phillipsdc00a682012-10-29 13:34:31 +0000565static void send_pad(void)
wdenk57b2d802003-06-27 21:31:46 +0000566{
567 int count = his_pad_count;
568
569 while (count-- > 0)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000570 putc(his_pad_char);
wdenk57b2d802003-06-27 21:31:46 +0000571}
572
573/* converts escaped kermit char to binary char */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000574static char ktrans(char in)
wdenk57b2d802003-06-27 21:31:46 +0000575{
576 if ((in & 0x60) == 0x40) {
577 return (char) (in & ~0x40);
578 } else if ((in & 0x7f) == 0x3f) {
579 return (char) (in | 0x40);
580 } else
581 return in;
582}
583
Kim Phillipsdc00a682012-10-29 13:34:31 +0000584static int chk1(char *buffer)
wdenk57b2d802003-06-27 21:31:46 +0000585{
586 int total = 0;
587
588 while (*buffer) {
589 total += *buffer++;
590 }
591 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
592}
593
Kim Phillipsdc00a682012-10-29 13:34:31 +0000594static void s1_sendpacket(char *packet)
wdenk57b2d802003-06-27 21:31:46 +0000595{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000596 send_pad();
wdenk57b2d802003-06-27 21:31:46 +0000597 while (*packet) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000598 putc(*packet++);
wdenk57b2d802003-06-27 21:31:46 +0000599 }
600}
601
602static char a_b[24];
Kim Phillipsdc00a682012-10-29 13:34:31 +0000603static void send_ack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000604{
605 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000606 a_b[1] = tochar(3);
607 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000608 a_b[3] = ACK_TYPE;
609 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000610 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000611 a_b[5] = his_eol;
612 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000613 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000614}
615
Kim Phillipsdc00a682012-10-29 13:34:31 +0000616static void send_nack(int n)
wdenk57b2d802003-06-27 21:31:46 +0000617{
618 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000619 a_b[1] = tochar(3);
620 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000621 a_b[3] = NACK_TYPE;
622 a_b[4] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000623 a_b[4] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000624 a_b[5] = his_eol;
625 a_b[6] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000626 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000627}
628
629
Kim Phillipsdc00a682012-10-29 13:34:31 +0000630static void (*os_data_init)(void);
631static void (*os_data_char)(char new_char);
wdenk57b2d802003-06-27 21:31:46 +0000632static int os_data_state, os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000633static char *os_data_addr, *os_data_addr_saved;
634static char *bin_start_address;
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530635
Kim Phillipsdc00a682012-10-29 13:34:31 +0000636static void bin_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000637{
638 os_data_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000639 os_data_addr = bin_start_address;
640}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530641
Kim Phillipsdc00a682012-10-29 13:34:31 +0000642static void os_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000643{
644 os_data_state_saved = os_data_state;
wdenk57b2d802003-06-27 21:31:46 +0000645 os_data_addr_saved = os_data_addr;
646}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530647
Kim Phillipsdc00a682012-10-29 13:34:31 +0000648static void os_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000649{
650 os_data_state = os_data_state_saved;
wdenk57b2d802003-06-27 21:31:46 +0000651 os_data_addr = os_data_addr_saved;
652}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530653
Kim Phillipsdc00a682012-10-29 13:34:31 +0000654static void bin_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000655{
656 switch (os_data_state) {
657 case 0: /* data */
658 *os_data_addr++ = new_char;
wdenk57b2d802003-06-27 21:31:46 +0000659 break;
660 }
661}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530662
Kim Phillipsdc00a682012-10-29 13:34:31 +0000663static void set_kerm_bin_mode(unsigned long *addr)
wdenk57b2d802003-06-27 21:31:46 +0000664{
665 bin_start_address = (char *) addr;
666 os_data_init = bin_data_init;
667 os_data_char = bin_data_char;
668}
669
670
671/* k_data_* simply handles the kermit escape translations */
672static int k_data_escape, k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000673static void k_data_init(void)
wdenk57b2d802003-06-27 21:31:46 +0000674{
675 k_data_escape = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000676 os_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000677}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530678
Kim Phillipsdc00a682012-10-29 13:34:31 +0000679static void k_data_save(void)
wdenk57b2d802003-06-27 21:31:46 +0000680{
681 k_data_escape_saved = k_data_escape;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000682 os_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000683}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530684
Kim Phillipsdc00a682012-10-29 13:34:31 +0000685static void k_data_restore(void)
wdenk57b2d802003-06-27 21:31:46 +0000686{
687 k_data_escape = k_data_escape_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000688 os_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000689}
Gururaja Hebbar K Rb4d7eb22008-08-07 13:13:27 +0530690
Kim Phillipsdc00a682012-10-29 13:34:31 +0000691static void k_data_char(char new_char)
wdenk57b2d802003-06-27 21:31:46 +0000692{
693 if (k_data_escape) {
694 /* last char was escape - translate this character */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000695 os_data_char(ktrans(new_char));
wdenk57b2d802003-06-27 21:31:46 +0000696 k_data_escape = 0;
697 } else {
698 if (new_char == his_quote) {
699 /* this char is escape - remember */
700 k_data_escape = 1;
701 } else {
702 /* otherwise send this char as-is */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000703 os_data_char(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000704 }
705 }
706}
707
708#define SEND_DATA_SIZE 20
Kim Phillipsdc00a682012-10-29 13:34:31 +0000709static char send_parms[SEND_DATA_SIZE];
710static char *send_ptr;
wdenk57b2d802003-06-27 21:31:46 +0000711
712/* handle_send_packet interprits the protocol info and builds and
713 sends an appropriate ack for what we can do */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000714static void handle_send_packet(int n)
wdenk57b2d802003-06-27 21:31:46 +0000715{
716 int length = 3;
717 int bytes;
718
719 /* initialize some protocol parameters */
720 his_eol = END_CHAR; /* default end of line character */
721 his_pad_count = 0;
722 his_pad_char = '\0';
723 his_quote = K_ESCAPE;
724
725 /* ignore last character if it filled the buffer */
726 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
727 --send_ptr;
728 bytes = send_ptr - send_parms; /* how many bytes we'll process */
729 do {
730 if (bytes-- <= 0)
731 break;
732 /* handle MAXL - max length */
733 /* ignore what he says - most I'll take (here) is 94 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000734 a_b[++length] = tochar(94);
wdenk57b2d802003-06-27 21:31:46 +0000735 if (bytes-- <= 0)
736 break;
737 /* handle TIME - time you should wait for my packets */
738 /* ignore what he says - don't wait for my ack longer than 1 second */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000739 a_b[++length] = tochar(1);
wdenk57b2d802003-06-27 21:31:46 +0000740 if (bytes-- <= 0)
741 break;
742 /* handle NPAD - number of pad chars I need */
743 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000744 his_pad_count = untochar(send_parms[2]);
745 a_b[++length] = tochar(0);
wdenk57b2d802003-06-27 21:31:46 +0000746 if (bytes-- <= 0)
747 break;
748 /* handle PADC - pad chars I need */
749 /* remember what he says - I need none */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000750 his_pad_char = ktrans(send_parms[3]);
wdenk57b2d802003-06-27 21:31:46 +0000751 a_b[++length] = 0x40; /* He should ignore this */
752 if (bytes-- <= 0)
753 break;
754 /* handle EOL - end of line he needs */
755 /* remember what he says - I need CR */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000756 his_eol = untochar(send_parms[4]);
757 a_b[++length] = tochar(END_CHAR);
wdenk57b2d802003-06-27 21:31:46 +0000758 if (bytes-- <= 0)
759 break;
760 /* handle QCTL - quote control char he'll use */
761 /* remember what he says - I'll use '#' */
762 his_quote = send_parms[5];
763 a_b[++length] = '#';
764 if (bytes-- <= 0)
765 break;
766 /* handle QBIN - 8-th bit prefixing */
767 /* ignore what he says - I refuse */
768 a_b[++length] = 'N';
769 if (bytes-- <= 0)
770 break;
771 /* handle CHKT - the clock check type */
772 /* ignore what he says - I do type 1 (for now) */
773 a_b[++length] = '1';
774 if (bytes-- <= 0)
775 break;
776 /* handle REPT - the repeat prefix */
777 /* ignore what he says - I refuse (for now) */
778 a_b[++length] = 'N';
779 if (bytes-- <= 0)
780 break;
781 /* handle CAPAS - the capabilities mask */
782 /* ignore what he says - I only do long packets - I don't do windows */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000783 a_b[++length] = tochar(2); /* only long packets */
784 a_b[++length] = tochar(0); /* no windows */
785 a_b[++length] = tochar(94); /* large packet msb */
786 a_b[++length] = tochar(94); /* large packet lsb */
wdenk57b2d802003-06-27 21:31:46 +0000787 } while (0);
788
789 a_b[0] = START_CHAR;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000790 a_b[1] = tochar(length);
791 a_b[2] = tochar(n);
wdenk57b2d802003-06-27 21:31:46 +0000792 a_b[3] = ACK_TYPE;
793 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000794 a_b[length] = tochar(chk1(&a_b[1]));
wdenk57b2d802003-06-27 21:31:46 +0000795 a_b[++length] = his_eol;
796 a_b[++length] = '\0';
Kim Phillipsdc00a682012-10-29 13:34:31 +0000797 s1_sendpacket(a_b);
wdenk57b2d802003-06-27 21:31:46 +0000798}
799
800/* k_recv receives a OS Open image file over kermit line */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000801static int k_recv(void)
wdenk57b2d802003-06-27 21:31:46 +0000802{
803 char new_char;
804 char k_state, k_state_saved;
805 int sum;
806 int done;
807 int length;
808 int n, last_n;
wdenk57b2d802003-06-27 21:31:46 +0000809 int len_lo, len_hi;
810
811 /* initialize some protocol parameters */
812 his_eol = END_CHAR; /* default end of line character */
813 his_pad_count = 0;
814 his_pad_char = '\0';
815 his_quote = K_ESCAPE;
816
817 /* initialize the k_recv and k_data state machine */
818 done = 0;
819 k_state = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000820 k_data_init();
wdenk57b2d802003-06-27 21:31:46 +0000821 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000822 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000823 n = 0; /* just to get rid of a warning */
824 last_n = -1;
825
826 /* expect this "type" sequence (but don't check):
827 S: send initiate
828 F: file header
829 D: data (multiple)
830 Z: end of file
831 B: break transmission
832 */
833
834 /* enter main loop */
835 while (!done) {
836 /* set the send packet pointer to begining of send packet parms */
837 send_ptr = send_parms;
838
839 /* With each packet, start summing the bytes starting with the length.
840 Save the current sequence number.
841 Note the type of the packet.
842 If a character less than SPACE (0x20) is received - error.
843 */
844
845#if 0
846 /* OLD CODE, Prior to checking sequence numbers */
847 /* first have all state machines save current states */
848 k_state_saved = k_state;
849 k_data_save ();
850#endif
851
852 /* get a packet */
853 /* wait for the starting character or ^C */
854 for (;;) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200855 switch (getchar()) {
wdenk57b2d802003-06-27 21:31:46 +0000856 case START_CHAR: /* start packet */
857 goto START;
858 case ETX_CHAR: /* ^C waiting for packet */
859 return (0);
860 default:
861 ;
862 }
863 }
864START:
865 /* get length of packet */
866 sum = 0;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200867 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000868 if ((new_char & 0xE0) == 0)
869 goto packet_error;
870 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000871 length = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000872 /* get sequence number */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200873 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000874 if ((new_char & 0xE0) == 0)
875 goto packet_error;
876 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000877 n = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000878 --length;
879
880 /* NEW CODE - check sequence numbers for retried packets */
881 /* Note - this new code assumes that the sequence number is correctly
882 * received. Handling an invalid sequence number adds another layer
883 * of complexity that may not be needed - yet! At this time, I'm hoping
884 * that I don't need to buffer the incoming data packets and can write
885 * the data into memory in real time.
886 */
887 if (n == last_n) {
888 /* same sequence number, restore the previous state */
889 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000890 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000891 } else {
892 /* new sequence number, checkpoint the download */
893 last_n = n;
894 k_state_saved = k_state;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000895 k_data_save();
wdenk57b2d802003-06-27 21:31:46 +0000896 }
897 /* END NEW CODE */
898
899 /* get packet type */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200900 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000901 if ((new_char & 0xE0) == 0)
902 goto packet_error;
903 sum += new_char & 0xff;
904 k_state = new_char;
905 --length;
906 /* check for extended length */
907 if (length == -2) {
908 /* (length byte was 0, decremented twice) */
909 /* get the two length bytes */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200910 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000911 if ((new_char & 0xE0) == 0)
912 goto packet_error;
913 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000914 len_hi = untochar(new_char);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200915 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000916 if ((new_char & 0xE0) == 0)
917 goto packet_error;
918 sum += new_char & 0xff;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000919 len_lo = untochar(new_char);
wdenk57b2d802003-06-27 21:31:46 +0000920 length = len_hi * 95 + len_lo;
921 /* check header checksum */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200922 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000923 if ((new_char & 0xE0) == 0)
924 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000925 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000926 goto packet_error;
927 sum += new_char & 0xff;
928/* --length; */ /* new length includes only data and block check to come */
929 }
930 /* bring in rest of packet */
931 while (length > 1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200932 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000933 if ((new_char & 0xE0) == 0)
934 goto packet_error;
935 sum += new_char & 0xff;
936 --length;
937 if (k_state == DATA_TYPE) {
938 /* pass on the data if this is a data packet */
939 k_data_char (new_char);
940 } else if (k_state == SEND_TYPE) {
941 /* save send pack in buffer as is */
942 *send_ptr++ = new_char;
943 /* if too much data, back off the pointer */
944 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
945 --send_ptr;
946 }
947 }
948 /* get and validate checksum character */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200949 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000950 if ((new_char & 0xE0) == 0)
951 goto packet_error;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000952 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
wdenk57b2d802003-06-27 21:31:46 +0000953 goto packet_error;
954 /* get END_CHAR */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200955 new_char = getchar();
wdenk57b2d802003-06-27 21:31:46 +0000956 if (new_char != END_CHAR) {
957 packet_error:
958 /* restore state machines */
959 k_state = k_state_saved;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000960 k_data_restore();
wdenk57b2d802003-06-27 21:31:46 +0000961 /* send a negative acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000962 send_nack(n);
wdenk57b2d802003-06-27 21:31:46 +0000963 } else if (k_state == SEND_TYPE) {
964 /* crack the protocol parms, build an appropriate ack packet */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000965 handle_send_packet(n);
wdenk57b2d802003-06-27 21:31:46 +0000966 } else {
967 /* send simple acknowledge packet in */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000968 send_ack(n);
wdenk57b2d802003-06-27 21:31:46 +0000969 /* quit if end of transmission */
970 if (k_state == BREAK_TYPE)
971 done = 1;
972 }
wdenk57b2d802003-06-27 21:31:46 +0000973 }
974 return ((ulong) os_data_addr - (ulong) bin_start_address);
975}
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200976
977static int getcxmodem(void) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200978 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200979 return (getchar());
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200980 return -1;
981}
Angus Ainslief283bd02013-06-26 16:54:24 -0600982static ulong load_serial_ymodem(ulong offset, int mode)
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200983{
984 int size;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200985 int err;
986 int res;
987 connection_info_t info;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200988 char ymodemBuf[1024];
989 ulong store_addr = ~0;
990 ulong addr = 0;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200991
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200992 size = 0;
Angus Ainslief283bd02013-06-26 16:54:24 -0600993 info.mode = mode;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000994 res = xyzModem_stream_open(&info, &err);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +0200995 if (!res) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200996
Pali Rohárb6f177b2021-08-03 16:28:42 +0200997 err = 0;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200998 while ((res =
Kim Phillipsdc00a682012-10-29 13:34:31 +0000999 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001000 store_addr = addr + offset;
1001 size += res;
1002 addr += res;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +09001003#ifdef CONFIG_MTD_NOR_FLASH
Kim Phillipsdc00a682012-10-29 13:34:31 +00001004 if (addr2info(store_addr)) {
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001005 int rc;
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001006
Kim Phillipsdc00a682012-10-29 13:34:31 +00001007 rc = flash_write((char *) ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001008 store_addr, res);
1009 if (rc != 0) {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001010 xyzModem_stream_terminate(true, &getcxmodem);
1011 xyzModem_stream_close(&err);
1012 printf("\n");
Simon Glassa606ffc2019-12-28 10:44:40 -07001013 flash_perror(rc);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001014 return (~0);
1015 }
1016 } else
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001017#endif
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001018 {
Kim Phillipsdc00a682012-10-29 13:34:31 +00001019 memcpy((char *)(store_addr), ymodemBuf,
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001020 res);
1021 }
1022
1023 }
Pali Rohárb6f177b2021-08-03 16:28:42 +02001024 if (err) {
1025 xyzModem_stream_terminate((err == xyzModem_cancel) ? false : true, &getcxmodem);
1026 xyzModem_stream_close(&err);
1027 printf("\n%s\n", xyzModem_error(err));
1028 return (~0); /* Download aborted */
1029 }
1030
Heinrich Schuchardta89adb02021-03-19 02:50:57 +00001031 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
1032 efi_set_bootdev("Uart", "", "",
1033 map_sysmem(offset, 0), size);
1034
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001035 } else {
Pali Rohárb6f177b2021-08-03 16:28:42 +02001036 printf("\n%s\n", xyzModem_error(err));
1037 return (~0); /* Download aborted */
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001038 }
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001039
Kim Phillipsdc00a682012-10-29 13:34:31 +00001040 xyzModem_stream_terminate(false, &getcxmodem);
Pali Rohárd9c805e2021-08-03 16:28:41 +02001041 xyzModem_stream_close(&err);
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02001042
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001043
Chris Packhambbaf4242016-10-25 20:22:48 +13001044 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001045
Kim Phillipsdc00a682012-10-29 13:34:31 +00001046 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
Simon Glass4d949a22017-08-03 12:22:10 -06001047 env_set_hex("filesize", size);
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001048
1049 return offset;
1050}
1051
Jon Loeligerd704d912007-07-10 11:02:44 -05001052#endif
wdenk57b2d802003-06-27 21:31:46 +00001053
1054/* -------------------------------------------------------------------- */
1055
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001056#if defined(CONFIG_CMD_LOADS)
wdenk57b2d802003-06-27 21:31:46 +00001057
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001058#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001059U_BOOT_CMD(
1060 loads, 3, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001061 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001062 "[ off ] [ baud ]\n"
1063 " - load S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001064 " with offset 'off' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001065);
1066
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001067#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001068U_BOOT_CMD(
1069 loads, 2, 0, do_load_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001070 "load S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001071 "[ off ]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001072 " - load S-Record file over serial line with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001073);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001074#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenk57b2d802003-06-27 21:31:46 +00001075
1076/*
1077 * SAVES always requires LOADS support, but not vice versa
1078 */
1079
1080
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001081#if defined(CONFIG_CMD_SAVES)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001082#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
wdenkf287a242003-07-01 21:06:45 +00001083U_BOOT_CMD(
1084 saves, 4, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001085 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001086 "[ off ] [size] [ baud ]\n"
1087 " - save S-Record file over serial line"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001088 " with offset 'off', size 'size' and baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001089);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001090#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
wdenkf287a242003-07-01 21:06:45 +00001091U_BOOT_CMD(
1092 saves, 3, 0, do_save_serial,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001093 "save S-Record file over serial line",
wdenk57b2d802003-06-27 21:31:46 +00001094 "[ off ] [size]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001095 " - save S-Record file over serial line with offset 'off' and size 'size'"
wdenk57b2d802003-06-27 21:31:46 +00001096);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001097#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001098#endif /* CONFIG_CMD_SAVES */
1099#endif /* CONFIG_CMD_LOADS */
wdenk57b2d802003-06-27 21:31:46 +00001100
1101
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001102#if defined(CONFIG_CMD_LOADB)
wdenkf287a242003-07-01 21:06:45 +00001103U_BOOT_CMD(
1104 loadb, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001105 "load binary file over serial line (kermit mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001106 "[ addr [ baud ] ]\n"
wdenk57b2d802003-06-27 21:31:46 +00001107 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001108 " at address 'addr' with baudrate 'baud'"
wdenk57b2d802003-06-27 21:31:46 +00001109);
1110
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001111U_BOOT_CMD(
Angus Ainslief283bd02013-06-26 16:54:24 -06001112 loadx, 3, 0, do_load_serial_bin,
1113 "load binary file over serial line (xmodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001114 "[ addr [ baud ] ]\n"
Angus Ainslief283bd02013-06-26 16:54:24 -06001115 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001116 " at address 'addr' with baudrate 'baud'"
Angus Ainslief283bd02013-06-26 16:54:24 -06001117);
1118
1119U_BOOT_CMD(
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001120 loady, 3, 0, do_load_serial_bin,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001121 "load binary file over serial line (ymodem mode)",
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001122 "[ addr [ baud ] ]\n"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001123 " - load binary file over serial line"
Heinrich Schuchardtcd373b52021-01-22 13:16:04 +01001124 " at address 'addr' with baudrate 'baud'"
Markus Klotzbuecher387f5412006-03-30 13:40:55 +02001125);
1126
Robert P. J. Dayaec64f02013-02-03 05:46:13 +00001127#endif /* CONFIG_CMD_LOADB */