cmd_load.c 26 KB

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