-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheepmake.c
528 lines (399 loc) · 11.1 KB
/
eepmake.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Parses EEPROM text file and createds binary .eep file
* Usage: eepmake input_file output_file
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <endian.h>
#include "eeptypes.h"
//todo: larger initial mallocs
struct header_t header;
struct atom_t *custom_atom, vinf_atom, dt_atom;
struct vendor_info_d* vinf;
bool product_serial_set, product_id_set, product_ver_set, vendor_set, product_set;
bool data_receive, has_dt, receive_dt;
char **data;
char *current_atom; //rearranged to write out
unsigned int data_len, custom_ct, total_size, data_cap, custom_cap;
int write_binary(char* out) {
FILE *fp;
int offset;
unsigned int i;
short crc;
fp=fopen(out, "wb");
if (!fp) {
printf("Error writing file %s\n", out);
return -1;
}
fwrite(&header, sizeof(header), 1, fp);
current_atom = (char *) malloc(vinf_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
//vendor information atom first part
memcpy(current_atom, &vinf_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-2;
//data first part
memcpy(current_atom+offset, vinf_atom.data, VENDOR_SIZE);
offset += VENDOR_SIZE;
//data strings
memcpy(current_atom+offset, vinf->vstr, vinf->vslen);
offset += vinf->vslen;
memcpy(current_atom+offset, vinf->pstr, vinf->pslen);
offset += vinf->pslen;
//vinf last part
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
if (has_dt) {
printf("Writing out DT...\n");
current_atom = (char *) malloc(dt_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &dt_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, dt_atom.data, dt_atom.dlen-CRC_SIZE);
offset += dt_atom.dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
for (i = 0; i<custom_ct; i++) {
custom_atom[i].count-=!has_dt;
current_atom = (char *) malloc(custom_atom[i].dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &custom_atom[i], ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, custom_atom[i].data, custom_atom[i].dlen-CRC_SIZE);
offset += custom_atom[i].dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
fflush(fp);
fclose(fp);
return 0;
}
int parse_data(char* c) {
int k;
char* i = c;
char* j = c;
while(*j != '\0')
{
*i = *j++;
if(isxdigit(*i))
i++;
}
*i = '\0';
int len = strlen(c);
if (len % 2 != 0) {
printf("Error: data must have an even number of hex digits\n");
return -1;
} else {
for (k = 0; k<len/2; k++) {
unsigned int byte;
if (data_len==data_cap) {
data_cap *=2;
*data = (char *) realloc(*data, data_cap);
}
sscanf(c, "%2x", &byte);
(*data)[data_len++] = byte;
c+=2;
}
}
return 0;
}
void finish_data() {
if (data_receive) {
data_receive = false;
*data = (char *) realloc(*data, data_len);
total_size+=ATOM_SIZE+data_len;
if (receive_dt) {
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = data_len+CRC_SIZE;
} else {
//finish atom description
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = data_len+CRC_SIZE;
custom_ct++;
}
}
}
int parse_command(char* cmd, char* c) {
uint32_t high1, high2;
bool continue_data=false;
/* Vendor info related part */
if (strcmp(cmd, "product_uuid")==0) {
product_serial_set = true; //required field
high1 = 0; high2 = 0;
sscanf(c, "%100s %08x-%04x-%04x-%04x-%04x%08x\n", cmd,
&vinf->serial[3],
&high1, &vinf->serial[2],
&high2, &vinf->serial[1],
&vinf->serial[0]);
vinf->serial[2] |= high1<<16;
vinf->serial[1] |= high2<<16;
if ((vinf->serial[3]==0) && (vinf->serial[2]==0) &&
(vinf->serial[1]==0) && (vinf->serial[0]==0)) {
//read 128 random bits from /dev/urandom
int random_file = open("/dev/urandom", O_RDONLY);
ssize_t result = read(random_file, vinf->serial, 16);
close(random_file);
if (result <= 0) {
printf("Unable to read from /dev/urandom to set up UUID");
return -1;
}
else {
//put in the version
vinf->serial[2] = (vinf->serial[2] & 0xffff0fff) | 0x00004000;
//put in the variant
vinf->serial[1] = (vinf->serial[1] & 0x3fffffff) | 0x80000000;
printf("UUID=%08x-%04x-%04x-%04x-%04x%08x\n",
vinf->serial[3],
vinf->serial[2]>>16, vinf->serial[2] & 0xffff,
vinf->serial[1]>>16, vinf->serial[1] & 0xffff,
vinf->serial[0]);
}
}
} else if (strcmp(cmd, "product_id")==0) {
product_id_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pid);
} else if (strcmp(cmd, "product_ver")==0) {
product_ver_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pver);
} else if (strcmp(cmd, "vendor")==0) {
vendor_set = true; //required field
vinf->vstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->vstr);
total_size-=vinf->vslen;
vinf_atom.dlen-=vinf->vslen;
vinf->vslen = strlen(vinf->vstr);
total_size+=vinf->vslen;
vinf_atom.dlen+=vinf->vslen;
} else if (strcmp(cmd, "product")==0) {
product_set = true; //required field
vinf->pstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->pstr);
total_size-=vinf->pslen;
vinf_atom.dlen-=vinf->pslen;
vinf->pslen = strlen(vinf->pstr);
total_size+=vinf->pslen;
vinf_atom.dlen+=vinf->pslen;
}
/* DT atom related part */
else if (strcmp(cmd, "dt_blob")==0) {
finish_data();
has_dt = true;
c+=strlen("dt_blob");
receive_dt=true;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &dt_atom.data;
*data = (char *) malloc(data_cap);
if (parse_data(c)) {
return -1;
}
continue_data = true;
}
/* Custom data related part */
else if (strcmp(cmd, "custom_data")==0) {
finish_data();
c+=strlen("custom_data");
if (custom_cap == custom_ct) {
custom_cap *= 2;
custom_atom = (struct atom_t*) realloc(custom_atom, custom_cap * sizeof(struct atom_t));
}
receive_dt=false;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &custom_atom[custom_ct].data;
*data = (char *) malloc(data_cap);
if (parse_data(c)) {
return -1;
}
continue_data = true;
} else if (strcmp(cmd, "end") ==0) {
//close last data atom
continue_data=false;
}
/* Incoming data */
else if (data_receive) {
if (parse_data(c)) {
return -1;
}
continue_data = true;
}
if (!continue_data) finish_data();
return 0;
}
int read_text(char* in) {
FILE * fp;
char * line = NULL;
char * c = NULL;
size_t len = 0;
ssize_t read;
int linect = 0;
char * command = (char*) malloc (101);
int i;
has_dt = false;
printf("Opening file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
//allocating memory and setting up required atoms
custom_cap = 1;
custom_atom = (struct atom_t*) malloc(sizeof(struct atom_t) * custom_cap);
total_size=ATOM_SIZE*2+HEADER_SIZE+VENDOR_SIZE;
vinf_atom.type = ATOM_VENDOR_TYPE;
vinf_atom.count = ATOM_VENDOR_NUM;
vinf = (struct vendor_info_d *) calloc(1, sizeof(struct vendor_info_d));
vinf_atom.data = (char *)vinf;
vinf_atom.dlen = VENDOR_SIZE + CRC_SIZE;
while ((read = getline(&line, &len, fp)) != -1) {
linect++;
c = line;
for (i=0; i<read; i++) if (c[i]=='#') c[i]='\0';
while (isspace(*c)) ++c;
if (*c=='\0' || *c=='\n' || *c=='\r') {
//empty line, do nothing
} else if (isalnum (*c)) {
sscanf(c, "%100s", command);
#ifdef DEBUG
printf("Processing line %u: %s", linect, c);
if ((*(c+strlen(c)-1))!='\n') printf("\n");
#endif
if (parse_command(command, c)) {
return -1;
}
} else printf("Can't parse line %u: %s", linect, c);
}
finish_data();
if (!product_serial_set || !product_id_set || !product_ver_set || !vendor_set || !product_set) {
printf("Warning: required fields missing in vendor information or GPIO map, using default values\n");
}
printf("Done reading\n");
return 0;
}
int read_dt(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening DT file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of DT data\n", size);
total_size+=ATOM_SIZE+size;
has_dt = true;
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = size+CRC_SIZE;
dt_atom.data = (char *) malloc(size);
if (!fread(dt_atom.data, size, 1, fp)) goto err;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return -1;
}
int read_custom(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening custom data file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of custom data\n", size);
total_size+=ATOM_SIZE+size;
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = size+CRC_SIZE;
custom_atom[custom_ct].data = (char *) malloc(size);
if (!fread(custom_atom[custom_ct].data, size, 1, fp)) goto err;
custom_ct++;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return -1;
}
int main(int argc, char *argv[]) {
int ret;
int i, custom_o=0;
if (argc<3) {
printf("Wrong input format.\n");
printf("Try 'eepmake input_file output_file [dt_file] [-c custom_file_1 ... custom_file_n]'\n");
return 1;
}
ret = read_text(argv[1]);
if (ret) {
printf("Error reading and parsing input, aborting\n");
return 1;
}
if (argc>3) {
if (strcmp(argv[3], "-c")==0) {
custom_o=4;
} else {
//DT file specified
if (dt_atom.dlen) total_size-=(ATOM_SIZE +dt_atom.dlen - CRC_SIZE);
ret = read_dt(argv[3]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 1;
}
}
}
if (argc>4 && strcmp(argv[4], "-c")==0) custom_o = 5;
if (custom_o)
for (i = custom_o; i<argc; i++) {
if (custom_cap == custom_ct) {
custom_cap *= 2;
custom_atom = (struct atom_t *)realloc(
custom_atom, custom_cap * sizeof(struct atom_t));
}
//new custom data file
ret = read_custom(argv[i]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 1;
}
}
header.signature = htole32(HEADER_SIGN);
header.ver = FORMAT_VERSION;
header.res = 0;
header.numatoms = 2+has_dt+custom_ct;
header.eeplen = total_size;
printf("Writing out...\n");
ret = write_binary(argv[2]);
if (ret) {
printf("Error writing output\n");
return 1;
}
printf("Done.\n");
return 0;
}