-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestProgram.cs
More file actions
577 lines (556 loc) · 24.8 KB
/
TestProgram.cs
File metadata and controls
577 lines (556 loc) · 24.8 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
using ASRClientCore.DeviceManager;
using ASRClientCore.Models.Enums;
using ASRClientCore.Models.Exceptions;
using ASRClientCore.Models.Interfaces;
using ASRClientCore.ProtocolHandlers;
using ASRClientCore.Utils;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Text;
using SPRDClientCore.Utils;
using ASRClientCore.Models;
using System.Net.WebSockets;
namespace ASRClientCore
{
class TestProgram
{
private static readonly object _lock = new object();
public static void Log(string log)
{
Console.Write("[Log] ");
Console.WriteLine(log);
}
public static void Log(string log, ConsoleColor color)
{
var origColor = Console.ForegroundColor;
lock (_lock)
{
Console.Write("[Log] ");
Console.ForegroundColor = color;
Console.WriteLine(log);
Console.ForegroundColor = origColor;
}
}
static void Main(string[] args)
{
ConnectionConfig cfg = ConnectionConfig.Parse(ref args);
Console.WriteLine($"Waiting for device connecting ({cfg.WaitTime / 1000}s). Connect your device directly just after powering down");
Console.WriteLine("ASRClientCore by YC, QQ:1145145343");
IAsrProtocolHandler device;
try
{
device = AsrAdbProtocolHandler.FindAndOpen(cfg.WaitTime);
}
catch
{
Console.WriteLine("fail to connect to device");
return;
}
device.Timeout = cfg.Timeout;
RequestManager rm = new RequestManager(device);
FlashManager fm = new FlashManager(rm);
try
{
ConsoleProgressBar progressBar = new ConsoleProgressBar();
fm.UpdatePercentage += progressBar.UpdateProgress;
fm.Log += msg => Console.WriteLine(msg);
DeviceStatus status = new DeviceStatus();
CommandExecutor executor = new CommandExecutor(fm, status);
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
executor.CancelAction();
};
try { fm.ErasePartition("fuck_you_asr"); } catch (BadResponseException) { }
executor.Execute(args.ToList());
while (!status.HasExited)
{
Console.Write("SPL >");
string? input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) continue;
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase) || input.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
status.HasExited = true;
continue;
}
executor.Execute(input);
}
}
catch (Exception ex)
{
Log($"error :{ex.Message}");
}
finally
{
fm.Dispose();
}
}
public class DeviceStatus
{
public bool HasExited { get; set; }
}
public class ConnectionConfig
{
public uint WaitTime { get; private set; } = 30000;
public uint Timeout { get; private set; } = 5000;
public static ConnectionConfig Parse(ref string[] args)
{
ConnectionConfig config = new();
List<string> cmds = new();
for (int i = 0; i < args.Length; i++)
{
string arg = args[i].ToLowerInvariant();
bool matched = true;
switch (arg)
{
case "--wait":
if (i + 1 < args.Length && uint.TryParse(args[i + 1], out uint waitTime))
{
config.WaitTime = waitTime * 1000;
i++;
}
break;
case "--timeout":
if (i + 1 < args.Length && uint.TryParse(args[i + 1], out uint timeout))
{
config.Timeout = timeout;
i++;
}
break;
default:
matched = false;
break;
}
if (!matched)
{
cmds.Add(args[i]);
}
}
args = cmds.ToArray();
return config;
}
}
class ConsoleProgressBar
{
public int BarWidth { get; set; } = 25;
public void UpdateProgress(int percentage)
{
if (percentage > 100) percentage = 100;
else if (percentage < 0) percentage = 0;
int progressWidth = (int)(percentage / 100.0 * BarWidth);
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
lock (_lock)
{
Console.CursorLeft = 0;
Console.Write('[');
Console.Write(new string('#', progressWidth));
Console.Write(new string(' ', BarWidth - progressWidth));
Console.Write(']');
Console.Write($"{percentage}%");
Console.ForegroundColor = tmp;
if (percentage == 100) Console.WriteLine();
}
}
public void UpdateSpeed(string speed)
{
lock (_lock)
{
var tmp = Console.CursorLeft;
Console.CursorLeft = BarWidth + 10;
Console.Write(speed);
Console.CursorLeft = tmp;
}
}
}
public class CommandExecutor(FlashManager manager, DeviceStatus status)
{
private readonly FlashManager manager = manager;
private readonly DeviceStatus status = status;
private CancellationTokenSource cts = new();
private string commandHelp =
@"指令帮助
[]内的参数必填, <>内的参数选填
参数指令:
--timeout [毫秒时间]:设置最大超时限制
--wait [秒数]:设置等待设备连接的时间(默认30秒)
运行时指令:
获取分区表:pl/partition_list <保存路径>(获取后设备永远卡死,必须手动重新重启)
重新分区:rp/repartition [分区表路径]
写入分区:w/write_part [分区名] [文件路径] (须先读取或擦除相应分区)
回读分区:r/read_part [分区名] <保存路径>
擦除分区:e/erase_part [分区名]
设置活动分区槽位: sas/set_active_slot [a或b]
写入内存:s/w_mem [文件路径] [地址] [模式] <分区名称>
备份全机:backup [分区表文件路径] <保存路径> (保存路径为文件夹路径)
恢复全机:restore [备份文件夹路径]
读取内存:p/pull_mem/read_mem [读取大小] [内存地址] <保存路径>
查找可读取的内存地址:p_loop
获取设备信息:info
关机: off/poweroff
开机(至XX模式): rst/reset <模式>
参数设置指令:
设置块大小:blk_size/bs [大小]
设置最大超时限制: timeout [毫秒时间]
开机模式:
0:Normal
1:DisconnectUSB,
2:Normal1,
3:BootLoader,
4:Calibration,
5:Ata,
6:CurrentTest,
7:UDL,
0x14:PowerOff
0x15:ColdRebootToNormal
写入内存模式:
0:Write(Send)Only,
1:WriteAndExecute,
2:WritePartition";
private static readonly HashSet<string> CommandKeys = new(StringComparer.OrdinalIgnoreCase)
{
"repartition","rp",
"pl","partition_list",
"r","read_part",
"w","write_part",
"e","erase_part",
"sas","set_active_slot",
"s","w_mem",
"backup","restore",
"p","pull_mem","read_mem",
"p_loop",
"info",
"off","poweroff",
"rst","reset",
"blk_size","bs",
"timeout",
};
private static List<string> ParseCommand(string command)
{
var result = new List<string>();
var sb = new StringBuilder();
bool inPath = false;
foreach (char c in command)
{
if (c == '"') inPath = !inPath;
else if (c == ' ' && !inPath)
{
if (sb.Length > 0)
{
result.Add(sb.ToString());
sb.Clear();
}
}
else sb.Append(c);
}
if (sb.Length > 0) result.Add(sb.ToString());
return result;
}
public void Execute(string command) => Execute(ParseCommand(command));
public void Execute(List<string> tokens)
{
var subCommands = new List<List<string>>();
List<string>? current = null;
foreach (var tok in tokens)
{
if (CommandKeys.Contains(tok))
{
current = new List<string> { tok };
subCommands.Add(current);
}
else if (current != null)
{
current.Add(tok);
}
else
{
Log(commandHelp);
return;
}
}
foreach (var args in subCommands)
{
bool shouldExit = ExecuteSingle(args);
if (shouldExit)
break;
}
}
public bool ExecuteSingle(List<string> args)
{
try
{
if (args.Count > 0)
switch (args[0])
{
default: Log(commandHelp); break;
case "pl" or "partition_list":
string path = args.Count > 1 ? args[1] : "partition.xml";
List<Partition> partitions = manager.GetPartitionList();
using (FileStream fs = File.Create(path))
PartitionToXml.SavePartitionsToXml(partitions, fs);
foreach (var part in partitions)
Console.WriteLine(part.ToString());
Log("please reboot your device by pressing power button for 10 more seconds");
status.HasExited = true;
break;
case "repartition" or "rp":
if (args.Count < 2)
{
Log("请指定分区表路径");
break;
}
string repartitionPath = args[1];
if (!File.Exists(repartitionPath))
{
Log($"{repartitionPath} not exist");
break;
}
manager.Repartition(PartitionToXml.LoadPartitionsXml(File.ReadAllText(repartitionPath)));
break;
case "r" or "read_part":
if (args.Count < 2)
{
Log("请指定分区名");
break;
}
string partName = args[1];
string? outputPath = args.Count > 2 ? args[2] : null;
try
{
using (FileStream fs = new FileStream(outputPath ?? $"{partName}.img", FileMode.Create, FileAccess.Write))
{
manager.ReadPartition(partName, fs);
}
}
catch (BadResponseException ex)
{
if (ex.Response != ResponseStatus.PartitionNotFound) throw;
Log($"{partName} not exist");
}
break;
case "w" or "write_part":
if (args.Count < 3)
{
Log("请指定分区名和文件路径");
break;
}
string writePartName = args[1];
string writeFilePath = args[2];
if (!File.Exists(writeFilePath))
{
Log($"文件 {writeFilePath} 不存在");
break;
}
try
{
using (FileStream fs = File.OpenRead(writeFilePath))
manager.WritePartition(writePartName, fs);
}
catch (BadResponseException) { }
break;
case "e" or "erase_part":
if (args.Count < 2)
{
Log("请指定分区名");
break;
}
try
{
manager.ErasePartition(args[1]);
}
catch (BadResponseException)
{
Log($"failed to erase {args[1]} partition");
}
break;
case "sas" or "set_active_slot":
if (args.Count < 2)
{
Log("请指定槽位");
break;
}
if (args[1] is not ("a" or "b"))
{
Log("槽位只能是a或b");
break;
}
manager.SetActiveSlot(args[1] switch
{
"a" => SlotToSetActive.SlotA,
"b" => SlotToSetActive.SlotB,
_ => throw new InvalidEnumArgumentException()
});
Log($"set active slot to {args[1]} successfully", ConsoleColor.Cyan);
break;
case "s" or "w_mem":
if (args.Count < 4)
{
Log("s/w_mem [文件路径] [地址] [模式] <分区名称>");
break;
}
if (!File.Exists(args[1]))
{
Log($"file not exist");
break;
}
try
{
using (FileStream fs = File.OpenRead(args[1]))
manager.WriteMemory(StrToSize.StringToSize(args[2]),
(WriteMemoryMode)StrToSize.StringToSize(args[3]),
fs, args.Count >= 5 ? args[4] : string.Empty);
}catch (BadResponseException) { }
break;
case "backup":
if (args.Count < 2)
{
Log("请指定分区表路径");
break;
}
string xmlPath = args[1];
if (!File.Exists(xmlPath))
{
Log("file not exist");
break;
}
string backupDir = args.Count > 2 ? args[2] : "ASRClientcore_backup";
if (!Directory.Exists(backupDir))
Directory.CreateDirectory(backupDir);
List<Partition> parts = PartitionToXml.LoadPartitionsXml(File.ReadAllText(xmlPath));
Log("Press Ctrl+C to cancel backup action", ConsoleColor.Cyan);
var token = cts.Token;
foreach (var part in parts)
{
if (token.IsCancellationRequested) break;
if (part.Name is "userdata" or "cache")
{
Log($"skip {part.Name} partition");
continue;
}
try
{
using (FileStream fs = new FileStream(Path.Combine(backupDir, $"{part.Name}.img"), FileMode.Create, FileAccess.Write))
manager.ReadPartition(part.Name, fs);
}
catch (BadResponseException ex)
{
if (ex.Response != ResponseStatus.PartitionNotFound) throw;
Log($"{part.Name} not exist");
}
}
break;
case "restore":
if (args.Count < 2)
{
Log("请指定备份文件夹路径");
break;
}
string restoreDir = args[1];
if (!Directory.Exists(restoreDir))
{
Log("文件夹不存在");
break;
}
var imgFiles = Directory.GetFiles(restoreDir, "*.img");
Log("Press Ctrl+C to cancel restore action", ConsoleColor.Cyan);
var ctoken = cts.Token;
foreach (var img in imgFiles)
{
if (ctoken.IsCancellationRequested) break;
string partname = Path.GetFileNameWithoutExtension(img);
if (partname.Contains("modem"))
{
Log($"skip {partname} part");
continue;
}
try
{
using (FileStream fs = File.OpenRead(img))
manager.WritePartition(partname, fs);
}
catch (BadResponseException)
{
Log($"failed to write {partname} partition");
}
}
break;
case "p" or "pull_mem" or "read_mem":
if (args.Count < 3)
{
Log("p [length] [addr] <path_to_save>");
break;
}
ulong address = StrToSize.StringToSize(args[1]);
ulong length = StrToSize.StringToSize(args[2]);
string? savePath = args.Count > 3 ? args[3] : $"memdump_{address:x}.bin";
if (length == 0)
{
Log("读取长度不能为0");
break;
}
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
manager.ReadMemory(address, length, fs);
}
break;
case "p_loop":
void add(ref uint i)
{
if (i < 0x20000) i += 0x2000;
else if (i < 0x200000) i += 0x20000;
else if (i < 0x2000000) i += 0x200000;
else i += 0x2000000;
}
ulong read = 0;
for (uint i = 0x200; read == 0 && i < 0xfe000000; add(ref i))
{
Log($"tried 0x{i:x}");
using (MemoryStream stream = new())
manager.ReadMemory(i, 8, stream);
}
break;
case "info":
Console.WriteLine(manager.DeviceInformation);
break;
case "off" or "poweroff":
status.HasExited = true;
manager.PowerDownDevice();
return true;
case "rst" or "reset":
status.HasExited = true;
BootMode mode = BootMode.Normal;
if (args.Count >= 2)
mode = (BootMode)StrToSize.StringToSize(args[1]);
manager.RebootDeviceToCustomMode(mode);
return true;
case "blk_size" or "bs":
if (args.Count < 2)
{
Log("请指定块大小");
break;
}
manager.PerBlockSize = (uint)StrToSize.StringToSize(args[1]);
break;
case "timeout":
if (args.Count < 2)
{
Log("请指定超时时间");
break;
}
manager.Timeout = (uint)StrToSize.StringToSize(args[1]);
break;
}
}
catch (OperationCanceledException) { }
return false;
}
public void CancelAction()
{
cts.Cancel();
cts.Dispose();
cts = new();
}
}
}
}