-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
229 lines (191 loc) · 6.18 KB
/
main.cpp
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
/*
Copyright (C) 2017 Alamy Liu <[email protected]>
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/* Based on LIBFTDI 0.20.4 */
/*
1. Read binary (buffer) from EEPROM or FILE
2. (a) Decode: convert binary[IN] to ftdi_eeprom structure[IN]
(b) copy ftdi_eeprom structure IN --> OUT
3. update ftdi_eeprom[OUT]
4. Build: convert ftdi_eeprom[OUT] to binary[OUT]
5. Write binary[OUT] to EEPROM or FILE
*/
#include <cerrno>
#include <iostream>
//#include <string>
#include <cstdlib> // malloc, free
#include <fstream> // ifstream, ofstream
#include <iomanip> // setw, setfill, ...
#include <stdlib.h> // atoi
#include <unistd.h> // getopt()
//#include <ftdi.h>
#include "Options.hpp"
#include "ftdi_dev.hpp"
//#include "DebugW.hpp" // Debug
using namespace std;
// Classes
Options *opt;
FTDIDEV *ftdi_dev;
FILE *file;
//Debug *dbg; /* Warning: should be created before List */
static void atexit_free_options(void)
{
// cout << __func__ << ":" << __LINE__ << endl;
delete opt;
}
static void atexit_delete_ftdidev(void)
{
// cout << __func__ << ":" << __LINE__ << endl;
delete ftdi_dev;
}
int main(int argc, char* argv[])
{
int rc = EXIT_SUCCESS;
try {
opt = new Options(argc, argv);
} catch (int e) {
if (e != -ECANCELED) {
cerr << "Unknown error: " << e << endl;
}
/* 'help' option exit */
exit( EXIT_SUCCESS );
}
atexit( &atexit_free_options );
opt->applyHiddenRules();
opt->ShowOpts();
/* open FTDI device */
/* Allow fails: so that FTDIDEV methods could still be used.
* i.e.: just browsing file content
*/
try {
ftdi_dev = new FTDIDEV( opt );
} catch (std::runtime_error &e) {
cerr << e.what() << endl;
exit( EXIT_FAILURE );
}
/*
} catch (int e) {
cerr << "Error: " << e << endl;
exit( EXIT_FAILURE );
}
*/
atexit( &atexit_delete_ftdidev );
if (opt->isInFTDIDEV() || opt->isOutFTDIDEV()) {
ftdi_dev->show_info(); /* debugging */
}
/* At this point, we know
* EEPROM size: from FTDIDEV
* Input file size: from Options
*/
/* Options conflict/error detection (after we know EEPROM size) */
if (opt->validateOptions( ftdi_dev->get_eeprom_size() ) != 0)
return EXIT_FAILURE;
/* ---------- EEPROM ---------- */
/*
* IN OUT size ?
* FTDI file oSize = iSize = EEPROM size
* FTDI FTDI oSize = iSize = EEPROM size
* file file oSize = iSize
* file FTDI just read EEPROM size (pad 0 or truncate)
*/
unsigned int iSize = 0, oSize = 0;
if ( opt->isInFTDIDEV() || opt->isOutFTDIDEV() ) {
iSize = oSize = ftdi_dev->get_eeprom_size();
} else {
/* eeprom size type is INT, prevent overflow */
long fSize = min(opt->getInFileSize(), (long)FTDI_MAX_EEPROM_SIZE);
oSize = iSize = static_cast<unsigned int>(fSize); /* Safe: value in INT scope */
}
cout << "Size (I,O) = " << iSize << ", " << oSize << endl;
ftdi_dev->set_buffer_sizes(iSize, oSize);
/*
* 1. INPUT: Read from EEPROM or File
*/
if ( ftdi_dev->read(
opt->isInFTDIDEV(),
opt->getInFname(),
opt->verboseMode()) < 0 )
{
cerr << "Failed to Read!" << endl;
return EXIT_FAILURE;
}
/*
* 2. DECODE (binary -> structure)
*/
ftdi_dev->decode( opt->verboseMode() );
/*
* 3. UPDATE
*/
/* Input Only or No Update: Skip UPDATE & ENCODE steps.
* Note: still output data (if required)
*/
if ( !opt->isOutputDefined() || !opt->isUpdate() )
goto skip_update;
if ( opt->isUpdate_vid() ) ftdi_dev->update_vid( opt->getUpdate_vid() );
if ( opt->isUpdate_pid() ) ftdi_dev->update_pid( opt->getUpdate_pid() );
#if 0
if ( opt->isUpdate_manufacturer() )
eeprom->update_manufacturer( opt->getUpdate_manufacturer() );
if ( opt->isUpdate_product() )
eeprom->update_product( opt->getUpdate_product() );
if ( opt->isUpdate_serial() )
eeprom->update_serial( opt->getUpdate_serial() );
#else
ftdi_dev->update_strings(
opt->getUpdate_manufacturer(),
opt->getUpdate_product(),
opt->getUpdate_serial()
);
#endif
/*
* 4. ENCODE (structure -> binary)
*/
/* if things go wrong, don't write out. But still like to show information */
try {
if ( ftdi_dev->encode( opt->verboseMode() ) < 0 ) {
cerr << "Something is wrong in ENCODING. No output!" << endl;
opt->setOutNULL();
rc = EXIT_FAILURE;
}
} catch (int e) {
cout << "Something is wrong (" << e << "). No output!" << endl;
opt->setOutNULL();
rc = EXIT_FAILURE;
}
skip_update:
/* show output information */
if ( opt->isOutputDefined() || opt->isUpdate() ) {
if ( opt->viewBinary() ) ftdi_dev->dump( oSize ); /* Binary dump */
// if ( opt->viewHuman() ) ftdi_dev->showInOut(); /* Human readable */
}
/*
* 5. OUTPUT: Write to EEPROM or File
*/
/* Need to check isOutputDefined() here, as the _input only_ case
* also comes here.
*/
if ( opt->isOutputDefined() ) {
if ( ftdi_dev->write(
opt->isOutFTDIDEV(),
opt->getOutFname(),
opt->verboseMode()) < 0 )
{
cerr << "Failed to Write!" << endl;
return EXIT_FAILURE;
}
}
// delete dbg;
return rc;
}