xyzModem.c 18 KB

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