Prepare for reintegration of nvapi for checking thermal limit
[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, 0xD22BDD7E, NVAPI, 0)
33 HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage, NVAPI_GETERRORMESSAGE, nvapi_QueryInterface, 0x6C2D048C, NVAPI, 0)
34 HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs, NVAPI_ENUMPHYSICALGPUS, nvapi_QueryInterface, 0xE5AC921F, NVAPI, 0)
35
36 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfDecreaseInfo, NVAPI_GPU_GETPERFDECREASEINFO, nvapi_QueryInterface, 0x7F7F4600, NVAPI, 0)
37
38 return 0;
39 }
40
41 void nvapi_close (NVAPI_PTR *nvapi)
42 {
43 if (nvapi)
44 {
45 if (nvapi->lib)
46 hc_dlclose (nvapi->lib);
47
48 myfree (nvapi);
49 }
50 }
51
52 int hm_NvAPI_Initialize (NVAPI_PTR *nvapi)
53 {
54 if (!nvapi) return (-1);
55
56 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Initialize ();
57
58 if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) NvAPI_rc = NVAPI_OK; // not a bug
59
60 if (NvAPI_rc != NVAPI_OK)
61 {
62 NvAPI_ShortString string = { 0 };
63
64 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
65
66 log_info ("WARN: %s %d %s\n", "NvAPI_Initialize()", NvAPI_rc, string);
67 }
68
69 return NvAPI_rc;
70 }
71
72 int hm_NvAPI_Unload (NVAPI_PTR *nvapi)
73 {
74 if (!nvapi) return (-1);
75
76 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Unload ();
77
78 if (NvAPI_rc != NVAPI_OK)
79 {
80 NvAPI_ShortString string = { 0 };
81
82 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
83
84 log_info ("WARN: %s %d %s\n", "NvAPI_Unload()", NvAPI_rc, string);
85 }
86
87 return NvAPI_rc;
88 }
89
90 int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
91 {
92 if (!nvapi) return (-1);
93
94 return nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
95 }
96
97 int hm_NvAPI_EnumPhysicalGPUs (NVAPI_PTR *nvapi, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
98 {
99 if (!nvapi) return (-1);
100
101 NvAPI_Status NvAPI_rc = nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
102
103 if (NvAPI_rc != NVAPI_OK)
104 {
105 NvAPI_ShortString string = { 0 };
106
107 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
108
109 log_info ("WARN: %s %d %s\n", "NvAPI_EnumPhysicalGPUs()", NvAPI_rc, string);
110 }
111
112 return NvAPI_rc;
113 }
114
115 int hm_NvAPI_GPU_GetPerfDecreaseInfo (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 *pPerfDecrInfo)
116 {
117 if (!nvapi) return (-1);
118
119 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetPerfDecreaseInfo (hPhysicalGpu, pPerfDecrInfo);
120
121 if (NvAPI_rc != NVAPI_OK)
122 {
123 NvAPI_ShortString string = { 0 };
124
125 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
126
127 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetPerfDecreaseInfo()", NvAPI_rc, string);
128 }
129
130 return NvAPI_rc;
131 }
132
133 #ifdef __MINGW64__
134
135 void __security_check_cookie (uintptr_t _StackCookie)
136 {
137 (void) _StackCookie;
138 }
139
140 void __GSHandlerCheck ()
141 {
142 }
143
144 #endif