forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploader.cs
980 lines (819 loc) · 31.5 KB
/
Uploader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using System.Security.Cryptography;
using Org.BouncyCastle.Security;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
using System.Xml;
using MissionPlanner.Comms;
namespace px4uploader
{
public class Uploader: IDisposable
{
public delegate void LogEventHandler(string message, int level = 0);
public delegate void ProgressEventHandler(double completed);
public event LogEventHandler LogEvent;
public event ProgressEventHandler ProgressEvent;
public ICommsSerial port;
Uploader self;
public bool skipotp = false;
public int board_type;
public int board_rev;
public int fw_maxsize;
public int chip;
public string chip_desc;
public int bl_rev;
public bool libre = false;
public enum Code : byte
{
// response codes
NOP = 0x00,
OK = 0x10,
FAILED = 0x11,
INSYNC = 0x12,
INVALID = 0x13,// # rev3+
BAD_SILICON_REV = 0x14,
// protocol commands
EOC = 0x20,
GET_SYNC = 0x21,
GET_DEVICE = 0x22, // returns DEVICE_ID and FREQ bytes
CHIP_ERASE = 0x23,
CHIP_VERIFY = 0x24,//# rev2 only
PROG_MULTI = 0x27,
READ_MULTI = 0x28,//# rev2 only
GET_CRC = 0x29,// # rev3+
GET_OTP = 0x2a, // read a byte from OTP at the given address
GET_SN = 0x2b, // read a word from UDID area ( Serial) at the given address
GET_CHIP = 0x2c, // read chip version (MCU IDCODE)
SET_DELAY = 0x2d, // set minimum boot delay
GET_CHIP_DES = 0x2e,
REBOOT = 0x30,
DEBUG = 0x31,
SET_BAUD = 0x33,
EXTF_ERASE = 0x34, // Erase sectors from external flash
EXTF_PROG_MULTI = 0x35, // write bytes at external flash program address and increment
EXTF_READ_MULTI = 0x36, // read bytes at address and increment
EXTF_GET_CRC = 0x37, // compute & return a CRC of data in external flash
}
public enum Info {
BL_REV = 1,// # bootloader protocol revision
BOARD_ID = 2,// # board type
BOARD_REV = 3,// # board revision
FLASH_SIZE = 4,// # max firmware size in bytes
VEC_AREA = 5,
EXTF_SIZE = 6
}
public const byte BL_REV_MIN = 2;// # minimum supported bootloader protocol
public const byte BL_REV_MAX = 20;// # maximum supported bootloader protocol
public const byte PROG_MULTI_MAX = 252;// # protocol max is 255, must be multiple of 4
public const byte READ_MULTI_MAX = 252;// # protocol max is 255, something overflows with >= 64
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct otp
{
public byte h1;
public byte h2;
public byte h3;
public byte h4;
public byte id_type;
public uint vid;
public uint pid;
// 19 bytes
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 19)]
public byte[] reserved;
// 128 bytes
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] signature;
}
public int ReadTimeout { get { return port.ReadTimeout; } set { port.ReadTimeout = value; } }
static Uploader()
{
readcerts();
}
public Uploader(ICommsSerial port)
{
self = this;
this.port = port;
this.port.ReadTimeout = 50;
this.port.WriteTimeout = 50;
try
{
this.port.Open();
}
catch (Exception ex)
{
try
{
this.port.Close();
}
catch { }
try
{
this.Dispose();
GC.Collect();
}
catch { }
throw ex;
}
}
public Uploader(string port, int baudrate): this(new SerialPort(port, baudrate))
{
}
public Uploader(string port) : this(new SerialPort(port))
{
}
public void close()
{
try
{
port.BaseStream.Flush();
port.Close();
}
catch { }
}
public static void ByteArrayToStructure(byte[] bytearray, ref object obj)
{
int len = Marshal.SizeOf(obj);
IntPtr i = Marshal.AllocHGlobal(len);
// create structure from ptr
obj = Marshal.PtrToStructure(i, obj.GetType());
// copy byte array to ptr
Marshal.Copy(bytearray, 0, i, len);
obj = Marshal.PtrToStructure(i, obj.GetType());
Marshal.FreeHGlobal(i);
}
static List<KeyValuePair<string, string>> certs = new List<KeyValuePair<string, string>>();
public int extf_maxsize;
public static void readcerts()
{
string vendor = "";
string publickey = "";
var file = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar + @"validcertificates.xml";
if (!File.Exists(file))
{
return;
}
using (XmlTextReader xmlreader = new XmlTextReader(file))
{
while (xmlreader.Read())
{
if (xmlreader.ReadToFollowing("CERTIFICATE"))
{
var xmlreader2 = xmlreader.ReadSubtree();
while (xmlreader2.Read())
{
switch (xmlreader2.Name)
{
case "VENDOR":
vendor = xmlreader2.ReadString();
break;
case "PUBLICKEY":
publickey = xmlreader2.ReadString();
break;
}
}
if (vendor != "")
{
certs.Add(new KeyValuePair<string, string>(vendor, publickey));
vendor = "";
publickey = "";
}
}
}
}
}
bool ByteArrayCompare(byte[] a1, byte[] a2)
{
if (a1.Length != a2.Length)
return false;
for (int i = 0; i < a1.Length; i++)
if (a1[i] != a2[i])
return false;
return true;
}
public bool verifyotp()
{
if (skipotp)
return true;
// check if is a fmuv2 and bootloader >= 4 else fail;
// 9 = fmuv2
// 5 = px4 1.x
if (board_type == 9) // &&up.bl_rev >= 4
{
try
{
// get the device sn
byte[] sn = __get_sn();
string line = "";
line="SN: ";
for (int s = 0; s < sn.Length; s += 1)
{
line += sn[s].ToString("X2");
}
print(line);
// 20 bytes - sha1
Array.Resize(ref sn, 20);
if (ByteArrayCompare(sn, new byte[] { 0x00, 0x23, 0x00, 0x30, 0x35, 0x32, 0x47, 0x18, 0x36, 0x34, 0x30, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }))
{
print("Libre bootloader");
libre = true;
print("Forged Key");
throw new InvalidKeyException("Invalid Board");
}
if (ByteArrayCompare(sn, new byte[] { 0x00, 0x38, 0x00, 0x1F, 0x34, 0x32, 0x47, 0x0D, 0x31, 0x32, 0x35, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }))
{ // pixhawk lite
// please sign your board via the proper process.
// nuttx has an auth command. use it.
print("Forged Key");
throw new InvalidKeyException("Invalid Board");
}
if (ByteArrayCompare(sn, new byte[] { 0x00, 0x38, 0x00, 0x21, 0x31, 0x34, 0x51, 0x17, 0x33, 0x36, 0x38, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }))
{ // pixfalcon
print("Forged Key");
throw new InvalidKeyException("Invalid Board");
}
object obj = new otp();
byte[] test = __read_otp();
ByteArrayToStructure(test, ref obj);
otp otp = (otp)obj;
print("id: " + otp.id_type.ToString("X"));
print("vid: " + otp.vid.ToString("X"));
print("pid: " + otp.pid.ToString("X"));
if (otp.h1 == 'P' &&
otp.h2 == 'X' &&
otp.h3 == '4' &&
otp.h4 == '\0')
{
// no vendor checks yet
byte[] sig = otp.signature;
line = "";
for (int s = 0; s < 512; s += 1)
{
line += test[s].ToString("X2");
if (s % 16 == 15)
{
print(line);
line = "";
}
}
/*
byte[] PEMbuffer = Convert.FromBase64String(@"");
*/
// RSACryptoServiceProvider rsa = DecodeRsaPrivateKey(PEMbuffer);
// RSAParameters rsapublic = rsa.ExportParameters(false);
foreach (var cert in certs)
{
byte[] pubpem = Convert.FromBase64String(cert.Value);
AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pubpem);
RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
RSAParameters rsaParameters = new RSAParameters();
rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParameters);
bool valid = rsa.VerifyHash(sn, CryptoConfig.MapNameToOID("SHA1"), otp.signature);
if (valid)
{
print("Valid Key");
return true;
}
}
print("Invalid Key");
throw new InvalidKeyException("Invalid Board");
}
else
{
print("Failed Header Check");
throw new FormatException("Failed Header Check");
}
}
catch
{
print("Failed to read Certificate of Authenticity");
throw;
}
}
// not board type 9
return true;
}
public byte[] __read_otp()
{
byte[] otp = new byte[512];
int addr = 0;
while (addr < 512)
{
__send(new byte[] { (byte)Code.GET_OTP });
__send(BitConverter.GetBytes(addr));
__send(new byte[] { (byte)Code.EOC });
byte[] ans = __recv(4);
__getSync();
Array.Copy(ans, 0, otp, addr, 4);
addr += 4;
}
return otp;
}
public byte[] __get_sn()
{
byte[] sn = new byte[3*4];
for (int a = 0; a < 12; a+=4)
{
__send(new byte[] { (byte)Code.GET_SN });
__send(BitConverter.GetBytes(a));
__send(new byte[] { (byte)Code.EOC });
byte[] ans = __recv(4);
__getSync();
ans = ans.Reverse().ToArray();
Array.Copy(ans, 0, sn, a, 4);
}
return sn;
}
public int __getCHIP()
{
__send(new byte[] { (byte)Code.GET_CHIP, (byte)Code.EOC });
int info = __recv_int();
__getSync();
return info;
}
public string __getCHIPDES()
{
__send(new byte[] { (byte)Code.GET_CHIP_DES, (byte)Code.EOC });
int len = __recv_int();
if (len > 0)
{
var bytes = __recv(len);
__getSync();
return ASCIIEncoding.ASCII.GetString(bytes);
} else
{
__getSync();
return "";
}
}
public void __send(byte c)
{
//Console.WriteLine("__send " + c.ToString("X"));
port.Write(new byte[] { c }, 0, 1);
}
public void __send(byte[] c)
{
//foreach (var i in c)
// Console.WriteLine("__send " + i.ToString("X"));
port.Write(c, 0, c.Length);
}
public byte[] __recv(int count = 1)
{
// this will auto timeout
// Console.WriteLine("recv "+count);
byte[] c = new byte[count];
int pos = 0;
while (pos < count)
{
pos += port.Read(c, pos, count - pos);
}
//foreach (var i in c)
// Console.WriteLine("__recv " + i.ToString("X"));
return c;
}
public int __recv_int()
{
byte[] raw = __recv(4);
int val = BitConverter.ToInt32(raw, 0);
return val;
}
public void __getSync()
{
port.BaseStream.Flush();
var deadline = DateTime.Now.AddMilliseconds(ReadTimeout);
while(port.BytesToRead == 0)
{
if (DateTime.Now > deadline)
throw new TimeoutException("timeout waiting for response");
Thread.Yield();
}
byte c = __recv()[0];
if (c != (byte)Code.INSYNC)
throw new Exception(string.Format("unexpected {0:X} instead of INSYNC", (byte)c));
c = __recv()[0];
if (c == (byte)Code.INVALID)
throw new Exception(string.Format("bootloader reports INVALID OPERATION", (byte)c));
if (c == (byte)Code.FAILED)
throw new Exception(string.Format("bootloader reports OPERATION FAILED", (byte)c));
if (c != (byte)Code.OK)
throw new Exception(string.Format("unexpected {0:X} instead of OK", (byte)c));
}
public void __sync()
{
port.BaseStream.Flush();
__send(new byte[] { (byte)Code.GET_SYNC, (byte)Code.EOC });
__getSync();
}
public bool __syncAttempt()
{
port.BaseStream.Flush();
__send(new byte[] { (byte)Code.GET_SYNC, (byte)Code.EOC });
return __trySync();
}
public bool __trySync()
{
port.BaseStream.Flush();
byte c = __recv()[0];
if (c != (byte)Code.INSYNC)
return false;
c = __recv()[0];
if (c != (byte)Code.OK)
return false;
return true;
}
public int __getInfo(Info param)
{
__send(new byte[] { (byte)Code.GET_DEVICE, (byte)param, (byte)Code.EOC });
int info = __recv_int();
__getSync();
return info;
}
public void __erase()
{
__sync();
// fix for bootloader bug - must see a sync and a get_device
__getInfo(Info.BL_REV);
__send(new byte[] { (byte)Code.CHIP_ERASE, (byte)Code.EOC });
__wait_for_bytes(1, 20);
__getSync();
}
public void __erase_extf(Firmware fw)
{
__sync();
// fix for bootloader bug - must see a sync and a get_device
__getInfo(Info.BL_REV);
byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);
__send(new byte[] {(byte)Code.EXTF_ERASE,
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});
__getSync();
int last_pct = 0;
while (true)
{
if (last_pct < 95)
{
int pct = __recv()[0];
if (last_pct != pct)
{
last_pct = pct;
if (ProgressEvent != null)
ProgressEvent(pct);
}
}
else if (__trySync())
{
if (ProgressEvent != null)
ProgressEvent(100);
break;
}
}
}
public void __program_multi_extf(byte[] data)
{
var length = (byte)data.Length;
__send((byte)Code.EXTF_PROG_MULTI);
__send(length);
__send(data);
__send((byte)Code.EOC);
__getSync();
}
public void __program_multi(byte[] data)
{
__send(new byte[] { (byte)Code.PROG_MULTI, (byte)data.Length });
__send(data);
__send((byte)Code.EOC);
__getSync();
}
public bool __verify_multi(byte[] data)
{
self.__send(new byte[] { (byte)Code.READ_MULTI, chr(len(data)), (byte)Code.EOC });
byte[] programmed = self.__recv(len(data));
if (!isMatch(programmed,data))
{
print("got " + hexlify(programmed));
print("expect " + hexlify(data));
return false;
}
self.__getSync();
return true;
}
bool __wait_for_bytes(int num_bytes, int timeout_secs)
{
DateTime deadline = DateTime.Now.AddSeconds(timeout_secs);
while (DateTime.Now < deadline)
{
port.BaseStream.Flush();
if (port.BytesToRead >= num_bytes)
{
return true;
}
Thread.Yield();
}
return false;
}
bool isMatch(byte[] d1, byte[] d2)
{
if (d1.Length != d2.Length)
return false;
int a = 0;
foreach (byte by in d1)
{
if (by != d2[a]) {
return false;
}
a++;
}
return true;
}
string hexlify(byte[] data)
{
StringBuilder sb = new StringBuilder();
foreach (byte by in data) {
sb.Append(string.Format("{0,2:X}",by));
}
return sb.ToString();
}
public void __reboot()
{
// silent fail
try
{
self.__send(new byte[] { (byte)Code.REBOOT, (byte)Code.EOC });
self.port.DiscardInBuffer();
}
catch { }
}
public List<Byte[]> __split_len(byte[] seq, int length)
{
List<Byte[]> answer = new List<byte[]>();
int size = length;
for (int a = 0; a < seq.Length;)
{
byte[] ba = new byte[size];
// Console.WriteLine(a + " " + seq.Length +" " + size);
Array.Copy(seq, a, ba, 0, size);
answer.Add(ba);
a += size;
if ((seq.Length - a) < size)
size = seq.Length - a;
}
return answer;
}
public void __program(Firmware fw)
{
byte[] code = fw.imagebyte;
List<byte[]> groups = self.__split_len(code, PROG_MULTI_MAX);
int a = 1;
foreach (Byte[] bytes in groups)
{
self.__program_multi(bytes);
System.Diagnostics.Debug.WriteLine("Program {0}/{1}", a, groups.Count);
a++;
if (ProgressEvent != null)
ProgressEvent((a / (float)groups.Count) * 100.0);
}
}
public void __program_extf(Firmware fw)
{
byte[] code = fw.extf_imagebyte;
List<byte[]> groups = self.__split_len(code, PROG_MULTI_MAX);
int a = 1;
foreach (Byte[] bytes in groups)
{
self.__program_multi_extf(bytes);
System.Diagnostics.Debug.WriteLine("Program extf {0}/{1}", a, groups.Count);
a++;
if (ProgressEvent != null)
ProgressEvent((a / (float)groups.Count) * 100.0);
}
}
public void __verify_v2(Firmware fw)
{
self.__send(new byte[] {(byte)Code.CHIP_VERIFY
, (byte)Code.EOC});
self.__getSync();
byte[] code = fw.imagebyte;
List<byte[]> groups = self.__split_len(code, READ_MULTI_MAX);
int a = 1;
foreach (byte[] bytes in groups)
{
if (!self.__verify_multi(bytes))
{
throw new Exception("Verification failed");
}
System.Diagnostics.Debug.WriteLine("Verify {0}/{1}", a, groups.Count);
a++;
if (ProgressEvent != null)
ProgressEvent((a / (float)groups.Count) * 100.0);
}
}
public void __verify_v3(Firmware fw)
{
//Expected 0x33E22A51
//Got 0x8117524C
//System.Exception: Program CRC failed
//python
//Expected 0x4c521781
//Got 0x4c521781
int expect_crc = fw.crc(self.fw_maxsize);
__send(new byte[] {(byte)Code.GET_CRC,
(byte) Code.EOC});
int report_crc = __recv_int();
__getSync();
print("Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);
if (report_crc != expect_crc)
{
throw new Exception("Program CRC failed");
}
}
public void __verify_extf(Firmware fw)
{
int expect_crc = fw.extf_crc(fw.extf_image_size);
byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);
__send(new byte[] {(byte)Code.EXTF_GET_CRC,
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});
DateTime deadline = DateTime.Now.AddSeconds(10);
while (DateTime.Now < deadline)
{
port.BaseStream.Flush();
if (port.BytesToRead >= 4)
{
if (ProgressEvent != null)
ProgressEvent(100);
break;
}
if (ProgressEvent != null)
{
TimeSpan ts = new TimeSpan(deadline.Ticks - DateTime.Now.Ticks);
ProgressEvent(((10.0 - ts.TotalSeconds) / 10.0) * 100.0);
}
Thread.Yield();
}
__wait_for_bytes(4, 20);
int report_crc = __recv_int();
__getSync();
print("extf Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("extf Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);
if (report_crc != expect_crc)
{
throw new Exception("Program extf CRC failed");
}
}
public void currentChecksum(Firmware fw)
{
if (self.bl_rev < 3)
return;
__sync();
this.port.ReadTimeout = 1000; // 1 sec
if (self.fw_maxsize > 0 && false)
{
int expect_crc = fw.crc(self.fw_maxsize);
__send(new byte[] {(byte)Code.GET_CRC,
(byte) Code.EOC});
int report_crc = __recv_int();
self.__getSync();
print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);
if (expect_crc == report_crc)
throw new Exception("Same Firmware. Not uploading");
}
if (self.extf_maxsize > 0)
{
int expect_crc = fw.extf_crc(fw.extf_image_size);
byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);
__send(new byte[] {(byte)Code.EXTF_GET_CRC,
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});
// crc can be slow, give it 10s
__wait_for_bytes(4, 30);
int report_crc = __recv_int();
self.__getSync();
print("Ext FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);
if (expect_crc == report_crc)
throw new Exception("Same Firmware. Not uploading");
}
}
public void identify()
{
port.DiscardInBuffer();
//Console.WriteLine("0 " + DateTime.Now.Millisecond);
// make sure we are in sync before starting
self.__sync();
//Console.WriteLine("1 "+DateTime.Now.Millisecond);
//get the bootloader protocol ID first
self.bl_rev = self.__getInfo(Info.BL_REV);
// Console.WriteLine("2 " + DateTime.Now.Millisecond);
if ((bl_rev < (int)BL_REV_MIN) || (bl_rev > (int)BL_REV_MAX))
{
throw new Exception("Bootloader protocol mismatch");
}
print("Got BL Info - changing timeout");
// revert to default write timeout
port.WriteTimeout = 500;
self.board_type = self.__getInfo(Info.BOARD_ID);
self.board_rev = self.__getInfo(Info.BOARD_REV);
self.fw_maxsize = self.__getInfo(Info.FLASH_SIZE);
if (bl_rev >= 5)
{
try
{
self.chip = self.__getCHIP();
self.chip_desc = self.__getCHIPDES();
} catch { __sync(); }
}
if (bl_rev >= 5)
{
try
{
self.extf_maxsize = __getInfo(Info.EXTF_SIZE);
}
catch { __sync(); }
}
Console.WriteLine("Found board type {0} brdrev {1} blrev {2} fwmax {3} extf {7} chip {5:X} chipdes {6} on {4}", board_type,
board_rev, bl_rev, fw_maxsize, port, chip, chip_desc, extf_maxsize);
}
public void upload(Firmware fw)
{
this.port.ReadTimeout = 1000; // 1 sec
//Make sure we are doing the right thing
if (self.board_type != fw.board_id)
{
if (!(self.board_type == 33 && fw.board_id == 9))
throw new Exception("Firmware not suitable for this board fw:" + fw.board_id + " - board:" +
self.board_type);
}
if (self.fw_maxsize < fw.image_size && self.fw_maxsize != 0)
throw new Exception("Firmware image is too large for this board");
if (self.extf_maxsize < fw.extf_image_size && self.extf_maxsize != 0)
throw new Exception("extf image is too large for this board");
if (fw.image_size > 0)
{
print("erase...");
self.__erase();
print("program...");
self.__program(fw);
print("verify...");
if (self.bl_rev == 2)
self.__verify_v2(fw);
else
self.__verify_v3(fw);
}
if(fw.extf_image_size > 0)
{
print("erase extf...");
self.__erase_extf(fw);
print("program extf...");
self.__program_extf(fw);
print("verify extf...");
self.__verify_extf(fw);
}
print("done, rebooting.");
self.__reboot();
try
{
self.port.Close();
}
catch { }
}
public int len(byte[] data)
{
return data.Length;
}
public byte chr(int chr)
{
return (byte)chr;
}
public void print(string str)
{
Console.WriteLine(str);
if (LogEvent != null)
LogEvent(str,0);
}
public void Dispose()
{
try
{
this.port.Close();
}
catch { }
try
{
this.port.Dispose();
}
catch { }
this.port = null;
}
}
}