1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "memory.h"
9 #include "event.h"
10 #include "ext_nvapi.h"
11 
12 #include "dynloader.h"
13 
nvapi_init(void * hashcat_ctx)14 int nvapi_init (void *hashcat_ctx)
15 {
16   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
17 
18   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
19 
20   memset (nvapi, 0, sizeof (NVAPI_PTR));
21 
22   #if defined (_WIN)
23 
24   #if defined (_WIN64)
25   nvapi->lib = hc_dlopen ("nvapi64.dll");
26   #else
27   nvapi->lib = hc_dlopen ("nvapi.dll");
28   #endif
29 
30   #else
31 
32   #if defined (__CYGWIN__)
33 
34   #if defined (__x86_x64__)
35   nvapi->lib = hc_dlopen ("nvapi64.dll");
36   #else
37   nvapi->lib = hc_dlopen ("nvapi.dll");
38   #endif
39 
40   #else
41   nvapi->lib = hc_dlopen ("nvapi.so"); // uhm yes, but .. yeah
42   #endif
43 
44   #endif
45 
46   if (!nvapi->lib)
47   {
48     //if (user_options->quiet == false)
49     //  event_log_error (hashcat_ctx, "Load of NVAPI library failed. Proceeding without NVAPI HWMon enabled.");
50 
51     return -1;
52   }
53 
54   HC_LOAD_FUNC(nvapi, nvapi_QueryInterface,             NVAPI_QUERYINTERFACE,             NVAPI,                0);
55   HC_LOAD_ADDR(nvapi, NvAPI_Initialize,                 NVAPI_INITIALIZE,                 nvapi_QueryInterface, 0x0150E828U, NVAPI, 0);
56   HC_LOAD_ADDR(nvapi, NvAPI_Unload,                     NVAPI_UNLOAD,                     nvapi_QueryInterface, 0xD22BDD7EU, NVAPI, 0);
57   HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage,            NVAPI_GETERRORMESSAGE,            nvapi_QueryInterface, 0x6C2D048CU, NVAPI, 0);
58   HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs,           NVAPI_ENUMPHYSICALGPUS,           nvapi_QueryInterface, 0xE5AC921FU, NVAPI, 0);
59   HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesInfo,    NVAPI_GPU_GETPERFPOLICIESINFO,    nvapi_QueryInterface, 0x409D9841U, NVAPI, 0);
60   HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesStatus,  NVAPI_GPU_GETPERFPOLICIESSTATUS,  nvapi_QueryInterface, 0x3D358A0CU, NVAPI, 0);
61   HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetBusId,               NVAPI_GPU_GETBUSID,               nvapi_QueryInterface, 0x1BE0B8E5U, NVAPI, 0);
62   HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetBusSlotId,           NVAPI_GPU_GETBUSSLOTID,           nvapi_QueryInterface, 0x2A0A350FU, NVAPI, 0);
63 
64   return 0;
65 }
66 
nvapi_close(void * hashcat_ctx)67 void nvapi_close (void *hashcat_ctx)
68 {
69   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
70 
71   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
72 
73   if (nvapi)
74   {
75     if (nvapi->lib)
76       hc_dlclose (nvapi->lib);
77 
78     hcfree (nvapi);
79   }
80 }
81 
hm_NvAPI_GetErrorMessage(NVAPI_PTR * nvapi,const NvAPI_Status NvAPI_rc,NvAPI_ShortString string)82 void hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, const NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
83 {
84   nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
85 }
86 
hm_NvAPI_Initialize(void * hashcat_ctx)87 int hm_NvAPI_Initialize (void *hashcat_ctx)
88 {
89   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
90 
91   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
92 
93   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_Initialize ();
94 
95   if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) return -1;
96 
97   if (NvAPI_rc != NVAPI_OK)
98   {
99     NvAPI_ShortString string = { 0 };
100 
101     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
102 
103     event_log_error (hashcat_ctx, "NvAPI_Initialize(): %s", string);
104 
105     return -1;
106   }
107 
108   return 0;
109 }
110 
hm_NvAPI_Unload(void * hashcat_ctx)111 int hm_NvAPI_Unload (void *hashcat_ctx)
112 {
113   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
114 
115   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
116 
117   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_Unload ();
118 
119   if (NvAPI_rc != NVAPI_OK)
120   {
121     NvAPI_ShortString string = { 0 };
122 
123     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
124 
125     event_log_error (hashcat_ctx, "NvAPI_Unload(): %s", string);
126 
127     return -1;
128   }
129 
130   return 0;
131 }
132 
hm_NvAPI_EnumPhysicalGPUs(void * hashcat_ctx,NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS],NvU32 * pGpuCount)133 int hm_NvAPI_EnumPhysicalGPUs (void *hashcat_ctx, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
134 {
135   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
136 
137   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
138 
139   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
140 
141   if (NvAPI_rc != NVAPI_OK)
142   {
143     NvAPI_ShortString string = { 0 };
144 
145     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
146 
147     event_log_error (hashcat_ctx, "NvAPI_EnumPhysicalGPUs(): %s", string);
148 
149     return -1;
150   }
151 
152   return 0;
153 }
154 
hm_NvAPI_GPU_GetPerfPoliciesInfo(void * hashcat_ctx,NvPhysicalGpuHandle hPhysicalGpu,NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 * perfPolicies_info)155 int hm_NvAPI_GPU_GetPerfPoliciesInfo (void *hashcat_ctx, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 *perfPolicies_info)
156 {
157   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
158 
159   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
160 
161   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_GPU_GetPerfPoliciesInfo (hPhysicalGpu, perfPolicies_info);
162 
163   if (NvAPI_rc != NVAPI_OK)
164   {
165     NvAPI_ShortString string = { 0 };
166 
167     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
168 
169     event_log_error (hashcat_ctx, "NvAPI_GPU_GetPerfPoliciesInfo(): %s", string);
170 
171     return -1;
172   }
173 
174   return 0;
175 }
176 
hm_NvAPI_GPU_GetPerfPoliciesStatus(void * hashcat_ctx,NvPhysicalGpuHandle hPhysicalGpu,NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 * perfPolicies_status)177 int hm_NvAPI_GPU_GetPerfPoliciesStatus (void *hashcat_ctx, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 *perfPolicies_status)
178 {
179   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
180 
181   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
182 
183   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_GPU_GetPerfPoliciesStatus (hPhysicalGpu, perfPolicies_status);
184 
185   if (NvAPI_rc != NVAPI_OK)
186   {
187     NvAPI_ShortString string = { 0 };
188 
189     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
190 
191     event_log_error (hashcat_ctx, "NvAPI_GPU_GetPerfPoliciesStatus(): %s", string);
192 
193     return -1;
194   }
195 
196   return 0;
197 }
198 
hm_NvAPI_GPU_GetBusId(void * hashcat_ctx,NvPhysicalGpuHandle hPhysicalGpu,NvU32 * pBusId)199 int hm_NvAPI_GPU_GetBusId (void *hashcat_ctx, NvPhysicalGpuHandle hPhysicalGpu, NvU32 *pBusId)
200 {
201   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
202 
203   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
204 
205   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_GPU_GetBusId (hPhysicalGpu, pBusId);
206 
207   if (NvAPI_rc != NVAPI_OK)
208   {
209     NvAPI_ShortString string = { 0 };
210 
211     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
212 
213     event_log_error (hashcat_ctx, "NvAPI_GPU_GetBusId(): %s", string);
214 
215     return -1;
216   }
217 
218   return 0;
219 }
220 
hm_NvAPI_GPU_GetBusSlotId(void * hashcat_ctx,NvPhysicalGpuHandle hPhysicalGpu,NvU32 * pBusSlotId)221 int hm_NvAPI_GPU_GetBusSlotId (void *hashcat_ctx, NvPhysicalGpuHandle hPhysicalGpu, NvU32 *pBusSlotId)
222 {
223   hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx;
224 
225   NVAPI_PTR *nvapi = (NVAPI_PTR *) hwmon_ctx->hm_nvapi;
226 
227   const NvAPI_Status NvAPI_rc = (NvAPI_Status) nvapi->NvAPI_GPU_GetBusSlotId (hPhysicalGpu, pBusSlotId);
228 
229   if (NvAPI_rc != NVAPI_OK)
230   {
231     NvAPI_ShortString string = { 0 };
232 
233     hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
234 
235     event_log_error (hashcat_ctx, "NvAPI_GPU_GetBusSlotId(): %s", string);
236 
237     return -1;
238   }
239 
240   return 0;
241 }
242