Skip to content

Commit

Permalink
Miscellaneous changes (zoogie#8)
Browse files Browse the repository at this point in the history
* Remove uneccessary externs

* Improve Makefile
- For Windows, the Makefile now checks if INTELSDKROOT is actually defined in order to prevent accidental inclusion of an 'include' directory (if one exists)
- The Makefile has a few comments reworded and cleaned up

* Another few rewordings in the Makefile

* Rectify a mistake that causes LFCS mining to forcefully use 28 group
bits

* Possibly prevent CL_DEVICE_NOT_FOUND errors from occurring

* Correct a comment's wording

* Simplify Makefile and enforce 64-bit compilation

* Use OpenCL 1.2 headers if unified headers are used

* Use fgets() instead of scanf() and add some utils

* Revert "Possibly prevent CL_DEVICE_NOT_FOUND errors from occurring"

This reverts commit 8c14941 since it
seems that there is no apparent benefit.

* Code reorganization & cleanup

* Finalize 'seedminer mode'

* Rewording and temporarily delete unistd.h

* Include unistd.h inside of utils.h instead

* Update utils.h

* Update utils.c
  • Loading branch information
Mike15678 authored and zoogie committed Jul 16, 2018
1 parent b089ad3 commit 6a12c90
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 57 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
PNAME = bfcl
OBJS = $(PNAME).o ocl_util.o utils.o sha1_16.o aes_128.o ocl_test.o ocl_brute.o
CFLAGS += -std=c11 -Wall -Werror -O2 -mrdrnd -m64

ifdef SYSTEMROOT
# Intel's OpenCL SDK installer sets an environmental variable on Windows.
CFLAGS += -std=c11 -Wall -Werror -O2 -mrdrnd -I$(INTELOCLSDKROOT)\include
LDFLAGS += -L$(INTELOCLSDKROOT)\lib\x64
# Intel's Windows OpenCL SDK installer sets an environmental variable.
ifdef INTELOCLSDKROOT
CFLAGS += -I$(INTELOCLSDKROOT)\include
LDFLAGS += -L$(INTELOCLSDKROOT)\lib\x64
endif
else
ifeq ($(shell uname), Linux)
# Intel's OpenCL SDK installer doesn't set an environment variable on Linux, so we'll have to specify its default installation location instead.
CFLAGS += -std=c11 -Wall -Werror -O2 -mrdrnd -I/opt/intel/opencl-sdk/include
# Intel's Linux OpenCL SDK installer doesn't set an environment variable, so we'll have to specify its default installation location instead, regardless if it exists or not.
CFLAGS += -I/opt/intel/opencl-sdk/include
LDFLAGS += -L/opt/intel/opencl-sdk/lib64
endif
ifeq ($(shell uname), Darwin)
# macOS's linker likes to warn you about library dirs not being found. That being said, macOS includes its own implementation of OpenCL, so CFLAGS and LDFLAGS are not needed.
CFLAGS += -std=c11 -Wall -Werror -O2 -mrdrnd
endif
endif

all: $(PNAME)
Expand All @@ -25,7 +25,7 @@ ifeq ($(shell uname), Darwin)
else
$(CC) $(LDFLAGS) -o $@ $^ -lOpenCL -lmbedcrypto
# If you want to use the mbedcrypto static library instead (whether you're using MSYS2 or are on Linux), change "-lmbedcrypto" to "-l:libmbedcrypto.a" without the quotes.
# Note: Ubuntu (probably Debian as well) doesn't install "libmbedcrypto.a" through apt-get, thus you would have to compile mbedtls yourself.
# Note: Ubuntu (probably Debian as well) doesn't install "libmbedcrypto.a" through apt-get, thus you would have to compile mbedtls yourself if you want to use its static libraries.
endif

clean:
Expand Down
49 changes: 15 additions & 34 deletions bfcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include "utils.h"
#include "ocl.h"
#include "ocl_brute.h"
#include "ocl_util.h"

int ocl_test();

int seedminer_mode = 0;

static inline cl_ushort u16be(const unsigned char *in){
cl_ushort out;
unsigned char *out8 = (unsigned char *)&out;
Expand All @@ -21,6 +18,7 @@ static inline cl_ushort u16be(const unsigned char *in){
const char invalid_parameters[] = "invalid parameters\n";

int main(int argc, const char *argv[]) {
seedminer_mode = 0;
int ret = 0;
if (argc == 1) {
ret = ocl_test();
Expand All @@ -34,28 +32,20 @@ int main(int argc, const char *argv[]) {
hex2bytes((unsigned char*)ver, 16, argv[3], 1);
hex2bytes((unsigned char*)&msky_offset, 4, argv[4], 1);
if (argc == 5 && !strcmp(argv[1], "msky")) {
rws_mode = 0;
group_bits = 28;
/*Uncomment the following (and delete this current line) when a new Seedminer Python script is realeased:
char response;
printf("\nWARNING: Deprecated parameters are being used (most likely due to using an outdated Seedminer Python script!\nIf problems occur and you are using Seedminer, download an updated Python script.\nWould you like to continue? Enter Y or N: \n");
scanf("%c", &response);
while (1 == 1) {
if (response == 'Y' || response == 'y')
break;
else if (response == 'N' || response == 'n')
exit(0);
else {
printf("Invalid option chosen!\nWould you like to continue with the mining? Enter Y or N: \n");
scanf(" %c", &response);
}
}*/
/*Uncomment the following (and delete this current line) when a new Seedminer Python script is released:
deprecation_notice_and_input();*/
} else if ((argc == 6 || argc == 7) && !strcmp(argv[5], "sws")) {
rws_mode = 0;
group_bits = 28;
} else if ((argc == 6 || argc == 7) && !strcmp(argv[5], "rws")) {
rws_mode = 1;
group_bits = 20;
}
if (argc == 7 && !strcmp(argv[6], "sm"))
if (argc == 7 && !strcmp(argv[6], "sm")) {
seedminer_mode = 1;
}
ret = ocl_brute_msky(msky, ver, msky_offset);
// More extremely condensed argument parsing incoming!
} else if ((argc == 6 && !strcmp(argv[1], "lfcs")) || ((argc == 7 && !strcmp(argv[1], "lfcs")) && (!strcmp(argv[6], "sws") || !strcmp(argv[6], "rws"))) || ((argc == 8 && !strcmp(argv[1], "msky")) && ((!strcmp(argv[6], "sws") && !strcmp(argv[7], "sm")) || (!strcmp(argv[6], "rws") && !strcmp(argv[7], "sm"))))) {
Expand All @@ -66,29 +56,20 @@ int main(int argc, const char *argv[]) {
hex2bytes((unsigned char*)ver, 8, argv[4], 1);
hex2bytes((unsigned char*)&lfcs_offset, 4, argv[5], 1);
if (argc == 6 && !strcmp(argv[1], "lfcs")) {
rws_mode = 0;
group_bits = 28;
/*Uncomment the following (and delete this current line) when a new Seedminer Python script is realeased:
char response;
printf("\nWARNING: Deprecated parameters are being used (most likely due to using an outdated Seedminer Python script!\nIf problems occur and you are using Seedminer, download an updated Python script.\nWould you like to continue? Enter Y or N: \n");
scanf("%c", &response);
while (1 == 1) {
if (response == 'Y' || response == 'y')
break;
else if (response == 'N' || response == 'n')
exit(0);
else {
printf("Invalid option chosen!\nWould you like to continue with the mining? Enter Y or N: \n");
scanf(" %c", &response);
}
}*/
/*Uncomment the following (and delete this current line) when a new Seedminer Python script is released:
deprecation_notice_and_input();*/
} else if ((argc == 7 || argc == 8) && !strcmp(argv[6], "sws")) {
rws_mode = 0;
group_bits = 28;
} else if ((argc == 7 || argc == 8) && !strcmp(argv[6], "rws")) {
rws_mode = 1;
group_bits = 20;
}
if (argc == 8 && !strcmp(argv[7], "sm"))
if (argc == 8 && !strcmp(argv[7], "sm")) {
seedminer_mode = 1;
group_bits = 28;
}
ret = ocl_brute_lfcs(lfcs, newflag, ver, lfcs_offset);
} else if (argc == 7) {
unsigned char console_id[8], emmc_cid[16], offset[2], src[16], ver[16];
Expand Down
3 changes: 2 additions & 1 deletion ocl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#define CL_TARGET_OPENCL_VERSION 120 // For use with unified headers since no OpenCL 2.0+ API calls are made.
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS // In the event unified headers aren't used. OpenCL 2.0+ headers are backwards-compatible.
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
Expand Down
10 changes: 6 additions & 4 deletions ocl_brute.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ static cl_ulong from_bcd(cl_ulong i) {
return o;
}

unsigned group_bits;

int ocl_brute_console_id(const cl_uchar *console_id, const cl_uchar *emmc_cid,
cl_uint offset0, const cl_uchar *src0, const cl_uchar *ver0,
cl_uint offset1, const cl_uchar *src1, const cl_uchar *ver1,
Expand Down Expand Up @@ -345,7 +343,9 @@ int ocl_brute_msky(const cl_uint *msky, const cl_uint *ver, cl_uint msky_offset)

size_t local;
OCL_ASSERT(clGetKernelWorkGroupInfo(kernel, device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, NULL));
printf("local work size: %u\n", (unsigned)local);
if (seedminer_mode != 1 || rws_mode != 1) {
printf("local work size: %u\n", (unsigned)local);
}

// there's no option to create it zero initialized
cl_uint out = 0;
Expand Down Expand Up @@ -465,7 +465,9 @@ int ocl_brute_lfcs(cl_uint lfcs_template, cl_ushort newflag, const cl_uint *ver,

size_t local;
OCL_ASSERT(clGetKernelWorkGroupInfo(kernel, device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, NULL));
printf("local work size: %u\n", (unsigned)local);
if (seedminer_mode != 1 || rws_mode != 1) {
printf("local work size: %u\n", (unsigned)local);
}

// there's no option to create it zero initialized
cl_uint out = 0;
Expand Down
2 changes: 1 addition & 1 deletion ocl_brute.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ int ocl_brute_msky(const cl_uint *msky, const cl_uint *ver, cl_uint msky_offset)

int ocl_brute_lfcs(cl_uint lfcs_template, cl_ushort newflag, const cl_uint *ver, cl_uint lfcs_offset);

extern unsigned group_bits;
unsigned group_bits;
16 changes: 10 additions & 6 deletions ocl_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "ocl.h"
#ifdef __APPLE__
#include <OpenCL/cl_ext.h>
#else
#include <CL/cl_ext.h>
#endif
#include "ocl.h"
#include "utils.h"
#include "ocl_util.h"

#define STATIC_ASSERT(c) static_assert(c, #c)
STATIC_ASSERT(sizeof(char) == sizeof(cl_char));
Expand Down Expand Up @@ -49,8 +48,9 @@ const char * ocl_err_msg(cl_int error_code) {
void ocl_assert(cl_int ret, const char * code, const char * file,
const char * function, unsigned line) {
// Silence "Out of Resources" error if Seedminer is being used
if (ret == CL_OUT_OF_RESOURCES && seedminer_mode == 1)
if (ret == CL_OUT_OF_RESOURCES && seedminer_mode == 1) {
exit(ret);
}
else if (ret != CL_SUCCESS) {
printf("%s: %s, function %s, line %u\n\t%s\nerror: %s\n",
__FUNCTION__, file, function, line, code, ocl_err_msg(ret));
Expand Down Expand Up @@ -207,8 +207,10 @@ void ocl_get_device(cl_platform_id *p_platform_id, cl_device_id *p_device_id) {
}
}
if (maximum > 0) {
printf("selected device %s on platform %s\n",
trim((char*)platforms[pl_idx].devices[dev_idx].name), trim((char*)platforms[pl_idx].name));
if (seedminer_mode != 1 || rws_mode != 1) {
printf("selected device %s on platform %s\n",
trim((char*)platforms[pl_idx].devices[dev_idx].name), trim((char*)platforms[pl_idx].name));
}
*p_platform_id = platforms[pl_idx].id;
*p_device_id = platforms[pl_idx].devices[dev_idx].id;
} else {
Expand Down Expand Up @@ -237,7 +239,9 @@ cl_program ocl_build_from_sources(
// printf("compiler options: %s\n", options);
err = clBuildProgram(program, 0, NULL, options, NULL, NULL);
get_hp_time(&t1);
printf("%.3f seconds for OpenCL compiling\n", hp_time_diff(&t0, &t1) / 1000000.0);
if (seedminer_mode != 1 || rws_mode != 1) {
printf("%.3f seconds for OpenCL compiling\n", hp_time_diff(&t0, &t1) / 1000000.0);
}
if (err != CL_SUCCESS) {
fprintf(stderr, "failed to compile program, error: %s, build log:\n", ocl_err_msg(err));
size_t len;
Expand Down
1 change: 0 additions & 1 deletion ocl_util.h

This file was deleted.

41 changes: 41 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <intrin.h>
#endif

#ifndef _WIN32
#include <unistd.h>
#endif

int htoi(char a){
if(a >= '0' && a <= '9'){
return a - '0';
Expand Down Expand Up @@ -178,3 +182,40 @@ char * trim(char *in) {
}
return first_non_ws;
}

void real_sleep(int sleep_sec) {
#ifdef _WIN32
Sleep(sleep_sec * 1000);
#else
sleep(sleep_sec);
#endif
}

void intHandler() {
// Not really needed for now.
stop_bfcl = 1;
exit(0);
}

void deprecation_notice_and_input() {
char response[2];
// NO BUFFER OVERFLOWS CAN HAPPEN HERE!!! >:(
printf("\nWARNING: Deprecated parameters are being used (most likely due to using an outdated Seedminer Python script)!\nIf problems occur and you are using Seedminer, download an updated Python script.\nWould you like to continue? Enter Y or N: ");
fflush(stdout);
// As long as your response starts with a "y" or an "n", it'll be accepted and the rest will be truncated.
fgets(response, sizeof(response), stdin);
fflush(stdin);
while (1) {
real_sleep(1);
if (!strcmp(response, "Y") || !strcmp(response, "y")) {
break;
} else if (!strcmp(response, "N") || !strcmp(response, "n")) {
exit(0);
} else {
printf("\nInvalid choice chosen!\nWould you like to continue with the mining? Enter Y or N: ");
fflush(stdout);
fgets(response, sizeof(response), stdin);
fflush(stdin);
}
}
}
14 changes: 14 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <signal.h> // I sure do hope this works on MS VS; for use with signal handling.

// a crude cross Windows/POSIX high precision timer
#ifdef _WIN32

Expand Down Expand Up @@ -36,3 +38,15 @@ int cpu_has_rdrand();
int rdrand_fill(unsigned long long *p, size_t size);

char * trim(char *in);

int stop_bfcl;

int seedminer_mode;

int rws_mode;

void real_sleep(int sleep_sec);

void intHandler();

void deprecation_notice_and_input();

0 comments on commit 6a12c90

Please sign in to comment.