3
0

rx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. */
  21. #include "libbb.h"
  22. #define SOH 0x01
  23. #define STX 0x02
  24. #define EOT 0x04
  25. #define ACK 0x06
  26. #define NAK 0x15
  27. #define BS 0x08
  28. /*
  29. Cf:
  30. http://www.textfiles.com/apple/xmodem
  31. http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt
  32. http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt
  33. http://www.phys.washington.edu/~belonis/xmodem/modmprot.col
  34. */
  35. #define TIMEOUT 1
  36. #define TIMEOUT_LONG 10
  37. #define MAXERRORS 10
  38. static int read_byte(int fd, unsigned int timeout)
  39. {
  40. char buf[1];
  41. int n;
  42. alarm(timeout);
  43. n = read(fd, &buf, 1);
  44. alarm(0);
  45. if (n == 1)
  46. return buf[0] & 0xff;
  47. else
  48. return -1;
  49. }
  50. static int receive(char *error_buf, size_t error_buf_size,
  51. int ttyfd, int filefd)
  52. {
  53. char blockBuf[1024];
  54. unsigned int errors = 0;
  55. unsigned int wantBlockNo = 1;
  56. unsigned int length = 0;
  57. int docrc = 1;
  58. char nak = 'C';
  59. unsigned int timeout = TIMEOUT_LONG;
  60. #define note_error(fmt,args...) \
  61. snprintf(error_buf, error_buf_size, fmt,##args)
  62. /* Flush pending input */
  63. tcflush(ttyfd, TCIFLUSH);
  64. /* Ask for CRC; if we get errors, we will go with checksum */
  65. write(ttyfd, &nak, 1);
  66. for (;;) {
  67. int blockBegin;
  68. int blockNo, blockNoOnesCompl;
  69. int blockLength;
  70. int cksum = 0;
  71. int crcHi = 0;
  72. int crcLo = 0;
  73. blockBegin = read_byte(ttyfd, timeout);
  74. if (blockBegin < 0)
  75. goto timeout;
  76. timeout = TIMEOUT;
  77. nak = NAK;
  78. switch (blockBegin) {
  79. case SOH:
  80. case STX:
  81. break;
  82. case EOT:
  83. nak = ACK;
  84. write(ttyfd, &nak, 1);
  85. goto done;
  86. default:
  87. goto error;
  88. }
  89. /* block no */
  90. blockNo = read_byte(ttyfd, TIMEOUT);
  91. if (blockNo < 0)
  92. goto timeout;
  93. /* block no one's compliment */
  94. blockNoOnesCompl = read_byte(ttyfd, TIMEOUT);
  95. if (blockNoOnesCompl < 0)
  96. goto timeout;
  97. if (blockNo != (255 - blockNoOnesCompl)) {
  98. note_error("bad block ones compl");
  99. goto error;
  100. }
  101. blockLength = (blockBegin == SOH) ? 128 : 1024;
  102. {
  103. int i;
  104. for (i = 0; i < blockLength; i++) {
  105. int cc = read_byte(ttyfd, TIMEOUT);
  106. if (cc < 0)
  107. goto timeout;
  108. blockBuf[i] = cc;
  109. }
  110. }
  111. if (docrc) {
  112. crcHi = read_byte(ttyfd, TIMEOUT);
  113. if (crcHi < 0)
  114. goto timeout;
  115. crcLo = read_byte(ttyfd, TIMEOUT);
  116. if (crcLo < 0)
  117. goto timeout;
  118. } else {
  119. cksum = read_byte(ttyfd, TIMEOUT);
  120. if (cksum < 0)
  121. goto timeout;
  122. }
  123. if (blockNo == ((wantBlockNo - 1) & 0xff)) {
  124. /* a repeat of the last block is ok, just ignore it. */
  125. /* this also ignores the initial block 0 which is */
  126. /* meta data. */
  127. goto next;
  128. } else if (blockNo != (wantBlockNo & 0xff)) {
  129. note_error("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo);
  130. goto error;
  131. }
  132. if (docrc) {
  133. int crc = 0;
  134. int i, j;
  135. int expectedCrcHi;
  136. int expectedCrcLo;
  137. for (i = 0; i < blockLength; i++) {
  138. crc = crc ^ (int) blockBuf[i] << 8;
  139. for (j = 0; j < 8; j++)
  140. if (crc & 0x8000)
  141. crc = crc << 1 ^ 0x1021;
  142. else
  143. crc = crc << 1;
  144. }
  145. expectedCrcHi = (crc >> 8) & 0xff;
  146. expectedCrcLo = crc & 0xff;
  147. if ((crcHi != expectedCrcHi) ||
  148. (crcLo != expectedCrcLo)) {
  149. note_error("crc error, expected 0x%02x 0x%02x, got 0x%02x 0x%02x", expectedCrcHi, expectedCrcLo, crcHi, crcLo);
  150. goto error;
  151. }
  152. } else {
  153. unsigned char expectedCksum = 0;
  154. int i;
  155. for (i = 0; i < blockLength; i++)
  156. expectedCksum += blockBuf[i];
  157. if (cksum != expectedCksum) {
  158. note_error("checksum error, expected 0x%02x, got 0x%02x", expectedCksum, cksum);
  159. goto error;
  160. }
  161. }
  162. wantBlockNo++;
  163. length += blockLength;
  164. if (full_write(filefd, blockBuf, blockLength) < 0) {
  165. note_error("write to file failed: %m");
  166. goto fatal;
  167. }
  168. next:
  169. errors = 0;
  170. nak = ACK;
  171. write(ttyfd, &nak, 1);
  172. continue;
  173. error:
  174. timeout:
  175. errors++;
  176. if (errors == MAXERRORS) {
  177. /* Abort */
  178. // if using crc, try again w/o crc
  179. if (nak == 'C') {
  180. nak = NAK;
  181. errors = 0;
  182. docrc = 0;
  183. goto timeout;
  184. }
  185. note_error("too many errors; giving up");
  186. fatal:
  187. /* 5 CAN followed by 5 BS */
  188. write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
  189. return -1;
  190. }
  191. /* Flush pending input */
  192. tcflush(ttyfd, TCIFLUSH);
  193. write(ttyfd, &nak, 1);
  194. }
  195. done:
  196. return length;
  197. #undef note_error
  198. }
  199. static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
  200. {
  201. }
  202. int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  203. int rx_main(int argc, char **argv)
  204. {
  205. char *fn;
  206. int ttyfd, filefd;
  207. struct termios tty, orig_tty;
  208. struct sigaction act;
  209. int n;
  210. char error_buf[256];
  211. if (argc != 2)
  212. bb_show_usage();
  213. fn = argv[1];
  214. ttyfd = xopen(CURRENT_TTY, O_RDWR);
  215. filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
  216. if (tcgetattr(ttyfd, &tty) < 0)
  217. bb_perror_msg_and_die("tcgetattr");
  218. orig_tty = tty;
  219. cfmakeraw(&tty);
  220. tcsetattr(ttyfd, TCSAFLUSH, &tty);
  221. memset(&act, 0, sizeof(act));
  222. act.sa_handler = sigalrm_handler;
  223. sigaction(SIGALRM, &act, 0);
  224. n = receive(error_buf, sizeof(error_buf), ttyfd, filefd);
  225. close(filefd);
  226. tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
  227. if (n < 0)
  228. bb_error_msg_and_die("\nreceive failed:\n %s", error_buf);
  229. fflush_stdout_and_exit(EXIT_SUCCESS);
  230. }