libbitfury.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * libbitfury.c - library for Bitfury chip/board
  3. *
  4. * Copyright (c) 2013 bitfury
  5. * Copyright (c) 2013 legkodymov
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. **/
  26. #include "config.h"
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include "miner.h"
  31. #include "libbitfury.h"
  32. #include "spidevc.h"
  33. #include "sha2.h"
  34. #include <time.h>
  35. #define BITFURY_REFRESH_DELAY 100
  36. #define BITFURY_DETECT_TRIES 3000 / BITFURY_REFRESH_DELAY
  37. // 0 .... 31 bit
  38. // 1000 0011 0101 0110 1001 1010 1100 0111
  39. // 1100 0001 0110 1010 0101 1001 1110 0011
  40. // C16A59E3
  41. unsigned char enaconf[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
  42. unsigned char disconf[4] = { 0, 0, 0, 0 };
  43. /* Configuration registers - control oscillators and such stuff. PROGRAMMED when magic number is matches, UNPROGRAMMED (default) otherwise */
  44. void config_reg(int cfgreg, int ena)
  45. {
  46. if (ena) spi_emit_data(0x7000+cfgreg*32, (void*)enaconf, 4);
  47. else spi_emit_data(0x7000+cfgreg*32, (void*)disconf, 4);
  48. }
  49. #define FIRST_BASE 61
  50. #define SECOND_BASE 4
  51. char counters[16] = { 64, 64,
  52. SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
  53. (FIRST_BASE)%65, (FIRST_BASE+1)%65, (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
  54. //char counters[16] = { 64, 64,
  55. // SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
  56. // (FIRST_BASE)%65, (FIRST_BASE+1)%65, (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
  57. char *buf = "Hello, World!\x55\xaa";
  58. char outbuf[16];
  59. /* Oscillator setup variants (maybe more), values inside of chip ANDed to not allow by programming errors work it at higher speeds */
  60. /* WARNING! no chip temperature control limits, etc. It may self-fry and make fried chips with great ease :-) So if trying to overclock */
  61. /* Do not place chip near flammable objects, provide adequate power protection and better wear eye protection ! */
  62. /* Thermal runaway in this case could produce nice flames of chippy fries */
  63. // Thermometer code from left to right - more ones ==> faster clock!
  64. unsigned char osc6[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 0x00 };
  65. /* Test vectors to calculate (using address-translated loads) */
  66. unsigned atrvec[] = {
  67. 0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
  68. 0,0,0,0,0,0,0,0,
  69. 0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  70. 0x9c4dfdc0, 0xf055c9e1, 0xe60f079d, 0xeeada6da, 0xd459883d, 0xd8049a9d, 0xd49f9a96, 0x15972fed, /* MIDSTATE */
  71. 0,0,0,0,0,0,0,0,
  72. 0x048b2528, 0x7acb2d4f, 0x0b290c1a, 0xbe00084a, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  73. 0x0317b3ea, 0x1d227d06, 0x3cca281e, 0xa6d0b9da, 0x1a359fe2, 0xa7287e27, 0x8b79c296, 0xc4d88274, /* MIDSTATE */
  74. 0,0,0,0,0,0,0,0,
  75. 0x328bcd4f, 0x75462d4f, 0x0b290c1a, 0x002c6dbc, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  76. 0xac4e38b6, 0xba0e3b3b, 0x649ad6f8, 0xf72e4c02, 0x93be06fb, 0x366d1126, 0xf4aae554, 0x4ff19c5b, /* MIDSTATE */
  77. 0,0,0,0,0,0,0,0,
  78. 0x72698140, 0x3bd62b4f, 0x3fd40c1a, 0x801e43e9, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  79. 0x9dbf91c9, 0x12e5066c, 0xf4184b87, 0x8060bc4d, 0x18f9c115, 0xf589d551, 0x0f7f18ae, 0x885aca59, /* MIDSTATE */
  80. 0,0,0,0,0,0,0,0,
  81. 0x6f3806c3, 0x41f82a4f, 0x3fd40c1a, 0x00334b39, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  82. };
  83. #define rotrFixed(x,y) (((x) >> (y)) | ((x) << (32-(y))))
  84. #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
  85. #define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
  86. #define Ch(x,y,z) (z^(x&(y^z)))
  87. #define Maj(x,y,z) (y^((x^y)&(y^z)))
  88. #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
  89. #define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
  90. /* SHA256 CONSTANTS */
  91. static const unsigned SHA_K[64] = {
  92. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  93. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  94. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  95. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  96. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  97. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  98. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  99. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  100. };
  101. void ms3_compute(unsigned *p)
  102. {
  103. unsigned a,b,c,d,e,f,g,h, ne, na, i;
  104. a = p[0]; b = p[1]; c = p[2]; d = p[3]; e = p[4]; f = p[5]; g = p[6]; h = p[7];
  105. for (i = 0; i < 3; i++) {
  106. ne = p[i+16] + SHA_K[i] + h + Ch(e,f,g) + S1(e) + d;
  107. na = p[i+16] + SHA_K[i] + h + Ch(e,f,g) + S1(e) + S0(a) + Maj(a,b,c);
  108. d = c; c = b; b = a; a = na;
  109. h = g; g = f; f = e; e = ne;
  110. }
  111. p[15] = a; p[14] = b; p[13] = c; p[12] = d; p[11] = e; p[10] = f; p[9] = g; p[8] = h;
  112. }
  113. int detect_chip(int chip_n) {
  114. unsigned w[16];
  115. int i;
  116. unsigned newbuf[17], oldbuf[17];
  117. memset(newbuf, 0, 17 * 4);
  118. memset(oldbuf, 0, 17 * 4);
  119. ms3_compute(&atrvec[0]);
  120. ms3_compute(&atrvec[20]);
  121. ms3_compute(&atrvec[40]);
  122. spi_init();
  123. spi_clear_buf();
  124. spi_emit_break(); /* First we want to break chain! Otherwise we'll get all of traffic bounced to output */
  125. spi_emit_fasync(chip_n);
  126. spi_emit_data(0x6000, (void*)osc6, 8); /* Program internal on-die slow oscillator frequency */
  127. config_reg(7,0); config_reg(8,0); config_reg(9,0); config_reg(10,0); config_reg(11,0);
  128. config_reg(6,1);
  129. config_reg(4,1); /* Enable slow oscillator */
  130. config_reg(1,0); config_reg(2,0); config_reg(3,0);
  131. spi_emit_data(0x0100, (void*)counters, 16); /* Program counters correctly for rounds processing, here baby should start consuming power */
  132. /* Prepare internal buffers */
  133. /* PREPARE BUFFERS (INITIAL PROGRAMMING) */
  134. memset(&w, 0, sizeof(w)); w[3] = 0xffffffff; w[4] = 0x80000000; w[15] = 0x00000280;
  135. spi_emit_data(0x1000, (void*)w, 16*4);
  136. spi_emit_data(0x1400, (void*)w, 8*4);
  137. memset(w, 0, sizeof(w)); w[0] = 0x80000000; w[7] = 0x100;
  138. spi_emit_data(0x1900, (void*)&w[0],8*4); /* Prepare MS and W buffers! */
  139. spi_emit_data(0x3000, (void*)&atrvec[0], 19*4);
  140. spi_txrx(spi_gettxbuf(), spi_getrxbuf(), spi_getbufsz());
  141. for (i = 0; i < BITFURY_DETECT_TRIES; i++) {
  142. spi_clear_buf();
  143. spi_emit_break();
  144. spi_emit_fasync(chip_n);
  145. spi_emit_data(0x3000, (void*)&atrvec[0], 19*4);
  146. spi_txrx(spi_gettxbuf(), spi_getrxbuf(), spi_getbufsz());
  147. memcpy(newbuf, spi_getrxbuf() + 4 + chip_n, 17*4);
  148. if (newbuf[16] != 0 && newbuf[16] != 0xFFFFFFFF) {
  149. return 0;
  150. }
  151. if (i && newbuf[16] != oldbuf[16])
  152. return 1;
  153. cgsleep_ms(BITFURY_REFRESH_DELAY);
  154. memcpy(oldbuf, newbuf, 17 * 4);
  155. }
  156. return 0;
  157. }
  158. int libbitfury_detectChips(void) {
  159. int n = 0;
  160. int detected;
  161. do {
  162. detected = detect_chip(n);
  163. if (detected) {
  164. n++;
  165. applog(LOG_WARNING, "BITFURY chip #%d detected", n);
  166. } else {
  167. }
  168. } while (detected);
  169. return n;
  170. }
  171. unsigned decnonce(unsigned in)
  172. {
  173. unsigned out;
  174. /* First part load */
  175. out = (in & 0xFF) << 24; in >>= 8;
  176. /* Byte reversal */
  177. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  178. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  179. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  180. out |= (in >> 2)&0x3FFFFF;
  181. /* Extraction */
  182. if (in & 1) out |= (1 << 23);
  183. if (in & 2) out |= (1 << 22);
  184. out -= 0x800004;
  185. return out;
  186. }
  187. int rehash(unsigned char *midstate, unsigned m7,
  188. unsigned ntime, unsigned nbits, unsigned nnonce) {
  189. unsigned char in[16];
  190. unsigned char hash1[32];
  191. unsigned int *in32 = (unsigned int *)in;
  192. unsigned char hex[65];
  193. unsigned int *mid32 = (unsigned int *)midstate;
  194. unsigned out32[8];
  195. unsigned char *out = (unsigned char *) out32;
  196. int i;
  197. sha256_ctx ctx;
  198. memset( &ctx, 0, sizeof( sha256_ctx ) );
  199. memcpy(ctx.h, mid32, 8*4);
  200. ctx.tot_len = 64;
  201. ctx.len = 0;
  202. nnonce = bswap_32(nnonce);
  203. in32[0] = bswap_32(m7);
  204. in32[1] = bswap_32(ntime);
  205. in32[2] = bswap_32(nbits);
  206. in32[3] = nnonce;
  207. sha256_update(&ctx, in, 16);
  208. sha256_final(&ctx, out);
  209. sha256(out, 32, out);
  210. if (out32[7] == 0) {
  211. bin2hex(hex, midstate, 32);
  212. bin2hex(hex, out, 32);
  213. applog(LOG_INFO, "! MS0: %08x, m7: %08x, ntime: %08x, nbits: %08x, nnonce: %08x\n\t\t\t out: %s\n", mid32[0], m7, ntime, nbits, nnonce, hex);
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. void work_to_payload(struct bitfury_payload *p, struct work *w) {
  219. unsigned char flipped_data[80];
  220. memset(p, 0, sizeof(struct bitfury_payload));
  221. swap32yes(flipped_data, w->data, 80 / 4);
  222. memcpy(p->midstate, w->midstate, 32);
  223. p->m7 = bswap_32(*(unsigned *)(flipped_data + 64));
  224. p->ntime = bswap_32(*(unsigned *)(flipped_data + 68));
  225. p->nbits = bswap_32(*(unsigned *)(flipped_data + 72));
  226. applog(LOG_INFO, "INFO nonc: %08x bitfury_scanHash MS0: %08x, ", p->nnonce, ((unsigned int *)w->midstate)[0]);
  227. applog(LOG_INFO, "INFO merkle[7]: %08x, ntime: %08x, nbits: %08x", p->m7, p->ntime, p->nbits);
  228. }
  229. int libbitfury_sendHashData(struct bitfury_device *bf, int chip_n) {
  230. int chip;
  231. static unsigned second_run;
  232. for (chip = 0; chip < chip_n; chip++) {
  233. unsigned char *hexstr;
  234. struct bitfury_device *d = bf + chip;
  235. unsigned *newbuf = d->newbuf;
  236. unsigned *oldbuf = d->oldbuf;
  237. struct bitfury_payload *p = &(d->payload);
  238. struct bitfury_payload *op = &(d->opayload);
  239. /* Programming next value */
  240. memcpy(atrvec, p, 20*4);
  241. ms3_compute(atrvec);
  242. spi_clear_buf(); spi_emit_break();
  243. spi_emit_fasync(chip);
  244. spi_emit_data(0x3000, (void*)&atrvec[0], 19*4);
  245. spi_txrx(spi_gettxbuf(), spi_getrxbuf(), spi_getbufsz());
  246. memcpy(newbuf, spi_getrxbuf()+4 + chip, 17*4);
  247. d->job_switched = newbuf[16] != oldbuf[16];
  248. if (second_run && d->job_switched) {
  249. int i;
  250. int results_num = 0;
  251. unsigned * results = d->results;
  252. for (i = 0; i < 16; i++) {
  253. if (oldbuf[i] != newbuf[i]) {
  254. unsigned pn; //possible nonce
  255. unsigned int s = 0; //TODO zero may be solution
  256. pn = decnonce(newbuf[i]);
  257. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn) ? pn : 0;
  258. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x400000) ? pn - 0x400000 : 0;
  259. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x800000) ? pn - 0x800000 : 0;
  260. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x2800000)? pn + 0x2800000 : 0;
  261. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x2C00000)? pn + 0x2C00000 : 0;
  262. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x400000) ? pn + 0x400000 : 0;
  263. if (s) {
  264. results[results_num++] = bswap_32(s);
  265. }
  266. }
  267. }
  268. d->results_n = results_num;
  269. memcpy(op, p, sizeof(struct bitfury_payload));
  270. memcpy(oldbuf, newbuf, 17 * 4);
  271. }
  272. cgsleep_ms(BITFURY_REFRESH_DELAY);
  273. }
  274. second_run = 1;
  275. return;
  276. }
  277. int libbitfury_readHashData(unsigned int *res) {
  278. return 0;
  279. }