Add more supported commands

This commit is contained in:
Gustave Monce
2024-03-05 20:34:12 +01:00
parent 59b6d04ce6
commit 34c344e1c1
3 changed files with 77 additions and 17 deletions
+10 -8
View File
@@ -40,6 +40,7 @@ namespace UnifiedFlashingPlatform
internal UInt64 FirstUsableSector;
internal UInt64 LastUsableSector;
internal bool HasChanged = false;
internal UInt32 SectorSize;
[XmlElement("Partition")]
public List<Partition> Partitions = new();
@@ -48,9 +49,10 @@ namespace UnifiedFlashingPlatform
{
}
internal GPT(byte[] GPTBuffer)
internal GPT(byte[] GPTBuffer, UInt32 SectorSize)
{
this.GPTBuffer = GPTBuffer;
this.SectorSize = SectorSize;
UInt32? TempHeaderOffset = ByteOperations.FindAscii(GPTBuffer, "EFI PART");
if (TempHeaderOffset == null)
{
@@ -59,7 +61,7 @@ namespace UnifiedFlashingPlatform
HeaderOffset = (UInt32)TempHeaderOffset;
HeaderSize = ByteOperations.ReadUInt32(GPTBuffer, HeaderOffset + 0x0C);
TableOffset = HeaderOffset + 0x1000;
TableOffset = HeaderOffset + SectorSize;
FirstUsableSector = ByteOperations.ReadUInt64(GPTBuffer, HeaderOffset + 0x28);
LastUsableSector = ByteOperations.ReadUInt64(GPTBuffer, HeaderOffset + 0x30);
MaxPartitions = ByteOperations.ReadUInt32(GPTBuffer, HeaderOffset + 0x50);
@@ -268,12 +270,12 @@ namespace UnifiedFlashingPlatform
// The partition in the xml is also present in the archive.
// If the length is specified in the xml, it must match the file in the archive.
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
ulong StreamLengthInSectors = (ulong)Entry.Length / SectorSize;
using (DecompressedStream DecompressedStream = new(Entry.Open()))
{
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
StreamLengthInSectors = (ulong)DecompressedStream.Length / SectorSize;
}
catch { }
}
@@ -466,10 +468,10 @@ namespace UnifiedFlashingPlatform
if (Entry != null)
{
DecompressedStream DecompressedStream = new(Entry.Open());
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
ulong StreamLengthInSectors = (ulong)Entry.Length / SectorSize;
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
StreamLengthInSectors = (ulong)DecompressedStream.Length / SectorSize;
}
catch { }
DecompressedStream.Close();
@@ -516,10 +518,10 @@ namespace UnifiedFlashingPlatform
}
ZipArchiveEntry Entry = Archive.Entries.FirstOrDefault(e => string.Equals(e.Name, NewPartition.Name, StringComparison.CurrentCultureIgnoreCase) || e.Name.StartsWith(NewPartition.Name + ".", true, System.Globalization.CultureInfo.GetCultureInfo("en-US")));
DecompressedStream DecompressedStream = new(Entry.Open());
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
ulong StreamLengthInSectors = (ulong)Entry.Length / SectorSize;
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
StreamLengthInSectors = (ulong)DecompressedStream.Length / SectorSize;
}
catch { }
DecompressedStream.Close();
+23 -9
View File
@@ -5,7 +5,7 @@ namespace UnifiedFlashingPlatform
{
public partial class UnifiedFlashingPlatformTransport
{
public void FlashSectors(UInt32 StartSector, byte[] Data, int Progress = 0)
public void FlashSectors(UInt32 StartSector, byte[] Data, byte TargetDevice = 0, int Progress = 0)
{
// Start sector is in UInt32, so max size of eMMC is 2 TB.
@@ -13,7 +13,7 @@ namespace UnifiedFlashingPlatform
string Header = FlashSignature; // NOKF
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
Request[0x05] = 0; // Target device = 0
Request[0x05] = TargetDevice; // Target device: 0: eMMC, 1: SDIO, 2: Other ???, 3: ???
Buffer.BlockCopy(BigEndian.GetBytes(StartSector, 4), 0, Request, 0x0B, 4); // Start sector
Buffer.BlockCopy(BigEndian.GetBytes(Data.Length / 0x200, 4), 0, Request, 0x0F, 4); // Sector count
Request[0x13] = (byte)Progress; // Progress (0 - 100)
@@ -41,8 +41,6 @@ namespace UnifiedFlashingPlatform
}
}
/* NOKN */
public void ResetPhone()
{
Debug.WriteLine("Rebooting phone");
@@ -59,8 +57,6 @@ namespace UnifiedFlashingPlatform
}
}
/* NOKS */
public GPT ReadGPT()
{
// If this function is used with a locked BootMgr v1,
@@ -94,8 +90,26 @@ namespace UnifiedFlashingPlatform
throw new NotSupportedException("ReadGPT: Error 0x" + Error.ToString("X4"));
}
byte[] GPTBuffer = new byte[Buffer.Length - 0x208];
System.Buffer.BlockCopy(Buffer, 0x208, GPTBuffer, 0, 0x4200);
// Length: 0x4400 for 512 (0x200) Sector Size (from sector 0 to sector 34)
// Length: 0x6000 for 4096 (0x1000) Sector Size (from sector 0 to sector 6)
UInt32 ReturnedGPTBufferLength = (UInt32)Buffer.Length - 8;
UInt32 SectorSize;
if (Buffer.Length == 0x4408)
{
SectorSize = 512;
}
else if (Buffer.Length == 0x6008)
{
SectorSize = 4096;
}
else
{
throw new NotSupportedException("ReadGPT: Unsupported output size! 0x" + ReturnedGPTBufferLength.ToString("X4"));
}
byte[] GPTBuffer = new byte[ReturnedGPTBufferLength - SectorSize];
System.Buffer.BlockCopy(Buffer, 8 + (Int32)SectorSize, GPTBuffer, 0, (Int32)ReturnedGPTBufferLength - (Int32)SectorSize);
/*if (Switch)
{
@@ -109,7 +123,7 @@ namespace UnifiedFlashingPlatform
}
}*/
return new GPT(GPTBuffer); // NOKT message header and MBR are ignored
return new GPT(GPTBuffer, SectorSize); // NOKT message header and MBR are ignored
}
public byte[] ReadPhoneInfo()
+44
View File
@@ -223,6 +223,50 @@ namespace UnifiedFlashingPlatform
ExecuteRawMethod(Request);
}
public void ClearScreen()
{
byte[] Request = new byte[6];
string Header = ClearScreenSignature; // NOKXCC
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
public byte[] Echo(byte[] DataPayload)
{
byte[] Request = new byte[10 + DataPayload.Length];
string Header = EchoSignature; // NOKXCE
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
Buffer.BlockCopy(BitConverter.GetBytes(DataPayload.Length).Reverse().ToArray(), 0, Request, 6, 4);
Buffer.BlockCopy(DataPayload, 0, Request, 10, DataPayload.Length);
byte[] Response = ExecuteRawMethod(Request);
if ((Response == null) || (Response.Length < 6 + DataPayload.Length))
{
return null;
}
byte[] Result = new byte[DataPayload.Length];
Buffer.BlockCopy(Response, 6, Result, 0, DataPayload.Length);
return Result;
}
public void TelemetryStart()
{
byte[] Request = new byte[4];
string Header = TelemetryStartSignature; // NOKS
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawVoidMethod(Request);
}
public void TelemetryEnd()
{
byte[] Request = new byte[4];
string Header = TelemetryEndSignature; // NOKN
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawVoidMethod(Request);
}
public ulong GetLogSize()
{
byte[] Request = new byte[0x10];