forked from 360trev/ME7Sum
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathme7sum.c
2823 lines (2379 loc) · 70.1 KB
/
me7sum.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* me7sum [firmware management tool for Bosch ME7.x firmware]
By 360trev and nyet
Inspired by work from Andy Whittaker's (tools and information)
See http://www.andywhittaker.com/ECU/BoschMotronicME71.aspx
Note: Uses configuration files (see my ini file example)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h> /* isprint() */
#include "os/os.h"
#include "inifile_prop.h"
#include "crc32.h"
#include "str.h"
#include "utils.h"
#include "range.h"
#include "md5.h"
#include "rsa.h"
//#define DEBUG_ROM_INFO
//#define DEBUG_ROMSYS_MATCHING
//#define DEBUG_CRC_MATCHING
//#define DEBUG_ROMSYS_PP_MATCHING
//#define DEBUG_RSA_MATCHING
//#define DEBUG_MAIN_MATCHING
//#define DEBUG_MULTIPOINT_MATCHING
#include "debug.h"
#define CHECK_BOOTROM_MP
#define RSA_MODULUS_SIZE 1024
#define RSA_BLOCK_SIZE (RSA_MODULUS_SIZE/8)
/* Images with 2+64 main mp descriptors do not have an end marker */
#ifdef CHECK_BOOTROM_MP
#define MAX_MP_BLOCK_LEN 66
#else
#define MAX_MP_BLOCK_LEN 64
#endif
// structures
struct ChecksumPair {
uint32_t v; // value
uint32_t iv; // inverse value
};
struct MultipointDescriptor {
struct Range r;
struct ChecksumPair csum;
};
#if 0
static int sbhexdump(struct strbuf *buf, const void *p, int len)
{
int i=len;
int ret=0;
const uint8_t *ptr=p;
while(i--)
ret+=sbprintf(buf, "%02x%s", *ptr++, ((i&0xf)==0 && len>32)?"\n":i?" ":"");
return ret;
}
static int sbprintdesc(struct strbuf *buf, const struct MultipointDescriptor *d)
{
int ret=sbprintf(buf, " ");
ret+=sbhexdump(buf, d, sizeof(*d));
ret+=sbprintf(buf, "\n");
ret+=sbprintf(buf, " %x-%x %x %x\n", d->r.start, d->r.end, d->csum.v, d->csum.iv);
return ret;
}
#endif
#ifndef __GIT_VERSION
#define __GIT_VERSION "unknown"
#endif
#define MAX_CRC_BLKS 4
#define MD5_MAX_BLKS 4
// main firmware checksum validation
struct rom_config {
int readonly;
uint32_t base_address; /* rom base address */
struct {
uint32_t n; /* offset of modulus */
uint32_t e; /* offset of exponent */
uint32_t s; /* offset of signature */
uint32_t ds; /* offset of default signature (unused?) */
int exponent; /* actual exponent */
struct Range md5[MD5_MAX_BLKS];
} rsa;
uint32_t romsys;
uint32_t crctab[2];
uint32_t multipoint_block_start[2]; /* start of multipoint block descriptors (two sets, first one isn't always there) */
uint32_t multipoint_desc_len; /* size of descriptors */
uint32_t main_checksum_offset; /* two start/end pairs, one at offset, other at offset+8 */
uint32_t main_checksum_final; /* two 4 byte checksum (one inv) for two blocks conctatenated above) */
struct {
struct Range r;
uint32_t offset;
} crc[MAX_CRC_BLKS+1]; /* 0/4 is pre-region (for kbox and other) Up to 5 CRC blocks (total) to check */
uint32_t csm_offset; /* ME7.1.1 */
};
struct info_config {
InfoItem EPK;
InfoItem sw_number;
InfoItem hw_number;
InfoItem part_number;
InfoItem sw_version;
InfoItem engine_id;
};
// globals
static FILE *ReportFile = NULL;
static struct rom_config Config;
static struct info_config InfoConfig;
#ifdef DEBUG_YES
static int Verbose = 2;
#else
static int Verbose = 0;
#endif
static int ChecksumsFound = 0;
static int ErrorsUncorrectable = 0;
static int ErrorsFound = 0;
static int ErrorsCorrected = 0;
//
// List of configurable properties to read from config file into our programme...
// [this stops us having to hardcode values into the code itself]
//
static PropertyListItem romProps[] = {
// get rom region information
{ GET_VALUE, &Config.base_address, "ignition", "rom_firmware_start", "0x800000"},
{ GET_VALUE, &Config.multipoint_block_start[0], "ignition", "rom_checksum_block_start0", "0"},
{ GET_VALUE, &Config.multipoint_block_start[1], "ignition", "rom_checksum_block_start", "0"},
{ GET_VALUE, &Config.multipoint_desc_len, "ignition", "rom_checksum_desc_len", "0x10"},
{ GET_VALUE, &Config.main_checksum_offset, "ignition", "rom_checksum_offset", "0"},
{ GET_VALUE, &Config.main_checksum_final, "ignition", "rom_checksum_final", "0"},
{ GET_VALUE, &Config.crc[0].r.start, "ignition", "rom_crc0_start", "0"},
{ GET_VALUE, &Config.crc[0].r.end, "ignition", "rom_crc0_end", "0"},
{ GET_VALUE, &Config.crc[1].r.start, "ignition", "rom_crc1_start", "0"},
{ GET_VALUE, &Config.crc[1].r.end, "ignition", "rom_crc1_end", "0"},
{ GET_VALUE, &Config.crc[1].offset, "ignition", "rom_crc1", "0"},
{ GET_VALUE, &Config.crc[2].r.start, "ignition", "rom_crc2_start", "0"},
{ GET_VALUE, &Config.crc[2].r.end, "ignition", "rom_crc2_end", "0"},
{ GET_VALUE, &Config.crc[2].offset, "ignition", "rom_crc2", "0"},
{ GET_VALUE, &Config.crc[3].r.start, "ignition", "rom_crc3_start", "0"},
{ GET_VALUE, &Config.crc[3].r.end, "ignition", "rom_crc3_end", "0"},
{ GET_VALUE, &Config.crc[3].offset, "ignition", "rom_crc3", "0"},
{ GET_VALUE, &Config.crc[4].r.start, "ignition", "rom_crc4_start", "0"},
{ GET_VALUE, &Config.crc[4].r.end, "ignition", "rom_crc4_end", "0"},
{ GET_VALUE, &Config.crc[4].offset, "ignition", "rom_crc4", "0"},
{ END_LIST, 0, "",""},
};
static InfoListItem romInfo[] = {
// get rom region information
{ "EPK", GET_VALUE, &InfoConfig.EPK, "info", "epk", "0", "41"},
{ "Part Number", GET_VALUE, &InfoConfig.part_number, "info", "part_number", "0", "12"},
{ "Engine ID", GET_VALUE, &InfoConfig.engine_id, "info", "engine_id", "0", "17"},
{ "SW Version", GET_VALUE, &InfoConfig.sw_version, "info", "sw_version", "0", "4"},
{ "HW Number", GET_VALUE, &InfoConfig.hw_number, "info", "hw_number", "0", "10"},
{ "SW Number", GET_VALUE, &InfoConfig.sw_number, "info", "sw_number", "0", "10"},
{ NULL,END_LIST,NULL,NULL,NULL}
};
static int FindRomInfo(const struct ImageHandle *ih);
static int DoRomInfo(const struct ImageHandle *ih, struct section *osconfig);
static int FindROMSYS(struct ImageHandle *ih);
static int DoROMSYS(struct ImageHandle *ih); // Startup in RSA, MP; ParamPage in RSA, MP, Main CSM, Main CRC
static int FindMainCRCPreBlk(const struct ImageHandle *ih);
static int FindMainCRCBlks(const struct ImageHandle *ih);
static int FindMainCRCOffsets(const struct ImageHandle *ih);
static int DoMainCRCs(struct ImageHandle *ih); // In ROMSYS Program Pages (sometimes), Main CSM, MP
static int FindMainCSMOffsets(const struct ImageHandle *ih);
static int DoMainCSMs(struct ImageHandle *ih); // In Main Program CSM, MP
static int DoROMSYS_ProgramPages(struct ImageHandle *ih); // In RSA (sometimes, in tuned files), MP
static int FindRSAOffsets(struct ImageHandle *ih);
static int FindMD5Ranges(struct ImageHandle *ih);
static int DoRSA(struct ImageHandle *ih); // In Main Program CSM, MP
static int FindCRCTab(const struct ImageHandle *ih);
static int DoCRCTab(struct ImageHandle *ih);
static int FindMainProgramOffset(const struct ImageHandle *ih);
static int FindMainProgramFinal(const struct ImageHandle *ih);
static int DoMainProgramCSM(struct ImageHandle *ih); // In MP
static int FindChecksumBlks(const struct ImageHandle *ih, int which);
static int DoChecksumBlk(struct ImageHandle *ih, uint32_t nStartBlk, struct strbuf *buf, int bootrom);
static void usage(const char *prog)
{
printf("Usage: %s [-v] [-i <config.ini>] <inrom.bin> [outrom.bin]\n", prog);
printf(" %s [-v] [-i <config.ini>] [-r <report.txt>] [-s] <inrom.bin>\n", prog);
exit(-1);
}
static int bytecmp(const void *buf, uint8_t byte, size_t len)
{
int i;
const uint8_t *p=buf;
for(i=0;i<len;i++) {
if (p[i]!=byte)
return p[i]-byte;
}
return 0;
}
#ifdef CHECK_BOOTROM_MP
/* returns 0 if desc[0] and [1] not in bootrom */
/* returns 1 if desc[0] and [1] are in bootrom, updates ih->bootrom_whitelist if whitelisted */
/* returns -1 if not in whitelist AND does not match next pair of non-bootrom descriptors */
static int check_whitelist(struct ImageHandle *ih, uint32_t addr)
{
struct MultipointDescriptor desc[4];
/* Check for hardcoded bootrom csums... if there, treat as ok */
static const uint32_t whitelist[][2] = {{0x0fa0f5cf, 0x0f4716b3},
{0x0e59d5c8, 0x1077fb35}};
int i;
for(i=0;i<4;i++)
memcpy_from_le32(desc+i, ih->d.u8+addr+Config.multipoint_desc_len*i,
sizeof(struct MultipointDescriptor));
for(i=0;i<2;i++) {
if (desc[i].r.start>=desc[i].r.end) return 0;
if (desc[i].r.start>=Config.base_address) return 0;
if (desc[i].r.end>=Config.base_address) return 0;
}
if (desc[0].r.start!=0 || desc[0].r.end!=0x3fff) return 0;
if (desc[1].r.start!=0x4000 || desc[1].r.end!=0x7fff) return 0;
for(i=0;i<2;i++) {
if (desc[0].csum.v==~desc[0].csum.iv &&
desc[1].csum.v==~desc[1].csum.iv &&
whitelist[i][0]==desc[0].csum.v &&
whitelist[i][1]==desc[1].csum.v) {
ih->bootrom_whitelist=1;
return 1;
}
}
if ((desc[0].csum.v != desc[2].csum.v) ||
(desc[1].csum.v != desc[3].csum.v) ||
(desc[0].csum.iv != desc[2].csum.iv) ||
(desc[1].csum.iv != desc[3].csum.iv)) {
printf("ERROR! Inconsistency in non-whitelisted bootrom multipoint descriptors!\n");
ErrorsUncorrectable++;
return -1;
}
return 1;
}
#endif
/*
* main()
*
*/
int main(int argc, char **argv)
{
int Step=0;
int iTemp;
int summary=0;
char *prog=argv[0];
char *inifile=NULL;
char *reportfile=NULL;
char *input=NULL;
char *output=NULL;
int i, c;
struct ImageHandle ih;
struct section *osconfig=NULL;
struct strbuf buf;
memset(&buf, 0, sizeof(buf));
// information about the tool
printf("ME7Sum (%s) [Management tool for Bosch ME7.x firmwares]\n",
__GIT_VERSION);
printf("Inspiration from Andy Whittaker's tools and information.\n");
printf("Written by 360trev and nyet [BSD License Open Source].\n");
opterr=0;
while ((c = getopt(argc, argv, "qsvi:r:")) != -1)
{
switch (c)
{
case 'q':
Verbose--;
break;
case 's':
summary++;
break;
case 'v':
Verbose++;
break;
case 'i':
inifile=optarg;
break;
case 'r':
reportfile=optarg;
break;
case '?':
if (optopt == 'i')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option '-%c'.\n", optopt);
// break; // fallthrough
default:
usage(prog);
return -1;
}
}
if (Verbose<0) Verbose=0;
argc-=optind;
argv+=optind;
if (argc==0 || argc>2)
usage(prog);
input = argv[0];
if (argc>1)
output = argv[1];
else
Config.readonly=1;
if (summary && output) {
fprintf(stderr, "-s cannot be used with output file\n");
usage(prog);
return -1;
}
if (reportfile && output) {
fprintf(stderr, "-r cannot be used with output file\n");
usage(prog);
return -1;
}
if (inifile)
{
printf("Attempting to open firmware config file '%s'\n",inifile);
// load properties file into memory
osconfig = read_properties(inifile);
if(osconfig == NULL)
{
fprintf(stderr, "failed to open ini file %s\n", inifile);
return -1;
}
}
if (reportfile) {
ReportFile = fopen(reportfile, "w");
if (!ReportFile) {
fprintf(stderr, "failed to open report file %s: %s\n", reportfile, strerror(errno));
return -1;
}
}
// get rom region information from config file (see defined property list)
process_properties_list(osconfig, romProps);
process_info_list(osconfig, romInfo);
// open the firmware file
printf("\nAttempting to open firmware file '%s'\n",input);
i=iload_file(&ih, input, 0, &buf);
if (buf.pbuf) {
if (i || Verbose>1) printf("%s", buf.pbuf);
free(buf.pbuf);
}
if (i)
{
printf("Failed to open firmware file '%s'\n",input);
ErrorsFound++;
return -1;
}
// sanity check: validate firmware file is at least 512kbytes length before proceeding.
if(ih.len != 512*1024 && ih.len != 1024*1024)
{
printf("File is an odd size (%d bytes). Are you sure this is a firmware dump?\n",
(int)ih.len);
ErrorsFound++;
goto out;
}
if(ih.len == (1024*1024))
{
/* Check to make sure it isn't a doubled up file or padded
with ff or 00 */
if (memcmp(ih.d.u8, ih.d.u8+512*1024, 512*1024)==0) {
printf("File is doubled up 512k dump. Treating as 512k\n");
ih.len=512*1024;
ih.pad = PADDING_DOUBLED;
} else if (bytecmp(ih.d.u8+512*1024, 0xff, 512*1024)==0) {
printf("File is padded from 512k to 1024k with 0xFF. Treating as 512k\n");
ih.len=512*1024;
ih.pad = PADDING_FF;
} else if (bytecmp(ih.d.u8+512*1024, 0xff, 512*1024-32)==0) {
printf("File is padded from 512k to 1024k with 0xFF. Treating as 1024k but will try 512k CRC hardcoded blocks\n");
ih.pad = PADDING_TRY_512K_CRC;
/*
} else if (bytecmp(ih.d.u8+512*1024, 0, 512*1024)==0) {
printf("File is padded from 512k to 1024k with zeros. Treating as 512k\n");
ih.len=512*1024;
ih.pad = PADDING_00;
*/
}
}
//
// ROM info
//
printf("\nStep #%d: Reading ROM info ..\n", ++Step);
if(InfoConfig.part_number.off==0)
{
FindRomInfo(&ih);
}
if(InfoConfig.part_number.off)
{
DoRomInfo(&ih, osconfig);
}
else
{
printf("Step #%d: ERROR! Skipping ROM info.. UNDEFINED\n", Step);
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_ROM;
//
// ROMSYS
//
printf("\nStep #%d: Reading ROMSYS ..\n", ++Step);
if(Config.romsys==0)
{
FindROMSYS(&ih);
}
if(Config.romsys)
{
DoROMSYS(&ih);
}
else
{
printf("Step #%d: ERROR! Skipping ROMSYS.. UNDEFINED\n", Step);
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_ROMSYS;
//
// CRC table(s)
//
printf("\nStep #%d: Finding CRC table(s) ..\n", ++Step);
if(!Config.crctab[0])
FindCRCTab(&ih);
if(Config.crctab[0]) {
DoCRCTab(&ih);
} else {
printf("Step #%d: ERROR! Couldn't find CRC table(s)\n", Step);
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
//
// RSA
//
printf("\nStep #%d: Reading RSA signatures ..\n", ++Step);
FindRSAOffsets(&ih);
if(Config.rsa.n && Config.rsa.s && Config.rsa.e) {
FindMD5Ranges(&ih);
if (Config.rsa.md5[0].start && Config.rsa.md5[0].end) {
DoRSA(&ih);
} else {
printf("Step #%d: ERROR! Detected RSA signature, but no MD5 regions\n", Step);
ErrorsUncorrectable++;
}
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_RSA;
//
// Main data CRC/checksums if specified
//
printf("\nStep #%d: Reading Main Data CRC/Checksums ..\n", ++Step);
if(Config.crc[0].r.start==0 && Config.crc[0].r.end==0)
{
FindMainCRCPreBlk(&ih);
}
if(Config.crc[1].r.start==0 && Config.crc[1].r.end==0)
{
FindMainCRCBlks(&ih);
}
// note, crc0 and crc4 don't have offsets!
if(Config.crc[1].offset==0)
{
FindMainCRCOffsets(&ih); /* Detect if using CRC algo */
}
if(Config.csm_offset==0)
{
FindMainCSMOffsets(&ih); /* Detect if using Checksum algo */
}
if(Config.crc[1].r.start && Config.crc[1].r.end &&
(Config.crc[1].offset || Config.csm_offset)) {
if(Verbose && Config.csm_offset) {
if(Config.crc[1].offset) {
printf(" %s has both main CRC and checksum offsets!\n",
ih.filename);
} else {
printf("WARNING: %s has no main CRC offset(s) but does have a main checksum offset!\n",
ih.filename);
DoMainCRCs(&ih);
}
}
/* Note: both CRC and checksum are possible! */
if(Config.crc[1].offset)
{
DoMainCRCs(&ih);
}
if(Config.csm_offset)
{
DoMainCSMs(&ih);
}
}
else
{
printf("Step #%d: ERROR! Skipping Main Data checksums ... UNDEFINED\n",
Step);
#ifdef DEBUG_CRC_MATCHING
DoMainCRCs(&ih);
DoMainCSMs(&ih);
#endif
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_CRC;
//
// ROMSYS Program Pages
//
if(Config.romsys)
{
printf("\nStep #%d: ROMSYS Program Pages\n", ++Step);
DoROMSYS_ProgramPages(&ih);
}
else
{
printf("Step #%d: ERROR! Skipping ROMSYS Program Pages.. UNDEFINED\n", Step);
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_ROMSYS_PP;
//
// Main program checksums
//
printf("\nStep #%d: Reading Main Program Checksums ..\n", ++Step);
if(Config.main_checksum_offset==0)
{
FindMainProgramOffset(&ih);
}
if(Config.main_checksum_final==0)
{
FindMainProgramFinal(&ih);
}
if (Config.main_checksum_offset && Config.main_checksum_final)
{
//DoMainProgramCSM(&ih, Config.main_checksum_offset, Config.main_checksum_final);
DoMainProgramCSM(&ih);
}
else
{
printf("Step #%d: ERROR! Skipping Main Program Checksums.. UNDEFINED\n", Step);
ErrorsUncorrectable++;
}
if(summary && summary<=Step) goto out;
DEBUG_EXIT_MAIN;
//
// Multi point checksums
//
printf("\nStep #%d: Reading Multipoint Checksum Blocks ..\n", ++Step);
for (i=0;i<2;i++) {
if(Config.multipoint_block_start[i]==0)
{
FindChecksumBlks(&ih, i);
}
if(Config.multipoint_block_start[i])
{
int bootrom=0;
int printed_dots=0;
#ifdef CHECK_BOOTROM_MP
/* Only check for whitelist in main multipoint block */
if (i==1)
bootrom = check_whitelist(&ih, Config.multipoint_block_start[i]);
#endif
/* Images with 2+64 main MP descriptors do not have an end marker */
for(iTemp=0; iTemp<MAX_MP_BLOCK_LEN; iTemp++)
{
int result=0;
struct strbuf buf;
if (iTemp>1) bootrom=0;
memset(&buf, 0, sizeof(buf));
sbprintf(&buf, "%2d) ",iTemp+1);
result = DoChecksumBlk(&ih,
Config.multipoint_block_start[i]+(Config.multipoint_desc_len*iTemp),
&buf, bootrom);
if (buf.pbuf) {
if (iTemp<3 || result<0 || Verbose>0 || iTemp>MAX_MP_BLOCK_LEN-4)
{
printf("%s", buf.pbuf);
printed_dots=0;
}
else if (!printed_dots) {
printed_dots=1;
printf(" ..........\n");
}
free (buf.pbuf);
}
if (result == 1) { break; } // end of blocks;
}
printf(" Multipoint #%d: [%d blocks x <16> = %d bytes]\n", i+1, iTemp, iTemp*16);
}
else
{
if (i!=0) {
printf("Step #%d: ERROR! Skipping Multipoint Checksum Block... UNDEFINED\n", Step);
ErrorsUncorrectable++;
}
}
}
DEBUG_EXIT_MULTIPOINT;
/* if (!Config.readonly) */ {
int errs;
printf("\nStep #%d: Looking for rechecks ..\n", ++Step);
if ((errs=ProcessRecordDeps())) {
printf("\n*** WARNING! Unsatisfied rechecks. You may have to rerun ME7Sum on this file!\n");
ErrorsFound+=errs;
}
}
//
// All done!
//
printf("\n*** Found %d checksums in %s\n", ChecksumsFound, input);
if(ErrorsUncorrectable)
{
printf("\n*** ABORTING! %d uncorrectable error(s) in %s! ***\n", ErrorsUncorrectable, input);
return -1;
}
if(output && ErrorsCorrected > 0)
{
struct strbuf buf;
memset(&buf, 0, sizeof(buf));
printf("\nAttempting to output corrected firmware file '%s'\n",output);
// write crc corrected file out
if (ih.pad == PADDING_DOUBLED) {
memcpy(ih.d.u8, ih.d.u8+512*1024, 512*1024);
}
save_file(output,ih.d.p,ih.pad==PADDING_NONE?ih.len:ih.len*2, &buf);
if(buf.pbuf) {
printf("%s", buf.pbuf);
free(buf.pbuf);
}
}
out:
// close the file
if(ih.d.p != 0) { ifree_file(&ih); }
// free config
if(osconfig != 0) { free_properties(osconfig); }
// Made minor alterations in output to circumvent issue #9 @nyetwurk
if (ErrorsCorrected!=ErrorsFound) {
printf("\n*** WARNING! %d/%d uncorrected error(s) in %s! ***\n",
ErrorsFound-ErrorsCorrected, ErrorsFound, input);
} else if (ErrorsFound == 0 && output){
printf("\n*** No errors were found and so no \"%s\" was generated.\n", output);
} else if (output) {
printf("\n*** DONE! %d/%d error(s) in %s corrected in %s! ***\n", ErrorsCorrected,
ErrorsFound, input, output);
} else {
printf("\n*** DONE! %d error(s) in %s! ***\n", ErrorsFound, input);
}
if (ReportFile) {
PrintAllRecords(ReportFile);
fclose(ReportFile);
}
FreeAllRecords();
return 0;
}
/*
* GetRomInfo
*
* - uses config file to parse rom data and show interesting information about this rom dump
*/
static int GetRomInfo(const struct ImageHandle *ih, struct section *osconfig)
{
InfoListItem *info;
int max_len=0;
if(ih == NULL) return(-1);
// Find the longest label so we know how big the label column should be
for(info=romInfo; info->attr_type!=END_LIST; info++)
{
if(info->item->off && info->item->len && strlen(info->label) > max_len) {
max_len=strlen(info->label);
}
}
if (!max_len) { return -1; }
for(info=romInfo; info->attr_type!=END_LIST; info++)
{
char *str_data;
InfoItem *item=info->item;
if(item->off == 0 || item->len == 0)
{
continue;
}
if(item->off+item->len >= ih->len)
{
printf("%s = INVALID OFFSET/LEN 0x%x/%d\n",info->label, item->off, item->len);
continue;
}
str_data=malloc(item->len+1);
/* snprintf null terminates for us if string is too long :) */
snprintf(str_data, item->len+1, "%s", ih->d.s+item->off); // Leave room for null termination
printf(" %-*s : '%s'\n", max_len, info->label, str_data);
free(str_data);
}
return 0;
}
static int DoRomInfo(const struct ImageHandle *ih, struct section *osconfig)
{
uint32_t num_of;
int i, max_len=0;
if(ih == NULL) return(-1);
GetRomInfo(ih, osconfig);
if ((num_of = get_property_value(osconfig, "dumps", "dump_show", NULL))<=0)
{
return 0;
}
// Find the longest label so we know how big the label column should be
for(i=1;i<=num_of;i++)
{
char label_str[81];
const char * ptr_label;
snprintf(label_str, sizeof(label_str), "dump_%d_label", i);
ptr_label = get_property(osconfig, "dumps", label_str, NULL);
if(ptr_label) {
if(strlen(ptr_label)>max_len) {
max_len = strlen(ptr_label);
}
ptr_label=NULL;
}
}
printf("\nROM Dumps:\n");
//
// Dynamically walks through the config file and shows all properties defined...
//
for(i=1;i<=num_of;i++)
{
char type_str[81];
char visible_str[81];
char label_str[81];
char offset_str[81];
char length_str[81];
#ifdef DEBUG_ROM_INFO
const char * ptr_type;
#endif
const char * ptr_visible;
const char * ptr_label;
uint32_t ptr_offset;
uint32_t ptr_length;
snprintf(type_str, sizeof(type_str), "dump_%d_type", i);
snprintf(visible_str,sizeof(visible_str), "dump_%d_visible",i);
snprintf(label_str, sizeof(label_str), "dump_%d_label", i);
snprintf(offset_str, sizeof(offset_str), "dump_%d_offset", i);
snprintf(length_str, sizeof(length_str), "dump_%d_len", i);
// get config out of ini file...
#ifdef DEBUG_ROM_INFO
ptr_type = get_property( osconfig, "dumps", type_str, NULL);
#endif
ptr_visible = get_property( osconfig, "dumps", visible_str, NULL);
ptr_label = get_property( osconfig, "dumps", label_str, NULL);
ptr_offset = get_property_value( osconfig, "dumps", offset_str, NULL);
ptr_length = get_property_value( osconfig, "dumps", length_str, NULL);
if(ptr_length == 0)
{
// zero length, skip
}
else if(ptr_offset+ptr_length >= ih->len)
{
printf("%s = INVALID OFFSET/LEN 0x%x/%d\n",ptr_label, ptr_offset, ptr_length);
}
else
{
char str_data[1024];
// restrict maximum dump to 1kbyte [buffer size]
if(ptr_length > sizeof(str_data) - 1) ptr_length = sizeof(str_data) - 1; // Leave room for null termination
DEBUG_ROM("\n%s = %s\n",type_str, ptr_type);
DEBUG_ROM("%s = %s\n",visible_str, ptr_visible);
DEBUG_ROM("%s = '%s'\n",label_str, ptr_label);
DEBUG_ROM("%s = 0x%x\n",offset_str, ptr_offset);
DEBUG_ROM("%s = %d\n",length_str, ptr_length);
/* snprintf null terminates for us if string is too long :) */
snprintf(str_data, sizeof(str_data), "%s", ih->d.s+ptr_offset);
if(! strcmp("true",ptr_visible))
{
printf(" %-*s : '%s'\n", max_len, ptr_label, str_data);
}
else
{
printf(" %-*s = 'HIDDEN'\n", max_len, ptr_label);
}
}
}
return 0;
}
/* NEEDLE/HAYSTACK util */
static int FindData(const struct ImageHandle *ih, const char *what,
const uint8_t *n, const uint8_t *m, int len, // needle, mask, len of needle/mask
int off_l, int off_h, // where to find hi/lo (short word offset into find array)
uint32_t *offset, size_t offset_len, // array to store discovered offsets, len of array
uint32_t *where) // address of match (ONLY if single match), NULL if not needed
{
/* Note that off_l and off_h are SHORT WORD offsets, i.e. 1 == 2 bytes */
int i, found=0;
uint32_t last_where=0;
assert((len&1)==0); // make sure its even
for(i=0;i+len<ih->len;i+=2)
{
i=search_image(ih, i, n, m, len, 2);
if (i<0) break;
else {
int high_shift=16;
uint16_t low=le16toh(ih->d.u16[i/2+off_l]);
uint16_t high=le16toh(ih->d.u16[i/2+off_h]);
uint32_t addr;
/* maybe segment address */
if (high&0xfe00) high_shift=14;
addr=(high<<high_shift) | low;
if (Verbose>1) {
printf(" Found possible %s #%d at 0x%x (from 0x%x)\n",
what, found+1, addr, i);
}
if (addr>Config.base_address && addr-Config.base_address<ih->len) {
if (Verbose>2) {
hexdump(ih->d.u8+i-4, 4, " [");
hexdump(ih->d.u8+i, len, "] ");
hexdump(ih->d.u8+i+1, 4, "\n");
}
if(found<offset_len)
{
offset[found]=addr-Config.base_address;
last_where = i;
}
found++;
} else if (Verbose>1) {
printf(" %s #%d at 0x%x (from 0x%x) out of range\n",
what, found+1, addr, i);
}
}
}
if (found==1 && where) *where=last_where;
return found;
}
//
// Calculate the Bosch Motronic ME71 checksum for the given range
//
static uint32_t CalcChecksumBlk8(const struct ImageHandle *ih, const struct Range *r)
{
uint32_t nChecksum = 0, nIndex;
for(nIndex = r->start; nIndex <= r->end; nIndex++)
{
nChecksum+=le16toh(ih->d.u8[nIndex]);
}
return nChecksum;
}