xyzModem.c 18 KB

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