xyzModem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. *==========================================================================
  3. *
  4. * xyzModem.c
  5. *
  6. * RedBoot stream handler for xyzModem protocol
  7. *
  8. *==========================================================================
  9. *####ECOSGPLCOPYRIGHTBEGIN####
  10. * -------------------------------------------
  11. * This file is part of eCos, the Embedded Configurable Operating System.
  12. * Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
  13. * Copyright (C) 2002 Gary Thomas
  14. *
  15. * eCos is free software; you can redistribute it and/or modify it under
  16. * the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 or (at your option) any later version.
  18. *
  19. * eCos is distributed in the hope that it will be useful, but WITHOUT ANY
  20. * WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with eCos; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  27. *
  28. * As a special exception, if other files instantiate templates or use macros
  29. * or inline functions from this file, or you compile this file and link it
  30. * with other works to produce a work based on this file, this file does not
  31. * by itself cause the resulting work to be covered by the GNU General Public
  32. * License. However the source code for this file must still be made available
  33. * in accordance with section (3) of the GNU General Public License.
  34. *
  35. * This exception does not invalidate any other reasons why a work based on
  36. * this file might be covered by the GNU General Public License.
  37. *
  38. * Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
  39. * at http: *sources.redhat.com/ecos/ecos-license/
  40. * -------------------------------------------
  41. *####ECOSGPLCOPYRIGHTEND####
  42. *==========================================================================
  43. *#####DESCRIPTIONBEGIN####
  44. *
  45. * Author(s): gthomas
  46. * Contributors: gthomas, tsmith, Yoshinori Sato
  47. * Date: 2000-07-14
  48. * Purpose:
  49. * Description:
  50. *
  51. * This code is part of RedBoot (tm).
  52. *
  53. *####DESCRIPTIONEND####
  54. *
  55. *==========================================================================
  56. */
  57. #include <common.h>
  58. #include <xyzModem.h>
  59. #include <stdarg.h>
  60. #include <crc.h>
  61. /* Assumption - run xyzModem protocol over the console port */
  62. /* Values magic to the protocol */
  63. #define SOH 0x01
  64. #define STX 0x02
  65. #define EOT 0x04
  66. #define ACK 0x06
  67. #define BSP 0x08
  68. #define NAK 0x15
  69. #define CAN 0x18
  70. #define EOF 0x1A /* ^Z for DOS officionados */
  71. #define USE_YMODEM_LENGTH
  72. /* Data & state local to the protocol */
  73. static struct {
  74. #ifdef REDBOOT
  75. hal_virtual_comm_table_t* __chan;
  76. #else
  77. int *__chan;
  78. #endif
  79. unsigned char pkt[1024], *bufp;
  80. unsigned char blk,cblk,crc1,crc2;
  81. unsigned char next_blk; /* Expected block */
  82. int len, mode, total_retries;
  83. int total_SOH, total_STX, total_CAN;
  84. bool crc_mode, at_eof, tx_ack;
  85. #ifdef USE_YMODEM_LENGTH
  86. unsigned long file_length, read_length;
  87. #endif
  88. } xyz;
  89. #define xyzModem_CHAR_TIMEOUT 2000 /* 2 seconds */
  90. #define xyzModem_MAX_RETRIES 20
  91. #define xyzModem_MAX_RETRIES_WITH_CRC 10
  92. #define xyzModem_CAN_COUNT 3 /* Wait for 3 CAN before quitting */
  93. #ifndef REDBOOT /*SB */
  94. typedef int cyg_int32;
  95. int CYGACC_COMM_IF_GETC_TIMEOUT (char chan,char *c) {
  96. #define DELAY 20
  97. unsigned long counter=0;
  98. while (!tstc() && (counter < xyzModem_CHAR_TIMEOUT*1000/DELAY)) {
  99. udelay(DELAY);
  100. counter++;
  101. }
  102. if (tstc()) {
  103. *c=getc();
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. void CYGACC_COMM_IF_PUTC(char x,char y) {
  109. putc(y);
  110. }
  111. /* Validate a hex character */
  112. __inline__ static bool
  113. _is_hex(char c)
  114. {
  115. return (((c >= '0') && (c <= '9')) ||
  116. ((c >= 'A') && (c <= 'F')) ||
  117. ((c >= 'a') && (c <= 'f')));
  118. }
  119. /* Convert a single hex nibble */
  120. __inline__ static int
  121. _from_hex(char c)
  122. {
  123. int ret = 0;
  124. if ((c >= '0') && (c <= '9')) {
  125. ret = (c - '0');
  126. } else if ((c >= 'a') && (c <= 'f')) {
  127. ret = (c - 'a' + 0x0a);
  128. } else if ((c >= 'A') && (c <= 'F')) {
  129. ret = (c - 'A' + 0x0A);
  130. }
  131. return ret;
  132. }
  133. /* Convert a character to lower case */
  134. __inline__ static char
  135. _tolower(char c)
  136. {
  137. if ((c >= 'A') && (c <= 'Z')) {
  138. c = (c - 'A') + 'a';
  139. }
  140. return c;
  141. }
  142. /* Parse (scan) a number */
  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. #ifdef REDBOOT
  243. char *p = zm_out_start;
  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, (char *)&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, (char *)&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, (char *)&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, (char *)&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. #ifdef REDBOOT
  419. int console_chan;
  420. #endif
  421. int stat = 0;
  422. int retries = xyzModem_MAX_RETRIES;
  423. int crc_retries = xyzModem_MAX_RETRIES_WITH_CRC;
  424. /* ZM_DEBUG(zm_out = zm_out_start); */
  425. #ifdef xyzModem_zmodem
  426. if (info->mode == xyzModem_zmodem) {
  427. *err = xyzModem_noZmodem;
  428. return -1;
  429. }
  430. #endif
  431. #ifdef REDBOOT
  432. /* Set up the I/O channel. Note: this allows for using a different port in the future */
  433. console_chan = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
  434. if (info->chan >= 0) {
  435. CYGACC_CALL_IF_SET_CONSOLE_COMM(info->chan);
  436. } else {
  437. CYGACC_CALL_IF_SET_CONSOLE_COMM(console_chan);
  438. }
  439. xyz.__chan = CYGACC_CALL_IF_CONSOLE_PROCS();
  440. CYGACC_CALL_IF_SET_CONSOLE_COMM(console_chan);
  441. CYGACC_COMM_IF_CONTROL(*xyz.__chan, __COMMCTL_SET_TIMEOUT, xyzModem_CHAR_TIMEOUT);
  442. #else
  443. /* TODO: CHECK ! */
  444. int dummy;
  445. xyz.__chan=&dummy;
  446. #endif
  447. xyz.len = 0;
  448. xyz.crc_mode = true;
  449. xyz.at_eof = false;
  450. xyz.tx_ack = false;
  451. xyz.mode = info->mode;
  452. xyz.total_retries = 0;
  453. xyz.total_SOH = 0;
  454. xyz.total_STX = 0;
  455. xyz.total_CAN = 0;
  456. #ifdef USE_YMODEM_LENGTH
  457. xyz.read_length = 0;
  458. xyz.file_length = 0;
  459. #endif
  460. CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  461. if (xyz.mode == xyzModem_xmodem) {
  462. /* X-modem doesn't have an information header - exit here */
  463. xyz.next_blk = 1;
  464. return 0;
  465. }
  466. while (retries-- > 0) {
  467. stat = xyzModem_get_hdr();
  468. if (stat == 0) {
  469. /* Y-modem file information header */
  470. if (xyz.blk == 0) {
  471. #ifdef USE_YMODEM_LENGTH
  472. /* skip filename */
  473. while (*xyz.bufp++);
  474. /* get the length */
  475. parse_num((char *)xyz.bufp, &xyz.file_length, NULL, " ");
  476. #endif
  477. /* The rest of the file name data block quietly discarded */
  478. xyz.tx_ack = true;
  479. }
  480. xyz.next_blk = 1;
  481. xyz.len = 0;
  482. return 0;
  483. } else
  484. if (stat == xyzModem_timeout) {
  485. if (--crc_retries <= 0) xyz.crc_mode = false;
  486. CYGACC_CALL_IF_DELAY_US(5*100000); /* Extra delay for startup */
  487. CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  488. xyz.total_retries++;
  489. ZM_DEBUG(zm_dprintf("NAK (%d)\n", __LINE__));
  490. }
  491. if (stat == xyzModem_cancel) {
  492. break;
  493. }
  494. }
  495. *err = stat;
  496. ZM_DEBUG(zm_flush());
  497. return -1;
  498. }
  499. int
  500. xyzModem_stream_read(char *buf, int size, int *err)
  501. {
  502. int stat, total, len;
  503. int retries;
  504. total = 0;
  505. stat = xyzModem_cancel;
  506. /* Try and get 'size' bytes into the buffer */
  507. while (!xyz.at_eof && (size > 0)) {
  508. if (xyz.len == 0) {
  509. retries = xyzModem_MAX_RETRIES;
  510. while (retries-- > 0) {
  511. stat = xyzModem_get_hdr();
  512. if (stat == 0) {
  513. if (xyz.blk == xyz.next_blk) {
  514. xyz.tx_ack = true;
  515. ZM_DEBUG(zm_dprintf("ACK block %d (%d)\n", xyz.blk, __LINE__));
  516. xyz.next_blk = (xyz.next_blk + 1) & 0xFF;
  517. #if defined(xyzModem_zmodem) || defined(USE_YMODEM_LENGTH)
  518. if (xyz.mode == xyzModem_xmodem || xyz.file_length == 0) {
  519. #else
  520. if (1) {
  521. #endif
  522. /* Data blocks can be padded with ^Z (EOF) characters */
  523. /* This code tries to detect and remove them */
  524. if ((xyz.bufp[xyz.len-1] == EOF) &&
  525. (xyz.bufp[xyz.len-2] == EOF) &&
  526. (xyz.bufp[xyz.len-3] == EOF)) {
  527. while (xyz.len && (xyz.bufp[xyz.len-1] == EOF)) {
  528. xyz.len--;
  529. }
  530. }
  531. }
  532. #ifdef USE_YMODEM_LENGTH
  533. /*
  534. * See if accumulated length exceeds that of the file.
  535. * If so, reduce size (i.e., cut out pad bytes)
  536. * Only do this for Y-modem (and Z-modem should it ever
  537. * be supported since it can fall back to Y-modem mode).
  538. */
  539. if (xyz.mode != xyzModem_xmodem && 0 != xyz.file_length) {
  540. xyz.read_length += xyz.len;
  541. if (xyz.read_length > xyz.file_length) {
  542. xyz.len -= (xyz.read_length - xyz.file_length);
  543. }
  544. }
  545. #endif
  546. break;
  547. } else if (xyz.blk == ((xyz.next_blk - 1) & 0xFF)) {
  548. /* Just re-ACK this so sender will get on with it */
  549. CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
  550. continue; /* Need new header */
  551. } else {
  552. stat = xyzModem_sequence;
  553. }
  554. }
  555. if (stat == xyzModem_cancel) {
  556. break;
  557. }
  558. if (stat == xyzModem_eof) {
  559. CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
  560. ZM_DEBUG(zm_dprintf("ACK (%d)\n", __LINE__));
  561. if (xyz.mode == xyzModem_ymodem) {
  562. CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  563. xyz.total_retries++;
  564. ZM_DEBUG(zm_dprintf("Reading Final Header\n"));
  565. stat = xyzModem_get_hdr();
  566. CYGACC_COMM_IF_PUTC(*xyz.__chan, ACK);
  567. ZM_DEBUG(zm_dprintf("FINAL ACK (%d)\n", __LINE__));
  568. }
  569. xyz.at_eof = true;
  570. break;
  571. }
  572. CYGACC_COMM_IF_PUTC(*xyz.__chan, (xyz.crc_mode ? 'C' : NAK));
  573. xyz.total_retries++;
  574. ZM_DEBUG(zm_dprintf("NAK (%d)\n", __LINE__));
  575. }
  576. if (stat < 0) {
  577. *err = stat;
  578. xyz.len = -1;
  579. return total;
  580. }
  581. }
  582. /* Don't "read" data from the EOF protocol package */
  583. if (!xyz.at_eof) {
  584. len = xyz.len;
  585. if (size < len) len = size;
  586. memcpy(buf, xyz.bufp, len);
  587. size -= len;
  588. buf += len;
  589. total += len;
  590. xyz.len -= len;
  591. xyz.bufp += len;
  592. }
  593. }
  594. return total;
  595. }
  596. void
  597. xyzModem_stream_close(int *err)
  598. {
  599. diag_printf("xyzModem - %s mode, %d(SOH)/%d(STX)/%d(CAN) packets, %d retries\n",
  600. xyz.crc_mode ? "CRC" : "Cksum",
  601. xyz.total_SOH, xyz.total_STX, xyz.total_CAN,
  602. xyz.total_retries);
  603. ZM_DEBUG(zm_flush());
  604. }
  605. /* Need to be able to clean out the input buffer, so have to take the */
  606. /* getc */
  607. void xyzModem_stream_terminate(bool abort, int (*getc)(void))
  608. {
  609. int c;
  610. if (abort) {
  611. ZM_DEBUG(zm_dprintf("!!!! TRANSFER ABORT !!!!\n"));
  612. switch (xyz.mode) {
  613. case xyzModem_xmodem:
  614. case xyzModem_ymodem:
  615. /* The X/YMODEM Spec seems to suggest that multiple CAN followed by an equal */
  616. /* number of Backspaces is a friendly way to get the other end to abort. */
  617. CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
  618. CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
  619. CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
  620. CYGACC_COMM_IF_PUTC(*xyz.__chan,CAN);
  621. CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
  622. CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
  623. CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
  624. CYGACC_COMM_IF_PUTC(*xyz.__chan,BSP);
  625. /* Now consume the rest of what's waiting on the line. */
  626. ZM_DEBUG(zm_dprintf("Flushing serial line.\n"));
  627. xyzModem_flush();
  628. xyz.at_eof = true;
  629. break;
  630. #ifdef xyzModem_zmodem
  631. case xyzModem_zmodem:
  632. /* Might support it some day I suppose. */
  633. #endif
  634. break;
  635. }
  636. } else {
  637. ZM_DEBUG(zm_dprintf("Engaging cleanup mode...\n"));
  638. /*
  639. * Consume any trailing crap left in the inbuffer from
  640. * previous recieved blocks. Since very few files are an exact multiple
  641. * of the transfer block size, there will almost always be some gunk here.
  642. * If we don't eat it now, RedBoot will think the user typed it.
  643. */
  644. ZM_DEBUG(zm_dprintf("Trailing gunk:\n"));
  645. while ((c = (*getc)()) > -1) ;
  646. ZM_DEBUG(zm_dprintf("\n"));
  647. /*
  648. * Make a small delay to give terminal programs like minicom
  649. * time to get control again after their file transfer program
  650. * exits.
  651. */
  652. CYGACC_CALL_IF_DELAY_US((cyg_int32)250000);
  653. }
  654. }
  655. char *
  656. xyzModem_error(int err)
  657. {
  658. switch (err) {
  659. case xyzModem_access:
  660. return "Can't access file";
  661. break;
  662. case xyzModem_noZmodem:
  663. return "Sorry, zModem not available yet";
  664. break;
  665. case xyzModem_timeout:
  666. return "Timed out";
  667. break;
  668. case xyzModem_eof:
  669. return "End of file";
  670. break;
  671. case xyzModem_cancel:
  672. return "Cancelled";
  673. break;
  674. case xyzModem_frame:
  675. return "Invalid framing";
  676. break;
  677. case xyzModem_cksum:
  678. return "CRC/checksum error";
  679. break;
  680. case xyzModem_sequence:
  681. return "Block sequence error";
  682. break;
  683. default:
  684. return "Unknown error";
  685. break;
  686. }
  687. }
  688. /*
  689. * RedBoot interface
  690. */
  691. #if 0 /* SB */
  692. GETC_IO_FUNCS(xyzModem_io, xyzModem_stream_open, xyzModem_stream_close,
  693. xyzModem_stream_terminate, xyzModem_stream_read, xyzModem_error);
  694. RedBoot_load(xmodem, xyzModem_io, false, false, xyzModem_xmodem);
  695. RedBoot_load(ymodem, xyzModem_io, false, false, xyzModem_ymodem);
  696. #endif