cmd_load.c 26 KB

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