-
Notifications
You must be signed in to change notification settings - Fork 0
/
hgcm.c
186 lines (171 loc) · 5.41 KB
/
hgcm.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include "hgcm.h"
void die(char *msg) {
perror(msg);
exit(-1);
}
//device fd
int fd = -1;
int hgcm_connect(const char *service_name) {
if (fd == -1) {
fd = open("/dev/vboxuser",O_RDWR);
if (fd == -1) {
die("open device error");
}
}
VBGLIOCHGCMCONNECT data = {
.Hdr.cbIn = sizeof(VBGLIOCHGCMCONNECT),
.Hdr.uVersion = VBGLREQHDR_VERSION,
.Hdr.uType = VBGLREQHDR_TYPE_DEFAULT,
.Hdr.rc = VERR_INTERNAL_ERROR,
.Hdr.cbOut = sizeof(VBGLREQHDR) + sizeof(uint32_t),
.Hdr.uReserved = 0,
.u.In.Loc.type = VMMDevHGCMLoc_LocalHost_Existing
};
memset(data.u.In.Loc.u.host.achName,0,128);
strncpy(data.u.In.Loc.u.host.achName,service_name,128);
ioctl(fd,VBGL_IOCTL_HGCM_CONNECT,&data);
if (data.Hdr.rc) { //error
return -1;
}
return data.u.Out.idClient;
}
HGCMFunctionParameter64 arg_buf[0x100];
int hgcm_call(int client_id,int func,char *params_fmt,...) {
va_list ap;
char *p,*bval,*type;
uint32_t ival;
uint64_t lval;
HGCMFunctionParameter64 params;
uint16_t index = 0;
va_start(ap,params_fmt);
for(p = params_fmt;*p;p++) {
if(*p!='%') {
continue;
}
switch (*++p) {
case 'u': //整数类型
ival = va_arg(ap,uint32_t);
params.type = VMMDevHGCMParmType_32bit;
params.u.value64 = 0;
params.u.value32 = ival;
arg_buf[index++] = params;
break;
case 'l':
lval = va_arg(ap,uint64_t);
params.type = VMMDevHGCMParmType_64bit;
params.u.value64 = lval;
arg_buf[index++] = params;
case 'b': //buffer类型
type = va_arg(ap,char *);
bval = va_arg(ap,char *);
ival = va_arg(ap,uint32_t);
if (!strcmp(type,"in")) {
params.type = VMMDevHGCMParmType_LinAddr_In;
} else if (!strcmp(type,"out")) {
params.type = VMMDevHGCMParmType_LinAddr_Out;
} else {
params.type = VMMDevHGCMParmType_LinAddr;
}
params.u.Pointer.size = ival;
params.u.Pointer.u.linearAddr = (uintptr_t)bval;
arg_buf[index++] = params;
break;
}
}
va_end(ap);
//printf("params count=%d\n",index);
uint8_t *data_buf = (uint8_t *)malloc(sizeof(VBGLIOCHGCMCALL) + sizeof(HGCMFunctionParameter64)*index);
VBGLIOCHGCMCALL data = {
.Hdr.cbIn = sizeof(VBGLIOCHGCMCALL) + sizeof(HGCMFunctionParameter64)*index,
.Hdr.uVersion = VBGLREQHDR_VERSION,
.Hdr.uType = VBGLREQHDR_TYPE_DEFAULT,
.Hdr.rc = VERR_INTERNAL_ERROR,
.Hdr.cbOut = sizeof(VBGLIOCHGCMCALL) + sizeof(HGCMFunctionParameter64)*index,
.Hdr.uReserved = 0,
.u32ClientID = client_id,
.u32Function = func,
.cMsTimeout = 100000, //忽略
.fInterruptible = 0,
.bReserved = 0,
.cParms = index
};
memcpy(data_buf,&data,sizeof(VBGLIOCHGCMCALL));
memcpy(data_buf+sizeof(VBGLIOCHGCMCALL),arg_buf,sizeof(HGCMFunctionParameter64)*index);
/*printf("=============before=====================\n");
unsigned char *d = (unsigned char *)data_buf;
for (int i=0;i<0x100;i++) {
printf("%02x ",d[i]);
}
printf("\n");
printf("=============after=====================\n");*/
ioctl(fd,VBGL_IOCTL_CODE_SIZE(IOCTL_HGCM_CALL,sizeof(VBGLIOCHGCMCALL) + sizeof(HGCMFunctionParameter64)*index),data_buf);
int error = ((VBGLIOCHGCMCALL *)data_buf)->Hdr.rc;
HGCMFunctionParameter64 *ans = (HGCMFunctionParameter64 *)(data_buf + sizeof(VBGLIOCHGCMCALL));
/*d = (unsigned char *)data_buf;
for (int i=0;i<0x100;i++) {
printf("%02x ",d[i]);
}
printf("\n");*/
for (int i=0;i<index;i++) {
HGCMFunctionParameter64 e = ans[i];
switch (e.type) {
case VMMDevHGCMParmType_32bit:
ans_buf[i] = e.u.value32;
break;
case VMMDevHGCMParmType_64bit:
ans_buf[i] = e.u.value64;
break;
case VMMDevHGCMParmType_PhysAddr:
case VMMDevHGCMParmType_LinAddr_Out:
ans_buf[i] = e.u.Pointer.u.linearAddr;
break;
}
}
free(data_buf);
if (error) { //error
return error;
}
/*for (int i=0;i<sizeof(VBGLIOCHGCMCALL)+sizeof(HGCMFunctionParameter64)*index;i++) {
printf("%02x",data_buf[i]);
}
printf("\n");*/
return 0;
}
int hgcm_disconnect(int client_id) {
VBGLIOCHGCMDISCONNECT data = {
.Hdr.cbIn = sizeof(VBGLIOCHGCMDISCONNECT),
.Hdr.uVersion = VBGLREQHDR_VERSION,
.Hdr.uType = VBGLREQHDR_TYPE_DEFAULT,
.Hdr.rc = VERR_INTERNAL_ERROR,
.Hdr.cbOut = sizeof(VBGLREQHDR),
.Hdr.uReserved = 0,
.u.In.idClient = client_id,
};
ioctl(fd,VBGL_IOCTL_HGCM_DISCONNECT,&data);
if (data.Hdr.rc) { //error
return -1;
}
return 0;
}
/*int main() {
//打开设备
/*fd = open("/dev/vboxuser",O_RDWR);
if (fd == -1) {
die("open device error");
}*/
/*int idClient = hgcm_connect("VBoxGuestPropSvc");
printf("idClient=%d\n",idClient);
char ans[0x100] = {0};
int ret = hgcm_call(idClient,2,"%b%b","in","foo",4,"in","bar",4);
ret = hgcm_call(idClient,1,"%b%b%u%u","in","foo",4,"out",ans,0x100,0,0);
printf("%s\n",ans);
printf("%d\n",hgcm_disconnect(idClient));
}*/