forked from ivanmeler/SamFirm_Reborn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CmdLine.cs
349 lines (334 loc) · 15 KB
/
CmdLine.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
using System;
using System.IO;
using System.Threading;
namespace hadesFirm
{
internal class CmdLine
{
private static string version = string.Empty;
private static string file = string.Empty;
private static string model = string.Empty;
private static string region = string.Empty;
private static string imei = string.Empty;
private static string logicValue = string.Empty;
private static string folder = string.Empty;
private static bool binary = false;
private static bool autodecrypt = false;
private static bool unzip = true;
private static string metafile = string.Empty;
private static string fwdest = string.Empty;
private static Command.Firmware dfw;
private static bool checkonly = false;
public static CmdLine.ProgressBarInfo progressBar = new CmdLine.ProgressBarInfo();
public static int Main(string[] args)
{
Thread.Sleep(200);
if (CmdLine.InputValidation(args))
return CmdLine.ProcessAction();
CmdLine.DisplayUsage();
return 1;
}
private static int ProcessAction()
{
int num = -1;
if (!string.IsNullOrEmpty(CmdLine.file))
{
if (!string.IsNullOrEmpty(CmdLine.region) && !string.IsNullOrEmpty(CmdLine.imei) && !string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.version))
num = CmdLine.DoDecrypt();
else if (!string.IsNullOrEmpty(CmdLine.logicValue) && !string.IsNullOrEmpty(CmdLine.version))
num = CmdLine.DoDecrypt();
}
else if (!string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.imei) && !string.IsNullOrEmpty(CmdLine.region))
num = !CmdLine.checkonly ? CmdLine.DoDownload() : CmdLine.DoCheck();
if (num == -1)
{
CmdLine.DisplayUsage();
num = 1;
}
return num;
}
private static int DoDecrypt()
{
Logger.WriteLog("========== hadesFirm Firmware Decrypter ==========\n", false);
Logger.WriteLog("Decrypting file " + CmdLine.file + "...", false);
string outputFile = Path.GetFileNameWithoutExtension(CmdLine.file);
CmdLine.CreateProgressbar();
if (CmdLine.file.EndsWith(".enc2"))
Crypto.SetDecryptKey(CmdLine.region, CmdLine.model, CmdLine.version);
else if (CmdLine.file.EndsWith(".enc4"))
Crypto.SetDecryptKey(CmdLine.version, CmdLine.logicValue);
return DecryptToFileOrDirectory(CmdLine.dfw, outputFile);
}
private static int DecryptToFileOrDirectory(Command.Firmware fw, string outputFile)
{
if (CmdLine.unzip)
{
string outputDirectory = Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile));
string metadataFile = Path.Combine(outputDirectory, "FirmwareInfo.txt");
Logger.WriteLog("Unzipping to " + outputDirectory + "...", false);
if (Crypto.DecryptAndUnzip(CmdLine.file, outputDirectory, false) != 0)
{
Logger.WriteLog("\nError decrypting and unzipping file", false);
Logger.WriteLog("Please make sure the filename is not modified and verify the version / logicValue argument", false);
return 3;
}
CmdLine.SaveMeta(fw, metadataFile);
}
else if (Crypto.Decrypt(CmdLine.file, outputFile, false) != 0)
{
Logger.WriteLog("\nError decrypting file", false);
Logger.WriteLog("Please make sure the filename is not modified and verify the version / logicValue argument", false);
File.Delete(Path.GetFileNameWithoutExtension(CmdLine.file));
return 3;
}
if (!string.IsNullOrEmpty(CmdLine.metafile))
CmdLine.SaveMeta(fw, CmdLine.metafile);
Logger.WriteLog("\nDecrypting successful", false);
return 0;
}
private static int DoCheck()
{
Logger.WriteLog("========== hadesFirm Firmware Update Check ==========\n", false);
Command.Firmware firmware;
if (string.IsNullOrEmpty(CmdLine.version))
{
firmware = Command.UpdateCheckAuto(CmdLine.model, CmdLine.region, CmdLine.imei, CmdLine.binary);
if (firmware.FetchAttempts == 0)
return 5;
}
else
firmware = Command.UpdateCheck(CmdLine.model, CmdLine.region, CmdLine.imei, CmdLine.version, CmdLine.binary, false);
return firmware.Version == null ? 2 : 0;
}
private static int DoDownload()
{
Logger.WriteLog("========== hadesFirm Firmware Downloader ==========\n", false);
Command.Firmware fw;
if (string.IsNullOrEmpty(CmdLine.version))
{
fw = Command.UpdateCheckAuto(CmdLine.model, CmdLine.region, CmdLine.imei, CmdLine.binary);
if (fw.FetchAttempts == 0)
return 5;
}
else
fw = Command.UpdateCheck(CmdLine.model, CmdLine.region, CmdLine.imei, CmdLine.version, CmdLine.binary, false);
if (fw.Version == null)
return 2;
dfw = fw;
CmdLine.file = Path.Combine(CmdLine.folder, fw.Filename);
Logger.WriteLog("Downloading...\n", false);
CmdLine.CreateProgressbar();
int num1;
do
{
Utility.ReconnectCmdLine();
Utility.ReconnectDownload = false;
num1 = Command.Download(fw.Path, fw.Filename, fw.Version, fw.Region, fw.Model_Type, CmdLine.file, fw.Size, false);
}
while (Utility.ReconnectDownload);
if (num1 != 200 && num1 != 206)
{
Logger.WriteLog("Error: " + (object) num1, false);
return 4;
}
if (CmdLine.autodecrypt)
{
if (CmdLine.file.EndsWith(".enc2"))
Crypto.SetDecryptKey(fw.Region, fw.Model, fw.Version);
else if (CmdLine.file.EndsWith(".enc4"))
{
if (fw.BinaryNature == 1)
Crypto.SetDecryptKey(fw.Version, fw.LogicValueFactory);
else
Crypto.SetDecryptKey(fw.Version, fw.LogicValueHome);
}
Logger.WriteLog("\nDecrypting...\n", false);
CmdLine.CreateProgressbar();
CmdLine.fwdest = Path.Combine(Path.GetDirectoryName(CmdLine.file), Path.GetFileNameWithoutExtension(fw.Filename));
DecryptToFileOrDirectory(fw, CmdLine.fwdest);
File.Delete(CmdLine.file);
//int num2 = Crypto.Decrypt(str, CmdLine.fwdest, false);
//File.Delete(str);
//if (num2 != 0)
//{
// File.Delete(CmdLine.fwdest);
// return 3;
//}
}
else
{
if (!string.IsNullOrEmpty(CmdLine.metafile))
CmdLine.SaveMeta(fw, CmdLine.metafile);
}
Logger.WriteLog("\nFinished", false);
return 0;
}
private static bool InputValidation(string[] args)
{
if (!CmdLine.ParseArgs(args))
{
Logger.WriteLog("Error parsing arguments\n", false);
return false;
}
if (!string.IsNullOrEmpty(CmdLine.file) && !File.Exists(CmdLine.file))
{
Logger.WriteLog("File " + CmdLine.file + " does not exist\n", false);
return false;
}
if (!string.IsNullOrEmpty(CmdLine.file) && !CmdLine.ParseFileName())
{
Logger.WriteLog("Could not parse filename. Make sure the filename was not edited\n", false);
return false;
}
if (string.IsNullOrEmpty(CmdLine.folder) || Directory.Exists(CmdLine.folder))
return true;
Logger.WriteLog("Folder " + CmdLine.folder + " does not exist\n", false);
return false;
}
public static void SetProgress(int value)
{
if (CmdLine.progressBar.Line == -1)
return;
CmdLine.progressBar.oldLine = Console.CursorTop;
int size = (int) ((double) (Console.BufferWidth - 2) * (double) value / 100.0);
if (CmdLine.progressBar.Progress != size)
{
Console.CursorTop = CmdLine.progressBar.Line;
Console.CursorLeft = 1;
Logger.WriteLog(new string(Utility.GetCharArray(size, '=')), true);
CmdLine.progressBar.Progress = size;
}
Console.CursorTop = CmdLine.progressBar.oldLine;
Console.CursorLeft = 0;
}
private static void DisplayUsage()
{
Logger.WriteLog("Usage:\n", false);
Logger.WriteLog("Update check:", false);
Logger.WriteLog(" hadesFirm.exe -c -model [device model] -region [region code] -imei [imei or serial number]\n [-version [pda/csc/phone/data]] [-binary]", false);
Logger.WriteLog("\nDecrypting:", false);
Logger.WriteLog(" hadesFirm.exe -file [path-to-file.zip.enc2] -version [pda/csc/phone/data] [-meta metafile]", false);
Logger.WriteLog(" hadesFirm.exe -file [path-to-file.zip.enc4] -version [pda/csc/phone/data] -logicValue [logicValue] [-meta metafile]", false);
Logger.WriteLog("\nDownloading:", false);
Logger.WriteLog(" hadesFirm.exe -model [device model] -region [region code] -imei [imei or serial number]\n [-version [pda/csc/phone/data]] [-folder [output folder]]\n [-binary] [-autodecrypt] [-nozip] [-meta metafile]", false);
}
public static void SaveMeta(Command.Firmware fw, string metafile)
{
if (fw.Version == null)
return;
if (!string.IsNullOrEmpty(Path.GetDirectoryName(metafile)) && !Directory.Exists(Path.GetDirectoryName(metafile)))
Directory.CreateDirectory(Path.GetDirectoryName(metafile));
using (TextWriter text = (TextWriter) File.CreateText(metafile))
{
text.WriteLine("[hadesFirmData]");
text.WriteLine("Model=" + fw.Model);
text.WriteLine("Devicename=" + fw.DisplayName);
text.WriteLine("Region=" + fw.Region);
text.WriteLine("Version=" + fw.Version);
text.WriteLine("OS=" + fw.OS);
if (!string.IsNullOrEmpty(CmdLine.fwdest) && File.Exists(CmdLine.fwdest))
{
text.WriteLine("Filesize=" + (object) new FileInfo(CmdLine.fwdest).Length);
text.WriteLine("Filename=" + CmdLine.fwdest);
}
text.WriteLine("ReleaseDate=" + fw.LastModified);
}
}
private static void CreateProgressbar()
{
try
{
Console.CursorTop = Console.CursorTop;
char[] spaceArray = Utility.GetSpaceArray(Console.BufferWidth);
spaceArray[0] = '[';
spaceArray[spaceArray.Length - 1] = ']';
CmdLine.progressBar.Line = Console.CursorTop;
Logger.WriteLog(new string(spaceArray), true);
}
catch (IOException ex)
{
CmdLine.progressBar.Line = -1;
}
}
private static bool ParseArgs(string[] args)
{
if (args.Length < 4 || args.Length > 12)
{
Logger.WriteLog("Error: Not enough / too many arguments", false);
return false;
}
int index1;
for (int index2 = 0; index2 < args.Length; index2 = index1 + 1)
{
string[] strArray = args;
int index3 = index2;
index1 = index3 + 1;
switch (strArray[index3])
{
case "-file":
CmdLine.file = args[index1];
break;
case "-version":
CmdLine.version = args[index1];
break;
case "-logicValue":
CmdLine.logicValue = args[index1];
break;
case "-folder":
CmdLine.folder = args[index1];
break;
case "-region":
CmdLine.region = args[index1].ToUpper();
break;
case "-imei":
CmdLine.imei = args[index1].ToUpper();
break;
case "-model":
CmdLine.model = args[index1];
break;
case "-binary":
--index1;
CmdLine.binary = true;
break;
case "-autodecrypt":
--index1;
CmdLine.autodecrypt = true;
break;
case "-nozip":
--index1;
CmdLine.unzip = false;
break;
case "-c":
--index1;
CmdLine.checkonly = true;
break;
case "-meta":
CmdLine.metafile = args[index1];
break;
default:
return false;
}
}
return true;
}
private static bool ParseFileName()
{
if (CmdLine.file.EndsWith(".enc4"))
return true;
string[] strArray = CmdLine.file.Split('_');
if (strArray.Length < 2)
return false;
CmdLine.model = strArray[0];
if (strArray[1].Length != 3)
return false;
CmdLine.region = strArray[1];
return true;
}
public struct ProgressBarInfo
{
public int Progress;
public int Line;
public int oldLine;
}
}
}