-
Notifications
You must be signed in to change notification settings - Fork 7
/
asix_eepromtool.c
417 lines (364 loc) · 10.3 KB
/
asix_eepromtool.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
asix_eepromtool
eeprom programming tool for ASIX-based USB ethernet interfaces
Extended by Albin Söderqvist <[email protected]>
- UNLICENSE -
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <sys/types.h>
#include <libusb.h>
#include <strings.h>
#include <endian.h>
#include <getopt.h>
#define ASIX_CMD_READ_EEPROM 0x0b
#define ASIX_CMD_WRITE_EEPROM 0x0c
#define ASIX_CMD_WRITE_EEPROM_EN 0x0d
#define ASIX_CMD_WRITE_EEPROM_DIS 0x0e
static libusb_device *dev, **devs;
static libusb_device_handle *device = NULL;
static int open_device(int bus, int devnum, unsigned int vid, unsigned int pid)
{
int i = 0;
int last_bus = 0;
int last_device = 0;
int status = libusb_init(NULL);
ssize_t cnt;
struct libusb_device_descriptor desc;
unsigned char serial_number[100];
if (status < 0) {
fprintf(stderr, "failed to initialize libusb");
return status;
}
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000106)
libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, 0);
#else
libusb_set_debug(NULL, 0);
#endif
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
libusb_exit(NULL);
return (int) cnt;
}
if (bus && devnum) {
/* Find the last bus matching vid and pid */
if (bus < 0) {
while ((dev = devs[i++]) != NULL) {
status = libusb_get_device_descriptor(dev, &desc);
if (status < 0) {
fprintf(stderr, "failed to get device descriptor");
return status;
}
if (desc.idVendor == vid
&& desc.idProduct == pid
&& libusb_get_bus_number(dev) > last_bus) {
last_bus = libusb_get_bus_number(dev);
}
}
if (last_bus > 0) {
bus = last_bus;
i = 0;
} else {
fprintf(stderr, "failed to get a valid address on bus %d", bus);
return -1;
}
}
/* Find the last device on the bus matching vid and pid */
if (devnum < 0) {
while ((dev = devs[i++]) != NULL) {
status = libusb_get_device_descriptor(dev, &desc);
if (status < 0) {
fprintf(stderr, "failed to get device descriptor");
return status;
}
if (desc.idVendor == vid
&& desc.idProduct == pid
&& libusb_get_bus_number(dev) == bus
&& libusb_get_device_address(dev) > last_device) {
last_device = libusb_get_device_address(dev);
}
}
if (last_device > 0) {
devnum = last_device;
i = 0;
} else {
fprintf(stderr, "failed to get a valid address on bus %d", bus);
return -1;
}
}
/* Find the device matching vid, pid, bus and device number */
while ((dev = devs[i++]) != NULL) {
status = libusb_get_device_descriptor(dev, &desc);
if (status < 0) {
fprintf(stderr, "failed to get device descriptor");
return status;
}
if (desc.idVendor == vid
&& desc.idProduct == pid
&& libusb_get_bus_number(dev) == bus
&& libusb_get_device_address(dev) == devnum) {
printf("Accessing bus %d, device %d, vid:pid %04x:%04x",
libusb_get_bus_number(dev), libusb_get_device_address(dev),
desc.idVendor, desc.idProduct);
break;
}
}
if (dev == NULL || libusb_get_device_address(dev) != devnum) {
fprintf(stderr, "Could not find USB device %d\n", devnum);
exit(1);
}
status = libusb_open(dev, &device);
if (status) {
printf("\nlibusb_open() failed. Is device connected? Are you root?\n");
exit(1);
}
} else {
device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid);
if (device == NULL) {
printf("libusb_open() failed. Is device connected? Are you root?\n");
exit(1);
}
}
status = libusb_get_string_descriptor_ascii(device, desc.iSerialNumber, serial_number, 100);
if (status > 0) {
printf(", serial number %s\n", serial_number);
} else {
printf("\n");
}
libusb_detach_kernel_driver(device, 0);
status = libusb_claim_interface(device, 0);
if (status != LIBUSB_SUCCESS) {
libusb_close(device);
printf("libusb_claim_interface failed: %s\n", libusb_error_name(status));
exit(1);
}
return status;
}
static int asix_read(uint8_t cmd, uint16_t value, uint16_t index, uint16_t size, void *data)
{
return libusb_control_transfer(device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
cmd,
value,
index,
data,
size,
100);
}
static int asix_write(uint8_t cmd, uint16_t value, uint16_t index, uint16_t size, void *data)
{
return libusb_control_transfer(device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
cmd,
value,
index,
data,
size,
100);
}
int read_eeprom(int rsize, uint16_t* buf)
{
int i, retval = 0;
uint16_t tmp16;
for (i = 0; i < rsize/2; i++) {
retval = asix_read(ASIX_CMD_READ_EEPROM, i, 0, 2, &tmp16);
if (retval < 0) {
return retval;
}
*(buf + i) = be16toh(tmp16);
}
return retval;
}
int write_eeprom(int wsize, uint16_t* buf)
{
int i, retval;
retval = asix_write(ASIX_CMD_WRITE_EEPROM_EN, 0, 0, 0, NULL);
if (retval < 0) {
return retval;
}
sleep(1);
for (i = 0; i < wsize/2; i++) {
retval = asix_write(ASIX_CMD_WRITE_EEPROM, i, htobe16(*(buf + i)), 0, NULL);
if (retval < 0) {
return retval;
}
usleep(50000);
}
retval = asix_write(ASIX_CMD_WRITE_EEPROM_DIS, 0, 0, 0, NULL);
if (retval < 0) {
return retval;
}
return retval;
}
void print_header()
{
printf("------------------------------------\n");
printf(" asix_eepromtool\n");
printf("------------------------------------\n");
}
void print_usage()
{
print_header();
printf("options:\n");
printf("--device=<vid:pid> -d <vid:pid> = vid and pid of device in hex eg. 0b95:772b\n");
printf("--bus=<bus> -b <bus> = (optional) bus number, e.g. 2 or -1 to select the last\n");
printf("--device-number=<device> -n <n> = (optional) device number, e.g. 4 or -1 to select the last\n");
printf("--read=<file> , -r <file> = save the eeprom contents to <file>\n");
printf("--write=<file> , -w <file> = write <file> to eeprom\n");
printf("--size=<# of bytes> , -s <# of bytes> = size of eeprom in bytes (e.g. 256 or 512)\n");
printf("\n");
printf("example:\n");
printf("asix_eepromtool -d 0b95:772b -r eep.bin -s 256\n");
printf("asix_eepromtool -d 0b95:772b -b 2 -n 10 -r eep.bin -s 256\n");
printf("\n");
printf("ps. Run this tool as root\n");
}
int main(int argc, char **argv)
{
int status;
uint8_t eepbuf[65536];
int c;
int devnum = 0;
int bus = 0;
uint16_t vid = 0;
uint16_t pid = 0;
uint16_t eepsize = 0;
char *tmp;
FILE *readFile = NULL;
FILE *writeFile = NULL;
if (argc == 1) {
print_usage();
exit(1);
}
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"bus", required_argument, 0, 'b'},
{"device-number", required_argument, 0, 'n'},
{"read", required_argument, 0, 'r'},
{"write", required_argument, 0, 'w'},
{"size", required_argument, 0, 's'},
{0, 0, 0, 0}
};
while (1) {
int option_index = 0;
c = getopt_long (argc, argv, "d:b:n:r:w:s:",
long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 0:
if (long_options[option_index].flag != 0) {
break;
}
case 'b':
bus = atoi(optarg);
if (bus < -1) {
print_usage();
exit(1);
}
break;
case 'n':
devnum = atoi(optarg);
if (devnum < -1) {
print_usage();
exit(1);
}
break;
case 'd':
tmp = strtok(optarg,":");
vid = strtoul(tmp,NULL,16);
tmp = strtok(NULL,":");
if (tmp != NULL) {
pid=strtoul(tmp,NULL,16);
}
break;
case 'r':
readFile = fopen(optarg,"wb");
break;
case 'w':
writeFile = fopen(optarg,"rb");
break;
case 's':
eepsize = strtoul(optarg,NULL,10);
break;
case '?':
print_usage();
exit(0);
break;
default:
exit(1);
}
}
print_header();
if ((vid == 0) | (pid == 0)) {
printf("Device VID:PID missing or wrong format\n");
exit(1);
}
if (eepsize == 0) {
printf("EEPROM size not specified\n");
exit(1);
}
if ((readFile == NULL) && (writeFile == NULL)) {
printf("Read/write filename not specified or file open error\n");
exit(1);
}
if (eepsize %2 != 0) {
printf("EEPROM size must be a multiple of 2\n");
exit(1);
}
printf("Device is %04X:%04X\n", vid, pid);
printf("EEPROM is %d bytes\n", eepsize);
if (bus && devnum) {
printf("Bus %d, device %d\n", bus, devnum);
}
status = open_device(bus, devnum, vid, pid);
if (status != 0) {
printf("Device open error %d\n",status);
exit(1);
} else {
printf("Device opened\n");
}
if (readFile != NULL) {
printf("Reading...\n");
status = read_eeprom(eepsize, (uint16_t*)&eepbuf);
fwrite(&eepbuf, 1, eepsize, readFile);
fclose(readFile);
}
if (writeFile != NULL) {
printf("Writing...\n");
fread(&eepbuf, 1, eepsize, writeFile);
status = write_eeprom(eepsize, (uint16_t*)&eepbuf);
fclose(writeFile);
}
if (status < 0) {
printf("Error %d\n", status);
} else {
printf("Done.\n");
}
}