Make GPT work with 4096 sector size content & improve command support

This commit is contained in:
Gustave Monce
2024-03-04 23:24:57 +01:00
parent 6f8357d685
commit 21ae2e2280
4 changed files with 85 additions and 59 deletions
+7 -7
View File
@@ -59,7 +59,7 @@ namespace UnifiedFlashingPlatform
HeaderOffset = (UInt32)TempHeaderOffset;
HeaderSize = ByteOperations.ReadUInt32(GPTBuffer, HeaderOffset + 0x0C);
TableOffset = HeaderOffset + 0x200;
TableOffset = HeaderOffset + 0x1000;
FirstUsableSector = ByteOperations.ReadUInt64(GPTBuffer, HeaderOffset + 0x28);
LastUsableSector = ByteOperations.ReadUInt64(GPTBuffer, HeaderOffset + 0x30);
MaxPartitions = ByteOperations.ReadUInt32(GPTBuffer, HeaderOffset + 0x50);
@@ -268,12 +268,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 / 0x200;
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
using (DecompressedStream DecompressedStream = new(Entry.Open()))
{
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x200;
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
}
catch { }
}
@@ -466,10 +466,10 @@ namespace UnifiedFlashingPlatform
if (Entry != null)
{
DecompressedStream DecompressedStream = new(Entry.Open());
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x200;
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x200;
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
}
catch { }
DecompressedStream.Close();
@@ -516,10 +516,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 / 0x200;
ulong StreamLengthInSectors = (ulong)Entry.Length / 0x1000;
try
{
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x200;
StreamLengthInSectors = (ulong)DecompressedStream.Length / 0x1000;
}
catch { }
DecompressedStream.Close();
+29 -29
View File
@@ -6,74 +6,74 @@
// Not valid commands
//
/* NOK */
private static string Signature = "NOK";
private static string Signature => "NOK";
/* NOKX */
private static string ExtendedMessageSignature = Signature + "X";
private static string ExtendedMessageSignature => $"{Signature}X";
/* NOKXC */
private static string CommonExtendedMessageSignature = ExtendedMessageSignature + "C";
private static string CommonExtendedMessageSignature => $"{ExtendedMessageSignature}C";
/* NOKXF */
private static string UFPExtendedMessageSignature = ExtendedMessageSignature + "F";
private static string UFPExtendedMessageSignature => $"{ExtendedMessageSignature}F";
//
// Normal commands
//
/* NOKF */
private static string FlashSignature = Signature + "F";
private static string FlashSignature => $"{Signature}F";
/* NOKI */
private static string HelloSignature = Signature + "I";
private static string HelloSignature => $"{Signature}I";
/* NOKM */
private static string MassStorageSignature = Signature + "M";
private static string MassStorageSignature => $"{Signature}M";
/* NOKN */
private static string TelemetryEndSignature = Signature + "N";
private static string TelemetryEndSignature => $"{Signature}N";
/* NOKR */
private static string RebootSignature = Signature + "R";
private static string RebootSignature => $"{Signature}R";
/* NOKS */
private static string TelemetryStartSignature = Signature + "S";
private static string TelemetryStartSignature => $"{Signature}S";
/* NOKT */
private static string GetGPTSignature = Signature + "T";
private static string GetGPTSignature => $"{Signature}T";
/* NOKV */
private static string InfoQuerySignature = Signature + "V";
private static string InfoQuerySignature => $"{Signature}V";
/* NOKZ */
private static string ShutdownSignature = Signature + "Z";
private static string ShutdownSignature => $"{Signature}Z";
//
// Common extended commands
//
/* NOKXCB */
private static string SwitchModeSignature = CommonExtendedMessageSignature + "B";
private static string SwitchModeSignature => $"{CommonExtendedMessageSignature}B";
/* NOKXCC */
private static string ClearScreenSignature = CommonExtendedMessageSignature + "C";
private static string ClearScreenSignature => $"{CommonExtendedMessageSignature}C";
/* NOKXCD */
private static string GetDirectoryEntriesSignature = CommonExtendedMessageSignature + "D";
private static string GetDirectoryEntriesSignature => $"{CommonExtendedMessageSignature}D";
/* NOKXCE */
private static string EchoSignature = CommonExtendedMessageSignature + "E";
private static string EchoSignature => $"{CommonExtendedMessageSignature}E";
/* NOKXCF */
private static string GetFileSignature = CommonExtendedMessageSignature + "F";
private static string GetFileSignature => $"{CommonExtendedMessageSignature}F";
/* NOKXCM */
private static string DisplayCustomMessageSignature = CommonExtendedMessageSignature + "M";
private static string DisplayCustomMessageSignature => $"{CommonExtendedMessageSignature}M";
/* NOKXCP */
private static string PutFileSignature = CommonExtendedMessageSignature + "P";
private static string PutFileSignature => $"{CommonExtendedMessageSignature}P";
/* NOKXCT */
private static string BenchmarkTestsSignature = CommonExtendedMessageSignature + "T";
private static string BenchmarkTestsSignature => $"{CommonExtendedMessageSignature}T";
//
// UFP extended commands
//
/* NOKXFF */
private static string AsyncFlashModeSignature = UFPExtendedMessageSignature + "F";
private static string AsyncFlashModeSignature => $"{UFPExtendedMessageSignature}F";
/* NOKXFI */
private static string UnlockSignature = UFPExtendedMessageSignature + "I";
private static string UnlockSignature => $"{UFPExtendedMessageSignature}I";
/* NOKXFO */
private static string RelockSignature = UFPExtendedMessageSignature + "O";
private static string RelockSignature => $"{UFPExtendedMessageSignature}O";
/* NOKXFR */
private static string ReadParamSignature = UFPExtendedMessageSignature + "R";
private static string ReadParamSignature => $"{UFPExtendedMessageSignature}R";
/* NOKXFS */
private static string SecureFlashSignature = UFPExtendedMessageSignature + "S";
private static string SecureFlashSignature => $"{UFPExtendedMessageSignature}S";
/* NOKXFT */
private static string TelemetryReadSignature = UFPExtendedMessageSignature + "T";
private static string TelemetryReadSignature => $"{UFPExtendedMessageSignature}T";
/* NOKXFW */
private static string WriteParamSignature = UFPExtendedMessageSignature + "W";
private static string WriteParamSignature => $"{UFPExtendedMessageSignature}W";
/* NOKXFX */
private static string GetLogsSignature = UFPExtendedMessageSignature + "X";
private static string GetLogsSignature => $"{UFPExtendedMessageSignature}X";
}
}
+20 -10
View File
@@ -11,21 +11,21 @@ namespace UnifiedFlashingPlatform
byte[] Request = new byte[Data.Length + 0x40];
string Header = FlashSignature;
string Header = FlashSignature; // NOKF
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
Request[0x05] = 0; // Device type = 0
Request[0x05] = 0; // Target device = 0
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)
Request[0x18] = 0; // Do Verify
Request[0x19] = 0; // Is Test
Request[0x18] = 0; // Verify needed
Request[0x19] = 0; // Skip write
Buffer.BlockCopy(Data, 0, Request, 0x40, Data.Length);
ExecuteRawMethod(Request);
}
internal void Hello()
public void Hello()
{
byte[] Request = new byte[4];
ByteOperations.WriteAsciiString(Request, 0, HelloSignature);
@@ -41,10 +41,9 @@ namespace UnifiedFlashingPlatform
}
}
/* NOKM */
/* NOKN */
internal void ResetPhone()
public void ResetPhone()
{
Debug.WriteLine("Rebooting phone");
try
@@ -62,7 +61,7 @@ namespace UnifiedFlashingPlatform
/* NOKS */
internal GPT ReadGPT()
public GPT ReadGPT()
{
// If this function is used with a locked BootMgr v1,
// then the mode-switching should be done outside this function,
@@ -113,14 +112,25 @@ namespace UnifiedFlashingPlatform
return new GPT(GPTBuffer); // NOKT message header and MBR are ignored
}
/* NOKV */
public byte[] ReadPhoneInfo()
{
byte[] Request = new byte[4];
ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); // NOKV
byte[] Response = ExecuteRawMethod(Request);
if ((Response == null) || (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU"))
{
throw new NotSupportedException();
}
return Response[5..];
}
public void Shutdown()
{
byte[] Request = new byte[4];
string Header = ShutdownSignature;
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
ExecuteRawVoidMethod(Request);
}
}
}
+29 -13
View File
@@ -97,7 +97,7 @@ namespace UnifiedFlashingPlatform
public byte[] ReadParam(string Param)
{
byte[] Request = new byte[0x0B];
string Header = ReadParamSignature;
string Header = ReadParamSignature; // NOKXFR
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length);
@@ -146,7 +146,7 @@ namespace UnifiedFlashingPlatform
public string ReadDeviceProcessorManufacturer()
{
return ReadStringParam("pm");
return ReadStringParam("pm\0\0");
}
public string ReadUnlockID()
@@ -157,15 +157,31 @@ namespace UnifiedFlashingPlatform
public void Relock()
{
byte[] Request = new byte[7];
string Header = RelockSignature;
string Header = RelockSignature; // NOKXFO
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
public void SwitchToMassStorageContext()
public void MassStorage()
{
byte[] Request = new byte[7];
string Header = SwitchModeSignature + "M";
string Header = MassStorageSignature; // NOKM
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
public void RebootPhone()
{
byte[] Request = new byte[7];
string Header = $"{SwitchModeSignature}R"; // NOKXCBR
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
public void SwitchToUFP()
{
byte[] Request = new byte[7];
string Header = $"{SwitchModeSignature}U"; // NOKXCBU
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
@@ -173,32 +189,32 @@ namespace UnifiedFlashingPlatform
public void ContinueBoot()
{
byte[] Request = new byte[7];
string Header = SwitchModeSignature + "W";
string Header = $"{SwitchModeSignature}W"; // NOKXCBW
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
}
public void ExtendedShutdown()
public void PowerOff()
{
byte[] Request = new byte[7];
string Header = SwitchModeSignature + "Z";
string Header = $"{SwitchModeSignature}Z"; // NOKXCBZ
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawVoidMethod(Request);
}
public void ExtendedResetPhone()
public void TransitionToUFPBootApp()
{
byte[] Request = new byte[7];
string Header = SwitchModeSignature + "R";
string Header = $"{SwitchModeSignature}T"; // NOKXCBT
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
ExecuteRawMethod(Request);
ExecuteRawVoidMethod(Request);
}
public ulong GetLogSize()
{
byte[] Request = new byte[0x10];
string Header = ReadParamSignature;
const string Param = "LZ";
string Header = ReadParamSignature; // NOKXFR
const string Param = "LZ\0\0";
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length);
Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length);