xyzModem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. *==========================================================================
  3. *
  4. * xyzModem.c
  5. *
  6. * RedBoot stream handler for xyzModem protocol
  7. *
  8. *==========================================================================
  9. *####ECOSGPLCOPYRIGHTBEGIN####
  10. * -------------------------------------------
  11. * This file is part of eCos, the Embedded Configurable Operating System.
  12. * Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
  13. * Copyright (C) 2002 Gary Thomas
  14. *
  15. * eCos is free software; you can redistribute it and/or modify it under
  16. * the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 or (at your option) any later version.
  18. *
  19. * eCos is distributed in the hope that it will be useful, but WITHOUT ANY
  20. * WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with eCos; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  27. *
  28. * As a special exception, if other files instantiate templates or use macros
  29. * or inline functions from this file, or you compile this file and link it
  30. * with other works to produce a work based on this file, this file does not
  31. * by itself cause the resulting work to be covered by the GNU General Public
  32. * License. However the source code for this file must still be made available
  33. * in accordance with section (3) of the GNU General Public License.
  34. *
  35. * This exception does not invalidate any other reasons why a work based on
  36. * this file might be covered by the GNU General Public License.
  37. *
  38. * Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
  39. * at http: *sources.redhat.com/ecos/ecos-license/
  40. * -------------------------------------------
  41. *####ECOSGPLCOPYRIGHTEND####
  42. *==========================================================================
  43. *#####DESCRIPTIONBEGIN####
  44. *
  45. * Author(s): gthomas
  46. * Contributors: gthomas, tsmith, Yoshinori Sato
  47. * Date: 2000-07-14
  48. * Purpose:
  49. * Description:
  50. *
  51. * This code is part of RedBoot (tm).
  52. *
  53. *####DESCRIPTIONEND####
  54. *
  55. *==========================================================================
  56. */
  57. #include <common.h>
  58. #include <xyzModem.h>
  59. #include <stdarg.h>
  60. #include <crc.h>
  61. /* Assumption - run xyzModem protocol over the console port */
  62. /* Values magic to the protocol */
  63. #define SOH 0x01
  64. #define STX 0x02
  65. #define EOT 0x04
  66. #define ACK 0x06
  67. #define BSP 0x08
  68. #define NAK 0x15
  69. #define CAN 0x18
  70. #define EOF 0x1A /* ^Z for DOS officionados */
  71. #define USE_YMODEM_LENGTH
  72. /* Data & state local to the protocol */
  73. static struct
  74. {
  75. #ifdef REDBOOT
  76. hal_virtual_comm_table_t *__chan;
  77. #else
  78. int *__chan;
  79. #endif
  80. unsigned char pkt[1024], *bufp;
  81. unsigned char blk, cblk, crc1, crc2;
  82. unsigned char next_blk; /* Expected block */
  83. int len, mode, total_retries;
  84. int total_SOH, total_STX, total_CAN;
  85. bool crc_mode, at_eof, tx_ack;
  86. #ifdef USE_YMODEM_LENGTH
  87. unsigned long file_length, read_length;
  88. #endif
  89. } xyz;
  90. #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
  91. #define xyzModem_MAX_RETRIES 20
  92. #define xyzModem_MAX_RETRIES_WITH_CRC 10
  93. #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
  94. #ifndef REDBOOT /*SB */
  95. typedef int cyg_int32;
  96. int
  97. CYGACC_COMM_IF_GETC_TIMEOUT (char chan, char *c)
  98. {
  99. #define DELAY 20
  100. unsigned long counter = 0;
  101. while (!tstc () && (counter < xyzModem_CHAR_TIMEOUT * 1000 / DELAY))
  102. {
  103. udelay (DELAY);
  104. counter++;
  105. }
  106. if (tstc ())
  107. {
  108. *c = getc ();
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. void
  114. CYGACC_COMM_IF_PUTC (char x, char y)
  115. {
  116. putc (y);
  117. }
  118. /* Validate a hex character */
  119. __inline__ static bool
  120. _is_hex (char c)
  121. {
  122. return (((c >= '0') && (c <= '9')) ||
  123. ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
  124. }
  125. /* Convert a single hex nibble */
  126. __inline__ static int
  127. _from_hex (char c)
  128. {
  129. int ret = 0;
  130. if ((c >= '0') && (c <= '9'))
  131. {
  132. ret = (c - '0');
  133. }
  134. else if ((c >= 'a') && (c <= 'f'))
  135. {
  136. ret = (c - 'a' + 0x0a);
  137. }
  138. else if ((c >= 'A') && (c <= 'F'))
  139. {
  140. ret = (c - 'A' + 0x0A);
  141. }
  142. return ret;
  143. }
  144. /* Convert a character to lower case */
  145. __inline__ static char
  146. _tolower (char c)
  147. {
  148. if ((c >= 'A') && (c <= 'Z'))
  149. {
  150. c = (c - 'A') + 'a';
  151. }
  152. return c;
  153. }
  154. /* Parse (scan) a number */
  155. bool
  156. parse_num (char *s, unsigned long *val, char **es, char *delim)
  157. {
  158. bool first = true;
  159. int radix = 10;
  160. char c;
  161. unsigned long result = 0;
  162. int digit;
  163. while (*s == ' ')
  164. s++;
  165. while (*s)
  166. {
  167. if (first && (s[0] == '0') && (_tolower (s[1]) == 'x'))
  168. {
  169. radix = 16;
  170. s += 2;
  171. }
  172. first = false;
  173. c = *s++;
  174. if (_is_hex (c) && ((digit = _from_hex (c)) < radix))
  175. {
  176. /* Valid digit */
  177. #ifdef CYGPKG_HAL_MIPS
  178. /* FIXME: tx49 compiler generates 0x2539018 for MUL which */
  179. /* isn't any good. */
  180. if (16 == radix)
  181. result = result << 4;
  182. else
  183. result = 10 * result;
  184. result += digit;
  185. #else
  186. result = (result * radix) + digit;
  187. #endif
  188. }
  189. else
  190. {
  191. if (delim != (char *) 0)
  192. {
  193. /* See if this character is one of the delimiters */
  194. char *dp = delim;
  195. while (*dp && (c != *dp))
  196. dp++;
  197. if (*dp)
  198. break; /* Found a good delimiter */
  199. }
  200. return false; /* Malformatted number */
  201. }
  202. }
  203. *val = result;
  204. if (es != (char **) 0)
  205. {
  206. *es = s;
  207. }
  208. return true;
  209. }
  210. #endif
  211. #define USE_SPRINTF
  212. #ifdef DEBUG
  213. #ifndef USE_SPRINTF
  214. /*
  215. * Note: this debug setup only works if the target platform has two serial ports
  216. * available so that the other one (currently only port 1) can be used for debug
  217. * messages.
  218. */
  219. static int
  220. zm_dprintf (char *fmt, ...)
  221. {
  222. int cur_console;
  223. va_list args;
  224. va_start (args, fmt);
  225. #ifdef REDBOOT
  226. cur_console =
  227. CYGACC_CALL_IF_SET_CONSOLE_COMM
  228. (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
  229. CYGACC_CALL_IF_SET_CONSOLE_COMM (1);
  230. #endif
  231. diag_vprintf (fmt, args);
  232. #ifdef REDBOOT
  233. CYGACC_CALL_IF_SET_CONSOLE_COMM (cur_console);
  234. #endif
  235. }
  236. static void
  237. zm_flush (void)
  238. {
  239. }
  240. #else
  241. /*
  242. * Note: this debug setup works by storing the strings in a fixed buffer
  243. */
  244. #define FINAL
  245. #ifdef FINAL
  246. static char *zm_out = (char *) 0x00380000;
  247. static char *zm_out_start = (char *) 0x00380000;
  248. #else
  249. static char zm_buf[8192];
  250. static char *zm_out = zm_buf;
  251. static char *zm_out_start = zm_buf;
  252. #endif
  253. static int
  254. zm_dprintf (char *fmt, ...)
  255. {
  256. int len;
  257. va_list args;
  258. va_start (args, fmt);
  259. len = diag_vsprintf (zm_out, fmt, args);
  260. zm_out += len;
  261. return len;
  262. }
  263. static void
  264. zm_flush (void)
  265. {
  266. #ifdef REDBOOT
  267. char *p = zm_out_start;
  268. while (*p)
  269. mon_write_char (*p++);
  270. #endif
  271. zm_out = zm_out_start;
  272. }
  273. #endif
  274. static void
  275. zm_dump_buf (void *buf, int len)
  276. {
  277. #ifdef REDBOOT
  278. diag_vdump_buf_with_offset (zm_dprintf, buf, len, 0);
  279. #else
  280. #endif
  281. }
  282. static unsigned char zm_buf[2048];
  283. static unsigned char *zm_bp;
  284. static void
  285. zm_new (void)
  286. {
  287. zm_bp = zm_buf;
  288. }
  289. static void
  290. zm_save (unsigned char c)
  291. {
  292. *zm_bp++ = c;
  293. }
  294. static void
  295. zm_dump (int line)
  296. {
  297. zm_dprintf ("Packet at line: %d\n", line);
  298. zm_dump_buf (zm_buf, zm_bp - zm_buf);
  299. }
  300. #define ZM_DEBUG(x) x
  301. #else
  302. #define ZM_DEBUG(x)
  303. #endif
  304. /* Wait for the line to go idle */
  305. static void
  306. xyzModem_flush (void)
  307. {
  308. int res;
  309. char c;
  310. while (true)
  311. {
  312. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
  313. if (!res)
  314. return;
  315. }
  316. }
  317. static int
  318. xyzModem_get_hdr (void)
  319. {
  320. char c;
  321. int res;
  322. bool hdr_found = false;
  323. int i, can_total, hdr_chars;
  324. unsigned short cksum;
  325. ZM_DEBUG (zm_new ());
  326. /* Find the start of a header */
  327. can_total = 0;
  328. hdr_chars = 0;
  329. if (xyz.tx_ack)
  330. {
  331. CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
  332. xyz.tx_ack = false;
  333. }
  334. while (!hdr_found)
  335. {
  336. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
  337. ZM_DEBUG (zm_save (c));
  338. if (res)
  339. {
  340. hdr_chars++;
  341. switch (c)
  342. {
  343. case SOH:
  344. xyz.total_SOH++;
  345. case STX:
  346. if (c == STX)
  347. xyz.total_STX++;
  348. hdr_found = true;
  349. break;
  350. case CAN:
  351. xyz.total_CAN++;
  352. ZM_DEBUG (zm_dump (__LINE__));
  353. if (++can_total == xyzModem_CAN_COUNT)
  354. {
  355. return xyzModem_cancel;
  356. }
  357. else
  358. {
  359. /* Wait for multiple CAN to avoid early quits */
  360. break;
  361. }
  362. case EOT:
  363. /* EOT only supported if no noise */
  364. if (hdr_chars == 1)
  365. {
  366. CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
  367. ZM_DEBUG (zm_dprintf ("ACK on EOT #%d\n", __LINE__));
  368. ZM_DEBUG (zm_dump (__LINE__));
  369. return xyzModem_eof;
  370. }
  371. default:
  372. /* Ignore, waiting for start of header */
  373. ;
  374. }
  375. }
  376. else
  377. {
  378. /* Data stream timed out */
  379. xyzModem_flush (); /* Toss any current input */
  380. ZM_DEBUG (zm_dump (__LINE__));
  381. CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
  382. return xyzModem_timeout;
  383. }
  384. }
  385. /* Header found, now read the data */
  386. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.blk);
  387. ZM_DEBUG (zm_save (xyz.blk));
  388. if (!res)
  389. {
  390. ZM_DEBUG (zm_dump (__LINE__));
  391. return xyzModem_timeout;
  392. }
  393. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.cblk);
  394. ZM_DEBUG (zm_save (xyz.cblk));
  395. if (!res)
  396. {
  397. ZM_DEBUG (zm_dump (__LINE__));
  398. return xyzModem_timeout;
  399. }
  400. xyz.len = (c == SOH) ? 128 : 1024;
  401. xyz.bufp = xyz.pkt;
  402. for (i = 0; i < xyz.len; i++)
  403. {
  404. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, &c);
  405. ZM_DEBUG (zm_save (c));
  406. if (res)
  407. {
  408. xyz.pkt[i] = c;
  409. }
  410. else
  411. {
  412. ZM_DEBUG (zm_dump (__LINE__));
  413. return xyzModem_timeout;
  414. }
  415. }
  416. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc1);
  417. ZM_DEBUG (zm_save (xyz.crc1));
  418. if (!res)
  419. {
  420. ZM_DEBUG (zm_dump (__LINE__));
  421. return xyzModem_timeout;
  422. }
  423. if (xyz.crc_mode)
  424. {
  425. res = CYGACC_COMM_IF_GETC_TIMEOUT (*xyz.__chan, (char *) &xyz.crc2);
  426. ZM_DEBUG (zm_save (xyz.crc2));
  427. if (!res)
  428. {
  429. ZM_DEBUG (zm_dump (__LINE__));
  430. return xyzModem_timeout;
  431. }
  432. }
  433. ZM_DEBUG (zm_dump (__LINE__));
  434. /* Validate the message */
  435. if ((xyz.blk ^ xyz.cblk) != (unsigned char) 0xFF)
  436. {
  437. ZM_DEBUG (zm_dprintf
  438. ("Framing error - blk: %x/%x/%x\n", xyz.blk, xyz.cblk,
  439. (xyz.blk ^ xyz.cblk)));
  440. ZM_DEBUG (zm_dump_buf (xyz.pkt, xyz.len));
  441. xyzModem_flush ();
  442. return xyzModem_frame;
  443. }
  444. /* Verify checksum/CRC */
  445. if (xyz.crc_mode)
  446. {
  447. cksum = cyg_crc16 (xyz.pkt, xyz.len);
  448. if (cksum != ((xyz.crc1 << 8) | xyz.crc2))
  449. {
  450. ZM_DEBUG (zm_dprintf ("CRC error - recvd: %02x%02x, computed: %x\n",
  451. xyz.crc1, xyz.crc2, cksum & 0xFFFF));
  452. return xyzModem_cksum;
  453. }
  454. }
  455. else
  456. {
  457. cksum = 0;
  458. for (i = 0; i < xyz.len; i++)
  459. {
  460. cksum += xyz.pkt[i];
  461. }
  462. if (xyz.crc1 != (cksum & 0xFF))
  463. {
  464. ZM_DEBUG (zm_dprintf
  465. ("Checksum error - recvd: %x, computed: %x\n", xyz.crc1,
  466. cksum & 0xFF));
  467. return xyzModem_cksum;
  468. }
  469. }
  470. /* If we get here, the message passes [structural] muster */
  471. return 0;
  472. }
  473. int
  474. xyzModem_stream_open (connection_info_t * info, int *err)
  475. {
  476. #ifdef REDBOOT
  477. int console_chan;
  478. #endif
  479. int stat = 0;
  480. int retries = xyzModem_MAX_RETRIES;
  481. int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
  482. /* ZM_DEBUG(zm_out = zm_out_start); */
  483. #ifdef xyzModem_zmodem
  484. if (info->mode == xyzModem_zmodem)
  485. {
  486. *err = xyzModem_noZmodem;
  487. return -1;
  488. }
  489. #endif
  490. #ifdef REDBOOT
  491. /* Set up the I/O channel. Note: this allows for using a different port in the future */
  492. console_chan =
  493. CYGACC_CALL_IF_SET_CONSOLE_COMM
  494. (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
  495. if (info->chan >= 0)
  496. {
  497. CYGACC_CALL_IF_SET_CONSOLE_COMM (info->chan);
  498. }
  499. else
  500. {
  501. CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan);
  502. }
  503. xyz.__chan = CYGACC_CALL_IF_CONSOLE_PROCS ();
  504. CYGACC_CALL_IF_SET_CONSOLE_COMM (console_chan);
  505. CYGACC_COMM_IF_CONTROL (*xyz.__chan, __COMMCTL_SET_TIMEOUT,
  506. xyzModem_CHAR_TIMEOUT);
  507. #else
  508. /* TODO: CHECK ! */
  509. int dummy = 0;
  510. xyz.__chan = &dummy;
  511. #endif
  512. xyz.len = 0;
  513. xyz.crc_mode = true;
  514. xyz.at_eof = false;
  515. xyz.tx_ack = false;
  516. xyz.mode = info->mode;
  517. xyz.total_retries = 0;
  518. xyz.total_SOH = 0;
  519. xyz.total_STX = 0;
  520. xyz.total_CAN = 0;
  521. #ifdef USE_YMODEM_LENGTH
  522. xyz.read_length = 0;
  523. xyz.file_length = 0;
  524. #endif
  525. CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  526. if (xyz.mode == xyzModem_xmodem)
  527. {
  528. /* X-modem doesn't have an information header - exit here */
  529. xyz.next_blk = 1;
  530. return 0;
  531. }
  532. while (retries-- > 0)
  533. {
  534. stat = xyzModem_get_hdr ();
  535. if (stat == 0)
  536. {
  537. /* Y-modem file information header */
  538. if (xyz.blk == 0)
  539. {
  540. #ifdef USE_YMODEM_LENGTH
  541. /* skip filename */
  542. while (*xyz.bufp++);
  543. /* get the length */
  544. parse_num ((char *) xyz.bufp, &xyz.file_length, NULL, " ");
  545. #endif
  546. /* The rest of the file name data block quietly discarded */
  547. xyz.tx_ack = true;
  548. }
  549. xyz.next_blk = 1;
  550. xyz.len = 0;
  551. return 0;
  552. }
  553. else if (stat == xyzModem_timeout)
  554. {
  555. if (--crc_retries <= 0)
  556. xyz.crc_mode = false;
  557. CYGACC_CALL_IF_DELAY_US (5 * 100000); /* Extra delay for startup */
  558. CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  559. xyz.total_retries++;
  560. ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
  561. }
  562. if (stat == xyzModem_cancel)
  563. {
  564. break;
  565. }
  566. }
  567. *err = stat;
  568. ZM_DEBUG (zm_flush ());
  569. return -1;
  570. }
  571. int
  572. xyzModem_stream_read (char *buf, int size, int *err)
  573. {
  574. int stat, total, len;
  575. int retries;
  576. total = 0;
  577. stat = xyzModem_cancel;
  578. /* Try and get 'size' bytes into the buffer */
  579. while (!xyz.at_eof && (size > 0))
  580. {
  581. if (xyz.len == 0)
  582. {
  583. retries = xyzModem_MAX_RETRIES;
  584. while (retries-- > 0)
  585. {
  586. stat = xyzModem_get_hdr ();
  587. if (stat == 0)
  588. {
  589. if (xyz.blk == xyz.next_blk)
  590. {
  591. xyz.tx_ack = true;
  592. ZM_DEBUG (zm_dprintf
  593. ("ACK block %d (%d)\n", xyz.blk, __LINE__));
  594. xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
  595. #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
  596. if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0)
  597. {
  598. #else
  599. if (1)
  600. {
  601. #endif
  602. /* Data blocks can be padded with ^Z (EOF) characters */
  603. /* This code tries to detect and remove them */
  604. if ((xyz.bufp[xyz.len - 1] == EOF) &&
  605. (xyz.bufp[xyz.len - 2] == EOF) &&
  606. (xyz.bufp[xyz.len - 3] == EOF))
  607. {
  608. while (xyz.len
  609. && (xyz.bufp[xyz.len - 1] == EOF))
  610. {
  611. xyz.len--;
  612. }
  613. }
  614. }
  615. #ifdef USE_YMODEM_LENGTH
  616. /*
  617. * See if accumulated length exceeds that of the file.
  618. * If so, reduce size (i.e., cut out pad bytes)
  619. * Only do this for Y-modem (and Z-modem should it ever
  620. * be supported since it can fall back to Y-modem mode).
  621. */
  622. if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length)
  623. {
  624. xyz.read_length += xyz.len;
  625. if (xyz.read_length > xyz.file_length)
  626. {
  627. xyz.len -= (xyz.read_length - xyz.file_length);
  628. }
  629. }
  630. #endif
  631. break;
  632. }
  633. else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF))
  634. {
  635. /* Just re-ACK this so sender will get on with it */
  636. CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
  637. continue; /* Need new header */
  638. }
  639. else
  640. {
  641. stat = xyzModem_sequence;
  642. }
  643. }
  644. if (stat == xyzModem_cancel)
  645. {
  646. break;
  647. }
  648. if (stat == xyzModem_eof)
  649. {
  650. CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
  651. ZM_DEBUG (zm_dprintf ("ACK (%d)\n", __LINE__));
  652. if (xyz.mode == xyzModem_ymodem)
  653. {
  654. CYGACC_COMM_IF_PUTC (*xyz.__chan,
  655. (xyz.crc_mode ? 'C' : NAK));
  656. xyz.total_retries++;
  657. ZM_DEBUG (zm_dprintf ("Reading Final Header\n"));
  658. stat = xyzModem_get_hdr ();
  659. CYGACC_COMM_IF_PUTC (*xyz.__chan, ACK);
  660. ZM_DEBUG (zm_dprintf ("FINAL ACK (%d)\n", __LINE__));
  661. }
  662. xyz.at_eof = true;
  663. break;
  664. }
  665. CYGACC_COMM_IF_PUTC (*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  666. xyz.total_retries++;
  667. ZM_DEBUG (zm_dprintf ("NAK (%d)\n", __LINE__));
  668. }
  669. if (stat < 0)
  670. {
  671. *err = stat;
  672. xyz.len = -1;
  673. return total;
  674. }
  675. }
  676. /* Don't "read" data from the EOF protocol package */
  677. if (!xyz.at_eof)
  678. {
  679. len = xyz.len;
  680. if (size < len)
  681. len = size;
  682. memcpy (buf, xyz.bufp, len);
  683. size -= len;
  684. buf += len;
  685. total += len;
  686. xyz.len -= len;
  687. xyz.bufp += len;
  688. }
  689. }
  690. return total;
  691. }
  692. void
  693. xyzModem_stream_close (int *err)
  694. {
  695. diag_printf
  696. ("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
  697. xyz.crc_mode ? "CRC" : "Cksum", xyz.total_SOH, xyz.total_STX,
  698. xyz.total_CAN, xyz.total_retries);
  699. ZM_DEBUG (zm_flush ());
  700. }
  701. /* Need to be able to clean out the input buffer, so have to take the */
  702. /* getc */
  703. void
  704. xyzModem_stream_terminate (bool abort, int (*getc) (void))
  705. {
  706. int c;
  707. if (abort)
  708. {
  709. ZM_DEBUG (zm_dprintf ("!!!! TRANSFER ABORT !!!!\n"));
  710. switch (xyz.mode)
  711. {
  712. case xyzModem_xmodem:
  713. case xyzModem_ymodem:
  714. /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
  715. /* number of Backspaces is a friendly way to get the other end to abort. */
  716. CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
  717. CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
  718. CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
  719. CYGACC_COMM_IF_PUTC (*xyz.__chan, CAN);
  720. CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
  721. CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
  722. CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
  723. CYGACC_COMM_IF_PUTC (*xyz.__chan, BSP);
  724. /* Now consume the rest of what's waiting on the line. */
  725. ZM_DEBUG (zm_dprintf ("Flushing serial line.\n"));
  726. xyzModem_flush ();
  727. xyz.at_eof = true;
  728. break;
  729. #ifdef xyzModem_zmodem
  730. case xyzModem_zmodem:
  731. /* Might support it some day I suppose. */
  732. #endif
  733. break;
  734. }
  735. }
  736. else
  737. {
  738. ZM_DEBUG (zm_dprintf ("Engaging cleanup mode...\n"));
  739. /*
  740. * Consume any trailing crap left in the inbuffer from
  741. * previous recieved blocks. Since very few files are an exact multiple
  742. * of the transfer block size, there will almost always be some gunk here.
  743. * If we don't eat it now, RedBoot will think the user typed it.
  744. */
  745. ZM_DEBUG (zm_dprintf ("Trailing gunk:\n"));
  746. while ((c = (*getc) ()) > -1);
  747. ZM_DEBUG (zm_dprintf ("\n"));
  748. /*
  749. * Make a small delay to give terminal programs like minicom
  750. * time to get control again after their file transfer program
  751. * exits.
  752. */
  753. CYGACC_CALL_IF_DELAY_US ((cyg_int32) 250000);
  754. }
  755. }
  756. char *
  757. xyzModem_error (int err)
  758. {
  759. switch (err)
  760. {
  761. case xyzModem_access:
  762. return "Can't access file";
  763. break;
  764. case xyzModem_noZmodem:
  765. return "Sorry, zModem not available yet";
  766. break;
  767. case xyzModem_timeout:
  768. return "Timed out";
  769. break;
  770. case xyzModem_eof:
  771. return "End of file";
  772. break;
  773. case xyzModem_cancel:
  774. return "Cancelled";
  775. break;
  776. case xyzModem_frame:
  777. return "Invalid framing";
  778. break;
  779. case xyzModem_cksum:
  780. return "CRC/checksum error";
  781. break;
  782. case xyzModem_sequence:
  783. return "Block sequence error";
  784. break;
  785. default:
  786. return "Unknown error";
  787. break;
  788. }
  789. }
  790. /*
  791. * RedBoot interface
  792. */
  793. #if 0 /* SB */
  794. GETC_IO_FUNCS (xyzModem_io, xyzModem_stream_open, xyzModem_stream_close,
  795. xyzModem_stream_terminate, xyzModem_stream_read,
  796. xyzModem_error);
  797. RedBoot_load (xmodem, xyzModem_io, false, false, xyzModem_xmodem);
  798. RedBoot_load (ymodem, xyzModem_io, false, false, xyzModem_ymodem);
  799. #endif