cmd_load.c 25 KB

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