Acurite minor bugfixes
[rtl-433.git] / src / rtl_eeprom.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #ifndef _WIN32
25 #include <unistd.h>
26 #else
27 #include <Windows.h>
28 #include "getopt/getopt.h"
29 #endif
30
31 #include "rtl-sdr.h"
32
33 #define EEPROM_SIZE     256
34 #define MAX_STR_SIZE    256
35 #define STR_OFFSET      0x09
36
37 static rtlsdr_dev_t *dev = NULL;
38
39 typedef struct rtlsdr_config {
40         uint16_t vendor_id;
41         uint16_t product_id;
42         char manufacturer[MAX_STR_SIZE];
43         char product[MAX_STR_SIZE];
44         char serial[MAX_STR_SIZE];
45         int have_serial;
46         int enable_ir;
47         int remote_wakeup;
48 } rtlsdr_config_t;
49
50 void dump_config(rtlsdr_config_t *conf)
51 {
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");
65 }
66
67 void usage(void)
68 {
69         fprintf(stderr,
70                 "rtl_eeprom, an EEPROM programming tool for "
71                 "RTL2832 based DVB-T receivers\n\n"
72                 "Usage:\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");
89         exit(1);
90 }
91
92 int get_string_descriptor(int pos, uint8_t *data, char *str)
93 {
94         int len, i, j = 0;
95
96         len = data[pos];
97
98         if (data[pos + 1] != 0x03)
99                 fprintf(stderr, "Error: invalid string descriptor!\n");
100
101         for (i = 2; i < len; i += 2)
102                 str[j++] = data[pos + i];
103
104         str[j] = 0x00;
105
106         return pos + i;
107 }
108
109 int set_string_descriptor(int pos, uint8_t *data, char *str)
110 {
111         int i = 0, j = 2;
112
113         if (pos < 0)
114                 return -1;
115
116         data[pos + 1] = 0x03;
117
118         while (str[i] != 0x00) {
119                 if ((pos + j) >= 78) {
120                         fprintf(stderr, "Error: string too long, truncated!\n");
121                         return -1;
122                 }
123                 data[pos + j++] = str[i++];
124                 data[pos + j++] = 0x00;
125         }
126
127         data[pos] = j;
128
129         return pos + j;
130 }
131
132 int parse_eeprom_to_conf(rtlsdr_config_t *conf, uint8_t *dat)
133 {
134         int pos;
135
136         if ((dat[0] != 0x28) || (dat[1] != 0x32))
137                 fprintf(stderr, "Error: invalid RTL2832 EEPROM header!\n");
138
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;
144
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);
148
149         return 0;
150 }
151
152 int gen_eeprom_from_conf(rtlsdr_config_t *conf, uint8_t *dat)
153 {
154         int pos;
155
156         dat[0] = 0x28;
157         dat[1] = 0x32;
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;
163         dat[7] = 0x14;
164         dat[7] |= conf->remote_wakeup ? 0x01 : 0x00;
165         dat[7] |= conf->enable_ir ? 0x02 : 0x00;
166         dat[8] = 0x02;
167
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);
171
172         dat[78] = 0x00;         /* length of IR config */
173
174         return pos;
175 }
176
177 enum configs {
178         CONF_NONE = 0,
179         REALTEK,
180         REALTEK_EEPROM,
181         TERRATEC_NOXON,
182         TERRATEC_T_BLACK,
183         TERRATEC_T_PLUS,
184 };
185
186 void gen_default_conf(rtlsdr_config_t *conf, int config)
187 {
188         switch (config) {
189         case REALTEK:
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;
197                 conf->enable_ir = 0;
198                 conf->remote_wakeup = 1;
199                 break;
200         case REALTEK_EEPROM:
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;
208                 conf->enable_ir = 1;
209                 conf->remote_wakeup = 0;
210                 break;
211         case TERRATEC_NOXON:
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;
219                 conf->enable_ir = 0;
220                 conf->remote_wakeup = 1;
221                 break;
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;
230                 conf->enable_ir = 1;
231                 conf->remote_wakeup = 0;
232                 break;
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;
241                 conf->enable_ir = 1;
242                 conf->remote_wakeup = 0;
243                 break;
244         default:
245                 break;
246         };
247 }
248
249 int main(int argc, char **argv)
250 {
251         int i, r, opt;
252         uint32_t dev_index = 0;
253         int device_count;
254         char *filename = NULL;
255         FILE *file = 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;
261         int flash_file = 0;
262         int default_config = 0;
263         int change = 0;
264         int ir_endpoint = 0;
265         char ch;
266
267         while ((opt = getopt(argc, argv, "d:m:p:s:i:g:w:r:h?")) != -1) {
268                 switch (opt) {
269                 case 'd':
270                         dev_index = atoi(optarg);
271                         break;
272                 case 'm':
273                         manuf_str = optarg;
274                         change = 1;
275                         break;
276                 case 'p':
277                         product_str = optarg;
278                         change = 1;
279                         break;
280                 case 's':
281                         serial_str = optarg;
282                         change = 1;
283                         break;
284                 case 'i':
285                         ir_endpoint = (atoi(optarg) > 0) ? 1 : -1;
286                         change = 1;
287                         break;
288                 case 'g':
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;
299
300                         if (default_config != CONF_NONE)
301                                 change = 1;
302                         break;
303                 case 'w':
304                         flash_file = 1;
305                         change = 1;
306                 case 'r':
307                         filename = optarg;
308                         break;
309                 default:
310                         usage();
311                         break;
312                 }
313         }
314
315         device_count = rtlsdr_get_device_count();
316         if (!device_count) {
317                 fprintf(stderr, "No supported devices found.\n");
318                 exit(1);
319         }
320
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");
325
326         fprintf(stderr, "Using device %d: %s\n",
327                 dev_index,
328                 rtlsdr_get_device_name(dev_index));
329
330         r = rtlsdr_open(&dev, dev_index);
331         if (r < 0) {
332                 fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
333                 exit(1);
334         }
335
336         fprintf(stderr, "\n");
337
338         r = rtlsdr_read_eeprom(dev, buf, 0, EEPROM_SIZE);
339         if (r < 0) {
340                 if (r == -3)
341                         fprintf(stderr, "No EEPROM has been found.\n");
342                 else
343                         fprintf(stderr, "Failed to read EEPROM, err %i.\n", r);
344                 goto exit;
345         }
346
347         if (r < 0)
348                 return -1;
349
350         fprintf(stderr, "Current configuration:\n");
351         parse_eeprom_to_conf(&conf, buf);
352         dump_config(&conf);
353
354         if (filename) {
355                 file = fopen(filename, flash_file ? "rb" : "wb");
356                 if (!file) {
357                         fprintf(stderr, "Error opening file!\n");
358                         goto exit;
359                 }
360                 if (flash_file) {
361                         if (fread(buf, 1, sizeof(buf), file) != sizeof(buf))
362                                 fprintf(stderr, "Error reading file!\n");
363                 } else {
364                         if (fwrite(buf, 1, sizeof(buf), file) != sizeof(buf))
365                                 fprintf(stderr, "Short write, exiting!\n");
366                         else
367                                 fprintf(stderr, "\nDump to %s successful.\n", filename);
368                 }
369         }
370
371         if (manuf_str)
372                 strncpy((char*)&conf.manufacturer, manuf_str, MAX_STR_SIZE);
373
374         if (product_str)
375                 strncpy((char*)&conf.product, product_str, MAX_STR_SIZE);
376
377         if (serial_str) {
378                 conf.have_serial = 1;
379                 strncpy((char*)&conf.serial, serial_str, MAX_STR_SIZE);
380         }
381
382         if (ir_endpoint != 0)
383                  conf.enable_ir = (ir_endpoint > 0) ? 1 : 0;
384
385         if (!change)
386                 goto exit;
387
388         fprintf(stderr, "\nNew configuration:\n");
389
390         if (default_config != CONF_NONE)
391                 gen_default_conf(&conf, default_config);
392
393         if (!flash_file) {
394                 if (gen_eeprom_from_conf(&conf, buf) < 0)
395                         goto exit;
396         }
397
398         parse_eeprom_to_conf(&conf, buf);
399         dump_config(&conf);
400
401         fprintf(stderr, "Write new configuration to device [y/n]? ");
402
403         while ((ch = getchar())) {
404                 if (ch != 'y')
405                         goto exit;
406                 else
407                         break;
408         }
409
410         r = rtlsdr_write_eeprom(dev, buf, 0, flash_file ? EEPROM_SIZE : 128);
411         if (r < 0)
412                 fprintf(stderr, "Error while writing EEPROM: %i\n", r);
413         else
414                 fprintf(stderr, "Configuration successfully written.\n");
415
416 exit:
417         if (file)
418                 fclose(file);
419
420         rtlsdr_close(dev);
421
422         return r >= 0 ? r : -r;
423 }