Adapted to new decode size parameter, disable debug output
[ftdi_eeprom] / src / main.c
1 /***************************************************************************
2                              main.c  -  description
3                            -------------------
4     begin                : Mon Apr  7 12:05:22 CEST 2003
5     copyright            : (C) 2003,2008 by Intra2net AG
6     email                : opensource@intra2net.com
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License version 2 as     *
13  *   published by the Free Software Foundation.                            *
14  *                                                                         *
15  ***************************************************************************/
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <confuse.h>
26 #include <ftdi.h>
27
28 int main(int argc, char *argv[]) {
29     /*
30         configuration options
31     */
32     cfg_opt_t opts[] = {
33         CFG_INT("vendor_id", 0, 0),
34         CFG_INT("product_id", 0, 0),
35         CFG_BOOL("self_powered", cfg_true, 0),
36         CFG_BOOL("remote_wakeup", cfg_true, 0),
37         CFG_BOOL("BM_type_chip", cfg_true, 0),
38         CFG_BOOL("in_is_isochronous", cfg_false, 0),
39         CFG_BOOL("out_is_isochronous", cfg_false, 0),
40         CFG_BOOL("suspend_pull_downs", cfg_false, 0),
41         CFG_BOOL("use_serial", cfg_false, 0),
42         CFG_BOOL("change_usb_version", cfg_false, 0),
43         CFG_INT("usb_version", 0, 0),
44         CFG_INT("max_power", 0, 0),
45         CFG_STR("manufacturer", "Acme Inc.", 0),
46         CFG_STR("product", "USB Serial Converter", 0),
47         CFG_STR("serial", "08-15", 0),
48         CFG_STR("filename", "", 0),
49         CFG_BOOL("flash_raw", cfg_false, 0),
50         CFG_END()
51     };
52     cfg_t *cfg;
53     
54     /*
55         normal variables
56     */ 
57     int _read = 0, _erase = 0, _flash = 0;
58     unsigned char eeprom_buf[128];
59     char *filename;
60     int size_check;
61     int i, argc_filename;
62     FILE *fp;
63
64     struct ftdi_context ftdi;
65     struct ftdi_eeprom eeprom;
66
67     printf("\nFTDI eeprom generator v%s\n", VERSION);
68     printf ("(c) Intra2net AG <opensource@intra2net.com>\n");
69     
70     if (argc != 2 && argc != 3) {
71         printf("Syntax: %s [commands] config-file\n", argv[0]);
72         printf("Valid commands:\n");
73         printf("--read-eeprom           Read eeprom and write to -filename- from config-file\n");
74         printf("--erase-eeprom          Erase eeprom\n");
75         printf("--flash-eeprom          Flash eeprom\n");
76         exit (-1);
77     }
78     
79     if (argc == 3) {
80         if (strcmp(argv[1], "--read-eeprom") == 0)
81             _read = 1;
82         if (strcmp(argv[1], "--erase-eeprom") == 0)
83             _erase = 1;
84         if (strcmp(argv[1], "--flash-eeprom") == 0)
85             _flash = 1;
86         
87         argc_filename = 2;
88     } else {
89         argc_filename = 1;
90     }
91
92     if ((fp = fopen(argv[argc_filename], "r")) == NULL) {
93         printf ("Can't open configuration file\n");
94         exit (-1);
95     }
96     fclose (fp);
97
98     cfg = cfg_init(opts, 0);
99     cfg_parse(cfg, argv[argc_filename]);
100     filename = cfg_getstr(cfg, "filename");
101
102     if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
103         printf("Hint: Self powered devices should have a max_power setting of 0.\n");
104
105     ftdi_init(&ftdi);
106     ftdi_eeprom_initdefaults (&eeprom);
107     eeprom.vendor_id = cfg_getint(cfg, "vendor_id");
108     eeprom.product_id = cfg_getint(cfg, "product_id");
109     eeprom.BM_type_chip = cfg_getbool(cfg, "BM_type_chip");
110
111     eeprom.self_powered = cfg_getbool(cfg, "self_powered");
112     eeprom.remote_wakeup = cfg_getbool(cfg, "remote_wakeup");
113     eeprom.max_power = cfg_getint(cfg, "max_power");
114
115     eeprom.in_is_isochronous = cfg_getbool(cfg, "in_is_isochronous");
116     eeprom.out_is_isochronous = cfg_getbool(cfg, "out_is_isochronous");
117     eeprom.suspend_pull_downs = cfg_getbool(cfg, "suspend_pull_downs");
118
119     eeprom.use_serial = cfg_getbool(cfg, "use_serial");
120     eeprom.change_usb_version = cfg_getbool(cfg, "change_usb_version");
121     eeprom.usb_version = cfg_getint(cfg, "usb_version");
122
123
124     eeprom.manufacturer = cfg_getstr(cfg, "manufacturer");
125     eeprom.product = cfg_getstr(cfg, "product");
126     eeprom.serial = cfg_getstr(cfg, "serial");
127
128
129     if (_read > 0 || _erase > 0 || _flash > 0) {
130         i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id);
131         
132         if (i == 0) {
133           printf("EEPROM size: %d\n", ftdi.eeprom_size);
134         }
135         else {
136             printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id);
137             printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi));
138             printf("Retrying with default FTDI id.\n");
139
140             i = ftdi_usb_open(&ftdi, 0x0403, 0x6001);
141             if (i != 0) {
142                 printf("Error: %s\n", ftdi.error_str);
143                 exit (-1);
144             }
145         }
146     }
147
148     if (_read > 0) {
149       printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char *)eeprom_buf));
150
151         ftdi_eeprom_decode(&eeprom, eeprom_buf, ftdi.eeprom_size);
152         /* Debug output */
153         /*
154         printf("vendor_id = \"%04x\"\n", eeprom.vendor_id);
155         printf("product_id = \"%04x\"\n", eeprom.product_id);
156         printf("BM_type_chip = \"%s\"\n", eeprom.BM_type_chip?"true":"false");
157         printf("self_powered = \"%s\"\n", eeprom.self_powered?"true":"false");
158         printf("remote_wakeup = \"%s\"\n", eeprom.remote_wakeup?"true":"false");
159         printf("max_power = \"%d\"\n", eeprom.max_power);
160         printf("in_is_isochronous = \"%s\"\n", eeprom.in_is_isochronous?"true":"false");
161         printf("out_is_isochronous = \"%s\"\n", eeprom.out_is_isochronous?"true":"false");
162         printf("suspend_pull_downs = \"%s\"\n", eeprom.suspend_pull_downs?"true":"false");
163         printf("use_serial = \"%s\"\n", eeprom.use_serial?"true":"false");
164         printf("change_usb_version = \"%s\"\n", eeprom.change_usb_version?"true":"false");
165         printf("usb_version = \"%d\"\n", eeprom.usb_version);
166         printf("manufacturer = \"%s\"\n", eeprom.manufacturer);
167         printf("product = \"%s\"\n", eeprom.product);
168         printf("serial = \"%s\"\n", eeprom.serial);
169         */
170
171         if (filename != NULL && strlen(filename) > 0) {
172             FILE *fp = fopen (filename, "wb");
173             fwrite (eeprom_buf, 1, 128, fp);
174             fclose (fp);
175         } else {
176             printf("Warning: Not writing eeprom, you must supply a valid filename\n");
177         }
178
179         goto cleanup;
180     }
181
182     if (_erase > 0) {
183         printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi));
184     }    
185
186     size_check = ftdi_eeprom_build(&eeprom, eeprom_buf);
187     if (size_check == -1) {
188         printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
189         printf ("You need to short your string by: %d bytes\n", size_check);
190         goto cleanup;
191     } else {
192         printf ("Used eeprom space: %d bytes\n", 128-size_check); 
193     }
194
195     if (_flash > 0) {
196       if (cfg_getbool(cfg, "flash_raw")) {
197         if (filename != NULL && strlen(filename) > 0) {
198           FILE *fp = fopen(filename, "rb");
199           fread(eeprom_buf, 1, 128, fp);
200           fclose(fp);
201         }
202       }
203       printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char *)eeprom_buf));
204     }
205
206     // Write to file?
207     if (filename != NULL && strlen(filename) > 0) {
208         fp = fopen(filename, "w");
209         if (fp == NULL) {
210             printf ("Can't write eeprom file.\n");
211             exit (-1);
212         } else 
213             printf ("Writing to file: %s\n", filename);
214         
215         fwrite(eeprom_buf, 128, 1, fp);
216         fclose(fp);
217     }
218
219 cleanup:
220     if (_read > 0 || _erase > 0 || _flash > 0) {
221         printf("FTDI close: %d\n", ftdi_usb_close(&ftdi));
222     }
223
224     ftdi_deinit (&ftdi);
225     
226     cfg_free(cfg);
227     
228     printf("\n");
229     return 0;
230 }