-
Notifications
You must be signed in to change notification settings - Fork 3
/
cachex_win.h
202 lines (169 loc) · 5.23 KB
/
cachex_win.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef CACHEX_WIN_H
#define CACHEX_WIN_H
#include "result.h"
#include <array>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
namespace windows_detail
{
static const UCHAR SCSI_IOCTL_DATA_OUT = 0;
static const UCHAR SCSI_IOCTL_DATA_IN = 1;
static const UCHAR SCSI_IOCTL_DATA_UNSPECIFIED = 2;
static const DWORD IOCTL_SCSI_PASS_THROUGH_DIRECT = 0x4D014;
static double init_qpc_freq()
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return static_cast<double>((freq.QuadPart / 1000));
}
static void MP_QueryPerformanceCounter(LARGE_INTEGER *lpCounter)
{
HANDLE hCurThread = GetCurrentThread();
unsigned long dwOldMask = SetThreadAffinityMask(hCurThread, 1);
QueryPerformanceCounter(lpCounter);
SetThreadAffinityMask(hCurThread, dwOldMask);
}
typedef struct _SCSI_PASS_THROUGH_DIRECT
{
USHORT Length;
UCHAR ScsiStatus;
UCHAR PathId;
UCHAR TargetId;
UCHAR Lun;
UCHAR CdbLength;
UCHAR SenseInfoLength;
UCHAR DataIn;
ULONG DataTransferLength;
ULONG TimeOutValue;
PVOID DataBuffer;
ULONG SenseInfoOffset;
UCHAR Cdb[16];
} SCSI_PASS_THROUGH_DIRECT, *PSCSI_PASS_THROUGH_DIRECT;
void sptd_exec(HANDLE handle, SCSI_PASS_THROUGH_DIRECT &sptd, CommandResult &rv)
{
LARGE_INTEGER PerfCountStart, PerfCountEnd;
static const double freq = windows_detail::init_qpc_freq();
DWORD dwBytesReturned;
windows_detail::MP_QueryPerformanceCounter(&PerfCountStart);
auto io_ok = DeviceIoControl(handle, IOCTL_SCSI_PASS_THROUGH_DIRECT, &sptd,
sizeof(sptd), &sptd, sizeof(sptd),
&dwBytesReturned, NULL);
windows_detail::MP_QueryPerformanceCounter(&PerfCountEnd);
if (io_ok && dwBytesReturned == sizeof(sptd))
{
rv.Valid = true;
rv.Duration = (PerfCountEnd.QuadPart - PerfCountStart.QuadPart) / freq;
rv.Data.resize(sptd.DataTransferLength);
rv.ScsiStatus = sptd.ScsiStatus;
}
else
{
rv.Valid = false;
}
}
template <std::size_t CDBLength>
SCSI_PASS_THROUGH_DIRECT
sptd_common(const std::array<std::uint8_t, CDBLength> &cdb)
{
auto sptd = SCSI_PASS_THROUGH_DIRECT{};
sptd.Length = sizeof(sptd);
sptd.CdbLength = CDBLength; // CDB size
sptd.TimeOutValue = 60; // SCSI timeout value
std::copy(std::begin(cdb), std::end(cdb), sptd.Cdb);
return sptd;
}
template <std::size_t CDBLength>
SCSI_PASS_THROUGH_DIRECT
sptd_for_read(CommandResult &rv, const std::array<std::uint8_t, CDBLength> &cdb)
{
auto sptd = sptd_common(cdb);
sptd.DataIn = SCSI_IOCTL_DATA_IN;
sptd.DataTransferLength = rv.Data.size();
sptd.DataBuffer = rv.Data.data();
return sptd;
}
template <std::size_t CDBLength>
SCSI_PASS_THROUGH_DIRECT
sptd_for_write(const std::vector<std::uint8_t> &data,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto sptd = sptd_common(cdb);
sptd.DataIn = SCSI_IOCTL_DATA_OUT;
sptd.DataTransferLength = static_cast<ULONG>(data.size());
sptd.DataBuffer = const_cast<std::uint8_t *>(data.data());
return sptd;
}
} // namespace windows_detail
struct platform_windows
{
using device_handle = HANDLE;
static std::uint32_t monotonic_clock() { return GetTickCount(); }
static device_handle open_volume(const char *DrivePath)
{
HANDLE hVolume;
UINT uDriveType;
char szVolumeName[8];
char szRootName[5];
DWORD dwAccessFlags;
char DriveLetter = DrivePath[0];
szRootName[0] = DriveLetter;
szRootName[1] = ':';
szRootName[2] = '\\';
szRootName[3] = '\0';
uDriveType = GetDriveType(szRootName);
switch (uDriveType)
{
case DRIVE_CDROM:
dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
break;
default:
printf("\nError: invalid drive type\n");
return INVALID_HANDLE_VALUE;
}
szVolumeName[0] = '\\';
szVolumeName[1] = '\\';
szVolumeName[2] = '.';
szVolumeName[3] = '\\';
szVolumeName[4] = DriveLetter;
szVolumeName[5] = ':';
szVolumeName[6] = '\0';
hVolume = CreateFile(szVolumeName, dwAccessFlags,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hVolume == INVALID_HANDLE_VALUE)
printf("\nError: invalid handle");
return hVolume;
}
static void close_handle(device_handle h) { CloseHandle(h); }
static bool handle_is_valid(device_handle h)
{
return h != INVALID_HANDLE_VALUE;
}
static void set_critical_priority()
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
}
static void set_normal_priority()
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
}
template <std::size_t CDBLength>
static void exec_command(device_handle handle, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb)
{
using namespace windows_detail;
auto sptd = sptd_for_read(rv, cdb);
sptd_exec(handle, sptd, rv);
}
template <std::size_t CDBLength>
static void send_data(device_handle handle, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb,
const std::vector<std::uint8_t> &data)
{
using namespace windows_detail;
auto sptd = sptd_for_write(data, cdb);
windows_detail::sptd_exec(handle, sptd, rv);
}
};
using platform = platform_windows;
#endif