Skip to content

Commit

Permalink
Add auto version select
Browse files Browse the repository at this point in the history
  • Loading branch information
darknight1050 committed Feb 5, 2024
1 parent 2fa13f5 commit e0981a0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
19 changes: 13 additions & 6 deletions Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Patches()
{
Versions =
[
new PatchesVersion("q2_9248600200800000.pe", new Dictionary<int, byte>() {
new PatchesVersion("9248600200800000", "q2_9248600200800000.pe", 0x100000, new Dictionary<int, byte>() {
{ 0x3e4c8, 0x21 },

{ 0x3e4dc, 0x08 },
Expand All @@ -37,21 +37,28 @@ public Patches()
];
}

public PatchesVersion? GetVersion(string name)
{
return Versions.Find(x => x.Name == name);
}


public class PatchesVersion
{
public string Name { get; private set; }
public string File { get; private set; }
public int Overflow { get; private set; }

public Dictionary<int, byte> Patches { get; private set; }
public int MaxAddress { get => Patches.Keys.Max(); }

public PatchesVersion(string file, IEnumerable<KeyValuePair<int, byte>> patches) : this(file, file, patches) { }
public PatchesVersion(string file, int overflow, IEnumerable<KeyValuePair<int, byte>> patches) : this(file, file, overflow, patches) { }

public PatchesVersion(string name, string file, IEnumerable<KeyValuePair<int, byte>> patches)
public PatchesVersion(string name, string file, int overflow, IEnumerable<KeyValuePair<int, byte>> patches)
{
Name = name;
File = file;
Overflow = overflow;
Patches = new Dictionary<int, byte>(patches);
}

Expand All @@ -60,9 +67,9 @@ public bool ApplyTo(ref byte[] buffer)
{
if (buffer.Length <= MaxAddress)
return false;
foreach (var patch in Patches)
{
buffer[patch.Key] = patch.Value;
foreach (var patch in Patches)
{
buffer[patch.Key] = patch.Value;
}
return true;
}
Expand Down
58 changes: 46 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ namespace Examples;
internal class Program
{

public static Dictionary<string, string>? GetDeviceInfo(QuestUsbDevice device)
{
Dictionary<string, string> info = new Dictionary<string, string>();
device.WriteS("oem device-info");
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)
{
var patches = new Patches();
Expand All @@ -23,30 +43,44 @@ public static void Main(string[] args)
Console.WriteLine("Can't find the Quest Device!");
return;
}
var selectedVersion = patches.Versions[0];
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, 0x100000 + end).ToArray();
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, 0x100000, end);

device.WriteS("getvar:serialno");
Console.WriteLine("Read: " + device.ReadS());
Array.Copy(firmware, 0, buffer, selectedVersion.Overflow, end);

Console.WriteLine("Unlock Device? y/n");
if(Console.ReadKey().Key == ConsoleKey.Y) {
if(Console.ReadKey(true).Key == ConsoleKey.Y) {
device.Write(buffer);
Console.WriteLine("Read: " + device.ReadS());
Console.WriteLine(device.ReadS());

device.WriteS("flash:unlock_token");
Console.WriteLine("Read: " + device.ReadS());
Console.WriteLine("Read: " + device.ReadS());
Console.WriteLine(device.ReadS());

device.Close();
}
}
Console.WriteLine("Finished! Press any key to close...");
Console.ReadKey();
Console.WriteLine("Finished!\nPress any key to close...");
Console.ReadKey(true);
}

}
41 changes: 31 additions & 10 deletions QuestUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public class QuestUsbDevice : IDisposable
private UsbEndpointWriter _writeEndpoint;
private UsbEndpointReader _readEnpoint;

private object _readBufferLock;
private List<byte> _readBuffer;

public QuestUsbDevice()
{
_readBufferLock = new object();
_readBuffer = new List<byte>();
}

public bool IsConnected()
{
return UsbDevice.AllDevices.Select(v => v.DevicePath).Contains(_device?.DevicePath);
Expand All @@ -26,6 +35,17 @@ public bool TryConnect()

_readEnpoint = _device.OpenEndpointReader(ReadEndpointID.Ep01);
_writeEndpoint = _device.OpenEndpointWriter(WriteEndpointID.Ep01);
_readEnpoint.DataReceived += (sender, args) =>
{
lock(_readBufferLock)
{
var buffer = new byte[args.Count];
Array.Copy(args.Buffer, buffer, args.Count);
_readBuffer.AddRange(buffer);
_readBuffer.Add((byte)'\n');
}
};
_readEnpoint.DataReceivedEnabled = true;
return true;
}

Expand Down Expand Up @@ -60,23 +80,24 @@ public bool Close()
{
if(!IsConnected())
return null;

List<byte> buffer = new List<byte>();
int readBytes = 0;
do
lock(_readBufferLock)
{
var readBuffer = new byte[512];
_readEnpoint.Read(readBuffer, 1000, out readBytes);
buffer.AddRange(readBuffer);
} while (readBytes == 512);
return buffer.ToArray();
if (_readBuffer.Count == 0)
return null;
var data = _readBuffer.ToArray();
_readBuffer.Clear();
return data;
}
}

public string? ReadS()
{
if (!IsConnected())
return null;
return Encoding.ASCII.GetString(Read());
var data = Read();
if(data == null)
return null;
return Encoding.ASCII.GetString(data);
}

public void Dispose()
Expand Down

0 comments on commit e0981a0

Please sign in to comment.