cmd_load.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
  32. static ulong load_serial (ulong offset);
  33. static int read_record (char *buf, ulong len);
  34. # if (CONFIG_COMMANDS & CFG_CMD_SAVES)
  35. static int save_serial (ulong offset, ulong size);
  36. static int write_record (char *buf);
  37. # endif /* CFG_CMD_SAVES */
  38. static int do_echo = 1;
  39. #endif /* CFG_CMD_LOADS */
  40. /* -------------------------------------------------------------------- */
  41. #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
  42. int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  43. {
  44. ulong offset = 0;
  45. ulong addr;
  46. int i;
  47. char *env_echo;
  48. int rcode = 0;
  49. #ifdef CFG_LOADS_BAUD_CHANGE
  50. DECLARE_GLOBAL_DATA_PTR;
  51. int load_baudrate, current_baudrate;
  52. load_baudrate = current_baudrate = gd->baudrate;
  53. #endif
  54. if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
  55. do_echo = 1;
  56. } else {
  57. do_echo = 0;
  58. }
  59. #ifdef CFG_LOADS_BAUD_CHANGE
  60. if (argc >= 2) {
  61. offset = simple_strtoul(argv[1], NULL, 16);
  62. }
  63. if (argc == 3) {
  64. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  65. /* default to current baudrate */
  66. if (load_baudrate == 0)
  67. load_baudrate = current_baudrate;
  68. }
  69. if (load_baudrate != current_baudrate) {
  70. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  71. load_baudrate);
  72. udelay(50000);
  73. gd->baudrate = load_baudrate;
  74. serial_setbrg ();
  75. udelay(50000);
  76. for (;;) {
  77. if (getc() == '\r')
  78. break;
  79. }
  80. }
  81. #else /* ! CFG_LOADS_BAUD_CHANGE */
  82. if (argc == 2) {
  83. offset = simple_strtoul(argv[1], NULL, 16);
  84. }
  85. #endif /* CFG_LOADS_BAUD_CHANGE */
  86. printf ("## Ready for S-Record download ...\n");
  87. addr = load_serial (offset);
  88. /*
  89. * Gather any trailing characters (for instance, the ^D which
  90. * is sent by 'cu' after sending a file), and give the
  91. * box some time (100 * 1 ms)
  92. */
  93. for (i=0; i<100; ++i) {
  94. if (tstc()) {
  95. (void) getc();
  96. }
  97. udelay(1000);
  98. }
  99. if (addr == ~0) {
  100. printf ("## S-Record download aborted\n");
  101. rcode = 1;
  102. } else {
  103. printf ("## Start Addr = 0x%08lX\n", addr);
  104. load_addr = addr;
  105. }
  106. #ifdef CFG_LOADS_BAUD_CHANGE
  107. if (load_baudrate != current_baudrate) {
  108. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  109. current_baudrate);
  110. udelay (50000);
  111. gd->baudrate = current_baudrate;
  112. serial_setbrg ();
  113. udelay (50000);
  114. for (;;) {
  115. if (getc() == 0x1B) /* ESC */
  116. break;
  117. }
  118. }
  119. #endif
  120. return rcode;
  121. }
  122. static ulong
  123. load_serial (ulong offset)
  124. {
  125. char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
  126. char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
  127. int binlen; /* no. of data bytes in S-Rec. */
  128. int type; /* return code for record type */
  129. ulong addr; /* load address from S-Record */
  130. ulong size; /* number of bytes transferred */
  131. char buf[32];
  132. ulong store_addr;
  133. ulong start_addr = ~0;
  134. ulong end_addr = 0;
  135. int line_count = 0;
  136. while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
  137. type = srec_decode (record, &binlen, &addr, binbuf);
  138. if (type < 0) {
  139. return (~0); /* Invalid S-Record */
  140. }
  141. switch (type) {
  142. case SREC_DATA2:
  143. case SREC_DATA3:
  144. case SREC_DATA4:
  145. store_addr = addr + offset;
  146. #ifndef CFG_NO_FLASH
  147. if (addr2info(store_addr)) {
  148. int rc;
  149. rc = flash_write((uchar *)binbuf,store_addr,binlen);
  150. if (rc != 0) {
  151. flash_perror (rc);
  152. return (~0);
  153. }
  154. } else
  155. #endif
  156. {
  157. memcpy ((char *)(store_addr), binbuf, binlen);
  158. }
  159. if ((store_addr) < start_addr)
  160. start_addr = store_addr;
  161. if ((store_addr + binlen - 1) > end_addr)
  162. end_addr = store_addr + binlen - 1;
  163. break;
  164. case SREC_END2:
  165. case SREC_END3:
  166. case SREC_END4:
  167. udelay (10000);
  168. size = end_addr - start_addr + 1;
  169. printf ("\n"
  170. "## First Load Addr = 0x%08lX\n"
  171. "## Last Load Addr = 0x%08lX\n"
  172. "## Total Size = 0x%08lX = %ld Bytes\n",
  173. start_addr, end_addr, size, size
  174. );
  175. flush_cache (start_addr, size);
  176. sprintf(buf, "%lX", size);
  177. setenv("filesize", buf);
  178. return (addr);
  179. case SREC_START:
  180. break;
  181. default:
  182. break;
  183. }
  184. if (!do_echo) { /* print a '.' every 100 lines */
  185. if ((++line_count % 100) == 0)
  186. putc ('.');
  187. }
  188. }
  189. return (~0); /* Download aborted */
  190. }
  191. static int
  192. read_record (char *buf, ulong len)
  193. {
  194. DECLARE_GLOBAL_DATA_PTR;
  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 (CONFIG_COMMANDS & CFG_CMD_SAVES)
  225. int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  226. {
  227. ulong offset = 0;
  228. ulong size = 0;
  229. #ifdef CFG_LOADS_BAUD_CHANGE
  230. DECLARE_GLOBAL_DATA_PTR;
  231. int save_baudrate, current_baudrate;
  232. save_baudrate = current_baudrate = gd->baudrate;
  233. #endif
  234. if (argc >= 2) {
  235. offset = simple_strtoul(argv[1], NULL, 16);
  236. }
  237. #ifdef CFG_LOADS_BAUD_CHANGE
  238. if (argc >= 3) {
  239. size = simple_strtoul(argv[2], NULL, 16);
  240. }
  241. if (argc == 4) {
  242. save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
  243. /* default to current baudrate */
  244. if (save_baudrate == 0)
  245. save_baudrate = current_baudrate;
  246. }
  247. if (save_baudrate != current_baudrate) {
  248. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  249. save_baudrate);
  250. udelay(50000);
  251. gd->baudrate = save_baudrate;
  252. serial_setbrg ();
  253. udelay(50000);
  254. for (;;) {
  255. if (getc() == '\r')
  256. break;
  257. }
  258. }
  259. #else /* ! CFG_LOADS_BAUD_CHANGE */
  260. if (argc == 3) {
  261. size = simple_strtoul(argv[2], NULL, 16);
  262. }
  263. #endif /* CFG_LOADS_BAUD_CHANGE */
  264. printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
  265. for (;;) {
  266. if (getc() == '\r')
  267. break;
  268. }
  269. if(save_serial (offset, size)) {
  270. printf ("## S-Record upload aborted\n");
  271. } else {
  272. printf ("## S-Record upload complete\n");
  273. }
  274. #ifdef CFG_LOADS_BAUD_CHANGE
  275. if (save_baudrate != current_baudrate) {
  276. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  277. (int)current_baudrate);
  278. udelay (50000);
  279. gd->baudrate = current_baudrate;
  280. serial_setbrg ();
  281. udelay (50000);
  282. for (;;) {
  283. if (getc() == 0x1B) /* ESC */
  284. break;
  285. }
  286. }
  287. #endif
  288. return 0;
  289. }
  290. #define SREC3_START "S0030000FC\n"
  291. #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
  292. #define SREC3_END "S70500000000FA\n"
  293. #define SREC_BYTES_PER_RECORD 16
  294. static int save_serial (ulong address, ulong count)
  295. {
  296. int i, c, reclen, checksum, length;
  297. char *hex = "0123456789ABCDEF";
  298. char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
  299. char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
  300. reclen = 0;
  301. checksum = 0;
  302. if(write_record(SREC3_START)) /* write the header */
  303. return (-1);
  304. do {
  305. if(count) { /* collect hex data in the buffer */
  306. c = *(volatile uchar*)(address + reclen); /* get one byte */
  307. checksum += c; /* accumulate checksum */
  308. data[2*reclen] = hex[(c>>4)&0x0f];
  309. data[2*reclen+1] = hex[c & 0x0f];
  310. data[2*reclen+2] = '\0';
  311. ++reclen;
  312. --count;
  313. }
  314. if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
  315. /* enough data collected for one record: dump it */
  316. if(reclen) { /* build & write a data record: */
  317. /* address + data + checksum */
  318. length = 4 + reclen + 1;
  319. /* accumulate length bytes into checksum */
  320. for(i = 0; i < 2; i++)
  321. checksum += (length >> (8*i)) & 0xff;
  322. /* accumulate address bytes into checksum: */
  323. for(i = 0; i < 4; i++)
  324. checksum += (address >> (8*i)) & 0xff;
  325. /* make proper checksum byte: */
  326. checksum = ~checksum & 0xff;
  327. /* output one record: */
  328. sprintf(record, SREC3_FORMAT, length, address, data, checksum);
  329. if(write_record(record))
  330. return (-1);
  331. }
  332. address += reclen; /* increment address */
  333. checksum = 0;
  334. reclen = 0;
  335. }
  336. }
  337. while(count);
  338. if(write_record(SREC3_END)) /* write the final record */
  339. return (-1);
  340. return(0);
  341. }
  342. static int
  343. 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 /* CFG_CMD_SAVES */
  355. #endif /* CFG_CMD_LOADS */
  356. #if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
  357. #define XON_CHAR 17
  358. #define XOFF_CHAR 19
  359. #define START_CHAR 0x01
  360. #define ETX_CHAR 0x03
  361. #define END_CHAR 0x0D
  362. #define SPACE 0x20
  363. #define K_ESCAPE 0x23
  364. #define SEND_TYPE 'S'
  365. #define DATA_TYPE 'D'
  366. #define ACK_TYPE 'Y'
  367. #define NACK_TYPE 'N'
  368. #define BREAK_TYPE 'B'
  369. #define tochar(x) ((char) (((x) + SPACE) & 0xff))
  370. #define untochar(x) ((int) (((x) - SPACE) & 0xff))
  371. extern int os_data_count;
  372. extern int os_data_header[8];
  373. static void set_kerm_bin_mode(unsigned long *);
  374. static int k_recv(void);
  375. static ulong load_serial_bin (ulong offset);
  376. char his_eol; /* character he needs at end of packet */
  377. int his_pad_count; /* number of pad chars he needs */
  378. char his_pad_char; /* pad chars he needs */
  379. char his_quote; /* quote chars he'll use */
  380. int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  381. {
  382. DECLARE_GLOBAL_DATA_PTR;
  383. ulong offset = 0;
  384. ulong addr;
  385. int load_baudrate, current_baudrate;
  386. int rcode = 0;
  387. char *s;
  388. /* pre-set offset from CFG_LOAD_ADDR */
  389. offset = CFG_LOAD_ADDR;
  390. /* pre-set offset from $loadaddr */
  391. if ((s = getenv("loadaddr")) != NULL) {
  392. offset = simple_strtoul(s, NULL, 16);
  393. }
  394. load_baudrate = current_baudrate = gd->baudrate;
  395. if (argc >= 2) {
  396. offset = simple_strtoul(argv[1], NULL, 16);
  397. }
  398. if (argc == 3) {
  399. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  400. /* default to current baudrate */
  401. if (load_baudrate == 0)
  402. load_baudrate = current_baudrate;
  403. }
  404. if (load_baudrate != current_baudrate) {
  405. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  406. load_baudrate);
  407. udelay(50000);
  408. gd->baudrate = load_baudrate;
  409. serial_setbrg ();
  410. udelay(50000);
  411. for (;;) {
  412. if (getc() == '\r')
  413. break;
  414. }
  415. }
  416. printf ("## Ready for binary (kermit) download "
  417. "to 0x%08lX at %d bps...\n",
  418. offset,
  419. load_baudrate);
  420. addr = load_serial_bin (offset);
  421. if (addr == ~0) {
  422. load_addr = 0;
  423. printf ("## Binary (kermit) download aborted\n");
  424. rcode = 1;
  425. } else {
  426. printf ("## Start Addr = 0x%08lX\n", addr);
  427. load_addr = addr;
  428. }
  429. if (load_baudrate != current_baudrate) {
  430. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  431. current_baudrate);
  432. udelay (50000);
  433. gd->baudrate = current_baudrate;
  434. serial_setbrg ();
  435. udelay (50000);
  436. for (;;) {
  437. if (getc() == 0x1B) /* ESC */
  438. break;
  439. }
  440. }
  441. #ifdef CONFIG_AUTOSCRIPT
  442. if (load_addr) {
  443. char *s;
  444. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  445. printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
  446. rcode = autoscript (load_addr);
  447. }
  448. }
  449. #endif
  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. 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. 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. 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. void s1_sendpacket (char *packet)
  500. {
  501. send_pad ();
  502. while (*packet) {
  503. putc (*packet++);
  504. }
  505. }
  506. static char a_b[24];
  507. 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. 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. /* os_data_* takes an OS Open image and puts it into memory, and
  532. puts the boot header in an array named os_data_header
  533. if image is binary, no header is stored in os_data_header.
  534. */
  535. void (*os_data_init) (void);
  536. void (*os_data_char) (char new_char);
  537. static int os_data_state, os_data_state_saved;
  538. int os_data_count;
  539. static int os_data_count_saved;
  540. static char *os_data_addr, *os_data_addr_saved;
  541. static char *bin_start_address;
  542. int os_data_header[8];
  543. static void bin_data_init (void)
  544. {
  545. os_data_state = 0;
  546. os_data_count = 0;
  547. os_data_addr = bin_start_address;
  548. }
  549. static void os_data_save (void)
  550. {
  551. os_data_state_saved = os_data_state;
  552. os_data_count_saved = os_data_count;
  553. os_data_addr_saved = os_data_addr;
  554. }
  555. static void os_data_restore (void)
  556. {
  557. os_data_state = os_data_state_saved;
  558. os_data_count = os_data_count_saved;
  559. os_data_addr = os_data_addr_saved;
  560. }
  561. static void bin_data_char (char new_char)
  562. {
  563. switch (os_data_state) {
  564. case 0: /* data */
  565. *os_data_addr++ = new_char;
  566. --os_data_count;
  567. break;
  568. }
  569. }
  570. static void set_kerm_bin_mode (unsigned long *addr)
  571. {
  572. bin_start_address = (char *) addr;
  573. os_data_init = bin_data_init;
  574. os_data_char = bin_data_char;
  575. }
  576. /* k_data_* simply handles the kermit escape translations */
  577. static int k_data_escape, k_data_escape_saved;
  578. void k_data_init (void)
  579. {
  580. k_data_escape = 0;
  581. os_data_init ();
  582. }
  583. void k_data_save (void)
  584. {
  585. k_data_escape_saved = k_data_escape;
  586. os_data_save ();
  587. }
  588. void k_data_restore (void)
  589. {
  590. k_data_escape = k_data_escape_saved;
  591. os_data_restore ();
  592. }
  593. void k_data_char (char new_char)
  594. {
  595. if (k_data_escape) {
  596. /* last char was escape - translate this character */
  597. os_data_char (ktrans (new_char));
  598. k_data_escape = 0;
  599. } else {
  600. if (new_char == his_quote) {
  601. /* this char is escape - remember */
  602. k_data_escape = 1;
  603. } else {
  604. /* otherwise send this char as-is */
  605. os_data_char (new_char);
  606. }
  607. }
  608. }
  609. #define SEND_DATA_SIZE 20
  610. char send_parms[SEND_DATA_SIZE];
  611. char *send_ptr;
  612. /* handle_send_packet interprits the protocol info and builds and
  613. sends an appropriate ack for what we can do */
  614. void handle_send_packet (int n)
  615. {
  616. int length = 3;
  617. int bytes;
  618. /* initialize some protocol parameters */
  619. his_eol = END_CHAR; /* default end of line character */
  620. his_pad_count = 0;
  621. his_pad_char = '\0';
  622. his_quote = K_ESCAPE;
  623. /* ignore last character if it filled the buffer */
  624. if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
  625. --send_ptr;
  626. bytes = send_ptr - send_parms; /* how many bytes we'll process */
  627. do {
  628. if (bytes-- <= 0)
  629. break;
  630. /* handle MAXL - max length */
  631. /* ignore what he says - most I'll take (here) is 94 */
  632. a_b[++length] = tochar (94);
  633. if (bytes-- <= 0)
  634. break;
  635. /* handle TIME - time you should wait for my packets */
  636. /* ignore what he says - don't wait for my ack longer than 1 second */
  637. a_b[++length] = tochar (1);
  638. if (bytes-- <= 0)
  639. break;
  640. /* handle NPAD - number of pad chars I need */
  641. /* remember what he says - I need none */
  642. his_pad_count = untochar (send_parms[2]);
  643. a_b[++length] = tochar (0);
  644. if (bytes-- <= 0)
  645. break;
  646. /* handle PADC - pad chars I need */
  647. /* remember what he says - I need none */
  648. his_pad_char = ktrans (send_parms[3]);
  649. a_b[++length] = 0x40; /* He should ignore this */
  650. if (bytes-- <= 0)
  651. break;
  652. /* handle EOL - end of line he needs */
  653. /* remember what he says - I need CR */
  654. his_eol = untochar (send_parms[4]);
  655. a_b[++length] = tochar (END_CHAR);
  656. if (bytes-- <= 0)
  657. break;
  658. /* handle QCTL - quote control char he'll use */
  659. /* remember what he says - I'll use '#' */
  660. his_quote = send_parms[5];
  661. a_b[++length] = '#';
  662. if (bytes-- <= 0)
  663. break;
  664. /* handle QBIN - 8-th bit prefixing */
  665. /* ignore what he says - I refuse */
  666. a_b[++length] = 'N';
  667. if (bytes-- <= 0)
  668. break;
  669. /* handle CHKT - the clock check type */
  670. /* ignore what he says - I do type 1 (for now) */
  671. a_b[++length] = '1';
  672. if (bytes-- <= 0)
  673. break;
  674. /* handle REPT - the repeat prefix */
  675. /* ignore what he says - I refuse (for now) */
  676. a_b[++length] = 'N';
  677. if (bytes-- <= 0)
  678. break;
  679. /* handle CAPAS - the capabilities mask */
  680. /* ignore what he says - I only do long packets - I don't do windows */
  681. a_b[++length] = tochar (2); /* only long packets */
  682. a_b[++length] = tochar (0); /* no windows */
  683. a_b[++length] = tochar (94); /* large packet msb */
  684. a_b[++length] = tochar (94); /* large packet lsb */
  685. } while (0);
  686. a_b[0] = START_CHAR;
  687. a_b[1] = tochar (length);
  688. a_b[2] = tochar (n);
  689. a_b[3] = ACK_TYPE;
  690. a_b[++length] = '\0';
  691. a_b[length] = tochar (chk1 (&a_b[1]));
  692. a_b[++length] = his_eol;
  693. a_b[++length] = '\0';
  694. s1_sendpacket (a_b);
  695. }
  696. /* k_recv receives a OS Open image file over kermit line */
  697. static int k_recv (void)
  698. {
  699. char new_char;
  700. char k_state, k_state_saved;
  701. int sum;
  702. int done;
  703. int length;
  704. int n, last_n;
  705. int z = 0;
  706. int len_lo, len_hi;
  707. /* initialize some protocol parameters */
  708. his_eol = END_CHAR; /* default end of line character */
  709. his_pad_count = 0;
  710. his_pad_char = '\0';
  711. his_quote = K_ESCAPE;
  712. /* initialize the k_recv and k_data state machine */
  713. done = 0;
  714. k_state = 0;
  715. k_data_init ();
  716. k_state_saved = k_state;
  717. k_data_save ();
  718. n = 0; /* just to get rid of a warning */
  719. last_n = -1;
  720. /* expect this "type" sequence (but don't check):
  721. S: send initiate
  722. F: file header
  723. D: data (multiple)
  724. Z: end of file
  725. B: break transmission
  726. */
  727. /* enter main loop */
  728. while (!done) {
  729. /* set the send packet pointer to begining of send packet parms */
  730. send_ptr = send_parms;
  731. /* With each packet, start summing the bytes starting with the length.
  732. Save the current sequence number.
  733. Note the type of the packet.
  734. If a character less than SPACE (0x20) is received - error.
  735. */
  736. #if 0
  737. /* OLD CODE, Prior to checking sequence numbers */
  738. /* first have all state machines save current states */
  739. k_state_saved = k_state;
  740. k_data_save ();
  741. #endif
  742. /* get a packet */
  743. /* wait for the starting character or ^C */
  744. for (;;) {
  745. switch (getc ()) {
  746. case START_CHAR: /* start packet */
  747. goto START;
  748. case ETX_CHAR: /* ^C waiting for packet */
  749. return (0);
  750. default:
  751. ;
  752. }
  753. }
  754. START:
  755. /* get length of packet */
  756. sum = 0;
  757. new_char = getc ();
  758. if ((new_char & 0xE0) == 0)
  759. goto packet_error;
  760. sum += new_char & 0xff;
  761. length = untochar (new_char);
  762. /* get sequence number */
  763. new_char = getc ();
  764. if ((new_char & 0xE0) == 0)
  765. goto packet_error;
  766. sum += new_char & 0xff;
  767. n = untochar (new_char);
  768. --length;
  769. /* NEW CODE - check sequence numbers for retried packets */
  770. /* Note - this new code assumes that the sequence number is correctly
  771. * received. Handling an invalid sequence number adds another layer
  772. * of complexity that may not be needed - yet! At this time, I'm hoping
  773. * that I don't need to buffer the incoming data packets and can write
  774. * the data into memory in real time.
  775. */
  776. if (n == last_n) {
  777. /* same sequence number, restore the previous state */
  778. k_state = k_state_saved;
  779. k_data_restore ();
  780. } else {
  781. /* new sequence number, checkpoint the download */
  782. last_n = n;
  783. k_state_saved = k_state;
  784. k_data_save ();
  785. }
  786. /* END NEW CODE */
  787. /* get packet type */
  788. new_char = getc ();
  789. if ((new_char & 0xE0) == 0)
  790. goto packet_error;
  791. sum += new_char & 0xff;
  792. k_state = new_char;
  793. --length;
  794. /* check for extended length */
  795. if (length == -2) {
  796. /* (length byte was 0, decremented twice) */
  797. /* get the two length bytes */
  798. new_char = getc ();
  799. if ((new_char & 0xE0) == 0)
  800. goto packet_error;
  801. sum += new_char & 0xff;
  802. len_hi = untochar (new_char);
  803. new_char = getc ();
  804. if ((new_char & 0xE0) == 0)
  805. goto packet_error;
  806. sum += new_char & 0xff;
  807. len_lo = untochar (new_char);
  808. length = len_hi * 95 + len_lo;
  809. /* check header checksum */
  810. new_char = getc ();
  811. if ((new_char & 0xE0) == 0)
  812. goto packet_error;
  813. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  814. goto packet_error;
  815. sum += new_char & 0xff;
  816. /* --length; */ /* new length includes only data and block check to come */
  817. }
  818. /* bring in rest of packet */
  819. while (length > 1) {
  820. new_char = getc ();
  821. if ((new_char & 0xE0) == 0)
  822. goto packet_error;
  823. sum += new_char & 0xff;
  824. --length;
  825. if (k_state == DATA_TYPE) {
  826. /* pass on the data if this is a data packet */
  827. k_data_char (new_char);
  828. } else if (k_state == SEND_TYPE) {
  829. /* save send pack in buffer as is */
  830. *send_ptr++ = new_char;
  831. /* if too much data, back off the pointer */
  832. if (send_ptr >= &send_parms[SEND_DATA_SIZE])
  833. --send_ptr;
  834. }
  835. }
  836. /* get and validate checksum character */
  837. new_char = getc ();
  838. if ((new_char & 0xE0) == 0)
  839. goto packet_error;
  840. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  841. goto packet_error;
  842. /* get END_CHAR */
  843. new_char = getc ();
  844. if (new_char != END_CHAR) {
  845. packet_error:
  846. /* restore state machines */
  847. k_state = k_state_saved;
  848. k_data_restore ();
  849. /* send a negative acknowledge packet in */
  850. send_nack (n);
  851. } else if (k_state == SEND_TYPE) {
  852. /* crack the protocol parms, build an appropriate ack packet */
  853. handle_send_packet (n);
  854. } else {
  855. /* send simple acknowledge packet in */
  856. send_ack (n);
  857. /* quit if end of transmission */
  858. if (k_state == BREAK_TYPE)
  859. done = 1;
  860. }
  861. ++z;
  862. }
  863. return ((ulong) os_data_addr - (ulong) bin_start_address);
  864. }
  865. #endif /* CFG_CMD_LOADB */
  866. /* -------------------------------------------------------------------- */
  867. #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
  868. #ifdef CFG_LOADS_BAUD_CHANGE
  869. U_BOOT_CMD(
  870. loads, 3, 0, do_load_serial,
  871. "loads - load S-Record file over serial line\n",
  872. "[ off ] [ baud ]\n"
  873. " - load S-Record file over serial line"
  874. " with offset 'off' and baudrate 'baud'\n"
  875. );
  876. #else /* ! CFG_LOADS_BAUD_CHANGE */
  877. U_BOOT_CMD(
  878. loads, 2, 0, do_load_serial,
  879. "loads - load S-Record file over serial line\n",
  880. "[ off ]\n"
  881. " - load S-Record file over serial line with offset 'off'\n"
  882. );
  883. #endif /* CFG_LOADS_BAUD_CHANGE */
  884. /*
  885. * SAVES always requires LOADS support, but not vice versa
  886. */
  887. #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
  888. #ifdef CFG_LOADS_BAUD_CHANGE
  889. U_BOOT_CMD(
  890. saves, 4, 0, do_save_serial,
  891. "saves - save S-Record file over serial line\n",
  892. "[ off ] [size] [ baud ]\n"
  893. " - save S-Record file over serial line"
  894. " with offset 'off', size 'size' and baudrate 'baud'\n"
  895. );
  896. #else /* ! CFG_LOADS_BAUD_CHANGE */
  897. U_BOOT_CMD(
  898. saves, 3, 0, do_save_serial,
  899. "saves - save S-Record file over serial line\n",
  900. "[ off ] [size]\n"
  901. " - save S-Record file over serial line with offset 'off' and size 'size'\n"
  902. );
  903. #endif /* CFG_LOADS_BAUD_CHANGE */
  904. #endif /* CFG_CMD_SAVES */
  905. #endif /* CFG_CMD_LOADS */
  906. #if (CONFIG_COMMANDS & CFG_CMD_LOADB)
  907. U_BOOT_CMD(
  908. loadb, 3, 0, do_load_serial_bin,
  909. "loadb - load binary file over serial line (kermit mode)\n",
  910. "[ off ] [ baud ]\n"
  911. " - load binary file over serial line"
  912. " with offset 'off' and baudrate 'baud'\n"
  913. );
  914. #endif /* CFG_CMD_LOADB */
  915. /* -------------------------------------------------------------------- */
  916. #if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
  917. int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  918. {
  919. extern int hwflow_onoff(int);
  920. if (argc == 2) {
  921. if (strcmp(argv[1], "off") == 0)
  922. hwflow_onoff(-1);
  923. else
  924. if (strcmp(argv[1], "on") == 0)
  925. hwflow_onoff(1);
  926. else
  927. printf("Usage: %s\n", cmdtp->usage);
  928. }
  929. printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
  930. return 0;
  931. }
  932. /* -------------------------------------------------------------------- */
  933. U_BOOT_CMD(
  934. hwflow, 2, 0, do_hwflow,
  935. "hwflow - turn the harwdare flow control on/off\n",
  936. "[on|off]\n - change RTS/CTS hardware flow control over serial line\n"
  937. );
  938. #endif /* CFG_CMD_HWFLOW */