-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathioctl_u.c
227 lines (214 loc) · 5.52 KB
/
ioctl_u.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
/* ioctl - toolbox
Copyright 2015-2021 Rivoreo
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program 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 General Public License for more details.
*/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
#include <sys/ioctl.h>
#else
#include <windows.h>
#ifdef _WINDOWSNT_NATIVE
#include <nt.h>
#else
#include <winioctl.h>
#endif
#ifndef O_SYNC
#define O_SYNC 0x101000
#endif
#endif
#if defined __INTERIX && !defined strtoull
extern unsigned long long int strtouq(const char *, char **, int);
#define strtoull strtouq
#endif
int ioctl_main(int argc, char *argv[]) {
int fd;
int res;
int read_only = 0;
int verbose = 0;
int length = -1;
int arg_size = 4;
int is_string = 0;
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
int direct_arg = 0;
#endif
uint32_t ioctl_nr;
void *ioctl_args;
uint8_t *ioctl_argp;
uint8_t *ioctl_argp_save;
int rem;
int no_close = 0;
while(1) {
int c = getopt(argc, argv, "rvdl:a:sh");
if(c == -1) break;
switch(c) {
case 'r':
read_only = 1;
break;
case 'v':
verbose = 1;
break;
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
case 'd':
direct_arg = 1;
break;
#endif
case 'l':
length = strtol(optarg, NULL, 0);
break;
case 'a':
if(is_string) {
fprintf(stderr, "%s: Option '-a' is conflict with '-s'\n", argv[0]);
return -1;
}
arg_size = strtoul(optarg, NULL, 0);
if(arg_size < 1 || arg_size > 8) {
fprintf(stderr, "%s: Invalid argument to '-a', invalid range\n", argv[0]);
return -1;
}
break;
case 's':
is_string = 1;
arg_size = sizeof(char *);
break;
case 'h':
fprintf(stderr, "Usage: %s [-l <length>] [-a <argsize> | -s] [-rv"
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
"d"
#endif
"h] <device> <ioctlnr> [<arg>] [...]\n"
" -l <length> Length of I/O buffer\n"
" -a <argsize> Size of each argument (1-8)\n"
" -s Argments are pointers to C string\n"
" -r Open device in read only mode\n"
" -v Be verbose\n"
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
" -d Direct argument (no iobuffer)\n"
#endif
" -h Print help\n", argv[0]);
return -1;
case '?':
//fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt);
return 1;
}
}
if(optind + 2 > argc) {
fprintf(stderr, "%s: too few arguments\n", argv[0]);
return 1;
}
if(strcmp(argv[optind], "-") == 0) {
fd = STDIN_FILENO;
no_close = 1;
} else fd = open(argv[optind], (read_only ? O_RDWR : O_RDONLY) | O_SYNC);
if(fd == -1) {
fprintf(stderr, "%s: Cannot open %s, %s\n", argv[0], argv[optind], strerror(errno));
return 1;
}
optind++;
ioctl_nr = strtoul(argv[optind], NULL, 0);
optind++;
#if !defined _WIN32 && !defined _WINDOWSNT_NATIVE
if(direct_arg) {
arg_size = 4;
length = 4;
}
#endif
if(length < 0) {
length = (argc - optind) * arg_size;
}
if(length) {
ioctl_args = calloc(1, length);
ioctl_argp_save = ioctl_argp = ioctl_args;
rem = length;
while(optind < argc) {
union {
uint8_t a1;
uint16_t a2;
uint32_t a4;
uint64_t a8;
char *ap;
} tmp;
if(rem < arg_size) {
fprintf(stderr, "%s: too many arguments\n", argv[0]);
//return 1;
goto failed;
}
if(is_string) tmp.ap = argv[optind];
else switch(arg_size) {
case 1:
tmp.a1 = strtoul(argv[optind], NULL, 0);
break;
case 2:
tmp.a2 = strtoul(argv[optind], NULL, 0);
break;
case 3 ... 4:
tmp.a4 = strtoul(argv[optind], NULL, 0);
break;
case 5 ... 8:
tmp.a1 = strtoull(argv[optind], NULL, 0);
break;
}
memcpy(ioctl_argp, &tmp, arg_size);
ioctl_argp += arg_size;
rem -= arg_size;
optind++;
}
}
if(verbose) {
fprintf(stderr, "sending ioctl 0x%x", (unsigned int)ioctl_nr);
rem = length;
while(rem--) fprintf(stderr, " 0x%02x", *ioctl_argp_save++);
fputc('\n', stderr);
}
#if defined _WIN32 || defined _WINDOWSNT_NATIVE
//int ioctl(int fd, int request, ...) {
int ioctl(int fd, int request, void *buffer) {
#ifdef _WINDOWSNT_NATIVE
IO_STATUS_BLOCK io_status;
long int status = NtDeviceIoControlFile((void *)fd, NULL, NULL, NULL, &io_status, request, buffer, length, buffer, length);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
length = io_status.Information;
#else
unsigned long int rsize;
if(!DeviceIoControl((void *)fd, request, buffer, length, buffer, length, &rsize, NULL)) return -1;
length = rsize;
#endif
return 0;
}
#else
if(direct_arg) res = ioctl(fd, ioctl_nr, *(uint32_t *)ioctl_args);
else
#endif
res = ioctl(fd, ioctl_nr, length ? ioctl_args : 0);
if(res < 0) {
fprintf(stderr, "ioctl 0x%x failed, %s\n", (unsigned int)ioctl_nr, strerror(errno));
//return 1;
goto failed;
}
if(!no_close) close(fd);
printf("%d\n", res);
if(length) {
printf("return buf:");
ioctl_argp = ioctl_args;
rem = length;
while(rem--) {
printf(" %02x", *ioctl_argp++);
}
putchar('\n');
}
if(length) free(ioctl_args);
return 0;
failed:
if(!no_close) close(fd);
if(length) free(ioctl_args);
return 1;
}