forked from nyetwurk/ME7Sum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
me7sum.c
1114 lines (936 loc) · 29.3 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 <errno.h>
#include <ctype.h> /* isprint() */
#include "os/os.h"
#include "inifile_prop.h"
#include "crc32.h"
#include "utils.h"
//#define DEBUG_ROM_INFO
//#define DEBUG_CRC_MATCHING
//#define DEBUG_MAIN_MATCHING
//#define DEBUG_MULTIPOINT_MATCHING
#include "debug.h"
// structures
struct Range {
uint32_t start;
uint32_t end;
};
struct ChecksumPair {
uint32_t v; // value
uint32_t iv; // inverse value
};
struct MultipointDescriptor {
struct Range r;
struct ChecksumPair csum;
};
#define MAX_CRC_BLKS 4
// main firmware checksum validation
struct rom_config {
int readonly;
uint32_t base_address; /* rom base address */
uint32_t multipoint_block_start[2]; /* start of multipoint block descriptors (two sets, first one isn't always there) */
uint32_t multipoint_block_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 */
};
struct info_config {
InfoItem EPK;
InfoItem part_number;
InfoItem engine_id;
InfoItem sw_version;
InfoItem hw_number;
InfoItem sw_number;
};
// globals
static struct rom_config Config;
static struct info_config InfoConfig;
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_block_len, "ignition", "rom_checksum_block_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 GetRomDump(const struct ImageHandle *ih, struct section *osconfig);
static int GetRomInfo(const struct ImageHandle *ih, struct section *osconfig);
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);
static int FindMainRomOffset(const struct ImageHandle *ih);
static int FindMainRomFinal(const struct ImageHandle *ih);
static int DoMainChecksum(struct ImageHandle *ih, uint32_t nOffset, uint32_t nCsumAddr);
static int FindChecksumBlks(const struct ImageHandle *ih, int which);
static int DoChecksumBlk(struct ImageHandle *ih, uint32_t nStartBlk);
/*
* main()
*
*/
static void usage(const char *prog)
{
printf("Usage: %s [-i <config.ini>] <inrom.bin> [outrom.bin]\n", prog);
exit(-1);
}
int main(int argc, char **argv)
{
int iTemp;
char *prog=argv[0];
char *inifile=NULL;
char *input=NULL;
char *output=NULL;
int i, c;
struct ImageHandle ih;
struct section *osconfig=NULL;
// information about the tool
printf("ME7Tool (%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\n");
opterr=0;
while ((c = getopt(argc, argv, "i:")) != -1)
{
switch (c)
{
case 'i':
inifile=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;
}
}
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 (inifile)
{
printf("Attemping to open firmware config file '%s'\n",inifile);
// load properties file into memory
osconfig = read_properties(inifile);
if(osconfig == NULL)
{
printf("failed to open config file\n");
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("\nAttemping to open firmware file '%s'\n",input);
if (iload_file(&ih, input, 0))
{
printf("failed to open firmware file '%s'\n",input);
goto out;
}
// sanity check: validate firmware file is at least 512kbytes length before proceeding.
if(ih.len < (1024*512))
{
printf("File too small. Are you sure this is a firmware dump?\n");
goto out;
}
//
// Step #0 Show interesting ROM information
//
GetRomInfo(&ih, osconfig);
GetRomDump(&ih, osconfig);
DEBUG_EXIT_ROM;
//
// Step #1 Main ROM CRCs if specified
//
printf("\nStep #1: Reading main ROM CRC...\n");
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);
}
if(Config.crc[1].r.start && Config.crc[1].r.end && Config.crc[1].offset)
{
DoMainCRCs(&ih);
}
else
{
printf("\nStep #1: ERROR! Skipping main ROM CRCs... UNDEFINED\n");
ErrorsUncorrectable++;
}
DEBUG_EXIT_CRC;
//
// Step #2 Main ROM checksums
//
printf("\nStep #2: Reading main ROM checksum...\n");
if(Config.main_checksum_offset==0)
{
FindMainRomOffset(&ih);
}
if(Config.main_checksum_final==0)
{
FindMainRomFinal(&ih);
}
if (Config.main_checksum_offset && Config.main_checksum_final)
{
DoMainChecksum(&ih, Config.main_checksum_offset, Config.main_checksum_final);
}
else
{
printf("Step #2: ERROR! Skipping main ROM checksum... UNDEFINED\n");
ErrorsUncorrectable++;
}
DEBUG_EXIT_MAIN;
//
// Step #3 Multi point checksums
//
printf("\nStep #3: Reading Multipoint Checksum Blocks...\n");
for (i=0;i<2;i++) {
if(Config.multipoint_block_start[i]==0)
{
FindChecksumBlks(&ih, i);
}
if(Config.multipoint_block_start[i])
{
for(iTemp=0; iTemp<64; iTemp++)
{
int result=0;
printf("%2d) ",iTemp+1);
fflush(stdout);
result = DoChecksumBlk(&ih, Config.multipoint_block_start[i]+(Config.multipoint_block_len*iTemp));
if (result == 1) { break; } // end of blocks;
}
printf("[%d x <16> = %d bytes]\n", iTemp, iTemp*16);
}
else
{
if (i!=0) {
printf("Step #3: ERROR! Skipping Multipoint Checksum Block... UNDEFINED\n");
ErrorsUncorrectable++;
}
}
}
DEBUG_EXIT_MULTIPOINT;
if(ErrorsUncorrectable)
{
printf("\n*** ABORTING! %d uncorrectable error(s) in %s! ***\n", ErrorsUncorrectable, input);
return -1;
}
if(output && ErrorsCorrected > 0)
{
printf("\nAttemping to output corrected firmware file '%s'\n",output);
// write crc corrected file out
save_file(output,ih.d.p,ih.len);
}
out:
// close the file
if(ih.d.p != 0) { ifree_file(&ih); }
// free config
if(osconfig != 0) { free_properties(osconfig); }
printf("\n*** DONE! %d/%d errors corrected in %s! ***\n", ErrorsCorrected, ErrorsFound, input);
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; }
printf("\nROM Info:\n");
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 GetRomDump(const struct ImageHandle *ih, struct section *osconfig)
{
uint32_t num_of;
int i, max_len=0;
if(ih == NULL) return(-1);
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;
}
static int FindMainCRCData(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;
for(i=0;i+len<ih->len;i+=2)
{
i=search_image(ih, i, n, m, len, 2);
if (i<0) break;
if (i+len<ih->len)
{
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=(high<<16) | low;
if (addr>Config.base_address && addr-Config.base_address<ih->len) {
DEBUG_CRC("Found possible %s #%d at 0x%x (from 0x%x)\n", what, found+1, addr, i);
#ifdef DEBUG_CRC_MATCHING
hexdump(ih->d.u8+i-4, 4, " [");
hexdump(ih->d.u8+i, len, "] ");
hexdump(ih->d.u8+i+1, 4, "\n");
#endif
if(found<offset_len)
{
offset[found]=addr-Config.base_address;
last_where = i;
}
found++;
}
}
}
if (found==1 && where) *where=last_where;
return found;
}
static int FindMainCRCPreBlk(const struct ImageHandle *ih)
{
int found;
uint32_t offset=0;
uint32_t where=0;
// LL LL HH HH s
uint8_t needle[] = {0xE6, 0xFC, 0x00, 0x00, 0xE6, 0xFD, 0x00, 0x00, 0xE0, 0x0E, 0xDA, 0x00, 0x00, 0x00, 0xF6, 0xF4};
uint8_t mask[] = {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x0f, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff};
printf(" Searching for main ROM CRC pre block...");
DEBUG_FLUSH_CRC;
found=FindMainCRCData(ih, "CRC pre block", needle, mask, sizeof(needle), 1, 3, &offset, 1, &where);
if (found==1)
{
// crc0 is reserved for pre-region
Config.crc[0].r.start=offset;
Config.crc[0].r.end=offset+(ih->d.u8[where+9]>>4)-1;
DEBUG_CRC("Found %s #%d 0x%x-0x%x (0x%x): ", "CRC pre block", 0, offset, Config.crc[0].r.end, where);
printf("OK\n");
return 0;
}
if (found>1)
{
DEBUG_CRC("Too many matches (%d). CRC block start find failed\n", found);
}
printf("skipped\n");
return 0;
}
static int FindMainCRCBlks(const struct ImageHandle *ih)
{
int i, found, ret0=-1, ret1=-1;
uint32_t offset[MAX_CRC_BLKS];
// LL LL HH
uint8_t n0[] = {0xE6, 0xF8, 0x00, 0x00, 0xE6, 0xF9, 0x00, 0x00, 0xF2, 0xF4, 0x00, 0x00, 0x24, 0x8F};
uint8_t m0[] = {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff};
// LL LL HH
uint8_t n1[] = {0x10, 0x9B, 0xE6, 0xF4, 0x00, 0x00, 0xE6, 0xF5, 0x00, 0x00, 0x26, 0xF4};
uint8_t m1[] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff};
printf(" Searching for main ROM CRC blocks...");
DEBUG_FLUSH_CRC;
found=FindMainCRCData(ih, "CRC block starts", n0, m0, sizeof(n0), 1, 3, offset, MAX_CRC_BLKS, NULL);
if (found>0 && found<=MAX_CRC_BLKS)
{
for (i=0;i<found;i++)
{
DEBUG_CRC("Found %s #%d at 0x%x\n", "CRC block start", i+1, offset[i]);
// crc0 is reserved for pre-region
if (i<MAX_CRC_BLKS)
Config.crc[i+1].r.start=offset[i];
ret0=0;
}
}
if (found>MAX_CRC_BLKS)
{
DEBUG_CRC("Too many matches (%d). CRC block start find failed\n", found);
}
found=FindMainCRCData(ih, "CRC block end", n1, m1, sizeof(n1), 2, MAX_CRC_BLKS, offset, 4, NULL);
if (found>0 && found<=MAX_CRC_BLKS)
{
for (i=0;i<found;i++)
{
DEBUG_CRC("Found %s #%d at 0x%x\n", "CRC block end", i+1, offset[i]);
// crc0 is reserved for pre-region
if (i<MAX_CRC_BLKS)
Config.crc[i+1].r.end=offset[i];
ret1=0;
}
}
if (found>MAX_CRC_BLKS)
{
DEBUG_CRC("Too many matches (%d). CRC block end find failed\n", found);
}
if (ret0||ret1)
{
if (ih->len==512*1024)
{
DEBUG_CRC("No CRC regions detected. Falling back to default 512k CRC blocks\n");
Config.crc[1].r.start=0x10000;
Config.crc[1].r.end=0x13fff;
Config.crc[2].r.start=0x14300;
Config.crc[2].r.end=0x17f67;
Config.crc[3].r.start=0x18191;
Config.crc[3].r.end=0x1fbff;
ret0=ret1=0;
}
}
printf("%s\n", (ret0||ret1)?"FAIL":"OK");
return ret0 & ret1;
}
#ifdef DEBUG_CRC_MATCHING
#define MAX_CRC_OFFSETS 10
#else
#define MAX_CRC_OFFSETS MAX_CRC_BLKS
#endif
static int FindMainCRCOffsets(const struct ImageHandle *ih)
{
int i, found;
uint32_t offset[MAX_CRC_OFFSETS];
// LL LL HH HH
uint8_t needle[] = {0xF6, 0xF5, 0x00, 0x00, 0xE6, 0xF4, 0x00, 0x00, 0xE6, 0xF5, 0x00, 0x00, 0xDA, 0x00 /*, 0x00, 0x00, 0xe6, 0x00, 0x04, 0x02 */};
uint8_t mask[] = {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff /*, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff */};
printf(" Searching for main ROM CRC offsets...");
DEBUG_FLUSH_CRC;
found=FindMainCRCData(ih, "CRC offset", needle, mask, sizeof(needle), 3, 5, offset, MAX_CRC_OFFSETS, NULL);
if (found>0 && found<=MAX_CRC_OFFSETS)
{
for (i=0;i<found;i++)
{
DEBUG_CRC("Found CRC #%d at 0x%x\n", i+1, offset[i]);
// crc0 is reserved for pre-region
if (i<MAX_CRC_BLKS)
Config.crc[i+1].offset=offset[i];
}
}
if (found!=3)
{
DEBUG_CRC("Did not find exactly 3 matches (got %d). CRC offset find failed\n", found);
memset(Config.crc, 0, sizeof(Config.crc));
printf("FAIL\n");
return -1;
}
/* if we found exactly 3 crc values, and region 4 exists, use region 3's crc for calcing 3+4 */
if (Config.crc[4].r.start && Config.crc[4].r.end)
{
Config.crc[4].offset=Config.crc[3].offset;
Config.crc[3].offset=0;
}
printf("OK\n");
return 0;
}
static int DoMainCRCs(struct ImageHandle *ih)
{
int result=0;
int i;
uint32_t nCalcCRCSeed = 0;
for (i=0; i<5; i++)
{
if(Config.crc[i].r.start && Config.crc[i].r.end)
{
uint32_t nStart = Config.crc[i].r.start;
size_t nLen = Config.crc[i].r.end - Config.crc[i].r.start + 1;
uint32_t nCRCAddr = Config.crc[i].offset;
uint32_t nCRC;
uint32_t nCalcCRC;
uint32_t *p32;
nCalcCRC = crc32(nCalcCRCSeed, ih->d.u8+nStart, nLen);
printf(" %d) Adr: 0x%06X-0x%06X", i, Config.crc[i].r.start, Config.crc[i].r.end);
if (nCRCAddr+4>ih->len)
{
printf(" @%05x INVALID ADDRESS\n", nCRCAddr);
}
else if (nCRCAddr)
{
/* possibly unaligned, so we cant do tricks wtih ih->d.u32 */
p32=(uint32_t *)(ih->d.u8 + nCRCAddr);
nCRC=le32toh(*p32);
printf(" @%05x CRC: %08X CalcCRC: %08X%s", nCRCAddr, nCRC, nCalcCRC, nCalcCRCSeed?"(r)":" ");
if (nCalcCRC != nCRC)
{
ErrorsFound++;
if (Config.readonly)
{
printf(" ** NOT OK **\n");
result|=-1;
}
else
{
*p32=le32toh(nCalcCRC);
ErrorsCorrected++;
printf(" ** FIXED **\n");
}
}
else
{
printf(" CRC OK\n");
}
} else {
printf(" CalcCRC: %08X%s\n", nCalcCRC, nCalcCRCSeed?"(r)":" ");
}
if (Config.crc[0].r.start && Config.crc[0].r.end)
nCalcCRCSeed=nCalcCRC;
}
else
{
DEBUG_CRC(" %d) Adr: 0x%06X-0x%06X SKIPPED\n", i, Config.crc[0].r.start, Config.crc[0].r.end);
}
}
return result;
}
static int FindMainRomOffset(const struct ImageHandle *ih)
{
int i, found=0, offset=0;
uint32_t needle[4];
uint32_t mask[4];
printf(" Searching for main ROM checksum...");
DEBUG_FLUSH_MAIN;
needle[0]=htole32(Config.base_address);
needle[1]=htole32(Config.base_address+0x0f000);
needle[2]=htole32(Config.base_address+0x20000);
needle[3]=htole32(Config.base_address+0x7ffff);
mask[0]=htole32(0xffffffff);
mask[1]=htole32(0xfffff000);
mask[2]=htole32(0xffffffff);
mask[3]=htole32(0xfff7ffff);
for(i=0;i+sizeof(needle)<ih->len;i+=2)
{
i=search_image(ih, i, needle, mask, sizeof(needle), 2);
if (i<0) break;
if (i+sizeof(needle)<ih->len)
{
DEBUG_MAIN("Found possible main block descriptor at 0x%x\n", i);
offset=i;
found++;
}
}
if (found==1)
{
DEBUG_MAIN("Found main block descriptor at 0x%x\n", offset);
Config.main_checksum_offset=offset;
printf("OK\n");
return 0;
}
printf("FAIL\n");
return -1;
}
static int FindMainRomFinal(const struct ImageHandle *ih)
{
int offset=ih->len-0x20;
struct ChecksumPair *csum = (struct ChecksumPair *)(ih->d.u8+offset);
if (csum->v == ~csum->iv)
{
DEBUG_MAIN("Found main csum at 0x%x\n", offset);
Config.main_checksum_final=offset;
return 0;
}
return -1;
}
static int NormalizeRange(const struct ImageHandle *ih, struct Range *r)
{
// special case: leave end markers alone
if (r->start==0xffffffff && r->end==0xffffffff) return 0;
// We are only reading the ROM. Therefore the start address must be
// higher than ROMSTART. Ignore addresses lower than this and
// remove the offset for addresses we're interested in.
if (r->start < Config.base_address || r->end < Config.base_address) //ROMSTART)
{
// The checksum block is outside our range
printf(" ERROR: INVALID STARTADDDR/ENDADDR 0x%x/0x%x is less than base address 0x%x\n",
r->start, r->end, Config.base_address);
return -1;
}
r->start -= Config.base_address; //ROMSTART;
r->end -= Config.base_address; //ROMSTART;
if(r->start>r->end)
{
// start is after end!
printf(" ERROR: INVALID STARTADDDR/ENDADDR: 0x%x>0x%x\n", r->start, r->end);
return -1;
}
if(r->start>=ih->len || r->end>=ih->len)
{
// The checksum block is outside our range
printf(" ERROR: INVALID STARTADDDR/ENDADDR: 0x%x/0x%x is past 0x%x\n", r->start, r->end, (int)ih->len);
return -1;
}
return 0;
}
//
// Calculate the Bosch Motronic ME71 checksum for the given range
//
static uint32_t CalcChecksumBlk(const struct ImageHandle *ih, const struct Range *r)
{
uint32_t nChecksum = 0, nIndex;
for(nIndex = r->start/2; nIndex <= r->end/2; nIndex++)
{
nChecksum+=le16toh(ih->d.u16[nIndex]);
}
return nChecksum;
}
//
// Reads the main checksum for the whole ROM
//
static int DoMainChecksum(struct ImageHandle *ih, uint32_t nOffset, uint32_t nCsumAddr)
{
int errors=0;
struct Range r[2];
struct ChecksumPair csum;
uint32_t nCalcChksum;
uint32_t nCalcChksum2;
printf(" ROM Checksum Block Offset Table @%05x [16 bytes]:\n",
Config.main_checksum_offset);
// C16x processors are little endian
// copy from (le) buffer into our descriptor
memcpy_from_le32(r, ih->d.u8+nOffset, sizeof(r));
if (NormalizeRange(ih, r) || NormalizeRange(ih, r+1) ||
r[0].start==0xffffffff || r[1].start==0xffffffff)
{
printf(" ERROR! BAD MAIN CHECKSUM DESCRIPTOR(s)\n");
ErrorsUncorrectable++;
return -1;
}
// block 1
nCalcChksum = CalcChecksumBlk(ih, r);
printf(" 1) Adr: 0x%06X-0x%06X\n", r[0].start, r[0].end);
if (r[0].end + 1 != r[1].start)
{
printf(" 2) Adr: 0x%06X-0x%06X MAP REGION SKIPPED, NOT PART OF MAIN CHECKSUM\n",
r[0].end+1, r[1].start-1);
}
// block 2
nCalcChksum2= CalcChecksumBlk(ih, r+1);
printf(" 3) Adr: 0x%06X-0x%06X\n", r[1].start, r[1].end);
nCalcChksum += nCalcChksum2;
// C16x processors are little endian
// copy from (le) buffer
memcpy_from_le32(&csum, ih->d.u8+nCsumAddr, sizeof(csum));
printf(" @%05x Chksum : 0x%08X", Config.main_checksum_final, csum.v);
if(csum.v != ~csum.iv)
{
printf(" ~Chksum : 0x%08X INV NOT OK", csum.iv);
errors++;
}
printf(" CalcChk: 0x%08X", nCalcChksum);
if(csum.v != nCalcChksum) { errors++; }
if(!errors)
{
printf(" Main ROM checksum OK\n");
return 0;
}
ErrorsFound+=errors;
if(Config.readonly)
{
printf(" ** NOT OK **\n");
return -1;
}
csum.v = nCalcChksum;
csum.iv = ~nCalcChksum;
memcpy_to_le32(ih->d.u8+nCsumAddr, &csum, sizeof(csum));
printf(" ** FIXED! **\n");
ErrorsCorrected+=errors;
return 0;
}
/* which=0: MP block #1 */
/* which=1: MP block #2 */
static int FindChecksumBlks(const struct ImageHandle *ih, int which)
{
int i, found=0, offset=0;
uint32_t needle[2];
int size=0;
printf(" Searching for multipoint block descriptor #%d...",
which+1);
DEBUG_FLUSH_MULTIPOINT;
if (which==0) {
needle[0]=htole32(Config.base_address+0x24000);
/* actually, mp #1 isn't allowed to match this,
its in mp #2 */
needle[1]=htole32(Config.base_address+0x27fff);
size=4;
} else {
needle[0]=htole32(Config.base_address);
needle[1]=htole32(Config.base_address+0x3fff);
size=8;
}
for(i=0;i+Config.multipoint_block_len<ih->len;i+=2)
{
i=search_image(ih, i, needle, NULL, size, 2);
if (i<0) break;
if (i+Config.multipoint_block_len<ih->len)
{
struct MultipointDescriptor *desc =