Added --gpu-temp-retain support for windows through nvapi
[hashcat.git] / src / ext_nvapi.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 *
5 * License.....: MIT
6 */
7
8 #include <ext_nvapi.h>
9
10 int nvapi_init (NVAPI_PTR *nvapi)
11 {
12 if (!nvapi) return (-1);
13
14 memset (nvapi, 0, sizeof (NVAPI_PTR));
15
16 #ifdef _WIN
17 #if __x86_64__
18 nvapi->lib = hc_dlopen ("nvapi64.dll");
19 #elif __x86__
20 nvapi->lib = hc_dlopen ("nvapi.dll");
21 #endif
22 #else
23 nvapi->lib = hc_dlopen ("nvapi.so", RTLD_NOW); // uhm yes, but .. yeah
24 #endif
25
26 if (!nvapi->lib)
27 {
28 //if (data.quiet == 0)
29 // log_info ("WARNING: load NVAPI library failed, proceed without NVAPI HWMon enabled.");
30
31 return (-1);
32 }
33
34 HC_LOAD_FUNC(nvapi, nvapi_QueryInterface, NVAPI_QUERYINTERFACE, NVAPI, 0)
35 HC_LOAD_ADDR(nvapi, NvAPI_Initialize, NVAPI_INITIALIZE, nvapi_QueryInterface, 0x0150E828, NVAPI, 0)
36 HC_LOAD_ADDR(nvapi, NvAPI_Unload, NVAPI_UNLOAD, nvapi_QueryInterface, 0xD22BDD7E, NVAPI, 0)
37 HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage, NVAPI_GETERRORMESSAGE, nvapi_QueryInterface, 0x6C2D048C, NVAPI, 0)
38 HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs, NVAPI_ENUMPHYSICALGPUS, nvapi_QueryInterface, 0xE5AC921F, NVAPI, 0)
39 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesInfo, NVAPI_GPU_GETPERFPOLICIESINFO, nvapi_QueryInterface, 0x409D9841, NVAPI, 0)
40 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesStatus, NVAPI_GPU_GETPERFPOLICIESSTATUS, nvapi_QueryInterface, 0x3D358A0C, NVAPI, 0)
41 HC_LOAD_ADDR(nvapi, NvAPI_GPU_SetCoolerLevels, NVAPI_GPU_SETCOOLERLEVELS, nvapi_QueryInterface, 0x891FA0AE, NVAPI, 0)
42
43 return 0;
44 }
45
46 void nvapi_close (NVAPI_PTR *nvapi)
47 {
48 if (nvapi)
49 {
50 if (nvapi->lib)
51 hc_dlclose (nvapi->lib);
52
53 myfree (nvapi);
54 }
55 }
56
57 int hm_NvAPI_Initialize (NVAPI_PTR *nvapi)
58 {
59 if (!nvapi) return (-1);
60
61 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Initialize ();
62
63 if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) NvAPI_rc = NVAPI_OK; // not a bug
64
65 if (NvAPI_rc != NVAPI_OK)
66 {
67 NvAPI_ShortString string = { 0 };
68
69 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
70
71 log_info ("WARN: %s %d %s\n", "NvAPI_Initialize()", NvAPI_rc, string);
72 }
73
74 return NvAPI_rc;
75 }
76
77 int hm_NvAPI_Unload (NVAPI_PTR *nvapi)
78 {
79 if (!nvapi) return (-1);
80
81 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Unload ();
82
83 if (NvAPI_rc != NVAPI_OK)
84 {
85 NvAPI_ShortString string = { 0 };
86
87 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
88
89 log_info ("WARN: %s %d %s\n", "NvAPI_Unload()", NvAPI_rc, string);
90 }
91
92 return NvAPI_rc;
93 }
94
95 int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
96 {
97 if (!nvapi) return (-1);
98
99 return nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
100 }
101
102 int hm_NvAPI_EnumPhysicalGPUs (NVAPI_PTR *nvapi, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
103 {
104 if (!nvapi) return (-1);
105
106 NvAPI_Status NvAPI_rc = nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
107
108 if (NvAPI_rc != NVAPI_OK)
109 {
110 NvAPI_ShortString string = { 0 };
111
112 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
113
114 log_info ("WARN: %s %d %s\n", "NvAPI_EnumPhysicalGPUs()", NvAPI_rc, string);
115 }
116
117 return NvAPI_rc;
118 }
119
120 int hm_NvAPI_GPU_GetPerfPoliciesInfo (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 *perfPolicies_info)
121 {
122 if (!nvapi) return (-1);
123
124 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetPerfPoliciesInfo (hPhysicalGpu, perfPolicies_info);
125
126 if (NvAPI_rc != NVAPI_OK)
127 {
128 NvAPI_ShortString string = { 0 };
129
130 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
131
132 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetPerfPoliciesInfo()", NvAPI_rc, string);
133 }
134
135 return NvAPI_rc;
136 }
137
138 int hm_NvAPI_GPU_GetPerfPoliciesStatus (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 *perfPolicies_status)
139 {
140 if (!nvapi) return (-1);
141
142 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetPerfPoliciesStatus (hPhysicalGpu, perfPolicies_status);
143
144 if (NvAPI_rc != NVAPI_OK)
145 {
146 NvAPI_ShortString string = { 0 };
147
148 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
149
150 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetPerfPoliciesStatus()", NvAPI_rc, string);
151 }
152
153 return NvAPI_rc;
154 }
155
156 int hm_NvAPI_GPU_SetCoolerLevels (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex, NV_GPU_COOLER_LEVELS *pCoolerLevels)
157 {
158 if (!nvapi) return (-1);
159
160 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_SetCoolerLevels (hPhysicalGpu, coolerIndex, pCoolerLevels);
161
162 if (NvAPI_rc != NVAPI_OK)
163 {
164 NvAPI_ShortString string = { 0 };
165
166 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
167
168 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_SetCoolerLevels()", NvAPI_rc, string);
169 }
170
171 return NvAPI_rc;
172 }
173
174 #ifdef __MINGW64__
175
176 void __security_check_cookie (uintptr_t _StackCookie)
177 {
178 (void) _StackCookie;
179 }
180
181 void __GSHandlerCheck ()
182 {
183 }
184
185 #endif