Add missing memset and update ext_nvapi.c header
[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 #if __x86_64__
17 nvapi->lib = hc_dlopen ("nvapi64.dll");
18 #elif __x86__
19 nvapi->lib = hc_dlopen ("nvapi.dll");
20 #endif
21
22 if (!nvapi->lib)
23 {
24 if (data.quiet == 0)
25 log_info ("WARNING: load NVAPI library failed, proceed without NVAPI HWMon enabled.");
26
27 return (-1);
28 }
29
30 HC_LOAD_FUNC(nvapi, nvapi_QueryInterface, NVAPI_QUERYINTERFACE, NVAPI, 0)
31 HC_LOAD_ADDR(nvapi, NvAPI_Initialize, NVAPI_INITIALIZE, nvapi_QueryInterface, 0x0150E828, NVAPI, 0)
32 HC_LOAD_ADDR(nvapi, NvAPI_Unload, NVAPI_UNLOAD, nvapi_QueryInterface, 0x0D22BDD7E, NVAPI, 0)
33 HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage, NVAPI_GETERRORMESSAGE, nvapi_QueryInterface, 0x6C2D048C, NVAPI, 0)
34 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetDynamicPstatesInfoEx, NVAPI_GPU_GETDYNAMICPSTATESINFOEX, nvapi_QueryInterface, 0x60DED2ED, NVAPI, 0)
35 HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs, NVAPI_ENUMPHYSICALGPUS, nvapi_QueryInterface, 0xE5AC921F, NVAPI, 0)
36 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetThermalSettings, NVAPI_GPU_GETTHERMALSETTINGS, nvapi_QueryInterface, 0xE3640A56, NVAPI, 0)
37 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetTachReading, NVAPI_GPU_GETTACHREADING, nvapi_QueryInterface, 0x5F608315, NVAPI, 0)
38
39 return 0;
40 }
41
42 void nvapi_close (NVAPI_PTR *nvapi)
43 {
44 if (nvapi)
45 {
46 if (nvapi->lib)
47 hc_dlclose (nvapi->lib);
48
49 myfree (nvapi);
50 }
51 }
52
53 int hm_NvAPI_Initialize (NVAPI_PTR *nvapi)
54 {
55 if (!nvapi) return (-1);
56
57 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Initialize ();
58
59 if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) NvAPI_rc = NVAPI_OK; // not a bug
60
61 if (NvAPI_rc != NVAPI_OK)
62 {
63 NvAPI_ShortString string = { 0 };
64
65 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
66
67 log_info ("WARN: %s %d %s\n", "NvAPI_Initialize()", NvAPI_rc, string);
68 }
69
70 return NvAPI_rc;
71 }
72
73 int hm_NvAPI_Unload (NVAPI_PTR *nvapi)
74 {
75 if (!nvapi) return (-1);
76
77 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Unload ();
78
79 if (NvAPI_rc != NVAPI_OK)
80 {
81 NvAPI_ShortString string = { 0 };
82
83 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
84
85 log_info ("WARN: %s %d %s\n", "NvAPI_Unload()", NvAPI_rc, string);
86 }
87
88 return NvAPI_rc;
89 }
90
91 int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
92 {
93 if (!nvapi) return (-1);
94
95 return nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
96 }
97
98 int hm_NvAPI_EnumPhysicalGPUs (NVAPI_PTR *nvapi, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
99 {
100 if (!nvapi) return (-1);
101
102 NvAPI_Status NvAPI_rc = nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
103
104 if (NvAPI_rc != NVAPI_OK)
105 {
106 NvAPI_ShortString string = { 0 };
107
108 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
109
110 log_info ("WARN: %s %d %s\n", "NvAPI_EnumPhysicalGPUs()", NvAPI_rc, string);
111 }
112
113 return NvAPI_rc;
114 }
115
116 int hm_NvAPI_GPU_GetThermalSettings (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 sensorIndex, NV_GPU_THERMAL_SETTINGS *pThermalSettings)
117 {
118 if (!nvapi) return (-1);
119
120 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetThermalSettings (hPhysicalGpu, sensorIndex, pThermalSettings);
121
122 if (NvAPI_rc != NVAPI_OK)
123 {
124 NvAPI_ShortString string = { 0 };
125
126 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
127
128 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetThermalSettings()", NvAPI_rc, string);
129 }
130
131 return NvAPI_rc;
132 }
133
134 int hm_NvAPI_GPU_GetTachReading (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGPU, NvU32 *pValue)
135 {
136 if (!nvapi) return (-1);
137
138 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetTachReading (hPhysicalGPU, pValue);
139
140 if (NvAPI_rc != NVAPI_OK)
141 {
142 NvAPI_ShortString string = { 0 };
143
144 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
145
146 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetTachReading()", NvAPI_rc, string);
147 }
148
149 return NvAPI_rc;
150 }
151
152 int hm_NvAPI_GPU_GetDynamicPstatesInfoEx (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_DYNAMIC_PSTATES_INFO_EX *pDynamicPstatesInfoEx)
153 {
154 if (!nvapi) return (-1);
155
156 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetDynamicPstatesInfoEx (hPhysicalGpu, pDynamicPstatesInfoEx);
157
158 if (NvAPI_rc != NVAPI_OK)
159 {
160 NvAPI_ShortString string = { 0 };
161
162 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
163
164 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetDynamicPstatesInfoEx()", NvAPI_rc, string);
165 }
166
167 return NvAPI_rc;
168 }
169
170 #ifdef __MINGW64__
171
172 void __security_check_cookie (uintptr_t _StackCookie)
173 {
174 (void) _StackCookie;
175 }
176
177 void __GSHandlerCheck ()
178 {
179 }
180
181 #endif