cmd_load.c 26 KB

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