-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
143 lines (131 loc) · 5.11 KB
/
Program.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
using LibUsbDotNet;
using LibUsbDotNet.Main;
using quest_bootloader_unlocker;
namespace Examples;
internal class Program
{
public static readonly int DEFAULT_VID = 0x2833;
public static readonly int DEFAULT_PID = 0x81;
public static Dictionary<string, string>? GetDeviceInfo(FastbootUsbDevice device)
{
Dictionary<string, string> info = new Dictionary<string, string>();
device.WriteS("oem device-info");
Thread.Sleep(100);
var data = device.ReadS();
if (data == null)
return null;
foreach (var item in data.Split("\n"))
{
var splitted = item.Replace("INFO", "").Split(":");
if (splitted.Length == 2)
{
info.Add(splitted[0].Trim(), splitted[1].Trim());
}
}
if (info.Count == 0)
return null;
return info;
}
public static void Main(string[] args)
{
if (args.Length <= 0)
{
var patches = new Patches();
using (var device = new FastbootUsbDevice(DEFAULT_VID, DEFAULT_PID))
{
if (!device.TryConnect())
{
Console.WriteLine("Can't find the Fastboot Device!");
return;
}
var deviceInfo = GetDeviceInfo(device);
if (deviceInfo == null)
{
Console.WriteLine("Can't get device info!");
return;
}
string? buildNumber;
if (!deviceInfo.TryGetValue("Build number", out buildNumber))
{
Console.WriteLine("Can't get device build number!");
return;
}
Console.WriteLine($"Build number: {buildNumber}!");
var selectedVersion = patches.GetVersion(buildNumber);
if (selectedVersion == null)
{
Console.WriteLine($"Build number: {buildNumber} not available!");
return;
}
var end = selectedVersion.MaxAddress + 1;
var buffer = Enumerable.Repeat((byte)0x0C, selectedVersion.Overflow + end).ToArray();
var firmware = File.ReadAllBytes(selectedVersion.File);
selectedVersion.ApplyTo(ref firmware);
Array.Copy(firmware, 0, buffer, selectedVersion.Overflow, end);
Console.WriteLine("Unlock Device? y/n");
if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
device.Write(buffer);
Thread.Sleep(100);
Console.WriteLine(device.ReadS());
device.WriteS("flash:unlock_token");
Thread.Sleep(100);
Console.WriteLine(device.ReadS());
device.Close();
}
}
Console.WriteLine("Finished!");
Console.WriteLine("Press any key to close...");
Console.ReadKey(true);
}
else
{
if (args[0].Equals("test"))
{
Console.WriteLine("Devices:");
foreach (UsbRegistry d in UsbDevice.AllDevices)
{
Console.WriteLine($"{d.Name} - VID: {d.Vid.ToString("X4")} PID: {d.Pid.ToString("X4")}");
}
Console.WriteLine();
FastbootUsbDevice device;
if (args.Length >= 3)
{
device = new FastbootUsbDevice(Convert.ToInt32(args[1], 16), Convert.ToInt32(args[2], 16));
}
else
{
device = new FastbootUsbDevice(DEFAULT_VID, DEFAULT_PID);
}
if (!device.TryConnect())
{
Console.WriteLine("Can't find the Fastboot Device!");
return;
}
var deviceInfo = GetDeviceInfo(device);
if (deviceInfo == null)
{
Console.WriteLine("Can't get device info!");
return;
}
Console.WriteLine("Device Info:");
foreach (var info in deviceInfo)
{
Console.WriteLine($"{info.Key}: {info.Value}");
}
var buffer = Enumerable.Repeat((byte)0x0C, 0x100000 + 0xFFFFF).ToArray();
Console.WriteLine("Test Device? y/n");
if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
device.Write(buffer);
Thread.Sleep(100);
Console.WriteLine(device.ReadS());
device.Close();
}
device.Dispose();
Console.WriteLine("Finished, check for crash!");
Console.WriteLine("Press any key to close...");
}
}
}
}