3
0

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