xyzModem.c 17 KB

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