cmd_load.c 26 KB

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