BinInspector.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MV485.util
  9. {
  10. public class BinInspector
  11. {
  12. public static void Inspect2(string binFilePath)
  13. {
  14. Console.WriteLine($"检查文件: {binFilePath}");
  15. using (var fs = new FileStream(binFilePath, FileMode.Open, FileAccess.Read))
  16. using (var reader = new BinaryReader(fs))
  17. {
  18. // 1. 检查 Header
  19. var header = ImageHeader.FromBinaryReader(reader);
  20. if (header.Magic != 0x96f3b83d)
  21. {
  22. Console.WriteLine("❌ Header Magic 错误!");
  23. return;
  24. }
  25. Console.WriteLine($"✅ Header Magic 正常: 0x{header.Magic:X8}");
  26. Console.WriteLine($"版本: v{header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision} (Build {header.VersionBuildNum})");
  27. Console.WriteLine($"Header大小: 0x{header.HeaderSize:X} ({header.HeaderSize} 字节)");
  28. Console.WriteLine($"正文大小: {header.ImageSize} 字节");
  29. // 2. 读取 header + body
  30. fs.Position = 0;
  31. var fullHeader = reader.ReadBytes(header.HeaderSize);
  32. var body = reader.ReadBytes((int)header.ImageSize);
  33. // 3. 检查 TLV
  34. var tlvInfo = TlvInfo.FromBinaryReader(reader);
  35. if (tlvInfo.Magic != 0x6907)
  36. {
  37. Console.WriteLine("❌ TLV Magic 错误!");
  38. return;
  39. }
  40. Console.WriteLine($"✅ TLV Magic 正常: 0x{tlvInfo.Magic:X4}");
  41. Console.WriteLine($"TLV总长度: {tlvInfo.TotalLength} 字节");
  42. int tlvRemaining = tlvInfo.TotalLength - 4;
  43. Console.WriteLine("解析 TLV Entries:");
  44. byte[] sha256Value = null;
  45. while (tlvRemaining > 0)
  46. {
  47. var entry = TlvEntry.FromBinaryReader(reader);
  48. Console.WriteLine($"- Type: 0x{entry.Type:X2}, Length: {entry.Length} 字节");
  49. if (entry.Type == 0x10 && entry.Length == 32) // SHA256
  50. {
  51. sha256Value = entry.Value;
  52. }
  53. tlvRemaining -= 4 + entry.Length;
  54. }
  55. if (sha256Value == null)
  56. {
  57. Console.WriteLine("❌ 没有找到 SHA256 校验值!");
  58. return;
  59. }
  60. string expectedHash = BitConverter.ToString(sha256Value).Replace("-", "").ToLowerInvariant();
  61. // 4. 验证 SHA256
  62. using (var sha = SHA256.Create())
  63. {
  64. var actualHash = sha.ComputeHash(fullHeader.Concat(body).ToArray());
  65. string actualHashHex = BitConverter.ToString(actualHash).Replace("-", "").ToLowerInvariant();
  66. Console.WriteLine($"期望SHA256: {expectedHash}");
  67. Console.WriteLine($"实际SHA256: {actualHashHex}");
  68. if (expectedHash == actualHashHex)
  69. Console.WriteLine("✅ SHA256 校验成功!");
  70. else
  71. Console.WriteLine("❌ SHA256 校验失败!");
  72. }
  73. }
  74. }
  75. public static bool Inspect(string binFilePath)
  76. {
  77. Console.WriteLine($"检查文件: {binFilePath}");
  78. using (var fs = new FileStream(binFilePath, FileMode.Open, FileAccess.Read))
  79. using (var reader = new BinaryReader(fs))
  80. {
  81. try
  82. {
  83. // 1. 检查 Header
  84. var header = ImageHeader.FromBinaryReader(reader);
  85. if (header.Magic != 0x96f3b83d)
  86. {
  87. Console.WriteLine("❌ Header Magic 错误!");
  88. return false;
  89. }
  90. Console.WriteLine($"✅ Header Magic 正常: 0x{header.Magic:X8}");
  91. Console.WriteLine($"版本: v{header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision} (Build {header.VersionBuildNum})");
  92. Console.WriteLine($"Header大小: 0x{header.HeaderSize:X} ({header.HeaderSize} 字节)");
  93. Console.WriteLine($"正文大小: {header.ImageSize} 字节");
  94. // 2. 验证 文件名最后6位数字
  95. var fileName = Path.GetFileNameWithoutExtension(binFilePath);
  96. if (fileName.Length < 6)
  97. {
  98. Console.WriteLine("❌ 文件名太短,无法解析版本号!");
  99. return false;
  100. }
  101. string last6Digits = fileName.Substring(fileName.Length - 6); // 最后6位
  102. if (!int.TryParse(last6Digits, out _))
  103. {
  104. Console.WriteLine("❌ 文件名最后6位不是纯数字!");
  105. return false;
  106. }
  107. int major = int.Parse(last6Digits.Substring(0, 2));
  108. int minor = int.Parse(last6Digits.Substring(2, 2));
  109. int revision = int.Parse(last6Digits.Substring(4, 2));
  110. if (header.VersionMajor != major || header.VersionMinor != minor || header.VersionRevision != revision)
  111. {
  112. Console.WriteLine($"❌ 版本号不匹配!文件名版本: {major}.{minor}.{revision},Header版本: {header.VersionMajor}.{header.VersionMinor}.{header.VersionRevision}");
  113. return false;
  114. }
  115. Console.WriteLine($"✅ 文件名版本与Header版本一致: v{major}.{minor}.{revision}");
  116. // 3. 读取 header + body
  117. fs.Position = 0;
  118. var fullHeader = reader.ReadBytes(header.HeaderSize);
  119. var body = reader.ReadBytes((int)header.ImageSize);
  120. // 4. 检查 TLV
  121. var tlvInfo = TlvInfo.FromBinaryReader(reader);
  122. if (tlvInfo.Magic != 0x6907)
  123. {
  124. Console.WriteLine("❌ TLV Magic 错误!");
  125. return false;
  126. }
  127. Console.WriteLine($"✅ TLV Magic 正常: 0x{tlvInfo.Magic:X4}");
  128. Console.WriteLine($"TLV总长度: {tlvInfo.TotalLength} 字节");
  129. int tlvRemaining = tlvInfo.TotalLength - 4;
  130. Console.WriteLine("解析 TLV Entries:");
  131. byte[] sha256Value = null;
  132. while (tlvRemaining > 0)
  133. {
  134. var entry = TlvEntry.FromBinaryReader(reader);
  135. Console.WriteLine($"- Type: 0x{entry.Type:X2}, Length: {entry.Length} 字节");
  136. if (entry.Type == 0x10 && entry.Length == 32) // SHA256
  137. {
  138. sha256Value = entry.Value;
  139. }
  140. tlvRemaining -= 4 + entry.Length;
  141. }
  142. if (sha256Value == null)
  143. {
  144. Console.WriteLine("❌ 没有找到 SHA256 校验值!");
  145. return false;
  146. }
  147. string expectedHash = BitConverter.ToString(sha256Value).Replace("-", "").ToLowerInvariant();
  148. // 5. 验证 SHA256
  149. using (var sha = SHA256.Create())
  150. {
  151. var actualHash = sha.ComputeHash(fullHeader.Concat(body).ToArray());
  152. string actualHashHex = BitConverter.ToString(actualHash).Replace("-", "").ToLowerInvariant();
  153. Console.WriteLine($"期望SHA256: {expectedHash}");
  154. Console.WriteLine($"实际SHA256: {actualHashHex}");
  155. if (expectedHash != actualHashHex)
  156. {
  157. Console.WriteLine("❌ SHA256 校验失败!");
  158. return false;
  159. }
  160. Console.WriteLine("✅ SHA256 校验成功!");
  161. }
  162. Console.WriteLine("✅ 文件检查全部通过!");
  163. return true;
  164. }
  165. catch (Exception ex)
  166. {
  167. Console.WriteLine($"❌ 检查出错: {ex.Message}");
  168. return false;
  169. }
  170. }
  171. }
  172. }
  173. ///////////////////////////////////////
  174. }