From b85ffcfea5d268f34b2de4f2119b30be8c699269 Mon Sep 17 00:00:00 2001 From: Steve Markgraf Date: Fri, 2 Nov 2012 21:47:14 +0100 Subject: [PATCH] add library support for EEPROM access and tool for modification rtl_eeprom can be used to modify the USB descriptor strings etc. Signed-off-by: Steve Markgraf --- include/rtl-sdr.h | 32 +++++ src/CMakeLists.txt | 9 +- src/Makefile.am | 5 +- src/librtlsdr.c | 65 +++++++++ src/rtl_eeprom.c | 348 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 457 insertions(+), 2 deletions(-) create mode 100644 src/rtl_eeprom.c diff --git a/include/rtl-sdr.h b/include/rtl-sdr.h index 157d2a5..6dca0d0 100644 --- a/include/rtl-sdr.h +++ b/include/rtl-sdr.h @@ -99,6 +99,38 @@ RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq, RTLSDR_API int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact, char *product, char *serial); +/*! + * Write the device EEPROM + * + * \param dev the device handle given by rtlsdr_open() + * \param data buffer of data to be written + * \param offset address where the data should be written + * \param len length of the data + * \return 0 on success + * \return -1 if device handle is invalid + * \return -2 if EEPROM size is exceeded + * \return -3 if no EEPROM was found + */ + +RTLSDR_API int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data, + uint8_t offset, uint16_t len); + +/*! + * Read the device EEPROM + * + * \param dev the device handle given by rtlsdr_open() + * \param data buffer where the data should be written + * \param offset address where the data should be read from + * \param len length of the data + * \return 0 on success + * \return -1 if device handle is invalid + * \return -2 if EEPROM size is exceeded + * \return -3 if no EEPROM was found + */ + +RTLSDR_API int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data, + uint8_t offset, uint16_t len); + RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq); /*! diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c490e5b..bfdaaf0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,7 +70,8 @@ add_executable(rtl_sdr rtl_sdr.c) add_executable(rtl_tcp rtl_tcp.c) add_executable(rtl_test rtl_test.c) add_executable(rtl_fm rtl_fm.c) -set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm) +add_executable(rtl_eeprom rtl_eeprom.c) +set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom) target_link_libraries(rtl_sdr rtlsdr_shared ${LIBUSB_LIBRARIES} @@ -88,6 +89,10 @@ target_link_libraries(rtl_fm rtlsdr_shared ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) +target_link_libraries(rtl_eeprom rtlsdr_shared + ${LIBUSB_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} +) if(UNIX) target_link_libraries(rtl_fm m) if(APPLE) @@ -102,10 +107,12 @@ target_link_libraries(rtl_sdr libgetopt_static) target_link_libraries(rtl_tcp libgetopt_static) target_link_libraries(rtl_test libgetopt_static) target_link_libraries(rtl_fm libgetopt_static) +target_link_libraries(rtl_eeprom libgetopt_static) set_property(TARGET rtl_sdr APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_tcp APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_test APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_fm APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) +set_property(TARGET rtl_eeprom APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) endif() ######################################################################## # Install built library files & utilities diff --git a/src/Makefile.am b/src/Makefile.am index 4e2aa6d..f1162f5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ lib_LTLIBRARIES = librtlsdr.la librtlsdr_la_SOURCES = librtlsdr.c tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r820t.c librtlsdr_la_LDFLAGS = -version-info $(LIBVERSION) -bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm +bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_sdr_SOURCES = rtl_sdr.c rtl_sdr_LDADD = librtlsdr.la @@ -23,3 +23,6 @@ rtl_test_LDADD = librtlsdr.la $(LIBM) rtl_fm_SOURCES = rtl_fm.c rtl_fm_LDADD = librtlsdr.la $(LIBM) + +rtl_eeprom_SOURCES = rtl_eeprom.c +rtl_eeprom_LDADD = librtlsdr.la $(LIBM) diff --git a/src/librtlsdr.c b/src/librtlsdr.c index 0328e9e..1af9f87 100644 --- a/src/librtlsdr.c +++ b/src/librtlsdr.c @@ -271,6 +271,8 @@ static rtlsdr_dongle_t known_devices[] = { #define CTRL_TIMEOUT 300 #define BULK_TIMEOUT 0 +#define EEPROM_ADDR 0xa0 + enum usb_reg { USB_SYSCTL = 0x2000, USB_CTRL = 0x2010, @@ -709,6 +711,69 @@ int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact, char *product, return 0; } +int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16_t len) +{ + int r = 0; + int i; + uint8_t cmd[2]; + + if (!dev) + return -1; + + if ((len + offset) > 256) + return -2; + + for (i = 0; i < len; i++) { + cmd[0] = i + offset; + r = rtlsdr_write_array(dev, IICB, EEPROM_ADDR, cmd, 1); + r = rtlsdr_read_array(dev, IICB, EEPROM_ADDR, &cmd[1], 1); + + /* only write the byte if it differs */ + if (cmd[1] == data[i]) + continue; + + cmd[1] = data[i]; + r = rtlsdr_write_array(dev, IICB, EEPROM_ADDR, cmd, 2); + if (r != sizeof(cmd)) + return -3; + + /* for some EEPROMs (e.g. ATC 240LC02) we need a delay + * between write operations, otherwise they will fail */ +#ifdef _WIN32 + Sleep(5); +#else + usleep(5000); +#endif + } + + return 0; +} + +int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16_t len) +{ + int r = 0; + int i; + + if (!dev) + return -1; + + if ((len + offset) > 256) + return -2; + + r = rtlsdr_write_array(dev, IICB, EEPROM_ADDR, &offset, 1); + if (r < 0) + return -3; + + for (i = 0; i < len; i++) { + r = rtlsdr_read_array(dev, IICB, EEPROM_ADDR, data + i, 1); + + if (r < 0) + return -3; + } + + return r; +} + int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq) { int r = -1; diff --git a/src/rtl_eeprom.c b/src/rtl_eeprom.c new file mode 100644 index 0000000..fe4d36a --- /dev/null +++ b/src/rtl_eeprom.c @@ -0,0 +1,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 + * + * 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 . + */ + +#include +#include +#include + +#ifndef _WIN32 +#include +#else +#include +#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 set manufacturer string\n" + "\t[-p set product string\n" + "\t[-s set serial number string\n" + "\t[-i <0,1> disable/enable IR-endpoint\n" + "\t[-g generate default config and write to device\n" + "\t[-w write dumped file to device\n" + "\t[-r 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; +}