rx.c 6.1 KB

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