rx.c 6.0 KB

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