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
|
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* rtl_eeprom, EEPROM modification tool
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <Windows.h>
#include "getopt/getopt.h"
#endif
#include "rtl-sdr.h"
#define EEPROM_SIZE 256
#define MAX_STR_SIZE 256
#define STR_OFFSET 0x09
static rtlsdr_dev_t *dev = NULL;
typedef struct rtlsdr_config {
uint16_t vendor_id;
uint16_t product_id;
char manufacturer[MAX_STR_SIZE];
char product[MAX_STR_SIZE];
char serial[MAX_STR_SIZE];
int have_serial;
int enable_ir;
int remote_wakeup;
} rtlsdr_config_t;
void dump_config(rtlsdr_config_t *conf)
{
fprintf(stderr, "__________________________________________\n");
fprintf(stderr, "Vendor ID:\t\t0x%04x\n", conf->vendor_id);
fprintf(stderr, "Product ID:\t\t0x%04x\n", conf->product_id);
fprintf(stderr, "Manufacturer:\t\t%s\n", conf->manufacturer);
fprintf(stderr, "Product:\t\t%s\n", conf->product);
fprintf(stderr, "Serial number:\t\t%s\n", conf->serial);
fprintf(stderr, "Serial number enabled:\t");
fprintf(stderr, conf->have_serial ? "yes\n": "no\n");
fprintf(stderr, "IR endpoint enabled:\t");
fprintf(stderr, conf->enable_ir ? "yes\n": "no\n");
fprintf(stderr, "Remote wakeup enabled:\t");
fprintf(stderr, conf->remote_wakeup ? "yes\n": "no\n");
fprintf(stderr, "__________________________________________\n");
}
void usage(void)
{
fprintf(stderr,
"rtl_eeprom, an EEPROM programming tool for "
"RTL2832 based DVB-T receivers\n\n"
"Usage:\n"
"\t[-d device_index (default: 0)]\n"
"\t[-m <str> set manufacturer string\n"
"\t[-p <str> set product string\n"
"\t[-s <str> set serial number string\n"
"\t[-i <0,1> disable/enable IR-endpoint\n"
"\t[-g <str> generate default config and write to device\n"
"\t[-w <filename> write dumped file to device\n"
"\t[-r <filename> dump EEPROM to file\n"
"\t[-h display this help text\n"
"\nUse on your own risk, especially -w!\n");
exit(1);
}
int get_string_descriptor(int pos, uint8_t *data, char *str)
{
int len, i, j = 0;
len = data[pos];
if (data[pos + 1] != 0x03)
fprintf(stderr, "Error: invalid string descriptor!\n");
for(i = 0; i < (len - 2); i += 2)
str[j++] = data[pos + 2 + i];
str[j] = 0x00;
return pos + i + 2;
}
int set_string_descriptor(int pos, uint8_t *data, char *str)
{
int i = 0, j = 2;
if (pos < 0)
return -1;
data[pos + 1] = 0x03;
while (str[i] != 0x00) {
if ((pos + j) >= 78) {
fprintf(stderr, "Error: string too long, truncated!\n");
return -1;
}
data[pos + j++] = str[i++];
data[pos + j++] = 0x00;
}
data[pos] = j;
return pos + j;
}
int parse_eeprom_to_conf(rtlsdr_config_t *conf, uint8_t *dat)
{
int pos;
if ((dat[0] != 0x28) || (dat[1] != 0x32))
fprintf(stderr, "Error: invalid RTL2832 EEPROM header!\n");
conf->vendor_id = dat[2] | (dat[3] << 8);
conf->product_id = dat[4] | (dat[5] << 8);
conf->have_serial = (dat[6] == 0xa5) ? 1 : 0;
conf->remote_wakeup = (dat[7] & 0x01) ? 1 : 0;
conf->enable_ir = (dat[7] & 0x02) ? 1 : 0;
pos = get_string_descriptor(STR_OFFSET, dat, conf->manufacturer);
pos = get_string_descriptor(pos, dat, conf->product);
get_string_descriptor(pos, dat, conf->serial);
return 0;
}
int gen_eeprom_from_conf(rtlsdr_config_t *conf, uint8_t *dat)
{
int pos;
dat[0] = 0x28;
dat[1] = 0x32;
dat[2] = conf->vendor_id & 0xff;
dat[3] = (conf->vendor_id >> 8) & 0xff ;
dat[4] = conf->product_id & 0xff;
dat[5] = (conf->product_id >> 8) & 0xff;
dat[6] = conf->have_serial ? 0xa5 : 0x00;
dat[7] = 0x14;
dat[7] |= conf->remote_wakeup ? 0x01 : 0x00;
dat[7] |= conf->enable_ir ? 0x02 : 0x00;
dat[8] = 0x02;
pos = set_string_descriptor(STR_OFFSET, dat, conf->manufacturer);
pos = set_string_descriptor(pos, dat, conf->product);
pos = set_string_descriptor(pos, dat, conf->serial);
dat[78] = 0x00; /* length of IR config */
return pos;
}
void gen_default_conf(rtlsdr_config_t *conf)
{
conf->vendor_id = 0x0bda;
conf->product_id = 0x2838;
strcpy(conf->manufacturer, "Realtek");
strcpy(conf->product, "RTL2838UHIDIR");
strcpy(conf->serial, "00000001");
conf->have_serial = 1;
conf->enable_ir = 1;
conf->remote_wakeup = 0;
}
int main(int argc, char **argv)
{
int i, r, opt, pos;
uint32_t dev_index = 0;
int device_count;
char *filename = NULL;
FILE *file = NULL;
uint16_t idVendor, idProduct;
char *manuf_str = NULL;
char *product_str = NULL;
char *serial_str = NULL;
uint8_t buf[EEPROM_SIZE];
rtlsdr_config_t conf;
int flash_file = 0;
int default_config = 0;
int change = 0;
int ir_endpoint = 0;
char ch;
while ((opt = getopt(argc, argv, "d:m:p:s:i:gw:r:h?")) != -1) {
switch (opt) {
case 'd':
dev_index = atoi(optarg);
break;
case 'm':
manuf_str = optarg;
change = 1;
break;
case 'p':
product_str = optarg;
change = 1;
break;
case 's':
serial_str = optarg;
change = 1;
break;
case 'i':
ir_endpoint = (atoi(optarg) > 0) ? 1 : -1;
change = 1;
break;
case 'g':
default_config = 1;
change = 1;
break;
case 'w':
flash_file = 1;
change = 1;
case 'r':
filename = optarg;
break;
default:
usage();
break;
}
}
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
exit(1);
}
fprintf(stderr, "Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++)
fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n",
dev_index,
rtlsdr_get_device_name(dev_index));
r = rtlsdr_open(&dev, dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1);
}
fprintf(stderr, "\n");
r = rtlsdr_read_eeprom(dev, buf, 0, EEPROM_SIZE);
if (r < 0) {
if (r == -3)
fprintf(stderr, "No EEPROM has been found.\n");
else
fprintf(stderr, "Failed to read EEPROM, err %i.\n", r);
goto exit;
}
if (r < 0)
return -1;
fprintf(stderr, "Current configuration:\n");
parse_eeprom_to_conf(&conf, buf);
dump_config(&conf);
if (filename) {
file = fopen(filename, flash_file ? "rb" : "wb");
if (!file) {
fprintf(stderr, "Error opening file!\n");
goto exit;
}
if (flash_file) {
if (fread(buf, 1, sizeof(buf), file) != sizeof(buf))
fprintf(stderr, "Error reading file!\n");
} else {
if (fwrite(buf, 1, sizeof(buf), file) != sizeof(buf))
fprintf(stderr, "Short write, exiting!\n");
else
fprintf(stderr, "\nDump to %s successful.\n", filename);
}
}
if (manuf_str)
strncpy((char*)&conf.manufacturer, manuf_str, MAX_STR_SIZE);
if (product_str)
strncpy((char*)&conf.product, product_str, MAX_STR_SIZE);
if (serial_str) {
conf.have_serial = 1;
strncpy((char*)&conf.serial, serial_str, MAX_STR_SIZE);
}
if (ir_endpoint != 0)
conf.enable_ir = (ir_endpoint > 0) ? 1 : 0;
if (!change)
goto exit;
fprintf(stderr, "\nNew configuration:\n");
if (default_config)
gen_default_conf(&conf);
if (!flash_file) {
if (gen_eeprom_from_conf(&conf, buf) < 0)
goto exit;
}
parse_eeprom_to_conf(&conf, buf);
dump_config(&conf);
fprintf(stderr, "Write new configuration to device [y/n]? ");
while ((ch = getchar())) {
if (ch != 'y')
goto exit;
else
break;
}
r = rtlsdr_write_eeprom(dev, buf, 0, flash_file ? EEPROM_SIZE : 128);
if (r < 0)
fprintf(stderr, "Error while writing EEPROM: %i\n", r);
else
fprintf(stderr, "Configuration successfully written.\n");
exit:
if (file)
fclose(file);
rtlsdr_close(dev);
out:
return r >= 0 ? r : -r;
}
|