Project Maintenance

This commit is contained in:
Gustave Monce
2021-08-09 20:21:09 +02:00
parent 9865ef7f79
commit 847ce0506d
411 changed files with 5922 additions and 54941 deletions
+55 -11
View File
@@ -31,7 +31,7 @@ namespace WPinternals
internal byte[] SecurityHeader;
internal byte[] ImageHeader;
internal byte[] StoreHeader;
private int?[] ChunkIndexes;
private readonly int?[] ChunkIndexes;
private FileStream FFUFile = null;
private int FileOpenCount = 0;
@@ -55,7 +55,10 @@ namespace WPinternals
byte[] ShortSecurityHeader = new byte[0x20];
FFUFile.Read(ShortSecurityHeader, 0, 0x20);
if (ByteOperations.ReadAsciiString(ShortSecurityHeader, 0x04, 0x0C) != "SignedImage ")
{
throw new BadImageFormatException();
}
ChunkSize = ByteOperations.ReadInt32(ShortSecurityHeader, 0x10) * 1024;
UInt32 SecurityHeaderSize = ByteOperations.ReadUInt32(ShortSecurityHeader, 0x00);
UInt32 CatalogSize = ByteOperations.ReadUInt32(ShortSecurityHeader, 0x18);
@@ -68,7 +71,10 @@ namespace WPinternals
byte[] ShortImageHeader = new byte[0x1C];
FFUFile.Read(ShortImageHeader, 0, 0x1C);
if (ByteOperations.ReadAsciiString(ShortImageHeader, 0x04, 0x0C) != "ImageFlash ")
{
throw new BadImageFormatException();
}
UInt32 ImageHeaderSize = ByteOperations.ReadUInt32(ShortImageHeader, 0x00);
UInt32 ManifestSize = ByteOperations.ReadUInt32(ShortImageHeader, 0x10);
ImageHeader = new byte[RoundUpToChunks(ImageHeaderSize + ManifestSize)];
@@ -107,7 +113,9 @@ namespace WPinternals
if (DiskAccessMethod == 0) // 0 = From begin, 2 = From end. We ignore chunks at end of disk. These contain secondairy GPT.
{
if ((ChunkIndex + ChunkCount - 1) > HighestChunkIndex)
{
HighestChunkIndex = ChunkIndex + ChunkCount - 1;
}
}
}
WriteDescriptorEntryOffset += 8 + (LocationCount * 0x08);
@@ -148,7 +156,9 @@ namespace WPinternals
TotalSize = HeaderSize + PayloadSize;
if (TotalSize != (UInt64)FFUFile.Length)
{
throw new WPinternalsException("Bad FFU file", "Bad FFU file: " + Path + "." + Environment.NewLine + "Expected size: " + TotalSize.ToString() + ". Actual size: " + FFUFile.Length + ".");
}
}
catch (WPinternalsException)
{
@@ -168,7 +178,7 @@ namespace WPinternals
{
bool Result = false;
FileStream FFUFile = new FileStream(FileName, FileMode.Open, FileAccess.Read);
FileStream FFUFile = new(FileName, FileMode.Open, FileAccess.Read);
byte[] Signature = new byte[0x10];
FFUFile.Read(Signature, 0, 0x10);
@@ -207,24 +217,34 @@ namespace WPinternals
if (FFUFile != null)
{
if (FFUFile.Position != Position)
{
FFUFile.Seek(Position, SeekOrigin.Begin);
}
}
}
internal UInt32 RoundUpToChunks(UInt32 Size)
{
if ((Size % ChunkSize) > 0)
{
return (UInt32)(((Size / ChunkSize) + 1) * ChunkSize);
}
else
{
return Size;
}
}
internal UInt32 RoundDownToChunks(UInt32 Size)
{
if ((Size % ChunkSize) > 0)
{
return (UInt32)((Size / ChunkSize) * ChunkSize);
}
else
{
return Size;
}
}
internal byte[] GetSectors(int StartSector, int SectorCount)
@@ -248,10 +268,14 @@ namespace WPinternals
int LastSector = (ChunkSize / 0x200) - 1;
if (j == FirstChunk)
{
FirstSector = GetSectorNumberInChunkFromSectorIndex(StartSector);
}
if (j == LastChunk)
{
LastSector = GetSectorNumberInChunkFromSectorIndex(StartSector + SectorCount - 1);
}
int Offset = FirstSector * 0x200;
int Size = (LastSector - FirstSector + 1) * 0x200;
@@ -268,9 +292,12 @@ namespace WPinternals
internal byte[] GetPartition(string Name)
{
Partition Target = GPT.Partitions.Where(p => (string.Compare(p.Name, Name, true) == 0)).FirstOrDefault();
Partition Target = GPT.Partitions.Find(p => (string.Compare(p.Name, Name, true) == 0));
if (Target == null)
{
throw new ArgumentOutOfRangeException();
}
return GetSectors((int)Target.FirstSector, (int)(Target.LastSector - Target.FirstSector + 1));
}
@@ -291,22 +318,26 @@ namespace WPinternals
private void WritePartition(string Name, string FilePath, Action<int, TimeSpan?> ProgressUpdateCallback, ProgressUpdater UpdaterPerSector, bool Compress = false)
{
Partition Target = GPT.Partitions.Where(p => (string.Compare(p.Name, Name, true) == 0)).FirstOrDefault();
Partition Target = GPT.Partitions.Find(p => (string.Compare(p.Name, Name, true) == 0));
if (Target == null)
{
throw new ArgumentOutOfRangeException();
}
int FirstChunk = GetChunkIndexFromSectorIndex((int)Target.FirstSector);
int LastChunk = GetChunkIndexFromSectorIndex((int)Target.LastSector);
ProgressUpdater Updater = UpdaterPerSector;
if ((Updater == null) && (ProgressUpdateCallback != null))
{
Updater = new ProgressUpdater(Target.LastSector - Target.FirstSector + 1, ProgressUpdateCallback);
}
byte[] Buffer = new byte[ChunkSize];
OpenFile();
FileStream OutputFile = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
FileStream OutputFile = new(FilePath, FileMode.Create, FileAccess.Write);
Stream OutStream = OutputFile;
// We use gzip compression
@@ -333,18 +364,21 @@ namespace WPinternals
int LastSector = (ChunkSize / 0x200) - 1;
if (j == FirstChunk)
{
FirstSector = GetSectorNumberInChunkFromSectorIndex((int)Target.FirstSector);
}
if (j == LastChunk)
{
LastSector = GetSectorNumberInChunkFromSectorIndex((int)Target.LastSector);
}
int Offset = FirstSector * 0x200;
int Size = (LastSector - FirstSector + 1) * 0x200;
OutStream.Write(Buffer, Offset, Size);
if (Updater != null)
Updater.IncreaseProgress((UInt64)(ChunkSize / 0x200));
Updater?.IncreaseProgress((UInt64)(ChunkSize / 0x200));
}
OutStream.Close();
@@ -356,7 +390,9 @@ namespace WPinternals
{
long BaseOffset = (long)SecurityHeader.Length + ImageHeader.Length + StoreHeader.Length;
if (ChunkIndexes[ChunkIndex] == null)
{
return new byte[ChunkSize];
}
else
{
OpenFile();
@@ -372,7 +408,9 @@ namespace WPinternals
{
long BaseOffset = SecurityHeader.Length + ImageHeader.Length + StoreHeader.Length;
if (ChunkIndexes[ChunkIndex] == null)
{
Array.Clear(Chunk, 0, ChunkSize);
}
else
{
OpenFile();
@@ -398,9 +436,12 @@ namespace WPinternals
{
Partition Target = GPT.GetPartition(PartitionName);
if (Target == null)
{
throw new InvalidOperationException("Partitionname is not found!");
}
int ChunkIndex = GetChunkIndexFromSectorIndex((int)Target.FirstSector);
return (ChunkIndexes[ChunkIndex] != null);
return ChunkIndexes[ChunkIndex] != null;
}
private int GetChunkIndexFromSectorIndex(ulong p)
@@ -423,7 +464,10 @@ namespace WPinternals
uint Length = (uint)ByteOperations.FindPattern(Data, Start, 0x100, new byte[] { 0x00 }, null, null) - Start;
uint? Offset0D = ByteOperations.FindPattern(Data, Start, 0x100, new byte[] { 0x0D }, null, null);
if ((Offset0D != null) && (Offset0D < (Start + Length)))
{
Length = (uint)Offset0D - Start;
}
Result = ByteOperations.ReadAsciiString(Data, Start, Length);
}
}
@@ -434,10 +478,10 @@ namespace WPinternals
internal string GetOSVersion()
{
byte[] efiesp = GetPartition("EFIESP");
MemoryStream s = new MemoryStream(efiesp);
DiscUtils.Fat.FatFileSystem fs = new DiscUtils.Fat.FatFileSystem(s);
MemoryStream s = new(efiesp);
DiscUtils.Fat.FatFileSystem fs = new(s);
Stream mss = fs.OpenFile(@"Windows\System32\Boot\mobilestartup.efi", FileMode.Open, FileAccess.Read);
MemoryStream msms = new MemoryStream();
MemoryStream msms = new();
mss.CopyTo(msms);
byte[] mobilestartup = msms.ToArray();
Version OSVersion = PE.GetProductVersion(mobilestartup);