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