xyzModem.c 21 KB

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