-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMtkPreloader.cs
More file actions
1135 lines (970 loc) · 34.1 KB
/
Copy pathMtkPreloader.cs
File metadata and controls
1135 lines (970 loc) · 34.1 KB
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.Text;
using System.Threading;
using MtkClient.Library.Config;
using MtkClient.Library.Connection;
using MtkClient.Library.Packet;
using MtkClient.Library.Recovery;
using MtkClient.Library.State;
using MtkClient.Library.Utils;
namespace MtkClient.Library
{
public class TargetConfig
{
public bool SecureBoot { get; set; }
public bool SLA { get; set; }
public bool Daa { get; set; }
public bool Sbc { get; set; }
public bool EppParam { get; set; }
public bool IsBrom { get; set; }
public int MeId { get; set; }
public int SocId { get; set; }
public uint RawValue { get; set; }
public override string ToString()
{
return $"SB={SecureBoot}, SLA={SLA}, DAA={Daa}, SBC={Sbc}";
}
}
public class PlCap
{
public bool Brom { get; set; }
public bool Preloader { get; set; }
public ushort HwCode { get; set; }
public ushort HwSubCode { get; set; }
public ushort HwVer { get; set; }
public ushort SwVer { get; set; }
public ushort BlVer { get; set; }
public byte[] RandomId { get; set; }
}
public class BromInfo
{
public ushort HwCode { get; set; }
public ushort HwSubCode { get; set; }
public ushort HwVer { get; set; }
public ushort SwVer { get; set; }
public ushort BlVer { get; set; }
public byte[] MeId { get; set; }
public byte[] SocId { get; set; }
public byte[] BromLog { get; set; }
public TargetConfig TargetConfig { get; set; }
public bool IsBrom { get; set; }
public override string ToString()
{
return $"HW:0x{HwCode:X4} Sub:0x{HwSubCode:X4} Ver:0x{HwVer:X4} SW:0x{SwVer:X4}";
}
}
public class Preloader : LogBase
{
public MtkConfig Config { get; }
public Port Port { get; }
public TargetConfig TargetCfg { get; set; }
public PlCap Capability { get; set; }
public BromInfo DeviceInfo { get; private set; }
public bool IsBrom { get; set; }
public MtkStateManager StateManager { get; }
public RecoveryManager RecoveryMgr { get; }
// BROM Commands
public const byte CMD_GET_HW_CODE = 0xFD;
public const byte CMD_GET_HW_DICT = 0xFC;
public const byte CMD_GET_BL_VER = 0xFE;
public const byte CMD_READ16 = 0xA2;
public const byte CMD_WRITE16 = 0xA4;
public const byte CMD_CHECKSUM16 = 0xA8;
public const byte CMD_READ32 = 0xD1;
public const byte CMD_WRITE32 = 0xD4;
public const byte CMD_JUMP_DA = 0xD5;
public const byte CMD_SEND_DA = 0xD7;
public const byte CMD_GET_TARGET_CONFIG = 0xD8;
public const byte CMD_SEND_EPP = 0xD9;
public const byte CMD_CHECKSUM = 0xDA;
public const byte CMD_UART1_LOG_EN = 0xDB;
public const byte CMD_UART1_SET_BAUDRATE = 0xDC;
public const byte CMD_SET_META_BOOT = 0xE1;
public const byte CMD_SEND_CERT = 0xE2;
public const byte CMD_GET_ME_ID = 0xE1;
public const byte CMD_GET_SOC_ID = 0xE7;
public const byte CMD_GET_BROM_LOG = 0xEF;
public const byte CMD_POWER_DOWN = 0xB7;
public const byte CMD_I2C_INIT = 0xB0;
public const byte CMD_COLD_RESET = 0xB1;
public const byte CMD_SLA_CHALLENGE = 0xB4;
public const byte CMD_PWR_INIT = 0xC4;
public const byte CMD_PWR_DEINIT = 0xC5;
public const byte CMD_PWR_READ16 = 0xC6;
public const byte CMD_PWR_WRITE16 = 0xC7;
// ACK/NACK
public const ushort ACK = 0x5A5A;
public const ushort NACK = 0xA5A5;
// BROM Sync
public static readonly byte[] SYNC_SEQUENCE = { 0xA0, 0x0A, 0x50, 0x05 };
// Timeouts
private const int CMD_TIMEOUT = 2000;
private const int LONG_TIMEOUT = 10000;
private const int SYNC_TIMEOUT = 50;
public Preloader(MtkConfig config, Port port, LogLevel logLevel = LogLevel.Info,
MtkStateManager stateManager = null, RecoveryManager recoveryManager = null)
{
InitLogger(logLevel);
Config = config;
Port = port;
TargetCfg = new TargetConfig();
Capability = new PlCap();
DeviceInfo = new BromInfo();
StateManager = stateManager ?? new MtkStateManager(logLevel);
RecoveryMgr = recoveryManager ?? new RecoveryManager(StateManager, logLevel);
}
#region BROM Handshake & Sync
public bool BromSync(int maxRetries = 50, CancellationToken cancellationToken = default)
{
Info("Starting BROM sync...");
for (int attempt = 0; attempt < maxRetries; attempt++)
{
if (cancellationToken.IsCancellationRequested) return false;
try
{
bool syncOk = true;
for (int i = 0; i < SYNC_SEQUENCE.Length; i++)
{
Port.UsbWrite(new[] { SYNC_SEQUENCE[i] });
var resp = Port.UsbRead(1, SYNC_TIMEOUT);
byte expected = (byte)(~SYNC_SEQUENCE[i] & 0xFF);
if (resp == null || resp.Length == 0 || resp[0] != expected)
{
syncOk = false;
break;
}
}
if (syncOk)
{
Info($"BROM sync OK (attempt {attempt + 1})");
IsBrom = true;
StateManager.TransitionTo(DeviceState.BromMode, "BROM sync");
return true;
}
}
catch (Exception ex)
{
Debug($"Sync attempt {attempt + 1} failed: {ex.Message}");
}
Thread.Sleep(50);
}
Warning("BROM sync failed");
return false;
}
public bool DetectMode()
{
try
{
var hw = GetHwCode();
if (hw.HasValue)
{
IsBrom = true;
StateManager.TransitionTo(DeviceState.BromMode, "HW code received");
return true;
}
}
catch { }
try
{
// Try preloader detection
Port.Cdc.Flush();
var data = Port.UsbRead(1, 200);
if (data != null && data.Length > 0)
{
IsBrom = false;
StateManager.TransitionTo(DeviceState.PreloaderMode, "Preloader detected");
return true;
}
}
catch { }
return false;
}
#endregion
#region Core BROM Commands
public ushort? GetHwCode()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_HW_CODE });
var ack = Port.RWord();
if (ack != ACK) return null;
var hwCode = Port.RWord();
var hwSubCode = Port.RWord();
var hwVer = Port.RWord();
var swVer = Port.RWord();
if (hwCode.HasValue)
{
Config.HwCode = hwCode.Value;
Capability.HwCode = hwCode.Value;
Capability.HwSubCode = hwSubCode ?? 0;
Capability.HwVer = hwVer ?? 0;
Capability.SwVer = swVer ?? 0;
DeviceInfo.HwCode = hwCode.Value;
DeviceInfo.HwSubCode = hwSubCode ?? 0;
DeviceInfo.HwVer = hwVer ?? 0;
DeviceInfo.SwVer = swVer ?? 0;
Info($"HW Code: 0x{hwCode.Value:X4}");
return hwCode.Value;
}
}
catch (Exception ex)
{
Error($"GetHwCode failed: {ex.Message}");
}
return null;
}
public ushort? GetHwDict()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_HW_DICT });
var ack = Port.RWord();
if (ack != ACK) return null;
var hwSubCode = Port.RWord();
var hwVer = Port.RWord();
var swVer = Port.RWord();
if (hwSubCode.HasValue)
{
Capability.HwSubCode = hwSubCode.Value;
Capability.HwVer = hwVer ?? 0;
Capability.SwVer = swVer ?? 0;
return hwSubCode;
}
}
catch (Exception ex)
{
Error($"GetHwDict failed: {ex.Message}");
}
return null;
}
public ushort? GetBlVer()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_BL_VER });
var ack = Port.RWord();
if (ack != ACK) return null;
var blVer = Port.RWord();
if (blVer.HasValue)
{
Capability.BlVer = blVer.Value;
DeviceInfo.BlVer = blVer.Value;
Config.BlVer = blVer.Value;
Debug($"BL Version: 0x{blVer.Value:X4}");
}
return blVer;
}
catch (Exception ex)
{
Error($"GetBlVer failed: {ex.Message}");
return null;
}
}
public TargetConfig GetTargetConfig()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_TARGET_CONFIG });
var ack = Port.RWord();
if (ack != ACK) return TargetCfg;
var targetConfig = Port.RdWord();
if (targetConfig.HasValue)
{
TargetCfg.RawValue = targetConfig.Value;
TargetCfg.SecureBoot = (targetConfig.Value & 1) != 0;
TargetCfg.SLA = (targetConfig.Value & 2) != 0;
TargetCfg.Daa = (targetConfig.Value & 4) != 0;
TargetCfg.Sbc = (targetConfig.Value & 8) != 0;
TargetCfg.EppParam = (targetConfig.Value & 0x10) != 0;
DeviceInfo.TargetConfig = TargetCfg;
// Update security state
if (TargetCfg.Daa)
StateManager.SetSecurityState(SecurityState.Daa);
else if (TargetCfg.SLA)
StateManager.SetSecurityState(SecurityState.Sla);
else if (TargetCfg.SecureBoot)
StateManager.SetSecurityState(SecurityState.SecureBoot);
else
StateManager.SetSecurityState(SecurityState.Unlocked);
Info($"Target config: {TargetCfg}");
}
Port.RWord(); // status
return TargetCfg;
}
catch (Exception ex)
{
Error($"GetTargetConfig failed: {ex.Message}");
return TargetCfg;
}
}
#endregion
#region Memory Access
public byte[] Read32(uint addr, int length = 4)
{
try
{
Port.UsbWrite(new byte[] { CMD_READ32 });
var ack = Port.RWord();
if (ack != ACK) return null;
Port.UsbWrite(MtkUtils.Pack32BE(addr));
Port.UsbWrite(MtkUtils.Pack32BE((uint)length));
ack = Port.RWord();
if (ack != ACK) return null;
var data = Port.UsbRead(length);
Port.RWord(); // status
return data;
}
catch (Exception ex)
{
Error($"Read32 0x{addr:X8} failed: {ex.Message}");
return null;
}
}
public uint? Read32Value(uint addr)
{
var data = Read32(addr, 4);
if (data != null && data.Length >= 4)
return MtkUtils.Unpack32BE(data);
return null;
}
public byte[] Read32Batch(uint addr, int totalLength)
{
const int MAX_CHUNK = 0x1000;
var result = new byte[totalLength];
int offset = 0;
while (offset < totalLength)
{
int chunkLen = Math.Min(MAX_CHUNK, totalLength - offset);
var chunk = Read32(addr + (uint)offset, chunkLen);
if (chunk == null) return null;
Array.Copy(chunk, 0, result, offset, chunk.Length);
offset += chunk.Length;
}
return result;
}
public bool Write32(uint addr, uint value)
{
try
{
Port.UsbWrite(new byte[] { CMD_WRITE32 });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE(addr));
Port.UsbWrite(MtkUtils.Pack32BE(4));
Port.UsbWrite(MtkUtils.Pack32BE(value));
ack = Port.RWord();
if (ack != ACK) return false;
Port.RWord(); // status
return true;
}
catch (Exception ex)
{
Error($"Write32 0x{addr:X8} failed: {ex.Message}");
return false;
}
}
public bool Write32Batch(uint addr, byte[] data)
{
const int MAX_CHUNK = 0x1000;
int offset = 0;
while (offset < data.Length)
{
int chunkLen = Math.Min(MAX_CHUNK, data.Length - offset);
try
{
Port.UsbWrite(new byte[] { CMD_WRITE32 });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE(addr + (uint)offset));
Port.UsbWrite(MtkUtils.Pack32BE((uint)chunkLen));
for (int i = 0; i < chunkLen; i += 4)
{
int wordLen = Math.Min(4, chunkLen - i);
var word = new byte[4];
Array.Copy(data, offset + i, word, 0, wordLen);
Port.UsbWrite(word);
}
ack = Port.RWord();
if (ack != ACK) return false;
Port.RWord(); // status
}
catch (Exception ex)
{
Error($"Write32Batch 0x{addr + (uint)offset:X8} failed: {ex.Message}");
return false;
}
offset += chunkLen;
}
return true;
}
public byte[] Read16(uint addr, int length = 2)
{
try
{
Port.UsbWrite(new byte[] { CMD_READ16 });
var ack = Port.RWord();
if (ack != ACK) return null;
Port.UsbWrite(MtkUtils.Pack32BE(addr));
Port.UsbWrite(MtkUtils.Pack32BE((uint)length));
ack = Port.RWord();
if (ack != ACK) return null;
var data = Port.UsbRead(length);
Port.RWord(); // status
return data;
}
catch (Exception ex)
{
Error($"Read16 0x{addr:X8} failed: {ex.Message}");
return null;
}
}
public bool Write16(uint addr, ushort value)
{
try
{
Port.UsbWrite(new byte[] { CMD_WRITE16 });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE(addr));
Port.UsbWrite(MtkUtils.Pack32BE(2));
Port.UsbWrite(new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) });
ack = Port.RWord();
if (ack != ACK) return false;
Port.RWord(); // status
return true;
}
catch (Exception ex)
{
Error($"Write16 0x{addr:X8} failed: {ex.Message}");
return false;
}
}
#endregion
#region Memory Operations (Patch, Dump, Verify)
public bool PatchMemory(uint addr, byte[] original, byte[] patched)
{
if (original != null && original.Length > 0)
{
var current = Read32(addr, original.Length);
if (current == null) return false;
bool match = true;
for (int i = 0; i < original.Length && i < current.Length; i++)
{
if (current[i] != original[i]) { match = false; break; }
}
if (!match)
{
Warning($"Memory at 0x{addr:X8} doesn't match expected pattern");
return false;
}
}
return Write32Batch(addr, patched);
}
public bool VerifyMemory(uint addr, byte[] expected)
{
var actual = Read32(addr, expected.Length);
if (actual == null || actual.Length != expected.Length) return false;
for (int i = 0; i < expected.Length; i++)
{
if (actual[i] != expected[i]) return false;
}
return true;
}
public byte[] DumpMemory(uint addr, int length, string label = null)
{
Info($"Dumping memory: 0x{addr:X8} len=0x{length:X} {label ?? ""}");
var progress = new Progress(length, prefix: label ?? "Dump");
var result = new byte[length];
int offset = 0;
const int chunkSize = 0x1000;
while (offset < length)
{
int chunk = Math.Min(chunkSize, length - offset);
var data = Read32(addr + (uint)offset, chunk);
if (data == null)
{
Error($"Dump failed at offset 0x{offset:X}");
return null;
}
Array.Copy(data, 0, result, offset, data.Length);
offset += data.Length;
progress.Update(data.Length);
}
return result;
}
#endregion
#region DA Upload & Jump
public bool SendDa(uint loadAddr, byte[] data, int sigLen = 0x100)
{
try
{
StateManager.SetDaStage(DaStage.Stage1Uploading);
Port.UsbWrite(new byte[] { CMD_SEND_DA });
var ack = Port.RWord();
if (ack != ACK)
{
Error("SendDA: command not acknowledged");
return false;
}
Port.UsbWrite(MtkUtils.Pack32BE(loadAddr));
Port.UsbWrite(MtkUtils.Pack32BE((uint)data.Length));
Port.UsbWrite(MtkUtils.Pack32BE((uint)sigLen));
ack = Port.RWord();
if (ack != ACK)
{
Error("SendDA: parameters not acknowledged");
return false;
}
int chunkSize = 4096;
var progress = new Progress(data.Length, prefix: "Sending DA");
for (int offset = 0; offset < data.Length; offset += chunkSize)
{
int len = Math.Min(chunkSize, data.Length - offset);
var chunk = new byte[len];
Array.Copy(data, offset, chunk, 0, len);
Port.UsbWrite(chunk);
progress.Update(len);
}
ushort checksum = MtkUtils.CalcChecksum(data);
Port.UsbWrite(MtkUtils.Pack16LE(checksum));
ack = Port.RWord();
if (ack == ACK)
{
Info("DA uploaded successfully");
return true;
}
Error($"SendDA: checksum NACK (expected ACK=0x{ACK:X4}, got 0x{ack:X4})");
return false;
}
catch (Exception ex)
{
Error($"SendDA failed: {ex.Message}");
StateManager.SetDaStage(DaStage.Error);
return false;
}
}
public bool SendDaWithRetry(uint loadAddr, byte[] data, int sigLen = 0x100, int maxRetries = 3)
{
return RecoveryMgr.ExecuteWithRetry(
() =>
{
if (!SendDa(loadAddr, data, sigLen))
throw new Exception("SendDA failed");
},
"SendDA",
new RetryPolicy { MaxRetries = maxRetries, InitialDelayMs = 500 }
);
}
public bool JumpDa(uint entryAddr)
{
try
{
Port.UsbWrite(new byte[] { CMD_JUMP_DA });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE(entryAddr));
ack = Port.RWord();
if (ack == ACK)
{
Info($"Jumped to DA at 0x{entryAddr:X8}");
StateManager.SetDaStage(DaStage.Stage1Running);
StateManager.TransitionTo(DeviceState.DaMode, "DA jump");
return true;
}
Error($"JumpDA: NACK at entry 0x{entryAddr:X8}");
return false;
}
catch (Exception ex)
{
Error($"JumpDA failed: {ex.Message}");
return false;
}
}
#endregion
#region Device Info Commands
public byte[] GetMeId()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_ME_ID });
var ack = Port.RWord();
if (ack != ACK) return null;
var lenData = Port.RdWord();
if (lenData == null) return null;
var meid = Port.UsbRead((int)lenData.Value);
Port.RWord(); // status
if (meid != null)
{
Config.SetMeid(meid);
DeviceInfo.MeId = meid;
Debug($"ME ID: {MtkUtils.ToHex(meid)}");
}
return meid;
}
catch (Exception ex)
{
Error($"GetMeId failed: {ex.Message}");
return null;
}
}
public byte[] GetSocId()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_SOC_ID });
var ack = Port.RWord();
if (ack != ACK) return null;
var lenData = Port.RdWord();
if (lenData == null) return null;
var socId = Port.UsbRead((int)lenData.Value);
Port.RWord(); // status
if (socId != null)
{
Config.SetSocId(socId);
DeviceInfo.SocId = socId;
Debug($"SOC ID: {MtkUtils.ToHex(socId)}");
}
return socId;
}
catch (Exception ex)
{
Error($"GetSocId failed: {ex.Message}");
return null;
}
}
public byte[] GetBromLog()
{
try
{
Port.UsbWrite(new byte[] { CMD_GET_BROM_LOG });
var ack = Port.RWord();
if (ack != ACK) return null;
var lenData = Port.RdWord();
if (lenData == null) return null;
var log = Port.UsbRead((int)lenData.Value);
if (log != null)
DeviceInfo.BromLog = log;
return log;
}
catch (Exception ex)
{
Error($"GetBromLog failed: {ex.Message}");
return null;
}
}
public BromInfo GetFullDeviceInfo()
{
Info("Reading full device info...");
GetHwCode();
GetHwDict();
GetBlVer();
GetTargetConfig();
GetMeId();
if (Config.ReadSocId)
GetSocId();
DeviceInfo.IsBrom = IsBrom;
Info($"Device: {DeviceInfo}");
return DeviceInfo;
}
#endregion
#region Security & Auth
public bool SendAuth(byte[] authData)
{
if (authData == null || authData.Length == 0)
{
Warning("No auth data provided");
return false;
}
try
{
Port.UsbWrite(new byte[] { CMD_SEND_CERT });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE((uint)authData.Length));
Port.UsbWrite(authData);
ack = Port.RWord();
if (ack == ACK)
{
Info("Auth data accepted");
return true;
}
Warning("Auth data rejected");
return false;
}
catch (Exception ex)
{
Error($"SendAuth failed: {ex.Message}");
return false;
}
}
public bool SendCert(byte[] certData)
{
if (certData == null || certData.Length == 0)
{
Warning("No cert data provided");
return false;
}
try
{
Port.UsbWrite(new byte[] { CMD_SEND_CERT });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE((uint)certData.Length));
Port.UsbWrite(certData);
ack = Port.RWord();
if (ack == ACK)
{
Info("Certificate accepted");
return true;
}
Warning("Certificate rejected");
return false;
}
catch (Exception ex)
{
Error($"SendCert failed: {ex.Message}");
return false;
}
}
public byte[] SlaChallenge()
{
try
{
Port.UsbWrite(new byte[] { CMD_SLA_CHALLENGE });
var ack = Port.RWord();
if (ack != ACK) return null;
var lenData = Port.RdWord();
if (lenData == null || lenData.Value == 0) return null;
var challenge = Port.UsbRead((int)lenData.Value);
Debug($"SLA challenge: {MtkUtils.ToHex(challenge)}");
return challenge;
}
catch (Exception ex)
{
Error($"SlaChallenge failed: {ex.Message}");
return null;
}
}
public bool SlaResponse(byte[] response)
{
if (response == null) return false;
try
{
Port.UsbWrite(MtkUtils.Pack32BE((uint)response.Length));
Port.UsbWrite(response);
var ack = Port.RWord();
if (ack == ACK)
{
Info("SLA authentication successful");
return true;
}
Warning("SLA authentication failed");
return false;
}
catch (Exception ex)
{
Error($"SlaResponse failed: {ex.Message}");
return false;
}
}
public bool SendEpp(byte[] eppData)
{
if (eppData == null) return false;
try
{
Port.UsbWrite(new byte[] { CMD_SEND_EPP });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(MtkUtils.Pack32BE((uint)eppData.Length));
Port.UsbWrite(eppData);
ack = Port.RWord();
return ack == ACK;
}
catch (Exception ex)
{
Error($"SendEpp failed: {ex.Message}");
return false;
}
}
#endregion
#region Watchdog & Power
public void DisableWatchdog()
{
if (Config.ChipConfig.Watchdog.HasValue)
{
Write32(Config.ChipConfig.Watchdog.Value, 0x22000064);
Info("Watchdog disabled");
}
}
public void DisableWatchdogViaReg(uint watchdogAddr)
{
Write32(watchdogAddr, 0x22000064);
Debug($"Watchdog disabled at 0x{watchdogAddr:X8}");
}
public void PowerDown()
{
try
{
Port.UsbWrite(new byte[] { CMD_POWER_DOWN });
StateManager.TransitionTo(DeviceState.Disconnected, "Power down");
}
catch { }
}
public void ColdReset()
{
try
{
Port.UsbWrite(new byte[] { CMD_COLD_RESET });
StateManager.TransitionTo(DeviceState.Rebooting, "Cold reset");
}
catch { }
}
#endregion
#region PMIC / Power Management
public bool PmicInit()
{
try
{
Port.UsbWrite(new byte[] { CMD_I2C_INIT });
var ack = Port.RWord();
if (ack != ACK) return false;
Port.UsbWrite(new byte[] { CMD_PWR_INIT });
ack = Port.RWord();
return ack == ACK;
}
catch (Exception ex)
{
Error($"PmicInit failed: {ex.Message}");
return false;
}
}
public ushort? PmicRead16(ushort addr)
{
try
{
Port.UsbWrite(new byte[] { CMD_PWR_READ16 });
var ack = Port.RWord();
if (ack != ACK) return null;
Port.UsbWrite(new byte[] { (byte)(addr >> 8), (byte)(addr & 0xFF) });
var value = Port.RWord();
Port.RWord(); // status
return value;
}
catch (Exception ex)
{
Error($"PmicRead16 failed: {ex.Message}");