cmd_load.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 (ulong 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. ulong offset = 0;
  50. ulong addr;
  51. int i;
  52. char *env_echo;
  53. int rcode = 0;
  54. #ifdef CFG_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 CFG_LOADS_BAUD_CHANGE
  64. if (argc >= 2) {
  65. offset = simple_strtoul(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 /* ! CFG_LOADS_BAUD_CHANGE */
  86. if (argc == 2) {
  87. offset = simple_strtoul(argv[1], NULL, 16);
  88. }
  89. #endif /* CFG_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 CFG_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 (ulong 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 CFG_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 CFG_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 CFG_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 /* ! CFG_LOADS_BAUD_CHANGE */
  262. if (argc == 3) {
  263. size = simple_strtoul(argv[2], NULL, 16);
  264. }
  265. #endif /* CFG_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 CFG_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. extern int os_data_count;
  377. static void set_kerm_bin_mode(unsigned long *);
  378. static int k_recv(void);
  379. static ulong load_serial_bin (ulong offset);
  380. char his_eol; /* character he needs at end of packet */
  381. int his_pad_count; /* number of pad chars he needs */
  382. char his_pad_char; /* pad chars he needs */
  383. char his_quote; /* quote chars he'll use */
  384. int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  385. {
  386. ulong offset = 0;
  387. ulong addr;
  388. int load_baudrate, current_baudrate;
  389. int rcode = 0;
  390. char *s;
  391. /* pre-set offset from CFG_LOAD_ADDR */
  392. offset = CFG_LOAD_ADDR;
  393. /* pre-set offset from $loadaddr */
  394. if ((s = getenv("loadaddr")) != NULL) {
  395. offset = simple_strtoul(s, NULL, 16);
  396. }
  397. load_baudrate = current_baudrate = gd->baudrate;
  398. if (argc >= 2) {
  399. offset = simple_strtoul(argv[1], NULL, 16);
  400. }
  401. if (argc == 3) {
  402. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  403. /* default to current baudrate */
  404. if (load_baudrate == 0)
  405. load_baudrate = current_baudrate;
  406. }
  407. if (load_baudrate != current_baudrate) {
  408. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  409. load_baudrate);
  410. udelay(50000);
  411. gd->baudrate = load_baudrate;
  412. serial_setbrg ();
  413. udelay(50000);
  414. for (;;) {
  415. if (getc() == '\r')
  416. break;
  417. }
  418. }
  419. if (strcmp(argv[0],"loady")==0) {
  420. printf ("## Ready for binary (ymodem) download "
  421. "to 0x%08lX at %d bps...\n",
  422. offset,
  423. load_baudrate);
  424. addr = load_serial_ymodem (offset);
  425. } else {
  426. printf ("## Ready for binary (kermit) download "
  427. "to 0x%08lX at %d bps...\n",
  428. offset,
  429. load_baudrate);
  430. addr = load_serial_bin (offset);
  431. if (addr == ~0) {
  432. load_addr = 0;
  433. printf ("## Binary (kermit) download aborted\n");
  434. rcode = 1;
  435. } else {
  436. printf ("## Start Addr = 0x%08lX\n", addr);
  437. load_addr = addr;
  438. }
  439. }
  440. if (load_baudrate != current_baudrate) {
  441. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  442. current_baudrate);
  443. udelay (50000);
  444. gd->baudrate = current_baudrate;
  445. serial_setbrg ();
  446. udelay (50000);
  447. for (;;) {
  448. if (getc() == 0x1B) /* ESC */
  449. break;
  450. }
  451. }
  452. #ifdef CONFIG_AUTOSCRIPT
  453. if (load_addr) {
  454. char *s;
  455. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  456. printf ("Running autoscript at addr 0x%08lX", load_addr);
  457. s = getenv ("autoscript_uname");
  458. if (s)
  459. printf (":%s ...\n", s);
  460. else
  461. puts (" ...\n");
  462. rcode = autoscript (load_addr, s);
  463. }
  464. }
  465. #endif
  466. return rcode;
  467. }
  468. static ulong load_serial_bin (ulong offset)
  469. {
  470. int size, i;
  471. char buf[32];
  472. set_kerm_bin_mode ((ulong *) offset);
  473. size = k_recv ();
  474. /*
  475. * Gather any trailing characters (for instance, the ^D which
  476. * is sent by 'cu' after sending a file), and give the
  477. * box some time (100 * 1 ms)
  478. */
  479. for (i=0; i<100; ++i) {
  480. if (tstc()) {
  481. (void) getc();
  482. }
  483. udelay(1000);
  484. }
  485. flush_cache (offset, size);
  486. printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
  487. sprintf(buf, "%X", size);
  488. setenv("filesize", buf);
  489. return offset;
  490. }
  491. void send_pad (void)
  492. {
  493. int count = his_pad_count;
  494. while (count-- > 0)
  495. putc (his_pad_char);
  496. }
  497. /* converts escaped kermit char to binary char */
  498. char ktrans (char in)
  499. {
  500. if ((in & 0x60) == 0x40) {
  501. return (char) (in & ~0x40);
  502. } else if ((in & 0x7f) == 0x3f) {
  503. return (char) (in | 0x40);
  504. } else
  505. return in;
  506. }
  507. int chk1 (char *buffer)
  508. {
  509. int total = 0;
  510. while (*buffer) {
  511. total += *buffer++;
  512. }
  513. return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
  514. }
  515. void s1_sendpacket (char *packet)
  516. {
  517. send_pad ();
  518. while (*packet) {
  519. putc (*packet++);
  520. }
  521. }
  522. static char a_b[24];
  523. void send_ack (int n)
  524. {
  525. a_b[0] = START_CHAR;
  526. a_b[1] = tochar (3);
  527. a_b[2] = tochar (n);
  528. a_b[3] = ACK_TYPE;
  529. a_b[4] = '\0';
  530. a_b[4] = tochar (chk1 (&a_b[1]));
  531. a_b[5] = his_eol;
  532. a_b[6] = '\0';
  533. s1_sendpacket (a_b);
  534. }
  535. void send_nack (int n)
  536. {
  537. a_b[0] = START_CHAR;
  538. a_b[1] = tochar (3);
  539. a_b[2] = tochar (n);
  540. a_b[3] = NACK_TYPE;
  541. a_b[4] = '\0';
  542. a_b[4] = tochar (chk1 (&a_b[1]));
  543. a_b[5] = his_eol;
  544. a_b[6] = '\0';
  545. s1_sendpacket (a_b);
  546. }
  547. void (*os_data_init) (void);
  548. void (*os_data_char) (char new_char);
  549. static int os_data_state, os_data_state_saved;
  550. int os_data_count;
  551. static int os_data_count_saved;
  552. static char *os_data_addr, *os_data_addr_saved;
  553. static char *bin_start_address;
  554. static void bin_data_init (void)
  555. {
  556. os_data_state = 0;
  557. os_data_count = 0;
  558. os_data_addr = bin_start_address;
  559. }
  560. static void os_data_save (void)
  561. {
  562. os_data_state_saved = os_data_state;
  563. os_data_count_saved = os_data_count;
  564. os_data_addr_saved = os_data_addr;
  565. }
  566. static void os_data_restore (void)
  567. {
  568. os_data_state = os_data_state_saved;
  569. os_data_count = os_data_count_saved;
  570. os_data_addr = os_data_addr_saved;
  571. }
  572. static void bin_data_char (char new_char)
  573. {
  574. switch (os_data_state) {
  575. case 0: /* data */
  576. *os_data_addr++ = new_char;
  577. --os_data_count;
  578. break;
  579. }
  580. }
  581. static void set_kerm_bin_mode (unsigned long *addr)
  582. {
  583. bin_start_address = (char *) addr;
  584. os_data_init = bin_data_init;
  585. os_data_char = bin_data_char;
  586. }
  587. /* k_data_* simply handles the kermit escape translations */
  588. static int k_data_escape, k_data_escape_saved;
  589. void k_data_init (void)
  590. {
  591. k_data_escape = 0;
  592. os_data_init ();
  593. }
  594. void k_data_save (void)
  595. {
  596. k_data_escape_saved = k_data_escape;
  597. os_data_save ();
  598. }
  599. void k_data_restore (void)
  600. {
  601. k_data_escape = k_data_escape_saved;
  602. os_data_restore ();
  603. }
  604. void k_data_char (char new_char)
  605. {
  606. if (k_data_escape) {
  607. /* last char was escape - translate this character */
  608. os_data_char (ktrans (new_char));
  609. k_data_escape = 0;
  610. } else {
  611. if (new_char == his_quote) {
  612. /* this char is escape - remember */
  613. k_data_escape = 1;
  614. } else {
  615. /* otherwise send this char as-is */
  616. os_data_char (new_char);
  617. }
  618. }
  619. }
  620. #define SEND_DATA_SIZE 20
  621. char send_parms[SEND_DATA_SIZE];
  622. char *send_ptr;
  623. /* handle_send_packet interprits the protocol info and builds and
  624. sends an appropriate ack for what we can do */
  625. void handle_send_packet (int n)
  626. {
  627. int length = 3;
  628. int bytes;
  629. /* initialize some protocol parameters */
  630. his_eol = END_CHAR; /* default end of line character */
  631. his_pad_count = 0;
  632. his_pad_char = '\0';
  633. his_quote = K_ESCAPE;
  634. /* ignore last character if it filled the buffer */
  635. if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
  636. --send_ptr;
  637. bytes = send_ptr - send_parms; /* how many bytes we'll process */
  638. do {
  639. if (bytes-- <= 0)
  640. break;
  641. /* handle MAXL - max length */
  642. /* ignore what he says - most I'll take (here) is 94 */
  643. a_b[++length] = tochar (94);
  644. if (bytes-- <= 0)
  645. break;
  646. /* handle TIME - time you should wait for my packets */
  647. /* ignore what he says - don't wait for my ack longer than 1 second */
  648. a_b[++length] = tochar (1);
  649. if (bytes-- <= 0)
  650. break;
  651. /* handle NPAD - number of pad chars I need */
  652. /* remember what he says - I need none */
  653. his_pad_count = untochar (send_parms[2]);
  654. a_b[++length] = tochar (0);
  655. if (bytes-- <= 0)
  656. break;
  657. /* handle PADC - pad chars I need */
  658. /* remember what he says - I need none */
  659. his_pad_char = ktrans (send_parms[3]);
  660. a_b[++length] = 0x40; /* He should ignore this */
  661. if (bytes-- <= 0)
  662. break;
  663. /* handle EOL - end of line he needs */
  664. /* remember what he says - I need CR */
  665. his_eol = untochar (send_parms[4]);
  666. a_b[++length] = tochar (END_CHAR);
  667. if (bytes-- <= 0)
  668. break;
  669. /* handle QCTL - quote control char he'll use */
  670. /* remember what he says - I'll use '#' */
  671. his_quote = send_parms[5];
  672. a_b[++length] = '#';
  673. if (bytes-- <= 0)
  674. break;
  675. /* handle QBIN - 8-th bit prefixing */
  676. /* ignore what he says - I refuse */
  677. a_b[++length] = 'N';
  678. if (bytes-- <= 0)
  679. break;
  680. /* handle CHKT - the clock check type */
  681. /* ignore what he says - I do type 1 (for now) */
  682. a_b[++length] = '1';
  683. if (bytes-- <= 0)
  684. break;
  685. /* handle REPT - the repeat prefix */
  686. /* ignore what he says - I refuse (for now) */
  687. a_b[++length] = 'N';
  688. if (bytes-- <= 0)
  689. break;
  690. /* handle CAPAS - the capabilities mask */
  691. /* ignore what he says - I only do long packets - I don't do windows */
  692. a_b[++length] = tochar (2); /* only long packets */
  693. a_b[++length] = tochar (0); /* no windows */
  694. a_b[++length] = tochar (94); /* large packet msb */
  695. a_b[++length] = tochar (94); /* large packet lsb */
  696. } while (0);
  697. a_b[0] = START_CHAR;
  698. a_b[1] = tochar (length);
  699. a_b[2] = tochar (n);
  700. a_b[3] = ACK_TYPE;
  701. a_b[++length] = '\0';
  702. a_b[length] = tochar (chk1 (&a_b[1]));
  703. a_b[++length] = his_eol;
  704. a_b[++length] = '\0';
  705. s1_sendpacket (a_b);
  706. }
  707. /* k_recv receives a OS Open image file over kermit line */
  708. static int k_recv (void)
  709. {
  710. char new_char;
  711. char k_state, k_state_saved;
  712. int sum;
  713. int done;
  714. int length;
  715. int n, last_n;
  716. int z = 0;
  717. int len_lo, len_hi;
  718. /* initialize some protocol parameters */
  719. his_eol = END_CHAR; /* default end of line character */
  720. his_pad_count = 0;
  721. his_pad_char = '\0';
  722. his_quote = K_ESCAPE;
  723. /* initialize the k_recv and k_data state machine */
  724. done = 0;
  725. k_state = 0;
  726. k_data_init ();
  727. k_state_saved = k_state;
  728. k_data_save ();
  729. n = 0; /* just to get rid of a warning */
  730. last_n = -1;
  731. /* expect this "type" sequence (but don't check):
  732. S: send initiate
  733. F: file header
  734. D: data (multiple)
  735. Z: end of file
  736. B: break transmission
  737. */
  738. /* enter main loop */
  739. while (!done) {
  740. /* set the send packet pointer to begining of send packet parms */
  741. send_ptr = send_parms;
  742. /* With each packet, start summing the bytes starting with the length.
  743. Save the current sequence number.
  744. Note the type of the packet.
  745. If a character less than SPACE (0x20) is received - error.
  746. */
  747. #if 0
  748. /* OLD CODE, Prior to checking sequence numbers */
  749. /* first have all state machines save current states */
  750. k_state_saved = k_state;
  751. k_data_save ();
  752. #endif
  753. /* get a packet */
  754. /* wait for the starting character or ^C */
  755. for (;;) {
  756. switch (getc ()) {
  757. case START_CHAR: /* start packet */
  758. goto START;
  759. case ETX_CHAR: /* ^C waiting for packet */
  760. return (0);
  761. default:
  762. ;
  763. }
  764. }
  765. START:
  766. /* get length of packet */
  767. sum = 0;
  768. new_char = getc ();
  769. if ((new_char & 0xE0) == 0)
  770. goto packet_error;
  771. sum += new_char & 0xff;
  772. length = untochar (new_char);
  773. /* get sequence number */
  774. new_char = getc ();
  775. if ((new_char & 0xE0) == 0)
  776. goto packet_error;
  777. sum += new_char & 0xff;
  778. n = untochar (new_char);
  779. --length;
  780. /* NEW CODE - check sequence numbers for retried packets */
  781. /* Note - this new code assumes that the sequence number is correctly
  782. * received. Handling an invalid sequence number adds another layer
  783. * of complexity that may not be needed - yet! At this time, I'm hoping
  784. * that I don't need to buffer the incoming data packets and can write
  785. * the data into memory in real time.
  786. */
  787. if (n == last_n) {
  788. /* same sequence number, restore the previous state */
  789. k_state = k_state_saved;
  790. k_data_restore ();
  791. } else {
  792. /* new sequence number, checkpoint the download */
  793. last_n = n;
  794. k_state_saved = k_state;
  795. k_data_save ();
  796. }
  797. /* END NEW CODE */
  798. /* get packet type */
  799. new_char = getc ();
  800. if ((new_char & 0xE0) == 0)
  801. goto packet_error;
  802. sum += new_char & 0xff;
  803. k_state = new_char;
  804. --length;
  805. /* check for extended length */
  806. if (length == -2) {
  807. /* (length byte was 0, decremented twice) */
  808. /* get the two length bytes */
  809. new_char = getc ();
  810. if ((new_char & 0xE0) == 0)
  811. goto packet_error;
  812. sum += new_char & 0xff;
  813. len_hi = untochar (new_char);
  814. new_char = getc ();
  815. if ((new_char & 0xE0) == 0)
  816. goto packet_error;
  817. sum += new_char & 0xff;
  818. len_lo = untochar (new_char);
  819. length = len_hi * 95 + len_lo;
  820. /* check header checksum */
  821. new_char = getc ();
  822. if ((new_char & 0xE0) == 0)
  823. goto packet_error;
  824. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  825. goto packet_error;
  826. sum += new_char & 0xff;
  827. /* --length; */ /* new length includes only data and block check to come */
  828. }
  829. /* bring in rest of packet */
  830. while (length > 1) {
  831. new_char = getc ();
  832. if ((new_char & 0xE0) == 0)
  833. goto packet_error;
  834. sum += new_char & 0xff;
  835. --length;
  836. if (k_state == DATA_TYPE) {
  837. /* pass on the data if this is a data packet */
  838. k_data_char (new_char);
  839. } else if (k_state == SEND_TYPE) {
  840. /* save send pack in buffer as is */
  841. *send_ptr++ = new_char;
  842. /* if too much data, back off the pointer */
  843. if (send_ptr >= &send_parms[SEND_DATA_SIZE])
  844. --send_ptr;
  845. }
  846. }
  847. /* get and validate checksum character */
  848. new_char = getc ();
  849. if ((new_char & 0xE0) == 0)
  850. goto packet_error;
  851. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  852. goto packet_error;
  853. /* get END_CHAR */
  854. new_char = getc ();
  855. if (new_char != END_CHAR) {
  856. packet_error:
  857. /* restore state machines */
  858. k_state = k_state_saved;
  859. k_data_restore ();
  860. /* send a negative acknowledge packet in */
  861. send_nack (n);
  862. } else if (k_state == SEND_TYPE) {
  863. /* crack the protocol parms, build an appropriate ack packet */
  864. handle_send_packet (n);
  865. } else {
  866. /* send simple acknowledge packet in */
  867. send_ack (n);
  868. /* quit if end of transmission */
  869. if (k_state == BREAK_TYPE)
  870. done = 1;
  871. }
  872. ++z;
  873. }
  874. return ((ulong) os_data_addr - (ulong) bin_start_address);
  875. }
  876. static int getcxmodem(void) {
  877. if (tstc())
  878. return (getc());
  879. return -1;
  880. }
  881. static ulong load_serial_ymodem (ulong offset)
  882. {
  883. int size;
  884. char buf[32];
  885. int err;
  886. int res;
  887. connection_info_t info;
  888. char ymodemBuf[1024];
  889. ulong store_addr = ~0;
  890. ulong addr = 0;
  891. size = 0;
  892. info.mode = xyzModem_ymodem;
  893. res = xyzModem_stream_open (&info, &err);
  894. if (!res) {
  895. while ((res =
  896. xyzModem_stream_read (ymodemBuf, 1024, &err)) > 0) {
  897. store_addr = addr + offset;
  898. size += res;
  899. addr += res;
  900. #ifndef CFG_NO_FLASH
  901. if (addr2info (store_addr)) {
  902. int rc;
  903. rc = flash_write ((char *) ymodemBuf,
  904. store_addr, res);
  905. if (rc != 0) {
  906. flash_perror (rc);
  907. return (~0);
  908. }
  909. } else
  910. #endif
  911. {
  912. memcpy ((char *) (store_addr), ymodemBuf,
  913. res);
  914. }
  915. }
  916. } else {
  917. printf ("%s\n", xyzModem_error (err));
  918. }
  919. xyzModem_stream_close (&err);
  920. xyzModem_stream_terminate (false, &getcxmodem);
  921. flush_cache (offset, size);
  922. printf ("## Total Size = 0x%08x = %d Bytes\n", size, size);
  923. sprintf (buf, "%X", size);
  924. setenv ("filesize", buf);
  925. return offset;
  926. }
  927. #endif
  928. /* -------------------------------------------------------------------- */
  929. #if defined(CONFIG_CMD_LOADS)
  930. #ifdef CFG_LOADS_BAUD_CHANGE
  931. U_BOOT_CMD(
  932. loads, 3, 0, do_load_serial,
  933. "loads - load S-Record file over serial line\n",
  934. "[ off ] [ baud ]\n"
  935. " - load S-Record file over serial line"
  936. " with offset 'off' and baudrate 'baud'\n"
  937. );
  938. #else /* ! CFG_LOADS_BAUD_CHANGE */
  939. U_BOOT_CMD(
  940. loads, 2, 0, do_load_serial,
  941. "loads - load S-Record file over serial line\n",
  942. "[ off ]\n"
  943. " - load S-Record file over serial line with offset 'off'\n"
  944. );
  945. #endif /* CFG_LOADS_BAUD_CHANGE */
  946. /*
  947. * SAVES always requires LOADS support, but not vice versa
  948. */
  949. #if defined(CONFIG_CMD_SAVES)
  950. #ifdef CFG_LOADS_BAUD_CHANGE
  951. U_BOOT_CMD(
  952. saves, 4, 0, do_save_serial,
  953. "saves - save S-Record file over serial line\n",
  954. "[ off ] [size] [ baud ]\n"
  955. " - save S-Record file over serial line"
  956. " with offset 'off', size 'size' and baudrate 'baud'\n"
  957. );
  958. #else /* ! CFG_LOADS_BAUD_CHANGE */
  959. U_BOOT_CMD(
  960. saves, 3, 0, do_save_serial,
  961. "saves - save S-Record file over serial line\n",
  962. "[ off ] [size]\n"
  963. " - save S-Record file over serial line with offset 'off' and size 'size'\n"
  964. );
  965. #endif /* CFG_LOADS_BAUD_CHANGE */
  966. #endif
  967. #endif
  968. #if defined(CONFIG_CMD_LOADB)
  969. U_BOOT_CMD(
  970. loadb, 3, 0, do_load_serial_bin,
  971. "loadb - load binary file over serial line (kermit mode)\n",
  972. "[ off ] [ baud ]\n"
  973. " - load binary file over serial line"
  974. " with offset 'off' and baudrate 'baud'\n"
  975. );
  976. U_BOOT_CMD(
  977. loady, 3, 0, do_load_serial_bin,
  978. "loady - load binary file over serial line (ymodem mode)\n",
  979. "[ off ] [ baud ]\n"
  980. " - load binary file over serial line"
  981. " with offset 'off' and baudrate 'baud'\n"
  982. );
  983. #endif
  984. /* -------------------------------------------------------------------- */
  985. #if defined(CONFIG_CMD_HWFLOW)
  986. int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  987. {
  988. extern int hwflow_onoff(int);
  989. if (argc == 2) {
  990. if (strcmp(argv[1], "off") == 0)
  991. hwflow_onoff(-1);
  992. else
  993. if (strcmp(argv[1], "on") == 0)
  994. hwflow_onoff(1);
  995. else
  996. printf("Usage: %s\n", cmdtp->usage);
  997. }
  998. printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
  999. return 0;
  1000. }
  1001. /* -------------------------------------------------------------------- */
  1002. U_BOOT_CMD(
  1003. hwflow, 2, 0, do_hwflow,
  1004. "hwflow - turn the harwdare flow control on/off\n",
  1005. "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
  1006. );
  1007. #endif