-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom-tinker.c
231 lines (200 loc) · 6.84 KB
/
eeprom-tinker.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
/*//////////////////////////////////////////////////////////////////////////////
* WARNING: THIS PROGRAM IS DANGEROUS AND CAN CAUSE HARM TO YOUR SYSTEM!!!!!
* Ok, you've been warned...it's not that dangerous as long as you use it safely
* and wisely. Always double check you are passing the correct i2c bus.
*
* This source borrows source from the following kernel files:
* - drivers/media/usb/em28xx/em28xx-i2c.c
* - drivers/media/common/tveeprom.c
*
*
* Copyright: 2019 Brad Love (Next Dimension Innovations) [email protected]
* SPDX-License-Identifier: GPL-3.0-or-later
*//////////////////////////////////////////////////////////////////////////////
#define _GNU_SOURCE
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "device-detection.h"
#include "eeprom-utils.h"
/********************************************************************************************************/
void device_info(struct em28xx_eeprom *tst_eeprom, struct tveeprom *eeprom_tv)
{
unsigned short vendor_ID = (tst_eeprom->vendor_ID[1] << 8) |
tst_eeprom->vendor_ID[0];
unsigned short product_ID = (tst_eeprom->product_ID[1] << 8) |
tst_eeprom->product_ID[0];
printf("\n");
printf("vid:pid %04x:%04x\n", vendor_ID, product_ID);
printf("Board Config: %02x (%s)\n", tst_eeprom->BoardConfigEx,
(tst_eeprom->BoardConfigEx & BOARD_CFG2_BULK_TS) ? "bulk" : "isoc");
printf("serial: %02x %02x %02x\n",
(eeprom_tv->serial_number >> 16) & 0xff,
(eeprom_tv->serial_number >> 8) & 0xff,
eeprom_tv->serial_number && 0xff);
printf("model: %02x %02x %02x %02x ( %d )\n",
(eeprom_tv->model >> 24) & 0xff, (eeprom_tv->model >> 16) & 0xff,
(eeprom_tv->model >> 8) & 0xff, eeprom_tv->model & 0xff, eeprom_tv->model);
printf("revision: %02x %02x %02x\n", (eeprom_tv->revision >> 16) & 0xff,
(eeprom_tv->revision >> 8) & 0xff, eeprom_tv->revision & 0xff);
}
/********************************************************************************************************/
void print_help()
{
printf("eeprom-tinker options\n");
printf("\t-d\tdevice detection\n");
printf("\t-i\tprint device info\n");
printf("\t-v\tverbose\n");
printf("\t-t\ttest mode, only output equivalent modification operation\n");
printf("\t-b\tset i2c bus number (dangerous! be sure this is correct!)\n");
printf("\t-m\t{pid|bulk|both} fix product id, fix bulk flag, or fix both\n");
printf("\t-h\tdisplay help\n");
}
/********************************************************************************************************/
int main (int argc, char **argv)
{
unsigned char eeprom_data[256] = { 0 };
struct em28xx_eeprom *tst_eeprom = (struct em28xx_eeprom*)&eeprom_data[0];
struct tveeprom eeprom_tv = { 0 };
char i2c_bus_string[128] = { 0 };
int hwconf_offset = 0, retval = 0, i2c_ret = 0;
int vflag = 0, iflag = 0, c = 0, mflag = 0, bulk_flag = 0, modded_flag = 0;
int pid_flag = 0, testonly_flag = 0, detect_flag = 0, num_found = 0;
unsigned long i2c_bus_no = ULONG_MAX;
unsigned char pid_offset = 0, boardconf_offset = 0;
printf("EEPROM Tinker\n\tWelcome to the danger zone\n");
printf("\tBe careful, this utility can harm your system if misused!\n\n");
if (argc == 1) {
print_help();
return 1;
}
while ((c = getopt (argc, argv, "vithdm:b:")) != -1) {
switch (c)
{
case 'v':
vflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'b':
i2c_bus_no = strtoul(optarg, NULL, 10);
sprintf(&i2c_bus_string[0], "/dev/i2c-%lu", i2c_bus_no);
if (access(i2c_bus_string, F_OK) != 0) {
printf("Invalid i2c bus number %lu\n", i2c_bus_no);
return 1;
}
break;
case 'm':
if (strncasecmp(optarg, "pid", 3) == 0)
pid_flag = 1;
else if (strncasecmp(optarg, "bulk", 4) == 0)
bulk_flag = 1;
else if (strncasecmp(optarg, "both", 4) == 0) {
pid_flag = 1;
bulk_flag = 1;
} else {
printf("Bad argument for -m\n\n");
print_help();
return 1;
}
mflag = 1;
break;
case 't':
testonly_flag = 1;
break;
case 'h':
print_help();
return 1;
case 'd':
detect_flag = 1;
break;
case '?':
if (optopt == 'b')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (!isprint (optopt))
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort();
}
}
if (detect_flag) {
num_found = em28xx_device_detect();
printf("=========================================\n");
printf("%d em28xx devices found\n", num_found);
printf("=========================================\n");
return 0;
}
if (i2c_bus_no == ULONG_MAX) {
fprintf (stderr, "Option -b is required, quitting\n");
return 1;
}
hwconf_offset = eeprom_check(vflag, i2c_bus_no, &eeprom_data[0]);
/**** Comment the line below to allow the eeprom-tinker to update the eeprom ****/
testonly_flag = 1;
/********************************************************************************/
if (hwconf_offset > 0) {
if (eeprom_decode(&eeprom_data[0], &eeprom_tv) != 0) {
printf("Eeprom decode failure, quitting...\n");
return -1;
}
eeprom_tv.byte_offset_start = hwconf_offset;
eeprom_tv.i2c_bus_no = i2c_bus_no;
if (iflag)
device_info(tst_eeprom, &eeprom_tv);
retval = eeprom_validate(0, &eeprom_data[0], &eeprom_tv);
if (mflag && retval > 0) {
if (bulk_flag && (retval & BULK_CONVERSION_POSSIBLE)) {
boardconf_offset = offsetof(struct em28xx_eeprom, BoardConfigEx);
printf("############################################\n");
printf("Update board config to bulk mode%s", testonly_flag ? "\n" : ": ");
i2c_ret = eeprom_update(testonly_flag, eeprom_tv.i2c_bus_no,
eeprom_tv.byte_offset_start + boardconf_offset,
tst_eeprom->BoardConfigEx);
if (!testonly_flag) {
if (!i2c_ret)
printf("OK\n");
else {
printf("ERROR\n");
printf("############################################\n");
return 1;
}
}
modded_flag = 1;
}
usleep(50 * 1000);
if (pid_flag && (retval & PID_MODIFICATION_POSSIBLE)) {
pid_offset = offsetof(struct em28xx_eeprom, product_ID[1]);
printf("############################################\n");
printf("Update Product ID%s", testonly_flag ? "\n" : ": ");
i2c_ret = eeprom_update(testonly_flag, eeprom_tv.i2c_bus_no,
eeprom_tv.byte_offset_start + pid_offset,
tst_eeprom->product_ID[1]);
if (!testonly_flag) {
if (!i2c_ret)
printf("OK\n");
else {
printf("ERROR\n");
printf("############################################\n");
return 1;
}
}
modded_flag = 1;
}
printf("############################################\n");
if (testonly_flag && modded_flag)
printf("\nOnly execute the above commands if you are absolutely confident\n");
}
} else {
return 1;
}
printf("\n");
return 0;
}