cmd_load.c 26 KB

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