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