]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/load.c
flash: complete CONFIG_SYS_NO_FLASH move with renaming
[people/ms/u-boot.git] / cmd / load.c
CommitLineData
8bde7f77 1/*
232c150a 2 * (C) Copyright 2000-2004
8bde7f77
WD
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
8bde7f77
WD
6 */
7
8/*
9 * Serial up- and download support
10 */
11#include <common.h>
12#include <command.h>
24b852a7 13#include <console.h>
8bde7f77
WD
14#include <s_record.h>
15#include <net.h>
27b207fd 16#include <exports.h>
f2841d37 17#include <xyzModem.h>
8bde7f77 18
d87080b7 19DECLARE_GLOBAL_DATA_PTR;
8bde7f77 20
c76fe474 21#if defined(CONFIG_CMD_LOADB)
6e66bd55 22static ulong load_serial_ymodem(ulong offset, int mode);
8546e239
WD
23#endif
24
c76fe474 25#if defined(CONFIG_CMD_LOADS)
088f1b19
KP
26static ulong load_serial(long offset);
27static int read_record(char *buf, ulong len);
c76fe474 28# if defined(CONFIG_CMD_SAVES)
088f1b19
KP
29static int save_serial(ulong offset, ulong size);
30static int write_record(char *buf);
90253178 31#endif
8bde7f77
WD
32
33static int do_echo = 1;
90253178 34#endif
8bde7f77
WD
35
36/* -------------------------------------------------------------------- */
37
c76fe474 38#if defined(CONFIG_CMD_LOADS)
088f1b19
KP
39static int do_load_serial(cmd_tbl_t *cmdtp, int flag, int argc,
40 char * const argv[])
8bde7f77 41{
2b22d608 42 long offset = 0;
8bde7f77
WD
43 ulong addr;
44 int i;
45 char *env_echo;
46 int rcode = 0;
6d0f6bcf 47#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77
WD
48 int load_baudrate, current_baudrate;
49
50 load_baudrate = current_baudrate = gd->baudrate;
51#endif
52
53 if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
54 do_echo = 1;
55 } else {
56 do_echo = 0;
57 }
58
6d0f6bcf 59#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77 60 if (argc >= 2) {
2b22d608 61 offset = simple_strtol(argv[1], NULL, 16);
8bde7f77
WD
62 }
63 if (argc == 3) {
64 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
65
66 /* default to current baudrate */
67 if (load_baudrate == 0)
68 load_baudrate = current_baudrate;
69 }
70 if (load_baudrate != current_baudrate) {
088f1b19 71 printf("## Switch baudrate to %d bps and press ENTER ...\n",
8bde7f77
WD
72 load_baudrate);
73 udelay(50000);
74 gd->baudrate = load_baudrate;
088f1b19 75 serial_setbrg();
8bde7f77
WD
76 udelay(50000);
77 for (;;) {
78 if (getc() == '\r')
79 break;
80 }
81 }
6d0f6bcf 82#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
8bde7f77 83 if (argc == 2) {
2b22d608 84 offset = simple_strtol(argv[1], NULL, 16);
8bde7f77 85 }
6d0f6bcf 86#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
8bde7f77 87
088f1b19 88 printf("## Ready for S-Record download ...\n");
8bde7f77 89
088f1b19 90 addr = load_serial(offset);
8bde7f77
WD
91
92 /*
93 * Gather any trailing characters (for instance, the ^D which
94 * is sent by 'cu' after sending a file), and give the
95 * box some time (100 * 1 ms)
96 */
97 for (i=0; i<100; ++i) {
232c150a
WD
98 if (tstc()) {
99 (void) getc();
8bde7f77
WD
100 }
101 udelay(1000);
102 }
103
104 if (addr == ~0) {
088f1b19 105 printf("## S-Record download aborted\n");
8bde7f77
WD
106 rcode = 1;
107 } else {
088f1b19 108 printf("## Start Addr = 0x%08lX\n", addr);
8bde7f77
WD
109 load_addr = addr;
110 }
111
6d0f6bcf 112#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77 113 if (load_baudrate != current_baudrate) {
088f1b19 114 printf("## Switch baudrate to %d bps and press ESC ...\n",
8bde7f77 115 current_baudrate);
088f1b19 116 udelay(50000);
8bde7f77 117 gd->baudrate = current_baudrate;
088f1b19
KP
118 serial_setbrg();
119 udelay(50000);
8bde7f77
WD
120 for (;;) {
121 if (getc() == 0x1B) /* ESC */
122 break;
123 }
124 }
125#endif
126 return rcode;
127}
128
088f1b19 129static ulong load_serial(long offset)
8bde7f77
WD
130{
131 char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
132 char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
133 int binlen; /* no. of data bytes in S-Rec. */
134 int type; /* return code for record type */
135 ulong addr; /* load address from S-Record */
136 ulong size; /* number of bytes transferred */
8bde7f77
WD
137 ulong store_addr;
138 ulong start_addr = ~0;
139 ulong end_addr = 0;
140 int line_count = 0;
141
142 while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
088f1b19 143 type = srec_decode(record, &binlen, &addr, binbuf);
8bde7f77
WD
144
145 if (type < 0) {
146 return (~0); /* Invalid S-Record */
147 }
148
149 switch (type) {
150 case SREC_DATA2:
151 case SREC_DATA3:
152 case SREC_DATA4:
153 store_addr = addr + offset;
e856bdcf 154#ifdef CONFIG_MTD_NOR_FLASH
8bde7f77
WD
155 if (addr2info(store_addr)) {
156 int rc;
157
77ddac94 158 rc = flash_write((char *)binbuf,store_addr,binlen);
8bde7f77 159 if (rc != 0) {
088f1b19 160 flash_perror(rc);
8bde7f77
WD
161 return (~0);
162 }
163 } else
164#endif
165 {
088f1b19 166 memcpy((char *)(store_addr), binbuf, binlen);
8bde7f77
WD
167 }
168 if ((store_addr) < start_addr)
169 start_addr = store_addr;
170 if ((store_addr + binlen - 1) > end_addr)
171 end_addr = store_addr + binlen - 1;
172 break;
173 case SREC_END2:
174 case SREC_END3:
175 case SREC_END4:
088f1b19 176 udelay(10000);
8bde7f77 177 size = end_addr - start_addr + 1;
088f1b19 178 printf("\n"
8bde7f77
WD
179 "## First Load Addr = 0x%08lX\n"
180 "## Last Load Addr = 0x%08lX\n"
181 "## Total Size = 0x%08lX = %ld Bytes\n",
182 start_addr, end_addr, size, size
183 );
088f1b19 184 flush_cache(start_addr, size);
41ef372c 185 setenv_hex("filesize", size);
8bde7f77
WD
186 return (addr);
187 case SREC_START:
188 break;
189 default:
190 break;
191 }
192 if (!do_echo) { /* print a '.' every 100 lines */
193 if ((++line_count % 100) == 0)
088f1b19 194 putc('.');
8bde7f77
WD
195 }
196 }
197
198 return (~0); /* Download aborted */
199}
200
088f1b19 201static int read_record(char *buf, ulong len)
8bde7f77
WD
202{
203 char *p;
204 char c;
205
206 --len; /* always leave room for terminating '\0' byte */
207
208 for (p=buf; p < buf+len; ++p) {
232c150a 209 c = getc(); /* read character */
8bde7f77 210 if (do_echo)
088f1b19 211 putc(c); /* ... and echo it */
8bde7f77
WD
212
213 switch (c) {
214 case '\r':
215 case '\n':
216 *p = '\0';
217 return (p - buf);
218 case '\0':
219 case 0x03: /* ^C - Control C */
220 return (-1);
221 default:
222 *p = c;
223 }
224
225 /* Check for the console hangup (if any different from serial) */
49cad547 226 if (gd->jt->getc != getc) {
8bde7f77
WD
227 if (ctrlc()) {
228 return (-1);
229 }
230 }
8bde7f77
WD
231 }
232
233 /* line too long - truncate */
234 *p = '\0';
235 return (p - buf);
236}
237
c76fe474 238#if defined(CONFIG_CMD_SAVES)
8bde7f77 239
54841ab5 240int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
8bde7f77
WD
241{
242 ulong offset = 0;
243 ulong size = 0;
6d0f6bcf 244#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77
WD
245 int save_baudrate, current_baudrate;
246
247 save_baudrate = current_baudrate = gd->baudrate;
248#endif
249
250 if (argc >= 2) {
251 offset = simple_strtoul(argv[1], NULL, 16);
252 }
6d0f6bcf 253#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77
WD
254 if (argc >= 3) {
255 size = simple_strtoul(argv[2], NULL, 16);
256 }
257 if (argc == 4) {
258 save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
259
260 /* default to current baudrate */
261 if (save_baudrate == 0)
262 save_baudrate = current_baudrate;
263 }
264 if (save_baudrate != current_baudrate) {
088f1b19 265 printf("## Switch baudrate to %d bps and press ENTER ...\n",
8bde7f77
WD
266 save_baudrate);
267 udelay(50000);
268 gd->baudrate = save_baudrate;
088f1b19 269 serial_setbrg();
8bde7f77
WD
270 udelay(50000);
271 for (;;) {
272 if (getc() == '\r')
273 break;
274 }
275 }
6d0f6bcf 276#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
8bde7f77
WD
277 if (argc == 3) {
278 size = simple_strtoul(argv[2], NULL, 16);
279 }
6d0f6bcf 280#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
8bde7f77 281
088f1b19 282 printf("## Ready for S-Record upload, press ENTER to proceed ...\n");
8bde7f77
WD
283 for (;;) {
284 if (getc() == '\r')
285 break;
286 }
088f1b19
KP
287 if (save_serial(offset, size)) {
288 printf("## S-Record upload aborted\n");
8bde7f77 289 } else {
088f1b19 290 printf("## S-Record upload complete\n");
8bde7f77 291 }
6d0f6bcf 292#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
8bde7f77 293 if (save_baudrate != current_baudrate) {
088f1b19 294 printf("## Switch baudrate to %d bps and press ESC ...\n",
8bde7f77 295 (int)current_baudrate);
088f1b19 296 udelay(50000);
8bde7f77 297 gd->baudrate = current_baudrate;
088f1b19
KP
298 serial_setbrg();
299 udelay(50000);
8bde7f77
WD
300 for (;;) {
301 if (getc() == 0x1B) /* ESC */
302 break;
303 }
304 }
305#endif
306 return 0;
307}
308
309#define SREC3_START "S0030000FC\n"
310#define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
311#define SREC3_END "S70500000000FA\n"
312#define SREC_BYTES_PER_RECORD 16
313
088f1b19 314static int save_serial(ulong address, ulong count)
8bde7f77
WD
315{
316 int i, c, reclen, checksum, length;
317 char *hex = "0123456789ABCDEF";
318 char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
319 char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
320
321 reclen = 0;
322 checksum = 0;
323
324 if(write_record(SREC3_START)) /* write the header */
325 return (-1);
326 do {
327 if(count) { /* collect hex data in the buffer */
328 c = *(volatile uchar*)(address + reclen); /* get one byte */
329 checksum += c; /* accumulate checksum */
330 data[2*reclen] = hex[(c>>4)&0x0f];
331 data[2*reclen+1] = hex[c & 0x0f];
332 data[2*reclen+2] = '\0';
333 ++reclen;
334 --count;
335 }
336 if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
337 /* enough data collected for one record: dump it */
338 if(reclen) { /* build & write a data record: */
339 /* address + data + checksum */
340 length = 4 + reclen + 1;
341
342 /* accumulate length bytes into checksum */
343 for(i = 0; i < 2; i++)
344 checksum += (length >> (8*i)) & 0xff;
345
346 /* accumulate address bytes into checksum: */
347 for(i = 0; i < 4; i++)
348 checksum += (address >> (8*i)) & 0xff;
349
350 /* make proper checksum byte: */
351 checksum = ~checksum & 0xff;
352
353 /* output one record: */
354 sprintf(record, SREC3_FORMAT, length, address, data, checksum);
355 if(write_record(record))
356 return (-1);
357 }
358 address += reclen; /* increment address */
359 checksum = 0;
360 reclen = 0;
361 }
362 }
363 while(count);
364 if(write_record(SREC3_END)) /* write the final record */
365 return (-1);
366 return(0);
367}
368
088f1b19 369static int write_record(char *buf)
8bde7f77
WD
370{
371 char c;
372
373 while((c = *buf++))
232c150a 374 putc(c);
8bde7f77
WD
375
376 /* Check for the console hangup (if any different from serial) */
377
378 if (ctrlc()) {
379 return (-1);
380 }
381 return (0);
382}
90253178 383# endif
8bde7f77 384
90253178 385#endif
8bde7f77
WD
386
387
c76fe474 388#if defined(CONFIG_CMD_LOADB)
65c450b4
JL
389/*
390 * loadb command (load binary) included
391 */
8bde7f77
WD
392#define XON_CHAR 17
393#define XOFF_CHAR 19
394#define START_CHAR 0x01
395#define ETX_CHAR 0x03
396#define END_CHAR 0x0D
397#define SPACE 0x20
398#define K_ESCAPE 0x23
399#define SEND_TYPE 'S'
400#define DATA_TYPE 'D'
401#define ACK_TYPE 'Y'
402#define NACK_TYPE 'N'
403#define BREAK_TYPE 'B'
404#define tochar(x) ((char) (((x) + SPACE) & 0xff))
405#define untochar(x) ((int) (((x) - SPACE) & 0xff))
406
8bde7f77
WD
407static void set_kerm_bin_mode(unsigned long *);
408static int k_recv(void);
088f1b19 409static ulong load_serial_bin(ulong offset);
8bde7f77
WD
410
411
088f1b19
KP
412static char his_eol; /* character he needs at end of packet */
413static int his_pad_count; /* number of pad chars he needs */
414static char his_pad_char; /* pad chars he needs */
415static char his_quote; /* quote chars he'll use */
8bde7f77 416
088f1b19
KP
417static int do_load_serial_bin(cmd_tbl_t *cmdtp, int flag, int argc,
418 char * const argv[])
8bde7f77 419{
8bde7f77
WD
420 ulong offset = 0;
421 ulong addr;
422 int load_baudrate, current_baudrate;
423 int rcode = 0;
424 char *s;
425
6d0f6bcf
JCPV
426 /* pre-set offset from CONFIG_SYS_LOAD_ADDR */
427 offset = CONFIG_SYS_LOAD_ADDR;
8bde7f77
WD
428
429 /* pre-set offset from $loadaddr */
430 if ((s = getenv("loadaddr")) != NULL) {
431 offset = simple_strtoul(s, NULL, 16);
432 }
433
434 load_baudrate = current_baudrate = gd->baudrate;
435
436 if (argc >= 2) {
437 offset = simple_strtoul(argv[1], NULL, 16);
438 }
439 if (argc == 3) {
440 load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
441
442 /* default to current baudrate */
443 if (load_baudrate == 0)
444 load_baudrate = current_baudrate;
445 }
446
447 if (load_baudrate != current_baudrate) {
088f1b19 448 printf("## Switch baudrate to %d bps and press ENTER ...\n",
8bde7f77
WD
449 load_baudrate);
450 udelay(50000);
451 gd->baudrate = load_baudrate;
088f1b19 452 serial_setbrg();
8bde7f77
WD
453 udelay(50000);
454 for (;;) {
455 if (getc() == '\r')
456 break;
457 }
458 }
459
f2841d37 460 if (strcmp(argv[0],"loady")==0) {
088f1b19 461 printf("## Ready for binary (ymodem) download "
f2841d37
MK
462 "to 0x%08lX at %d bps...\n",
463 offset,
464 load_baudrate);
465
6e66bd55
AA
466 addr = load_serial_ymodem(offset, xyzModem_ymodem);
467
468 } else if (strcmp(argv[0],"loadx")==0) {
469 printf("## Ready for binary (xmodem) download "
470 "to 0x%08lX at %d bps...\n",
471 offset,
472 load_baudrate);
473
474 addr = load_serial_ymodem(offset, xyzModem_xmodem);
8bde7f77 475
8bde7f77 476 } else {
8bde7f77 477
088f1b19 478 printf("## Ready for binary (kermit) download "
f2841d37
MK
479 "to 0x%08lX at %d bps...\n",
480 offset,
481 load_baudrate);
088f1b19 482 addr = load_serial_bin(offset);
f2841d37
MK
483
484 if (addr == ~0) {
485 load_addr = 0;
088f1b19 486 printf("## Binary (kermit) download aborted\n");
f2841d37
MK
487 rcode = 1;
488 } else {
088f1b19 489 printf("## Start Addr = 0x%08lX\n", addr);
f2841d37
MK
490 load_addr = addr;
491 }
492 }
8bde7f77 493 if (load_baudrate != current_baudrate) {
088f1b19 494 printf("## Switch baudrate to %d bps and press ESC ...\n",
8bde7f77 495 current_baudrate);
088f1b19 496 udelay(50000);
8bde7f77 497 gd->baudrate = current_baudrate;
088f1b19
KP
498 serial_setbrg();
499 udelay(50000);
8bde7f77
WD
500 for (;;) {
501 if (getc() == 0x1B) /* ESC */
502 break;
503 }
504 }
505
8bde7f77
WD
506 return rcode;
507}
508
509
088f1b19 510static ulong load_serial_bin(ulong offset)
8bde7f77
WD
511{
512 int size, i;
8bde7f77 513
088f1b19
KP
514 set_kerm_bin_mode((ulong *) offset);
515 size = k_recv();
8bde7f77
WD
516
517 /*
518 * Gather any trailing characters (for instance, the ^D which
519 * is sent by 'cu' after sending a file), and give the
520 * box some time (100 * 1 ms)
521 */
522 for (i=0; i<100; ++i) {
232c150a
WD
523 if (tstc()) {
524 (void) getc();
8bde7f77
WD
525 }
526 udelay(1000);
527 }
528
088f1b19 529 flush_cache(offset, size);
8bde7f77
WD
530
531 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
41ef372c 532 setenv_hex("filesize", size);
8bde7f77
WD
533
534 return offset;
535}
536
088f1b19 537static void send_pad(void)
8bde7f77
WD
538{
539 int count = his_pad_count;
540
541 while (count-- > 0)
088f1b19 542 putc(his_pad_char);
8bde7f77
WD
543}
544
545/* converts escaped kermit char to binary char */
088f1b19 546static char ktrans(char in)
8bde7f77
WD
547{
548 if ((in & 0x60) == 0x40) {
549 return (char) (in & ~0x40);
550 } else if ((in & 0x7f) == 0x3f) {
551 return (char) (in | 0x40);
552 } else
553 return in;
554}
555
088f1b19 556static int chk1(char *buffer)
8bde7f77
WD
557{
558 int total = 0;
559
560 while (*buffer) {
561 total += *buffer++;
562 }
563 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
564}
565
088f1b19 566static void s1_sendpacket(char *packet)
8bde7f77 567{
088f1b19 568 send_pad();
8bde7f77 569 while (*packet) {
088f1b19 570 putc(*packet++);
8bde7f77
WD
571 }
572}
573
574static char a_b[24];
088f1b19 575static void send_ack(int n)
8bde7f77
WD
576{
577 a_b[0] = START_CHAR;
088f1b19
KP
578 a_b[1] = tochar(3);
579 a_b[2] = tochar(n);
8bde7f77
WD
580 a_b[3] = ACK_TYPE;
581 a_b[4] = '\0';
088f1b19 582 a_b[4] = tochar(chk1(&a_b[1]));
8bde7f77
WD
583 a_b[5] = his_eol;
584 a_b[6] = '\0';
088f1b19 585 s1_sendpacket(a_b);
8bde7f77
WD
586}
587
088f1b19 588static void send_nack(int n)
8bde7f77
WD
589{
590 a_b[0] = START_CHAR;
088f1b19
KP
591 a_b[1] = tochar(3);
592 a_b[2] = tochar(n);
8bde7f77
WD
593 a_b[3] = NACK_TYPE;
594 a_b[4] = '\0';
088f1b19 595 a_b[4] = tochar(chk1(&a_b[1]));
8bde7f77
WD
596 a_b[5] = his_eol;
597 a_b[6] = '\0';
088f1b19 598 s1_sendpacket(a_b);
8bde7f77
WD
599}
600
601
088f1b19
KP
602static void (*os_data_init)(void);
603static void (*os_data_char)(char new_char);
8bde7f77 604static int os_data_state, os_data_state_saved;
8bde7f77
WD
605static char *os_data_addr, *os_data_addr_saved;
606static char *bin_start_address;
a9fe0c3e 607
088f1b19 608static void bin_data_init(void)
8bde7f77
WD
609{
610 os_data_state = 0;
8bde7f77
WD
611 os_data_addr = bin_start_address;
612}
a9fe0c3e 613
088f1b19 614static void os_data_save(void)
8bde7f77
WD
615{
616 os_data_state_saved = os_data_state;
8bde7f77
WD
617 os_data_addr_saved = os_data_addr;
618}
a9fe0c3e 619
088f1b19 620static void os_data_restore(void)
8bde7f77
WD
621{
622 os_data_state = os_data_state_saved;
8bde7f77
WD
623 os_data_addr = os_data_addr_saved;
624}
a9fe0c3e 625
088f1b19 626static void bin_data_char(char new_char)
8bde7f77
WD
627{
628 switch (os_data_state) {
629 case 0: /* data */
630 *os_data_addr++ = new_char;
8bde7f77
WD
631 break;
632 }
633}
a9fe0c3e 634
088f1b19 635static void set_kerm_bin_mode(unsigned long *addr)
8bde7f77
WD
636{
637 bin_start_address = (char *) addr;
638 os_data_init = bin_data_init;
639 os_data_char = bin_data_char;
640}
641
642
643/* k_data_* simply handles the kermit escape translations */
644static int k_data_escape, k_data_escape_saved;
088f1b19 645static void k_data_init(void)
8bde7f77
WD
646{
647 k_data_escape = 0;
088f1b19 648 os_data_init();
8bde7f77 649}
a9fe0c3e 650
088f1b19 651static void k_data_save(void)
8bde7f77
WD
652{
653 k_data_escape_saved = k_data_escape;
088f1b19 654 os_data_save();
8bde7f77 655}
a9fe0c3e 656
088f1b19 657static void k_data_restore(void)
8bde7f77
WD
658{
659 k_data_escape = k_data_escape_saved;
088f1b19 660 os_data_restore();
8bde7f77 661}
a9fe0c3e 662
088f1b19 663static void k_data_char(char new_char)
8bde7f77
WD
664{
665 if (k_data_escape) {
666 /* last char was escape - translate this character */
088f1b19 667 os_data_char(ktrans(new_char));
8bde7f77
WD
668 k_data_escape = 0;
669 } else {
670 if (new_char == his_quote) {
671 /* this char is escape - remember */
672 k_data_escape = 1;
673 } else {
674 /* otherwise send this char as-is */
088f1b19 675 os_data_char(new_char);
8bde7f77
WD
676 }
677 }
678}
679
680#define SEND_DATA_SIZE 20
088f1b19
KP
681static char send_parms[SEND_DATA_SIZE];
682static char *send_ptr;
8bde7f77
WD
683
684/* handle_send_packet interprits the protocol info and builds and
685 sends an appropriate ack for what we can do */
088f1b19 686static void handle_send_packet(int n)
8bde7f77
WD
687{
688 int length = 3;
689 int bytes;
690
691 /* initialize some protocol parameters */
692 his_eol = END_CHAR; /* default end of line character */
693 his_pad_count = 0;
694 his_pad_char = '\0';
695 his_quote = K_ESCAPE;
696
697 /* ignore last character if it filled the buffer */
698 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
699 --send_ptr;
700 bytes = send_ptr - send_parms; /* how many bytes we'll process */
701 do {
702 if (bytes-- <= 0)
703 break;
704 /* handle MAXL - max length */
705 /* ignore what he says - most I'll take (here) is 94 */
088f1b19 706 a_b[++length] = tochar(94);
8bde7f77
WD
707 if (bytes-- <= 0)
708 break;
709 /* handle TIME - time you should wait for my packets */
710 /* ignore what he says - don't wait for my ack longer than 1 second */
088f1b19 711 a_b[++length] = tochar(1);
8bde7f77
WD
712 if (bytes-- <= 0)
713 break;
714 /* handle NPAD - number of pad chars I need */
715 /* remember what he says - I need none */
088f1b19
KP
716 his_pad_count = untochar(send_parms[2]);
717 a_b[++length] = tochar(0);
8bde7f77
WD
718 if (bytes-- <= 0)
719 break;
720 /* handle PADC - pad chars I need */
721 /* remember what he says - I need none */
088f1b19 722 his_pad_char = ktrans(send_parms[3]);
8bde7f77
WD
723 a_b[++length] = 0x40; /* He should ignore this */
724 if (bytes-- <= 0)
725 break;
726 /* handle EOL - end of line he needs */
727 /* remember what he says - I need CR */
088f1b19
KP
728 his_eol = untochar(send_parms[4]);
729 a_b[++length] = tochar(END_CHAR);
8bde7f77
WD
730 if (bytes-- <= 0)
731 break;
732 /* handle QCTL - quote control char he'll use */
733 /* remember what he says - I'll use '#' */
734 his_quote = send_parms[5];
735 a_b[++length] = '#';
736 if (bytes-- <= 0)
737 break;
738 /* handle QBIN - 8-th bit prefixing */
739 /* ignore what he says - I refuse */
740 a_b[++length] = 'N';
741 if (bytes-- <= 0)
742 break;
743 /* handle CHKT - the clock check type */
744 /* ignore what he says - I do type 1 (for now) */
745 a_b[++length] = '1';
746 if (bytes-- <= 0)
747 break;
748 /* handle REPT - the repeat prefix */
749 /* ignore what he says - I refuse (for now) */
750 a_b[++length] = 'N';
751 if (bytes-- <= 0)
752 break;
753 /* handle CAPAS - the capabilities mask */
754 /* ignore what he says - I only do long packets - I don't do windows */
088f1b19
KP
755 a_b[++length] = tochar(2); /* only long packets */
756 a_b[++length] = tochar(0); /* no windows */
757 a_b[++length] = tochar(94); /* large packet msb */
758 a_b[++length] = tochar(94); /* large packet lsb */
8bde7f77
WD
759 } while (0);
760
761 a_b[0] = START_CHAR;
088f1b19
KP
762 a_b[1] = tochar(length);
763 a_b[2] = tochar(n);
8bde7f77
WD
764 a_b[3] = ACK_TYPE;
765 a_b[++length] = '\0';
088f1b19 766 a_b[length] = tochar(chk1(&a_b[1]));
8bde7f77
WD
767 a_b[++length] = his_eol;
768 a_b[++length] = '\0';
088f1b19 769 s1_sendpacket(a_b);
8bde7f77
WD
770}
771
772/* k_recv receives a OS Open image file over kermit line */
088f1b19 773static int k_recv(void)
8bde7f77
WD
774{
775 char new_char;
776 char k_state, k_state_saved;
777 int sum;
778 int done;
779 int length;
780 int n, last_n;
8bde7f77
WD
781 int len_lo, len_hi;
782
783 /* initialize some protocol parameters */
784 his_eol = END_CHAR; /* default end of line character */
785 his_pad_count = 0;
786 his_pad_char = '\0';
787 his_quote = K_ESCAPE;
788
789 /* initialize the k_recv and k_data state machine */
790 done = 0;
791 k_state = 0;
088f1b19 792 k_data_init();
8bde7f77 793 k_state_saved = k_state;
088f1b19 794 k_data_save();
8bde7f77
WD
795 n = 0; /* just to get rid of a warning */
796 last_n = -1;
797
798 /* expect this "type" sequence (but don't check):
799 S: send initiate
800 F: file header
801 D: data (multiple)
802 Z: end of file
803 B: break transmission
804 */
805
806 /* enter main loop */
807 while (!done) {
808 /* set the send packet pointer to begining of send packet parms */
809 send_ptr = send_parms;
810
811 /* With each packet, start summing the bytes starting with the length.
812 Save the current sequence number.
813 Note the type of the packet.
814 If a character less than SPACE (0x20) is received - error.
815 */
816
817#if 0
818 /* OLD CODE, Prior to checking sequence numbers */
819 /* first have all state machines save current states */
820 k_state_saved = k_state;
821 k_data_save ();
822#endif
823
824 /* get a packet */
825 /* wait for the starting character or ^C */
826 for (;;) {
232c150a 827 switch (getc ()) {
8bde7f77
WD
828 case START_CHAR: /* start packet */
829 goto START;
830 case ETX_CHAR: /* ^C waiting for packet */
831 return (0);
832 default:
833 ;
834 }
835 }
836START:
837 /* get length of packet */
838 sum = 0;
088f1b19 839 new_char = getc();
8bde7f77
WD
840 if ((new_char & 0xE0) == 0)
841 goto packet_error;
842 sum += new_char & 0xff;
088f1b19 843 length = untochar(new_char);
8bde7f77 844 /* get sequence number */
088f1b19 845 new_char = getc();
8bde7f77
WD
846 if ((new_char & 0xE0) == 0)
847 goto packet_error;
848 sum += new_char & 0xff;
088f1b19 849 n = untochar(new_char);
8bde7f77
WD
850 --length;
851
852 /* NEW CODE - check sequence numbers for retried packets */
853 /* Note - this new code assumes that the sequence number is correctly
854 * received. Handling an invalid sequence number adds another layer
855 * of complexity that may not be needed - yet! At this time, I'm hoping
856 * that I don't need to buffer the incoming data packets and can write
857 * the data into memory in real time.
858 */
859 if (n == last_n) {
860 /* same sequence number, restore the previous state */
861 k_state = k_state_saved;
088f1b19 862 k_data_restore();
8bde7f77
WD
863 } else {
864 /* new sequence number, checkpoint the download */
865 last_n = n;
866 k_state_saved = k_state;
088f1b19 867 k_data_save();
8bde7f77
WD
868 }
869 /* END NEW CODE */
870
871 /* get packet type */
088f1b19 872 new_char = getc();
8bde7f77
WD
873 if ((new_char & 0xE0) == 0)
874 goto packet_error;
875 sum += new_char & 0xff;
876 k_state = new_char;
877 --length;
878 /* check for extended length */
879 if (length == -2) {
880 /* (length byte was 0, decremented twice) */
881 /* get the two length bytes */
088f1b19 882 new_char = getc();
8bde7f77
WD
883 if ((new_char & 0xE0) == 0)
884 goto packet_error;
885 sum += new_char & 0xff;
088f1b19
KP
886 len_hi = untochar(new_char);
887 new_char = getc();
8bde7f77
WD
888 if ((new_char & 0xE0) == 0)
889 goto packet_error;
890 sum += new_char & 0xff;
088f1b19 891 len_lo = untochar(new_char);
8bde7f77
WD
892 length = len_hi * 95 + len_lo;
893 /* check header checksum */
088f1b19 894 new_char = getc();
8bde7f77
WD
895 if ((new_char & 0xE0) == 0)
896 goto packet_error;
088f1b19 897 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
8bde7f77
WD
898 goto packet_error;
899 sum += new_char & 0xff;
900/* --length; */ /* new length includes only data and block check to come */
901 }
902 /* bring in rest of packet */
903 while (length > 1) {
088f1b19 904 new_char = getc();
8bde7f77
WD
905 if ((new_char & 0xE0) == 0)
906 goto packet_error;
907 sum += new_char & 0xff;
908 --length;
909 if (k_state == DATA_TYPE) {
910 /* pass on the data if this is a data packet */
911 k_data_char (new_char);
912 } else if (k_state == SEND_TYPE) {
913 /* save send pack in buffer as is */
914 *send_ptr++ = new_char;
915 /* if too much data, back off the pointer */
916 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
917 --send_ptr;
918 }
919 }
920 /* get and validate checksum character */
088f1b19 921 new_char = getc();
8bde7f77
WD
922 if ((new_char & 0xE0) == 0)
923 goto packet_error;
088f1b19 924 if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
8bde7f77
WD
925 goto packet_error;
926 /* get END_CHAR */
088f1b19 927 new_char = getc();
8bde7f77
WD
928 if (new_char != END_CHAR) {
929 packet_error:
930 /* restore state machines */
931 k_state = k_state_saved;
088f1b19 932 k_data_restore();
8bde7f77 933 /* send a negative acknowledge packet in */
088f1b19 934 send_nack(n);
8bde7f77
WD
935 } else if (k_state == SEND_TYPE) {
936 /* crack the protocol parms, build an appropriate ack packet */
088f1b19 937 handle_send_packet(n);
8bde7f77
WD
938 } else {
939 /* send simple acknowledge packet in */
088f1b19 940 send_ack(n);
8bde7f77
WD
941 /* quit if end of transmission */
942 if (k_state == BREAK_TYPE)
943 done = 1;
944 }
8bde7f77
WD
945 }
946 return ((ulong) os_data_addr - (ulong) bin_start_address);
947}
f2841d37
MK
948
949static int getcxmodem(void) {
cf48eb9a 950 if (tstc())
f2841d37
MK
951 return (getc());
952 return -1;
953}
6e66bd55 954static ulong load_serial_ymodem(ulong offset, int mode)
f2841d37
MK
955{
956 int size;
f2841d37
MK
957 int err;
958 int res;
959 connection_info_t info;
cf48eb9a
WD
960 char ymodemBuf[1024];
961 ulong store_addr = ~0;
962 ulong addr = 0;
f2841d37 963
cf48eb9a 964 size = 0;
6e66bd55 965 info.mode = mode;
088f1b19 966 res = xyzModem_stream_open(&info, &err);
f2841d37 967 if (!res) {
f2841d37 968
cf48eb9a 969 while ((res =
088f1b19 970 xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
cf48eb9a
WD
971 store_addr = addr + offset;
972 size += res;
973 addr += res;
e856bdcf 974#ifdef CONFIG_MTD_NOR_FLASH
088f1b19 975 if (addr2info(store_addr)) {
cf48eb9a
WD
976 int rc;
977
088f1b19 978 rc = flash_write((char *) ymodemBuf,
cf48eb9a
WD
979 store_addr, res);
980 if (rc != 0) {
981 flash_perror (rc);
982 return (~0);
983 }
984 } else
f2841d37 985#endif
cf48eb9a 986 {
088f1b19 987 memcpy((char *)(store_addr), ymodemBuf,
cf48eb9a
WD
988 res);
989 }
990
991 }
992 } else {
088f1b19 993 printf("%s\n", xyzModem_error(err));
f2841d37 994 }
cf48eb9a 995
088f1b19
KP
996 xyzModem_stream_close(&err);
997 xyzModem_stream_terminate(false, &getcxmodem);
f2841d37
MK
998
999
0a6036da 1000 flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
f2841d37 1001
088f1b19 1002 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
41ef372c 1003 setenv_hex("filesize", size);
f2841d37
MK
1004
1005 return offset;
1006}
1007
90253178 1008#endif
8bde7f77
WD
1009
1010/* -------------------------------------------------------------------- */
1011
c76fe474 1012#if defined(CONFIG_CMD_LOADS)
8bde7f77 1013
6d0f6bcf 1014#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
0d498393
WD
1015U_BOOT_CMD(
1016 loads, 3, 0, do_load_serial,
2fb2604d 1017 "load S-Record file over serial line",
8bde7f77
WD
1018 "[ off ] [ baud ]\n"
1019 " - load S-Record file over serial line"
a89c33db 1020 " with offset 'off' and baudrate 'baud'"
8bde7f77
WD
1021);
1022
6d0f6bcf 1023#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
0d498393
WD
1024U_BOOT_CMD(
1025 loads, 2, 0, do_load_serial,
2fb2604d 1026 "load S-Record file over serial line",
8bde7f77 1027 "[ off ]\n"
a89c33db 1028 " - load S-Record file over serial line with offset 'off'"
8bde7f77 1029);
6d0f6bcf 1030#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
8bde7f77
WD
1031
1032/*
1033 * SAVES always requires LOADS support, but not vice versa
1034 */
1035
1036
c76fe474 1037#if defined(CONFIG_CMD_SAVES)
6d0f6bcf 1038#ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
0d498393
WD
1039U_BOOT_CMD(
1040 saves, 4, 0, do_save_serial,
2fb2604d 1041 "save S-Record file over serial line",
8bde7f77
WD
1042 "[ off ] [size] [ baud ]\n"
1043 " - save S-Record file over serial line"
a89c33db 1044 " with offset 'off', size 'size' and baudrate 'baud'"
8bde7f77 1045);
6d0f6bcf 1046#else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
0d498393
WD
1047U_BOOT_CMD(
1048 saves, 3, 0, do_save_serial,
2fb2604d 1049 "save S-Record file over serial line",
8bde7f77 1050 "[ off ] [size]\n"
a89c33db 1051 " - save S-Record file over serial line with offset 'off' and size 'size'"
8bde7f77 1052);
6d0f6bcf 1053#endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
70d7cb92
RD
1054#endif /* CONFIG_CMD_SAVES */
1055#endif /* CONFIG_CMD_LOADS */
8bde7f77
WD
1056
1057
c76fe474 1058#if defined(CONFIG_CMD_LOADB)
0d498393
WD
1059U_BOOT_CMD(
1060 loadb, 3, 0, do_load_serial_bin,
2fb2604d 1061 "load binary file over serial line (kermit mode)",
8bde7f77
WD
1062 "[ off ] [ baud ]\n"
1063 " - load binary file over serial line"
6e66bd55
AA
1064 " with offset 'off' and baudrate 'baud'"
1065);
1066
1067U_BOOT_CMD(
1068 loadx, 3, 0, do_load_serial_bin,
1069 "load binary file over serial line (xmodem mode)",
1070 "[ off ] [ baud ]\n"
1071 " - load binary file over serial line"
a89c33db 1072 " with offset 'off' and baudrate 'baud'"
8bde7f77
WD
1073);
1074
f2841d37
MK
1075U_BOOT_CMD(
1076 loady, 3, 0, do_load_serial_bin,
2fb2604d 1077 "load binary file over serial line (ymodem mode)",
f2841d37
MK
1078 "[ off ] [ baud ]\n"
1079 " - load binary file over serial line"
a89c33db 1080 " with offset 'off' and baudrate 'baud'"
f2841d37
MK
1081);
1082
70d7cb92 1083#endif /* CONFIG_CMD_LOADB */