-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMtkClass.cs
More file actions
227 lines (197 loc) · 7.5 KB
/
Copy pathMtkClass.cs
File metadata and controls
227 lines (197 loc) · 7.5 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
using System;
using System.IO;
using System.Threading;
using MtkClient.Library.Config;
using MtkClient.Library.Connection;
using MtkClient.Library.DA;
using MtkClient.Library.Exploit;
using MtkClient.Library.Flash;
using MtkClient.Library.Logging;
using MtkClient.Library.Payload;
using MtkClient.Library.Recovery;
using MtkClient.Library.State;
using MtkClient.Library.Utils;
namespace MtkClient.Library
{
public class Mtk : LogBase
{
public MtkConfig Config { get; }
public Port Port { get; set; }
public Preloader Preloader { get; set; }
public DaLoader DaLoader { get; set; }
public ExploitHandler ExploitHandler { get; set; }
public MtkStateManager StateManager { get; }
public RecoveryManager RecoveryMgr { get; }
public FlashEngine FlashEngine { get; set; }
public PayloadEngine PayloadEngine { get; set; }
public Meta MetaHandler { get; set; }
public MtkLogger Logger { get; }
public bool Connected { get; private set; }
public Mtk(MtkConfig config = null, LogLevel logLevel = LogLevel.Info)
{
InitLogger(logLevel);
Config = config ?? new MtkConfig(logLevel);
StateManager = new MtkStateManager(logLevel);
RecoveryMgr = new RecoveryManager(StateManager, logLevel);
Logger = MtkLogger.Instance;
Logger.MinLevel = logLevel;
}
public bool Setup(string serialPort = null, CancellationToken cancellationToken = default)
{
try
{
StateManager.TransitionTo(DeviceState.Connecting, "Setup");
Port = new Port(Config, Config.Iot ? UsbIds.DefaultIds : null, serialPort, Config.LogLevel);
if (!Port.RunHandshake())
{
Error("Failed to connect to device");
StateManager.TransitionTo(DeviceState.Error, "Handshake failed");
return false;
}
Preloader = new Preloader(Config, Port, Config.LogLevel, StateManager, RecoveryMgr);
// BROM sync
if (!Preloader.BromSync(cancellationToken: cancellationToken))
{
Warning("BROM sync failed, trying direct mode...");
}
// Get HW code
var hwCode = Preloader.GetHwCode();
if (hwCode == null)
{
Error("Failed to get HW code");
return false;
}
Config.HwCode = hwCode.Value;
Config.ChipConfig = HwConfig.GetConfig(hwCode.Value);
Info($"Device: {Config.ChipConfig.Name} (0x{hwCode.Value:X4})");
// Disable watchdog
if (!Config.SkipWdt)
Preloader.DisableWatchdog();
// Get target config
Preloader.GetTargetConfig();
Preloader.GetMeId();
if (Config.ReadSocId)
Preloader.GetSocId();
// Initialize DA loader
DaLoader = new DaLoader(Config, Port, Preloader, StateManager, RecoveryMgr, Config.LogLevel);
// Initialize exploit handler
ExploitHandler = new ExploitHandler(Config, Port,
(addr, len) => Preloader.Read32(addr, len),
(addr, val) => Preloader.Write32(addr, val),
StateManager, RecoveryMgr,
Config.LogLevel);
// Initialize payload engine
PayloadEngine = new PayloadEngine(Preloader, Config, Config.LogLevel);
// Initialize META handler
MetaHandler = new Meta(Config, Port, StateManager, Config.LogLevel);
Connected = true;
return true;
}
catch (Exception ex)
{
Error($"Setup failed: {ex.Message}");
Logger.LogCrash("Mtk.Setup", ex, LogCategory.General);
StateManager.TransitionTo(DeviceState.Error, ex.Message);
return false;
}
}
public bool StartDa(string daPath = null)
{
if (DaLoader == null)
{
Error("Not connected");
return false;
}
if (!string.IsNullOrEmpty(daPath))
DaLoader.SetDa(daPath);
else if (!string.IsNullOrEmpty(Config.Loader))
DaLoader.SetDa(Config.Loader);
else
DaLoader.AutoSelectDa();
if (!DaLoader.UploadDa())
{
Error("DA upload failed");
return false;
}
if (!DaLoader.StartWithRecovery())
{
Error("DA start failed");
return false;
}
// Initialize flash engine
FlashEngine = new FlashEngine(DaLoader, Config, RecoveryMgr, Config.LogLevel);
Info("DA started, flash engine ready");
return true;
}
public ExploitResult BypassSecurity(string exploitName = null)
{
if (ExploitHandler == null)
{
Error("Not connected");
return new ExploitResult { Success = false, Message = "Not connected" };
}
return ExploitHandler.RunExploitWithRecovery(exploitName ?? Config.Ptype);
}
public bool Crasher()
{
Info("Sending crash sequence...");
try
{
for (int i = 0; i < 10; i++)
Port.UsbWrite(new byte[] { 0x00 });
return true;
}
catch (Exception ex)
{
Error($"Crasher failed: {ex.Message}");
return false;
}
}
public byte[] ParsePreloader(byte[] data)
{
if (data == null || data.Length < 4) return null;
int idx = MtkUtils.FindBinary(data, new byte[] { 0x4D, 0x4D, 0x4D, 0x01 });
if (idx != -1)
{
Info($"Found preloader header at offset 0x{idx:X}");
var preloader = new byte[data.Length - idx];
Array.Copy(data, idx, preloader, 0, preloader.Length);
return preloader;
}
return data;
}
public bool PatchPreloaderSecurityDa1(byte[] data)
{
if (data == null) return false;
var pattern = new byte[] { 0x01, 0x00, 0xA0, 0xE3, 0x1E, 0xFF, 0x2F, 0xE1 };
int idx = MtkUtils.FindBinary(data, pattern);
if (idx != -1)
{
data[idx] = 0x00;
Info($"Patched security check DA1 at 0x{idx:X}");
return true;
}
return false;
}
public bool PatchPreloaderSecurityDa2(byte[] data)
{
if (data == null) return false;
var pattern = new byte[] { 0x01, 0x00, 0xA0, 0xE3, 0x1E, 0xFF, 0x2F, 0xE1 };
int idx = MtkUtils.FindBinary(data, pattern);
if (idx != -1)
{
data[idx] = 0x00;
Info($"Patched security check DA2 at 0x{idx:X}");
return true;
}
return false;
}
public void Close()
{
DaLoader?.Close();
Port?.Close();
Connected = false;
StateManager.TransitionTo(DeviceState.Disconnected, "Closed");
}
}
}