|
@@ -0,0 +1,212 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Security.Cryptography;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace MV485.util
|
|
|
+{
|
|
|
+ public class BinInspector
|
|
|
+ {
|
|
|
+ public static void Inspect2(string binFilePath)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"检查文件: {binFilePath}");
|
|
|
+
|
|
|
+ using (var fs = new FileStream(binFilePath, FileMode.Open, FileAccess.Read))
|
|
|
+ using (var reader = new BinaryReader(fs))
|
|
|
+ {
|
|
|
+ // 1. 检查 Header
|
|
|
+ var header = ImageHeader.FromBinaryReader(reader);
|
|
|
+ if (header.Magic != 0x96f3b83d)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ Header Magic 错误!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine($"✅ Header Magic 正常: 0x{header.Magic:X8}");
|
|
|
+ Console.WriteLine($"版本: v{header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision} (Build {header.VersionBuildNum})");
|
|
|
+ Console.WriteLine($"Header大小: 0x{header.HeaderSize:X} ({header.HeaderSize} 字节)");
|
|
|
+ Console.WriteLine($"正文大小: {header.ImageSize} 字节");
|
|
|
+
|
|
|
+ // 2. 读取 header + body
|
|
|
+ fs.Position = 0;
|
|
|
+ var fullHeader = reader.ReadBytes(header.HeaderSize);
|
|
|
+ var body = reader.ReadBytes((int)header.ImageSize);
|
|
|
+
|
|
|
+ // 3. 检查 TLV
|
|
|
+ var tlvInfo = TlvInfo.FromBinaryReader(reader);
|
|
|
+ if (tlvInfo.Magic != 0x6907)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ TLV Magic 错误!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Console.WriteLine($"✅ TLV Magic 正常: 0x{tlvInfo.Magic:X4}");
|
|
|
+ Console.WriteLine($"TLV总长度: {tlvInfo.TotalLength} 字节");
|
|
|
+
|
|
|
+ int tlvRemaining = tlvInfo.TotalLength - 4;
|
|
|
+ Console.WriteLine("解析 TLV Entries:");
|
|
|
+ byte[] sha256Value = null;
|
|
|
+
|
|
|
+ while (tlvRemaining > 0)
|
|
|
+ {
|
|
|
+ var entry = TlvEntry.FromBinaryReader(reader);
|
|
|
+ Console.WriteLine($"- Type: 0x{entry.Type:X2}, Length: {entry.Length} 字节");
|
|
|
+
|
|
|
+ if (entry.Type == 0x10 && entry.Length == 32) // SHA256
|
|
|
+ {
|
|
|
+ sha256Value = entry.Value;
|
|
|
+ }
|
|
|
+
|
|
|
+ tlvRemaining -= 4 + entry.Length;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sha256Value == null)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ 没有找到 SHA256 校验值!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string expectedHash = BitConverter.ToString(sha256Value).Replace("-", "").ToLowerInvariant();
|
|
|
+
|
|
|
+ // 4. 验证 SHA256
|
|
|
+ using (var sha = SHA256.Create())
|
|
|
+ {
|
|
|
+ var actualHash = sha.ComputeHash(fullHeader.Concat(body).ToArray());
|
|
|
+ string actualHashHex = BitConverter.ToString(actualHash).Replace("-", "").ToLowerInvariant();
|
|
|
+
|
|
|
+ Console.WriteLine($"期望SHA256: {expectedHash}");
|
|
|
+ Console.WriteLine($"实际SHA256: {actualHashHex}");
|
|
|
+
|
|
|
+ if (expectedHash == actualHashHex)
|
|
|
+ Console.WriteLine("✅ SHA256 校验成功!");
|
|
|
+ else
|
|
|
+ Console.WriteLine("❌ SHA256 校验失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static bool Inspect(string binFilePath)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"检查文件: {binFilePath}");
|
|
|
+
|
|
|
+ using (var fs = new FileStream(binFilePath, FileMode.Open, FileAccess.Read))
|
|
|
+ using (var reader = new BinaryReader(fs))
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // 1. 检查 Header
|
|
|
+ var header = ImageHeader.FromBinaryReader(reader);
|
|
|
+ if (header.Magic != 0x96f3b83d)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ Header Magic 错误!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine($"✅ Header Magic 正常: 0x{header.Magic:X8}");
|
|
|
+ Console.WriteLine($"版本: v{header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision} (Build {header.VersionBuildNum})");
|
|
|
+ Console.WriteLine($"Header大小: 0x{header.HeaderSize:X} ({header.HeaderSize} 字节)");
|
|
|
+ Console.WriteLine($"正文大小: {header.ImageSize} 字节");
|
|
|
+
|
|
|
+ // 2. 验证 文件名最后6位数字
|
|
|
+ var fileName = Path.GetFileNameWithoutExtension(binFilePath);
|
|
|
+ if (fileName.Length < 6)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ 文件名太短,无法解析版本号!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ string last6Digits = fileName.Substring(fileName.Length - 6); // 最后6位
|
|
|
+ if (!int.TryParse(last6Digits, out _))
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ 文件名最后6位不是纯数字!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ int major = int.Parse(last6Digits.Substring(0, 2));
|
|
|
+ int minor = int.Parse(last6Digits.Substring(2, 2));
|
|
|
+ int revision = int.Parse(last6Digits.Substring(4, 2));
|
|
|
+
|
|
|
+ if (header.VersionMajor != major || header.VersionMinor != minor || header.VersionRevision != revision)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"❌ 版本号不匹配!文件名版本: {major}.{minor}.{revision},Header版本: {header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine($"✅ 文件名版本与Header版本一致: v{major}.{minor}.{revision}");
|
|
|
+
|
|
|
+ // 3. 读取 header + body
|
|
|
+ fs.Position = 0;
|
|
|
+ var fullHeader = reader.ReadBytes(header.HeaderSize);
|
|
|
+ var body = reader.ReadBytes((int)header.ImageSize);
|
|
|
+
|
|
|
+ // 4. 检查 TLV
|
|
|
+ var tlvInfo = TlvInfo.FromBinaryReader(reader);
|
|
|
+ if (tlvInfo.Magic != 0x6907)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ TLV Magic 错误!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Console.WriteLine($"✅ TLV Magic 正常: 0x{tlvInfo.Magic:X4}");
|
|
|
+ Console.WriteLine($"TLV总长度: {tlvInfo.TotalLength} 字节");
|
|
|
+
|
|
|
+ int tlvRemaining = tlvInfo.TotalLength - 4;
|
|
|
+ Console.WriteLine("解析 TLV Entries:");
|
|
|
+ byte[] sha256Value = null;
|
|
|
+
|
|
|
+ while (tlvRemaining > 0)
|
|
|
+ {
|
|
|
+ var entry = TlvEntry.FromBinaryReader(reader);
|
|
|
+ Console.WriteLine($"- Type: 0x{entry.Type:X2}, Length: {entry.Length} 字节");
|
|
|
+
|
|
|
+ if (entry.Type == 0x10 && entry.Length == 32) // SHA256
|
|
|
+ {
|
|
|
+ sha256Value = entry.Value;
|
|
|
+ }
|
|
|
+
|
|
|
+ tlvRemaining -= 4 + entry.Length;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sha256Value == null)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ 没有找到 SHA256 校验值!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ string expectedHash = BitConverter.ToString(sha256Value).Replace("-", "").ToLowerInvariant();
|
|
|
+
|
|
|
+ // 5. 验证 SHA256
|
|
|
+ using (var sha = SHA256.Create())
|
|
|
+ {
|
|
|
+ var actualHash = sha.ComputeHash(fullHeader.Concat(body).ToArray());
|
|
|
+ string actualHashHex = BitConverter.ToString(actualHash).Replace("-", "").ToLowerInvariant();
|
|
|
+
|
|
|
+ Console.WriteLine($"期望SHA256: {expectedHash}");
|
|
|
+ Console.WriteLine($"实际SHA256: {actualHashHex}");
|
|
|
+
|
|
|
+ if (expectedHash != actualHashHex)
|
|
|
+ {
|
|
|
+ Console.WriteLine("❌ SHA256 校验失败!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine("✅ SHA256 校验成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine("✅ 文件检查全部通过!");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"❌ 检查出错: {ex.Message}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ ///////////////////////////////////////
|
|
|
+}
|