forked from VR2VYE/OpenGD77CPS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FirmwareUpdate.cs
1589 lines (1523 loc) · 41 KB
/
FirmwareUpdate.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 DMR;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using UsbLibrary;
/* Roger Clark
* Note. This Class is potentially unused, as it seems to relate to firmware update which is not handled by the CPS application
*/
internal class FirmwareUpdate : Win32Usb, IFirmwareUpdate
{
public class Class18
{
public enum SectorType
{
InternalFLASH,
OptionBytes,
OTP,
DeviceFeature,
Other
}
public SectorType Type;
public string Name;
public uint dwStartAddress;
public uint dwSectorIndex;
public uint dwSectorSize;
public Class18(string string_0, SectorType sectorType_0, uint uint_0, uint uint_1, ushort ushort_0)
{
this.Name = string_0;
this.Type = sectorType_0;
this.dwStartAddress = uint_0;
this.dwSectorSize = uint_1;
this.dwSectorIndex = ushort_0;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct Struct0
{
public byte bLength;
public byte bDescriptorType;
public ushort bcdUSB;
public byte bDeviceClass;
public byte bDeviceSubClass;
public byte bDeviceProtocol;
public byte bMaxPacketSize0;
public ushort idVendor;
public ushort idProduct;
public ushort bcdDevice;
public byte iManufacturer;
public byte iProduct;
public byte iSerialNumber;
public byte bNumConfigurations;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
public struct Struct1
{
public byte bLength;
public byte bDescriptorType;
public byte bmAttributes;
public ushort wDetachTimeOut;
public ushort wTransfertSize;
public ushort bcdDFUVersion;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct Struct2
{
public byte bStatus;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] bwPollTimeout;
public byte bState;
public byte iString;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct Struct3
{
public byte bLength;
public byte bDescriptorType;
public byte bInterfaceNumber;
public byte bAlternateSetting;
public byte bNumEndpoints;
public byte bInterfaceClass;
public byte bInterfaceSubClass;
public byte bInterfaceProtocol;
public byte iInterface;
}
private const byte CST_CMD = 162;
private const byte SUB_CMD_PRG = 80;
private const byte SUB_CMD_READ_INFO = 81;
private const byte SUB_CMD_WRITE = 82;
private const byte SUB_CMD_READ = 83;
private const byte SUB_CMD_END = 84;
private const byte DATA_ACK = 65;
private const byte DATA_NACK = 78;
private const ushort HID_VID = 4660;
private const ushort HID_PID = 22136;
private const byte HID_DETACH_REPORT_ID = 128;
private const byte USAGE_DETACH = 85;
private const uint STDFU_ERROR_OFFSET = 305397760u;
private const uint STDFU_NOERROR = 305397760u;
private const byte STATE_IDLE = 0;
private const byte STATE_DETACH = 1;
private const byte STATE_DFU_IDLE = 2;
private const byte STATE_DFU_DOWNLOAD_SYNC = 3;
private const byte STATE_DFU_DOWNLOAD_BUS = 4;
private const byte STATE_DFU_DOWNLOAD_IDLE = 5;
private const byte STATE_DFU_MANIFEST_SYNC = 6;
private const byte STATE_DFU_MANIFEST = 7;
private const byte STATE_DFU_MANIFEST_WAIT_RESET = 8;
private const byte STATE_DFU_UPLOAD_IDLE = 9;
private const byte STATE_DFU_ERROR = 10;
private const byte STATE_DFU_UPLOAD_SYNC = 145;
private const byte STATE_DFU_UPLOAD_BUSY = 146;
private static readonly byte[] PACK_PRG;
private static readonly byte[] PACK_READ_INFO;
private static readonly byte[] PACK_READ;
private static readonly byte[] PACK_WRITE;
private static readonly byte[] PACK_END;
private static readonly byte[] PACK_ACK;
private static readonly byte[] PACK_NACK;
private static readonly byte[] PACK_LOW_VOLT;
private IntPtr INVALID_HANDLE_VALUE;
private Guid GUID_DFU;
private Guid GUID_APP;
//private Thread thread;
private string DFU_FilePath;
private ushort VID;
private ushort PID;
private ushort Version;
private string ImageName;
private string DFU_DevicePath;
private SafeFileHandle _ParentHandle = null;// Roger Clark. As this is uniniialised, it will potentially cause a crash if method_9() is ever called
private List<Class18> Sectors;
private ushort MaxWriteBlockSize;
private uint[] CrcTable;
public event FirmwareUpdateProgressEventHandler OnFirmwareUpdateProgress;
[CompilerGenerated]
public bool method_0()
{
return this.CCancelComm;
}
bool CCancelComm;
[CompilerGenerated]
public void method_1(bool bool_0)
{
this.CCancelComm = bool_0;
}
/*
private bool _IsRead;
[CompilerGenerated]
public bool method_2()
{
return this._IsRead;
}
[CompilerGenerated]
public void method_3(bool bool_0)
{
this._IsRead = bool_0;
}*/
public bool getIsThreadAlive()
{
//if (this.thread != null)
//{
// return this.thread.IsAlive;
//}
return false;
}
public void method_5()
{
//if (this.getIsThreadAlive())
//{
// this.thread.Join();
//}
}
public void UpdateFirmware()
{
Console.WriteLine("Here");
/*
if (this.method_2())
{
this.thread = new Thread(this.method_6);
}
else
{
this.thread = new Thread(this.method_7);
}
this.thread.Start();*/
}
public void MassErase()
{
Console.WriteLine("MassErase");
IntPtr zero = IntPtr.Zero;
try
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "检测设备", false, false));//Testing Equipment
}
this.method_8();
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "正在打开设备", false, false));//Opening the device
}
zero = this.method_13(out this.MaxWriteBlockSize);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(20f, "擦除数据", false, false));//Erase the data
}
this.method_14(zero);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "擦除完成", false, false));//Erase completed
}
}
catch (Exception ex)
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, ex.Message, true, true));
}
}
}
private void method_6()
{
int num = 0;
//int num2 = -1;
uint eEROM_SPACE = Settings.EEROM_SPACE;
IntPtr intptr_ = IntPtr.Zero;
ushort num3 = 0;
ushort num4 = 0;
byte[] array = new byte[eEROM_SPACE];
byte[] array2 = new byte[this.MaxWriteBlockSize];
try
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "检测设备", false, false));// Test equipment
}
this.method_8();
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(3f, "正在打开设备", false, false));// Opening the device
}
intptr_ = this.method_13(out this.MaxWriteBlockSize);
Thread.Sleep(500);
switch (this.method_21(intptr_))
{
case 1:
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "对讲机还未完成初始化", true, true));//Intercom has not yet completed initialization
break;
case 0:
case 2:
if (!this.method_22(intptr_))
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "机型不匹配", true, true));//Models do not match
}
else
{
this.method_23(intptr_);
this.method_26(intptr_, 0u);
while (true)
{
this.method_18(intptr_, array2, num3);
if (Settings.CUR_PWD == "DT8168")
{
break;
}
string text = "";
for (num = 0; num < 8; num++)
{
char c = Convert.ToChar(array2[Settings.ADDR_PWD + num]);
if ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\b".IndexOf(c) < 0)
{
break;
}
text += c;
}
if (string.IsNullOrEmpty(text))
{
break;
}
if (!(text != Settings.CUR_PWD))
{
break;
}
Settings.CUR_PWD = "";
PasswordForm passwordForm = new PasswordForm();
if (passwordForm.ShowDialog() == DialogResult.OK)
{
continue;
}
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "密码错误", false, true));//wrong password
return;
}
num4 = (ushort)(eEROM_SPACE / this.MaxWriteBlockSize);
for (num3 = 0; num3 < eEROM_SPACE / this.MaxWriteBlockSize; num3 = (ushort)(num3 + 1))
{
if (this.method_0())
{
return;
}
this.method_18(intptr_, array2, num3);
Array.Copy(array2, 0, array, num3 * this.MaxWriteBlockSize, this.MaxWriteBlockSize);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(10 + (num3 + 1) * 90 / (int)num4), num3.ToString(), false, false));
}
}
Thread.Sleep(500);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, num3.ToString(), false, true));
}
MainForm.ByteToData(array);
}
break;
}
}
catch (Exception ex)
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, ex.Message, true, true));
}
}
finally
{
this.method_25(intptr_);
FirmwareUpdate.STDFU_Close(ref intptr_);
}
}
private void method_7()
{
int num = 0;
//int num2 = -1;
IntPtr intptr_ = IntPtr.Zero;
ushort num3 = 0;
ushort num4 = 0;
byte[] array = new byte[this.MaxWriteBlockSize];
byte[] array2 = MainForm.DataToByte();
int num5 = array2.Length;
try
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "检测设备", false, false));//Testing Equipment
}
this.method_8();
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "正在打开设备", false, false));//Opening the device
}
intptr_ = this.method_13(out this.MaxWriteBlockSize);
Thread.Sleep(500);
switch (this.method_21(intptr_))
{
case 0:
if (!this.method_22(intptr_))
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "机型不匹配", true, true));//Models do not match
}
else
{
this.method_24(intptr_);
this.method_26(intptr_, 0u);
while (true)
{
this.method_18(intptr_, array, num3);
if (Settings.CUR_MODE <= 0)
{
int aDDR_DEVICE_INFO = Settings.ADDR_DEVICE_INFO;
if (array[aDDR_DEVICE_INFO] != array2[aDDR_DEVICE_INFO] || (array[aDDR_DEVICE_INFO + 1] == array2[aDDR_DEVICE_INFO + 1] && array[aDDR_DEVICE_INFO + 2] == array2[aDDR_DEVICE_INFO + 2]))
{
;
}
Array.Copy(array, Settings.ADDR_DEVICE_INFO + Settings.OFS_MODEL, array2, Settings.ADDR_DEVICE_INFO + Settings.OFS_MODEL, Settings.SPACE_DEVICE_INFO - Settings.OFS_MODEL);
}
else
{
Array.Copy(array, Settings.ADDR_DEVICE_INFO + Settings.OFS_CPS_SW_VER, array2, Settings.ADDR_DEVICE_INFO + Settings.OFS_CPS_SW_VER, Settings.SPACE_DEVICE_INFO - Settings.OFS_CPS_SW_VER);
}
if (Settings.CUR_PWD == "DT8168")
{
break;
}
string text = "";
for (num = 0; num < 8; num++)
{
char c = Convert.ToChar(array[Settings.ADDR_PWD + num]);
if ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\b".IndexOf(c) < 0)
{
break;
}
text += c;
}
if (string.IsNullOrEmpty(text))
{
break;
}
if (!(text != Settings.CUR_PWD))
{
break;
}
Settings.CUR_PWD = "";
PasswordForm passwordForm = new PasswordForm();
if (passwordForm.ShowDialog() != DialogResult.OK)
{
return;
}
}
byte[] array3 = new byte[6];
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
int hour = DateTime.Now.Hour;
int minute = DateTime.Now.Minute;
array3[0] = (byte)(year / 1000 << 4 | year / 100 % 10);
array3[1] = (byte)(year % 100 / 10 << 4 | year % 10);
array3[2] = (byte)(month / 10 << 4 | month % 10);
array3[3] = (byte)(day / 10 << 4 | day % 10);
array3[4] = (byte)(hour / 10 << 4 | hour % 10);
array3[5] = (byte)(minute / 10 << 4 | minute % 10);
Array.Copy(array3, 0, array2, Settings.ADDR_DEVICE_INFO + Settings.OFS_LAST_PRG_TIME, 6);
num4 = (ushort)(num5 / (int)this.MaxWriteBlockSize);
num3 = 0;
while (true)
{
if (num3 >= num5 / (int)this.MaxWriteBlockSize)
{
break;
}
if (this.method_0())
{
return;
}
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(10 + (num3 + 1) * 90 / (int)num4), num3.ToString(), false, false));
}
try
{
Array.Copy(array2, num3 * this.MaxWriteBlockSize, array, 0, this.MaxWriteBlockSize);
this.method_19(intptr_, array, num3);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
num3 = (ushort)(num3 + 1);
}
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "完成", false, true));//carry out
}
}
break;
case 2:
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "设备电池电平太低,不能执行操作,请给设备充电,然后重试。", true, true));//Device battery level is too low for operation, please charge device and try again.
}
break;
case 1:
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "对讲机还未完成初始化", true, true));//Intercom has not yet completed initialization
break;
}
}
catch (Exception ex2)
{
FirmwareUpdate.STDFU_Close(ref intptr_);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, ex2.Message, true, true));
}
}
finally
{
this.method_25(intptr_);
FirmwareUpdate.STDFU_Close(ref intptr_);
}
}
private void method_8()
{
if (this.method_9(4660, 22136))
{
IntPtr intPtr = Marshal.AllocHGlobal(65);
Marshal.WriteByte(intPtr, 0, 128);
Marshal.WriteByte(intPtr, 1, 85);
for (int i = 2; i < 65; i++)
{
Marshal.WriteByte(intPtr, i, 0);
}
if (Win32Usb.HidD_SetFeature(this._ParentHandle, intPtr, 65u))
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(1f, "HID 分离成功", false, false));//HID separation successful
}
}
else if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(1f, "HID 分离错误 = " + Marshal.GetLastWin32Error().ToString(), false, false));//HID separation error =
}
Marshal.FreeHGlobal(intPtr);
Thread.Sleep(5000);
}
}
public bool method_9(ushort ushort_0, ushort ushort_1)
{
string value = string.Format("vid_{0:x4}&pid_{1:x4}", ushort_0, ushort_1);
Guid guid = default(Guid);
Win32Usb.HidD_GetHidGuid(out guid);
IntPtr intPtr = Win32Usb.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 18u);
try
{
DeviceInterfaceData deviceInterfaceData = default(DeviceInterfaceData);
deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);
for (int i = 0; Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref guid, (uint)i, ref deviceInterfaceData); i++)
{
string text = this.method_10(intPtr, ref deviceInterfaceData);
if (text.IndexOf(value) >= 0)
{
return true;
}
}
if (Marshal.GetLastWin32Error() != 0 && 259L != Marshal.GetLastWin32Error())
{
if (1784L == Marshal.GetLastWin32Error())
{
throw new Exception("Size member of hInfoSet is not set correctly (5 for 32bit or 8 for 64bit)");
}
throw new Exception("SetupDiEnumDeviceInterfaces returned error " + Marshal.GetLastWin32Error().ToString());
}
}
finally
{
Win32Usb.SetupDiDestroyDeviceInfoList(intPtr);
}
return false;
}
private string method_10(IntPtr intptr_0, ref DeviceInterfaceData deviceInterfaceData_0)
{
uint nDeviceInterfaceDetailDataSize = 0u;
DeviceInterfaceDetailData deviceInterfaceDetailData;
if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, IntPtr.Zero, 0u, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
{
deviceInterfaceDetailData = default(DeviceInterfaceDetailData);
if (IntPtr.Size == 8)
{
deviceInterfaceDetailData.Size = 8;
goto IL_0042;
}
if (IntPtr.Size == 4)
{
deviceInterfaceDetailData.Size = 5;
goto IL_0042;
}
throw new Exception("Operating system is neither 32 nor 64 bits!");
}
goto IL_0068;
IL_0068:
return null;
IL_0042:
if (Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, ref deviceInterfaceDetailData, nDeviceInterfaceDetailDataSize, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
{
return deviceInterfaceDetailData.DevicePath;
}
goto IL_0068;
}
public bool ParseDFU_File(string Filepath, out ushort VID, out ushort PID, out ushort Version)
{
uint num = 0u;
bool result = true;
try
{
byte[] array = File.ReadAllBytes(Filepath);
if (Encoding.UTF8.GetString(array, 0, 5) != "DfuSe")
{
throw new Exception("File signature error");
}
if (array[5] != 1)
{
throw new Exception("DFU file version must be 1");
}
if (!(Encoding.UTF8.GetString(array, array.Length - 8, 3) != "UFD") && array[array.Length - 5] == 16 && array[array.Length - 10] == 26 && array[array.Length - 9] == 1)
{
num = BitConverter.ToUInt32(array, array.Length - 4);
if (num != this.calculateCRC(array))
{
throw new Exception("File CRC error");
}
VID = BitConverter.ToUInt16(array, array.Length - 12);
PID = BitConverter.ToUInt16(array, array.Length - 14);
Version = BitConverter.ToUInt16(array, array.Length - 16);
return result;
}
throw new Exception("File suffix error");
}
catch
{
VID = 0;
PID = 0;
Version = 0;
return false;
}
}
private void method_11(byte[] byte_0, out byte[] byte_1, out uint uint_0, out uint uint_1)
{
uint num = 0u;
uint num2 = 0u;
try
{
byte_0 = File.ReadAllBytes(this.DFU_FilePath);
if (Encoding.UTF8.GetString(byte_0, 0, 5) != "DfuSe")
{
throw new Exception("File signature error");
}
if (byte_0[5] != 1)
{
throw new Exception("DFU file version must be 1");
}
if (byte_0[10] != 1)
{
throw new Exception("There should be exactly one target in the DFU file");
}
if (!(Encoding.UTF8.GetString(byte_0, byte_0.Length - 8, 3) != "UFD") && byte_0[byte_0.Length - 5] == 16 && byte_0[byte_0.Length - 10] == 26 && byte_0[byte_0.Length - 9] == 1)
{
num = BitConverter.ToUInt32(byte_0, byte_0.Length - 4);
if (num != this.calculateCRC(byte_0))
{
throw new Exception("File CRC error");
}
this.VID = BitConverter.ToUInt16(byte_0, byte_0.Length - 12);
this.PID = BitConverter.ToUInt16(byte_0, byte_0.Length - 14);
this.Version = BitConverter.ToUInt16(byte_0, byte_0.Length - 16);
if (Encoding.UTF8.GetString(byte_0, 11, 6) != "Target")
{
throw new Exception("Target signature error");
}
BitConverter.ToUInt32(byte_0, 277);
if (byte_0[18] != 0)
{
int num3 = Array.FindIndex(byte_0, 22, FirmwareUpdate.smethod_0);
this.ImageName = Encoding.UTF8.GetString(byte_0, 22, num3 - 22);
}
else
{
this.ImageName = "DFU Image";
}
num2 = BitConverter.ToUInt32(byte_0, 281);
if (num2 != 1)
{
throw new Exception("We only expect one element in the target");
}
uint_0 = BitConverter.ToUInt32(byte_0, 285);
uint_1 = BitConverter.ToUInt32(byte_0, 289);
byte_1 = byte_0.Skip(293).Take((int)uint_1).ToArray();
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(5f, "DFU 文件解析" + uint_1.ToString() + uint_0.ToString(), false, false));
}
goto end_IL_0004;
}
throw new Exception("File suffix error");
end_IL_0004:;
}
catch (Exception ex)
{
throw new Exception("DFU file read failed. " + ex.Message);
}
}
private uint calculateCRC(byte[] byte_0)
{
uint num = 4294967295u;
for (int i = 0; i < byte_0.Length - 4; i++)
{
num = (this.CrcTable[(num ^ byte_0[i]) & 0xFF] ^ num >> 8);
}
return num;
}
private IntPtr method_13(out ushort ushort_0)
{
int num = 10;
uint num2 = 0u;
Guid gUID_DFU = this.GUID_DFU;
DeviceInterfaceData deviceInterfaceData = default(DeviceInterfaceData);
deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);
DeviceInterfaceDetailData deviceInterfaceDetailData = default(DeviceInterfaceDetailData);
uint num3 = 0u;
IntPtr intPtr = Win32Usb.SetupDiGetClassDevs(ref gUID_DFU, null, IntPtr.Zero, 18u);
IntPtr zero = IntPtr.Zero;
this.Sectors = null;
ushort_0 = 1024;
try
{
if (intPtr == this.INVALID_HANDLE_VALUE)
{
throw new Exception("SetupDiGetClassDevs error code = " + Marshal.GetLastWin32Error().ToString());// 错误码 = error code
}
while (num-- > 0)
{
for (num2 = 0u; Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref gUID_DFU, num2, ref deviceInterfaceData); num2++)
{
}
if (num2 != 0)
{
break;
}
Thread.Sleep(500);
}
if (1 == num2)
{
Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref gUID_DFU, 0u, ref deviceInterfaceData);
Win32Usb.SetupDiGetDeviceInterfaceDetail(intPtr, ref deviceInterfaceData, IntPtr.Zero, 0u, ref num3, IntPtr.Zero);
if (IntPtr.Size == 8)
{
deviceInterfaceDetailData.Size = 8;
goto IL_0101;
}
if (IntPtr.Size == 4)
{
deviceInterfaceDetailData.Size = 5;
goto IL_0101;
}
throw new Exception("Operating system version");//操作系统版本!
}
throw new Exception("DFU device not found");// 未找到DFU设备
IL_0101:
if (Marshal.SizeOf(deviceInterfaceDetailData) < num3)
{
throw new Exception("DeviceInterfaceDetailData Too small ");// 太小 = Too small
}
if (Win32Usb.SetupDiGetDeviceInterfaceDetail(intPtr, ref deviceInterfaceData, ref deviceInterfaceDetailData, num3, ref num3, IntPtr.Zero))
{
this.DFU_DevicePath = deviceInterfaceDetailData.DevicePath.ToUpper();
if (305397760 == FirmwareUpdate.STDFU_Open(this.DFU_DevicePath, out zero))
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(10f, "DFU The device opens successfully", false, false));// 设备打开成功
}
Struct0 @struct = default(Struct0);
if (305397760 == FirmwareUpdate.STDFU_GetDeviceDescriptor(ref zero, ref @struct))
{
switch (@struct.bcdDevice)
{
default:
throw new Exception("未支持的驱动版本 Unsupported driver version = " + @struct.bcdDevice.ToString("X4"));
case 528:
ushort_0 = 2048;
break;
case 282:
case 512:
ushort_0 = 1024;
break;
}
uint num5 = 0u;
uint num6 = 0u;
Struct1 struct2 = default(Struct1);
if (305397760 == FirmwareUpdate.STDFU_GetDFUDescriptor(ref zero, ref num5, ref num6, ref struct2))
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(14f, "获取DFU版本 " + struct2.bcdDFUVersion.ToString("X"), false, false));
}
this.Sectors = this.method_28(zero);
return zero;
}
throw new Exception("STDFU_GetDFUDescriptor 失败, 错误码 = " + Marshal.GetLastWin32Error().ToString());
}
throw new Exception("STDFU_GetDeviceDescriptor 失败, 错误码 = " + Marshal.GetLastWin32Error().ToString());
}
throw new Exception("STDFU_Open 失败, 错误码 = " + Marshal.GetLastWin32Error().ToString());
}
return zero;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Win32Usb.SetupDiDestroyDeviceInfoList(intPtr);
}
}
private bool method_14(IntPtr intptr_0)
{
uint num = 0u;
byte[] byte_ = new byte[16]
{
65,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255
};
if (305397760 == (num = FirmwareUpdate.STDFU_SelectCurrentConfiguration(ref intptr_0, 0u, 0u, 1u)))
{
if (!this.method_20(ref intptr_0))
{
return false;
}
if (305397760 == (num = FirmwareUpdate.STDFU_Dnload(ref intptr_0, byte_, 1u, 0)))
{
if (!this.method_20(ref intptr_0))
{
return false;
}
return true;
}
throw new Exception("STDFU_Dnload returned (返回) " + num.ToString("X8"));
}
throw new Exception("STDFU_SelectCurrentConfiguration returned (返回) " + num.ToString("X8"));
}
private void method_15(IntPtr intptr_0, uint uint_0, uint uint_1, List<Class18> cLJ3sEY9awr3Obv1TP)
{
foreach (Class18 item in cLJ3sEY9awr3Obv1TP)
{
if (uint_0 < item.dwStartAddress + item.dwSectorSize && uint_0 + uint_1 > item.dwStartAddress)
{
this.method_16(intptr_0, item.dwStartAddress);
}
}
}
private bool method_16(IntPtr intptr_0, uint uint_0)
{
uint num = 0u;
byte[] array = new byte[16]
{
65,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255
};
array[1] = (byte)(uint_0 & 0xFF);
array[2] = (byte)(uint_0 >> 8 & 0xFF);
array[3] = (byte)(uint_0 >> 16 & 0xFF);
array[4] = (byte)(uint_0 >> 24 & 0xFF);
if (305397760 == (num = FirmwareUpdate.STDFU_SelectCurrentConfiguration(ref intptr_0, 0u, 0u, 0u)))
{
if (!this.method_20(ref intptr_0))
{
return false;
}
if (305397760 == (num = FirmwareUpdate.STDFU_Dnload(ref intptr_0, array, 5u, 0)))
{
if (!this.method_20(ref intptr_0))
{
return false;
}
return true;
}
throw new Exception("STDFU_Dnload returned " + num.ToString("X8"));
}
throw new Exception("STDFU_SelectCurrentConfiguration returned " + num.ToString("X8"));
}
private bool method_17(IntPtr intptr_0, uint uint_0, byte[] byte_0, uint uint_1)
{
if (byte_0.Length > this.MaxWriteBlockSize)
{
throw new Exception("Block size too big (" + byte_0.Length.ToString() + ")");
}
if (uint_1 == 0)
{
this.method_26(intptr_0, uint_0);
}
if (!this.method_20(ref intptr_0))
{
return false;
}
FirmwareUpdate.STDFU_Dnload(ref intptr_0, byte_0, (uint)byte_0.Length, (ushort)(uint_1 + 2));
if (!this.method_20(ref intptr_0))
{
return false;
}
return true;
}
private bool method_18(IntPtr intptr_0, byte[] byte_0, ushort ushort_0)
{
if (!this.method_20(ref intptr_0))
{
return false;
}
FirmwareUpdate.STDFU_Upload(ref intptr_0, byte_0, this.MaxWriteBlockSize, (ushort)(ushort_0 + 2));
if (!this.method_20(ref intptr_0))
{
return false;
}
return true;
}
private bool method_19(IntPtr intptr_0, byte[] byte_0, ushort ushort_0)
{
if (!this.method_20(ref intptr_0))
{
return false;
}
FirmwareUpdate.STDFU_Dnload(ref intptr_0, byte_0, this.MaxWriteBlockSize, (ushort)(ushort_0 + 2));
if (!this.method_20(ref intptr_0))
{
return false;
}
return true;
}
private bool method_20(ref IntPtr intptr_0)
{
long num = 50000000L;
long ticks = DateTime.Now.Ticks;
Struct2 @struct = default(Struct2);
FirmwareUpdate.STDFU_Getstatus(ref intptr_0, ref @struct);
while (true)
{
if (@struct.bState != 2)
{
if (ticks + num <= DateTime.Now.Ticks)
{
break;
}
Thread.Sleep(100);
Application.DoEvents();
FirmwareUpdate.STDFU_Clrstatus(ref intptr_0);
FirmwareUpdate.STDFU_Getstatus(ref intptr_0, ref @struct);
continue;
}
return true;
}
FirmwareUpdate.STDFU_Close(ref intptr_0);
return false;
}
private int method_21(IntPtr intptr_0)