forked from munjeni/xflasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1mode.c
executable file
·285 lines (229 loc) · 5.58 KB
/
s1mode.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
279
280
281
282
283
284
285
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>
#include <asm/byteorder.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <ctype.h>
#define TIMEOUT 5000
#define USBVID 0x0FCE
#define USBPID 0xADDE
#define EPIN 0x81
#define EPOUT 0x01
#define MAX_USBFS_BULK_SIZE 400000
static void display_buffer_hex(char *message, char *buffer, unsigned int size)
{
unsigned int i, j, k;
printf("%s[%d]:\n", message, (int)size);
for (i=0; i<size; i+=16) {
printf("\n %08X ", i);
for(j=0,k=0; k<16; j++,k++) {
if (i+j < size) {
printf("%02X", buffer[i+j] & 0xff);
} else {
printf(" ");
}
printf(" ");
}
printf(" ");
for(j=0,k=0; k<16; j++,k++) {
if (i+j < size) {
if ((buffer[i+j] < 32) || (buffer[i+j] > 126)) {
printf(".");
} else {
printf("%c", buffer[i+j]);
}
}
}
}
printf("\n\n" );
}
struct usb_handle
{
char fname[64];
int desc;
unsigned char ep_in;
unsigned char ep_out;
};
static inline int badname(const char *name)
{
while (*name) {
if (!isdigit(*name++))
return 1;
}
return 0;
}
static int get_vidpid(int fd)
{
struct usb_device_descriptor *dev;
//struct usb_interface_descriptor *ifc;
char desc[1024];
int n;
if ((n = read(fd, desc, sizeof(desc))) == 0)
return 0;
dev = (void *)desc;
//fprintf(stderr, "found vid: %04x\n", dev->idVendor);
//fprintf(stderr, "found pid: %04x\n", dev->idProduct);
if (dev->idVendor != USBVID || dev->idProduct != USBPID)
return 0;
return 1;
}
static unsigned long transfer_bulk_async(struct usb_handle *h, int ep, char *bytes, unsigned long size, int timeout, int exact)
{
unsigned long count = 0;
struct usbdevfs_bulktransfer bulk;
int n;
if (ep == EPIN)
{
if (h->ep_in == 0)
return 0;
while (size > 0)
{
int xfer = (size > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : size;
bulk.ep = h->ep_in;
bulk.len = xfer;
bulk.data = bytes;
bulk.timeout = timeout;
do
{
n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
if (n < 0) {
printf("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
return 0;
}
}
while(n < 0);
count += n;
size -= n;
if (n < xfer)
break;
}
}
if (ep == EPOUT)
{
if (h->ep_out == 0)
return 0;
if (size == 0) {
bulk.ep = h->ep_out;
bulk.len = 0;
bulk.data = bytes;
bulk.timeout = 0;
n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
if (n != 0) {
fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n", n, errno, strerror(errno));
return 0;
}
return 0;
}
while (size > 0) {
int xfer;
xfer = (size > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : size;
bulk.ep = h->ep_out;
bulk.len = xfer;
bulk.data = bytes;
bulk.timeout = timeout;
n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
if (n != xfer) {
printf("ERROR: n = %d, errno = %d (%s)\n", n, errno, strerror(errno));
return 0;
}
count += xfer;
size -= xfer;
}
}
if (exact) {
if (count != size) {
printf("Error read/write!!! Need nBytes: 0x%lx but done: 0x%lx\n", size, count);
return 0;
}
}
if (ep == EPIN) {
printf("Successfully read 0x%lx bytes from handle.\n", count);
display_buffer_hex("Raw input ", bytes, count);
}
if (ep == EPOUT) {
printf("Successfully write 0x%lx bytes to handle.\n", count);
//display_buffer_hex("Raw output ", bytes, count);
}
return count;
}
struct usb_handle *get_fastboot(void)
{
char busname[64], devname[64];
DIR *busdir, *devdir;
struct dirent *de;
int fd;
int found_usb = 0;
int n;
int ifc;
struct usb_handle *usb = NULL;
busdir = opendir("/dev/bus/usb");
//fprintf(stderr, "busdir: %p\n", busdir);
while ((de = readdir(busdir)) && (found_usb == 0)) {
//fprintf(stderr, "dirent: %p\n", de);
if (badname(de->d_name))
continue;
sprintf(busname, "%s/%s", "/dev/bus/usb", de->d_name);
//fprintf(stderr, "busname: %s\n", busname);
devdir = opendir(busname);
while ((de = readdir(devdir)) && (found_usb == 0)) {
if (badname(de->d_name))
continue;
sprintf(devname, "%s/%s", busname, de->d_name);
//fprintf(stderr, "devname: %s\n", devname);
if ((fd = open(devname, O_RDWR)) < 1) {
fprintf(stderr, "cannot open %s for writing\n", devname);
continue;
}
if (get_vidpid(fd)) {
fprintf(stderr, "found device with vid:%04x pid:%04x.\n", USBVID, USBPID);
usb = calloc(1, sizeof(struct usb_handle));
usb->ep_in = EPIN;
usb->ep_out = EPOUT;
usb->desc = fd;
ifc = 0;
if ((n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc)) != 0) {
fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n",
n, errno, strerror(errno));
closedir(devdir);
closedir(busdir);
return NULL;
}
found_usb = 1;
}
}
closedir(devdir);
}
closedir(busdir);
return usb;
}
int usb_close(struct usb_handle *h)
{
int fd;
fd = h->desc;
h->desc = -1;
if (fd >= 0) {
close(fd);
//printf("usb closed %d\n", fd);
}
return 0;
}
int main(void)
{
struct usb_handle *usb;
usb = get_fastboot();
if (usb == NULL) {
printf("\nNo usb device with vid:%04x pid:%04x !\n", USBVID, USBPID);
exit(1);
}
char test1[4096];
transfer_bulk_async(usb, EPIN, test1, sizeof(test1), TIMEOUT, 0);
usb_close(usb);
return 0;
}