diff --git a/BadConnectionException.cs b/BadConnectionException.cs index d6c930e..2fcd67f 100644 --- a/BadConnectionException.cs +++ b/BadConnectionException.cs @@ -22,5 +22,12 @@ using System; namespace UnifiedFlashingPlatform { - public class BadConnectionException : Exception { public BadConnectionException() { } public BadConnectionException(string message) : base(message) { } public BadConnectionException(string message, Exception innerException) : base(message, innerException) { } } + public class BadConnectionException : Exception + { + public BadConnectionException() + { + } + public BadConnectionException(string message) : base(message) { } + public BadConnectionException(string message, Exception innerException) : base(message, innerException) { } + } } \ No newline at end of file diff --git a/BigEndian.cs b/BigEndian.cs index b566c04..75aeb7a 100644 --- a/BigEndian.cs +++ b/BigEndian.cs @@ -31,17 +31,13 @@ namespace UnifiedFlashingPlatform { Bytes = BitConverter.GetBytes((short)Value); } - else if (Value is ushort) - { - Bytes = BitConverter.GetBytes((ushort)Value); - } - else if (Value is int) - { - Bytes = BitConverter.GetBytes((int)Value); - } else { - Bytes = Value is uint ? BitConverter.GetBytes((uint)Value) : throw new NotSupportedException(); + Bytes = Value is ushort + ? BitConverter.GetBytes((ushort)Value) + : Value is int + ? BitConverter.GetBytes((int)Value) + : Value is uint ? BitConverter.GetBytes((uint)Value) : throw new NotSupportedException(); } byte[] Result = new byte[Bytes.Length]; @@ -75,7 +71,7 @@ namespace UnifiedFlashingPlatform } } - public static UInt16 ToUInt16(byte[] Buffer, int Offset) + public static ushort ToUInt16(byte[] Buffer, int Offset) { byte[] Bytes = new byte[2]; for (int i = 0; i < 2; i++) @@ -86,7 +82,7 @@ namespace UnifiedFlashingPlatform return BitConverter.ToUInt16(Bytes, 0); } - public static Int16 ToInt16(byte[] Buffer, int Offset) + public static short ToInt16(byte[] Buffer, int Offset) { byte[] Bytes = new byte[2]; for (int i = 0; i < 2; i++) @@ -97,7 +93,7 @@ namespace UnifiedFlashingPlatform return BitConverter.ToInt16(Bytes, 0); } - public static UInt32 ToUInt32(byte[] Buffer, int Offset) + public static uint ToUInt32(byte[] Buffer, int Offset) { byte[] Bytes = new byte[4]; for (int i = 0; i < 4; i++) @@ -108,7 +104,7 @@ namespace UnifiedFlashingPlatform return BitConverter.ToUInt32(Bytes, 0); } - public static Int32 ToInt32(byte[] Buffer, int Offset) + public static int ToInt32(byte[] Buffer, int Offset) { byte[] Bytes = new byte[4]; for (int i = 0; i < 4; i++) diff --git a/ByteOperations.cs b/ByteOperations.cs index 4fa6ee8..11752e4 100644 --- a/ByteOperations.cs +++ b/ByteOperations.cs @@ -27,21 +27,21 @@ namespace UnifiedFlashingPlatform { internal static class ByteOperations { - internal static string ReadAsciiString(byte[] ByteArray, UInt32 Offset, UInt32 Length) + internal static string ReadAsciiString(byte[] ByteArray, uint Offset, uint Length) { byte[] Bytes = new byte[Length]; Buffer.BlockCopy(ByteArray, (int)Offset, Bytes, 0, (int)Length); return System.Text.Encoding.ASCII.GetString(Bytes); } - internal static string ReadUnicodeString(byte[] ByteArray, UInt32 Offset, UInt32 Length) + internal static string ReadUnicodeString(byte[] ByteArray, uint Offset, uint Length) { byte[] Bytes = new byte[Length]; Buffer.BlockCopy(ByteArray, (int)Offset, Bytes, 0, (int)Length); return System.Text.Encoding.Unicode.GetString(Bytes); } - internal static void WriteAsciiString(byte[] ByteArray, UInt32 Offset, string Text, UInt32? MaxBufferLength = null) + internal static void WriteAsciiString(byte[] ByteArray, uint Offset, string Text, uint? MaxBufferLength = null) { if (MaxBufferLength != null) { @@ -58,7 +58,7 @@ namespace UnifiedFlashingPlatform Buffer.BlockCopy(TextBytes, 0, ByteArray, (int)Offset, WriteLength); } - internal static void WriteUnicodeString(byte[] ByteArray, UInt32 Offset, string Text, UInt32? MaxBufferLength = null) + internal static void WriteUnicodeString(byte[] ByteArray, uint Offset, string Text, uint? MaxBufferLength = null) { if (MaxBufferLength != null) { @@ -75,117 +75,108 @@ namespace UnifiedFlashingPlatform Buffer.BlockCopy(TextBytes, 0, ByteArray, (int)Offset, WriteLength); } - internal static UInt32 ReadUInt32(byte[] ByteArray, UInt32 Offset) + internal static uint ReadUInt32(byte[] ByteArray, uint Offset) { return BitConverter.ToUInt32(ByteArray, (int)Offset); } - internal static void WriteUInt32(byte[] ByteArray, UInt32 Offset, UInt32 Value) + internal static void WriteUInt32(byte[] ByteArray, uint Offset, uint Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 4); } - internal static Int32 ReadInt32(byte[] ByteArray, UInt32 Offset) + internal static int ReadInt32(byte[] ByteArray, uint Offset) { return BitConverter.ToInt32(ByteArray, (int)Offset); } - internal static void WriteInt32(byte[] ByteArray, UInt32 Offset, Int32 Value) + internal static void WriteInt32(byte[] ByteArray, uint Offset, int Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 4); } - internal static UInt16 ReadUInt16(byte[] ByteArray, UInt32 Offset) + internal static ushort ReadUInt16(byte[] ByteArray, uint Offset) { return BitConverter.ToUInt16(ByteArray, (int)Offset); } - internal static void WriteUInt16(byte[] ByteArray, UInt32 Offset, UInt16 Value) + internal static void WriteUInt16(byte[] ByteArray, uint Offset, ushort Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 2); } - internal static Int16 ReadInt16(byte[] ByteArray, UInt32 Offset) + internal static short ReadInt16(byte[] ByteArray, uint Offset) { return BitConverter.ToInt16(ByteArray, (int)Offset); } - internal static void WriteInt16(byte[] ByteArray, UInt32 Offset, Int16 Value) + internal static void WriteInt16(byte[] ByteArray, uint Offset, short Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 2); } - internal static byte ReadUInt8(byte[] ByteArray, UInt32 Offset) + internal static byte ReadUInt8(byte[] ByteArray, uint Offset) { return ByteArray[Offset]; } - internal static void WriteUInt8(byte[] ByteArray, UInt32 Offset, byte Value) + internal static void WriteUInt8(byte[] ByteArray, uint Offset, byte Value) { ByteArray[Offset] = Value; } - internal static UInt32 ReadUInt24(byte[] ByteArray, UInt32 Offset) + internal static uint ReadUInt24(byte[] ByteArray, uint Offset) { - return (UInt32)(ByteArray[Offset] + (ByteArray[Offset + 1] << 8) + (ByteArray[Offset + 2] << 16)); + return (uint)(ByteArray[Offset] + (ByteArray[Offset + 1] << 8) + (ByteArray[Offset + 2] << 16)); } - internal static void WriteUInt24(byte[] ByteArray, UInt32 Offset, UInt32 Value) + internal static void WriteUInt24(byte[] ByteArray, uint Offset, uint Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 3); } - internal static UInt64 ReadUInt64(byte[] ByteArray, UInt32 Offset) + internal static ulong ReadUInt64(byte[] ByteArray, uint Offset) { return BitConverter.ToUInt64(ByteArray, (int)Offset); } - internal static void WriteUInt64(byte[] ByteArray, UInt32 Offset, UInt64 Value) + internal static void WriteUInt64(byte[] ByteArray, uint Offset, ulong Value) { Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, ByteArray, (int)Offset, 8); } - internal static Guid ReadGuid(byte[] ByteArray, UInt32 Offset) + internal static Guid ReadGuid(byte[] ByteArray, uint Offset) { byte[] GuidBuffer = new byte[0x10]; Buffer.BlockCopy(ByteArray, (int)Offset, GuidBuffer, 0, 0x10); return new Guid(GuidBuffer); } - internal static void WriteGuid(byte[] ByteArray, UInt32 Offset, Guid Value) + internal static void WriteGuid(byte[] ByteArray, uint Offset, Guid Value) { Buffer.BlockCopy(Value.ToByteArray(), 0, ByteArray, (int)Offset, 0x10); } - internal static UInt32 Align(UInt32 Base, UInt32 Offset, UInt32 Alignment) + internal static uint Align(uint Base, uint Offset, uint Alignment) { - if (((Offset - Base) % Alignment) == 0) - { - return Offset; - } - else - { - return ((((Offset - Base) / Alignment) + 1) * Alignment) + Base; - } + return ((Offset - Base) % Alignment) == 0 ? Offset : ((((Offset - Base) / Alignment) + 1) * Alignment) + Base; } - internal static UInt32? FindPatternInFile(string FileName, byte[] Pattern, byte[] Mask, out byte[] OutPattern) + internal static uint? FindPatternInFile(string FileName, byte[] Pattern, byte[] Mask, out byte[]? OutPattern) { // The mask is optional. // In the mask 0x00 means the value must match, and 0xFF means that this position is a wildcard. - UInt32? Result = null; + uint? Result = null; FileStream Stream = new(FileName, FileMode.Open, FileAccess.Read); byte[] Buffer = new byte[0x10000 + Pattern.Length - 1]; - UInt32 BufferReadPosition = 0; // Position in buffer where file-chunk is being read. - UInt32 BytesInBuffer = 0; - UInt32 BytesRead; - UInt32 SearchPositionFile = 0; - UInt32 SearchPositionBuffer = 0; - UInt32 BufferFileOffset = 0; // Offset in file where data from buffer is located. - bool Match = false; + uint BytesInBuffer = 0; + uint BytesRead; + uint SearchPositionFile = 0; + uint SearchPositionBuffer = 0; + uint BufferFileOffset = 0; // Offset in file where data from buffer is located. int i; OutPattern = null; @@ -199,16 +190,16 @@ namespace UnifiedFlashingPlatform { System.Buffer.BlockCopy(Buffer, (int)SearchPositionBuffer, Buffer, 0, (int)(BytesInBuffer - SearchPositionBuffer)); } - BufferReadPosition = BytesInBuffer - SearchPositionBuffer; + uint BufferReadPosition = BytesInBuffer - SearchPositionBuffer; BytesInBuffer -= SearchPositionBuffer; BufferFileOffset += SearchPositionBuffer; SearchPositionBuffer = 0; - BytesRead = (UInt32)Stream.Read(Buffer, (int)BufferReadPosition, Buffer.Length - (int)BufferReadPosition); + BytesRead = (uint)Stream.Read(Buffer, (int)BufferReadPosition, Buffer.Length - (int)BufferReadPosition); BytesInBuffer += BytesRead; } - Match = true; + bool Match = true; for (i = 0; i < Pattern.Length; i++) { if (Buffer[SearchPositionBuffer + i] != Pattern[i]) @@ -239,22 +230,22 @@ namespace UnifiedFlashingPlatform return Result; } - internal static UInt32? FindAscii(byte[] SourceBuffer, string Pattern) + internal static uint? FindAscii(byte[] SourceBuffer, string Pattern) { return FindPattern(SourceBuffer, System.Text.Encoding.ASCII.GetBytes(Pattern), null, null); } - internal static UInt32? FindUnicode(byte[] SourceBuffer, string Pattern) + internal static uint? FindUnicode(byte[] SourceBuffer, string Pattern) { return FindPattern(SourceBuffer, System.Text.Encoding.Unicode.GetBytes(Pattern), null, null); } - internal static UInt32? FindUint(byte[] SourceBuffer, UInt32 Pattern) + internal static uint? FindUint(byte[] SourceBuffer, uint Pattern) { return FindPattern(SourceBuffer, BitConverter.GetBytes(Pattern), null, null); } - internal static UInt32? FindPattern(byte[] SourceBuffer, byte[] Pattern, byte[] Mask, byte[] OutPattern) + internal static uint? FindPattern(byte[] SourceBuffer, byte[] Pattern, byte[]? Mask, byte[]? OutPattern) { return FindPattern(SourceBuffer, 0, null, Pattern, Mask, OutPattern); } @@ -264,20 +255,19 @@ namespace UnifiedFlashingPlatform return System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(Array1, Array2); } - internal static UInt32? FindPattern(byte[] SourceBuffer, uint SourceOffset, uint? SourceSize, byte[] Pattern, byte[] Mask, byte[] OutPattern) + internal static uint? FindPattern(byte[] SourceBuffer, uint SourceOffset, uint? SourceSize, byte[] Pattern, byte[] Mask, byte[] OutPattern) { // The mask is optional. // In the mask 0x00 means the value must match, and 0xFF means that this position is a wildcard. - UInt32? Result = null; + uint? Result = null; - UInt32 SearchPosition = SourceOffset; - bool Match = false; + uint SearchPosition = SourceOffset; int i; while ((SearchPosition <= (SourceBuffer.Length - Pattern.Length)) && ((SourceSize == null) || (SearchPosition <= (SourceOffset + SourceSize - Pattern.Length)))) { - Match = true; + bool Match = true; for (i = 0; i < Pattern.Length; i++) { if (SourceBuffer[SearchPosition + i] != Pattern[i]) @@ -308,11 +298,11 @@ namespace UnifiedFlashingPlatform return Result; } - internal static byte CalculateChecksum8(byte[] Buffer, UInt32 Offset, UInt32 Size) + internal static byte CalculateChecksum8(byte[] Buffer, uint Offset, uint Size) { byte Checksum = 0; - for (UInt32 i = Offset; i < (Offset + Size); i++) + for (uint i = Offset; i < (Offset + Size); i++) { Checksum += Buffer[i]; } @@ -320,19 +310,19 @@ namespace UnifiedFlashingPlatform return (byte)(0x100 - Checksum); } - internal static UInt16 CalculateChecksum16(byte[] Buffer, UInt32 Offset, UInt32 Size) + internal static ushort CalculateChecksum16(byte[] Buffer, uint Offset, uint Size) { - UInt16 Checksum = 0; + ushort Checksum = 0; - for (UInt32 i = Offset; i < (Offset + Size - 1); i += 2) + for (uint i = Offset; i < (Offset + Size - 1); i += 2) { Checksum += BitConverter.ToUInt16(Buffer, (int)i); } - return (UInt16)(0x10000 - Checksum); + return (ushort)(0x10000 - Checksum); } - private static readonly UInt32[] CRC32Table = new UInt32[] { + private static readonly uint[] CRC32Table = new uint[] { 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, @@ -378,7 +368,7 @@ namespace UnifiedFlashingPlatform 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; - internal static UInt32 CRC32(byte[] Input, UInt32 Offset, UInt32 Length) + internal static uint CRC32(byte[] Input, uint Offset, uint Length) { if ((Input == null) || ((Offset + Length) > Input.Length)) { @@ -388,7 +378,7 @@ namespace UnifiedFlashingPlatform unchecked { uint crc = (uint)(((uint)0) ^ (-1)); - for (var i = Offset; i < (Offset + Length); i++) + for (uint i = Offset; i < (Offset + Length); i++) { crc = (crc >> 8) ^ CRC32Table[(crc ^ Input[i]) & 0xFF]; } diff --git a/DecompressedStream.cs b/DecompressedStream.cs index 4b17ac4..113c964 100644 --- a/DecompressedStream.cs +++ b/DecompressedStream.cs @@ -31,8 +31,8 @@ namespace UnifiedFlashingPlatform { private readonly Stream UnderlyingStream; private readonly bool IsSourceCompressed; - private readonly UInt64 DecompressedLength; - private Int64 ReadPosition = 0; + private readonly ulong DecompressedLength; + private long ReadPosition = 0; // For reading a compressed stream internal DecompressedStream(Stream InputStream) @@ -44,28 +44,28 @@ namespace UnifiedFlashingPlatform Buffer.BlockCopy(Encoding.ASCII.GetBytes("CompressedPartition"), 0, Signature, 0x01, "CompressedPartition".Length); Signature["CompressedPartition".Length + 1] = 0x00; - int PrimaryHeaderSize = 0x0A + "CompressedPartition".Length; + _ = 0x0A + "CompressedPartition".Length; byte[] SignatureRead = new byte[Signature.Length]; - UnderlyingStream.Read(SignatureRead, 0, Signature.Length); + _ = UnderlyingStream.Read(SignatureRead, 0, Signature.Length); IsSourceCompressed = StructuralComparisons.StructuralEqualityComparer.Equals(Signature, SignatureRead); if (IsSourceCompressed) { byte[] FormatVersionBytes = new byte[4]; - UnderlyingStream.Read(FormatVersionBytes, 0, 4); + _ = UnderlyingStream.Read(FormatVersionBytes, 0, 4); if (BitConverter.ToUInt32(FormatVersionBytes, 0) > 1) // Max supported format version = 1 { throw new InvalidDataException(); } byte[] HeaderSizeBytes = new byte[4]; - UnderlyingStream.Read(HeaderSizeBytes, 0, 4); - UInt32 HeaderSize = BitConverter.ToUInt32(HeaderSizeBytes, 0); + _ = UnderlyingStream.Read(HeaderSizeBytes, 0, 4); + uint HeaderSize = BitConverter.ToUInt32(HeaderSizeBytes, 0); if (HeaderSize >= (Signature.Length + 0x10)) { byte[] DecompressedLengthBytes = new byte[8]; - UnderlyingStream.Read(DecompressedLengthBytes, 0, 8); + _ = UnderlyingStream.Read(DecompressedLengthBytes, 0, 8); DecompressedLength = BitConverter.ToUInt64(DecompressedLengthBytes, 0); } else @@ -73,11 +73,11 @@ namespace UnifiedFlashingPlatform throw new InvalidDataException(); } - UInt32 HeaderBytesRemaining = (UInt32)(HeaderSize - Signature.Length - 0x10); + uint HeaderBytesRemaining = (uint)(HeaderSize - Signature.Length - 0x10); if (HeaderBytesRemaining > 0) { byte[] HeaderBytes = new byte[HeaderBytesRemaining]; - UnderlyingStream.Read(HeaderBytes, 0, (int)HeaderBytesRemaining); + _ = UnderlyingStream.Read(HeaderBytes, 0, (int)HeaderBytesRemaining); } UnderlyingStream = new GZipStream(UnderlyingStream, CompressionMode.Decompress, false); @@ -88,45 +88,47 @@ namespace UnifiedFlashingPlatform } } - public override bool CanRead { get { return true; } } - public override bool CanSeek { get { return false; } } + public override bool CanRead => true; + public override bool CanSeek => false; public override int Read(byte[] buffer, int offset, int count) { int RealCount = UnderlyingStream.Read(buffer, offset, count); ReadPosition += RealCount; return RealCount; } - public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } public override long Position { - get { return ReadPosition; } - set { throw new NotSupportedException(); } + get => ReadPosition; + set => throw new NotSupportedException(); } - public override bool CanTimeout { get { return UnderlyingStream.CanTimeout; } } - public override bool CanWrite { get { return true; } } - public override long Length + public override bool CanTimeout => UnderlyingStream.CanTimeout; + public override bool CanWrite => true; + public override long Length => IsSourceCompressed ? (long)DecompressedLength : UnderlyingStream.Length; + public override void SetLength(long value) { - get - { - if (IsSourceCompressed) - { - return (long)DecompressedLength; - } - else - { - return UnderlyingStream.Length; - } - } + throw new NotSupportedException(); + } + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + public override void Flush() + { + UnderlyingStream.Flush(); + } + public override void Close() + { + UnderlyingStream.Close(); } - public override void SetLength(long value) { throw new NotSupportedException(); } - public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } - public override void Flush() { UnderlyingStream.Flush(); } - public override void Close() { UnderlyingStream.Close(); } protected override void Dispose(bool disposing) { if (disposing) { - this.UnderlyingStream.Dispose(); + UnderlyingStream.Dispose(); } } } diff --git a/GPT.cs b/GPT.cs index 5886913..18464b0 100644 --- a/GPT.cs +++ b/GPT.cs @@ -31,16 +31,16 @@ namespace UnifiedFlashingPlatform public class GPT { private byte[] GPTBuffer; - private readonly UInt32 HeaderOffset; - private readonly UInt32 HeaderSize; - private UInt32 TableOffset; - private UInt32 TableSize; - private readonly UInt32 PartitionEntrySize; - private readonly UInt32 MaxPartitions; - internal UInt64 FirstUsableSector; - internal UInt64 LastUsableSector; + private readonly uint HeaderOffset; + private readonly uint HeaderSize; + private uint TableOffset; + private uint TableSize; + private readonly uint PartitionEntrySize; + private readonly uint MaxPartitions; + internal ulong FirstUsableSector; + internal ulong LastUsableSector; internal bool HasChanged = false; - internal UInt32 SectorSize; + internal uint SectorSize; [XmlElement("Partition")] public List Partitions = new(); @@ -49,17 +49,17 @@ namespace UnifiedFlashingPlatform { } - internal GPT(byte[] GPTBuffer, UInt32 SectorSize) + internal GPT(byte[] GPTBuffer, uint SectorSize) { this.GPTBuffer = GPTBuffer; this.SectorSize = SectorSize; - UInt32? TempHeaderOffset = ByteOperations.FindAscii(GPTBuffer, "EFI PART"); + uint? TempHeaderOffset = ByteOperations.FindAscii(GPTBuffer, "EFI PART"); if (TempHeaderOffset == null) { throw new WPinternalsException("Bad GPT", "The GPT read isn't valid. Couldn't find the text \"EFI PART\"."); } - HeaderOffset = (UInt32)TempHeaderOffset; + HeaderOffset = (uint)TempHeaderOffset; HeaderSize = ByteOperations.ReadUInt32(GPTBuffer, HeaderOffset + 0x0C); TableOffset = HeaderOffset + SectorSize; FirstUsableSector = ByteOperations.ReadUInt64(GPTBuffer, HeaderOffset + 0x28); @@ -72,23 +72,25 @@ namespace UnifiedFlashingPlatform throw new WPinternalsException("Bad GPT", "The GPT read isn't valid. The sizes defined in the GPT header exceed the provided GPT size."); } - UInt32 PartitionOffset = TableOffset; + uint PartitionOffset = TableOffset; while (PartitionOffset < (TableOffset + TableSize)) { - string Name = ByteOperations.ReadUnicodeString(GPTBuffer, PartitionOffset + 0x38, 0x48).TrimEnd(new char[] {(char)0, ' '}); + string Name = ByteOperations.ReadUnicodeString(GPTBuffer, PartitionOffset + 0x38, 0x48).TrimEnd(new char[] { (char)0, ' ' }); if (Name.Length == 0) { break; } - Partition CurrentPartition = new(); - CurrentPartition.Name = Name; - CurrentPartition.FirstSector = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x20); - CurrentPartition.LastSector = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x28); - CurrentPartition.PartitionTypeGuid = ByteOperations.ReadGuid(GPTBuffer, PartitionOffset + 0x00); - CurrentPartition.PartitionGuid = ByteOperations.ReadGuid(GPTBuffer, PartitionOffset + 0x10); - CurrentPartition.Attributes = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x30); + Partition CurrentPartition = new() + { + Name = Name, + FirstSector = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x20), + LastSector = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x28), + PartitionTypeGuid = ByteOperations.ReadGuid(GPTBuffer, PartitionOffset + 0x00), + PartitionGuid = ByteOperations.ReadGuid(GPTBuffer, PartitionOffset + 0x10), + Attributes = ByteOperations.ReadUInt64(GPTBuffer, PartitionOffset + 0x30) + }; Partitions.Add(CurrentPartition); PartitionOffset += PartitionEntrySize; } @@ -156,7 +158,7 @@ namespace UnifiedFlashingPlatform SBL2.PartitionTypeGuid = HackPartition.PartitionTypeGuid; SBL2.PartitionGuid = HackPartition.PartitionGuid; - Partitions.Remove(HackPartition); + _ = Partitions.Remove(HackPartition); SBL1.LastSector++; } @@ -179,7 +181,7 @@ namespace UnifiedFlashingPlatform Array.Clear(GPTBuffer, (int)TableOffset, (int)TableSize); } - UInt32 PartitionOffset = TableOffset; + uint PartitionOffset = TableOffset; foreach (Partition CurrentPartition in Partitions) { ByteOperations.WriteGuid(GPTBuffer, PartitionOffset + 0x00, CurrentPartition.PartitionTypeGuid); @@ -210,7 +212,7 @@ namespace UnifiedFlashingPlatform MergePartitions(tr.ReadToEnd(), RoundToChunks); } - internal void MergePartitions(string Xml, bool RoundToChunks, ZipArchive Archive = null) + internal void MergePartitions(string Xml, bool RoundToChunks, ZipArchive? Archive = null) { GPT GptToMerge; if (Xml == null) @@ -352,7 +354,7 @@ namespace UnifiedFlashingPlatform if ((NewPartition == null) || (NewPartition.FirstSector == 0)) { DynamicPartitions.Insert(0, OldPartition); - this.Partitions.Remove(OldPartition); + _ = Partitions.Remove(OldPartition); } else { @@ -370,8 +372,8 @@ namespace UnifiedFlashingPlatform // The partitions in the new GPT data will be applied to the current partition-table. // Existing partitions, which are overwritten by the new partitions will be removed from the existing GPT. // Existing partition with the same name in the existing GPT is reused (guids and attribs remain, if not specified). - UInt64 LowestSector = 0; - Partition DPP = this.GetPartition("DPP"); + ulong LowestSector = 0; + Partition DPP = GetPartition("DPP"); if (DPP != null) { LowestSector = DPP.LastSector + 1; @@ -391,14 +393,14 @@ namespace UnifiedFlashingPlatform throw new WPinternalsException("Bad sector alignment for partition: " + NewPartition.Name + ". The partition is located before DPP."); } - Partition CurrentPartition = this.GetPartition(NewPartition.Name); + Partition CurrentPartition = GetPartition(NewPartition.Name); if (CurrentPartition == null) { CurrentPartition = new Partition { Name = NewPartition.Name }; - this.Partitions.Add(CurrentPartition); + Partitions.Add(CurrentPartition); HasChanged = true; } @@ -448,11 +450,11 @@ namespace UnifiedFlashingPlatform CurrentPartition.PartitionTypeGuid = Guid.NewGuid(); } - for (int i = this.Partitions.Count - 1; i >= 0; i--) + for (int i = Partitions.Count - 1; i >= 0; i--) { - if (this.Partitions[i] != CurrentPartition && (CurrentPartition.FirstSector <= this.Partitions[i].LastSector) && (CurrentPartition.LastSector >= this.Partitions[i].FirstSector)) + if (Partitions[i] != CurrentPartition && (CurrentPartition.FirstSector <= Partitions[i].LastSector) && (CurrentPartition.LastSector >= Partitions[i].FirstSector)) { - this.Partitions.RemoveAt(i); + Partitions.RemoveAt(i); HasChanged = true; } } @@ -462,7 +464,7 @@ namespace UnifiedFlashingPlatform { // All partitions listed in the archive, which are present in the existing GPT, should overwrite the existing partition. // Check if the sizes of the partitions in the archive do not exceed the size of the partition, as listed in the GPT. - foreach (Partition OldPartition in this.Partitions) + foreach (Partition OldPartition in Partitions) { ZipArchiveEntry Entry = Archive.Entries.FirstOrDefault(e => string.Equals(e.Name, OldPartition.Name, StringComparison.CurrentCultureIgnoreCase) || e.Name.StartsWith(OldPartition.Name + ".", true, System.Globalization.CultureInfo.GetCultureInfo("en-US"))); if (Entry != null) @@ -476,8 +478,8 @@ namespace UnifiedFlashingPlatform catch { } DecompressedStream.Close(); - UInt64 MaxPartitionSizeInSectors = OldPartition.SizeInSectors; - Partition NextPartition = this.Partitions.Where(p => p.FirstSector > OldPartition.FirstSector).OrderBy(p => p.FirstSector).FirstOrDefault(); + ulong MaxPartitionSizeInSectors = OldPartition.SizeInSectors; + Partition NextPartition = Partitions.Where(p => p.FirstSector > OldPartition.FirstSector).OrderBy(p => p.FirstSector).FirstOrDefault(); if (NextPartition != null) { MaxPartitionSizeInSectors = NextPartition.FirstSector - OldPartition.FirstSector; @@ -498,15 +500,15 @@ namespace UnifiedFlashingPlatform // All remaining partitions in the archive, which were listed in the original GPT, // should be added at the end of the partition-table. - UInt64 FirstFreeSector = 0x5000; - if (this.Partitions.Count > 0) + ulong FirstFreeSector = 0x5000; + if (Partitions.Count > 0) { - FirstFreeSector = this.Partitions.Max(p => p.LastSector) + 1; + FirstFreeSector = Partitions.Max(p => p.LastSector) + 1; // Always start a new partition on a new chunk (0x100 sector boundary), to be more flexible during custom flash if (RoundToChunks && (((double)FirstFreeSector % 0x100) != 0)) { - FirstFreeSector = (UInt64)(Math.Ceiling((double)FirstFreeSector / 0x100) * 0x100); + FirstFreeSector = (ulong)(Math.Ceiling((double)FirstFreeSector / 0x100) * 0x100); } } foreach (Partition NewPartition in DynamicPartitions) @@ -530,18 +532,18 @@ namespace UnifiedFlashingPlatform NewPartition.SizeInSectors = StreamLengthInSectors; HasChanged = true; } - this.Partitions.Add(NewPartition); + Partitions.Add(NewPartition); FirstFreeSector += StreamLengthInSectors; // Always start a new partition on a new chunk (0x100 sector boundary), to be more flexible during custom flash if (RoundToChunks && (((double)FirstFreeSector % 0x100) != 0)) { - FirstFreeSector = (UInt64)(Math.Ceiling((double)FirstFreeSector / 0x100) * 0x100); + FirstFreeSector = (ulong)(Math.Ceiling((double)FirstFreeSector / 0x100) * 0x100); } } } - Rebuild(); + _ = Rebuild(); } internal void WritePartitions(string Path) @@ -549,7 +551,7 @@ namespace UnifiedFlashingPlatform string DirPath = System.IO.Path.GetDirectoryName(Path); if (!string.IsNullOrEmpty(DirPath) && !Directory.Exists(DirPath)) { - Directory.CreateDirectory(DirPath); + _ = Directory.CreateDirectory(DirPath); } XmlSerializer x = new(typeof(GPT), ""); @@ -581,7 +583,7 @@ namespace UnifiedFlashingPlatform { // This is necessary, because the partitions and backup-partitions can exchange. // This may cause the startsector to be higher than the maximum allowed sector for flashing with a Lumia V1 programmer (hardcoded in programmer) - foreach (string RevisePartitionName in (List)(new(new string[] { "SBL1", "SBL2", "SBL3", "UEFI", "TZ", "RPM", "WINSECAPP" }))) + foreach (string RevisePartitionName in (List)new(new string[] { "SBL1", "SBL2", "SBL3", "UEFI", "TZ", "RPM", "WINSECAPP" })) { Partition RevisePartition = GetPartition(RevisePartitionName); Partition ReviseBackupPartition = GetPartition("BACKUP_" + RevisePartitionName); @@ -607,30 +609,20 @@ namespace UnifiedFlashingPlatform public class Partition { - private UInt64 _SizeInSectors; - private UInt64 _FirstSector; - private UInt64 _LastSector; + private ulong _SizeInSectors; + private ulong _FirstSector; + private ulong _LastSector; public string Name; // 0x48 public Guid PartitionTypeGuid; // 0x10 public Guid PartitionGuid; // 0x10 [XmlIgnore] - internal UInt64 Attributes; // 0x08 + internal ulong Attributes; // 0x08 [XmlIgnore] - internal UInt64 SizeInSectors + internal ulong SizeInSectors { - get - { - if (_SizeInSectors != 0) - { - return _SizeInSectors; - } - else - { - return LastSector - FirstSector + 1; - } - } + get => _SizeInSectors != 0 ? _SizeInSectors : LastSector - FirstSector + 1; set { _SizeInSectors = value; @@ -642,12 +634,9 @@ namespace UnifiedFlashingPlatform } [XmlIgnore] - internal UInt64 FirstSector // 0x08 + internal ulong FirstSector // 0x08 { - get - { - return _FirstSector; - } + get => _FirstSector; set { _FirstSector = value; @@ -659,12 +648,9 @@ namespace UnifiedFlashingPlatform } [XmlIgnore] - internal UInt64 LastSector // 0x08 + internal ulong LastSector // 0x08 { - get - { - return _LastSector; - } + get => _LastSector; set { _LastSector = value; @@ -673,51 +659,27 @@ namespace UnifiedFlashingPlatform } [XmlIgnore] - public string Volume - { - get - { - return @"\\?\Volume" + PartitionGuid.ToString("b") + @"\"; - } - } + public string Volume => @"\\?\Volume" + PartitionGuid.ToString("b") + @"\"; [XmlElement(ElementName = "FirstSector")] public string FirstSectorAsString { - get - { - return "0x" + FirstSector.ToString("X16"); - } - set - { - FirstSector = Convert.ToUInt64(value, 16); - } + get => "0x" + FirstSector.ToString("X16"); + set => FirstSector = Convert.ToUInt64(value, 16); } [XmlElement(ElementName = "LastSector")] public string LastSectorAsString { - get - { - return "0x" + LastSector.ToString("X16"); - } - set - { - LastSector = Convert.ToUInt64(value, 16); - } + get => "0x" + LastSector.ToString("X16"); + set => LastSector = Convert.ToUInt64(value, 16); } [XmlElement(ElementName = "Attributes")] public string AttributesAsString { - get - { - return "0x" + Attributes.ToString("X16"); - } - set - { - Attributes = Convert.ToUInt64(value, 16); - } + get => "0x" + Attributes.ToString("X16"); + set => Attributes = Convert.ToUInt64(value, 16); } } } \ No newline at end of file diff --git a/ReadSeekableStream.cs b/ReadSeekableStream.cs index 35d1c59..1880682 100644 --- a/ReadSeekableStream.cs +++ b/ReadSeekableStream.cs @@ -44,8 +44,8 @@ namespace UnifiedFlashingPlatform _seekBackBuffer = new byte[seekBackBufferSize]; } - public override bool CanRead { get { return true; } } - public override bool CanSeek { get { return true; } } + public override bool CanRead => true; + public override bool CanSeek => true; public override int Read(byte[] buffer, int offset, int count) { @@ -66,9 +66,9 @@ namespace UnifiedFlashingPlatform { _underlyingPosition += bytesReadFromUnderlying; - var copyToBufferCount = Math.Min(bytesReadFromUnderlying, _seekBackBuffer.Length); - var copyToBufferOffset = Math.Min(_seekBackBufferCount, _seekBackBuffer.Length - copyToBufferCount); - var bufferBytesToMove = Math.Min(_seekBackBufferCount - 1, copyToBufferOffset); + int copyToBufferCount = Math.Min(bytesReadFromUnderlying, _seekBackBuffer.Length); + int copyToBufferOffset = Math.Min(_seekBackBufferCount, _seekBackBuffer.Length - copyToBufferCount); + int bufferBytesToMove = Math.Min(_seekBackBufferCount - 1, copyToBufferOffset); if (bufferBytesToMove > 0) { @@ -90,28 +90,17 @@ namespace UnifiedFlashingPlatform return SeekFromEnd((int)Math.Max(0, -offset)); } - var relativeOffset = origin == SeekOrigin.Current + long relativeOffset = origin == SeekOrigin.Current ? offset : offset - Position; - if (relativeOffset == 0) - { - return Position; - } - else if (relativeOffset > 0) - { - return SeekForward(relativeOffset); - } - else - { - return SeekBackwards(-relativeOffset); - } + return relativeOffset == 0 ? Position : relativeOffset > 0 ? SeekForward(relativeOffset) : SeekBackwards(-relativeOffset); } private long SeekForward(long origOffset) { long offset = origOffset; - var seekBackBufferLength = _seekBackBuffer.Length; + int seekBackBufferLength = _seekBackBuffer.Length; int backwardSoughtBytes = _seekBackBufferCount - _seekBackBufferIndex; int seekForwardInBackBuffer = (int)Math.Min(offset, backwardSoughtBytes); @@ -123,37 +112,34 @@ namespace UnifiedFlashingPlatform // first completely fill seekBackBuffer to remove special cases from while loop below if (_seekBackBufferCount < seekBackBufferLength) { - var maxRead = seekBackBufferLength - _seekBackBufferCount; + int maxRead = seekBackBufferLength - _seekBackBufferCount; if (offset < maxRead) { maxRead = (int)offset; } - var bytesRead = _underlyingStream.Read(_seekBackBuffer, _seekBackBufferCount, maxRead); + int bytesRead = _underlyingStream.Read(_seekBackBuffer, _seekBackBufferCount, maxRead); _underlyingPosition += bytesRead; _seekBackBufferCount += bytesRead; _seekBackBufferIndex = _seekBackBufferCount; if (bytesRead < maxRead) { - if (_seekBackBufferCount < offset) - { - throw new NotSupportedException("Reached end of stream seeking forward " + origOffset + " bytes"); - } - - return Position; + return _seekBackBufferCount < offset + ? throw new NotSupportedException("Reached end of stream seeking forward " + origOffset + " bytes") + : Position; } offset -= bytesRead; } // now alternate between filling tempBuffer and seekBackBuffer bool fillTempBuffer = true; - var tempBuffer = new byte[seekBackBufferLength]; + byte[] tempBuffer = new byte[seekBackBufferLength]; while (offset > 0) { - var maxRead = offset < seekBackBufferLength ? (int)offset : seekBackBufferLength; - var bytesRead = _underlyingStream.Read(fillTempBuffer ? tempBuffer : _seekBackBuffer, 0, maxRead); + int maxRead = offset < seekBackBufferLength ? (int)offset : seekBackBufferLength; + int bytesRead = _underlyingStream.Read(fillTempBuffer ? tempBuffer : _seekBackBuffer, 0, maxRead); _underlyingPosition += bytesRead; - var bytesReadDiff = maxRead - bytesRead; + int bytesReadDiff = maxRead - bytesRead; offset -= bytesRead; if (bytesReadDiff > 0 /* reached end-of-stream */ || offset == 0) { @@ -187,7 +173,7 @@ namespace UnifiedFlashingPlatform private long SeekBackwards(long offset) { - var intOffset = (int)offset; + int intOffset = (int)offset; if (offset > int.MaxValue || intOffset > _seekBackBufferIndex) { throw new NotSupportedException("Cannot currently seek backwards more than " + _seekBackBufferIndex + " bytes"); @@ -199,8 +185,8 @@ namespace UnifiedFlashingPlatform private long SeekFromEnd(long offset) { - var intOffset = (int)offset; - var seekBackBufferLength = _seekBackBuffer.Length; + int intOffset = (int)offset; + int seekBackBufferLength = _seekBackBuffer.Length; if (offset > int.MaxValue || intOffset > seekBackBufferLength) { throw new NotSupportedException("Cannot seek backwards from end more than " + seekBackBufferLength + " bytes"); @@ -209,19 +195,16 @@ namespace UnifiedFlashingPlatform // first completely fill seekBackBuffer to remove special cases from while loop below if (_seekBackBufferCount < seekBackBufferLength) { - var maxRead = seekBackBufferLength - _seekBackBufferCount; - var bytesRead = _underlyingStream.Read(_seekBackBuffer, _seekBackBufferCount, maxRead); + int maxRead = seekBackBufferLength - _seekBackBufferCount; + int bytesRead = _underlyingStream.Read(_seekBackBuffer, _seekBackBufferCount, maxRead); _underlyingPosition += bytesRead; _seekBackBufferCount += bytesRead; _seekBackBufferIndex = Math.Max(0, _seekBackBufferCount - intOffset); if (bytesRead < maxRead) { - if (_seekBackBufferCount < intOffset) - { - throw new NotSupportedException("Could not seek backwards from end " + intOffset + " bytes"); - } - - return Position; + return _seekBackBufferCount < intOffset + ? throw new NotSupportedException("Could not seek backwards from end " + intOffset + " bytes") + : Position; } } else @@ -231,12 +214,12 @@ namespace UnifiedFlashingPlatform // now alternate between filling tempBuffer and seekBackBuffer bool fillTempBuffer = true; - var tempBuffer = new byte[seekBackBufferLength]; + byte[] tempBuffer = new byte[seekBackBufferLength]; while (true) { - var bytesRead = _underlyingStream.Read(fillTempBuffer ? tempBuffer : _seekBackBuffer, 0, seekBackBufferLength); + int bytesRead = _underlyingStream.Read(fillTempBuffer ? tempBuffer : _seekBackBuffer, 0, seekBackBufferLength); _underlyingPosition += bytesRead; - var bytesReadDiff = seekBackBufferLength - bytesRead; + int bytesReadDiff = seekBackBufferLength - bytesRead; if (bytesReadDiff > 0) // reached end-of-stream { if (fillTempBuffer) @@ -265,18 +248,33 @@ namespace UnifiedFlashingPlatform public override long Position { - get { return _underlyingPosition - (_seekBackBufferCount - _seekBackBufferIndex); } - set { Seek(value, SeekOrigin.Begin); } + get => _underlyingPosition - (_seekBackBufferCount - _seekBackBufferIndex); + set => Seek(value, SeekOrigin.Begin); } - public override bool CanTimeout { get { return _underlyingStream.CanTimeout; } } - public override bool CanWrite { get { return _underlyingStream.CanWrite; } } - public override long Length { get { return _underlyingStream.Length; } } - public override void SetLength(long value) { _underlyingStream.SetLength(value); } - public override void Write(byte[] buffer, int offset, int count) { _underlyingStream.Write(buffer, offset, count); } - public override void Flush() { _underlyingStream.Flush(); } - public override void Close() { _underlyingStream.Close(); } - public new void Dispose() { _underlyingStream.Dispose(); } + public override bool CanTimeout => _underlyingStream.CanTimeout; + public override bool CanWrite => _underlyingStream.CanWrite; + public override long Length => _underlyingStream.Length; + public override void SetLength(long value) + { + _underlyingStream.SetLength(value); + } + public override void Write(byte[] buffer, int offset, int count) + { + _underlyingStream.Write(buffer, offset, count); + } + public override void Flush() + { + _underlyingStream.Flush(); + } + public override void Close() + { + _underlyingStream.Close(); + } + public new void Dispose() + { + _underlyingStream.Dispose(); + } protected override void Dispose(bool disposing) { if (disposing) diff --git a/UnifiedFlashingPlatformTransport.WPI.cs b/UnifiedFlashingPlatformTransport.WPI.cs index baff060..1a1d3c5 100644 --- a/UnifiedFlashingPlatformTransport.WPI.cs +++ b/UnifiedFlashingPlatformTransport.WPI.cs @@ -5,7 +5,7 @@ namespace UnifiedFlashingPlatform { public partial class UnifiedFlashingPlatformTransport { - public void FlashSectors(UInt32 StartSector, byte[] Data, byte TargetDevice = 0, int Progress = 0) + public void FlashSectors(uint StartSector, byte[] Data, byte TargetDevice = 0, int Progress = 0) { // Start sector is in UInt32, so max size of eMMC is 2 TB. @@ -22,7 +22,7 @@ namespace UnifiedFlashingPlatform Buffer.BlockCopy(Data, 0, Request, 0x40, Data.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void Hello() @@ -84,7 +84,7 @@ namespace UnifiedFlashingPlatform throw new InvalidOperationException("Unable to read GPT!"); } - UInt16 Error = (UInt16)((Buffer[6] << 8) + Buffer[7]); + ushort Error = (ushort)((Buffer[6] << 8) + Buffer[7]); if (Error > 0) { throw new NotSupportedException("ReadGPT: Error 0x" + Error.ToString("X4")); @@ -93,23 +93,15 @@ namespace UnifiedFlashingPlatform // 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")); - } + uint ReturnedGPTBufferLength = (uint)Buffer.Length - 8; + uint SectorSize = Buffer.Length == 0x4408 + ? 512 + : Buffer.Length == 0x6008 + ? (uint)4096 + : 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); + System.Buffer.BlockCopy(Buffer, 8 + (int)SectorSize, GPTBuffer, 0, (int)ReturnedGPTBufferLength - (int)SectorSize); /*if (Switch) { @@ -131,12 +123,9 @@ namespace UnifiedFlashingPlatform 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..]; + return (Response == null) || (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") + ? throw new NotSupportedException() + : Response[5..]; } public void Shutdown() diff --git a/UnifiedFlashingPlatformTransport.cs b/UnifiedFlashingPlatformTransport.cs index 08d012a..8dc96e9 100644 --- a/UnifiedFlashingPlatformTransport.cs +++ b/UnifiedFlashingPlatformTransport.cs @@ -31,10 +31,10 @@ namespace UnifiedFlashingPlatform public partial class UnifiedFlashingPlatformTransport : IDisposable { private bool Disposed = false; - private readonly USBDevice USBDevice = null; - private USBPipe InputPipe = null; - private USBPipe OutputPipe = null; - private object UsbLock = new(); + private readonly USBDevice? USBDevice = null; + private readonly USBPipe? InputPipe = null; + private readonly USBPipe? OutputPipe = null; + private readonly object UsbLock = new(); public UnifiedFlashingPlatformTransport(string DevicePath) { @@ -67,7 +67,7 @@ namespace UnifiedFlashingPlatform public byte[] ExecuteRawMethod(byte[] RawMethod, int Length) { byte[] Buffer = new byte[0xF000]; // Should be at least 0x4408 for receiving the GPT packet. - byte[] Result = null; + byte[]? Result = null; lock (UsbLock) { OutputPipe.Write(RawMethod, 0, Length); @@ -117,55 +117,32 @@ namespace UnifiedFlashingPlatform public string ReadStringParam(string Param) { byte[] Bytes = ReadParam(Param); - if (Bytes == null) - { - return null; - } - - return Encoding.ASCII.GetString(Bytes).Trim('\0'); + return Bytes == null ? null : Encoding.ASCII.GetString(Bytes).Trim('\0'); } public AppType ReadAppType() { byte[] Bytes = ReadParam("APPT"); - if (Bytes == null) - { - return AppType.Min; - } - - if (Bytes[0] == 1) - { - return AppType.UEFI; - } - - return AppType.Min; + return Bytes == null ? AppType.Min : Bytes[0] == 1 ? AppType.UEFI : AppType.Min; } public ResetProtectionInfo? ReadResetProtection() { byte[] Bytes = ReadParam("ATRP"); - if (Bytes == null) - { - return null; - } - - return new ResetProtectionInfo() - { - IsResetProtectionEnabled = Bytes[0] == 1, - MajorVersion = BitConverter.ToUInt32(Bytes[1..5].Reverse().ToArray()), - MinorVersion = BitConverter.ToUInt32(Bytes[5..9].Reverse().ToArray()) - }; + return Bytes == null + ? null + : new ResetProtectionInfo() + { + IsResetProtectionEnabled = Bytes[0] == 1, + MajorVersion = BitConverter.ToUInt32(Bytes[1..5].Reverse().ToArray()), + MinorVersion = BitConverter.ToUInt32(Bytes[5..9].Reverse().ToArray()) + }; } public bool? ReadBitlocker() { byte[] Bytes = ReadParam("BITL"); - if (Bytes == null) - { - return null; - } - - return Bytes[0] == 1; + return Bytes == null ? null : Bytes[0] == 1; } public string ReadBuildInfo() @@ -176,26 +153,16 @@ namespace UnifiedFlashingPlatform public ushort? ReadCurrentBootOption() { byte[] Bytes = ReadParam("CUFO"); - if (Bytes == null || Bytes.Length != 2) - { - return null; - } - - return BitConverter.ToUInt16(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 2 ? null : BitConverter.ToUInt16(Bytes.Reverse().ToArray()); } public bool? ReadDeviceAsyncSupport() { byte[] Bytes = ReadParam("DAS\0"); - if (Bytes == null || Bytes.Length != 2) - { - return null; - } - - return BitConverter.ToUInt16(Bytes.Reverse().ToArray()) == 1; + return Bytes == null || Bytes.Length != 2 ? null : BitConverter.ToUInt16(Bytes.Reverse().ToArray()) == 1; } - public UInt64? ReadDirectoryEntriesSize(string PartitionName, string DirectoryName) + public ulong? ReadDirectoryEntriesSize(string PartitionName, string DirectoryName) { if (PartitionName.Length > 35) { @@ -249,15 +216,15 @@ namespace UnifiedFlashingPlatform return null; } - UInt16 ManufacturerLength = BitConverter.ToUInt16(Bytes[0..2].Reverse().ToArray()); - UInt16 FamilyLength = BitConverter.ToUInt16(Bytes[2..4].Reverse().ToArray()); - UInt16 ProductNameLength = BitConverter.ToUInt16(Bytes[4..6].Reverse().ToArray()); - UInt16 ProductVersionLength = BitConverter.ToUInt16(Bytes[6..8].Reverse().ToArray()); - UInt16 SKUNumberLength = BitConverter.ToUInt16(Bytes[8..10].Reverse().ToArray()); - UInt16 BaseboardManufacturerLength = BitConverter.ToUInt16(Bytes[10..12].Reverse().ToArray()); - UInt16 BaseboardProductLength = BitConverter.ToUInt16(Bytes[12..14].Reverse().ToArray()); + ushort ManufacturerLength = BitConverter.ToUInt16(Bytes[0..2].Reverse().ToArray()); + ushort FamilyLength = BitConverter.ToUInt16(Bytes[2..4].Reverse().ToArray()); + ushort ProductNameLength = BitConverter.ToUInt16(Bytes[4..6].Reverse().ToArray()); + ushort ProductVersionLength = BitConverter.ToUInt16(Bytes[6..8].Reverse().ToArray()); + ushort SKUNumberLength = BitConverter.ToUInt16(Bytes[8..10].Reverse().ToArray()); + ushort BaseboardManufacturerLength = BitConverter.ToUInt16(Bytes[10..12].Reverse().ToArray()); + ushort BaseboardProductLength = BitConverter.ToUInt16(Bytes[12..14].Reverse().ToArray()); - Int32 CurrentOffset = 14; + int CurrentOffset = 14; string Manufacturer = Encoding.ASCII.GetString(Bytes[CurrentOffset..(CurrentOffset + ManufacturerLength)]); CurrentOffset += ManufacturerLength; @@ -293,82 +260,54 @@ namespace UnifiedFlashingPlatform // // Gets the last FFU Flash Operation Data verify speed in KB/s // - public UInt32? ReadDataVerifySpeed() + public uint? ReadDataVerifySpeed() { byte[] Bytes = ReadParam("DTSP"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public Guid? ReadDeviceID() { byte[] Bytes = ReadParam("DUI\0"); - if (Bytes == null || Bytes.Length != 16) - { - return null; - } - - return new Guid(Bytes); + return Bytes == null || Bytes.Length != 16 ? null : new Guid(Bytes); } - public UInt32? ReadEmmcTestResult() + public uint? ReadEmmcTestResult() { byte[] Bytes = ReadParam("EMMT"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } // // Gets the eMMC Size in sectors, if present // - public UInt32? ReadEmmcSize() + public uint? ReadEmmcSize() { byte[] Bytes = ReadParam("EMS\0"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } // // Gets the eMMC Write speed in KB/s // - public UInt32? ReadEmmcWriteSpeed() + public uint? ReadEmmcWriteSpeed() { byte[] Bytes = ReadParam("EMWS"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public FlashAppInfo? ReadFlashAppInfo() { byte[] Bytes = ReadParam("FAI\0"); - if (Bytes == null || Bytes.Length != 6 || Bytes[0] != 2) - { - return null; - } - - return new FlashAppInfo() - { - ProtocolMajorVersion = Bytes[1], - ProtocolMinorVersion = Bytes[2], - ImplementationMajorVersion = Bytes[3], - ImplementationMinorVersion = Bytes[4] - }; + return Bytes == null || Bytes.Length != 6 || Bytes[0] != 2 + ? null + : new FlashAppInfo() + { + ProtocolMajorVersion = Bytes[1], + ProtocolMinorVersion = Bytes[2], + ImplementationMajorVersion = Bytes[3], + ImplementationMinorVersion = Bytes[4] + }; } // @@ -380,18 +319,13 @@ namespace UnifiedFlashingPlatform return ReadStringParam("FO\0\0"); } - public UInt32? ReadFlashingStatus() + public uint? ReadFlashingStatus() { byte[] Bytes = ReadParam("FS\0\0"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } - public UInt64? ReadFileSize(string PartitionName, string FileName) + public ulong? ReadFileSize(string PartitionName, string FileName) { if (PartitionName.Length > 35) { @@ -426,17 +360,12 @@ namespace UnifiedFlashingPlatform public bool? ReadSecureBootStatus() { byte[] Bytes = ReadParam("GSBS"); - if (Bytes == null) - { - return null; - } - - return Bytes[0] == 1; + return Bytes == null ? null : Bytes[0] == 1; } public UefiVariable? ReadUEFIVariable(Guid Guid, string Name, uint Size) { - byte[] Request = new byte[39 + (Name.Length + 1) * 2]; + byte[] Request = new byte[39 + ((Name.Length + 1) * 2)]; string Header = ReadParamSignature; // NOKXFR string Param = "GUFV"; @@ -469,7 +398,7 @@ namespace UnifiedFlashingPlatform public uint? ReadUEFIVariableSize(Guid Guid, string Name) { - byte[] Request = new byte[39 + (Name.Length + 1) * 2]; + byte[] Request = new byte[39 + ((Name.Length + 1) * 2)]; string Header = "NOKXFR"; // NOKXFR string Param = "GUVS"; @@ -491,29 +420,19 @@ namespace UnifiedFlashingPlatform byte[] Result = new byte[Response[0x10]]; Buffer.BlockCopy(Response, 0x11, Result, 0, Response[0x10]); - if (Result == null || Result.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Result.Reverse().ToArray()); + return Result == null || Result.Length != 4 ? null : BitConverter.ToUInt32(Result.Reverse().ToArray()); } // // Returns the largest memory region in bytes available for use by UFP // - public UInt64? ReadLargestMemoryRegion() + public ulong? ReadLargestMemoryRegion() { byte[] Bytes = ReadParam("LGMR"); - if (Bytes == null || Bytes.Length != 8) - { - return null; - } - - return BitConverter.ToUInt64(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 8 ? null : BitConverter.ToUInt64(Bytes.Reverse().ToArray()); } - public UInt64? ReadLogSize(DeviceLogType LogType) + public ulong? ReadLogSize(DeviceLogType LogType) { byte[] Request = new byte[0x10]; string Header = ReadParamSignature; // NOKXFR @@ -544,7 +463,7 @@ namespace UnifiedFlashingPlatform return ReadStringParam("MAC\0"); } - public UInt32? ReadModeData(Mode Mode) + public uint? ReadModeData(Mode Mode) { byte[] Request = new byte[0x10]; string Header = ReadParamSignature; // NOKXFR @@ -563,12 +482,7 @@ namespace UnifiedFlashingPlatform byte[] Result = new byte[Response[0x10]]; Buffer.BlockCopy(Response, 0x11, Result, 0, Response[0x10]); - if (Result == null || Result.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Result.Reverse().ToArray()); + return Result == null || Result.Length != 4 ? null : BitConverter.ToUInt32(Result.Reverse().ToArray()); } public string ReadProcessorManufacturer() @@ -579,15 +493,10 @@ namespace UnifiedFlashingPlatform // // Gets the SD Card Size in sectors, if present // - public UInt32? ReadSDCardSize() + public uint? ReadSDCardSize() { byte[] Bytes = ReadParam("SDS\0"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public string ReadSupportedFFUProtocolInfo() @@ -605,26 +514,16 @@ namespace UnifiedFlashingPlatform public Guid? ReadSerialNumber() { byte[] Bytes = ReadParam("SN\0\0"); - if (Bytes == null || Bytes.Length != 16) - { - return null; - } - - return new Guid(Bytes); + return Bytes == null || Bytes.Length != 16 ? null : new Guid(Bytes); } // // Returns the size of system memory in kB // - public UInt64? ReadSizeOfSystemMemory() + public ulong? ReadSizeOfSystemMemory() { byte[] Bytes = ReadParam("SOSM"); - if (Bytes == null || Bytes.Length != 8) - { - return null; - } - - return BitConverter.ToUInt64(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 8 ? null : BitConverter.ToUInt64(Bytes.Reverse().ToArray()); } public string ReadSecurityStatus() @@ -639,15 +538,10 @@ namespace UnifiedFlashingPlatform return ReadStringParam("TELS"); } - public UInt32? ReadTransferSize() + public uint? ReadTransferSize() { byte[] Bytes = ReadParam("TS\0\0"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } // @@ -682,27 +576,19 @@ namespace UnifiedFlashingPlatform public USBSpeed? ReadUSBSpeed() { byte[] Bytes = ReadParam("USBS"); - if (Bytes == null || Bytes.Length != 2) - { - return null; - } - - return new USBSpeed() - { - CurrentUSBSpeed = Bytes[0], - MaxUSBSpeed = Bytes[1] - }; + return Bytes == null || Bytes.Length != 2 + ? null + : new USBSpeed() + { + CurrentUSBSpeed = Bytes[0], + MaxUSBSpeed = Bytes[1] + }; } - public UInt32? ReadWriteBufferSize() + public uint? ReadWriteBufferSize() { byte[] Bytes = ReadParam("WBS\0"); - if (Bytes == null || Bytes.Length != 4) - { - return null; - } - - return BitConverter.ToUInt32(Bytes.Reverse().ToArray()); + return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public void Relock() @@ -710,7 +596,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[7]; string Header = RelockSignature; // NOKXFO Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void MassStorage() @@ -718,7 +604,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[7]; string Header = MassStorageSignature; // NOKM Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void RebootPhone() @@ -726,7 +612,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[7]; string Header = $"{SwitchModeSignature}R"; // NOKXCBR Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void SwitchToUFP() @@ -734,7 +620,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[7]; string Header = $"{SwitchModeSignature}U"; // NOKXCBU Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void ContinueBoot() @@ -742,7 +628,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[7]; string Header = $"{SwitchModeSignature}W"; // NOKXCBW Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void PowerOff() @@ -771,7 +657,7 @@ namespace UnifiedFlashingPlatform Buffer.BlockCopy(BitConverter.GetBytes(Row).Reverse().ToArray(), 0, Request, 6, 2); Buffer.BlockCopy(MessageBuffer, 0, Request, 8, MessageBuffer.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public void ClearScreen() @@ -779,7 +665,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[6]; string Header = ClearScreenSignature; // NOKXCC Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); + _ = ExecuteRawMethod(Request); } public byte[] Echo(byte[] DataPayload) diff --git a/WPInternalsException.cs b/WPInternalsException.cs index 188b68a..08187e4 100644 --- a/WPInternalsException.cs +++ b/WPInternalsException.cs @@ -25,7 +25,7 @@ namespace UnifiedFlashingPlatform internal class WPinternalsException : Exception { // Message and SubMessaage are always printable - internal string SubMessage = null; + internal string? SubMessage = null; internal WPinternalsException() : base() { }