rx.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* vi: set sw=4 ts=4: */
  2. /*-------------------------------------------------------------------------
  3. * Filename: xmodem.c
  4. * Copyright: Copyright (C) 2001, Hewlett-Packard Company
  5. * Author: Christopher Hoover <ch@hpl.hp.com>
  6. * Description: xmodem functionality for uploading of kernels
  7. * and the like
  8. * Created at: Thu Dec 20 01:58:08 PST 2001
  9. *-----------------------------------------------------------------------*/
  10. /*
  11. * xmodem.c: xmodem functionality for uploading of kernels and
  12. * the like
  13. *
  14. * Copyright (C) 2001 Hewlett-Packard Laboratories
  15. *
  16. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  17. *
  18. * This was originally written for blob and then adapted for busybox.
  19. */
  20. #include "libbb.h"
  21. #define SOH 0x01
  22. #define STX 0x02
  23. #define EOT 0x04
  24. #define ACK 0x06
  25. #define NAK 0x15
  26. #define BS 0x08
  27. /*
  28. Cf:
  29. http://www.textfiles.com/apple/xmodem
  30. http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
  31. http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
  32. http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
  33. */
  34. #define TIMEOUT 1
  35. #define TIMEOUT_LONG 10
  36. #define MAXERRORS 10
  37. #define read_fd STDIN_FILENO
  38. #define write_fd STDOUT_FILENO
  39. static int read_byte(unsigned timeout)
  40. {
  41. char buf[1];
  42. int n;
  43. alarm(timeout);
  44. /* NOT safe_read! We want ALRM to interrupt us */
  45. n = read(read_fd, buf, 1);
  46. alarm(0);
  47. if (n == 1)
  48. return (unsigned char)buf[0];
  49. return -1;
  50. }
  51. static int receive(/*int read_fd, */int file_fd)
  52. {
  53. unsigned char blockBuf[1024];
  54. unsigned errors = 0;
  55. unsigned wantBlockNo = 1;
  56. unsigned length = 0;
  57. int do_crc = 1;
  58. char nak = 'C';
  59. unsigned timeout = TIMEOUT_LONG;
  60. /* Flush pending input */
  61. tcflush(read_fd, TCIFLUSH);
  62. /* Ask for CRC; if we get errors, we will go with checksum */
  63. full_write(write_fd, &nak, 1);
  64. for (;;) {
  65. int blockBegin;
  66. int blockNo, blockNoOnesCompl;
  67. int blockLength;
  68. int cksum_crc; /* cksum OR crc */
  69. int expected;
  70. int i,j;
  71. blockBegin = read_byte(timeout);
  72. if (blockBegin < 0)
  73. goto timeout;
  74. timeout = TIMEOUT;
  75. nak = NAK;
  76. switch (blockBegin) {
  77. case SOH:
  78. case STX:
  79. break;
  80. case EOT:
  81. nak = ACK;
  82. full_write(write_fd, &nak, 1);
  83. return length;
  84. default:
  85. goto error;
  86. }
  87. /* block no */
  88. blockNo = read_byte(TIMEOUT);
  89. if (blockNo < 0)
  90. goto timeout;
  91. /* block no one's compliment */
  92. blockNoOnesCompl = read_byte(TIMEOUT);
  93. if (blockNoOnesCompl < 0)
  94. goto timeout;
  95. if (blockNo != (255 - blockNoOnesCompl)) {
  96. bb_error_msg("bad block ones compl");
  97. goto error;
  98. }
  99. blockLength = (blockBegin == SOH) ? 128 : 1024;
  100. for (i = 0; i < blockLength; i++) {
  101. int cc = read_byte(TIMEOUT);
  102. if (cc < 0)
  103. goto timeout;
  104. blockBuf[i] = cc;
  105. }
  106. if (do_crc) {
  107. cksum_crc = read_byte(TIMEOUT);
  108. if (cksum_crc < 0)
  109. goto timeout;
  110. cksum_crc = (cksum_crc << 8) | read_byte(TIMEOUT);
  111. if (cksum_crc < 0)
  112. goto timeout;
  113. } else {
  114. cksum_crc = read_byte(TIMEOUT);
  115. if (cksum_crc < 0)
  116. goto timeout;
  117. }
  118. if (blockNo == ((wantBlockNo - 1) & 0xff)) {
  119. /* a repeat of the last block is ok, just ignore it. */
  120. /* this also ignores the initial block 0 which is */
  121. /* meta data. */
  122. goto next;
  123. }
  124. if (blockNo != (wantBlockNo & 0xff)) {
  125. bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
  126. goto error;
  127. }
  128. expected = 0;
  129. if (do_crc) {
  130. for (i = 0; i < blockLength; i++) {
  131. expected = expected ^ blockBuf[i] << 8;
  132. for (j = 0; j < 8; j++) {
  133. if (expected & 0x8000)
  134. expected = expected << 1 ^ 0x1021;
  135. else
  136. expected = expected << 1;
  137. }
  138. }
  139. expected &= 0xffff;
  140. } else {
  141. for (i = 0; i < blockLength; i++)
  142. expected += blockBuf[i];
  143. expected &= 0xff;
  144. }
  145. if (cksum_crc != expected) {
  146. bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x"
  147. : "checksum error, expected 0x%02x, got 0x%02x",
  148. expected, cksum_crc);
  149. goto error;
  150. }
  151. wantBlockNo++;
  152. length += blockLength;
  153. errno = 0;
  154. if (full_write(file_fd, blockBuf, blockLength) != blockLength) {
  155. bb_perror_msg("can't write to file");
  156. goto fatal;
  157. }
  158. next:
  159. errors = 0;
  160. nak = ACK;
  161. full_write(write_fd, &nak, 1);
  162. continue;
  163. error:
  164. timeout:
  165. errors++;
  166. if (errors == MAXERRORS) {
  167. /* Abort */
  168. /* if were asking for crc, try again w/o crc */
  169. if (nak == 'C') {
  170. nak = NAK;
  171. errors = 0;
  172. do_crc = 0;
  173. goto timeout;
  174. }
  175. bb_error_msg("too many errors; giving up");
  176. fatal:
  177. /* 5 CAN followed by 5 BS. Don't try too hard... */
  178. safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10);
  179. return -1;
  180. }
  181. /* Flush pending input */
  182. tcflush(read_fd, TCIFLUSH);
  183. full_write(write_fd, &nak, 1);
  184. } /* for (;;) */
  185. }
  186. static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
  187. {
  188. }
  189. int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  190. int rx_main(int argc, char **argv)
  191. {
  192. struct termios tty, orig_tty;
  193. int termios_err;
  194. int file_fd;
  195. int n;
  196. if (argc != 2)
  197. bb_show_usage();
  198. /* Disabled by vda:
  199. * why we can't receive from stdin? Why we *require*
  200. * controlling tty?? */
  201. /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/
  202. file_fd = xopen(argv[1], O_RDWR|O_CREAT|O_TRUNC);
  203. termios_err = tcgetattr(read_fd, &tty);
  204. if (termios_err == 0) {
  205. orig_tty = tty;
  206. cfmakeraw(&tty);
  207. tcsetattr(read_fd, TCSAFLUSH, &tty);
  208. }
  209. /* No SA_RESTART: we want ALRM to interrupt read() */
  210. signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
  211. n = receive(file_fd);
  212. if (termios_err == 0)
  213. tcsetattr(read_fd, TCSAFLUSH, &orig_tty);
  214. if (ENABLE_FEATURE_CLEAN_UP)
  215. close(file_fd);
  216. fflush_stdout_and_exit(n >= 0);
  217. }