xms.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. enum {
  5. Soh= 0x1,
  6. Stx= 0x2,
  7. Eot= 0x4,
  8. Ack= 0x6,
  9. Nak= 0x15,
  10. Cancel= 0x18,
  11. };
  12. int send(uchar*, int);
  13. int notifyf(void*, char*);
  14. int debug, progress, onek;
  15. void
  16. errorout(int ctl, int bytes)
  17. {
  18. uchar buf[2];
  19. buf[0] = Cancel;
  20. write(1, buf, 1);
  21. fprint(2, "\nxms: gave up after %d bytes\n", bytes);
  22. write(ctl, "rawoff", 6);
  23. exits("cancel");
  24. }
  25. ushort
  26. updcrc(int c, ushort crc)
  27. {
  28. int count;
  29. for (count=8; --count>=0;) {
  30. if (crc & 0x8000) {
  31. crc <<= 1;
  32. crc += (((c<<=1) & 0400) != 0);
  33. crc ^= 0x1021;
  34. }
  35. else {
  36. crc <<= 1;
  37. crc += (((c<<=1) & 0400) != 0);
  38. }
  39. }
  40. return crc;
  41. }
  42. void
  43. main(int argc, char **argv)
  44. {
  45. uchar c;
  46. uchar buf[1024+5];
  47. uchar seqno;
  48. int fd, ctl;
  49. long n;
  50. int sum;
  51. uchar *p;
  52. int bytes;
  53. int crcmode;
  54. ARGBEGIN{
  55. case 'd':
  56. debug = 1;
  57. break;
  58. case 'p':
  59. progress = 1;
  60. break;
  61. case '1':
  62. onek = 1;
  63. break;
  64. }ARGEND
  65. if(argc != 1){
  66. fprint(2, "usage: xms filename\n");
  67. exits("usage");
  68. }
  69. fd = open(argv[0], OREAD);
  70. if(fd < 0){
  71. perror("xms");
  72. exits("open");
  73. }
  74. ctl = open("/dev/consctl", OWRITE);
  75. if(ctl < 0){
  76. perror("xms");
  77. exits("consctl");
  78. }
  79. write(ctl, "rawon", 5);
  80. /* give the other side a 30 seconds to signal ready */
  81. atnotify(notifyf, 1);
  82. alarm(30*1000);
  83. crcmode = 0;
  84. for(;;){
  85. if(read(0, &c, 1) != 1){
  86. fprint(2, "xms: timeout\n");
  87. exits("timeout");
  88. }
  89. c = c & 0x7f;
  90. if(c == Nak)
  91. break;
  92. if(c == 'C') {
  93. if (debug)
  94. fprint(2, "crc mode engaged\n");
  95. crcmode = 1;
  96. break;
  97. }
  98. }
  99. alarm(0);
  100. /* send the file in 128/1024 byte chunks */
  101. for(bytes = 0, seqno = 1; ; bytes += n, seqno++){
  102. n = read(fd, buf+3, onek ? 1024 : 128);
  103. if(n < 0)
  104. exits("read");
  105. if(n == 0)
  106. break;
  107. if(n < (onek ? 1024 : 128))
  108. memset(&buf[n+3], 0, (onek ? 1024 : 128)-n);
  109. buf[0] = onek ? Stx : Soh;
  110. buf[1] = seqno;
  111. buf[2] = 255 - seqno;
  112. /* calculate checksum and stuff into last byte */
  113. if (crcmode) {
  114. unsigned short crc;
  115. crc = 0;
  116. for(p = buf + 3; p < &buf[(onek ? 1024 : 128)+3]; p++)
  117. crc = updcrc(*p, crc);
  118. crc = updcrc(0, crc);
  119. crc = updcrc(0, crc);
  120. buf[(onek ? 1024 : 128) + 3] = crc >> 8;
  121. buf[(onek ? 1024 : 128) + 4] = crc;
  122. }
  123. else {
  124. sum = 0;
  125. for(p = buf + 3; p < &buf[(onek ? 1024 : 128)+3]; p++)
  126. sum += *p;
  127. buf[(onek ? 1024 : 128) + 3] = sum;
  128. }
  129. if(send(buf, (onek ? 1024 : 128) + 4 + crcmode) < 0)
  130. errorout(ctl, bytes);
  131. if (progress && bytes % 10240 == 0)
  132. fprint(2, "%dK\n", bytes / 1024);
  133. }
  134. /* tell other side we're done */
  135. buf[0] = Eot;
  136. if(send(buf, 1) < 0)
  137. errorout(ctl, bytes);
  138. fprint(2, "xms: %d bytes transmitted\n", bytes);
  139. write(ctl, "rawoff", 6);
  140. exits(0);
  141. }
  142. /*
  143. * send a message till it's acked or we give up
  144. */
  145. int
  146. send(uchar *buf, int len)
  147. {
  148. int tries;
  149. int n;
  150. uchar c;
  151. for(tries = 0;; tries++, sleep(2*1000)){
  152. if(tries == 10)
  153. return -1;
  154. if(write(1, buf, len) != len)
  155. return -1;
  156. alarm(30*1000);
  157. n = read(0, &c, 1);
  158. alarm(0);
  159. if(debug) switch(c){
  160. case Soh:
  161. fprint(2, " Soh");
  162. break;
  163. case Eot:
  164. fprint(2, " Eot");
  165. break;
  166. case Ack:
  167. fprint(2, " Ack");
  168. break;
  169. case Nak:
  170. fprint(2, " Nak");
  171. break;
  172. case Cancel:
  173. fprint(2, "\nremote Cancel\n");
  174. return -1;
  175. default:
  176. if(isprint(c))
  177. fprint(2, "%c", c);
  178. else
  179. fprint(2, " \\0%o", c);
  180. }
  181. c = c & 0x7f;
  182. if(n == 1 && c == Ack)
  183. break;
  184. }
  185. return 0;
  186. }
  187. int
  188. notifyf(void *a, char *msg)
  189. {
  190. USED(a);
  191. if(strcmp(msg, "alarm") == 0)
  192. return 1;
  193. return 0;
  194. }