forked from synapticon/siitool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
364 lines (305 loc) · 8.98 KB
/
main.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* siitool
*
* Simple program to print the values of the SII EEPROM content. It reads the
* raw binaries.
*
* Frank Jeschke <[email protected]>
*/
/*
* Copyright (c) 2013, Synapticon GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Execution of this software or parts of it exclusively takes place on hardware
* produced by Synapticon GmbH.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Synapticon GmbH.
*/
#include "sii.h"
#include "esi.h"
#include "esifile.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_BUFFER_SIZE (2*1000*1000)
#define MAX_FILENAME_SIZE (256)
enum eInputFileType {
UNDEFINED = 0
,ESIXML
,SIIEEPROM
};
//static int g_print_offsets = 0;
static int g_print_content = 0;
static unsigned int g_add_pdo_mapping = 0;
static unsigned int g_add_dc_section = 0;
static const char *base(const char *prog)
{
const char *p = prog;
while (*p != '\0') {
if (*p == '/')
prog = ++p;
else
p++;
}
return prog;
}
static char *get_suffix(const char *filename)
{
char *suffix = (char *)(filename+strlen(filename));
while (suffix != filename) {
if (*suffix == '.')
return (suffix+1);
suffix--;
}
return NULL; /* file doesn't have a valid suffix */
}
static enum eInputFileType check_file_suffix(const char *filename)
{
enum eInputFileType type = UNDEFINED;
char *suffix = get_suffix(filename);
if (suffix == NULL)
type = UNDEFINED;
else {
if (strncmp(suffix, "bin", strlen(suffix)) == 0)
type = SIIEEPROM;
else if (strncmp(suffix, "sii", strlen(suffix)) == 0)
type = SIIEEPROM;
else if (strncmp(suffix, "xml", strlen(suffix)) == 0)
type = ESIXML;
else {
fprintf(stderr, "Warning, unrecognized suffix: '%s'\n", suffix);
type = UNDEFINED;
}
}
return type;
}
static enum eInputFileType check_first_bytes(unsigned char *buffer)
{
enum eInputFileType type = UNDEFINED;
unsigned char *head = buffer;
if ((*(int *)head & 0xffffff) == 0xbfbbef) {
head += 3;
}
if (strncmp((char *)head, "<?xml", 5) == 0)
type = ESIXML;
else
type = SIIEEPROM; /* educated guess: if it's not XML it must be SII */
return type;
}
static enum eInputFileType file_type(const char *filename, unsigned char *buffer)
{
enum eInputFileType inputtype1 = UNDEFINED;
if (filename != NULL)
inputtype1 = check_file_suffix(filename);
enum eInputFileType inputtype2 = UNDEFINED;
inputtype2 = check_first_bytes(buffer);
if (inputtype1 == UNDEFINED)
return inputtype2;
if ((inputtype1 == ESIXML) && (inputtype1 != inputtype2)) {
fprintf(stderr, "Error, file suffix and content of file doesn't match!\n");
return UNDEFINED;
}
return inputtype1;
}
static void printhelp(const char *prog)
{
printf("Usage: %s [-h] [-v] [-p] [-o outfile] [filename]\n", prog);
printf(" -h print this help and exit\n");
printf(" -v print version an exit\n");
printf(" -m write pdo mapping to SII file\n");
printf(" -c write DC configuration to SII file\n");
printf(" -o <name> write output to file <name>\n");
printf(" -p print content human readable\n");
printf(" -d <num> select device number <num>, default <num> = 0\n");
printf(" filename path to eeprom file, if missing read from stdin\n");
printf("\nRecognized file types: SII and ESI/XML.\n");
}
static unsigned char * read_input(FILE *f, unsigned char *bufptr, size_t *size)
{
size_t capacity = *size;
size_t count = 0;
int input;
unsigned char *buffer = NULL;
if (bufptr == NULL) {
buffer = malloc(capacity);
}
while ((input=fgetc(f)) != EOF) {
buffer[count++] = (unsigned char)(input&0xff);
if (count >= (capacity - 1)) {
capacity += (capacity / 2);
buffer = realloc(buffer, capacity);
if (buffer == NULL) {
fprintf(stderr, "Realloc failed! Out of memory?\n");
*size = 0;
return NULL;
}
}
}
/* Fill the last part of the buffer capacity with zero */
memset((buffer+count), 0, capacity-count);
*size = capacity;
return buffer;
}
static int parse_xml_input(const unsigned char *buffer, size_t length, unsigned int device, const char *output)
{
EsiData *esi = esi_init_string(buffer, length);
//esi_print_xml(esi);
int include_pdo_strings = g_add_pdo_mapping || g_print_content;
if (esi_parse(esi, device, include_pdo_strings)) {
fprintf(stderr, "Error something went wrong in XML parsing\n");
esi_release(esi);
return -1;
}
SiiInfo *sii = esi_get_sii(esi);
sii_cat_sort(sii);
if (g_print_content) {
sii_print(sii);
} else {
sii_generate(sii, g_add_pdo_mapping, g_add_dc_section);
int ret = sii_write_bin(sii, output);
if (ret < 0) {
fprintf(stderr, "Error, couldn't write output file\n");
esi_release(esi);
return -1;
}
printf("= %s generated\n", output);
}
esi_release(esi);
return 0;
}
static int parse_sii_input(const unsigned char *buffer, const char *output)
{
SiiInfo *sii = sii_init_string(buffer, 1024);
//alternative: SiiInfo *sii = sii_init_file(filename) */
if (g_print_content)
sii_print(sii);
else {
sii_generate(sii, g_add_pdo_mapping, g_add_dc_section);
int ret = sii_write_bin(sii, output);
if (ret < 0) {
fprintf(stderr, "Error, couldn't write output file\n");
return -1;
}
printf("= %s generated\n", output);
}
sii_release(sii);
return 0;
}
int main(int argc, char *argv[])
{
FILE *f;
unsigned char *eeprom = NULL;
size_t eeprom_length = MAX_BUFFER_SIZE;
char *filename = NULL;
char *output = NULL;
int ret = -1;
unsigned int device = 0;
/* FIXME rewrite using getopt() */
for (int i=1; i<argc; i++) {
switch (argv[i][0]) {
case '-':
if (argv[i][1] == 'h') {
printhelp(base(argv[0]));
return 0;
} else if (argv[i][1] == 'v') {
printf("%s %s\n",
base(argv[0]),
VERSION);
return 0;
} else if (argv[i][1] == 'o') {
i++;
output = malloc(strlen(argv[i])+1);
memmove(output, argv[i], strlen(argv[i])+1);
} else if (argv[i][1] == 'p') {
g_print_content = 1;
} else if (argv[i][1] == 'm') {
g_add_pdo_mapping = 1;
} else if (argv[i][1] == 'c') {
g_add_dc_section = 1;
} else if (argv[i][1] == 'd') {
sscanf(argv[i+1], "%d", &device);
} else if (argv[i][1] == '\0') { /* read from stdin (default) */
filename = NULL;
} else {
fprintf(stderr, "Invalid argument\n");
printhelp(base(argv[0]));
return 0;
}
break;
default:
/* asuming file name */
filename = argv[i];
break;
}
}
if (filename == NULL)
eeprom = read_input(stdin, eeprom, &eeprom_length);
else {
f = fopen(filename, "r");
if (f == NULL) {
perror("Error open input file");
ret = -1;
goto finish;
}
#if DEBUG == 1
printf("Start reading contents of file %s\n", filename);
#endif
eeprom = read_input(f, eeprom, &eeprom_length);
fclose(f);
}
if (eeprom == NULL) {
goto finish;
}
/* recognize input */
enum eInputFileType filetype = file_type(filename, eeprom);
unsigned char *xml_start = eeprom;
switch (filetype) {
case ESIXML:
#if DEBUG == 1
printf("Processing ESI/XML file\n");
#endif
/* Start XML processing at the first '<' character to avoid strange behavior when parsing. */
while (*xml_start != '<')
xml_start++;
ret = parse_xml_input(xml_start, eeprom_length, device, output);
break;
case SIIEEPROM:
#if DEBUG == 1
printf("Processing SII/EEPROM file\n");
#endif
ret = parse_sii_input(eeprom, output);
break;
case UNDEFINED:
default:
ret = -1;
goto finish;
}
finish:
if (eeprom)
free(eeprom);
if (output)
free(output);
return ret;
}