Analyze binary structure based on platform. Args: bin_file (Path): Path to binary file platform (str): Target platform Returns: BinaryAnalysis: Binary analysis information
(bin_file: Path, platform: str)
| 83 | |
| 84 | |
| 85 | def _analyze_binary_structure(bin_file: Path, platform: str) -> BinaryAnalysis: |
| 86 | """ |
| 87 | Analyze binary structure based on platform. |
| 88 | |
| 89 | Args: |
| 90 | bin_file (Path): Path to binary file |
| 91 | platform (str): Target platform |
| 92 | |
| 93 | Returns: |
| 94 | BinaryAnalysis: Binary analysis information |
| 95 | """ |
| 96 | print(f"Analyzing {platform} binary: {bin_file}") |
| 97 | |
| 98 | if not bin_file.exists(): |
| 99 | raise RuntimeError(f"Binary file not found: {bin_file}") |
| 100 | |
| 101 | with open(bin_file, "rb") as f: |
| 102 | data = f.read() |
| 103 | |
| 104 | analysis = BinaryAnalysis( |
| 105 | platform=platform, |
| 106 | size=len(data), |
| 107 | header=data[:32] if len(data) >= 32 else data, |
| 108 | ) |
| 109 | |
| 110 | if platform == "esp32": |
| 111 | # ESP32 binary format analysis |
| 112 | print(f"ESP32 binary size: {len(data)} bytes") |
| 113 | if len(data) >= 32: |
| 114 | # ESP32 image header |
| 115 | magic = data[0] |
| 116 | segments = data[1] |
| 117 | flash_mode = data[2] |
| 118 | flash_size_freq = data[3] |
| 119 | analysis.esp32 = Esp32Header( |
| 120 | magic=hex(magic), |
| 121 | segments=segments, |
| 122 | flash_mode=flash_mode, |
| 123 | flash_size_freq=flash_size_freq, |
| 124 | ) |
| 125 | print(f"ESP32 image - Magic: {hex(magic)}, Segments: {segments}") |
| 126 | |
| 127 | elif platform == "avr": |
| 128 | # AVR/Arduino binary format (Intel HEX) |
| 129 | print(f"AVR binary size: {len(data)} bytes") |
| 130 | if bin_file.suffix.lower() == ".hex": |
| 131 | print("Intel HEX format detected") |
| 132 | analysis.format = "intel_hex" |
| 133 | else: |
| 134 | analysis.format = "binary" |
| 135 | |
| 136 | # Print hex dump of first 64 bytes for analysis |
| 137 | print(f"\nFirst 64 bytes of {platform} binary (hex):") |
| 138 | for i in range(0, min(64, len(data)), 16): |
| 139 | hex_bytes = " ".join(f"{b:02x}" for b in data[i : i + 16]) |
| 140 | ascii_chars = "".join( |
| 141 | chr(b) if 32 <= b < 127 else "." for b in data[i : i + 16] |
| 142 | ) |
no test coverage detected