private FwPacket ProcessNextFwPacket(BinaryReaderEndian packetReader) { FwPacket packet = new FwPacket(); try { // Seek to next packet start while (true) { if (packetReader.ReadByte() == 0x00) { byte tableId = packetReader.ReadByte(); if (tableId == 0xB5 || tableId == 0xB6) { packetReader.BaseStream.Seek(-2, SeekOrigin.Current); break; } else { packetReader.BaseStream.Seek(-1, SeekOrigin.Current); } } } long startPos = packetReader.BaseStream.Position; // Skip over table id packetReader.ReadInt16(); packet.Length = (ushort)(packetReader.ReadUInt16() & 0x0FFF); packet.SectionType = packetReader.ReadUInt16(); packet.Unknown = packetReader.ReadUInt32(); packet.VendorId = packetReader.ReadByte(); packetReader.ReadByte(); packet.ModelId = packetReader.ReadByte(); packetReader.BaseStream.Seek(3, SeekOrigin.Current); packet.FirmwareVersion = packetReader.ReadByte(); packet.FileOffset = packetReader.ReadUInt32(); packet.FileLength = packetReader.ReadUInt32(); packet.Data = packetReader.ReadBytes(packet.Length - 25); packet.Crc32 = packetReader.ReadUInt32(); packetReader.BaseStream.Seek(startPos + 1, SeekOrigin.Begin); byte[] wholePacket = packetReader.ReadBytes(packet.Length + 3); packet.hasPassedCRC = (Crc32.crc32_mpeg(wholePacket, (uint)wholePacket.Length) == 0); } catch (EndOfStreamException ex) { throw ex; } catch (Exception) { packet.hasPassedCRC = false; } return(packet); }
public bool AddPacket(FwPacket firmwarePacket) { if (VendorId == firmwarePacket.VendorId && ModelId == firmwarePacket.ModelId && firmwarePacket.hasPassedCRC && firmwarePacket.SectionType == 1 && !isComplete) { if (firmwarePacket.Data.Length != BlockSize) { //Console.WriteLine("Blocksize differs! At offset: 0x{0:X2}", firmwarePacket.FileOffset); } if (!CompletedOffsets.Contains(firmwarePacket.FileOffset)) { FirmwareStream.Seek(firmwarePacket.FileOffset, SeekOrigin.Begin); FirmwareStream.Write(firmwarePacket.Data, 0, firmwarePacket.Data.Length); BytesCompleted += (uint)firmwarePacket.Data.Length; CompletedOffsets.Add(firmwarePacket.FileOffset); if (BytesCompleted >= FileSize) { FirmwareStream.Close(); DirectoryInfo fwDir = Directory.CreateDirectory(Path.Combine(SaveDirectory, BaseName)); File.Move(Path.Combine(SaveDirectory, string.Format("{0}.tmp", BaseName)), Path.Combine(fwDir.FullName, FileName)); isComplete = true; FwCreateDate = DateTime.Now; File.WriteAllText(Path.Combine(fwDir.FullName, "date_stamp.txt"), FwCreateDate.Ticks.ToString()); Extractors.IFirmwareExtractor currentExtractor; if ((currentExtractor = Extractor) != null) { try { currentExtractor.ExtractParts(Path.Combine(fwDir.FullName, FileName)); GetVersion(); } catch (Exception ex) { Logger.AddLogItem("Error extracting firmware!", "FirmwareFile", LogLevels.Warning, ex); } } } } return(true); } else { return(false); } }
public FirmwareFile(FwPacket firmwarePacket, string saveDirectory) { VendorId = firmwarePacket.VendorId; ModelId = firmwarePacket.ModelId; FileSize = firmwarePacket.FileLength; BlockSize = (uint)firmwarePacket.Data.Length; VersionId = firmwarePacket.FirmwareVersion; BytesCompleted = 0; isComplete = false; SaveDirectory = saveDirectory; CompletedOffsets = new HashSet<uint>(); FirmwareStream = File.Create(Path.Combine(SaveDirectory, string.Format("{0}.tmp", BaseName))); SetExtractorList(); AddPacket(firmwarePacket); }
public FirmwareFile(FwPacket firmwarePacket, string saveDirectory) { VendorId = firmwarePacket.VendorId; ModelId = firmwarePacket.ModelId; FileSize = firmwarePacket.FileLength; BlockSize = (uint)firmwarePacket.Data.Length; VersionId = firmwarePacket.FirmwareVersion; BytesCompleted = 0; isComplete = false; SaveDirectory = saveDirectory; CompletedOffsets = new HashSet <uint>(); FirmwareStream = File.Create(Path.Combine(SaveDirectory, string.Format("{0}.tmp", BaseName))); SetExtractorList(); AddPacket(firmwarePacket); }
public bool AddPacket(FwPacket firmwarePacket) { if (VendorId == firmwarePacket.VendorId && ModelId == firmwarePacket.ModelId && firmwarePacket.hasPassedCRC && firmwarePacket.SectionType == 1 && !isComplete) { if (firmwarePacket.Data.Length != BlockSize) { //Console.WriteLine("Blocksize differs! At offset: 0x{0:X2}", firmwarePacket.FileOffset); } if (!CompletedOffsets.Contains(firmwarePacket.FileOffset)) { FirmwareStream.Seek(firmwarePacket.FileOffset, SeekOrigin.Begin); FirmwareStream.Write(firmwarePacket.Data, 0, firmwarePacket.Data.Length); BytesCompleted += (uint)firmwarePacket.Data.Length; CompletedOffsets.Add(firmwarePacket.FileOffset); if (BytesCompleted >= FileSize) { FirmwareStream.Close(); DirectoryInfo fwDir = Directory.CreateDirectory(Path.Combine(SaveDirectory, BaseName)); File.Move(Path.Combine(SaveDirectory, string.Format("{0}.tmp", BaseName)), Path.Combine(fwDir.FullName, FileName)); isComplete = true; FwCreateDate = DateTime.Now; File.WriteAllText(Path.Combine(fwDir.FullName, "date_stamp.txt"), FwCreateDate.Ticks.ToString()); Extractors.IFirmwareExtractor currentExtractor; if ((currentExtractor = Extractor) != null) { try { currentExtractor.ExtractParts(Path.Combine(fwDir.FullName, FileName)); GetVersion(); } catch (Exception ex) { Logger.AddLogItem("Error extracting firmware!", "FirmwareFile", LogLevels.Warning, ex); } } } } return true; } else { return false; } }
private FwPacket ProcessNextFwPacket(BinaryReaderEndian packetReader) { FwPacket packet = new FwPacket(); try { // Seek to next packet start while (true) { if (packetReader.ReadByte() == 0x00) { byte tableId = packetReader.ReadByte(); if (tableId == 0xB5 || tableId == 0xB6) { packetReader.BaseStream.Seek(-2, SeekOrigin.Current); break; } else { packetReader.BaseStream.Seek(-1, SeekOrigin.Current); } } } long startPos = packetReader.BaseStream.Position; // Skip over table id packetReader.ReadInt16(); packet.Length = (ushort)(packetReader.ReadUInt16() & 0x0FFF); packet.SectionType = packetReader.ReadUInt16(); packet.Unknown = packetReader.ReadUInt32(); packet.VendorId = packetReader.ReadByte(); packetReader.ReadByte(); packet.ModelId = packetReader.ReadByte(); packetReader.BaseStream.Seek(3, SeekOrigin.Current); packet.FirmwareVersion = packetReader.ReadByte(); packet.FileOffset = packetReader.ReadUInt32(); packet.FileLength = packetReader.ReadUInt32(); packet.Data = packetReader.ReadBytes(packet.Length - 25); packet.Crc32 = packetReader.ReadUInt32(); packetReader.BaseStream.Seek(startPos + 1, SeekOrigin.Begin); byte[] wholePacket = packetReader.ReadBytes(packet.Length + 3); packet.hasPassedCRC = (Crc32.crc32_mpeg(wholePacket, (uint)wholePacket.Length) == 0); } catch (EndOfStreamException ex) { throw ex; } catch (Exception) { packet.hasPassedCRC = false; } return packet; }