2 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
3 * rtl_eeprom, EEPROM modification tool
4 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "getopt/getopt.h"
33 #define EEPROM_SIZE 256
34 #define MAX_STR_SIZE 256
35 #define STR_OFFSET 0x09
37 static rtlsdr_dev_t *dev = NULL;
39 typedef struct rtlsdr_config {
42 char manufacturer[MAX_STR_SIZE];
43 char product[MAX_STR_SIZE];
44 char serial[MAX_STR_SIZE];
50 void dump_config(rtlsdr_config_t *conf)
52 fprintf(stderr, "__________________________________________\n");
53 fprintf(stderr, "Vendor ID:\t\t0x%04x\n", conf->vendor_id);
54 fprintf(stderr, "Product ID:\t\t0x%04x\n", conf->product_id);
55 fprintf(stderr, "Manufacturer:\t\t%s\n", conf->manufacturer);
56 fprintf(stderr, "Product:\t\t%s\n", conf->product);
57 fprintf(stderr, "Serial number:\t\t%s\n", conf->serial);
58 fprintf(stderr, "Serial number enabled:\t");
59 fprintf(stderr, conf->have_serial ? "yes\n": "no\n");
60 fprintf(stderr, "IR endpoint enabled:\t");
61 fprintf(stderr, conf->enable_ir ? "yes\n": "no\n");
62 fprintf(stderr, "Remote wakeup enabled:\t");
63 fprintf(stderr, conf->remote_wakeup ? "yes\n": "no\n");
64 fprintf(stderr, "__________________________________________\n");
70 "rtl_eeprom, an EEPROM programming tool for "
71 "RTL2832 based DVB-T receivers\n\n"
73 "\t[-d device_index (default: 0)]\n"
74 "\t[-m <str> set manufacturer string]\n"
75 "\t[-p <str> set product string]\n"
76 "\t[-s <str> set serial number string]\n"
77 "\t[-i <0,1> disable/enable IR-endpoint]\n"
78 "\t[-g <conf> generate default config and write to device]\n"
79 "\t[ <conf> can be one of:]\n"
80 "\t[ realtek\t\tRealtek default (as without EEPROM)]\n"
81 "\t[ realtek_oem\t\tRealtek default OEM with EEPROM]\n"
82 "\t[ noxon\t\tTerratec NOXON DAB Stick]\n"
83 "\t[ terratec_black\tTerratec T Stick Black]\n"
84 "\t[ terratec_plus\tTerratec T Stick+ (DVB-T/DAB)]\n"
85 "\t[-w <filename> write dumped file to device]\n"
86 "\t[-r <filename> dump EEPROM to file]\n"
87 "\t[-h display this help text]\n"
88 "\nUse on your own risk, especially -w!\n");
92 int get_string_descriptor(int pos, uint8_t *data, char *str)
98 if (data[pos + 1] != 0x03)
99 fprintf(stderr, "Error: invalid string descriptor!\n");
101 for (i = 2; i < len; i += 2)
102 str[j++] = data[pos + i];
109 int set_string_descriptor(int pos, uint8_t *data, char *str)
116 data[pos + 1] = 0x03;
118 while (str[i] != 0x00) {
119 if ((pos + j) >= 78) {
120 fprintf(stderr, "Error: string too long, truncated!\n");
123 data[pos + j++] = str[i++];
124 data[pos + j++] = 0x00;
132 int parse_eeprom_to_conf(rtlsdr_config_t *conf, uint8_t *dat)
136 if ((dat[0] != 0x28) || (dat[1] != 0x32))
137 fprintf(stderr, "Error: invalid RTL2832 EEPROM header!\n");
139 conf->vendor_id = dat[2] | (dat[3] << 8);
140 conf->product_id = dat[4] | (dat[5] << 8);
141 conf->have_serial = (dat[6] == 0xa5) ? 1 : 0;
142 conf->remote_wakeup = (dat[7] & 0x01) ? 1 : 0;
143 conf->enable_ir = (dat[7] & 0x02) ? 1 : 0;
145 pos = get_string_descriptor(STR_OFFSET, dat, conf->manufacturer);
146 pos = get_string_descriptor(pos, dat, conf->product);
147 get_string_descriptor(pos, dat, conf->serial);
152 int gen_eeprom_from_conf(rtlsdr_config_t *conf, uint8_t *dat)
158 dat[2] = conf->vendor_id & 0xff;
159 dat[3] = (conf->vendor_id >> 8) & 0xff ;
160 dat[4] = conf->product_id & 0xff;
161 dat[5] = (conf->product_id >> 8) & 0xff;
162 dat[6] = conf->have_serial ? 0xa5 : 0x00;
164 dat[7] |= conf->remote_wakeup ? 0x01 : 0x00;
165 dat[7] |= conf->enable_ir ? 0x02 : 0x00;
168 pos = set_string_descriptor(STR_OFFSET, dat, conf->manufacturer);
169 pos = set_string_descriptor(pos, dat, conf->product);
170 pos = set_string_descriptor(pos, dat, conf->serial);
172 dat[78] = 0x00; /* length of IR config */
186 void gen_default_conf(rtlsdr_config_t *conf, int config)
190 fprintf(stderr, "Realtek default (as without EEPROM)\n");
191 conf->vendor_id = 0x0bda;
192 conf->product_id = 0x2832;
193 strcpy(conf->manufacturer, "Generic");
194 strcpy(conf->product, "RTL2832U DVB-T");
195 strcpy(conf->serial, "0");
196 conf->have_serial = 1;
198 conf->remote_wakeup = 1;
201 fprintf(stderr, "Realtek default OEM with EEPROM\n");
202 conf->vendor_id = 0x0bda;
203 conf->product_id = 0x2838;
204 strcpy(conf->manufacturer, "Realtek");
205 strcpy(conf->product, "RTL2838UHIDIR");
206 strcpy(conf->serial, "00000001");
207 conf->have_serial = 1;
209 conf->remote_wakeup = 0;
212 fprintf(stderr, "Terratec NOXON DAB Stick\n");
213 conf->vendor_id = 0x0ccd;
214 conf->product_id = 0x00b3;
215 strcpy(conf->manufacturer, "NOXON");
216 strcpy(conf->product, "DAB Stick");
217 strcpy(conf->serial, "0");
218 conf->have_serial = 1;
220 conf->remote_wakeup = 1;
222 case TERRATEC_T_BLACK:
223 fprintf(stderr, "Terratec T Stick Black\n");
224 conf->vendor_id = 0x0ccd;
225 conf->product_id = 0x00a9;
226 strcpy(conf->manufacturer, "Realtek");
227 strcpy(conf->product, "RTL2838UHIDIR");
228 strcpy(conf->serial, "00000001");
229 conf->have_serial = 1;
231 conf->remote_wakeup = 0;
233 case TERRATEC_T_PLUS:
234 fprintf(stderr, "Terratec ran T Stick+\n");
235 conf->vendor_id = 0x0ccd;
236 conf->product_id = 0x00d7;
237 strcpy(conf->manufacturer, "Realtek");
238 strcpy(conf->product, "RTL2838UHIDIR");
239 strcpy(conf->serial, "00000001");
240 conf->have_serial = 1;
242 conf->remote_wakeup = 0;
249 int main(int argc, char **argv)
252 uint32_t dev_index = 0;
254 char *filename = NULL;
256 char *manuf_str = NULL;
257 char *product_str = NULL;
258 char *serial_str = NULL;
259 uint8_t buf[EEPROM_SIZE];
260 rtlsdr_config_t conf;
262 int default_config = 0;
267 while ((opt = getopt(argc, argv, "d:m:p:s:i:g:w:r:h?")) != -1) {
270 dev_index = atoi(optarg);
277 product_str = optarg;
285 ir_endpoint = (atoi(optarg) > 0) ? 1 : -1;
289 if (!strcmp(optarg, "realtek"))
290 default_config = REALTEK;
291 else if (!strcmp(optarg, "realtek_oem"))
292 default_config = REALTEK_EEPROM;
293 else if (!strcmp(optarg, "noxon"))
294 default_config = TERRATEC_NOXON;
295 else if (!strcmp(optarg, "terratec_black"))
296 default_config = TERRATEC_T_BLACK;
297 else if (!strcmp(optarg, "terratec_plus"))
298 default_config = TERRATEC_T_PLUS;
300 if (default_config != CONF_NONE)
315 device_count = rtlsdr_get_device_count();
317 fprintf(stderr, "No supported devices found.\n");
321 fprintf(stderr, "Found %d device(s):\n", device_count);
322 for (i = 0; i < device_count; i++)
323 fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
324 fprintf(stderr, "\n");
326 fprintf(stderr, "Using device %d: %s\n",
328 rtlsdr_get_device_name(dev_index));
330 r = rtlsdr_open(&dev, dev_index);
332 fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
336 fprintf(stderr, "\n");
338 r = rtlsdr_read_eeprom(dev, buf, 0, EEPROM_SIZE);
341 fprintf(stderr, "No EEPROM has been found.\n");
343 fprintf(stderr, "Failed to read EEPROM, err %i.\n", r);
350 fprintf(stderr, "Current configuration:\n");
351 parse_eeprom_to_conf(&conf, buf);
355 file = fopen(filename, flash_file ? "rb" : "wb");
357 fprintf(stderr, "Error opening file!\n");
361 if (fread(buf, 1, sizeof(buf), file) != sizeof(buf))
362 fprintf(stderr, "Error reading file!\n");
364 if (fwrite(buf, 1, sizeof(buf), file) != sizeof(buf))
365 fprintf(stderr, "Short write, exiting!\n");
367 fprintf(stderr, "\nDump to %s successful.\n", filename);
372 strncpy((char*)&conf.manufacturer, manuf_str, MAX_STR_SIZE);
375 strncpy((char*)&conf.product, product_str, MAX_STR_SIZE);
378 conf.have_serial = 1;
379 strncpy((char*)&conf.serial, serial_str, MAX_STR_SIZE);
382 if (ir_endpoint != 0)
383 conf.enable_ir = (ir_endpoint > 0) ? 1 : 0;
388 fprintf(stderr, "\nNew configuration:\n");
390 if (default_config != CONF_NONE)
391 gen_default_conf(&conf, default_config);
394 if (gen_eeprom_from_conf(&conf, buf) < 0)
398 parse_eeprom_to_conf(&conf, buf);
401 fprintf(stderr, "Write new configuration to device [y/n]? ");
403 while ((ch = getchar())) {
410 r = rtlsdr_write_eeprom(dev, buf, 0, flash_file ? EEPROM_SIZE : 128);
412 fprintf(stderr, "Error while writing EEPROM: %i\n", r);
414 fprintf(stderr, "Configuration successfully written.\n");
422 return r >= 0 ? r : -r;