This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
densoecu.cpp
executable file
·1659 lines (1489 loc) · 41.6 KB
/
densoecu.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
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
#include "StdAfx.h"
#include ".\densoecu.h"
densoecu::densoecu(string portname)
{
model = wrx2002;
dc = new densocomm(portname);
rom = new unsigned char[szrom];
page4k = new unsigned char[szpage4k];
for (int i = 0; i < szrom; i++)
rom[i] = 0;
baudrate = 0;
set_state(normal);
listenmode = false;
l = new log();
lastrsp = 0;
doecusim = false;
kernel_flash_enabled = false;
validate_len = 0;
validate_addr = locrambase;
}
densoecu::~densoecu(void)
{
delete dc;
delete[] rom;
delete[] page4k;
}
void densoecu::addlog(log *_l)
{
delete l;
l = _l;
dc->addlog(l);
}
void densoecu::listenonly(bool _listenmode)
{
listenmode = _listenmode;
}
void densoecu::set_ecu_sim_mode(bool _doecusim)
{
doecusim = _doecusim;
}
void densoecu::set_model(ecumodel _model)
{
model = _model;
set_state(normal);
}
void densoecu::set_state(ecustate _state)
{
int newbaudrate = get_baudrate_for_state(_state);
if (newbaudrate != baudrate)
{
baudrate = newbaudrate;
delay(100); // give buffers time to flush out
dc->set_baud(baudrate);
}
state = _state;
}
ecustate densoecu::get_state()
{
return state;
}
int densoecu::get_baudrate_for_state(ecustate _state)
{
switch(model)
{
case wrx2002:
switch(_state)
{
case normal:
default:
return 4800;
case bootloader:
case bootloader_auth:
return 9600;
case oecukernel:
return 39473;
}
break;
case wrx2004:
case sti2004:
case fxt2004:
default:
switch(_state)
{
case normal:
case bootloader_auth:
default:
return 4800;
case bootloader:
return 15625;
case oecukernel:
return 39473;
}
break;
}
}
bool densoecu::open()
{
if (dc->open())
{
if (doecusim || model != wrx2002)
dc->LE_high();
else
dc->LE_low();
return true;
}
return false;
}
void densoecu::close()
{
dc->close();
}
bool densoecu::listen()
{
switch(model)
{
case wrx2002:
switch(state)
{
case normal:
return listen_denso();
break;
case bootloader_auth:
case bootloader:
return listen_denso02_bootloader();
break;
case oecukernel:
return listen_kernel();
break;
default:
return false;
}
break;
case wrx2004:
case sti2004:
case fxt2004:
default:
switch(state)
{
case normal:
case bootloader_auth:
return listen_denso();
break;
case bootloader:
return listen_denso_bootloader();
break;
case oecukernel:
return listen_kernel();
break;
default:
return false;
}
break;
}
}
bool densoecu::kernel_verify_command_length(int datalen,int expectedlen)
{
if (datalen == expectedlen)
return true;
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_error_bad_data_length,NULL,0);
return false;
}
bool densoecu::listen_kernel()
{
unsigned char rsp;
int datalen = szrxbuf;
int i,addr,len;
unsigned int crc;
if (!dc->read_kernel_rsp_varlen(&rsp,rxbuf,&datalen))
return false;
dump(rsp,rxbuf,datalen);
// todo emulate kernel
switch(rsp)
{
case kernel_cmd_get_version_info:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel get version info\n");
strcpy((char*)rxbuf,"OpenECU Kernel V0.01");
dc->write_kernel_rsp(kernel_rsp_get_version_info,rxbuf,(int)strlen((char*)rxbuf));
break;
case kernel_rsp_get_version_info:
l->print(log_densoecu,"kernel response version [%s]\n",rxbuf);
break;
case kernel_cmd_CRC_area:
if (!kernel_verify_command_length(datalen,5))
break;
addr = byte_to_int24(rxbuf);
len = byte_to_int16(rxbuf+3);
l->print(log_densoecu,"kernel crc32 area addr: %06X len %04X\n",addr,len);
crc = crc32(rom+addr,len);
l->print(log_densoecu,"crc32 result: %08X\n",crc);
int32_to_byte(rxbuf,crc);
dc->write_kernel_rsp(kernel_rsp_CRC_area,rxbuf,4);
break;
case kernel_rsp_CRC_area:
if (!kernel_verify_command_length(datalen,4))
break;
crc = byte_to_int32(rxbuf);
l->print(log_densoecu,"kernel response crc32: %08X\n",crc);
break;
case kernel_cmd_read_area:
if (!kernel_verify_command_length(datalen,5))
break;
addr = byte_to_int24(rxbuf);
len = byte_to_int16(rxbuf+3);
l->print(log_densoecu,"kernel read area addr: %06X len %04X\n",addr,len);
dc->write_kernel_rsp(kernel_rsp_read_area,rom+addr,len);
break;
case kernel_rsp_read_area:
l->print(log_densoecu,"kernel response read area bytes: %04X\n",datalen);
break;
case kernel_cmd_read_programming_voltage:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel read programming voltage\n");
int16_to_byte(rxbuf,12*50);
dc->write_kernel_rsp(kernel_rsp_read_programming_voltage,rxbuf,2);
break;
case kernel_rsp_read_programming_voltage:
if (!kernel_verify_command_length(datalen,2))
break;
l->print(log_densoecu,"kernel response programming voltage: %2.2lfV\n",byte_to_int16(rxbuf)/50.0);
break;
case kernel_cmd_flash_enable:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel flash enable\n");
kernel_flash_enabled = true;
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_flash_enable,NULL,0);
break;
case kernel_rsp_flash_enable:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel response flash enabled\n");
break;
case kernel_cmd_flash_disable:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel flash disnable\n");
kernel_flash_enabled = false;
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_flash_disable,NULL,0);
break;
case kernel_rsp_flash_disable:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel response flash disabled\n");
break;
case kernel_cmd_write_flash_buffer:
if (datalen < 4)
{
l->print(log_densoecu,"kernel write flash buffer data length invalid!\n");
break;
}
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"kernel write flash buffer addr:%06X len:%02X\n",addr,datalen-3);
addr = addr % szpage4k;
if (addr+datalen-3 > szpage4k)
{
l->print(log_densoecu,"simulation error!\n");
}
else
{
memcpy(page4k+addr,rxbuf+3,datalen-3);
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_write_flash_buffer,NULL,0);
}
break;
case kernel_rsp_write_flash_buffer:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel response write flash buffer OK\n");
break;
case kernel_cmd_commit_flash_buffer:
if (!kernel_verify_command_length(datalen,7))
break;
addr = byte_to_int24(rxbuf);
crc = byte_to_int32(rxbuf+3);
l->print(log_densoecu,"kernel commit flash buffer addr:%06X crc:%08X\n",addr,crc);
// verify crc
if (crc != crc32(page4k,szpage4k))
{
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_error_bad_crc,NULL,0);
break;
}
// do the full simulation of writing to flash
for (i = 0; i < szpage4k; i++)
{
rom[addr+i] &= page4k[i]; // Vpp only turns 1's into 0's
// todo: delay
if (rom[addr+i] != page4k[i] || !kernel_flash_enabled)
{
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_error_programming_failure,NULL,0);
break;
}
}
if (!listenmode)
{
delay(330);
rxbuf[0] = 1; // flashed in 1 attempt
dc->write_kernel_rsp(kernel_rsp_commit_flash_buffer,rxbuf,1);
}
break;
case kernel_rsp_commit_flash_buffer:
if (!kernel_verify_command_length(datalen,1))
break;
l->print(log_densoecu,"kernel response commit flash buffer OK (%d pulses)\n",rxbuf[0]);
break;
case kernel_cmd_validate_flash_buffer:
if (!kernel_verify_command_length(datalen,4))
break;
crc = byte_to_int32(rxbuf);
l->print(log_densoecu,"kernel validate flash buffer crc:%08X\n",crc);
// verify crc
if (crc != crc32(page4k,szpage4k))
{
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_error_bad_crc,NULL,0);
break;
}
if (!listenmode)
{
delay(330);
dc->write_kernel_rsp(kernel_rsp_validate_flash_buffer,NULL,0);
}
break;
case kernel_rsp_validate_flash_buffer:
if (!kernel_verify_command_length(datalen,0))
break;
l->print(log_densoecu,"kernel response validate flash buffer OK )\n");
break;
case kernel_cmd_blank_16k_page:
if (!kernel_verify_command_length(datalen,3))
break;
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"kernel blank page addr:%06X \n",addr);
if (kernel_flash_enabled)
memset(rom+addr,0xFF,szpage);
if (!listenmode)
{
delay(330);
if (!kernel_flash_enabled)
dc->write_kernel_rsp(kernel_rsp_error_programming_failure,NULL,0);
else
{
rxbuf[0] = 1; // blanked in 1 attempt
dc->write_kernel_rsp(kernel_rsp_blank_16k_page,rxbuf,1);
}
}
break;
case kernel_rsp_blank_16k_page:
if (!kernel_verify_command_length(datalen,1))
break;
l->print(log_densoecu,"kernel response blank page OK (%d pulses)\n",rxbuf[0]);
break;
default:
if (!listenmode)
dc->write_kernel_rsp(kernel_rsp_error_bad_command,NULL,0);
break;
}
return true;
}
void densoecu::compress_mem_map(int *addr)
{
if (*addr >= 0x28000)
*addr -= 0x8000;
}
void densoecu::expand_mem_map(int *addr)
{
if (*addr >= 0x20000)
*addr += 0x8000;
}
bool densoecu::listen_denso02_bootloader()
{
unsigned char rsp;
unsigned int datalen = 1;
int checksum = 0;
dc->set_default_timeout(3000);
if (!dc->read_serial_bytes(&rsp,&datalen))
return false;
checksum += rsp;
int addr,len;
switch(rsp)
{
case denso02_bootloader_cmd_start:
l->print(log_densoecu,"denso02_bootloader_cmd_start\n");
datalen = 1;
if (!dc->read_serial_bytes(&rsp,&datalen))
return false;
checksum += rsp;
// verify checksum
datalen = 1;
if (!dc->read_serial_bytes(&rsp,&datalen))
return false;
if (rsp != 0x100-(checksum & 0xFF))
{
l->print(log_densoecu,"bad checksum.\n");
return false;
}
rxbuf[0] = 0x00;
if (state == bootloader_auth)
{
if (!dc->write_denso02_cmd(denso02_bootloader_cmd_start,rxbuf,1))
return false;
set_state(bootloader);
}
break;
case denso02_bootloader_cmd_write_kernel_ram:
// read address and length
datalen = 5;
if (!dc->read_serial_bytes(rxbuf,&datalen))
{
l->print(log_densoecu,"denso write kernel area timeout!");
return false;
}
checksum += dc->checksum(rxbuf,datalen);
addr = byte_to_int24(rxbuf);
len = byte_to_int16(rxbuf+3);
l->print(log_densoecu,"denso write kernel area addr:%06X len:%02X\n",addr,len);
if (addr < locrambase || addr+len > locrambase + 0x1800)
{
l->print(log_densoecu,"bad address range.\n");
rsp = denso02_bootloader_rsp_bad_address;
if (!dc->write_raw(&rsp,1))
return false;
}
/*
// todo: verify kernel length, return 0x22 if overrun
if (len > datalen - 5)
{
if (!dc->write_raw(densocomm::denso02_bootloader_rsp_data_overrun,1))
return false;
}
*/
// now read data block
datalen = len;
if (!dc->read_serial_bytes(rxbuf,&datalen))
return false;
checksum += dc->checksum(rxbuf,datalen);
// verify checksum
datalen = 1;
if (!dc->read_serial_bytes(&rsp,&datalen))
return false;
if (rsp != 0x100-(checksum & 0xFF))
{
l->print(log_densoecu,"bad checksum.\n");
// todo: send 0x03
rsp = denso02_bootloader_rsp_bad_checksum;
if (!dc->write_raw(&rsp,1))
return false;
return false;
}
transform_kernel_block02(rxbuf,len,0,false);
memcpy(rom+addr,rxbuf,len);
// verify valid kernel
if (byte_to_int16(rom+locrambase+2) != denso_magic_number)
{
// invalid_kernel magic number
l->print(log_densoecu,"invalid magic number.\n");
rsp = denso02_bootloader_rsp_bad_magic_number;
if (!dc->write_raw(&rsp,1))
return false;
}
l->print(log_densoecu,"valid kernel loaded.\n");
delay(100);
set_state(oecukernel);
break;
default:
l->print(log_densoecu,"unk [%02X]\n",rsp);
}
return true;
}
bool densoecu::listen_denso()
{
unsigned char rsp;
int datalen = szrxbuf;
if (model == wrx2002 && state == normal)
{
// look for 200ms pulse DSR/DTE (for Denso init sequence)
if (!dc->is_LE_high())
{
for (int d = 0; d < 300; d+=10)
{
if (dc->is_LE_high())
break;
delay(10);
}
if (dc->is_LE_high())
{
l->print(log_densoecu,"LE low pulse\n");
// correctly signalled
set_state(bootloader_auth);
return true;
}
}
}
if (model == wrx2002 && state == normal)
dc->set_default_timeout(100);
if (!dc->read_denso_rsp_varlen(&rsp,rxbuf,&datalen))
{
dc->set_default_timeout(3000);
return false;
}
dc->set_default_timeout(3000);
int addr;
dump(rsp,rxbuf,datalen);
switch(rsp)
{
case denso_write_ram:
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"denso write ram addr:%06X len:%02X\n",addr,datalen-3);
if (addr < locrambase || addr+datalen-3 > szram+locrambase)
respond_denso_fail();
else
{
memcpy(&rom[addr-locrambase],&rxbuf[3],datalen-3);
if (!listenmode)
dc->write_denso_rsp(denso_rsp_write_ram,&rxbuf[3],datalen-3);
}
break;
case denso_rsp_write_ram:
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"denso response write ram addr:%06X len:%02X\n",addr,datalen);
break;
case denso_reset_trouble_codes:
l->print(log_densoecu,"denso reset trouble codesh\n");
respond_denso_ok();
break;
case denso_rsp_reset_trouble_codes:
l->print(log_densoecu,"denso response reset trouble codesh\n");
break;
case denso_read_param:
l->print(log_densoecu,"denso read param %02x\n",rxbuf[0]);
if (!listenmode)
{
// fake params for now
//case param_boost:
rxbuf[0] = 77;
dc->write_denso_rsp(denso_rsp_read_param,rxbuf,1);
}
break;
case denso_rsp_read_param:
l->print(log_densoecu,"denso response read param %02X : %02X\n",rxbuf[0],rxbuf[1]);
break;
case denso_0x81:
l->print(log_densoecu,"denso command 0x81\n");
if (!listenmode)
{
rxbuf[0] = 0xEF;
rxbuf[1] = 0x8F;
dc->write_denso_rsp(denso_rsp_0x81,rxbuf,2);
}
break;
case denso_rsp_0x81:
l->print(log_densoecu,"denso response 0xC1 %02X %02X\n",rxbuf[0],rxbuf[1]);
break;
case denso_0x83:
l->print(log_densoecu,"denso command 0x83 %02X\n",rxbuf[0]);
if (!listenmode)
{
rxbuf[0] = 0x00;
rxbuf[1] = 0x00;
rxbuf[2] = 0xFF;
rxbuf[3] = 0x00;
rxbuf[4] = 0xFF;
rxbuf[5] = 0x00;
dc->write_denso_rsp(denso_rsp_0x83,rxbuf,6);
}
break;
case denso_0x30:
respond_denso_ok();
break;
case denso_0x10:
l->print(log_densoecu,"denso reflash state\n");
if (state == bootloader_auth)
{
respond_denso_ok();
// now we should change into reflash state,
set_state(bootloader);
}
else
respond_denso_fail();
break;
case denso_cmd_challenge:
switch(rxbuf[0])
{
case 1: // get challenge number
l->print(log_densoecu,"denso get challenge\n");
if (!listenmode)
{
rxbuf[1] = rand() % 256;
rxbuf[2] = rand() % 256;
rxbuf[3] = rand() % 256;
rxbuf[4] = rand() % 256;
memcpy(lastchallenge,rxbuf+1,4);
crazy_transform(lastchallenge);
dc->write_denso_rsp(denso_rsp_challenge,rxbuf,5);
}
break;
case 2: // responding to challenge
l->print(log_densoecu,"denso respond to challenge %02X %02X %02X %02X\n",rxbuf[1],rxbuf[2],rxbuf[3],rxbuf[4]);
if (!listenmode)
{// test validity
if (memcmp(lastchallenge,rxbuf+1,4) == 0)
{
l->print(log_densoecu,"denso challenge is correct\n");
rxbuf[1] = 0x34;
dc->write_denso_rsp(denso_rsp_challenge,rxbuf,2);
set_state(bootloader_auth);
}
else
{
l->print(log_densoecu,"denso challenge is incorrect\n");
l->print(log_densoecu,"correct response is %02X %02X %02X %02X\n",lastchallenge[0],lastchallenge[1],lastchallenge[2],lastchallenge[3]);
respond_denso_fail();
}
}
break;
}
case denso_rsp_challenge:
l->print(log_densoecu,"denso response challenge\n");
break;
}
lastrsp = rsp;
return true;
}
bool densoecu::listen_denso_bootloader()
{
unsigned char rsp;
int datalen = szrxbuf;
if (!dc->read_denso_rsp_varlen(&rsp,rxbuf,&datalen))
{
dc->set_default_timeout(3000);
return false;
}
dc->set_default_timeout(3000);
int addr;
dump(rsp,rxbuf,datalen);
switch(rsp)
{
case denso_bootloader_cmd_write_kernel_ram:
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"denso write flash addr:%06X len:%02X\n",addr,datalen-3);
if (!listenmode)
{
if (addr < validate_addr
|| addr+datalen-3 > validate_addr+validate_len
|| addr+datalen-3 > szram+locrambase)
respond_denso_fail();
else
{
transform_kernel_block04(rxbuf+3,datalen-3,false);
memcpy(rom+addr,rxbuf+3,datalen-3);
dc->write_denso_rsp(denso_bootloader_rsp_write_kernel_ram,NULL,0);
}
}
break;
case denso_bootloader_rsp_write_kernel_ram:
addr = byte_to_int24(rxbuf);
l->print(log_densoecu,"denso response write flash addr:%06X len:%02X\n",addr,datalen);
break;
case denso_bootloader_cmd_enter_kernel:
l->print(log_densoecu,"denso enter kernel\n");
// todo: verify flash mode
// verify correct parameters
if (datalen != 2 || byte_to_int16(rxbuf) != denso_bootloader_rsp_ok)
{
l->print(log_densoecu,"enter kernel: invalid parameters.\n");
// this actually uses bad checksum error code
int16_to_byte(rxbuf,denso_bootloader_rsp_error_bad_checksum);
dc->write_denso_rsp_echocheck(denso_bootloader_rsp_error,rxbuf,2);
}
// verify valid kernel
else if (bootloader_calculate_odd_checksum(rom+validate_addr,validate_len) != denso_odd_checksum)
{
// invalid_kernel magic number
l->print(log_densoecu,"invalid checksum.\n");
int16_to_byte(rxbuf,denso_bootloader_rsp_error_bad_checksum);
dc->write_denso_rsp_echocheck(denso_bootloader_rsp_error,rxbuf,2);
}
else
{
l->print(log_densoecu,"valid kernel loaded.\n");
int16_to_byte(rxbuf,denso_bootloader_rsp_ok);
dc->write_denso_rsp_echocheck(denso_bootloader_rsp_enter_kernel,rxbuf,2);
// now we should change into kernel state,
set_state(oecukernel);
}
break;
case denso_bootloader_rsp_enter_kernel:
if (datalen == 2)
l->print(log_densoecu,"denso response enter kernel: [%02X} [%02X]\n",rxbuf[0],rxbuf[1]);
break;
case denso_bootloader_validate_kram_addr:
validate_addr = byte_to_int24(rxbuf);
validate_len = byte_to_int16(&rxbuf[5]);
l->print(log_densoecu,"denso validate flash addr:%06X len:%04X\n",validate_addr,validate_len);
if (lastrsp != denso_0x10 && !listenmode)
{
rxbuf[0] = 0x84;
dc->write_denso_rsp(denso_bootloader_rsp_validate_kram_addr,rxbuf,1);
}
break;
case denso_bootloader_rsp_validate_kram_addr:
l->print(log_densoecu,"denso response blank flash %02X\n",rxbuf[0]);
break;
}
lastrsp = rsp;
return true;
}
int densoecu::byte_to_int32(unsigned char *data)
{
return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
}
int densoecu::byte_to_int24(unsigned char *data)
{
return (data[0] << 16) + (data[1] << 8) + data[2];
}
int densoecu::byte_to_int16(unsigned char *data)
{
return (data[0] << 8) + data[1];
}
void densoecu::int16_to_byte(unsigned char *data,int i)
{
data[0] = i >> 8;
data[1] = i & 0xFF;
}
void densoecu::int24_to_byte(unsigned char *data,int i)
{
data[0] = i >> 16;
data[1] = (i >> 8) & 0xFF;
data[2] = i & 0xFF;
}
void densoecu::int32_to_byte(unsigned char *data,int i)
{
data[0] = i >> 24;
data[1] = (i >> 16) & 0xFF;
data[2] = (i >> 8) & 0xFF;
data[3] = i & 0xFF;
}
bool densoecu::respond_denso_ok()
{
if (listenmode)
return true;
return dc->write_denso_rsp(denso_rsp_ok,NULL,0);
}
bool densoecu::respond_denso_fail()
{
if (listenmode)
return true;
return dc->write_denso_rsp(denso_rsp_fail,NULL,0);
}
void densoecu::delay(int ms)
{
if (listenmode)
return; // don't do delays if listening
Sleep(ms);
}
void densoecu::dump(unsigned char rsp,unsigned char *data,int datalen)
{
int szbuf = datalen*4+64;
char *buf = new char[szbuf];
char *pbuf = buf;
pbuf += _snprintf(pbuf,szbuf-(pbuf-buf),"[%02X] ",rsp);
for (int i = 0; i < datalen; i++)
{
if (i > 15 && (i & 0xF) == 0)
pbuf += _snprintf(pbuf,szbuf-(pbuf-buf),"\n ");
pbuf += _snprintf(pbuf,szbuf-(pbuf-buf),"%02X ",data[i]);
}
pbuf += _snprintf(pbuf,szbuf-(pbuf-buf),"\n");
l->print(log_densoecu,buf);
delete buf;
}
bool densoecu::load_ram_from_file(string filename)
{
FILE *fp;
if (NULL == (fp = fopen(filename.c_str(),"rb")))
{
l->print(log_densoecu,"can't open ram input file.\n");
return false;
}
fseek(fp,0,SEEK_END);
int filesize = min(szram,ftell(fp));
fseek(fp,0,SEEK_SET);
if (filesize != fread(rom+locrambase,1,filesize,fp))
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
bool densoecu::save_ram_to_file(string filename)
{
FILE *fp;
if (NULL == (fp = fopen(filename.c_str(),"wb")))
{
l->print(log_densoecu,"can't open ram output file.\n");
return false;
}
if (szram != fwrite(rom+locrambase,1,szram,fp))
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
bool densoecu::load_rom_from_file(string filename)
{
FILE *fp;
if (NULL == (fp = fopen(filename.c_str(),"rb")))
{
l->print(log_densoecu,"can't open rom input file.\n");
return false;
}
fseek(fp,0,SEEK_END);
int filesize = min(szrom,ftell(fp));
fseek(fp,0,SEEK_SET);
if (filesize != fread(rom,1,filesize,fp))
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
bool densoecu::save_rom_to_file(string filename)
{
FILE *fp;
if (NULL == (fp = fopen(filename.c_str(),"wb")))
{
l->print(log_densoecu,"can't open rom output file.\n");
return false;
}
if (szrom != fwrite(rom,1,szrom,fp))
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
void densoecu::crazy_transform(unsigned char *data)
{
int table2[16] =
{
0x53DA, 0x33BC, 0x72EB, 0x437D,
0x7CA3, 0x3382, 0x834F, 0x3608,
0xAFB8, 0x503D, 0xDBA3, 0x9D34,
0x3563, 0x6B70, 0x6E74, 0x88F0
};
int loword = byte_to_int16(&data[2]);
int hiword = byte_to_int16(&data[0]);
int num;
for (int i = 15; i >= 0; i--)
{
num = table2[i];
num ^= loword;
num = transformnybbles(num);
num = ((num >> 3) + (num << 13)) & 0xFFFF; // barrel shift right 3
num ^= hiword;
hiword = loword;
loword = num;
}
// words reversed in output for some reason
int16_to_byte(&data[2],hiword);
int16_to_byte(&data[0],loword);
}
int densoecu::transformnybbles(int n)
{
int table1[32] =
{
0x5, 0x6, 0x7, 0x1,
0x9, 0xC, 0xD, 0x8,
0xA, 0xD, 0x2, 0xB,
0xF, 0x4, 0x0, 0x3,
0xB, 0x4, 0x6, 0x0,
0xF, 0x2, 0xD, 0x9,
0x5, 0xC, 0x1, 0xA,
0x3, 0xD, 0xE, 0x8
};
int result = 0;
// fix n for simulated wrap-around
n += (n & 0xFF) << 16;
for (int i = 0; i < 4; i++)
{
result += (table1[(n >> (i*4)) % 32]) << (i*4);
}
return result;
}
bool densoecu::write_denso_ram_area(unsigned char *data,int addr,int len)
{
l->print(log_densoecu,"writing denso ram area addr: %06X len: %04X\n",addr,len);
delay(200);
for (int i = 0; i < len;)
{
int blksz = min(len-i,64);
if (!write_denso_ram_block(&data[i],addr+i,blksz))
return false;