-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
278 lines (226 loc) · 9.68 KB
/
main.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* hantek.sys
*
* Copyright 2022 Daniel Kucera
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "ddk/wdm.h"
#include "wine/debug.h"
#include "winioctl.h"
#include <stdint.h>
#include <libusb-1.0/libusb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <stdlib.h>
#define VENDOR_REQUEST 0x40
#define DEVICE_TO_HOST 0x80
#define DEFAULT_PORT 8484
int sock = 0, client_fd;
WINE_DEFAULT_DEBUG_CHANNEL(hantek);
libusb_device_handle *usbdev;
struct __attribute__((__packed__)) hantekCommand {
uint32_t io_code;
uint16_t input_len;
uint16_t output_len;
};
struct __attribute__((__packed__)) hantekResponse {
uint8_t ret_val;
uint16_t input_len;
uint16_t output_len;
};
static NTSTATUS WINAPI hantek_ioctl_usb( DEVICE_OBJECT *device, IRP *irp )
{
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
int r;
int requestTimeout = 5000;
TRACE( "ioctl %x insize %u outsize %u\n",
irpsp->Parameters.DeviceIoControl.IoControlCode,
irpsp->Parameters.DeviceIoControl.InputBufferLength,
irpsp->Parameters.DeviceIoControl.OutputBufferLength );
TRACE("outdata %s\n", wine_dbgstr_an(irp->UserBuffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength));
TRACE("indata %s\n", wine_dbgstr_an(irp->AssociatedIrp.SystemBuffer, irpsp->Parameters.DeviceIoControl.InputBufferLength));
uint8_t* indata = irp->AssociatedIrp.SystemBuffer;
int transferSize;
switch(irpsp->Parameters.DeviceIoControl.IoControlCode) {
case 0x222059:
TRACE( "control transfer\n" );
// libusb_control_transfer(libusb_device_handle *dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout)
uint8_t bmRequestType = VENDOR_REQUEST + DEVICE_TO_HOST * (indata[0] == 1);
uint8_t bRequest = indata[4];
uint16_t wValue = indata[6] + indata[7] * 0x100;
uint16_t wIndex = indata[8] + indata[9] * 0x100;
if (indata[0]) {
r = libusb_control_transfer(usbdev, bmRequestType, bRequest, wValue, wIndex, (unsigned char*)irp->UserBuffer, (uint16_t)irpsp->Parameters.DeviceIoControl.OutputBufferLength, requestTimeout);
TRACE("control read reqlen %d readlen %d %s\n", irpsp->Parameters.DeviceIoControl.OutputBufferLength, r, wine_dbgstr_an(irp->UserBuffer, r));
if (r < 0) {
irp->IoStatus.Information = 0;
} else {
irp->IoStatus.Information = r;
}
irp->IoStatus.Status = STATUS_SUCCESS;
} else {
r = libusb_control_transfer(usbdev, bmRequestType, bRequest, wValue, wIndex, (unsigned char*)irp->UserBuffer, (uint16_t)irpsp->Parameters.DeviceIoControl.OutputBufferLength, requestTimeout);
irp->IoStatus.Information = 0;
irp->IoStatus.Status = STATUS_SUCCESS;
}
break;
case 0x222051:
TRACE( "write bulk\n" );
r = libusb_bulk_transfer(usbdev, 0x02, (unsigned char*)irp->UserBuffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength, &transferSize, requestTimeout);
TRACE("write bulk reqlen %d writelen %d\n", irpsp->Parameters.DeviceIoControl.OutputBufferLength, transferSize);
irp->IoStatus.Information = 0;
irp->IoStatus.Status = STATUS_SUCCESS;
break;
case 0x22204e:
TRACE( "read bulk\n" );
r = libusb_bulk_transfer(usbdev, 0x86, (unsigned char*)irp->UserBuffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength, &transferSize, requestTimeout);
TRACE("read bulk reqlen %d readlen %d %s\n", irpsp->Parameters.DeviceIoControl.OutputBufferLength, transferSize, wine_dbgstr_an(irp->UserBuffer, r));
irp->IoStatus.Information = transferSize;
irp->IoStatus.Status = STATUS_SUCCESS;
break;
default:
TRACE( "------ unhandled ioctl --------\n" );
}
TRACE( "------ IOCTL COMPLETE --------\n" );
IoCompleteRequest( irp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
static NTSTATUS WINAPI hantek_ioctl_tcp( DEVICE_OBJECT *device, IRP *irp )
{
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
struct hantekCommand cmd;
struct hantekResponse res;
int valread;
TRACE( "ioctl %x insize %u outsize %u\n",
irpsp->Parameters.DeviceIoControl.IoControlCode,
irpsp->Parameters.DeviceIoControl.InputBufferLength,
irpsp->Parameters.DeviceIoControl.OutputBufferLength );
TRACE("outdata %s\n", wine_dbgstr_an(irp->UserBuffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength));
TRACE("indata %s\n", wine_dbgstr_an(irp->AssociatedIrp.SystemBuffer, irpsp->Parameters.DeviceIoControl.InputBufferLength));
cmd.io_code = irpsp->Parameters.DeviceIoControl.IoControlCode;
cmd.input_len = irpsp->Parameters.DeviceIoControl.InputBufferLength;
cmd.output_len = irpsp->Parameters.DeviceIoControl.OutputBufferLength;
send(sock, &cmd, sizeof(cmd), 0);
send(sock, irp->AssociatedIrp.SystemBuffer, cmd.input_len, 0);
send(sock, irp->UserBuffer, cmd.output_len, 0);
valread = recv(sock, &res, sizeof(res), MSG_WAITALL);
TRACE("res len: %d ret: %d in_len: %d, out_len:%d \n", valread, res.ret_val, res.input_len, res.output_len);
if(valread != sizeof(res)){
TRACE("short header, len %d\n", valread);
return STATUS_PIPE_NOT_AVAILABLE;
}
if (res.input_len) {
valread = recv(sock, irp->AssociatedIrp.SystemBuffer, res.input_len, MSG_WAITALL);
if(valread != res.input_len){
TRACE("short in_data\n");
return STATUS_PIPE_NOT_AVAILABLE;
}
}
if (res.output_len) {
valread = recv(sock, (unsigned char*)irp->UserBuffer, res.output_len, MSG_WAITALL);
if(valread != res.output_len){
TRACE("short out_data len: %d %s\n", valread, strerror(errno));
return STATUS_PIPE_NOT_AVAILABLE;
}
}
irp->IoStatus.Information = res.output_len + res.input_len;
irp->IoStatus.Status = res.ret_val;
TRACE( "------ IOCTL COMPLETE --------\n" );
IoCompleteRequest( irp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
int hantek_connect(DRIVER_OBJECT *driver) {
struct sockaddr_in serv_addr;
int hantek_port = DEFAULT_PORT;
const char* hantek_port_var = getenv("HANTEK_PORT");
const char* hantek_host_var = getenv("HANTEK_HOST");
if(hantek_port_var) {
hantek_port = atoi(hantek_port_var);
}
if(!hantek_host_var) {
const struct libusb_version* version;
int r;
version = libusb_get_version();
TRACE("Using libusb v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
r = libusb_init(NULL);
if (r < 0) {
TRACE("libusb_init failed with code %d\n", r);
return STATUS_INVALID_PARAMETER;
}
usbdev = libusb_open_device_with_vid_pid(NULL, 0x04b5, 0x6cde);
if (usbdev == NULL) {
TRACE("hantek device not found\n");
return STATUS_DEVICE_DOES_NOT_EXIST;
}
driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = hantek_ioctl_usb;
return 0;
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
ERR("Socket creation error");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(hantek_port);
ERR("Hantek connecting to %s:%d\n", hantek_host_var, hantek_port);
if (inet_pton(AF_INET, hantek_host_var, &serv_addr.sin_addr) <= 0) {
ERR("Invalid address/ Address not supported %s\n", hantek_host_var);
return -1;
}
if ((client_fd = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) {
ERR("Connection Failed\n");
return -1;
}
int flag = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = hantek_ioctl_tcp;
return 0;
}
NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
{
NTSTATUS status;
UNICODE_STRING nameW, dos_nameW;
DEVICE_OBJECT *device;
// d6CDE-0
static const WCHAR hantek_deviceW[] = {'\\','D','e','v','i','c','e','\\','d','6','C','D','E','-','0',0};
static const WCHAR hantek_dos_deviceW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','d','6','C','D','E','-','0',0};
if (hantek_connect(driver)){
return STATUS_DEVICE_DOES_NOT_EXIST;
}
TRACE( "(%p, %s) hantek\n", driver, debugstr_w(path->Buffer) );
TRACE( "create device hantek (%s) dos device (%s)\n", debugstr_w(hantek_deviceW), debugstr_w(hantek_dos_deviceW));
RtlInitUnicodeString( &nameW, hantek_deviceW );
RtlInitUnicodeString( &dos_nameW, hantek_dos_deviceW );
//status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device );
status = IoCreateDevice( driver, 0, &nameW, FILE_DEVICE_DISK, 0, FALSE, &device );
if (status) {
FIXME( "failed to create device %x\n", status );
return status;
}
status = IoCreateSymbolicLink( &dos_nameW, &nameW );
if (status) {
FIXME( "failed to create symlink %x\n", status );
return status;
}
return STATUS_SUCCESS;
}