rx.c 6.3 KB

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