cmd_load.c 26 KB

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