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