-
Notifications
You must be signed in to change notification settings - Fork 8
/
GForm_Main.cs
2894 lines (2649 loc) · 121 KB
/
GForm_Main.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DarkUI.Controls;
using DarkUI.Forms;
using SAE.J2534;
public class GForm_Main : DarkForm
{
//bool ECU_Unlocked = false;
bool VehicleConnected = false;
private DarkButton darkButton2;
private OpenFileDialog openFileDialog1;
byte Unlocking_Mode = 0x41;
bool WritingBinaryMode = true; //if false we are in writing firmware mode, this is set later anyway
private DarkButton darkButton_FlashFW;
private GForm_Main GForm_Main_0;
private DarkGroupBox DarkgroupBox1;
private DarkButton darkButton4;
private DarkButton darkButton5;
private DarkButton darkButton6;
private DarkButton darkButton3;
public Editortable Editortable_0;
public string Version = "v1.1.7";
private DarkTextBox darkTextBoxJ2534Command;
private DarkLabel darkLabel1;
private DarkButton darkButtonJ2534Command;
private DarkComboBox darkComboBoxUnlockMode;
private bool BadResponceReceived = false;
public Class_Checksums Class_Checksums_0;
public System.Windows.Forms.Timer Timer1 = new System.Windows.Forms.Timer();
//public System.Windows.Forms.Timer TimerJ2534 = new System.Windows.Forms.Timer();
public API api;
public Device device;
public Channel channel;
public bool J2534Connected = false;
private int SelectedPlatformIndex = 0;
private DarkGroupBox darkGroupBox2;
private DarkCheckBox darkCheckBoxLogsCommands;
private IContainer components;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem clearLogsToolStripMenuItem;
public string LastFileOpenedEditor = "";
public GForm_Main()
{
this.Enabled = false;
this.InitializeComponent();
this.darkTextBox_0.Text = this.darkTextBox_0.Text + Environment.NewLine;
this.darkTextBox_0.Text = this.darkTextBox_0.Text + Environment.NewLine;
this.darkTextBox_0.Text = this.darkTextBox_0.Text + Environment.NewLine;
darkLabel_5.Text = "";
darkLabel_8.Text = "";
GForm_Main_0 = this;
Timer1.Interval = 1000;
Timer1.Tick += new EventHandler(TimerEventProcessor);
Timer1.Start();
//label1.Text = "";
//TimerJ2534.Interval = 1000;
//TimerJ2534.Tick += new EventHandler(TimerEventProcessorJ2534);
}
public void LoadSettings()
{
try
{
string TFilePath = Application.StartupPath + @"\Settings.txt";
if (File.Exists(TFilePath))
{
string[] AllLines = File.ReadAllLines(TFilePath);
for (int i = 0; i < AllLines.Length; i++)
{
if (AllLines[i].Contains("J2534AdapterName=") && AllLines[i][0] != '#') J2534AdapterName = AllLines[i].Split('=')[1];
if (AllLines[i].Contains("SelectedPlatformIndex=") && AllLines[i][0] != '#') SelectedPlatformIndex = int.Parse(AllLines[i].Split('=')[1]);
if (AllLines[i].Contains("LastFileOpenedEditor=") && AllLines[i][0] != '#') LastFileOpenedEditor = AllLines[i].Split('=')[1];
if (AllLines[i].Contains("LogsCommands=") && AllLines[i][0] != '#') darkCheckBoxLogsCommands.Checked = bool.Parse(AllLines[i].Split('=')[1]);
}
if (J2534AdapterName != "") LoadAdapter();
LoadPlatform();
}
}
catch (Exception ex)
{
this.method_1("--------------------------------------");
this.method_1("Could not load Settings.txt with error: " + Environment.NewLine + ex);
}
}
public void SaveSettings()
{
string SettingTxt = "";
SettingTxt = SettingTxt + "J2534AdapterName=" + J2534AdapterName + Environment.NewLine;
SettingTxt = SettingTxt + "SelectedPlatformIndex=" + SelectedPlatformIndex.ToString() + Environment.NewLine;
SettingTxt = SettingTxt + "LastFileOpenedEditor=" + LastFileOpenedEditor + Environment.NewLine;
SettingTxt = SettingTxt + "LogsCommands=" + darkCheckBoxLogsCommands.Checked.ToString() + Environment.NewLine;
string TFilePath = Application.StartupPath + @"\Settings.txt";
File.Create(TFilePath).Dispose();
File.WriteAllText(TFilePath, SettingTxt);
}
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
Timer1.Stop();
DarkMessageBox.Show(this, "To access the most lastest updates and features, purchase the software at:" + Environment.NewLine + "https://www.bmdevs-shop.com/", "Outdated!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Editortable_0 = new Editortable(ref GForm_Main_0);
Class_Checksums_0 = new Class_Checksums();
Class_Checksums_0.Load(ref GForm_Main_0);
Class_RWD.Load(ref GForm_Main_0);
this.Text = this.Text + " (" + Version + ")";
Editortable_0.ClassEditor_0.LoadSupportedECUDefinitions();
darkComboBoxUnlockMode.SelectedIndex = 0;
LoadSettings();
this.method_1("--------------------------------------");
this.Enabled = true;
}
/*private void TimerEventProcessorJ2534(Object myObject, EventArgs myEventArgs)
{
CheckConnected();
}*/
private void method_0(string string_3)
{
if (this.darkTextBox_0.InvokeRequired)
{
Delegate0 delegate_ = new Delegate0(this.method_0);
this.method_22(delegate_, new object[]
{
string_3
});
return;
}
this.darkTextBox_0.Text = string_3;
}
public void method_Log(string string_3)
{
this.darkTextBox_0.Text += string_3;
//Send to ROM Editor logs
Editortable_0.method_Log(string_3);
}
public void ClearLogs()
{
this.darkTextBox_0.Text = "";
}
public void method_1(string string_3)
{
try
{
Console.WriteLine(string_3);
Class5 @class = new Class5();
@class.gform0_0 = this;
@class.string_0 = string_3;
this.darkTextBox_0.BeginInvoke(new MethodInvoker(@class.method_0));
//Send to ROM Editor logs
Editortable_0.method_1(string_3);
Application.DoEvents();
}
catch { }
}
private void method_2(object sender, EventArgs e)
{
APIInfo[] apilist = APIFactory.GetAPIList();
for (int i = 0; i < apilist.Length; i++)
{
APIInfo apiinfo = apilist[i];
this.method_1("--------------------------------------");
this.method_1("Adapter name:" + apiinfo.Name);
this.method_1("File name:" + apiinfo.Filename);
this.method_1("API details: " + apiinfo.Details);
}
}
public void LoadAdapter()
{
APIInfo[] apilist = APIFactory.GetAPIList();
for (int i = 0; i < apilist.Length; i++)
{
APIInfo apiinfo = apilist[i];
if (apiinfo.Filename == J2534AdapterName)
{
this.method_1("J2534 adapter selected: " + apiinfo.Name);
i = apilist.Length;
}
}
darkButton1.Enabled = true;
darkButton_4.Enabled = true;
darkButton_0.Enabled = true;
}
private void method_3(object sender, EventArgs e)
{
GForm_J2534Select gform = new GForm_J2534Select();
if (gform.ShowDialog() == DialogResult.OK)
{
J2534AdapterName = gform.APIInfo_0.Filename;
GForm_Main_0.SaveSettings();
gform.Dispose();
LoadAdapter();
}
}
private void SetCommandText(byte[] CommandArray)
{
try
{
darkTextBoxJ2534Command.Text = "";
for (int i = 0; i < CommandArray.Length; i++)
{
darkTextBoxJ2534Command.Text = darkTextBoxJ2534Command.Text + CommandArray[i].ToString("X2");
if (i < CommandArray.Length - 1) darkTextBoxJ2534Command.Text = darkTextBoxJ2534Command.Text + ",";
}
}
catch { }
}
/*public void ConnectJ2534()
{
if (J2534Connected) return;
try
{
api = APIFactory.GetAPI(J2534AdapterName);
device = api.GetDevice("");
channel = device.GetChannel(Protocol.ISO15765, Baud.CAN, ConnectFlag.CAN_29BIT_ID, false);
LoadJ2534Channel(channel);
J2534Connected = true;
TimerJ2534.Start();
this.method_1("J2534 adapter connected");
label1.ForeColor = Color.DarkGreen;
label1.Text = "Connected";
}
catch
{
Disconnect();
}
}
public void Disconnect()
{
this.method_1("J2534 adapter disconnected!");
channel = null;
device = null;
api = null;
J2534Connected = false;
TimerJ2534.Stop();
this.darkButton_DownloadROM.Enabled = false;
this.darkButton_Unlock41.Enabled = false;
this.darkButton_Unlock01.Enabled = false;
this.darkButton_FlashRom.Enabled = false;
this.darkButton_FlashFW.Enabled = false;
this.darkButtonJ2534Command.Enabled = false;
VehicleConnected = false;
label1.ForeColor = Color.Red;
label1.Text = "Disconnected";
}
public void CheckConnected()
{
if (J2534Connected)
{
//Console.WriteLine("checking");
try
{
//Console.WriteLine(device.);
}
catch(Exception ex)
{
this.method_1("ERROR: " + ex);
Disconnect();
return;
}
if (channel.IsDisposed || device.IsDisposed || api.IsDisposed)
{
Disconnect();
}
}
}*/
private void darkButton1_Click(object sender, EventArgs e)
{
//ECU_Unlocked = false;
this.Enabled = false;
this.darkButton_DownloadROM.Enabled = false;
this.darkButton_FlashRom.Enabled = false;
this.darkButton_FlashFW.Enabled = false;
this.darkButtonJ2534Command.Enabled = false;
VehicleConnected = false;
this.darkTextBox_1.Text = "";
this.darkTextBox_2.Text = "";
//ConnectJ2534();
//darkButtonJ2534Command.Enabled = true; //########
//darkButton_FlashFW.Enabled = true; //########
using (API api = APIFactory.GetAPI(J2534AdapterName))
{
try
{
using (Device device = api.GetDevice(""))
{
using (Channel channel = device.GetChannel(Protocol.ISO15765, Baud.CAN, ConnectFlag.CAN_29BIT_ID, false))
{
LoadJ2534Channel(channel);
int num2 = 0;
byte[] arraySend1 = new byte[]
{
0x22, //Read Data by ID (F190)
0xF1,
0x90
};
byte[] Received = SendJ2534Message(channel, arraySend1, 5, true);
if (BadResponceReceived)
{
Class_ODB.NegativeResponse negativeResponse = (Class_ODB.NegativeResponse)Received[6];
if (negativeResponse == Class_ODB.NegativeResponse.REQUEST_OUT_OF_RANGE)
{
//we have responce from ecu, it mean the ecu is connected but just cant proceed to reading VIN
//enable other buttons (read/write & j2534 commands for further use)
this.method_1("We detected the ECU but could not read the VIN!");
this.darkButtonJ2534Command.Enabled = true;
VehicleConnected = true;
SetButtons();
}
this.Enabled = true;
return;
}
int num4 = smethod_0(Received, byte_1);
if (num4 != -1)
{
byte[] bytes = new byte[0x10];
Array.Copy(Received, 8, bytes, 0, 0x10);
this.darkTextBox_1.Text = Encoding.ASCII.GetString(bytes); //Display VIN number
this.method_1("VIN:" + Encoding.ASCII.GetString(bytes));
num2 = 1;
}
//#############################################################
//#############################################################
arraySend1 = new byte[]
{
0x22, //Read Data by ID (F181)
0xF1,
0x81
};
Received = SendJ2534Message(channel, arraySend1, 5, true);
if (BadResponceReceived)
{
this.Enabled = true;
return;
}
int num6 = smethod_0(Received, byte_0);
if (num6 != -1)
{
byte[] bytes = new byte[0x10];
Array.Copy(Received, 7, bytes, 0, 0x10);
this.darkTextBox_2.Text = Encoding.ASCII.GetString(bytes); //Display CAL_ID Number
this.method_1("ID:" + Encoding.ASCII.GetString(bytes));
this.method_1("Vehicle is Online");
num2 = 2;
}
//#############################################################
if (num2 == 1)
{
this.method_1("Vehicle is in recovery mode?");
DarkMessageBox.Show(this, "Failed to retrieve vin number, assuming recovery mode, read disabled", "RECOVERY MODE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (num2 != 2)
{
DarkMessageBox.Show(this, "ECU was not detected.\n\rMake sure you have selected the correct platform and the vehicle is on and your device is plugged in.\n\rProvided you have checked these things. Please send a message to the discord group or the page with your vehicle \n\rDomestic Market, Make, Model,Year,Transmission, and Device you are using to Connect.", "Failed to detect Ecu", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
this.darkButtonJ2534Command.Enabled = true;
VehicleConnected = true;
SetButtons();
this.Enabled = true;
}
}
}
}
catch (Exception ex)
{
this.Enabled = true;
DarkMessageBox.Show(this, ex.Message);
}
}
this.Enabled = true;
return;
}
public void method_4(int int_0)
{
try
{
Class5_Status @class = new Class5_Status();
@class.gform0_0 = this;
@class.string_0 = "Reading: " + int_0.ToString() + "%";
this.darkLabel_7.BeginInvoke(new MethodInvoker(@class.method_0));
Class5_Percent @class2 = new Class5_Percent();
@class2.gform0_0 = this;
@class2.Percentt = int_0;
this.darkProgressBar_0.BeginInvoke(new MethodInvoker(@class2.method_0));
}
catch { }
Application.DoEvents();
}
public void method_5(int int_0)
{
try
{
Class5_Status @class = new Class5_Status();
@class.gform0_0 = this;
@class.string_0 = "Writing: " + int_0.ToString() + "%";
this.darkLabel_7.BeginInvoke(new MethodInvoker(@class.method_0));
Class5_Percent @class2 = new Class5_Percent();
@class2.gform0_0 = this;
@class2.Percentt = int_0;
this.darkProgressBar_0.BeginInvoke(new MethodInvoker(@class2.method_0));
}
catch { }
Application.DoEvents();
}
public void ResetProgressBar()
{
Class5_Status @class = new Class5_Status();
@class.gform0_0 = this;
@class.string_0 = "Status";
this.darkLabel_7.BeginInvoke(new MethodInvoker(@class.method_0));
Class5_Percent @class2 = new Class5_Percent();
@class2.gform0_0 = this;
@class2.Percentt = 0;
this.darkProgressBar_0.BeginInvoke(new MethodInvoker(@class2.method_0));
Application.DoEvents();
}
private void method_6(object sender, ProgressChangedEventArgs e)
{
string text = e.UserState as string;
this.darkLabel_8.Text = text;
this.method_4(e.ProgressPercentage);
}
private void method_7(object sender, RunWorkerCompletedEventArgs e)
{
if (this.byte_7 != null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.RestoreDirectory = true;
saveFileDialog.Filter = "Honda Rom Dump|*.Bin";
saveFileDialog.FileName = this.darkTextBox_2.Text;
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
this.Enabled = true;
return;
}
File.WriteAllBytes(saveFileDialog.FileName, this.byte_7);
this.method_1("File saved: " + saveFileDialog.FileName);
DarkMessageBox.Show(this, "Successfully Saved File!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
this.Enabled = true;
}
public byte[] SendJ2534Message(Channel channel, byte[] MessageBytes, int receivelenght, bool Logs)
{
try
{
BadResponceReceived = false;
byte[] arrayCommand = new byte[]
{
0x18,
0xDA,
ECU_Byte, //-> 0x10|0x11
0xF1
};
SetCommandText(MessageBytes);
//Add the rest of the messages bytes to the final array
byte[] arrayCommandFinal = new byte[arrayCommand.Length + MessageBytes.Length];
int MessageIndex = 0;
for (int i = 0; i < arrayCommand.Length; i++)
{
arrayCommandFinal[MessageIndex] = arrayCommand[i];
MessageIndex++;
}
for (int i = 0; i < MessageBytes.Length; i++)
{
arrayCommandFinal[MessageIndex] = MessageBytes[i];
MessageIndex++;
}
//Send message
SAE.J2534.Message messageCommands = new SAE.J2534.Message(arrayCommandFinal, TxFlag.CAN_29BIT_ID | TxFlag.ISO15765_FRAME_PAD);
channel.SendMessage(messageCommands);
if (Logs && darkCheckBoxLogsCommands.Checked) this.method_1("Send: " + smethod_1(messageCommands.Data));
//Receive message
bool SendPendingResp = false;
int RetryCount = 0;
int RetryMaxCount = (10 * 1000) / 5; //->Max ~10sec for responce pending, each try take 5ms
while (true)
{
if (SendPendingResp && Logs) this.method_1("Waiting for responce..");
if (RetryCount >= RetryMaxCount)
{
this.method_1("Timeout waiting for response");
BadResponceReceived = true;
break;
}
GetMessageResults messagesReceived = channel.GetMessages(receivelenght, 1000);
if (messagesReceived.Result.IsOK() ||
(messagesReceived.Result.IsNotOK() && messagesReceived.Messages.Length > 0 && messagesReceived.Messages.Length != receivelenght))
{
int IndexReceived = 1;
foreach (SAE.J2534.Message message3 in messagesReceived.Messages)
{
//Gather Negative Responce
int num2 = smethod_2(message3.Data, this.byte_5, 0); //looking for 0x11, 0x7F
if (num2 > 0)
{
for (int k = 0; k < 2; k++)
{
this.byte_6[k] = message3.Data[k + num2 + 2]; //0x27, 0x35
}
Class_ODB.Mode mode = (Class_ODB.Mode)this.byte_6[0];
string str2 = mode.ToString();
Class_ODB.NegativeResponse negativeResponse = (Class_ODB.NegativeResponse)this.byte_6[1];
// wait for another message if response pending
if (this.byte_6[1] == 0x78)
{
if (Logs && darkCheckBoxLogsCommands.Checked) this.method_1("Received:" + smethod_1(message3.Data));
if (!SendPendingResp && Logs) this.method_1("Response pending...");
SendPendingResp = true;
receivelenght = 1;
Thread.Sleep(5);
RetryCount++;
continue;
}
else
{
if (Logs && darkCheckBoxLogsCommands.Checked) this.method_1("Received:" + smethod_1(message3.Data));
this.method_1("BAD Response: " + str2 + "|" + negativeResponse.ToString());
BadResponceReceived = true;
//break;
return message3.Data; //still return the responce
}
}
if (IndexReceived >= receivelenght)
{
if (Logs && darkCheckBoxLogsCommands.Checked) this.method_1("Received:" + smethod_1(message3.Data));
return message3.Data;
}
IndexReceived++;
}
}
if (messagesReceived.Result.IsNotOK())
{
if (SendPendingResp)
{
Thread.Sleep(5);
RetryCount++;
continue;
}
else
{
//Retry receiving 1x responce before fully return null responce
if (receivelenght > 1 && messagesReceived.Messages.Length == 0)
{
receivelenght = 1;
continue;
}
//return null responce
this.method_1("Result NOT OK!!");
BadResponceReceived = true;
break;
}
}
}
}
catch (Exception ex)
{
this.method_1("--------------------------------------");
this.method_1("Could not send J2534 message with error: " + Environment.NewLine + ex);
}
return null;
}
public void method_ReadROM(object sender, DoWorkEventArgs e)
{
using (API api = APIFactory.GetAPI(J2534AdapterName))
{
try
{
using (Device device = api.GetDevice(""))
{
using (Channel channel = device.GetChannel(Protocol.ISO15765, Baud.CAN, ConnectFlag.CAN_29BIT_ID, false))
{
LoadJ2534Channel(channel);
bool ECU_Unlocked = false;
device.SetProgrammingVoltage(Pin.PIN_12, 5000);
//################################################################
//Unlocking ECU before performing any actions
byte[] arraySend1 = new byte[] { 0x10, 0x03 };
byte[] Received = SendJ2534Message(channel, arraySend1, 3, true);
if (BadResponceReceived) return;
if (Received != null) this.method_1("Diag Mode Set");
//################################################################
byte SeedSendByte = this.Unlocking_Mode;
arraySend1 = new byte[] { 0x27, SeedSendByte };
this.method_1("Requesting Seed");
Received = SendJ2534Message(channel, arraySend1, 3, true);
if (BadResponceReceived) return;
//################################################################
byte[] byte_ = new byte[] { 0x67, SeedSendByte };
byte[] array6 = new byte[4];
bool TwoBytesMode = false;
byte b = 1;
//################################################################
if (Received != null)
{
int num = smethod_2(Received, byte_, 0);
if (num > 0)
{
if (Received.Length < 10)
{
array6 = new byte[2];
TwoBytesMode = true;
}
int index = 0;
while (true)
{
if ((!TwoBytesMode && index >= 4) || (TwoBytesMode && index >= 2))
{
if (!TwoBytesMode)
{
b = Received[(index + num) + 2];
Array.Reverse(array6);
}
this.method_1("Security Request - Seed Bytes:" + smethod_1(array6));
if (!TwoBytesMode) this.method_1("Security Request - Algorithm:" + b.ToString("X2"));
break;
}
array6[index] = Received[(index + num) + 2];
index++;
}
}
}
//################################################################
if (array6[0] != 0)
{
uint value = 0;
if (!TwoBytesMode) value = Class_Cypher.GetKey41(BitConverter.ToUInt32(array6, 0), b);
else value = Class_Cypher.GetKey01(array6, darkTextBox_2.Text);
byte[] bytes = BitConverter.GetBytes(value);
this.method_1("Security Request - Key to Send:" + smethod_1(bytes));
arraySend1 = new byte[] { 0x27, (byte)(SeedSendByte + 1) };
byte[] array8 = new byte[arraySend1.Length + 5];
if (TwoBytesMode) array8 = new byte[arraySend1.Length + 2];
for (int i = 0; i < arraySend1.Length; i++) array8[i] = arraySend1[i];
array8[2] = bytes[0]; //SecurityKey Byte1
array8[3] = bytes[1]; //SecurityKey Byte2
if (!TwoBytesMode)
{
array8[2] = bytes[2]; //SecurityKey Byte3
array8[3] = bytes[3]; //SecurityKey Byte4
array8[4] = b; //Algorithm Byte
}
byte[] byte_2 = new byte[] { 0x67, (byte)(SeedSendByte + 1) };
Received = SendJ2534Message(channel, array8, 3, true);
if (BadResponceReceived) return;
if (Received != null)
{
int num = smethod_2(Received, byte_2, 0); //looking for 0x67, 0x42
if (num > 0)
{
this.method_1("Security Authorized: ECU Unlocked");
ECU_Unlocked = true;
}
else
{
this.method_1("Recv:" + smethod_1(Received));
}
}
}
else
{
this.method_1("Result NOT OK!!");
}
//################################################################
if (!ECU_Unlocked)
{
this.method_1("ECU is NOT Unlocked!");
return;
}
else
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
this.byte_7 = this.method_10(channel, this.backgroundWorker_1); //READ ECU ROM
stopwatch.Stop();
TimeSpan timeSpan = TimeSpan.FromMilliseconds((double)stopwatch.ElapsedMilliseconds);
this.method_1("Successfully read " + this.byte_7.Length + " bytes within flash memory in " + timeSpan.Minutes + ":" + timeSpan.Seconds);
this.backgroundWorker_1.ReportProgress(0);
device.SetProgrammingVoltage(Pin.PIN_12, -1);
}
}
}
}
catch (Exception ex)
{
DarkMessageBox.Show(ex.Message);
}
}
}
public static int smethod_0(byte[] byte_12, byte[] byte_13)
{
if (byte_12 == null) return -1;
if (byte_13.Length > byte_12.Length)
{
return -1;
}
for (int i = 0; i < byte_12.Length - byte_13.Length; i++)
{
bool flag = true;
for (int j = 0; j < byte_13.Length; j++)
{
if (byte_12[i + j] != byte_13[j])
{
flag = false;
break;
}
}
if (flag)
{
return i;
}
}
return -1;
}
public static string smethod_1(byte[] byte_12)
{
StringBuilder stringBuilder = new StringBuilder(byte_12.Length * 2);
foreach (byte b in byte_12)
{
stringBuilder.Append("0x");
stringBuilder.AppendFormat("{0:X2} ", b);
}
return stringBuilder.ToString();
}
private static int smethod_2(byte[] byte_12, byte[] byte_13, int int_0 = 0)
{
int num = byte_12.Length - byte_13.Length;
byte b = byte_13[0]; //0x67 || 0x11
while (int_0 <= num)
{
if (byte_12[int_0] == b)
{
for (int num2 = 1; num2 != byte_13.Length; num2++)
{
if (byte_12[int_0 + num2] != byte_13[num2]) //0x42 || 0x7F
{
goto IL_32;
}
}
return int_0;
}
IL_32:
int_0++;
}
return -1;
}
public byte[] method_10(Channel channel_0, BackgroundWorker backgroundWorker_1 = null)
{
//READ ECU ROM FUNCTION
DateTime now = DateTime.Now;
Class6 class6 = new Class6();
class6.gform0_0 = this;
class6.byte_0 = new byte[1];
class6.uint_0 = 4U;
this.darkTextBox_0.BeginInvoke(new MethodInvoker(class6.method_0));
Class7 class7 = new Class7();
class7.class6_0 = class6;
class7.uint_0 = 0U;
while ((ulong)class7.uint_0 <= (ulong)((long)class9_0.ReadingSize))
{
Application.DoEvents();
TimeSpan timeSpan = TimeSpan.FromTicks(DateTime.Now.Subtract(now).Ticks * ((long)class9_0.ReadingSize - (long)((ulong)(class7.uint_0 + 1U))) / (long)((ulong)(class7.uint_0 + 1U)));
this.method_12(class7.uint_0, class7.class6_0.uint_0, out class7.class6_0.byte_0, channel_0);
string userState = "Time Remaining:" + string.Format("{0:mm\\:ss}", timeSpan);
this.SetDownloadRate((long)((ulong)class7.uint_0));
if ((long)class7.class6_0.byte_0.Length != (long)((ulong)class7.class6_0.uint_0))
{
Control control = this.darkTextBox_0;
MethodInvoker method;
if ((method = class7.class6_0.methodInvoker_0) == null)
{
method = (class7.class6_0.methodInvoker_0 = new MethodInvoker(class7.class6_0.method_1));
}
control.BeginInvoke(method);
}
try
{
Buffer.BlockCopy(class7.class6_0.byte_0, 0, this.byte_7, (int)class7.uint_0, class7.class6_0.byte_0.Length);
goto IL_213;
}
catch
{
this.darkTextBox_0.BeginInvoke(new MethodInvoker(class7.method_0));
goto IL_213;
}
goto IL_1B4;
IL_1CD:
if (backgroundWorker_1 != null)
{
backgroundWorker_1.ReportProgress((int)(class7.uint_0 / (float)class9_0.ReadingSize * 100f), userState);
}
class7.uint_0 += class7.class6_0.uint_0;
continue;
IL_1B4:
this.darkTextBox_0.BeginInvoke(new MethodInvoker(class7.method_1));
goto IL_1CD;
IL_213:
if (class7.uint_0 % 256U == 0U)
{
goto IL_1B4;
}
goto IL_1CD;
}
return this.byte_7;
}
private void SetText1(string Texxxt)
{
Class8_Text1 @class = new Class8_Text1();
@class.gform0_0 = this;
@class.ThisText = Texxxt;
this.darkLabel_5.BeginInvoke(new MethodInvoker(@class.method_0));
}
private void SetText2(string Texxxt)
{
Class8_Text2 @class = new Class8_Text2();
@class.gform0_0 = this;
@class.ThisText = Texxxt;
this.darkLabel_8.BeginInvoke(new MethodInvoker(@class.method_0));
}
private void SetDownloadRate(long long_1)
{
Class8 @class = new Class8();
@class.gform0_0 = this;
if (this.long_0 != 0L)
{
DateTime now = DateTime.Now;
TimeSpan timeSpan = now - this.dateTime_0;
long num = long_1 - this.long_0;
@class.double_0 = (double)num / timeSpan.TotalSeconds;
@class.TotalSeconds_0 = timeSpan.TotalSeconds;
this.long_0 = long_1;
this.dateTime_0 = now;
this.darkLabel_5.BeginInvoke(new MethodInvoker(@class.method_0));
return;
}
this.dateTime_0 = DateTime.Now;
this.long_0 = long_1;
}
private void SetUploadRate(long long_1)
{
Class8_Upload @class = new Class8_Upload();
@class.gform0_0 = this;
if (this.long_0 != 0L)
{
DateTime now = DateTime.Now;
TimeSpan timeSpan = now - this.dateTime_0;
long num = long_1 - this.long_0;
@class.double_0 = (double)num / timeSpan.TotalSeconds;
@class.TotalSeconds_0 = timeSpan.TotalSeconds;
this.long_0 = long_1;
this.dateTime_0 = now;
this.darkLabel_5.BeginInvoke(new MethodInvoker(@class.method_0));
return;
}
this.dateTime_0 = DateTime.Now;
this.long_0 = long_1;
}
public void method_12(uint uint_0, uint uint_1, out byte[] byte_12, Channel channel_0)
{
byte_12 = new byte[1];
byte[] arraySend1 = new byte[]
{
35, //0x23 -> Read_data_by_address
20, //0x14
(byte)((uint_0 >> 0x18) & 0xff),
(byte)((uint_0 >> 0x10) & 0xff),
(byte)((uint_0 >> 8) & 0xff),
(byte)(uint_0 & 0xff),
4
};
byte[] byte_13 = new byte[]
{
ECU_Byte,
99
};
byte[] Received = SendJ2534Message(channel_0, arraySend1, 3, true);
if (BadResponceReceived) return;
if (Received != null)
{
//if (messages.Result != ResultCode.DEVICE_NOT_CONNECTED)
//{
int num = smethod_2(Received, byte_13, 0);
if (num > 0)
{
num += 2;
Array.Resize<byte>(ref byte_12, Received.Length - num);
Array.Copy(Received, num, byte_12, 0, Received.Length - num);
}
}
}
private void method_13(object sender, EventArgs e)
{
this.Enabled = false;
if (J2534AdapterName.Length == 0)
{
GForm_J2534Select gform = new GForm_J2534Select();
if (gform.ShowDialog() != DialogResult.OK)
{